Starting migration from gnuk to own solution.
gnuk/openpgp will be left as another pkcs15 app. Lots of work has been done in the meanwhile. Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
225
neug.c
225
neug.c
@@ -55,8 +55,8 @@ static uint8_t ep_round = 0;
|
||||
|
||||
static void ep_init (int mode)
|
||||
{
|
||||
random_word = 0xcbf29ce484222325;
|
||||
ep_round = 0;
|
||||
random_word = 0xcbf29ce484222325;
|
||||
ep_round = 0;
|
||||
}
|
||||
|
||||
/* Here, we assume a little endian architecture. */
|
||||
@@ -88,7 +88,6 @@ static int ep_process (int mode)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const uint32_t *ep_output (int mode)
|
||||
{
|
||||
(void) mode;
|
||||
@@ -99,49 +98,49 @@ static const uint32_t *ep_output (int mode)
|
||||
* 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;
|
||||
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)
|
||||
{
|
||||
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;
|
||||
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)
|
||||
{
|
||||
rb->buf[rb->tail++] = v;
|
||||
if (rb->tail == rb->size)
|
||||
rb->tail = 0;
|
||||
if (rb->tail == rb->head)
|
||||
rb->full = 1;
|
||||
rb->empty = 0;
|
||||
rb->buf[rb->tail++] = v;
|
||||
if (rb->tail == rb->size)
|
||||
rb->tail = 0;
|
||||
if (rb->tail == rb->head)
|
||||
rb->full = 1;
|
||||
rb->empty = 0;
|
||||
}
|
||||
|
||||
static uint32_t rb_del (struct rng_rb *rb)
|
||||
{
|
||||
uint32_t v = rb->buf[rb->head++];
|
||||
uint32_t v = rb->buf[rb->head++];
|
||||
|
||||
if (rb->head == rb->size)
|
||||
rb->head = 0;
|
||||
if (rb->head == rb->tail)
|
||||
rb->empty = 1;
|
||||
rb->full = 0;
|
||||
if (rb->head == rb->size)
|
||||
rb->head = 0;
|
||||
if (rb->head == rb->tail)
|
||||
rb->empty = 1;
|
||||
rb->full = 0;
|
||||
|
||||
return v;
|
||||
return v;
|
||||
}
|
||||
|
||||
uint8_t neug_mode;
|
||||
@@ -154,13 +153,12 @@ static struct rng_rb the_ring_buffer;
|
||||
/**
|
||||
* @brief Random number generation thread.
|
||||
*/
|
||||
void *
|
||||
neug_task ()
|
||||
void *neug_task ()
|
||||
{
|
||||
struct rng_rb *rb = &the_ring_buffer;
|
||||
int mode = neug_mode;
|
||||
struct rng_rb *rb = &the_ring_buffer;
|
||||
int mode = neug_mode;
|
||||
|
||||
rng_should_terminate = 0;
|
||||
rng_should_terminate = 0;
|
||||
//chopstx_mutex_init (&mode_mtx);
|
||||
//chopstx_cond_init (&mode_cond);
|
||||
|
||||
@@ -170,20 +168,20 @@ neug_task ()
|
||||
|
||||
if ((n = ep_process (mode)))
|
||||
{
|
||||
int i;
|
||||
const uint32_t *vp;
|
||||
int i;
|
||||
const uint32_t *vp;
|
||||
|
||||
vp = ep_output (mode);
|
||||
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++)
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
rb_add (rb, *vp++);
|
||||
if (rb->full)
|
||||
break;
|
||||
rb_add (rb, *vp++);
|
||||
if (rb->full)
|
||||
break;
|
||||
}
|
||||
|
||||
//chopstx_cond_signal (&rb->data_available);
|
||||
@@ -193,32 +191,31 @@ neug_task ()
|
||||
|
||||
//adc_stop ();
|
||||
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize NeuG.
|
||||
*/
|
||||
void
|
||||
neug_init (uint32_t *buf, uint8_t size)
|
||||
void neug_init (uint32_t *buf, uint8_t size)
|
||||
{
|
||||
const uint32_t *u = (const uint32_t *)unique_device_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);
|
||||
const uint32_t *u = (const uint32_t *)unique_device_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);
|
||||
@@ -227,14 +224,13 @@ neug_init (uint32_t *buf, uint8_t size)
|
||||
/**
|
||||
* @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)
|
||||
(void)rb_del (rb);
|
||||
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);
|
||||
}
|
||||
@@ -247,95 +243,36 @@ neug_flush (void)
|
||||
* 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;
|
||||
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);
|
||||
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);
|
||||
|
||||
return v;
|
||||
return v;
|
||||
}
|
||||
|
||||
int
|
||||
neug_get_nonblock (uint32_t *p)
|
||||
void neug_wait_full (void)
|
||||
{
|
||||
struct rng_rb *rb = &the_ring_buffer;
|
||||
int r = 0;
|
||||
struct rng_rb *rb = &the_ring_buffer;
|
||||
|
||||
//chopstx_mutex_lock (&rb->m);
|
||||
if (rb->empty)
|
||||
{
|
||||
r = -1;
|
||||
//chopstx_cond_signal (&rb->space_available);
|
||||
}
|
||||
else
|
||||
*p = rb_del (rb);
|
||||
//chopstx_mutex_unlock (&rb->m);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int neug_consume_random (void (*proc) (uint32_t, int))
|
||||
{
|
||||
int i = 0;
|
||||
struct rng_rb *rb = &the_ring_buffer;
|
||||
|
||||
//chopstx_mutex_lock (&rb->m);
|
||||
while (!rb->empty)
|
||||
{
|
||||
uint32_t v;
|
||||
|
||||
v = rb_del (rb);
|
||||
proc (v, i);
|
||||
i++;
|
||||
}
|
||||
//chopstx_cond_signal (&rb->space_available);
|
||||
//chopstx_mutex_unlock (&rb->m);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void
|
||||
neug_wait_full (void)
|
||||
{
|
||||
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);
|
||||
while (!rb->full)
|
||||
neug_task(); //chopstx_cond_wait (&rb->data_available, &rb->m);
|
||||
//chopstx_mutex_unlock (&rb->m);
|
||||
}
|
||||
|
||||
void
|
||||
neug_fini (void)
|
||||
void neug_fini (void)
|
||||
{
|
||||
rng_should_terminate = 1;
|
||||
neug_get (1);
|
||||
rng_should_terminate = 1;
|
||||
neug_get (1);
|
||||
//chopstx_join (rng_thread, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
neug_mode_select (uint8_t mode)
|
||||
{
|
||||
if (neug_mode == mode)
|
||||
return;
|
||||
|
||||
neug_wait_full ();
|
||||
|
||||
//chopstx_mutex_lock (&mode_mtx);
|
||||
neug_mode = mode;
|
||||
neug_flush ();
|
||||
//chopstx_cond_wait (&mode_cond, &mode_mtx);
|
||||
//chopstx_mutex_unlock (&mode_mtx);
|
||||
|
||||
neug_wait_full ();
|
||||
neug_flush ();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user