From c38fcd7a5ff288502927cd8b5ddde37f7d7e5438 Mon Sep 17 00:00:00 2001 From: asterix Date: Fri, 15 Oct 2010 13:32:09 +0000 Subject: [PATCH] Add entropy generation for stm32. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4430 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/sec/random_stm32.c | 109 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 bertos/sec/random_stm32.c diff --git a/bertos/sec/random_stm32.c b/bertos/sec/random_stm32.c new file mode 100644 index 00000000..d321ef0b --- /dev/null +++ b/bertos/sec/random_stm32.c @@ -0,0 +1,109 @@ +/** + * \file + * + * + * \brief STM32 backend implementation entropy pulling. + * \author Daniele Basile + * + */ + +#include "random_p.h" + +#include + +#include + +#include + +struct stm32_adc *adc = (struct stm32_adc *)ADC1_BASE; + +/* + * Return the cpu core temperature in raw format + */ +INLINE uint16_t hw_readRawTemp(void) +{ + /* We sample only from one channel */ + adc->SQR1 |= BV(SQR1_SQ_LEN_SHIFT); + adc->SQR3 = (ADC_TEMP_CH & SQR3_SQ_MASK); + + /* Start convertion */ + adc->CR2 |= CR2_EXTTRIG_SWSTRT_SET; + + /* Wait in polling until conversion is done */ + while (!(adc->SR & BV(SR_EOC))) + cpu_relax(); + + /* Return the last converted data */ + return (uint16_t)adc->DR; +} + +INLINE void hw_initIntTemp(void) +{ + RCC->APB2ENR |= RCC_APB2_ADC1; + + /* Reset registry */ + adc->CR1 = 0; + adc->CR2 = 0; + adc->SQR1 = 0; + adc->SQR2 = 0; + adc->SQR3 = 0; + + /* + * Configure ADC + * - Regular mode + * - Wake up adc + * - Wake up temperature and Vrefint + */ + adc->CR2 |= BV(CR2_ADON) | ADC_EXTERNALTRIGCONV_NONE | BV(CR2_TSVREFE); + + /* Set 17.1usec sampling time*/ + adc->SMPR1 |= ((ADC_SAMPLETIME_239CYCLES5 << SMPR1_CH17) | (ADC_SAMPLETIME_239CYCLES5 << SMPR1_CH16)); +} + + +void random_pull_entropy(uint8_t *entropy, size_t len) +{ + // We use the internal temperature sensor of LM3S as a source of entropy. + // The last bit of the acquisition is very variable and with a decent distribution + // to consider it "entropic". It does not really matter because it will + // go through a randomness extractor anyway. + hw_initIntTemp(); + + for (size_t j=0; j