source: azure_iot_hub_riscv/trunk/wolfssl-4.4.0/wolfcrypt/src/sha.c@ 453

Last change on this file since 453 was 453, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 24.4 KB
Line 
1/* sha.c
2 *
3 * Copyright (C) 2006-2020 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 2 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#ifdef HAVE_CONFIG_H
24 #include <config.h>
25#endif
26
27#include <wolfssl/wolfcrypt/settings.h>
28
29#if !defined(NO_SHA)
30
31#if defined(HAVE_FIPS) && \
32 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
33
34 /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
35 #define FIPS_NO_WRAPPERS
36
37 #ifdef USE_WINDOWS_API
38 #pragma code_seg(".fipsA$j")
39 #pragma const_seg(".fipsB$j")
40 #endif
41#endif
42
43#include <wolfssl/wolfcrypt/sha.h>
44#include <wolfssl/wolfcrypt/error-crypt.h>
45#include <wolfssl/wolfcrypt/hash.h>
46
47#ifdef WOLF_CRYPTO_CB
48 #include <wolfssl/wolfcrypt/cryptocb.h>
49#endif
50
51/* fips wrapper calls, user can call direct */
52#if defined(HAVE_FIPS) && \
53 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
54
55 int wc_InitSha(wc_Sha* sha)
56 {
57 if (sha == NULL) {
58 return BAD_FUNC_ARG;
59 }
60 return InitSha_fips(sha);
61 }
62 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
63 {
64 (void)heap;
65 (void)devId;
66 if (sha == NULL) {
67 return BAD_FUNC_ARG;
68 }
69 return InitSha_fips(sha);
70 }
71
72 int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
73 {
74 if (sha == NULL || (data == NULL && len > 0)) {
75 return BAD_FUNC_ARG;
76 }
77 return ShaUpdate_fips(sha, data, len);
78 }
79
80 int wc_ShaFinal(wc_Sha* sha, byte* out)
81 {
82 if (sha == NULL || out == NULL) {
83 return BAD_FUNC_ARG;
84 }
85 return ShaFinal_fips(sha,out);
86 }
87 void wc_ShaFree(wc_Sha* sha)
88 {
89 (void)sha;
90 /* Not supported in FIPS */
91 }
92
93#else /* else build without fips, or for FIPS v2 */
94
95
96#if defined(WOLFSSL_TI_HASH)
97 /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
98
99#else
100
101#include <wolfssl/wolfcrypt/logging.h>
102#ifdef NO_INLINE
103 #include <wolfssl/wolfcrypt/misc.h>
104#else
105 #define WOLFSSL_MISC_INCLUDED
106 #include <wolfcrypt/src/misc.c>
107#endif
108
109
110/* Hardware Acceleration */
111#if defined(WOLFSSL_PIC32MZ_HASH)
112 #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
113
114#elif defined(STM32_HASH)
115
116 /* Supports CubeMX HAL or Standard Peripheral Library */
117 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
118 {
119 if (sha == NULL) {
120 return BAD_FUNC_ARG;
121 }
122
123 (void)devId;
124 (void)heap;
125
126 wc_Stm32_Hash_Init(&sha->stmCtx);
127
128 return 0;
129 }
130
131 int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
132 {
133 int ret;
134
135 if (sha == NULL || (data == NULL && len > 0)) {
136 return BAD_FUNC_ARG;
137 }
138
139 ret = wolfSSL_CryptHwMutexLock();
140 if (ret == 0) {
141 ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1,
142 data, len);
143 wolfSSL_CryptHwMutexUnLock();
144 }
145 return ret;
146 }
147
148 int wc_ShaFinal(wc_Sha* sha, byte* hash)
149 {
150 int ret;
151
152 if (sha == NULL || hash == NULL) {
153 return BAD_FUNC_ARG;
154 }
155
156 ret = wolfSSL_CryptHwMutexLock();
157 if (ret == 0) {
158 ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1,
159 hash, WC_SHA_DIGEST_SIZE);
160 wolfSSL_CryptHwMutexUnLock();
161 }
162
163 (void)wc_InitSha(sha); /* reset state */
164
165 return ret;
166 }
167
168
169#elif defined(FREESCALE_LTC_SHA)
170
171 #include "fsl_ltc.h"
172 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
173 {
174 if (sha == NULL) {
175 return BAD_FUNC_ARG;
176 }
177
178 (void)devId;
179 (void)heap;
180
181 LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0);
182 return 0;
183 }
184
185 int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
186 {
187 LTC_HASH_Update(&sha->ctx, data, len);
188 return 0;
189 }
190
191 int wc_ShaFinal(wc_Sha* sha, byte* hash)
192 {
193 uint32_t hashlen = WC_SHA_DIGEST_SIZE;
194 LTC_HASH_Finish(&sha->ctx, hash, &hashlen);
195 return wc_InitSha(sha); /* reset state */
196 }
197
198
199#elif defined(FREESCALE_MMCAU_SHA)
200
201 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
202 #include "cau_api.h"
203 #else
204 #include "fsl_mmcau.h"
205 #endif
206
207 #define USE_SHA_SOFTWARE_IMPL /* Only for API's, actual transform is here */
208
209 #define XTRANSFORM(S,B) Transform((S),(B))
210 #define XTRANSFORM_LEN(S,B,L) Transform_Len((S),(B),(L))
211
212 #ifndef WC_HASH_DATA_ALIGNMENT
213 /* these hardware API's require 4 byte (word32) alignment */
214 #define WC_HASH_DATA_ALIGNMENT 4
215 #endif
216
217 static int InitSha(wc_Sha* sha)
218 {
219 int ret = 0;
220 ret = wolfSSL_CryptHwMutexLock();
221 if (ret != 0) {
222 return ret;
223 }
224 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
225 cau_sha1_initialize_output(sha->digest);
226 #else
227 MMCAU_SHA1_InitializeOutput((uint32_t*)sha->digest);
228 #endif
229 wolfSSL_CryptHwMutexUnLock();
230
231 sha->buffLen = 0;
232 sha->loLen = 0;
233 sha->hiLen = 0;
234
235 return ret;
236 }
237
238 static int Transform(wc_Sha* sha, const byte* data)
239 {
240 int ret = wolfSSL_CryptHwMutexLock();
241 if (ret == 0) {
242 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
243 cau_sha1_hash_n((byte*)data, 1, sha->digest);
244 #else
245 MMCAU_SHA1_HashN((byte*)data, 1, (uint32_t*)sha->digest);
246 #endif
247 wolfSSL_CryptHwMutexUnLock();
248 }
249 return ret;
250 }
251
252 static int Transform_Len(wc_Sha* sha, const byte* data, word32 len)
253 {
254 int ret = wolfSSL_CryptHwMutexLock();
255 if (ret == 0) {
256 #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
257 if ((size_t)data % WC_HASH_DATA_ALIGNMENT) {
258 /* data pointer is NOT aligned,
259 * so copy and perform one block at a time */
260 byte* local = (byte*)sha->buffer;
261 while (len >= WC_SHA_BLOCK_SIZE) {
262 XMEMCPY(local, data, WC_SHA_BLOCK_SIZE);
263 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
264 cau_sha1_hash_n(local, 1, sha->digest);
265 #else
266 MMCAU_SHA1_HashN(local, 1, sha->digest);
267 #endif
268 data += WC_SHA_BLOCK_SIZE;
269 len -= WC_SHA_BLOCK_SIZE;
270 }
271 }
272 else
273 #endif
274 {
275 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
276 cau_sha1_hash_n((byte*)data, len/WC_SHA_BLOCK_SIZE, sha->digest);
277 #else
278 MMCAU_SHA1_HashN((byte*)data, len/WC_SHA_BLOCK_SIZE,
279 (uint32_t*)sha->digest);
280 #endif
281 }
282 wolfSSL_CryptHwMutexUnLock();
283 }
284 return ret;
285 }
286
287#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
288 /* wolfcrypt/src/port/caam/caam_sha.c */
289
290#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
291 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
292
293 #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
294
295 #define USE_SHA_SOFTWARE_IMPL
296
297 static int InitSha(wc_Sha* sha)
298 {
299 int ret = 0;
300
301 sha->digest[0] = 0x67452301L;
302 sha->digest[1] = 0xEFCDAB89L;
303 sha->digest[2] = 0x98BADCFEL;
304 sha->digest[3] = 0x10325476L;
305 sha->digest[4] = 0xC3D2E1F0L;
306
307 sha->buffLen = 0;
308 sha->loLen = 0;
309 sha->hiLen = 0;
310
311 /* always start firstblock = 1 when using hw engine */
312 sha->ctx.isfirstblock = 1;
313 sha->ctx.sha_type = SHA1;
314 if(sha->ctx.mode == ESP32_SHA_HW){
315 /* release hw engine */
316 esp_sha_hw_unlock();
317 }
318 /* always set mode as INIT
319 * whether using HW or SW is determined at first call of update()
320 */
321 sha->ctx.mode = ESP32_SHA_INIT;
322
323 return ret;
324 }
325
326#elif defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
327 !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
328
329 /* implemented in wolfcrypt/src/port/Renesas/renesas_tsip_sha.c */
330
331#else
332 /* Software implementation */
333 #define USE_SHA_SOFTWARE_IMPL
334
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 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
349 sha->flags = 0;
350 #endif
351
352 return ret;
353 }
354#endif /* End Hardware Acceleration */
355
356/* Software implementation */
357#ifdef USE_SHA_SOFTWARE_IMPL
358
359static WC_INLINE void AddLength(wc_Sha* sha, word32 len)
360{
361 word32 tmp = sha->loLen;
362 if ((sha->loLen += len) < tmp)
363 sha->hiLen++; /* carry low to high */
364}
365
366/* Check if custom wc_Sha transform is used */
367#ifndef XTRANSFORM
368 #define XTRANSFORM(S,B) Transform((S),(B))
369
370 #define blk0(i) (W[i] = *((word32*)&data[i*sizeof(word32)]))
371 #define blk1(i) (W[(i)&15] = \
372 rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1))
373
374 #define f1(x,y,z) ((z)^((x) &((y)^(z))))
375 #define f2(x,y,z) ((x)^(y)^(z))
376 #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
377 #define f4(x,y,z) ((x)^(y)^(z))
378
379 #ifdef WOLFSSL_NUCLEUS_1_2
380 /* nucleus.h also defines R1-R4 */
381 #undef R1
382 #undef R2
383 #undef R3
384 #undef R4
385 #endif
386
387 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
388 #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
389 rotlFixed((v),5); (w) = rotlFixed((w),30);
390 #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
391 rotlFixed((v),5); (w) = rotlFixed((w),30);
392 #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
393 rotlFixed((v),5); (w) = rotlFixed((w),30);
394 #define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
395 rotlFixed((v),5); (w) = rotlFixed((w),30);
396 #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
397 rotlFixed((v),5); (w) = rotlFixed((w),30);
398
399 static int Transform(wc_Sha* sha, const byte* data)
400 {
401 word32 W[WC_SHA_BLOCK_SIZE / sizeof(word32)];
402
403 /* Copy context->state[] to working vars */
404 word32 a = sha->digest[0];
405 word32 b = sha->digest[1];
406 word32 c = sha->digest[2];
407 word32 d = sha->digest[3];
408 word32 e = sha->digest[4];
409
410 #ifdef USE_SLOW_SHA
411 word32 t, i;
412
413 for (i = 0; i < 16; i++) {
414 R0(a, b, c, d, e, i);
415 t = e; e = d; d = c; c = b; b = a; a = t;
416 }
417
418 for (; i < 20; i++) {
419 R1(a, b, c, d, e, i);
420 t = e; e = d; d = c; c = b; b = a; a = t;
421 }
422
423 for (; i < 40; i++) {
424 R2(a, b, c, d, e, i);
425 t = e; e = d; d = c; c = b; b = a; a = t;
426 }
427
428 for (; i < 60; i++) {
429 R3(a, b, c, d, e, i);
430 t = e; e = d; d = c; c = b; b = a; a = t;
431 }
432
433 for (; i < 80; i++) {
434 R4(a, b, c, d, e, i);
435 t = e; e = d; d = c; c = b; b = a; a = t;
436 }
437 #else
438 /* nearly 1 K bigger in code size but 25% faster */
439 /* 4 rounds of 20 operations each. Loop unrolled. */
440 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);
441 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);
442 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);
443 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);
444
445 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);
446
447 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);
448 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);
449 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);
450 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);
451 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);
452
453 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);
454 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);
455 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);
456 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);
457 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);
458
459 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);
460 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);
461 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);
462 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);
463 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);
464 #endif
465
466 /* Add the working vars back into digest state[] */
467 sha->digest[0] += a;
468 sha->digest[1] += b;
469 sha->digest[2] += c;
470 sha->digest[3] += d;
471 sha->digest[4] += e;
472
473 (void)data; /* Not used */
474
475 return 0;
476 }
477#endif /* !USE_CUSTOM_SHA_TRANSFORM */
478
479
480int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
481{
482 int ret = 0;
483
484 if (sha == NULL)
485 return BAD_FUNC_ARG;
486
487 sha->heap = heap;
488#ifdef WOLF_CRYPTO_CB
489 sha->devId = devId;
490#endif
491
492#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
493 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
494 sha->ctx.mode = ESP32_SHA_INIT;
495 sha->ctx.isfirstblock = 1;
496#endif
497 ret = InitSha(sha);
498 if (ret != 0)
499 return ret;
500
501#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
502 ret = wolfAsync_DevCtxInit(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA,
503 sha->heap, devId);
504#else
505 (void)devId;
506#endif /* WOLFSSL_ASYNC_CRYPT */
507
508 return ret;
509}
510
511/* do block size increments/updates */
512int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
513{
514 int ret = 0;
515 word32 blocksLen;
516 byte* local;
517
518 if (sha == NULL || (data == NULL && len > 0)) {
519 return BAD_FUNC_ARG;
520 }
521
522#ifdef WOLF_CRYPTO_CB
523 if (sha->devId != INVALID_DEVID) {
524 ret = wc_CryptoCb_ShaHash(sha, data, len, NULL);
525 if (ret != CRYPTOCB_UNAVAILABLE)
526 return ret;
527 ret = 0; /* reset ret */
528 /* fall-through when unavailable */
529 }
530#endif
531#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
532 if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
533 #if defined(HAVE_INTEL_QA)
534 return IntelQaSymSha(&sha->asyncDev, NULL, data, len);
535 #endif
536 }
537#endif /* WOLFSSL_ASYNC_CRYPT */
538
539 /* check that internal buffLen is valid */
540 if (sha->buffLen >= WC_SHA_BLOCK_SIZE)
541 return BUFFER_E;
542
543 if (data == NULL && len == 0) {
544 /* valid, but do nothing */
545 return 0;
546 }
547
548 /* add length for final */
549 AddLength(sha, len);
550
551 local = (byte*)sha->buffer;
552
553 /* process any remainder from previous operation */
554 if (sha->buffLen > 0) {
555 blocksLen = min(len, WC_SHA_BLOCK_SIZE - sha->buffLen);
556 XMEMCPY(&local[sha->buffLen], data, blocksLen);
557
558 sha->buffLen += blocksLen;
559 data += blocksLen;
560 len -= blocksLen;
561
562 if (sha->buffLen == WC_SHA_BLOCK_SIZE) {
563 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
564 ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
565 #endif
566
567 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
568 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
569 if (sha->ctx.mode == ESP32_SHA_INIT) {
570 esp_sha_try_hw_lock(&sha->ctx);
571 }
572 if (sha->ctx.mode == ESP32_SHA_SW) {
573 ret = XTRANSFORM(sha, (const byte*)local);
574 } else {
575 esp_sha_process(sha, (const byte*)local);
576 }
577 #else
578 ret = XTRANSFORM(sha, (const byte*)local);
579 #endif
580 if (ret != 0)
581 return ret;
582
583 sha->buffLen = 0;
584 }
585 }
586
587 /* process blocks */
588#ifdef XTRANSFORM_LEN
589 /* get number of blocks */
590 /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
591 /* len (masked by 0xFFFFFFC0) returns block aligned length */
592 blocksLen = len & ~(WC_SHA_BLOCK_SIZE-1);
593 if (blocksLen > 0) {
594 /* Byte reversal performed in function if required. */
595 XTRANSFORM_LEN(sha, data, blocksLen);
596 data += blocksLen;
597 len -= blocksLen;
598 }
599#else
600 while (len >= WC_SHA_BLOCK_SIZE) {
601 word32* local32 = sha->buffer;
602 /* optimization to avoid memcpy if data pointer is properly aligned */
603 /* Little Endian requires byte swap, so can't use data directly */
604 #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(LITTLE_ENDIAN_ORDER)
605 if (((size_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
606 local32 = (word32*)data;
607 }
608 else
609 #endif
610 {
611 XMEMCPY(local32, data, WC_SHA_BLOCK_SIZE);
612 }
613
614 data += WC_SHA_BLOCK_SIZE;
615 len -= WC_SHA_BLOCK_SIZE;
616
617 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
618 ByteReverseWords(local32, local32, WC_SHA_BLOCK_SIZE);
619 #endif
620
621 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
622 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
623 if (sha->ctx.mode == ESP32_SHA_INIT){
624 esp_sha_try_hw_lock(&sha->ctx);
625 }
626 if (sha->ctx.mode == ESP32_SHA_SW){
627 ret = XTRANSFORM(sha, (const byte*)local32);
628 } else {
629 esp_sha_process(sha, (const byte*)local32);
630 }
631 #else
632 ret = XTRANSFORM(sha, (const byte*)local32);
633 #endif
634 }
635#endif /* XTRANSFORM_LEN */
636
637 /* save remainder */
638 if (len > 0) {
639 XMEMCPY(local, data, len);
640 sha->buffLen = len;
641 }
642
643 return ret;
644}
645
646int wc_ShaFinalRaw(wc_Sha* sha, byte* hash)
647{
648#ifdef LITTLE_ENDIAN_ORDER
649 word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
650#endif
651
652 if (sha == NULL || hash == NULL) {
653 return BAD_FUNC_ARG;
654 }
655
656#ifdef LITTLE_ENDIAN_ORDER
657 ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE);
658 XMEMCPY(hash, digest, WC_SHA_DIGEST_SIZE);
659#else
660 XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
661#endif
662
663 return 0;
664}
665
666int wc_ShaFinal(wc_Sha* sha, byte* hash)
667{
668 int ret;
669 byte* local;
670
671 if (sha == NULL || hash == NULL) {
672 return BAD_FUNC_ARG;
673 }
674
675 local = (byte*)sha->buffer;
676
677#ifdef WOLF_CRYPTO_CB
678 if (sha->devId != INVALID_DEVID) {
679 ret = wc_CryptoCb_ShaHash(sha, NULL, 0, hash);
680 if (ret != CRYPTOCB_UNAVAILABLE)
681 return ret;
682 ret = 0; /* reset ret */
683 /* fall-through when unavailable */
684 }
685#endif
686#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
687 if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
688 #if defined(HAVE_INTEL_QA)
689 return IntelQaSymSha(&sha->asyncDev, hash, NULL, WC_SHA_DIGEST_SIZE);
690 #endif
691 }
692#endif /* WOLFSSL_ASYNC_CRYPT */
693
694 local[sha->buffLen++] = 0x80; /* add 1 */
695
696 /* pad with zeros */
697 if (sha->buffLen > WC_SHA_PAD_SIZE) {
698 XMEMSET(&local[sha->buffLen], 0, WC_SHA_BLOCK_SIZE - sha->buffLen);
699 sha->buffLen += WC_SHA_BLOCK_SIZE - sha->buffLen;
700
701 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
702 ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
703 #endif
704
705 #if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
706 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
707 if (sha->ctx.mode == ESP32_SHA_INIT) {
708 esp_sha_try_hw_lock(&sha->ctx);
709 }
710 if (sha->ctx.mode == ESP32_SHA_SW) {
711 ret = XTRANSFORM(sha, (const byte*)local);
712 } else {
713 ret = esp_sha_process(sha, (const byte*)local);
714 }
715 #else
716 ret = XTRANSFORM(sha, (const byte*)local);
717 #endif
718 if (ret != 0)
719 return ret;
720
721 sha->buffLen = 0;
722 }
723 XMEMSET(&local[sha->buffLen], 0, WC_SHA_PAD_SIZE - sha->buffLen);
724
725#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
726 ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
727#endif
728
729 /* store lengths */
730 /* put lengths in bits */
731 sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + (sha->hiLen << 3);
732 sha->loLen = sha->loLen << 3;
733
734 /* ! length ordering dependent on digest endian type ! */
735 XMEMCPY(&local[WC_SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
736 XMEMCPY(&local[WC_SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
737
738#if defined(FREESCALE_MMCAU_SHA)
739 /* Kinetis requires only these bytes reversed */
740 ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
741 &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
742 2 * sizeof(word32));
743#endif
744
745#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
746 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
747 if (sha->ctx.mode == ESP32_SHA_INIT) {
748 esp_sha_try_hw_lock(&sha->ctx);
749 }
750 if (sha->ctx.mode == ESP32_SHA_SW) {
751 ret = XTRANSFORM(sha, (const byte*)local);
752 } else {
753 ret = esp_sha_digest_process(sha, 1);
754 }
755#else
756 ret = XTRANSFORM(sha, (const byte*)local);
757#endif
758
759#ifdef LITTLE_ENDIAN_ORDER
760 ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE);
761#endif
762
763 XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
764
765 (void)InitSha(sha); /* reset state */
766
767 return ret;
768}
769
770#endif /* USE_SHA_SOFTWARE_IMPL */
771
772
773int wc_InitSha(wc_Sha* sha)
774{
775 return wc_InitSha_ex(sha, NULL, INVALID_DEVID);
776}
777
778void wc_ShaFree(wc_Sha* sha)
779{
780 if (sha == NULL)
781 return;
782
783#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
784 wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA);
785#endif /* WOLFSSL_ASYNC_CRYPT */
786
787#ifdef WOLFSSL_PIC32MZ_HASH
788 wc_ShaPic32Free(sha);
789#endif
790#if (defined(WOLFSSL_RENESAS_TSIP_CRYPT) && \
791 !defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH))
792 if (sha->msg != NULL) {
793 XFREE(sha->msg, sha->heap, DYNAMIC_TYPE_TMP_BUFFER);
794 sha->msg = NULL;
795 }
796#endif
797}
798
799#endif /* !WOLFSSL_TI_HASH */
800#endif /* HAVE_FIPS */
801
802#ifndef WOLFSSL_TI_HASH
803#if !defined(WOLFSSL_RENESAS_TSIP_CRYPT) || \
804 defined(NO_WOLFSSL_RENESAS_TSIP_CRYPT_HASH)
805int wc_ShaGetHash(wc_Sha* sha, byte* hash)
806{
807 int ret;
808 wc_Sha tmpSha;
809
810 if (sha == NULL || hash == NULL)
811 return BAD_FUNC_ARG;
812
813#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
814 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
815 if(sha->ctx.mode == ESP32_SHA_INIT){
816 esp_sha_try_hw_lock(&sha->ctx);
817 }
818 if(sha->ctx.mode != ESP32_SHA_SW)
819 esp_sha_digest_process(sha, 0);
820#endif
821
822 ret = wc_ShaCopy(sha, &tmpSha);
823 if (ret == 0) {
824 ret = wc_ShaFinal(&tmpSha, hash);
825#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
826 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
827 sha->ctx.mode = ESP32_SHA_SW;
828#endif
829
830
831 }
832 return ret;
833}
834
835int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
836{
837 int ret = 0;
838
839 if (src == NULL || dst == NULL)
840 return BAD_FUNC_ARG;
841
842 XMEMCPY(dst, src, sizeof(wc_Sha));
843
844#ifdef WOLFSSL_ASYNC_CRYPT
845 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
846#endif
847#ifdef WOLFSSL_PIC32MZ_HASH
848 ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
849#endif
850#if defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
851 !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
852 dst->ctx.mode = src->ctx.mode;
853 dst->ctx.isfirstblock = src->ctx.isfirstblock;
854 dst->ctx.sha_type = src->ctx.sha_type;
855#endif
856#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
857 dst->flags |= WC_HASH_FLAG_ISCOPY;
858#endif
859 return ret;
860}
861#endif /* defined(WOLFSSL_RENESAS_TSIP_CRYPT) ... */
862#endif /* !WOLFSSL_TI_HASH */
863
864
865#if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
866int wc_ShaSetFlags(wc_Sha* sha, word32 flags)
867{
868 if (sha) {
869 sha->flags = flags;
870 }
871 return 0;
872}
873int wc_ShaGetFlags(wc_Sha* sha, word32* flags)
874{
875 if (sha && flags) {
876 *flags = sha->flags;
877 }
878 return 0;
879}
880#endif
881
882#endif /* !NO_SHA */
Note: See TracBrowser for help on using the repository browser.