source: asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/sha.c@ 372

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

wolfsslを3.15.7にバージョンアップ

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 16.1 KB
Line 
1/* sha.c
2 *
3 * Copyright (C) 2006-2017 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
46/* fips wrapper calls, user can call direct */
47#if defined(HAVE_FIPS) && \
48 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
49
50 int wc_InitSha(wc_Sha* sha)
51 {
52 if (sha == NULL) {
53 return BAD_FUNC_ARG;
54 }
55 return InitSha_fips(sha);
56 }
57 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
58 {
59 (void)heap;
60 (void)devId;
61 if (sha == NULL) {
62 return BAD_FUNC_ARG;
63 }
64 return InitSha_fips(sha);
65 }
66
67 int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
68 {
69 if (sha == NULL || (data == NULL && len > 0)) {
70 return BAD_FUNC_ARG;
71 }
72 return ShaUpdate_fips(sha, data, len);
73 }
74
75 int wc_ShaFinal(wc_Sha* sha, byte* out)
76 {
77 if (sha == NULL || out == NULL) {
78 return BAD_FUNC_ARG;
79 }
80 return ShaFinal_fips(sha,out);
81 }
82 void wc_ShaFree(wc_Sha* sha)
83 {
84 (void)sha;
85 /* Not supported in FIPS */
86 }
87
88#else /* else build without fips, or for FIPS v2 */
89
90
91#if defined(WOLFSSL_TI_HASH)
92 /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
93
94#else
95
96#include <wolfssl/wolfcrypt/logging.h>
97#ifdef NO_INLINE
98 #include <wolfssl/wolfcrypt/misc.h>
99#else
100 #define WOLFSSL_MISC_INCLUDED
101 #include <wolfcrypt/src/misc.c>
102#endif
103
104
105/* Hardware Acceleration */
106#if defined(WOLFSSL_PIC32MZ_HASH)
107 #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
108
109#elif defined(STM32_HASH)
110
111 /* Supports CubeMX HAL or Standard Peripheral Library */
112 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
113 {
114 if (sha == NULL) {
115 return BAD_FUNC_ARG;
116 }
117
118 (void)devId;
119 (void)heap;
120
121 wc_Stm32_Hash_Init(&sha->stmCtx);
122
123 return 0;
124 }
125
126 int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
127 {
128 int ret;
129
130 if (sha == NULL || (data == NULL && len > 0)) {
131 return BAD_FUNC_ARG;
132 }
133
134 ret = wolfSSL_CryptHwMutexLock();
135 if (ret == 0) {
136 ret = wc_Stm32_Hash_Update(&sha->stmCtx, HASH_AlgoSelection_SHA1,
137 data, len);
138 wolfSSL_CryptHwMutexUnLock();
139 }
140 return ret;
141 }
142
143 int wc_ShaFinal(wc_Sha* sha, byte* hash)
144 {
145 int ret;
146
147 if (sha == NULL || hash == NULL) {
148 return BAD_FUNC_ARG;
149 }
150
151 ret = wolfSSL_CryptHwMutexLock();
152 if (ret == 0) {
153 ret = wc_Stm32_Hash_Final(&sha->stmCtx, HASH_AlgoSelection_SHA1,
154 hash, WC_SHA_DIGEST_SIZE);
155 wolfSSL_CryptHwMutexUnLock();
156 }
157
158 (void)wc_InitSha(sha); /* reset state */
159
160 return ret;
161 }
162
163
164#elif defined(FREESCALE_LTC_SHA)
165
166 #include "fsl_ltc.h"
167 int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
168 {
169 if (sha == NULL) {
170 return BAD_FUNC_ARG;
171 }
172
173 (void)devId;
174 (void)heap;
175
176 LTC_HASH_Init(LTC_BASE, &sha->ctx, kLTC_Sha1, NULL, 0);
177 return 0;
178 }
179
180 int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
181 {
182 LTC_HASH_Update(&sha->ctx, data, len);
183 return 0;
184 }
185
186 int wc_ShaFinal(wc_Sha* sha, byte* hash)
187 {
188 uint32_t hashlen = WC_SHA_DIGEST_SIZE;
189 LTC_HASH_Finish(&sha->ctx, hash, &hashlen);
190 return wc_InitSha(sha); /* reset state */
191 }
192
193
194#elif defined(FREESCALE_MMCAU_SHA)
195
196 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
197 #include "cau_api.h"
198 #else
199 #include "fsl_mmcau.h"
200 #endif
201
202 #define USE_SHA_SOFTWARE_IMPL /* Only for API's, actual transform is here */
203 #define XTRANSFORM(S,B) Transform((S),(B))
204
205 static int InitSha(wc_Sha* sha)
206 {
207 int ret = 0;
208 ret = wolfSSL_CryptHwMutexLock();
209 if (ret != 0) {
210 return ret;
211 }
212 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
213 cau_sha1_initialize_output(sha->digest);
214 #else
215 MMCAU_SHA1_InitializeOutput((uint32_t*)sha->digest);
216 #endif
217 wolfSSL_CryptHwMutexUnLock();
218
219 sha->buffLen = 0;
220 sha->loLen = 0;
221 sha->hiLen = 0;
222
223 return ret;
224 }
225
226 static int Transform(wc_Sha* sha, byte* data)
227 {
228 int ret = wolfSSL_CryptHwMutexLock();
229 if(ret == 0) {
230 #ifdef FREESCALE_MMCAU_CLASSIC_SHA
231 cau_sha1_hash_n(data, 1, sha->digest);
232 #else
233 MMCAU_SHA1_HashN(data, 1, (uint32_t*)sha->digest);
234 #endif
235 wolfSSL_CryptHwMutexUnLock();
236 }
237 return ret;
238 }
239
240#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
241 /* wolfcrypt/src/port/caam/caam_sha.c */
242#else
243
244 /* Software implementation */
245 #define USE_SHA_SOFTWARE_IMPL
246
247 static int InitSha(wc_Sha* sha)
248 {
249 int ret = 0;
250
251 sha->digest[0] = 0x67452301L;
252 sha->digest[1] = 0xEFCDAB89L;
253 sha->digest[2] = 0x98BADCFEL;
254 sha->digest[3] = 0x10325476L;
255 sha->digest[4] = 0xC3D2E1F0L;
256
257 sha->buffLen = 0;
258 sha->loLen = 0;
259 sha->hiLen = 0;
260
261 return ret;
262 }
263
264#endif /* End Hardware Acceleration */
265
266
267/* Software implementation */
268#ifdef USE_SHA_SOFTWARE_IMPL
269
270static WC_INLINE void AddLength(wc_Sha* sha, word32 len)
271{
272 word32 tmp = sha->loLen;
273 if ((sha->loLen += len) < tmp)
274 sha->hiLen++; /* carry low to high */
275}
276
277/* Check if custom wc_Sha transform is used */
278#ifndef XTRANSFORM
279 #define XTRANSFORM(S,B) Transform((S),(B))
280
281 #define blk0(i) (W[i] = sha->buffer[i])
282 #define blk1(i) (W[(i)&15] = \
283 rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1))
284
285 #define f1(x,y,z) ((z)^((x) &((y)^(z))))
286 #define f2(x,y,z) ((x)^(y)^(z))
287 #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
288 #define f4(x,y,z) ((x)^(y)^(z))
289
290 #ifdef WOLFSSL_NUCLEUS_1_2
291 /* nucleus.h also defines R1-R4 */
292 #undef R1
293 #undef R2
294 #undef R3
295 #undef R4
296 #endif
297
298 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
299 #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
300 rotlFixed((v),5); (w) = rotlFixed((w),30);
301 #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
302 rotlFixed((v),5); (w) = rotlFixed((w),30);
303 #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
304 rotlFixed((v),5); (w) = rotlFixed((w),30);
305 #define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
306 rotlFixed((v),5); (w) = rotlFixed((w),30);
307 #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
308 rotlFixed((v),5); (w) = rotlFixed((w),30);
309
310 static void Transform(wc_Sha* sha, byte* data)
311 {
312 word32 W[WC_SHA_BLOCK_SIZE / sizeof(word32)];
313
314 /* Copy context->state[] to working vars */
315 word32 a = sha->digest[0];
316 word32 b = sha->digest[1];
317 word32 c = sha->digest[2];
318 word32 d = sha->digest[3];
319 word32 e = sha->digest[4];
320
321 #ifdef USE_SLOW_SHA
322 word32 t, i;
323
324 for (i = 0; i < 16; i++) {
325 R0(a, b, c, d, e, i);
326 t = e; e = d; d = c; c = b; b = a; a = t;
327 }
328
329 for (; i < 20; i++) {
330 R1(a, b, c, d, e, i);
331 t = e; e = d; d = c; c = b; b = a; a = t;
332 }
333
334 for (; i < 40; i++) {
335 R2(a, b, c, d, e, i);
336 t = e; e = d; d = c; c = b; b = a; a = t;
337 }
338
339 for (; i < 60; i++) {
340 R3(a, b, c, d, e, i);
341 t = e; e = d; d = c; c = b; b = a; a = t;
342 }
343
344 for (; i < 80; i++) {
345 R4(a, b, c, d, e, i);
346 t = e; e = d; d = c; c = b; b = a; a = t;
347 }
348 #else
349 /* nearly 1 K bigger in code size but 25% faster */
350 /* 4 rounds of 20 operations each. Loop unrolled. */
351 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);
352 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);
353 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);
354 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);
355
356 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);
357
358 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);
359 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);
360 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);
361 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);
362 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);
363
364 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);
365 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);
366 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);
367 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);
368 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);
369
370 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);
371 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);
372 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);
373 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);
374 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);
375 #endif
376
377 /* Add the working vars back into digest state[] */
378 sha->digest[0] += a;
379 sha->digest[1] += b;
380 sha->digest[2] += c;
381 sha->digest[3] += d;
382 sha->digest[4] += e;
383
384 (void)data; /* Not used */
385 }
386#endif /* !USE_CUSTOM_SHA_TRANSFORM */
387
388
389int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
390{
391 int ret = 0;
392
393 if (sha == NULL)
394 return BAD_FUNC_ARG;
395
396 sha->heap = heap;
397
398 ret = InitSha(sha);
399 if (ret != 0)
400 return ret;
401
402#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
403 ret = wolfAsync_DevCtxInit(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA,
404 sha->heap, devId);
405#else
406 (void)devId;
407#endif /* WOLFSSL_ASYNC_CRYPT */
408
409 return ret;
410}
411
412int wc_ShaUpdate(wc_Sha* sha, const byte* data, word32 len)
413{
414 byte* local;
415
416 if (sha == NULL ||(data == NULL && len > 0)) {
417 return BAD_FUNC_ARG;
418 }
419
420 /* do block size increments */
421 local = (byte*)sha->buffer;
422
423#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
424 if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
425 #if defined(HAVE_INTEL_QA)
426 return IntelQaSymSha(&sha->asyncDev, NULL, data, len);
427 #endif
428 }
429#endif /* WOLFSSL_ASYNC_CRYPT */
430
431 /* check that internal buffLen is valid */
432 if (sha->buffLen >= WC_SHA_BLOCK_SIZE)
433 return BUFFER_E;
434
435 while (len) {
436 word32 add = min(len, WC_SHA_BLOCK_SIZE - sha->buffLen);
437 XMEMCPY(&local[sha->buffLen], data, add);
438
439 sha->buffLen += add;
440 data += add;
441 len -= add;
442
443 if (sha->buffLen == WC_SHA_BLOCK_SIZE) {
444#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
445 ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
446#endif
447 XTRANSFORM(sha, local);
448 AddLength(sha, WC_SHA_BLOCK_SIZE);
449 sha->buffLen = 0;
450 }
451 }
452
453 return 0;
454}
455
456int wc_ShaFinalRaw(wc_Sha* sha, byte* hash)
457{
458#ifdef LITTLE_ENDIAN_ORDER
459 word32 digest[WC_SHA_DIGEST_SIZE / sizeof(word32)];
460#endif
461
462 if (sha == NULL || hash == NULL) {
463 return BAD_FUNC_ARG;
464 }
465
466#ifdef LITTLE_ENDIAN_ORDER
467 ByteReverseWords((word32*)digest, (word32*)sha->digest, WC_SHA_DIGEST_SIZE);
468 XMEMCPY(hash, digest, WC_SHA_DIGEST_SIZE);
469#else
470 XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
471#endif
472
473 return 0;
474}
475
476int wc_ShaFinal(wc_Sha* sha, byte* hash)
477{
478 byte* local;
479
480 if (sha == NULL || hash == NULL) {
481 return BAD_FUNC_ARG;
482 }
483
484 local = (byte*)sha->buffer;
485
486#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
487 if (sha->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA) {
488 #if defined(HAVE_INTEL_QA)
489 return IntelQaSymSha(&sha->asyncDev, hash, NULL, WC_SHA_DIGEST_SIZE);
490 #endif
491 }
492#endif /* WOLFSSL_ASYNC_CRYPT */
493
494 AddLength(sha, sha->buffLen); /* before adding pads */
495
496 local[sha->buffLen++] = 0x80; /* add 1 */
497
498 /* pad with zeros */
499 if (sha->buffLen > WC_SHA_PAD_SIZE) {
500 XMEMSET(&local[sha->buffLen], 0, WC_SHA_BLOCK_SIZE - sha->buffLen);
501 sha->buffLen += WC_SHA_BLOCK_SIZE - sha->buffLen;
502
503#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
504 ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
505#endif
506 XTRANSFORM(sha, local);
507 sha->buffLen = 0;
508 }
509 XMEMSET(&local[sha->buffLen], 0, WC_SHA_PAD_SIZE - sha->buffLen);
510
511#if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
512 ByteReverseWords(sha->buffer, sha->buffer, WC_SHA_BLOCK_SIZE);
513#endif
514
515 /* store lengths */
516 /* put lengths in bits */
517 sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + (sha->hiLen << 3);
518 sha->loLen = sha->loLen << 3;
519
520 /* ! length ordering dependent on digest endian type ! */
521 XMEMCPY(&local[WC_SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
522 XMEMCPY(&local[WC_SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
523
524#if defined(FREESCALE_MMCAU_SHA)
525 /* Kinetis requires only these bytes reversed */
526 ByteReverseWords(&sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
527 &sha->buffer[WC_SHA_PAD_SIZE/sizeof(word32)],
528 2 * sizeof(word32));
529#endif
530
531 XTRANSFORM(sha, local);
532#ifdef LITTLE_ENDIAN_ORDER
533 ByteReverseWords(sha->digest, sha->digest, WC_SHA_DIGEST_SIZE);
534#endif
535 XMEMCPY(hash, sha->digest, WC_SHA_DIGEST_SIZE);
536
537 return InitSha(sha); /* reset state */
538}
539
540#endif /* USE_SHA_SOFTWARE_IMPL */
541
542
543int wc_InitSha(wc_Sha* sha)
544{
545 return wc_InitSha_ex(sha, NULL, INVALID_DEVID);
546}
547
548void wc_ShaFree(wc_Sha* sha)
549{
550 if (sha == NULL)
551 return;
552
553#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
554 wolfAsync_DevCtxFree(&sha->asyncDev, WOLFSSL_ASYNC_MARKER_SHA);
555#endif /* WOLFSSL_ASYNC_CRYPT */
556
557#ifdef WOLFSSL_PIC32MZ_HASH
558 wc_ShaPic32Free(sha);
559#endif
560}
561
562#endif /* !WOLFSSL_TI_HASH */
563#endif /* HAVE_FIPS */
564
565#ifndef WOLFSSL_TI_HASH
566int wc_ShaGetHash(wc_Sha* sha, byte* hash)
567{
568 int ret;
569 wc_Sha tmpSha;
570
571 if (sha == NULL || hash == NULL)
572 return BAD_FUNC_ARG;
573
574 ret = wc_ShaCopy(sha, &tmpSha);
575 if (ret == 0) {
576 ret = wc_ShaFinal(&tmpSha, hash);
577 }
578 return ret;
579}
580
581int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
582{
583 int ret = 0;
584
585 if (src == NULL || dst == NULL)
586 return BAD_FUNC_ARG;
587
588 XMEMCPY(dst, src, sizeof(wc_Sha));
589
590#ifdef WOLFSSL_ASYNC_CRYPT
591 ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
592#endif
593#ifdef WOLFSSL_PIC32MZ_HASH
594 ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
595#endif
596
597 return ret;
598}
599#endif /* !WOLFSSL_TI_HASH */
600
601#endif /* !NO_SHA */
Note: See TracBrowser for help on using the repository browser.