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