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