helpfor studient
Dependents: STM32_F103-C8T6basecanblink_led
Fork of mbed-dev by
Diff: targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c
- Revision:
- 181:96ed750bd169
- Parent:
- 180:b0033dcd6934
--- a/targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c Thu Dec 07 14:01:42 2017 +0000 +++ b/targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c Wed Jan 17 15:23:54 2018 +0000 @@ -23,9 +23,9 @@ #include <stdlib.h> #include <string.h> #include "cmsis.h" -#include "NUC472_442.h" #include "us_ticker_api.h" #include "trng_api.h" +#include "crypto-misc.h" /* * Get Random number generator. @@ -33,25 +33,6 @@ #define PRNG_KEY_SIZE (0x20UL) -static volatile int g_PRNG_done; -volatile int g_AES_done; - -/* Implementation that should never be optimized out by the compiler */ -static void trng_zeroize( void *v, size_t n ) { - volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; -} - -void CRYPTO_IRQHandler() -{ - if (PRNG_GET_INT_FLAG()) { - g_PRNG_done = 1; - PRNG_CLR_INT_FLAG(); - } else if (AES_GET_INT_FLAG()) { - g_AES_done = 1; - AES_CLR_INT_FLAG(); - } -} - static void trng_get(unsigned char *pConversionData) { uint32_t *p32ConversionData; @@ -59,8 +40,9 @@ p32ConversionData = (uint32_t *)pConversionData; PRNG_Open(PRNG_KEY_SIZE_256, 1, us_ticker_read()); + crypto_prng_prestart(); PRNG_Start(); - while (!g_PRNG_done); + crypto_prng_wait(); PRNG_Read(p32ConversionData); } @@ -68,23 +50,21 @@ void trng_init(trng_t *obj) { (void)obj; - /* Unlock protected registers */ - SYS_UnlockReg(); - /* Enable IP clock */ - CLK_EnableModuleClock(CRPT_MODULE); - /* Lock protected registers */ - SYS_LockReg(); + /* Init crypto module */ + crypto_init(); - NVIC_EnableIRQ(CRPT_IRQn); PRNG_ENABLE_INT(); } void trng_free(trng_t *obj) { (void)obj; + PRNG_DISABLE_INT(); - NVIC_DisableIRQ(CRPT_IRQn); + + /* Uninit crypto module */ + crypto_uninit(); } int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) @@ -103,11 +83,10 @@ trng_get(tmpBuff); memcpy(output, tmpBuff, length); cur_length += length; - trng_zeroize(tmpBuff, sizeof(tmpBuff)); + crypto_zeroize(tmpBuff, sizeof(tmpBuff)); } *output_length = cur_length; return 0; } - + #endif -