|
@@ -80,8 +80,6 @@ hydro_random_init(void)
|
|
|
const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
|
|
|
hydro_hash_state st;
|
|
|
uint16_t ebits = 0;
|
|
|
- uint16_t tc;
|
|
|
- bool a, b;
|
|
|
|
|
|
hydro_hash_init(&st, ctx, NULL);
|
|
|
|
|
@@ -99,6 +97,50 @@ 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[32];
|
|
|
+
|
|
|
+ hydro_hash_init(&st, ctx, NULL);
|
|
|
+
|
|
|
+ for (;;) {
|
|
|
+ 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) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ delay(10);
|
|
|
+ }
|
|
|
+ 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>
|
|
@@ -122,12 +164,28 @@ hydro_random_init(void)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+#elif defined(__wasi__)
|
|
|
+
|
|
|
+#include <unistd.h>
|
|
|
+
|
|
|
+static int
|
|
|
+hydro_random_init(void)
|
|
|
+{
|
|
|
+ if (getentropy(hydro_random_context.state,
|
|
|
+ sizeof hydro_random_context.state) != 0) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
#elif defined(__unix__)
|
|
|
|
|
|
#include <errno.h>
|
|
|
#include <fcntl.h>
|
|
|
#ifdef __linux__
|
|
|
-#include <poll.h>
|
|
|
+# include <poll.h>
|
|
|
#endif
|
|
|
#include <sys/types.h>
|
|
|
#include <unistd.h>
|
|
@@ -254,11 +312,11 @@ hydro_random_init(void)
|
|
|
}
|
|
|
|
|
|
#else
|
|
|
-#error Need an entropy source
|
|
|
+# error Need an entropy source
|
|
|
#endif
|
|
|
|
|
|
#else
|
|
|
-#error Unsupported platform
|
|
|
+# error Unsupported platform
|
|
|
#endif
|
|
|
|
|
|
static void
|