Make SHA-256/-512 optional. Note that no-sha switches off *all* SHA.
[openssl.git] / crypto / sha / sha512.c
index 074ac0ebe951f2d8f9269577432c6fa4686b7eee..bd1a125485cf64d3cc3e00145e32e9148d41249f 100644 (file)
@@ -1,8 +1,10 @@
 /* crypto/sha/sha512.c */
 /* ====================================================================
- * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 2004 The OpenSSL Project.  All rights reserved
+ * according to the OpenSSL license [found in ../../LICENSE].
  * ====================================================================
  */
+#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512)
 /*
  * IMPLEMENTATION NOTES.
  *
 
 const char *SHA512_version="SHA-512" OPENSSL_VERSION_PTEXT;
 
+#if defined(_M_IX86) || defined(_M_AMD64) || defined(__i386) || defined(__x86_64)
+#define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
+#endif
+
 int SHA384_Init (SHA512_CTX *c)
        {
        c->h[0]=U64(0xcbbb9d5dc1059ed8);
@@ -59,7 +65,7 @@ int SHA384_Init (SHA512_CTX *c)
        c->h[6]=U64(0xdb0c2e0d64f98fa7);
        c->h[7]=U64(0x47b5481dbefa4fa4);
         c->Nl=0;        c->Nh=0;
-        c->num=0;
+        c->num=0;       c->md_len=SHA384_DIGEST_LENGTH;
         return 1;
        }
 
@@ -74,18 +80,21 @@ int SHA512_Init (SHA512_CTX *c)
        c->h[6]=U64(0x1f83d9abfb41bd6b);
        c->h[7]=U64(0x5be0cd19137e2179);
         c->Nl=0;        c->Nh=0;
-        c->num=0;
+        c->num=0;       c->md_len=SHA512_DIGEST_LENGTH;
         return 1;
        }
 
-static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num);
+#ifndef SHA512_ASM
+static
+#endif
+void sha512_block (SHA512_CTX *ctx, const void *in, size_t num);
 
-static int sha512_final (unsigned char *md, SHA512_CTX *c, size_t msz)
+int SHA512_Final (unsigned char *md, SHA512_CTX *c)
        {
        unsigned char *p=(unsigned char *)c->u.p;
        size_t n=c->num;
 
-       p[n]=0x80;
+       p[n]=0x80;      /* There always is a room for one */
        n++;
        if (n > (sizeof(c->u)-16))
                memset (p+n,0,sizeof(c->u)-n), n=0,
@@ -96,50 +105,76 @@ static int sha512_final (unsigned char *md, SHA512_CTX *c, size_t msz)
        c->u.d[SHA_LBLOCK-2] = c->Nh;
        c->u.d[SHA_LBLOCK-1] = c->Nl;
 #else
-       p[sizeof(c->u)-1]  = (c->Nl)&0xFF;
-       p[sizeof(c->u)-2]  = (c->Nl>>8)&0xFF;
-       p[sizeof(c->u)-3]  = (c->Nl>>16)&0xFF;
-       p[sizeof(c->u)-4]  = (c->Nl>>24)&0xFF;
-       p[sizeof(c->u)-5]  = (c->Nl>>32)&0xFF;
-       p[sizeof(c->u)-6]  = (c->Nl>>40)&0xFF;
-       p[sizeof(c->u)-7]  = (c->Nl>>48)&0xFF;
-       p[sizeof(c->u)-8]  = (c->Nl>>56)&0xFF;
-       p[sizeof(c->u)-9]  = (c->Nh)&0xFF;
-       p[sizeof(c->u)-10] = (c->Nh>>8)&0xFF;
-       p[sizeof(c->u)-11] = (c->Nh>>16)&0xFF;
-       p[sizeof(c->u)-12] = (c->Nh>>24)&0xFF;
-       p[sizeof(c->u)-13] = (c->Nh>>32)&0xFF;
-       p[sizeof(c->u)-14] = (c->Nh>>40)&0xFF;
-       p[sizeof(c->u)-15] = (c->Nh>>48)&0xFF;
-       p[sizeof(c->u)-16] = (c->Nh>>56)&0xFF;
+       p[sizeof(c->u)-1]  = (unsigned char)(c->Nl);
+       p[sizeof(c->u)-2]  = (unsigned char)(c->Nl>>8);
+       p[sizeof(c->u)-3]  = (unsigned char)(c->Nl>>16);
+       p[sizeof(c->u)-4]  = (unsigned char)(c->Nl>>24);
+       p[sizeof(c->u)-5]  = (unsigned char)(c->Nl>>32);
+       p[sizeof(c->u)-6]  = (unsigned char)(c->Nl>>40);
+       p[sizeof(c->u)-7]  = (unsigned char)(c->Nl>>48);
+       p[sizeof(c->u)-8]  = (unsigned char)(c->Nl>>56);
+       p[sizeof(c->u)-9]  = (unsigned char)(c->Nh);
+       p[sizeof(c->u)-10] = (unsigned char)(c->Nh>>8);
+       p[sizeof(c->u)-11] = (unsigned char)(c->Nh>>16);
+       p[sizeof(c->u)-12] = (unsigned char)(c->Nh>>24);
+       p[sizeof(c->u)-13] = (unsigned char)(c->Nh>>32);
+       p[sizeof(c->u)-14] = (unsigned char)(c->Nh>>40);
+       p[sizeof(c->u)-15] = (unsigned char)(c->Nh>>48);
+       p[sizeof(c->u)-16] = (unsigned char)(c->Nh>>56);
 #endif
 
        sha512_block (c,p,1);
 
        if (md==0) return 0;
 
