2 * Entropy accumulator implementation
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_ENTROPY_C)
30 #include "mbedtls/entropy.h"
31 #include "mbedtls/entropy_poll.h"
35 #if defined(MBEDTLS_FS_IO)
39 #if defined(MBEDTLS_SELF_TEST)
40 #if defined(MBEDTLS_PLATFORM_C)
41 #include "mbedtls/platform.h"
44 #define mbedtls_printf printf
45 #endif /* MBEDTLS_PLATFORM_C */
46 #endif /* MBEDTLS_SELF_TEST */
48 #if defined(MBEDTLS_HAVEGE_C)
49 #include "mbedtls/havege.h"
52 /* Implementation that should never be optimized out by the compiler */
53 static void mbedtls_zeroize( void *v
, size_t n
) {
54 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
57 #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
59 void mbedtls_entropy_init( mbedtls_entropy_context
*ctx
)
61 memset( ctx
, 0, sizeof(mbedtls_entropy_context
) );
63 #if defined(MBEDTLS_THREADING_C)
64 mbedtls_mutex_init( &ctx
->mutex
);
67 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
68 mbedtls_sha512_starts( &ctx
->accumulator
, 0 );
70 mbedtls_sha256_starts( &ctx
->accumulator
, 0 );
72 #if defined(MBEDTLS_HAVEGE_C)
73 mbedtls_havege_init( &ctx
->havege_data
);
76 #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
77 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
78 mbedtls_entropy_add_source( ctx
, mbedtls_platform_entropy_poll
, NULL
,
79 MBEDTLS_ENTROPY_MIN_PLATFORM
,
80 MBEDTLS_ENTROPY_SOURCE_STRONG
);
82 #if defined(MBEDTLS_TIMING_C)
83 mbedtls_entropy_add_source( ctx
, mbedtls_hardclock_poll
, NULL
,
84 MBEDTLS_ENTROPY_MIN_HARDCLOCK
,
85 MBEDTLS_ENTROPY_SOURCE_WEAK
);
87 #if defined(MBEDTLS_HAVEGE_C)
88 mbedtls_entropy_add_source( ctx
, mbedtls_havege_poll
, &ctx
->havege_data
,
89 MBEDTLS_ENTROPY_MIN_HAVEGE
,
90 MBEDTLS_ENTROPY_SOURCE_STRONG
);
92 #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
93 mbedtls_entropy_add_source( ctx
, mbedtls_hardware_poll
, NULL
,
94 MBEDTLS_ENTROPY_MIN_HARDWARE
,
95 MBEDTLS_ENTROPY_SOURCE_STRONG
);
97 #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
100 void mbedtls_entropy_free( mbedtls_entropy_context
*ctx
)
102 #if defined(MBEDTLS_HAVEGE_C)
103 mbedtls_havege_free( &ctx
->havege_data
);
105 #if defined(MBEDTLS_THREADING_C)
106 mbedtls_mutex_free( &ctx
->mutex
);
108 mbedtls_zeroize( ctx
, sizeof( mbedtls_entropy_context
) );
111 int mbedtls_entropy_add_source( mbedtls_entropy_context
*ctx
,
112 mbedtls_entropy_f_source_ptr f_source
, void *p_source
,
113 size_t threshold
, int strong
)
117 #if defined(MBEDTLS_THREADING_C)
118 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
122 index
= ctx
->source_count
;
123 if( index
>= MBEDTLS_ENTROPY_MAX_SOURCES
)
125 ret
= MBEDTLS_ERR_ENTROPY_MAX_SOURCES
;
129 ctx
->source
[index
].f_source
= f_source
;
130 ctx
->source
[index
].p_source
= p_source
;
131 ctx
->source
[index
].threshold
= threshold
;
132 ctx
->source
[index
].strong
= strong
;
137 #if defined(MBEDTLS_THREADING_C)
138 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
139 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
146 * Entropy accumulator update
148 static int entropy_update( mbedtls_entropy_context
*ctx
, unsigned char source_id
,
149 const unsigned char *data
, size_t len
)
151 unsigned char header
[2];
152 unsigned char tmp
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
153 size_t use_len
= len
;
154 const unsigned char *p
= data
;
156 if( use_len
> MBEDTLS_ENTROPY_BLOCK_SIZE
)
158 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
159 mbedtls_sha512( data
, len
, tmp
, 0 );
161 mbedtls_sha256( data
, len
, tmp
, 0 );
164 use_len
= MBEDTLS_ENTROPY_BLOCK_SIZE
;
167 header
[0] = source_id
;
168 header
[1] = use_len
& 0xFF;
170 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
171 mbedtls_sha512_update( &ctx
->accumulator
, header
, 2 );
172 mbedtls_sha512_update( &ctx
->accumulator
, p
, use_len
);
174 mbedtls_sha256_update( &ctx
->accumulator
, header
, 2 );
175 mbedtls_sha256_update( &ctx
->accumulator
, p
, use_len
);
181 int mbedtls_entropy_update_manual( mbedtls_entropy_context
*ctx
,
182 const unsigned char *data
, size_t len
)
186 #if defined(MBEDTLS_THREADING_C)
187 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
191 ret
= entropy_update( ctx
, MBEDTLS_ENTROPY_SOURCE_MANUAL
, data
, len
);
193 #if defined(MBEDTLS_THREADING_C)
194 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
195 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
202 * Run through the different sources to add entropy to our accumulator
204 static int entropy_gather_internal( mbedtls_entropy_context
*ctx
)
206 int ret
, i
, have_one_strong
= 0;
207 unsigned char buf
[MBEDTLS_ENTROPY_MAX_GATHER
];
210 if( ctx
->source_count
== 0 )
211 return( MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED
);
214 * Run through our entropy sources
216 for( i
= 0; i
< ctx
->source_count
; i
++ )
218 if( ctx
->source
[i
].strong
== MBEDTLS_ENTROPY_SOURCE_STRONG
)
222 if( ( ret
= ctx
->source
[i
].f_source( ctx
->source
[i
].p_source
,
223 buf
, MBEDTLS_ENTROPY_MAX_GATHER
, &olen
) ) != 0 )
229 * Add if we actually gathered something
233 entropy_update( ctx
, (unsigned char) i
, buf
, olen
);
234 ctx
->source
[i
].size
+= olen
;
238 if( have_one_strong
== 0 )
239 return( MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE
);
245 * Thread-safe wrapper for entropy_gather_internal()
247 int mbedtls_entropy_gather( mbedtls_entropy_context
*ctx
)
251 #if defined(MBEDTLS_THREADING_C)
252 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
256 ret
= entropy_gather_internal( ctx
);
258 #if defined(MBEDTLS_THREADING_C)
259 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
260 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
266 int mbedtls_entropy_func( void *data
, unsigned char *output
, size_t len
)
268 int ret
, count
= 0, i
, done
;
269 mbedtls_entropy_context
*ctx
= (mbedtls_entropy_context
*) data
;
270 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
272 if( len
> MBEDTLS_ENTROPY_BLOCK_SIZE
)
273 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
275 #if defined(MBEDTLS_THREADING_C)
276 if( ( ret
= mbedtls_mutex_lock( &ctx
->mutex
) ) != 0 )
281 * Always gather extra entropy before a call
285 if( count
++ > ENTROPY_MAX_LOOP
)
287 ret
= MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
;
291 if( ( ret
= entropy_gather_internal( ctx
) ) != 0 )
295 for( i
= 0; i
< ctx
->source_count
; i
++ )
296 if( ctx
->source
[i
].size
< ctx
->source
[i
].threshold
)
301 memset( buf
, 0, MBEDTLS_ENTROPY_BLOCK_SIZE
);
303 #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
304 mbedtls_sha512_finish( &ctx
->accumulator
, buf
);
307 * Reset accumulator and counters and recycle existing entropy
309 memset( &ctx
->accumulator
, 0, sizeof( mbedtls_sha512_context
) );
310 mbedtls_sha512_starts( &ctx
->accumulator
, 0 );
311 mbedtls_sha512_update( &ctx
->accumulator
, buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
);
314 * Perform second SHA-512 on entropy
316 mbedtls_sha512( buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
, buf
, 0 );
317 #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
318 mbedtls_sha256_finish( &ctx
->accumulator
, buf
);
321 * Reset accumulator and counters and recycle existing entropy
323 memset( &ctx
->accumulator
, 0, sizeof( mbedtls_sha256_context
) );
324 mbedtls_sha256_starts( &ctx
->accumulator
, 0 );
325 mbedtls_sha256_update( &ctx
->accumulator
, buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
);
328 * Perform second SHA-256 on entropy
330 mbedtls_sha256( buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
, buf
, 0 );
331 #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
333 for( i
= 0; i
< ctx
->source_count
; i
++ )
334 ctx
->source
[i
].size
= 0;
336 memcpy( output
, buf
, len
);
341 #if defined(MBEDTLS_THREADING_C)
342 if( mbedtls_mutex_unlock( &ctx
->mutex
) != 0 )
343 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR
);
349 #if defined(MBEDTLS_FS_IO)
350 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context
*ctx
, const char *path
)
352 int ret
= MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
;
354 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
356 if( ( f
= fopen( path
, "wb" ) ) == NULL
)
357 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
);
359 if( ( ret
= mbedtls_entropy_func( ctx
, buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
) ) != 0 )
362 if( fwrite( buf
, 1, MBEDTLS_ENTROPY_BLOCK_SIZE
, f
) != MBEDTLS_ENTROPY_BLOCK_SIZE
)
364 ret
= MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
;
375 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context
*ctx
, const char *path
)
379 unsigned char buf
[ MBEDTLS_ENTROPY_MAX_SEED_SIZE
];
381 if( ( f
= fopen( path
, "rb" ) ) == NULL
)
382 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
);
384 fseek( f
, 0, SEEK_END
);
385 n
= (size_t) ftell( f
);
386 fseek( f
, 0, SEEK_SET
);
388 if( n
> MBEDTLS_ENTROPY_MAX_SEED_SIZE
)
389 n
= MBEDTLS_ENTROPY_MAX_SEED_SIZE
;
391 if( fread( buf
, 1, n
, f
) != n
)
394 return( MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
);
399 mbedtls_entropy_update_manual( ctx
, buf
, n
);
401 return( mbedtls_entropy_write_seed_file( ctx
, path
) );
403 #endif /* MBEDTLS_FS_IO */
405 #if defined(MBEDTLS_SELF_TEST)
407 * Dummy source function
409 static int entropy_dummy_source( void *data
, unsigned char *output
,
410 size_t len
, size_t *olen
)
414 memset( output
, 0x2a, len
);
421 * The actual entropy quality is hard to test, but we can at least
422 * test that the functions don't cause errors and write the correct
423 * amount of data to buffers.
425 int mbedtls_entropy_self_test( int verbose
)
428 mbedtls_entropy_context ctx
;
429 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
] = { 0 };
430 unsigned char acc
[MBEDTLS_ENTROPY_BLOCK_SIZE
] = { 0 };
434 mbedtls_printf( " ENTROPY test: " );
436 mbedtls_entropy_init( &ctx
);
438 /* First do a gather to make sure we have default sources */
439 if( ( ret
= mbedtls_entropy_gather( &ctx
) ) != 0 )
442 ret
= mbedtls_entropy_add_source( &ctx
, entropy_dummy_source
, NULL
, 16,
443 MBEDTLS_ENTROPY_SOURCE_WEAK
);
447 if( ( ret
= mbedtls_entropy_update_manual( &ctx
, buf
, sizeof buf
) ) != 0 )
451 * To test that mbedtls_entropy_func writes correct number of bytes:
452 * - use the whole buffer and rely on ASan to detect overruns
453 * - collect entropy 8 times and OR the result in an accumulator:
454 * any byte should then be 0 with probably 2^(-64), so requiring
455 * each of the 32 or 64 bytes to be non-zero has a false failure rate
456 * of at most 2^(-58) which is acceptable.
458 for( i
= 0; i
< 8; i
++ )
460 if( ( ret
= mbedtls_entropy_func( &ctx
, buf
, sizeof( buf
) ) ) != 0 )
463 for( j
= 0; j
< sizeof( buf
); j
++ )
467 for( j
= 0; j
< sizeof( buf
); j
++ )
477 mbedtls_entropy_free( &ctx
);
482 mbedtls_printf( "failed\n" );
484 mbedtls_printf( "passed\n" );
486 mbedtls_printf( "\n" );
491 #endif /* MBEDTLS_SELF_TEST */
493 #endif /* MBEDTLS_ENTROPY_C */