random: integrate the random module into the wizard
[bertos.git] / bertos / sec / random.h
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  * $WIZ$ module_name = "random"
37  * $WIZ$ module_configuration = "bertos/cfg/cfg_random.h"
38  * $WIZ$ module_supports = "stm32 or lm3s"
39  */
40
41 #ifndef SEC_RANDOM_H
42 #define SEC_RANDOM_H
43
44 #include "cfg/cfg_random.h"
45 #include <cfg/compiler.h>
46
47 /**
48  * \name Security level definition
49  *
50  *
51  * Configure the security level required by the application.
52  *
53  * Application developers are suggested to keep the strongest
54  * setting (default) unless there are memory or code size issues.
55  *
56  * Available settings are:
57  *
58  *   * \a RANDOM_SECURITY_STRONG: The random library will use
59  *     an entropy pool, automatically feeded by drivers, to gather
60  *     entropy from hardware sources. Data from the pool will
61  *     be used to reseed a secure random number generator. Moreover,
62  *     the generator will be automatically initialised
63  *     with enough entropy to generate safe random numbers even
64  *     immediately after hw reset.
65  *     The overall structure is the same as used by modern
66  *         desktop PCs for generating secure random numbers.
67  *
68  *  * \a RANDOM_SECURITY_MEDIUM: This intermediate settings will
69  *     avoid usage of an entropy pool, to reduce memory and code
70  *     usage. The security of this settings relies only on the
71  *     good behaviour of the random number generator (even though
72  *     it will be well-seeded at startup).
73  *
74  *  * \a RANDOM_SECURITY_MINIMUM: This is the lighter setting that
75  *     allows minimal memory and code usage, and it suggested only
76  *     for extremely constrained systems, that only generates few
77  *     random numbers. Even if the generator is still secure on
78  *     paper, its seeding will not be safe (though still entropic
79  *     to allow different sequences to be generated after each reset).
80  *
81  * $WIZ$ random_level = "RANDOM_SECURITY_MINIMUM", "RANDOM_SECURITY_MEDIUM", "RANDOM_SECURITY_STRONG"
82  * \{
83  */
84 #define RANDOM_SECURITY_MINIMUM        0
85 #define RANDOM_SECURITY_MEDIUM         1
86 #define RANDOM_SECURITY_STRONG         2
87 /** \} */
88
89 void random_init(void);
90
91 void random_gen(uint8_t *out, size_t len);
92
93 INLINE uint8_t random_gen8(void)
94 {
95         uint8_t x;
96         random_gen(&x, 1);
97         return x;
98 }
99
100 INLINE uint16_t random_gen16(void)
101 {
102         uint8_t x;
103         random_gen(&x, 2);
104         return x;
105 }
106
107 INLINE uint32_t random_gen32(void)
108 {
109         uint8_t x;
110         random_gen(&x, 4);
111         return x;
112 }
113
114 #endif /* SEC_RANDOM_H */