Fix SHA512 and optimize BN for mingw64.
[openssl.git] / crypto / sha / sha512.c
index b328fe71d4d7ea69e6a54110f020fe27a1233bb8..86115d890d5e563fccd3877189294d29a81f8232 100644 (file)
@@ -4,6 +4,8 @@
  * according to the OpenSSL license [found in ../../LICENSE].
  * ====================================================================
  */
+#include <openssl/opensslconf.h>
+#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512)
 /*
  * IMPLEMENTATION NOTES.
  *
 #include <stdlib.h>
 #include <string.h>
 
-#include <openssl/opensslconf.h>
 #include <openssl/crypto.h>
 #include <openssl/sha.h>
 #include <openssl/opensslv.h>
 
-const char *SHA512_version="SHA-512" OPENSSL_VERSION_PTEXT;
+#include "cryptlib.h"
+
+const char SHA512_version[]="SHA-512" OPENSSL_VERSION_PTEXT;
 
-#if defined(_M_IX86) || defined(_M_AMD64) || defined(__i386) || defined(__x86_64)
+#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
+    defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || \
+    defined(__s390__) || defined(__s390x__) || \
+    defined(SHA512_ASM)
 #define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
 #endif
 
 int SHA384_Init (SHA512_CTX *c)
        {
+#if defined(SHA512_ASM) && (defined(__arm__) || defined(__arm))
+       /* maintain dword order required by assembler module */
+       unsigned int *h = (unsigned int *)c->h;
+
+       h[0]  = 0xcbbb9d5d; h[1]  = 0xc1059ed8;
+       h[2]  = 0x629a292a; h[3]  = 0x367cd507;
+       h[4]  = 0x9159015a; h[5]  = 0x3070dd17;
+       h[6]  = 0x152fecd8; h[7]  = 0xf70e5939;
+       h[8]  = 0x67332667; h[9]  = 0xffc00b31;
+       h[10] = 0x8eb44a87; h[11] = 0x68581511;
+       h[12] = 0xdb0c2e0d; h[13] = 0x64f98fa7;
+       h[14] = 0x47b5481d; h[15] = 0xbefa4fa4;
+#else
        c->h[0]=U64(0xcbbb9d5dc1059ed8);
        c->h[1]=U64(0x629a292a367cd507);
        c->h[2]=U64(0x9159015a3070dd17);
@@ -63,6 +82,7 @@ int SHA384_Init (SHA512_CTX *c)
        c->h[5]=U64(0x8eb44a8768581511);
        c->h[6]=U64(0xdb0c2e0d64f98fa7);
        c->h[7]=U64(0x47b5481dbefa4fa4);
+#endif
         c->Nl=0;        c->Nh=0;
         c->num=0;       c->md_len=SHA384_DIGEST_LENGTH;
         return 1;
