blob: 3acd5bca106d6a58129fd3c4284a636ae224ef6d [file] [log] [blame]
Paul Bakkerf3b86c12011-01-27 15:24:17 +00001/**
2 * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Paul Bakker9af723c2014-05-01 13:03:14 +02004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The HAVEGE RNG was designed by Andre Seznec in 2002.
27 *
28 * http://www.irisa.fr/caps/projects/hipsor/publi.php
29 *
30 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#if defined(POLARSSL_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/havege.h"
42#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Paul Bakker23986e52011-04-24 08:57:21 +000044#include <string.h>
Paul Bakker23986e52011-04-24 08:57:21 +000045
Paul Bakkera317a982014-06-18 16:44:11 +020046/* Implementation that should never be optimized out by the compiler */
47static void polarssl_zeroize( void *v, size_t n ) {
48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/* ------------------------------------------------------------------------
52 * On average, one iteration accesses two 8-word blocks in the havege WALK
53 * table, and generates 16 words in the RES array.
54 *
55 * The data read in the WALK table is updated and permuted after each use.
56 * The result of the hardware clock counter read is used for this update.
57 *
58 * 25 conditional tests are present. The conditional tests are grouped in
59 * two nested groups of 12 conditional tests and 1 test that controls the
60 * permutation; on average, there should be 6 tests executed and 3 of them
61 * should be mispredicted.
62 * ------------------------------------------------------------------------
63 */
64
65#define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
66
67#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
68#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
69
70#define TST1_LEAVE U1++; }
71#define TST2_LEAVE U2++; }
72
73#define ONE_ITERATION \
74 \
75 PTEST = PT1 >> 20; \
76 \
77 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
78 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
79 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
80 \
81 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
82 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
83 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
84 \
85 PTX = (PT1 >> 18) & 7; \
86 PT1 &= 0x1FFF; \
87 PT2 &= 0x1FFF; \
88 CLK = (int) hardclock(); \
89 \
90 i = 0; \
91 A = &WALK[PT1 ]; RES[i++] ^= *A; \
92 B = &WALK[PT2 ]; RES[i++] ^= *B; \
93 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
94 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
95 \
96 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
97 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
98 *B = IN ^ U1; \
99 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
100 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
101 \
102 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
103 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
104 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
105 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
106 \
107 if( PTEST & 1 ) SWAP( A, C ); \
108 \
109 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
110 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
111 *B = IN; CLK = (int) hardclock(); \
112 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
113 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
114 \
115 A = &WALK[PT1 ^ 4]; \
116 B = &WALK[PT2 ^ 1]; \
117 \
118 PTEST = PT2 >> 1; \
119 \
120 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
121 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
122 PTY = (PT2 >> 10) & 7; \
123 \
124 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
125 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
126 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
127 \
128 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
129 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
130 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
131 \
132 C = &WALK[PT1 ^ 5]; \
133 D = &WALK[PT2 ^ 5]; \
134 \
135 RES[i++] ^= *A; \
136 RES[i++] ^= *B; \
137 RES[i++] ^= *C; \
138 RES[i++] ^= *D; \
139 \
140 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
141 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
142 *B = IN ^ U2; \
143 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
144 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
145 \
146 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
147 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
148 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
149 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
150 \
151 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
152 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
153 *B = IN; \
154 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
155 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
156 \
Paul Bakker66d5d072014-06-17 16:39:18 +0200157 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
159 PT1 ^= (PT2 ^ 0x10) & 0x10; \
160 \
161 for( n++, i = 0; i < 16; i++ ) \
162 hs->pool[n % COLLECT_SIZE] ^= RES[i];
163
164/*
165 * Entropy gathering function
166 */
167static void havege_fill( havege_state *hs )
168{
169 int i, n = 0;
170 int U1, U2, *A, *B, *C, *D;
171 int PT1, PT2, *WALK, RES[16];
172 int PTX, PTY, CLK, PTEST, IN;
173
174 WALK = hs->WALK;
175 PT1 = hs->PT1;
176 PT2 = hs->PT2;
177
178 PTX = U1 = 0;
179 PTY = U2 = 0;
180
181 memset( RES, 0, sizeof( RES ) );
182
183 while( n < COLLECT_SIZE * 4 )
184 {
185 ONE_ITERATION
186 ONE_ITERATION
187 ONE_ITERATION
188 ONE_ITERATION
189 }
190
191 hs->PT1 = PT1;
192 hs->PT2 = PT2;
193
194 hs->offset[0] = 0;
195 hs->offset[1] = COLLECT_SIZE / 2;
196}
197
198/*
199 * HAVEGE initialization
200 */
201void havege_init( havege_state *hs )
202{
203 memset( hs, 0, sizeof( havege_state ) );
204
205 havege_fill( hs );
206}
207
Paul Bakkera317a982014-06-18 16:44:11 +0200208void havege_free( havege_state *hs )
209{
210 if( hs == NULL )
211 return;
212
213 polarssl_zeroize( hs, sizeof( havege_state ) );
214}
215
Paul Bakker5121ce52009-01-03 21:22:43 +0000216/*
217 * HAVEGE rand function
218 */
Paul Bakkera3d195c2011-11-27 21:07:34 +0000219int havege_random( void *p_rng, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220{
Paul Bakkera3d195c2011-11-27 21:07:34 +0000221 int val;
222 size_t use_len;
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 havege_state *hs = (havege_state *) p_rng;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000224 unsigned char *p = buf;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Paul Bakkera3d195c2011-11-27 21:07:34 +0000226 while( len > 0 )
227 {
228 use_len = len;
229 if( use_len > sizeof(int) )
230 use_len = sizeof(int);
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakkera3d195c2011-11-27 21:07:34 +0000232 if( hs->offset[1] >= COLLECT_SIZE )
233 havege_fill( hs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Paul Bakkera3d195c2011-11-27 21:07:34 +0000235 val = hs->pool[hs->offset[0]++];
236 val ^= hs->pool[hs->offset[1]++];
237
238 memcpy( p, &val, use_len );
Paul Bakker9af723c2014-05-01 13:03:14 +0200239
Paul Bakkera3d195c2011-11-27 21:07:34 +0000240 len -= use_len;
241 p += use_len;
242 }
243
244 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245}
246
Paul Bakker9af723c2014-05-01 13:03:14 +0200247#endif /* POLARSSL_HAVEGE_C */