pic32cx_mt_crypto_hash.c
Go to the documentation of this file.
1 /**
2  * @file pic32cx_mt_crypto_hash.c
3  * @brief PIC32CX MTC/MTG/MTSH 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.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "pic32c.h"
36 #include "core/crypto.h"
39 #include "hash/hash_algorithms.h"
40 #include "debug.h"
41 
42 //Check crypto library configuration
43 #if (PIC32CX_MT_CRYPTO_HASH_SUPPORT == ENABLED)
44 
45 //Padding string
46 static const uint8_t padding[128] =
47 {
48  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
56 };
57 
58 
59 /**
60  * @brief Update hash value
61  * @param[in] algo Hash algorithm
62  * @param[in] data Pointer to the input buffer
63  * @param[in] length Length of the input buffer
64  * @param[in,out] h Hash value
65  **/
66 
67 void hashProcessData(uint32_t algo, const uint8_t *data, size_t length,
68  uint32_t *h)
69 {
70  size_t blockSize;
71  uint32_t *p;
72 
73  //Get block size
74  blockSize = (algo == SHA_MR_ALGO_SHA512) ? 128 : 64;
75 
76  //Acquire exclusive access to the SHA module
78 
79  //Perform software reset
80  SHA_REGS->SHA_CR = SHA_CR_SWRST_Msk;
81 
82  //Select the relevant hash algorithm
83  SHA_REGS->SHA_MR = SHA_MR_UIHV_Msk | SHA_MR_SMOD_MANUAL_START | algo;
84 
85  //Set the WUIHV bit before loading the initial hash value
86  SHA_REGS->SHA_CR = SHA_CR_WUIHV_Msk;
87 
88  //Restore initial hash value
89  SHA_REGS->SHA_IDATAR[0] = h[0];
90  SHA_REGS->SHA_IDATAR[1] = h[1];
91  SHA_REGS->SHA_IDATAR[2] = h[2];
92  SHA_REGS->SHA_IDATAR[3] = h[3];
93  SHA_REGS->SHA_IDATAR[4] = h[4];
94 
95  //SHA-256 or SHA-512 algorithm?
96  if(algo == SHA_MR_ALGO_SHA256 || algo == SHA_MR_ALGO_SHA512)
97  {
98  SHA_REGS->SHA_IDATAR[5] = h[5];
99  SHA_REGS->SHA_IDATAR[6] = h[6];
100  SHA_REGS->SHA_IDATAR[7] = h[7];
101  }
102 
103  //SHA-512 algorithm?
104  if(algo == SHA_MR_ALGO_SHA512)
105  {
106  SHA_REGS->SHA_IDATAR[8] = h[8];
107  SHA_REGS->SHA_IDATAR[9] = h[9];
108  SHA_REGS->SHA_IDATAR[10] = h[10];
109  SHA_REGS->SHA_IDATAR[11] = h[11];
110  SHA_REGS->SHA_IDATAR[12] = h[12];
111  SHA_REGS->SHA_IDATAR[13] = h[13];
112  SHA_REGS->SHA_IDATAR[14] = h[14];
113  SHA_REGS->SHA_IDATAR[15] = h[15];
114  }
115 
116  //The FIRST bit indicates that the next block to process is the first one
117  //of the message
118  SHA_REGS->SHA_CR = SHA_CR_FIRST_Msk;
119 
120  //Input data are processed in a block-by-block fashion
121  while(length >= blockSize)
122  {
123  //Write the block to be processed to the input data registers
124  p = (uint32_t *) data;
125  SHA_REGS->SHA_IDATAR[0] = p[0];
126  SHA_REGS->SHA_IDATAR[1] = p[1];
127  SHA_REGS->SHA_IDATAR[2] = p[2];
128  SHA_REGS->SHA_IDATAR[3] = p[3];
129  SHA_REGS->SHA_IDATAR[4] = p[4];
130  SHA_REGS->SHA_IDATAR[5] = p[5];
131  SHA_REGS->SHA_IDATAR[6] = p[6];
132  SHA_REGS->SHA_IDATAR[7] = p[7];
133  SHA_REGS->SHA_IDATAR[8] = p[8];
134  SHA_REGS->SHA_IDATAR[9] = p[9];
135  SHA_REGS->SHA_IDATAR[10] = p[10];
136  SHA_REGS->SHA_IDATAR[11] = p[11];
137  SHA_REGS->SHA_IDATAR[12] = p[12];
138  SHA_REGS->SHA_IDATAR[13] = p[13];
139  SHA_REGS->SHA_IDATAR[14] = p[14];
140  SHA_REGS->SHA_IDATAR[15] = p[15];
141 
142  //SHA-512 algorithm?
143  if(algo == SHA_MR_ALGO_SHA512)
144  {
145  SHA_REGS->SHA_IODATAR[0] = p[16];
146  SHA_REGS->SHA_IODATAR[1] = p[17];
147  SHA_REGS->SHA_IODATAR[2] = p[18];
148  SHA_REGS->SHA_IODATAR[3] = p[19];
149  SHA_REGS->SHA_IODATAR[4] = p[20];
150  SHA_REGS->SHA_IODATAR[5] = p[21];
151  SHA_REGS->SHA_IODATAR[6] = p[22];
152  SHA_REGS->SHA_IODATAR[7] = p[23];
153  SHA_REGS->SHA_IODATAR[8] = p[24];
154  SHA_REGS->SHA_IODATAR[9] = p[25];
155  SHA_REGS->SHA_IODATAR[10] = p[26];
156  SHA_REGS->SHA_IODATAR[11] = p[27];
157  SHA_REGS->SHA_IODATAR[12] = p[28];
158  SHA_REGS->SHA_IODATAR[13] = p[29];
159  SHA_REGS->SHA_IODATAR[14] = p[30];
160  SHA_REGS->SHA_IODATAR[15] = p[31];
161  }
162 
163  //Set the START bit to begin the processing
164  SHA_REGS->SHA_CR = SHA_CR_START_Msk;
165 
166  //When processing completes, the DATRDY flag is raised
167  while((SHA_REGS->SHA_ISR & SHA_ISR_DATRDY_Msk) == 0)
168  {
169  }
170 
171  //Advance data pointer
172  data += blockSize;
173  length -= blockSize;
174  }
175 
176  //Save intermediate hash value
177  h[0] = SHA_REGS->SHA_IODATAR[0];
178  h[1] = SHA_REGS->SHA_IODATAR[1];
179  h[2] = SHA_REGS->SHA_IODATAR[2];
180  h[3] = SHA_REGS->SHA_IODATAR[3];
181  h[4] = SHA_REGS->SHA_IODATAR[4];
182 
183  //SHA-256 or SHA-512 algorithm?
184  if(algo == SHA_MR_ALGO_SHA256 || algo == SHA_MR_ALGO_SHA512)
185  {
186  h[5] = SHA_REGS->SHA_IODATAR[5];
187  h[6] = SHA_REGS->SHA_IODATAR[6];
188  h[7] = SHA_REGS->SHA_IODATAR[7];
189  }
190 
191  //SHA-512 algorithm?
192  if(algo == SHA_MR_ALGO_SHA512)
193  {
194  h[8] = SHA_REGS->SHA_IODATAR[8];
195  h[9] = SHA_REGS->SHA_IODATAR[9];
196  h[10] = SHA_REGS->SHA_IODATAR[10];
197  h[11] = SHA_REGS->SHA_IODATAR[11];
198  h[12] = SHA_REGS->SHA_IODATAR[12];
199  h[13] = SHA_REGS->SHA_IODATAR[13];
200  h[14] = SHA_REGS->SHA_IODATAR[14];
201  h[15] = SHA_REGS->SHA_IODATAR[15];
202  }
203 
204  //Release exclusive access to the SHA module
206 }
207 
208 
209 #if (SHA1_SUPPORT == ENABLED)
210 
211 /**
212  * @brief Initialize SHA-1 message digest context
213  * @param[in] context Pointer to the SHA-1 context to initialize
214  **/
215 
216 void sha1Init(Sha1Context *context)
217 {
218  //Set initial hash value
219  context->h[0] = BETOH32(0x67452301);
220  context->h[1] = BETOH32(0xEFCDAB89);
221  context->h[2] = BETOH32(0x98BADCFE);
222  context->h[3] = BETOH32(0x10325476);
223  context->h[4] = BETOH32(0xC3D2E1F0);
224 
225  //Number of bytes in the buffer
226  context->size = 0;
227  //Total length of the message
228  context->totalSize = 0;
229 }
230 
231 
232 /**
233  * @brief Update the SHA-1 context with a portion of the message being hashed
234  * @param[in] context Pointer to the SHA-1 context
235  * @param[in] data Pointer to the buffer being hashed
236  * @param[in] length Length of the buffer
237  **/
238 
239 void sha1Update(Sha1Context *context, const void *data, size_t length)
240 {
241  size_t n;
242 
243  //Process the incoming data
244  while(length > 0)
245  {
246  //Check whether some data is pending in the buffer
247  if(context->size == 0 && length >= 64)
248  {
249  //The length must be a multiple of 64 bytes
250  n = length - (length % 64);
251 
252  //Update hash value
253  hashProcessData(SHA_MR_ALGO_SHA1, data, n, context->h);
254 
255  //Update the SHA-1 context
256  context->totalSize += n;
257  //Advance the data pointer
258  data = (uint8_t *) data + n;
259  //Remaining bytes to process
260  length -= n;
261  }
262  else
263  {
264  //The buffer can hold at most 64 bytes
265  n = MIN(length, 64 - context->size);
266 
267  //Copy the data to the buffer
268  osMemcpy(context->buffer + context->size, data, n);
269 
270  //Update the SHA-1 context
271  context->size += n;
272  context->totalSize += n;
273  //Advance the data pointer
274  data = (uint8_t *) data + n;
275  //Remaining bytes to process
276  length -= n;
277 
278  //Check whether the buffer is full
279  if(context->size == 64)
280  {
281  //Update hash value
282  hashProcessData(SHA_MR_ALGO_SHA1, context->buffer, context->size,
283  context->h);
284 
285  //Empty the buffer
286  context->size = 0;
287  }
288  }
289  }
290 }
291 
292 
293 /**
294  * @brief Finish the SHA-1 message digest
295  * @param[in] context Pointer to the SHA-1 context
296  * @param[out] digest Calculated digest (optional parameter)
297  **/
298 
299 void sha1Final(Sha1Context *context, uint8_t *digest)
300 {
301  size_t paddingSize;
302  uint64_t totalSize;
303 
304  //Length of the original message (before padding)
305  totalSize = context->totalSize * 8;
306 
307  //Pad the message so that its length is congruent to 56 modulo 64
308  if(context->size < 56)
309  {
310  paddingSize = 56 - context->size;
311  }
312  else
313  {
314  paddingSize = 64 + 56 - context->size;
315  }
316 
317  //Append padding
318  sha1Update(context, padding, paddingSize);
319 
320  //Append the length of the original message
321  context->w[14] = htobe32((uint32_t) (totalSize >> 32));
322  context->w[15] = htobe32((uint32_t) totalSize);
323 
324  //Calculate the message digest
325  hashProcessData(SHA_MR_ALGO_SHA1, context->buffer, 64, context->h);
326 
327  //Copy the resulting digest
328  if(digest != NULL)
329  {
330  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
331  }
332 }
333 
334 
335 /**
336  * @brief Finish the SHA-1 message digest (no padding added)
337  * @param[in] context Pointer to the SHA-1 context
338  * @param[out] digest Calculated digest
339  **/
340 
341 void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
342 {
343  //Copy the resulting digest
344  osMemcpy(digest, context->digest, SHA1_DIGEST_SIZE);
345 }
346 
347 #endif
348 #if (SHA224_SUPPORT == ENABLED)
349 
350 /**
351  * @brief Initialize SHA-224 message digest context
352  * @param[in] context Pointer to the SHA-224 context to initialize
353  **/
354 
355 void sha224Init(Sha224Context *context)
356 {
357  //Set initial hash value
358  context->h[0] = BETOH32(0xC1059ED8);
359  context->h[1] = BETOH32(0x367CD507);
360  context->h[2] = BETOH32(0x3070DD17);
361  context->h[3] = BETOH32(0xF70E5939);
362  context->h[4] = BETOH32(0xFFC00B31);
363  context->h[5] = BETOH32(0x68581511);
364  context->h[6] = BETOH32(0x64F98FA7);
365  context->h[7] = BETOH32(0xBEFA4FA4);
366 
367  //Number of bytes in the buffer
368  context->size = 0;
369  //Total length of the message
370  context->totalSize = 0;
371 }
372 
373 #endif
374 #if (SHA256_SUPPORT == ENABLED)
375 
376 /**
377  * @brief Initialize SHA-256 message digest context
378  * @param[in] context Pointer to the SHA-256 context to initialize
379  **/
380 
381 void sha256Init(Sha256Context *context)
382 {
383  //Set initial hash value
384  context->h[0] = BETOH32(0x6A09E667);
385  context->h[1] = BETOH32(0xBB67AE85);
386  context->h[2] = BETOH32(0x3C6EF372);
387  context->h[3] = BETOH32(0xA54FF53A);
388  context->h[4] = BETOH32(0x510E527F);
389  context->h[5] = BETOH32(0x9B05688C);
390  context->h[6] = BETOH32(0x1F83D9AB);
391  context->h[7] = BETOH32(0x5BE0CD19);
392 
393  //Number of bytes in the buffer
394  context->size = 0;
395  //Total length of the message
396  context->totalSize = 0;
397 }
398 
399 
400 /**
401  * @brief Update the SHA-256 context with a portion of the message being hashed
402  * @param[in] context Pointer to the SHA-256 context
403  * @param[in] data Pointer to the buffer being hashed
404  * @param[in] length Length of the buffer
405  **/
406 
407 void sha256Update(Sha256Context *context, const void *data, size_t length)
408 {
409  size_t n;
410 
411  //Process the incoming data
412  while(length > 0)
413  {
414  //Check whether some data is pending in the buffer
415  if(context->size == 0 && length >= 64)
416  {
417  //The length must be a multiple of 64 bytes
418  n = length - (length % 64);
419 
420  //Update hash value
421  hashProcessData(SHA_MR_ALGO_SHA256, data, n, context->h);
422 
423  //Update the SHA-256 context
424  context->totalSize += n;
425  //Advance the data pointer
426  data = (uint8_t *) data + n;
427  //Remaining bytes to process
428  length -= n;
429  }
430  else
431  {
432  //The buffer can hold at most 64 bytes
433  n = MIN(length, 64 - context->size);
434 
435  //Copy the data to the buffer
436  osMemcpy(context->buffer + context->size, data, n);
437 
438  //Update the SHA-256 context
439  context->size += n;
440  context->totalSize += n;
441  //Advance the data pointer
442  data = (uint8_t *) data + n;
443  //Remaining bytes to process
444  length -= n;
445 
446  //Check whether the buffer is full
447  if(context->size == 64)
448  {
449  //Update hash value
450  hashProcessData(SHA_MR_ALGO_SHA256, context->buffer, context->size,
451  context->h);
452 
453  //Empty the buffer
454  context->size = 0;
455  }
456  }
457  }
458 }
459 
460 
461 /**
462  * @brief Finish the SHA-256 message digest
463  * @param[in] context Pointer to the SHA-256 context
464  * @param[out] digest Calculated digest (optional parameter)
465  **/
466 
467 void sha256Final(Sha256Context *context, uint8_t *digest)
468 {
469  size_t paddingSize;
470  uint64_t totalSize;
471 
472  //Length of the original message (before padding)
473  totalSize = context->totalSize * 8;
474 
475  //Pad the message so that its length is congruent to 56 modulo 64
476  if(context->size < 56)
477  {
478  paddingSize = 56 - context->size;
479  }
480  else
481  {
482  paddingSize = 64 + 56 - context->size;
483  }
484 
485  //Append padding
486  sha256Update(context, padding, paddingSize);
487 
488  //Append the length of the original message
489  context->w[14] = htobe32((uint32_t) (totalSize >> 32));
490  context->w[15] = htobe32((uint32_t) totalSize);
491 
492  //Calculate the message digest
493  hashProcessData(SHA_MR_ALGO_SHA256, context->buffer, 64, context->h);
494 
495  //Copy the resulting digest
496  if(digest != NULL)
497  {
498  osMemcpy(digest, context->digest, SHA256_DIGEST_SIZE);
499  }
500 }
501 
502 
503 /**
504  * @brief Finish the SHA-256 message digest (no padding added)
505  * @param[in] context Pointer to the SHA-256 context
506  * @param[out] digest Calculated digest
507  **/
508 
509 void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
510 {
511  //Copy the resulting digest
512  osMemcpy(digest, context->digest, SHA256_DIGEST_SIZE);
513 }
514 
515 #endif
516 #if (SHA384_SUPPORT == ENABLED)
517 
518 /**
519  * @brief Initialize SHA-384 message digest context
520  * @param[in] context Pointer to the SHA-384 context to initialize
521  **/
522 
523 void sha384Init(Sha384Context *context)
524 {
525  //Set initial hash value
526  context->h[0] = BETOH64(0xCBBB9D5DC1059ED8);
527  context->h[1] = BETOH64(0x629A292A367CD507);
528  context->h[2] = BETOH64(0x9159015A3070DD17);
529  context->h[3] = BETOH64(0x152FECD8F70E5939);
530  context->h[4] = BETOH64(0x67332667FFC00B31);
531  context->h[5] = BETOH64(0x8EB44A8768581511);
532  context->h[6] = BETOH64(0xDB0C2E0D64F98FA7);
533  context->h[7] = BETOH64(0x47B5481DBEFA4FA4);
534 
535  //Number of bytes in the buffer
536  context->size = 0;
537  //Total length of the message
538  context->totalSize = 0;
539 }
540 
541 
542 /**
543  * @brief Finish the SHA-384 message digest (no padding added)
544  * @param[in] context Pointer to the SHA-384 context
545  * @param[out] digest Calculated digest
546  **/
547 
548 void sha384FinalRaw(Sha384Context *context, uint8_t *digest)
549 {
550  //Copy the resulting digest
551  osMemcpy(digest, context->digest, SHA384_DIGEST_SIZE);
552 }
553 
554 #endif
555 #if (SHA512_SUPPORT == ENABLED)
556 
557 /**
558  * @brief Initialize SHA-512 message digest context
559  * @param[in] context Pointer to the SHA-512 context to initialize
560  **/
561 
562 void sha512Init(Sha512Context *context)
563 {
564  //Set initial hash value
565  context->h[0] = BETOH64(0x6A09E667F3BCC908);
566  context->h[1] = BETOH64(0xBB67AE8584CAA73B);
567  context->h[2] = BETOH64(0x3C6EF372FE94F82B);
568  context->h[3] = BETOH64(0xA54FF53A5F1D36F1);
569  context->h[4] = BETOH64(0x510E527FADE682D1);
570  context->h[5] = BETOH64(0x9B05688C2B3E6C1F);
571  context->h[6] = BETOH64(0x1F83D9ABFB41BD6B);
572  context->h[7] = BETOH64(0x5BE0CD19137E2179);
573 
574  //Number of bytes in the buffer
575  context->size = 0;
576  //Total length of the message
577  context->totalSize = 0;
578 }
579 
580 
581 /**
582  * @brief Update the SHA-512 context with a portion of the message being hashed
583  * @param[in] context Pointer to the SHA-512 context
584  * @param[in] data Pointer to the buffer being hashed
585  * @param[in] length Length of the buffer
586  **/
587 
588 void sha512Update(Sha512Context *context, const void *data, size_t length)
589 {
590  size_t n;
591 
592  //Process the incoming data
593  while(length > 0)
594  {
595  //Check whether some data is pending in the buffer
596  if(context->size == 0 && length >= 128)
597  {
598  //The length must be a multiple of 128 bytes
599  n = length - (length % 128);
600 
601  //Update hash value
602  hashProcessData(SHA_MR_ALGO_SHA512, data, n, (uint32_t *) context->h);
603 
604  //Update the SHA-512 context
605  context->totalSize += n;
606  //Advance the data pointer
607  data = (uint8_t *) data + n;
608  //Remaining bytes to process
609  length -= n;
610  }
611  else
612  {
613  //The buffer can hold at most 128 bytes
614  n = MIN(length, 128 - context->size);
615 
616  //Copy the data to the buffer
617  osMemcpy(context->buffer + context->size, data, n);
618 
619  //Update the SHA-512 context
620  context->size += n;
621  context->totalSize += n;
622  //Advance the data pointer
623  data = (uint8_t *) data + n;
624  //Remaining bytes to process
625  length -= n;
626 
627  //Check whether the buffer is full
628  if(context->size == 128)
629  {
630  //Update hash value
631  hashProcessData(SHA_MR_ALGO_SHA512, context->buffer, context->size,
632  (uint32_t *) context->h);
633 
634  //Empty the buffer
635  context->size = 0;
636  }
637  }
638  }
639 }
640 
641 
642 /**
643  * @brief Finish the SHA-512 message digest
644  * @param[in] context Pointer to the SHA-512 context
645  * @param[out] digest Calculated digest (optional parameter)
646  **/
647 
648 void sha512Final(Sha512Context *context, uint8_t *digest)
649 {
650  size_t paddingSize;
651  uint64_t totalSize;
652 
653  //Length of the original message (before padding)
654  totalSize = context->totalSize * 8;
655 
656  //Pad the message so that its length is congruent to 112 modulo 128
657  if(context->size < 112)
658  {
659  paddingSize = 112 - context->size;
660  }
661  else
662  {
663  paddingSize = 128 + 112 - context->size;
664  }
665 
666  //Append padding
667  sha512Update(context, padding, paddingSize);
668 
669  //Append the length of the original message
670  context->w[14] = 0;
671  context->w[15] = htobe64(totalSize);
672 
673  //Calculate the message digest
674  hashProcessData(SHA_MR_ALGO_SHA512, context->buffer, 128,
675  (uint32_t *) context->h);
676 
677  //Copy the resulting digest
678  if(digest != NULL)
679  {
680  osMemcpy(digest, context->digest, SHA512_DIGEST_SIZE);
681  }
682 }
683 
684 #endif
685 #if (SHA512_224_SUPPORT == ENABLED)
686 
687 /**
688  * @brief Initialize SHA-512/224 message digest context
689  * @param[in] context Pointer to the SHA-512/224 context to initialize
690  **/
691 
693 {
694  //Set initial hash value
695  context->h[0] = BETOH64(0x8C3D37C819544DA2);
696  context->h[1] = BETOH64(0x73E1996689DCD4D6);
697  context->h[2] = BETOH64(0x1DFAB7AE32FF9C82);
698  context->h[3] = BETOH64(0x679DD514582F9FCF);
699  context->h[4] = BETOH64(0x0F6D2B697BD44DA8);
700  context->h[5] = BETOH64(0x77E36F7304C48942);
701  context->h[6] = BETOH64(0x3F9D85A86A1D36C8);
702  context->h[7] = BETOH64(0x1112E6AD91D692A1);
703 
704  //Number of bytes in the buffer
705  context->size = 0;
706  //Total length of the message
707  context->totalSize = 0;
708 }
709 
710 #endif
711 #if (SHA512_256_SUPPORT == ENABLED)
712 
713 /**
714  * @brief Initialize SHA-512/256 message digest context
715  * @param[in] context Pointer to the SHA-512/256 context to initialize
716  **/
717 
719 {
720  //Set initial hash value
721  context->h[0] = BETOH64(0x22312194FC2BF72C);
722  context->h[1] = BETOH64(0x9F555FA3C84C64C2);
723  context->h[2] = BETOH64(0x2393B86B6F53B151);
724  context->h[3] = BETOH64(0x963877195940EABD);
725  context->h[4] = BETOH64(0x96283EE2A88EFFE3);
726  context->h[5] = BETOH64(0xBE5E1E2553863992);
727  context->h[6] = BETOH64(0x2B0199FC2C85B8AA);
728  context->h[7] = BETOH64(0x0EB72DDC81C52CA2);
729 
730  //Number of bytes in the buffer
731  context->size = 0;
732  //Total length of the message
733  context->totalSize = 0;
734 }
735 
736 #endif
737 #endif
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void sha1FinalRaw(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest (no padding added)
void hashProcessData(uint32_t algo, const uint8_t *data, size_t length, uint32_t *h)
Update hash value.
void sha224Init(Sha224Context *context)
Initialize SHA-224 message digest context.
PIC32CX MTC/MTG/MTSH hash hardware accelerator.
SHA-256 algorithm context.
Definition: sha256.h:62
uint8_t p
Definition: ndp.h:300
uint64_t w[16]
Definition: sha512.h:70
uint8_t data[]
Definition: ethernet.h:222
void sha512Final(Sha512Context *context, uint8_t *digest)
Finish the SHA-512 message digest.
void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest (no padding added)
size_t size
Definition: sha256.h:73
uint32_t h[8]
Definition: sha256.h:65
#define BETOH32(value)
Definition: cpu_endian.h:451
void sha384FinalRaw(Sha384Context *context, uint8_t *digest)
Finish the SHA-384 message digest (no padding added)
uint64_t totalSize
Definition: sha1.h:74
void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
size_t size
Definition: sha512.h:73
uint8_t h
Definition: ndp.h:302
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
uint32_t w[16]
Definition: sha256.h:70
void sha512Update(Sha512Context *context, const void *data, size_t length)
Update the SHA-512 context with a portion of the message being hashed.
void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.
SHA-512 algorithm context.
Definition: sha512.h:62
uint32_t h[5]
Definition: sha1.h:65
General definitions for cryptographic algorithms.
uint8_t buffer[128]
Definition: sha512.h:71
void sha384Init(Sha384Context *context)
Initialize SHA-384 message digest context.
#define htobe64(value)
Definition: cpu_endian.h:447
uint8_t length
Definition: tcp.h:368
uint8_t buffer[64]
Definition: sha256.h:71
void sha512Init(Sha512Context *context)
Initialize SHA-512 message digest context.
#define MIN(a, b)
Definition: os_port.h:63
#define SHA384_DIGEST_SIZE
Definition: sha384.h:41
PIC32CX MTC/MTG/MTSH hardware cryptographic accelerator.
Collection of hash algorithms.
#define htobe32(value)
Definition: cpu_endian.h:446
void sha512_224Init(Sha512_224Context *context)
Initialize SHA-512/224 message digest context.
uint8_t digest[32]
Definition: sha256.h:66
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
uint64_t h[8]
Definition: sha512.h:65
uint8_t n
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
uint8_t digest[64]
Definition: sha512.h:66
void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
size_t size
Definition: sha1.h:73
#define BETOH64(value)
Definition: cpu_endian.h:452
void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
uint32_t w[16]
Definition: sha1.h:70
SHA-1 algorithm context.
Definition: sha1.h:62
OsMutex pic32cxmtCryptoMutex
uint8_t buffer[64]
Definition: sha1.h:71
void sha512_256Init(Sha512_256Context *context)
Initialize SHA-512/256 message digest context.
uint64_t totalSize
Definition: sha512.h:74
uint64_t totalSize
Definition: sha256.h:74
void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
uint8_t digest[20]
Definition: sha1.h:66
#define SHA512_DIGEST_SIZE
Definition: sha512.h:45
Debugging facilities.