NeuG integrated
This commit is contained in:
33
ChangeLog
33
ChangeLog
@@ -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>
|
2011-07-22 NIIBE Yutaka <gniibe@fsij.org>
|
||||||
|
|
||||||
* boards/OLIMEX_STM32_H103/board.h (BOARD_NAME): Fixed.
|
* boards/OLIMEX_STM32_H103/board.h (BOARD_NAME): Fixed.
|
||||||
|
|||||||
@@ -62,6 +62,20 @@
|
|||||||
#define ADC_CHANNEL_SENSOR 16 /**< @brief Internal temperature sensor.*/
|
#define ADC_CHANNEL_SENSOR 16 /**< @brief Internal temperature sensor.*/
|
||||||
#define ADC_CHANNEL_VREFINT 17 /**< @brief Internal reference. */
|
#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. */
|
/* Driver pre-compile time settings. */
|
||||||
/*===========================================================================*/
|
/*===========================================================================*/
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ CSRC = $(PORTSRC) \
|
|||||||
main.c usb_lld.c \
|
main.c usb_lld.c \
|
||||||
usb_desc.c usb_prop.c \
|
usb_desc.c usb_prop.c \
|
||||||
usb-icc.c openpgp.c ac.c openpgp-do.c flash.c hardclock.c \
|
usb-icc.c openpgp.c ac.c openpgp-do.c flash.c hardclock.c \
|
||||||
random.c
|
random.c neug.c
|
||||||
|
|
||||||
ifneq ($(ENABLE_DEBUG),)
|
ifneq ($(ENABLE_DEBUG),)
|
||||||
CSRC += debug.c
|
CSRC += debug.c
|
||||||
|
|||||||
16
src/flash.c
16
src/flash.c
@@ -147,8 +147,6 @@ flash_erase_page (uint32_t addr)
|
|||||||
* .bss
|
* .bss
|
||||||
* _end
|
* _end
|
||||||
* <alignment to page>
|
* <alignment to page>
|
||||||
* random_bits_start
|
|
||||||
* <one page>
|
|
||||||
* ch_certificate_startp
|
* ch_certificate_startp
|
||||||
* <2048 bytes>
|
* <2048 bytes>
|
||||||
* _data_pool
|
* _data_pool
|
||||||
@@ -613,15 +611,6 @@ flash_erase_binary (uint8_t file_id)
|
|||||||
#endif
|
#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;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -641,11 +630,6 @@ flash_write_binary (uint8_t file_id, const uint8_t *data,
|
|||||||
maxsize = FLASH_CH_CERTIFICATE_SIZE;
|
maxsize = FLASH_CH_CERTIFICATE_SIZE;
|
||||||
p = &ch_certificate_start;
|
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)
|
else if (file_id == FILEID_SERIAL_NO)
|
||||||
{
|
{
|
||||||
maxsize = 6;
|
maxsize = 6;
|
||||||
|
|||||||
@@ -292,6 +292,7 @@ extern uint8_t admin_authorized;
|
|||||||
|
|
||||||
#define SIZE_PW_STATUS_BYTES 7
|
#define SIZE_PW_STATUS_BYTES 7
|
||||||
|
|
||||||
|
extern void random_init (void);
|
||||||
/* 16-byte random bytes */
|
/* 16-byte random bytes */
|
||||||
extern const uint8_t *random_bytes_get (void);
|
extern const uint8_t *random_bytes_get (void);
|
||||||
extern void random_bytes_free (const uint8_t *);
|
extern void random_bytes_free (const uint8_t *);
|
||||||
|
|||||||
@@ -27,8 +27,9 @@
|
|||||||
/*
|
/*
|
||||||
* ST32F103 memory setup.
|
* ST32F103 memory setup.
|
||||||
*/
|
*/
|
||||||
__main_stack_size__ = 0x0400;
|
__main_stack_size__ = 0x0200;
|
||||||
__stacks_total_size__ = __main_stack_size__;
|
__process_stack_size__ = 0x0200;
|
||||||
|
__stacks_total_size__ = __main_stack_size__ + __process_stack_size__;
|
||||||
|
|
||||||
MEMORY
|
MEMORY
|
||||||
{
|
{
|
||||||
@@ -114,14 +115,6 @@ SECTIONS
|
|||||||
PROVIDE(end = .);
|
PROVIDE(end = .);
|
||||||
_end = .;
|
_end = .;
|
||||||
|
|
||||||
.gnuk_random :
|
|
||||||
{
|
|
||||||
. = ALIGN (@FLASH_PAGE_SIZE@);
|
|
||||||
random_bits_start = .;
|
|
||||||
LONG(0xffffffff);
|
|
||||||
. = ALIGN (@FLASH_PAGE_SIZE@);
|
|
||||||
} > flash =0xffffffff
|
|
||||||
|
|
||||||
.gnuk_ch_certificate :
|
.gnuk_ch_certificate :
|
||||||
{
|
{
|
||||||
. = ALIGN (@FLASH_PAGE_SIZE@);
|
. = ALIGN (@FLASH_PAGE_SIZE@);
|
||||||
|
|||||||
@@ -213,7 +213,8 @@ main (int argc, char **argv)
|
|||||||
flash_unlock ();
|
flash_unlock ();
|
||||||
device_initialize_once ();
|
device_initialize_once ();
|
||||||
usb_lld_init ();
|
usb_lld_init ();
|
||||||
USB_Init();
|
USB_Init ();
|
||||||
|
random_init ();
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
stdout_init ();
|
stdout_init ();
|
||||||
|
|||||||
41
src/neug.c
41
src/neug.c
@@ -41,8 +41,7 @@ static Thread *rng_thread;
|
|||||||
*/
|
*/
|
||||||
static adcsample_t samp[ADC_GRP1_NUM_CHANNELS * ADC_GRP1_BUF_DEPTH];
|
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 (adcsample_t *buffer, size_t n);
|
||||||
static void adccb_err (ADCDriver *adcp, adcerror_t err);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ADC conversion group.
|
* ADC conversion group.
|
||||||
@@ -53,10 +52,8 @@ static void adccb_err (ADCDriver *adcp, adcerror_t err);
|
|||||||
static const ADCConversionGroup adcgrpcfg = {
|
static const ADCConversionGroup adcgrpcfg = {
|
||||||
FALSE,
|
FALSE,
|
||||||
ADC_GRP1_NUM_CHANNELS,
|
ADC_GRP1_NUM_CHANNELS,
|
||||||
adccb,
|
|
||||||
adccb_err,
|
|
||||||
0,
|
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),
|
ADC_SMPR1_SMP_SENSOR(ADC_SAMPLE_1P5) | ADC_SMPR1_SMP_VREF(ADC_SAMPLE_1P5),
|
||||||
0,
|
0,
|
||||||
ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
|
ADC_SQR1_NUM_CH(ADC_GRP1_NUM_CHANNELS),
|
||||||
@@ -67,21 +64,14 @@ static const ADCConversionGroup adcgrpcfg = {
|
|||||||
/*
|
/*
|
||||||
* ADC end conversion callback.
|
* 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;
|
(void) buffer; (void) n;
|
||||||
|
if (adcp->ad_state == ADC_COMPLETE)
|
||||||
chSysLockFromIsr();
|
chEvtSignalI (rng_thread, ADC_DATA_AVAILABLE);
|
||||||
if (adcp->state == ADC_COMPLETE)
|
|
||||||
chEvtSignalFlagsI (rng_thread, ADC_DATA_AVAILABLE);
|
|
||||||
chSysUnlockFromIsr();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void adccb_err (ADCDriver *adcp, adcerror_t err)
|
|
||||||
{
|
|
||||||
(void)adcp; (void)err;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TinyMT routines.
|
* TinyMT routines.
|
||||||
@@ -319,7 +309,7 @@ static int rng_gen (struct rng_rb *rb)
|
|||||||
| ((samp[4] & 0x01) << 4) | ((samp[5] & 0x01) << 5)
|
| ((samp[4] & 0x01) << 4) | ((samp[5] & 0x01) << 5)
|
||||||
| ((samp[6] & 0x01) << 6) | ((samp[7] & 0x01) << 7));
|
| ((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.
|
* Put a random byte to entropy pool.
|
||||||
@@ -356,7 +346,7 @@ static msg_t rng (void *arg)
|
|||||||
rng_thread = chThdSelf ();
|
rng_thread = chThdSelf ();
|
||||||
|
|
||||||
adcStart (&ADCD1, NULL);
|
adcStart (&ADCD1, NULL);
|
||||||
adcStartConversion (&ADCD1, &adcgrpcfg, samp, ADC_GRP1_BUF_DEPTH);
|
adcStartConversion (&ADCD1, &adcgrpcfg, samp, ADC_GRP1_BUF_DEPTH, adccb);
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@@ -372,7 +362,7 @@ static msg_t rng (void *arg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct rng_rb the_ring_buffer;
|
static struct rng_rb the_ring_buffer;
|
||||||
static WORKING_AREA(wa_rng, 64);
|
static WORKING_AREA(wa_rng, 128);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize NeuG.
|
* @brief Initialize NeuG.
|
||||||
@@ -450,3 +440,14 @@ neug_get (int kick)
|
|||||||
|
|
||||||
return v;
|
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 ();
|
||||||
|
}
|
||||||
|
|||||||
55
src/random.c
55
src/random.c
@@ -24,6 +24,21 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "ch.h"
|
#include "ch.h"
|
||||||
#include "gnuk.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
|
* Return pointer to random 16-byte
|
||||||
@@ -31,25 +46,8 @@
|
|||||||
const uint8_t *
|
const uint8_t *
|
||||||
random_bytes_get (void)
|
random_bytes_get (void)
|
||||||
{
|
{
|
||||||
uint32_t addr, addr0;
|
neug_wait_full ();
|
||||||
|
return (const uint8_t *)random_word;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -58,11 +56,8 @@ random_bytes_get (void)
|
|||||||
void
|
void
|
||||||
random_bytes_free (const uint8_t *p)
|
random_bytes_free (const uint8_t *p)
|
||||||
{
|
{
|
||||||
int i;
|
(void)p;
|
||||||
uint32_t addr = (uint32_t)p;
|
neug_flush ();
|
||||||
|
|
||||||
for (i = 0; i < 8; i++)
|
|
||||||
flash_clear_halfword (addr+i*2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -71,15 +66,5 @@ random_bytes_free (const uint8_t *p)
|
|||||||
uint32_t
|
uint32_t
|
||||||
get_salt (void)
|
get_salt (void)
|
||||||
{
|
{
|
||||||
const uint8_t *u = unique_device_id (); /* 12-byte unique id */
|
return neug_get (NEUG_KICK_FILLING);
|
||||||
uint32_t r = 0;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < 4; i++)
|
|
||||||
{
|
|
||||||
r <<= 8;
|
|
||||||
r |= u[hardclock () % 12];
|
|
||||||
}
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user