Get __STRICT_ALIGNMENT from <machine/endian.h> and decide upon it, rather
authormiod <miod@openbsd.org>
Wed, 7 May 2014 22:05:48 +0000 (22:05 +0000)
committermiod <miod@openbsd.org>
Wed, 7 May 2014 22:05:48 +0000 (22:05 +0000)
than defining it for not (i386 and amd64 (and sometimes s390)) only.

Compile-time tests remain compile-time tests, and runtime-test remain
runtime-test instead of being converted to compile-time tests, per matthew@'s
explicit demand (rationale: this makes sure the compiler checks your code even
if you won't run it).

No functional change except on s390 (which we don't run on) and vax (which we
run on, but noone cares about)

ok matthew@

18 files changed:
lib/libcrypto/modes/cbc128.c
lib/libcrypto/modes/ccm128.c
lib/libcrypto/modes/cfb128.c
lib/libcrypto/modes/ctr128.c
lib/libcrypto/modes/gcm128.c
lib/libcrypto/modes/modes_lcl.h
lib/libcrypto/modes/ofb128.c
lib/libcrypto/modes/xts128.c
lib/libcrypto/sha/sha512.c
lib/libssl/src/crypto/modes/cbc128.c
lib/libssl/src/crypto/modes/ccm128.c
lib/libssl/src/crypto/modes/cfb128.c
lib/libssl/src/crypto/modes/ctr128.c
lib/libssl/src/crypto/modes/gcm128.c
lib/libssl/src/crypto/modes/modes_lcl.h
lib/libssl/src/crypto/modes/ofb128.c
lib/libssl/src/crypto/modes/xts128.c
lib/libssl/src/crypto/sha/sha512.c

index 0e54f75..e4920a9 100644 (file)
 #endif
 #include <assert.h>
 
-#ifndef STRICT_ALIGNMENT
-#  define STRICT_ALIGNMENT 0
+#undef STRICT_ALIGNMENT
+#ifdef __STRICT_ALIGNMENT
+#define STRICT_ALIGNMENT 1
+#else
+#define STRICT_ALIGNMENT 0
 #endif
 
 void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
@@ -136,8 +139,7 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
                                in  += 16;
                                out += 16;
                        }
-               }
-               else  if (16%sizeof(size_t) == 0) { /* always true */
+               } else if (16%sizeof(size_t) == 0) { /* always true */
                        while (len>=16) {
                                size_t *out_t=(size_t *)out, *iv_t=(size_t *)iv;
 
@@ -166,8 +168,7 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
                                in  += 16;
                                out += 16;
                        }
-               }
-               else if (16%sizeof(size_t) == 0) { /* always true */
+               } else if (16%sizeof(size_t) == 0) { /* always true */
                        while (len>=16) {
                                size_t c, *out_t=(size_t *)out, *ivec_t=(size_t *)ivec;
                                const size_t *in_t=(const size_t *)in;
index 3ce11d0..13bc7ad 100644 (file)
@@ -197,7 +197,7 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
        if (ctx->blocks > (U64(1)<<61)) return -2; /* too much data */
 
        while (len>=16) {
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                union { u64 u[2]; u8 c[16]; } temp;
 
                memcpy (temp.c,inp,16);
@@ -210,7 +210,7 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
                (*block)(ctx->cmac.c,ctx->cmac.c,key);
                (*block)(ctx->nonce.c,scratch.c,key);
                ctr64_inc(ctx->nonce.c);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                temp.u[0] ^= scratch.u[0];
                temp.u[1] ^= scratch.u[1];
                memcpy(out,temp.c,16);
@@ -268,12 +268,12 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
        if (n!=len) return -1;
 
        while (len>=16) {
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                union { u64 u[2]; u8 c[16]; } temp;
 #endif
                (*block)(ctx->nonce.c,scratch.c,key);
                ctr64_inc(ctx->nonce.c);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                memcpy (temp.c,inp,16);
                ctx->cmac.u[0] ^= (scratch.u[0] ^= temp.u[0]);
                ctx->cmac.u[1] ^= (scratch.u[1] ^= temp.u[1]);
index 4e6f5d3..731cb28 100644 (file)
@@ -83,7 +83,7 @@ void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
                        --len;
                        n = (n+1) % 16;
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
@@ -128,7 +128,7 @@ void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
                        --len;
                        n = (n+1) % 16;
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
index 96af854..ab45e0b 100644 (file)
@@ -133,7 +133,7 @@ void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
                        n = (n+1) % 16;
                }
 
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
index 92b7f4f..f3bcb7d 100644 (file)
@@ -60,7 +60,7 @@
 #endif
 #include <assert.h>
 
-#if defined(BSWAP4) && defined(STRICT_ALIGNMENT)
+#if defined(BSWAP4) && defined(__STRICT_ALIGNMENT)
 /* redefine, because alignment is ensured */
 #undef GETU32
 #define        GETU32(p)       BSWAP4(*(const u32 *)(p))
@@ -935,7 +935,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
                                return 0;
                        }
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out)%sizeof(size_t) != 0)
                        break;
 #endif
