32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
43 #if (SAM9X7_CRYPTO_HASH_SUPPORT == ENABLED)
46 static const uint8_t padding[128] =
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
73 blockSize = (algo == SHA_MR_ALGO_SHA512) ? 128 : 64;
79 SHA_REGS->SHA_CR = SHA_CR_SWRST_Msk;
82 SHA_REGS->SHA_MR = SHA_MR_UIHV_Msk | SHA_MR_SMOD_MANUAL_START | algo;
85 SHA_REGS->SHA_CR = SHA_CR_WUIHV_Msk;
88 SHA_REGS->SHA_IDATAR[0] =
h[0];
89 SHA_REGS->SHA_IDATAR[1] =
h[1];
90 SHA_REGS->SHA_IDATAR[2] =
h[2];
91 SHA_REGS->SHA_IDATAR[3] =
h[3];
92 SHA_REGS->SHA_IDATAR[4] =
h[4];
95 if(algo == SHA_MR_ALGO_SHA256 || algo == SHA_MR_ALGO_SHA512)
97 SHA_REGS->SHA_IDATAR[5] =
h[5];
98 SHA_REGS->SHA_IDATAR[6] =
h[6];
99 SHA_REGS->SHA_IDATAR[7] =
h[7];
103 if(algo == SHA_MR_ALGO_SHA512)
105 SHA_REGS->SHA_IDATAR[8] =
h[8];
106 SHA_REGS->SHA_IDATAR[9] =
h[9];
107 SHA_REGS->SHA_IDATAR[10] =
h[10];
108 SHA_REGS->SHA_IDATAR[11] =
h[11];
109 SHA_REGS->SHA_IDATAR[12] =
h[12];
110 SHA_REGS->SHA_IDATAR[13] =
h[13];
111 SHA_REGS->SHA_IDATAR[14] =
h[14];
112 SHA_REGS->SHA_IDATAR[15] =
h[15];
117 SHA_REGS->SHA_CR = SHA_CR_FIRST_Msk;
120 while(
length >= blockSize)
141 if(algo == SHA_MR_ALGO_SHA512)
162 SHA_REGS->SHA_CR = SHA_CR_START_Msk;
165 while((SHA_REGS->SHA_ISR & SHA_ISR_DATRDY_Msk) == 0)
175 h[0] = SHA_REGS->SHA_IODATAR[0];
176 h[1] = SHA_REGS->SHA_IODATAR[1];
177 h[2] = SHA_REGS->SHA_IODATAR[2];
178 h[3] = SHA_REGS->SHA_IODATAR[3];
179 h[4] = SHA_REGS->SHA_IODATAR[4];
182 if(algo == SHA_MR_ALGO_SHA256 || algo == SHA_MR_ALGO_SHA512)
184 h[5] = SHA_REGS->SHA_IODATAR[5];
185 h[6] = SHA_REGS->SHA_IODATAR[6];
186 h[7] = SHA_REGS->SHA_IODATAR[7];
190 if(algo == SHA_MR_ALGO_SHA512)
192 h[8] = SHA_REGS->SHA_IODATAR[8];
193 h[9] = SHA_REGS->SHA_IODATAR[9];
194 h[10] = SHA_REGS->SHA_IODATAR[10];
195 h[11] = SHA_REGS->SHA_IODATAR[11];
196 h[12] = SHA_REGS->SHA_IODATAR[12];
197 h[13] = SHA_REGS->SHA_IODATAR[13];
198 h[14] = SHA_REGS->SHA_IODATAR[14];
199 h[15] = SHA_REGS->SHA_IODATAR[15];
207 #if (SHA1_SUPPORT == ENABLED)
217 context->
h[0] =
BETOH32(0x67452301);
218 context->
h[1] =
BETOH32(0xEFCDAB89);
219 context->
h[2] =
BETOH32(0x98BADCFE);
220 context->
h[3] =
BETOH32(0x10325476);
221 context->
h[4] =
BETOH32(0xC3D2E1F0);
277 if(context->
size == 64)
306 if(context->
size < 56)
308 paddingSize = 56 - context->
size;
312 paddingSize = 64 + 56 - context->
size;
319 context->
w[14] =
htobe32((uint32_t) (totalSize >> 32));
320 context->
w[15] =
htobe32((uint32_t) totalSize);
346 #if (SHA224_SUPPORT == ENABLED)
356 context->
h[0] =
BETOH32(0xC1059ED8);
357 context->
h[1] =
BETOH32(0x367CD507);
358 context->
h[2] =
BETOH32(0x3070DD17);
359 context->
h[3] =
BETOH32(0xF70E5939);
360 context->
h[4] =
BETOH32(0xFFC00B31);
361 context->
h[5] =
BETOH32(0x68581511);
362 context->
h[6] =
BETOH32(0x64F98FA7);
363 context->
h[7] =
BETOH32(0xBEFA4FA4);
372 #if (SHA256_SUPPORT == ENABLED)
382 context->
h[0] =
BETOH32(0x6A09E667);
383 context->
h[1] =
BETOH32(0xBB67AE85);
384 context->
h[2] =
BETOH32(0x3C6EF372);
385 context->
h[3] =
BETOH32(0xA54FF53A);
386 context->
h[4] =
BETOH32(0x510E527F);
387 context->
h[5] =
BETOH32(0x9B05688C);
388 context->
h[6] =
BETOH32(0x1F83D9AB);
389 context->
h[7] =
BETOH32(0x5BE0CD19);
445 if(context->
size == 64)
474 if(context->
size < 56)
476 paddingSize = 56 - context->
size;
480 paddingSize = 64 + 56 - context->
size;
487 context->
w[14] =
htobe32((uint32_t) (totalSize >> 32));
488 context->
w[15] =
htobe32((uint32_t) totalSize);
514 #if (SHA384_SUPPORT == ENABLED)
524 context->
h[0] =
BETOH64(0xCBBB9D5DC1059ED8);
525 context->
h[1] =
BETOH64(0x629A292A367CD507);
526 context->
h[2] =
BETOH64(0x9159015A3070DD17);
527 context->
h[3] =
BETOH64(0x152FECD8F70E5939);
528 context->
h[4] =
BETOH64(0x67332667FFC00B31);
529 context->
h[5] =
BETOH64(0x8EB44A8768581511);
530 context->
h[6] =
BETOH64(0xDB0C2E0D64F98FA7);
531 context->
h[7] =
BETOH64(0x47B5481DBEFA4FA4);
553 #if (SHA512_SUPPORT == ENABLED)
563 context->
h[0] =
BETOH64(0x6A09E667F3BCC908);
564 context->
h[1] =
BETOH64(0xBB67AE8584CAA73B);
565 context->
h[2] =
BETOH64(0x3C6EF372FE94F82B);
566 context->
h[3] =
BETOH64(0xA54FF53A5F1D36F1);
567 context->
h[4] =
BETOH64(0x510E527FADE682D1);
568 context->
h[5] =
BETOH64(0x9B05688C2B3E6C1F);
569 context->
h[6] =
BETOH64(0x1F83D9ABFB41BD6B);
570 context->
h[7] =
BETOH64(0x5BE0CD19137E2179);
626 if(context->
size == 128)
630 (uint32_t *) context->
h);
655 if(context->
size < 112)
657 paddingSize = 112 - context->
size;
661 paddingSize = 128 + 112 - context->
size;
669 context->
w[15] =
htobe64(totalSize);
673 (uint32_t *) context->
h);
683 #if (SHA512_224_SUPPORT == ENABLED)
693 context->
h[0] =
BETOH64(0x8C3D37C819544DA2);
694 context->
h[1] =
BETOH64(0x73E1996689DCD4D6);
695 context->
h[2] =
BETOH64(0x1DFAB7AE32FF9C82);
696 context->
h[3] =
BETOH64(0x679DD514582F9FCF);
697 context->
h[4] =
BETOH64(0x0F6D2B697BD44DA8);
698 context->
h[5] =
BETOH64(0x77E36F7304C48942);
699 context->
h[6] =
BETOH64(0x3F9D85A86A1D36C8);
700 context->
h[7] =
BETOH64(0x1112E6AD91D692A1);
709 #if (SHA512_256_SUPPORT == ENABLED)
719 context->
h[0] =
BETOH64(0x22312194FC2BF72C);
720 context->
h[1] =
BETOH64(0x9F555FA3C84C64C2);
721 context->
h[2] =
BETOH64(0x2393B86B6F53B151);
722 context->
h[3] =
BETOH64(0x963877195940EABD);
723 context->
h[4] =
BETOH64(0x96283EE2A88EFFE3);
724 context->
h[5] =
BETOH64(0xBE5E1E2553863992);
725 context->
h[6] =
BETOH64(0x2B0199FC2C85B8AA);
726 context->
h[7] =
BETOH64(0x0EB72DDC81C52CA2);