bugfixes; build system changed again %-)
[syren.git] / src / xyssl / havege.c
blob3b619f273f96fd926a4a6a0443d1e6a28d566828
1 /*
2 * HAVEGE: HArdware Volatile Entropy Gathering and Expansion
4 * Copyright (C) 2006-2007 Christophe Devine
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License, version 2.1 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
21 * The HAVEGE RNG was designed by Andre Seznec in 2002.
23 * http://www.irisa.fr/caps/projects/hipsor/publi.php
25 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
28 #include <string.h>
29 #include <time.h>
31 #include "xyssl/config.h"
33 #if defined(XYSSL_HAVEGE_C)
35 #include "xyssl/havege.h"
36 #include "xyssl/timing.h"
38 /* ------------------------------------------------------------------------
39 * On average, one iteration accesses two 8-word blocks in the havege WALK
40 * table, and generates 16 words in the RES array.
42 * The data read in the WALK table is updated and permuted after each use.
43 * The result of the hardware clock counter read is used for this update.
45 * 25 conditional tests are present. The conditional tests are grouped in
46 * two nested groups of 12 conditional tests and 1 test that controls the
47 * permutation; on average, there should be 6 tests executed and 3 of them
48 * should be mispredicted.
49 * ------------------------------------------------------------------------
52 #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
54 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
55 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
57 #define TST1_LEAVE U1++; }
58 #define TST2_LEAVE U2++; }
60 #define ONE_ITERATION \
62 PTEST = PT1 >> 20; \
64 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
65 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
66 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \
68 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
69 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
70 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \
72 PTX = (PT1 >> 18) & 7; \
73 PT1 &= 0x1FFF; \
74 PT2 &= 0x1FFF; \
75 CLK = (int) hardclock(); \
77 i = 0; \
78 A = &WALK[PT1 ]; RES[i++] ^= *A; \
79 B = &WALK[PT2 ]; RES[i++] ^= *B; \
80 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \
81 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \
83 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \
84 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \
85 *B = IN ^ U1; \
86 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \
87 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \
89 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \
90 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \
91 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \
92 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \
94 if( PTEST & 1 ) SWAP( A, C ); \
96 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \
97 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \
98 *B = IN; CLK = (int) hardclock(); \
99 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \
100 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \
102 A = &WALK[PT1 ^ 4]; \
103 B = &WALK[PT2 ^ 1]; \
105 PTEST = PT2 >> 1; \
107 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \
108 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \
109 PTY = (PT2 >> 10) & 7; \
111 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
112 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
113 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \
115 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
116 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
117 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \
119 C = &WALK[PT1 ^ 5]; \
120 D = &WALK[PT2 ^ 5]; \
122 RES[i++] ^= *A; \
123 RES[i++] ^= *B; \
124 RES[i++] ^= *C; \
125 RES[i++] ^= *D; \
127 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \
128 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \
129 *B = IN ^ U2; \
130 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \
131 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \
133 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \
134 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \
135 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \
136 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \
138 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \
139 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \
140 *B = IN; \
141 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \
142 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \
144 PT1 = ( RES[(i - 8) ^ PTX] ^ \
145 WALK[PT1 ^ PTX ^ 7] ) & (~1); \
146 PT1 ^= (PT2 ^ 0x10) & 0x10; \
148 for( n++, i = 0; i < 16; i++ ) \
149 hs->pool[n % COLLECT_SIZE] ^= RES[i];
152 * Entropy gathering function
154 static void havege_fill( havege_state *hs )
156 int i, n = 0;
157 int U1, U2, *A, *B, *C, *D;
158 int PT1, PT2, *WALK, RES[16];
159 int PTX, PTY, CLK, PTEST, IN;
161 WALK = hs->WALK;
162 PT1 = hs->PT1;
163 PT2 = hs->PT2;
165 PTX = U1 = 0;
166 PTY = U2 = 0;
168 memset( RES, 0, sizeof( RES ) );
170 while( n < COLLECT_SIZE * 4 )
172 ONE_ITERATION
173 ONE_ITERATION
174 ONE_ITERATION
175 ONE_ITERATION
178 hs->PT1 = PT1;
179 hs->PT2 = PT2;
181 hs->offset[0] = 0;
182 hs->offset[1] = COLLECT_SIZE / 2;
186 * HAVEGE initialization
188 void havege_init( havege_state *hs )
190 memset( hs, 0, sizeof( havege_state ) );
192 havege_fill( hs );
196 * HAVEGE rand function
198 int havege_rand( void *p_rng )
200 int ret;
201 havege_state *hs = (havege_state *) p_rng;
203 if( hs->offset[1] >= COLLECT_SIZE )
204 havege_fill( hs );
206 ret = hs->pool[hs->offset[0]++];
207 ret ^= hs->pool[hs->offset[1]++];
209 return( ret );
212 #if defined(XYSSL_RAND_TEST)
214 #include <stdio.h>
216 int main( int argc, char *argv[] )
218 FILE *f;
219 time_t t;
220 int i, j, k;
221 havege_state hs;
222 unsigned char buf[1024];
224 if( argc < 2 )
226 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
227 return( 1 );
230 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
232 printf( "failed to open '%s' for writing.\n", argv[0] );
233 return( 1 );
236 havege_init( &hs );
238 t = time( NULL );
240 for( i = 0, k = 32768; i < k; i++ )
242 for( j = 0; j < sizeof( buf ); j++ )
243 buf[j] = havege_rand( &hs );
245 fwrite( buf, sizeof( buf ), 1, f );
247 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
248 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
249 fflush( stdout );
252 if( t == time( NULL ) )
253 t--;
255 fclose( f );
256 return( 0 );
259 #endif
261 #endif