-       for (n=0;msz>0;n++,msz-=8)
+       switch (c->md_len)
                {
-               SHA_LONG64 t = c->h[n];
-
-               *(md++) = (t>>56)&0xFF; *(md++) = (t>>48)&0xFF;
-               *(md++) = (t>>40)&0xFF; *(md++) = (t>>32)&0xFF;
-               *(md++) = (t>>24)&0xFF; *(md++) = (t>>16)&0xFF;
-               *(md++) = (t>>8)&0xFF;  *(md++) = (t)&0xFF;
+               /* Let compiler decide if it's appropriate to unroll... */
+               case SHA384_DIGEST_LENGTH:
+                       for (n=0;n<SHA384_DIGEST_LENGTH/8;n++)
+                               {
+                               SHA_LONG64 t = c->h[n];
+
+                               *(md++) = (unsigned char)(t>>56);
+                               *(md++) = (unsigned char)(t>>48);
+                               *(md++) = (unsigned char)(t>>40);
+                               *(md++) = (unsigned char)(t>>32);
+                               *(md++) = (unsigned char)(t>>24);
+                               *(md++) = (unsigned char)(t>>16);
+                               *(md++) = (unsigned char)(t>>8);
+                               *(md++) = (unsigned char)(t);
+                               }
+                       break;
+               case SHA512_DIGEST_LENGTH:
+                       for (n=0;n<SHA512_DIGEST_LENGTH/8;n++)
+                               {
+                               SHA_LONG64 t = c->h[n];
+
+                               *(md++) = (unsigned char)(t>>56);
+                               *(md++) = (unsigned char)(t>>48);
+                               *(md++) = (unsigned char)(t>>40);
+                               *(md++) = (unsigned char)(t>>32);
+                               *(md++) = (unsigned char)(t>>24);
+                               *(md++) = (unsigned char)(t>>16);
+                               *(md++) = (unsigned char)(t>>8);
+                               *(md++) = (unsigned char)(t);
+                               }
+                       break;
+               /* ... as well as make sure md_len is not abused. */
+               default:        return 0;
                }
 
        return 1;
        }
 
 int SHA384_Final (unsigned char *md,SHA512_CTX *c)
-{   return sha512_final (md,c,SHA384_DIGEST_LENGTH);   }
-int SHA512_Final (unsigned char *md,SHA512_CTX *c)
-{   return sha512_final (md,c,SHA512_DIGEST_LENGTH);   }
+{   return SHA512_Final (md,c);   }
 
 int SHA512_Update (SHA512_CTX *c, const void *_data, size_t len)
        {
        SHA_LONG64      l;
-       unsigned char  *p=c->u.p,*data=(unsigned char *)_data;
+       unsigned char  *p=c->u.p;
+       const unsigned char *data=(const unsigned char *)_data;
 
        if (len==0) return  1;
 
@@ -167,7 +202,7 @@ int SHA512_Update (SHA512_CTX *c, const void *_data, size_t len)
        if (len >= sizeof(c->u))
                {
 #ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
-               if ((int)data%sizeof(c->u.d[0]) != 0)
+               if ((size_t)data%sizeof(c->u.d[0]) != 0)
                        while (len >= sizeof(c->u))
                                memcpy (p,data,sizeof(c->u)),
                                sha512_block (c,p,1),
@@ -200,7 +235,7 @@ unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md)
        if (md == NULL) md=m;
        SHA384_Init(&c);
        SHA512_Update(&c,d,n);
-       sha512_final(md,&c,sizeof(m));
+       SHA512_Final(md,&c);
        OPENSSL_cleanse(&c,sizeof(c));
        return(md);
        }
@@ -213,11 +248,12 @@ unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md)
        if (md == NULL) md=m;
        SHA512_Init(&c);
        SHA512_Update(&c,d,n);
-       sha512_final(md,&c,sizeof(m));
+       SHA512_Final(md,&c);
        OPENSSL_cleanse(&c,sizeof(c));
        return(md);
        }
 
