Avoid shadow warning.
[openssl.git] / crypto / sha / sha256.c
index 5f3845f55ca7f67176e1c6b3e507e6d4c7d3b330..f687ca832118a8092a48c4ac5d8d6f912c0deed2 100644 (file)
@@ -4,12 +4,12 @@
  * according to the OpenSSL license [found in ../../LICENSE].
  * ====================================================================
  */
+#include <openssl/opensslconf.h>
 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA256)
 
 #include <stdlib.h>
 #include <string.h>
 
-#include <openssl/opensslconf.h>
 #include <openssl/crypto.h>
 #include <openssl/sha.h>
 #include <openssl/opensslv.h>
@@ -18,23 +18,23 @@ const char *SHA256_version="SHA-256" OPENSSL_VERSION_PTEXT;
 
 int SHA224_Init (SHA256_CTX *c)
        {
+       memset (c,0,sizeof(*c));
        c->h[0]=0xc1059ed8UL;   c->h[1]=0x367cd507UL;
        c->h[2]=0x3070dd17UL;   c->h[3]=0xf70e5939UL;
        c->h[4]=0xffc00b31UL;   c->h[5]=0x68581511UL;
        c->h[6]=0x64f98fa7UL;   c->h[7]=0xbefa4fa4UL;
-       c->Nl=0;        c->Nh=0;
-       c->num=0;       c->md_len=SHA224_DIGEST_LENGTH;
+       c->md_len=SHA224_DIGEST_LENGTH;
        return 1;
        }
 
 int SHA256_Init (SHA256_CTX *c)
        {
+       memset (c,0,sizeof(*c));
        c->h[0]=0x6a09e667UL;   c->h[1]=0xbb67ae85UL;
        c->h[2]=0x3c6ef372UL;   c->h[3]=0xa54ff53aUL;
        c->h[4]=0x510e527fUL;   c->h[5]=0x9b05688cUL;
        c->h[6]=0x1f83d9abUL;   c->h[7]=0x5be0cd19UL;
-       c->Nl=0;        c->Nh=0;
-       c->num=0;       c->md_len=SHA256_DIGEST_LENGTH;
+       c->md_len=SHA256_DIGEST_LENGTH;
        return 1;
        }
 
@@ -69,17 +69,11 @@ int SHA224_Update(SHA256_CTX *c, const void *data, size_t len)
 int SHA224_Final (unsigned char *md, SHA256_CTX *c)
 {   return SHA256_Final (md,c);   }
 
-#ifndef        SHA_LONG_LOG2
-#define        SHA_LONG_LOG2   2       /* default to 32 bits */
-#endif
-
 #define        DATA_ORDER_IS_BIG_ENDIAN
 
 #define        HASH_LONG               SHA_LONG
-#define        HASH_LONG_LOG2          SHA_LONG_LOG2
 #define        HASH_CTX                SHA256_CTX
 #define        HASH_CBLOCK             SHA_CBLOCK
-#define        HASH_LBLOCK             SHA_LBLOCK
 /*
  * Note that FIPS180-2 discusses "Truncation of the Hash Function Output."
  * default: case below covers for it. It's not clear however if it's
@@ -90,21 +84,21 @@ int SHA224_Final (unsigned char *md, SHA256_CTX *c)
  */
 #define        HASH_MAKE_STRING(c,s)   do {    \
        unsigned long ll;               \
-       unsigned int  n               \
+       unsigned int  nn;               \
        switch ((c)->md_len)            \
        {   case SHA224_DIGEST_LENGTH:  \
-               for (n=0;n<SHA224_DIGEST_LENGTH/4;n++)  \
-               {   ll=(c)->h[n]; HOST_l2c(ll,(s));   } \
+               for (nn=0;nn<SHA224_DIGEST_LENGTH/4;nn++)       \
+               {   ll=(c)->h[nn]; HOST_l2c(ll,(s));   }        \
                break;                  \
            case SHA256_DIGEST_LENGTH:  \
-               for (n=0;n<SHA256_DIGEST_LENGTH/4;n++)  \
-               {   ll=(c)->h[n]; HOST_l2c(ll,(s));   } \
+               for (nn=0;nn<SHA256_DIGEST_LENGTH/4;nn++)       \
+               {   ll=(c)->h[nn]; HOST_l2c(ll,(s));   }        \
                break;                  \
            default:                    \
                if ((c)->md_len > SHA256_DIGEST_LENGTH) \
                    return 0;                           \
-               for (n=0;n<(c)->md_len/4;n++)           \
-               {   ll=(c)->h[n]; HOST_l2c(ll,(s));   } \
+               for (nn=0;nn<(c)->md_len/4;nn++)                \
+               {   ll=(c)->h[nn]; HOST_l2c(ll,(s));   }        \
                break;                  \
        }                               \
        } while (0)
@@ -112,16 +106,15 @@ int SHA224_Final (unsigned char *md, SHA256_CTX *c)
 #define        HASH_UPDATE             SHA256_Update
 #define        HASH_TRANSFORM          SHA256_Transform
 #define        HASH_FINAL              SHA256_Final
-#define        HASH_BLOCK_HOST_ORDER   sha256_block_host_order
 #define        HASH_BLOCK_DATA_ORDER   sha256_block_data_order
-void sha256_block_host_order (SHA256_CTX *ctx, const void *in, size_t num);
+#ifndef SHA256_ASM
+static
+#endif
 void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num);
 
 #include "md32_common.h"
 
-#ifdef SHA256_ASM
-void sha256_block (SHA256_CTX *ctx, const void *in, size_t num, int host);
-#else
+#ifndef SHA256_ASM
 static const SHA_LONG K256[64] = {
        0x428a2f98UL,0x71374491UL,0xb5c0fbcfUL,0xe9b5dba5UL,
        0x3956c25bUL,0x59f111f1UL,0x923f82a4UL,0xab1c5ed5UL,
@@ -155,10 +148,10 @@ static const SHA_LONG K256[64] = {
 
 #ifdef OPENSSL_SMALL_FOOTPRINT
 
-static void sha256_block (SHA256_CTX *ctx, const void *in, size_t num, int host)
+static void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num)
        {
        unsigned MD32_REG_T a,b,c,d,e,f,g,h,s0,s1,T1,T2;
-       SHA_LONG        X[16];
+       SHA_LONG        X[16],l;
        int i;
        const unsigned char *data=in;
 
@@ -167,33 +160,13 @@ static void sha256_block (SHA256_CTX *ctx, const void *in, size_t num, int host)
        a = ctx->h[0];  b = ctx->h[1];  c = ctx->h[2];  d = ctx->h[3];
        e = ctx->h[4];  f = ctx->h[5];  g = ctx->h[6];  h = ctx->h[7];
 
-       if (host)
-               {
-               const SHA_LONG *W=(const SHA_LONG *)data;
-
-               for (i=0;i<16;i++)
-                       {
-                       T1 = X[i] = W[i];
-                       T1 += h + Sigma1(e) + Ch(e,f,g) + K256[i];
-                       T2 = Sigma0(a) + Maj(a,b,c);
-                       h = g;  g = f;  f = e;  e = d + T1;
-                       d = c;  c = b;  b = a;  a = T1 + T2;
-                       }
-
-               data += SHA256_CBLOCK;
-               }
-       else
+       for (i=0;i<16;i++)
                {
-               SHA_LONG l;
-
-               for (i=0;i<16;i++)
-                       {
-                       HOST_c2l(data,l); T1 = X[i] = l;
-                       T1 += h + Sigma1(e) + Ch(e,f,g) + K256[i];
-                       T2 = Sigma0(a) + Maj(a,b,c);
-                       h = g;  g = f;  f = e;  e = d + T1;
-                       d = c;  c = b;  b = a;  a = T1 + T2;
-                       }
+               HOST_c2l(data,l); T1 = X[i] = l;
+               T1 += h + Sigma1(e) + Ch(e,f,g) + K256[i];
+               T2 = Sigma0(a) + Maj(a,b,c);
+               h = g;  g = f;  f = e;  e = d + T1;
+               d = c;  c = b;  b = a;  a = T1 + T2;
                }
 
        for (;i<64;i++)
@@ -227,19 +200,20 @@ static void sha256_block (SHA256_CTX *ctx, const void *in, size_t num, int host)
        T1 = X[(i)&0x0f] += s0 + s1 + X[(i+9)&0x0f];    \
        ROUND_00_15(i,a,b,c,d,e,f,g,h);         } while (0)
 
-static void sha256_block (SHA256_CTX *ctx, const void *in, size_t num, int host)
+static void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num)
        {
        unsigned MD32_REG_T a,b,c,d,e,f,g,h,s0,s1,T1;
        SHA_LONG        X[16];
        int i;
        const unsigned char *data=in;
+       const union { long one; char little; } is_endian = {1};
 
                        while (num--) {
 
        a = ctx->h[0];  b = ctx->h[1];  c = ctx->h[2];  d = ctx->h[3];
        e = ctx->h[4];  f = ctx->h[5];  g = ctx->h[6];  h = ctx->h[7];
 
-       if (host)
+       if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)in%4)==0)
                {
                const SHA_LONG *W=(const SHA_LONG *)data;
 
@@ -305,15 +279,4 @@ static void sha256_block (SHA256_CTX *ctx, const void *in, size_t num, int host)
 #endif
 #endif /* SHA256_ASM */
 
-/*
- * Idea is to trade couple of cycles for some space. On IA-32 we save
- * about 4K in "big footprint" case. In "small footprint" case any gain
- * is appreciated:-)
- */
-void HASH_BLOCK_HOST_ORDER (SHA256_CTX *ctx, const void *in, size_t num)
-{   sha256_block (ctx,in,num,1);   }
-
-void HASH_BLOCK_DATA_ORDER (SHA256_CTX *ctx, const void *in, size_t num)
-{   sha256_block (ctx,in,num,0);   }
-
 #endif /* OPENSSL_NO_SHA256 */