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