2 * Entropy accumulator implementation
4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
6 * This file is part of mbed TLS (https://tls.mbed.org)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #if !defined(POLARSSL_CONFIG_FILE)
24 #include "polarssl/config.h"
26 #include POLARSSL_CONFIG_FILE
29 #if defined(POLARSSL_ENTROPY_C)
31 #include "polarssl/entropy.h"
32 #include "polarssl/entropy_poll.h"
36 #if defined(POLARSSL_FS_IO)
40 #if defined(POLARSSL_SELF_TEST)
41 #if defined(POLARSSL_PLATFORM_C)
42 #include "polarssl/platform.h"
45 #define polarssl_printf printf
46 #endif /* POLARSSL_PLATFORM_C */
47 #endif /* POLARSSL_SELF_TEST */
49 #if defined(POLARSSL_HAVEGE_C)
50 #include "polarssl/havege.h"
53 /* Implementation that should never be optimized out by the compiler */
54 static void polarssl_zeroize( void *v
, size_t n
) {
55 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
58 #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
60 void entropy_init( entropy_context
*ctx
)
62 memset( ctx
, 0, sizeof(entropy_context
) );
64 #if defined(POLARSSL_THREADING_C)
65 polarssl_mutex_init( &ctx
->mutex
);
68 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
69 sha512_starts( &ctx
->accumulator
, 0 );
71 sha256_starts( &ctx
->accumulator
, 0 );
73 #if defined(POLARSSL_HAVEGE_C)
74 havege_init( &ctx
->havege_data
);
77 #if !defined(POLARSSL_NO_DEFAULT_ENTROPY_SOURCES)
78 #if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
79 entropy_add_source( ctx
, platform_entropy_poll
, NULL
,
80 ENTROPY_MIN_PLATFORM
);
82 #if defined(POLARSSL_TIMING_C)
83 entropy_add_source( ctx
, hardclock_poll
, NULL
, ENTROPY_MIN_HARDCLOCK
);
85 #if defined(POLARSSL_HAVEGE_C)
86 entropy_add_source( ctx
, havege_poll
, &ctx
->havege_data
,
89 #endif /* POLARSSL_NO_DEFAULT_ENTROPY_SOURCES */
92 void entropy_free( entropy_context
*ctx
)
94 #if defined(POLARSSL_HAVEGE_C)
95 havege_free( &ctx
->havege_data
);
97 #if defined(POLARSSL_THREADING_C)
98 polarssl_mutex_free( &ctx
->mutex
);
100 polarssl_zeroize( ctx
, sizeof( entropy_context
) );
103 int entropy_add_source( entropy_context
*ctx
,
104 f_source_ptr f_source
, void *p_source
,
109 #if defined(POLARSSL_THREADING_C)
110 if( ( ret
= polarssl_mutex_lock( &ctx
->mutex
) ) != 0 )
114 index
= ctx
->source_count
;
115 if( index
>= ENTROPY_MAX_SOURCES
)
117 ret
= POLARSSL_ERR_ENTROPY_MAX_SOURCES
;
121 ctx
->source
[index
].f_source
= f_source
;
122 ctx
->source
[index
].p_source
= p_source
;
123 ctx
->source
[index
].threshold
= threshold
;
128 #if defined(POLARSSL_THREADING_C)
129 if( polarssl_mutex_unlock( &ctx
->mutex
) != 0 )
130 return( POLARSSL_ERR_THREADING_MUTEX_ERROR
);
137 * Entropy accumulator update
139 static int entropy_update( entropy_context
*ctx
, unsigned char source_id
,
140 const unsigned char *data
, size_t len
)
142 unsigned char header
[2];
143 unsigned char tmp
[ENTROPY_BLOCK_SIZE
];
144 size_t use_len
= len
;
145 const unsigned char *p
= data
;
147 if( use_len
> ENTROPY_BLOCK_SIZE
)
149 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
150 sha512( data
, len
, tmp
, 0 );
152 sha256( data
, len
, tmp
, 0 );
155 use_len
= ENTROPY_BLOCK_SIZE
;
158 header
[0] = source_id
;
159 header
[1] = use_len
& 0xFF;
161 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
162 sha512_update( &ctx
->accumulator
, header
, 2 );
163 sha512_update( &ctx
->accumulator
, p
, use_len
);
165 sha256_update( &ctx
->accumulator
, header
, 2 );
166 sha256_update( &ctx
->accumulator
, p
, use_len
);
172 int entropy_update_manual( entropy_context
*ctx
,
173 const unsigned char *data
, size_t len
)
177 #if defined(POLARSSL_THREADING_C)
178 if( ( ret
= polarssl_mutex_lock( &ctx
->mutex
) ) != 0 )
182 ret
= entropy_update( ctx
, ENTROPY_SOURCE_MANUAL
, data
, len
);
184 #if defined(POLARSSL_THREADING_C)
185 if( polarssl_mutex_unlock( &ctx
->mutex
) != 0 )
186 return( POLARSSL_ERR_THREADING_MUTEX_ERROR
);
193 * Run through the different sources to add entropy to our accumulator
195 static int entropy_gather_internal( entropy_context
*ctx
)
198 unsigned char buf
[ENTROPY_MAX_GATHER
];
201 if( ctx
->source_count
== 0 )
202 return( POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED
);
205 * Run through our entropy sources
207 for( i
= 0; i
< ctx
->source_count
; i
++ )
210 if( ( ret
= ctx
->source
[i
].f_source( ctx
->source
[i
].p_source
,
211 buf
, ENTROPY_MAX_GATHER
, &olen
) ) != 0 )
217 * Add if we actually gathered something
221 entropy_update( ctx
, (unsigned char) i
, buf
, olen
);
222 ctx
->source
[i
].size
+= olen
;
230 * Thread-safe wrapper for entropy_gather_internal()
232 int entropy_gather( entropy_context
*ctx
)
236 #if defined(POLARSSL_THREADING_C)
237 if( ( ret
= polarssl_mutex_lock( &ctx
->mutex
) ) != 0 )
241 ret
= entropy_gather_internal( ctx
);
243 #if defined(POLARSSL_THREADING_C)
244 if( polarssl_mutex_unlock( &ctx
->mutex
) != 0 )
245 return( POLARSSL_ERR_THREADING_MUTEX_ERROR
);
251 int entropy_func( void *data
, unsigned char *output
, size_t len
)
253 int ret
, count
= 0, i
, reached
;
254 entropy_context
*ctx
= (entropy_context
*) data
;
255 unsigned char buf
[ENTROPY_BLOCK_SIZE
];
257 if( len
> ENTROPY_BLOCK_SIZE
)
258 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED
);
260 #if defined(POLARSSL_THREADING_C)
261 if( ( ret
= polarssl_mutex_lock( &ctx
->mutex
) ) != 0 )
266 * Always gather extra entropy before a call
270 if( count
++ > ENTROPY_MAX_LOOP
)
272 ret
= POLARSSL_ERR_ENTROPY_SOURCE_FAILED
;
276 if( ( ret
= entropy_gather_internal( ctx
) ) != 0 )
281 for( i
= 0; i
< ctx
->source_count
; i
++ )
282 if( ctx
->source
[i
].size
>= ctx
->source
[i
].threshold
)
285 while( reached
!= ctx
->source_count
);
287 memset( buf
, 0, ENTROPY_BLOCK_SIZE
);
289 #if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
290 sha512_finish( &ctx
->accumulator
, buf
);
293 * Reset accumulator and counters and recycle existing entropy
295 memset( &ctx
->accumulator
, 0, sizeof( sha512_context
) );
296 sha512_starts( &ctx
->accumulator
, 0 );
297 sha512_update( &ctx
->accumulator
, buf
, ENTROPY_BLOCK_SIZE
);
300 * Perform second SHA-512 on entropy
302 sha512( buf
, ENTROPY_BLOCK_SIZE
, buf
, 0 );
303 #else /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
304 sha256_finish( &ctx
->accumulator
, buf
);
307 * Reset accumulator and counters and recycle existing entropy
309 memset( &ctx
->accumulator
, 0, sizeof( sha256_context
) );
310 sha256_starts( &ctx
->accumulator
, 0 );
311 sha256_update( &ctx
->accumulator
, buf
, ENTROPY_BLOCK_SIZE
);
314 * Perform second SHA-256 on entropy
316 sha256( buf
, ENTROPY_BLOCK_SIZE
, buf
, 0 );
317 #endif /* POLARSSL_ENTROPY_SHA512_ACCUMULATOR */
319 for( i
= 0; i
< ctx
->source_count
; i
++ )
320 ctx
->source
[i
].size
= 0;
322 memcpy( output
, buf
, len
);
327 #if defined(POLARSSL_THREADING_C)
328 if( polarssl_mutex_unlock( &ctx
->mutex
) != 0 )
329 return( POLARSSL_ERR_THREADING_MUTEX_ERROR
);
335 #if defined(POLARSSL_FS_IO)
336 int entropy_write_seed_file( entropy_context
*ctx
, const char *path
)
338 int ret
= POLARSSL_ERR_ENTROPY_FILE_IO_ERROR
;
340 unsigned char buf
[ENTROPY_BLOCK_SIZE
];
342 if( ( f
= fopen( path
, "wb" ) ) == NULL
)
343 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR
);
345 if( ( ret
= entropy_func( ctx
, buf
, ENTROPY_BLOCK_SIZE
) ) != 0 )
348 if( fwrite( buf
, 1, ENTROPY_BLOCK_SIZE
, f
) != ENTROPY_BLOCK_SIZE
)
350 ret
= POLARSSL_ERR_ENTROPY_FILE_IO_ERROR
;
361 int entropy_update_seed_file( entropy_context
*ctx
, const char *path
)
365 unsigned char buf
[ ENTROPY_MAX_SEED_SIZE
];
367 if( ( f
= fopen( path
, "rb" ) ) == NULL
)
368 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR
);
370 fseek( f
, 0, SEEK_END
);
371 n
= (size_t) ftell( f
);
372 fseek( f
, 0, SEEK_SET
);
374 if( n
> ENTROPY_MAX_SEED_SIZE
)
375 n
= ENTROPY_MAX_SEED_SIZE
;
377 if( fread( buf
, 1, n
, f
) != n
)
380 return( POLARSSL_ERR_ENTROPY_FILE_IO_ERROR
);
385 entropy_update_manual( ctx
, buf
, n
);
387 return( entropy_write_seed_file( ctx
, path
) );
389 #endif /* POLARSSL_FS_IO */
391 #if defined(POLARSSL_SELF_TEST)
393 * Dummy source function
395 static int entropy_dummy_source( void *data
, unsigned char *output
,
396 size_t len
, size_t *olen
)
400 memset( output
, 0x2a, len
);
407 * The actual entropy quality is hard to test, but we can at least
408 * test that the functions don't cause errors and write the correct
409 * amount of data to buffers.
411 int entropy_self_test( int verbose
)
415 unsigned char buf
[ENTROPY_BLOCK_SIZE
] = { 0 };
416 unsigned char acc
[ENTROPY_BLOCK_SIZE
] = { 0 };
420 polarssl_printf( " ENTROPY test: " );
422 entropy_init( &ctx
);
424 ret
= entropy_add_source( &ctx
, entropy_dummy_source
, NULL
, 16 );
428 if( ( ret
= entropy_gather( &ctx
) ) != 0 )
431 if( ( ret
= entropy_update_manual( &ctx
, buf
, sizeof buf
) ) != 0 )
435 * To test that entropy_func writes correct number of bytes:
436 * - use the whole buffer and rely on ASan to detect overruns
437 * - collect entropy 8 times and OR the result in an accumulator:
438 * any byte should then be 0 with probably 2^(-64), so requiring
439 * each of the 32 or 64 bytes to be non-zero has a false failure rate
440 * of at most 2^(-58) which is acceptable.
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
++ )
461 entropy_free( &ctx
);
466 polarssl_printf( "failed\n" );
468 polarssl_printf( "passed\n" );
470 polarssl_printf( "\n" );
475 #endif /* POLARSSL_SELF_TEST */
477 #endif /* POLARSSL_ENTROPY_C */