NeuG integrated

This commit is contained in:
NIIBE Yutaka
2011-10-06 16:56:08 +09:00
parent b1bb1354f8
commit 94f2c25c3b
9 changed files with 95 additions and 83 deletions

View File

@@ -1,3 +1,36 @@
2011-10-06 NIIBE Yutaka <gniibe@fsij.org>
* src/main.c (main): Call random_init.
* src/gnuk.ld.in (__process_stack_size__): Fix.
(.gnuk_random): Removed.
* src/flash.c (flash_erase_binary, flash_write_binary): Remove
support of random_byte in flash ROM.
* src/neug.c (adccb): Use old API (was: chEvtSignalFlagsI).
(adccb_err): Remove.
(rng_gen, rng): Add the last argument adccb for adcStartConversion:
This is old API of ADC driver.
(adcgrpcfg): Remove callbacks, add CONT and SWSTART: This is old
API of ADC driver.
(adccb): Remove the first argument: This is old API of ADC driver.
(neug_wait_full): New.
* ChibiOS_2.0.8/os/hal/platforms/STM32/adc_lld.h (ADC_SAMPLE_1P5):
Add (from new API).
* src/random.c (random_init): New.
(random_bytes_get, random_bytes_free, get_salt): Use NeuG.
* src/Makefile.in (CSRC): Add neug.c.
* src/neug.c: New. Verbatim copy of NeuG/src/random.c.
* boards/common/mcuconf-common.h (USE_STM32_ADC1): TRUE for NewG RNG.
* src/chconf.h (CH_USE_SEMAPHORES): TRUE as ADC driver requires it.
* src/halconf.h (CH_HAL_USE_ADC); TRUE for NewG RNG.
2011-07-22 NIIBE Yutaka <gniibe@fsij.org>
* boards/OLIMEX_STM32_H103/board.h (BOARD_NAME): Fixed.

View File

@@ -62,6 +62,20 @@
#define ADC_CHANNEL_SENSOR 16 /**< @brief Internal temperature sensor.*/
#define ADC_CHANNEL_VREFINT 17 /**< @brief Internal reference. */
/**
* @name Sampling rates
* @{
*/
#define ADC_SAMPLE_1P5 0 /**< @brief 1.5 cycles sampling time. */
#define ADC_SAMPLE_7P5 1 /**< @brief 7.5 cycles sampling time. */
#define ADC_SAMPLE_13P5 2 /**< @brief 13.5 cycles sampling time. */
#define ADC_SAMPLE_28P5 3 /**< @brief 28.5 cycles sampling time. */
#define ADC_SAMPLE_41P5 4 /**< @brief 41.5 cycles sampling time. */
#define ADC_SAMPLE_55P5 5 /**< @brief 55.5 cycles sampling time. */
#define ADC_SAMPLE_71P5 6 /**< @brief 71.5 cycles sampling time. */
#define ADC_SAMPLE_239P5 7 /**< @brief 239.5 cycles sampling time. */
/** @} */
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/

View File

@@ -91,7 +91,7 @@ CSRC = $(PORTSRC) \
main.c usb_lld.c \
usb_desc.c usb_prop.c \
usb-icc.c openpgp.c ac.c openpgp-do.c flash.c hardclock.c \
random.c
random.c neug.c
ifneq ($(ENABLE_DEBUG),)
CSRC += debug.c

View File

