#include <string.h>
+#include "internal/endian.h"
#include "crypto/chacha.h"
#include "crypto/ctype.h"
{
u32 x[16];
int i;
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
memcpy(x, input, sizeof(x));
QUARTERROUND(3, 4, 9, 14);
}
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
for (i = 0; i < 16; ++i)
output->u[i] = x[i] + input[i];
} else {
#include "internal/bio.h"
#include <openssl/evp.h>
#include <openssl/rand.h>
+#include "internal/endian.h"
#include "crypto/evp.h"
static int ok_write(BIO *h, const char *buf, int num);
static void longswap(void *_ptr, size_t len)
{
- const union {
- long one;
- char little;
- } is_endian = {
- 1
- };
-
- if (is_endian.little) {
+ DECLARE_IS_ENDIAN;
+
+ if (IS_LITTLE_ENDIAN) {
size_t i;
unsigned char *p = _ptr, c;
#include <stdio.h>
#include "internal/cryptlib.h"
+#include "internal/endian.h"
#ifndef OPENSSL_NO_CHACHA
}
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
memcpy(ctr, (unsigned char *)&actx->len, POLY1305_BLOCK_SIZE);
} else {
ctr[0] = (unsigned char)(actx->len.aad);
}
if (in == NULL /* explicit final */
|| plen != len) { /* or tls mode */
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
unsigned char temp[POLY1305_BLOCK_SIZE];
if (actx->aad) { /* wrap up aad */
Poly1305_Update(POLY1305_ctx(actx), zero,
POLY1305_BLOCK_SIZE - rem);
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
Poly1305_Update(POLY1305_ctx(actx),
(unsigned char *)&actx->len, POLY1305_BLOCK_SIZE);
} else {
#include <string.h>
#include <openssl/crypto.h>
+#include "internal/endian.h"
#include "crypto/modes.h"
#if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
static void ctr128_inc_aligned(unsigned char *counter)
{
size_t *data, c, d, n;
- const union {
- long one;
- char little;
- } is_endian = {
- 1
- };
-
- if (is_endian.little || ((size_t)counter % sizeof(size_t)) != 0) {
+ DECLARE_IS_ENDIAN;
+
+ if (IS_LITTLE_ENDIAN || ((size_t)counter % sizeof(size_t)) != 0) {
ctr128_inc(counter);
return;
}
#include <string.h>
#include <openssl/crypto.h>
#include "internal/cryptlib.h"
+#include "internal/endian.h"
#include "crypto/modes.h"
#if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
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 };
+ DECLARE_IS_ENDIAN;
static const size_t rem_8bit[256] = {
PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
Z.hi ^= (u64)rem_8bit[rem] << 32;
}
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
# ifdef BSWAP8
Xi[0] = BSWAP8(Z.hi);
Xi[1] = BSWAP8(Z.lo);
*/
{
int j;
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
for (j = 0; j < 16; ++j) {
V = Htable[j];
Htable[j].hi = V.lo;
u128 Z;
int cnt = 15;
size_t rem, nlo, nhi;
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
nlo = ((const u8 *)Xi)[15];
nhi = nlo >> 4;
Z.lo ^= Htable[nlo].lo;
}
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
# ifdef BSWAP8
Xi[0] = BSWAP8(Z.hi);
Xi[1] = BSWAP8(Z.lo);
u128 Z;
int cnt;
size_t rem, nlo, nhi;
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
# if 1
do {
Z.hi ^= ((u64)rem_8bit[rem << 4]) << 48;
# endif
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
# ifdef BSWAP8
Xi[0] = BSWAP8(Z.hi);
Xi[1] = BSWAP8(Z.lo);
long X;
int i, j;
const long *xi = (const long *)Xi;
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
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 (IS_LITTLE_ENDIAN) {
if (sizeof(long) == 8) {
# ifdef BSWAP8
X = (long)(BSWAP8(xi[j]));
}
}
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
# ifdef BSWAP8
Xi[0] = BSWAP8(Z.hi);
Xi[1] = BSWAP8(Z.lo);
void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
memset(ctx, 0, sizeof(*ctx));
ctx->block = block;
(*block) (ctx->H.c, ctx->H.c, key);
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
/* H is stored in host byte order */
#ifdef BSWAP8
ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
size_t len)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
unsigned int ctr;
#ifdef GCM_FUNCREF_4BIT
void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
GCM_MUL(ctx);
}
len0 <<= 3;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
#ifdef BSWAP8
ctx->Xi.u[1] ^= BSWAP8(len0);
#else
GCM_MUL(ctx);
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
#ifdef BSWAP4
ctr = BSWAP4(ctx->Xi.d[3]);
#else
(*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
#ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
#else
const unsigned char *in, unsigned char *out,
size_t len)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
unsigned int n, ctr, mres;
size_t i;
u64 mlen = ctx->len.u[1];
ctx->ares = 0;
}
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
#ifdef BSWAP4
ctr = BSWAP4(ctx->Yi.d[3]);
#else
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
if (len) {
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
if (n == 0) {
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
#ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
#else
const unsigned char *in, unsigned char *out,
size_t len)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
unsigned int n, ctr, mres;
size_t i;
u64 mlen = ctx->len.u[1];
ctx->ares = 0;
}
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
#ifdef BSWAP4
ctr = BSWAP4(ctx->Yi.d[3]);
#else
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
if (len) {
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
if (n == 0) {
(*block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
#ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
#else
#if defined(OPENSSL_SMALL_FOOTPRINT)
return CRYPTO_gcm128_encrypt(ctx, in, out, len);
#else
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
unsigned int n, ctr, mres;
size_t i;
u64 mlen = ctx->len.u[1];
ctx->ares = 0;
}
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctr = BSWAP4(ctx->Yi.d[3]);
# else
while (len >= GHASH_CHUNK) {
(*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
ctr += GHASH_CHUNK / 16;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
(*stream) (in, out, j, key, ctx->Yi.c);
ctr += (unsigned int)j;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
if (len) {
(*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
#if defined(OPENSSL_SMALL_FOOTPRINT)
return CRYPTO_gcm128_decrypt(ctx, in, out, len);
#else
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
unsigned int n, ctr, mres;
size_t i;
u64 mlen = ctx->len.u[1];
ctx->ares = 0;
}
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctr = BSWAP4(ctx->Yi.d[3]);
# else
GHASH(ctx, in, GHASH_CHUNK);
(*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
ctr += GHASH_CHUNK / 16;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
# endif
(*stream) (in, out, j, key, ctx->Yi.c);
ctr += (unsigned int)j;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
if (len) {
(*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
++ctr;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
# ifdef BSWAP4
ctx->Yi.d[3] = BSWAP4(ctr);
# else
int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
size_t len)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
u64 alen = ctx->len.u[0] << 3;
u64 clen = ctx->len.u[1] << 3;
#ifdef GCM_FUNCREF_4BIT
GCM_MUL(ctx);
#endif
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
#ifdef BSWAP8
alen = BSWAP8(alen);
clen = BSWAP8(clen);
#include <openssl/evp.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
+#include "internal/endian.h"
#include "crypto/modes.h"
#include "crypto/siv.h"
__owur static ossl_inline uint64_t siv128_getword(SIV_BLOCK const *b, size_t i)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
return byteswap8(b->word[i]);
return b->word[i];
}
static ossl_inline void siv128_putword(SIV_BLOCK *b, size_t i, uint64_t x)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little)
+ if (IS_LITTLE_ENDIAN)
b->word[i] = byteswap8(x);
else
b->word[i] = x;
#include <string.h>
#include <openssl/crypto.h>
+#include "internal/endian.h"
#include "crypto/modes.h"
#ifndef STRICT_ALIGNMENT
const unsigned char *inp, unsigned char *out,
size_t len, int enc)
{
- const union {
- long one;
- char little;
- } is_endian = {
- 1
- };
+ DECLARE_IS_ENDIAN;
union {
u64 u[2];
u32 d[4];
if (len == 0)
return 0;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
unsigned int carry, res;
res = 0x87 & (((int)tweak.d[3]) >> 31);
u8 c[16];
} tweak1;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
unsigned int carry, res;
res = 0x87 & (((int)tweak.d[3]) >> 31);
#include <openssl/crypto.h>
#include <openssl/sha.h>
#include <openssl/opensslv.h>
+#include "internal/endian.h"
int SHA224_Init(SHA256_CTX *c)
{
SHA_LONG X[16];
int i;
const unsigned char *data = in;
- const union {
- long one;
- char little;
- } is_endian = {
- 1
- };
+ DECLARE_IS_ENDIAN;
while (num--) {
g = ctx->h[6];
h = ctx->h[7];
- if (!is_endian.little && sizeof(SHA_LONG) == 4
+ if (!IS_LITTLE_ENDIAN && sizeof(SHA_LONG) == 4
&& ((size_t)in % 4) == 0) {
const SHA_LONG *W = (const SHA_LONG *)data;
#include <openssl/opensslconf.h>
#include <openssl/sha.h>
+#include "internal/endian.h"
#define DATA_ORDER_IS_BIG_ENDIAN
E = c->h4;
for (;;) {
- const union {
- long one;
- char little;
- } is_endian = {
- 1
- };
-
- if (!is_endian.little && sizeof(SHA_LONG) == 4
+ DECLARE_IS_ENDIAN;
+
+ if (!IS_LITTLE_ENDIAN && sizeof(SHA_LONG) == 4
&& ((size_t)p % 4) == 0) {
const SHA_LONG *W = (const SHA_LONG *)data;
* https://www.openssl.org/source/license.html
*/
-#ifndef OSSL_TEST_OSSL_TEST_ENDIAN_H
-# define OSSL_TEST_OSSL_TEST_ENDIAN_H
+#ifndef OSSL_INTERNAL_ENDIAN_H
+# define OSSL_INTERNAL_ENDIAN_H
# define DECLARE_IS_ENDIAN \
const union { \
/* chacha20_poly1305 cipher implementation */
+#include "internal/endian.h"
#include "cipher_chacha20_poly1305.h"
static int chacha_poly1305_tls_init(PROV_CIPHER_CTX *bctx,
size_t tail, tohash_len, buf_len, plen = ctx->tls_payload_length;
unsigned char *buf, *tohash, *ctr, storage[sizeof(zero) + 32];
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
if (len != plen + POLY1305_BLOCK_SIZE)
return 0;
Poly1305_Update(poly, zero, tail);
}
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
memcpy(ctr, (unsigned char *)&ctx->len, POLY1305_BLOCK_SIZE);
} else {
ctr[0] = (unsigned char)(ctx->len.aad);
size_t olen = 0;
int rv = 0;
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
if (!ctx->mac_inited) {
#if !defined(OPENSSL_SMALL_FOOTPRINT)
if ((rem = (size_t)ctx->len.text % POLY1305_BLOCK_SIZE))
Poly1305_Update(poly, zero, POLY1305_BLOCK_SIZE - rem);
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
Poly1305_Update(poly, (unsigned char *)&ctx->len,
POLY1305_BLOCK_SIZE);
} else {
*/
#include <string.h>
+#include "internal/endian.h"
static ossl_inline uint32_t load32(const uint8_t *src)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
uint32_t w;
memcpy(&w, src, sizeof(w));
return w;
static ossl_inline uint64_t load64(const uint8_t *src)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
uint64_t w;
memcpy(&w, src, sizeof(w));
return w;
static ossl_inline void store32(uint8_t *dst, uint32_t w)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
memcpy(dst, &w, sizeof(w));
} else {
uint8_t *p = (uint8_t *)dst;
static ossl_inline void store64(uint8_t *dst, uint64_t w)
{
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (is_endian.little) {
+ if (IS_LITTLE_ENDIAN) {
memcpy(dst, &w, sizeof(w));
} else {
uint8_t *p = (uint8_t *)dst;
#include "internal/cryptlib.h"
#include "crypto/evp.h"
#include "internal/numbers.h"
+#include "internal/endian.h"
#include "prov/implementations.h"
#include "prov/provider_ctx.h"
#include "prov/provider_util.h"
static uint32_t be32(uint32_t host)
{
uint32_t big = 0;
- const union {
- long one;
- char little;
- } is_endian = { 1 };
+ DECLARE_IS_ENDIAN;
- if (!is_endian.little)
+ if (!IS_LITTLE_ENDIAN)
return host;
big |= (host & 0xff000000) >> 24;
#include <string.h>
#include "testutil.h"
#include "internal/nelem.h"
-#include "ossl_test_endian.h"
+#include "internal/endian.h"
#include <openssl/params.h>
#include <openssl/bn.h>