From 7f5d17891e05458836147cd2b05889fde1e7be19 Mon Sep 17 00:00:00 2001
From: miod <>
Date: Wed, 23 Apr 2014 18:40:39 +0000
Subject: Figure out endianness at compile-time, using _BYTE_ORDER from
 <machine/endian.h>, rather than writing 1 to a 32-bit variable and checking
 whether the first byte is nonzero.

tweaks and ok matthew@; ok beck@ tedu@
---
 src/lib/libcrypto/evp/bio_ok.c              |  6 +--
 src/lib/libcrypto/modes/ctr128.c            |  7 +--
 src/lib/libcrypto/modes/gcm128.c            | 75 ++++++++++++-----------------
 src/lib/libcrypto/modes/modes_lcl.h         |  1 +
 src/lib/libcrypto/modes/xts128.c            |  5 +-
 src/lib/libcrypto/rc4/rc4_enc.c             | 10 ++--
 src/lib/libcrypto/sha/sha256.c              |  5 +-
 src/lib/libcrypto/sha/sha_locl.h            |  5 +-
 src/lib/libssl/d1_pkt.c                     |  7 +--
 src/lib/libssl/src/crypto/evp/bio_ok.c      |  6 +--
 src/lib/libssl/src/crypto/modes/ctr128.c    |  7 +--
 src/lib/libssl/src/crypto/modes/gcm128.c    | 75 ++++++++++++-----------------
 src/lib/libssl/src/crypto/modes/modes_lcl.h |  1 +
 src/lib/libssl/src/crypto/modes/xts128.c    |  5 +-
 src/lib/libssl/src/crypto/rc4/rc4_enc.c     | 10 ++--
 src/lib/libssl/src/crypto/sha/sha256.c      |  5 +-
 src/lib/libssl/src/crypto/sha/sha_locl.h    |  5 +-
 src/lib/libssl/src/ssl/d1_pkt.c             |  7 +--
 18 files changed, 104 insertions(+), 138 deletions(-)

diff --git a/src/lib/libcrypto/evp/bio_ok.c b/src/lib/libcrypto/evp/bio_ok.c
index fdb742f554..09a762ffac 100644
--- a/src/lib/libcrypto/evp/bio_ok.c
+++ b/src/lib/libcrypto/evp/bio_ok.c
@@ -120,6 +120,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <assert.h>
+#include <machine/endian.h>
 #include "cryptlib.h"
 #include <openssl/buffer.h>
 #include <openssl/bio.h>
@@ -463,9 +464,8 @@ static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
 	}
 
 static void longswap(void *_ptr, size_t len)
