Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Entropy accumulator implementation |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_ENTROPY_C) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 11 | |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 12 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) |
Simon Butcher | 1ceab6e | 2016-06-21 10:14:00 +0100 | [diff] [blame] | 13 | #warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! " |
| 14 | #warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES " |
| 15 | #warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE " |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 16 | #endif |
| 17 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 18 | #include "mbedtls/entropy.h" |
| 19 | #include "mbedtls/entropy_poll.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 20 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 21 | #include "mbedtls/error.h" |
Dave Rodgman | bae79fa | 2023-06-28 11:54:53 +0100 | [diff] [blame] | 22 | #include "mbedtls/sha256.h" |
| 23 | #include "mbedtls/sha512.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 24 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #endif |
| 30 | |
Paul Bakker | 217efbc | 2016-07-14 14:30:03 +0100 | [diff] [blame] | 31 | #include "mbedtls/platform.h" |
Paul Bakker | 217efbc | 2016-07-14 14:30:03 +0100 | [diff] [blame] | 32 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/platform.h" |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_HAVEGE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 36 | #include "mbedtls/havege.h" |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 39 | #define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */ |
| 40 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 41 | void mbedtls_entropy_init(mbedtls_entropy_context *ctx) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 42 | { |
Andres Amaya Garcia | a7559cb | 2017-06-29 16:12:31 +0100 | [diff] [blame] | 43 | ctx->source_count = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | memset(ctx->source, 0, sizeof(ctx->source)); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 47 | mbedtls_mutex_init(&ctx->mutex); |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Andres Amaya Garcia | 95869c4 | 2017-06-29 16:31:44 +0100 | [diff] [blame] | 50 | ctx->accumulator_started = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | mbedtls_sha512_init(&ctx->accumulator); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 53 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | mbedtls_sha256_init(&ctx->accumulator); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 55 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #if defined(MBEDTLS_HAVEGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | mbedtls_havege_init(&ctx->havege_data); |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 58 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 59 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 60 | /* Reminder: Update ENTROPY_HAVE_STRONG in the test files |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 61 | * when adding more strong entropy sources here. */ |
Hanno Becker | c6deafc | 2017-07-23 14:06:42 +0100 | [diff] [blame] | 62 | |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 63 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | mbedtls_entropy_add_source(ctx, mbedtls_null_entropy_poll, NULL, |
| 65 | 1, MBEDTLS_ENTROPY_SOURCE_STRONG); |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 66 | #endif |
| 67 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | #if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES) |
| 69 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL, |
| 71 | MBEDTLS_ENTROPY_MIN_PLATFORM, |
| 72 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 73 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 75 | mbedtls_entropy_add_source(ctx, mbedtls_hardclock_poll, NULL, |
| 76 | MBEDTLS_ENTROPY_MIN_HARDCLOCK, |
| 77 | MBEDTLS_ENTROPY_SOURCE_WEAK); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 78 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | #if defined(MBEDTLS_HAVEGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | mbedtls_entropy_add_source(ctx, mbedtls_havege_poll, &ctx->havege_data, |
| 81 | MBEDTLS_ENTROPY_MIN_HAVEGE, |
| 82 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Paul Bakker | 28c7e7f | 2011-12-15 19:49:30 +0000 | [diff] [blame] | 83 | #endif |
Manuel Pégourié-Gonnard | 3f77dfb | 2015-06-19 10:06:21 +0200 | [diff] [blame] | 84 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL, |
| 86 | MBEDTLS_ENTROPY_MIN_HARDWARE, |
| 87 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Manuel Pégourié-Gonnard | 3f77dfb | 2015-06-19 10:06:21 +0200 | [diff] [blame] | 88 | #endif |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 89 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL, |
| 91 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 92 | MBEDTLS_ENTROPY_SOURCE_STRONG); |
Andres Amaya Garcia | a7559cb | 2017-06-29 16:12:31 +0100 | [diff] [blame] | 93 | ctx->initial_entropy_run = 0; |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 94 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 98 | void mbedtls_entropy_free(mbedtls_entropy_context *ctx) |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 99 | { |
Gilles Peskine | b158321 | 2021-02-22 21:26:54 +0100 | [diff] [blame] | 100 | /* If the context was already free, don't call free() again. |
| 101 | * This is important for mutexes which don't allow double-free. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | if (ctx->accumulator_started == -1) { |
Gilles Peskine | b158321 | 2021-02-22 21:26:54 +0100 | [diff] [blame] | 103 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | } |
Gilles Peskine | b158321 | 2021-02-22 21:26:54 +0100 | [diff] [blame] | 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | #if defined(MBEDTLS_HAVEGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 107 | mbedtls_havege_free(&ctx->havege_data); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 108 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | mbedtls_mutex_free(&ctx->mutex); |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 111 | #endif |
Andres Amaya Garcia | a7559cb | 2017-06-29 16:12:31 +0100 | [diff] [blame] | 112 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | mbedtls_sha512_free(&ctx->accumulator); |
Andres Amaya Garcia | a7559cb | 2017-06-29 16:12:31 +0100 | [diff] [blame] | 114 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | mbedtls_sha256_free(&ctx->accumulator); |
Andres Amaya Garcia | a7559cb | 2017-06-29 16:12:31 +0100 | [diff] [blame] | 116 | #endif |
| 117 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 118 | ctx->initial_entropy_run = 0; |
| 119 | #endif |
| 120 | ctx->source_count = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source)); |
Gilles Peskine | b158321 | 2021-02-22 21:26:54 +0100 | [diff] [blame] | 122 | ctx->accumulator_started = -1; |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 125 | int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx, |
| 126 | mbedtls_entropy_f_source_ptr f_source, void *p_source, |
| 127 | size_t threshold, int strong) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 128 | { |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 129 | int idx, ret = 0; |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 133 | return ret; |
| 134 | } |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 135 | #endif |
| 136 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 137 | idx = ctx->source_count; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 138 | if (idx >= MBEDTLS_ENTROPY_MAX_SOURCES) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | ret = MBEDTLS_ERR_ENTROPY_MAX_SOURCES; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 140 | goto exit; |
| 141 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 142 | |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 143 | ctx->source[idx].f_source = f_source; |
| 144 | ctx->source[idx].p_source = p_source; |
| 145 | ctx->source[idx].threshold = threshold; |
| 146 | ctx->source[idx].strong = strong; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 147 | |
| 148 | ctx->source_count++; |
| 149 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 150 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 153 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 154 | } |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 155 | #endif |
| 156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | return ret; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Entropy accumulator update |
| 162 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id, |
| 164 | const unsigned char *data, size_t len) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 165 | { |
| 166 | unsigned char header[2]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 168 | size_t use_len = len; |
| 169 | const unsigned char *p = data; |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 170 | int ret = 0; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | if (use_len > MBEDTLS_ENTROPY_BLOCK_SIZE) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | if ((ret = mbedtls_sha512_ret(data, len, tmp, 0)) != 0) { |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 175 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | } |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 177 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 178 | if ((ret = mbedtls_sha256_ret(data, len, tmp, 0)) != 0) { |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 179 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | } |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 181 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 182 | p = tmp; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | header[0] = source_id; |
| 187 | header[1] = use_len & 0xFF; |
| 188 | |
Andres Amaya Garcia | 95869c4 | 2017-06-29 16:31:44 +0100 | [diff] [blame] | 189 | /* |
| 190 | * Start the accumulator if this has not already happened. Note that |
| 191 | * it is sufficient to start the accumulator here only because all calls to |
| 192 | * gather entropy eventually execute this code. |
| 193 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 195 | if (ctx->accumulator_started == 0 && |
| 196 | (ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) { |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 197 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | } else { |
Andres Amaya Garcia | 95869c4 | 2017-06-29 16:31:44 +0100 | [diff] [blame] | 199 | ctx->accumulator_started = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | } |
| 201 | if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, header, 2)) != 0) { |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 202 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | } |
| 204 | ret = mbedtls_sha512_update_ret(&ctx->accumulator, p, use_len); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 205 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 206 | if (ctx->accumulator_started == 0 && |
| 207 | (ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) { |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 208 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | } else { |
Andres Amaya Garcia | 95869c4 | 2017-06-29 16:31:44 +0100 | [diff] [blame] | 210 | ctx->accumulator_started = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 211 | } |
| 212 | if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, header, 2)) != 0) { |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 213 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 214 | } |
| 215 | ret = mbedtls_sha256_update_ret(&ctx->accumulator, p, use_len); |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 216 | #endif |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 217 | |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 218 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 219 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
Andres Amaya Garcia | 6512193 | 2017-07-05 15:45:47 +0100 | [diff] [blame] | 220 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | return ret; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 224 | int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx, |
| 225 | const unsigned char *data, size_t len) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 226 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 227 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 230 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 231 | return ret; |
| 232 | } |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 233 | #endif |
| 234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len); |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 239 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 240 | } |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 241 | #endif |
| 242 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 243 | return ret; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Run through the different sources to add entropy to our accumulator |
| 248 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | static int entropy_gather_internal(mbedtls_entropy_context *ctx) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 250 | { |
Gilles Peskine | 006c1b5 | 2019-09-30 17:29:54 +0200 | [diff] [blame] | 251 | int ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 252 | int i; |
| 253 | int have_one_strong = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER]; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 255 | size_t olen; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 256 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | if (ctx->source_count == 0) { |
| 258 | return MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED; |
| 259 | } |
Paul Bakker | 43655f4 | 2011-12-15 20:11:16 +0000 | [diff] [blame] | 260 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 261 | /* |
| 262 | * Run through our entropy sources |
| 263 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | for (i = 0; i < ctx->source_count; i++) { |
| 265 | if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) { |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 266 | have_one_strong = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 267 | } |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 268 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 269 | olen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | if ((ret = ctx->source[i].f_source(ctx->source[i].p_source, |
| 271 | buf, MBEDTLS_ENTROPY_MAX_GATHER, &olen)) != 0) { |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 272 | goto cleanup; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Add if we actually gathered something |
| 277 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 278 | if (olen > 0) { |
| 279 | if ((ret = entropy_update(ctx, (unsigned char) i, |
| 280 | buf, olen)) != 0) { |
| 281 | return ret; |
| 282 | } |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 283 | ctx->source[i].size += olen; |
| 284 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | if (have_one_strong == 0) { |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 288 | ret = MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | } |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 290 | |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 291 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 292 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 293 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | return ret; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 297 | /* |
| 298 | * Thread-safe wrapper for entropy_gather_internal() |
| 299 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 300 | int mbedtls_entropy_gather(mbedtls_entropy_context *ctx) |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 301 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 302 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 305 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 306 | return ret; |
| 307 | } |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 308 | #endif |
| 309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | ret = entropy_gather_internal(ctx); |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 313 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 314 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 315 | } |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 316 | #endif |
| 317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 318 | return ret; |
Paul Bakker | 47703a0 | 2014-02-06 15:01:20 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | int mbedtls_entropy_func(void *data, unsigned char *output, size_t len) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 322 | { |
Gilles Peskine | 85485c7 | 2019-10-08 15:04:16 +0200 | [diff] [blame] | 323 | int ret, count = 0, i, thresholds_reached; |
| 324 | size_t strong_size; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data; |
| 326 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 327 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 328 | if (len > MBEDTLS_ENTROPY_BLOCK_SIZE) { |
| 329 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 330 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 331 | |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 332 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 333 | /* Update the NV entropy seed before generating any entropy for outside |
| 334 | * use. |
| 335 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 336 | if (ctx->initial_entropy_run == 0) { |
Paul Bakker | fc9c7c8 | 2016-06-01 15:25:50 +0100 | [diff] [blame] | 337 | ctx->initial_entropy_run = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 338 | if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) { |
| 339 | return ret; |
| 340 | } |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 341 | } |
| 342 | #endif |
| 343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 345 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 346 | return ret; |
| 347 | } |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 348 | #endif |
| 349 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 350 | /* |
| 351 | * Always gather extra entropy before a call |
| 352 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 353 | do { |
| 354 | if (count++ > ENTROPY_MAX_LOOP) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 356 | goto exit; |
| 357 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 358 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 359 | if ((ret = entropy_gather_internal(ctx)) != 0) { |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 360 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 361 | } |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 362 | |
Gilles Peskine | 85485c7 | 2019-10-08 15:04:16 +0200 | [diff] [blame] | 363 | thresholds_reached = 1; |
| 364 | strong_size = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | for (i = 0; i < ctx->source_count; i++) { |
| 366 | if (ctx->source[i].size < ctx->source[i].threshold) { |
Gilles Peskine | 85485c7 | 2019-10-08 15:04:16 +0200 | [diff] [blame] | 367 | thresholds_reached = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | } |
| 369 | if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) { |
Gilles Peskine | 85485c7 | 2019-10-08 15:04:16 +0200 | [diff] [blame] | 370 | strong_size += ctx->source[i].size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 371 | } |
Gilles Peskine | 85485c7 | 2019-10-08 15:04:16 +0200 | [diff] [blame] | 372 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 373 | } while (!thresholds_reached || strong_size < MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 375 | memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
Andres Amaya Garcia | b2b063f | 2017-07-20 16:45:24 +0100 | [diff] [blame] | 378 | /* |
| 379 | * Note that at this stage it is assumed that the accumulator was started |
| 380 | * in a previous call to entropy_update(). If this is not guaranteed, the |
| 381 | * code below will fail. |
| 382 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | if ((ret = mbedtls_sha512_finish_ret(&ctx->accumulator, buf)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 384 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | } |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 386 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 387 | /* |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 388 | * Reset accumulator and counters and recycle existing entropy |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 389 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | mbedtls_sha512_free(&ctx->accumulator); |
| 391 | mbedtls_sha512_init(&ctx->accumulator); |
| 392 | if ((ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 393 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | } |
| 395 | if ((ret = mbedtls_sha512_update_ret(&ctx->accumulator, buf, |
| 396 | MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 397 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | } |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 399 | |
| 400 | /* |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 401 | * Perform second SHA-512 on entropy |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 402 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 403 | if ((ret = mbedtls_sha512_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 404 | buf, 0)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 405 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | #else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 408 | if ((ret = mbedtls_sha256_finish_ret(&ctx->accumulator, buf)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 409 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | } |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * Reset accumulator and counters and recycle existing entropy |
| 414 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 415 | mbedtls_sha256_free(&ctx->accumulator); |
| 416 | mbedtls_sha256_init(&ctx->accumulator); |
| 417 | if ((ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 418 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 419 | } |
| 420 | if ((ret = mbedtls_sha256_update_ret(&ctx->accumulator, buf, |
| 421 | MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 422 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | } |
Paul Bakker | b13d3ff | 2014-03-26 12:51:25 +0100 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * Perform second SHA-256 on entropy |
| 427 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 428 | if ((ret = mbedtls_sha256_ret(buf, MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 429 | buf, 0)) != 0) { |
Andres Amaya Garcia | 207cea5 | 2017-06-29 13:28:13 +0100 | [diff] [blame] | 430 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 431 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | #endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */ |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 433 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 434 | for (i = 0; i < ctx->source_count; i++) { |
Paul Bakker | bd4a9d0 | 2011-12-10 17:02:19 +0000 | [diff] [blame] | 435 | ctx->source[i].size = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | memcpy(output, buf, len); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 439 | |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 440 | ret = 0; |
| 441 | |
| 442 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 443 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 445 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 446 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 447 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 448 | } |
Paul Bakker | f4e7dc5 | 2013-09-28 15:23:57 +0200 | [diff] [blame] | 449 | #endif |
| 450 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 451 | return ret; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 454 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 455 | int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx) |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 456 | { |
| 457 | int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
Andres Amaya Garcia | af0b31d | 2017-07-05 14:23:54 +0100 | [diff] [blame] | 458 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 459 | |
| 460 | /* Read new seed and write it to NV */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { |
| 462 | return ret; |
| 463 | } |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 464 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 465 | if (mbedtls_nv_seed_write(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) { |
| 466 | return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
| 467 | } |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 468 | |
| 469 | /* Manually update the remaining stream with a separator value to diverge */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 470 | memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 471 | ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 472 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | return ret; |
Paul Bakker | d5c9f6d | 2016-06-01 11:30:54 +0100 | [diff] [blame] | 474 | } |
| 475 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | #if defined(MBEDTLS_FS_IO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path) |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 479 | { |
Victor Krasnoshchok | a0c2d19 | 2020-09-03 00:07:05 +0300 | [diff] [blame] | 480 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Victor Krasnoshchok | e79812e | 2020-08-27 00:19:55 +0300 | [diff] [blame] | 481 | FILE *f = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 483 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 484 | if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 485 | ret = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 486 | goto exit; |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 487 | } |
| 488 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 489 | if ((f = fopen(path, "wb")) == NULL) { |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 490 | ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
| 491 | goto exit; |
| 492 | } |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 493 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 494 | if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != MBEDTLS_ENTROPY_BLOCK_SIZE) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 495 | ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 496 | goto exit; |
| 497 | } |
| 498 | |
| 499 | ret = 0; |
| 500 | |
| 501 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 502 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 503 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 504 | if (f != NULL) { |
| 505 | fclose(f); |
| 506 | } |
Victor Krasnoshchok | e79812e | 2020-08-27 00:19:55 +0300 | [diff] [blame] | 507 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | return ret; |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 511 | int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path) |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 512 | { |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 513 | int ret = 0; |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 514 | FILE *f; |
| 515 | size_t n; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 516 | unsigned char buf[MBEDTLS_ENTROPY_MAX_SEED_SIZE]; |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 517 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 518 | if ((f = fopen(path, "rb")) == NULL) { |
| 519 | return MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
| 520 | } |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 521 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 522 | fseek(f, 0, SEEK_END); |
| 523 | n = (size_t) ftell(f); |
| 524 | fseek(f, 0, SEEK_SET); |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 526 | if (n > MBEDTLS_ENTROPY_MAX_SEED_SIZE) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | n = MBEDTLS_ENTROPY_MAX_SEED_SIZE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 528 | } |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 529 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 530 | if (fread(buf, 1, n, f) != n) { |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 531 | ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 532 | } else { |
| 533 | ret = mbedtls_entropy_update_manual(ctx, buf, n); |
| 534 | } |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 536 | fclose(f); |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 537 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 538 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
Andres Amaya Garcia | 1adcd95 | 2017-06-26 09:58:59 +0100 | [diff] [blame] | 539 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 540 | if (ret != 0) { |
| 541 | return ret; |
| 542 | } |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 543 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 544 | return mbedtls_entropy_write_seed_file(ctx, path); |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 545 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 66ff70d | 2014-03-26 11:54:05 +0100 | [diff] [blame] | 547 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | #if defined(MBEDTLS_SELF_TEST) |
Simon Butcher | 669c635 | 2016-09-15 18:57:34 +0100 | [diff] [blame] | 549 | #if !defined(MBEDTLS_TEST_NULL_ENTROPY) |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 550 | /* |
| 551 | * Dummy source function |
| 552 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 553 | static int entropy_dummy_source(void *data, unsigned char *output, |
| 554 | size_t len, size_t *olen) |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 555 | { |
| 556 | ((void) data); |
| 557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 558 | memset(output, 0x2a, len); |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 559 | *olen = len; |
| 560 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 561 | return 0; |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 562 | } |
Simon Butcher | 669c635 | 2016-09-15 18:57:34 +0100 | [diff] [blame] | 563 | #endif /* !MBEDTLS_TEST_NULL_ENTROPY */ |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 564 | |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 565 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 566 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 567 | static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len) |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 568 | { |
| 569 | int ret = 0; |
| 570 | size_t entropy_len = 0; |
| 571 | size_t olen = 0; |
| 572 | size_t attempts = buf_len; |
| 573 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 574 | while (attempts > 0 && entropy_len < buf_len) { |
| 575 | if ((ret = mbedtls_hardware_poll(NULL, buf + entropy_len, |
| 576 | buf_len - entropy_len, &olen)) != 0) { |
| 577 | return ret; |
| 578 | } |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 579 | |
| 580 | entropy_len += olen; |
| 581 | attempts--; |
| 582 | } |
| 583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | if (entropy_len < buf_len) { |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 585 | ret = 1; |
| 586 | } |
| 587 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 588 | return ret; |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 592 | static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf, |
| 593 | size_t buf_len) |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 594 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 595 | unsigned char set = 0xFF; |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 596 | unsigned char unset = 0x00; |
| 597 | size_t i; |
| 598 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 599 | for (i = 0; i < buf_len; i++) { |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 600 | set &= buf[i]; |
| 601 | unset |= buf[i]; |
| 602 | } |
| 603 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 604 | return set == 0xFF || unset == 0x00; |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 605 | } |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 606 | |
| 607 | /* |
Zachary Fleckenstein | 4364fc9 | 2022-12-09 09:26:42 -0500 | [diff] [blame] | 608 | * A test to ensure that the entropy sources are functioning correctly |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 609 | * and there is no obvious failure. The test performs the following checks: |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 610 | * - The entropy source is not providing only 0s (all bits unset) or 1s (all |
| 611 | * bits set). |
| 612 | * - The entropy source is not providing values in a pattern. Because the |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 613 | * hardware could be providing data in an arbitrary length, this check polls |
| 614 | * the hardware entropy source twice and compares the result to ensure they |
| 615 | * are not equal. |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 616 | * - The error code returned by the entropy source is not an error. |
| 617 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 618 | int mbedtls_entropy_source_self_test(int verbose) |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 619 | { |
| 620 | int ret = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 621 | unsigned char buf0[2 * sizeof(unsigned long long int)]; |
| 622 | unsigned char buf1[2 * sizeof(unsigned long long int)]; |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 623 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 624 | if (verbose != 0) { |
| 625 | mbedtls_printf(" ENTROPY_BIAS test: "); |
| 626 | } |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 627 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 628 | memset(buf0, 0x00, sizeof(buf0)); |
| 629 | memset(buf1, 0x00, sizeof(buf1)); |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 630 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 631 | if ((ret = mbedtls_entropy_source_self_test_gather(buf0, sizeof(buf0))) != 0) { |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 632 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 633 | } |
| 634 | if ((ret = mbedtls_entropy_source_self_test_gather(buf1, sizeof(buf1))) != 0) { |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 635 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 636 | } |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 637 | |
| 638 | /* Make sure that the returned values are not all 0 or 1 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 639 | if ((ret = mbedtls_entropy_source_self_test_check_bits(buf0, sizeof(buf0))) != 0) { |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 640 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 641 | } |
| 642 | if ((ret = mbedtls_entropy_source_self_test_check_bits(buf1, sizeof(buf1))) != 0) { |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 643 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 644 | } |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 645 | |
| 646 | /* Make sure that the entropy source is not returning values in a |
| 647 | * pattern */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 648 | ret = memcmp(buf0, buf1, sizeof(buf0)) == 0; |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 649 | |
| 650 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 651 | if (verbose != 0) { |
| 652 | if (ret != 0) { |
| 653 | mbedtls_printf("failed\n"); |
| 654 | } else { |
| 655 | mbedtls_printf("passed\n"); |
| 656 | } |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 657 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 658 | mbedtls_printf("\n"); |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 659 | } |
| 660 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 661 | return ret != 0; |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 662 | } |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 663 | |
| 664 | #endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */ |
Andres AG | b34e42e | 2016-08-22 11:08:50 +0100 | [diff] [blame] | 665 | |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 666 | /* |
| 667 | * The actual entropy quality is hard to test, but we can at least |
| 668 | * test that the functions don't cause errors and write the correct |
| 669 | * amount of data to buffers. |
| 670 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 671 | int mbedtls_entropy_self_test(int verbose) |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 672 | { |
Andres Amaya Garcia | a928e67 | 2016-09-13 13:30:02 +0100 | [diff] [blame] | 673 | int ret = 1; |
Simon Butcher | 669c635 | 2016-09-15 18:57:34 +0100 | [diff] [blame] | 674 | #if !defined(MBEDTLS_TEST_NULL_ENTROPY) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | mbedtls_entropy_context ctx; |
| 676 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
| 677 | unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 678 | size_t i, j; |
Simon Butcher | 669c635 | 2016-09-15 18:57:34 +0100 | [diff] [blame] | 679 | #endif /* !MBEDTLS_TEST_NULL_ENTROPY */ |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 680 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 681 | if (verbose != 0) { |
| 682 | mbedtls_printf(" ENTROPY test: "); |
| 683 | } |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 684 | |
Andres Amaya Garcia | a928e67 | 2016-09-13 13:30:02 +0100 | [diff] [blame] | 685 | #if !defined(MBEDTLS_TEST_NULL_ENTROPY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 686 | mbedtls_entropy_init(&ctx); |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 687 | |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 688 | /* First do a gather to make sure we have default sources */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 689 | if ((ret = mbedtls_entropy_gather(&ctx)) != 0) { |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 690 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 691 | } |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 693 | ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16, |
| 694 | MBEDTLS_ENTROPY_SOURCE_WEAK); |
| 695 | if (ret != 0) { |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 696 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 697 | } |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 698 | |
Dave Rodgman | 1868870 | 2023-02-02 12:40:50 +0000 | [diff] [blame] | 699 | if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) { |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 700 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 701 | } |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 702 | |
| 703 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | * To test that mbedtls_entropy_func writes correct number of bytes: |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 705 | * - use the whole buffer and rely on ASan to detect overruns |
| 706 | * - collect entropy 8 times and OR the result in an accumulator: |
| 707 | * any byte should then be 0 with probably 2^(-64), so requiring |
| 708 | * each of the 32 or 64 bytes to be non-zero has a false failure rate |
| 709 | * of at most 2^(-58) which is acceptable. |
| 710 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 711 | for (i = 0; i < 8; i++) { |
| 712 | if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) { |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 713 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 714 | } |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 715 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 716 | for (j = 0; j < sizeof(buf); j++) { |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 717 | acc[j] |= buf[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 718 | } |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 721 | for (j = 0; j < sizeof(buf); j++) { |
| 722 | if (acc[j] == 0) { |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 723 | ret = 1; |
| 724 | goto cleanup; |
| 725 | } |
| 726 | } |
| 727 | |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 728 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 729 | if ((ret = mbedtls_entropy_source_self_test(0)) != 0) { |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 730 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 731 | } |
Andres AG | e7723ec | 2016-08-25 10:18:50 +0100 | [diff] [blame] | 732 | #endif |
| 733 | |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 734 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | mbedtls_entropy_free(&ctx); |
Andres Amaya Garcia | a928e67 | 2016-09-13 13:30:02 +0100 | [diff] [blame] | 736 | #endif /* !MBEDTLS_TEST_NULL_ENTROPY */ |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 737 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 738 | if (verbose != 0) { |
| 739 | if (ret != 0) { |
| 740 | mbedtls_printf("failed\n"); |
| 741 | } else { |
| 742 | mbedtls_printf("passed\n"); |
| 743 | } |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 744 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 745 | mbedtls_printf("\n"); |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 748 | return ret != 0; |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 749 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | #endif /* MBEDTLS_SELF_TEST */ |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 751 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 752 | #endif /* MBEDTLS_ENTROPY_C */ |