32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "esp_crypto_lock.h"
36 #include "hal/sha_types.h"
37 #include "soc/hwcrypto_reg.h"
38 #include "esp_private/periph_ctrl.h"
46 #if (ESP32_S2_CRYPTO_HASH_SUPPORT == ENABLED)
49 static const uint8_t padding[128] =
51 0x80, 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 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
89 esp_crypto_dma_lock_acquire();
91 periph_module_enable(PERIPH_SHA_MODULE);
94 REG_WRITE(SHA_MODE_REG, algo);
97 REG_WRITE(SHA_H_BASE,
h[0]);
98 REG_WRITE(SHA_H_BASE + 4,
h[1]);
99 REG_WRITE(SHA_H_BASE + 8,
h[2]);
100 REG_WRITE(SHA_H_BASE + 12,
h[3]);
101 REG_WRITE(SHA_H_BASE + 16,
h[4]);
106 REG_WRITE(SHA_H_BASE + 20,
h[5]);
107 REG_WRITE(SHA_H_BASE + 24,
h[6]);
108 REG_WRITE(SHA_H_BASE + 28,
h[7]);
114 REG_WRITE(SHA_H_BASE + 32,
h[8]);
115 REG_WRITE(SHA_H_BASE + 36,
h[9]);
116 REG_WRITE(SHA_H_BASE + 40,
h[10]);
117 REG_WRITE(SHA_H_BASE + 44,
h[11]);
118 REG_WRITE(SHA_H_BASE + 48,
h[12]);
119 REG_WRITE(SHA_H_BASE + 52,
h[13]);
120 REG_WRITE(SHA_H_BASE + 56,
h[14]);
121 REG_WRITE(SHA_H_BASE + 60,
h[15]);
125 while(
length >= blockSize)
129 REG_WRITE(SHA_TEXT_BASE, temp);
131 REG_WRITE(SHA_TEXT_BASE + 4, temp);
133 REG_WRITE(SHA_TEXT_BASE + 8, temp);
135 REG_WRITE(SHA_TEXT_BASE + 12, temp);
137 REG_WRITE(SHA_TEXT_BASE + 16, temp);
139 REG_WRITE(SHA_TEXT_BASE + 20, temp);
141 REG_WRITE(SHA_TEXT_BASE + 24, temp);
143 REG_WRITE(SHA_TEXT_BASE + 28, temp);
145 REG_WRITE(SHA_TEXT_BASE + 32, temp);
147 REG_WRITE(SHA_TEXT_BASE + 36, temp);
149 REG_WRITE(SHA_TEXT_BASE + 40, temp);
151 REG_WRITE(SHA_TEXT_BASE + 44, temp);
153 REG_WRITE(SHA_TEXT_BASE + 48, temp);
155 REG_WRITE(SHA_TEXT_BASE + 52, temp);
157 REG_WRITE(SHA_TEXT_BASE + 56, temp);
159 REG_WRITE(SHA_TEXT_BASE + 60, temp);
165 REG_WRITE(SHA_TEXT_BASE + 64, temp);
167 REG_WRITE(SHA_TEXT_BASE + 68, temp);
169 REG_WRITE(SHA_TEXT_BASE + 72, temp);
171 REG_WRITE(SHA_TEXT_BASE + 76, temp);
173 REG_WRITE(SHA_TEXT_BASE + 80, temp);
175 REG_WRITE(SHA_TEXT_BASE + 84, temp);
177 REG_WRITE(SHA_TEXT_BASE + 88, temp);
179 REG_WRITE(SHA_TEXT_BASE + 92, temp);
181 REG_WRITE(SHA_TEXT_BASE + 96, temp);
183 REG_WRITE(SHA_TEXT_BASE + 100, temp);
185 REG_WRITE(SHA_TEXT_BASE + 104, temp);
187 REG_WRITE(SHA_TEXT_BASE + 108, temp);
189 REG_WRITE(SHA_TEXT_BASE + 112, temp);
191 REG_WRITE(SHA_TEXT_BASE + 116, temp);
193 REG_WRITE(SHA_TEXT_BASE + 120, temp);
195 REG_WRITE(SHA_TEXT_BASE + 124, temp);
199 REG_WRITE(SHA_CONTINUE_REG, 1);
202 while(REG_READ(SHA_BUSY_REG) != 0)
212 h[0] = REG_READ(SHA_H_BASE);
213 h[1] = REG_READ(SHA_H_BASE + 4);
214 h[2] = REG_READ(SHA_H_BASE + 8);
215 h[3] = REG_READ(SHA_H_BASE + 12);
216 h[4] = REG_READ(SHA_H_BASE + 16);
221 h[5] = REG_READ(SHA_H_BASE + 20);
222 h[6] = REG_READ(SHA_H_BASE + 24);
223 h[7] = REG_READ(SHA_H_BASE + 28);
229 h[8] = REG_READ(SHA_H_BASE + 32);
230 h[9] = REG_READ(SHA_H_BASE + 36);
231 h[10] = REG_READ(SHA_H_BASE + 40);
232 h[11] = REG_READ(SHA_H_BASE + 44);
233 h[12] = REG_READ(SHA_H_BASE + 48);
234 h[13] = REG_READ(SHA_H_BASE + 52);
235 h[14] = REG_READ(SHA_H_BASE + 56);
236 h[15] = REG_READ(SHA_H_BASE + 60);
240 periph_module_disable(PERIPH_SHA_MODULE);
242 esp_crypto_dma_lock_release();
246 #if (SHA1_SUPPORT == ENABLED)
256 context->
h[0] =
BETOH32(0x67452301);
257 context->
h[1] =
BETOH32(0xEFCDAB89);
258 context->
h[2] =
BETOH32(0x98BADCFE);
259 context->
h[3] =
BETOH32(0x10325476);
260 context->
h[4] =
BETOH32(0xC3D2E1F0);
316 if(context->
size == 64)
345 if(context->
size < 56)
347 paddingSize = 56 - context->
size;
351 paddingSize = 64 + 56 - context->
size;
358 context->
w[14] =
htobe32((uint32_t) (totalSize >> 32));
359 context->
w[15] =
htobe32((uint32_t) totalSize);
385 #if (SHA224_SUPPORT == ENABLED)
395 context->
h[0] =
BETOH32(0xC1059ED8);
396 context->
h[1] =
BETOH32(0x367CD507);
397 context->
h[2] =
BETOH32(0x3070DD17);
398 context->
h[3] =
BETOH32(0xF70E5939);
399 context->
h[4] =
BETOH32(0xFFC00B31);
400 context->
h[5] =
BETOH32(0x68581511);
401 context->
h[6] =
BETOH32(0x64F98FA7);
402 context->
h[7] =
BETOH32(0xBEFA4FA4);
411 #if (SHA256_SUPPORT == ENABLED)
421 context->
h[0] =
BETOH32(0x6A09E667);
422 context->
h[1] =
BETOH32(0xBB67AE85);
423 context->
h[2] =
BETOH32(0x3C6EF372);
424 context->
h[3] =
BETOH32(0xA54FF53A);
425 context->
h[4] =
BETOH32(0x510E527F);
426 context->
h[5] =
BETOH32(0x9B05688C);
427 context->
h[6] =
BETOH32(0x1F83D9AB);
428 context->
h[7] =
BETOH32(0x5BE0CD19);
484 if(context->
size == 64)
513 if(context->
size < 56)
515 paddingSize = 56 - context->
size;
519 paddingSize = 64 + 56 - context->
size;
526 context->
w[14] =
htobe32((uint32_t) (totalSize >> 32));
527 context->
w[15] =
htobe32((uint32_t) totalSize);
553 #if (SHA384_SUPPORT == ENABLED)
563 context->
h[0] =
BETOH64(0xCBBB9D5DC1059ED8);
564 context->
h[1] =
BETOH64(0x629A292A367CD507);
565 context->
h[2] =
BETOH64(0x9159015A3070DD17);
566 context->
h[3] =
BETOH64(0x152FECD8F70E5939);
567 context->
h[4] =
BETOH64(0x67332667FFC00B31);
568 context->
h[5] =
BETOH64(0x8EB44A8768581511);
569 context->
h[6] =
BETOH64(0xDB0C2E0D64F98FA7);
570 context->
h[7] =
BETOH64(0x47B5481DBEFA4FA4);
592 #if (SHA512_SUPPORT == ENABLED)
602 context->
h[0] =
BETOH64(0x6A09E667F3BCC908);
603 context->
h[1] =
BETOH64(0xBB67AE8584CAA73B);
604 context->
h[2] =
BETOH64(0x3C6EF372FE94F82B);
605 context->
h[3] =
BETOH64(0xA54FF53A5F1D36F1);
606 context->
h[4] =
BETOH64(0x510E527FADE682D1);
607 context->
h[5] =
BETOH64(0x9B05688C2B3E6C1F);
608 context->
h[6] =
BETOH64(0x1F83D9ABFB41BD6B);
609 context->
h[7] =
BETOH64(0x5BE0CD19137E2179);
665 if(context->
size == 128)
669 (uint32_t *) context->
h);
694 if(context->
size < 112)
696 paddingSize = 112 - context->
size;
700 paddingSize = 128 + 112 - context->
size;
708 context->
w[15] =
htobe64(totalSize);
712 (uint32_t *) context->
h);
722 #if (SHA512_224_SUPPORT == ENABLED)
732 context->
h[0] =
BETOH64(0x8C3D37C819544DA2);
733 context->
h[1] =
BETOH64(0x73E1996689DCD4D6);
734 context->
h[2] =
BETOH64(0x1DFAB7AE32FF9C82);
735 context->
h[3] =
BETOH64(0x679DD514582F9FCF);
736 context->
h[4] =
BETOH64(0x0F6D2B697BD44DA8);
737 context->
h[5] =
BETOH64(0x77E36F7304C48942);
738 context->
h[6] =
BETOH64(0x3F9D85A86A1D36C8);
739 context->
h[7] =
BETOH64(0x1112E6AD91D692A1);
748 #if (SHA512_384_SUPPORT == ENABLED)
758 context->
h[0] =
BETOH64(0x22312194FC2BF72C);
759 context->
h[1] =
BETOH64(0x9F555FA3C84C64C2);
760 context->
h[2] =
BETOH64(0x2393B86B6F53B151);
761 context->
h[3] =
BETOH64(0x963877195940EABD);
762 context->
h[4] =
BETOH64(0x96283EE2A88EFFE3);
763 context->
h[5] =
BETOH64(0xBE5E1E2553863992);
764 context->
h[6] =
BETOH64(0x2B0199FC2C85B8AA);
765 context->
h[7] =
BETOH64(0x0EB72DDC81C52CA2);