Skip to content
Permalink
Browse files

Add support for nRF52832 boards using HW RNG

Tested on an Adafruit Feather nRF52832.
  • Loading branch information...
spinda committed Apr 5, 2019
1 parent e4d9b3a commit c4edbe52eaeedf5cff20a60396b2593e718840a2
Showing with 53 additions and 1 deletion.
  1. +52 −0 impl/random.h
  2. +1 −1 library.properties
@@ -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,4 +1,4 @@
architectures=avr
architectures=avr,nrf52
author=Frank Denis <libhydrogen@pureftpd.org>
category=Other
includes=hydrogen.h

0 comments on commit c4edbe5

Please sign in to comment.
You can’t perform that action at this time.