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