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