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