summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMiod Vallat <miod@cvs.openbsd.org>2014-05-07 22:05:49 +0000
committerMiod Vallat <miod@cvs.openbsd.org>2014-05-07 22:05:49 +0000
commit8906389194935dbfcf0ef025420cf78e09d0ece8 (patch)
treeca44756fe996247fed8af7b631a32851151a8939 /lib
parentcdabd6d89101b4aa090a0f858b81947548abbee6 (diff)
Get __STRICT_ALIGNMENT from <machine/endian.h> and decide upon it, rather
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@
Diffstat (limited to 'lib')
-rw-r--r--lib/libcrypto/modes/cbc128.c40
-rw-r--r--lib/libcrypto/modes/ccm128.c10
-rw-r--r--lib/libcrypto/modes/cfb128.c15
-rw-r--r--lib/libcrypto/modes/ctr128.c2
-rw-r--r--lib/libcrypto/modes/gcm128.c6
-rw-r--r--lib/libcrypto/modes/modes_lcl.h12
-rw-r--r--lib/libcrypto/modes/ofb128.c13
-rw-r--r--lib/libcrypto/modes/xts128.c9
-rw-r--r--lib/libcrypto/sha/sha512.c5
9 files changed, 44 insertions, 68 deletions
diff --git a/lib/libcrypto/modes/cbc128.c b/lib/libcrypto/modes/cbc128.c
index 8f8bd563b96..e4920a93ac2 100644
--- a/lib/libcrypto/modes/cbc128.c
+++ b/lib/libcrypto/modes/cbc128.c
@@ -48,7 +48,8 @@
*
*/
-#include "modes.h"
+#include <openssl/crypto.h>
+#include "modes_lcl.h"
#include <string.h>
#ifndef MODES_DEBUG
@@ -58,13 +59,11 @@
#endif
#include <assert.h>
+#undef STRICT_ALIGNMENT
+#ifdef __STRICT_ALIGNMENT
#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
-# define STRICT_ALIGNMENT 0
+#else
+#define STRICT_ALIGNMENT 0
#endif
void CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
@@ -121,7 +120,7 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
unsigned char ivec[16], block128_f block)
{
size_t n;
- union { size_t align; unsigned char c[16]; } tmp;
+ union { size_t t[16/sizeof(size_t)]; unsigned char c[16]; } tmp;
assert(in && out && key && ivec);
@@ -140,12 +139,13 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
in += 16;
out += 16;
}
- }
- else {
+ } else if (16%sizeof(size_t) == 0) { /* always true */
while (len>=16) {
+ size_t *out_t=(size_t *)out, *iv_t=(size_t *)iv;
+
(*block)(in, out, key);
- for(n=0; n<16; n+=sizeof(size_t))
- *(size_t *)(out+n) ^= *(size_t *)(iv+n);
+ for(n=0; n<16/sizeof(size_t); n++)
+ out_t[n] ^= iv_t[n];
iv = in;
len -= 16;
in += 16;
@@ -168,16 +168,16 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
in += 16;
out += 16;
}
- }
- else {
- size_t c;
+ } 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;
+
(*block)(in, tmp.c, key);
- for(n=0; n<16; n+=sizeof(size_t)) {
- c = *(size_t *)(in+n);
- *(size_t *)(out+n) =
- *(size_t *)(tmp.c+n) ^ *(size_t *)(ivec+n);
- *(size_t *)(ivec+n) = c;
+ for(n=0; n<16/sizeof(size_t); n++) {
+ c = in_t[n];
+ out_t[n] = tmp.t[n] ^ ivec_t[n];
+ ivec_t[n] = c;
}
len -= 16;
in += 16;
diff --git a/lib/libcrypto/modes/ccm128.c b/lib/libcrypto/modes/ccm128.c
index c9b35e5b35e..13bc7adf31d 100644
--- a/lib/libcrypto/modes/ccm128.c
+++ b/lib/libcrypto/modes/ccm128.c
@@ -87,7 +87,7 @@ int CRYPTO_ccm128_setiv(CCM128_CONTEXT *ctx,
ctx->nonce.c[11] = (u8)(mlen>>(32%(sizeof(mlen)*8)));
}
else
- *(u32*)(&ctx->nonce.c[8]) = 0;
+ ctx->nonce.u[1] = 0;
ctx->nonce.c[12] = (u8)(mlen>>24);
ctx->nonce.c[13] = (u8)(mlen>>16);
@@ -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]);
diff --git a/lib/libcrypto/modes/cfb128.c b/lib/libcrypto/modes/cfb128.c
index e5938c6137c..731cb2864aa 100644
--- a/lib/libcrypto/modes/cfb128.c
+++ b/lib/libcrypto/modes/cfb128.c
@@ -48,7 +48,8 @@
*
*/
-#include "modes.h"
+#include <openssl/crypto.h>
+#include "modes_lcl.h"
#include <string.h>
#ifndef MODES_DEBUG
@@ -58,14 +59,6 @@
#endif
#include <assert.h>
-#define STRICT_ALIGNMENT
-#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
-
/* The input and output encrypted as though 128bit cfb mode is being
* used. The extra state information to record how much of the
* 128bit block we have used is contained in *num;
@@ -90,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
@@ -135,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
diff --git a/lib/libcrypto/modes/ctr128.c b/lib/libcrypto/modes/ctr128.c
index 96af854f8a0..ab45e0bd7a0 100644
--- a/lib/libcrypto/modes/ctr128.c
+++ b/lib/libcrypto/modes/ctr128.c
@@ -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
diff --git a/lib/libcrypto/modes/gcm128.c b/lib/libcrypto/modes/gcm128.c
index 92b7f4f3c8a..f3bcb7dd6e1 100644
--- a/lib/libcrypto/modes/gcm128.c
+++ b/lib/libcrypto/modes/gcm128.c
@@ -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
diff --git a/lib/libcrypto/modes/modes_lcl.h b/lib/libcrypto/modes/modes_lcl.h
index 68c0e355ad9..a53333df3d7 100644
--- a/lib/libcrypto/modes/modes_lcl.h
+++ b/lib/libcrypto/modes/modes_lcl.h
@@ -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
diff --git a/lib/libcrypto/modes/ofb128.c b/lib/libcrypto/modes/ofb128.c
index c732e2ec58e..147c80c5498 100644
--- a/lib/libcrypto/modes/ofb128.c
+++ b/lib/libcrypto/modes/ofb128.c
@@ -48,7 +48,8 @@
*
*/
-#include "modes.h"
+#include <openssl/crypto.h>
+#include "modes_lcl.h"
#include <string.h>
#ifndef MODES_DEBUG
@@ -58,14 +59,6 @@
#endif
#include <assert.h>
-#define STRICT_ALIGNMENT
-#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
-
/* The input and output encrypted as though 128bit ofb mode is being
* used. The extra state information to record how much of the
* 128bit block we have used is contained in *num;
@@ -89,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
diff --git a/lib/libcrypto/modes/xts128.c b/lib/libcrypto/modes/xts128.c
index de23de457d8..9dcd16885d4 100644
--- a/lib/libcrypto/modes/xts128.c
+++ b/lib/libcrypto/modes/xts128.c
@@ -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);
diff --git a/lib/libcrypto/sha/sha512.c b/lib/libcrypto/sha/sha512.c
index d8fa933cde2..e05718d0a83 100644
--- a/lib/libcrypto/sha/sha512.c
+++ b/lib/libcrypto/sha/sha512.c
@@ -53,10 +53,7 @@
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