msp432e4_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file msp432e4_crypto_hash.c
3  * @brief MSP432E4 hash hardware accelerator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include <stdint.h>
36 #include "msp432.h"
37 #include "inc/hw_shamd5.h"
38 #include "driverlib/types.h"
39 #include "core/crypto.h"
42 #include "hash/hash_algorithms.h"
43 #include "debug.h"
44 
45 //Check crypto library configuration
46 #if (MSP432E4_CRYPTO_HASH_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief Reset SHA/MD5 module
51  **/
52 
53 void hashReset(void)
54 {
55  uint32_t temp;
56 
57  //Perform software reset
58  SHAMD5->SYSCONFIG |= SHAMD5_SYSCONFIG_SOFTRESET;
59 
60  //Wait for the reset to complete
61  while((SHAMD5->SYSSTATUS & SHAMD5_SYSSTATUS_RESETDONE) == 0)
62  {
63  }
64 
65  //Force idle mode
66  temp = SHAMD5->SYSCONFIG & ~SHAMD5_SYSCONFIG_SIDLE_M;
67  SHAMD5->SYSCONFIG = temp | SHAMD5_SYSCONFIG_SIDLE_FORCE;
68 }
69 
70 
71 /**
72  * @brief Update hash value
73  * @param[in] data Pointer to the input buffer
74  * @param[in] length Length of the input buffer
75  * @param[out] digest Resulting digest value
76  * @param[in] digestSize Size of the digest, in bytes
77  **/
78 
79 void hashProcessData(const uint8_t *data, size_t length, uint8_t *digest,
80  size_t digestSize)
81 {
82  size_t i;
83  uint32_t temp;
84 
85  //Specify the length
86  SHAMD5->LENGTH = length;
87 
88  //Digest input data
89  while(length >= 64)
90  {
91  //Wait for the SHA/MD5 engine to be ready to accept data
92  while((SHAMD5->IRQSTATUS & SHAMD5_IRQSTATUS_INPUT_READY) == 0)
93  {
94  }
95 
96  //Write 64-byte block
97  SHAMD5->DATA_0_IN = LOAD32LE(data);
98  SHAMD5->DATA_1_IN = LOAD32LE(data + 4);
99  SHAMD5->DATA_2_IN = LOAD32LE(data + 8);
100  SHAMD5->DATA_3_IN = LOAD32LE(data + 12);
101  SHAMD5->DATA_4_IN = LOAD32LE(data + 16);
102  SHAMD5->DATA_5_IN = LOAD32LE(data + 20);
103  SHAMD5->DATA_6_IN = LOAD32LE(data + 24);
104  SHAMD5->DATA_7_IN = LOAD32LE(data + 28);
105  SHAMD5->DATA_8_IN = LOAD32LE(data + 32);
106  SHAMD5->DATA_9_IN = LOAD32LE(data + 36);
107  SHAMD5->DATA_10_IN = LOAD32LE(data + 40);
108  SHAMD5->DATA_11_IN = LOAD32LE(data + 44);
109  SHAMD5->DATA_12_IN = LOAD32LE(data + 48);
110  SHAMD5->DATA_13_IN = LOAD32LE(data + 52);
111  SHAMD5->DATA_14_IN = LOAD32LE(data + 56);
112  SHAMD5->DATA_15_IN = LOAD32LE(data + 60);
113 
114  //Advance data pointer
115  data += 64;
116  length -= 64;
117  }
118 
119  //Process final block
120  if(length > 0)
121  {
122  //Wait for the SHA/MD5 engine to be ready to accept data
123  while((SHAMD5->IRQSTATUS & SHAMD5_IRQSTATUS_INPUT_READY) == 0)
124  {
125  }
126 
127  //Write final block
128  for(i = 0; i < length; i += 4)
129  {
130  //Write 32-bit word
131  HWREG(SHAMD5_BASE + SHAMD5_O_DATA_0_IN + i) = LOAD32LE(data);
132  //Advance data pointer
133  data += 4;
134  }
135  }
136 
137  //Wait for the output to be ready
138  while((SHAMD5->IRQSTATUS & SHAMD5_IRQSTATUS_OUTPUT_READY) == 0)
139  {
140  }
141 
142  //Read the resulting output value
143  for(i = 0; i < digestSize; i += 4)
144  {
145  temp = HWREG(SHAMD5_BASE + SHAMD5_O_IDIGEST_A + i);
146  STORE32LE(temp, digest + i);
147  }
148 }
149 
150 
151 #if (MD5_SUPPORT == ENABLED)
152 
153 /**
154  * @brief Digest a message using MD5
155  * @param[in] data Pointer to the message being hashed
156  * @param[in] length Length of the message
157  * @param[out] digest Pointer to the calculated digest
158  * @return Error code
159  **/
160 
161 error_t md5Compute(const void *data, size_t length, uint8_t *digest)
162 {
163  //Acquire exclusive access to the SHA/MD5 module
165 
166  //Reset the SHA/MD5 module before use
167  hashReset();
168 
169  //Configure the SHA/MD5 module
170  SHAMD5->MODE = SHAMD5_MODE_ALGO_MD5 | SHAMD5_MODE_ALGO_CONSTANT |
171  SHAMD5_MODE_CLOSE_HASH;
172 
173  //Digest the message
175 
176  //Release exclusive access to the SHA/MD5 module
178 
179  //Sucessful processing
180  return NO_ERROR;
181 }
182 
183 
184 /**
185  * @brief Initialize MD5 message digest context
186  * @param[in] context Pointer to the MD5 context to initialize
187  **/
188 
189 void md5Init(Md5Context *context)
190 {
191  //Set initial hash value
192  context->h[0] = 0x67452301;
193  context->h[1] = 0xEFCDAB89;
194  context->h[2] = 0x98BADCFE;
195  context->h[3] = 0x10325476;
196 
197  //Number of bytes in the buffer
198  context->size = 0;
199  //Total length of the message
200  context->totalSize = 0;
201 }
202 
203 
204 /**
205  * @brief Update the MD5 context with a portion of the message being hashed
206  * @param[in] context Pointer to the MD5 context
207  * @param[in] data Pointer to the buffer being hashed
208  * @param[in] length Length of the buffer
209  **/
210 
211 void md5Update(Md5Context *context, const void *data, size_t length)
212 {
213  size_t n;
214 
215  //Acquire exclusive access to the SHA/MD5 module
217 
218  //Reset the SHA/MD5 module before use
219  hashReset();
220 
221  //Configure the SHA/MD5 module
222  SHAMD5->MODE = SHAMD5_MODE_ALGO_MD5;
223 
224  //Restore hash context
225  SHAMD5->IDIGEST_A = context->h[0];
226  SHAMD5->IDIGEST_B = context->h[1];
227  SHAMD5->IDIGEST_C = context->h[2];
228  SHAMD5->IDIGEST_D = context->h[3];
229 
230  //Restore the value of the SHA_DIGEST_COUNT register
231  SHAMD5->DIGEST_COUNT = context->totalSize;
232 
233  //Process the incoming data
234  while(length > 0)
235  {
236  //Check whether some data is pending in the buffer
237  if(context->size == 0 && length >= 64)
238  {
239  //The length must be a multiple of 64 bytes
240  n = length - (length % 64);
241 
242  //Update hash value
244 
245  //Advance the data pointer
246  data = (uint8_t *) data + n;
247  //Remaining bytes to process
248  length -= n;
249  }
250  else
251  {
252  //The buffer can hold at most 64 bytes
253  n = MIN(length, 64 - context->size);
254 
255  //Copy the data to the buffer
256  osMemcpy(context->buffer + context->size, data, n);
257 
258  //Update the MD5 context
259  context->size += n;
260  //Advance the data pointer
261  data = (uint8_t *) data + n;
262  //Remaining bytes to process
263  length -= n;
264 
265  //Check whether the buffer is full
266  if(context->size == 64)
267  {
268  //Update hash value
269  hashProcessData(context->buffer, context->size, context->digest,
271 
272  //Empty the buffer
273  context->size = 0;
274  }
275  }
276  }
277 
278  //Save the value of the SHA_DIGEST_COUNT register
279  context->totalSize = SHAMD5->DIGEST_COUNT;
280 
281  //Release exclusive access to the SHA/MD5 module
283 }
284 
285 
286 /**
287  * @brief Finish the MD5 message digest
288  * @param[in] context Pointer to the MD5 context
289  * @param[out] digest Calculated digest (optional parameter)
290  **/
291 
292 void md5Final(Md5Context *context, uint8_t *digest)
293 {
294  //Acquire exclusive access to the SHA/MD5 module
296 
297  //Reset the SHA/MD5 module before use
298  hashReset();
299 
300  //Configure the SHA/MD5 module
301  SHAMD5->MODE = SHAMD5_MODE_ALGO_MD5 | SHAMD5_MODE_CLOSE_HASH;
302 
303  //Restore hash context
304  SHAMD5->IDIGEST_A = context->h[0];
305  SHAMD5->IDIGEST_B = context->h[1];
306  SHAMD5->IDIGEST_C = context->h[2];
307  SHAMD5->IDIGEST_D = context->h[3];
308 
309  //Restore the value of the SHA_DIGEST_COUNT register
310  SHAMD5->DIGEST_COUNT = context->totalSize;
311 
312  //Finish digest calculation
313  hashProcessData(context->buffer, context->size, context->digest,
315 
316  //Release exclusive access to the SHA/MD5 module
318 
319  //Copy the resulting digest
320  if(digest != NULL)
321  {
322  osMemcpy(digest, context->digest, MD5_DIGEST_SIZE);
323  }
324 }
325 
326 
327 /**
328  * @brief Finish the MD5 message digest (no padding added)
329  * @param[in] context Pointer to the MD5 context
330  * @param[out] digest Calculated digest
331  **/
332 
333 void md5FinalRaw(Md5Context *context, uint8_t *digest)
334 {
335  //Copy the resulting digest
336  osMemcpy(digest, context->digest, MD5_DIGEST_SIZE);
337 }
338 
339 #endif
340 #if (SHA1_SUPPORT == ENABLED)
341 
342 /**
343  * @brief Digest a message using SHA-1
344  * @param[in] data Pointer to the message being hashed
345  * @param[in] length Length of the message
346  * @param[out] digest Pointer to the calculated digest
347  * @return Error code
348  **/
349 
350 error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
351 {
352  //Acquire exclusive access to the SHA/MD5 module
354 
355  //Reset the SHA/MD5 module before use
356  hashReset();
357 
358  //Configure the SHA/MD5 module
359  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA1 | SHAMD5_MODE_ALGO_CONSTANT |
360  SHAMD5_MODE_CLOSE_HASH;
361 
362  //Digest the message
364 
365  //Release exclusive access to the SHA/MD5 module
367 
368  //Sucessful processing
369  return NO_ERROR;
370 }
371 
372 
373 /**
374  * @brief Initialize SHA-1 message digest context
375  * @param[in] context Pointer to the SHA-1 context to initialize
376  **/
377 
378 void sha1Init(Sha1Context *context)
379 {
380  //Set initial hash value
381  context->h[0] = BETOH32(0x67452301);
382  context->h[1] = BETOH32(0xEFCDAB89);
383  context->h[2] = BETOH32(0x98BADCFE);
384  context->h[3] = BETOH32(0x10325476);
385  context->h[4] = BETOH32(0xC3D2E1F0);
386 
387  //Number of bytes in the buffer
388  context->size = 0;
389  //Total length of the message
390  context->totalSize = 0;
391 }
392 
393 
394 /**
395  * @brief Update the SHA-1 context with a portion of the message being hashed
396  * @param[in] context Pointer to the SHA-1 context
397  * @param[in] data Pointer to the buffer being hashed
398  * @param[in] length Length of the buffer
399  **/
400 
401 void sha1Update(Sha1Context *context, const void *data, size_t length)
402 {
403  size_t n;
404 
405  //Acquire exclusive access to the SHA/MD5 module
407 
408  //Reset the SHA/MD5 module before use
409  hashReset();
410 
411  //Configure the SHA/MD5 module
412  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA1;
413 
414  //Restore hash context
415  SHAMD5->IDIGEST_A = context->h[0];
416  SHAMD5->IDIGEST_B = context->h[1];
417  SHAMD5->IDIGEST_C = context->h[2];
418  SHAMD5->IDIGEST_D = context->h[3];
419  SHAMD5->IDIGEST_E = context->h[4];
420 
421  //Restore the value of the SHA_DIGEST_COUNT register
422  SHAMD5->DIGEST_COUNT = context->totalSize;
423 
424  //Process the incoming data
425  while(length > 0)
426  {
427  //Check whether some data is pending in the buffer
428  if(context->size == 0 && length >= 64)
429  {
430  //The length must be a multiple of 64 bytes
431  n = length - (length % 64);
432 
433  //Update hash value
435 
436  //Advance the data pointer
437  data = (uint8_t *) data + n;
438  //Remaining bytes to process
439  length -= n;
440  }
441  else
442  {
443  //The buffer can hold at most 64 bytes
444  n = MIN(length, 64 - context->size);
445 
446  //Copy the data to the buffer
447  osMemcpy(context->buffer + context->size, data, n);
448 
449  //Update the SHA-1 context
450  context->size += n;
451  //Advance the data pointer
452  data = (uint8_t *) data + n;
453  //Remaining bytes to process
454  length -= n;
455 
456  //Check whether the buffer is full
457  if(context->size == 64)
458  {
459  //Update hash value
460  hashProcessData(context->buffer, context->size, context->digest,
462 
463  //Empty the buffer
464  context->size = 0;
465  }
466  }
467  }
468 
469  //Save the value of the SHA_DIGEST_COUNT register
470  context->totalSize = SHAMD5->DIGEST_COUNT;
471 
472  //Release exclusive access to the SHA/MD5 module
474 }
475 
476 
477 /**
478  * @brief Finish the SHA-1 message digest
479  * @param[in] context Pointer to the SHA-1 context
480  * @param[out] digest Calculated digest (optional parameter)
481  **/
482 
483 void sha1Final(Sha1Context *context, uint8_t *digest)
484 {
485  //Acquire exclusive access to the SHA/MD5 module
487 
488  //Reset the SHA/MD5 module before use
489  hashReset();
490 
491  //Configure the SHA/MD5 module
492  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA1 | SHAMD5_MODE_CLOSE_HASH;
493 
494  //Restore hash context
495  SHAMD5->IDIGEST_A = context->h[0];
496  SHAMD5->IDIGEST_B = context->h[1];
497  SHAMD5->IDIGEST_C = context->h[2];
498  SHAMD5->IDIGEST_D = context->h[3];
499  SHAMD5->IDIGEST_E = context->h[4];
500 
501  //Restore the value of the SHA_DIGEST_COUNT register
502  SHAMD5->DIGEST_COUNT = context->totalSize;
503 
504  //Finish digest calculation
505  hashProcessData(context->buffer, context->size, context->digest,
507 
508  //Release exclusive access to the SHA/MD5 module
510 
511  //Copy the resulting digest
512  if(digest != NULL)
513  {
514  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
515  }
516 }
517 
518 
519 /**
520  * @brief Finish the SHA-1 message digest (no padding added)
521  * @param[in] context Pointer to the SHA-1 context
522  * @param[out] digest Calculated digest
523  **/
524 
525 void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
526 {
527  //Copy the resulting digest
528  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
529 }
530 
531 #endif
532 #if (SHA224_SUPPORT == ENABLED)
533 
534 /**
535  * @brief Digest a message using SHA-224
536  * @param[in] data Pointer to the message being hashed
537  * @param[in] length Length of the message
538  * @param[out] digest Pointer to the calculated digest
539  * @return Error code
540  **/
541 
542 error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
543 {
544  //Acquire exclusive access to the SHA/MD5 module
546 
547  //Reset the SHA/MD5 module before use
548  hashReset();
549 
550  //Configure the SHA/MD5 module
551  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA224 | SHAMD5_MODE_ALGO_CONSTANT |
552  SHAMD5_MODE_CLOSE_HASH;
553 
554  //Digest the message
556 
557  //Release exclusive access to the SHA/MD5 module
559 
560  //Sucessful processing
561  return NO_ERROR;
562 }
563 
564 
565 /**
566  * @brief Initialize SHA-224 message digest context
567  * @param[in] context Pointer to the SHA-224 context to initialize
568  **/
569 
570 void sha224Init(Sha224Context *context)
571 {
572  //Set initial hash value
573  context->h[0] = BETOH32(0xC1059ED8);
574  context->h[1] = BETOH32(0x367CD507);
575  context->h[2] = BETOH32(0x3070DD17);
576  context->h[3] = BETOH32(0xF70E5939);
577  context->h[4] = BETOH32(0xFFC00B31);
578  context->h[5] = BETOH32(0x68581511);
579  context->h[6] = BETOH32(0x64F98FA7);
580  context->h[7] = BETOH32(0xBEFA4FA4);
581 
582  //Number of bytes in the buffer
583  context->size = 0;
584  //Total length of the message
585  context->totalSize = 0;
586 }
587 
588 
589 /**
590  * @brief Finish the SHA-224 message digest
591  * @param[in] context Pointer to the SHA-224 context
592  * @param[out] digest Calculated digest (optional parameter)
593  **/
594 
595 void sha224Final(Sha224Context *context, uint8_t *digest)
596 {
597  //Acquire exclusive access to the SHA/MD5 module
599 
600  //Reset the SHA/MD5 module before use
601  hashReset();
602 
603  //Configure the SHA/MD5 module
604  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA224 | SHAMD5_MODE_CLOSE_HASH;
605 
606  //Restore hash context
607  SHAMD5->IDIGEST_A = context->h[0];
608  SHAMD5->IDIGEST_B = context->h[1];
609  SHAMD5->IDIGEST_C = context->h[2];
610  SHAMD5->IDIGEST_D = context->h[3];
611  SHAMD5->IDIGEST_E = context->h[4];
612  SHAMD5->IDIGEST_F = context->h[5];
613  SHAMD5->IDIGEST_G = context->h[6];
614  SHAMD5->IDIGEST_H = context->h[7];
615 
616  //Restore the value of the SHA_DIGEST_COUNT register
617  SHAMD5->DIGEST_COUNT = context->totalSize;
618 
619  //Finish digest calculation
620  hashProcessData(context->buffer, context->size, context->digest,
622 
623  //Release exclusive access to the SHA/MD5 module
625 
626  //Copy the resulting digest
627  if(digest != NULL)
628  {
629  osMemcpy(digest, context->digest, SHA224_DIGEST_SIZE);
630  }
631 }
632 
633 #endif
634 #if (SHA256_SUPPORT == ENABLED)
635 
636 /**
637  * @brief Digest a message using SHA-256
638  * @param[in] data Pointer to the message being hashed
639  * @param[in] length Length of the message
640  * @param[out] digest Pointer to the calculated digest
641  * @return Error code
642  **/
643 
644 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
645 {
646  //Acquire exclusive access to the SHA/MD5 module
648 
649  //Reset the SHA/MD5 module before use
650  hashReset();
651 
652  //Configure the SHA/MD5 module
653  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA256 | SHAMD5_MODE_ALGO_CONSTANT |
654  SHAMD5_MODE_CLOSE_HASH;
655 
656  //Digest the message
658 
659  //Release exclusive access to the SHA/MD5 module
661 
662  //Sucessful processing
663  return NO_ERROR;
664 }
665 
666 
667 /**
668  * @brief Initialize SHA-256 message digest context
669  * @param[in] context Pointer to the SHA-256 context to initialize
670  **/
671 
672 void sha256Init(Sha256Context *context)
673 {
674  //Set initial hash value
675  context->h[0] = BETOH32(0x6A09E667);
676  context->h[1] = BETOH32(0xBB67AE85);
677  context->h[2] = BETOH32(0x3C6EF372);
678  context->h[3] = BETOH32(0xA54FF53A);
679  context->h[4] = BETOH32(0x510E527F);
680  context->h[5] = BETOH32(0x9B05688C);
681  context->h[6] = BETOH32(0x1F83D9AB);
682  context->h[7] = BETOH32(0x5BE0CD19);
683 
684  //Number of bytes in the buffer
685  context->size = 0;
686  //Total length of the message
687  context->totalSize = 0;
688 }
689 
690 
691 /**
692  * @brief Update the SHA-256 context with a portion of the message being hashed
693  * @param[in] context Pointer to the SHA-256 context
694  * @param[in] data Pointer to the buffer being hashed
695  * @param[in] length Length of the buffer
696  **/
697 
698 void sha256Update(Sha256Context *context, const void *data, size_t length)
699 {
700  size_t n;
701 
702  //Acquire exclusive access to the SHA/MD5 module
704 
705  //Reset the SHA/MD5 module before use
706  hashReset();
707 
708  //Configure the SHA/MD5 module
709  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA256;
710 
711  //Restore hash context
712  SHAMD5->IDIGEST_A = context->h[0];
713  SHAMD5->IDIGEST_B = context->h[1];
714  SHAMD5->IDIGEST_C = context->h[2];
715  SHAMD5->IDIGEST_D = context->h[3];
716  SHAMD5->IDIGEST_E = context->h[4];
717  SHAMD5->IDIGEST_F = context->h[5];
718  SHAMD5->IDIGEST_G = context->h[6];
719  SHAMD5->IDIGEST_H = context->h[7];
720 
721  //Restore the value of the SHA_DIGEST_COUNT register
722  SHAMD5->DIGEST_COUNT = context->totalSize;
723 
724  //Process the incoming data
725  while(length > 0)
726  {
727  //Check whether some data is pending in the buffer
728  if(context->size == 0 && length >= 64)
729  {
730  //The length must be a multiple of 64 bytes
731  n = length - (length % 64);
732 
733  //Update hash value
735 
736  //Advance the data pointer
737  data = (uint8_t *) data + n;
738  //Remaining bytes to process
739  length -= n;
740  }
741  else
742  {
743  //The buffer can hold at most 64 bytes
744  n = MIN(length, 64 - context->size);
745 
746  //Copy the data to the buffer
747  osMemcpy(context->buffer + context->size, data, n);
748 
749  //Update the SHA-256 context
750  context->size += n;
751  //Advance the data pointer
752  data = (uint8_t *) data + n;
753  //Remaining bytes to process
754  length -= n;
755 
756  //Check whether the buffer is full
757  if(context->size == 64)
758  {
759  //Update hash value
760  hashProcessData(context->buffer, context->size, context->digest,
762 
763  //Empty the buffer
764  context->size = 0;
765  }
766  }
767  }
768 
769  //Save the value of the SHA_DIGEST_COUNT register
770  context->totalSize = SHAMD5->DIGEST_COUNT;
771 
772  //Release exclusive access to the SHA/MD5 module
774 }
775 
776 
777 /**
778  * @brief Finish the SHA-256 message digest
779  * @param[in] context Pointer to the SHA-256 context
780  * @param[out] digest Calculated digest (optional parameter)
781  **/
782 
783 void sha256Final(Sha256Context *context, uint8_t *digest)
784 {
785  //Acquire exclusive access to the SHA/MD5 module
787 
788  //Reset the SHA/MD5 module before use
789  hashReset();
790 
791  //Configure the SHA/MD5 module
792  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA256 | SHAMD5_MODE_CLOSE_HASH;
793 
794  //Restore hash context
795  SHAMD5->IDIGEST_A = context->h[0];
796  SHAMD5->IDIGEST_B = context->h[1];
797  SHAMD5->IDIGEST_C = context->h[2];
798  SHAMD5->IDIGEST_D = context->h[3];
799  SHAMD5->IDIGEST_E = context->h[4];
800  SHAMD5->IDIGEST_F = context->h[5];
801  SHAMD5->IDIGEST_G = context->h[6];
802  SHAMD5->IDIGEST_H = context->h[7];
803 
804  //Restore the value of the SHA_DIGEST_COUNT register
805  SHAMD5->DIGEST_COUNT = context->totalSize;
806 
807  //Finish digest calculation
808  hashProcessData(context->buffer, context->size, context->digest,
810 
811  //Release exclusive access to the SHA/MD5 module
813 
814  //Copy the resulting digest
815  if(digest != NULL)
816  {
817  osMemcpy(digest, context->digest, SHA256_DIGEST_SIZE);
818  }
819 }
820 
821 
822 /**
823  * @brief Finish the SHA-256 message digest (no padding added)
824  * @param[in] context Pointer to the SHA-256 context
825  * @param[out] digest Calculated digest
826  **/
827 
828 void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
829 {
830  //Copy the resulting digest
831  osMemcpy(digest, context->digest, SHA256_DIGEST_SIZE);
832 }
833 
834 #endif
835 #endif
#define BETOH32(value)
Definition: cpu_endian.h:451
#define LOAD32LE(p)
Definition: cpu_endian.h:203
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
General definitions for cryptographic algorithms.
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
Collection of hash algorithms.
#define MD5_DIGEST_SIZE
Definition: md5.h:45
OsMutex msp432e4CryptoMutex
MSP432E4 hardware cryptographic accelerator.
void sha224Init(Sha224Context *context)
Initialize SHA-224 message digest context.
void hashReset(void)
Reset SHA/MD5 module.
error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-224.
void md5FinalRaw(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest (no padding added)
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest (no padding added)
void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
void md5Update(Md5Context *context, const void *data, size_t length)
Update the MD5 context with a portion of the message being hashed.
error_t md5Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using MD5.
void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
void md5Init(Md5Context *context)
Initialize MD5 message digest context.
void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.
void md5Final(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest.
error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-1.
void sha224Final(Sha224Context *context, uint8_t *digest)
Finish the SHA-224 message digest.
void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
void hashProcessData(const uint8_t *data, size_t length, uint8_t *digest, size_t digestSize)
Update hash value.
MSP432E4 hash hardware accelerator.
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define MIN(a, b)
Definition: os_port.h:63
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
#define SHA224_DIGEST_SIZE
Definition: sha224.h:41
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
MD5 algorithm context.
Definition: md5.h:62
uint32_t h[4]
Definition: md5.h:65
uint64_t totalSize
Definition: md5.h:74
size_t size
Definition: md5.h:73
uint8_t digest[16]
Definition: md5.h:66
uint8_t buffer[64]
Definition: md5.h:71
SHA-1 algorithm context.
Definition: sha1.h:62
uint8_t digest[20]
Definition: sha1.h:66
uint64_t totalSize
Definition: sha1.h:74
size_t size
Definition: sha1.h:73
uint32_t h[5]
Definition: sha1.h:65
uint8_t buffer[64]
Definition: sha1.h:71
SHA-256 algorithm context.
Definition: sha256.h:62
uint8_t digest[32]
Definition: sha256.h:66
uint64_t totalSize
Definition: sha256.h:74
size_t size
Definition: sha256.h:73
uint8_t buffer[64]
Definition: sha256.h:71
uint32_t h[8]
Definition: sha256.h:65
uint8_t length
Definition: tcp.h:368