Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
d0773d
Date:
Tue Mar 03 22:52:52 2015 +0000
Revision:
4:28ac50e1d49c
Parent:
0:1239e9b70ca2
CyaSSL example

Who changed what in which revision?

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