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 {

targets/TARGET_NUVOTON/TARGET_M480/crypto/crypto-misc.c renamed to targets/TARGET_NUVOTON/TARGET_M480/crypto/crypto-misc.cpp

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,48 @@
1919
#include "mbed_assert.h"
2020
#include "mbed_critical.h"
2121
#include "mbed_error.h"
22+
#include "cmsis_os2.h"
23+
#include "mbed_rtos_storage.h"
24+
#include <string.h>
2225
#include <limits.h>
2326
#include "nu_modutil.h"
2427
#include "nu_bitutil.h"
2528
#include "crypto-misc.h"
29+
#include "SingletonPtr.h"
30+
#include "Mutex.h"
31+
32+
/* Consideration for choosing proper synchronization mechanism
33+
*
34+
* 1. We choose mutex to synchronize access to crypto non-SHA AC. We can guarantee:
35+
* (1) No deadlock
36+
* We just lock mutex for a short sequence of operations rather than the whole lifetime
37+
* of crypto context.
38+
* (2) No priority inversion
39+
* Mutex supports priority inheritance and it is enabled.
40+
* 2. We choose atomic flag to synchronize access to crypto SHA AC. We can guarantee:
41+
* (1) No deadlock
42+
* With SHA AC not supporting context save & restore, we provide SHA S/W fallback when
43+
* SHA AC is not available.
44+
* (2) No biting CPU
45+
* Same reason as above.
46+
*/
47+
48+
/* Mutex for crypto AES AC management */
49+
static SingletonPtr<rtos::Mutex> crypto_aes_mutex;
50+
51+
/* Mutex for crypto DES AC management */
52+
static SingletonPtr<rtos::Mutex> crypto_des_mutex;
53+
54+
/* Mutex for crypto ECC AC management */
55+
static SingletonPtr<rtos::Mutex> crypto_ecc_mutex;
56+
57+
/* Atomic flag for crypto SHA AC management */
58+
static core_util_atomic_flag crypto_sha_atomic_flag = CORE_UTIL_ATOMIC_FLAG_INIT;
2659

27-
/* Track if AES H/W is available */
28-
static uint16_t crypto_aes_avail = 1;
29-
/* Track if DES H/W is available */
30-
static uint16_t crypto_des_avail = 1;
31-
/* Track if SHA H/W is available */
32-
static uint16_t crypto_sha_avail = 1;
33-
/* Track if ECC H/W is available */
34-
static uint16_t crypto_ecc_avail = 1;
3560

3661
/* Crypto (AES, DES, SHA, etc.) init counter. Crypto's keeps active as it is non-zero. */
3762
static uint16_t crypto_init_counter = 0U;
3863

39-
static bool crypto_submodule_acquire(uint16_t *submodule_avail);
40-
static void crypto_submodule_release(uint16_t *submodule_avail);
41-
4264
/* Crypto done flags */
4365
#define CRYPTO_DONE_OK BIT0 /* Done with OK */
4466
#define CRYPTO_DONE_ERR BIT1 /* Done with error */
@@ -119,44 +141,52 @@ void crypto_zeroize32(uint32_t *v, size_t n)
119141
}
120142
}
121143

122-
bool crypto_aes_acquire(void)
144+
void crypto_aes_acquire(void)
123145
{
124-
return crypto_submodule_acquire(&crypto_aes_avail);
146+
/* Don't check return code of Mutex::lock(void)
147+
*
148+
* This function treats RTOS errors as fatal system errors, so it can only return osOK.
149+
* Use of the return value is deprecated, as the return is expected to become void in
150+
* the future.
151+
*/
152+
crypto_aes_mutex->lock();
125153
}
126154

127155
void crypto_aes_release(void)
128156
{
129-
crypto_submodule_release(&crypto_aes_avail);
157+
crypto_aes_mutex->unlock();
130158
}
131159

132-
bool crypto_des_acquire(void)
160+
void crypto_des_acquire(void)
133161
{
134-
return crypto_submodule_acquire(&crypto_des_avail);
162+
/* Don't check return code of Mutex::lock(void) */
163+
crypto_des_mutex->lock();
135164
}
136165

137166
void crypto_des_release(void)
138167
{
139-
crypto_submodule_release(&crypto_des_avail);
168+
crypto_des_mutex->unlock();
140169
}
141170

142-
bool crypto_sha_acquire(void)
171+
void crypto_ecc_acquire(void)
143172
{
144-
return crypto_submodule_acquire(&crypto_sha_avail);
173+
/* Don't check return code of Mutex::lock(void) */
174+
crypto_ecc_mutex->lock();
145175
}
146176

147-
void crypto_sha_release(void)
177+
void crypto_ecc_release(void)
148178
{
149-
crypto_submodule_release(&crypto_sha_avail);
179+
crypto_ecc_mutex->unlock();
150180
}
151181

152-
bool crypto_ecc_acquire(void)
182+
bool crypto_sha_try_acquire(void)
153183
{
154-
return crypto_submodule_acquire(&crypto_ecc_avail);
184+
return !core_util_atomic_flag_test_and_set(&crypto_sha_atomic_flag);
155185
}
156186

157-
void crypto_ecc_release(void)
187+
void crypto_sha_release(void)
158188
{
159-
crypto_submodule_release(&crypto_ecc_avail);
189+
core_util_atomic_flag_clear(&crypto_sha_atomic_flag);
160190
}
161191

162192
void crypto_prng_prestart(void)
@@ -240,18 +270,6 @@ bool crypto_dma_buffs_overlap(const void *in_buff, size_t in_buff_size, const vo
240270
return overlap;
241271
}
242272

243-
static bool crypto_submodule_acquire(uint16_t *submodule_avail)
244-
{
245-
uint16_t expectedCurrentValue = 1;
246-
return core_util_atomic_cas_u16(submodule_avail, &expectedCurrentValue, 0);
247-
}
248-
249-
static void crypto_submodule_release(uint16_t *submodule_avail)
250-
{
251-
uint16_t expectedCurrentValue = 0;
252-
while (! core_util_atomic_cas_u16(submodule_avail, &expectedCurrentValue, 1));
253-
}
254-
255273
static void crypto_submodule_prestart(volatile uint16_t *submodule_done)
256274
{
257275
*submodule_done = 0;
@@ -285,7 +303,7 @@ static bool crypto_submodule_wait(volatile uint16_t *submodule_done)
285303
}
286304

287305
/* Crypto interrupt handler */
288-
void CRYPTO_IRQHandler()
306+
extern "C" void CRYPTO_IRQHandler()
289307
{
290308
uint32_t intsts;
291309

0 commit comments

Comments
 (0)