]> granicus.if.org Git - esp-idf/commitdiff
esp32 hwcrypto: Use spinlock instead of lock to protect AES
authorAngus Gratton <angus@espressif.com>
Tue, 15 Aug 2017 23:06:52 +0000 (09:06 +1000)
committerAngus Gratton <gus@projectgus.com>
Fri, 25 Aug 2017 06:08:03 +0000 (16:08 +1000)
More than doubles performance of mbedTLS AES self-tests.

components/esp32/hwcrypto/aes.c

index 7a9c93f32fc01adf927bda39fc8ab17cde3a7040..716bc636fe676eec2ad81822cd5fff6e22a48308 100644 (file)
 #include "soc/hwcrypto_reg.h"
 #include <sys/lock.h>
 
-static _lock_t aes_lock;
+#include <freertos/FreeRTOS.h>
+
+#include "soc/cpu.h"
+#include <stdio.h>
+
+
+/* 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;
 }