32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
43 #if (PIC32CX_MT_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
74 blockSize = (algo == SHA_MR_ALGO_SHA512) ? 128 : 64;
80 SHA_REGS->SHA_CR = SHA_CR_SWRST_Msk;
83 SHA_REGS->SHA_MR = SHA_MR_UIHV_Msk | SHA_MR_SMOD_MANUAL_START | algo;
86 SHA_REGS->SHA_CR = SHA_CR_WUIHV_Msk;
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];
96 if(algo == SHA_MR_ALGO_SHA256 || algo == SHA_MR_ALGO_SHA512)
98 SHA_REGS->SHA_IDATAR[5] =
h[5];
99 SHA_REGS->SHA_IDATAR[6] =
h[6];
100 SHA_REGS->SHA_IDATAR[7] =
h[7];
104 if(algo == SHA_MR_ALGO_SHA512)
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];
118 SHA_REGS->SHA_CR = SHA_CR_FIRST_Msk;
121 while(
length >= blockSize)
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];
143 if(algo == SHA_MR_ALGO_SHA512)
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];
164 SHA_REGS->SHA_CR = SHA_CR_START_Msk;
167 while((SHA_REGS->SHA_ISR & SHA_ISR_DATRDY_Msk) == 0)
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];
184 if(algo == SHA_MR_ALGO_SHA256 || algo == SHA_MR_ALGO_SHA512)
186 h[5] = SHA_REGS->SHA_IODATAR[5];
187 h[6] = SHA_REGS->SHA_IODATAR[6];
188 h[7] = SHA_REGS->SHA_IODATAR[7];
192 if(algo == SHA_MR_ALGO_SHA512)
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];
209 #if (SHA1_SUPPORT == ENABLED)
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);
279 if(context->
size == 64)
308 if(context->
size < 56)
310 paddingSize = 56 - context->
size;
314 paddingSize = 64 + 56 - context->
size;
321 context->
w[14] =
htobe32((uint32_t) (totalSize >> 32));
322 context->
w[15] =
htobe32((uint32_t) totalSize);
348 #if (SHA224_SUPPORT == ENABLED)
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);
374 #if (SHA256_SUPPORT == ENABLED)
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);
447 if(context->
size == 64)
476 if(context->
size < 56)
478 paddingSize = 56 - context->
size;
482 paddingSize = 64 + 56 - context->
size;
489 context->
w[14] =
htobe32((uint32_t) (totalSize >> 32));
490 context->
w[15] =
htobe32((uint32_t) totalSize);
516 #if (SHA384_SUPPORT == ENABLED)
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);
555 #if (SHA512_SUPPORT == ENABLED)
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);
628 if(context->
size == 128)
632 (uint32_t *) context->
h);
657 if(context->
size < 112)
659 paddingSize = 112 - context->
size;
663 paddingSize = 128 + 112 - context->
size;
671 context->
w[15] =
htobe64(totalSize);
675 (uint32_t *) context->
h);
685 #if (SHA512_224_SUPPORT == ENABLED)
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);
711 #if (SHA512_256_SUPPORT == ENABLED)
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);