wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

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