PolarSSL v1.1.4
havege.c
Go to the documentation of this file.
00001 
00025 /*
00026  *  The HAVEGE RNG was designed by Andre Seznec in 2002.
00027  *
00028  *  http://www.irisa.fr/caps/projects/hipsor/publi.php
00029  *
00030  *  Contact: seznec(at)irisa_dot_fr - orocheco(at)irisa_dot_fr
00031  */
00032 
00033 #include "polarssl/config.h"
00034 
00035 #if defined(POLARSSL_HAVEGE_C)
00036 
00037 #include "polarssl/havege.h"
00038 #include "polarssl/timing.h"
00039 
00040 #include <string.h>
00041 #include <time.h>
00042 
00043 /* ------------------------------------------------------------------------
00044  * On average, one iteration accesses two 8-word blocks in the havege WALK
00045  * table, and generates 16 words in the RES array.
00046  *
00047  * The data read in the WALK table is updated and permuted after each use.
00048  * The result of the hardware clock counter read is used  for this update.
00049  *
00050  * 25 conditional tests are present.  The conditional tests are grouped in
00051  * two nested  groups of 12 conditional tests and 1 test that controls the
00052  * permutation; on average, there should be 6 tests executed and 3 of them
00053  * should be mispredicted.
00054  * ------------------------------------------------------------------------
00055  */
00056 
00057 #define SWAP(X,Y) { int *T = X; X = Y; Y = T; }
00058 
00059 #define TST1_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
00060 #define TST2_ENTER if( PTEST & 1 ) { PTEST ^= 3; PTEST >>= 1;
00061 
00062 #define TST1_LEAVE U1++; }
00063 #define TST2_LEAVE U2++; }
00064 
00065 #define ONE_ITERATION                                   \
00066                                                         \
00067     PTEST = PT1 >> 20;                                  \
00068                                                         \
00069     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
00070     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
00071     TST1_ENTER  TST1_ENTER  TST1_ENTER  TST1_ENTER      \
00072                                                         \
00073     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
00074     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
00075     TST1_LEAVE  TST1_LEAVE  TST1_LEAVE  TST1_LEAVE      \
00076                                                         \
00077     PTX = (PT1 >> 18) & 7;                              \
00078     PT1 &= 0x1FFF;                                      \
00079     PT2 &= 0x1FFF;                                      \
00080     CLK = (int) hardclock();                            \
00081                                                         \
00082     i = 0;                                              \
00083     A = &WALK[PT1    ]; RES[i++] ^= *A;                 \
00084     B = &WALK[PT2    ]; RES[i++] ^= *B;                 \
00085     C = &WALK[PT1 ^ 1]; RES[i++] ^= *C;                 \
00086     D = &WALK[PT2 ^ 4]; RES[i++] ^= *D;                 \
00087                                                         \
00088     IN = (*A >> (1)) ^ (*A << (31)) ^ CLK;              \
00089     *A = (*B >> (2)) ^ (*B << (30)) ^ CLK;              \
00090     *B = IN ^ U1;                                       \
00091     *C = (*C >> (3)) ^ (*C << (29)) ^ CLK;              \
00092     *D = (*D >> (4)) ^ (*D << (28)) ^ CLK;              \
00093                                                         \
00094     A = &WALK[PT1 ^ 2]; RES[i++] ^= *A;                 \
00095     B = &WALK[PT2 ^ 2]; RES[i++] ^= *B;                 \
00096     C = &WALK[PT1 ^ 3]; RES[i++] ^= *C;                 \
00097     D = &WALK[PT2 ^ 6]; RES[i++] ^= *D;                 \
00098                                                         \
00099     if( PTEST & 1 ) SWAP( A, C );                       \
00100                                                         \
00101     IN = (*A >> (5)) ^ (*A << (27)) ^ CLK;              \
00102     *A = (*B >> (6)) ^ (*B << (26)) ^ CLK;              \
00103     *B = IN; CLK = (int) hardclock();                   \
00104     *C = (*C >> (7)) ^ (*C << (25)) ^ CLK;              \
00105     *D = (*D >> (8)) ^ (*D << (24)) ^ CLK;              \
00106                                                         \
00107     A = &WALK[PT1 ^ 4];                                 \
00108     B = &WALK[PT2 ^ 1];                                 \
00109                                                         \
00110     PTEST = PT2 >> 1;                                   \
00111                                                         \
00112     PT2 = (RES[(i - 8) ^ PTY] ^ WALK[PT2 ^ PTY ^ 7]);   \
00113     PT2 = ((PT2 & 0x1FFF) & (~8)) ^ ((PT1 ^ 8) & 0x8);  \
00114     PTY = (PT2 >> 10) & 7;                              \
00115                                                         \
00116     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
00117     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
00118     TST2_ENTER  TST2_ENTER  TST2_ENTER  TST2_ENTER      \
00119                                                         \
00120     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
00121     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
00122     TST2_LEAVE  TST2_LEAVE  TST2_LEAVE  TST2_LEAVE      \
00123                                                         \
00124     C = &WALK[PT1 ^ 5];                                 \
00125     D = &WALK[PT2 ^ 5];                                 \
00126                                                         \
00127     RES[i++] ^= *A;                                     \
00128     RES[i++] ^= *B;                                     \
00129     RES[i++] ^= *C;                                     \
00130     RES[i++] ^= *D;                                     \
00131                                                         \
00132     IN = (*A >> ( 9)) ^ (*A << (23)) ^ CLK;             \
00133     *A = (*B >> (10)) ^ (*B << (22)) ^ CLK;             \
00134     *B = IN ^ U2;                                       \
00135     *C = (*C >> (11)) ^ (*C << (21)) ^ CLK;             \
00136     *D = (*D >> (12)) ^ (*D << (20)) ^ CLK;             \
00137                                                         \
00138     A = &WALK[PT1 ^ 6]; RES[i++] ^= *A;                 \
00139     B = &WALK[PT2 ^ 3]; RES[i++] ^= *B;                 \
00140     C = &WALK[PT1 ^ 7]; RES[i++] ^= *C;                 \
00141     D = &WALK[PT2 ^ 7]; RES[i++] ^= *D;                 \
00142                                                         \
00143     IN = (*A >> (13)) ^ (*A << (19)) ^ CLK;             \
00144     *A = (*B >> (14)) ^ (*B << (18)) ^ CLK;             \
00145     *B = IN;                                            \
00146     *C = (*C >> (15)) ^ (*C << (17)) ^ CLK;             \
00147     *D = (*D >> (16)) ^ (*D << (16)) ^ CLK;             \
00148                                                         \
00149     PT1 = ( RES[(i - 8) ^ PTX] ^                        \
00150             WALK[PT1 ^ PTX ^ 7] ) & (~1);               \
00151     PT1 ^= (PT2 ^ 0x10) & 0x10;                         \
00152                                                         \
00153     for( n++, i = 0; i < 16; i++ )                      \
00154         hs->pool[n % COLLECT_SIZE] ^= RES[i];
00155 
00156 /*
00157  * Entropy gathering function
00158  */
00159 static void havege_fill( havege_state *hs )
00160 {
00161     int i, n = 0;
00162     int  U1,  U2, *A, *B, *C, *D;
00163     int PT1, PT2, *WALK, RES[16];
00164     int PTX, PTY, CLK, PTEST, IN;
00165 
00166     WALK = hs->WALK;
00167     PT1  = hs->PT1;
00168     PT2  = hs->PT2;
00169 
00170     PTX  = U1 = 0;
00171     PTY  = U2 = 0;
00172 
00173     memset( RES, 0, sizeof( RES ) );
00174 
00175     while( n < COLLECT_SIZE * 4 )
00176     {
00177         ONE_ITERATION
00178         ONE_ITERATION
00179         ONE_ITERATION
00180         ONE_ITERATION
00181     }
00182 
00183     hs->PT1 = PT1;
00184     hs->PT2 = PT2;
00185 
00186     hs->offset[0] = 0;
00187     hs->offset[1] = COLLECT_SIZE / 2;
00188 }
00189 
00190 /*
00191  * HAVEGE initialization
00192  */
00193 void havege_init( havege_state *hs )
00194 {
00195     memset( hs, 0, sizeof( havege_state ) );
00196 
00197     havege_fill( hs );
00198 }
00199 
00200 /*
00201  * HAVEGE rand function
00202  */
00203 int havege_random( void *p_rng, unsigned char *buf, size_t len )
00204 {
00205     int val;
00206     size_t use_len;
00207     havege_state *hs = (havege_state *) p_rng;
00208     unsigned char *p = buf;
00209 
00210     while( len > 0 )
00211     {
00212         use_len = len;
00213         if( use_len > sizeof(int) )
00214             use_len = sizeof(int);
00215 
00216         if( hs->offset[1] >= COLLECT_SIZE )
00217             havege_fill( hs );
00218 
00219         val  = hs->pool[hs->offset[0]++];
00220         val ^= hs->pool[hs->offset[1]++];
00221 
00222         memcpy( p, &val, use_len );
00223         
00224         len -= use_len;
00225         p += use_len;
00226     }
00227 
00228     return( 0 );
00229 }
00230 
00231 #endif