Skip to content

Commit 21970e3

Browse files
committed
M467: Seed PRNG with TRNG for SCAP
According to TRM, it is suggested PRNG be seeded by TRNG on every Crypto H/W reset.
1 parent d92d75e commit 21970e3

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

targets/TARGET_NUVOTON/TARGET_M460/crypto/crypto-misc.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "crypto-misc.h"
2929
#include "platform/SingletonPtr.h"
3030
#include "platform/PlatformMutex.h"
31+
#include "hal/trng_api.h"
3132

3233
/* Consideration for choosing proper synchronization mechanism
3334
*
@@ -93,8 +94,33 @@ void crypto_init(void)
9394
CLK_EnableModuleClock(CRPT_MODULE);
9495
SYS_ResetModule(CRPT_RST);
9596
SYS_LockReg(); // Lock protected register
96-
97+
9798
NVIC_EnableIRQ(CRPT_IRQn);
99+
100+
/* Seed PRNG with TRNG to enable SCAP
101+
*
102+
* According to TRM, it is suggested PRNG be seeded by TRNG on
103+
* every Crypto H/W reset.
104+
*
105+
* To serialize access to TRNG, we invoke Mbed OS TRNG HAL API whose
106+
* implementations are thread-safe, instead of BSP RNG driver.
107+
*/
108+
trng_t trng_ctx;
109+
trng_init(&trng_ctx);
110+
111+
/* Wait for PRNG free */
112+
while (CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk);
113+
114+
/* Reload seed from TRNG for the first time */
115+
CRPT->PRNG_CTL = (PRNG_KEY_SIZE_256 << CRPT_PRNG_CTL_KEYSZ_Pos) | CRPT_PRNG_CTL_START_Msk | CRPT_PRNG_CTL_SEEDRLD_Msk | PRNG_CTL_SEEDSRC_TRNG;
116+
117+
/* Wait for PRNG done */
118+
while (CRPT->PRNG_CTL & CRPT_PRNG_CTL_BUSY_Msk);
119+
120+
/* No reload seed for following times */
121+
CRPT->PRNG_CTL = 0;
122+
123+
trng_free(&trng_ctx);
98124
}
99125
core_util_critical_section_exit();
100126
}

0 commit comments

Comments
 (0)