Further synchronizations with md32_common.h update, consistent naming
[openssl.git] / crypto / sha / sha512.c
index dc1047d59e28784b1b72ed98f2a05ad33abf06a5..e7a7e5dea8b21c8a49039aa5ec243a54a4b83d50 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>
 
+#include "cryptlib.h"
+
 const char *SHA512_version="SHA-512" OPENSSL_VERSION_PTEXT;
 
 #if defined(_M_IX86) || defined(_M_AMD64) || defined(__i386) || defined(__x86_64)
@@ -83,7 +86,10 @@ int SHA512_Init (SHA512_CTX *c)
         return 1;
        }
 
-static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num);
+#ifndef SHA512_ASM
+static
+#endif
+void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t num);
 
 int SHA512_Final (unsigned char *md, SHA512_CTX *c)
        {
@@ -94,32 +100,32 @@ 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
        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);
+       sha512_block_data_order (c,p,1);
 
        if (md==0) return 0;
 
@@ -131,10 +137,14 @@ int SHA512_Final (unsigned char *md, SHA512_CTX *c)
                                {
                                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;
+                               *(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:
@@ -142,10 +152,14 @@ int SHA512_Final (unsigned char *md, SHA512_CTX *c)
                                {
                                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;
+                               *(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. */
@@ -183,7 +197,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);
                        }
                }
 
@@ -193,12 +207,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;
@@ -213,7 +227,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)
        {
@@ -241,6 +255,7 @@ unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md)
        return(md);
        }
 
+#ifndef SHA512_ASM
 static const SHA_LONG64 K512[80] = {
         U64(0x428a2f98d728ae22),U64(0x7137449123ef65cd),
         U64(0xb5c0fbcfec4d3b2f),U64(0xe9b5dba58189dbbc),
@@ -286,40 +301,75 @@ 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;           \
                                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,lo;                     \
+                               asm("xchgb %%ah,%%al;xchgb %%dh,%%dl;"\
+                                   "roll $16,%%eax; roll $16,%%edx; "\
+                                   "xchgb %%ah,%%al;xchgb %%dh,%%dl;" \
+                               : "=a"(lo),"=d"(hi)             \
+                               : "0"(p[1]),"1"(p[0]) : "cc");  \
+                               ((SHA_LONG64)hi)<<32|lo;        })
+#   else
+#    define PULL64(x) ({ const unsigned int *p=(const unsigned int *)(&(x));\
+                       unsigned int hi,lo;                     \
+                               asm ("bswapl %0; bswapl %1;"    \
+                               : "=r"(lo),"=r"(hi)             \
+                               : "0"(p[1]),"1"(p[0]));         \
+                               ((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 */
+#   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))
+#  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
@@ -332,18 +382,17 @@ 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)
+#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;        \
+       void    sha512_block_sse2(void *,const void *,size_t);  \
+       if (!(OPENSSL_ia32cap_P & (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)
+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;
@@ -404,7 +453,7 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        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)
+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;
@@ -476,3 +525,7 @@ static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
        }
 
 #endif
+
+#endif /* SHA512_ASM */
+
+#endif /* OPENSSL_NO_SHA512 */