diff --git a/src/crypto.h b/src/crypto.h index 97b640e..ad3afa5 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -30,8 +30,8 @@ struct fastd_cipher { /**< Checks if the algorithm is available on the platform used. If NULL, the algorithm is always available. */ bool (*available)(void); - /** Initializes a cipher context with the given key */ - fastd_cipher_state_t *(*init)(const uint8_t *key); + /** Initializes a cipher context with the given key and cipher-specific flags */ + fastd_cipher_state_t *(*init)(const uint8_t *key, int flags); /** Encrypts or decrypts data */ bool (*crypt)( const fastd_cipher_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t len, @@ -51,8 +51,8 @@ struct fastd_mac { /**< Checks if the algorithm is available on the platform used. If NULL, the algorithm is always available. */ bool (*available)(void); - /** Initializes a MAC context with the given key */ - fastd_mac_state_t *(*init)(const uint8_t *key); + /** Initializes a MAC context with the given key and mac-specific flags */ + fastd_mac_state_t *(*init)(const uint8_t *key, int flags); /** Computes the MAC of data blocks */ bool (*digest)( const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t length); 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 5818b33..34ba61d 100644 --- a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c +++ b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c @@ -14,6 +14,8 @@ #include "../../../../alloc.h" #include "../../../../crypto.h" +#include + #include @@ -24,7 +26,9 @@ struct fastd_cipher_state { /** Initializes the cipher state */ -static fastd_cipher_state_t *aes128_ctr_init(const uint8_t *key) { +static fastd_cipher_state_t *aes128_ctr_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t); state->aes = EVP_CIPHER_CTX_new(); diff --git a/src/crypto/cipher/null/memcpy/null_memcpy.c b/src/crypto/cipher/null/memcpy/null_memcpy.c index 6f904f4..e24f434 100644 --- a/src/crypto/cipher/null/memcpy/null_memcpy.c +++ b/src/crypto/cipher/null/memcpy/null_memcpy.c @@ -13,9 +13,13 @@ #include "../../../../crypto.h" +#include + /** Doesn't do anything as the null cipher doesn't use any state */ -static fastd_cipher_state_t *null_init(UNUSED const uint8_t *key) { +static fastd_cipher_state_t *null_init(UNUSED const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + return NULL; } diff --git a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c index 7f95c81..2d439ef 100644 --- a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c +++ b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c @@ -20,6 +20,8 @@ #include #endif +#include + /** The cipher state */ struct fastd_cipher_state { @@ -28,7 +30,9 @@ struct fastd_cipher_state { /** Initializes the cipher state */ -static fastd_cipher_state_t *salsa20_init(const uint8_t *key) { +static fastd_cipher_state_t *salsa20_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t); memcpy(state->key, key, crypto_stream_salsa20_KEYBYTES); diff --git a/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c b/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c index dfc1e3a..19cb70f 100644 --- a/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c +++ b/src/crypto/cipher/salsa20/xmm/salsa20_xmm.c @@ -18,6 +18,8 @@ #include "../../../../cpuid.h" #include "../../../../crypto.h" +#include + /** The length of the key used by Salsa20 */ #define KEYBYTES 32 @@ -41,7 +43,9 @@ static bool salsa20_available(void) { } /** Initializes the cipher state */ -static fastd_cipher_state_t *salsa20_init(const uint8_t *key) { +static fastd_cipher_state_t *salsa20_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t); memcpy(state->key, key, KEYBYTES); diff --git a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c index be173b6..a897114 100644 --- a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c +++ b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c @@ -20,6 +20,8 @@ #include #endif +#include + /** The cipher state */ struct fastd_cipher_state { @@ -28,7 +30,9 @@ struct fastd_cipher_state { /** Initializes the cipher state */ -static fastd_cipher_state_t *salsa2012_init(const uint8_t *key) { +static fastd_cipher_state_t *salsa2012_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t); memcpy(state->key, key, crypto_stream_salsa2012_KEYBYTES); diff --git a/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c b/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c index eac032d..3e4cf02 100644 --- a/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c +++ b/src/crypto/cipher/salsa2012/xmm/salsa2012_xmm.c @@ -18,6 +18,8 @@ #include "../../../../cpuid.h" #include "../../../../crypto.h" +#include + /** The length of the key used by Salsa20/12 */ #define KEYBYTES 32 @@ -41,7 +43,9 @@ static bool salsa2012_available(void) { } /** Initializes the cipher state */ -static fastd_cipher_state_t *salsa2012_init(const uint8_t *key) { +static fastd_cipher_state_t *salsa2012_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_cipher_state_t *state = fastd_new(fastd_cipher_state_t); memcpy(state->key, key, KEYBYTES); diff --git a/src/crypto/mac/ghash/builtin/ghash_builtin.c b/src/crypto/mac/ghash/builtin/ghash_builtin.c index c839616..1498a90 100644 --- a/src/crypto/mac/ghash/builtin/ghash_builtin.c +++ b/src/crypto/mac/ghash/builtin/ghash_builtin.c @@ -14,6 +14,8 @@ #include "../../../../alloc.h" #include "../../../../crypto.h" +#include + /** MAC state used by this GHASH implmentation */ struct fastd_mac_state { @@ -54,7 +56,9 @@ static inline void mulH_a(fastd_block128_t *x, const fastd_mac_state_t *cstate) /** Initializes the MAC state with the unpacked key data */ -static fastd_mac_state_t *ghash_init(const uint8_t *key) { +static fastd_mac_state_t *ghash_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_mac_state_t *state = fastd_new_aligned(fastd_mac_state_t, 16); fastd_block128_t Hbase[4]; diff --git a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h index 271ba3b..89d2a96 100644 --- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h +++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq.h @@ -16,7 +16,7 @@ #include "../../../../crypto.h" -fastd_mac_state_t *fastd_ghash_pclmulqdq_init(const uint8_t *key); +fastd_mac_state_t *fastd_ghash_pclmulqdq_init(const uint8_t *key, int flags); bool fastd_ghash_pclmulqdq_digest( const fastd_mac_state_t *state, fastd_block128_t *out, const fastd_block128_t *in, size_t length); 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 14bb75c..072f0f2 100644 --- a/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c +++ b/src/crypto/mac/ghash/pclmulqdq/ghash_pclmulqdq_impl.c @@ -14,6 +14,8 @@ #include "../../../../alloc.h" #include "ghash_pclmulqdq.h" +#include + #include #include #include @@ -59,7 +61,9 @@ static inline __m128i byteswap(__m128i v) { /** Initializes the state used by this GHASH implementation */ -fastd_mac_state_t *fastd_ghash_pclmulqdq_init(const uint8_t *key) { +fastd_mac_state_t *fastd_ghash_pclmulqdq_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_mac_state_t *state = fastd_new_aligned(fastd_mac_state_t, 16); memcpy(&state->H, key, sizeof(__m128i)); diff --git a/src/crypto/mac/uhash/builtin/uhash_builtin.c b/src/crypto/mac/uhash/builtin/uhash_builtin.c index d21ee37..b127e62 100644 --- a/src/crypto/mac/uhash/builtin/uhash_builtin.c +++ b/src/crypto/mac/uhash/builtin/uhash_builtin.c @@ -16,6 +16,8 @@ #include "../../../../log.h" #include "../../../../util.h" +#include + /** MAC state used by this UHASH implmentation */ struct fastd_mac_state { @@ -78,7 +80,9 @@ static inline uint64_t mod_p36(uint64_t a) { /** Initializes the MAC state with the unpacked key data */ -static fastd_mac_state_t *uhash_init(const uint8_t *key) { +static fastd_mac_state_t *uhash_init(const uint8_t *key, UNUSED int flags) { + assert(flags == 0); + fastd_mac_state_t *state = fastd_new(fastd_mac_state_t); const uint32_t *key32 = (const uint32_t *)key; diff --git a/src/methods/cipher_test/cipher_test.c b/src/methods/cipher_test/cipher_test.c index 9bd87ec..61b9001 100644 --- a/src/methods/cipher_test/cipher_test.c +++ b/src/methods/cipher_test/cipher_test.c @@ -77,7 +77,7 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in fastd_method_common_init(&session->common, initiator); session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); - session->cipher_state = session->cipher->init(secret); + session->cipher_state = session->cipher->init(secret, 0); pr_warn("using cipher-test method; this method must be used for testing and benchmarks only"); diff --git a/src/methods/composed_gmac/composed_gmac.c b/src/methods/composed_gmac/composed_gmac.c index 2f545d6..0cbb2b5 100644 --- a/src/methods/composed_gmac/composed_gmac.c +++ b/src/methods/composed_gmac/composed_gmac.c @@ -120,10 +120,10 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); - session->cipher_state = session->cipher->init(secret); + session->cipher_state = session->cipher->init(secret, 0); session->gmac_cipher = fastd_cipher_get(method->gmac_cipher_info); - session->gmac_cipher_state = session->gmac_cipher->init(secret + method->cipher_info->key_length); + session->gmac_cipher_state = session->gmac_cipher->init(secret + method->cipher_info->key_length, 0); fastd_block128_t H; @@ -141,7 +141,7 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in } session->ghash = fastd_mac_get(method->ghash_info); - session->ghash_state = session->ghash->init(H.b); + session->ghash_state = session->ghash->init(H.b, 0); return session; } diff --git a/src/methods/composed_umac/composed_umac.c b/src/methods/composed_umac/composed_umac.c index 7930d76..dbd9670 100644 --- a/src/methods/composed_umac/composed_umac.c +++ b/src/methods/composed_umac/composed_umac.c @@ -114,14 +114,14 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); - session->cipher_state = session->cipher->init(secret); + session->cipher_state = session->cipher->init(secret, 0); session->umac_cipher = fastd_cipher_get(method->umac_cipher_info); - session->umac_cipher_state = session->umac_cipher->init(secret + method->cipher_info->key_length); + session->umac_cipher_state = session->umac_cipher->init(secret + method->cipher_info->key_length, 0); session->uhash = fastd_mac_get(method->uhash_info); - session->uhash_state = - session->uhash->init(secret + method->cipher_info->key_length + method->umac_cipher_info->key_length); + session->uhash_state = session->uhash->init( + secret + method->cipher_info->key_length + method->umac_cipher_info->key_length, 0); return session; } diff --git a/src/methods/generic_gmac/generic_gmac.c b/src/methods/generic_gmac/generic_gmac.c index 7c31713..68cc6da 100644 --- a/src/methods/generic_gmac/generic_gmac.c +++ b/src/methods/generic_gmac/generic_gmac.c @@ -95,7 +95,7 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); - session->cipher_state = session->cipher->init(secret); + session->cipher_state = session->cipher->init(secret, 0); static const fastd_block128_t zeroblock = {}; fastd_block128_t H; @@ -111,7 +111,7 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in } session->ghash = fastd_mac_get(method->ghash_info); - session->ghash_state = session->ghash->init(H.b); + session->ghash_state = session->ghash->init(H.b, 0); return session; } diff --git a/src/methods/generic_poly1305/generic_poly1305.c b/src/methods/generic_poly1305/generic_poly1305.c index c381146..7af4a58 100644 --- a/src/methods/generic_poly1305/generic_poly1305.c +++ b/src/methods/generic_poly1305/generic_poly1305.c @@ -93,7 +93,7 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in fastd_method_common_init(&session->common, initiator); session->method = method; session->cipher = fastd_cipher_get(session->method->cipher_info); - session->cipher_state = session->cipher->init(secret); + session->cipher_state = session->cipher->init(secret, 0); return session; } diff --git a/src/methods/generic_umac/generic_umac.c b/src/methods/generic_umac/generic_umac.c index f8f9a69..efbd455 100644 --- a/src/methods/generic_umac/generic_umac.c +++ b/src/methods/generic_umac/generic_umac.c @@ -88,10 +88,10 @@ method_session_init(const fastd_method_t *method, const uint8_t *secret, bool in session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); - session->cipher_state = session->cipher->init(secret); + session->cipher_state = session->cipher->init(secret, 0); session->uhash = fastd_mac_get(method->uhash_info); - session->uhash_state = session->uhash->init(secret + method->cipher_info->key_length); + session->uhash_state = session->uhash->init(secret + method->cipher_info->key_length, 0); return session; } diff --git a/test/benchmark-uhash.c b/test/benchmark-uhash.c index 77fa41a..c321a6d 100644 --- a/test/benchmark-uhash.c +++ b/test/benchmark-uhash.c @@ -47,7 +47,7 @@ int main(void) { return 77; } - fastd_mac_state_t *mac_state = fastd_mac_uhash_builtin.init(key); + fastd_mac_state_t *mac_state = fastd_mac_uhash_builtin.init(key, 0); run_benchmark(mac_state, 100000000, 20); run_benchmark(mac_state, 100000000, 100); diff --git a/test/test-uhash.c b/test/test-uhash.c index 35a0111..807ecc3 100644 --- a/test/test-uhash.c +++ b/test/test-uhash.c @@ -16,7 +16,7 @@ #include static int setup(void **state) { - fastd_mac_state_t *mac_state = fastd_mac_uhash_builtin.init(key); + fastd_mac_state_t *mac_state = fastd_mac_uhash_builtin.init(key, 0); *state = mac_state; return 0; }