Skip to content

Commit 8b57228

Browse files
committed
Add threading support to the mldsa allocator
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
1 parent c1420c9 commit 8b57228

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

drivers/pqcp/src/mldsa_native_buffer_alloc.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include <stdint.h>
1414
#include "src/sys.h"
1515

16+
#if defined(MBEDTLS_THREADING_C)
17+
#include "threading_internal.h"
18+
#endif
19+
1620
// Sufficient for signing with TF_PSA_CRYPTO_PQCP_MLDSA_87_ENABLED
1721
#define TF_PSA_CRYPTO_MLD_ALLOC_BUFFER_SIZE 123200
1822

@@ -22,26 +26,47 @@ struct tf_psa_crypto_mld_context {
2226
int alloc_offset; // Negative values indicate allocator errors
2327
};
2428

29+
#if defined(MBEDTLS_THREADING_C)
30+
#define TF_PSA_CRYPTO_MLD_ALLOC_LOCK() mbedtls_mutex_lock(&mbedtls_threading_pqcp_buffer_alloc_mutex)
31+
#define TF_PSA_CRYPTO_MLD_ALLOC_UNLOCK() mbedtls_mutex_unlock(&mbedtls_threading_pqcp_buffer_alloc_mutex)
32+
#else /* MBEDTLS_THREADING_C */
33+
#define TF_PSA_CRYPTO_MLD_ALLOC_LOCK() 0
34+
#define TF_PSA_CRYPTO_MLD_ALLOC_UNLOCK() 0
35+
#endif /* MBEDTLS_THREADING_C */
36+
2537
#define TF_PSA_CRYPTO_MLD_CUSTOM_ALLOC(v, T, N, context) \
2638
T *(v) = NULL; \
2739
do { \
2840
/* Verify that the allocation would fit in the buffer by itself, avoiding overflows \
2941
This should be optimized away at compile-time */ \
3042
if ((N) > 0 && (N) <= TF_PSA_CRYPTO_MLD_ALLOC_BUFFER_SIZE / sizeof(T)) { \
43+
if ((context).alloc_offset == 0) { \
44+
(context).alloc_offset = TF_PSA_CRYPTO_MLD_ALLOC_LOCK(); \
45+
} \
3146
if ((context).alloc_offset >= 0) { \
3247
if ((size_t) (context).alloc_offset <= \
3348
TF_PSA_CRYPTO_MLD_ALLOC_BUFFER_SIZE - MLD_ALIGN_UP(sizeof(T) * (N))) { \
3449
(v) = (T *) (tf_psa_crypto_mld_alloc_buffer + (context).alloc_offset); \
3550
(context).alloc_offset += MLD_ALIGN_UP(sizeof(T) * (N)); \
3651
} else { \
37-
/* Fail all further allocations in this function -> goto cleanup */ \
52+
/* Fail all further allocations in this function -> goto cleanup
53+
* If we ended up here, that implies (alloc_offset != 0) \
54+
* thus we don't need to call UNLOCK */ \
3855
(context).alloc_offset = PSA_ERROR_INSUFFICIENT_MEMORY; \
3956
} \
4057
} \
4158
} \
4259
} while (0)
4360

44-
#define TF_PSA_CRYPTO_MLD_CUSTOM_FREE(v, T, N, context)
61+
#define TF_PSA_CRYPTO_MLD_CUSTOM_FREE(v, T, N, context) \
62+
do { \
63+
if ((v) != NULL) { \
64+
/* Only unlock after freeing the last allocation */ \
65+
if ((uint8_t *) (v) == tf_psa_crypto_mld_alloc_buffer) { \
66+
(void) TF_PSA_CRYPTO_MLD_ALLOC_UNLOCK(); \
67+
} \
68+
} \
69+
} while (0)
4570

4671
#endif /* TF_PSA_CRYPTO_PQCP_BUFFER_ALLOC */
4772

platform/threading.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ void mbedtls_threading_set_alt(
268268
mbedtls_mutex_init(&mbedtls_threading_key_slot_mutex);
269269
mbedtls_mutex_init(&mbedtls_threading_psa_globaldata_mutex);
270270
mbedtls_mutex_init(&mbedtls_threading_psa_rngdata_mutex);
271+
#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED) && defined(TF_PSA_CRYPTO_PQCP_BUFFER_ALLOC)
272+
mbedtls_mutext_init(&mbedtls_threading_pqcp_buffer_alloc_mutex)
271273
#endif
274+
#endif /* MBEDTLS_PSA_CRYPTO_C */
272275
}
273276

274277
/*
@@ -286,7 +289,10 @@ void mbedtls_threading_free_alt(void)
286289
mbedtls_mutex_free(&mbedtls_threading_key_slot_mutex);
287290
mbedtls_mutex_free(&mbedtls_threading_psa_globaldata_mutex);
288291
mbedtls_mutex_free(&mbedtls_threading_psa_rngdata_mutex);
292+
#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED) && defined(TF_PSA_CRYPTO_PQCP_BUFFER_ALLOC)
293+
mbedtls_mutext_free(&mbedtls_threading_pqcp_buffer_alloc_mutex)
289294
#endif
295+
#endif /* MBEDTLS_PSA_CRYPTO_C */
290296
}
291297
#endif /* MBEDTLS_THREADING_ALT */
292298

@@ -306,6 +312,9 @@ mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT;
306312
mbedtls_threading_mutex_t mbedtls_threading_key_slot_mutex MUTEX_INIT;
307313
mbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex MUTEX_INIT;
308314
mbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex MUTEX_INIT;
315+
#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED) && defined(TF_PSA_CRYPTO_PQCP_BUFFER_ALLOC)
316+
mbedtls_threading_mutex_t mbedtls_threading_pqcp_buffer_alloc_mutex MUTEX_INIT;
309317
#endif
318+
#endif /* MBEDTLS_PSA_CRYPTO_C */
310319

311320
#endif /* MBEDTLS_THREADING_C */

platform/threading_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ extern mbedtls_threading_mutex_t mbedtls_threading_psa_globaldata_mutex;
6868
* This mutex must be held when reading or writing to the PSA
6969
* global_data rng_state or rng struct members. */
7070
extern mbedtls_threading_mutex_t mbedtls_threading_psa_rngdata_mutex;
71+
72+
#if defined(TF_PSA_CRYPTO_PQCP_MLDSA_ENABLED) && defined(TF_PSA_CRYPTO_PQCP_BUFFER_ALLOC)
73+
extern mbedtls_threading_mutex_t mbedtls_threading_pqcp_buffer_alloc_mutex;
7174
#endif
75+
#endif /* MBEDTLS_PSA_CRYPTO_C */
7276

7377
#endif /* MBEDTLS_THREADING_C */
7478

0 commit comments

Comments
 (0)