From 14c7551db6bd565fe4fbcb2d07a31ee0ecc6fd44 Mon Sep 17 00:00:00 2001 From: nils Date: Wed, 27 Nov 2024 12:23:02 +0100 Subject: [PATCH 1/3] Release the lock in mbedtls_sha256_free if it was not released already. Requires making pico_sha256_unlock include-able. Fixes #2103. --- src/rp2_common/pico_mbedtls/pico_mbedtls.c | 3 +++ src/rp2_common/pico_sha256/include/pico/sha256.h | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/rp2_common/pico_mbedtls/pico_mbedtls.c b/src/rp2_common/pico_mbedtls/pico_mbedtls.c index 3279a42d1..e3c79b71a 100644 --- a/src/rp2_common/pico_mbedtls/pico_mbedtls.c +++ b/src/rp2_common/pico_mbedtls/pico_mbedtls.c @@ -36,6 +36,9 @@ void mbedtls_sha256_init(__unused mbedtls_sha256_context *ctx) { } void mbedtls_sha256_free(__unused mbedtls_sha256_context *ctx) { + if (ctx->locked) { + pico_sha256_unlock(ctx); + } } int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224) { diff --git a/src/rp2_common/pico_sha256/include/pico/sha256.h b/src/rp2_common/pico_sha256/include/pico/sha256.h index 80aa5af37..6a36b6166 100644 --- a/src/rp2_common/pico_sha256/include/pico/sha256.h +++ b/src/rp2_common/pico_sha256/include/pico/sha256.h @@ -58,6 +58,16 @@ typedef struct pico_sha256_state { size_t total_data_size; } pico_sha256_state_t; +/*! \brief Release the internal lock on the SHA256 hardware + * \ingroup pico_sha256 + * + * Release the internal lock on the hardware. + * Fails if the internal lock was not claimed. + * + * @param state A pointer to a pico_sha256_state_t instance + */ +void __weak pico_sha256_unlock(pico_sha256_state_t *state); + /*! \brief Start a SHA-256 calculation returning immediately with an error if the SHA-256 hardware is not available * \ingroup pico_sha256 * From 07222ae013f5c74d4ea3ae605e896f773007f48f Mon Sep 17 00:00:00 2001 From: nils Date: Wed, 27 Nov 2024 16:41:39 +0100 Subject: [PATCH 2/3] addres lock release API comment --- src/rp2_common/pico_mbedtls/pico_mbedtls.c | 4 +--- src/rp2_common/pico_sha256/include/pico/sha256.h | 6 +++--- src/rp2_common/pico_sha256/sha256.c | 6 ++++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/rp2_common/pico_mbedtls/pico_mbedtls.c b/src/rp2_common/pico_mbedtls/pico_mbedtls.c index e3c79b71a..853d97cdf 100644 --- a/src/rp2_common/pico_mbedtls/pico_mbedtls.c +++ b/src/rp2_common/pico_mbedtls/pico_mbedtls.c @@ -36,9 +36,7 @@ void mbedtls_sha256_init(__unused mbedtls_sha256_context *ctx) { } void mbedtls_sha256_free(__unused mbedtls_sha256_context *ctx) { - if (ctx->locked) { - pico_sha256_unlock(ctx); - } + pico_sha256_cleanup(ctx); } int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224) { diff --git a/src/rp2_common/pico_sha256/include/pico/sha256.h b/src/rp2_common/pico_sha256/include/pico/sha256.h index 6a36b6166..8394fe355 100644 --- a/src/rp2_common/pico_sha256/include/pico/sha256.h +++ b/src/rp2_common/pico_sha256/include/pico/sha256.h @@ -58,15 +58,15 @@ typedef struct pico_sha256_state { size_t total_data_size; } pico_sha256_state_t; -/*! \brief Release the internal lock on the SHA256 hardware +/*! \brief Release the internal lock on the SHA-256 hardware * \ingroup pico_sha256 * - * Release the internal lock on the hardware. + * Release the internal lock on the SHA-256 hardware. * Fails if the internal lock was not claimed. * * @param state A pointer to a pico_sha256_state_t instance */ -void __weak pico_sha256_unlock(pico_sha256_state_t *state); +void pico_sha256_cleanup(pico_sha256_state_t *state); /*! \brief Start a SHA-256 calculation returning immediately with an error if the SHA-256 hardware is not available * \ingroup pico_sha256 diff --git a/src/rp2_common/pico_sha256/sha256.c b/src/rp2_common/pico_sha256/sha256.c index e0cc73974..91009c804 100644 --- a/src/rp2_common/pico_sha256/sha256.c +++ b/src/rp2_common/pico_sha256/sha256.c @@ -30,6 +30,12 @@ void __weak pico_sha256_unlock(pico_sha256_state_t *state) { state->locked = false; } +void pico_sha256_cleanup(pico_sha256_state_t *state) { + if (state->locked) { + pico_sha256_unlock(state); + } +} + int pico_sha256_try_start(pico_sha256_state_t *state, enum sha256_endianness endianness, bool use_dma) { memset(state, 0, sizeof(*state)); if (!pico_sha256_lock(state)) return PICO_ERROR_RESOURCE_IN_USE; From 3b3155b391a6544d1e76b0509cc87c42565bb82b Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Wed, 29 Jan 2025 13:09:45 +0000 Subject: [PATCH 3/3] Fix description of pico_sha256_cleanup --- src/rp2_common/pico_sha256/include/pico/sha256.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rp2_common/pico_sha256/include/pico/sha256.h b/src/rp2_common/pico_sha256/include/pico/sha256.h index 8394fe355..ab0c81ad7 100644 --- a/src/rp2_common/pico_sha256/include/pico/sha256.h +++ b/src/rp2_common/pico_sha256/include/pico/sha256.h @@ -62,7 +62,7 @@ typedef struct pico_sha256_state { * \ingroup pico_sha256 * * Release the internal lock on the SHA-256 hardware. - * Fails if the internal lock was not claimed. + * Does nothing if the internal lock was not claimed. * * @param state A pointer to a pico_sha256_state_t instance */