mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-06-10 11:59:05 +02:00
Use internal TRNG of Pico.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -371,6 +371,8 @@ if(NOT TARGET pico_keys_sdk)
|
|||||||
else()
|
else()
|
||||||
pico_add_library(pico_keys_sdk)
|
pico_add_library(pico_keys_sdk)
|
||||||
pico_add_extra_outputs(${CMAKE_PROJECT_NAME})
|
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()
|
endif()
|
||||||
target_sources(pico_keys_sdk INTERFACE ${SOURCES})
|
target_sources(pico_keys_sdk INTERFACE ${SOURCES})
|
||||||
target_include_directories(pico_keys_sdk INTERFACE ${INCLUDES})
|
target_include_directories(pico_keys_sdk INTERFACE ${INCLUDES})
|
||||||
|
|||||||
@@ -29,36 +29,19 @@
|
|||||||
#include "esp_compat.h"
|
#include "esp_compat.h"
|
||||||
#else
|
#else
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
|
|
||||||
#include "hwrng.h"
|
#include "hwrng.h"
|
||||||
#include "hardware/structs/rosc.h"
|
|
||||||
#include "hardware/gpio.h"
|
|
||||||
#include "hardware/adc.h"
|
|
||||||
#include "bsp/board.h"
|
#include "bsp/board.h"
|
||||||
|
#include "pico/rand.h"
|
||||||
#include "pico/time.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void adc_start() {
|
void hwrng_start() {
|
||||||
#if defined(ENABLE_EMULATION)
|
#if defined(ENABLE_EMULATION)
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
#elif defined(ESP_PLATFORM)
|
#elif defined(ESP_PLATFORM)
|
||||||
bootloader_random_enable();
|
bootloader_random_enable();
|
||||||
#else
|
|
||||||
adc_init();
|
|
||||||
adc_gpio_init(27);
|
|
||||||
adc_select_input(1);
|
|
||||||
#endif
|
#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 uint64_t random_word = 0xcbf29ce484222325;
|
||||||
static uint8_t ep_round = 0;
|
static uint8_t ep_round = 0;
|
||||||
|
|
||||||
@@ -81,17 +64,9 @@ static int ep_process() {
|
|||||||
#elif defined(ESP_PLATFORM)
|
#elif defined(ESP_PLATFORM)
|
||||||
esp_fill_random((uint8_t *)&word, sizeof(word));
|
esp_fill_random((uint8_t *)&word, sizeof(word));
|
||||||
#else
|
#else
|
||||||
for (int n = 0; n < 64; n++) {
|
word = get_rand_64();
|
||||||
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;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
random_word ^= word ^ board_millis() ^ adc_read();
|
random_word ^= word ^ board_millis();
|
||||||
random_word *= 0x00000100000001B3;
|
random_word *= 0x00000100000001B3;
|
||||||
if (++ep_round == 8) {
|
if (++ep_round == 8) {
|
||||||
ep_round = 0;
|
ep_round = 0;
|
||||||
@@ -100,10 +75,6 @@ static int ep_process() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint32_t *ep_output() {
|
|
||||||
return (uint32_t *) &random_word;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct rng_rb {
|
struct rng_rb {
|
||||||
uint32_t *buf;
|
uint32_t *buf;
|
||||||
uint8_t head, tail;
|
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) {
|
static void rb_init(struct rng_rb *rb, uint32_t *p, uint8_t size) {
|
||||||
#ifdef ENABLE_EMULATION
|
|
||||||
#endif
|
|
||||||
rb->buf = p;
|
rb->buf = p;
|
||||||
rb->size = size;
|
rb->size = size;
|
||||||
rb->head = rb->tail = 0;
|
rb->head = rb->tail = 0;
|
||||||
@@ -156,8 +125,7 @@ void *neug_task() {
|
|||||||
|
|
||||||
if ((n = ep_process())) {
|
if ((n = ep_process())) {
|
||||||
int i;
|
int i;
|
||||||
const uint32_t *vp;
|
const uint32_t *vp = (const uint32_t *) &random_word;
|
||||||
vp = ep_output();
|
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
rb_add(rb, *vp++);
|
rb_add(rb, *vp++);
|
||||||
@@ -174,7 +142,7 @@ void neug_init(uint32_t *buf, uint8_t size) {
|
|||||||
|
|
||||||
rb_init(rb, buf, size);
|
rb_init(rb, buf, size);
|
||||||
|
|
||||||
adc_start();
|
hwrng_start();
|
||||||
|
|
||||||
ep_init();
|
ep_init();
|
||||||
}
|
}
|
||||||
@@ -218,7 +186,3 @@ void neug_wait_full() {
|
|||||||
neug_task();
|
neug_task();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void neug_fini(void) {
|
|
||||||
neug_get();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -29,6 +29,5 @@ void neug_init(uint32_t *buf, uint8_t size);
|
|||||||
uint32_t neug_get();
|
uint32_t neug_get();
|
||||||
void neug_flush(void);
|
void neug_flush(void);
|
||||||
void neug_wait_full();
|
void neug_wait_full();
|
||||||
void neug_fini(void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -34,10 +34,6 @@ void random_init(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void random_fini(void) {
|
|
||||||
neug_fini();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return pointer to random 32-byte
|
* Return pointer to random 32-byte
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -23,7 +23,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
void random_init(void);
|
void random_init(void);
|
||||||
void random_fini(void);
|
|
||||||
|
|
||||||
/* 32-byte random bytes */
|
/* 32-byte random bytes */
|
||||||
const uint8_t *random_bytes_get(size_t);
|
const uint8_t *random_bytes_get(size_t);
|
||||||
|
|||||||
Reference in New Issue
Block a user