mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Revision:
179:b0033dcd6934
Parent:
172:7d866c31b3c5
Child:
180:96ed750bd169
diff -r 79309dc6340a -r b0033dcd6934 targets/TARGET_NUVOTON/TARGET_M480/trng_api.c
--- a/targets/TARGET_NUVOTON/TARGET_M480/trng_api.c	Thu Nov 23 11:57:25 2017 +0000
+++ b/targets/TARGET_NUVOTON/TARGET_M480/trng_api.c	Thu Dec 07 14:01:42 2017 +0000
@@ -25,9 +25,17 @@
 /*
  * Get Random number generator.
  */
+
+#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()) {
@@ -77,21 +85,22 @@
 int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
 {
     (void)obj;
-
-    *output_length = 0;
-    if (length < 32) {
-        unsigned char tmpBuff[32];
+    unsigned char tmpBuff[PRNG_KEY_SIZE];
+    size_t cur_length = 0;
+    
+    while (length >= sizeof(tmpBuff)) {
+        trng_get(output);
+        output += sizeof(tmpBuff);
+        cur_length += sizeof(tmpBuff);
+        length -= sizeof(tmpBuff);
+    }
+    if (length > 0) {
         trng_get(tmpBuff);
-        memcpy(output, &tmpBuff, length);
-        *output_length = length;
-    } else {
-        for (unsigned i = 0; i < (length/32); i++) {
-            trng_get(output);
-            *output_length += 32;
-            output += 32;
-        }
+        memcpy(output, tmpBuff, length);
+        cur_length += length;
+        trng_zeroize(tmpBuff, sizeof(tmpBuff));
     }
-
+    *output_length = cur_length;
     return 0;
 }