@@ -1113,7 +1113,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
                                return 0;
                        }
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out)%sizeof(size_t) != 0)
                        break;
 #endif
index 68c0e35..a53333d 100644 (file)
@@ -22,14 +22,6 @@ typedef unsigned long long u64;
 typedef unsigned int u32;
 typedef unsigned char u8;
 
-#define STRICT_ALIGNMENT 1
-#if defined(__i386)    || defined(__i386__)    || \
-    defined(__x86_64)  || defined(__x86_64__)  || \
-    defined(_M_IX86)   || defined(_M_AMD64)    || defined(_M_X64) || \
-    defined(__s390__)  || defined(__s390x__)
-# undef STRICT_ALIGNMENT
-#endif
-
 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
 #if defined(__GNUC__) && __GNUC__>=2
 # if defined(__x86_64) || defined(__x86_64__)
@@ -47,7 +39,7 @@ typedef unsigned char u8;
 #  define BSWAP4(x) ({ u32 ret=(x);                    \
                        asm ("bswapl %0"                \
                        : "+r"(ret));   ret;            })
-# elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
+# elif (defined(__arm__) || defined(__arm)) && !defined(__STRICT_ALIGNMENT)
 #  define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x);     \
                        asm ("rev %0,%0; rev %1,%1"     \
                        : "+r"(hi),"+r"(lo));           \
@@ -60,7 +52,7 @@ typedef unsigned char u8;
 #endif
 #endif
 
-#if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
+#if defined(BSWAP4) && !defined(__STRICT_ALIGNMENT)
 #define GETU32(p)      BSWAP4(*(const u32 *)(p))
 #define PUTU32(p,v)    *(u32 *)(p) = BSWAP4(v)
 #else
index 01c0170..147c80c 100644 (file)
@@ -82,7 +82,7 @@ void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
                        --len;
                        n = (n+1) % 16;
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
index de23de4..9dcd168 100644 (file)
@@ -47,6 +47,7 @@
  * ====================================================================
  */
 
+#include <machine/endian.h>
 #include <openssl/crypto.h>
 #include "modes_lcl.h"
 #include <string.h>
@@ -74,7 +75,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
        if (!enc && (len%16)) len-=16;
 
        while (len>=16) {
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                memcpy(scratch.c,inp,16);
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
@@ -83,7 +84,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
                scratch.u[1] = ((u64*)inp)[1]^tweak.u[1];
 #endif
                (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
                memcpy(out,scratch.c,16);
@@ -152,7 +153,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
                        }
                        tweak1.c[0] ^= (u8)(0x87&(0-c));
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                memcpy(scratch.c,inp,16);
                scratch.u[0] ^= tweak1.u[0];
                scratch.u[1] ^= tweak1.u[1];
@@ -172,7 +173,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
                (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
                memcpy (out,scratch.c,16);
index d8fa933..e05718d 100644 (file)
 
 const char SHA512_version[]="SHA-512" OPENSSL_VERSION_PTEXT;
 
-#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
-    defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || \
-    defined(__s390__) || defined(__s390x__) || \
-    defined(SHA512_ASM)
+#if !defined(__STRICT_ALIGNMENT) || defined(SHA512_ASM)
 #define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
 #endif
 
index 0e54f75..e4920a9 100644 (file)
 #endif
 #include <assert.h>
 
-#ifndef STRICT_ALIGNMENT
-#  define STRICT_ALIGNMENT 0
+#undef STRICT_ALIGNMENT
+#ifdef __STRICT_ALIGNMENT
+#define STRICT_ALIGNMENT 1
+#else
+#define STRICT_ALIGNMENT 0
 #endif
 
 void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
@@ -136,8 +139,7 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
                                in  += 16;
                                out += 16;
                        }
-               }
-               else  if (16%sizeof(size_t) == 0) { /* always true */
+               } else if (16%sizeof(size_t) == 0) { /* always true */
                        while (len>=16) {
                                size_t *out_t=(size_t *)out, *iv_t=(size_t *)iv;
 
@@ -166,8 +168,7 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
                                in  += 16;
                                out += 16;
                        }
-               }
-               else if (16%sizeof(size_t) == 0) { /* always true */
+               } else if (16%sizeof(size_t) == 0) { /* always true */
                        while (len>=16) {
                                size_t c, *out_t=(size_t *)out, *ivec_t=(size_t *)ivec;
                                const size_t *in_t=(const size_t *)in;
index 3ce11d0..13bc7ad 100644 (file)
@@ -197,7 +197,7 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
        if (ctx->blocks > (U64(1)<<61)) return -2; /* too much data */
 
        while (len>=16) {
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                union { u64 u[2]; u8 c[16]; } temp;
 
                memcpy (temp.c,inp,16);
@@ -210,7 +210,7 @@ int CRYPTO_ccm128_encrypt(CCM128_CONTEXT *ctx,
                (*block)(ctx->cmac.c,ctx->cmac.c,key);
                (*block)(ctx->nonce.c,scratch.c,key);
                ctr64_inc(ctx->nonce.c);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                temp.u[0] ^= scratch.u[0];
                temp.u[1] ^= scratch.u[1];
                memcpy(out,temp.c,16);
@@ -268,12 +268,12 @@ int CRYPTO_ccm128_decrypt(CCM128_CONTEXT *ctx,
        if (n!=len) return -1;
 
        while (len>=16) {
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                union { u64 u[2]; u8 c[16]; } temp;
 #endif
                (*block)(ctx->nonce.c,scratch.c,key);
                ctr64_inc(ctx->nonce.c);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                memcpy (temp.c,inp,16);
                ctx->cmac.u[0] ^= (scratch.u[0] ^= temp.u[0]);
                ctx->cmac.u[1] ^= (scratch.u[1] ^= temp.u[1]);
index 4e6f5d3..731cb28 100644 (file)
@@ -83,7 +83,7 @@ void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
                        --len;
                        n = (n+1) % 16;
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
@@ -128,7 +128,7 @@ void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
                        --len;
                        n = (n+1) % 16;
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
index 96af854..ab45e0b 100644 (file)
@@ -133,7 +133,7 @@ void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
                        n = (n+1) % 16;
                }
 
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
index 92b7f4f..f3bcb7d 100644 (file)
@@ -60,7 +60,7 @@
 #endif
 #include <assert.h>
 
-#if defined(BSWAP4) && defined(STRICT_ALIGNMENT)
+#if defined(BSWAP4) && defined(__STRICT_ALIGNMENT)
 /* redefine, because alignment is ensured */
 #undef GETU32
 #define        GETU32(p)       BSWAP4(*(const u32 *)(p))
@@ -935,7 +935,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
                                return 0;
                        }
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out)%sizeof(size_t) != 0)
                        break;
 #endif
@@ -1113,7 +1113,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
                                return 0;
                        }
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out)%sizeof(size_t) != 0)
                        break;
 #endif
