| Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | *  \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3 | * | 
| Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 31 | #include "polarssl/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else | 
|  | 33 | #include POLARSSL_CONFIG_FILE | 
|  | 34 | #endif | 
| 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 | #if defined(POLARSSL_HAVEGE_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 38 | #include "polarssl/havege.h" | 
|  | 39 | #include "polarssl/timing.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 41 | #include <string.h> | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 42 |  | 
| Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 43 | /* Implementation that should never be optimized out by the compiler */ | 
|  | 44 | static void polarssl_zeroize( void *v, size_t n ) { | 
|  | 45 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; | 
|  | 46 | } | 
|  | 47 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | /* ------------------------------------------------------------------------ | 
|  | 49 | * On average, one iteration accesses two 8-word blocks in the havege WALK | 
|  | 50 | * table, and generates 16 words in the RES array. | 
|  | 51 | * | 
|  | 52 | * The data read in the WALK table is updated and permuted after each use. | 
|  | 53 | * The result of the hardware clock counter read is used  for this update. | 
|  | 54 | * | 
|  | 55 | * 25 conditional tests are present.  The conditional tests are grouped in | 
|  | 56 | * two nested  groups of 12 conditional tests and 1 test that controls the | 
|  | 57 | * permutation; on average, there should be 6 tests executed and 3 of them | 
|  | 58 | * should be mispredicted. | 
|  | 59 | * ------------------------------------------------------------------------ | 
|  | 60 | */ | 
|  | 61 |  | 
|  | 62 | #define SWAP(X,Y) { int *T = X; X = Y; Y = T; } | 
|  | 63 |  | 
|  | 64 | #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; | 
|  | 65 | #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; | 
|  | 66 |  | 
|  | 67 | #define TST1_LEAVE U1++; } | 
|  | 68 | #define TST2_LEAVE U2++; } | 
|  | 69 |  | 
|  | 70 | #define ONE_ITERATION                                   \ | 
|  | 71 | \ | 
|  | 72 | PTEST = PT1 >> 20;                                  \ | 
|  | 73 | \ | 
|  | 74 | TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \ | 
|  | 75 | TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \ | 
|  | 76 | TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \ | 
|  | 77 | \ | 
|  | 78 | TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \ | 
|  | 79 | TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \ | 
|  | 80 | TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \ | 
|  | 81 | \ | 
|  | 82 | PTX = (PT1 >> 18) & 7;                              \ | 
|  | 83 | PT1 &= 0x1FFF;                                      \ | 
|  | 84 | PT2 &= 0x1FFF;                                      \ | 
|  | 85 | CLK = (int) hardclock();                            \ | 
|  | 86 | \ | 
|  | 87 | i = 0;                                              \ | 
|  | 88 | A = &WALK[PT1    ]; RES[i++] ^= *A;                 \ | 
|  | 89 | B = &WALK[PT2    ]; RES[i++] ^= *B;                 \ | 
|  | 90 | C = &WALK[PT1 ^ 1]; RES[i++] ^= *C;                 \ | 
|  | 91 | D = &WALK[PT2 ^ 4]; RES[i++] ^= *D;                 \ | 
|  | 92 | \ | 
|  | 93 | IN = (*A >> (1)) ^ (*A << (31)) ^ CLK;              \ | 
|  | 94 | *A = (*B >> (2)) ^ (*B << (30)) ^ CLK;              \ | 
|  | 95 | *B = IN ^ U1;                                       \ | 
|  | 96 | *C = (*C >> (3)) ^ (*C << (29)) ^ CLK;              \ | 
|  | 97 | *D = (*D >> (4)) ^ (*D << (28)) ^ CLK;              \ | 
|  | 98 | \ | 
|  | 99 | A = &WALK[PT1 ^ 2]; RES[i++] ^= *A;                 \ | 
|  | 100 | B = &WALK[PT2 ^ 2]; RES[i++] ^= *B;                 \ | 
|  | 101 | C = &WALK[PT1 ^ 3]; RES[i++] ^= *C;                 \ | 
|  | 102 | D = &WALK[PT2 ^ 6]; RES[i++] ^= *D;                 \ | 
|  | 103 | \ | 
|  | 104 | if( PTEST & 1 ) SWAP( A, C );                       \ | 
|  | 105 | \ | 
|  | 106 | IN = (*A >> (5)) ^ (*A << (27)) ^ CLK;              \ | 
|  | 107 | *A = (*B >> (6)) ^ (*B << (26)) ^ CLK;              \ | 
|  | 108 | *B = IN; CLK = (int) hardclock();                   \ | 
|  | 109 | *C = (*C >> (7)) ^ (*C << (25)) ^ CLK;              \ | 
|  | 110 | *D = (*D >> (8)) ^ (*D << (24)) ^ CLK;              \ | 
|  | 111 | \ | 
|  | 112 | A = &WALK[PT1 ^ 4];                                 \ | 
|  | 113 | B = &WALK[PT2 ^ 1];                                 \ | 
|  | 114 | \ | 
|  | 115 | PTEST = PT2 >> 1;                                   \ | 
|  | 116 | \ | 
|  | 117 | PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]);   \ | 
|  | 118 | PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8);  \ | 
|  | 119 | PTY = (PT2 >> 10) & 7;                              \ | 
|  | 120 | \ | 
|  | 121 | TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \ | 
|  | 122 | TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \ | 
|  | 123 | TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \ | 
|  | 124 | \ | 
|  | 125 | TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \ | 
|  | 126 | TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \ | 
|  | 127 | TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \ | 
|  | 128 | \ | 
|  | 129 | C = &WALK[PT1 ^ 5];                                 \ | 
|  | 130 | D = &WALK[PT2 ^ 5];                                 \ | 
|  | 131 | \ | 
|  | 132 | RES[i++] ^= *A;                                     \ | 
|  | 133 | RES[i++] ^= *B;                                     \ | 
|  | 134 | RES[i++] ^= *C;                                     \ | 
|  | 135 | RES[i++] ^= *D;                                     \ | 
|  | 136 | \ | 
|  | 137 | IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK;             \ | 
|  | 138 | *A = (*B >> (10)) ^ (*B << (22)) ^ CLK;             \ | 
|  | 139 | *B = IN ^ U2;                                       \ | 
|  | 140 | *C = (*C >> (11)) ^ (*C << (21)) ^ CLK;             \ | 
|  | 141 | *D = (*D >> (12)) ^ (*D << (20)) ^ CLK;             \ | 
|  | 142 | \ | 
|  | 143 | A = &WALK[PT1 ^ 6]; RES[i++] ^= *A;                 \ | 
|  | 144 | B = &WALK[PT2 ^ 3]; RES[i++] ^= *B;                 \ | 
|  | 145 | C = &WALK[PT1 ^ 7]; RES[i++] ^= *C;                 \ | 
|  | 146 | D = &WALK[PT2 ^ 7]; RES[i++] ^= *D;                 \ | 
|  | 147 | \ | 
|  | 148 | IN = (*A >> (13)) ^ (*A << (19)) ^ CLK;             \ | 
|  | 149 | *A = (*B >> (14)) ^ (*B << (18)) ^ CLK;             \ | 
|  | 150 | *B = IN;                                            \ | 
|  | 151 | *C = (*C >> (15)) ^ (*C << (17)) ^ CLK;             \ | 
|  | 152 | *D = (*D >> (16)) ^ (*D << (16)) ^ CLK;             \ | 
|  | 153 | \ | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 154 | PT1 = ( RES[( i - 8 ) ^ PTX] ^                      \ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | WALK[PT1 ^ PTX ^ 7] ) & (~1);               \ | 
|  | 156 | PT1 ^= (PT2 ^ 0x10) & 0x10;                         \ | 
|  | 157 | \ | 
|  | 158 | for( n++, i = 0; i < 16; i++ )                      \ | 
|  | 159 | hs->pool[n % COLLECT_SIZE] ^= RES[i]; | 
|  | 160 |  | 
|  | 161 | /* | 
|  | 162 | * Entropy gathering function | 
|  | 163 | */ | 
|  | 164 | static void havege_fill( havege_state *hs ) | 
|  | 165 | { | 
|  | 166 | int i, n = 0; | 
|  | 167 | int  U1,  U2, *A, *B, *C, *D; | 
|  | 168 | int PT1, PT2, *WALK, RES[16]; | 
|  | 169 | int PTX, PTY, CLK, PTEST, IN; | 
|  | 170 |  | 
|  | 171 | WALK = hs->WALK; | 
|  | 172 | PT1  = hs->PT1; | 
|  | 173 | PT2  = hs->PT2; | 
|  | 174 |  | 
|  | 175 | PTX  = U1 = 0; | 
|  | 176 | PTY  = U2 = 0; | 
|  | 177 |  | 
|  | 178 | memset( RES, 0, sizeof( RES ) ); | 
|  | 179 |  | 
|  | 180 | while( n < COLLECT_SIZE * 4 ) | 
|  | 181 | { | 
|  | 182 | ONE_ITERATION | 
|  | 183 | ONE_ITERATION | 
|  | 184 | ONE_ITERATION | 
|  | 185 | ONE_ITERATION | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | hs->PT1 = PT1; | 
|  | 189 | hs->PT2 = PT2; | 
|  | 190 |  | 
|  | 191 | hs->offset[0] = 0; | 
|  | 192 | hs->offset[1] = COLLECT_SIZE / 2; | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | /* | 
|  | 196 | * HAVEGE initialization | 
|  | 197 | */ | 
|  | 198 | void havege_init( havege_state *hs ) | 
|  | 199 | { | 
|  | 200 | memset( hs, 0, sizeof( havege_state ) ); | 
|  | 201 |  | 
|  | 202 | havege_fill( hs ); | 
|  | 203 | } | 
|  | 204 |  | 
| Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 205 | void havege_free( havege_state *hs ) | 
|  | 206 | { | 
|  | 207 | if( hs == NULL ) | 
|  | 208 | return; | 
|  | 209 |  | 
|  | 210 | polarssl_zeroize( hs, sizeof( havege_state ) ); | 
|  | 211 | } | 
|  | 212 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | /* | 
|  | 214 | * HAVEGE rand function | 
|  | 215 | */ | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 216 | int havege_random( void *p_rng, unsigned char *buf, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 217 | { | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 218 | int val; | 
|  | 219 | size_t use_len; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | havege_state *hs = (havege_state *) p_rng; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 221 | unsigned char *p = buf; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 222 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 223 | while( len > 0 ) | 
|  | 224 | { | 
|  | 225 | use_len = len; | 
|  | 226 | if( use_len > sizeof(int) ) | 
|  | 227 | use_len = sizeof(int); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 228 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 229 | if( hs->offset[1] >= COLLECT_SIZE ) | 
|  | 230 | havege_fill( hs ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 232 | val  = hs->pool[hs->offset[0]++]; | 
|  | 233 | val ^= hs->pool[hs->offset[1]++]; | 
|  | 234 |  | 
|  | 235 | memcpy( p, &val, use_len ); | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 236 |  | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 237 | len -= use_len; | 
|  | 238 | p += use_len; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | return( 0 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 244 | #endif /* POLARSSL_HAVEGE_C */ |