324f4bdeb70d5c73684de60e6d2e1c9af182f651
[bertos.git] / bertos / sec / random_p.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 Internal function definitions for random
34  * \author Giovanni Bajo <rasky@develer.com>
35  *
36  */
37
38 #ifndef SEC_RANDOM_P_H
39 #define SEC_RANDOM_P_H
40
41 #include <cfg/compiler.h>
42 #include <sec/random.h>
43
44 /********************************************************************************/
45 /* Configuration of the random module                                           */
46 /********************************************************************************/
47
48 #define POOL_NONE       0
49 #define POOL_YARROW     1
50 #define POOL_NAME1      Yarrow
51
52 #define PRNG_ISAAC      1
53 #define PRNG_X917       2
54 #define PRNG_YARROW     3
55 #define PRNG_NAMEU1     ISAAC
56 #define PRNG_NAMEL1     isaac
57 #define PRNG_NAMEU2     X917
58 #define PRNG_NAMEL2     x917
59 #define PRNG_NAMEU3     Yarrow
60 #define PRNG_NAMEL3     yarrow
61
62 #define EXTRACTOR_NONE  0
63 #define EXTRACTOR_SHA1  1
64 #define EXTRACTOR_NAME1 SHA1
65
66 #if RANDOM_SECURITY_LEVEL == RANDOM_SECURITY_STRONG
67         #define CONFIG_RANDOM_POOL          POOL_YARROW
68         #define CONFIG_RANDOM_EXTRACTOR     EXTRACTOR_NONE   // not required with a pool
69         #define CONFIG_RANDOM_PRNG          PRNG_YARROW
70 #elif RANDOM_SECURITY_LEVEL == RANDOM_SECURITY_MEDIUM
71         #define CONFIG_RANDOM_POOL          POOL_NONE
72         #define CONFIG_RANDOM_EXTRACTOR     EXTRACTOR_SHA1
73         #define CONFIG_RANDOM_PRNG          PRNG_X917
74 #elif RANDOM_SECURITY_LEVEL == RANDOM_SECURITY_MINIMUM
75         #define CONFIG_RANDOM_POOL          POOL_NONE
76         #define CONFIG_RANDOM_EXTRACTOR     EXTRACTOR_NONE
77         #define CONFIG_RANDOM_PRNG          PRNG_ISAAC
78 #else
79         #error Unsupported random security level value
80 #endif
81
82 /***************************************************************************/
83 /* Internal functions used by BeRTOS drivers to push data into             */
84 /* the entropy pool                                                        */
85 /***************************************************************************/
86
87 #if CONFIG_RANDOM_POOL != POOL_NONE
88
89 enum EntropySource
90 {
91         ENTROPY_SOURCE_IRQ,
92         ENTROPY_SOURCE_ADC,
93 };
94
95 /*
96  * Add entropy to the global entropy pool.
97  */
98 void random_add_entropy(enum EntropySource source_idx,
99                                             const uint8_t *data, size_t len,
100                                                 int entropy);
101
102
103 /*
104  * Add entropy to the global interrupt pool based on the IRQ
105  * call time.
106  *
107  * This function can be called from interrupt handlers that are
108  * triggered at unpredictable intervals (so it should not be
109  * called from clock-driven interrupts like ADC, PWM, etc.).
110  *
111  */
112 void random_add_entropy_irq(int irq);
113
114 #endif
115
116 /*
117  * This hardware-dependent function can be used to pull raw
118  * entropy from a hardware source at startup only. It is used
119  * for initial seeding of the random generator and should not
120  * be used in different situations.
121  */
122 void random_pull_entropy(uint8_t *entropy, size_t len);
123
124 #endif /* SEC_RANDOM_P_H */