Skip to content

Commit 3a8c1aa

Browse files
committed
[NUC472/M487] Use interrupt signal rather than polling to check operation completion in DES alter.
This is to be consistent with PRNG/AES.
1 parent 0c10984 commit 3a8c1aa

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
355355

356356
/* Init crypto module */
357357
crypto_init();
358+
/* Enable DES interrupt */
359+
TDES_ENABLE_INT();
358360

359361
/* Configure TDES_CTL register
360362
*
@@ -407,17 +409,9 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
407409

408410
TDES_SetDMATransfer(0, (uint32_t) dmabuf_in, (uint32_t) dmabuf_out, data_len);
409411

410-
/* Ensure memory accesses above are completed before DMA is started
411-
*
412-
* Replacing __DSB() with __DMB() is also OK in this case.
413-
*
414-
* Refer to "multi-master systems" section with DMA in:
415-
* https://static.docs.arm.com/dai0321/a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf
416-
*/
417-
__DSB();
418-
/* Start enc/dec */
412+
crypto_des_prestart();
419413
TDES_Start(0, CRYPTO_DMA_ONE_SHOT);
420-
while (CRPT->TDES_STS & CRPT_TDES_STS_BUSY_Msk);
414+
crypto_des_wait();
421415

422416
memcpy(out_pos, dmabuf_out, data_len);
423417
in_pos += data_len;
@@ -457,6 +451,8 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
457451
}
458452
}
459453

454+
/* Disable DES interrupt */
455+
TDES_DISABLE_INT();
460456
/* Uninit crypto module */
461457
crypto_uninit();
462458

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
355355

356356
/* Init crypto module */
357357
crypto_init();
358+
/* Enable DES interrupt */
359+
TDES_ENABLE_INT();
358360

359361
/* Configure TDES_CTL register
360362
*
@@ -407,17 +409,9 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
407409

408410
TDES_SetDMATransfer(0, (uint32_t) dmabuf_in, (uint32_t) dmabuf_out, data_len);
409411

410-
/* Ensure memory accesses above are completed before DMA is started
411-
*
412-
* Replacing __DSB() with __DMB() is also OK in this case.
413-
*
414-
* Refer to "multi-master systems" section with DMA in:
415-
* https://static.docs.arm.com/dai0321/a/DAI0321A_programming_guide_memory_barriers_for_m_profile.pdf
416-
*/
417-
__DSB();
418-
/* Start enc/dec */
412+
crypto_des_prestart();
419413
TDES_Start(0, CRYPTO_DMA_ONE_SHOT);
420-
while (CRPT->TDES_STS & CRPT_TDES_STS_BUSY_Msk);
414+
crypto_des_wait();
421415

422416
memcpy(out_pos, dmabuf_out, data_len);
423417
in_pos += data_len;
@@ -457,6 +451,8 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
457451
}
458452
}
459453

454+
/* Disable DES interrupt */
455+
TDES_DISABLE_INT();
460456
/* Uninit crypto module */
461457
crypto_uninit();
462458

targets/TARGET_NUVOTON/TARGET_M480/crypto/crypto-misc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static void crypto_submodule_release(uint16_t *submodule_avail);
4141
static volatile uint16_t crypto_prng_done;
4242
/* Track if AES H/W operation is done */
4343
static volatile uint16_t crypto_aes_done;
44+
/* Track if DES H/W operation is done */
45+
static volatile uint16_t crypto_des_done;
4446

4547
static void crypto_submodule_prestart(volatile uint16_t *submodule_done);
4648
static bool crypto_submodule_wait(volatile uint16_t *submodule_done);
@@ -150,6 +152,16 @@ bool crypto_aes_wait(void)
150152
return crypto_submodule_wait(&crypto_aes_done);
151153
}
152154

155+
void crypto_des_prestart(void)
156+
{
157+
crypto_submodule_prestart(&crypto_des_done);
158+
}
159+
160+
bool crypto_des_wait(void)
161+
{
162+
return crypto_submodule_wait(&crypto_des_done);
163+
}
164+
153165
bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to)
154166
{
155167
uint32_t buff_ = (uint32_t) buff;
@@ -201,5 +213,8 @@ void CRYPTO_IRQHandler()
201213
} else if (AES_GET_INT_FLAG()) {
202214
crypto_aes_done = 1;
203215
AES_CLR_INT_FLAG();
216+
} else if (TDES_GET_INT_FLAG()) {
217+
crypto_des_done = 1;
218+
TDES_CLR_INT_FLAG();
204219
}
205220
}

targets/TARGET_NUVOTON/TARGET_M480/crypto/crypto-misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void crypto_prng_prestart(void);
6565
bool crypto_prng_wait(void);
6666
void crypto_aes_prestart(void);
6767
bool crypto_aes_wait(void);
68+
void crypto_des_prestart(void);
69+
bool crypto_des_wait(void);
6870

6971

7072
/* Check if buffer can be used for crypto DMA. It has the following requirements:

targets/TARGET_NUVOTON/TARGET_NUC472/crypto/crypto-misc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static void crypto_submodule_release(uint16_t *submodule_avail);
4141
static volatile uint16_t crypto_prng_done;
4242
/* Track if AES H/W operation is done */
4343
static volatile uint16_t crypto_aes_done;
44+
/* Track if DES H/W operation is done */
45+
static volatile uint16_t crypto_des_done;
4446

4547
static void crypto_submodule_prestart(volatile uint16_t *submodule_done);
4648
static bool crypto_submodule_wait(volatile uint16_t *submodule_done);
@@ -150,6 +152,16 @@ bool crypto_aes_wait(void)
150152
return crypto_submodule_wait(&crypto_aes_done);
151153
}
152154

155+
void crypto_des_prestart(void)
156+
{
157+
crypto_submodule_prestart(&crypto_des_done);
158+
}
159+
160+
bool crypto_des_wait(void)
161+
{
162+
return crypto_submodule_wait(&crypto_des_done);
163+
}
164+
153165
bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to)
154166
{
155167
uint32_t buff_ = (uint32_t) buff;
@@ -201,5 +213,8 @@ void CRYPTO_IRQHandler()
201213
} else if (AES_GET_INT_FLAG()) {
202214
crypto_aes_done = 1;
203215
AES_CLR_INT_FLAG();
216+
} else if (TDES_GET_INT_FLAG()) {
217+
crypto_des_done = 1;
218+
TDES_CLR_INT_FLAG();
204219
}
205220
}

targets/TARGET_NUVOTON/TARGET_NUC472/crypto/crypto-misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void crypto_prng_prestart(void);
6565
bool crypto_prng_wait(void);
6666
void crypto_aes_prestart(void);
6767
bool crypto_aes_wait(void);
68+
void crypto_des_prestart(void);
69+
bool crypto_des_wait(void);
6870

6971

7072
/* Check if buffer can be used for crypto DMA. It has the following requirements:

0 commit comments

Comments
 (0)