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