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