From 3c59cebe9112f6055a0fa0cf6efa332557a16745 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 3 Jan 2019 18:18:20 +0100 Subject: [PATCH] Make the blake2b and poly1305 state opaque --- .../aes256gcm/aesni/aead_aes256gcm_aesni.c | 50 +++++++++---------- .../crypto_generichash/blake2b/ref/blake2.h | 9 +++- .../blake2b/ref/generichash_blake2b.c | 17 ++++--- .../include/sodium/crypto_aead_aes256gcm.h | 4 +- .../sodium/crypto_generichash_blake2b.h | 7 +-- 5 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c index dc54bca7..69707a68 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -51,10 +51,10 @@ _bswap64(const uint64_t x) } #endif -typedef struct context { - CRYPTO_ALIGN(16) unsigned char H[16]; - __m128i rkeys[16]; -} context; +typedef struct aes256gcm_state { + __m128i rkeys[16]; + unsigned char H[16]; +} aes256gcm_state; static inline void aesni_key256_expand(const unsigned char *key, __m128i * const rkeys) @@ -488,10 +488,10 @@ int crypto_aead_aes256gcm_beforenm(crypto_aead_aes256gcm_state *ctx_, const unsigned char *k) { - context *ctx = (context *) ctx_; - __m128i *rkeys = ctx->rkeys; - __m128i zero = _mm_setzero_si128(); - unsigned char *H = ctx->H; + aes256gcm_state *ctx = (aes256gcm_state *) (void *) ctx_; + unsigned char *H = ctx->H; + __m128i *rkeys = ctx->rkeys; + __m128i zero = _mm_setzero_si128(); COMPILER_ASSERT((sizeof *ctx_) >= (sizeof *ctx)); aesni_key256_expand(k, rkeys); @@ -509,13 +509,13 @@ crypto_aead_aes256gcm_encrypt_detached_afternm(unsigned char *c, const unsigned char *npub, const crypto_aead_aes256gcm_state *ctx_) { - const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); - const context *ctx = (const context *) ctx_; - const __m128i *rkeys = ctx->rkeys; - __m128i Hv, H2v, H3v, H4v, accv; - unsigned long long i, j; - unsigned long long adlen_rnd64 = adlen & ~63ULL; - unsigned long long mlen_rnd128 = mlen & ~127ULL; + const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); + const aes256gcm_state *ctx = (const aes256gcm_state *) (const void *) ctx_; + const __m128i *rkeys = ctx->rkeys; + __m128i Hv, H2v, H3v, H4v, accv; + unsigned long long i, j; + unsigned long long adlen_rnd64 = adlen & ~63ULL; + unsigned long long mlen_rnd128 = mlen & ~127ULL; CRYPTO_ALIGN(16) uint32_t n2[4]; CRYPTO_ALIGN(16) unsigned char H[16]; CRYPTO_ALIGN(16) unsigned char T[16]; @@ -647,14 +647,14 @@ crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned char * const unsigned char *npub, const crypto_aead_aes256gcm_state *ctx_) { - const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); - const context *ctx = (const context *) ctx_; - const __m128i *rkeys = ctx->rkeys; - __m128i Hv, H2v, H3v, H4v, accv; - unsigned long long i, j; - unsigned long long adlen_rnd64 = adlen & ~63ULL; - unsigned long long mlen; - unsigned long long mlen_rnd128; + const __m128i rev = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); + const aes256gcm_state *ctx = (const aes256gcm_state *) (const void *) ctx_; + const __m128i *rkeys = ctx->rkeys; + __m128i Hv, H2v, H3v, H4v, accv; + unsigned long long i, j; + unsigned long long adlen_rnd64 = adlen & ~63ULL; + unsigned long long mlen; + unsigned long long mlen_rnd128; CRYPTO_ALIGN(16) uint32_t n2[4]; CRYPTO_ALIGN(16) unsigned char H[16]; CRYPTO_ALIGN(16) unsigned char T[16]; @@ -862,7 +862,7 @@ crypto_aead_aes256gcm_encrypt(unsigned char *c, ret = crypto_aead_aes256gcm_encrypt_afternm (c, clen_p, m, mlen, ad, adlen, nsec, npub, (const crypto_aead_aes256gcm_state *) &ctx); - sodium_memzero(ctx, sizeof ctx); + sodium_memzero(&ctx, sizeof ctx); return ret; } @@ -906,7 +906,7 @@ crypto_aead_aes256gcm_decrypt(unsigned char *m, ret = crypto_aead_aes256gcm_decrypt_afternm (m, mlen_p, nsec, c, clen, ad, adlen, npub, (const crypto_aead_aes256gcm_state *) &ctx); - sodium_memzero(ctx, sizeof ctx); + sodium_memzero(&ctx, sizeof ctx); return ret; } diff --git a/src/libsodium/crypto_generichash/blake2b/ref/blake2.h b/src/libsodium/crypto_generichash/blake2b/ref/blake2.h index c6c4fccb..6ea2832e 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/blake2.h +++ b/src/libsodium/crypto_generichash/blake2b/ref/blake2.h @@ -65,7 +65,14 @@ typedef struct blake2b_param_ { uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */ } blake2b_param; -typedef crypto_generichash_blake2b_state blake2b_state; +typedef struct blake2b_state { + uint64_t h[8]; + uint64_t t[2]; + uint64_t f[2]; + uint8_t buf[2 * 128]; + size_t buflen; + uint8_t last_node; +} blake2b_state; #if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) #pragma pack() diff --git a/src/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c b/src/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c index 4bd08550..99aa9324 100644 --- a/src/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c +++ b/src/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c @@ -53,10 +53,10 @@ crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state, assert(outlen <= UINT8_MAX); assert(keylen <= UINT8_MAX); if (key == NULL || keylen <= 0U) { - if (blake2b_init(state, (uint8_t) outlen) != 0) { + if (blake2b_init((blake2b_state *) (void *) state, (uint8_t) outlen) != 0) { return -1; /* LCOV_EXCL_LINE */ } - } else if (blake2b_init_key(state, (uint8_t) outlen, key, + } else if (blake2b_init_key((blake2b_state *) (void *) state, (uint8_t) outlen, key, (uint8_t) keylen) != 0) { return -1; /* LCOV_EXCL_LINE */ } @@ -76,11 +76,12 @@ crypto_generichash_blake2b_init_salt_personal( assert(outlen <= UINT8_MAX); assert(keylen <= UINT8_MAX); if (key == NULL || keylen <= 0U) { - if (blake2b_init_salt_personal(state, (uint8_t) outlen, salt, - personal) != 0) { + if (blake2b_init_salt_personal((blake2b_state *) (void *) state, + (uint8_t) outlen, salt, personal) != 0) { return -1; /* LCOV_EXCL_LINE */ } - } else if (blake2b_init_key_salt_personal(state, (uint8_t) outlen, key, + } else if (blake2b_init_key_salt_personal((blake2b_state *) (void *) state, + (uint8_t) outlen, key, (uint8_t) keylen, salt, personal) != 0) { return -1; /* LCOV_EXCL_LINE */ @@ -93,7 +94,8 @@ crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state, const unsigned char *in, unsigned long long inlen) { - return blake2b_update(state, (const uint8_t *) in, (uint64_t) inlen); + return blake2b_update((blake2b_state *) (void *) state, + (const uint8_t *) in, (uint64_t) inlen); } int @@ -101,7 +103,8 @@ crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state, unsigned char *out, const size_t outlen) { assert(outlen <= UINT8_MAX); - return blake2b_final(state, (uint8_t *) out, (uint8_t) outlen); + return blake2b_final((blake2b_state *) (void *) state, + (uint8_t *) out, (uint8_t) outlen); } int diff --git a/src/libsodium/include/sodium/crypto_aead_aes256gcm.h b/src/libsodium/include/sodium/crypto_aead_aes256gcm.h index 752586cc..2d31a975 100644 --- a/src/libsodium/include/sodium/crypto_aead_aes256gcm.h +++ b/src/libsodium/include/sodium/crypto_aead_aes256gcm.h @@ -56,7 +56,9 @@ size_t crypto_aead_aes256gcm_abytes(void); SODIUM_EXPORT size_t crypto_aead_aes256gcm_messagebytes_max(void); -typedef CRYPTO_ALIGN(16) unsigned char crypto_aead_aes256gcm_state[512]; +typedef CRYPTO_ALIGN(16) struct crypto_aead_aes256gcm_state_ { + unsigned char opaque[512]; +} crypto_aead_aes256gcm_state; SODIUM_EXPORT size_t crypto_aead_aes256gcm_statebytes(void); diff --git a/src/libsodium/include/sodium/crypto_generichash_blake2b.h b/src/libsodium/include/sodium/crypto_generichash_blake2b.h index f1110a4d..ecda3625 100644 --- a/src/libsodium/include/sodium/crypto_generichash_blake2b.h +++ b/src/libsodium/include/sodium/crypto_generichash_blake2b.h @@ -21,12 +21,7 @@ extern "C" { #endif typedef struct CRYPTO_ALIGN(64) crypto_generichash_blake2b_state { - uint64_t h[8]; - uint64_t t[2]; - uint64_t f[2]; - uint8_t buf[2 * 128]; - size_t buflen; - uint8_t last_node; + unsigned char opaque[384]; } crypto_generichash_blake2b_state; #if defined(__IBMC__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC)