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