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-2025 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.5.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] h Resulting hash value
76  * @param[in] hLen Length of the hash value, in words
77  **/
78 
79 void hashProcessData(const uint8_t *data, size_t length, uint32_t *h,
80  size_t hLen)
81 {
82  size_t i;
83 
84  //Specify the length
85  SHAMD5->LENGTH = length;
86 
87  //Digest input data
88  while(length >= 64)
89  {
90  //Wait for the SHA/MD5 engine to be ready to accept data
91  while((SHAMD5->IRQSTATUS & SHAMD5_IRQSTATUS_INPUT_READY) == 0)
92  {
93  }
94 
95  //Write 64-byte block
96  SHAMD5->DATA_0_IN = LOAD32LE(data);
97  SHAMD5->DATA_1_IN = LOAD32LE(data + 4);
98  SHAMD5->DATA_2_IN = LOAD32LE(data + 8);
99  SHAMD5->DATA_3_IN = LOAD32LE(data + 12);
100  SHAMD5->DATA_4_IN = LOAD32LE(data + 16);
101  SHAMD5->DATA_5_IN = LOAD32LE(data + 20);
102  SHAMD5->DATA_6_IN = LOAD32LE(data + 24);
103  SHAMD5->DATA_7_IN = LOAD32LE(data + 28);
104  SHAMD5->DATA_8_IN = LOAD32LE(data + 32);
105  SHAMD5->DATA_9_IN = LOAD32LE(data + 36);
106  SHAMD5->DATA_10_IN = LOAD32LE(data + 40);
107  SHAMD5->DATA_11_IN = LOAD32LE(data + 44);
108  SHAMD5->DATA_12_IN = LOAD32LE(data + 48);
109  SHAMD5->DATA_13_IN = LOAD32LE(data + 52);
110  SHAMD5->DATA_14_IN = LOAD32LE(data + 56);
111  SHAMD5->DATA_15_IN = LOAD32LE(data + 60);
112 
113  //Advance data pointer
114  data += 64;
115  length -= 64;
116  }
117 
118  //Process final block
119  if(length > 0)
120  {
121  //Wait for the SHA/MD5 engine to be ready to accept data
122  while((SHAMD5->IRQSTATUS & SHAMD5_IRQSTATUS_INPUT_READY) == 0)
123  {
124  }
125 
126  //Write final block
127  for(i = 0; i < length; i += 4)
128  {
129  //Write 32-bit word
130  HWREG(SHAMD5_BASE + SHAMD5_O_DATA_0_IN + i) = LOAD32LE(data);
131  //Advance data pointer
132  data += 4;
133  }
134  }
135 
136  //Wait for the output to be ready
137  while((SHAMD5->IRQSTATUS & SHAMD5_IRQSTATUS_OUTPUT_READY) == 0)
138  {
139  }
140 
141  //Read the resulting output value
142  for(i = 0; i < hLen; i++)
143  {
144  h[i] = HWREG(SHAMD5_BASE + SHAMD5_O_IDIGEST_A + i * 4);
145  }
146 }
147 
148 
149 #if (MD5_SUPPORT == ENABLED)
150 
151 /**
152  * @brief Digest a message using MD5
153  * @param[in] data Pointer to the message being hashed
154  * @param[in] length Length of the message
155  * @param[out] digest Pointer to the calculated digest
156  * @return Error code
157  **/
158 
159 error_t md5Compute(const void *data, size_t length, uint8_t *digest)
160 {
161  uint_t i;
162  uint32_t h[4];
163 
164  //Acquire exclusive access to the SHA/MD5 module
166 
167  //Reset the SHA/MD5 module before use
168  hashReset();
169 
170  //Configure the SHA/MD5 module
171  SHAMD5->MODE = SHAMD5_MODE_ALGO_MD5 | SHAMD5_MODE_ALGO_CONSTANT |
172  SHAMD5_MODE_CLOSE_HASH;
173 
174  //Digest the message
176 
177  //Release exclusive access to the SHA/MD5 module
179 
180  //Copy the resulting digest
181  for(i = 0; i < (MD5_DIGEST_SIZE / 4); i++)
182  {
183  STORE32LE(h[i], digest + i * 4);
184  }
185 
186  //Sucessful processing
187  return NO_ERROR;
188 }
189 
190 
191 /**
192  * @brief Initialize MD5 message digest context
193  * @param[in] context Pointer to the MD5 context to initialize
194  **/
195 
196 void md5Init(Md5Context *context)
197 {
198  //Set initial hash value
199  context->h[0] = 0x67452301;
200  context->h[1] = 0xEFCDAB89;
201  context->h[2] = 0x98BADCFE;
202  context->h[3] = 0x10325476;
203 
204  //Number of bytes in the buffer
205  context->size = 0;
206  //Total length of the message
207  context->totalSize = 0;
208 }
209 
210 
211 /**
212  * @brief Update the MD5 context with a portion of the message being hashed
213  * @param[in] context Pointer to the MD5 context
214  * @param[in] data Pointer to the buffer being hashed
215  * @param[in] length Length of the buffer
216  **/
217 
218 void md5Update(Md5Context *context, const void *data, size_t length)
219 {
220  size_t n;
221 
222  //Acquire exclusive access to the SHA/MD5 module
224 
225  //Reset the SHA/MD5 module before use
226  hashReset();
227 
228  //Configure the SHA/MD5 module
229  SHAMD5->MODE = SHAMD5_MODE_ALGO_MD5;
230 
231  //Restore hash context
232  SHAMD5->IDIGEST_A = context->h[0];
233  SHAMD5->IDIGEST_B = context->h[1];
234  SHAMD5->IDIGEST_C = context->h[2];
235  SHAMD5->IDIGEST_D = context->h[3];
236 
237  //Restore the value of the SHA_DIGEST_COUNT register
238  SHAMD5->DIGEST_COUNT = context->totalSize;
239 
240  //Process the incoming data
241  while(length > 0)
242  {
243  //Check whether some data is pending in the buffer
244  if(context->size == 0 && length >= 64)
245  {
246  //The length must be a multiple of 64 bytes
247  n = length - (length % 64);
248 
249  //Update hash value
250  hashProcessData(data, n, context->h, MD5_DIGEST_SIZE / 4);
251 
252  //Advance the data pointer
253  data = (uint8_t *) data + n;
254  //Remaining bytes to process
255  length -= n;
256  }
257  else
258  {
259  //The buffer can hold at most 64 bytes
260  n = MIN(length, 64 - context->size);
261 
262  //Copy the data to the buffer
263  osMemcpy(context->buffer + context->size, data, n);
264 
265  //Update the MD5 context
266  context->size += n;
267  //Advance the data pointer
268  data = (uint8_t *) data + n;
269  //Remaining bytes to process
270  length -= n;
271 
272  //Check whether the buffer is full
273  if(context->size == 64)
274  {
275  //Update hash value
276  hashProcessData(context->buffer, context->size, context->h,
277  MD5_DIGEST_SIZE / 4);
278 
279  //Empty the buffer
280  context->size = 0;
281  }
282  }
283  }
284 
285  //Save the value of the SHA_DIGEST_COUNT register
286  context->totalSize = SHAMD5->DIGEST_COUNT;
287 
288  //Release exclusive access to the SHA/MD5 module
290 }
291 
292 
293 /**
294  * @brief Finish the MD5 message digest
295  * @param[in] context Pointer to the MD5 context
296  * @param[out] digest Calculated digest
297  **/
298 
299 void md5Final(Md5Context *context, uint8_t *digest)
300 {
301  uint_t i;
302 
303  //Acquire exclusive access to the SHA/MD5 module
305 
306  //Reset the SHA/MD5 module before use
307  hashReset();
308 
309  //Configure the SHA/MD5 module
310  SHAMD5->MODE = SHAMD5_MODE_ALGO_MD5 | SHAMD5_MODE_CLOSE_HASH;
311 
312  //Restore hash context
313  SHAMD5->IDIGEST_A = context->h[0];
314  SHAMD5->IDIGEST_B = context->h[1];
315  SHAMD5->IDIGEST_C = context->h[2];
316  SHAMD5->IDIGEST_D = context->h[3];
317 
318  //Restore the value of the SHA_DIGEST_COUNT register
319  SHAMD5->DIGEST_COUNT = context->totalSize;
320 
321  //Finish digest calculation
322  hashProcessData(context->buffer, context->size, context->h,
323  MD5_DIGEST_SIZE / 4);
324 
325  //Release exclusive access to the SHA/MD5 module
327 
328  //Copy the resulting digest
329  for(i = 0; i < (MD5_DIGEST_SIZE / 4); i++)
330  {
331  STORE32LE(context->h[i], digest + i * 4);
332  }
333 }
334 
335 
336 /**
337  * @brief Finish the MD5 message digest (no padding added)
338  * @param[in] context Pointer to the MD5 context
339  * @param[out] digest Calculated digest
340  **/
341 
342 void md5FinalRaw(Md5Context *context, uint8_t *digest)
343 {
344  uint_t i;
345 
346  //Copy the resulting digest
347  for(i = 0; i < (MD5_DIGEST_SIZE / 4); i++)
348  {
349  STORE32LE(context->h[i], digest + i * 4);
350  }
351 }
352 
353 #endif
354 #if (SHA1_SUPPORT == ENABLED)
355 
356 /**
357  * @brief Digest a message using SHA-1
358  * @param[in] data Pointer to the message being hashed
359  * @param[in] length Length of the message
360  * @param[out] digest Pointer to the calculated digest
361  * @return Error code
362  **/
363 
364 error_t sha1Compute(const void *data, size_t length, uint8_t *digest)
365 {
366  uint_t i;
367  uint32_t h[5];
368 
369  //Acquire exclusive access to the SHA/MD5 module
371 
372  //Reset the SHA/MD5 module before use
373  hashReset();
374 
375  //Configure the SHA/MD5 module
376  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA1 | SHAMD5_MODE_ALGO_CONSTANT |
377  SHAMD5_MODE_CLOSE_HASH;
378 
379  //Digest the message
381 
382  //Release exclusive access to the SHA/MD5 module
384 
385  //Copy the resulting digest
386  for(i = 0; i < (SHA1_DIGEST_SIZE / 4); i++)
387  {
388  STORE32LE(h[i], digest + i * 4);
389  }
390 
391  //Sucessful processing
392  return NO_ERROR;
393 }
394 
395 
396 /**
397  * @brief Initialize SHA-1 message digest context
398  * @param[in] context Pointer to the SHA-1 context to initialize
399  **/
400 
401 void sha1Init(Sha1Context *context)
402 {
403  //Set initial hash value
404  context->h[0] = BETOH32(0x67452301);
405  context->h[1] = BETOH32(0xEFCDAB89);
406  context->h[2] = BETOH32(0x98BADCFE);
407  context->h[3] = BETOH32(0x10325476);
408  context->h[4] = BETOH32(0xC3D2E1F0);
409 
410  //Number of bytes in the buffer
411  context->size = 0;
412  //Total length of the message
413  context->totalSize = 0;
414 }
415 
416 
417 /**
418  * @brief Update the SHA-1 context with a portion of the message being hashed
419  * @param[in] context Pointer to the SHA-1 context
420  * @param[in] data Pointer to the buffer being hashed
421  * @param[in] length Length of the buffer
422  **/
423 
424 void sha1Update(Sha1Context *context, const void *data, size_t length)
425 {
426  size_t n;
427 
428  //Acquire exclusive access to the SHA/MD5 module
430 
431  //Reset the SHA/MD5 module before use
432  hashReset();
433 
434  //Configure the SHA/MD5 module
435  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA1;
436 
437  //Restore hash context
438  SHAMD5->IDIGEST_A = context->h[0];
439  SHAMD5->IDIGEST_B = context->h[1];
440  SHAMD5->IDIGEST_C = context->h[2];
441  SHAMD5->IDIGEST_D = context->h[3];
442  SHAMD5->IDIGEST_E = context->h[4];
443 
444  //Restore the value of the SHA_DIGEST_COUNT register
445  SHAMD5->DIGEST_COUNT = context->totalSize;
446 
447  //Process the incoming data
448  while(length > 0)
449  {
450  //Check whether some data is pending in the buffer
451  if(context->size == 0 && length >= 64)
452  {
453  //The length must be a multiple of 64 bytes
454  n = length - (length % 64);
455 
456  //Update hash value
457  hashProcessData(data, n, context->h, SHA1_DIGEST_SIZE / 4);
458 
459  //Advance the data pointer
460  data = (uint8_t *) data + n;
461  //Remaining bytes to process
462  length -= n;
463  }
464  else
465  {
466  //The buffer can hold at most 64 bytes
467  n = MIN(length, 64 - context->size);
468 
469  //Copy the data to the buffer
470  osMemcpy(context->buffer + context->size, data, n);
471 
472  //Update the SHA-1 context
473  context->size += n;
474  //Advance the data pointer
475  data = (uint8_t *) data + n;
476  //Remaining bytes to process
477  length -= n;
478 
479  //Check whether the buffer is full
480  if(context->size == 64)
481  {
482  //Update hash value
483  hashProcessData(context->buffer, context->size, context->h,
484  SHA1_DIGEST_SIZE / 4);
485 
486  //Empty the buffer
487  context->size = 0;
488  }
489  }
490  }
491 
492  //Save the value of the SHA_DIGEST_COUNT register
493  context->totalSize = SHAMD5->DIGEST_COUNT;
494 
495  //Release exclusive access to the SHA/MD5 module
497 }
498 
499 
500 /**
501  * @brief Finish the SHA-1 message digest
502  * @param[in] context Pointer to the SHA-1 context
503  * @param[out] digest Calculated digest
504  **/
505 
506 void sha1Final(Sha1Context *context, uint8_t *digest)
507 {
508  uint_t i;
509 
510  //Acquire exclusive access to the SHA/MD5 module
512 
513  //Reset the SHA/MD5 module before use
514  hashReset();
515 
516  //Configure the SHA/MD5 module
517  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA1 | SHAMD5_MODE_CLOSE_HASH;
518 
519  //Restore hash context
520  SHAMD5->IDIGEST_A = context->h[0];
521  SHAMD5->IDIGEST_B = context->h[1];
522  SHAMD5->IDIGEST_C = context->h[2];
523  SHAMD5->IDIGEST_D = context->h[3];
524  SHAMD5->IDIGEST_E = context->h[4];
525 
526  //Restore the value of the SHA_DIGEST_COUNT register
527  SHAMD5->DIGEST_COUNT = context->totalSize;
528 
529  //Finish digest calculation
530  hashProcessData(context->buffer, context->size, context->h,
531  SHA1_DIGEST_SIZE / 4);
532 
533  //Release exclusive access to the SHA/MD5 module
535 
536  //Copy the resulting digest
537  for(i = 0; i < (SHA1_DIGEST_SIZE / 4); i++)
538  {
539  STORE32LE(context->h[i], digest + i * 4);
540  }
541 }
542 
543 
544 /**
545  * @brief Finish the SHA-1 message digest (no padding added)
546  * @param[in] context Pointer to the SHA-1 context
547  * @param[out] digest Calculated digest
548  **/
549 
550 void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
551 {
552  uint_t i;
553 
554  //Copy the resulting digest
555  for(i = 0; i < (SHA1_DIGEST_SIZE / 4); i++)
556  {
557  STORE32LE(context->h[i], digest + i * 4);
558  }
559 }
560 
561 #endif
562 #if (SHA224_SUPPORT == ENABLED)
563 
564 /**
565  * @brief Digest a message using SHA-224
566  * @param[in] data Pointer to the message being hashed
567  * @param[in] length Length of the message
568  * @param[out] digest Pointer to the calculated digest
569  * @return Error code
570  **/
571 
572 error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
573 {
574  uint_t i;
575  uint32_t h[7];
576 
577  //Acquire exclusive access to the SHA/MD5 module
579 
580  //Reset the SHA/MD5 module before use
581  hashReset();
582 
583  //Configure the SHA/MD5 module
584  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA224 | SHAMD5_MODE_ALGO_CONSTANT |
585  SHAMD5_MODE_CLOSE_HASH;
586 
587  //Digest the message
589 
590  //Release exclusive access to the SHA/MD5 module
592 
593  //Copy the resulting digest
594  for(i = 0; i < (SHA224_DIGEST_SIZE / 4); i++)
595  {
596  STORE32LE(h[i], digest + i * 4);
597  }
598 
599  //Sucessful processing
600  return NO_ERROR;
601 }
602 
603 
604 /**
605  * @brief Initialize SHA-224 message digest context
606  * @param[in] context Pointer to the SHA-224 context to initialize
607  **/
608 
609 void sha224Init(Sha224Context *context)
610 {
611  //Set initial hash value
612  context->h[0] = BETOH32(0xC1059ED8);
613  context->h[1] = BETOH32(0x367CD507);
614  context->h[2] = BETOH32(0x3070DD17);
615  context->h[3] = BETOH32(0xF70E5939);
616  context->h[4] = BETOH32(0xFFC00B31);
617  context->h[5] = BETOH32(0x68581511);
618  context->h[6] = BETOH32(0x64F98FA7);
619  context->h[7] = BETOH32(0xBEFA4FA4);
620 
621  //Number of bytes in the buffer
622  context->size = 0;
623  //Total length of the message
624  context->totalSize = 0;
625 }
626 
627 
628 /**
629  * @brief Finish the SHA-224 message digest
630  * @param[in] context Pointer to the SHA-224 context
631  * @param[out] digest Calculated digest
632  **/
633 
634 void sha224Final(Sha224Context *context, uint8_t *digest)
635 {
636  uint_t i;
637 
638  //Acquire exclusive access to the SHA/MD5 module
640 
641  //Reset the SHA/MD5 module before use
642  hashReset();
643 
644  //Configure the SHA/MD5 module
645  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA224 | SHAMD5_MODE_CLOSE_HASH;
646 
647  //Restore hash context
648  SHAMD5->IDIGEST_A = context->h[0];
649  SHAMD5->IDIGEST_B = context->h[1];
650  SHAMD5->IDIGEST_C = context->h[2];
651  SHAMD5->IDIGEST_D = context->h[3];
652  SHAMD5->IDIGEST_E = context->h[4];
653  SHAMD5->IDIGEST_F = context->h[5];
654  SHAMD5->IDIGEST_G = context->h[6];
655  SHAMD5->IDIGEST_H = context->h[7];
656 
657  //Restore the value of the SHA_DIGEST_COUNT register
658  SHAMD5->DIGEST_COUNT = context->totalSize;
659 
660  //Finish digest calculation
661  hashProcessData(context->buffer, context->size, context->h,
662  SHA224_DIGEST_SIZE / 4);
663 
664  //Release exclusive access to the SHA/MD5 module
666 
667  //Copy the resulting digest
668  for(i = 0; i < (SHA224_DIGEST_SIZE / 4); i++)
669  {
670  STORE32LE(context->h[i], digest + i * 4);
671  }
672 }
673 
674 #endif
675 #if (SHA256_SUPPORT == ENABLED)
676 
677 /**
678  * @brief Digest a message using SHA-256
679  * @param[in] data Pointer to the message being hashed
680  * @param[in] length Length of the message
681  * @param[out] digest Pointer to the calculated digest
682  * @return Error code
683  **/
684 
685 error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
686 {
687  uint_t i;
688  uint32_t h[8];
689 
690  //Acquire exclusive access to the SHA/MD5 module
692 
693  //Reset the SHA/MD5 module before use
694  hashReset();
695 
696  //Configure the SHA/MD5 module
697  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA256 | SHAMD5_MODE_ALGO_CONSTANT |
698  SHAMD5_MODE_CLOSE_HASH;
699 
700  //Digest the message
702 
703  //Release exclusive access to the SHA/MD5 module
705 
706  //Copy the resulting digest
707  for(i = 0; i < (SHA256_DIGEST_SIZE / 4); i++)
708  {
709  STORE32LE(h[i], digest + i * 4);
710  }
711 
712  //Sucessful processing
713  return NO_ERROR;
714 }
715 
716 
717 /**
718  * @brief Initialize SHA-256 message digest context
719  * @param[in] context Pointer to the SHA-256 context to initialize
720  **/
721 
722 void sha256Init(Sha256Context *context)
723 {
724  //Set initial hash value
725  context->h[0] = BETOH32(0x6A09E667);
726  context->h[1] = BETOH32(0xBB67AE85);
727  context->h[2] = BETOH32(0x3C6EF372);
728  context->h[3] = BETOH32(0xA54FF53A);
729  context->h[4] = BETOH32(0x510E527F);
730  context->h[5] = BETOH32(0x9B05688C);
731  context->h[6] = BETOH32(0x1F83D9AB);
732  context->h[7] = BETOH32(0x5BE0CD19);
733 
734  //Number of bytes in the buffer
735  context->size = 0;
736  //Total length of the message
737  context->totalSize = 0;
738 }
739 
740 
741 /**
742  * @brief Update the SHA-256 context with a portion of the message being hashed
743  * @param[in] context Pointer to the SHA-256 context
744  * @param[in] data Pointer to the buffer being hashed
745  * @param[in] length Length of the buffer
746  **/
747 
748 void sha256Update(Sha256Context *context, const void *data, size_t length)
749 {
750  size_t n;
751 
752  //Acquire exclusive access to the SHA/MD5 module
754 
755  //Reset the SHA/MD5 module before use
756  hashReset();
757 
758  //Configure the SHA/MD5 module
759  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA256;
760 
761  //Restore hash context
762  SHAMD5->IDIGEST_A = context->h[0];
763  SHAMD5->IDIGEST_B = context->h[1];
764  SHAMD5->IDIGEST_C = context->h[2];
765  SHAMD5->IDIGEST_D = context->h[3];
766  SHAMD5->IDIGEST_E = context->h[4];
767  SHAMD5->IDIGEST_F = context->h[5];
768  SHAMD5->IDIGEST_G = context->h[6];
769  SHAMD5->IDIGEST_H = context->h[7];
770 
771  //Restore the value of the SHA_DIGEST_COUNT register
772  SHAMD5->DIGEST_COUNT = context->totalSize;
773 
774  //Process the incoming data
775  while(length > 0)
776  {
777  //Check whether some data is pending in the buffer
778  if(context->size == 0 && length >= 64)
779  {
780  //The length must be a multiple of 64 bytes
781  n = length - (length % 64);
782 
783  //Update hash value
784  hashProcessData(data, n, context->h, SHA256_DIGEST_SIZE / 4);
785 
786  //Advance the data pointer
787  data = (uint8_t *) data + n;
788  //Remaining bytes to process
789  length -= n;
790  }
791  else
792  {
793  //The buffer can hold at most 64 bytes
794  n = MIN(length, 64 - context->size);
795 
796  //Copy the data to the buffer
797  osMemcpy(context->buffer + context->size, data, n);
798 
799  //Update the SHA-256 context
800  context->size += n;
801  //Advance the data pointer
802  data = (uint8_t *) data + n;
803  //Remaining bytes to process
804  length -= n;
805 
806  //Check whether the buffer is full
807  if(context->size == 64)
808  {
809  //Update hash value
810  hashProcessData(context->buffer, context->size, context->h,
811  SHA256_DIGEST_SIZE / 4);
812 
813  //Empty the buffer
814  context->size = 0;
815  }
816  }
817  }
818 
819  //Save the value of the SHA_DIGEST_COUNT register
820  context->totalSize = SHAMD5->DIGEST_COUNT;
821 
822  //Release exclusive access to the SHA/MD5 module
824 }
825 
826 
827 /**
828  * @brief Finish the SHA-256 message digest
829  * @param[in] context Pointer to the SHA-256 context
830  * @param[out] digest Calculated digest
831  **/
832 
833 void sha256Final(Sha256Context *context, uint8_t *digest)
834 {
835  uint_t i;
836 
837  //Acquire exclusive access to the SHA/MD5 module
839 
840  //Reset the SHA/MD5 module before use
841  hashReset();
842 
843  //Configure the SHA/MD5 module
844  SHAMD5->MODE = SHAMD5_MODE_ALGO_SHA256 | SHAMD5_MODE_CLOSE_HASH;
845 
846  //Restore hash context
847  SHAMD5->IDIGEST_A = context->h[0];
848  SHAMD5->IDIGEST_B = context->h[1];
849  SHAMD5->IDIGEST_C = context->h[2];
850  SHAMD5->IDIGEST_D = context->h[3];
851  SHAMD5->IDIGEST_E = context->h[4];
852  SHAMD5->IDIGEST_F = context->h[5];
853  SHAMD5->IDIGEST_G = context->h[6];
854  SHAMD5->IDIGEST_H = context->h[7];
855 
856  //Restore the value of the SHA_DIGEST_COUNT register
857  SHAMD5->DIGEST_COUNT = context->totalSize;
858 
859  //Finish digest calculation
860  hashProcessData(context->buffer, context->size, context->h,
861  SHA256_DIGEST_SIZE / 4);
862 
863  //Release exclusive access to the SHA/MD5 module
865 
866  //Copy the resulting digest
867  for(i = 0; i < (SHA256_DIGEST_SIZE / 4); i++)
868  {
869  STORE32LE(context->h[i], digest + i * 4);
870  }
871 }
872 
873 
874 /**
875  * @brief Finish the SHA-256 message digest (no padding added)
876  * @param[in] context Pointer to the SHA-256 context
877  * @param[out] digest Calculated digest
878  **/
879 
880 void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
881 {
882  uint_t i;
883 
884  //Copy the resulting digest
885  for(i = 0; i < (SHA256_DIGEST_SIZE / 4); i++)
886  {
887  STORE32LE(context->h[i], digest + i * 4);
888  }
889 }
890 
891 #endif
892 #endif
SHA-256 algorithm context.
Definition: sha256.h:62
uint8_t data[]
Definition: ethernet.h:222
#define STORE32LE(a, p)
Definition: cpu_endian.h:279
size_t size
Definition: sha256.h:69
void md5Final(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest.
#define SHA224_DIGEST_SIZE
Definition: sha224.h:41
void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest (no padding added)
uint32_t h[8]
Definition: sha256.h:63
#define BETOH32(value)
Definition: cpu_endian.h:451
void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
OsMutex msp432e4CryptoMutex
void md5FinalRaw(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest (no padding added)
void hashReset(void)
Reset SHA/MD5 module.
error_t md5Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using MD5.
uint64_t totalSize
Definition: sha1.h:70
void md5Init(Md5Context *context)
Initialize MD5 message digest context.
void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
uint8_t buffer[64]
Definition: md5.h:67
MSP432E4 hardware cryptographic accelerator.
uint8_t h
Definition: ndp.h:302
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
error_t
Error codes.
Definition: error.h:43
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.
uint32_t h[5]
Definition: sha1.h:63
General definitions for cryptographic algorithms.
MD5 algorithm context.
Definition: md5.h:62
uint8_t length
Definition: tcp.h:375
uint8_t buffer[64]
Definition: sha256.h:67
#define MIN(a, b)
Definition: os_port.h:63
uint64_t totalSize
Definition: md5.h:70
#define MD5_DIGEST_SIZE
Definition: md5.h:45
Collection of hash algorithms.
void hashProcessData(const uint8_t *data, size_t length, uint32_t *h, size_t hLen)
Update hash value.
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
uint8_t n
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
size_t size
Definition: sha1.h:69
size_t size
Definition: md5.h:69
error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
SHA-1 algorithm context.
Definition: sha1.h:62
error_t sha224Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-224.
uint8_t buffer[64]
Definition: sha1.h:67
#define LOAD32LE(p)
Definition: cpu_endian.h:203
uint64_t totalSize
Definition: sha256.h:70
void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
unsigned int uint_t
Definition: compiler_port.h:57
MSP432E4 hash hardware accelerator.
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
void sha224Init(Sha224Context *context)
Initialize SHA-224 message digest context.
uint32_t h[4]
Definition: md5.h:63
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void md5Update(Md5Context *context, const void *data, size_t length)
Update the MD5 context with a portion of the message being hashed.
void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.