@@ -70,6 +90,19 @@ int SHA384_Init (SHA512_CTX *c)
 
 int SHA512_Init (SHA512_CTX *c)
        {
+#if defined(SHA512_ASM) && (defined(__arm__) || defined(__arm))
+       /* maintain dword order required by assembler module */
+       unsigned int *h = (unsigned int *)c->h;
+
+       h[0]  = 0x6a09e667; h[1]  = 0xf3bcc908;
+       h[2]  = 0xbb67ae85; h[3]  = 0x84caa73b;
+       h[4]  = 0x3c6ef372; h[5]  = 0xfe94f82b;
+       h[6]  = 0xa54ff53a; h[7]  = 0x5f1d36f1;
+       h[8]  = 0x510e527f; h[9]  = 0xade682d1;
+       h[10] = 0x9b05688c; h[11] = 0x2b3e6c1f;
+       h[12] = 0x1f83d9ab; h[13] = 0xfb41bd6b;
+       h[14] = 0x5be0cd19; h[15] = 0x137e2179;
+#else
        c->h[0]=U64(0x6a09e667f3bcc908);
        c->h[1]=U64(0xbb67ae8584caa73b);
        c->h[2]=U64(0x3c6ef372fe94f82b);
@@ -78,6 +111,7 @@ int SHA512_Init (SHA512_CTX *c)
        c->h[5]=U64(0x9b05688c2b3e6c1f);
        c->h[6]=U64(0x1f83d9abfb41bd6b);
        c->h[7]=U64(0x5be0cd19137e2179);
+#endif
         c->Nl=0;        c->Nh=0;
         c->num=0;       c->md_len=SHA512_DIGEST_LENGTH;
         return 1;
@@ -86,7 +120,7 @@ int SHA512_Init (SHA512_CTX *c)
 #ifndef SHA512_ASM
 static
 #endif
-void sha512_block (SHA512_CTX *ctx, const void *in, size_t num);
+void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t num);
 
 int SHA512_Final (unsigned char *md, SHA512_CTX *c)
        {
@@ -97,7 +131,7 @@ int SHA512_Final (unsigned char *md, SHA512_CTX *c)
        n++;
        if (n > (sizeof(c->u)-16))
                memset (p+n,0,sizeof(c->u)-n), n=0,
-               sha512_block (c,p,1);
+               sha512_block_data_order (c,p,1);
 
        memset (p+n,0,sizeof(c->u)-16-n);
 #ifdef B_ENDIAN
@@ -122,10 +156,28 @@ int SHA512_Final (unsigned char *md, SHA512_CTX *c)
        p[sizeof(c->u)-16] = (unsigned char)(c->Nh>>56);
 #endif
 
-       sha512_block (c,p,1);
+       sha512_block_data_order (c,p,1);
 
        if (md==0) return 0;
 
+#if defined(SHA512_ASM) && (defined(__arm__) || defined(__arm))
+       /* recall assembler dword order... */
+       n = c->md_len;
+       if (n == SHA384_DIGEST_LENGTH || n == SHA512_DIGEST_LENGTH)
+               {
+               unsigned int *h = (unsigned int *)c->h, t;
+
+               for (n/=4;n;n--)
+                       {
+                       t = *(h++);
+                       *(md++) = (unsigned char)(t>>24);
+                       *(md++) = (unsigned char)(t>>16);
+                       *(md++) = (unsigned char)(t>>8);
+                       *(md++) = (unsigned char)(t);
+                       }
+               }
+       else    return 0;
+#else
        switch (c->md_len)
                {
                /* Let compiler decide if it's appropriate to unroll... */
@@ -162,7 +214,7 @@ int SHA512_Final (unsigned char *md, SHA512_CTX *c)
                /* ... as well as make sure md_len is not abused. */
                default:        return 0;
                }
-
+#endif
        return 1;
        }
 
@@ -194,7 +246,7 @@ int SHA512_Update (SHA512_CTX *c, const void *_data, size_t len)
                else    {
                        memcpy (p+c->num,data,n), c->num = 0;
                        len-=n, data+=n;
-                       sha512_block (c,p,1);
+                       sha512_block_data_order (c,p,1);
                        }
                }
 
@@ -204,12 +256,12 @@ int SHA512_Update (SHA512_CTX *c, const void *_data, size_t len)
                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),
+                               sha512_block_data_order (c,p,1),
                                len  -= sizeof(c->u),
                                data += sizeof(c->u);
                else
 #endif
-                       sha512_block (c,data,len/sizeof(c->u)),
+                       sha512_block_data_order (c,data,len/sizeof(c->u)),
                        data += len,
                        len  %= sizeof(c->u),
                        data -= len;
@@ -224,7 +276,7 @@ int SHA384_Update (SHA512_CTX *c, const void *data, size_t len)
 {   return SHA512_Update (c,data,len);   }
 
 void SHA512_Transform (SHA512_CTX *c, const unsigned char *data)
-{   sha512_block (c,data,1);  }
+{   sha512_block_data_order (c,data,1);  }
 
 unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md)
        {
@@ -298,40 +350,79 @@ static const SHA_LONG64 K512[80] = {
 #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;           \
+#   define ROTR(a,n)   ({ SHA_LONG64 ret;              \
                                asm ("rorq %1,%0"       \
                                : "=r"(ret)             \
                                : "J"(n),"0"(a)         \
                                : "cc"); ret;           })
