From 7a08e6823ef636917a95dbc4928048723efe864d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 27 Nov 2013 20:53:00 +0100 Subject: Allow checking if a crypto algorithm is available at runtime --- src/crypto.h | 4 ++++ .../aes128_ctr/nacl/cipher_aes128_ctr_nacl.c | 2 ++ .../cipher/aes128_ctr/openssl/aes128_ctr_openssl.c | 2 ++ .../cipher/blowfish_ctr/builtin/blowfish_ctr.c | 2 ++ src/crypto/cipher/ciphers.c.in | 25 ++++++++++++++++------ src/crypto/cipher/null/memcpy/null_memcpy.c | 2 ++ src/crypto/cipher/salsa20/nacl/salsa20_nacl.c | 2 ++ src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c | 2 ++ src/crypto/mac/ghash/builtin/ghash_builtin.c | 2 ++ src/crypto/mac/macs.c.in | 25 ++++++++++++++++------ src/fastd.h | 5 ++++- 11 files changed, 58 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/crypto.h b/src/crypto.h index a11b460..968483e 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -36,6 +36,8 @@ struct fastd_cipher_info { }; struct fastd_cipher { + bool (*available)(void); + fastd_cipher_context_t* (*initialize)(fastd_context_t *ctx); fastd_cipher_state_t* (*init_state)(fastd_context_t *ctx, const fastd_cipher_context_t *cctx, const uint8_t *key); @@ -51,6 +53,8 @@ struct fastd_mac_info { }; struct fastd_mac { + bool (*available)(void); + fastd_mac_context_t* (*initialize)(fastd_context_t *ctx); fastd_mac_state_t* (*init_state)(fastd_context_t *ctx, const fastd_mac_context_t *mctx, const uint8_t *key); 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 e5f6379..783a9d1 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 @@ -67,6 +67,8 @@ static void aes128_ctr_free(fastd_context_t *ctx UNUSED, fastd_cipher_context_t } const fastd_cipher_t fastd_cipher_aes128_ctr_nacl = { + .available = fastd_true, + .initialize = aes128_ctr_initialize, .init_state = aes128_ctr_init_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 3ab12d5..37ed95c 100644 --- a/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c +++ b/src/crypto/cipher/aes128_ctr/openssl/aes128_ctr_openssl.c @@ -75,6 +75,8 @@ static void aes128_ctr_free(fastd_context_t *ctx UNUSED, fastd_cipher_context_t } const fastd_cipher_t fastd_cipher_aes128_ctr_openssl = { + .available = fastd_true, + .initialize = aes128_ctr_initialize, .init_state = aes128_ctr_init_state, diff --git a/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c b/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c index f3424b9..8c18203 100644 --- a/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c +++ b/src/crypto/cipher/blowfish_ctr/builtin/blowfish_ctr.c @@ -274,6 +274,8 @@ static void blowfish_ctr_free(fastd_context_t *ctx UNUSED, fastd_cipher_context_ } const fastd_cipher_t fastd_cipher_blowfish_ctr_builtin = { + .available = fastd_true, + .initialize = blowfish_ctr_initialize, .init_state = blowfish_ctr_init_state, diff --git a/src/crypto/cipher/ciphers.c.in b/src/crypto/cipher/ciphers.c.in index 508196e..b3c6b1b 100644 --- a/src/crypto/cipher/ciphers.c.in +++ b/src/crypto/cipher/ciphers.c.in @@ -50,9 +50,15 @@ static const cipher_entry_t ciphers[] = { @CIPHER_LIST@ const fastd_cipher_t** fastd_cipher_config_alloc(void) { const fastd_cipher_t **cipher_conf = calloc(array_size(ciphers), sizeof(const fastd_cipher_t*)); - size_t i; - for (i = 0; i < array_size(ciphers); i++) - cipher_conf[i] = ciphers[i].impls[0].impl; + size_t i, j; + for (i = 0; i < array_size(ciphers); i++) { + for (j = 0; ciphers[i].impls[j].impl; j++) { + if (ciphers[i].impls[j].impl->available()) + break; + } + + cipher_conf[i] = ciphers[i].impls[j].impl; + } return cipher_conf; } @@ -68,6 +74,9 @@ bool fastd_cipher_config(const fastd_cipher_t **cipher_conf, const char *name, c size_t j; for (j = 0; ciphers[i].impls[j].impl; j++) { if (!strcmp(ciphers[i].impls[j].name, impl)) { + if (!ciphers[i].impls[j].impl->available()) + return false; + cipher_conf[i] = ciphers[i].impls[j].impl; return true; } @@ -99,15 +108,17 @@ void fastd_cipher_free(fastd_context_t *ctx) { } const fastd_cipher_info_t* fastd_cipher_info_get_by_name(const char *name) { - size_t i; + size_t i, j; for (i = 0; i < array_size(ciphers); i++) { if (strcmp(ciphers[i].name, name)) continue; - if (!ciphers[i].impls[0].impl) - continue; + for (j = 0; ciphers[i].impls[j].impl; j++) { + if (ciphers[i].impls[j].impl->available()) + return ciphers[i].info; + } - return ciphers[i].info; + break; } return NULL; diff --git a/src/crypto/cipher/null/memcpy/null_memcpy.c b/src/crypto/cipher/null/memcpy/null_memcpy.c index 1784ac9..8c05b17 100644 --- a/src/crypto/cipher/null/memcpy/null_memcpy.c +++ b/src/crypto/cipher/null/memcpy/null_memcpy.c @@ -47,6 +47,8 @@ static void null_free(fastd_context_t *ctx UNUSED, fastd_cipher_context_t *cctx } const fastd_cipher_t fastd_cipher_null_memcpy = { + .available = fastd_true, + .initialize = null_initialize, .init_state = null_init_state, diff --git a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c index ddcc124..457e39c 100644 --- a/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c +++ b/src/crypto/cipher/salsa20/nacl/salsa20_nacl.c @@ -60,6 +60,8 @@ static void salsa20_free(fastd_context_t *ctx UNUSED, fastd_cipher_context_t *cc } const fastd_cipher_t fastd_cipher_salsa20_nacl = { + .available = fastd_true, + .initialize = salsa20_initialize, .init_state = salsa20_init_state, diff --git a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c index 36985a6..9619afe 100644 --- a/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c +++ b/src/crypto/cipher/salsa2012/nacl/salsa2012_nacl.c @@ -60,6 +60,8 @@ static void salsa2012_free(fastd_context_t *ctx UNUSED, fastd_cipher_context_t * } const fastd_cipher_t fastd_cipher_salsa2012_nacl = { + .available = fastd_true, + .initialize = salsa2012_initialize, .init_state = salsa2012_init_state, diff --git a/src/crypto/mac/ghash/builtin/ghash_builtin.c b/src/crypto/mac/ghash/builtin/ghash_builtin.c index cc47427..cc81e74 100644 --- a/src/crypto/mac/ghash/builtin/ghash_builtin.c +++ b/src/crypto/mac/ghash/builtin/ghash_builtin.c @@ -132,6 +132,8 @@ static void ghash_free(fastd_context_t *ctx UNUSED, fastd_mac_context_t *mctx UN } const fastd_mac_t fastd_mac_ghash_builtin = { + .available = fastd_true, + .initialize = ghash_initialize, .init_state = ghash_init_state, diff --git a/src/crypto/mac/macs.c.in b/src/crypto/mac/macs.c.in index 9952396..3587322 100644 --- a/src/crypto/mac/macs.c.in +++ b/src/crypto/mac/macs.c.in @@ -50,9 +50,15 @@ static const mac_entry_t macs[] = { @MAC_LIST@ const fastd_mac_t** fastd_mac_config_alloc(void) { const fastd_mac_t **mac_conf = calloc(array_size(macs), sizeof(const fastd_mac_t*)); - size_t i; - for (i = 0; i < array_size(macs); i++) - mac_conf[i] = macs[i].impls[0].impl; + size_t i, j; + for (i = 0; i < array_size(macs); i++) { + for (j = 0; macs[i].impls[j].impl; j++) { + if (macs[i].impls[j].impl->available()) + break; + } + + mac_conf[i] = macs[i].impls[j].impl; + } return mac_conf; } @@ -68,6 +74,9 @@ bool fastd_mac_config(const fastd_mac_t **mac_conf, const char *name, const char size_t j; for (j = 0; macs[i].impls[j].impl; j++) { if (!strcmp(macs[i].impls[j].name, impl)) { + if (!macs[i].impls[j].impl->available()) + return false; + mac_conf[i] = macs[i].impls[j].impl; return true; } @@ -99,15 +108,17 @@ void fastd_mac_free(fastd_context_t *ctx) { } const fastd_mac_info_t* fastd_mac_info_get_by_name(const char *name) { - size_t i; + size_t i, j; for (i = 0; i < array_size(macs); i++) { if (strcmp(macs[i].name, name)) continue; - if (!macs[i].impls[0].impl) - continue; + for (j = 0; macs[i].impls[j].impl; j++) { + if (macs[i].impls[j].impl->available()) + return macs[i].info; + } - return macs[i].info; + break; } return NULL; diff --git a/src/fastd.h b/src/fastd.h index 012d532..237af4b 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -358,7 +358,6 @@ static inline int fastd_rand(fastd_context_t *ctx, int min, int max) { return (r%(max-min) + min); } - #define FASTD_DEFAULT_LOG_LEVEL LL_VERBOSE @@ -519,4 +518,8 @@ static inline void xor_a(fastd_block128_t *x, const fastd_block128_t *a) { xor(x, x, a); } +static inline bool fastd_true(void) { + return true; +} + #endif /* _FASTD_FASTD_H_ */ -- cgit v1.2.3