26 #if !defined(POLARSSL_CONFIG_FILE)
29 #include POLARSSL_CONFIG_FILE
32 #if defined(POLARSSL_ENTROPY_C)
37 #if defined(POLARSSL_FS_IO)
41 #if defined(POLARSSL_HAVEGE_C)
46 static void polarssl_zeroize(
void *v,
size_t n ) {
47 volatile unsigned char *p = v;
while( n-- ) *p++ = 0;
50 #define ENTROPY_MAX_LOOP 256
52 void entropy_init( entropy_context *ctx )
56 #if defined(POLARSSL_THREADING_C)
60 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
65 #if defined(POLARSSL_HAVEGE_C)
69 #if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
70 #if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
74 #if defined(POLARSSL_TIMING_C)
77 #if defined(POLARSSL_HAVEGE_C)
86 #if defined(POLARSSL_HAVEGE_C)
90 #if defined(POLARSSL_THREADING_C)
101 #if defined(POLARSSL_THREADING_C)
120 #if defined(POLARSSL_THREADING_C)
131 static int entropy_update(
entropy_context *ctx,
unsigned char source_id,
132 const unsigned char *data,
size_t len )
134 unsigned char header[2];
136 size_t use_len = len;
137 const unsigned char *p = data;
141 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
142 sha512( data, len, tmp, 0 );
144 sha256( data, len, tmp, 0 );
150 header[0] = source_id;
151 header[1] = use_len & 0xFF;
153 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
165 const unsigned char *data,
size_t len )
169 #if defined(POLARSSL_THREADING_C)
176 #if defined(POLARSSL_THREADING_C)
213 entropy_update( ctx, (
unsigned char) i, buf, olen );
228 #if defined(POLARSSL_THREADING_C)
233 ret = entropy_gather_internal( ctx );
235 #if defined(POLARSSL_THREADING_C)
243 int entropy_func(
void *data,
unsigned char *output,
size_t len )
245 int ret, count = 0, i, reached;
252 #if defined(POLARSSL_THREADING_C)
262 if( count++ > ENTROPY_MAX_LOOP )
268 if( ( ret = entropy_gather_internal( ctx ) ) != 0 )
281 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
314 memcpy( output, buf, len );
319 #if defined(POLARSSL_THREADING_C)
327 #if defined(POLARSSL_FS_IO)
334 if( ( f = fopen( path,
"wb" ) ) == NULL )
359 if( ( f = fopen( path,
"rb" ) ) == NULL )
362 fseek( f, 0, SEEK_END );
363 n = (size_t) ftell( f );
364 fseek( f, 0, SEEK_SET );
369 if( fread( buf, 1, n, f ) != n )
383 #if defined(POLARSSL_SELF_TEST)
385 #if defined(POLARSSL_PLATFORM_C)
389 #define polarssl_printf printf
395 static int entropy_dummy_source(
void *data,
unsigned char *output,
396 size_t len,
size_t *olen )
400 memset( output, 0x2a, len );
442 for( i = 0; i < 8; i++ )
444 if( ( ret =
entropy_func( &ctx, buf,
sizeof( buf ) ) ) != 0 )
447 for( j = 0; j <
sizeof( buf ); j++ )
451 for( j = 0; j <
sizeof( buf ); j++ )