-#  elif defined(_ARCH_PPC) && defined(__64BIT__)
+#   if !defined(B_ENDIAN)
+#    define PULL64(x) ({ SHA_LONG64 ret=*((const SHA_LONG64 *)(&(x))); \
+                               asm ("bswapq    %0"             \
+                               : "=r"(ret)                     \
+                               : "0"(ret)); ret;               })
+#   endif
+#  elif (defined(__i386) || defined(__i386__)) && !defined(B_ENDIAN)
+#   if defined(I386_ONLY)
+#    define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\
+                        unsigned int hi=p[0],lo=p[1];          \
+                               asm("xchgb %%ah,%%al;xchgb %%dh,%%dl;"\
+                                   "roll $16,%%eax; roll $16,%%edx; "\
+                                   "xchgb %%ah,%%al;xchgb %%dh,%%dl;" \
+                               : "=a"(lo),"=d"(hi)             \
+                               : "0"(lo),"1"(hi) : "cc");      \
+                               ((SHA_LONG64)hi)<<32|lo;        })
+#   else
+#    define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\
+                        unsigned int hi=p[0],lo=p[1];          \
+                               asm ("bswapl %0; bswapl %1;"    \
+                               : "=r"(lo),"=r"(hi)             \
+                               : "0"(lo),"1"(hi));             \
+                               ((SHA_LONG64)hi)<<32|lo;        })
+#   endif
+#  elif (defined(_ARCH_PPC) && defined(__64BIT__)) || defined(_ARCH_PPC64)
 #   define ROTR(a,n)   ({ unsigned long ret;           \
                                asm ("rotrdi %0,%1,%2"  \
                                : "=r"(ret)             \
                                : "r"(a),"K"(n)); ret;  })
 #  endif
+# elif defined(_MSC_VER)
+#  if defined(_WIN64)  /* applies to both IA-64 and AMD64 */
+#   pragma intrinsic(_rotr64)
+#   define ROTR(a,n)   _rotr64((a),n)
+#  endif
+#  if defined(_M_IX86) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
+#   if defined(I386_ONLY)
+    static SHA_LONG64 __fastcall __pull64be(const void *x)
+    {  _asm    mov     edx, [ecx + 0]
+       _asm    mov     eax, [ecx + 4]
+       _asm    xchg    dh,dl
+       _asm    xchg    ah,al
+       _asm    rol     edx,16
+       _asm    rol     eax,16
+       _asm    xchg    dh,dl
+       _asm    xchg    ah,al
+    }
+#   else
+    static SHA_LONG64 __fastcall __pull64be(const void *x)
+    {  _asm    mov     edx, [ecx + 0]
+       _asm    mov     eax, [ecx + 4]
+       _asm    bswap   edx
+       _asm    bswap   eax
+    }
+#   endif
+#   define PULL64(x) __pull64be(&(x))
+#   if _MSC_VER<=1200
+#    pragma inline_depth(0)
+#   endif
+#  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 ROTR
 #define ROTR(x,s)      (((x)>>s) | (x)<<(64-s))
 #endif
@@ -344,28 +435,74 @@ 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)
-#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)
+
+#if defined(__i386) || defined(__i386__) || defined(_M_IX86)
+/*
+ * This code should give better results on 32-bit CPU with less than
+ * ~24 registers, both size and performance wise...
+ */
+static void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t num)
+       {
+       const SHA_LONG64 *W=in;
+       SHA_LONG64      A,E,T;
+       SHA_LONG64      X[9+80],*F;
+       int i;
+
+                       while (num--) {
+
+       F    = X+80;
+       A    = ctx->h[0];       F[1] = ctx->h[1];
+       F[2] = ctx->h[2];       F[3] = ctx->h[3];
+       E    = ctx->h[4];       F[5] = ctx->h[5];
+       F[6] = ctx->h[6];       F[7] = ctx->h[7];
+
+       for (i=0;i<16;i++,F--)
+               {
+#ifdef B_ENDIAN
+               T = W[i];
+#else
+               T = PULL64(W[i]);
 #endif
+               F[0] = A;
+               F[4] = E;
+               F[8] = T;
+               T   += F[7] + Sigma1(E) + Ch(E,F[5],F[6]) + K512[i];
+               E    = F[3] + T;
+               A    = T + Sigma0(A) + Maj(A,F[1],F[2]);
+               }
+
+       for (;i<80;i++,F--)
+               {
+               T    = sigma0(F[8+16-1]);
+               T   += sigma1(F[8+16-14]);
+               T   += F[8+16] + F[8+16-9];
+
+               F[0] = A;
+               F[4] = E;
+               F[8] = T;
+               T   += F[7] + Sigma1(E) + Ch(E,F[5],F[6]) + K512[i];
+               E    = F[3] + T;
+               A    = T + Sigma0(A) + Maj(A,F[1],F[2]);
+               }
+
+       ctx->h[0] += A;         ctx->h[1] += F[1];
+       ctx->h[2] += F[2];      ctx->h[3] += F[3];
+       ctx->h[4] += E;         ctx->h[5] += F[5];
+       ctx->h[6] += F[6];      ctx->h[7] += F[7];
 
-#ifdef OPENSSL_SMALL_FOOTPRINT
+                       W+=SHA_LBLOCK;
+                       }
+       }
 
