From 20ee3b5a4f110f53a73746e18fc0eb0cbbb7845c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 2 Nov 2013 04:32:18 +0100 Subject: Implement the first step towards a more flexible way to support crypto methods --- CMakeLists.txt | 27 +-- src/CMakeLists.txt | 3 +- src/config.c | 64 +---- src/config.y | 2 - src/crypto.c | 53 ----- src/crypto.h | 21 -- src/crypto/CMakeLists.txt | 3 + src/crypto/cipher/CMakeLists.txt | 34 +++ src/crypto/cipher/aes128_ctr/CMakeLists.txt | 18 ++ src/crypto/cipher/aes128_ctr/nacl/CMakeLists.txt | 6 + .../aes128_ctr/nacl/cipher_aes128_ctr_nacl.c | 76 ++++++ src/crypto/cipher/ciphers.c.in | 103 ++++++++ src/crypto_linux.c | 262 --------------------- src/fastd.c | 15 +- src/fastd.h | 34 ++- src/methods/aes128_gcm/aes128_gcm.c | 20 +- src/methods/methods.c.in | 5 +- src/types.h | 17 +- 18 files changed, 309 insertions(+), 454 deletions(-) create mode 100644 src/crypto/CMakeLists.txt create mode 100644 src/crypto/cipher/CMakeLists.txt create mode 100644 src/crypto/cipher/aes128_ctr/CMakeLists.txt create mode 100644 src/crypto/cipher/aes128_ctr/nacl/CMakeLists.txt create mode 100644 src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c create mode 100644 src/crypto/cipher/ciphers.c.in delete mode 100644 src/crypto_linux.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a00617..371530b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,13 +30,10 @@ set(WITH_CMDLINE_OPERATION TRUE CACHE BOOL "Include support for setting options set(WITH_CMDLINE_COMMANDS TRUE CACHE BOOL "Include support for setting handler scripts (e.g. --on-up) on the command line") -set(WITH_CRYPTO_AES128CTR_NACL TRUE CACHE BOOL "Include the AES128-CTR implementation from the NaCl library") -set(WITH_CRYPTO_GHASH_BUILTIN TRUE CACHE BOOL "Include the built-in GHASH implementation") +set(WITH_CIPHER_AES128_CTR TRUE CACHE BOOL "Include the AES128-CTR cipher algorithm") +set(WITH_CIPHER_AES128_CTR_NACL TRUE CACHE BOOL "Include the AES128-CTR implementation from the NaCl library") -if(LINUX) - set(WITH_CRYPTO_AES128CTR_LINUX TRUE CACHE BOOL "Support using the AES128-CTR implementation in the Linux kernel") - set(WITH_CRYPTO_GHASH_LINUX TRUE CACHE BOOL "Support using the GHASH implementation in the Linux kernel") -endif(LINUX) +set(WITH_CRYPTO_GHASH_BUILTIN TRUE CACHE BOOL "Include the built-in GHASH implementation") set(WITH_METHOD_XSALSA20_POLY1305 TRUE CACHE BOOL "Include xsalsa20-poly1305 method") set(WITH_METHOD_AES128_GCM TRUE CACHE BOOL "Include aes128-gcm method") @@ -47,31 +44,21 @@ set(USE_LIBSODIUM FALSE CACHE BOOL "Use libsodium instead of NaCl") set(MAX_CONFIG_DEPTH 10 CACHE STRING "Maximum config include depth") -if(WITH_CRYPTO_AES128CTR_NACL OR WITH_CRYPTO_AES128CTR_LINUX) - set(WITH_CRYPTO_AES128CTR TRUE) -endif(WITH_CRYPTO_AES128CTR_NACL OR WITH_CRYPTO_AES128CTR_LINUX) - -if(WITH_CRYPTO_GHASH_BUILTIN OR WITH_CRYPTO_GHASH_LINUX) +if(WITH_CRYPTO_GHASH_BUILTIN) set(WITH_CRYPTO_GHASH TRUE) -endif(WITH_CRYPTO_GHASH_BUILTIN OR WITH_CRYPTO_GHASH_LINUX) +endif(WITH_CRYPTO_GHASH_BUILTIN) # Ensure the value is numeric math(EXPR MAX_CONFIG_DEPTH_NUM ${MAX_CONFIG_DEPTH}) -set(USE_CRYPTO_AES128CTR FALSE) set(USE_CRYPTO_GHASH FALSE) if(WITH_METHOD_AES128_GCM) - set(USE_CRYPTO_AES128CTR TRUE) set(USE_CRYPTO_GHASH TRUE) endif(WITH_METHOD_AES128_GCM) -if(USE_CRYPTO_AES128CTR AND NOT WITH_CRYPTO_AES128CTR) - MESSAGE(FATAL_ERROR "No AES128-CTR implementation was selected, but a selected method needs it.") -endif(USE_CRYPTO_AES128CTR AND NOT WITH_CRYPTO_AES128CTR) - if(USE_CRYPTO_GHASH AND NOT WITH_CRYPTO_GHASH) MESSAGE(FATAL_ERROR "No GHASH implementation was selected, but a selected method needs it.") endif(USE_CRYPTO_GHASH AND NOT WITH_CRYPTO_GHASH) @@ -95,7 +82,7 @@ set(NACL_LIBRARY_DIRS "") set(NACL_LIBRARIES "") set(NACL_LDFLAGS_OTHER "") -if(WITH_METHOD_XSALSA20_POLY1305 OR WITH_CRYPTO_AES128CTR_NACL) +if(WITH_METHOD_XSALSA20_POLY1305 OR WITH_CIPHER_AES128_CTR_NACL) if(USE_LIBSODIUM) pkg_check_modules(SODIUM REQUIRED libsodium) @@ -114,7 +101,7 @@ if(WITH_METHOD_XSALSA20_POLY1305 OR WITH_CRYPTO_AES128CTR_NACL) set(NACL_INCLUDE_DIRS "${NACL_INCLUDE_DIR}") set(NACL_LIBRARIES "${NACL_LIBRARY}") endif(USE_LIBSODIUM) -endif(WITH_METHOD_XSALSA20_POLY1305 OR WITH_CRYPTO_AES128CTR_NACL) +endif() if(WITH_CAPABILITIES) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e312f96..68b077e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ link_directories(${UECC_LIBRARY_DIRS} ${NACL_LIBRARY_DIRS}) add_subdirectory(protocols) add_subdirectory(methods) +add_subdirectory(crypto) BISON_TARGET(fastd_config_parse config.y ${CMAKE_CURRENT_BINARY_DIR}/config.yy.c) @@ -16,7 +17,6 @@ add_executable(fastd capabilities.c config.c crypto.c - crypto_linux.c handshake.c hkdf_sha256.c lex.c @@ -34,6 +34,7 @@ add_executable(fastd ${BISON_fastd_config_parse_OUTPUTS} ${PROTOCOL_SOURCES} ${METHOD_SOURCES} + ${CRYPTO_SOURCES} ) set_property(TARGET fastd PROPERTY COMPILE_FLAGS "${FASTD_CFLAGS}") set_property(TARGET fastd PROPERTY LINK_FLAGS "-pthread ${UECC_LDFLAGS_OTHER} ${NACL_LDFLAGS_OTHER}") diff --git a/src/config.c b/src/config.c index 407ef97..8cf0dcb 100644 --- a/src/config.c +++ b/src/config.c @@ -43,36 +43,8 @@ extern const fastd_protocol_t fastd_protocol_ec25519_fhmqvc; -#ifdef USE_CRYPTO_AES128CTR -#ifdef WITH_CRYPTO_AES128CTR_NACL -extern const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_nacl; -#endif -#ifdef WITH_CRYPTO_AES128CTR_LINUX -extern const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_linux; -#endif - -#ifdef WITH_CRYPTO_AES128CTR_NACL -static const fastd_crypto_aes128ctr_t *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_nacl; -#else -static const fastd_crypto_aes128ctr_t *fastd_crypto_aes128ctr_default = &fastd_crypto_aes128ctr_linux; -#endif - -#endif - #ifdef USE_CRYPTO_GHASH -#ifdef WITH_CRYPTO_GHASH_BUILTIN extern const fastd_crypto_ghash_t fastd_crypto_ghash_builtin; -#endif -#ifdef WITH_CRYPTO_GHASH_LINUX -extern const fastd_crypto_ghash_t fastd_crypto_ghash_linux; -#endif - -#ifdef WITH_CRYPTO_GHASH_BUILTIN -static const fastd_crypto_ghash_t *fastd_crypto_ghash_default = &fastd_crypto_ghash_builtin; -#else -static const fastd_crypto_ghash_t *fastd_crypto_ghash_default = &fastd_crypto_ghash_linux; -#endif - #endif static void default_config(fastd_config_t *conf) { @@ -102,16 +74,15 @@ static void default_config(fastd_config_t *conf) { conf->key_refresh = 3300; /* 55 minutes */ conf->key_refresh_splay = 300; /* 5 minutes */ -#ifdef USE_CRYPTO_AES128CTR - conf->crypto_aes128ctr = fastd_crypto_aes128ctr_default; -#endif #ifdef USE_CRYPTO_GHASH - conf->crypto_ghash = fastd_crypto_ghash_default; + conf->crypto_ghash = &fastd_crypto_ghash_builtin; #endif conf->peer_group = calloc(1, sizeof(fastd_peer_group_config_t)); conf->peer_group->name = strdup("default"); conf->peer_group->max_connections = -1; + + conf->ciphers = fastd_cipher_config_alloc(); } bool fastd_config_protocol(fastd_context_t *ctx UNUSED, fastd_config_t *conf, const char *name) { @@ -142,37 +113,12 @@ bool fastd_config_method(fastd_context_t *ctx, fastd_config_t *conf, const char } bool fastd_config_crypto(fastd_context_t *ctx UNUSED, fastd_config_t *conf UNUSED, const char *alg UNUSED, const char *impl UNUSED) { -#ifdef USE_CRYPTO_AES128CTR - if (!strcasecmp(alg, "aes128-ctr") || !strcasecmp(alg, "aes128") || !strcasecmp(alg, "aes-ctr") || !strcasecmp(alg, "aes")) { - if (!strcasecmp(impl, "default")) - conf->crypto_aes128ctr = fastd_crypto_aes128ctr_default; -#ifdef WITH_CRYPTO_AES128CTR_NACL - else if (!strcasecmp(impl, "nacl")) - conf->crypto_aes128ctr = &fastd_crypto_aes128ctr_nacl; -#endif -#ifdef WITH_CRYPTO_AES128CTR_LINUX - else if (!strcasecmp(impl, "linux")) - conf->crypto_aes128ctr = &fastd_crypto_aes128ctr_linux; -#endif - else - return false; - - return true; - } - else -#endif #ifdef USE_CRYPTO_GHASH if (!strcasecmp(alg, "ghash")) { if (!strcasecmp(impl, "default")) - conf->crypto_ghash = fastd_crypto_ghash_default; -#ifdef WITH_CRYPTO_GHASH_BUILTIN + conf->crypto_ghash = &fastd_crypto_ghash_builtin; else if (!strcasecmp(impl, "builtin")) conf->crypto_ghash = &fastd_crypto_ghash_builtin; -#endif -#ifdef WITH_CRYPTO_GHASH_LINUX - else if (!strcasecmp(impl, "linux")) - conf->crypto_ghash = &fastd_crypto_ghash_linux; -#endif else return false; @@ -725,6 +671,8 @@ void fastd_config_release(fastd_context_t *ctx, fastd_config_t *conf) { fastd_string_stack_free(conf->methods); + fastd_cipher_config_free(conf->ciphers); + free(conf->user); free(conf->group); free(conf->groups); diff --git a/src/config.y b/src/config.y index bb9884e..ffc719d 100644 --- a/src/config.y +++ b/src/config.y @@ -128,8 +128,6 @@ #include #include - #include - #include void fastd_config_error(YYLTYPE *loc, fastd_context_t *ctx, fastd_config_t *conf, const char *filename, int depth, const char *s); } diff --git a/src/crypto.c b/src/crypto.c index 802c5da..1583349 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -28,59 +28,6 @@ #include "crypto.h" -#ifdef USE_CRYPTO_AES128CTR -#ifdef WITH_CRYPTO_AES128CTR_NACL - -#include - - -struct fastd_crypto_aes128ctr_state { - fastd_buffer_t d; -}; - - -static fastd_crypto_aes128ctr_context_t* aes128ctr_init(fastd_context_t *ctx UNUSED) { - return (fastd_crypto_aes128ctr_context_t*)1; -} - -static fastd_crypto_aes128ctr_state_t* aes128ctr_set_key(fastd_context_t *ctx, const fastd_crypto_aes128ctr_context_t *cctx UNUSED, const fastd_block128_t *key) { - fastd_crypto_aes128ctr_state_t *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state_t)); - - cstate->d = fastd_buffer_alloc(ctx, crypto_stream_aes128ctr_BEFORENMBYTES, 0, 0); - crypto_stream_aes128ctr_beforenm(cstate->d.data, key->b); - - return cstate; -} - -static bool aes128ctr_crypt(fastd_context_t *ctx UNUSED, const fastd_crypto_aes128ctr_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const fastd_block128_t *iv) { - crypto_stream_aes128ctr_xor_afternm(out->b, in->b, len, iv->b, cstate->d.data); - return true; -} - -static void aes128ctr_free_state(fastd_context_t *ctx UNUSED, fastd_crypto_aes128ctr_state_t *cstate) { - if (cstate) { - fastd_buffer_free(cstate->d); - free(cstate); - } -} - -static void aes128ctr_free(fastd_context_t *ctx UNUSED, fastd_crypto_aes128ctr_context_t *cctx UNUSED) { -} - -const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_nacl = { - .name = "nacl", - - .init = aes128ctr_init, - .set_key = aes128ctr_set_key, - .crypt = aes128ctr_crypt, - - .free_state = aes128ctr_free_state, - .free = aes128ctr_free, -}; - -#endif -#endif - #ifdef USE_CRYPTO_GHASH #ifdef WITH_CRYPTO_GHASH_BUILTIN diff --git a/src/crypto.h b/src/crypto.h index 7894f85..b954651 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -29,27 +29,6 @@ #include "types.h" -#include - - -typedef union fastd_block128 { - uint8_t b[16]; - uint64_t qw[2]; -} __attribute__((aligned(16))) fastd_block128_t; - - -#ifdef USE_CRYPTO_AES128CTR -struct fastd_crypto_aes128ctr { - const char *name; - - fastd_crypto_aes128ctr_context_t* (*init)(fastd_context_t *ctx); - fastd_crypto_aes128ctr_state_t* (*set_key)(fastd_context_t *ctx, const fastd_crypto_aes128ctr_context_t *cctx, const fastd_block128_t *key); - bool (*crypt)(fastd_context_t *ctx, const fastd_crypto_aes128ctr_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const fastd_block128_t *iv); - - void (*free_state)(fastd_context_t *ctx, fastd_crypto_aes128ctr_state_t *cstate); - void (*free)(fastd_context_t *ctx, fastd_crypto_aes128ctr_context_t *cctx); -}; -#endif #ifdef USE_CRYPTO_GHASH struct fastd_crypto_ghash { diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt new file mode 100644 index 0000000..1c78e03 --- /dev/null +++ b/src/crypto/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(cipher) + +set(CRYPTO_SOURCES "${CIPHER_SOURCES}" PARENT_SCOPE) diff --git a/src/crypto/cipher/CMakeLists.txt b/src/crypto/cipher/CMakeLists.txt new file mode 100644 index 0000000..00b8560 --- /dev/null +++ b/src/crypto/cipher/CMakeLists.txt @@ -0,0 +1,34 @@ +set(CIPHERS "") + +if(WITH_CIPHER_AES128_CTR) + list(APPEND CIPHERS aes128_ctr) +endif(WITH_CIPHER_AES128_CTR) + +set(CIPHER_SOURCES "${CMAKE_CURRENT_BINARY_DIR}/ciphers.c") + +set(CIPHER_DEFINITIONS "") +set(CIPHER_IMPLS "") +set(CIPHER_LIST "") + +foreach(cipher ${CIPHERS}) + add_subdirectory(${cipher}) + + list(APPEND CIPHER_SOURCES ${IMPL_SOURCES}) + + set(CIPHER_LIST "${CIPHER_LIST}\n{\"${CIPHER_NAME}\", cipher_${cipher}_impls},") + set(CIPHER_IMPLS "${CIPHER_IMPLS}\nstatic const fastd_cipher_t *const cipher_${cipher}_impls[] = {") + + foreach(impl ${IMPLS}) + set(CIPHER_DEFINITIONS "${CIPHER_DEFINITIONS}\nextern const fastd_cipher_t fastd_cipher_${cipher}_${impl};") + set(CIPHER_IMPLS "${CIPHER_IMPLS}&fastd_cipher_${cipher}_${impl}, ") + endforeach(impl) + + set(CIPHER_IMPLS "${CIPHER_IMPLS}NULL};") + +endforeach(cipher) + + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ciphers.c.in ${CMAKE_CURRENT_BINARY_DIR}/ciphers.c) + + +set(CIPHER_SOURCES "${CIPHER_SOURCES}" PARENT_SCOPE) diff --git a/src/crypto/cipher/aes128_ctr/CMakeLists.txt b/src/crypto/cipher/aes128_ctr/CMakeLists.txt new file mode 100644 index 0000000..6237a7c --- /dev/null +++ b/src/crypto/cipher/aes128_ctr/CMakeLists.txt @@ -0,0 +1,18 @@ +set(IMPLS "") + +if(WITH_CIPHER_AES128_CTR_NACL) + list(APPEND IMPLS nacl) +endif(WITH_CIPHER_AES128_CTR_NACL) + +set(IMPL_SOURCES "") + +foreach(impl ${IMPLS}) + add_subdirectory(${impl}) + + list(APPEND IMPL_SOURCES $) +endforeach(impl) + + +set(CIPHER_NAME "aes128-ctr" PARENT_SCOPE) +set(IMPLS "${IMPLS}" PARENT_SCOPE) +set(IMPL_SOURCES "${IMPL_SOURCES}" PARENT_SCOPE) diff --git a/src/crypto/cipher/aes128_ctr/nacl/CMakeLists.txt b/src/crypto/cipher/aes128_ctr/nacl/CMakeLists.txt new file mode 100644 index 0000000..50e4b4c --- /dev/null +++ b/src/crypto/cipher/aes128_ctr/nacl/CMakeLists.txt @@ -0,0 +1,6 @@ +include_directories(${FASTD_SOURCE_DIR}/src ${FASTD_BINARY_DIR} ${NACL_INCLUDE_DIRS}) + +add_library(cipher_aes128_ctr_nacl OBJECT + cipher_aes128_ctr_nacl.c +) +set_property(TARGET method_xsalsa20_poly1305 PROPERTY COMPILE_FLAGS "${FASTD_CFLAGS}") 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 new file mode 100644 index 0000000..f63e46f --- /dev/null +++ b/src/crypto/cipher/aes128_ctr/nacl/cipher_aes128_ctr_nacl.c @@ -0,0 +1,76 @@ +/* + Copyright (c) 2012-2013, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "../../../../fastd.h" +#include + + +struct fastd_cipher_state { + fastd_buffer_t d; +}; + + +static fastd_cipher_context_t* aes128_ctr_initialize(fastd_context_t *ctx UNUSED) { + return NULL; +} + +static fastd_cipher_state_t* aes128_ctr_init_state(fastd_context_t *ctx, const fastd_cipher_context_t *cctx UNUSED, const uint8_t *key) { + fastd_block128_t k; + memcpy(k.b, key, sizeof(fastd_block128_t)); + + fastd_cipher_state_t *state = malloc(sizeof(fastd_cipher_state_t)); + + state->d = fastd_buffer_alloc(ctx, crypto_stream_aes128ctr_BEFORENMBYTES, 0, 0); + crypto_stream_aes128ctr_beforenm(state->d.data, 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 fastd_block128_t *iv) { + crypto_stream_aes128ctr_xor_afternm(out->b, in->b, len, iv->b, state->d.data); + return true; +} + +static void aes128_ctr_free_state(fastd_context_t *ctx UNUSED, fastd_cipher_state_t *state) { + if (state) { + fastd_buffer_free(state->d); + free(state); + } +} + +static void aes128_ctr_free(fastd_context_t *ctx UNUSED, fastd_cipher_context_t *cctx UNUSED) { +} + +const fastd_cipher_t fastd_cipher_aes128_ctr_nacl = { + .name = "nacl", + + .initialize = aes128_ctr_initialize, + .init_state = aes128_ctr_init_state, + .crypt = aes128_ctr_crypt, + + .free_state = aes128_ctr_free_state, + .free = aes128_ctr_free, +}; diff --git a/src/crypto/cipher/ciphers.c.in b/src/crypto/cipher/ciphers.c.in new file mode 100644 index 0000000..ee710f8 --- /dev/null +++ b/src/crypto/cipher/ciphers.c.in @@ -0,0 +1,103 @@ +/* + Copyright (c) 2012-2013, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include + + +@CIPHER_DEFINITIONS@ + +typedef struct cipher_impl_list { + const char *name; + const fastd_cipher_t *const *impls; +} cipher_impl_list_t; + +@CIPHER_IMPLS@ + +static const cipher_impl_list_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]; + + return cipher_conf; +} + +void fastd_cipher_config_free(const fastd_cipher_t **cipher_conf) { + free(cipher_conf); +} + +bool fastd_cipher_config(const fastd_cipher_t **cipher_conf, const char *name, const char *impl) { + size_t i; + for (i = 0; i < array_size(ciphers); i++) { + if (!strcmp(ciphers[i].name, name)) { + size_t j; + for (j = 0; ciphers[i].impls[j]; j++) { + if (!strcmp(ciphers[i].impls[j]->name, impl)) { + cipher_conf[i] = ciphers[i].impls[j]; + return true; + } + } + + return false; + } + } + + return false; +} + +void fastd_cipher_init(fastd_context_t *ctx) { + ctx->cipher_contexts = calloc(array_size(ciphers), sizeof(fastd_cipher_context_t*)); + + size_t i; + for (i = 0; i < array_size(ciphers); i++) + ctx->cipher_contexts[i] = ctx->conf->ciphers[i]->initialize(ctx); +} + +void fastd_cipher_free(fastd_context_t *ctx) { + size_t i; + for (i = 0; i < array_size(ciphers); i++) + ctx->conf->ciphers[i]->free(ctx, ctx->cipher_contexts[i]); + + free(ctx->cipher_contexts); +} + +bool fastd_cipher_get_by_name(fastd_context_t *ctx, const char *name, const fastd_cipher_t **cipher, fastd_cipher_context_t **cctx) { + size_t i; + for (i = 0; i < array_size(ciphers); i++) { + if (!strcmp(ciphers[i].name, name)) { + *cipher = ctx->conf->ciphers[i]; + *cctx = ctx->cipher_contexts[i]; + return true; + } + } + + return false; +} diff --git a/src/crypto_linux.c b/src/crypto_linux.c deleted file mode 100644 index 2cd7145..0000000 --- a/src/crypto_linux.c +++ /dev/null @@ -1,262 +0,0 @@ -/* - Copyright (c) 2012-2013, Matthias Schiffer - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - - -#include "fastd.h" -#include "crypto.h" - - -#if (defined(USE_CRYPTO_AES128CTR) && defined(WITH_CRYPTO_AES128CTR_LINUX)) || (defined(USE_CRYPTO_GHASH) && defined(WITH_CRYPTO_GHASH_LINUX)) - -#include -#include - -#ifndef SOL_ALG -#define SOL_ALG 279 -#endif - -#endif - - -#ifdef USE_CRYPTO_AES128CTR -#ifdef WITH_CRYPTO_AES128CTR_LINUX - -struct fastd_crypto_aes128ctr_context { - int fd; -}; - -struct fastd_crypto_aes128ctr_state { - int fd; -}; - -static fastd_crypto_aes128ctr_context_t* aes128ctr_init(fastd_context_t *ctx) { - int fd = socket(AF_ALG, SOCK_SEQPACKET, 0); - if (fd < 0) - goto error; - - struct sockaddr_alg sa = {}; - sa.salg_family = AF_ALG; - strcpy((char*)sa.salg_type, "skcipher"); - strcpy((char*)sa.salg_name, "ctr(aes)"); - if (bind(fd, (struct sockaddr*)&sa, sizeof(sa)) < 0) - goto error; - - fastd_crypto_aes128ctr_context_t *cctx = malloc(sizeof(fastd_crypto_aes128ctr_context_t)); - cctx->fd = fd; - return cctx; - - error: - if (fd >= 0) - close(fd); - - pr_error(ctx, "no kernel support for AES-CTR was found"); - return NULL; -} - -static fastd_crypto_aes128ctr_state_t* aes128ctr_set_key(fastd_context_t *ctx, const fastd_crypto_aes128ctr_context_t *cctx, const fastd_block128_t *key) { - if (setsockopt(cctx->fd, SOL_ALG, ALG_SET_KEY, key->b, 16) < 0) { - pr_error_errno(ctx, "aes128ctr_set_key(linux): setsockopt"); - return NULL; - } - - int fd = accept(cctx->fd, NULL, NULL); - - if (fd < 0) { - pr_error_errno(ctx, "aes128ctr_set_key(linux): accept"); - return NULL; - } - - fastd_crypto_aes128ctr_state_t *cstate = malloc(sizeof(fastd_crypto_aes128ctr_state_t)); - cstate->fd = fd; - - return cstate; -} - -static bool aes128ctr_crypt(fastd_context_t *ctx, const fastd_crypto_aes128ctr_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const fastd_block128_t *iv) { - if (!len) - return false; - - struct iovec vec = { .iov_base = (void*)in, .iov_len = len }; - - static const size_t cmsglen = sizeof(struct cmsghdr)+sizeof(struct af_alg_iv)+16; - struct cmsghdr *cmsg = alloca(cmsglen); - cmsg->cmsg_len = cmsglen; - cmsg->cmsg_level = SOL_ALG; - cmsg->cmsg_type = ALG_SET_IV; - - struct af_alg_iv *alg_iv = (void*)CMSG_DATA(cmsg); - alg_iv->ivlen = 16; - memcpy(alg_iv->iv, iv, 16); - - struct msghdr msg = { - .msg_iov = &vec, - .msg_iovlen = 1, - .msg_control = cmsg, - .msg_controllen = cmsglen - }; - - if (sendmsg(cstate->fd, &msg, 0) < 0) { - pr_error_errno(ctx, "aes128ctr_crypt(linux): sendmsg"); - return false; - } - - msg.msg_control = NULL; - msg.msg_controllen = 0; - vec.iov_base = out; - - if (recvmsg(cstate->fd, &msg, 0) < 0) { - pr_error_errno(ctx, "aes128ctr_crypt(linux): recvmsg"); - return false; - } - - return true; -} - -static void aes128ctr_free_state(fastd_context_t *ctx UNUSED, fastd_crypto_aes128ctr_state_t *cstate) { - if (cstate) { - close(cstate->fd); - free(cstate); - } -} - -static void aes128ctr_free(fastd_context_t *ctx UNUSED, fastd_crypto_aes128ctr_context_t *cctx) { - if (cctx) { - close(cctx->fd); - free(cctx); - } -} - -const fastd_crypto_aes128ctr_t fastd_crypto_aes128ctr_linux = { - .name = "linux", - - .init = aes128ctr_init, - .set_key = aes128ctr_set_key, - .crypt = aes128ctr_crypt, - - .free_state = aes128ctr_free_state, - .free = aes128ctr_free, -}; - -#endif -#endif - -#ifdef USE_CRYPTO_GHASH -#ifdef WITH_CRYPTO_GHASH_LINUX - -struct fastd_crypto_ghash_context { - int fd; -}; - -struct fastd_crypto_ghash_state { - int fd; -}; - -static fastd_crypto_ghash_context_t* ghash_init(fastd_context_t *ctx) { - int fd = socket(AF_ALG, SOCK_SEQPACKET, 0); - if (fd < 0) - goto error; - - struct sockaddr_alg sa = {}; - sa.salg_family = AF_ALG; - strcpy((char*)sa.salg_type, "hash"); - strcpy((char*)sa.salg_name, "ghash"); - if (bind(fd, (struct sockaddr*)&sa, sizeof(sa)) < 0) - goto error; - - fastd_crypto_ghash_context_t *cctx = malloc(sizeof(fastd_crypto_ghash_context_t)); - cctx->fd = fd; - return cctx; - - error: - if (fd >= 0) - close(fd); - - pr_error(ctx, "no kernel support for GHASH was found"); - return NULL; -} - -static fastd_crypto_ghash_state_t* ghash_set_h(fastd_context_t *ctx, const fastd_crypto_ghash_context_t *cctx, const fastd_block128_t *h) { - if (setsockopt(cctx->fd, SOL_ALG, ALG_SET_KEY, h, 16) < 0) { - pr_error_errno(ctx, "ghash_set_h(linux): setsockopt"); - return NULL; - } - - int fd = accept(cctx->fd, NULL, NULL); - - if (fd < 0) { - pr_error_errno(ctx, "ghash_set_h(linux): accept"); - return NULL; - } - - fastd_crypto_ghash_state_t *cstate = malloc(sizeof(fastd_crypto_ghash_state_t)); - cstate->fd = fd; - - return cstate; -} - -static bool ghash_hash(fastd_context_t *ctx, const fastd_crypto_ghash_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t n_blocks) { - if (!n_blocks) - return false; - - if (write(cstate->fd, in, n_blocks*16) < 0) { - pr_error_errno(ctx, "ghash_hash(linux): write"); - return false; - } - - if (read(cstate->fd, out, 16) < 16) { - pr_error_errno(ctx, "ghash_hash(linux): read"); - return false; - } - - return true; -} - -static void ghash_free_state(fastd_context_t *ctx UNUSED, fastd_crypto_ghash_state_t *cstate) { - if (cstate) { - close(cstate->fd); - free(cstate); - } -} - -static void ghash_free(fastd_context_t *ctx UNUSED, fastd_crypto_ghash_context_t *cctx) { - if (cctx) { - close(cctx->fd); - free(cctx); - } -} - -const fastd_crypto_ghash_t fastd_crypto_ghash_linux = { - .name = "linux", - - .init = ghash_init, - .set_h = ghash_set_h, - .hash = ghash_hash, - - .free_state = ghash_free_state, - .free = ghash_free, -}; - -#endif -#endif diff --git a/src/fastd.c b/src/fastd.c index ea197d3..534a30e 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -145,12 +145,8 @@ static void close_log(fastd_context_t *ctx) { closelog(); } -static void crypto_init(fastd_context_t *ctx UNUSED) { -#ifdef USE_CRYPTO_AES128CTR - ctx->crypto_aes128ctr = ctx->conf->crypto_aes128ctr->init(ctx); - if (!ctx->crypto_aes128ctr) - exit_error(ctx, "Unable to initialize AES128-CTR implementation"); -#endif +static void crypto_init(fastd_context_t *ctx) { + fastd_cipher_init(ctx); #ifdef USE_CRYPTO_GHASH ctx->crypto_ghash = ctx->conf->crypto_ghash->init(ctx); @@ -160,15 +156,12 @@ static void crypto_init(fastd_context_t *ctx UNUSED) { } static void crypto_free(fastd_context_t *ctx UNUSED) { -#ifdef USE_CRYPTO_AES128CTR - ctx->conf->crypto_aes128ctr->free(ctx, ctx->crypto_aes128ctr); - ctx->crypto_aes128ctr = NULL; -#endif - #ifdef USE_CRYPTO_GHASH ctx->conf->crypto_ghash->free(ctx, ctx->crypto_ghash); ctx->crypto_ghash = NULL; #endif + + fastd_cipher_free(ctx); } diff --git a/src/fastd.h b/src/fastd.h index 9c168af..be4cf11 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -100,6 +100,17 @@ struct fastd_method { bool (*decrypt)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in); }; +struct fastd_cipher { + const char *name; + + 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); + bool (*crypt)(fastd_context_t *ctx, const fastd_cipher_state_t *cstate, fastd_block128_t *out, const fastd_block128_t *in, size_t len, const fastd_block128_t *iv); + + void (*free_state)(fastd_context_t *ctx, fastd_cipher_state_t *cstate); + void (*free)(fastd_context_t *ctx, fastd_cipher_context_t *cctx); +}; + union fastd_peer_address { struct sockaddr sa; struct sockaddr_in in; @@ -225,9 +236,8 @@ struct fastd_config { unsigned key_refresh; unsigned key_refresh_splay; -#ifdef USE_CRYPTO_AES128CTR - const fastd_crypto_aes128ctr_t *crypto_aes128ctr; -#endif + const fastd_cipher_t **ciphers; + #ifdef USE_CRYPTO_GHASH const fastd_crypto_ghash_t *crypto_ghash; #endif @@ -305,9 +315,8 @@ struct fastd_context { fastd_stats_t tx_dropped; fastd_stats_t tx_error; -#ifdef USE_CRYPTO_AES128CTR - fastd_crypto_aes128ctr_context_t *crypto_aes128ctr; -#endif + fastd_cipher_context_t **cipher_contexts; + #ifdef USE_CRYPTO_GHASH fastd_crypto_ghash_context_t *crypto_ghash; #endif @@ -340,8 +349,8 @@ void fastd_socket_close(fastd_context_t *ctx, fastd_socket_t *sock); void fastd_socket_error(fastd_context_t *ctx, fastd_socket_t *sock); void fastd_setfd(const fastd_context_t *ctx, int fd, int set, int unset); -void fastd_setfl(const fastd_context_t *ctx, int fd, int set, int unset); - +void fastd_setfl(const fastd_context_t *ctx, int fd, int set, int unset) +; void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t *remote); int fastd_vsnprintf(const fastd_context_t *ctx, char *buffer, size_t size, const char *format, va_list ap); @@ -352,6 +361,14 @@ bool fastd_read_config(fastd_context_t *ctx, fastd_config_t *conf, const char *f const fastd_method_t* fastd_method_get_by_name(const char *name); +const fastd_cipher_t** fastd_cipher_config_alloc(void); +void fastd_cipher_config_free(const fastd_cipher_t **cipher_conf); +bool fastd_cipher_config(const fastd_cipher_t **cipher_conf, const char *name, const char *impl); + +void fastd_cipher_init(fastd_context_t *ctx); +void fastd_cipher_free(fastd_context_t *ctx); +bool fastd_cipher_get_by_name(fastd_context_t *ctx, const char *name, const fastd_cipher_t **cipher, fastd_cipher_context_t **cctx); + bool fastd_config_protocol(fastd_context_t *ctx, fastd_config_t *conf, const char *name); bool fastd_config_method(fastd_context_t *ctx, fastd_config_t *conf, const char *name); bool fastd_config_crypto(fastd_context_t *ctx, fastd_config_t *conf, const char *alg, const char *impl); @@ -408,6 +425,7 @@ static inline int fastd_rand(fastd_context_t *ctx, int min, int max) { const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) +#define array_size(array) (sizeof(array)/sizeof((array)[0])) static inline size_t block_count(size_t l, size_t a) { return (l+a-1)/a; diff --git a/src/methods/aes128_gcm/aes128_gcm.c b/src/methods/aes128_gcm/aes128_gcm.c index 297031b..20f393f 100644 --- a/src/methods/aes128_gcm/aes128_gcm.c +++ b/src/methods/aes128_gcm/aes128_gcm.c @@ -32,7 +32,10 @@ struct fastd_method_session_state { fastd_method_common_t common; - fastd_crypto_aes128ctr_state_t *cstate_aes128ctr; + const fastd_cipher_t *aes128_ctr; + fastd_cipher_context_t *aes128_ctr_ctx; + fastd_cipher_state_t *aes128_ctr_state; + fastd_crypto_ghash_state_t *cstate_ghash; }; @@ -68,14 +71,15 @@ static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, c fastd_method_common_init(ctx, &session->common, initiator); - fastd_block128_t key; - memcpy(key.b, secret, sizeof(fastd_block128_t)); - session->cstate_aes128ctr = ctx->conf->crypto_aes128ctr->set_key(ctx, ctx->crypto_aes128ctr, &key); + if (!fastd_cipher_get_by_name(ctx, "aes128-ctr", &session->aes128_ctr, &session->aes128_ctr_ctx)) + exit_bug(ctx, "aes128-gcm: can't instanciate aes128-ctr"); + + session->aes128_ctr_state = session->aes128_ctr->init_state(ctx, session->aes128_ctr_ctx, secret); static const fastd_block128_t zeroblock = {}; fastd_block128_t H; - ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, &H, &zeroblock, sizeof(fastd_block128_t), &zeroblock); + session->aes128_ctr->crypt(ctx, session->aes128_ctr_state, &H, &zeroblock, sizeof(fastd_block128_t), &zeroblock); session->cstate_ghash = ctx->conf->crypto_ghash->set_h(ctx, ctx->crypto_ghash, &H); @@ -107,7 +111,7 @@ static void method_session_superseded(fastd_context_t *ctx, fastd_method_session static void method_session_free(fastd_context_t *ctx, fastd_method_session_state_t *session) { if (session) { - ctx->conf->crypto_aes128ctr->free_state(ctx, session->cstate_aes128ctr); + session->aes128_ctr->free_state(ctx, session->aes128_ctr_state); ctx->conf->crypto_ghash->free_state(ctx, session->cstate_ghash); secure_memzero(session, sizeof(fastd_method_session_state_t)); @@ -145,7 +149,7 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast fastd_block128_t *outblocks = out->data; fastd_block128_t sig; - bool ok = ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), &nonce); + bool ok = session->aes128_ctr->crypt(ctx, session->aes128_ctr_state, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), &nonce); if (ok) { if (tail_len) @@ -201,7 +205,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_block128_t *outblocks = out->data; fastd_block128_t sig; - bool ok = ctx->conf->crypto_aes128ctr->crypt(ctx, session->cstate_aes128ctr, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), &nonce); + bool ok = session->aes128_ctr->crypt(ctx, session->aes128_ctr_state, outblocks, inblocks, n_blocks*sizeof(fastd_block128_t), &nonce); if (ok) { if (tail_len) diff --git a/src/methods/methods.c.in b/src/methods/methods.c.in index 6931edd..8f5bb10 100644 --- a/src/methods/methods.c.in +++ b/src/methods/methods.c.in @@ -30,13 +30,12 @@ @METHOD_DEFINITIONS@ static const fastd_method_t *const methods[] = { @METHOD_LIST@ - NULL }; const fastd_method_t* fastd_method_get_by_name(const char *name) { - int i; - for (i = 0; methods[i]; i++) { + size_t i; + for (i = 0; i < array_size(methods); i++) { if (!strcmp(methods[i]->name, name)) return methods[i]; } diff --git a/src/types.h b/src/types.h index a2b7d38..894b2af 100644 --- a/src/types.h +++ b/src/types.h @@ -36,6 +36,7 @@ #include #include +#include #define UNUSED __attribute__((unused)) @@ -113,6 +114,7 @@ typedef struct fastd_context fastd_context_t; typedef struct fastd_protocol fastd_protocol_t; typedef struct fastd_method fastd_method_t; +typedef struct fastd_cipher fastd_cipher_t; typedef struct fastd_handshake fastd_handshake_t; @@ -121,14 +123,17 @@ typedef struct fastd_string_stack fastd_string_stack_t; typedef struct fastd_resolve_return fastd_resolve_return_t; -#ifdef USE_CRYPTO_AES128CTR -typedef struct fastd_crypto_aes128ctr fastd_crypto_aes128ctr_t; -#endif #ifdef USE_CRYPTO_GHASH typedef struct fastd_crypto_ghash fastd_crypto_ghash_t; #endif +typedef union fastd_block128 { + uint8_t b[16]; + uint64_t qw[2]; +} __attribute__((aligned(16))) fastd_block128_t; + + /* May be defined by the protocol/method/crypto implementations however they like */ typedef struct fastd_protocol_config fastd_protocol_config_t; typedef struct fastd_protocol_state fastd_protocol_state_t; @@ -137,10 +142,8 @@ typedef struct fastd_protocol_peer_state fastd_protocol_peer_state_t; typedef struct fastd_method_session_state fastd_method_session_state_t; -#ifdef USE_CRYPTO_AES128CTR -typedef struct fastd_crypto_aes128ctr_context fastd_crypto_aes128ctr_context_t; -typedef struct fastd_crypto_aes128ctr_state fastd_crypto_aes128ctr_state_t; -#endif +typedef struct fastd_cipher_context fastd_cipher_context_t; +typedef struct fastd_cipher_state fastd_cipher_state_t; #ifdef USE_CRYPTO_GHASH typedef struct fastd_crypto_ghash_context fastd_crypto_ghash_context_t; -- cgit v1.2.3