From f6574519514820e4c68b68ea54db85e8d1efae42 Mon Sep 17 00:00:00 2001 From: konradderda <55538957+konradderda@users.noreply.github.com> Date: Tue, 1 Oct 2019 17:35:27 +0200 Subject: [PATCH] [nrf528xx] always enable NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT (#4210) If all of COMMISSIONER/JOINER/TIME_SYNC switches are not enabled and CC310 is not disabled, RNG always produces the same values (even after issuing a reset). This is caused by mbed TLS library which by default uses 256 keys internally but CC310 AES module can handle only 128 bit keys. We can enable NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT options for all configurations to enable software implementation when keys longer than 128 bits are used. --- .../openthread-core-nrf52811-config.h | 15 -------- .../libraries/crypto/aes_alt.c | 24 ++----------- .../libraries/crypto/aes_alt.h | 34 +++++++------------ .../libraries/crypto/aes_alt_soft.c | 4 --- .../libraries/crypto/aes_alt_soft.h | 4 --- .../crypto/nrf52840-mbedtls-config.h | 15 -------- 6 files changed, 15 insertions(+), 81 deletions(-) diff --git a/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h b/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h index 32c62fb15..1e0a798e7 100644 --- a/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h +++ b/examples/platforms/nrf528xx/nrf52811/openthread-core-nrf52811-config.h @@ -224,21 +224,6 @@ #define OPENTHREAD_CONFIG_MAC_HEADER_IE_SUPPORT 1 #endif -/** - * @def NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT - * - * Define as 1 to enable AES usage in interrupt context and AES-256, by introducing a software AES under platform layer. - * - * @note This feature must be enabled to support AES-256 used by Commissioner and Joiner, and AES usage in interrupt - * context used by Header IE related features. - * - */ -#if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE || OPENTHREAD_CONFIG_JOINER_ENABLE || OPENTHREAD_CONFIG_MAC_HEADER_IE_SUPPORT -#define NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT 1 -#else -#define NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT 0 -#endif - /* * Suppress the ARMCC warning on unreachable statement, * e.g. break after assert(false) or ExitNow() macro. diff --git a/third_party/NordicSemiconductor/libraries/crypto/aes_alt.c b/third_party/NordicSemiconductor/libraries/crypto/aes_alt.c index a5882db7c..72a08c3fc 100644 --- a/third_party/NordicSemiconductor/libraries/crypto/aes_alt.c +++ b/third_party/NordicSemiconductor/libraries/crypto/aes_alt.c @@ -51,23 +51,18 @@ #include "aes_alt_cc310.h" -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT #include #include "aes_alt_soft.h" -#endif void mbedtls_aes_init(mbedtls_aes_context * ctx) { -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT uint32_t active_vector_id = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) >> SCB_ICSR_VECTACTIVE_Pos; // Check if this function is called from main thread. if (active_vector_id == 0) { aes_soft_init(ctx); -#endif aes_cc310_init(ctx); -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT ctx->using_cc310 = true; } else @@ -75,24 +70,19 @@ void mbedtls_aes_init(mbedtls_aes_context * ctx) aes_soft_init(ctx); ctx->using_cc310 = false; } -#endif } void mbedtls_aes_free(mbedtls_aes_context * ctx) { -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT if (ctx->using_cc310) { -#endif aes_cc310_free(ctx); -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT aes_soft_free(ctx); } else { aes_soft_free(ctx); - } -#endif + } } int mbedtls_aes_setkey_enc(mbedtls_aes_context * ctx, @@ -101,19 +91,15 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context * ctx, { int result; -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT if (ctx->using_cc310 && keybits == 128) - { -#endif + { result = aes_cc310_setkey_enc(ctx, key, keybits); -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT } else { ctx->using_cc310 = false; result = aes_soft_setkey_enc(ctx, key, keybits); - } -#endif + } return result; } @@ -132,18 +118,14 @@ int mbedtls_aes_crypt_ecb(mbedtls_aes_context * ctx, { int result; -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT if (ctx->using_cc310) { -#endif result = aes_cc310_crypt_ecb(ctx, mode, input, output); -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT } else { result = aes_soft_crypt_ecb(ctx, mode, input, output); } -#endif return result; } diff --git a/third_party/NordicSemiconductor/libraries/crypto/aes_alt.h b/third_party/NordicSemiconductor/libraries/crypto/aes_alt.h index 0f9e0731a..87af8d0dc 100644 --- a/third_party/NordicSemiconductor/libraries/crypto/aes_alt.h +++ b/third_party/NordicSemiconductor/libraries/crypto/aes_alt.h @@ -71,30 +71,20 @@ extern "C" { */ typedef struct { -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT bool using_cc310; ///< Indicate whether it's using cc310 or not. -#endif - -#if !NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT - union + struct { -#endif // !NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT - struct - { - SaSiAesUserContext_t user_context; ///< User context for CC310 AES. - uint8_t key_buffer[32]; ///< Buffer for an encryption key. - SaSiAesUserKeyData_t key; ///< CC310 AES key structure. - SaSiAesEncryptMode_t mode; ///< Current context operation mode (encrypt/decrypt). - } hardware; - struct - { - int nr; ///< number of rounds */ - uint32_t *rk; ///< AES round keys */ - uint32_t buf[68]; ///< unaligned data */ - } software; -#if !NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT - }; -#endif // !NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT + SaSiAesUserContext_t user_context; ///< User context for CC310 AES. + uint8_t key_buffer[32]; ///< Buffer for an encryption key. + SaSiAesUserKeyData_t key; ///< CC310 AES key structure. + SaSiAesEncryptMode_t mode; ///< Current context operation mode (encrypt/decrypt). + } hardware; + struct + { + int nr; ///< number of rounds */ + uint32_t *rk; ///< AES round keys */ + uint32_t buf[68]; ///< unaligned data */ + } software; } mbedtls_aes_context; diff --git a/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.c b/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.c index a72cffdbf..31500702d 100644 --- a/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.c +++ b/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.c @@ -50,8 +50,6 @@ #ifdef MBEDTLS_AES_ALT -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT - /* * 32-bit integer manipulation macros (little endian) */ @@ -399,6 +397,4 @@ int aes_soft_crypt_ecb( mbedtls_aes_context *ctx, return aes_encrypt( ctx, input, output ); } -#endif /* NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT */ - #endif /* MBEDTLS_AES_ALT */ diff --git a/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.h b/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.h index ba6acfc54..93869556d 100644 --- a/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.h +++ b/third_party/NordicSemiconductor/libraries/crypto/aes_alt_soft.h @@ -52,8 +52,6 @@ #ifdef MBEDTLS_AES_ALT -#if NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT - #include "aes_alt.h" #ifdef __cplusplus @@ -106,8 +104,6 @@ int aes_soft_crypt_ecb(mbedtls_aes_context * ctx, } #endif -#endif /* NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT */ - #endif /* MBEDTLS_AES_ALT */ #endif /* AES_ALT_SOFT_H */ diff --git a/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h b/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h index 9f543725a..e7f237511 100644 --- a/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h +++ b/third_party/NordicSemiconductor/libraries/crypto/nrf52840-mbedtls-config.h @@ -51,19 +51,4 @@ _Pragma("diag_suppress=68") #endif -/** - * @def NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT - * - * Define as 1 to enable AES usage in interrupt context and AES-256, by introducing a software AES under platform layer. - * - * @note This feature must be enabled to support AES-256 used by Commissioner and Joiner, and AES usage in interrupt context - * used by Header IE related features. - * - */ -#if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE || OPENTHREAD_CONFIG_JOINER_ENABLE || OPENTHREAD_CONFIG_MAC_HEADER_IE_SUPPORT -#define NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT 1 -#else -#define NRF_MBEDTLS_AES_ALT_INTERRUPT_CONTEXT 0 -#endif - #endif // NRF52840_MBEDTLS_CONFIG_H_