Premaster secret handling fixes
[openssl.git] / crypto / md32_common.h
index e3ec1a72d63274d18cb4bfbb0fec8be0450a2997..21461548d15ee35554af94363f6abf7277a78322 100644 (file)
@@ -1,6 +1,6 @@
 /* crypto/md32_common.h */
 /* ====================================================================
- * Copyright (c) 1999-2006 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1999-2007 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -64,7 +64,8 @@
  * HASH_CBLOCK
  *     size of a unit chunk HASH_BLOCK operates on.
  * HASH_LONG
- *     has to be at lest 32 bit wide.
+ *     has to be at lest 32 bit wide, if it's wider, then
+ *     HASH_LONG_LOG2 *has to* be defined along
  * HASH_CTX
  *     context structure that at least contains following
  *     members:
@@ -72,8 +73,8 @@
  *                     ...
  *                     HASH_LONG       Nl,Nh;
  *                     either {
- *                       HASH_LONG     data[HASH_LBLOCK];
- *                       unsigned char data[HASH_CBLOCK];
+ *                     HASH_LONG       data[HASH_LBLOCK];
+ *                     unsigned char   data[HASH_CBLOCK];
  *                     };
  *                     unsigned int    num;
  *                     ...
@@ -97,6 +98,7 @@
  *     #define DATA_ORDER_IS_LITTLE_ENDIAN
  *
  *     #define HASH_LONG               MD5_LONG
+ *     #define HASH_LONG_LOG2          MD5_LONG_LOG2
  *     #define HASH_CTX                MD5_CTX
  *     #define HASH_CBLOCK             MD5_CBLOCK
  *     #define HASH_UPDATE             MD5_Update
  */
 #undef ROTATE
 #ifndef PEDANTIC
-# if defined(_MSC_VER) || defined(__ICC)
+# if defined(_MSC_VER)
 #  define ROTATE(a,n)  _lrotl(a,n)
-# elif defined(__MWERKS__)
-#  if defined(__POWERPC__)
-#   define ROTATE(a,n) __rlwinm(a,n,0,31)
-#  elif defined(__MC68K__)
-    /* Motorola specific tweak. <appro@fy.chalmers.se> */
-#   define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
-#  else
-#   define ROTATE(a,n) __rol(a,n)
-#  endif
+# elif defined(__ICC)
+#  define ROTATE(a,n)  _rotl(a,n)
 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
   /*
    * Some GNU C inline assembler templates. Note that these are
                                asm (                   \
                                "roll %1,%0"            \
                                : "=r"(ret)             \
-                               : "I"(n), "0"(a)        \
+                               : "I"(n), "0"((unsigned int)(a))        \
                                : "cc");                \
                           ret;                         \
                        })
                                   asm ("bswapl %0":"=r"(r):"0"(r));    \
                                   *((unsigned int *)(c))=r; (c)+=4; r; })
 #   endif
+#  elif defined(__aarch64__)
+#   if defined(__BYTE_ORDER__)
+#    if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
+#     define HOST_c2l(c,l)     ({ unsigned int r;              \
+                                  asm ("rev    %w0,%w1"        \
+                                       :"=r"(r)                \
+                                       :"r"(*((const unsigned int *)(c))));\
+                                  (c)+=4; (l)=r;               })
+#     define HOST_l2c(l,c)     ({ unsigned int r;              \
+                                  asm ("rev    %w0,%w1"        \
+                                       :"=r"(r)                \
+                                       :"r"((unsigned int)(l)));\
+                                  *((unsigned int *)(c))=r; (c)+=4; r; })
+#    elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
+#     define HOST_c2l(c,l)     ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
+#     define HOST_l2c(l,c)     (*((unsigned int *)(c))=(l), (c)+=4, (l))
+#    endif
+#   endif
 #  endif
 # endif
-#endif
-#if defined(__s390__) || defined(__s390x__)
-# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
-# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
+# if defined(__s390__) || defined(__s390x__)
+#  define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
+#  define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
+# endif
 #endif
 
 #ifndef HOST_c2l
 #define HOST_c2l(c,l)  (l =(((unsigned long)(*((c)++)))<<24),          \
                         l|=(((unsigned long)(*((c)++)))<<16),          \
                         l|=(((unsigned long)(*((c)++)))<< 8),          \
-                        l|=(((unsigned long)(*((c)++)))    ),          \
-                        l)
+                        l|=(((unsigned long)(*((c)++)))    )           )
 #endif
 #ifndef HOST_l2c
 #define HOST_l2c(l,c)  (*((c)++)=(unsigned char)(((l)>>24)&0xff),      \
 #ifndef PEDANTIC
 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
 #  if defined(__s390x__)
-#   define HOST_c2l(c,l)       ({ asm ("lrv    %0,0(%1)"               \
-                                       :"=r"(l) : "r"(c));             \
+#   define HOST_c2l(c,l)       ({ asm ("lrv    %0,%1"                  \
+                                  :"=d"(l) :"m"(*(const unsigned int *)(c)));\
                                   (c)+=4; (l);                         })
-#   define HOST_l2c(l,c)       ({ asm ("strv   %0,0(%1)"               \
-                                       : : "r"(l),"r"(c) : "memory");  \
+#   define HOST_l2c(l,c)       ({ asm ("strv   %1,%0"                  \
+                                  :"=m"(*(unsigned int *)(c)) :"d"(l));\
                                   (c)+=4; (l);                         })
 #  endif
 # endif
-#endif
-#if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
-# ifndef B_ENDIAN
-   /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
-#  define HOST_c2l(c,l)        ((l)=*((const unsigned int *)(c)), (c)+=4, l)
-#  define HOST_l2c(l,c)        (*((unsigned int *)(c))=(l), (c)+=4, l)
+# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
+#  ifndef B_ENDIAN
+    /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
+#   define HOST_c2l(c,l)       ((l)=*((const unsigned int *)(c)), (c)+=4, l)
+#   define HOST_l2c(l,c)       (*((unsigned int *)(c))=(l), (c)+=4, l)
+#  endif
 # endif
 #endif
 
 #define HOST_c2l(c,l)  (l =(((unsigned long)(*((c)++)))    ),          \
                         l|=(((unsigned long)(*((c)++)))<< 8),          \
                         l|=(((unsigned long)(*((c)++)))<<16),          \
-                        l|=(((unsigned long)(*((c)++)))<<24),          \
-                        l)
+                        l|=(((unsigned long)(*((c)++)))<<24)           )
 #endif
 #ifndef HOST_l2c
 #define HOST_l2c(l,c)  (*((c)++)=(unsigned char)(((l)    )&0xff),      \
@@ -291,7 +302,7 @@ int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
         * Wei Dai <weidai@eskimo.com> for pointing it out. */
        if (l < c->Nl) /* overflow */
                c->Nh++;
