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