Remove tag files.
[bertos.git] / bertos / sec / random.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 High-level random number generation functions.
34  * \author Giovanni Bajo <rasky@develer.com>
35  *
36  */
37
38 #include "random.h"
39 #include "random_p.h"
40
41 #include <cfg/macros.h>
42 #include <drv/timer.h>
43 #include <sec/random.h>
44 #include <sec/prng.h>
45 #include <sec/entropy.h>
46 #include <sec/util.h>
47 #include <sec/hash/sha1.h>
48 #include <sec/prng/isaac.h>
49 #include <sec/prng/x917.h>
50 #include <sec/prng/yarrow.h>
51 #include <sec/entropy/yarrow_pool.h>
52
53 /********************************************************************************/
54 /* Configuration of the random module                                           */
55 /********************************************************************************/
56
57 #define POOL_CONTEXT          PP_CAT(PP_CAT(POOL_NAMEU, CONFIG_RANDOM_POOL), Context)
58 #define POOL_INIT             PP_CAT(PP_CAT(POOL_NAMEL, CONFIG_RANDOM_POOL), _init)
59
60 #define EXTRACTOR_STACKINIT   PP_CAT(PP_CAT(EXTRACTOR_NAME, CONFIG_RANDOM_EXTRACTOR), _stackinit)
61
62 #define PRNG_CONTEXT          PP_CAT(PP_CAT(PRNG_NAMEU, CONFIG_RANDOM_PRNG), Context)
63 #define PRNG_INIT             PP_CAT(PP_CAT(PRNG_NAMEL, CONFIG_RANDOM_PRNG), _init)
64
65
66 /********************************************************************************/
67 /* Global state                                                                 */
68 /********************************************************************************/
69
70 #if CONFIG_RANDOM_POOL != POOL_NONE
71 static POOL_CONTEXT epool_ctx;
72 static EntropyPool * const epool = (EntropyPool *)&epool_ctx;
73 #endif
74
75 static PRNG_CONTEXT prng_ctx;
76 static PRNG * const prng = (PRNG*)&prng_ctx;
77
78 static bool initialized = 0;
79
80
81 /********************************************************************************/
82 /* Code                                                                         */
83 /********************************************************************************/
84
85 /*
86  * Reseed the PRNG if there is enough entropy available at this time.
87  *
88  * Some designs (eg: fortuna) suggest to artificially limit the frequency of
89  * this operation to something like 0.1s, to avoid attacks that try to exhaust
90  * the entropy pool.
91  *
92  * We don't believe such attacks are available in an embedded system (as an attacker
93  * does not have a way to ask random numbers from the pool) but we will play safe
94  * here in case eg. the user does something wrong.
95  */
96 static void optional_reseeding(void)
97 {
98 #if CONFIG_RANDOM_POOL != POOL_NONE
99         static ticks_t last_reseed = -1000;
100
101         // We don't allow more than 10 reseedings per second
102         // (as suggested by Fortuna)
103         ticks_t current = timer_clock();
104         if (ticks_to_ms(current - last_reseed) < 100)
105                 return;
106
107         if (entropy_seeding_ready(epool))
108         {
109                 uint8_t seed[prng_seed_len(prng)];
110
111                 entropy_make_seed(epool, seed, sizeof(seed));
112                 prng_reseed(prng, seed);
113
114                 last_reseed = current;
115                 PURGE(seed);
116         }
117 #endif
118 }
119
120
121 /*
122  * Perform the initial seeding of the PRNG.
123  *
124  * At startup, we want to immediately seed the PRNG to a point where it can
125  * generate safe-enough random numbers. To do this, we rely on a hw-dependent
126  * function to pull entropy from available hw sources, and then feed it
127  * through an extractor (if any is configured).
128  */
129 static void initial_seeding(void)
130 {
131 #if CONFIG_RANDOM_POOL != POOL_NONE
132
133         // We feed entropy into the pool, until it is ready to perform a seeding.
134         do
135         {
136                 uint8_t buf[16];
137                 random_pull_entropy(buf, sizeof(buf));
138                 entropy_add(epool, 0, buf, sizeof(buf), sizeof(buf)*8);
139         } while (!entropy_seeding_ready(epool));
140
141         optional_reseeding();
142
143 #elif CONFIG_RANDOM_EXTRACTOR != EXTRACTOR_NONE
144
145         uint8_t seed[prng_seed_len(prng)];
146         Hash *h = EXTRACTOR_STACKINIT();
147
148         // "Randomness Extraction and Key Derivation Using the CBC, Cascade and
149         // HMAC Modes" by Yevgeniy Dodis et al. suggests that an padded cascaded hash
150         // function with N bits of output must have at least 2n bits of min-entropy as input
151         // to be a randomness extractor (it generates an output that is computationally
152         // indistiguishable from an uniform distribution).
153         size_t hlen = hash_digest_len(h);
154         uint8_t buf[hlen*2];
155         size_t sidx = 0;
156
157         while (sidx < sizeof(seed))
158         {
159                 size_t cnt = MIN(sizeof(seed) - sidx, hlen);
160
161                 random_pull_entropy(buf, sizeof(buf));
162
163                 hash_begin(h);
164                 hash_update(h, buf, sizeof(buf));
165                 memcpy(seed+sidx, hash_final(h), cnt);
166                 sidx += cnt;
167         }
168
169         prng_reseed(prng, seed);
170         PURGE(buf);
171         PURGE(seed);
172
173 #else
174
175         // If we have been configured without a proper randomness extractor, we do not
176         // have many solutions but feeding the entropy bits directly to the PRNG, and
177         // hoping for the best.
178         uint8_t seed[prng_seed_len(prng)];
179         random_pull_entropy(seed, sizeof(seed));
180         prng_reseed(prng, seed);
181         PURGE(seed);
182
183 #endif
184 }
185
186 void random_init(void)
187 {
188 #if CONFIG_RANDOM_POOL != POOL_NONE
189         POOL_INIT(&epool_ctx);
190 #endif
191         PRNG_INIT(&prng_ctx);
192
193         initialized = 1;
194         initial_seeding();
195 }
196
197 void random_gen(uint8_t *out, size_t len)
198 {
199         ASSERT(initialized);
200
201         optional_reseeding();
202         prng_generate(prng, out, len);
203 }
204
205 #if CONFIG_RANDOM_POOL != POOL_NONE
206
207 void random_add_entropy(enum EntropySource source_idx,
208                         const uint8_t *data, size_t len,
209                         int entropy)
210 {
211         entropy_add(epool, source_idx, data, len, entropy);
212 }
213
214 #endif