Parcourir la source

Add support for nRF52832 boards using HW RNG

Tested on an Adafruit Feather nRF52832.
Michael Smith il y a 6 ans
Parent
commit
c4edbe52ea
2 fichiers modifiés avec 53 ajouts et 1 suppressions
  1. 52 0
      impl/random.h
  2. 1 1
      library.properties

+ 52 - 0
impl/random.h

@@ -97,6 +97,58 @@ hydro_random_init(void)
     return 0;
 }
 
+#elif (defined(NRF52832_XXAA) || defined(NRF52832_XXAB)) && !defined(__unix__)
+
+// Important: The SoftDevice *must* be activated to enable reading from the RNG
+// http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Frng.html
+
+#include <nrf_soc.h>
+
+static int
+hydro_random_init(void)
+{
+    const char       ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
+    hydro_hash_state st;
+
+    const uint8_t    total_bytes = 32;
+    uint8_t          remaining_bytes = total_bytes;
+    uint8_t          available_bytes;
+
+    uint8_t          rand_buffer[total_bytes];
+
+    hydro_hash_init(&st, ctx, NULL);
+
+    while (true) {
+        if (sd_rand_application_bytes_available_get(&available_bytes) != NRF_SUCCESS) {
+            return -1;
+        }
+
+        if (available_bytes > 0) {
+            if (available_bytes > remaining_bytes) {
+                available_bytes = remaining_bytes;
+            }
+
+            if (sd_rand_application_vector_get(rand_buffer, available_bytes) != NRF_SUCCESS) {
+                return -1;
+            }
+
+            hydro_hash_update(&st, rand_buffer, total_bytes);
+            remaining_bytes -= available_bytes;
+        }
+
+        if (remaining_bytes > 0) {
+            delay(10);
+        } else {
+            break;
+        }
+    }
+
+    hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
+    hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
+
+    return 0;
+}
+
 #elif defined(_WIN32)
 
 #include <windows.h>

+ 1 - 1
library.properties

@@ -1,4 +1,4 @@
-architectures=avr
+architectures=avr,nrf52
 author=Frank Denis <libhydrogen@pureftpd.org>
 category=Other
 includes=hydrogen.h