mbed

Fork of mbed-dev by mbed official

Revision:
181:96ed750bd169
Parent:
180:b0033dcd6934
--- a/targets/TARGET_NUVOTON/TARGET_M480/trng_api.c	Thu Dec 07 14:01:42 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M480/trng_api.c	Wed Jan 17 15:23:54 2018 +0000
@@ -21,6 +21,7 @@
 #include "cmsis.h"
 #include "us_ticker_api.h"
 #include "trng_api.h"
+#include "crypto-misc.h"
 
 /*
  * Get Random number generator.
@@ -28,25 +29,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;
@@ -54,8 +36,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);
 }
@@ -63,23 +46,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();
-
-    NVIC_EnableIRQ(CRPT_IRQn);
+    
+    /* Init crypto module */
+    crypto_init();
+    
     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)
@@ -98,11 +79,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
-