Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TLS_cyassl TLS_cyassl
sha.c
00001 /* sha.c 00002 * 00003 * Copyright (C) 2006-2013 wolfSSL Inc. 00004 * 00005 * This file is part of CyaSSL. 00006 * 00007 * CyaSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * CyaSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00020 */ 00021 00022 00023 #ifdef HAVE_CONFIG_H 00024 #include <config.h> 00025 #endif 00026 00027 #include <cyassl/ctaocrypt/settings.h> 00028 00029 //#ifndef NO_SHA 00030 00031 #include <cyassl/ctaocrypt/sha.h> 00032 #ifdef NO_INLINE 00033 #include <cyassl/ctaocrypt/misc.h> 00034 #else 00035 #include <ctaocrypt/src/misc.c> 00036 #endif 00037 00038 00039 #ifdef STM32F2_HASH 00040 /* 00041 * STM32F2 hardware SHA1 support through the STM32F2 standard peripheral 00042 * library. Documentation located in STM32F2xx Standard Peripheral Library 00043 * document (See note in README). 00044 */ 00045 #include "stm32f2xx.h" 00046 #include "stm32f2xx_hash.h" 00047 00048 void InitSha(Sha* sha) 00049 { 00050 /* STM32F2 struct notes: 00051 * sha->buffer = first 4 bytes used to hold partial block if needed 00052 * sha->buffLen = num bytes currently stored in sha->buffer 00053 * sha->loLen = num bytes that have been written to STM32 FIFO 00054 */ 00055 XMEMSET(sha->buffer, 0, SHA_REG_SIZE); 00056 sha->buffLen = 0; 00057 sha->loLen = 0; 00058 00059 /* initialize HASH peripheral */ 00060 HASH_DeInit(); 00061 00062 /* configure algo used, algo mode, datatype */ 00063 HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE); 00064 HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH 00065 | HASH_DataType_8b); 00066 00067 /* reset HASH processor */ 00068 HASH->CR |= HASH_CR_INIT; 00069 } 00070 00071 void ShaUpdate(Sha* sha, const byte* data, word32 len) 00072 { 00073 word32 i = 0; 00074 word32 fill = 0; 00075 word32 diff = 0; 00076 00077 /* if saved partial block is available */ 00078 if (sha->buffLen) { 00079 fill = 4 - sha->buffLen; 00080 00081 /* if enough data to fill, fill and push to FIFO */ 00082 if (fill <= len) { 00083 XMEMCPY((byte*)sha->buffer + sha->buffLen, data, fill); 00084 HASH_DataIn(*(uint32_t*)sha->buffer); 00085 00086 data += fill; 00087 len -= fill; 00088 sha->loLen += 4; 00089 sha->buffLen = 0; 00090 } else { 00091 /* append partial to existing stored block */ 00092 XMEMCPY((byte*)sha->buffer + sha->buffLen, data, len); 00093 sha->buffLen += len; 00094 return; 00095 } 00096 } 00097 00098 /* write input block in the IN FIFO */ 00099 for(i = 0; i < len; i += 4) 00100 { 00101 diff = len - i; 00102 if ( diff < 4) { 00103 /* store incomplete last block, not yet in FIFO */ 00104 XMEMSET(sha->buffer, 0, SHA_REG_SIZE); 00105 XMEMCPY((byte*)sha->buffer, data, diff); 00106 sha->buffLen = diff; 00107 } else { 00108 HASH_DataIn(*(uint32_t*)data); 00109 data+=4; 00110 } 00111 } 00112 00113 /* keep track of total data length thus far */ 00114 sha->loLen += (len - sha->buffLen); 00115 } 00116 00117 void ShaFinal(Sha* sha, byte* hash) 00118 { 00119 __IO uint16_t nbvalidbitsdata = 0; 00120 00121 /* finish reading any trailing bytes into FIFO */ 00122 if (sha->buffLen) { 00123 HASH_DataIn(*(uint32_t*)sha->buffer); 00124 sha->loLen += sha->buffLen; 00125 } 00126 00127 /* calculate number of valid bits in last word of input data */ 00128 nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE); 00129 00130 /* configure number of valid bits in last word of the data */ 00131 HASH_SetLastWordValidBitsNbr(nbvalidbitsdata); 00132 00133 /* start HASH processor */ 00134 HASH_StartDigest(); 00135 00136 /* wait until Busy flag == RESET */ 00137 while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {} 00138 00139 /* read message digest */ 00140 sha->digest[0] = HASH->HR[0]; 00141 sha->digest[1] = HASH->HR[1]; 00142 sha->digest[2] = HASH->HR[2]; 00143 sha->digest[3] = HASH->HR[3]; 00144 sha->digest[4] = HASH->HR[4]; 00145 00146 ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE); 00147 00148 XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE); 00149 00150 InitSha(sha); /* reset state */ 00151 } 00152 00153 #else /* CTaoCrypt software implementation */ 00154 00155 #ifndef min 00156 00157 static INLINE word32 min(word32 a, word32 b) 00158 { 00159 return a > b ? b : a; 00160 } 00161 00162 #endif /* min */ 00163 00164 00165 void InitSha(Sha* sha) 00166 { 00167 sha->digest[0] = 0x67452301L; 00168 sha->digest[1] = 0xEFCDAB89L; 00169 sha->digest[2] = 0x98BADCFEL; 00170 sha->digest[3] = 0x10325476L; 00171 sha->digest[4] = 0xC3D2E1F0L; 00172 00173 sha->buffLen = 0; 00174 sha->loLen = 0; 00175 sha->hiLen = 0; 00176 } 00177 00178 #define blk0(i) (W[i] = sha->buffer[i]) 00179 #define blk1(i) (W[i&15] = \ 00180 rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1)) 00181 00182 #define f1(x,y,z) (z^(x &(y^z))) 00183 #define f2(x,y,z) (x^y^z) 00184 #define f3(x,y,z) ((x&y)|(z&(x|y))) 00185 #define f4(x,y,z) (x^y^z) 00186 00187 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ 00188 #define R0(v,w,x,y,z,i) z+= f1(w,x,y) + blk0(i) + 0x5A827999+ \ 00189 rotlFixed(v,5); w = rotlFixed(w,30); 00190 #define R1(v,w,x,y,z,i) z+= f1(w,x,y) + blk1(i) + 0x5A827999+ \ 00191 rotlFixed(v,5); w = rotlFixed(w,30); 00192 #define R2(v,w,x,y,z,i) z+= f2(w,x,y) + blk1(i) + 0x6ED9EBA1+ \ 00193 rotlFixed(v,5); w = rotlFixed(w,30); 00194 #define R3(v,w,x,y,z,i) z+= f3(w,x,y) + blk1(i) + 0x8F1BBCDC+ \ 00195 rotlFixed(v,5); w = rotlFixed(w,30); 00196 #define R4(v,w,x,y,z,i) z+= f4(w,x,y) + blk1(i) + 0xCA62C1D6+ \ 00197 rotlFixed(v,5); w = rotlFixed(w,30); 00198 00199 00200 static void Transform(Sha* sha) 00201 { 00202 word32 W[SHA_BLOCK_SIZE / sizeof(word32)]; 00203 00204 /* Copy context->state[] to working vars */ 00205 word32 a = sha->digest[0]; 00206 word32 b = sha->digest[1]; 00207 word32 c = sha->digest[2]; 00208 word32 d = sha->digest[3]; 00209 word32 e = sha->digest[4]; 00210 00211 #ifdef USE_SLOW_SHA 00212 word32 t, i; 00213 00214 for (i = 0; i < 16; i++) { 00215 R0(a, b, c, d, e, i); 00216 t = e; e = d; d = c; c = b; b = a; a = t; 00217 } 00218 00219 for (; i < 20; i++) { 00220 R1(a, b, c, d, e, i); 00221 t = e; e = d; d = c; c = b; b = a; a = t; 00222 } 00223 00224 for (; i < 40; i++) { 00225 R2(a, b, c, d, e, i); 00226 t = e; e = d; d = c; c = b; b = a; a = t; 00227 } 00228 00229 for (; i < 60; i++) { 00230 R3(a, b, c, d, e, i); 00231 t = e; e = d; d = c; c = b; b = a; a = t; 00232 } 00233 00234 for (; i < 80; i++) { 00235 R4(a, b, c, d, e, i); 00236 t = e; e = d; d = c; c = b; b = a; a = t; 00237 } 00238 #else 00239 /* nearly 1 K bigger in code size but 25% faster */ 00240 /* 4 rounds of 20 operations each. Loop unrolled. */ 00241 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); 00242 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); 00243 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); 00244 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); 00245 00246 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); 00247 00248 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); 00249 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); 00250 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); 00251 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); 00252 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); 00253 00254 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); 00255 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); 00256 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); 00257 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); 00258 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); 00259 00260 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); 00261 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); 00262 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); 00263 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); 00264 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); 00265 #endif 00266 00267 /* Add the working vars back into digest state[] */ 00268 sha->digest[0] += a; 00269 sha->digest[1] += b; 00270 sha->digest[2] += c; 00271 sha->digest[3] += d; 00272 sha->digest[4] += e; 00273 } 00274 00275 00276 static INLINE void AddLength(Sha* sha, word32 len) 00277 { 00278 word32 tmp = sha->loLen; 00279 if ( (sha->loLen += len) < tmp) 00280 sha->hiLen++; /* carry low to high */ 00281 } 00282 00283 00284 void ShaUpdate(Sha* sha, const byte* data, word32 len) 00285 { 00286 /* do block size increments */ 00287 byte* local = (byte*)sha->buffer; 00288 00289 while (len) { 00290 word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen); 00291 XMEMCPY(&local[sha->buffLen], data, add); 00292 00293 sha->buffLen += add; 00294 data += add; 00295 len -= add; 00296 00297 if (sha->buffLen == SHA_BLOCK_SIZE) { 00298 #ifdef LITTLE_ENDIAN_ORDER 00299 ByteReverseBytes(local, local, SHA_BLOCK_SIZE); 00300 #endif 00301 Transform(sha); 00302 AddLength(sha, SHA_BLOCK_SIZE); 00303 sha->buffLen = 0; 00304 } 00305 } 00306 } 00307 00308 00309 void ShaFinal(Sha* sha, byte* hash) 00310 { 00311 byte* local = (byte*)sha->buffer; 00312 00313 AddLength(sha, sha->buffLen); /* before adding pads */ 00314 00315 local[sha->buffLen++] = 0x80; /* add 1 */ 00316 00317 /* pad with zeros */ 00318 if (sha->buffLen > SHA_PAD_SIZE) { 00319 XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen); 00320 sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen; 00321 00322 #ifdef LITTLE_ENDIAN_ORDER 00323 ByteReverseBytes(local, local, SHA_BLOCK_SIZE); 00324 #endif 00325 Transform(sha); 00326 sha->buffLen = 0; 00327 } 00328 XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen); 00329 00330 /* put lengths in bits */ 00331 sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + 00332 (sha->hiLen << 3); 00333 sha->loLen = sha->loLen << 3; 00334 00335 /* store lengths */ 00336 #ifdef LITTLE_ENDIAN_ORDER 00337 ByteReverseBytes(local, local, SHA_BLOCK_SIZE); 00338 #endif 00339 /* ! length ordering dependent on digest endian type ! */ 00340 XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32)); 00341 XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32)); 00342 00343 Transform(sha); 00344 #ifdef LITTLE_ENDIAN_ORDER 00345 ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE); 00346 #endif 00347 XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE); 00348 00349 InitSha(sha); /* reset state */ 00350 } 00351 00352 #endif /* STM32F2_HASH */ 00353 00354 //#endif /* NO_SHA */
Generated on Thu Jul 14 2022 20:26:03 by
1.7.2