/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/library/havege.c

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

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4