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