summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormiod <>2014-04-23 18:40:39 +0000
committermiod <>2014-04-23 18:40:39 +0000
commit7f5d17891e05458836147cd2b05889fde1e7be19 (patch)
treee1e4b99a334105ef85f59fe57d8b382b9d71b773
parent909fa81274d3ab37a13fd6a376b38c3652c1ec4f (diff)
downloadopenbsd-7f5d17891e05458836147cd2b05889fde1e7be19.tar.gz
openbsd-7f5d17891e05458836147cd2b05889fde1e7be19.tar.bz2
openbsd-7f5d17891e05458836147cd2b05889fde1e7be19.zip
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@
-rw-r--r--src/lib/libcrypto/evp/bio_ok.c6
-rw-r--r--src/lib/libcrypto/modes/ctr128.c7
-rw-r--r--src/lib/libcrypto/modes/gcm128.c75
-rw-r--r--src/lib/libcrypto/modes/modes_lcl.h1
-rw-r--r--src/lib/libcrypto/modes/xts128.c5
-rw-r--r--src/lib/libcrypto/rc4/rc4_enc.c10
-rw-r--r--src/lib/libcrypto/sha/sha256.c5
-rw-r--r--src/lib/libcrypto/sha/sha_locl.h5
-rw-r--r--src/lib/libssl/d1_pkt.c7
-rw-r--r--src/lib/libssl/src/crypto/evp/bio_ok.c6
-rw-r--r--src/lib/libssl/src/crypto/modes/ctr128.c7
-rw-r--r--src/lib/libssl/src/crypto/modes/gcm128.c75
-rw-r--r--src/lib/libssl/src/crypto/modes/modes_lcl.h1
-rw-r--r--src/lib/libssl/src/crypto/modes/xts128.c5
-rw-r--r--src/lib/libssl/src/crypto/rc4/rc4_enc.c10
-rw-r--r--src/lib/libssl/src/crypto/sha/sha256.c5
-rw-r--r--src/lib/libssl/src/crypto/sha/sha_locl.h5
-rw-r--r--src/lib/libssl/src/ssl/d1_pkt.c7
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 @@
120#include <stdio.h> 120#include <stdio.h>
121#include <errno.h> 121#include <errno.h>
122#include <assert.h> 122#include <assert.h>
123#include <machine/endian.h>
123#include "cryptlib.h" 124#include "cryptlib.h"
124#include <openssl/buffer.h> 125#include <openssl/buffer.h>
125#include <openssl/bio.h> 126#include <openssl/bio.h>
@@ -463,9 +464,8 @@ static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
463 } 464 }
464 465
465static void longswap(void *_ptr, size_t len) 466static void longswap(void *_ptr, size_t len)
466{ const union { long one; char little; } is_endian = {1}; 467{
467 468 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
468 if (is_endian.little) {
469 size_t i; 469 size_t i;
470 unsigned char *p=_ptr,c; 470 unsigned char *p=_ptr,c;
471 471
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) {
77} 77}
78 78
79#if !defined(OPENSSL_SMALL_FOOTPRINT) 79#if !defined(OPENSSL_SMALL_FOOTPRINT)
80static void ctr128_inc_aligned(unsigned char *counter) { 80static void
81ctr128_inc_aligned(unsigned char *counter)
82{
81 size_t *data,c,n; 83 size_t *data,c,n;
82 const union { long one; char little; } is_endian = {1};
83 84
84 if (is_endian.little) { 85 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
85 ctr128_inc(counter); 86 ctr128_inc(counter);
86 return; 87 return;
87 } 88 }
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])
147 u128 Z = { 0, 0}; 147 u128 Z = { 0, 0};
148 const u8 *xi = (const u8 *)Xi+15; 148 const u8 *xi = (const u8 *)Xi+15;
149 size_t rem, n = *xi; 149 size_t rem, n = *xi;
150 const union { long one; char little; } is_endian = {1};
151 static const size_t rem_8bit[256] = { 150 static const size_t rem_8bit[256] = {
152 PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246), 151 PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
153 PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E), 152 PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
@@ -231,7 +230,7 @@ static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
231 Z.hi ^= (u64)rem_8bit[rem]<<32; 230 Z.hi ^= (u64)rem_8bit[rem]<<32;
232 } 231 }
233 232
234 if (is_endian.little) { 233 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
235#ifdef BSWAP8 234#ifdef BSWAP8
236 Xi[0] = BSWAP8(Z.hi); 235 Xi[0] = BSWAP8(Z.hi);
237 Xi[1] = BSWAP8(Z.lo); 236 Xi[1] = BSWAP8(Z.lo);
@@ -307,9 +306,8 @@ static void gcm_init_4bit(u128 Htable[16], u64 H[2])
307 */ 306 */
308 { 307 {
309 int j; 308 int j;
310 const union { long one; char little; } is_endian = {1};
311 309
312 if (is_endian.little) 310 if (_BYTE_ORDER == _LITTLE_ENDIAN)
313 for (j=0;j<16;++j) { 311 for (j=0;j<16;++j) {
314 V = Htable[j]; 312 V = Htable[j];
315 Htable[j].hi = V.lo; 313 Htable[j].hi = V.lo;
@@ -337,7 +335,6 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
337 u128 Z; 335 u128 Z;
338 int cnt = 15; 336 int cnt = 15;
339 size_t rem, nlo, nhi; 337 size_t rem, nlo, nhi;
340 const union { long one; char little; } is_endian = {1};
341 338
342 nlo = ((const u8 *)Xi)[15]; 339 nlo = ((const u8 *)Xi)[15];
343 nhi = nlo>>4; 340 nhi = nlo>>4;
@@ -376,7 +373,7 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
376 Z.lo ^= Htable[nlo].lo; 373 Z.lo ^= Htable[nlo].lo;
377 } 374 }
378 375
379 if (is_endian.little) { 376 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
380#ifdef BSWAP8 377#ifdef BSWAP8
381 Xi[0] = BSWAP8(Z.hi); 378 Xi[0] = BSWAP8(Z.hi);
382 Xi[1] = BSWAP8(Z.lo); 379 Xi[1] = BSWAP8(Z.lo);
@@ -409,7 +406,6 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
409 u128 Z; 406 u128 Z;
410 int cnt; 407 int cnt;
411 size_t rem, nlo, nhi; 408 size_t rem, nlo, nhi;
412 const union { long one; char little; } is_endian = {1};
413 409
414#if 1 410#if 1
415 do { 411 do {
@@ -546,7 +542,7 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
546 Z.hi ^= ((u64)rem_8bit[rem<<4])<<48; 542 Z.hi ^= ((u64)rem_8bit[rem<<4])<<48;
547#endif 543#endif
548 544
549 if (is_endian.little) { 545 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
550#ifdef BSWAP8 546#ifdef BSWAP8
551 Xi[0] = BSWAP8(Z.hi); 547 Xi[0] = BSWAP8(Z.hi);
552 Xi[1] = BSWAP8(Z.lo); 548 Xi[1] = BSWAP8(Z.lo);
@@ -588,13 +584,12 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
588 long X; 584 long X;
589 int i,j; 585 int i,j;
590 const long *xi = (const long *)Xi; 586 const long *xi = (const long *)Xi;
591 const union { long one; char little; } is_endian = {1};
592 587
593 V.hi = H[0]; /* H is in host byte order, no byte swapping */ 588 V.hi = H[0]; /* H is in host byte order, no byte swapping */
594 V.lo = H[1]; 589 V.lo = H[1];
595 590
596 for (j=0; j<16/sizeof(long); ++j) { 591 for (j=0; j<16/sizeof(long); ++j) {
597 if (is_endian.little) { 592 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
598 if (sizeof(long)==8) { 593 if (sizeof(long)==8) {
599#ifdef BSWAP8 594#ifdef BSWAP8
600 X = (long)(BSWAP8(xi[j])); 595 X = (long)(BSWAP8(xi[j]));
@@ -620,7 +615,7 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
620 } 615 }
621 } 616 }
622 617
623 if (is_endian.little) { 618 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
624#ifdef BSWAP8 619#ifdef BSWAP8
625 Xi[0] = BSWAP8(Z.hi); 620 Xi[0] = BSWAP8(Z.hi);
626 Xi[1] = BSWAP8(Z.lo); 621 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);
685 680
686void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block) 681void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block)
687{ 682{
688 const union { long one; char little; } is_endian = {1};
689
690 memset(ctx,0,sizeof(*ctx)); 683 memset(ctx,0,sizeof(*ctx));
691 ctx->block = block; 684 ctx->block = block;
692 ctx->key = key; 685 ctx->key = key;
693 686
694 (*block)(ctx->H.c,ctx->H.c,key); 687 (*block)(ctx->H.c,ctx->H.c,key);
695 688
696 if (is_endian.little) { 689 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
697 /* H is stored in host byte order */ 690 /* H is stored in host byte order */
698#ifdef BSWAP8 691#ifdef BSWAP8
699 ctx->H.u[0] = BSWAP8(ctx->H.u[0]); 692 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)
755 748
756void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len) 749void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
757{ 750{
758 const union { long one; char little; } is_endian = {1};
759 unsigned int ctr; 751 unsigned int ctr;
760#ifdef GCM_FUNCREF_4BIT 752#ifdef GCM_FUNCREF_4BIT
761 void (*gcm_gmult_p)(u64 Xi[2],const u128 Htable[16]) = ctx->gmult; 753 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)
790 GCM_MUL(ctx,Yi); 782 GCM_MUL(ctx,Yi);
791 } 783 }
792 len0 <<= 3; 784 len0 <<= 3;
793 if (is_endian.little) { 785 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
794#ifdef BSWAP8 786#ifdef BSWAP8
795 ctx->Yi.u[1] ^= BSWAP8(len0); 787 ctx->Yi.u[1] ^= BSWAP8(len0);
796#else 788#else
@@ -809,7 +801,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
809 801
810 GCM_MUL(ctx,Yi); 802 GCM_MUL(ctx,Yi);
811 803
812 if (is_endian.little) 804 if (_BYTE_ORDER == _LITTLE_ENDIAN)
813#ifdef BSWAP4 805#ifdef BSWAP4
814 ctr = BSWAP4(ctx->Yi.d[3]); 806 ctr = BSWAP4(ctx->Yi.d[3]);
815#else 807#else
@@ -821,7 +813,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
821 813
822 (*ctx->block)(ctx->Yi.c,ctx->EK0.c,ctx->key); 814 (*ctx->block)(ctx->Yi.c,ctx->EK0.c,ctx->key);
823 ++ctr; 815 ++ctr;
824 if (is_endian.little) 816 if (_BYTE_ORDER == _LITTLE_ENDIAN)
825#ifdef BSWAP4 817#ifdef BSWAP4
826 ctx->Yi.d[3] = BSWAP4(ctr); 818 ctx->Yi.d[3] = BSWAP4(ctr);
827#else 819#else
@@ -892,7 +884,6 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
892 const unsigned char *in, unsigned char *out, 884 const unsigned char *in, unsigned char *out,
893 size_t len) 885 size_t len)
894{ 886{
895 const union { long one; char little; } is_endian = {1};
896 unsigned int n, ctr; 887 unsigned int n, ctr;
897 size_t i; 888 size_t i;
898 u64 mlen = ctx->len.u[1]; 889 u64 mlen = ctx->len.u[1];
@@ -920,7 +911,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
920 ctx->ares = 0; 911 ctx->ares = 0;
921 } 912 }
922 913
923 if (is_endian.little) 914 if (_BYTE_ORDER == _LITTLE_ENDIAN)
924#ifdef BSWAP4 915#ifdef BSWAP4
925 ctr = BSWAP4(ctx->Yi.d[3]); 916 ctr = BSWAP4(ctx->Yi.d[3]);
926#else 917#else
@@ -958,7 +949,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
958 949
959 (*block)(ctx->Yi.c,ctx->EKi.c,key); 950 (*block)(ctx->Yi.c,ctx->EKi.c,key);
960 ++ctr; 951 ++ctr;
961 if (is_endian.little) 952 if (_BYTE_ORDER == _LITTLE_ENDIAN)
962#ifdef BSWAP4 953#ifdef BSWAP4
963 ctx->Yi.d[3] = BSWAP4(ctr); 954 ctx->Yi.d[3] = BSWAP4(ctr);
964#else 955#else
@@ -984,7 +975,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
984 975
985 (*block)(ctx->Yi.c,ctx->EKi.c,key); 976 (*block)(ctx->Yi.c,ctx->EKi.c,key);
986 ++ctr; 977 ++ctr;
987 if (is_endian.little) 978 if (_BYTE_ORDER == _LITTLE_ENDIAN)
988#ifdef BSWAP4 979#ifdef BSWAP4
989 ctx->Yi.d[3] = BSWAP4(ctr); 980 ctx->Yi.d[3] = BSWAP4(ctr);
990#else 981#else
@@ -1007,7 +998,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
1007 998
1008 (*block)(ctx->Yi.c,ctx->EKi.c,key); 999 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1009 ++ctr; 1000 ++ctr;
1010 if (is_endian.little) 1001 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1011#ifdef BSWAP4 1002#ifdef BSWAP4
1012 ctx->Yi.d[3] = BSWAP4(ctr); 1003 ctx->Yi.d[3] = BSWAP4(ctr);
1013#else 1004#else
@@ -1027,7 +1018,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
1027 if (len) { 1018 if (len) {
1028 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1019 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1029 ++ctr; 1020 ++ctr;
1030 if (is_endian.little) 1021 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1031#ifdef BSWAP4 1022#ifdef BSWAP4
1032 ctx->Yi.d[3] = BSWAP4(ctr); 1023 ctx->Yi.d[3] = BSWAP4(ctr);
1033#else 1024#else
@@ -1049,7 +1040,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
1049 if (n==0) { 1040 if (n==0) {
1050 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1041 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1051 ++ctr; 1042 ++ctr;
1052 if (is_endian.little) 1043 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1053#ifdef BSWAP4 1044#ifdef BSWAP4
1054 ctx->Yi.d[3] = BSWAP4(ctr); 1045 ctx->Yi.d[3] = BSWAP4(ctr);
1055#else 1046#else
@@ -1072,7 +1063,6 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1072 const unsigned char *in, unsigned char *out, 1063 const unsigned char *in, unsigned char *out,
1073 size_t len) 1064 size_t len)
1074{ 1065{
1075 const union { long one; char little; } is_endian = {1};
1076 unsigned int n, ctr; 1066 unsigned int n, ctr;
1077 size_t i; 1067 size_t i;
1078 u64 mlen = ctx->len.u[1]; 1068 u64 mlen = ctx->len.u[1];
@@ -1097,7 +1087,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1097 ctx->ares = 0; 1087 ctx->ares = 0;
1098 } 1088 }
1099 1089
1100 if (is_endian.little) 1090 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1101#ifdef BSWAP4 1091#ifdef BSWAP4
1102 ctr = BSWAP4(ctx->Yi.d[3]); 1092 ctr = BSWAP4(ctx->Yi.d[3]);
1103#else 1093#else
@@ -1138,7 +1128,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1138 1128
1139 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1129 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1140 ++ctr; 1130 ++ctr;
1141 if (is_endian.little) 1131 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1142#ifdef BSWAP4 1132#ifdef BSWAP4
1143 ctx->Yi.d[3] = BSWAP4(ctr); 1133 ctx->Yi.d[3] = BSWAP4(ctr);
1144#else 1134#else
@@ -1162,7 +1152,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1162 1152
1163 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1153 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1164 ++ctr; 1154 ++ctr;
1165 if (is_endian.little) 1155 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1166#ifdef BSWAP4 1156#ifdef BSWAP4
1167 ctx->Yi.d[3] = BSWAP4(ctr); 1157 ctx->Yi.d[3] = BSWAP4(ctr);
1168#else 1158#else
@@ -1184,7 +1174,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1184 1174
1185 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1175 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1186 ++ctr; 1176 ++ctr;
1187 if (is_endian.little) 1177 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1188#ifdef BSWAP4 1178#ifdef BSWAP4
1189 ctx->Yi.d[3] = BSWAP4(ctr); 1179 ctx->Yi.d[3] = BSWAP4(ctr);
1190#else 1180#else
@@ -1206,7 +1196,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1206 if (len) { 1196 if (len) {
1207 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1197 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1208 ++ctr; 1198 ++ctr;
1209 if (is_endian.little) 1199 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1210#ifdef BSWAP4 1200#ifdef BSWAP4
1211 ctx->Yi.d[3] = BSWAP4(ctr); 1201 ctx->Yi.d[3] = BSWAP4(ctr);
1212#else 1202#else
@@ -1231,7 +1221,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1231 if (n==0) { 1221 if (n==0) {
1232 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1222 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1233 ++ctr; 1223 ++ctr;
1234 if (is_endian.little) 1224 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1235#ifdef BSWAP4 1225#ifdef BSWAP4
1236 ctx->Yi.d[3] = BSWAP4(ctr); 1226 ctx->Yi.d[3] = BSWAP4(ctr);
1237#else 1227#else
@@ -1256,7 +1246,6 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1256 const unsigned char *in, unsigned char *out, 1246 const unsigned char *in, unsigned char *out,
1257 size_t len, ctr128_f stream) 1247 size_t len, ctr128_f stream)
1258{ 1248{
1259 const union { long one; char little; } is_endian = {1};
1260 unsigned int n, ctr; 1249 unsigned int n, ctr;
1261 size_t i; 1250 size_t i;
1262 u64 mlen = ctx->len.u[1]; 1251 u64 mlen = ctx->len.u[1];
@@ -1280,7 +1269,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1280 ctx->ares = 0; 1269 ctx->ares = 0;
1281 } 1270 }
1282 1271
1283 if (is_endian.little) 1272 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1284#ifdef BSWAP4 1273#ifdef BSWAP4
1285 ctr = BSWAP4(ctx->Yi.d[3]); 1274 ctr = BSWAP4(ctx->Yi.d[3]);
1286#else 1275#else
@@ -1306,7 +1295,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1306 while (len>=GHASH_CHUNK) { 1295 while (len>=GHASH_CHUNK) {
1307 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c); 1296 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
1308 ctr += GHASH_CHUNK/16; 1297 ctr += GHASH_CHUNK/16;
1309 if (is_endian.little) 1298 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1310#ifdef BSWAP4 1299#ifdef BSWAP4
1311 ctx->Yi.d[3] = BSWAP4(ctr); 1300 ctx->Yi.d[3] = BSWAP4(ctr);
1312#else 1301#else
@@ -1325,7 +1314,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1325 1314
1326 (*stream)(in,out,j,key,ctx->Yi.c); 1315 (*stream)(in,out,j,key,ctx->Yi.c);
1327 ctr += (unsigned int)j; 1316 ctr += (unsigned int)j;
1328 if (is_endian.little) 1317 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1329#ifdef BSWAP4 1318#ifdef BSWAP4
1330 ctx->Yi.d[3] = BSWAP4(ctr); 1319 ctx->Yi.d[3] = BSWAP4(ctr);
1331#else 1320#else
@@ -1349,7 +1338,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1349 if (len) { 1338 if (len) {
1350 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key); 1339 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
1351 ++ctr; 1340 ++ctr;
1352 if (is_endian.little) 1341 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1353#ifdef BSWAP4 1342#ifdef BSWAP4
1354 ctx->Yi.d[3] = BSWAP4(ctr); 1343 ctx->Yi.d[3] = BSWAP4(ctr);
1355#else 1344#else
@@ -1371,7 +1360,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1371 const unsigned char *in, unsigned char *out, 1360 const unsigned char *in, unsigned char *out,
1372 size_t len,ctr128_f stream) 1361 size_t len,ctr128_f stream)
1373{ 1362{
1374 const union { long one; char little; } is_endian = {1};
1375 unsigned int n, ctr; 1363 unsigned int n, ctr;
1376 size_t i; 1364 size_t i;
1377 u64 mlen = ctx->len.u[1]; 1365 u64 mlen = ctx->len.u[1];
@@ -1395,7 +1383,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1395 ctx->ares = 0; 1383 ctx->ares = 0;
1396 } 1384 }
1397 1385
1398 if (is_endian.little) 1386 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1399#ifdef BSWAP4 1387#ifdef BSWAP4
1400 ctr = BSWAP4(ctx->Yi.d[3]); 1388 ctr = BSWAP4(ctx->Yi.d[3]);
1401#else 1389#else
@@ -1424,7 +1412,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1424 GHASH(ctx,in,GHASH_CHUNK); 1412 GHASH(ctx,in,GHASH_CHUNK);
1425 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c); 1413 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
1426 ctr += GHASH_CHUNK/16; 1414 ctr += GHASH_CHUNK/16;
1427 if (is_endian.little) 1415 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1428#ifdef BSWAP4 1416#ifdef BSWAP4
1429 ctx->Yi.d[3] = BSWAP4(ctr); 1417 ctx->Yi.d[3] = BSWAP4(ctr);
1430#else 1418#else
@@ -1454,7 +1442,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1454#endif 1442#endif
1455 (*stream)(in,out,j,key,ctx->Yi.c); 1443 (*stream)(in,out,j,key,ctx->Yi.c);
1456 ctr += (unsigned int)j; 1444 ctr += (unsigned int)j;
1457 if (is_endian.little) 1445 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1458#ifdef BSWAP4 1446#ifdef BSWAP4
1459 ctx->Yi.d[3] = BSWAP4(ctr); 1447 ctx->Yi.d[3] = BSWAP4(ctr);
1460#else 1448#else
@@ -1469,7 +1457,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1469 if (len) { 1457 if (len) {
1470 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key); 1458 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
1471 ++ctr; 1459 ++ctr;
1472 if (is_endian.little) 1460 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1473#ifdef BSWAP4 1461#ifdef BSWAP4
1474 ctx->Yi.d[3] = BSWAP4(ctr); 1462 ctx->Yi.d[3] = BSWAP4(ctr);
1475#else 1463#else
@@ -1492,7 +1480,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1492int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag, 1480int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
1493 size_t len) 1481 size_t len)
1494{ 1482{
1495 const union { long one; char little; } is_endian = {1};
1496 u64 alen = ctx->len.u[0]<<3; 1483 u64 alen = ctx->len.u[0]<<3;
1497 u64 clen = ctx->len.u[1]<<3; 1484 u64 clen = ctx->len.u[1]<<3;
1498#ifdef GCM_FUNCREF_4BIT 1485#ifdef GCM_FUNCREF_4BIT
@@ -1502,7 +1489,7 @@ int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
1502 if (ctx->mres || ctx->ares) 1489 if (ctx->mres || ctx->ares)
1503 GCM_MUL(ctx,Xi); 1490 GCM_MUL(ctx,Xi);
1504 1491
1505 if (is_endian.little) { 1492 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
1506#ifdef BSWAP8 1493#ifdef BSWAP8
1507 alen = BSWAP8(alen); 1494 alen = BSWAP8(alen);
1508 clen = BSWAP8(clen); 1495 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 @@
6 */ 6 */
7 7
8#include <openssl/modes.h> 8#include <openssl/modes.h>
9#include <machine/endian.h>
9 10
10 11
11#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) 12#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],
62 const unsigned char *inp, unsigned char *out, 62 const unsigned char *inp, unsigned char *out,
63 size_t len, int enc) 63 size_t len, int enc)
64{ 64{
65 const union { long one; char little; } is_endian = {1};
66 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch; 65 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch;
67 unsigned int i; 66 unsigned int i;
68 67
@@ -98,7 +97,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
98 97
99 if (len==0) return 0; 98 if (len==0) return 0;
100 99
101 if (is_endian.little) { 100 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
102 unsigned int carry,res; 101 unsigned int carry,res;
103 102
104 res = 0x87&(((int)tweak.d[3])>>31); 103 res = 0x87&(((int)tweak.d[3])>>31);
@@ -134,7 +133,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
134 else { 133 else {
135 union { u64 u[2]; u8 c[16]; } tweak1; 134 union { u64 u[2]; u8 c[16]; } tweak1;
136 135
137 if (is_endian.little) { 136 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
138 unsigned int carry,res; 137 unsigned int carry,res;
139 138
140 res = 0x87&(((int)tweak.d[3])>>31); 139 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 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#include <machine/endian.h>
59#include <openssl/rc4.h> 60#include <openssl/rc4.h>
60#include "rc4_locl.h" 61#include "rc4_locl.h"
61 62
@@ -124,7 +125,6 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
124 ((size_t)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 ) 125 ((size_t)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 )
125 { 126 {
126 RC4_CHUNK ichunk,otp; 127 RC4_CHUNK ichunk,otp;
127 const union { long one; char little; } is_endian = {1};
128 128
129 /* 129 /*
130 * I reckon we can afford to implement both endian 130 * 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,
132 * because the machine code appears to be very compact 132 * because the machine code appears to be very compact
133 * and redundant 1-2KB is perfectly tolerable (i.e. 133 * and redundant 1-2KB is perfectly tolerable (i.e.
134 * in case the compiler fails to eliminate it:-). By 134 * in case the compiler fails to eliminate it:-). By
135 * suggestion from Terrel Larson <terr@terralogic.net> 135 * suggestion from Terrel Larson <terr@terralogic.net>.
136 * who also stands for the is_endian union:-)
137 * 136 *
138 * Special notes. 137 * Special notes.
139 * 138 *
140 * - is_endian is declared automatic as doing otherwise
141 * (declaring static) prevents gcc from eliminating
142 * the redundant code;
143 * - compilers (those I've tried) don't seem to have 139 * - compilers (those I've tried) don't seem to have
144 * problems eliminating either the operators guarded 140 * problems eliminating either the operators guarded
145 * by "if (sizeof(RC4_CHUNK)==8)" or the condition 141 * 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,
154 * 150 *
155 * <appro@fy.chalmers.se> 151 * <appro@fy.chalmers.se>
156 */ 152 */
157 if (!is_endian.little) 153 if (_BYTE_ORDER != _LITTLE_ENDIAN)
158 { /* BIG-ENDIAN CASE */ 154 { /* BIG-ENDIAN CASE */
159# define BESHFT(c) (((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1)) 155# define BESHFT(c) (((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1))
160 for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK)) 156 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 @@
9 9
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12#include <machine/endian.h>
12 13
13#include <openssl/crypto.h> 14#include <openssl/crypto.h>
14#include <openssl/sha.h> 15#include <openssl/sha.h>
@@ -206,14 +207,14 @@ static void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num
206 SHA_LONG X[16]; 207 SHA_LONG X[16];
207 int i; 208 int i;
208 const unsigned char *data=in; 209 const unsigned char *data=in;
209 const union { long one; char little; } is_endian = {1};
210 210
211 while (num--) { 211 while (num--) {
212 212
213 a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3]; 213 a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3];
214 e = ctx->h[4]; f = ctx->h[5]; g = ctx->h[6]; h = ctx->h[7]; 214 e = ctx->h[4]; f = ctx->h[5]; g = ctx->h[6]; h = ctx->h[7];
215 215
216 if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)in%4)==0) 216 if (_BYTE_ORDER != _LITTLE_ENDIAN &&
217 sizeof(SHA_LONG)==4 && ((size_t)in%4)==0)
217 { 218 {
218 const SHA_LONG *W=(const SHA_LONG *)data; 219 const SHA_LONG *W=(const SHA_LONG *)data;
219 220
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)
202#endif 202#endif
203 203
204#if !defined(SHA_1) || !defined(SHA1_ASM) 204#if !defined(SHA_1) || !defined(SHA1_ASM)
205#include <machine/endian.h>
205static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num) 206static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
206 { 207 {
207 const unsigned char *data=p; 208 const unsigned char *data=p;
@@ -221,9 +222,9 @@ static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
221 222
222 for (;;) 223 for (;;)
223 { 224 {
224 const union { long one; char little; } is_endian = {1};
225 225
226 if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)p%4)==0) 226 if (_BYTE_ORDER != _LITTLE_ENDIAN &&
227 sizeof(SHA_LONG)==4 && ((size_t)p%4)==0)
227 { 228 {
228 const SHA_LONG *W=(const SHA_LONG *)data; 229 const SHA_LONG *W=(const SHA_LONG *)data;
229 230
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 @@
115 115
116#include <stdio.h> 116#include <stdio.h>
117#include <errno.h> 117#include <errno.h>
118#include <machine/endian.h>
118#include "ssl_locl.h" 119#include "ssl_locl.h"
119#include <openssl/evp.h> 120#include <openssl/evp.h>
120#include <openssl/buffer.h> 121#include <openssl/buffer.h>
@@ -129,13 +130,9 @@ satsub64be(const unsigned char *v1, const unsigned char *v2)
129 130
130 if (sizeof(long) == 8) 131 if (sizeof(long) == 8)
131 do { 132 do {
132 const union {
133 long one;
134 char little;
135 } is_endian = {1};
136 long l; 133 long l;
137 134
138 if (is_endian.little) 135 if (_BYTE_ORDER == _LITTLE_ENDIAN)
139 break; 136 break;
140 /* not reached on little-endians */ 137 /* not reached on little-endians */
141 /* following test is redundant, because input is 138 /* 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 @@
120#include <stdio.h> 120#include <stdio.h>
121#include <errno.h> 121#include <errno.h>
122#include <assert.h> 122#include <assert.h>
123#include <machine/endian.h>
123#include "cryptlib.h" 124#include "cryptlib.h"
124#include <openssl/buffer.h> 125#include <openssl/buffer.h>
125#include <openssl/bio.h> 126#include <openssl/bio.h>
@@ -463,9 +464,8 @@ static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
463 } 464 }
464 465
465static void longswap(void *_ptr, size_t len) 466static void longswap(void *_ptr, size_t len)
466{ const union { long one; char little; } is_endian = {1}; 467{
467 468 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
468 if (is_endian.little) {
469 size_t i; 469 size_t i;
470 unsigned char *p=_ptr,c; 470 unsigned char *p=_ptr,c;
471 471
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) {
77} 77}
78 78
79#if !defined(OPENSSL_SMALL_FOOTPRINT) 79#if !defined(OPENSSL_SMALL_FOOTPRINT)
80static void ctr128_inc_aligned(unsigned char *counter) { 80static void
81ctr128_inc_aligned(unsigned char *counter)
82{
81 size_t *data,c,n; 83 size_t *data,c,n;
82 const union { long one; char little; } is_endian = {1};
83 84
84 if (is_endian.little) { 85 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
85 ctr128_inc(counter); 86 ctr128_inc(counter);
86 return; 87 return;
87 } 88 }
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])
147 u128 Z = { 0, 0}; 147 u128 Z = { 0, 0};
148 const u8 *xi = (const u8 *)Xi+15; 148 const u8 *xi = (const u8 *)Xi+15;
149 size_t rem, n = *xi; 149 size_t rem, n = *xi;
150 const union { long one; char little; } is_endian = {1};
151 static const size_t rem_8bit[256] = { 150 static const size_t rem_8bit[256] = {
152 PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246), 151 PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
153 PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E), 152 PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
@@ -231,7 +230,7 @@ static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
231 Z.hi ^= (u64)rem_8bit[rem]<<32; 230 Z.hi ^= (u64)rem_8bit[rem]<<32;
232 } 231 }
233 232
234 if (is_endian.little) { 233 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
235#ifdef BSWAP8 234#ifdef BSWAP8
236 Xi[0] = BSWAP8(Z.hi); 235 Xi[0] = BSWAP8(Z.hi);
237 Xi[1] = BSWAP8(Z.lo); 236 Xi[1] = BSWAP8(Z.lo);
@@ -307,9 +306,8 @@ static void gcm_init_4bit(u128 Htable[16], u64 H[2])
307 */ 306 */
308 { 307 {
309 int j; 308 int j;
310 const union { long one; char little; } is_endian = {1};
311 309
312 if (is_endian.little) 310 if (_BYTE_ORDER == _LITTLE_ENDIAN)
313 for (j=0;j<16;++j) { 311 for (j=0;j<16;++j) {
314 V = Htable[j]; 312 V = Htable[j];
315 Htable[j].hi = V.lo; 313 Htable[j].hi = V.lo;
@@ -337,7 +335,6 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
337 u128 Z; 335 u128 Z;
338 int cnt = 15; 336 int cnt = 15;
339 size_t rem, nlo, nhi; 337 size_t rem, nlo, nhi;
340 const union { long one; char little; } is_endian = {1};
341 338
342 nlo = ((const u8 *)Xi)[15]; 339 nlo = ((const u8 *)Xi)[15];
343 nhi = nlo>>4; 340 nhi = nlo>>4;
@@ -376,7 +373,7 @@ static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
376 Z.lo ^= Htable[nlo].lo; 373 Z.lo ^= Htable[nlo].lo;
377 } 374 }
378 375
379 if (is_endian.little) { 376 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
380#ifdef BSWAP8 377#ifdef BSWAP8
381 Xi[0] = BSWAP8(Z.hi); 378 Xi[0] = BSWAP8(Z.hi);
382 Xi[1] = BSWAP8(Z.lo); 379 Xi[1] = BSWAP8(Z.lo);
@@ -409,7 +406,6 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
409 u128 Z; 406 u128 Z;
410 int cnt; 407 int cnt;
411 size_t rem, nlo, nhi; 408 size_t rem, nlo, nhi;
412 const union { long one; char little; } is_endian = {1};
413 409
414#if 1 410#if 1
415 do { 411 do {
@@ -546,7 +542,7 @@ static void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],
546 Z.hi ^= ((u64)rem_8bit[rem<<4])<<48; 542 Z.hi ^= ((u64)rem_8bit[rem<<4])<<48;
547#endif 543#endif
548 544
549 if (is_endian.little) { 545 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
550#ifdef BSWAP8 546#ifdef BSWAP8
551 Xi[0] = BSWAP8(Z.hi); 547 Xi[0] = BSWAP8(Z.hi);
552 Xi[1] = BSWAP8(Z.lo); 548 Xi[1] = BSWAP8(Z.lo);
@@ -588,13 +584,12 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
588 long X; 584 long X;
589 int i,j; 585 int i,j;
590 const long *xi = (const long *)Xi; 586 const long *xi = (const long *)Xi;
591 const union { long one; char little; } is_endian = {1};
592 587
593 V.hi = H[0]; /* H is in host byte order, no byte swapping */ 588 V.hi = H[0]; /* H is in host byte order, no byte swapping */
594 V.lo = H[1]; 589 V.lo = H[1];
595 590
596 for (j=0; j<16/sizeof(long); ++j) { 591 for (j=0; j<16/sizeof(long); ++j) {
597 if (is_endian.little) { 592 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
598 if (sizeof(long)==8) { 593 if (sizeof(long)==8) {
599#ifdef BSWAP8 594#ifdef BSWAP8
600 X = (long)(BSWAP8(xi[j])); 595 X = (long)(BSWAP8(xi[j]));
@@ -620,7 +615,7 @@ static void gcm_gmult_1bit(u64 Xi[2],const u64 H[2])
620 } 615 }
621 } 616 }
622 617
623 if (is_endian.little) { 618 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
624#ifdef BSWAP8 619#ifdef BSWAP8
625 Xi[0] = BSWAP8(Z.hi); 620 Xi[0] = BSWAP8(Z.hi);
626 Xi[1] = BSWAP8(Z.lo); 621 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);
685 680
686void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block) 681void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx,void *key,block128_f block)
687{ 682{
688 const union { long one; char little; } is_endian = {1};
689
690 memset(ctx,0,sizeof(*ctx)); 683 memset(ctx,0,sizeof(*ctx));
691 ctx->block = block; 684 ctx->block = block;
692 ctx->key = key; 685 ctx->key = key;
693 686
694 (*block)(ctx->H.c,ctx->H.c,key); 687 (*block)(ctx->H.c,ctx->H.c,key);
695 688
696 if (is_endian.little) { 689 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
697 /* H is stored in host byte order */ 690 /* H is stored in host byte order */
698#ifdef BSWAP8 691#ifdef BSWAP8
699 ctx->H.u[0] = BSWAP8(ctx->H.u[0]); 692 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)
755 748
756void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len) 749void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
757{ 750{
758 const union { long one; char little; } is_endian = {1};
759 unsigned int ctr; 751 unsigned int ctr;
760#ifdef GCM_FUNCREF_4BIT 752#ifdef GCM_FUNCREF_4BIT
761 void (*gcm_gmult_p)(u64 Xi[2],const u128 Htable[16]) = ctx->gmult; 753 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)
790 GCM_MUL(ctx,Yi); 782 GCM_MUL(ctx,Yi);
791 } 783 }
792 len0 <<= 3; 784 len0 <<= 3;
793 if (is_endian.little) { 785 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
794#ifdef BSWAP8 786#ifdef BSWAP8
795 ctx->Yi.u[1] ^= BSWAP8(len0); 787 ctx->Yi.u[1] ^= BSWAP8(len0);
796#else 788#else
@@ -809,7 +801,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
809 801
810 GCM_MUL(ctx,Yi); 802 GCM_MUL(ctx,Yi);
811 803
812 if (is_endian.little) 804 if (_BYTE_ORDER == _LITTLE_ENDIAN)
813#ifdef BSWAP4 805#ifdef BSWAP4
814 ctr = BSWAP4(ctx->Yi.d[3]); 806 ctr = BSWAP4(ctx->Yi.d[3]);
815#else 807#else
@@ -821,7 +813,7 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
821 813
822 (*ctx->block)(ctx->Yi.c,ctx->EK0.c,ctx->key); 814 (*ctx->block)(ctx->Yi.c,ctx->EK0.c,ctx->key);
823 ++ctr; 815 ++ctr;
824 if (is_endian.little) 816 if (_BYTE_ORDER == _LITTLE_ENDIAN)
825#ifdef BSWAP4 817#ifdef BSWAP4
826 ctx->Yi.d[3] = BSWAP4(ctr); 818 ctx->Yi.d[3] = BSWAP4(ctr);
827#else 819#else
@@ -892,7 +884,6 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
892 const unsigned char *in, unsigned char *out, 884 const unsigned char *in, unsigned char *out,
893 size_t len) 885 size_t len)
894{ 886{
895 const union { long one; char little; } is_endian = {1};
896 unsigned int n, ctr; 887 unsigned int n, ctr;
897 size_t i; 888 size_t i;
898 u64 mlen = ctx->len.u[1]; 889 u64 mlen = ctx->len.u[1];
@@ -920,7 +911,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
920 ctx->ares = 0; 911 ctx->ares = 0;
921 } 912 }
922 913
923 if (is_endian.little) 914 if (_BYTE_ORDER == _LITTLE_ENDIAN)
924#ifdef BSWAP4 915#ifdef BSWAP4
925 ctr = BSWAP4(ctx->Yi.d[3]); 916 ctr = BSWAP4(ctx->Yi.d[3]);
926#else 917#else
@@ -958,7 +949,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
958 949
959 (*block)(ctx->Yi.c,ctx->EKi.c,key); 950 (*block)(ctx->Yi.c,ctx->EKi.c,key);
960 ++ctr; 951 ++ctr;
961 if (is_endian.little) 952 if (_BYTE_ORDER == _LITTLE_ENDIAN)
962#ifdef BSWAP4 953#ifdef BSWAP4
963 ctx->Yi.d[3] = BSWAP4(ctr); 954 ctx->Yi.d[3] = BSWAP4(ctr);
964#else 955#else
@@ -984,7 +975,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
984 975
985 (*block)(ctx->Yi.c,ctx->EKi.c,key); 976 (*block)(ctx->Yi.c,ctx->EKi.c,key);
986 ++ctr; 977 ++ctr;
987 if (is_endian.little) 978 if (_BYTE_ORDER == _LITTLE_ENDIAN)
988#ifdef BSWAP4 979#ifdef BSWAP4
989 ctx->Yi.d[3] = BSWAP4(ctr); 980 ctx->Yi.d[3] = BSWAP4(ctr);
990#else 981#else
@@ -1007,7 +998,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
1007 998
1008 (*block)(ctx->Yi.c,ctx->EKi.c,key); 999 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1009 ++ctr; 1000 ++ctr;
1010 if (is_endian.little) 1001 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1011#ifdef BSWAP4 1002#ifdef BSWAP4
1012 ctx->Yi.d[3] = BSWAP4(ctr); 1003 ctx->Yi.d[3] = BSWAP4(ctr);
1013#else 1004#else
@@ -1027,7 +1018,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
1027 if (len) { 1018 if (len) {
1028 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1019 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1029 ++ctr; 1020 ++ctr;
1030 if (is_endian.little) 1021 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1031#ifdef BSWAP4 1022#ifdef BSWAP4
1032 ctx->Yi.d[3] = BSWAP4(ctr); 1023 ctx->Yi.d[3] = BSWAP4(ctr);
1033#else 1024#else
@@ -1049,7 +1040,7 @@ int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
1049 if (n==0) { 1040 if (n==0) {
1050 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1041 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1051 ++ctr; 1042 ++ctr;
1052 if (is_endian.little) 1043 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1053#ifdef BSWAP4 1044#ifdef BSWAP4
1054 ctx->Yi.d[3] = BSWAP4(ctr); 1045 ctx->Yi.d[3] = BSWAP4(ctr);
1055#else 1046#else
@@ -1072,7 +1063,6 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1072 const unsigned char *in, unsigned char *out, 1063 const unsigned char *in, unsigned char *out,
1073 size_t len) 1064 size_t len)
1074{ 1065{
1075 const union { long one; char little; } is_endian = {1};
1076 unsigned int n, ctr; 1066 unsigned int n, ctr;
1077 size_t i; 1067 size_t i;
1078 u64 mlen = ctx->len.u[1]; 1068 u64 mlen = ctx->len.u[1];
@@ -1097,7 +1087,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1097 ctx->ares = 0; 1087 ctx->ares = 0;
1098 } 1088 }
1099 1089
1100 if (is_endian.little) 1090 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1101#ifdef BSWAP4 1091#ifdef BSWAP4
1102 ctr = BSWAP4(ctx->Yi.d[3]); 1092 ctr = BSWAP4(ctx->Yi.d[3]);
1103#else 1093#else
@@ -1138,7 +1128,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1138 1128
1139 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1129 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1140 ++ctr; 1130 ++ctr;
1141 if (is_endian.little) 1131 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1142#ifdef BSWAP4 1132#ifdef BSWAP4
1143 ctx->Yi.d[3] = BSWAP4(ctr); 1133 ctx->Yi.d[3] = BSWAP4(ctr);
1144#else 1134#else
@@ -1162,7 +1152,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1162 1152
1163 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1153 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1164 ++ctr; 1154 ++ctr;
1165 if (is_endian.little) 1155 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1166#ifdef BSWAP4 1156#ifdef BSWAP4
1167 ctx->Yi.d[3] = BSWAP4(ctr); 1157 ctx->Yi.d[3] = BSWAP4(ctr);
1168#else 1158#else
@@ -1184,7 +1174,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1184 1174
1185 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1175 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1186 ++ctr; 1176 ++ctr;
1187 if (is_endian.little) 1177 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1188#ifdef BSWAP4 1178#ifdef BSWAP4
1189 ctx->Yi.d[3] = BSWAP4(ctr); 1179 ctx->Yi.d[3] = BSWAP4(ctr);
1190#else 1180#else
@@ -1206,7 +1196,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1206 if (len) { 1196 if (len) {
1207 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1197 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1208 ++ctr; 1198 ++ctr;
1209 if (is_endian.little) 1199 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1210#ifdef BSWAP4 1200#ifdef BSWAP4
1211 ctx->Yi.d[3] = BSWAP4(ctr); 1201 ctx->Yi.d[3] = BSWAP4(ctr);
1212#else 1202#else
@@ -1231,7 +1221,7 @@ int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1231 if (n==0) { 1221 if (n==0) {
1232 (*block)(ctx->Yi.c,ctx->EKi.c,key); 1222 (*block)(ctx->Yi.c,ctx->EKi.c,key);
1233 ++ctr; 1223 ++ctr;
1234 if (is_endian.little) 1224 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1235#ifdef BSWAP4 1225#ifdef BSWAP4
1236 ctx->Yi.d[3] = BSWAP4(ctr); 1226 ctx->Yi.d[3] = BSWAP4(ctr);
1237#else 1227#else
@@ -1256,7 +1246,6 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1256 const unsigned char *in, unsigned char *out, 1246 const unsigned char *in, unsigned char *out,
1257 size_t len, ctr128_f stream) 1247 size_t len, ctr128_f stream)
1258{ 1248{
1259 const union { long one; char little; } is_endian = {1};
1260 unsigned int n, ctr; 1249 unsigned int n, ctr;
1261 size_t i; 1250 size_t i;
1262 u64 mlen = ctx->len.u[1]; 1251 u64 mlen = ctx->len.u[1];
@@ -1280,7 +1269,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1280 ctx->ares = 0; 1269 ctx->ares = 0;
1281 } 1270 }
1282 1271
1283 if (is_endian.little) 1272 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1284#ifdef BSWAP4 1273#ifdef BSWAP4
1285 ctr = BSWAP4(ctx->Yi.d[3]); 1274 ctr = BSWAP4(ctx->Yi.d[3]);
1286#else 1275#else
@@ -1306,7 +1295,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1306 while (len>=GHASH_CHUNK) { 1295 while (len>=GHASH_CHUNK) {
1307 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c); 1296 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
1308 ctr += GHASH_CHUNK/16; 1297 ctr += GHASH_CHUNK/16;
1309 if (is_endian.little) 1298 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1310#ifdef BSWAP4 1299#ifdef BSWAP4
1311 ctx->Yi.d[3] = BSWAP4(ctr); 1300 ctx->Yi.d[3] = BSWAP4(ctr);
1312#else 1301#else
@@ -1325,7 +1314,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1325 1314
1326 (*stream)(in,out,j,key,ctx->Yi.c); 1315 (*stream)(in,out,j,key,ctx->Yi.c);
1327 ctr += (unsigned int)j; 1316 ctr += (unsigned int)j;
1328 if (is_endian.little) 1317 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1329#ifdef BSWAP4 1318#ifdef BSWAP4
1330 ctx->Yi.d[3] = BSWAP4(ctr); 1319 ctx->Yi.d[3] = BSWAP4(ctr);
1331#else 1320#else
@@ -1349,7 +1338,7 @@ int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1349 if (len) { 1338 if (len) {
1350 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key); 1339 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
1351 ++ctr; 1340 ++ctr;
1352 if (is_endian.little) 1341 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1353#ifdef BSWAP4 1342#ifdef BSWAP4
1354 ctx->Yi.d[3] = BSWAP4(ctr); 1343 ctx->Yi.d[3] = BSWAP4(ctr);
1355#else 1344#else
@@ -1371,7 +1360,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1371 const unsigned char *in, unsigned char *out, 1360 const unsigned char *in, unsigned char *out,
1372 size_t len,ctr128_f stream) 1361 size_t len,ctr128_f stream)
1373{ 1362{
1374 const union { long one; char little; } is_endian = {1};
1375 unsigned int n, ctr; 1363 unsigned int n, ctr;
1376 size_t i; 1364 size_t i;
1377 u64 mlen = ctx->len.u[1]; 1365 u64 mlen = ctx->len.u[1];
@@ -1395,7 +1383,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1395 ctx->ares = 0; 1383 ctx->ares = 0;
1396 } 1384 }
1397 1385
1398 if (is_endian.little) 1386 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1399#ifdef BSWAP4 1387#ifdef BSWAP4
1400 ctr = BSWAP4(ctx->Yi.d[3]); 1388 ctr = BSWAP4(ctx->Yi.d[3]);
1401#else 1389#else
@@ -1424,7 +1412,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1424 GHASH(ctx,in,GHASH_CHUNK); 1412 GHASH(ctx,in,GHASH_CHUNK);
1425 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c); 1413 (*stream)(in,out,GHASH_CHUNK/16,key,ctx->Yi.c);
1426 ctr += GHASH_CHUNK/16; 1414 ctr += GHASH_CHUNK/16;
1427 if (is_endian.little) 1415 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1428#ifdef BSWAP4 1416#ifdef BSWAP4
1429 ctx->Yi.d[3] = BSWAP4(ctr); 1417 ctx->Yi.d[3] = BSWAP4(ctr);
1430#else 1418#else
@@ -1454,7 +1442,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1454#endif 1442#endif
1455 (*stream)(in,out,j,key,ctx->Yi.c); 1443 (*stream)(in,out,j,key,ctx->Yi.c);
1456 ctr += (unsigned int)j; 1444 ctr += (unsigned int)j;
1457 if (is_endian.little) 1445 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1458#ifdef BSWAP4 1446#ifdef BSWAP4
1459 ctx->Yi.d[3] = BSWAP4(ctr); 1447 ctx->Yi.d[3] = BSWAP4(ctr);
1460#else 1448#else
@@ -1469,7 +1457,7 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1469 if (len) { 1457 if (len) {
1470 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key); 1458 (*ctx->block)(ctx->Yi.c,ctx->EKi.c,key);
1471 ++ctr; 1459 ++ctr;
1472 if (is_endian.little) 1460 if (_BYTE_ORDER == _LITTLE_ENDIAN)
1473#ifdef BSWAP4 1461#ifdef BSWAP4
1474 ctx->Yi.d[3] = BSWAP4(ctr); 1462 ctx->Yi.d[3] = BSWAP4(ctr);
1475#else 1463#else
@@ -1492,7 +1480,6 @@ int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1492int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag, 1480int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
1493 size_t len) 1481 size_t len)
1494{ 1482{
1495 const union { long one; char little; } is_endian = {1};
1496 u64 alen = ctx->len.u[0]<<3; 1483 u64 alen = ctx->len.u[0]<<3;
1497 u64 clen = ctx->len.u[1]<<3; 1484 u64 clen = ctx->len.u[1]<<3;
1498#ifdef GCM_FUNCREF_4BIT 1485#ifdef GCM_FUNCREF_4BIT
@@ -1502,7 +1489,7 @@ int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
1502 if (ctx->mres || ctx->ares) 1489 if (ctx->mres || ctx->ares)
1503 GCM_MUL(ctx,Xi); 1490 GCM_MUL(ctx,Xi);
1504 1491
1505 if (is_endian.little) { 1492 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
1506#ifdef BSWAP8 1493#ifdef BSWAP8
1507 alen = BSWAP8(alen); 1494 alen = BSWAP8(alen);
1508 clen = BSWAP8(clen); 1495 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 @@
6 */ 6 */
7 7
8#include <openssl/modes.h> 8#include <openssl/modes.h>
9#include <machine/endian.h>
9 10
10 11
11#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) 12#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],
62 const unsigned char *inp, unsigned char *out, 62 const unsigned char *inp, unsigned char *out,
63 size_t len, int enc) 63 size_t len, int enc)
64{ 64{
65 const union { long one; char little; } is_endian = {1};
66 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch; 65 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch;
67 unsigned int i; 66 unsigned int i;
68 67
@@ -98,7 +97,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
98 97
99 if (len==0) return 0; 98 if (len==0) return 0;
100 99
101 if (is_endian.little) { 100 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
102 unsigned int carry,res; 101 unsigned int carry,res;
103 102
104 res = 0x87&(((int)tweak.d[3])>>31); 103 res = 0x87&(((int)tweak.d[3])>>31);
@@ -134,7 +133,7 @@ int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],
134 else { 133 else {
135 union { u64 u[2]; u8 c[16]; } tweak1; 134 union { u64 u[2]; u8 c[16]; } tweak1;
136 135
137 if (is_endian.little) { 136 if (_BYTE_ORDER == _LITTLE_ENDIAN) {
138 unsigned int carry,res; 137 unsigned int carry,res;
139 138
140 res = 0x87&(((int)tweak.d[3])>>31); 139 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 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#include <machine/endian.h>
59#include <openssl/rc4.h> 60#include <openssl/rc4.h>
60#include "rc4_locl.h" 61#include "rc4_locl.h"
61 62
@@ -124,7 +125,6 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
124 ((size_t)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 ) 125 ((size_t)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 )
125 { 126 {
126 RC4_CHUNK ichunk,otp; 127 RC4_CHUNK ichunk,otp;
127 const union { long one; char little; } is_endian = {1};
128 128
129 /* 129 /*
130 * I reckon we can afford to implement both endian 130 * 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,
132 * because the machine code appears to be very compact 132 * because the machine code appears to be very compact
133 * and redundant 1-2KB is perfectly tolerable (i.e. 133 * and redundant 1-2KB is perfectly tolerable (i.e.
134 * in case the compiler fails to eliminate it:-). By 134 * in case the compiler fails to eliminate it:-). By
135 * suggestion from Terrel Larson <terr@terralogic.net> 135 * suggestion from Terrel Larson <terr@terralogic.net>.
136 * who also stands for the is_endian union:-)
137 * 136 *
138 * Special notes. 137 * Special notes.
139 * 138 *
140 * - is_endian is declared automatic as doing otherwise
141 * (declaring static) prevents gcc from eliminating
142 * the redundant code;
143 * - compilers (those I've tried) don't seem to have 139 * - compilers (those I've tried) don't seem to have
144 * problems eliminating either the operators guarded 140 * problems eliminating either the operators guarded
145 * by "if (sizeof(RC4_CHUNK)==8)" or the condition 141 * 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,
154 * 150 *
155 * <appro@fy.chalmers.se> 151 * <appro@fy.chalmers.se>
156 */ 152 */
157 if (!is_endian.little) 153 if (_BYTE_ORDER != _LITTLE_ENDIAN)
158 { /* BIG-ENDIAN CASE */ 154 { /* BIG-ENDIAN CASE */
159# define BESHFT(c) (((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1)) 155# define BESHFT(c) (((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1))
160 for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK)) 156 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 @@
9 9
10#include <stdlib.h> 10#include <stdlib.h>
11#include <string.h> 11#include <string.h>
12#include <machine/endian.h>
12 13
13#include <openssl/crypto.h> 14#include <openssl/crypto.h>
14#include <openssl/sha.h> 15#include <openssl/sha.h>
@@ -206,14 +207,14 @@ static void sha256_block_data_order (SHA256_CTX *ctx, const void *in, size_t num
206 SHA_LONG X[16]; 207 SHA_LONG X[16];
207 int i; 208 int i;
208 const unsigned char *data=in; 209 const unsigned char *data=in;
209 const union { long one; char little; } is_endian = {1};
210 210
211 while (num--) { 211 while (num--) {
212 212
213 a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3]; 213 a = ctx->h[0]; b = ctx->h[1]; c = ctx->h[2]; d = ctx->h[3];
214 e = ctx->h[4]; f = ctx->h[5]; g = ctx->h[6]; h = ctx->h[7]; 214 e = ctx->h[4]; f = ctx->h[5]; g = ctx->h[6]; h = ctx->h[7];
215 215
216 if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)in%4)==0) 216 if (_BYTE_ORDER != _LITTLE_ENDIAN &&
217 sizeof(SHA_LONG)==4 && ((size_t)in%4)==0)
217 { 218 {
218 const SHA_LONG *W=(const SHA_LONG *)data; 219 const SHA_LONG *W=(const SHA_LONG *)data;
219 220
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)
202#endif 202#endif
203 203
204#if !defined(SHA_1) || !defined(SHA1_ASM) 204#if !defined(SHA_1) || !defined(SHA1_ASM)
205#include <machine/endian.h>
205static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num) 206static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
206 { 207 {
207 const unsigned char *data=p; 208 const unsigned char *data=p;
@@ -221,9 +222,9 @@ static void HASH_BLOCK_DATA_ORDER (SHA_CTX *c, const void *p, size_t num)
221 222
222 for (;;) 223 for (;;)
223 { 224 {
224 const union { long one; char little; } is_endian = {1};
225 225
226 if (!is_endian.little && sizeof(SHA_LONG)==4 && ((size_t)p%4)==0) 226 if (_BYTE_ORDER != _LITTLE_ENDIAN &&
227 sizeof(SHA_LONG)==4 && ((size_t)p%4)==0)
227 { 228 {
228 const SHA_LONG *W=(const SHA_LONG *)data; 229 const SHA_LONG *W=(const SHA_LONG *)data;
229 230
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 @@
115 115
116#include <stdio.h> 116#include <stdio.h>
117#include <errno.h> 117#include <errno.h>
118#include <machine/endian.h>
118#include "ssl_locl.h" 119#include "ssl_locl.h"
119#include <openssl/evp.h> 120#include <openssl/evp.h>
120#include <openssl/buffer.h> 121#include <openssl/buffer.h>
@@ -129,13 +130,9 @@ satsub64be(const unsigned char *v1, const unsigned char *v2)
129 130
130 if (sizeof(long) == 8) 131 if (sizeof(long) == 8)
131 do { 132 do {
132 const union {
133 long one;
134 char little;
135 } is_endian = {1};
136 long l; 133 long l;
137 134
138 if (is_endian.little) 135 if (_BYTE_ORDER == _LITTLE_ENDIAN)
139 break; 136 break;
140 /* not reached on little-endians */ 137 /* not reached on little-endians */
141 /* following test is redundant, because input is 138 /* following test is redundant, because input is