Add entropy generation for stm32.
[bertos.git] / bertos / sec / random_stm32.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief STM32 backend implementation entropy pulling.
34  * \author Daniele Basile <asterix@develer.com>
35  *
36  */
37
38 #include "random_p.h"
39
40 #include <cpu/power.h>
41
42 #include <drv/clock_cm3.h>
43
44 #include <io/cm3.h>
45
46 struct stm32_adc *adc = (struct stm32_adc *)ADC1_BASE;
47
48 /*
49  * Return the cpu core temperature in raw format
50  */
51 INLINE uint16_t hw_readRawTemp(void)
52 {
53         /* We sample only from one channel */
54         adc->SQR1 |= BV(SQR1_SQ_LEN_SHIFT);
55         adc->SQR3 = (ADC_TEMP_CH & SQR3_SQ_MASK);
56
57         /* Start convertion */
58         adc->CR2 |= CR2_EXTTRIG_SWSTRT_SET;
59
60         /* Wait in polling until conversion is done */
61         while (!(adc->SR & BV(SR_EOC)))
62                 cpu_relax();
63
64         /* Return the last converted data */
65     return (uint16_t)adc->DR;
66 }
67
68 INLINE void hw_initIntTemp(void)
69 {
70         RCC->APB2ENR |= RCC_APB2_ADC1;
71
72         /* Reset registry */
73         adc->CR1 = 0;
74         adc->CR2 = 0;
75         adc->SQR1 = 0;
76         adc->SQR2 = 0;
77         adc->SQR3 = 0;
78
79         /*
80          * Configure ADC
81          *  - Regular mode
82          *  - Wake up adc
83          *  - Wake up temperature and Vrefint
84          */
85         adc->CR2 |= BV(CR2_ADON) | ADC_EXTERNALTRIGCONV_NONE | BV(CR2_TSVREFE);
86
87         /* Set 17.1usec sampling time*/
88         adc->SMPR1 |= ((ADC_SAMPLETIME_239CYCLES5 << SMPR1_CH17) | (ADC_SAMPLETIME_239CYCLES5 << SMPR1_CH16));
89 }
90
91
92 void random_pull_entropy(uint8_t *entropy, size_t len)
93 {
94         // We use the internal temperature sensor of LM3S as a source of entropy.
95         // The last bit of the acquisition is very variable and with a decent distribution
96         // to consider it "entropic". It does not really matter because it will
97         // go through a randomness extractor anyway.
98         hw_initIntTemp();
99
100         for (size_t j=0; j<len; j++)
101         {
102                 uint8_t accum = 0;
103                 for (int b=0; b<8; ++b)
104                         if (hw_readRawTemp() & 1)
105                                 accum |= 1<<b;
106
107                 *entropy++ = accum;
108         }
109 }