@@ -147,8 +147,6 @@ flash_erase_page (uint32_t addr)
* .bss
* _end
* <alignment to page>
* random_bits_start
* <one page>
* ch_certificate_startp
* <2048 bytes>
* _data_pool
@@ -613,15 +611,6 @@ flash_erase_binary (uint8_t file_id)
#endif
}
return 0;
}
else if (file_id == FILEID_RANDOM)
{
p = &random_bits_start;
if (flash_check_blank (p, FLASH_PAGE_SIZE) == 0)
flash_erase_page ((uint32_t)p);
return 0;
}
else
@@ -641,11 +630,6 @@ flash_write_binary (uint8_t file_id, const uint8_t *data,
maxsize = FLASH_CH_CERTIFICATE_SIZE;
p = &ch_certificate_start;
}
else if (file_id == FILEID_RANDOM)
{
maxsize = FLASH_PAGE_SIZE;
p = &random_bits_start;
}
else if (file_id == FILEID_SERIAL_NO)
{
maxsize = 6;

View File

@@ -292,6 +292,7 @@ extern uint8_t admin_authorized;
#define SIZE_PW_STATUS_BYTES 7
extern void random_init (void);
/* 16-byte random bytes */
extern const uint8_t *random_bytes_get (void);
extern void random_bytes_free (const uint8_t *);

View File

@@ -27,8 +27,9 @@
/*
* ST32F103 memory setup.
*/
__main_stack_size__ = 0x0400;
__stacks_total_size__ = __main_stack_size__;
__main_stack_size__ = 0x0200;
__process_stack_size__ = 0x0200;
__stacks_total_size__ = __main_stack_size__ + __process_stack_size__;
MEMORY
{
@@ -114,14 +115,6 @@ SECTIONS
PROVIDE(end = .);
_end = .;
.gnuk_random :
{
. = ALIGN (@FLASH_PAGE_SIZE@);
random_bits_start = .;
LONG(0xffffffff);
. = ALIGN (@FLASH_PAGE_SIZE@);
} > flash =0xffffffff
.gnuk_ch_certificate :
{
. = ALIGN (@FLASH_PAGE_SIZE@);

View File

@@ -214,6 +214,7 @@ main (int argc, char **argv)
device_initialize_once ();
usb_lld_init ();
USB_Init ();
random_init ();
#ifdef DEBUG
stdout_init ();

View File

@@ -41,8 +41,7 @@ static Thread *rng_thread;
*/
static adcsample_t samp[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
static void adccb (ADCDriver *adcp, adcsample_t *buffer, size_t n);
static void adccb_err (ADCDriver *adcp, adcerror_t err);
static void adccb (adcsample_t *buffer, size_t n);
/*
* ADC conversion group.
@@ -53,10 +52,8 @@ static void adccb_err (ADCDriver *adcp, adcerror_t err);
static const ADCConversionGroup adcgrpcfg = {
FALSE,
ADC_GRP1_NUM_CHANNELS,
adccb,
adccb_err,
0,
ADC_CR2_TSVREFE,
ADC_CR2_EXTSEL_SWSTART | ADC_CR2_TSVREFE | ADC_CR2_CONT,
ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_1P5) | ADC_SMPR1_SMP_VREF(ADC_SAMPLE_1P5),
0,
ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
@@ -67,21 +64,14 @@ static const ADCConversionGroup adcgrpcfg = {
/*
* ADC end conversion callback.
*/
static void adccb (ADCDriver *adcp, adcsample_t *buffer, size_t n)
static void adccb (adcsample_t *buffer, size_t n)
{
ADCDriver *adcp = &ADCD1;
(void) buffer; (void) n;
chSysLockFromIsr();
if (adcp->state == ADC_COMPLETE)
chEvtSignalFlagsI (rng_thread, ADC_DATA_AVAILABLE);
chSysUnlockFromIsr();
if (adcp->ad_state == ADC_COMPLETE)
chEvtSignalI (rng_thread, ADC_DATA_AVAILABLE);
}
static void adccb_err (ADCDriver *adcp, adcerror_t err)
{
(void)adcp; (void)err;
}
/*
* TinyMT routines.
@@ -319,7 +309,7 @@ static int rng_gen (struct rng_rb *rb)
| ((samp[4] & 0x01) << 4) | ((samp[5] & 0x01) << 5)
| ((samp[6] & 0x01) << 6) | ((samp[7] & 0x01) << 7));
adcStartConversion (&ADCD1, &adcgrpcfg, samp, ADC_GRP1_BUF_DEPTH);
adcStartConversion (&ADCD1, &adcgrpcfg, samp, ADC_GRP1_BUF_DEPTH, adccb);
/*
* Put a random byte to entropy pool.
@@ -356,7 +346,7 @@ static msg_t rng (void *arg)
rng_thread = chThdSelf ();
adcStart (&ADCD1, NULL);
adcStartConversion (&ADCD1, &adcgrpcfg, samp, ADC_GRP1_BUF_DEPTH);
adcStartConversion (&ADCD1, &adcgrpcfg, samp, ADC_GRP1_BUF_DEPTH, adccb);
while (1)
{
@@ -372,7 +362,7 @@ static msg_t rng (void *arg)
}
static struct rng_rb the_ring_buffer;
static WORKING_AREA(wa_rng, 64);
static WORKING_AREA(wa_rng, 128);
/**
* @brief Initialize NeuG.
@@ -450,3 +440,14 @@ neug_get (int kick)
return v;
}
void
neug_wait_full (void)
{
struct rng_rb *rb = &the_ring_buffer;
chMtxLock (&rb->m);
while (!rb->full)
chCondWait (&rb->data_available);
chMtxUnlock ();
}

View File

@@ -24,6 +24,21 @@
#include "config.h"
#include "ch.h"
#include "gnuk.h"
#include "neug.h"
#define RANDOM_BYTES_LENGTH 16
static uint32_t random_word[RANDOM_BYTES_LENGTH/sizeof (uint32_t)];
void
random_init (void)
{
int i;
neug_init (random_word, RANDOM_BYTES_LENGTH/sizeof (uint32_t));
for (i = 0; i < NEUG_PRE_LOOP; i++)
(void)neug_get (NEUG_KICK_FILLING);
}
/*
* Return pointer to random 16-byte
@@ -31,25 +46,8 @@
const uint8_t *
random_bytes_get (void)
{
uint32_t addr, addr0;
addr = (uint32_t)&random_bits_start + ((hardclock () << 4) & 0x3f0);
addr0 = addr;
while (1)
{
if (*(uint32_t *)addr != 0 && *(uint32_t *)addr != 0xffffffff)
break;
addr += 16;
if (addr >= ((uint32_t)&random_bits_start) + 1024)
addr = ((uint32_t)&random_bits_start);
if (addr == addr0)
fatal (FATAL_RANDOM);
}
return (const uint8_t *)addr;
neug_wait_full ();
return (const uint8_t *)random_word;
}
/*
@@ -58,11 +56,8 @@ random_bytes_get (void)
void
random_bytes_free (const uint8_t *p)
{
int i;
uint32_t addr = (uint32_t)p;
for (i = 0; i < 8; i++)
flash_clear_halfword (addr+i*2);
(void)p;
neug_flush ();
}
/*
@@ -71,15 +66,5 @@ random_bytes_free (const uint8_t *p)
uint32_t
get_salt (void)
{
const uint8_t *u = unique_device_id (); /* 12-byte unique id */
uint32_t r = 0;
int i;
for (i = 0; i < 4; i++)
{
r <<= 8;
r |= u[hardclock () % 12];
}
return r;
return neug_get (NEUG_KICK_FILLING);
}