MultiTech / CyaSSL

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha.c Source File

sha.c

00001 /* sha.c
00002  *
00003  * Copyright (C) 2006-2014 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00020  */
00021 
00022 
00023 #ifdef HAVE_CONFIG_H
00024     #include <config.h>
00025 #endif
00026 
00027 #include <cyassl/ctaocrypt/settings.h>
00028 
00029 #if !defined(NO_SHA)
00030 
00031 #ifdef CYASSL_PIC32MZ_HASH
00032 #define InitSha   InitSha_sw
00033 #define ShaUpdate ShaUpdate_sw
00034 #define ShaFinal  ShaFinal_sw
00035 #endif
00036 
00037 #ifdef HAVE_FIPS
00038     /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
00039     #define FIPS_NO_WRAPPERS
00040 #endif
00041 
00042 #include <cyassl/ctaocrypt/sha.h>
00043 #include <cyassl/ctaocrypt/logging.h>
00044 #include <cyassl/ctaocrypt/error-crypt.h>
00045 
00046 #ifdef NO_INLINE
00047     #include <cyassl/ctaocrypt/misc.h>
00048 #else
00049     #include <ctaocrypt/src/misc.c>
00050 #endif
00051 
00052 #ifdef FREESCALE_MMCAU
00053     #include "cau_api.h"
00054     #define XTRANSFORM(S,B)  cau_sha1_hash_n((B), 1, ((S))->digest)
00055 #else
00056     #define XTRANSFORM(S,B)  Transform((S))
00057 #endif
00058 
00059 
00060 #ifdef STM32F2_HASH
00061     /*
00062      * STM32F2 hardware SHA1 support through the STM32F2 standard peripheral
00063      * library. Documentation located in STM32F2xx Standard Peripheral Library
00064      * document (See note in README).
00065      */
00066     #include "stm32f2xx.h"
00067     #include "stm32f2xx_hash.h"
00068 
00069     int InitSha(Sha* sha)
00070     {
00071         /* STM32F2 struct notes:
00072          * sha->buffer  = first 4 bytes used to hold partial block if needed 
00073          * sha->buffLen = num bytes currently stored in sha->buffer
00074          * sha->loLen   = num bytes that have been written to STM32 FIFO
00075          */
00076         XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
00077         sha->buffLen = 0;
00078         sha->loLen = 0;
00079 
00080         /* initialize HASH peripheral */
00081         HASH_DeInit();
00082 
00083         /* configure algo used, algo mode, datatype */
00084         HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
00085         HASH->CR |= (HASH_AlgoSelection_SHA1 | HASH_AlgoMode_HASH 
00086                  | HASH_DataType_8b);
00087 
00088         /* reset HASH processor */
00089         HASH->CR |= HASH_CR_INIT;
00090 
00091         return 0;
00092     }
00093 
00094     int ShaUpdate(Sha* sha, const byte* data, word32 len)
00095     {
00096         word32 i = 0;
00097         word32 fill = 0;
00098         word32 diff = 0;
00099 
00100         /* if saved partial block is available */
00101         if (sha->buffLen) {
00102             fill = 4 - sha->buffLen;
00103 
00104             /* if enough data to fill, fill and push to FIFO */
00105             if (fill <= len) {
00106                 XMEMCPY((byte*)sha->buffer + sha->buffLen, data, fill);
00107                 HASH_DataIn(*(uint32_t*)sha->buffer);
00108 
00109                 data += fill;
00110                 len -= fill;
00111                 sha->loLen += 4;
00112                 sha->buffLen = 0;
00113             } else {
00114                 /* append partial to existing stored block */
00115                 XMEMCPY((byte*)sha->buffer + sha->buffLen, data, len);
00116                 sha->buffLen += len;
00117                 return;
00118             }
00119         }
00120        
00121         /* write input block in the IN FIFO */
00122         for(i = 0; i < len; i += 4)
00123         {
00124             diff = len - i;
00125             if ( diff < 4) {
00126                 /* store incomplete last block, not yet in FIFO */
00127                 XMEMSET(sha->buffer, 0, SHA_REG_SIZE);
00128                 XMEMCPY((byte*)sha->buffer, data, diff);
00129                 sha->buffLen = diff;
00130             } else {
00131                 HASH_DataIn(*(uint32_t*)data);
00132                 data+=4;
00133             }
00134         }
00135 
00136         /* keep track of total data length thus far */ 
00137         sha->loLen += (len - sha->buffLen);
00138 
00139         return 0;
00140     }
00141 
00142     int ShaFinal(Sha* sha, byte* hash)
00143     {
00144         __IO uint16_t nbvalidbitsdata = 0;
00145         
00146         /* finish reading any trailing bytes into FIFO */
00147         if (sha->buffLen) {
00148             HASH_DataIn(*(uint32_t*)sha->buffer);
00149             sha->loLen += sha->buffLen;
00150         }
00151 
00152         /* calculate number of valid bits in last word of input data */
00153         nbvalidbitsdata = 8 * (sha->loLen % SHA_REG_SIZE);
00154 
00155         /* configure number of valid bits in last word of the data */
00156         HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
00157 
00158         /* start HASH processor */
00159         HASH_StartDigest();
00160 
00161         /* wait until Busy flag == RESET */
00162         while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
00163 
00164         /* read message digest */
00165         sha->digest[0] = HASH->HR[0];
00166         sha->digest[1] = HASH->HR[1];
00167         sha->digest[2] = HASH->HR[2];
00168         sha->digest[3] = HASH->HR[3];
00169         sha->digest[4] = HASH->HR[4];
00170         
00171         ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
00172 
00173         XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
00174 
00175         return InitSha(sha);  /* reset state */
00176     }
00177 
00178 #else /* CTaoCrypt software implementation */
00179 
00180 #ifndef min
00181 
00182     static INLINE word32 min(word32 a, word32 b)
00183     {
00184         return a > b ? b : a;
00185     }
00186 
00187 #endif /* min */
00188 
00189 
00190 int InitSha(Sha* sha)
00191 {
00192     #ifdef FREESCALE_MMCAU
00193         cau_sha1_initialize_output(sha->digest);
00194     #else
00195         sha->digest[0] = 0x67452301L;
00196         sha->digest[1] = 0xEFCDAB89L;
00197         sha->digest[2] = 0x98BADCFEL;
00198         sha->digest[3] = 0x10325476L;
00199         sha->digest[4] = 0xC3D2E1F0L;
00200     #endif
00201 
00202     sha->buffLen = 0;
00203     sha->loLen   = 0;
00204     sha->hiLen   = 0;
00205 
00206     return 0;
00207 }
00208 
00209 #ifndef FREESCALE_MMCAU
00210 
00211 #define blk0(i) (W[i] = sha->buffer[i])
00212 #define blk1(i) (W[i&15] = \
00213                    rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1))
00214 
00215 #define f1(x,y,z) (z^(x &(y^z)))
00216 #define f2(x,y,z) (x^y^z)
00217 #define f3(x,y,z) ((x&y)|(z&(x|y)))
00218 #define f4(x,y,z) (x^y^z)
00219 
00220 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
00221 #define R0(v,w,x,y,z,i) z+= f1(w,x,y) + blk0(i) + 0x5A827999+ \
00222                         rotlFixed(v,5); w = rotlFixed(w,30);
00223 #define R1(v,w,x,y,z,i) z+= f1(w,x,y) + blk1(i) + 0x5A827999+ \
00224                         rotlFixed(v,5); w = rotlFixed(w,30);
00225 #define R2(v,w,x,y,z,i) z+= f2(w,x,y) + blk1(i) + 0x6ED9EBA1+ \
00226                         rotlFixed(v,5); w = rotlFixed(w,30);
00227 #define R3(v,w,x,y,z,i) z+= f3(w,x,y) + blk1(i) + 0x8F1BBCDC+ \
00228                         rotlFixed(v,5); w = rotlFixed(w,30);
00229 #define R4(v,w,x,y,z,i) z+= f4(w,x,y) + blk1(i) + 0xCA62C1D6+ \
00230                         rotlFixed(v,5); w = rotlFixed(w,30);
00231 
00232 
00233 static void Transform(Sha* sha)
00234 {
00235     word32 W[SHA_BLOCK_SIZE / sizeof(word32)];
00236 
00237     /* Copy context->state[] to working vars */ 
00238     word32 a = sha->digest[0];
00239     word32 b = sha->digest[1];
00240     word32 c = sha->digest[2];
00241     word32 d = sha->digest[3];
00242     word32 e = sha->digest[4];
00243 
00244 #ifdef USE_SLOW_SHA
00245     word32 t, i;
00246 
00247     for (i = 0; i < 16; i++) {
00248         R0(a, b, c, d, e, i);
00249         t = e; e = d; d = c; c = b; b = a; a = t;
00250     }
00251 
00252     for (; i < 20; i++) {
00253         R1(a, b, c, d, e, i);
00254         t = e; e = d; d = c; c = b; b = a; a = t;
00255     }
00256 
00257     for (; i < 40; i++) {
00258         R2(a, b, c, d, e, i);
00259         t = e; e = d; d = c; c = b; b = a; a = t;
00260     }
00261 
00262     for (; i < 60; i++) {
00263         R3(a, b, c, d, e, i);
00264         t = e; e = d; d = c; c = b; b = a; a = t;
00265     }
00266 
00267     for (; i < 80; i++) {
00268         R4(a, b, c, d, e, i);
00269         t = e; e = d; d = c; c = b; b = a; a = t;
00270     }
00271 #else
00272     /* nearly 1 K bigger in code size but 25% faster  */
00273     /* 4 rounds of 20 operations each. Loop unrolled. */
00274     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);
00275     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);
00276     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);
00277     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);
00278 
00279     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);
00280 
00281     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);
00282     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);
00283     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);
00284     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);
00285     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);
00286 
00287     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);
00288     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);
00289     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);
00290     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);
00291     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);
00292 
00293     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);
00294     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);
00295     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);
00296     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);
00297     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);
00298 #endif
00299 
00300     /* Add the working vars back into digest state[] */
00301     sha->digest[0] += a;
00302     sha->digest[1] += b;
00303     sha->digest[2] += c;
00304     sha->digest[3] += d;
00305     sha->digest[4] += e;
00306 }
00307 
00308 #endif /* FREESCALE_MMCAU */
00309 
00310 
00311 static INLINE void AddLength(Sha* sha, word32 len)
00312 {
00313     word32 tmp = sha->loLen;
00314     if ( (sha->loLen += len) < tmp)
00315         sha->hiLen++;                       /* carry low to high */
00316 }
00317 
00318 
00319 int ShaUpdate(Sha* sha, const byte* data, word32 len)
00320 {
00321     /* do block size increments */
00322     byte* local = (byte*)sha->buffer;
00323 
00324     while (len) {
00325         word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
00326         XMEMCPY(&local[sha->buffLen], data, add);
00327 
00328         sha->buffLen += add;
00329         data         += add;
00330         len          -= add;
00331 
00332         if (sha->buffLen == SHA_BLOCK_SIZE) {
00333             #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
00334                 ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
00335             #endif
00336             XTRANSFORM(sha, local);
00337             AddLength(sha, SHA_BLOCK_SIZE);
00338             sha->buffLen = 0;
00339         }
00340     }
00341 
00342     return 0;
00343 }
00344 
00345 
00346 int ShaFinal(Sha* sha, byte* hash)
00347 {
00348     byte* local = (byte*)sha->buffer;
00349 
00350     AddLength(sha, sha->buffLen);  /* before adding pads */
00351 
00352     local[sha->buffLen++] = 0x80;  /* add 1 */
00353 
00354     /* pad with zeros */
00355     if (sha->buffLen > SHA_PAD_SIZE) {
00356         XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
00357         sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
00358 
00359         #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
00360             ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
00361         #endif
00362         XTRANSFORM(sha, local);
00363         sha->buffLen = 0;
00364     }
00365     XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
00366    
00367     /* put lengths in bits */
00368     sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + 
00369                  (sha->hiLen << 3);
00370     sha->loLen = sha->loLen << 3;
00371 
00372     /* store lengths */
00373     #if defined(LITTLE_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
00374         ByteReverseWords(sha->buffer, sha->buffer, SHA_BLOCK_SIZE);
00375     #endif
00376     /* ! length ordering dependent on digest endian type ! */
00377     XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
00378     XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
00379 
00380     #ifdef FREESCALE_MMCAU
00381         /* Kinetis requires only these bytes reversed */
00382         ByteReverseWords(&sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
00383                          &sha->buffer[SHA_PAD_SIZE/sizeof(word32)],
00384                          2 * sizeof(word32));
00385     #endif
00386 
00387     XTRANSFORM(sha, local);
00388     #ifdef LITTLE_ENDIAN_ORDER
00389         ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
00390     #endif
00391     XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
00392 
00393     return InitSha(sha);  /* reset state */
00394 }
00395 
00396 #endif /* STM32F2_HASH */
00397 
00398 
00399 int ShaHash(const byte* data, word32 len, byte* hash)
00400 {
00401     int ret = 0;
00402 #ifdef CYASSL_SMALL_STACK
00403     Sha* sha;
00404 #else
00405     Sha sha[1];
00406 #endif
00407 
00408 #ifdef CYASSL_SMALL_STACK
00409     sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
00410     if (sha == NULL)
00411         return MEMORY_E;
00412 #endif
00413 
00414     if ((ret = InitSha(sha)) != 0) {
00415         CYASSL_MSG("InitSha failed");
00416     }
00417     else {
00418         ShaUpdate(sha, data, len);
00419         ShaFinal(sha, hash);
00420     }
00421 
00422 #ifdef CYASSL_SMALL_STACK
00423     XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
00424 #endif
00425 
00426     return ret;
00427 }
00428 
00429 #endif /* NO_SHA */