Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/entropy.h" |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 3 | #include "mbedtls/entropy_poll.h" |
Gilles Peskine | 72d40fc | 2020-04-14 21:28:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/md.h" |
Mohammad Azim Khan | 67735d5 | 2017-04-06 11:55:43 +0100 | [diff] [blame] | 5 | #include "string.h" |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 6 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 7 | typedef enum { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 8 | DUMMY_CONSTANT_LENGTH, /* Output context->length bytes */ |
| 9 | DUMMY_REQUESTED_LENGTH, /* Output whatever length was requested */ |
| 10 | DUMMY_FAIL, /* Return an error code */ |
| 11 | } entropy_dummy_instruction; |
| 12 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 13 | typedef struct { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 14 | entropy_dummy_instruction instruction; |
| 15 | size_t length; /* Length to return for DUMMY_CONSTANT_LENGTH */ |
| 16 | size_t calls; /* Incremented at each call */ |
| 17 | } entropy_dummy_context; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Dummy entropy source |
| 21 | * |
| 22 | * If data is NULL, write exactly the requested length. |
| 23 | * Otherwise, write the length indicated by data or error if negative |
| 24 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 25 | static int entropy_dummy_source(void *arg, unsigned char *output, |
| 26 | size_t len, size_t *olen) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 27 | { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 28 | entropy_dummy_context *context = arg; |
| 29 | ++context->calls; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 30 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | switch (context->instruction) { |
Gilles Peskine | ed04a67 | 2019-10-08 14:37:27 +0200 | [diff] [blame] | 32 | case DUMMY_CONSTANT_LENGTH: |
| 33 | *olen = context->length; |
| 34 | break; |
| 35 | case DUMMY_REQUESTED_LENGTH: |
| 36 | *olen = len; |
| 37 | break; |
| 38 | case DUMMY_FAIL: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 39 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 42 | memset(output, 0x2a, *olen); |
| 43 | return 0; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 44 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Ability to clear entropy sources to allow testing with just predefined |
| 48 | * entropy sources. This function or tests depending on it might break if there |
| 49 | * are internal changes to how entropy sources are registered. |
| 50 | * |
| 51 | * To be called immediately after mbedtls_entropy_init(). |
| 52 | * |
| 53 | * Just resetting the counter. New sources will overwrite existing ones. |
| 54 | * This might break memory checks in the future if sources need 'free-ing' then |
| 55 | * as well. |
| 56 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 57 | static void entropy_clear_sources(mbedtls_entropy_context *ctx) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 58 | { |
| 59 | ctx->source_count = 0; |
| 60 | } |
| 61 | |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 63 | /* |
| 64 | * NV seed read/write functions that use a buffer instead of a file |
| 65 | */ |
| 66 | static unsigned char buffer_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 67 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | int buffer_nv_seed_read(unsigned char *buf, size_t buf_len) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 69 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) { |
| 71 | return -1; |
| 72 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 73 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | memcpy(buf, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 75 | return 0; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | int buffer_nv_seed_write(unsigned char *buf, size_t buf_len) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 79 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) { |
| 81 | return -1; |
| 82 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | memcpy(buffer_seed, buf, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 85 | return 0; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | * NV seed read/write helpers that fill the base seedfile |
| 90 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | static int write_nv_seed(unsigned char *buf, size_t buf_len) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 92 | { |
| 93 | FILE *f; |
| 94 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) { |
| 96 | return -1; |
| 97 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 98 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) { |
| 100 | return -1; |
| 101 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 102 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 103 | if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != |
| 104 | MBEDTLS_ENTROPY_BLOCK_SIZE) { |
Gilles Peskine | fa27636 | 2023-10-17 18:08:24 +0200 | [diff] [blame] | 105 | fclose(f); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | return -1; |
| 107 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 108 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | fclose(f); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 111 | return 0; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | int read_nv_seed(unsigned char *buf, size_t buf_len) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 115 | { |
| 116 | FILE *f; |
| 117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 118 | if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) { |
| 119 | return -1; |
| 120 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) { |
| 123 | return -1; |
| 124 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | if (fread(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) != |
| 127 | MBEDTLS_ENTROPY_BLOCK_SIZE) { |
Gilles Peskine | fa27636 | 2023-10-17 18:08:24 +0200 | [diff] [blame] | 128 | fclose(f); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 129 | return -1; |
| 130 | } |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | fclose(f); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 133 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 134 | return 0; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 135 | } |
Paul Bakker | 4a6c6fc | 2016-06-01 16:34:25 +0100 | [diff] [blame] | 136 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 137 | /* END_HEADER */ |
| 138 | |
| 139 | /* BEGIN_DEPENDENCIES |
Gilles Peskine | bfda1a9 | 2023-04-28 23:41:38 +0200 | [diff] [blame] | 140 | * depends_on:MBEDTLS_ENTROPY_C:!MBEDTLS_PSA_INJECT_ENTROPY |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 141 | * END_DEPENDENCIES |
| 142 | */ |
| 143 | |
Gilles Peskine | 3d979f7 | 2021-02-22 21:24:02 +0100 | [diff] [blame] | 144 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | void entropy_init_free(int reinit) |
Gilles Peskine | 3d979f7 | 2021-02-22 21:24:02 +0100 | [diff] [blame] | 146 | { |
| 147 | mbedtls_entropy_context ctx; |
| 148 | |
| 149 | /* Double free is not explicitly documented to work, but it is convenient |
| 150 | * to call mbedtls_entropy_free() unconditionally on an error path without |
| 151 | * checking whether it has already been called in the success path. */ |
| 152 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 153 | mbedtls_entropy_init(&ctx); |
| 154 | mbedtls_entropy_free(&ctx); |
Gilles Peskine | 3d979f7 | 2021-02-22 21:24:02 +0100 | [diff] [blame] | 155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | if (reinit) { |
| 157 | mbedtls_entropy_init(&ctx); |
| 158 | } |
| 159 | mbedtls_entropy_free(&ctx); |
Gilles Peskine | 3d979f7 | 2021-02-22 21:24:02 +0100 | [diff] [blame] | 160 | |
| 161 | /* This test case always succeeds, functionally speaking. A plausible |
| 162 | * bug might trigger an invalid pointer dereference or a memory leak. */ |
| 163 | goto exit; |
| 164 | } |
| 165 | /* END_CASE */ |
| 166 | |
Simon Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 167 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 168 | void entropy_seed_file(char *path, int ret) |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | mbedtls_entropy_init(&ctx); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) == ret); |
| 175 | TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path) == ret); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 176 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 177 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 178 | mbedtls_entropy_free(&ctx); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 179 | } |
| 180 | /* END_CASE */ |
| 181 | |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 182 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | void entropy_write_base_seed_file(int ret) |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 184 | { |
| 185 | mbedtls_entropy_context ctx; |
| 186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | mbedtls_entropy_init(&ctx); |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret); |
| 190 | TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret); |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 191 | |
| 192 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | mbedtls_entropy_free(&ctx); |
Victor Krasnoshchok | b3129ba | 2020-08-29 22:54:37 +0300 | [diff] [blame] | 194 | } |
| 195 | /* END_CASE */ |
| 196 | |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 197 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | void entropy_no_sources() |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 199 | { |
| 200 | mbedtls_entropy_context ctx; |
| 201 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | mbedtls_entropy_init(&ctx); |
| 204 | entropy_clear_sources(&ctx); |
| 205 | TEST_EQUAL(mbedtls_entropy_func(&ctx, buf, sizeof(buf)), |
| 206 | MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED); |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 207 | |
| 208 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | mbedtls_entropy_free(&ctx); |
Gilles Peskine | 7f24651 | 2019-10-08 14:51:49 +0200 | [diff] [blame] | 210 | } |
| 211 | /* END_CASE */ |
| 212 | |
| 213 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 214 | void entropy_too_many_sources() |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 215 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 217 | size_t i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | entropy_dummy_context dummy = { DUMMY_REQUESTED_LENGTH, 0, 0 }; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 220 | mbedtls_entropy_init(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 221 | |
| 222 | /* |
| 223 | * It's hard to tell precisely when the error will occur, |
| 224 | * since we don't know how many sources were automatically added. |
| 225 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | for (i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++) { |
| 227 | (void) mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy, |
| 228 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK); |
| 229 | } |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy, |
| 232 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK) |
| 233 | == MBEDTLS_ERR_ENTROPY_MAX_SOURCES); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 234 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 235 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 236 | mbedtls_entropy_free(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 237 | } |
| 238 | /* END_CASE */ |
| 239 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 240 | /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | void entropy_func_len(int len, int ret) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 242 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | mbedtls_entropy_context ctx; |
| 244 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 }; |
| 245 | unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 }; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 246 | size_t i, j; |
| 247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 248 | mbedtls_entropy_init(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 249 | |
| 250 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | * See comments in mbedtls_entropy_self_test() |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 252 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 253 | for (i = 0; i < 8; i++) { |
| 254 | TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, len) == ret); |
| 255 | for (j = 0; j < sizeof(buf); j++) { |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 256 | acc[j] |= buf[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | } |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | if (ret == 0) { |
| 261 | for (j = 0; j < (size_t) len; j++) { |
| 262 | TEST_ASSERT(acc[j] != 0); |
| 263 | } |
| 264 | } |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 265 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | for (j = len; j < sizeof(buf); j++) { |
| 267 | TEST_ASSERT(acc[j] == 0); |
| 268 | } |
Gilles Peskine | 7aba036 | 2021-01-31 00:07:11 +0100 | [diff] [blame] | 269 | |
| 270 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 271 | mbedtls_entropy_free(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 272 | } |
| 273 | /* END_CASE */ |
| 274 | |
| 275 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | void entropy_source_fail(char *path) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 277 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | mbedtls_entropy_context ctx; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 279 | unsigned char buf[16]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 280 | entropy_dummy_context dummy = { DUMMY_FAIL, 0, 0 }; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | mbedtls_entropy_init(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, |
| 285 | &dummy, 16, |
| 286 | MBEDTLS_ENTROPY_SOURCE_WEAK) |
| 287 | == 0); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 288 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, sizeof(buf)) |
| 290 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED); |
| 291 | TEST_ASSERT(mbedtls_entropy_gather(&ctx) |
| 292 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED); |
Simon Butcher | b7f45c5 | 2016-09-15 18:42:26 +0100 | [diff] [blame] | 293 | #if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) |
| 295 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED); |
| 296 | TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path) |
| 297 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 298 | #else |
| 299 | ((void) path); |
| 300 | #endif |
| 301 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 302 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 303 | mbedtls_entropy_free(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 304 | } |
| 305 | /* END_CASE */ |
| 306 | |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 307 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 308 | void entropy_threshold(int threshold, int chunk_size, int result) |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 310 | mbedtls_entropy_context ctx; |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 311 | entropy_dummy_context strong = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | { DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0 }; |
| 313 | entropy_dummy_context weak = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 315 | int ret; |
| 316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 317 | mbedtls_entropy_init(&ctx); |
| 318 | entropy_clear_sources(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 319 | |
Gilles Peskine | cbd91e0 | 2019-11-25 19:50:54 +0100 | [diff] [blame] | 320 | /* Set strong source that reaches its threshold immediately and |
| 321 | * a weak source whose threshold is a test parameter. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 322 | TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, |
| 323 | &strong, 1, |
| 324 | MBEDTLS_ENTROPY_SOURCE_STRONG) == 0); |
| 325 | TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, |
| 326 | &weak, threshold, |
| 327 | MBEDTLS_ENTROPY_SOURCE_WEAK) == 0); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 329 | ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf)); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 330 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 331 | if (result >= 0) { |
| 332 | TEST_ASSERT(ret == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 333 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Gilles Peskine | ae67939 | 2019-11-25 18:26:23 +0100 | [diff] [blame] | 334 | /* If the NV seed functionality is enabled, there are two entropy |
| 335 | * updates: before and after updating the NV seed. */ |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 336 | result *= 2; |
| 337 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 338 | TEST_ASSERT(weak.calls == (size_t) result); |
| 339 | } else { |
| 340 | TEST_ASSERT(ret == result); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 343 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | mbedtls_entropy_free(&ctx); |
Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 345 | } |
| 346 | /* END_CASE */ |
| 347 | |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 348 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | void entropy_calls(int strength1, int strength2, |
| 350 | int threshold, int chunk_size, |
| 351 | int result) |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 352 | { |
| 353 | /* |
| 354 | * if result >= 0: result = expected number of calls to source 1 |
| 355 | * if result < 0: result = expected return code from mbedtls_entropy_func() |
| 356 | */ |
| 357 | |
| 358 | mbedtls_entropy_context ctx; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 359 | entropy_dummy_context dummy1 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 }; |
| 360 | entropy_dummy_context dummy2 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 }; |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 361 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; |
| 362 | int ret; |
| 363 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 364 | mbedtls_entropy_init(&ctx); |
| 365 | entropy_clear_sources(&ctx); |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 366 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 367 | TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, |
| 368 | &dummy1, threshold, |
| 369 | strength1) == 0); |
| 370 | TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, |
| 371 | &dummy2, threshold, |
| 372 | strength2) == 0); |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 373 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf)); |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 375 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | if (result >= 0) { |
| 377 | TEST_ASSERT(ret == 0); |
Gilles Peskine | ae67939 | 2019-11-25 18:26:23 +0100 | [diff] [blame] | 378 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 379 | /* If the NV seed functionality is enabled, there are two entropy |
| 380 | * updates: before and after updating the NV seed. */ |
| 381 | result *= 2; |
| 382 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | TEST_ASSERT(dummy1.calls == (size_t) result); |
| 384 | } else { |
| 385 | TEST_ASSERT(ret == result); |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | mbedtls_entropy_free(&ctx); |
Gilles Peskine | 65fc068 | 2019-10-08 15:01:34 +0200 | [diff] [blame] | 390 | } |
| 391 | /* END_CASE */ |
| 392 | |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 393 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | void nv_seed_file_create() |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 395 | { |
| 396 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 399 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 400 | TEST_ASSERT(write_nv_seed(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 401 | } |
| 402 | /* END_CASE */ |
| 403 | |
Paul Bakker | b598c29 | 2016-06-01 16:57:11 +0100 | [diff] [blame] | 404 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 405 | void entropy_nv_seed_std_io() |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 406 | { |
| 407 | unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 408 | unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 409 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | memset(io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 411 | memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 412 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 413 | mbedtls_platform_set_nv_seed(mbedtls_platform_std_nv_seed_read, |
| 414 | mbedtls_platform_std_nv_seed_write); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 415 | |
| 416 | /* Check if platform NV read and write manipulate the same data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 417 | TEST_ASSERT(write_nv_seed(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
| 418 | TEST_ASSERT(mbedtls_nv_seed_read(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == |
| 419 | MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 420 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 421 | TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 424 | |
| 425 | /* Check if platform NV write and raw read manipulate the same data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 426 | TEST_ASSERT(mbedtls_nv_seed_write(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == |
| 427 | MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 428 | TEST_ASSERT(read_nv_seed(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 430 | TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 431 | } |
| 432 | /* END_CASE */ |
| 433 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 434 | /* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 435 | void entropy_nv_seed(data_t *read_seed) |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 436 | { |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 437 | #if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR) |
| 438 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 439 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA512); |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 440 | #elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR) |
| 441 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 443 | #else |
| 444 | #error "Unsupported entropy accumulator" |
| 445 | #endif |
| 446 | mbedtls_md_context_t accumulator; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 447 | mbedtls_entropy_context ctx; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 448 | int (*original_mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) = |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 449 | mbedtls_nv_seed_read; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 450 | int (*original_mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) = |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 451 | mbedtls_nv_seed_write; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 452 | |
| 453 | unsigned char header[2]; |
| 454 | unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 455 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 456 | unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 457 | unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 458 | unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 459 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | memset(entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 461 | memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 462 | memset(empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 463 | memset(check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 464 | memset(check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 465 | |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 466 | // Make sure we read/write NV seed from our buffers |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 467 | mbedtls_platform_set_nv_seed(buffer_nv_seed_read, buffer_nv_seed_write); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 468 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 469 | mbedtls_md_init(&accumulator); |
| 470 | mbedtls_entropy_init(&ctx); |
| 471 | entropy_clear_sources(&ctx); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 472 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | TEST_ASSERT(mbedtls_entropy_add_source(&ctx, mbedtls_nv_seed_poll, NULL, |
| 474 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 475 | MBEDTLS_ENTROPY_SOURCE_STRONG) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 476 | |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 477 | // Set the initial NV seed to read |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | TEST_ASSERT(read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE); |
| 479 | memcpy(buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Gilles Peskine | 66afcca | 2019-06-12 19:33:42 +0200 | [diff] [blame] | 480 | |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 481 | // Do an entropy run |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 482 | TEST_ASSERT(mbedtls_entropy_func(&ctx, entropy, sizeof(entropy)) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 483 | // Determine what should have happened with manual entropy internal logic |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 484 | |
| 485 | // Init accumulator |
| 486 | header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 487 | TEST_ASSERT(mbedtls_md_setup(&accumulator, md_info, 0) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 488 | |
| 489 | // First run for updating write_seed |
| 490 | header[0] = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 491 | TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0); |
| 492 | TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0); |
| 493 | TEST_ASSERT(mbedtls_md_update(&accumulator, |
| 494 | read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
| 495 | TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 496 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 497 | TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0); |
| 498 | TEST_ASSERT(mbedtls_md_update(&accumulator, |
| 499 | buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 500 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 501 | TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 502 | check_seed) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 503 | |
| 504 | // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed) |
| 505 | header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 506 | TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0); |
| 507 | TEST_ASSERT(mbedtls_md_update(&accumulator, |
| 508 | empty, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 509 | |
| 510 | header[0] = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 511 | TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0); |
| 512 | TEST_ASSERT(mbedtls_md_update(&accumulator, |
| 513 | check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
| 514 | TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 515 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 516 | TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 517 | check_entropy) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 518 | |
| 519 | // Check result of both NV file and entropy received with the manual calculations |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 520 | TEST_ASSERT(memcmp(check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
| 521 | TEST_ASSERT(memcmp(check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0); |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 522 | |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 523 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 524 | mbedtls_md_free(&accumulator); |
| 525 | mbedtls_entropy_free(&ctx); |
Gilles Peskine | e39b903 | 2019-06-12 19:31:29 +0200 | [diff] [blame] | 526 | mbedtls_nv_seed_read = original_mbedtls_nv_seed_read; |
| 527 | mbedtls_nv_seed_write = original_mbedtls_nv_seed_write; |
Paul Bakker | ffbfb4c | 2016-06-01 15:36:18 +0100 | [diff] [blame] | 528 | } |
| 529 | /* END_CASE */ |
| 530 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 531 | /* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 532 | void entropy_selftest(int result) |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 533 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 534 | TEST_ASSERT(mbedtls_entropy_self_test(1) == result); |
Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 535 | } |
| 536 | /* END_CASE */ |