| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  Entropy accumulator implementation | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-09-05 12:53:20 +02:00
										 |  |  |  *  Copyright The Mbed TLS Contributors | 
					
						
							| 
									
										
										
										
											2024-01-30 14:09:13 +01:00
										 |  |  |  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  | #include "common.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ENTROPY_C)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "mbedtls/entropy.h"
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #include "entropy_poll.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-07 16:25:01 +02:00
										 |  |  | #include "mbedtls/platform_util.h"
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  | #include "mbedtls/error.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_FS_IO)
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "mbedtls/platform.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENTROPY_MAX_LOOP    256     /**< Maximum amount to loop before error */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_entropy_init(mbedtls_entropy_context *ctx) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     ctx->source_count = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memset(ctx->source, 0, sizeof(ctx->source)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_mutex_init(&ctx->mutex); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->accumulator_started = 0; | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     mbedtls_md_init(&ctx->accumulator); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Reminder: Update ENTROPY_HAVE_STRONG in the test files
 | 
					
						
							|  |  |  |      *           when adding more strong entropy sources here. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
 | 
					
						
							|  |  |  | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL, | 
					
						
							|  |  |  |                                MBEDTLS_ENTROPY_MIN_PLATFORM, | 
					
						
							|  |  |  |                                MBEDTLS_ENTROPY_SOURCE_STRONG); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL, | 
					
						
							|  |  |  |                                MBEDTLS_ENTROPY_MIN_HARDWARE, | 
					
						
							|  |  |  |                                MBEDTLS_ENTROPY_SOURCE_STRONG); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ENTROPY_NV_SEED)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL, | 
					
						
							|  |  |  |                                MBEDTLS_ENTROPY_BLOCK_SIZE, | 
					
						
							|  |  |  |                                MBEDTLS_ENTROPY_SOURCE_STRONG); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     ctx->initial_entropy_run = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_entropy_free(mbedtls_entropy_context *ctx) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-31 15:25:10 +02:00
										 |  |  |     if (ctx == NULL) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 18:27:58 +01:00
										 |  |  |     /* If the context was already free, don't call free() again.
 | 
					
						
							|  |  |  |      * This is important for mutexes which don't allow double-free. */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (ctx->accumulator_started == -1) { | 
					
						
							| 
									
										
										
										
											2021-03-12 18:27:58 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-12 18:27:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_mutex_free(&ctx->mutex); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     mbedtls_md_free(&ctx->accumulator); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #if defined(MBEDTLS_ENTROPY_NV_SEED)
 | 
					
						
							|  |  |  |     ctx->initial_entropy_run = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     ctx->source_count = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source)); | 
					
						
							| 
									
										
										
										
											2021-03-12 18:27:58 +01:00
										 |  |  |     ctx->accumulator_started = -1; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx, | 
					
						
							|  |  |  |                                mbedtls_entropy_f_source_ptr f_source, void *p_source, | 
					
						
							|  |  |  |                                size_t threshold, int strong) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int idx, ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     idx = ctx->source_count; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (idx >= MBEDTLS_ENTROPY_MAX_SOURCES) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES; | 
					
						
							|  |  |  |         goto exit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->source[idx].f_source  = f_source; | 
					
						
							|  |  |  |     ctx->source[idx].p_source  = p_source; | 
					
						
							|  |  |  |     ctx->source[idx].threshold = threshold; | 
					
						
							|  |  |  |     ctx->source[idx].strong    = strong; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->source_count++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exit: | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_THREADING_MUTEX_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Entropy accumulator update | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id, | 
					
						
							|  |  |  |                           const unsigned char *data, size_t len) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char header[2]; | 
					
						
							|  |  |  |     unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE]; | 
					
						
							|  |  |  |     size_t use_len = len; | 
					
						
							|  |  |  |     const unsigned char *p = data; | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) { | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |         if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), | 
					
						
							|  |  |  |                               data, len, tmp)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         p = tmp; | 
					
						
							|  |  |  |         use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     header[0] = source_id; | 
					
						
							|  |  |  |     header[1] = use_len & 0xFF; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Start the accumulator if this has not already happened. Note that | 
					
						
							|  |  |  |      * it is sufficient to start the accumulator here only because all calls to | 
					
						
							|  |  |  |      * gather entropy eventually execute this code. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     if (ctx->accumulator_started == 0) { | 
					
						
							|  |  |  |         ret = mbedtls_md_setup(&ctx->accumulator, | 
					
						
							|  |  |  |                                mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), 0); | 
					
						
							|  |  |  |         if (ret != 0) { | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ret = mbedtls_md_starts(&ctx->accumulator); | 
					
						
							|  |  |  |         if (ret != 0) { | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ctx->accumulator_started = 1; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     if ((ret = mbedtls_md_update(&ctx->accumulator, header, 2)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     ret = mbedtls_md_update(&ctx->accumulator, p, use_len); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | cleanup: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_platform_zeroize(tmp, sizeof(tmp)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx, | 
					
						
							|  |  |  |                                   const unsigned char *data, size_t len) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_THREADING_MUTEX_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Run through the different sources to add entropy to our accumulator | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static int entropy_gather_internal(mbedtls_entropy_context *ctx) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |     int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     int have_one_strong = 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER]; | 
					
						
							|  |  |  |     size_t olen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (ctx->source_count == 0) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Run through our entropy sources | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     for (i = 0; i < ctx->source_count; i++) { | 
					
						
							|  |  |  |         if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             have_one_strong = 1; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         olen = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         if ((ret = ctx->source[i].f_source(ctx->source[i].p_source, | 
					
						
							|  |  |  |                                            buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * Add if we actually gathered something | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         if (olen > 0) { | 
					
						
							|  |  |  |             if ((ret = entropy_update(ctx, (unsigned char) i, | 
					
						
							|  |  |  |                                       buf, olen)) != 0) { | 
					
						
							|  |  |  |                 return ret; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             ctx->source[i].size += olen; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (have_one_strong == 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | cleanup: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_platform_zeroize(buf, sizeof(buf)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Thread-safe wrapper for entropy_gather_internal() | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_gather(mbedtls_entropy_context *ctx) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     ret = entropy_gather_internal(ctx); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_THREADING_MUTEX_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_func(void *data, unsigned char *output, size_t len) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |     int ret, count = 0, i, thresholds_reached; | 
					
						
							|  |  |  |     size_t strong_size; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data; | 
					
						
							|  |  |  |     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (len > MBEDTLS_ENTROPY_BLOCK_SIZE) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ENTROPY_NV_SEED)
 | 
					
						
							|  |  |  |     /* Update the NV entropy seed before generating any entropy for outside
 | 
					
						
							|  |  |  |      * use. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (ctx->initial_entropy_run == 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ctx->initial_entropy_run = 1; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) { | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Always gather extra entropy before a call | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     do { | 
					
						
							|  |  |  |         if (count++ > ENTROPY_MAX_LOOP) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; | 
					
						
							|  |  |  |             goto exit; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         if ((ret = entropy_gather_internal(ctx)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             goto exit; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |         thresholds_reached = 1; | 
					
						
							|  |  |  |         strong_size = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         for (i = 0; i < ctx->source_count; i++) { | 
					
						
							|  |  |  |             if (ctx->source[i].size < ctx->source[i].threshold) { | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |                 thresholds_reached = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) { | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |                 strong_size += ctx->source[i].size; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } while (!thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Note that at this stage it is assumed that the accumulator was started | 
					
						
							|  |  |  |      * in a previous call to entropy_update(). If this is not guaranteed, the | 
					
						
							|  |  |  |      * code below will fail. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     if ((ret = mbedtls_md_finish(&ctx->accumulator, buf)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto exit; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * Reset accumulator and counters and recycle existing entropy | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     mbedtls_md_free(&ctx->accumulator); | 
					
						
							|  |  |  |     mbedtls_md_init(&ctx->accumulator); | 
					
						
							|  |  |  |     ret = mbedtls_md_setup(&ctx->accumulator, | 
					
						
							|  |  |  |                            mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), 0); | 
					
						
							|  |  |  |     if (ret != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto exit; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     ret = mbedtls_md_starts(&ctx->accumulator); | 
					
						
							|  |  |  |     if (ret != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto exit; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     if ((ret = mbedtls_md_update(&ctx->accumulator, buf, | 
					
						
							|  |  |  |                                  MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto exit; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |      * Perform second hashing on entropy | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     if ((ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_ENTROPY_MD), | 
					
						
							|  |  |  |                           buf, MBEDTLS_ENTROPY_BLOCK_SIZE, buf)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto exit; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     for (i = 0; i < ctx->source_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ctx->source[i].size = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memcpy(output, buf, len); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exit: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_platform_zeroize(buf, sizeof(buf)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_THREADING_MUTEX_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ENTROPY_NV_SEED)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; | 
					
						
							|  |  |  |     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Read new seed  and write it to NV */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (mbedtls_nv_seed_write(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Manually update the remaining stream with a separator value to diverge */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); | 
					
						
							|  |  |  |     ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | #endif /* MBEDTLS_ENTROPY_NV_SEED */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_FS_IO)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |     FILE *f = NULL; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |         ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto exit; | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((f = fopen(path, "wb")) == NULL) { | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |         ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; | 
					
						
							|  |  |  |         goto exit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ | 
					
						
							|  |  |  |     mbedtls_setbuf(f, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != MBEDTLS_ENTROPY_BLOCK_SIZE) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; | 
					
						
							|  |  |  |         goto exit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exit: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_platform_zeroize(buf, sizeof(buf)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (f != NULL) { | 
					
						
							|  |  |  |         fclose(f); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  |     FILE *f; | 
					
						
							|  |  |  |     size_t n; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     unsigned char buf[MBEDTLS_ENTROPY_MAX_SEED_SIZE]; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((f = fopen(path, "rb")) == NULL) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |     /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ | 
					
						
							|  |  |  |     mbedtls_setbuf(f, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     fseek(f, 0, SEEK_END); | 
					
						
							|  |  |  |     n = (size_t) ftell(f); | 
					
						
							|  |  |  |     fseek(f, 0, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (n > MBEDTLS_ENTROPY_MAX_SEED_SIZE) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         n = MBEDTLS_ENTROPY_MAX_SEED_SIZE; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (fread(buf, 1, n, f) != n) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         ret = mbedtls_entropy_update_manual(ctx, buf, n); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     fclose(f); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_platform_zeroize(buf, sizeof(buf)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (ret != 0) { | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return mbedtls_entropy_write_seed_file(ctx, path); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | #endif /* MBEDTLS_FS_IO */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_SELF_TEST)
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Dummy source function | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static int entropy_dummy_source(void *data, unsigned char *output, | 
					
						
							|  |  |  |                                 size_t len, size_t *olen) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     ((void) data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memset(output, 0x2a, len); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     *olen = len; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  |     size_t entropy_len = 0; | 
					
						
							|  |  |  |     size_t olen = 0; | 
					
						
							|  |  |  |     size_t attempts = buf_len; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     while (attempts > 0 && entropy_len < buf_len) { | 
					
						
							|  |  |  |         if ((ret = mbedtls_hardware_poll(NULL, buf + entropy_len, | 
					
						
							|  |  |  |                                          buf_len - entropy_len, &olen)) != 0) { | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         entropy_len += olen; | 
					
						
							|  |  |  |         attempts--; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (entropy_len < buf_len) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         ret = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf, | 
					
						
							|  |  |  |                                                        size_t buf_len) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     unsigned char set = 0xFF; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     unsigned char unset = 0x00; | 
					
						
							|  |  |  |     size_t i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     for (i = 0; i < buf_len; i++) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         set &= buf[i]; | 
					
						
							|  |  |  |         unset |= buf[i]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return set == 0xFF || unset == 0x00; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |  * A test to ensure that the entropy sources are functioning correctly | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |  * and there is no obvious failure. The test performs the following checks: | 
					
						
							|  |  |  |  *  - The entropy source is not providing only 0s (all bits unset) or 1s (all | 
					
						
							|  |  |  |  *    bits set). | 
					
						
							|  |  |  |  *  - The entropy source is not providing values in a pattern. Because the | 
					
						
							|  |  |  |  *    hardware could be providing data in an arbitrary length, this check polls | 
					
						
							|  |  |  |  *    the hardware entropy source twice and compares the result to ensure they | 
					
						
							|  |  |  |  *    are not equal. | 
					
						
							|  |  |  |  *  - The error code returned by the entropy source is not an error. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_source_self_test(int verbose) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     unsigned char buf0[2 * sizeof(unsigned long long int)]; | 
					
						
							|  |  |  |     unsigned char buf1[2 * sizeof(unsigned long long int)]; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (verbose != 0) { | 
					
						
							|  |  |  |         mbedtls_printf("  ENTROPY_BIAS test: "); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memset(buf0, 0x00, sizeof(buf0)); | 
					
						
							|  |  |  |     memset(buf1, 0x00, sizeof(buf1)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Make sure that the returned values are not all 0 or 1 */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  |     if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Make sure that the entropy source is not returning values in a
 | 
					
						
							|  |  |  |      * pattern */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     ret = memcmp(buf0, buf1, sizeof(buf0)) == 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | cleanup: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (verbose != 0) { | 
					
						
							|  |  |  |         if (ret != 0) { | 
					
						
							|  |  |  |             mbedtls_printf("failed\n"); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             mbedtls_printf("passed\n"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         mbedtls_printf("\n"); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret != 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * The actual entropy quality is hard to test, but we can at least | 
					
						
							|  |  |  |  * test that the functions don't cause errors and write the correct | 
					
						
							|  |  |  |  * amount of data to buffers. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | int mbedtls_entropy_self_test(int verbose) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret = 1; | 
					
						
							|  |  |  |     mbedtls_entropy_context ctx; | 
					
						
							|  |  |  |     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; | 
					
						
							|  |  |  |     unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; | 
					
						
							|  |  |  |     size_t i, j; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (verbose != 0) { | 
					
						
							|  |  |  |         mbedtls_printf("  ENTROPY test: "); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_entropy_init(&ctx); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* First do a gather to make sure we have default sources */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_entropy_gather(&ctx)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16, | 
					
						
							|  |  |  |                                      MBEDTLS_ENTROPY_SOURCE_WEAK); | 
					
						
							|  |  |  |     if (ret != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * To test that mbedtls_entropy_func writes correct number of bytes: | 
					
						
							|  |  |  |      * - use the whole buffer and rely on ASan to detect overruns | 
					
						
							|  |  |  |      * - collect entropy 8 times and OR the result in an accumulator: | 
					
						
							|  |  |  |      *   any byte should then be 0 with probably 2^(-64), so requiring | 
					
						
							|  |  |  |      *   each of the 32 or 64 bytes to be non-zero has a false failure rate | 
					
						
							|  |  |  |      *   of at most 2^(-58) which is acceptable. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     for (i = 0; i < 8; i++) { | 
					
						
							|  |  |  |         if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         for (j = 0; j < sizeof(buf); j++) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             acc[j] |= buf[j]; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     for (j = 0; j < sizeof(buf); j++) { | 
					
						
							|  |  |  |         if (acc[j] == 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             ret = 1; | 
					
						
							|  |  |  |             goto cleanup; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if ((ret = mbedtls_entropy_source_self_test(0)) != 0) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         goto cleanup; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | cleanup: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_entropy_free(&ctx); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (verbose != 0) { | 
					
						
							|  |  |  |         if (ret != 0) { | 
					
						
							|  |  |  |             mbedtls_printf("failed\n"); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             mbedtls_printf("passed\n"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         mbedtls_printf("\n"); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     return ret != 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | #endif /* MBEDTLS_SELF_TEST */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* MBEDTLS_ENTROPY_C */
 |