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