/src/wolfssl-sp-math-all/wolfcrypt/src/sha.c
Line | Count | Source |
1 | | /* sha.c |
2 | | * |
3 | | * Copyright (C) 2006-2025 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
23 | | |
24 | | #ifdef DEBUG_WOLFSSL_VERBOSE |
25 | | #if defined(WOLFSSL_ESPIDF) |
26 | | #include <esp_log.h> |
27 | | #endif |
28 | | #endif |
29 | | |
30 | | #if !defined(NO_SHA) |
31 | | |
32 | | #if FIPS_VERSION3_GE(2,0,0) |
33 | | /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */ |
34 | | #define FIPS_NO_WRAPPERS |
35 | | |
36 | | #ifdef USE_WINDOWS_API |
37 | | #pragma code_seg(".fipsA$k") |
38 | | #pragma const_seg(".fipsB$k") |
39 | | #endif |
40 | | #endif |
41 | | |
42 | | #include <wolfssl/wolfcrypt/sha.h> |
43 | | #include <wolfssl/wolfcrypt/hash.h> |
44 | | |
45 | | #ifdef WOLF_CRYPTO_CB |
46 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
47 | | #endif |
48 | | |
49 | | #ifdef WOLFSSL_IMXRT1170_CAAM |
50 | | #include <wolfssl/wolfcrypt/port/caam/wolfcaam_fsl_nxp.h> |
51 | | #endif |
52 | | |
53 | | #if defined(WOLFSSL_PSOC6_CRYPTO) |
54 | | #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h> |
55 | | #endif |
56 | | |
57 | | /* Assume no hash HW available until supporting HW found. */ |
58 | | #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW |
59 | | |
60 | | #if defined(WOLFSSL_ESP32_CRYPT) && \ |
61 | | !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
62 | | /* define a single keyword for simplicity & readability |
63 | | * |
64 | | * by default the HW acceleration is on for ESP32-WROOM32 |
65 | | * but individual components can be turned off. |
66 | | */ |
67 | | #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW |
68 | | #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h" |
69 | | |
70 | | /* Although we have hardware acceleration, |
71 | | ** we may need to fall back to software */ |
72 | | #define USE_SHA_SOFTWARE_IMPL |
73 | | |
74 | | #elif defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) |
75 | | /* The ESP32C3 is different; HW crypto here. Not yet implemented. |
76 | | ** We'll be using software for RISC-V at this time */ |
77 | | #else |
78 | | #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW |
79 | | #endif |
80 | | |
81 | | #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW |
82 | | #if defined(WOLFSSL_ESP32_CRYPT) && \ |
83 | | !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
84 | | /* define a single keyword for simplicity & readability |
85 | | * |
86 | | * by default the HW acceleration is on for ESP32-WROOM32 |
87 | | * but individual components can be turned off. |
88 | | */ |
89 | | #define WOLFSSL_USE_ESP32_CRYPT_HASH_HW |
90 | | #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h" |
91 | | |
92 | | /* Although we have hardware acceleration, |
93 | | ** we may need to fall back to software */ |
94 | | #define USE_SHA_SOFTWARE_IMPL |
95 | | static const char* TAG = "wc_sha"; |
96 | | #elif defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) |
97 | | /* The ESP32C3 is different; HW crypto here. Not yet implemented. |
98 | | ** We'll be using software for RISC-V at this time */ |
99 | | static const char* TAG = "wc_sha-c3"; |
100 | | #else |
101 | | #undef WOLFSSL_USE_ESP32_CRYPT_HASH_HW |
102 | | #endif |
103 | | |
104 | | #if defined(WOLFSSL_TI_HASH) |
105 | | /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */ |
106 | | |
107 | | #else |
108 | | |
109 | | #ifdef NO_INLINE |
110 | | #include <wolfssl/wolfcrypt/misc.h> |
111 | | #else |
112 | | #define WOLFSSL_MISC_INCLUDED |
113 | | #include <wolfcrypt/src/misc.c> |
114 | | #endif |
115 | | |
116 | | #if FIPS_VERSION3_GE(6,0,0) |
117 | | const unsigned int wolfCrypt_FIPS_sha_ro_sanity[2] = |
118 | | { 0x1a2b3c4d, 0x00000013 }; |
119 | | int wolfCrypt_FIPS_SHA_sanity(void) |
120 | | { |
121 | | return 0; |
122 | | } |
123 | | #endif |
124 | | |
125 | | /* Hardware Acceleration */ |
126 | | #if defined(WOLFSSL_PIC32MZ_HASH) |
127 | | #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h> |
128 | | |
129 | | #elif defined(STM32_HASH) |
130 | | |
131 | | /* Supports CubeMX HAL or Standard Peripheral Library */ |
132 | | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
133 | | { |
134 | | if (sha == NULL) { |
135 | | return BAD_FUNC_ARG; |
136 | | } |
137 | | |
138 | | (void)devId; |
139 | | (void)heap; |
140 | | |
141 | | wc_Stm32_Hash_Init(&sha->stmCtx); |
142 | | |
143 | | return 0; |
144 | | } |
145 | | |
146 | | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
147 | | { |
148 | | int ret; |
149 | | |
150 | | if (sha == NULL || (data == NULL && len > 0)) { |
151 | | return BAD_FUNC_ARG; |
152 | | } |
153 | | |
154 | | ret = wolfSSL_CryptHwMutexLock(); |
155 | | if (ret == 0) { |
156 | | ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1, |
157 | | data, len, WC_SHA_BLOCK_SIZE); |
158 | | wolfSSL_CryptHwMutexUnLock(); |
159 | | } |
160 | | return ret; |
161 | | } |
162 | | |
163 | | int wc_ShaFinal(wc_Sha* sha, byte* hash) |
164 | | { |
165 | | int ret; |
166 | | |
167 | | if (sha == NULL || hash == NULL) { |
168 | | return BAD_FUNC_ARG; |
169 | | } |
170 | | |
171 | | ret = wolfSSL_CryptHwMutexLock(); |
172 | | if (ret == 0) { |
173 | | ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1, |
174 | | hash, WC_SHA_DIGEST_SIZE); |
175 | | wolfSSL_CryptHwMutexUnLock(); |
176 | | } |
177 | | |
178 | | (void)wc_InitSha(sha); /* reset state */ |
179 | | |
180 | | return ret; |
181 | | } |
182 | | |
183 | | |
184 | | #elif defined(FREESCALE_LTC_SHA) |
185 | | |
186 | | #include "fsl_ltc.h" |
187 | | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
188 | | { |
189 | | if (sha == NULL) { |
190 | | return BAD_FUNC_ARG; |
191 | | } |
192 | | |
193 | | (void)devId; |
194 | | (void)heap; |
195 | | |
196 | | LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0); |
197 | | return 0; |
198 | | } |
199 | | |
200 | | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
201 | | { |
202 | | LTC_HASH_Update(&sha->ctx, data, len); |
203 | | return 0; |
204 | | } |
205 | | |
206 | | int wc_ShaFinal(wc_Sha* sha, byte* hash) |
207 | | { |
208 | | word32 hashlen = WC_SHA_DIGEST_SIZE; |
209 | | LTC_HASH_Finish(&sha->ctx, hash, &hashlen); |
210 | | return wc_InitSha(sha); /* reset state */ |
211 | | } |
212 | | |
213 | | |
214 | | #elif defined(FREESCALE_MMCAU_SHA) |
215 | | |
216 | | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
217 | | #include "cau_api.h" |
218 | | #else |
219 | | #include "fsl_mmcau.h" |
220 | | #endif |
221 | | |
222 | | #define USE_SHA_SOFTWARE_IMPL /* Only for API's, actual transform is here */ |
223 | | |
224 | | #define XTRANSFORM(S,B) Transform((S),(B)) |
225 | | #define XTRANSFORM_LEN(S,B,L) Transform_Len((S),(B),(L)) |
226 | | |
227 | | #ifndef WC_HASH_DATA_ALIGNMENT |
228 | | /* these hardware API's require 4 byte (word32) alignment */ |
229 | | #define WC_HASH_DATA_ALIGNMENT 4 |
230 | | #endif |
231 | | |
232 | | static int InitSha(wc_Sha* sha) |
233 | | { |
234 | | int ret = 0; |
235 | | ret = wolfSSL_CryptHwMutexLock(); |
236 | | if (ret != 0) { |
237 | | return ret; |
238 | | } |
239 | | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
240 | | cau_sha1_initialize_output(sha->digest); |
241 | | #else |
242 | | MMCAU_SHA1_InitializeOutput((word32*)sha->digest); |
243 | | #endif |
244 | | wolfSSL_CryptHwMutexUnLock(); |
245 | | |
246 | | sha->buffLen = 0; |
247 | | sha->loLen = 0; |
248 | | sha->hiLen = 0; |
249 | | |
250 | | return ret; |
251 | | } |
252 | | |
253 | | static int Transform(wc_Sha* sha, const byte* data) |
254 | | { |
255 | | int ret = wolfSSL_CryptHwMutexLock(); |
256 | | if (ret == 0) { |
257 | | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
258 | | cau_sha1_hash_n((byte*)data, 1, sha->digest); |
259 | | #else |
260 | | MMCAU_SHA1_HashN((byte*)data, 1, (word32*)sha->digest); |
261 | | #endif |
262 | | wolfSSL_CryptHwMutexUnLock(); |
263 | | } |
264 | | return ret; |
265 | | } |
266 | | |
267 | | static int Transform_Len(wc_Sha* sha, const byte* data, word32 len) |
268 | | { |
269 | | int ret = wolfSSL_CryptHwMutexLock(); |
270 | | if (ret == 0) { |
271 | | #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0 |
272 | | if ((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) { |
273 | | /* data pointer is NOT aligned, |
274 | | * so copy and perform one block at a time */ |
275 | | byte* local = (byte*)sha->buffer; |
276 | | while (len >= WC_SHA_BLOCK_SIZE) { |
277 | | XMEMCPY(local, data, WC_SHA_BLOCK_SIZE); |
278 | | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
279 | | cau_sha1_hash_n(local, 1, sha->digest); |
280 | | #else |
281 | | MMCAU_SHA1_HashN(local, 1, sha->digest); |
282 | | #endif |
283 | | data += WC_SHA_BLOCK_SIZE; |
284 | | len -= WC_SHA_BLOCK_SIZE; |
285 | | } |
286 | | } |
287 | | else |
288 | | #endif |
289 | | { |
290 | | #ifdef FREESCALE_MMCAU_CLASSIC_SHA |
291 | | cau_sha1_hash_n((byte*)data, len/WC_SHA_BLOCK_SIZE, sha->digest); |
292 | | #else |
293 | | MMCAU_SHA1_HashN((byte*)data, len/WC_SHA_BLOCK_SIZE, |
294 | | (word32*)sha->digest); |
295 | | #endif |
296 | | } |
297 | | wolfSSL_CryptHwMutexUnLock(); |
298 | | } |
299 | | return ret; |
300 | | } |
301 | | |
302 | | #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH) && \ |
303 | | !defined(WOLFSSL_QNX_CAAM) |
304 | | /* wolfcrypt/src/port/caam/caam_sha.c */ |
305 | | |
306 | | #elif defined(MAX3266X_SHA) |
307 | | /* Already brought in by sha.h */ |
308 | | /* #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> */ |
309 | | |
310 | | #elif defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) || \ |
311 | | defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) |
312 | | |
313 | | /* This function initializes SHA. |
314 | | ** This is automatically called by wc_ShaHash */ |
315 | | static int InitSha(wc_Sha* sha) |
316 | | { |
317 | | int ret = 0; |
318 | | |
319 | | sha->digest[0] = 0x67452301L; |
320 | | sha->digest[1] = 0xEFCDAB89L; |
321 | | sha->digest[2] = 0x98BADCFEL; |
322 | | sha->digest[3] = 0x10325476L; |
323 | | sha->digest[4] = 0xC3D2E1F0L; |
324 | | |
325 | | sha->buffLen = 0; |
326 | | sha->loLen = 0; |
327 | | sha->hiLen = 0; |
328 | | |
329 | | /* HW needs to be carefully initialized, taking into account soft copy. |
330 | | ** If already in use; copy may revert to SW as needed. */ |
331 | | ret = esp_sha_init(&(sha->ctx), WC_HASH_TYPE_SHA); |
332 | | |
333 | | return ret; |
334 | | } |
335 | | |
336 | | #elif (defined(WOLFSSL_RENESAS_TSIP_TLS) || \ |
337 | | defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \ |
338 | | !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) |
339 | | |
340 | | /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */ |
341 | | |
342 | | #elif defined(WOLFSSL_RENESAS_RSIP) && \ |
343 | | !defined(NO_WOLFSSL_RENESAS_FSPSM_HASH) |
344 | | |
345 | | /* implemented in wolfcrypt/src/port/Renesas/renesas_fspsm_sha.c */ |
346 | | |
347 | | #elif defined(WOLFSSL_IMXRT_DCP) |
348 | | #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h> |
349 | | /* implemented in wolfcrypt/src/port/nxp/dcp_port.c */ |
350 | | |
351 | | #elif defined(WOLFSSL_SILABS_SE_ACCEL) |
352 | | |
353 | | /* implemented in wolfcrypt/src/port/silabs/silabs_hash.c */ |
354 | | |
355 | | #elif defined(WOLFSSL_RENESAS_RX64_HASH) |
356 | | |
357 | | /* implemented in wolfcrypt/src/port/Renesas/renesas_rx64_hw_sha.c */ |
358 | | |
359 | | #elif defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH) |
360 | | |
361 | | #include <wolfssl/wolfcrypt/port/nxp/se050_port.h> |
362 | | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
363 | | { |
364 | | if (sha == NULL) { |
365 | | return BAD_FUNC_ARG; |
366 | | } |
367 | | (void)devId; |
368 | | |
369 | | return se050_hash_init(&sha->se050Ctx, heap); |
370 | | } |
371 | | |
372 | | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
373 | | { |
374 | | return se050_hash_update(&sha->se050Ctx, data, len); |
375 | | |
376 | | } |
377 | | |
378 | | int wc_ShaFinal(wc_Sha* sha, byte* hash) |
379 | | { |
380 | | int ret = 0; |
381 | | ret = se050_hash_final(&sha->se050Ctx, hash, WC_SHA_DIGEST_SIZE, |
382 | | kAlgorithm_SSS_SHA1); |
383 | | return ret; |
384 | | } |
385 | | int wc_ShaFinalRaw(wc_Sha* sha, byte* hash) |
386 | | { |
387 | | int ret = 0; |
388 | | ret = se050_hash_final(&sha->se050Ctx, hash, WC_SHA_DIGEST_SIZE, |
389 | | kAlgorithm_SSS_SHA1); |
390 | | return ret; |
391 | | } |
392 | | |
393 | | #elif defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_HASH) |
394 | | /* implemented in wolfcrypt/src/port/psa/psa_hash.c */ |
395 | | #elif defined(PSOC6_HASH_SHA1) |
396 | | /* Implemented in wolfcrypt/src/port/cypress/psoc6_crypto.c */ |
397 | | #else |
398 | | /* Software implementation */ |
399 | | #define USE_SHA_SOFTWARE_IMPL |
400 | | |
401 | | static int InitSha(wc_Sha* sha) |
402 | 391k | { |
403 | 391k | int ret = 0; |
404 | | |
405 | 391k | sha->digest[0] = 0x67452301L; |
406 | 391k | sha->digest[1] = 0xEFCDAB89L; |
407 | 391k | sha->digest[2] = 0x98BADCFEL; |
408 | 391k | sha->digest[3] = 0x10325476L; |
409 | 391k | sha->digest[4] = 0xC3D2E1F0L; |
410 | | |
411 | 391k | sha->buffLen = 0; |
412 | 391k | XMEMSET(sha->buffer, 0, sizeof(sha->buffer)); |
413 | 391k | sha->loLen = 0; |
414 | 391k | sha->hiLen = 0; |
415 | 391k | #ifdef WOLFSSL_HASH_FLAGS |
416 | 391k | sha->flags = 0; |
417 | 391k | #endif |
418 | | |
419 | 391k | return ret; |
420 | 391k | } |
421 | | #endif /* End Hardware Acceleration */ |
422 | | |
423 | | /* Software implementation */ |
424 | | #ifdef USE_SHA_SOFTWARE_IMPL |
425 | | |
426 | | static WC_INLINE void AddLength(wc_Sha* sha, word32 len) |
427 | 461k | { |
428 | 461k | word32 tmp = sha->loLen; |
429 | 461k | if ((sha->loLen += len) < tmp) |
430 | 0 | sha->hiLen++; /* carry low to high */ |
431 | 461k | } |
432 | | |
433 | | /* Check if custom wc_Sha transform is used */ |
434 | | #ifndef XTRANSFORM |
435 | 1.02M | #define XTRANSFORM(S,B) Transform((S),(B)) |
436 | | |
437 | 17.0M | #define blk0(i) (W[i] = *((word32*)&data[(i)*sizeof(word32)])) |
438 | 68.0M | #define blk1(i) (W[(i)&15] = \ |
439 | 68.0M | rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1)) |
440 | | |
441 | 21.2M | #define f1(x,y,z) ((z)^((x) &((y)^(z)))) |
442 | 21.2M | #define f2(x,y,z) ((x)^(y)^(z)) |
443 | 21.2M | #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y)))) |
444 | 21.2M | #define f4(x,y,z) ((x)^(y)^(z)) |
445 | | |
446 | | #if defined(WOLFSSL_NUCLEUS_1_2) || defined(NUCLEUS_PLUS_2_3) |
447 | | /* nucleus.h also defines R1-R4 */ |
448 | | #undef R1 |
449 | | #undef R2 |
450 | | #undef R3 |
451 | | #undef R4 |
452 | | #endif |
453 | | |
454 | | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
455 | 17.0M | #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \ |
456 | 17.0M | rotlFixed((v),5); (w) = rotlFixed((w),30); |
457 | 4.25M | #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \ |
458 | 4.25M | rotlFixed((v),5); (w) = rotlFixed((w),30); |
459 | 21.2M | #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \ |
460 | 21.2M | rotlFixed((v),5); (w) = rotlFixed((w),30); |
461 | 21.2M | #define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \ |
462 | 21.2M | rotlFixed((v),5); (w) = rotlFixed((w),30); |
463 | 21.2M | #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \ |
464 | 21.2M | rotlFixed((v),5); (w) = rotlFixed((w),30); |
465 | | |
466 | | static int Transform(wc_Sha* sha, const byte* data) |
467 | 1.06M | { |
468 | 1.06M | word32 W[WC_SHA_BLOCK_SIZE / sizeof(word32)]; |
469 | | |
470 | | /* Copy context->state[] to working vars */ |
471 | 1.06M | word32 a = sha->digest[0]; |
472 | 1.06M | word32 b = sha->digest[1]; |
473 | 1.06M | word32 c = sha->digest[2]; |
474 | 1.06M | word32 d = sha->digest[3]; |
475 | 1.06M | word32 e = sha->digest[4]; |
476 | | |
477 | | #ifdef USE_SLOW_SHA |
478 | | word32 t, i; |
479 | | |
480 | | for (i = 0; i < 16; i++) { |
481 | | R0(a, b, c, d, e, i); |
482 | | t = e; e = d; d = c; c = b; b = a; a = t; |
483 | | } |
484 | | |
485 | | for (; i < 20; i++) { |
486 | | R1(a, b, c, d, e, i); |
487 | | t = e; e = d; d = c; c = b; b = a; a = t; |
488 | | } |
489 | | |
490 | | for (; i < 40; i++) { |
491 | | R2(a, b, c, d, e, i); |
492 | | t = e; e = d; d = c; c = b; b = a; a = t; |
493 | | } |
494 | | |
495 | | for (; i < 60; i++) { |
496 | | R3(a, b, c, d, e, i); |
497 | | t = e; e = d; d = c; c = b; b = a; a = t; |
498 | | } |
499 | | |
500 | | for (; i < 80; i++) { |
501 | | R4(a, b, c, d, e, i); |
502 | | t = e; e = d; d = c; c = b; b = a; a = t; |
503 | | } |
504 | | #else |
505 | | /* nearly 1 K bigger in code size but 25% faster */ |
506 | | /* 4 rounds of 20 operations each. Loop unrolled. */ |
507 | 1.06M | R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); |
508 | 1.06M | R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); |
509 | 1.06M | R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); |
510 | 1.06M | R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); |
511 | | |
512 | 1.06M | R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); |
513 | | |
514 | 1.06M | R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); |
515 | 1.06M | R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); |
516 | 1.06M | R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); |
517 | 1.06M | R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); |
518 | 1.06M | R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); |
519 | | |
520 | 1.06M | R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); |
521 | 1.06M | R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); |
522 | 1.06M | R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); |
523 | 1.06M | R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); |
524 | 1.06M | R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); |
525 | | |
526 | 1.06M | R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); |
527 | 1.06M | R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); |
528 | 1.06M | R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); |
529 | 1.06M | R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); |
530 | 1.06M | R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); |
531 | 1.06M | #endif |
532 | | |
533 | | /* Add the working vars back into digest state[] */ |
534 | 1.06M | sha->digest[0] += a; |
535 | 1.06M | sha->digest[1] += b; |
536 | 1.06M | sha->digest[2] += c; |
537 | 1.06M | sha->digest[3] += d; |
538 | 1.06M | sha->digest[4] += e; |
539 | | |
540 | 1.06M | (void)data; /* Not used */ |
541 | | |
542 | 1.06M | return 0; |
543 | 1.06M | } |
544 | | #endif /* XTRANSFORM when USE_SHA_SOFTWARE_IMPL is enabled */ |
545 | | |
546 | | |
547 | | /* |
548 | | ** wolfCrypt InitSha external wrapper. |
549 | | ** |
550 | | ** we'll assume this is ALWAYS for a new, uninitialized sha |
551 | | */ |
552 | | int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId) |
553 | 234k | { |
554 | 234k | int ret = 0; |
555 | 234k | if (sha == NULL) { |
556 | 0 | return BAD_FUNC_ARG; |
557 | 0 | } |
558 | | |
559 | 234k | sha->heap = heap; |
560 | 234k | #ifdef WOLF_CRYPTO_CB |
561 | 234k | sha->devId = devId; |
562 | 234k | sha->devCtx = NULL; |
563 | 234k | #endif |
564 | | |
565 | | #ifdef MAX3266X_SHA_CB |
566 | | ret = wc_MXC_TPU_SHA_Init(&(sha->mxcCtx)); |
567 | | if (ret != 0) { |
568 | | return ret; |
569 | | } |
570 | | #endif |
571 | | |
572 | | #ifdef WOLFSSL_USE_ESP32_CRYPT_HASH_HW |
573 | | if (sha->ctx.mode != ESP32_SHA_INIT) { |
574 | | /* it may be interesting to see old values during debugging */ |
575 | | ESP_LOGV(TAG, "Set ctx mode from prior value: %d", sha->ctx.mode); |
576 | | } |
577 | | /* We know this is a fresh, uninitialized item, so set to INIT */ |
578 | | sha->ctx.mode = ESP32_SHA_INIT; |
579 | | #endif |
580 | | |
581 | 234k | ret = InitSha(sha); |
582 | 234k | if (ret != 0) { |
583 | 0 | return ret; |
584 | 0 | } |
585 | | |
586 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
587 | | ret = wolfAsync_DevCtxInit(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA, |
588 | | sha->heap, devId); |
589 | | #else |
590 | 234k | (void)devId; |
591 | 234k | #endif /* WOLFSSL_ASYNC_CRYPT */ |
592 | | #ifdef WOLFSSL_IMXRT1170_CAAM |
593 | | ret = wc_CAAM_HashInit(&sha->hndl, &sha->ctx, WC_HASH_TYPE_SHA); |
594 | | #endif |
595 | | |
596 | 234k | return ret; |
597 | 234k | } /* wc_InitSha_ex */ |
598 | | |
599 | | /* do block size increments/updates */ |
600 | | int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len) |
601 | 446k | { |
602 | 446k | int ret = 0; |
603 | 446k | word32 blocksLen; |
604 | 446k | byte* local; |
605 | | |
606 | 446k | if (sha == NULL || (data == NULL && len > 0)) { |
607 | 0 | return BAD_FUNC_ARG; |
608 | 0 | } |
609 | | |
610 | 446k | if (data == NULL && len == 0) { |
611 | | /* valid, but do nothing */ |
612 | 53 | return 0; |
613 | 53 | } |
614 | | |
615 | 446k | #ifdef WOLF_CRYPTO_CB |
616 | 446k | if (sha->devId != INVALID_DEVID) { |
617 | 0 | ret = wc_CryptoCb_ShaHash(sha, data, len, NULL); |
618 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
619 | 0 | return ret; |
620 | 0 | ret = 0; /* reset ret */ |
621 | | /* fall-through when unavailable */ |
622 | 0 | } |
623 | 446k | #endif |
624 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
625 | | if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) { |
626 | | #if defined(HAVE_INTEL_QA) |
627 | | return IntelQaSymSha(&sha->asyncDev, NULL, data, len); |
628 | | #endif |
629 | | } |
630 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
631 | | |
632 | | /* check that internal buffLen is valid */ |
633 | 446k | if (sha->buffLen >= WC_SHA_BLOCK_SIZE) { |
634 | 0 | return BUFFER_E; |
635 | 0 | } |
636 | | |
637 | | /* add length for final */ |
638 | 446k | AddLength(sha, len); |
639 | | |
640 | 446k | local = (byte*)sha->buffer; |
641 | | |
642 | | /* process any remainder from previous operation */ |
643 | 446k | if (sha->buffLen > 0) { |
644 | 232k | blocksLen = min(len, WC_SHA_BLOCK_SIZE - sha->buffLen); |
645 | 232k | XMEMCPY(&local[sha->buffLen], data, blocksLen); |
646 | | |
647 | 232k | sha->buffLen += blocksLen; |
648 | 232k | data += blocksLen; |
649 | 232k | len -= blocksLen; |
650 | | |
651 | 232k | if (sha->buffLen == WC_SHA_BLOCK_SIZE) { |
652 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
653 | | if (sha->ctx.mode == ESP32_SHA_INIT) { |
654 | | #if defined(WOLFSSL_DEBUG_MUTEX) |
655 | | { |
656 | | ESP_LOGI(TAG, "wc_ShaUpdate try hardware"); |
657 | | } |
658 | | #endif |
659 | | esp_sha_try_hw_lock(&sha->ctx); |
660 | | } |
661 | | #endif |
662 | | |
663 | 67.0k | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
664 | | #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \ |
665 | | defined(CONFIG_IDF_TARGET_ESP8684) || \ |
666 | | defined(CONFIG_IDF_TARGET_ESP32C3) || \ |
667 | | defined(CONFIG_IDF_TARGET_ESP32C6) \ |
668 | | ) && \ |
669 | | defined(WOLFSSL_ESP32_CRYPT) && \ |
670 | | !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
671 | | /* For Espressif RISC-V Targets, we *may* need to reverse bytes |
672 | | * depending on if HW is active or not. */ |
673 | | if (esp_sha_need_byte_reversal(&sha->ctx)) |
674 | | #endif |
675 | 67.0k | { |
676 | 67.0k | ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE); |
677 | 67.0k | } |
678 | 67.0k | #endif |
679 | | |
680 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
681 | | if (sha->ctx.mode == ESP32_SHA_SW) { |
682 | | #if defined(WOLFSSL_DEBUG_MUTEX) |
683 | | { |
684 | | ESP_LOGI(TAG, "wc_ShaUpdate process software"); |
685 | | } |
686 | | #endif |
687 | | ret = XTRANSFORM(sha, (const byte*)local); |
688 | | } |
689 | | else { |
690 | | #if defined(WOLFSSL_DEBUG_MUTEX) |
691 | | { |
692 | | ESP_LOGI(TAG, "wc_ShaUpdate process hardware"); |
693 | | } |
694 | | #endif |
695 | | esp_sha_process(sha, (const byte*)local); |
696 | | } |
697 | | #elif defined (WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) |
698 | | ESP_LOGI(TAG, "wc_ShaUpdate not implemented for ESP32C3"); |
699 | | ret = XTRANSFORM(sha, (const byte*)local); |
700 | | #else |
701 | 67.0k | ret = XTRANSFORM(sha, (const byte*)local); |
702 | 67.0k | #endif |
703 | 67.0k | if (ret != 0) { |
704 | 0 | return ret; |
705 | 0 | } |
706 | | |
707 | 67.0k | sha->buffLen = 0; /* Nothing left to do, so set to zero. */ |
708 | 67.0k | } /* (sha->buffLen == WC_SHA_BLOCK_SIZE) */ |
709 | 232k | } /* (sha->buffLen > 0) Process any remainder from previous operation. */ |
710 | | |
711 | | /* process blocks */ |
712 | | #ifdef XTRANSFORM_LEN |
713 | | /* get number of blocks */ |
714 | | /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */ |
715 | | /* len (masked by 0xFFFFFFC0) returns block aligned length */ |
716 | | blocksLen = len & ~(WC_SHA_BLOCK_SIZE-1); |
717 | | if (blocksLen > 0) { |
718 | | /* Byte reversal performed in function if required. */ |
719 | | XTRANSFORM_LEN(sha, data, blocksLen); |
720 | | data += blocksLen; |
721 | | len -= blocksLen; |
722 | | } |
723 | | #else |
724 | 1.25M | while (len >= WC_SHA_BLOCK_SIZE) { |
725 | 809k | word32* local32 = sha->buffer; |
726 | | /* optimization to avoid memcpy if data pointer is properly aligned */ |
727 | | /* Little Endian requires byte swap, so can't use data directly */ |
728 | | #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER) |
729 | | if (((wc_ptr_t)data % WC_HASH_DATA_ALIGNMENT) == 0) { |
730 | | local32 = (word32*)data; |
731 | | } |
732 | | else |
733 | | #endif |
734 | 809k | { |
735 | 809k | XMEMCPY(local32, data, WC_SHA_BLOCK_SIZE); |
736 | 809k | } |
737 | | |
738 | 809k | data += WC_SHA_BLOCK_SIZE; |
739 | 809k | len -= WC_SHA_BLOCK_SIZE; |
740 | | |
741 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
742 | | if (sha->ctx.mode == ESP32_SHA_INIT){ |
743 | | esp_sha_try_hw_lock(&sha->ctx); |
744 | | } |
745 | | #endif |
746 | | |
747 | 809k | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
748 | | #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \ |
749 | | defined(CONFIG_IDF_TARGET_ESP8684) || \ |
750 | | defined(CONFIG_IDF_TARGET_ESP32C3) || \ |
751 | | defined(CONFIG_IDF_TARGET_ESP32C6) \ |
752 | | ) && \ |
753 | | defined(WOLFSSL_ESP32_CRYPT) && \ |
754 | | !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
755 | | /* For Espressif RISC-V Targets, we *may* need to reverse bytes |
756 | | * depending on if HW is active or not. */ |
757 | | if (esp_sha_need_byte_reversal(&sha->ctx)) |
758 | | #endif |
759 | 809k | { |
760 | 809k | ByteReverseWords(local32, local32, WC_SHA_BLOCK_SIZE); |
761 | 809k | } |
762 | 809k | #endif |
763 | | |
764 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
765 | | if (sha->ctx.mode == ESP32_SHA_SW){ |
766 | | ret = XTRANSFORM(sha, (const byte*)local32); |
767 | | } |
768 | | else { |
769 | | esp_sha_process(sha, (const byte*)local32); |
770 | | } |
771 | | #else |
772 | 809k | ret = XTRANSFORM(sha, (const byte*)local32); |
773 | 809k | #endif |
774 | 809k | } |
775 | 446k | #endif /* XTRANSFORM_LEN */ |
776 | | |
777 | | /* save remainder */ |
778 | 446k | if (len > 0) { |
779 | 230k | XMEMCPY(local, data, len); |
780 | 230k | sha->buffLen = len; |
781 | 230k | } |
782 | | |
783 | 446k | return ret; |
784 | 446k | } |
785 | | |
786 | | int wc_ShaFinalRaw(wc_Sha* sha, byte* hash) |
787 | 200 | { |
788 | 200 | #ifdef LITTLE_ENDIAN_ORDER |
789 | 200 | word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)]; |
790 | 200 | XMEMSET(digest, 0, sizeof(digest)); |
791 | 200 | #endif |
792 | | |
793 | 200 | if (sha == NULL || hash == NULL) { |
794 | 0 | return BAD_FUNC_ARG; |
795 | 0 | } |
796 | | |
797 | 200 | #ifdef LITTLE_ENDIAN_ORDER |
798 | | #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \ |
799 | | defined(CONFIG_IDF_TARGET_ESP8684) || \ |
800 | | defined(CONFIG_IDF_TARGET_ESP32C3) || \ |
801 | | defined(CONFIG_IDF_TARGET_ESP32C6) \ |
802 | | ) && \ |
803 | | defined(WOLFSSL_ESP32_CRYPT) && \ |
804 | | !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
805 | | /* For Espressif RISC-V Targets, we *may* need to reverse bytes |
806 | | * depending on if HW is active or not. */ |
807 | | if (esp_sha_need_byte_reversal(&sha->ctx)) |
808 | | #endif |
809 | 200 | { |
810 | 200 | ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE); |
811 | 200 | } |
812 | 200 | XMEMCPY(hash, (byte *)&digest[0], WC_SHA_DIGEST_SIZE); |
813 | | #else |
814 | | XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE); |
815 | | #endif |
816 | | |
817 | 200 | return 0; |
818 | 200 | } |
819 | | |
820 | | /* |
821 | | ** Finalizes hashing of data. Result is placed into hash. |
822 | | ** Resets state of sha struct. |
823 | | */ |
824 | | int wc_ShaFinal(wc_Sha* sha, byte* hash) |
825 | 141k | { |
826 | 141k | int ret; |
827 | 141k | byte* local; |
828 | | |
829 | 141k | if (sha == NULL || hash == NULL) { |
830 | 0 | return BAD_FUNC_ARG; |
831 | 0 | } |
832 | | |
833 | 141k | local = (byte*)sha->buffer; |
834 | | |
835 | 141k | #ifdef WOLF_CRYPTO_CB |
836 | 141k | if (sha->devId != INVALID_DEVID) { |
837 | 0 | ret = wc_CryptoCb_ShaHash(sha, NULL, 0, hash); |
838 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
839 | 0 | return ret; |
840 | | /* fall-through when unavailable */ |
841 | 0 | } |
842 | 141k | #endif |
843 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
844 | | if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) { |
845 | | #if defined(HAVE_INTEL_QA) |
846 | | return IntelQaSymSha(&sha->asyncDev, hash, NULL, WC_SHA_DIGEST_SIZE); |
847 | | #endif |
848 | | } |
849 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
850 | | |
851 | | /* we'll add a 0x80 byte at the end, |
852 | | ** so make sure we have appropriate buffer length. */ |
853 | 141k | if (sha->buffLen > WC_SHA_BLOCK_SIZE - 1) { |
854 | | /* exit with error code if there's a bad buffer size in buffLen */ |
855 | 0 | return BAD_STATE_E; |
856 | 0 | } /* buffLen check */ |
857 | | |
858 | 141k | local[sha->buffLen++] = 0x80; /* add 1 */ |
859 | | |
860 | | /* pad with zeros */ |
861 | 141k | if (sha->buffLen > WC_SHA_PAD_SIZE) { |
862 | 5.08k | if (sha->buffLen < WC_SHA_BLOCK_SIZE) { |
863 | 3.68k | XMEMSET(&local[sha->buffLen], 0, WC_SHA_BLOCK_SIZE - sha->buffLen); |
864 | 3.68k | } |
865 | | |
866 | 5.08k | sha->buffLen += WC_SHA_BLOCK_SIZE - sha->buffLen; |
867 | | |
868 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
869 | | /* For a fresh sha.ctx, try to use hardware acceleration */ |
870 | | if (sha->ctx.mode == ESP32_SHA_INIT) { |
871 | | esp_sha_try_hw_lock(&sha->ctx); |
872 | | } |
873 | | #endif |
874 | | |
875 | 5.08k | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
876 | | #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \ |
877 | | defined(CONFIG_IDF_TARGET_ESP8684) || \ |
878 | | defined(CONFIG_IDF_TARGET_ESP32C3) || \ |
879 | | defined(CONFIG_IDF_TARGET_ESP32C6) \ |
880 | | ) && \ |
881 | | defined(WOLFSSL_ESP32_CRYPT) && \ |
882 | | !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
883 | | /* For Espressif RISC-V Targets, we *may* need to reverse bytes |
884 | | * depending on if HW is active or not. */ |
885 | | if (esp_sha_need_byte_reversal(&sha->ctx)) |
886 | | #endif |
887 | 5.08k | { |
888 | 5.08k | ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE); |
889 | 5.08k | } |
890 | 5.08k | #endif |
891 | | |
892 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
893 | | /* if HW was busy, we may need to fall back to SW. */ |
894 | | if (sha->ctx.mode == ESP32_SHA_SW) { |
895 | | ret = XTRANSFORM(sha, (const byte*)local); |
896 | | } |
897 | | else { |
898 | | ret = esp_sha_process(sha, (const byte*)local); |
899 | | } |
900 | | #else |
901 | | /* |
902 | | ** The #if defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) also falls |
903 | | ** though here to SW, as it's not yet implemented for HW. |
904 | | */ |
905 | 5.08k | ret = XTRANSFORM(sha, (const byte*)local); |
906 | 5.08k | #endif |
907 | 5.08k | if (ret != 0) { |
908 | 0 | return ret; |
909 | 0 | } |
910 | | |
911 | 5.08k | sha->buffLen = 0; |
912 | 5.08k | } /* (sha->buffLen > WC_SHA_PAD_SIZE) */ |
913 | | |
914 | 141k | XMEMSET(&local[sha->buffLen], 0, WC_SHA_PAD_SIZE - sha->buffLen); |
915 | | |
916 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
917 | | if (sha->ctx.mode == ESP32_SHA_INIT) { |
918 | | esp_sha_try_hw_lock(&sha->ctx); |
919 | | } |
920 | | #endif |
921 | | |
922 | 141k | #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA) |
923 | | #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \ |
924 | | defined(CONFIG_IDF_TARGET_ESP8684) || \ |
925 | | defined(CONFIG_IDF_TARGET_ESP32C3) || \ |
926 | | defined(CONFIG_IDF_TARGET_ESP32C6) \ |
927 | | ) && \ |
928 | | defined(WOLFSSL_ESP32_CRYPT) && \ |
929 | | !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
930 | | /* For Espressif RISC-V Targets, we *may* need to reverse bytes |
931 | | * depending on if HW is active or not. */ |
932 | | if (esp_sha_need_byte_reversal(&sha->ctx)) |
933 | | #endif |
934 | 141k | { /* reminder local also points to sha->buffer */ |
935 | 141k | ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE); |
936 | 141k | } |
937 | 141k | #endif |
938 | | |
939 | | /* store lengths */ |
940 | | /* put lengths in bits */ |
941 | 141k | sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + (sha->hiLen << 3); |
942 | 141k | sha->loLen = sha->loLen << 3; |
943 | | |
944 | | /* ! length ordering dependent on digest endian type ! */ |
945 | 141k | XMEMCPY(&local[WC_SHA_PAD_SIZE], &sha->hiLen, sizeof(word32)); |
946 | 141k | XMEMCPY(&local[WC_SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32)); |
947 | | |
948 | | #if defined(FREESCALE_MMCAU_SHA) |
949 | | /* Kinetis requires only these bytes reversed */ |
950 | | ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], |
951 | | &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], |
952 | | 2 * sizeof(word32)); |
953 | | #endif |
954 | | |
955 | | |
956 | | #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \ |
957 | | defined(CONFIG_IDF_TARGET_ESP8684) || \ |
958 | | defined(CONFIG_IDF_TARGET_ESP32C3) || \ |
959 | | defined(CONFIG_IDF_TARGET_ESP32C6) \ |
960 | | ) && \ |
961 | | defined(WOLFSSL_ESP32_CRYPT) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
962 | | if (sha->ctx.mode == ESP32_SHA_HW) { |
963 | | #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG) |
964 | | { |
965 | | ESP_LOGV(TAG, "Start: Reverse PAD SIZE Endianness."); |
966 | | } |
967 | | #endif |
968 | | ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], /* out */ |
969 | | &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)], /* in */ |
970 | | 2 * sizeof(word32) /* byte count to reverse */ |
971 | | ); |
972 | | #if defined(WOLFSSL_SUPER_VERBOSE_DEBUG) |
973 | | { |
974 | | ESP_LOGV(TAG, "End: Reverse PAD SIZE Endianness."); |
975 | | } |
976 | | #endif |
977 | | } /* end if (sha->ctx.mode == ESP32_SHA_HW) */ |
978 | | #endif |
979 | | |
980 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
981 | | if (sha->ctx.mode == ESP32_SHA_SW) { |
982 | | ret = XTRANSFORM(sha, (const byte*)local); |
983 | | } |
984 | | else { |
985 | | ret = esp_sha_digest_process(sha, 1); |
986 | | } |
987 | | /* |
988 | | ** The #if defined(WOLFSSL_USE_ESP32C3_CRYPT_HASH_HW) also falls |
989 | | ** though here to SW, as it's not yet implemented for HW. |
990 | | */ |
991 | | #else |
992 | 141k | ret = XTRANSFORM(sha, (const byte*)local); |
993 | 141k | #endif |
994 | | |
995 | 141k | #ifdef LITTLE_ENDIAN_ORDER |
996 | | #if ( defined(CONFIG_IDF_TARGET_ESP32C2) || \ |
997 | | defined(CONFIG_IDF_TARGET_ESP8684) || \ |
998 | | defined(CONFIG_IDF_TARGET_ESP32C3) || \ |
999 | | defined(CONFIG_IDF_TARGET_ESP32C6) \ |
1000 | | ) && \ |
1001 | | defined(WOLFSSL_ESP32_CRYPT) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
1002 | | /* For Espressif RISC-V Targets, we *may* need to reverse bytes |
1003 | | * depending on if HW is active or not. */ |
1004 | | if (esp_sha_need_byte_reversal(&sha->ctx)) |
1005 | | #endif |
1006 | 141k | { |
1007 | 141k | ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE); |
1008 | 141k | } |
1009 | 141k | #endif |
1010 | | |
1011 | 141k | XMEMCPY(hash, (byte *)&sha->digest[0], WC_SHA_DIGEST_SIZE); |
1012 | | |
1013 | | /* we'll always reset state upon exit and return the error code from above, |
1014 | | * which may cause fall back to SW if HW is busy. we do not return result |
1015 | | * of initSha here */ |
1016 | 141k | (void)InitSha(sha); /* reset state */ |
1017 | 141k | return ret; |
1018 | 141k | } |
1019 | | |
1020 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_CURL) |
1021 | | /* Apply SHA1 transformation to the data */ |
1022 | | /* @param sha a pointer to wc_Sha structure */ |
1023 | | /* @param data data to be applied SHA1 transformation */ |
1024 | | /* @return 0 on successful, otherwise non-zero on failure */ |
1025 | | int wc_ShaTransform(wc_Sha* sha, const unsigned char* data) |
1026 | | { |
1027 | | /* sanity check */ |
1028 | | if (sha == NULL || data == NULL) { |
1029 | | return BAD_FUNC_ARG; |
1030 | | } |
1031 | | return (Transform(sha, data)); |
1032 | | } |
1033 | | #endif |
1034 | | |
1035 | | #endif /* USE_SHA_SOFTWARE_IMPL */ |
1036 | | |
1037 | | /* |
1038 | | ** This function initializes SHA. This is automatically called by wc_ShaHash. |
1039 | | */ |
1040 | | int wc_InitSha(wc_Sha* sha) |
1041 | 158 | { |
1042 | 158 | return wc_InitSha_ex(sha, NULL, INVALID_DEVID); |
1043 | 158 | } |
1044 | | |
1045 | | |
1046 | | #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) |
1047 | | |
1048 | | #ifndef MAX3266X_SHA |
1049 | | |
1050 | | void wc_ShaFree(wc_Sha* sha) |
1051 | 234k | { |
1052 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE) |
1053 | | int ret = 0; |
1054 | | #endif |
1055 | | |
1056 | 234k | if (sha == NULL) |
1057 | 0 | return; |
1058 | | |
1059 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE) |
1060 | | #ifndef WOLF_CRYPTO_CB_FIND |
1061 | | if (sha->devId != INVALID_DEVID) |
1062 | | #endif |
1063 | | { |
1064 | | ret = wc_CryptoCb_Free(sha->devId, WC_ALGO_TYPE_HASH, |
1065 | | WC_HASH_TYPE_SHA, (void*)sha); |
1066 | | /* If they want the standard free, they can call it themselves */ |
1067 | | /* via their callback setting devId to INVALID_DEVID */ |
1068 | | /* otherwise assume the callback handled it */ |
1069 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1070 | | return; |
1071 | | /* fall-through when unavailable */ |
1072 | | } |
1073 | | |
1074 | | /* silence compiler warning */ |
1075 | | (void)ret; |
1076 | | |
1077 | | #endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */ |
1078 | | |
1079 | | #if defined(WOLFSSL_ESP32) && !defined(NO_WOLFSSL_ESP32_CRYPT_HASH) |
1080 | | esp_sha_release_unfinished_lock(&sha->ctx); |
1081 | | #endif |
1082 | | |
1083 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
1084 | | wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA); |
1085 | | #endif /* WOLFSSL_ASYNC_CRYPT */ |
1086 | | |
1087 | | #ifdef WOLFSSL_PIC32MZ_HASH |
1088 | | wc_ShaPic32Free(sha); |
1089 | | #endif |
1090 | | #ifdef MAX3266X_SHA_CB |
1091 | | wc_MXC_TPU_SHA_Free(&(sha->mxcCtx)); |
1092 | | #endif |
1093 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH) |
1094 | | se050_hash_free(&sha->se050Ctx); |
1095 | | #endif |
1096 | | #if (defined(WOLFSSL_RENESAS_TSIP_TLS) || \ |
1097 | | defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) && \ |
1098 | | !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) || \ |
1099 | | (defined(WOLFSSL_RENESAS_RSIP) && (WOLFSSL_RENESAS_RZFSP_VER >= 220)) ||\ |
1100 | | defined(WOLFSSL_RENESAS_RX64_HASH) |
1101 | | XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER); |
1102 | | sha->msg = NULL; |
1103 | | #endif |
1104 | | #ifdef WOLFSSL_IMXRT_DCP |
1105 | | DCPShaFree(sha); |
1106 | | #endif |
1107 | | |
1108 | | #if defined(PSOC6_HASH_SHA1) |
1109 | | wc_Psoc6_Sha_Free(); |
1110 | | #endif |
1111 | 234k | } |
1112 | | |
1113 | | #endif /* !MAX3266X_SHA */ |
1114 | | #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */ |
1115 | | #endif /* !WOLFSSL_TI_HASH */ |
1116 | | |
1117 | | #if !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_IMXRT_DCP) |
1118 | | |
1119 | | #if ((!defined(WOLFSSL_RENESAS_TSIP_TLS) && \ |
1120 | | !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)) || \ |
1121 | | defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)) && \ |
1122 | | (!defined(WOLFSSL_RENESAS_RSIP) || \ |
1123 | | defined(NO_WOLFSSL_RENESAS_FSPSM_HASH)) |
1124 | | #if !defined(WOLFSSL_RENESAS_RX64_HASH) |
1125 | | |
1126 | | #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) |
1127 | | |
1128 | | #ifndef MAX3266X_SHA |
1129 | | |
1130 | | /* wc_ShaGetHash get hash value */ |
1131 | | int wc_ShaGetHash(wc_Sha* sha, byte* hash) |
1132 | 1.10k | { |
1133 | 1.10k | int ret; |
1134 | 1.10k | WC_DECLARE_VAR(tmpSha, wc_Sha, 1, 0); |
1135 | | |
1136 | 1.10k | if (sha == NULL || hash == NULL) { |
1137 | 0 | return BAD_FUNC_ARG; |
1138 | 0 | } |
1139 | | |
1140 | 1.10k | WC_ALLOC_VAR_EX(tmpSha, wc_Sha, 1, NULL, DYNAMIC_TYPE_TMP_BUFFER, |
1141 | 1.10k | return MEMORY_E); |
1142 | | |
1143 | 1.09k | ret = wc_ShaCopy(sha, tmpSha); |
1144 | 1.09k | if (ret == 0) { |
1145 | 1.09k | ret = wc_ShaFinal(tmpSha, hash); |
1146 | 1.09k | } |
1147 | | |
1148 | 1.09k | WC_FREE_VAR_EX(tmpSha, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
1149 | | |
1150 | 1.09k | return ret; |
1151 | 1.10k | } |
1152 | | |
1153 | | int wc_ShaCopy(wc_Sha* src, wc_Sha* dst) |
1154 | 1.15k | { |
1155 | 1.15k | int ret = 0; |
1156 | | |
1157 | 1.15k | if (src == NULL || dst == NULL) |
1158 | 0 | return BAD_FUNC_ARG; |
1159 | | |
1160 | | #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_COPY) |
1161 | | #ifndef WOLF_CRYPTO_CB_FIND |
1162 | | if (src->devId != INVALID_DEVID) |
1163 | | #endif |
1164 | | { |
1165 | | /* Cast the source and destination to be void to keep the abstraction */ |
1166 | | ret = wc_CryptoCb_Copy(src->devId, WC_ALGO_TYPE_HASH, |
1167 | | WC_HASH_TYPE_SHA, (void*)src, (void*)dst); |
1168 | | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
1169 | | return ret; |
1170 | | /* fall-through when unavailable */ |
1171 | | } |
1172 | | ret = 0; /* Reset ret to 0 to avoid returning the callback error code */ |
1173 | | #endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_COPY */ |
1174 | | |
1175 | 1.15k | XMEMCPY(dst, src, sizeof(wc_Sha)); |
1176 | | |
1177 | | #if defined(WOLFSSL_SILABS_SE_ACCEL) && defined(WOLFSSL_SILABS_SE_ACCEL_3) |
1178 | | dst->silabsCtx.hash_ctx.cmd_ctx = &dst->silabsCtx.cmd_ctx; |
1179 | | dst->silabsCtx.hash_ctx.hash_type_ctx = &dst->silabsCtx.hash_type_ctx; |
1180 | | #endif |
1181 | | |
1182 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) |
1183 | | ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev); |
1184 | | #endif |
1185 | | |
1186 | | #ifdef WOLFSSL_PIC32MZ_HASH |
1187 | | ret = wc_Pic32HashCopy(&src->cache, &dst->cache); |
1188 | | #endif |
1189 | | |
1190 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_HASH) |
1191 | | ret = se050_hash_copy(&src->se050Ctx, &dst->se050Ctx); |
1192 | | #endif |
1193 | | |
1194 | | #if defined(WOLFSSL_USE_ESP32_CRYPT_HASH_HW) |
1195 | | esp_sha_ctx_copy(src, dst); |
1196 | | #endif |
1197 | | |
1198 | | #ifdef MAX3266X_SHA_CB |
1199 | | ret = wc_MXC_TPU_SHA_Copy(&(src->mxcCtx), &(dst->mxcCtx)); |
1200 | | if (ret != 0) { |
1201 | | return ret; |
1202 | | } |
1203 | | #endif |
1204 | | |
1205 | | #if defined(PSOC6_HASH_SHA1) |
1206 | | wc_Psoc6_Sha1_Sha2_Init(dst, WC_PSOC6_SHA1, 0); |
1207 | | #endif |
1208 | | |
1209 | 1.15k | #ifdef WOLFSSL_HASH_FLAGS |
1210 | 1.15k | dst->flags |= WC_HASH_FLAG_ISCOPY; |
1211 | 1.15k | #endif |
1212 | 1.15k | return ret; |
1213 | 1.15k | } |
1214 | | #endif /* WOLFSSL_RENESAS_RX64_HASH */ |
1215 | | #endif /* !MAX3266X_SHA */ |
1216 | | #endif /* !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_HASH) */ |
1217 | | #endif /* !defined(WOLFSSL_RENESAS_TSIP_TLS) && \ |
1218 | | !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY) || |
1219 | | defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH) */ |
1220 | | #endif /* !defined(WOLFSSL_TI_HASH) && !defined(WOLFSSL_IMXRT_DCP) */ |
1221 | | |
1222 | | #ifdef WOLFSSL_HASH_FLAGS |
1223 | | int wc_ShaSetFlags(wc_Sha* sha, word32 flags) |
1224 | 93.7k | { |
1225 | 93.7k | if (sha) { |
1226 | 93.7k | sha->flags = flags; |
1227 | 93.7k | } |
1228 | 93.7k | return 0; |
1229 | 93.7k | } |
1230 | | int wc_ShaGetFlags(wc_Sha* sha, word32* flags) |
1231 | 0 | { |
1232 | 0 | if (sha && flags) { |
1233 | 0 | *flags = sha->flags; |
1234 | 0 | } |
1235 | 0 | return 0; |
1236 | 0 | } |
1237 | | #endif |
1238 | | |
1239 | | #endif /* !NO_SHA */ |