Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Fri Jun 26 00:39:20 2015 +0000
Revision:
0:d92f9d21154c
wolfSSL 3.6.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:d92f9d21154c 1 /* sha.c
wolfSSL 0:d92f9d21154c 2 *
wolfSSL 0:d92f9d21154c 3 * Copyright (C) 2006-2015 wolfSSL Inc.
wolfSSL 0:d92f9d21154c 4 *
wolfSSL 0:d92f9d21154c 5 * This file is part of wolfSSL. (formerly known as CyaSSL)
wolfSSL 0:d92f9d21154c 6 *
wolfSSL 0:d92f9d21154c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 0:d92f9d21154c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:d92f9d21154c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:d92f9d21154c 10 * (at your option) any later version.
wolfSSL 0:d92f9d21154c 11 *
wolfSSL 0:d92f9d21154c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 0:d92f9d21154c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:d92f9d21154c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:d92f9d21154c 15 * GNU General Public License for more details.
wolfSSL 0:d92f9d21154c 16 *
wolfSSL 0:d92f9d21154c 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:d92f9d21154c 18 * along with this program; if not, write to the Free Software
wolfSSL 0:d92f9d21154c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:d92f9d21154c 20 */
wolfSSL 0:d92f9d21154c 21
wolfSSL 0:d92f9d21154c 22
wolfSSL 0:d92f9d21154c 23 #ifdef HAVE_CONFIG_H
wolfSSL 0:d92f9d21154c 24 #include <config.h>
wolfSSL 0:d92f9d21154c 25 #endif
wolfSSL 0:d92f9d21154c 26
wolfSSL 0:d92f9d21154c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 0:d92f9d21154c 28
wolfSSL 0:d92f9d21154c 29 #if !defined(NO_SHA)
wolfSSL 0:d92f9d21154c 30
wolfSSL 0:d92f9d21154c 31 #include <wolfssl/wolfcrypt/sha.h>
wolfSSL 0:d92f9d21154c 32 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 0:d92f9d21154c 33 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 0:d92f9d21154c 34
wolfSSL 0:d92f9d21154c 35 #ifdef NO_INLINE
wolfSSL 0:d92f9d21154c 36 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 0:d92f9d21154c 37 #else
wolfSSL 0:d92f9d21154c 38 #include <wolfcrypt/src/misc.c>
wolfSSL 0:d92f9d21154c 39 #endif
wolfSSL 0:d92f9d21154c 40
wolfSSL 0:d92f9d21154c 41 /* fips wrapper calls, user can call direct */
wolfSSL 0:d92f9d21154c 42 #ifdef HAVE_FIPS
wolfSSL 0:d92f9d21154c 43 int wc_InitSha(Sha* sha)
wolfSSL 0:d92f9d21154c 44 {
wolfSSL 0:d92f9d21154c 45 return InitSha_fips(sha);
wolfSSL 0:d92f9d21154c 46 }
wolfSSL 0:d92f9d21154c 47
wolfSSL 0:d92f9d21154c 48
wolfSSL 0:d92f9d21154c 49 int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
wolfSSL 0:d92f9d21154c 50 {
wolfSSL 0:d92f9d21154c 51 return ShaUpdate_fips(sha, data, len);
wolfSSL 0:d92f9d21154c 52 }
wolfSSL 0:d92f9d21154c 53
wolfSSL 0:d92f9d21154c 54
wolfSSL 0:d92f9d21154c 55 int wc_ShaFinal(Sha* sha, byte* out)
wolfSSL 0:d92f9d21154c 56 {
wolfSSL 0:d92f9d21154c 57 return ShaFinal_fips(sha,out);
wolfSSL 0:d92f9d21154c 58 }
wolfSSL 0:d92f9d21154c 59
wolfSSL 0:d92f9d21154c 60 int wc_ShaHash(const byte* data, word32 sz, byte* out)
wolfSSL 0:d92f9d21154c 61 {
wolfSSL 0:d92f9d21154c 62 return ShaHash(data, sz, out);
wolfSSL 0:d92f9d21154c 63 }
wolfSSL 0:d92f9d21154c 64
wolfSSL 0:d92f9d21154c 65 #else /* else build without fips */
wolfSSL 0:d92f9d21154c 66
wolfSSL 0:d92f9d21154c 67 #if defined(WOLFSSL_TI_HASH)
wolfSSL 0:d92f9d21154c 68 /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
wolfSSL 0:d92f9d21154c 69 #else
wolfSSL 0:d92f9d21154c 70
wolfSSL 0:d92f9d21154c 71 #ifdef WOLFSSL_PIC32MZ_HASH
wolfSSL 0:d92f9d21154c 72 #define wc_InitSha wc_InitSha_sw
wolfSSL 0:d92f9d21154c 73 #define wc_ShaUpdate wc_ShaUpdate_sw
wolfSSL 0:d92f9d21154c 74 #define wc_ShaFinal wc_ShaFinal_sw
wolfSSL 0:d92f9d21154c 75 #endif
wolfSSL 0:d92f9d21154c 76
wolfSSL 0:d92f9d21154c 77
wolfSSL 0:d92f9d21154c 78 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 79 #include "cau_api.h"
wolfSSL 0:d92f9d21154c 80 #define XTRANSFORM(S,B) cau_sha1_hash_n((B), 1, ((S))->digest)
wolfSSL 0:d92f9d21154c 81 #else
wolfSSL 0:d92f9d21154c 82 #define XTRANSFORM(S,B) Transform((S))
wolfSSL 0:d92f9d21154c 83 #endif
wolfSSL 0:d92f9d21154c 84
wolfSSL 0:d92f9d21154c 85 #ifdef STM32F2_HASH
wolfSSL 0:d92f9d21154c 86 /*
wolfSSL 0:d92f9d21154c 87 * STM32F2 hardware SHA1 support through the STM32F2 standard peripheral
wolfSSL 0:d92f9d21154c 88 * library. Documentation located in STM32F2xx Standard Peripheral Library
wolfSSL 0:d92f9d21154c 89 * document (See note in README).
wolfSSL 0:d92f9d21154c 90 */
wolfSSL 0:d92f9d21154c 91 #include "stm32f2xx.h"
wolfSSL 0:d92f9d21154c 92 #include "stm32f2xx_hash.h"
wolfSSL 0:d92f9d21154c 93
wolfSSL 0:d92f9d21154c 94 int wc_InitSha(Sha* sha)
wolfSSL 0:d92f9d21154c 95 {
wolfSSL 0:d92f9d21154c 96 /* STM32F2 struct notes:
wolfSSL 0:d92f9d21154c 97 * sha->buffer = first 4 bytes used to hold partial block if needed
wolfSSL 0:d92f9d21154c 98 * sha->buffLen = num bytes currently stored in sha->buffer
wolfSSL 0:d92f9d21154c 99 * sha->loLen = num bytes that have been written to STM32 FIFO
wolfSSL 0:d92f9d21154c 100 */
wolfSSL 0:d92f9d21154c 101 XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
wolfSSL 0:d92f9d21154c 102 sha->buffLen = 0;
wolfSSL 0:d92f9d21154c 103 sha->loLen = 0;
wolfSSL 0:d92f9d21154c 104
wolfSSL 0:d92f9d21154c 105 /* initialize HASH peripheral */
wolfSSL 0:d92f9d21154c 106 HASH_DeInit();
wolfSSL 0:d92f9d21154c 107
wolfSSL 0:d92f9d21154c 108 /* configure algo used, algo mode, datatype */
wolfSSL 0:d92f9d21154c 109 HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
wolfSSL 0:d92f9d21154c 110 HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH
wolfSSL 0:d92f9d21154c 111 | HASH_DataType_8b);
wolfSSL 0:d92f9d21154c 112
wolfSSL 0:d92f9d21154c 113 /* reset HASH processor */
wolfSSL 0:d92f9d21154c 114 HASH->CR |= HASH_CR_INIT;
wolfSSL 0:d92f9d21154c 115
wolfSSL 0:d92f9d21154c 116 return 0;
wolfSSL 0:d92f9d21154c 117 }
wolfSSL 0:d92f9d21154c 118
wolfSSL 0:d92f9d21154c 119 int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
wolfSSL 0:d92f9d21154c 120 {
wolfSSL 0:d92f9d21154c 121 word32 i = 0;
wolfSSL 0:d92f9d21154c 122 word32 fill = 0;
wolfSSL 0:d92f9d21154c 123 word32 diff = 0;
wolfSSL 0:d92f9d21154c 124
wolfSSL 0:d92f9d21154c 125 /* if saved partial block is available */
wolfSSL 0:d92f9d21154c 126 if (sha->buffLen) {
wolfSSL 0:d92f9d21154c 127 fill = 4 - sha->buffLen;
wolfSSL 0:d92f9d21154c 128
wolfSSL 0:d92f9d21154c 129 /* if enough data to fill, fill and push to FIFO */
wolfSSL 0:d92f9d21154c 130 if (fill <= len) {
wolfSSL 0:d92f9d21154c 131 XMEMCPY((byte*)sha->buffer + sha->buffLen, data, fill);
wolfSSL 0:d92f9d21154c 132 HASH_DataIn(*(uint32_t*)sha->buffer);
wolfSSL 0:d92f9d21154c 133
wolfSSL 0:d92f9d21154c 134 data += fill;
wolfSSL 0:d92f9d21154c 135 len -= fill;
wolfSSL 0:d92f9d21154c 136 sha->loLen += 4;
wolfSSL 0:d92f9d21154c 137 sha->buffLen = 0;
wolfSSL 0:d92f9d21154c 138 } else {
wolfSSL 0:d92f9d21154c 139 /* append partial to existing stored block */
wolfSSL 0:d92f9d21154c 140 XMEMCPY((byte*)sha->buffer + sha->buffLen, data, len);
wolfSSL 0:d92f9d21154c 141 sha->buffLen += len;
wolfSSL 0:d92f9d21154c 142 return;
wolfSSL 0:d92f9d21154c 143 }
wolfSSL 0:d92f9d21154c 144 }
wolfSSL 0:d92f9d21154c 145
wolfSSL 0:d92f9d21154c 146 /* write input block in the IN FIFO */
wolfSSL 0:d92f9d21154c 147 for(i = 0; i < len; i += 4)
wolfSSL 0:d92f9d21154c 148 {
wolfSSL 0:d92f9d21154c 149 diff = len - i;
wolfSSL 0:d92f9d21154c 150 if ( diff < 4) {
wolfSSL 0:d92f9d21154c 151 /* store incomplete last block, not yet in FIFO */
wolfSSL 0:d92f9d21154c 152 XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
wolfSSL 0:d92f9d21154c 153 XMEMCPY((byte*)sha->buffer, data, diff);
wolfSSL 0:d92f9d21154c 154 sha->buffLen = diff;
wolfSSL 0:d92f9d21154c 155 } else {
wolfSSL 0:d92f9d21154c 156 HASH_DataIn(*(uint32_t*)data);
wolfSSL 0:d92f9d21154c 157 data+=4;
wolfSSL 0:d92f9d21154c 158 }
wolfSSL 0:d92f9d21154c 159 }
wolfSSL 0:d92f9d21154c 160
wolfSSL 0:d92f9d21154c 161 /* keep track of total data length thus far */
wolfSSL 0:d92f9d21154c 162 sha->loLen += (len - sha->buffLen);
wolfSSL 0:d92f9d21154c 163
wolfSSL 0:d92f9d21154c 164 return 0;
wolfSSL 0:d92f9d21154c 165 }
wolfSSL 0:d92f9d21154c 166
wolfSSL 0:d92f9d21154c 167 int wc_ShaFinal(Sha* sha, byte* hash)
wolfSSL 0:d92f9d21154c 168 {
wolfSSL 0:d92f9d21154c 169 __IO uint16_t nbvalidbitsdata = 0;
wolfSSL 0:d92f9d21154c 170
wolfSSL 0:d92f9d21154c 171 /* finish reading any trailing bytes into FIFO */
wolfSSL 0:d92f9d21154c 172 if (sha->buffLen) {
wolfSSL 0:d92f9d21154c 173 HASH_DataIn(*(uint32_t*)sha->buffer);
wolfSSL 0:d92f9d21154c 174 sha->loLen += sha->buffLen;
wolfSSL 0:d92f9d21154c 175 }
wolfSSL 0:d92f9d21154c 176
wolfSSL 0:d92f9d21154c 177 /* calculate number of valid bits in last word of input data */
wolfSSL 0:d92f9d21154c 178 nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE);
wolfSSL 0:d92f9d21154c 179
wolfSSL 0:d92f9d21154c 180 /* configure number of valid bits in last word of the data */
wolfSSL 0:d92f9d21154c 181 HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
wolfSSL 0:d92f9d21154c 182
wolfSSL 0:d92f9d21154c 183 /* start HASH processor */
wolfSSL 0:d92f9d21154c 184 HASH_StartDigest();
wolfSSL 0:d92f9d21154c 185
wolfSSL 0:d92f9d21154c 186 /* wait until Busy flag == RESET */
wolfSSL 0:d92f9d21154c 187 while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
wolfSSL 0:d92f9d21154c 188
wolfSSL 0:d92f9d21154c 189 /* read message digest */
wolfSSL 0:d92f9d21154c 190 sha->digest[0] = HASH->HR[0];
wolfSSL 0:d92f9d21154c 191 sha->digest[1] = HASH->HR[1];
wolfSSL 0:d92f9d21154c 192 sha->digest[2] = HASH->HR[2];
wolfSSL 0:d92f9d21154c 193 sha->digest[3] = HASH->HR[3];
wolfSSL 0:d92f9d21154c 194 sha->digest[4] = HASH->HR[4];
wolfSSL 0:d92f9d21154c 195
wolfSSL 0:d92f9d21154c 196 ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
wolfSSL 0:d92f9d21154c 197
wolfSSL 0:d92f9d21154c 198 XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
wolfSSL 0:d92f9d21154c 199
wolfSSL 0:d92f9d21154c 200 return wc_InitSha(sha); /* reset state */
wolfSSL 0:d92f9d21154c 201 }
wolfSSL 0:d92f9d21154c 202
wolfSSL 0:d92f9d21154c 203 #else /* wc_ software implementation */
wolfSSL 0:d92f9d21154c 204
wolfSSL 0:d92f9d21154c 205 #ifndef WOLFSSL_HAVE_MIN
wolfSSL 0:d92f9d21154c 206 #define WOLFSSL_HAVE_MIN
wolfSSL 0:d92f9d21154c 207
wolfSSL 0:d92f9d21154c 208 static INLINE word32 min(word32 a, word32 b)
wolfSSL 0:d92f9d21154c 209 {
wolfSSL 0:d92f9d21154c 210 return a > b ? b : a;
wolfSSL 0:d92f9d21154c 211 }
wolfSSL 0:d92f9d21154c 212
wolfSSL 0:d92f9d21154c 213 #endif /* WOLFSSL_HAVE_MIN */
wolfSSL 0:d92f9d21154c 214
wolfSSL 0:d92f9d21154c 215
wolfSSL 0:d92f9d21154c 216 int wc_InitSha(Sha* sha)
wolfSSL 0:d92f9d21154c 217 {
wolfSSL 0:d92f9d21154c 218 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 219 cau_sha1_initialize_output(sha->digest);
wolfSSL 0:d92f9d21154c 220 #else
wolfSSL 0:d92f9d21154c 221 sha->digest[0] = 0x67452301L;
wolfSSL 0:d92f9d21154c 222 sha->digest[1] = 0xEFCDAB89L;
wolfSSL 0:d92f9d21154c 223 sha->digest[2] = 0x98BADCFEL;
wolfSSL 0:d92f9d21154c 224 sha->digest[3] = 0x10325476L;
wolfSSL 0:d92f9d21154c 225 sha->digest[4] = 0xC3D2E1F0L;
wolfSSL 0:d92f9d21154c 226 #endif
wolfSSL 0:d92f9d21154c 227
wolfSSL 0:d92f9d21154c 228 sha->buffLen = 0;
wolfSSL 0:d92f9d21154c 229 sha->loLen = 0;
wolfSSL 0:d92f9d21154c 230 sha->hiLen = 0;
wolfSSL 0:d92f9d21154c 231
wolfSSL 0:d92f9d21154c 232 return 0;
wolfSSL 0:d92f9d21154c 233 }
wolfSSL 0:d92f9d21154c 234
wolfSSL 0:d92f9d21154c 235 #ifndef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 236
wolfSSL 0:d92f9d21154c 237 #define blk0(i) (W[i] = sha->buffer[i])
wolfSSL 0:d92f9d21154c 238 #define blk1(i) (W[(i)&15] = \
wolfSSL 0:d92f9d21154c 239 rotlFixed(W[((i)+13)&15]^W[((i)+8)&15]^W[((i)+2)&15]^W[(i)&15],1))
wolfSSL 0:d92f9d21154c 240
wolfSSL 0:d92f9d21154c 241 #define f1(x,y,z) ((z)^((x) &((y)^(z))))
wolfSSL 0:d92f9d21154c 242 #define f2(x,y,z) ((x)^(y)^(z))
wolfSSL 0:d92f9d21154c 243 #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y))))
wolfSSL 0:d92f9d21154c 244 #define f4(x,y,z) ((x)^(y)^(z))
wolfSSL 0:d92f9d21154c 245
wolfSSL 0:d92f9d21154c 246 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
wolfSSL 0:d92f9d21154c 247 #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \
wolfSSL 0:d92f9d21154c 248 rotlFixed((v),5); (w) = rotlFixed((w),30);
wolfSSL 0:d92f9d21154c 249 #define R1(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk1((i)) + 0x5A827999+ \
wolfSSL 0:d92f9d21154c 250 rotlFixed((v),5); (w) = rotlFixed((w),30);
wolfSSL 0:d92f9d21154c 251 #define R2(v,w,x,y,z,i) (z)+= f2((w),(x),(y)) + blk1((i)) + 0x6ED9EBA1+ \
wolfSSL 0:d92f9d21154c 252 rotlFixed((v),5); (w) = rotlFixed((w),30);
wolfSSL 0:d92f9d21154c 253 #define R3(v,w,x,y,z,i) (z)+= f3((w),(x),(y)) + blk1((i)) + 0x8F1BBCDC+ \
wolfSSL 0:d92f9d21154c 254 rotlFixed((v),5); (w) = rotlFixed((w),30);
wolfSSL 0:d92f9d21154c 255 #define R4(v,w,x,y,z,i) (z)+= f4((w),(x),(y)) + blk1((i)) + 0xCA62C1D6+ \
wolfSSL 0:d92f9d21154c 256 rotlFixed((v),5); (w) = rotlFixed((w),30);
wolfSSL 0:d92f9d21154c 257
wolfSSL 0:d92f9d21154c 258 static void Transform(Sha* sha)
wolfSSL 0:d92f9d21154c 259 {
wolfSSL 0:d92f9d21154c 260 word32 W[SHA_BLOCK_SIZE / sizeof(word32)];
wolfSSL 0:d92f9d21154c 261
wolfSSL 0:d92f9d21154c 262 /* Copy context->state[] to working vars */
wolfSSL 0:d92f9d21154c 263 word32 a = sha->digest[0];
wolfSSL 0:d92f9d21154c 264 word32 b = sha->digest[1];
wolfSSL 0:d92f9d21154c 265 word32 c = sha->digest[2];
wolfSSL 0:d92f9d21154c 266 word32 d = sha->digest[3];
wolfSSL 0:d92f9d21154c 267 word32 e = sha->digest[4];
wolfSSL 0:d92f9d21154c 268
wolfSSL 0:d92f9d21154c 269 #ifdef USE_SLOW_SHA
wolfSSL 0:d92f9d21154c 270 word32 t, i;
wolfSSL 0:d92f9d21154c 271
wolfSSL 0:d92f9d21154c 272 for (i = 0; i < 16; i++) {
wolfSSL 0:d92f9d21154c 273 R0(a, b, c, d, e, i);
wolfSSL 0:d92f9d21154c 274 t = e; e = d; d = c; c = b; b = a; a = t;
wolfSSL 0:d92f9d21154c 275 }
wolfSSL 0:d92f9d21154c 276
wolfSSL 0:d92f9d21154c 277 for (; i < 20; i++) {
wolfSSL 0:d92f9d21154c 278 R1(a, b, c, d, e, i);
wolfSSL 0:d92f9d21154c 279 t = e; e = d; d = c; c = b; b = a; a = t;
wolfSSL 0:d92f9d21154c 280 }
wolfSSL 0:d92f9d21154c 281
wolfSSL 0:d92f9d21154c 282 for (; i < 40; i++) {
wolfSSL 0:d92f9d21154c 283 R2(a, b, c, d, e, i);
wolfSSL 0:d92f9d21154c 284 t = e; e = d; d = c; c = b; b = a; a = t;
wolfSSL 0:d92f9d21154c 285 }
wolfSSL 0:d92f9d21154c 286
wolfSSL 0:d92f9d21154c 287 for (; i < 60; i++) {
wolfSSL 0:d92f9d21154c 288 R3(a, b, c, d, e, i);
wolfSSL 0:d92f9d21154c 289 t = e; e = d; d = c; c = b; b = a; a = t;
wolfSSL 0:d92f9d21154c 290 }
wolfSSL 0:d92f9d21154c 291
wolfSSL 0:d92f9d21154c 292 for (; i < 80; i++) {
wolfSSL 0:d92f9d21154c 293 R4(a, b, c, d, e, i);
wolfSSL 0:d92f9d21154c 294 t = e; e = d; d = c; c = b; b = a; a = t;
wolfSSL 0:d92f9d21154c 295 }
wolfSSL 0:d92f9d21154c 296 #else
wolfSSL 0:d92f9d21154c 297 /* nearly 1 K bigger in code size but 25% faster */
wolfSSL 0:d92f9d21154c 298 /* 4 rounds of 20 operations each. Loop unrolled. */
wolfSSL 0:d92f9d21154c 299 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 0:d92f9d21154c 300 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 0:d92f9d21154c 301 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 0:d92f9d21154c 302 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 0:d92f9d21154c 303
wolfSSL 0:d92f9d21154c 304 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 0:d92f9d21154c 305
wolfSSL 0:d92f9d21154c 306 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 0:d92f9d21154c 307 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 0:d92f9d21154c 308 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 0:d92f9d21154c 309 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 0:d92f9d21154c 310 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 0:d92f9d21154c 311
wolfSSL 0:d92f9d21154c 312 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 0:d92f9d21154c 313 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 0:d92f9d21154c 314 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 0:d92f9d21154c 315 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 0:d92f9d21154c 316 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 0:d92f9d21154c 317
wolfSSL 0:d92f9d21154c 318 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 0:d92f9d21154c 319 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 0:d92f9d21154c 320 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 0:d92f9d21154c 321 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 0:d92f9d21154c 322 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 0:d92f9d21154c 323 #endif
wolfSSL 0:d92f9d21154c 324
wolfSSL 0:d92f9d21154c 325 /* Add the working vars back into digest state[] */
wolfSSL 0:d92f9d21154c 326 sha->digest[0] += a;
wolfSSL 0:d92f9d21154c 327 sha->digest[1] += b;
wolfSSL 0:d92f9d21154c 328 sha->digest[2] += c;
wolfSSL 0:d92f9d21154c 329 sha->digest[3] += d;
wolfSSL 0:d92f9d21154c 330 sha->digest[4] += e;
wolfSSL 0:d92f9d21154c 331 }
wolfSSL 0:d92f9d21154c 332
wolfSSL 0:d92f9d21154c 333 #endif /* FREESCALE_MMCAU */
wolfSSL 0:d92f9d21154c 334
wolfSSL 0:d92f9d21154c 335
wolfSSL 0:d92f9d21154c 336 static INLINE void AddLength(Sha* sha, word32 len)
wolfSSL 0:d92f9d21154c 337 {
wolfSSL 0:d92f9d21154c 338 word32 tmp = sha->loLen;
wolfSSL 0:d92f9d21154c 339 if ( (sha->loLen += len) < tmp)
wolfSSL 0:d92f9d21154c 340 sha->hiLen++; /* carry low to high */
wolfSSL 0:d92f9d21154c 341 }
wolfSSL 0:d92f9d21154c 342
wolfSSL 0:d92f9d21154c 343
wolfSSL 0:d92f9d21154c 344 int wc_ShaUpdate(Sha* sha, const byte* data, word32 len)
wolfSSL 0:d92f9d21154c 345 {
wolfSSL 0:d92f9d21154c 346 /* do block size increments */
wolfSSL 0:d92f9d21154c 347 byte* local = (byte*)sha->buffer;
wolfSSL 0:d92f9d21154c 348
wolfSSL 0:d92f9d21154c 349 while (len) {
wolfSSL 0:d92f9d21154c 350 word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
wolfSSL 0:d92f9d21154c 351 XMEMCPY(&local[sha->buffLen], data, add);
wolfSSL 0:d92f9d21154c 352
wolfSSL 0:d92f9d21154c 353 sha->buffLen += add;
wolfSSL 0:d92f9d21154c 354 data += add;
wolfSSL 0:d92f9d21154c 355 len -= add;
wolfSSL 0:d92f9d21154c 356
wolfSSL 0:d92f9d21154c 357 if (sha->buffLen == SHA_BLOCK_SIZE) {
wolfSSL 0:d92f9d21154c 358 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 359 ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 360 #endif
wolfSSL 0:d92f9d21154c 361 XTRANSFORM(sha, local);
wolfSSL 0:d92f9d21154c 362 AddLength(sha, SHA_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 363 sha->buffLen = 0;
wolfSSL 0:d92f9d21154c 364 }
wolfSSL 0:d92f9d21154c 365 }
wolfSSL 0:d92f9d21154c 366
wolfSSL 0:d92f9d21154c 367 return 0;
wolfSSL 0:d92f9d21154c 368 }
wolfSSL 0:d92f9d21154c 369
wolfSSL 0:d92f9d21154c 370
wolfSSL 0:d92f9d21154c 371 int wc_ShaFinal(Sha* sha, byte* hash)
wolfSSL 0:d92f9d21154c 372 {
wolfSSL 0:d92f9d21154c 373 byte* local = (byte*)sha->buffer;
wolfSSL 0:d92f9d21154c 374
wolfSSL 0:d92f9d21154c 375 AddLength(sha, sha->buffLen); /* before adding pads */
wolfSSL 0:d92f9d21154c 376
wolfSSL 0:d92f9d21154c 377 local[sha->buffLen++] = 0x80; /* add 1 */
wolfSSL 0:d92f9d21154c 378
wolfSSL 0:d92f9d21154c 379 /* pad with zeros */
wolfSSL 0:d92f9d21154c 380 if (sha->buffLen > SHA_PAD_SIZE) {
wolfSSL 0:d92f9d21154c 381 XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
wolfSSL 0:d92f9d21154c 382 sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
wolfSSL 0:d92f9d21154c 383
wolfSSL 0:d92f9d21154c 384 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 385 ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 386 #endif
wolfSSL 0:d92f9d21154c 387 XTRANSFORM(sha, local);
wolfSSL 0:d92f9d21154c 388 sha->buffLen = 0;
wolfSSL 0:d92f9d21154c 389 }
wolfSSL 0:d92f9d21154c 390 XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
wolfSSL 0:d92f9d21154c 391
wolfSSL 0:d92f9d21154c 392 /* put lengths in bits */
wolfSSL 0:d92f9d21154c 393 sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) +
wolfSSL 0:d92f9d21154c 394 (sha->hiLen << 3);
wolfSSL 0:d92f9d21154c 395 sha->loLen = sha->loLen << 3;
wolfSSL 0:d92f9d21154c 396
wolfSSL 0:d92f9d21154c 397 /* store lengths */
wolfSSL 0:d92f9d21154c 398 #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
wolfSSL 0:d92f9d21154c 399 ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
wolfSSL 0:d92f9d21154c 400 #endif
wolfSSL 0:d92f9d21154c 401 /* ! length ordering dependent on digest endian type ! */
wolfSSL 0:d92f9d21154c 402 XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
wolfSSL 0:d92f9d21154c 403 XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
wolfSSL 0:d92f9d21154c 404
wolfSSL 0:d92f9d21154c 405 #ifdef FREESCALE_MMCAU
wolfSSL 0:d92f9d21154c 406 /* Kinetis requires only these bytes reversed */
wolfSSL 0:d92f9d21154c 407 ByteReverseWords(&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
wolfSSL 0:d92f9d21154c 408 &sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
wolfSSL 0:d92f9d21154c 409 2 * sizeof(word32));
wolfSSL 0:d92f9d21154c 410 #endif
wolfSSL 0:d92f9d21154c 411
wolfSSL 0:d92f9d21154c 412 XTRANSFORM(sha, local);
wolfSSL 0:d92f9d21154c 413 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:d92f9d21154c 414 ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
wolfSSL 0:d92f9d21154c 415 #endif
wolfSSL 0:d92f9d21154c 416 XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
wolfSSL 0:d92f9d21154c 417
wolfSSL 0:d92f9d21154c 418 return wc_InitSha(sha); /* reset state */
wolfSSL 0:d92f9d21154c 419 }
wolfSSL 0:d92f9d21154c 420
wolfSSL 0:d92f9d21154c 421 #endif /* STM32F2_HASH */
wolfSSL 0:d92f9d21154c 422
wolfSSL 0:d92f9d21154c 423
wolfSSL 0:d92f9d21154c 424 int wc_ShaHash(const byte* data, word32 len, byte* hash)
wolfSSL 0:d92f9d21154c 425 {
wolfSSL 0:d92f9d21154c 426 int ret = 0;
wolfSSL 0:d92f9d21154c 427 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 428 Sha* sha;
wolfSSL 0:d92f9d21154c 429 #else
wolfSSL 0:d92f9d21154c 430 Sha sha[1];
wolfSSL 0:d92f9d21154c 431 #endif
wolfSSL 0:d92f9d21154c 432
wolfSSL 0:d92f9d21154c 433 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 434 sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 435 if (sha == NULL)
wolfSSL 0:d92f9d21154c 436 return MEMORY_E;
wolfSSL 0:d92f9d21154c 437 #endif
wolfSSL 0:d92f9d21154c 438
wolfSSL 0:d92f9d21154c 439 if ((ret = wc_InitSha(sha)) != 0) {
wolfSSL 0:d92f9d21154c 440 WOLFSSL_MSG("wc_InitSha failed");
wolfSSL 0:d92f9d21154c 441 }
wolfSSL 0:d92f9d21154c 442 else {
wolfSSL 0:d92f9d21154c 443 wc_ShaUpdate(sha, data, len);
wolfSSL 0:d92f9d21154c 444 wc_ShaFinal(sha, hash);
wolfSSL 0:d92f9d21154c 445 }
wolfSSL 0:d92f9d21154c 446
wolfSSL 0:d92f9d21154c 447 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 0:d92f9d21154c 448 XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:d92f9d21154c 449 #endif
wolfSSL 0:d92f9d21154c 450
wolfSSL 0:d92f9d21154c 451 return ret;
wolfSSL 0:d92f9d21154c 452
wolfSSL 0:d92f9d21154c 453 }
wolfSSL 0:d92f9d21154c 454
wolfSSL 0:d92f9d21154c 455 #endif /* HAVE_FIPS */
wolfSSL 0:d92f9d21154c 456 #endif /* WOLFSSL_TI_HASH */
wolfSSL 0:d92f9d21154c 457 #endif /* NO_SHA */
wolfSSL 0:d92f9d21154c 458
wolfSSL 0:d92f9d21154c 459