Do not ask the user to pass either -DB_ENDIAN or -DL_ENDIAN to the compiler,
authormiod <miod@openbsd.org>
Fri, 18 Apr 2014 19:41:21 +0000 (19:41 +0000)
committermiod <miod@openbsd.org>
Fri, 18 Apr 2014 19:41:21 +0000 (19:41 +0000)
but rather figure out the endianness from <machine/endian.h> automagically;
help from guenther@

ok jca@ guenther@ beck@ and the rest of the `Buena SSL rampage club'

lib/libcrypto/bn/bn_nist.c
lib/libcrypto/crypto/Makefile
lib/libcrypto/des/cfb_enc.c
lib/libcrypto/sha/sha512.c
lib/libcrypto/whrlpool/wp_block.c
lib/libssl/src/crypto/bn/bn_nist.c
lib/libssl/src/crypto/des/cfb_enc.c
lib/libssl/src/crypto/sha/sha512.c
lib/libssl/src/crypto/whrlpool/wp_block.c

index 1e4cf83..4d4a7b8 100644 (file)
@@ -59,6 +59,8 @@
 #include "bn_lcl.h"
 #include "cryptlib.h"
 
+#include <machine/endian.h>
+
 
 #define BN_NIST_192_TOP        (192+BN_BITS2-1)/BN_BITS2
 #define BN_NIST_224_TOP        (224+BN_BITS2-1)/BN_BITS2
@@ -318,7 +320,7 @@ static void nist_cp_bn(BN_ULONG *dst, const BN_ULONG *src, int top)
                                                :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
 #define bn_32_set_0(to, n)             (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
 #define bn_cp_32(to,n,from,m)          ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
-# if defined(L_ENDIAN)
+# if _BYTE_ORDER == _LITTLE_ENDIAN
 #  if defined(__arch64__)
 #   define NIST_INT64 long
 #  else
index 31cab70..f46702b 100644 (file)
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.25 2014/04/18 17:32:31 miod Exp $
+# $OpenBSD: Makefile,v 1.26 2014/04/18 19:41:21 miod Exp $
 
 LIB=   crypto
 
@@ -7,16 +7,6 @@ LCRYPTO_SRC=   ${SSL_SRC}/crypto
 
 CFLAGS+= -Wall
 
-# arm and sh default to little endian, mips defaults to big endian
-.if ${MACHINE_ARCH} == "alpha" || ${MACHINE_ARCH} == "amd64" || \
-    ${MACHINE_ARCH} == "arm" || ${MACHINE_ARCH} == "i386" || \
-    ${MACHINE_ARCH} == "mips64el" || ${MACHINE_ARCH} == "sh" || \
-    ${MACHINE_ARCH} == "vax"
-CFLAGS+= -DL_ENDIAN
-.else
-CFLAGS+= -DB_ENDIAN
-.endif 
-
 .include <bsd.own.mk>          # for 'NOPIC' definition
 .if !defined(NOPIC)
 CFLAGS+= -DDSO_DLFCN -DHAVE_DLFCN_H
index dd5237a..9bf6466 100644 (file)
@@ -58,6 +58,7 @@
 
 #include "des_locl.h"
 #include <assert.h>
+#include <machine/endian.h>
 
 /* The input and output are loaded in multiples of 8 bits.
  * What this means is that if you hame numbits=12 and length=2
@@ -76,7 +77,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
        register int num=numbits/8,n=(numbits+7)/8,i,rem=numbits%8;
        DES_LONG ti[2];
        unsigned char *iv;
-#ifndef L_ENDIAN
+#if _BYTE_ORDER != _LITTLE_ENDIAN
        unsigned char ovec[16];
 #else
        unsigned int  sh[4];
@@ -114,7 +115,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                { v0=d0; v1=d1; }
                        else
                                {
-#ifndef L_ENDIAN
+#if _BYTE_ORDER != _LITTLE_ENDIAN
                                iv=&ovec[0];
                                l2c(v0,iv);
                                l2c(v1,iv);
@@ -129,7 +130,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                        for(i=0 ; i < 8 ; ++i)
                                                ovec[i]=ovec[i+num]<<rem |
                                                        ovec[i+num+1]>>(8-rem);
-#ifdef L_ENDIAN
+#if _BYTE_ORDER == _LITTLE_ENDIAN
                                v0=sh[0], v1=sh[1];
 #else
                                iv=&ovec[0];
@@ -157,7 +158,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                { v0=d0; v1=d1; }
                        else
                                {
-#ifndef L_ENDIAN
+#if _BYTE_ORDER != _LITTLE_ENDIAN
                                iv=&ovec[0];
                                l2c(v0,iv);
                                l2c(v1,iv);
@@ -172,7 +173,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                        for(i=0 ; i < 8 ; ++i)
                                                ovec[i]=ovec[i+num]<<rem |
                                                        ovec[i+num+1]>>(8-rem);
-#ifdef L_ENDIAN
+#if _BYTE_ORDER == _LITTLE_ENDIAN
                                v0=sh[0], v1=sh[1];
 #else
                                iv=&ovec[0];
index c92f18e..49afaa0 100644 (file)
@@ -43,6 +43,7 @@
  */
 #include <stdlib.h>
 #include <string.h>
+#include <machine/endian.h>
 
 #include <openssl/crypto.h>
 #include <openssl/sha.h>
@@ -108,7 +109,7 @@ int SHA512_Final (unsigned char *md, SHA512_CTX *c)
                sha512_block_data_order (c,p,1);
 
        memset (p+n,0,sizeof(c->u)-16-n);
-#ifdef B_ENDIAN
+#if _BYTE_ORDER == _BIG_ENDIAN
        c->u.d[SHA_LBLOCK-2] = c->Nh;
        c->u.d[SHA_LBLOCK-1] = c->Nl;
 #else
@@ -437,7 +438,7 @@ static void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t num
 
        for (i=0;i<16;i++)
                {
-#ifdef B_ENDIAN
+#if _BYTE_ORDER == _BIG_ENDIAN
                T1 = X[i] = W[i];
 #else
                T1 = X[i] = PULL64(W[i]);
@@ -492,7 +493,7 @@ static void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t 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];
 
-#ifdef B_ENDIAN
+#if _BYTE_ORDER == _BIG_ENDIAN
        T1 = X[0] = W[0];       ROUND_00_15(0,a,b,c,d,e,f,g,h);
        T1 = X[1] = W[1];       ROUND_00_15(1,h,a,b,c,d,e,f,g);
        T1 = X[2] = W[2];       ROUND_00_15(2,g,h,a,b,c,d,e,f);
index fadad01..11164e5 100644 (file)
@@ -37,6 +37,7 @@
 
 #include "wp_locl.h"
 #include <string.h>
+#include <machine/endian.h>
 
 typedef unsigned char          u8;
 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32)
@@ -82,10 +83,10 @@ typedef unsigned long long  u64;
 #      define ROTATE(a,n)      ({ u64 ret; asm ("rolq %1,%0"   \
                                   : "=r"(ret) : "J"(n),"0"(a) : "cc"); ret; })
 #  elif defined(__ia64) || defined(__ia64__)
-#    if defined(L_ENDIAN)
+#    if _BYTE_ORDER == _LITTLE_ENDIAN
 #      define ROTATE(a,n)      ({ u64 ret; asm ("shrp %0=%1,%1,%2"     \
                                   : "=r"(ret) : "r"(a),"M"(64-(n))); ret; })
-#    elif defined(B_ENDIAN)
+#    else
 #      define ROTATE(a,n)      ({ u64 ret; asm ("shrp %0=%1,%1,%2"     \
                                   : "=r"(ret) : "r"(a),"M"(n)); ret; })
 #    endif
@@ -94,9 +95,9 @@ typedef unsigned long long    u64;
 
 #if defined(OPENSSL_SMALL_FOOTPRINT)
 #  if !defined(ROTATE)
-#    if defined(L_ENDIAN)      /* little-endians have to rotate left */
+#    if _BYTE_ORDER == _LITTLE_ENDIAN  /* little-endians have to rotate left */
 #      define ROTATE(i,n)      ((i)<<(n) ^ (i)>>(64-n))
-#    elif defined(B_ENDIAN)    /* big-endians have to rotate right */
+#    else                              /* big-endians have to rotate right */
 #      define ROTATE(i,n)      ((i)>>(n) ^ (i)<<(64-n))
 #    endif
 #  endif
index 1e4cf83..4d4a7b8 100644 (file)
@@ -59,6 +59,8 @@
 #include "bn_lcl.h"
 #include "cryptlib.h"
 
+#include <machine/endian.h>
+
 
 #define BN_NIST_192_TOP        (192+BN_BITS2-1)/BN_BITS2
 #define BN_NIST_224_TOP        (224+BN_BITS2-1)/BN_BITS2
@@ -318,7 +320,7 @@ static void nist_cp_bn(BN_ULONG *dst, const BN_ULONG *src, int top)
                                                :(to[(n)/2] =((m)&1)?(from[(m)/2]>>32):(from[(m)/2]&BN_MASK2l)))
 #define bn_32_set_0(to, n)             (((n)&1)?(to[(n)/2]&=BN_MASK2l):(to[(n)/2]=0));
 #define bn_cp_32(to,n,from,m)          ((m)>=0)?bn_cp_32_naked(to,n,from,m):bn_32_set_0(to,n)
-# if defined(L_ENDIAN)
+# if _BYTE_ORDER == _LITTLE_ENDIAN
 #  if defined(__arch64__)
 #   define NIST_INT64 long
 #  else
index dd5237a..9bf6466 100644 (file)
@@ -58,6 +58,7 @@
 
 #include "des_locl.h"
 #include <assert.h>
+#include <machine/endian.h>
 
 /* The input and output are loaded in multiples of 8 bits.
  * What this means is that if you hame numbits=12 and length=2
@@ -76,7 +77,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
        register int num=numbits/8,n=(numbits+7)/8,i,rem=numbits%8;
        DES_LONG ti[2];
        unsigned char *iv;
-#ifndef L_ENDIAN
+#if _BYTE_ORDER != _LITTLE_ENDIAN
        unsigned char ovec[16];
 #else
        unsigned int  sh[4];
@@ -114,7 +115,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                { v0=d0; v1=d1; }
                        else
                                {
-#ifndef L_ENDIAN
+#if _BYTE_ORDER != _LITTLE_ENDIAN
                                iv=&ovec[0];
                                l2c(v0,iv);
                                l2c(v1,iv);
@@ -129,7 +130,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                        for(i=0 ; i < 8 ; ++i)
                                                ovec[i]=ovec[i+num]<<rem |
                                                        ovec[i+num+1]>>(8-rem);
-#ifdef L_ENDIAN
+#if _BYTE_ORDER == _LITTLE_ENDIAN
                                v0=sh[0], v1=sh[1];
 #else
                                iv=&ovec[0];
@@ -157,7 +158,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                { v0=d0; v1=d1; }
                        else
                                {
-#ifndef L_ENDIAN
+#if _BYTE_ORDER != _LITTLE_ENDIAN
                                iv=&ovec[0];
                                l2c(v0,iv);
                                l2c(v1,iv);
@@ -172,7 +173,7 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
                                        for(i=0 ; i < 8 ; ++i)
                                                ovec[i]=ovec[i+num]<<rem |
                                                        ovec[i+num+1]>>(8-rem);
-#ifdef L_ENDIAN
+#if _BYTE_ORDER == _LITTLE_ENDIAN
                                v0=sh[0], v1=sh[1];
 #else
                                iv=&ovec[0];
index c92f18e..49afaa0 100644 (file)
@@ -43,6 +43,7 @@
  */
 #include <stdlib.h>
 #include <string.h>
+#include <machine/endian.h>
 
 #include <openssl/crypto.h>
 #include <openssl/sha.h>
@@ -108,7 +109,7 @@ int SHA512_Final (unsigned char *md, SHA512_CTX *c)
                sha512_block_data_order (c,p,1);
 
        memset (p+n,0,sizeof(c->u)-16-n);
-#ifdef B_ENDIAN
+#if _BYTE_ORDER == _BIG_ENDIAN
        c->u.d[SHA_LBLOCK-2] = c->Nh;
        c->u.d[SHA_LBLOCK-1] = c->Nl;
 #else
@@ -437,7 +438,7 @@ static void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t num
 
        for (i=0;i<16;i++)
                {
-#ifdef B_ENDIAN
+#if _BYTE_ORDER == _BIG_ENDIAN
                T1 = X[i] = W[i];
 #else
                T1 = X[i] = PULL64(W[i]);
@@ -492,7 +493,7 @@ static void sha512_block_data_order (SHA512_CTX *ctx, const void *in, size_t 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];
 
-#ifdef B_ENDIAN
+#if _BYTE_ORDER == _BIG_ENDIAN
        T1 = X[0] = W[0];       ROUND_00_15(0,a,b,c,d,e,f,g,h);
        T1 = X[1] = W[1];       ROUND_00_15(1,h,a,b,c,d,e,f,g);
        T1 = X[2] = W[2];       ROUND_00_15(2,g,h,a,b,c,d,e,f);
index fadad01..11164e5 100644 (file)
@@ -37,6 +37,7 @@
 
 #include "wp_locl.h"
 #include <string.h>
+#include <machine/endian.h>
 
 typedef unsigned char          u8;
 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32)
@@ -82,10 +83,10 @@ typedef unsigned long long  u64;
 #      define ROTATE(a,n)      ({ u64 ret; asm ("rolq %1,%0"   \
                                   : "=r"(ret) : "J"(n),"0"(a) : "cc"); ret; })
 #  elif defined(__ia64) || defined(__ia64__)
-#    if defined(L_ENDIAN)
+#    if _BYTE_ORDER == _LITTLE_ENDIAN
 #      define ROTATE(a,n)      ({ u64 ret; asm ("shrp %0=%1,%1,%2"     \
                                   : "=r"(ret) : "r"(a),"M"(64-(n))); ret; })
-#    elif defined(B_ENDIAN)
+#    else
 #      define ROTATE(a,n)      ({ u64 ret; asm ("shrp %0=%1,%1,%2"     \
                                   : "=r"(ret) : "r"(a),"M"(n)); ret; })
 #    endif
@@ -94,9 +95,9 @@ typedef unsigned long long    u64;
 
 #if defined(OPENSSL_SMALL_FOOTPRINT)
 #  if !defined(ROTATE)
-#    if defined(L_ENDIAN)      /* little-endians have to rotate left */
+#    if _BYTE_ORDER == _LITTLE_ENDIAN  /* little-endians have to rotate left */
 #      define ROTATE(i,n)      ((i)<<(n) ^ (i)>>(64-n))
-#    elif defined(B_ENDIAN)    /* big-endians have to rotate right */
+#    else                              /* big-endians have to rotate right */
 #      define ROTATE(i,n)      ((i)>>(n) ^ (i)<<(64-n))
 #    endif
 #  endif