mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
havege.c
00001 /** 00002 * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion 00003 * 00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved 00005 * 00006 * This file is part of mbed TLS (https://tls.mbed.org) 00007 * 00008 * This program is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License along 00019 * with this program; if not, write to the Free Software Foundation, Inc., 00020 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00021 */ 00022 /* 00023 * The HAVEGE RNG was designed by Andre Seznec in 2002. 00024 * 00025 * http://www.irisa.fr/caps/projects/hipsor/publi.php 00026 * 00027 * Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr 00028 */ 00029 00030 #if !defined(POLARSSL_CONFIG_FILE) 00031 #include "polarssl/config.h" 00032 #else 00033 #include POLARSSL_CONFIG_FILE 00034 #endif 00035 00036 #if defined(POLARSSL_HAVEGE_C) 00037 00038 #include "polarssl/havege.h" 00039 #include "polarssl/timing.h" 00040 00041 #include <string.h> 00042 00043 /* Implementation that should never be optimized out by the compiler */ 00044 static void polarssl_zeroize( void *v, size_t n ) { 00045 volatile unsigned char *p = v; while( n-- ) *p++ = 0; 00046 } 00047 00048 /* ------------------------------------------------------------------------ 00049 * On average, one iteration accesses two 8-word blocks in the havege WALK 00050 * table, and generates 16 words in the RES array. 00051 * 00052 * The data read in the WALK table is updated and permuted after each use. 00053 * The result of the hardware clock counter read is used for this update. 00054 * 00055 * 25 conditional tests are present. The conditional tests are grouped in 00056 * two nested groups of 12 conditional tests and 1 test that controls the 00057 * permutation; on average, there should be 6 tests executed and 3 of them 00058 * should be mispredicted. 00059 * ------------------------------------------------------------------------ 00060 */ 00061 00062 #define SWAP(X,Y) { int *T = X; X = Y; Y = T; } 00063 00064 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; 00065 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1; 00066 00067 #define TST1_LEAVE U1++; } 00068 #define TST2_LEAVE U2++; } 00069 00070 #define ONE_ITERATION \ 00071 \ 00072 PTEST = PT1 >> 20; \ 00073 \ 00074 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \ 00075 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \ 00076 TST1_ENTER TST1_ENTER TST1_ENTER TST1_ENTER \ 00077 \ 00078 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \ 00079 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \ 00080 TST1_LEAVE TST1_LEAVE TST1_LEAVE TST1_LEAVE \ 00081 \ 00082 PTX = (PT1 >> 18) & 7; \ 00083 PT1 &= 0x1FFF; \ 00084 PT2 &= 0x1FFF; \ 00085 CLK = (int) hardclock(); \ 00086 \ 00087 i = 0; \ 00088 A = &WALK[PT1 ]; RES[i++] ^= *A; \ 00089 B = &WALK[PT2 ]; RES[i++] ^= *B; \ 00090 C = &WALK[PT1 ^ 1]; RES[i++] ^= *C; \ 00091 D = &WALK[PT2 ^ 4]; RES[i++] ^= *D; \ 00092 \ 00093 IN = (*A >> (1)) ^ (*A << (31)) ^ CLK; \ 00094 *A = (*B >> (2)) ^ (*B << (30)) ^ CLK; \ 00095 *B = IN ^ U1; \ 00096 *C = (*C >> (3)) ^ (*C << (29)) ^ CLK; \ 00097 *D = (*D >> (4)) ^ (*D << (28)) ^ CLK; \ 00098 \ 00099 A = &WALK[PT1 ^ 2]; RES[i++] ^= *A; \ 00100 B = &WALK[PT2 ^ 2]; RES[i++] ^= *B; \ 00101 C = &WALK[PT1 ^ 3]; RES[i++] ^= *C; \ 00102 D = &WALK[PT2 ^ 6]; RES[i++] ^= *D; \ 00103 \ 00104 if( PTEST & 1 ) SWAP( A, C ); \ 00105 \ 00106 IN = (*A >> (5)) ^ (*A << (27)) ^ CLK; \ 00107 *A = (*B >> (6)) ^ (*B << (26)) ^ CLK; \ 00108 *B = IN; CLK = (int) hardclock(); \ 00109 *C = (*C >> (7)) ^ (*C << (25)) ^ CLK; \ 00110 *D = (*D >> (8)) ^ (*D << (24)) ^ CLK; \ 00111 \ 00112 A = &WALK[PT1 ^ 4]; \ 00113 B = &WALK[PT2 ^ 1]; \ 00114 \ 00115 PTEST = PT2 >> 1; \ 00116 \ 00117 PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]); \ 00118 PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8); \ 00119 PTY = (PT2 >> 10) & 7; \ 00120 \ 00121 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \ 00122 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \ 00123 TST2_ENTER TST2_ENTER TST2_ENTER TST2_ENTER \ 00124 \ 00125 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \ 00126 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \ 00127 TST2_LEAVE TST2_LEAVE TST2_LEAVE TST2_LEAVE \ 00128 \ 00129 C = &WALK[PT1 ^ 5]; \ 00130 D = &WALK[PT2 ^ 5]; \ 00131 \ 00132 RES[i++] ^= *A; \ 00133 RES[i++] ^= *B; \ 00134 RES[i++] ^= *C; \ 00135 RES[i++] ^= *D; \ 00136 \ 00137 IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK; \ 00138 *A = (*B >> (10)) ^ (*B << (22)) ^ CLK; \ 00139 *B = IN ^ U2; \ 00140 *C = (*C >> (11)) ^ (*C << (21)) ^ CLK; \ 00141 *D = (*D >> (12)) ^ (*D << (20)) ^ CLK; \ 00142 \ 00143 A = &WALK[PT1 ^ 6]; RES[i++] ^= *A; \ 00144 B = &WALK[PT2 ^ 3]; RES[i++] ^= *B; \ 00145 C = &WALK[PT1 ^ 7]; RES[i++] ^= *C; \ 00146 D = &WALK[PT2 ^ 7]; RES[i++] ^= *D; \ 00147 \ 00148 IN = (*A >> (13)) ^ (*A << (19)) ^ CLK; \ 00149 *A = (*B >> (14)) ^ (*B << (18)) ^ CLK; \ 00150 *B = IN; \ 00151 *C = (*C >> (15)) ^ (*C << (17)) ^ CLK; \ 00152 *D = (*D >> (16)) ^ (*D << (16)) ^ CLK; \ 00153 \ 00154 PT1 = ( RES[( i - 8 ) ^ PTX] ^ \ 00155 WALK[PT1 ^ PTX ^ 7] ) & (~1); \ 00156 PT1 ^= (PT2 ^ 0x10) & 0x10; \ 00157 \ 00158 for( n++, i = 0; i < 16; i++ ) \ 00159 hs->pool[n % COLLECT_SIZE] ^= RES[i]; 00160 00161 /* 00162 * Entropy gathering function 00163 */ 00164 static void havege_fill( havege_state *hs ) 00165 { 00166 int i, n = 0; 00167 int U1, U2, *A, *B, *C, *D; 00168 int PT1, PT2, *WALK, RES[16]; 00169 int PTX, PTY, CLK, PTEST, IN; 00170 00171 WALK = hs->WALK; 00172 PT1 = hs->PT1; 00173 PT2 = hs->PT2; 00174 00175 PTX = U1 = 0; 00176 PTY = U2 = 0; 00177 00178 memset( RES, 0, sizeof( RES ) ); 00179 00180 while( n < COLLECT_SIZE * 4 ) 00181 { 00182 ONE_ITERATION 00183 ONE_ITERATION 00184 ONE_ITERATION 00185 ONE_ITERATION 00186 } 00187 00188 hs->PT1 = PT1; 00189 hs->PT2 = PT2; 00190 00191 hs->offset[0] = 0; 00192 hs->offset[1] = COLLECT_SIZE / 2; 00193 } 00194 00195 /* 00196 * HAVEGE initialization 00197 */ 00198 void havege_init( havege_state *hs ) 00199 { 00200 memset( hs, 0, sizeof( havege_state ) ); 00201 00202 havege_fill( hs ); 00203 } 00204 00205 void havege_free( havege_state *hs ) 00206 { 00207 if( hs == NULL ) 00208 return; 00209 00210 polarssl_zeroize( hs, sizeof( havege_state ) ); 00211 } 00212 00213 /* 00214 * HAVEGE rand function 00215 */ 00216 int havege_random( void *p_rng, unsigned char *buf, size_t len ) 00217 { 00218 int val; 00219 size_t use_len; 00220 havege_state *hs = (havege_state *) p_rng; 00221 unsigned char *p = buf; 00222 00223 while( len > 0 ) 00224 { 00225 use_len = len; 00226 if( use_len > sizeof(int) ) 00227 use_len = sizeof(int); 00228 00229 if( hs->offset[1] >= COLLECT_SIZE ) 00230 havege_fill( hs ); 00231 00232 val = hs->pool[hs->offset[0]++]; 00233 val ^= hs->pool[hs->offset[1]++]; 00234 00235 memcpy( p, &val, use_len ); 00236 00237 len -= use_len; 00238 p += use_len; 00239 } 00240 00241 return( 0 ); 00242 } 00243 00244 #endif /* POLARSSL_HAVEGE_C */ 00245
Generated on Tue Jul 12 2022 13:50:37 by 1.7.2