/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/security/xyssl-0.9/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 program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, 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 #include <string.h>
00029 #include <time.h>
00030 
00031 #include "xyssl/config.h"
00032 
00033 #if defined(XYSSL_HAVEGE_C)
00034 
00035 #include "xyssl/havege.h"
00036 #include "xyssl/timing.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 *p_rng )
00199 {
00200     int ret;
00201     havege_state *hs = (havege_state *) p_rng;
00202 
00203     if( hs->offset[1] >= COLLECT_SIZE )
00204         havege_fill( hs );
00205 
00206     ret  = hs->pool[hs->offset[0]++];
00207     ret ^= hs->pool[hs->offset[1]++];
00208 
00209     return( ret );
00210 }
00211 
00212 #if defined(XYSSL_RAND_TEST)
00213 
00214 #include <stdio.h>
00215 
00216 int main( int argc, char *argv[] )
00217 {
00218     FILE *f;
00219     time_t t;
00220     int i, j, k;
00221     havege_state hs;
00222     unsigned char buf[1024];
00223 
00224     if( argc < 2 )
00225     {
00226         fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
00227         return( 1 );
00228     }
00229 
00230     if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
00231     {
00232         printf( "failed to open '%s' for writing.\n", argv[0] );
00233         return( 1 );
00234     }
00235 
00236     havege_init( &hs );
00237 
00238     t = time( NULL );
00239 
00240     for( i = 0, k = 32768; i < k; i++ )
00241     {
00242         for( j = 0; j < sizeof( buf ); j++ )
00243             buf[j] = havege_rand( &hs );
00244 
00245         fwrite( buf, sizeof( buf ), 1, f );
00246 
00247         printf( "Generating 32Mb of data in file '%s'... %04.1f" \
00248                 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
00249         fflush( stdout );
00250     }
00251 
00252     if( t == time( NULL ) )
00253         t--;
00254 
00255     fclose( f );
00256     return( 0 );
00257 }
00258 
00259 #endif
00260 
00261 #endif

Generated on Fri Jul 11 17:59:46 2008 for Mobile-C by  doxygen 1.5.4