From dfcb241850c11808295de83dc070542dea537d11 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 22 Nov 2016 20:56:36 +1100 Subject: [PATCH] mbedTLS SHA Acceleration: Add missing esp_sha_lock_engine() function --- components/esp32/hwcrypto/sha.c | 20 +++++++++++++++++--- components/esp32/include/hwcrypto/sha.h | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/components/esp32/hwcrypto/sha.c b/components/esp32/hwcrypto/sha.c index fd8075fc72..601981e2a1 100644 --- a/components/esp32/hwcrypto/sha.c +++ b/components/esp32/hwcrypto/sha.c @@ -133,14 +133,29 @@ inline static bool sha_engines_all_idle() { && !engine_states[2].in_use; } +static void esp_sha_lock_engine_inner(sha_engine_state *engine); + bool esp_sha_try_lock_engine(esp_sha_type sha_type) { sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)]; if(_lock_try_acquire(&engine->lock) != 0) { /* This SHA engine is already in use */ return false; + } else { + esp_sha_lock_engine_inner(engine); + return true; } +} + +void esp_sha_lock_engine(esp_sha_type sha_type) +{ + sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)]; + _lock_acquire(&engine->lock); + esp_sha_lock_engine_inner(engine); +} +static void esp_sha_lock_engine_inner(sha_engine_state *engine) +{ _lock_acquire(&state_change_lock); if (sha_engines_all_idle()) { @@ -157,10 +172,9 @@ bool esp_sha_try_lock_engine(esp_sha_type sha_type) assert( !engine->in_use && "in_use flag should be cleared" ); engine->in_use = true; - - return true; } + void esp_sha_unlock_engine(esp_sha_type sha_type) { sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)]; @@ -253,7 +267,7 @@ void esp_sha(esp_sha_type sha_type, const unsigned char *input, size_t ilen, uns { size_t block_len = block_length(sha_type); - esp_sha_try_lock_engine(sha_type); + esp_sha_lock_engine(sha_type); SHA_CTX ctx; ets_sha_init(&ctx); diff --git a/components/esp32/include/hwcrypto/sha.h b/components/esp32/include/hwcrypto/sha.h index 3162b13317..2a0ec78abe 100644 --- a/components/esp32/include/hwcrypto/sha.h +++ b/components/esp32/include/hwcrypto/sha.h @@ -131,7 +131,7 @@ void esp_sha_read_digest_state(esp_sha_type sha_type, void *digest_state); * while a TLS connection is open, suggest using * esp_sha_try_lock_engine() and failing over to software SHA. */ -bool esp_sha_lock_engine(esp_sha_type sha_type); +void esp_sha_lock_engine(esp_sha_type sha_type); /** * @brief Try and obtain exclusive access to a particular SHA engine -- 2.40.0