From: Angus Gratton Date: Tue, 15 Aug 2017 23:06:52 +0000 (+1000) Subject: esp32 hwcrypto: Use spinlock instead of lock to protect AES X-Git-Tag: v3.1-dev~347^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=50e0a54630747f76c0d94455bf1a0d7a4908856a;p=esp-idf esp32 hwcrypto: Use spinlock instead of lock to protect AES More than doubles performance of mbedTLS AES self-tests. --- diff --git a/components/esp32/hwcrypto/aes.c b/components/esp32/hwcrypto/aes.c index 7a9c93f32f..716bc636fe 100644 --- a/components/esp32/hwcrypto/aes.c +++ b/components/esp32/hwcrypto/aes.c @@ -32,12 +32,26 @@ #include "soc/hwcrypto_reg.h" #include -static _lock_t aes_lock; +#include + +#include "soc/cpu.h" +#include + + +/* AES uses a spinlock mux not a lock as the underlying block operation + only takes 208 cycles (to write key & compute block), +600 cycles + for DPORT protection but +3400 cycles again if you use a full sized lock. + + For CBC, CFB, etc. this may mean that interrupts are disabled for a longer + period of time for bigger lengths. However at the moment this has to happen + anyway due to DPORT protection... +*/ +static portMUX_TYPE aes_spinlock = portMUX_INITIALIZER_UNLOCKED; void esp_aes_acquire_hardware( void ) { /* newlib locks lazy initialize on ESP-IDF */ - _lock_acquire(&aes_lock); + portENTER_CRITICAL(&aes_spinlock); DPORT_STALL_OTHER_CPU_START(); { @@ -65,7 +79,7 @@ void esp_aes_release_hardware( void ) } DPORT_STALL_OTHER_CPU_END(); - _lock_release(&aes_lock); + portEXIT_CRITICAL(&aes_spinlock); } void esp_aes_init( esp_aes_context *ctx ) @@ -178,6 +192,7 @@ int esp_aes_crypt_ecb( esp_aes_context *ctx, esp_aes_setkey_hardware(ctx, mode); esp_aes_block(input, output); esp_aes_release_hardware(); + return 0; }