Adding license headers.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2022-03-25 12:08:48 +01:00
parent 9c707df93b
commit a01bd39f21
18 changed files with 443 additions and 363 deletions

View File

@@ -1,69 +1,56 @@
/*
* neug.c - true random number generation
/*
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
* Copyright (c) 2022 Pol Henarejos.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* Copyright (C) 2011, 2012, 2013, 2016, 2017, 2018
* Free Software Initiative of Japan
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of NeuG, a True Random Number Generator
* implementation based on quantization error of ADC (for STM32F103).
*
* NeuG is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NeuG is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//Part of the code is taken from GnuK (GPLv3)
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "pico/stdlib.h"
//#include <chopstx.h>
#include "neug.h"
//#include "adc.h"
#include "hardware/structs/rosc.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
#include "bsp/board.h"
#include "pico/unique_id.h"
void adc_start ()
{
void adc_start() {
adc_init();
adc_gpio_init(27);
adc_select_input(1);
}
void
adc_stop (void)
{
void adc_stop() {
}
static uint64_t random_word = 0xcbf29ce484222325;
static uint8_t ep_round = 0;
static void ep_init (int mode)
{
static void ep_init() {
random_word = 0xcbf29ce484222325;
ep_round = 0;
}
/* Here, we assume a little endian architecture. */
static int ep_process (int mode)
{
if (ep_round == 0)
{
ep_init(mode);
static int ep_process () {
if (ep_round == 0) {
ep_init();
}
uint64_t word = 0x0;
for (int n = 0; n < 64; n++) {
@@ -78,48 +65,34 @@ static int ep_process (int mode)
}
random_word ^= word^board_millis()^adc_read();
random_word *= 0x00000100000001B3;
if (++ep_round == 8)
{
if (++ep_round == 8) {
ep_round = 0;
return 2; //2 words
}
return 0;
}
static const uint32_t *ep_output (int mode)
{
(void) mode;
static const uint32_t *ep_output() {
return (uint32_t *)&random_word;
}
/*
* Ring buffer, filled by generator, consumed by neug_get routine.
*/
struct rng_rb {
uint32_t *buf;
//chopstx_mutex_t m;
//chopstx_cond_t data_available;
//chopstx_cond_t space_available;
uint8_t head, tail;
uint8_t size;
unsigned int full :1;
unsigned int empty :1;
};
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) {
rb->buf = p;
rb->size = size;
//chopstx_mutex_init (&rb->m);
//chopstx_cond_init (&rb->data_available);
//chopstx_cond_init (&rb->space_available);
rb->head = rb->tail = 0;
rb->full = 0;
rb->empty = 1;
}
static void rb_add (struct rng_rb *rb, uint32_t v)
{
static void rb_add(struct rng_rb *rb, uint32_t v) {
rb->buf[rb->tail++] = v;
if (rb->tail == rb->size)
rb->tail = 0;
@@ -128,8 +101,7 @@ static void rb_add (struct rng_rb *rb, uint32_t v)
rb->empty = 0;
}
static uint32_t rb_del (struct rng_rb *rb)
{
static uint32_t rb_del(struct rng_rb *rb) {
uint32_t v = rb->buf[rb->head++];
if (rb->head == rb->size)
@@ -141,140 +113,70 @@ static uint32_t rb_del (struct rng_rb *rb)
return v;
}
uint8_t neug_mode;
static int rng_should_terminate;
static struct rng_rb the_ring_buffer;
//static chopstx_t rng_thread;
/**
* @brief Random number generation thread.
*/
void *neug_task ()
{
void *neug_task() {
struct rng_rb *rb = &the_ring_buffer;
int mode = neug_mode;
int n;
rng_should_terminate = 0;
//chopstx_mutex_init (&mode_mtx);
//chopstx_cond_init (&mode_cond);
if ((n = ep_process())) {
int i;
const uint32_t *vp;
//while (!rng_should_terminate)
{
int n;
vp = ep_output();
if ((n = ep_process (mode)))
{
int i;
const uint32_t *vp;
vp = ep_output (mode);
//chopstx_mutex_lock (&rb->m);
//while (rb->full)
//chopstx_cond_wait (&rb->space_available, &rb->m);
for (i = 0; i < n; i++)
{
rb_add (rb, *vp++);
if (rb->full)
break;
}
//chopstx_cond_signal (&rb->data_available);
//chopstx_mutex_unlock (&rb->m);
}
}
//adc_stop ();
for (i = 0; i < n; i++) {
rb_add (rb, *vp++);
if (rb->full)
break;
}
}
return NULL;
}
/**
* @brief Initialize NeuG.
*/
void neug_init (uint32_t *buf, uint8_t size)
{
void neug_init(uint32_t *buf, uint8_t size) {
pico_unique_board_id_t unique_id;
pico_get_unique_board_id(&unique_id);
const uint32_t *u = (const uint32_t *)unique_id.id;
struct rng_rb *rb = &the_ring_buffer;
int i;
/*
* This initialization ensures that it generates different sequence
* even if all physical conditions are same.
*/
neug_mode = NEUG_MODE_CONDITIONED;
rb_init (rb, buf, size);
/* Enable ADCs */
adc_start ();
ep_init (neug_mode);
//rng_thread = chopstx_create (PRIO_RNG, STACK_ADDR_RNG, STACK_SIZE_RNG,
// rng, rb);
rb_init(rb, buf, size);
adc_start();
ep_init();
}
/**
* @breif Flush random bytes.
*/
void neug_flush (void)
{
void neug_flush(void) {
struct rng_rb *rb = &the_ring_buffer;
//chopstx_mutex_lock (&rb->m);
while (!rb->empty)
rb_del (rb);
//chopstx_cond_signal (&rb->space_available);
//chopstx_mutex_unlock (&rb->m);
}
/**
* @brief Get random word (32-bit) from NeuG.
* @detail With NEUG_KICK_FILLING, it wakes up RNG thread.
* With NEUG_NO_KICK, it doesn't wake up RNG thread automatically,
* it is needed to call neug_kick_filling later.
*/
uint32_t neug_get (int kick)
{
uint32_t neug_get(int kick) {
struct rng_rb *rb = &the_ring_buffer;
uint32_t v;
//chopstx_mutex_lock (&rb->m);
while (rb->empty)
neug_task(); //chopstx_cond_wait (&rb->data_available, &rb->m);
v = rb_del (rb);
//if (kick)
//chopstx_cond_signal (&rb->space_available);
//chopstx_mutex_unlock (&rb->m);
neug_task();
v = rb_del(rb);
return v;
}
void neug_wait_full (void) //should be called only on core1
{
void neug_wait_full(void) { //should be called only on core1
struct rng_rb *rb = &the_ring_buffer;
//chopstx_mutex_lock (&rb->m);
while (!rb->full) {
//neug_task(); //chopstx_cond_wait (&rb->data_available, &rb->m);
sleep_ms(1);
}
//chopstx_mutex_unlock (&rb->m);
}
void neug_fini (void)
{
rng_should_terminate = 1;
neug_get (1);
//chopstx_join (rng_thread, NULL);
void neug_fini(void) {
neug_get(1);
}

View File

@@ -1,14 +1,29 @@
#define NEUG_NO_KICK 0
#define NEUG_KICK_FILLING 1
/*
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
* Copyright (c) 2022 Pol Henarejos.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _NEUG_H_
#define _NEUG_H_
#define NEUG_PRE_LOOP 32
#define NEUG_MODE_CONDITIONED 0 /* Conditioned data. */
#define NEUG_MODE_RAW 1 /* CRC-32 filtered sample data. */
#define NEUG_MODE_RAW_DATA 2 /* Sample data directly. */
void neug_init(uint32_t *buf, uint8_t size);
uint32_t neug_get();
void neug_flush(void);
void neug_wait_full(void);
void neug_fini(void);
void neug_init (uint32_t *buf, uint8_t size);
uint32_t neug_get (int kick);
void neug_flush (void);
void neug_wait_full (void);
void neug_fini (void);
#endif

View File

@@ -1,27 +1,21 @@
/*
* random.c -- get random bytes
/*
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
* Copyright (c) 2022 Pol Henarejos.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* Copyright (C) 2010, 2011, 2012, 2013, 2015
* Free Software Initiative of Japan
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Gnuk, a GnuPG USB Token implementation.
*
* Gnuk is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <string.h>
@@ -30,18 +24,16 @@
#define RANDOM_BYTES_LENGTH 32
static uint32_t random_word[RANDOM_BYTES_LENGTH/sizeof (uint32_t)];
void random_init (void)
{
void random_init(void) {
int i;
neug_init (random_word, RANDOM_BYTES_LENGTH/sizeof (uint32_t));
neug_init(random_word, RANDOM_BYTES_LENGTH/sizeof (uint32_t));
for (i = 0; i < NEUG_PRE_LOOP; i++)
neug_get (NEUG_KICK_FILLING);
neug_get();
}
void random_fini (void)
{
void random_fini(void) {
neug_fini ();
}
@@ -50,13 +42,12 @@ void random_fini (void)
*/
void random_bytes_free (const uint8_t *p);
#define MAX_RANDOM_BUFFER 1024
const uint8_t * random_bytes_get (size_t len)
{
const uint8_t * random_bytes_get(size_t len) {
if (len > MAX_RANDOM_BUFFER)
return NULL;
static uint32_t return_word[MAX_RANDOM_BUFFER/sizeof(uint32_t)];
for (int ix = 0; ix < len; ix += RANDOM_BYTES_LENGTH) {
neug_wait_full ();
neug_wait_full();
memcpy(return_word+ix/sizeof(uint32_t), random_word, RANDOM_BYTES_LENGTH);
random_bytes_free((const uint8_t *)random_word);
}
@@ -66,53 +57,48 @@ const uint8_t * random_bytes_get (size_t len)
/*
* Free pointer to random 32-byte
*/
void random_bytes_free (const uint8_t *p)
{
void random_bytes_free(const uint8_t *p) {
(void)p;
memset (random_word, 0, RANDOM_BYTES_LENGTH);
neug_flush ();
memset(random_word, 0, RANDOM_BYTES_LENGTH);
neug_flush();
}
/*
* Return 4-byte salt
*/
void random_get_salt (uint8_t *p)
{
void random_get_salt(uint8_t *p) {
uint32_t rnd;
rnd = neug_get (NEUG_KICK_FILLING);
memcpy (p, &rnd, sizeof (uint32_t));
rnd = neug_get (NEUG_KICK_FILLING);
memcpy (p + sizeof (uint32_t), &rnd, sizeof (uint32_t));
rnd = neug_get();
memcpy(p, &rnd, sizeof (uint32_t));
rnd = neug_get();
memcpy(p + sizeof (uint32_t), &rnd, sizeof (uint32_t));
}
/*
* Random byte iterator
*/
int random_gen (void *arg, unsigned char *out, size_t out_len)
{
int random_gen(void *arg, unsigned char *out, size_t out_len) {
uint8_t *index_p = (uint8_t *)arg;
uint8_t index = index_p ? *index_p : 0;
size_t n;
while (out_len)
{
neug_wait_full ();
while (out_len) {
neug_wait_full();
n = RANDOM_BYTES_LENGTH - index;
if (n > out_len)
n = out_len;
memcpy (out, ((unsigned char *)random_word) + index, n);
memcpy(out, ((unsigned char *)random_word) + index, n);
out += n;
out_len -= n;
index += n;
if (index >= RANDOM_BYTES_LENGTH)
{
if (index >= RANDOM_BYTES_LENGTH) {
index = 0;
neug_flush ();
neug_flush();
}
}

View File

@@ -1,3 +1,24 @@
/*
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
* Copyright (c) 2022 Pol Henarejos.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _RANDOM_H_
#define _RANDOM_H_
void random_init (void);
void random_fini (void);
@@ -10,3 +31,5 @@ void random_get_salt (uint8_t *p);
/* iterator returning a byta at a time */
int random_gen (void *arg, unsigned char *output, size_t output_len);
#endif