Use internal TRNG of Pico.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos 2024-09-06 15:16:34 +02:00
parent 3cbbb6a16c
commit 76ba851df1
No known key found for this signature in database
GPG key ID: C0095B7870A4CCD3
5 changed files with 8 additions and 48 deletions

View file

@ -371,6 +371,8 @@ if(NOT TARGET pico_keys_sdk)
else()
pico_add_library(pico_keys_sdk)
pico_add_extra_outputs(${CMAKE_PROJECT_NAME})
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE pico_keys_sdk pico_stdlib pico_multicore pico_rand hardware_flash pico_unique_id pico_aon_timer tinyusb_device tinyusb_board)
endif()
target_sources(pico_keys_sdk INTERFACE ${SOURCES})
target_include_directories(pico_keys_sdk INTERFACE ${INCLUDES})

View file

@ -29,36 +29,19 @@
#include "esp_compat.h"
#else
#include "pico/stdlib.h"
#include "hwrng.h"
#include "hardware/structs/rosc.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
#include "bsp/board.h"
#include "pico/time.h"
#include "pico/rand.h"
#endif
void adc_start() {
void hwrng_start() {
#if defined(ENABLE_EMULATION)
srand(time(0));
#elif defined(ESP_PLATFORM)
bootloader_random_enable();
#else
adc_init();
adc_gpio_init(27);
adc_select_input(1);
#endif
}
void adc_stop() {
}
#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM)
uint32_t adc_read() {
return 0;
}
#endif
static uint64_t random_word = 0xcbf29ce484222325;
static uint8_t ep_round = 0;
@ -81,17 +64,9 @@ static int ep_process() {
#elif defined(ESP_PLATFORM)
esp_fill_random((uint8_t *)&word, sizeof(word));
#else
for (int n = 0; n < 64; n++) {
uint8_t bit1, bit2;
do {
bit1 = rosc_hw->randombit & 0xff;
//sleep_ms(1);
bit2 = rosc_hw->randombit & 0xff;
} while (bit1 == bit2);
word = (word << 1) | bit1;
}
word = get_rand_64();
#endif
random_word ^= word ^ board_millis() ^ adc_read();
random_word ^= word ^ board_millis();
random_word *= 0x00000100000001B3;
if (++ep_round == 8) {
ep_round = 0;
@ -100,10 +75,6 @@ static int ep_process() {
return 0;
}
static const uint32_t *ep_output() {
return (uint32_t *) &random_word;
}
struct rng_rb {
uint32_t *buf;
uint8_t head, tail;
@ -113,8 +84,6 @@ struct rng_rb {
};
static void rb_init(struct rng_rb *rb, uint32_t *p, uint8_t size) {
#ifdef ENABLE_EMULATION
#endif
rb->buf = p;
rb->size = size;
rb->head = rb->tail = 0;
@ -156,8 +125,7 @@ void *neug_task() {
if ((n = ep_process())) {
int i;
const uint32_t *vp;
vp = ep_output();
const uint32_t *vp = (const uint32_t *) &random_word;
for (i = 0; i < n; i++) {
rb_add(rb, *vp++);
@ -174,7 +142,7 @@ void neug_init(uint32_t *buf, uint8_t size) {
rb_init(rb, buf, size);
adc_start();
hwrng_start();
ep_init();
}
@ -218,7 +186,3 @@ void neug_wait_full() {
neug_task();
}
}
void neug_fini(void) {
neug_get();
}

View file

@ -29,6 +29,5 @@ void neug_init(uint32_t *buf, uint8_t size);
uint32_t neug_get();
void neug_flush(void);
void neug_wait_full();
void neug_fini(void);
#endif

View file

@ -34,10 +34,6 @@ void random_init(void) {
}
}
void random_fini(void) {
neug_fini();
}
/*
* Return pointer to random 32-byte
*/

View file

@ -23,7 +23,6 @@
#include <stdint.h>
void random_init(void);
void random_fini(void);
/* 32-byte random bytes */
const uint8_t *random_bytes_get(size_t);