From aa1d894e102e23d162b8e2bccd4b3bf1700de2f2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 30 Nov 2013 05:34:49 +0100 Subject: Make the crypto implementations independent of fastd.h (and fix more minor bugs) --- .../aes128_ctr/nacl/cipher_aes128_ctr_nacl.c | 12 ++++----- .../cipher/aes128_ctr/openssl/aes128_ctr_openssl.c | 7 ++--- .../cipher/blowfish_ctr/builtin/blowfish_ctr.c | 8 +++--- src/crypto/cipher/ciphers.c.in | 1 + src/crypto/cipher/null/memcpy/null_memcpy.c | 6 ++--- src/crypto/cipher/salsa20/nacl/salsa20_nacl.c | 7 ++--- src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c | 7 ++--- src/crypto/mac/ghash/builtin/ghash_builtin.c | 10 +++++--- src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c | 9 +------ src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h | 10 +++----- .../mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c | 30 ++++++++++++++-------- src/crypto/mac/macs.c.in | 1 + 12 files changed, 58 insertions(+), 50 deletions(-) (limited to 'src/crypto') diff --git a/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c b/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c index f4756a3..ca32e72 100644 --- a/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c +++ b/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c @@ -25,6 +25,7 @@ #include "../../../../crypto.h" + #include @@ -33,26 +34,25 @@ struct __attribute__((aligned(16))) fastd_cipher_state { }; -static fastd_cipher_state_t* aes128_ctr_init(fastd_context_t *ctx, const uint8_t *key) { +static fastd_cipher_state_t* aes128_ctr_init(const uint8_t *key) { fastd_block128_t k; memcpy(k.b, key, sizeof(fastd_block128_t)); fastd_cipher_state_t *state; - int err = posix_memalign((void**)&state, 16, sizeof(fastd_cipher_state_t)); - if (err) - exit_error(ctx, "posix_memalign: %s", strerror(err)); + if (posix_memalign((void**)&state, 16, sizeof(fastd_cipher_state_t))) + abort(); crypto_stream_aes128ctr_beforenm(state->d, k.b); return state; } -static bool aes128_ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { +static bool aes128_ctr_crypt(const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { crypto_stream_aes128ctr_xor_afternm(out->b, in->b, len, iv, state->d); return true; } -static void aes128_ctr_free(fastd_context_t *ctx UNUSED, fastd_cipher_state_t *state) { +static void aes128_ctr_free(fastd_cipher_state_t *state) { if (state) { secure_memzero(state, sizeof(*state)); free(state); diff --git a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c index b3c739c..22b0ebe 100644 --- a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c +++ b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c @@ -25,6 +25,7 @@ #include "../../../../crypto.h" + #include @@ -33,7 +34,7 @@ struct fastd_cipher_state { }; -static fastd_cipher_state_t* aes128_ctr_init(fastd_context_t *ctx UNUSED, const uint8_t *key) { +static fastd_cipher_state_t* aes128_ctr_init(const uint8_t *key) { fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t)); state->aes = EVP_CIPHER_CTX_new(); @@ -42,7 +43,7 @@ static fastd_cipher_state_t* aes128_ctr_init(fastd_context_t *ctx UNUSED, const return state; } -static bool aes128_ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { +static bool aes128_ctr_crypt(const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { int clen, clen2; if (!EVP_EncryptInit(state->aes, NULL, NULL, iv)) @@ -60,7 +61,7 @@ static bool aes128_ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_sta return true; } -static void aes128_ctr_free(fastd_context_t *ctx UNUSED, fastd_cipher_state_t *state) { +static void aes128_ctr_free(fastd_cipher_state_t *state) { if (state) { EVP_CIPHER_CTX_free(state->aes); free(state); diff --git a/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c b/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c index 3e82e38..feb1406 100644 --- a/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c +++ b/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c @@ -26,6 +26,8 @@ #include "../../../../crypto.h" +#include + typedef union bf_block { fastd_block128_t b; @@ -209,7 +211,7 @@ static inline uint32_t bf_f(const fastd_cipher_state_t *state, uint32_t x) { BF_SWAP(L, R); \ }) -static fastd_cipher_state_t* blowfish_ctr_init(fastd_context_t *ctx UNUSED, const uint8_t *key) { +static fastd_cipher_state_t* blowfish_ctr_init(const uint8_t *key) { uint32_t key32[14]; memcpy(key32, key, 56); bf_ntohl(key32, 14); @@ -241,7 +243,7 @@ static fastd_cipher_state_t* blowfish_ctr_init(fastd_context_t *ctx UNUSED, cons return state; } -static bool blowfish_ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { +static bool blowfish_ctr_crypt(const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { register bf_block_t block; register uint32_t ctr[2]; @@ -271,7 +273,7 @@ static bool blowfish_ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_s return true; } -static void blowfish_ctr_free(fastd_context_t *ctx UNUSED, fastd_cipher_state_t *state) { +static void blowfish_ctr_free(fastd_cipher_state_t *state) { if (state) { secure_memzero(state, sizeof(*state)); free(state); diff --git a/src/crypto/cipher/ciphers.c.in b/src/crypto/cipher/ciphers.c.in index 19893f6..735d28d 100644 --- a/src/crypto/cipher/ciphers.c.in +++ b/src/crypto/cipher/ciphers.c.in @@ -25,6 +25,7 @@ #include +#include @CIPHER_DEFINITIONS@ diff --git a/src/crypto/cipher/null/memcpy/null_memcpy.c b/src/crypto/cipher/null/memcpy/null_memcpy.c index 7f0b8b3..b3f5dd3 100644 --- a/src/crypto/cipher/null/memcpy/null_memcpy.c +++ b/src/crypto/cipher/null/memcpy/null_memcpy.c @@ -27,16 +27,16 @@ #include "../../../../crypto.h" -static fastd_cipher_state_t* null_init(fastd_context_t *ctx UNUSED, const uint8_t *key UNUSED) { +static fastd_cipher_state_t* null_init(const uint8_t *key UNUSED) { return NULL; } -static bool null_memcpy(fastd_context_t *ctx UNUSED, const fastd_cipher_state_t *state UNUSED, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv UNUSED) { +static bool null_memcpy(const fastd_cipher_state_t *state UNUSED, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv UNUSED) { memcpy(out, in, len); return true; } -static void null_free(fastd_context_t *ctx UNUSED, fastd_cipher_state_t *state UNUSED) { +static void null_free(fastd_cipher_state_t *state UNUSED) { } const fastd_cipher_t fastd_cipher_null_memcpy = { diff --git a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c index ed14c3c..fafc282 100644 --- a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c +++ b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c @@ -25,6 +25,7 @@ #include "../../../../crypto.h" + #include @@ -33,19 +34,19 @@ struct __attribute__((aligned(16))) fastd_cipher_state { }; -static fastd_cipher_state_t* salsa20_init(fastd_context_t *ctx UNUSED, const uint8_t *key) { +static fastd_cipher_state_t* salsa20_init(const uint8_t *key) { fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t)); memcpy(state->key, key, crypto_stream_salsa20_KEYBYTES); return state; } -static bool salsa20_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { +static bool salsa20_crypt(const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { crypto_stream_salsa20_xor(out->b, in->b, len, iv, state->key); return true; } -static void salsa20_free(fastd_context_t *ctx UNUSED, fastd_cipher_state_t *state) { +static void salsa20_free(fastd_cipher_state_t *state) { if (state) { secure_memzero(state, sizeof(*state)); free(state); diff --git a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c index 79f01c4..69862f7 100644 --- a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c +++ b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c @@ -25,6 +25,7 @@ #include "../../../../crypto.h" + #include @@ -33,19 +34,19 @@ struct __attribute__((aligned(16))) fastd_cipher_state { }; -static fastd_cipher_state_t* salsa2012_init(fastd_context_t *ctx UNUSED, const uint8_t *key) { +static fastd_cipher_state_t* salsa2012_init(const uint8_t *key) { fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t)); memcpy(state->key, key, crypto_stream_salsa2012_KEYBYTES); return state; } -static bool salsa2012_crypt(fastd_context_t *ctx UNUSED, const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { +static bool salsa2012_crypt(const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const uint8_t *iv) { crypto_stream_salsa2012_xor(out->b, in->b, len, iv, state->key); return true; } -static void salsa2012_free(fastd_context_t *ctx UNUSED, fastd_cipher_state_t *state) { +static void salsa2012_free(fastd_cipher_state_t *state) { if (state) { secure_memzero(state, sizeof(*state)); free(state); diff --git a/src/crypto/mac/ghash/builtin/ghash_builtin.c b/src/crypto/mac/ghash/builtin/ghash_builtin.c index 341408f..c518663 100644 --- a/src/crypto/mac/ghash/builtin/ghash_builtin.c +++ b/src/crypto/mac/ghash/builtin/ghash_builtin.c @@ -61,8 +61,10 @@ static inline void mulH_a(fastd_block128_t *x, const fastd_mac_state_t *cstate) } -static fastd_mac_state_t* ghash_init(fastd_context_t *ctx UNUSED, const uint8_t *key) { - fastd_mac_state_t *state = malloc(sizeof(fastd_mac_state_t)); +static fastd_mac_state_t* ghash_init(const uint8_t *key) { + fastd_mac_state_t *state; + if (posix_memalign((void**)&state, 16, sizeof(fastd_mac_state_t))) + abort(); fastd_block128_t Hbase[4]; fastd_block128_t Rbase[4]; @@ -105,7 +107,7 @@ static fastd_mac_state_t* ghash_init(fastd_context_t *ctx UNUSED, const uint8_t return state; } -static bool ghash_hash(fastd_context_t *ctx UNUSED, const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) { +static bool ghash_hash(const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) { memset(out, 0, sizeof(fastd_block128_t)); size_t i; @@ -117,7 +119,7 @@ static bool ghash_hash(fastd_context_t *ctx UNUSED, const fastd_mac_state_t *sta return true; } -static void ghash_free(fastd_context_t *ctx UNUSED, fastd_mac_state_t *state) { +static void ghash_free(fastd_mac_state_t *state) { if (state) { secure_memzero(state, sizeof(*state)); free(state); diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c index 8f1edb0..419d406 100644 --- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c +++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.c @@ -34,17 +34,10 @@ static bool ghash_available(void) { return ((fastd_cpuid()&REQ) == REQ); } -static void ghash_free(fastd_context_t *ctx UNUSED, fastd_mac_state_t *state) { - if (state) { - secure_memzero(state, sizeof(*state)); - free(state); - } -} - const fastd_mac_t fastd_mac_ghash_pclmulqdq = { .available = ghash_available, .init = fastd_ghash_pclmulqdq_init, .hash = fastd_ghash_pclmulqdq_hash, - .free = ghash_free, + .free = fastd_ghash_pclmulqdq_free, }; diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h index ccb1ecf..81313a3 100644 --- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h +++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h @@ -27,10 +27,6 @@ #include "../../../../crypto.h" -struct fastd_mac_state { - fastd_block128_t H; -}; - - -fastd_mac_state_t* fastd_ghash_pclmulqdq_init(fastd_context_t *ctx, const uint8_t *key); -bool fastd_ghash_pclmulqdq_hash(fastd_context_t *ctx, const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks); +fastd_mac_state_t* fastd_ghash_pclmulqdq_init(const uint8_t *key); +bool fastd_ghash_pclmulqdq_hash(const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks); +void fastd_ghash_pclmulqdq_free(fastd_mac_state_t *state); diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c index 134d4ed..201ec9a 100644 --- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c +++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c @@ -35,6 +35,11 @@ typedef union vecblock { fastd_block128_t b; } vecblock_t; +struct fastd_mac_state { + vecblock_t H; +}; + + static inline __m128i shl(__m128i v, int a) { __m128i tmpl = _mm_slli_epi64(v, a); __m128i tmpr = _mm_srli_epi64(v, 64-a); @@ -58,18 +63,24 @@ static inline __m128i byteswap(__m128i v) { } -fastd_mac_state_t* fastd_ghash_pclmulqdq_init(fastd_context_t *ctx UNUSED, const uint8_t *key) { - fastd_mac_state_t *state = malloc(sizeof(fastd_mac_state_t)); - - vecblock_t h; - memcpy(&h, key, sizeof(__m128i)); +fastd_mac_state_t* fastd_ghash_pclmulqdq_init(const uint8_t *key) { + fastd_mac_state_t *state; + if (posix_memalign((void**)&state, 16, sizeof(fastd_mac_state_t))) + abort(); - h.v = byteswap(h.v); - state->H = h.b; + memcpy(&state->H, key, sizeof(__m128i)); + state->H.v = byteswap(state->H.v); return state; } +void fastd_ghash_pclmulqdq_free(fastd_mac_state_t *state) { + if (state) { + secure_memzero(state, sizeof(*state)); + free(state); + } +} + static __m128i gmul(__m128i v, __m128i h) { /* multiply */ __m128i z0, z1, z2, tmp; @@ -123,15 +134,14 @@ static __m128i gmul(__m128i v, __m128i h) { } -bool fastd_ghash_pclmulqdq_hash(fastd_context_t *ctx UNUSED, const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) { - vecblock_t h = {.b = state->H}; +bool fastd_ghash_pclmulqdq_hash(const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) { vecblock_t v = {.v = _mm_setzero_si128()}; size_t i; for (i = 0; i < n_blocks; i++) { __m128i b = ((vecblock_t)in[i]).v; v.v = _mm_xor_si128(v.v, byteswap(b)); - v.v = gmul(v.v, h.v); + v.v = gmul(v.v, state->H.v); } v.v = byteswap(v.v); diff --git a/src/crypto/mac/macs.c.in b/src/crypto/mac/macs.c.in index 57f327a..cdf1b10 100644 --- a/src/crypto/mac/macs.c.in +++ b/src/crypto/mac/macs.c.in @@ -25,6 +25,7 @@ #include +#include @MAC_DEFINITIONS@ -- cgit v1.2.3