+#ifndef SHA512_ASM
 static const SHA_LONG64 K512[80] = {
         U64(0x428a2f98d728ae22),U64(0x7137449123ef65cd),
         U64(0xb5c0fbcfec4d3b2f),U64(0xe9b5dba58189dbbc),
@@ -260,10 +296,47 @@ static const SHA_LONG64 K512[80] = {
         U64(0x4cc5d4becb3e42b6),U64(0x597f299cfc657e2a),
         U64(0x5fcb6fab3ad6faec),U64(0x6c44198c4a475817) };
 
-#define B(x,j)    (((SHA_LONG64)(*(((unsigned char *)(&x))+j)))<<((7-j)*8))
+#ifndef PEDANTIC
+# if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
+#  if defined(__x86_64) || defined(__x86_64__)
+#   define PULL64(x) ({ SHA_LONG64 ret=*((const SHA_LONG64 *)(&(x)));  \
+                               asm ("bswapq    %0"             \
+                               : "=r"(ret)                     \
+                               : "0"(ret)); ret;               })
+#  endif
+# endif
+#endif
+
+#ifndef PULL64
+#define B(x,j)    (((SHA_LONG64)(*(((const unsigned char *)(&x))+j)))<<((7-j)*8))
 #define PULL64(x) (B(x,0)|B(x,1)|B(x,2)|B(x,3)|B(x,4)|B(x,5)|B(x,6)|B(x,7))
+#endif
 
+#ifndef PEDANTIC
+# if defined(_MSC_VER)
+#  if defined(_WIN64)  /* applies to both IA-64 and AMD64 */
+#   define ROTR(a,n)   _rotr64((a),n)
+#  endif
+# elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
+#  if defined(__x86_64) || defined(__x86_64__)
+#   define ROTR(a,n)   ({ unsigned long ret;           \
+                               asm ("rorq %1,%0"       \
+                               : "=r"(ret)             \
+                               : "J"(n),"0"(a)         \
+                               : "cc"); ret;           })
+#  elif defined(_ARCH_PPC) && defined(__64BIT__)
+#   define ROTR(a,n)   ({ unsigned long ret;           \
+                               asm ("rotrdi %0,%1,%2"  \
+                               : "=r"(ret)             \
+                               : "r"(a),"K"(n)); ret;  })
+#  endif
+# endif
+#endif
+
+#ifndef ROTR
 #define ROTR(x,s)      (((x)>>s) | (x)<<(64-s))
+#endif
+
 #define Sigma0(x)      (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
 #define Sigma1(x)      (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
 #define sigma0(x)      (ROTR((x),1)  ^ ROTR((x),8)  ^ ((x)>>7))
@@ -272,6 +345,15 @@ static const SHA_LONG64 K512[80] = {
 #define Ch(x,y,z)      (((x) & (y)) ^ ((~(x)) & (z)))
 #define Maj(x,y,z)     (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
 
+#if defined(OPENSSL_IA32_SSE2) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
+#define        GO_FOR_SSE2(ctx,in,num)         do {            \
+       extern int      OPENSSL_ia32cap;                \
+       void            sha512_block_sse2(void *,const void *,size_t);  \
+       if (!(OPENSSL_ia32cap & (1<<26))) break;        \
+       sha512_block_sse2(ctx->h,in,num); return;       \
+                                       } while (0)
+#endif
+
 #ifdef OPENSSL_SMALL_FOOTPRINT
 
 static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
@@ -281,6 +363,10 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        SHA_LONG64      X[16];
        int i;
 
+#ifdef GO_FOR_SSE2
+       GO_FOR_SSE2(ctx,in,num);
+#endif
+
                        while (num--) {
 
        a = ctx->h[0];  b = ctx->h[1];  c = ctx->h[2];  d = ctx->h[3];
@@ -314,6 +400,7 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d;
        ctx->h[4] += e; ctx->h[5] += f; ctx->h[6] += g; ctx->h[7] += h;
 
+                       W+=SHA_LBLOCK;
                        }
        }
 
@@ -327,7 +414,7 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
 #define        ROUND_16_80(i,a,b,c,d,e,f,g,h,X)        do {    \
        s0 = X[(i+1)&0x0f];     s0 = sigma0(s0);        \
        s1 = X[(i+14)&0x0f];    s1 = sigma1(s1);        \
-       T1 = X[i&0x0f] += s0 + s1 + X[(i+9)&0x0f];      \
+       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 sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
@@ -337,6 +424,10 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        SHA_LONG64      X[16];
        int i;
 
+#ifdef GO_FOR_SSE2
+       GO_FOR_SSE2(ctx,in,num);
+#endif
+
                        while (num--) {
 
        a = ctx->h[0];  b = ctx->h[1];  c = ctx->h[2];  d = ctx->h[3];
@@ -393,7 +484,12 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d;
        ctx->h[4] += e; ctx->h[5] += f; ctx->h[6] += g; ctx->h[7] += h;
 
+                       W+=SHA_LBLOCK;
                        }
        }
 
 #endif
+
+#endif /* SHA512_ASM */
+
+#endif /* OPENSSL_NO_SHA512 */