-{	const union { long one; char little; } is_endian = {1};
-
-	if (is_endian.little) {
+{
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 		size_t i;
 		unsigned char *p=_ptr,c;
 
diff --git a/src/lib/libcrypto/modes/ctr128.c b/src/lib/libcrypto/modes/ctr128.c
index ee642c5863..96af854f8a 100644
--- a/src/lib/libcrypto/modes/ctr128.c
+++ b/src/lib/libcrypto/modes/ctr128.c
@@ -77,11 +77,12 @@ static void ctr128_inc(unsigned char *counter) {
 }
 
 #if !defined(OPENSSL_SMALL_FOOTPRINT)
-static void ctr128_inc_aligned(unsigned char *counter) {
+static void
+ctr128_inc_aligned(unsigned char *counter)
+{
 	size_t *data,c,n;
-	const union { long one; char little; } is_endian = {1};
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 		ctr128_inc(counter);
 		return;
 	}
diff --git a/src/lib/libcrypto/modes/gcm128.c b/src/lib/libcrypto/modes/gcm128.c
index a495db110f..92b7f4f3c8 100644
--- a/src/lib/libcrypto/modes/gcm128.c
+++ b/src/lib/libcrypto/modes/gcm128.c
@@ -147,7 +147,6 @@ static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
 	u128 Z = { 0, 0};
 	const u8 *xi = (const u8 *)Xi+15;
 	size_t rem, n = *xi;
-	const union { long one; char little; } is_endian = {1};
 	static const size_t rem_8bit[256] = {
 		PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
 		PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
@@ -231,7 +230,7 @@ static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
 			Z.hi ^= (u64)rem_8bit[rem]<<32;
 	}
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -307,9 +306,8 @@ static void gcm_init_4bit(u128 Htable[16], u64 H[2])
 	 */
 	{
 	int j;
-	const union { long one; char little; } is_endian = {1};
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 		for (j=0;j<16;++j) {
 			V = Htable[j];
 			Htable[j].hi = V.lo;
@@ -337,7 +335,6 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
 	u128 Z;
 	int cnt = 15;
 	size_t rem, nlo, nhi;
-	const union { long one; char little; } is_endian = {1};
 
 	nlo  = ((const u8 *)Xi)[15];
 	nhi  = nlo>>4;
@@ -376,7 +373,7 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
 		Z.lo ^= Htable[nlo].lo;
 	}
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -409,7 +406,6 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
     u128 Z;
     int cnt;
     size_t rem, nlo, nhi;
-    const union { long one; char little; } is_endian = {1};
 
 #if 1
     do {
@@ -546,7 +542,7 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
 	Z.hi ^= ((u64)rem_8bit[rem<<4])<<48;
 #endif
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -588,13 +584,12 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
 	long X;
 	int  i,j;
 	const long *xi = (const long *)Xi;
-	const union { long one; char little; } is_endian = {1};
 
 	V.hi = H[0];	/* H is in host byte order, no byte swapping */
 	V.lo = H[1];
 
 	for (j=0; j<16/sizeof(long); ++j) {
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 			if (sizeof(long)==8) {
 #ifdef BSWAP8
 				X = (long)(BSWAP8(xi[j]));
@@ -620,7 +615,7 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
 		}
 	}
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -685,15 +680,13 @@ void gcm_ghash_neon(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
 
 void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block)
 {
-	const union { long one; char little; } is_endian = {1};
-
 	memset(ctx,0,sizeof(*ctx));
 	ctx->block = block;
 	ctx->key   = key;
 
 	(*block)(ctx->H.c,ctx->H.c,key);
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 		/* H is stored in host byte order */
 #ifdef BSWAP8
 		ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
@@ -755,7 +748,6 @@ void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block)
 
 void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int ctr;
 #ifdef GCM_FUNCREF_4BIT
 	void (*gcm_gmult_p)(u64 Xi[2],const u128 Htable[16])	= ctx->gmult;
@@ -790,7 +782,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 			GCM_MUL(ctx,Yi);
 		}
 		len0 <<= 3;
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 			ctx->Yi.u[1]  ^= BSWAP8(len0);
 #else
@@ -809,7 +801,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 
 		GCM_MUL(ctx,Yi);
 
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -821,7 +813,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 
 	(*ctx->block)(ctx->Yi.c,ctx->EK0.c,ctx->key);
 	++ctr;
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -892,7 +884,6 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64        mlen  = ctx->len.u[1];
@@ -920,7 +911,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -958,7 +949,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -984,7 +975,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1007,7 +998,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1027,7 +1018,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		if (len) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1049,7 +1040,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		if (n==0) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1072,7 +1063,6 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64        mlen  = ctx->len.u[1];
@@ -1097,7 +1087,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -1138,7 +1128,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1162,7 +1152,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1184,7 +1174,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1206,7 +1196,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		if (len) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1231,7 +1221,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		if (n==0) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1256,7 +1246,6 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len, ctr128_f stream)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64   mlen = ctx->len.u[1];
@@ -1280,7 +1269,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -1306,7 +1295,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 	while (len>=GHASH_CHUNK) {
 		(*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
 		ctr += GHASH_CHUNK/16;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1325,7 +1314,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 
 		(*stream)(in,out,j,key,ctx->Yi.c);
 		ctr += (unsigned int)j;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1349,7 +1338,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 	if (len) {
 		(*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
 		++ctr;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1371,7 +1360,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len,ctr128_f stream)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64   mlen = ctx->len.u[1];
@@ -1395,7 +1383,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -1424,7 +1412,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 		GHASH(ctx,in,GHASH_CHUNK);
 		(*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
 		ctr += GHASH_CHUNK/16;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1454,7 +1442,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 #endif
 		(*stream)(in,out,j,key,ctx->Yi.c);
 		ctr += (unsigned int)j;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1469,7 +1457,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 	if (len) {
 		(*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
 		++ctr;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1492,7 +1480,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
 			size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	u64 alen = ctx->len.u[0]<<3;
 	u64 clen = ctx->len.u[1]<<3;
 #ifdef GCM_FUNCREF_4BIT
@@ -1502,7 +1489,7 @@ int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
 	if (ctx->mres || ctx->ares)
 		GCM_MUL(ctx,Xi);
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		alen = BSWAP8(alen);
 		clen = BSWAP8(clen);
diff --git a/src/lib/libcrypto/modes/modes_lcl.h b/src/lib/libcrypto/modes/modes_lcl.h
index b32c1b43c5..9057f7fd76 100644
--- a/src/lib/libcrypto/modes/modes_lcl.h
+++ b/src/lib/libcrypto/modes/modes_lcl.h
@@ -6,6 +6,7 @@
  */
 
 #include <openssl/modes.h>
+#include <machine/endian.h>
 
 
 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
diff --git a/src/lib/libcrypto/modes/xts128.c b/src/lib/libcrypto/modes/xts128.c
index 9cf27a25e9..de23de457d 100644
--- a/src/lib/libcrypto/modes/xts128.c
+++ b/src/lib/libcrypto/modes/xts128.c
@@ -62,7 +62,6 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
 	const unsigned char *inp, unsigned char *out,
 	size_t len, int enc)
 {
-	const union { long one; char little; } is_endian = {1};
 	union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch;
 	unsigned int i;
 
@@ -98,7 +97,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
 
 		if (len==0)	return 0;
 
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 			unsigned int carry,res;
 			
 			res = 0x87&(((int)tweak.d[3])>>31);
@@ -134,7 +133,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
 	else {
 		union { u64 u[2]; u8 c[16]; } tweak1;
 
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 			unsigned int carry,res;
 
 			res = 0x87&(((int)tweak.d[3])>>31);
diff --git a/src/lib/libcrypto/rc4/rc4_enc.c b/src/lib/libcrypto/rc4/rc4_enc.c
index 8c4fc6c7a3..d8fc939dac 100644
--- a/src/lib/libcrypto/rc4/rc4_enc.c
+++ b/src/lib/libcrypto/rc4/rc4_enc.c
@@ -56,6 +56,7 @@
  * [including the GNU Public Licence.]
  */
 
+#include <machine/endian.h>
 #include <openssl/rc4.h>
 #include "rc4_locl.h"
 
@@ -124,7 +125,6 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 	       ((size_t)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 )
 		{
 		RC4_CHUNK ichunk,otp;
-		const union { long one; char little; } is_endian = {1};
 
 		/*
 		 * I reckon we can afford to implement both endian
@@ -132,14 +132,10 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 		 * because the machine code appears to be very compact
 		 * and redundant 1-2KB is perfectly tolerable (i.e.
 		 * in case the compiler fails to eliminate it:-). By
-		 * suggestion from Terrel Larson <terr@terralogic.net>
-		 * who also stands for the is_endian union:-)
+		 * suggestion from Terrel Larson <terr@terralogic.net>.
 		 *
 		 * Special notes.
 		 *
-		 * - is_endian is declared automatic as doing otherwise
-		 *   (declaring static) prevents gcc from eliminating
-		 *   the redundant code;
 		 * - compilers (those I've tried) don't seem to have
 		 *   problems eliminating either the operators guarded
 		 *   by "if (sizeof(RC4_CHUNK)==8)" or the condition
@@ -154,7 +150,7 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 		 *
 		 *			<appro@fy.chalmers.se>
 		 */
-		if (!is_endian.little)
+		if (_BYTE_ORDER != _LITTLE_ENDIAN)
 			{	/* BIG-ENDIAN CASE */
 # define BESHFT(c)	(((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1))
 			for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK))
diff --git a/src/lib/libcrypto/sha/sha256.c b/src/lib/libcrypto/sha/sha256.c
index 4eae074849..e767afde5a 100644
--- a/src/lib/libcrypto/sha/sha256.c
+++ b/src/lib/libcrypto/sha/sha256.c
@@ -9,6 +9,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <machine/endian.h>
 
 #include <openssl/crypto.h>
 #include <openssl/sha.h>
@@ -206,14 +207,14 @@ static void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num
 	SHA_LONG	X[16];
 	int i;
 	const unsigned char *data=in;
-	const union { long one; char little; } is_endian = {1};
 
 			while (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];
 
-	if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)in%4)==0)
+	if (_BYTE_ORDER != _LITTLE_ENDIAN &&
+	    sizeof(SHA_LONG)==4 && ((size_t)in%4)==0)
 		{
 		const SHA_LONG *W=(const SHA_LONG *)data;
 
diff --git a/src/lib/libcrypto/sha/sha_locl.h b/src/lib/libcrypto/sha/sha_locl.h
index 6c6cd64282..1210176dda 100644
--- a/src/lib/libcrypto/sha/sha_locl.h
+++ b/src/lib/libcrypto/sha/sha_locl.h
@@ -202,6 +202,7 @@ fips_md_init_ctx(SHA1, SHA)
 #endif
 
 #if !defined(SHA_1) || !defined(SHA1_ASM)
+#include <machine/endian.h>
 static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
 	{
 	const unsigned char *data=p;
@@ -221,9 +222,9 @@ static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
 
 	for (;;)
 			{
-	const union { long one; char little; } is_endian = {1};
 
-	if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)p%4)==0)
+	if (_BYTE_ORDER != _LITTLE_ENDIAN &&
+	    sizeof(SHA_LONG)==4 && ((size_t)p%4)==0)
 		{
 		const SHA_LONG *W=(const SHA_LONG *)data;
 
diff --git a/src/lib/libssl/d1_pkt.c b/src/lib/libssl/d1_pkt.c
index 80a4c076bf..7cfada4e6b 100644
--- a/src/lib/libssl/d1_pkt.c
+++ b/src/lib/libssl/d1_pkt.c
@@ -115,6 +115,7 @@
 
 #include <stdio.h>
 #include <errno.h>
+#include <machine/endian.h>
 #include "ssl_locl.h"
 #include <openssl/evp.h>
 #include <openssl/buffer.h>
@@ -129,13 +130,9 @@ satsub64be(const unsigned char *v1, const unsigned char *v2)
 
 	if (sizeof(long) == 8)
 		do {
-			const union {
-				long one;
-				char little;
-			} is_endian = {1};
 			long l;
 
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 				break;
 			/* not reached on little-endians */
 			/* following test is redundant, because input is
diff --git a/src/lib/libssl/src/crypto/evp/bio_ok.c b/src/lib/libssl/src/crypto/evp/bio_ok.c
index fdb742f554..09a762ffac 100644
--- a/src/lib/libssl/src/crypto/evp/bio_ok.c
+++ b/src/lib/libssl/src/crypto/evp/bio_ok.c
@@ -120,6 +120,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <assert.h>
+#include <machine/endian.h>
 #include "cryptlib.h"
 #include <openssl/buffer.h>
 #include <openssl/bio.h>
@@ -463,9 +464,8 @@ static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
 	}
 
 static void longswap(void *_ptr, size_t len)
-{	const union { long one; char little; } is_endian = {1};
-
-	if (is_endian.little) {
+{
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 		size_t i;
 		unsigned char *p=_ptr,c;
 
diff --git a/src/lib/libssl/src/crypto/modes/ctr128.c b/src/lib/libssl/src/crypto/modes/ctr128.c
index ee642c5863..96af854f8a 100644
--- a/src/lib/libssl/src/crypto/modes/ctr128.c
+++ b/src/lib/libssl/src/crypto/modes/ctr128.c
@@ -77,11 +77,12 @@ static void ctr128_inc(unsigned char *counter) {
 }
 
 #if !defined(OPENSSL_SMALL_FOOTPRINT)
-static void ctr128_inc_aligned(unsigned char *counter) {
+static void
+ctr128_inc_aligned(unsigned char *counter)
+{
 	size_t *data,c,n;
-	const union { long one; char little; } is_endian = {1};
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 		ctr128_inc(counter);
 		return;
 	}
diff --git a/src/lib/libssl/src/crypto/modes/gcm128.c b/src/lib/libssl/src/crypto/modes/gcm128.c
index a495db110f..92b7f4f3c8 100644
--- a/src/lib/libssl/src/crypto/modes/gcm128.c
+++ b/src/lib/libssl/src/crypto/modes/gcm128.c
@@ -147,7 +147,6 @@ static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
 	u128 Z = { 0, 0};
 	const u8 *xi = (const u8 *)Xi+15;
 	size_t rem, n = *xi;
-	const union { long one; char little; } is_endian = {1};
 	static const size_t rem_8bit[256] = {
 		PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
 		PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
@@ -231,7 +230,7 @@ static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
 			Z.hi ^= (u64)rem_8bit[rem]<<32;
 	}
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -307,9 +306,8 @@ static void gcm_init_4bit(u128 Htable[16], u64 H[2])
 	 */
 	{
 	int j;
-	const union { long one; char little; } is_endian = {1};
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 		for (j=0;j<16;++j) {
 			V = Htable[j];
 			Htable[j].hi = V.lo;
@@ -337,7 +335,6 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
 	u128 Z;
 	int cnt = 15;
 	size_t rem, nlo, nhi;
-	const union { long one; char little; } is_endian = {1};
 
 	nlo  = ((const u8 *)Xi)[15];
 	nhi  = nlo>>4;
@@ -376,7 +373,7 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
 		Z.lo ^= Htable[nlo].lo;
 	}
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -409,7 +406,6 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
     u128 Z;
     int cnt;
     size_t rem, nlo, nhi;
-    const union { long one; char little; } is_endian = {1};
 
 #if 1
     do {
@@ -546,7 +542,7 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
 	Z.hi ^= ((u64)rem_8bit[rem<<4])<<48;
 #endif
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -588,13 +584,12 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
 	long X;
 	int  i,j;
 	const long *xi = (const long *)Xi;
-	const union { long one; char little; } is_endian = {1};
 
 	V.hi = H[0];	/* H is in host byte order, no byte swapping */
 	V.lo = H[1];
 
 	for (j=0; j<16/sizeof(long); ++j) {
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 			if (sizeof(long)==8) {
 #ifdef BSWAP8
 				X = (long)(BSWAP8(xi[j]));
@@ -620,7 +615,7 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
 		}
 	}
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		Xi[0] = BSWAP8(Z.hi);
 		Xi[1] = BSWAP8(Z.lo);
@@ -685,15 +680,13 @@ void gcm_ghash_neon(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
 
 void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block)
 {
-	const union { long one; char little; } is_endian = {1};
-
 	memset(ctx,0,sizeof(*ctx));
 	ctx->block = block;
 	ctx->key   = key;
 
 	(*block)(ctx->H.c,ctx->H.c,key);
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 		/* H is stored in host byte order */
 #ifdef BSWAP8
 		ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
@@ -755,7 +748,6 @@ void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block)
 
 void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int ctr;
 #ifdef GCM_FUNCREF_4BIT
 	void (*gcm_gmult_p)(u64 Xi[2],const u128 Htable[16])	= ctx->gmult;
@@ -790,7 +782,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 			GCM_MUL(ctx,Yi);
 		}
 		len0 <<= 3;
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 			ctx->Yi.u[1]  ^= BSWAP8(len0);
 #else
@@ -809,7 +801,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 
 		GCM_MUL(ctx,Yi);
 
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -821,7 +813,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
 
 	(*ctx->block)(ctx->Yi.c,ctx->EK0.c,ctx->key);
 	++ctr;
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -892,7 +884,6 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64        mlen  = ctx->len.u[1];
@@ -920,7 +911,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -958,7 +949,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -984,7 +975,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1007,7 +998,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1027,7 +1018,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		if (len) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1049,7 +1040,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
 		if (n==0) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1072,7 +1063,6 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64        mlen  = ctx->len.u[1];
@@ -1097,7 +1087,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -1138,7 +1128,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1162,7 +1152,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1184,7 +1174,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1206,7 +1196,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		if (len) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1231,7 +1221,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
 		if (n==0) {
 			(*block)(ctx->Yi.c,ctx->EKi.c,key);
 			++ctr;
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 				ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1256,7 +1246,6 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len, ctr128_f stream)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64   mlen = ctx->len.u[1];
@@ -1280,7 +1269,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -1306,7 +1295,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 	while (len>=GHASH_CHUNK) {
 		(*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
 		ctr += GHASH_CHUNK/16;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1325,7 +1314,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 
 		(*stream)(in,out,j,key,ctx->Yi.c);
 		ctr += (unsigned int)j;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1349,7 +1338,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
 	if (len) {
 		(*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
 		++ctr;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1371,7 +1360,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 		const unsigned char *in, unsigned char *out,
 		size_t len,ctr128_f stream)
 {
-	const union { long one; char little; } is_endian = {1};
 	unsigned int n, ctr;
 	size_t i;
 	u64   mlen = ctx->len.u[1];
@@ -1395,7 +1383,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 		ctx->ares = 0;
 	}
 
-	if (is_endian.little)
+	if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 		ctr = BSWAP4(ctx->Yi.d[3]);
 #else
@@ -1424,7 +1412,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 		GHASH(ctx,in,GHASH_CHUNK);
 		(*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
 		ctr += GHASH_CHUNK/16;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1454,7 +1442,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 #endif
 		(*stream)(in,out,j,key,ctx->Yi.c);
 		ctr += (unsigned int)j;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1469,7 +1457,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 	if (len) {
 		(*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
 		++ctr;
-		if (is_endian.little)
+		if (_BYTE_ORDER == _LITTLE_ENDIAN)
 #ifdef BSWAP4
 			ctx->Yi.d[3] = BSWAP4(ctr);
 #else
@@ -1492,7 +1480,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
 int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
 			size_t len)
 {
-	const union { long one; char little; } is_endian = {1};
 	u64 alen = ctx->len.u[0]<<3;
 	u64 clen = ctx->len.u[1]<<3;
 #ifdef GCM_FUNCREF_4BIT
@@ -1502,7 +1489,7 @@ int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
 	if (ctx->mres || ctx->ares)
 		GCM_MUL(ctx,Xi);
 
-	if (is_endian.little) {
+	if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 #ifdef BSWAP8
 		alen = BSWAP8(alen);
 		clen = BSWAP8(clen);
diff --git a/src/lib/libssl/src/crypto/modes/modes_lcl.h b/src/lib/libssl/src/crypto/modes/modes_lcl.h
index b32c1b43c5..9057f7fd76 100644
--- a/src/lib/libssl/src/crypto/modes/modes_lcl.h
+++ b/src/lib/libssl/src/crypto/modes/modes_lcl.h
@@ -6,6 +6,7 @@
  */
 
 #include <openssl/modes.h>
+#include <machine/endian.h>
 
 
 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
diff --git a/src/lib/libssl/src/crypto/modes/xts128.c b/src/lib/libssl/src/crypto/modes/xts128.c
index 9cf27a25e9..de23de457d 100644
--- a/src/lib/libssl/src/crypto/modes/xts128.c
+++ b/src/lib/libssl/src/crypto/modes/xts128.c
@@ -62,7 +62,6 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
 	const unsigned char *inp, unsigned char *out,
 	size_t len, int enc)
 {
-	const union { long one; char little; } is_endian = {1};
 	union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch;
 	unsigned int i;
 
@@ -98,7 +97,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
 
 		if (len==0)	return 0;
 
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 			unsigned int carry,res;
 			
 			res = 0x87&(((int)tweak.d[3])>>31);
@@ -134,7 +133,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
 	else {
 		union { u64 u[2]; u8 c[16]; } tweak1;
 
-		if (is_endian.little) {
+		if (_BYTE_ORDER == _LITTLE_ENDIAN) {
 			unsigned int carry,res;
 
 			res = 0x87&(((int)tweak.d[3])>>31);
diff --git a/src/lib/libssl/src/crypto/rc4/rc4_enc.c b/src/lib/libssl/src/crypto/rc4/rc4_enc.c
index 8c4fc6c7a3..d8fc939dac 100644
--- a/src/lib/libssl/src/crypto/rc4/rc4_enc.c
+++ b/src/lib/libssl/src/crypto/rc4/rc4_enc.c
@@ -56,6 +56,7 @@
  * [including the GNU Public Licence.]
  */
 
+#include <machine/endian.h>
 #include <openssl/rc4.h>
 #include "rc4_locl.h"
 
@@ -124,7 +125,6 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 	       ((size_t)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 )
 		{
 		RC4_CHUNK ichunk,otp;
-		const union { long one; char little; } is_endian = {1};
 
 		/*
 		 * I reckon we can afford to implement both endian
@@ -132,14 +132,10 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 		 * because the machine code appears to be very compact
 		 * and redundant 1-2KB is perfectly tolerable (i.e.
 		 * in case the compiler fails to eliminate it:-). By
-		 * suggestion from Terrel Larson <terr@terralogic.net>
-		 * who also stands for the is_endian union:-)
+		 * suggestion from Terrel Larson <terr@terralogic.net>.
 		 *
 		 * Special notes.
 		 *
-		 * - is_endian is declared automatic as doing otherwise
-		 *   (declaring static) prevents gcc from eliminating
-		 *   the redundant code;
 		 * - compilers (those I've tried) don't seem to have
 		 *   problems eliminating either the operators guarded
 		 *   by "if (sizeof(RC4_CHUNK)==8)" or the condition
@@ -154,7 +150,7 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 		 *
 		 *			<appro@fy.chalmers.se>
 		 */
-		if (!is_endian.little)
+		if (_BYTE_ORDER != _LITTLE_ENDIAN)
 			{	/* BIG-ENDIAN CASE */
 # define BESHFT(c)	(((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1))
 			for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK))
diff --git a/src/lib/libssl/src/crypto/sha/sha256.c b/src/lib/libssl/src/crypto/sha/sha256.c
index 4eae074849..e767afde5a 100644
--- a/src/lib/libssl/src/crypto/sha/sha256.c
+++ b/src/lib/libssl/src/crypto/sha/sha256.c
@@ -9,6 +9,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <machine/endian.h>
 
 #include <openssl/crypto.h>
 #include <openssl/sha.h>
@@ -206,14 +207,14 @@ static void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num
 	SHA_LONG	X[16];
 	int i;
 	const unsigned char *data=in;
-	const union { long one; char little; } is_endian = {1};
 
 			while (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];
 
-	if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)in%4)==0)
+	if (_BYTE_ORDER != _LITTLE_ENDIAN &&
+	    sizeof(SHA_LONG)==4 && ((size_t)in%4)==0)
 		{
 		const SHA_LONG *W=(const SHA_LONG *)data;
 
diff --git a/src/lib/libssl/src/crypto/sha/sha_locl.h b/src/lib/libssl/src/crypto/sha/sha_locl.h
index 6c6cd64282..1210176dda 100644
--- a/src/lib/libssl/src/crypto/sha/sha_locl.h
+++ b/src/lib/libssl/src/crypto/sha/sha_locl.h
@@ -202,6 +202,7 @@ fips_md_init_ctx(SHA1, SHA)
 #endif
 
 #if !defined(SHA_1) || !defined(SHA1_ASM)
+#include <machine/endian.h>
 static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
 	{
 	const unsigned char *data=p;
@@ -221,9 +222,9 @@ static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
 
 	for (;;)
 			{
-	const union { long one; char little; } is_endian = {1};
 
-	if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)p%4)==0)
+	if (_BYTE_ORDER != _LITTLE_ENDIAN &&
+	    sizeof(SHA_LONG)==4 && ((size_t)p%4)==0)
 		{
 		const SHA_LONG *W=(const SHA_LONG *)data;
 
diff --git a/src/lib/libssl/src/ssl/d1_pkt.c b/src/lib/libssl/src/ssl/d1_pkt.c
index 80a4c076bf..7cfada4e6b 100644
--- a/src/lib/libssl/src/ssl/d1_pkt.c
+++ b/src/lib/libssl/src/ssl/d1_pkt.c
@@ -115,6 +115,7 @@
 
 #include <stdio.h>
 #include <errno.h>
+#include <machine/endian.h>
 #include "ssl_locl.h"
 #include <openssl/evp.h>
 #include <openssl/buffer.h>
@@ -129,13 +130,9 @@ satsub64be(const unsigned char *v1, const unsigned char *v2)
 
 	if (sizeof(long) == 8)
 		do {
-			const union {
-				long one;
-				char little;
-			} is_endian = {1};
 			long l;
 
-			if (is_endian.little)
+			if (_BYTE_ORDER == _LITTLE_ENDIAN)
 				break;
 			/* not reached on little-endians */
 			/* following test is redundant, because input is
-- 
cgit v1.2.3-55-g6feb