From cfbd0db491f012ff945ab629442610eb19c20c58 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sat, 7 Dec 2019 22:34:33 +0100 Subject: [PATCH] btstack_crypto: use rijndael aes128 if ENABLE_SOFTWARE_AES128 [a2472] --- src/btstack_crypto.c | 39 +++++++++++++++++++++++++++++++++------ src/btstack_crypto.h | 11 +++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/btstack_crypto.c b/src/btstack_crypto.c index df2cbe40f..ab09c12a4 100644 --- a/src/btstack_crypto.c +++ b/src/btstack_crypto.c @@ -46,6 +46,30 @@ #include "btstack_util.h" #include "hci.h" +// +// AES128 Configuration +// + +// By default, AES128 is computed by Bluetooth Controller using HCI Command/Event asynchronously +// as fallback/alternative, a software implementation can be used +// configure ECC implementations +#if defined(HAVE_AES128) && defined(ENABLE_SOFTWARE_AES128) +#error "If you have custom AES128 implementation (HAVE_AES128), please disable software AES128 (ENABLE_SOFTWARE_AES128) in bstack_config.h" +#endif + +#ifdef ENABLE_SOFTWARE_AES128 +#define HAVE_AES128 +#include "rijndael.h" +#endif + +#ifdef HAVE_AES128 +#define USE_BTSTACK_AES128 +#endif + +// +// ECC Configuration +// + // backwards-compatitility ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONS -> ENABLE_MICRO_ECC_P256 #if defined(ENABLE_MICRO_ECC_FOR_LE_SECURE_CONNECTIONS) && !defined(ENABLE_MICRO_ECC_P256) #define ENABLE_MICRO_ECC_P256 @@ -78,12 +102,6 @@ #define ENABLE_ECC_P256 #endif -// Software AES128 -#ifdef HAVE_AES128 -#define USE_BTSTACK_AES128 -void btstack_aes128_calc(const uint8_t * key, const uint8_t * plaintext, uint8_t * result); -#endif - // degbugging // #define DEBUG_CCM @@ -146,6 +164,15 @@ static mbedtls_ecp_group mbedtls_ec_group; #endif /* ENABLE_ECC_P256 */ +#ifdef ENABLE_SOFTWARE_AES128 +// AES128 using public domain rijndael implementation +void btstack_aes128_calc(const uint8_t * key, const uint8_t * plaintext, uint8_t * ciphertext){ + uint32_t rk[RKLENGTH(KEYBITS)]; + int nrounds = rijndaelSetupEncrypt(rk, &key[0], KEYBITS); + rijndaelEncrypt(rk, nrounds, plaintext, ciphertext); +} +#endif + static void btstack_crypto_done(btstack_crypto_t * btstack_crypto){ btstack_linked_list_pop(&btstack_crypto_operations); (*btstack_crypto->context_callback.callback)(btstack_crypto->context_callback.context); diff --git a/src/btstack_crypto.h b/src/btstack_crypto.h index 6d32bc142..78fb4bc06 100644 --- a/src/btstack_crypto.h +++ b/src/btstack_crypto.h @@ -270,6 +270,17 @@ void btstack_crypto_ccm_encrypt_block(btstack_crypto_ccm_t * request, uint16_t l */ void btstack_crypto_ccm_decrypt_block(btstack_crypto_ccm_t * request, uint16_t len, const uint8_t * ciphertext, uint8_t * plaintext, void (* callback)(void * arg), void * callback_arg); +#ifdef HAVE_AES128 +/** + * Encrypt plaintext using AES128 + * @note Prototype for custom AES128 implementation + * @param key (16 bytes) + * @param plaintext (16 bytes) + * @param ciphertext (16 bytes) + */ +void btstack_aes128_calc(const uint8_t * key, const uint8_t * plaintext, uint8_t * ciphertext); +#endif + // PTS testing only - not possible when using Buetooth Controller for ECC operations void btstack_crypto_ecc_p256_set_key(const uint8_t * public_key, const uint8_t * private_key);