Skip to content

Commit ca44675

Browse files
committed
[Nuvoton] Fix crypto AC management
1. For SHA AC, use atomic flag to manage its ownership. (1) Nuvoton SHA AC doesn't support SHA context save & restore, so S/W SHA fallback has been supported before. To make non-blocking 'acquire' semantics clearer, introduce 'try_acquire' to substitute for 'acquire'. (2) No biting CPU due to mechanism above. (3) No deadlock due to mechanism above. 2. For AES/DES/ECC AC, change to mutex to manage their ownership. (1) Change crypto-misc.c to crypto-misc.cpp to utilize C++ SingletonPtr which guarantees thread-safe mutex construct-on-first-use. (2) With change to crypto-misc.cpp, add 'extern "C"' modifier to CRYPTO_IRQHandler() to avoid name mangling in C++. (3) No priority inversion because mutex has osMutexPrioInherit attribute bit set. (4) No deadlock because these AC are all locked for a short sequence of operations rather than the whole lifetime of mbedtls context. (5) For double mbedtls_internal_ecp_init() issue, it has been fixed in upper mbedtls layer. So no need to change ecc init/free flow.
1 parent b16b1db commit ca44675

File tree

14 files changed

+178
-119
lines changed

14 files changed

+178
-119
lines changed

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/aes/aes_alt.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
144144
error("Buffer for AES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
145145
}
146146

147-
/* TODO: Change busy-wait to other means to release CPU */
148147
/* Acquire ownership of AES H/W */
149-
while (! crypto_aes_acquire());
150-
148+
crypto_aes_acquire();
149+
151150
/* Init crypto module */
152151
crypto_init();
153152
/* Enable AES interrupt */

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/des/des_alt.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,9 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
349349
error("Buffer for DES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
350350
}
351351

352-
/* TODO: Change busy-wait to other means to release CPU */
353352
/* Acquire ownership of DES H/W */
354-
while (! crypto_des_acquire());
355-
353+
crypto_des_acquire();
354+
356355
/* Init crypto module */
357356
crypto_init();
358357
/* Enable DES interrupt */

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/ecp/ecp_internal_alt.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,23 @@ unsigned char mbedtls_internal_ecp_grp_capable( const mbedtls_ecp_group *grp )
223223

224224
int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp )
225225
{
226-
/* TODO: Change busy-wait with other means to release CPU */
226+
/* Behavior of mbedtls_internal_ecp_init()/mbedtls_internal_ecp_free()
227+
*
228+
* mbedtls_internal_ecp_init()/mbedtls_internal_ecp_free() are like pre-op/post-op calls
229+
* and they guarantee:
230+
*
231+
* 1. Paired
232+
* 2. No overlapping
233+
* 3. Upper public function cannot return when ECP alter. is still activated.
234+
*/
235+
227236
/* Acquire ownership of ECC accelerator */
228-
while (! crypto_ecc_acquire());
237+
crypto_ecc_acquire();
229238

230-
/* Init crypto module */
239+
/* Initialize crypto module */
231240
crypto_init();
241+
242+
/* Enable ECC interrupt */
232243
ECC_ENABLE_INT();
233244

234245
return 0;
@@ -238,9 +249,10 @@ void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp )
238249
{
239250
/* Disable ECC interrupt */
240251
ECC_DISABLE_INT();
252+
241253
/* Uninit crypto module */
242254
crypto_uninit();
243-
255+
244256
/* Release ownership of ECC accelerator */
245257
crypto_ecc_release();
246258
}
@@ -590,7 +602,7 @@ NU_STATIC int internal_run_eccop(const mbedtls_ecp_group *grp,
590602
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
591603
goto cleanup;
592604
}
593-
605+
594606
/* Configure ECC curve coefficients A/B */
595607
/* Special case for A = -3 */
596608
if (grp->A.p == NULL) {
@@ -644,7 +656,7 @@ NU_STATIC int internal_run_eccop(const mbedtls_ecp_group *grp,
644656
cleanup:
645657

646658
mbedtls_mpi_free(&N_);
647-
659+
648660
return ret;
649661
}
650662

@@ -698,7 +710,7 @@ NU_STATIC int internal_run_modop(mbedtls_mpi *r,
698710
const mbedtls_mpi *Np;
699711

700712
mbedtls_mpi_init(&N_);
701-
713+
702714
/* Use INTERNAL_MPI_NORM(Np, N1, N_, P) to get normalized MPI
703715
*
704716
* N_: Holds normalized MPI if the passed-in MPI N1 is not

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha1_alt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
static void mbedtls_sha1_init_internal(mbedtls_sha1_context *ctx, int try_hw)
3333
{
34-
if (try_hw && crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_try_acquire()) {
3535
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha1_hw_init(&ctx->hw_ctx);
3737
} else {

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha256_alt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
static void mbedtls_sha256_init_internal(mbedtls_sha256_context *ctx, int try_hw)
3333
{
34-
if (try_hw && crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_try_acquire()) {
3535
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha256_hw_init(&ctx->hw_ctx);
3737
} else {

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/sha/sha512_alt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
static void mbedtls_sha512_init_internal(mbedtls_sha512_context *ctx, int try_hw)
3333
{
34-
if (try_hw && crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_try_acquire()) {
3535
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha512_hw_init(&ctx->hw_ctx);
3737
} else {

features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/aes/aes_alt.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ static void __nvt_aes_crypt( mbedtls_aes_context *ctx,
144144
error("Buffer for AES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
145145
}
146146

147-
/* TODO: Change busy-wait to other means to release CPU */
148147
/* Acquire ownership of AES H/W */
149-
while (! crypto_aes_acquire());
150-
148+
crypto_aes_acquire();
149+
151150
/* Init crypto module */
152151
crypto_init();
153152
/* Enable AES interrupt */

features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/des/des_alt.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,10 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
348348
(! crypto_dma_buff_compat(dmabuf_out, MAXSIZE_DMABUF, 8))) {
349349
error("Buffer for DES alter. DMA requires to be word-aligned and located in 0x20000000-0x2FFFFFFF region.");
350350
}
351-
352-
/* TODO: Change busy-wait to other means to release CPU */
351+
353352
/* Acquire ownership of DES H/W */
354-
while (! crypto_des_acquire());
355-
353+
crypto_des_acquire();
354+
356355
/* Init crypto module */
357356
crypto_init();
358357
/* Enable DES interrupt */

features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha1_alt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
static void mbedtls_sha1_init_internal(mbedtls_sha1_context *ctx, int try_hw)
3333
{
34-
if (try_hw && crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_try_acquire()) {
3535
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha1_hw_init(&ctx->hw_ctx);
3737
} else {

features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/sha/sha256_alt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
static void mbedtls_sha256_init_internal(mbedtls_sha256_context *ctx, int try_hw)
3333
{
34-
if (try_hw && crypto_sha_acquire()) {
34+
if (try_hw && crypto_sha_try_acquire()) {
3535
ctx->active_ctx = &ctx->hw_ctx;
3636
mbedtls_sha256_hw_init(&ctx->hw_ctx);
3737
} else {

0 commit comments

Comments
 (0)