index 68c0e35..a53333d 100644 (file)
@@ -22,14 +22,6 @@ typedef unsigned long long u64;
 typedef unsigned int u32;
 typedef unsigned char u8;
 
-#define STRICT_ALIGNMENT 1
-#if defined(__i386)    || defined(__i386__)    || \
-    defined(__x86_64)  || defined(__x86_64__)  || \
-    defined(_M_IX86)   || defined(_M_AMD64)    || defined(_M_X64) || \
-    defined(__s390__)  || defined(__s390x__)
-# undef STRICT_ALIGNMENT
-#endif
-
 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
 #if defined(__GNUC__) && __GNUC__>=2
 # if defined(__x86_64) || defined(__x86_64__)
@@ -47,7 +39,7 @@ typedef unsigned char u8;
 #  define BSWAP4(x) ({ u32 ret=(x);                    \
                        asm ("bswapl %0"                \
                        : "+r"(ret));   ret;            })
-# elif (defined(__arm__) || defined(__arm)) && !defined(STRICT_ALIGNMENT)
+# elif (defined(__arm__) || defined(__arm)) && !defined(__STRICT_ALIGNMENT)
 #  define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x);     \
                        asm ("rev %0,%0; rev %1,%1"     \
                        : "+r"(hi),"+r"(lo));           \
@@ -60,7 +52,7 @@ typedef unsigned char u8;
 #endif
 #endif
 
-#if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
+#if defined(BSWAP4) && !defined(__STRICT_ALIGNMENT)
 #define GETU32(p)      BSWAP4(*(const u32 *)(p))
 #define PUTU32(p,v)    *(u32 *)(p) = BSWAP4(v)
 #else
index 01c0170..147c80c 100644 (file)
@@ -82,7 +82,7 @@ void CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
                        --len;
                        n = (n+1) % 16;
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
                        break;
 #endif
index de23de4..9dcd168 100644 (file)
@@ -47,6 +47,7 @@
  * ====================================================================
  */
 
+#include <machine/endian.h>
 #include <openssl/crypto.h>
 #include "modes_lcl.h"
 #include <string.h>
@@ -74,7 +75,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
        if (!enc && (len%16)) len-=16;
 
        while (len>=16) {
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                memcpy(scratch.c,inp,16);
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
@@ -83,7 +84,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
                scratch.u[1] = ((u64*)inp)[1]^tweak.u[1];
 #endif
                (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
                memcpy(out,scratch.c,16);
@@ -152,7 +153,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
                        }
                        tweak1.c[0] ^= (u8)(0x87&(0-c));
                }
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                memcpy(scratch.c,inp,16);
                scratch.u[0] ^= tweak1.u[0];
                scratch.u[1] ^= tweak1.u[1];
@@ -172,7 +173,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
                (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
-#if defined(STRICT_ALIGNMENT)
+#ifdef __STRICT_ALIGNMENT
                scratch.u[0] ^= tweak.u[0];
                scratch.u[1] ^= tweak.u[1];
                memcpy (out,scratch.c,16);
index d8fa933..e05718d 100644 (file)
 
 const char SHA512_version[]="SHA-512" OPENSSL_VERSION_PTEXT;
 
-#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
-    defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || \
-    defined(__s390__) || defined(__s390x__) || \
-    defined(SHA512_ASM)
+#if !defined(__STRICT_ALIGNMENT) || defined(SHA512_ASM)
 #define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
 #endif