blob: 610f9c5f810387e1f1b2cfb7d53813384f8d0319 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * HAVEGE: HArdware Volatile Entropy Gathering and Expansion
3 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakker5121ce52009-01-03 21:22:43 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23/*
24 * The HAVEGE RNG was designed by Andre Seznec in 2002.
25 *
26 * http://www.irisa.fr/caps/projects/hipsor/publi.php
27 *
28 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
29 */
30
31#include <string.h>
32#include <time.h>
33
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#if defined(POLARSSL_HAVEGE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/havege.h"
39#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
41/* ------------------------------------------------------------------------
42 * On average, one iteration accesses two 8-word blocks in the havege WALK
43 * table, and generates 16 words in the RES array.
44 *
45 * The data read in the WALK table is updated and permuted after each use.
46 * The result of the hardware clock counter read is used for this update.
47 *
48 * 25 conditional tests are present. The conditional tests are grouped in
49 * two nested groups of 12 conditional tests and 1 test that controls the
50 * permutation; on average, there should be 6 tests executed and 3 of them
51 * should be mispredicted.
52 * ------------------------------------------------------------------------
53 */
54
55#define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
56
57#define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
58#define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
59
60#define TST1_LEAVE U1++; }
61#define TST2_LEAVE U2++; }
62
63#define ONE_ITERATION \
64 \
65 PTEST = PT1 >> 20; \
66 \
67 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
68 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
69 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
70 \
71 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
72 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
73 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
74 \
75 PTX = (PT1 >> 18) & 7; \
76 PT1 &= 0x1FFF; \
77 PT2 &= 0x1FFF; \
78 CLK = (int) hardclock(); \
79 \
80 i = 0; \
81 A = &WALK[PT1 ]; RES[i++] ^= *A; \
82 B = &WALK[PT2 ]; RES[i++] ^= *B; \
83 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
84 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
85 \
86 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
87 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
88 *B = IN ^ U1; \
89 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
90 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
91 \
92 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
93 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
94 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
95 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
96 \
97 if( PTEST & 1 ) SWAP( A, C ); \
98 \
99 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
100 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
101 *B = IN; CLK = (int) hardclock(); \
102 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
103 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
104 \
105 A = &WALK[PT1 ^ 4]; \
106 B = &WALK[PT2 ^ 1]; \
107 \
108 PTEST = PT2 >> 1; \
109 \
110 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
111 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
112 PTY = (PT2 >> 10) & 7; \
113 \
114 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
115 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
116 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
117 \
118 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
119 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
120 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
121 \
122 C = &WALK[PT1 ^ 5]; \
123 D = &WALK[PT2 ^ 5]; \
124 \
125 RES[i++] ^= *A; \
126 RES[i++] ^= *B; \
127 RES[i++] ^= *C; \
128 RES[i++] ^= *D; \
129 \
130 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
131 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
132 *B = IN ^ U2; \
133 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
134 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
135 \
136 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
137 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
138 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
139 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
140 \
141 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
142 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
143 *B = IN; \
144 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
145 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
146 \
147 PT1 = ( RES[(i - 8) ^ PTX] ^ \
148 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
149 PT1 ^= (PT2 ^ 0x10) & 0x10; \
150 \
151 for( n++, i = 0; i < 16; i++ ) \
152 hs->pool[n % COLLECT_SIZE] ^= RES[i];
153
154/*
155 * Entropy gathering function
156 */
157static void havege_fill( havege_state *hs )
158{
159 int i, n = 0;
160 int U1, U2, *A, *B, *C, *D;
161 int PT1, PT2, *WALK, RES[16];
162 int PTX, PTY, CLK, PTEST, IN;
163
164 WALK = hs->WALK;
165 PT1 = hs->PT1;
166 PT2 = hs->PT2;
167
168 PTX = U1 = 0;
169 PTY = U2 = 0;
170
171 memset( RES, 0, sizeof( RES ) );
172
173 while( n < COLLECT_SIZE * 4 )
174 {
175 ONE_ITERATION
176 ONE_ITERATION
177 ONE_ITERATION
178 ONE_ITERATION
179 }
180
181 hs->PT1 = PT1;
182 hs->PT2 = PT2;
183
184 hs->offset[0] = 0;
185 hs->offset[1] = COLLECT_SIZE / 2;
186}
187
188/*
189 * HAVEGE initialization
190 */
191void havege_init( havege_state *hs )
192{
193 memset( hs, 0, sizeof( havege_state ) );
194
195 havege_fill( hs );
196}
197
198/*
199 * HAVEGE rand function
200 */
201int havege_rand( void *p_rng )
202{
203 int ret;
204 havege_state *hs = (havege_state *) p_rng;
205
206 if( hs->offset[1] >= COLLECT_SIZE )
207 havege_fill( hs );
208
209 ret = hs->pool[hs->offset[0]++];
210 ret ^= hs->pool[hs->offset[1]++];
211
212 return( ret );
213}
214
Paul Bakker40e46942009-01-03 21:51:57 +0000215#if defined(POLARSSL_RAND_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217#include <stdio.h>
218
219int main( int argc, char *argv[] )
220{
221 FILE *f;
222 time_t t;
223 int i, j, k;
224 havege_state hs;
225 unsigned char buf[1024];
226
227 if( argc < 2 )
228 {
229 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
230 return( 1 );
231 }
232
233 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
234 {
235 printf( "failed to open '%s' for writing.\n", argv[0] );
236 return( 1 );
237 }
238
239 havege_init( &hs );
240
241 t = time( NULL );
242
243 for( i = 0, k = 32768; i < k; i++ )
244 {
245 for( j = 0; j < sizeof( buf ); j++ )
246 buf[j] = havege_rand( &hs );
247
248 fwrite( buf, sizeof( buf ), 1, f );
249
250 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
251 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
252 fflush( stdout );
253 }
254
255 if( t == time( NULL ) )
256 t--;
257
258 fclose( f );
259 return( 0 );
260}
261
262#endif
263
264#endif