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