cyassl re-port with cellular comms, PSK test

Dependencies:   VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src

Committer:
ashleymills
Date:
Fri Apr 26 16:59:36 2013 +0000
Revision:
1:b211d97b0068
Parent:
0:e979170e02e7
nothing

Who changed what in which revision?

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