Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Sat Aug 18 22:20:43 2018 +0000
Revision:
15:117db924cf7c
wolfSSL 3.15.3

Who changed what in which revision?

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