mbedTLS SHA Acceleration: Add missing esp_sha_lock_engine() function

This commit is contained in:
Angus Gratton 2016-11-22 20:56:36 +11:00
parent 2561b68af8
commit dfcb241850
2 changed files with 18 additions and 4 deletions

View File

@ -133,14 +133,29 @@ inline static bool sha_engines_all_idle() {
&& !engine_states[2].in_use; && !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) bool esp_sha_try_lock_engine(esp_sha_type sha_type)
{ {
sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)]; sha_engine_state *engine = &engine_states[sha_engine_index(sha_type)];
if(_lock_try_acquire(&engine->lock) != 0) { if(_lock_try_acquire(&engine->lock) != 0) {
/* This SHA engine is already in use */ /* This SHA engine is already in use */
return false; 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); _lock_acquire(&state_change_lock);
if (sha_engines_all_idle()) { 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" ); assert( !engine->in_use && "in_use flag should be cleared" );
engine->in_use = true; engine->in_use = true;
return true;
} }
void esp_sha_unlock_engine(esp_sha_type sha_type) void esp_sha_unlock_engine(esp_sha_type sha_type)
{ {
sha_engine_state *engine = &engine_states[sha_engine_index(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); size_t block_len = block_length(sha_type);
esp_sha_try_lock_engine(sha_type); esp_sha_lock_engine(sha_type);
SHA_CTX ctx; SHA_CTX ctx;
ets_sha_init(&ctx); ets_sha_init(&ctx);

View File

@ -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 * while a TLS connection is open, suggest using
* esp_sha_try_lock_engine() and failing over to software SHA. * 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 * @brief Try and obtain exclusive access to a particular SHA engine