-static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
+#elif defined(OPENSSL_SMALL_FOOTPRINT)
+
+static void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t num)
        {
        const SHA_LONG64 *W=in;
        SHA_LONG64      a,b,c,d,e,f,g,h,s0,s1,T1,T2;
        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];
@@ -410,23 +547,19 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        h = Sigma0(a) + Maj(a,b,c);                     \
        d += T1;        h += T1;                } while (0)
 
-#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];    \
-       ROUND_00_15(i,a,b,c,d,e,f,g,h);         } while (0)
+#define        ROUND_16_80(i,j,a,b,c,d,e,f,g,h,X)      do {    \
+       s0 = X[(j+1)&0x0f];     s0 = sigma0(s0);        \
+       s1 = X[(j+14)&0x0f];    s1 = sigma1(s1);        \
+       T1 = X[(j)&0x0f] += s0 + s1 + X[(j+9)&0x0f];    \
+       ROUND_00_15(i+j,a,b,c,d,e,f,g,h);               } while (0)
 
-static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
+static void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t num)
        {
        const SHA_LONG64 *W=in;
        SHA_LONG64      a,b,c,d,e,f,g,h,s0,s1,T1;
        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];
@@ -468,16 +601,24 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        T1 = X[15] = PULL64(W[15]);     ROUND_00_15(15,b,c,d,e,f,g,h,a);
 #endif
 
-       for (i=16;i<80;i+=8)
+       for (i=16;i<80;i+=16)
                {
-               ROUND_16_80(i+0,a,b,c,d,e,f,g,h,X);
-               ROUND_16_80(i+1,h,a,b,c,d,e,f,g,X);
-               ROUND_16_80(i+2,g,h,a,b,c,d,e,f,X);
-               ROUND_16_80(i+3,f,g,h,a,b,c,d,e,X);
-               ROUND_16_80(i+4,e,f,g,h,a,b,c,d,X);
-               ROUND_16_80(i+5,d,e,f,g,h,a,b,c,X);
-               ROUND_16_80(i+6,c,d,e,f,g,h,a,b,X);
-               ROUND_16_80(i+7,b,c,d,e,f,g,h,a,X);
+               ROUND_16_80(i, 0,a,b,c,d,e,f,g,h,X);
+               ROUND_16_80(i, 1,h,a,b,c,d,e,f,g,X);
+               ROUND_16_80(i, 2,g,h,a,b,c,d,e,f,X);
+               ROUND_16_80(i, 3,f,g,h,a,b,c,d,e,X);
+               ROUND_16_80(i, 4,e,f,g,h,a,b,c,d,X);
+               ROUND_16_80(i, 5,d,e,f,g,h,a,b,c,X);
+               ROUND_16_80(i, 6,c,d,e,f,g,h,a,b,X);
+               ROUND_16_80(i, 7,b,c,d,e,f,g,h,a,X);
+               ROUND_16_80(i, 8,a,b,c,d,e,f,g,h,X);
+               ROUND_16_80(i, 9,h,a,b,c,d,e,f,g,X);
+               ROUND_16_80(i,10,g,h,a,b,c,d,e,f,X);
+               ROUND_16_80(i,11,f,g,h,a,b,c,d,e,X);
+               ROUND_16_80(i,12,e,f,g,h,a,b,c,d,X);
+               ROUND_16_80(i,13,d,e,f,g,h,a,b,c,X);
+               ROUND_16_80(i,14,c,d,e,f,g,h,a,b,X);
+               ROUND_16_80(i,15,b,c,d,e,f,g,h,a,X);
                }
 
        ctx->h[0] += a; ctx->h[1] += b; ctx->h[2] += c; ctx->h[3] += d;
@@ -490,3 +631,5 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
 #endif
 
 #endif /* SHA512_ASM */
+
+#endif /* OPENSSL_NO_SHA512 */