Skip to content

Commit 9edbcd7

Browse files
author
Cruz Monrreal
authored
Merge pull request #9081 from OpenNuvoton/nuvoton_refine_crypto_ac
Nuvoton: Fix crypto AC management
2 parents 6f57600 + ca44675 commit 9edbcd7

File tree

14 files changed

+185
-127
lines changed

14 files changed

+185
-127
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: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* would be defined in mbedtls/ecp.h from ecp.c for our inclusion */
5454
#define ECP_SHORTWEIERSTRASS
5555

56+
#include "mbedtls/platform.h"
5657
#include "mbedtls/ecp_internal.h"
5758
#include "mbed_toolchain.h"
5859
#include "mbed_assert.h"
@@ -222,12 +223,23 @@ unsigned char mbedtls_internal_ecp_grp_capable( const mbedtls_ecp_group *grp )
222223

223224
int mbedtls_internal_ecp_init( const mbedtls_ecp_group *grp )
224225
{
225-
/* 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+
226236
/* Acquire ownership of ECC accelerator */
227-
while (! crypto_ecc_acquire());
237+
crypto_ecc_acquire();
228238

229-
/* Init crypto module */
239+
/* Initialize crypto module */
230240
crypto_init();
241+
242+
/* Enable ECC interrupt */
231243
ECC_ENABLE_INT();
232244

233245
return 0;
@@ -237,9 +249,10 @@ void mbedtls_internal_ecp_free( const mbedtls_ecp_group *grp )
237249
{
238250
/* Disable ECC interrupt */
239251
ECC_DISABLE_INT();
252+
240253
/* Uninit crypto module */
241254
crypto_uninit();
242-
255+
243256
/* Release ownership of ECC accelerator */
244257
crypto_ecc_release();
245258
}
@@ -589,7 +602,7 @@ NU_STATIC int internal_run_eccop(const mbedtls_ecp_group *grp,
589602
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
590603
goto cleanup;
591604
}
592-
605+
593606
/* Configure ECC curve coefficients A/B */
594607
/* Special case for A = -3 */
595608
if (grp->A.p == NULL) {
@@ -632,10 +645,9 @@ NU_STATIC int internal_run_eccop(const mbedtls_ecp_group *grp,
632645
crypto_ecc_prestart();
633646
CRPT->ECC_CTL = (grp->pbits << CRPT_ECC_CTL_CURVEM_Pos) | eccop | CRPT_ECC_CTL_FSEL_Msk | CRPT_ECC_CTL_START_Msk;
634647
ecc_done = crypto_ecc_wait();
635-
636-
/* FIXME: Better error code for ECC accelerator error */
637-
MBEDTLS_MPI_CHK(ecc_done ? 0 : -1);
638-
648+
649+
MBEDTLS_MPI_CHK(ecc_done ? 0 : MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED);
650+
639651
/* (X1, Y1) hold the normalized result. */
640652
MBEDTLS_MPI_CHK(internal_mpi_read_eccreg(&R->X, (uint32_t *) CRPT->ECC_X1, NU_ECC_BIGNUM_MAXWORD));
641653
MBEDTLS_MPI_CHK(internal_mpi_read_eccreg(&R->Y, (uint32_t *) CRPT->ECC_Y1, NU_ECC_BIGNUM_MAXWORD));
@@ -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
@@ -726,10 +738,9 @@ NU_STATIC int internal_run_modop(mbedtls_mpi *r,
726738
crypto_ecc_prestart();
727739
CRPT->ECC_CTL = (pbits << CRPT_ECC_CTL_CURVEM_Pos) | (ECCOP_MODULE | modop) | CRPT_ECC_CTL_FSEL_Msk | CRPT_ECC_CTL_START_Msk;
728740
ecc_done = crypto_ecc_wait();
729-
730-
/* FIXME: Better error code for ECC accelerator error */
731-
MBEDTLS_MPI_CHK(ecc_done ? 0 : -1);
732-
741+
742+
MBEDTLS_MPI_CHK(ecc_done ? 0 : MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED);
743+
733744
/* X1 holds the result. */
734745
MBEDTLS_MPI_CHK(internal_mpi_read_eccreg(r, (uint32_t *) CRPT->ECC_X1, NU_ECC_BIGNUM_MAXWORD));
735746

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)