-       c->Nh+=(len>>29);       /* might cause compiler warning on 16-bit */
+       c->Nh+=(HASH_LONG)(len>>29);    /* might cause compiler warning on 16-bit */
        c->Nl=l;
 
        n = c->num;
@@ -299,7 +310,7 @@ int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
                {
                p=(unsigned char *)c->data;
 
-               if ((n+len) >= HASH_CBLOCK)
+               if (len >= HASH_CBLOCK || len+n >= HASH_CBLOCK)
                        {
                        memcpy (p+n,data,HASH_CBLOCK-n);
                        HASH_BLOCK_DATA_ORDER (c,p,1);
@@ -329,7 +340,7 @@ int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
        if (len != 0)
                {
                p = (unsigned char *)c->data;
-               c->num = len;
+               c->num = (unsigned int)len;
                memcpy (p,data,len);
                }
        return 1;
@@ -352,9 +363,11 @@ int HASH_FINAL (unsigned char *md, HASH_CTX *c)
 
        if (n > (HASH_CBLOCK-8))
                {
+               memset (p+n,0,HASH_CBLOCK-n);
+               n=0;
                HASH_BLOCK_DATA_ORDER (c,p,1);
-               memset (p,0,HASH_CBLOCK);
                }
+       memset (p+n,0,HASH_CBLOCK-8-n);
 
        p += HASH_CBLOCK-8;
 #if   defined(DATA_ORDER_IS_BIG_ENDIAN)
@@ -379,6 +392,7 @@ int HASH_FINAL (unsigned char *md, HASH_CTX *c)
        }
 
 #ifndef MD32_REG_T
+#if defined(__alpha) || defined(__sparcv9) || defined(__mips)
 #define MD32_REG_T long
 /*
  * This comment was originaly written for MD5, which is why it
@@ -396,9 +410,15 @@ int HASH_FINAL (unsigned char *md, HASH_CTX *c)
  * Well, to be honest it should say that this *prevents* 
  * performance degradation.
  *                             <appro@fy.chalmers.se>
- * Apparently there're LP64 compilers that generate better
- * code if A-D are declared int. Most notably GCC-x86_64
- * generates better code.
+ */
+#else
+/*
+ * Above is not absolute and there are LP64 compilers that
+ * generate better code if MD32_REG_T is defined int. The above
+ * pre-processor condition reflects the circumstances under which
+ * the conclusion was made and is subject to further extension.
  *                             <appro@fy.chalmers.se>
  */
+#define MD32_REG_T int
+#endif
 #endif