blob: 9b1df8fb1f3cd1e51eacfd9d6101579c889e5bad [file] [log] [blame]
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/entropy.h"
Paul Bakkerffbfb4c2016-06-01 15:36:18 +01003#include "mbedtls/entropy_poll.h"
Gilles Peskine72d40fc2020-04-14 21:28:42 +02004#include "mbedtls/md.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01005#include "string.h"
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +02006
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01007typedef enum {
Gilles Peskineed04a672019-10-08 14:37:27 +02008 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 Peskine1b6c09a2023-01-11 14:52:35 +010013typedef struct {
Gilles Peskineed04a672019-10-08 14:37:27 +020014 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é-Gonnardc7c56b22014-05-30 11:42:01 +020018
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 Peskine1b6c09a2023-01-11 14:52:35 +010025static int entropy_dummy_source(void *arg, unsigned char *output,
26 size_t len, size_t *olen)
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020027{
Gilles Peskineed04a672019-10-08 14:37:27 +020028 entropy_dummy_context *context = arg;
29 ++context->calls;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010031 switch (context->instruction) {
Gilles Peskineed04a672019-10-08 14:37:27 +020032 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 Peskine1b6c09a2023-01-11 14:52:35 +010039 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020040 }
41
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042 memset(output, 0x2a, *olen);
43 return 0;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +020044}
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010045
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 Peskine1b6c09a2023-01-11 14:52:35 +010057static void entropy_clear_sources(mbedtls_entropy_context *ctx)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010058{
59 ctx->source_count = 0;
60}
61
Gilles Peskine7f246512019-10-08 14:51:49 +020062#if defined(MBEDTLS_ENTROPY_NV_SEED)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010063/*
64 * NV seed read/write functions that use a buffer instead of a file
65 */
66static unsigned char buffer_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
67
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068int buffer_nv_seed_read(unsigned char *buf, size_t buf_len)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010069{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
71 return -1;
72 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 memcpy(buf, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE);
75 return 0;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010076}
77
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010078int buffer_nv_seed_write(unsigned char *buf, size_t buf_len)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010079{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
81 return -1;
82 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 memcpy(buffer_seed, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
85 return 0;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010086}
87
88/*
89 * NV seed read/write helpers that fill the base seedfile
90 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091static int write_nv_seed(unsigned char *buf, size_t buf_len)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010092{
93 FILE *f;
94
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
96 return -1;
97 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +010098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) {
100 return -1;
101 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) !=
104 MBEDTLS_ENTROPY_BLOCK_SIZE) {
Gilles Peskinefa276362023-10-17 18:08:24 +0200105 fclose(f);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 return -1;
107 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 fclose(f);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 return 0;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100112}
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114int read_nv_seed(unsigned char *buf, size_t buf_len)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100115{
116 FILE *f;
117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
119 return -1;
120 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) {
123 return -1;
124 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 if (fread(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) !=
127 MBEDTLS_ENTROPY_BLOCK_SIZE) {
Gilles Peskinefa276362023-10-17 18:08:24 +0200128 fclose(f);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 return -1;
130 }
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 fclose(f);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 return 0;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100135}
Paul Bakker4a6c6fc2016-06-01 16:34:25 +0100136#endif /* MBEDTLS_ENTROPY_NV_SEED */
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200137/* END_HEADER */
138
139/* BEGIN_DEPENDENCIES
Gilles Peskinebfda1a92023-04-28 23:41:38 +0200140 * depends_on:MBEDTLS_ENTROPY_C:!MBEDTLS_PSA_INJECT_ENTROPY
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200141 * END_DEPENDENCIES
142 */
143
Gilles Peskine3d979f72021-02-22 21:24:02 +0100144/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145void entropy_init_free(int reinit)
Gilles Peskine3d979f72021-02-22 21:24:02 +0100146{
147 mbedtls_entropy_context ctx;
148
149 /* Double free is not explicitly documented to work, but it is convenient
150 * to call mbedtls_entropy_free() unconditionally on an error path without
151 * checking whether it has already been called in the success path. */
152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 mbedtls_entropy_init(&ctx);
154 mbedtls_entropy_free(&ctx);
Gilles Peskine3d979f72021-02-22 21:24:02 +0100155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (reinit) {
157 mbedtls_entropy_init(&ctx);
158 }
159 mbedtls_entropy_free(&ctx);
Gilles Peskine3d979f72021-02-22 21:24:02 +0100160
161 /* This test case always succeeds, functionally speaking. A plausible
162 * bug might trigger an invalid pointer dereference or a memory leak. */
163 goto exit;
164}
165/* END_CASE */
166
Simon Butcherb7f45c52016-09-15 18:42:26 +0100167/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168void entropy_seed_file(char *path, int ret)
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200169{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) == ret);
175 TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path) == ret);
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200176
Paul Bakkerbd51b262014-07-10 15:26:12 +0200177exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 mbedtls_entropy_free(&ctx);
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200179}
180/* END_CASE */
181
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300182/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183void entropy_write_base_seed_file(int ret)
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300184{
185 mbedtls_entropy_context ctx;
186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 mbedtls_entropy_init(&ctx);
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300188
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
190 TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300191
192exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 mbedtls_entropy_free(&ctx);
Victor Krasnoshchokb3129ba2020-08-29 22:54:37 +0300194}
195/* END_CASE */
196
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200197/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198void entropy_no_sources()
Gilles Peskine7f246512019-10-08 14:51:49 +0200199{
200 mbedtls_entropy_context ctx;
201 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 mbedtls_entropy_init(&ctx);
204 entropy_clear_sources(&ctx);
205 TEST_EQUAL(mbedtls_entropy_func(&ctx, buf, sizeof(buf)),
206 MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED);
Gilles Peskine7f246512019-10-08 14:51:49 +0200207
208exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 mbedtls_entropy_free(&ctx);
Gilles Peskine7f246512019-10-08 14:51:49 +0200210}
211/* END_CASE */
212
213/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214void entropy_too_many_sources()
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200217 size_t i;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 entropy_dummy_context dummy = { DUMMY_REQUESTED_LENGTH, 0, 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200221
222 /*
223 * It's hard to tell precisely when the error will occur,
224 * since we don't know how many sources were automatically added.
225 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 for (i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++) {
227 (void) mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
228 16, MBEDTLS_ENTROPY_SOURCE_WEAK);
229 }
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
232 16, MBEDTLS_ENTROPY_SOURCE_WEAK)
233 == MBEDTLS_ERR_ENTROPY_MAX_SOURCES);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200234
Paul Bakkerbd51b262014-07-10 15:26:12 +0200235exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 mbedtls_entropy_free(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200237}
238/* END_CASE */
239
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100240/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241void entropy_func_len(int len, int ret)
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_entropy_context ctx;
244 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
245 unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200246 size_t i, j;
247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200249
250 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 * See comments in mbedtls_entropy_self_test()
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200252 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 for (i = 0; i < 8; i++) {
254 TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, len) == ret);
255 for (j = 0; j < sizeof(buf); j++) {
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200256 acc[j] |= buf[j];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 }
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200258 }
259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 if (ret == 0) {
261 for (j = 0; j < (size_t) len; j++) {
262 TEST_ASSERT(acc[j] != 0);
263 }
264 }
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 for (j = len; j < sizeof(buf); j++) {
267 TEST_ASSERT(acc[j] == 0);
268 }
Gilles Peskine7aba0362021-01-31 00:07:11 +0100269
270exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 mbedtls_entropy_free(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200272}
273/* END_CASE */
274
275/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276void entropy_source_fail(char *path)
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_entropy_context ctx;
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200279 unsigned char buf[16];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 entropy_dummy_context dummy = { DUMMY_FAIL, 0, 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 mbedtls_entropy_init(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200283
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
285 &dummy, 16,
286 MBEDTLS_ENTROPY_SOURCE_WEAK)
287 == 0);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, sizeof(buf))
290 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
291 TEST_ASSERT(mbedtls_entropy_gather(&ctx)
292 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
Simon Butcherb7f45c52016-09-15 18:42:26 +0100293#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path)
295 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
296 TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path)
297 == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200298#else
299 ((void) path);
300#endif
301
Paul Bakkerbd51b262014-07-10 15:26:12 +0200302exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 mbedtls_entropy_free(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200304}
305/* END_CASE */
306
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100307/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308void entropy_threshold(int threshold, int chunk_size, int result)
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_entropy_context ctx;
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100311 entropy_dummy_context strong =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 { DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0 };
313 entropy_dummy_context weak = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200315 int ret;
316
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 mbedtls_entropy_init(&ctx);
318 entropy_clear_sources(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200319
Gilles Peskinecbd91e02019-11-25 19:50:54 +0100320 /* Set strong source that reaches its threshold immediately and
321 * a weak source whose threshold is a test parameter. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
323 &strong, 1,
324 MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
325 TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
326 &weak, threshold,
327 MBEDTLS_ENTROPY_SOURCE_WEAK) == 0);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200330
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331 if (result >= 0) {
332 TEST_ASSERT(ret == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100333#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskineae679392019-11-25 18:26:23 +0100334 /* If the NV seed functionality is enabled, there are two entropy
335 * updates: before and after updating the NV seed. */
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100336 result *= 2;
337#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 TEST_ASSERT(weak.calls == (size_t) result);
339 } else {
340 TEST_ASSERT(ret == result);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200341 }
342
Paul Bakkerbd51b262014-07-10 15:26:12 +0200343exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 mbedtls_entropy_free(&ctx);
Manuel Pégourié-Gonnardc7c56b22014-05-30 11:42:01 +0200345}
346/* END_CASE */
347
Gilles Peskine65fc0682019-10-08 15:01:34 +0200348/* BEGIN_CASE */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349void entropy_calls(int strength1, int strength2,
350 int threshold, int chunk_size,
351 int result)
Gilles Peskine65fc0682019-10-08 15:01:34 +0200352{
353 /*
354 * if result >= 0: result = expected number of calls to source 1
355 * if result < 0: result = expected return code from mbedtls_entropy_func()
356 */
357
358 mbedtls_entropy_context ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 entropy_dummy_context dummy1 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
360 entropy_dummy_context dummy2 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
Gilles Peskine65fc0682019-10-08 15:01:34 +0200361 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
362 int ret;
363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 mbedtls_entropy_init(&ctx);
365 entropy_clear_sources(&ctx);
Gilles Peskine65fc0682019-10-08 15:01:34 +0200366
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100367 TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
368 &dummy1, threshold,
369 strength1) == 0);
370 TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
371 &dummy2, threshold,
372 strength2) == 0);
Gilles Peskine65fc0682019-10-08 15:01:34 +0200373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
Gilles Peskine65fc0682019-10-08 15:01:34 +0200375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 if (result >= 0) {
377 TEST_ASSERT(ret == 0);
Gilles Peskineae679392019-11-25 18:26:23 +0100378#if defined(MBEDTLS_ENTROPY_NV_SEED)
379 /* If the NV seed functionality is enabled, there are two entropy
380 * updates: before and after updating the NV seed. */
381 result *= 2;
382#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 TEST_ASSERT(dummy1.calls == (size_t) result);
384 } else {
385 TEST_ASSERT(ret == result);
Gilles Peskine65fc0682019-10-08 15:01:34 +0200386 }
387
388exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 mbedtls_entropy_free(&ctx);
Gilles Peskine65fc0682019-10-08 15:01:34 +0200390}
391/* END_CASE */
392
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100393/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394void nv_seed_file_create()
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100395{
396 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100399
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 TEST_ASSERT(write_nv_seed(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100401}
402/* END_CASE */
403
Paul Bakkerb598c292016-06-01 16:57:11 +0100404/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405void entropy_nv_seed_std_io()
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100406{
407 unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
408 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 memset(io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE);
411 memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 mbedtls_platform_set_nv_seed(mbedtls_platform_std_nv_seed_read,
414 mbedtls_platform_std_nv_seed_write);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100415
416 /* Check if platform NV read and write manipulate the same data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 TEST_ASSERT(write_nv_seed(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
418 TEST_ASSERT(mbedtls_nv_seed_read(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
419 MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100420
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100422
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100424
425 /* Check if platform NV write and raw read manipulate the same data */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 TEST_ASSERT(mbedtls_nv_seed_write(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
427 MBEDTLS_ENTROPY_BLOCK_SIZE);
428 TEST_ASSERT(read_nv_seed(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100429
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100431}
432/* END_CASE */
433
Gilles Peskine66afcca2019-06-12 19:33:42 +0200434/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435void entropy_nv_seed(data_t *read_seed)
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100436{
Gilles Peskine66afcca2019-06-12 19:33:42 +0200437#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
438 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
Gilles Peskine66afcca2019-06-12 19:33:42 +0200440#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
441 const mbedtls_md_info_t *md_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
Gilles Peskine66afcca2019-06-12 19:33:42 +0200443#else
444#error "Unsupported entropy accumulator"
445#endif
446 mbedtls_md_context_t accumulator;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100447 mbedtls_entropy_context ctx;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 int (*original_mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
Gilles Peskinee39b9032019-06-12 19:31:29 +0200449 mbedtls_nv_seed_read;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 int (*original_mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
Gilles Peskinee39b9032019-06-12 19:31:29 +0200451 mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100452
453 unsigned char header[2];
454 unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
455 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
456 unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100457 unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
458 unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 memset(entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
461 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
462 memset(empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
463 memset(check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE);
464 memset(check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100465
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100466 // Make sure we read/write NV seed from our buffers
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 mbedtls_platform_set_nv_seed(buffer_nv_seed_read, buffer_nv_seed_write);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 mbedtls_md_init(&accumulator);
470 mbedtls_entropy_init(&ctx);
471 entropy_clear_sources(&ctx);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 TEST_ASSERT(mbedtls_entropy_add_source(&ctx, mbedtls_nv_seed_poll, NULL,
474 MBEDTLS_ENTROPY_BLOCK_SIZE,
475 MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100476
Gilles Peskine66afcca2019-06-12 19:33:42 +0200477 // Set the initial NV seed to read
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 TEST_ASSERT(read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE);
479 memcpy(buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE);
Gilles Peskine66afcca2019-06-12 19:33:42 +0200480
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100481 // Do an entropy run
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 TEST_ASSERT(mbedtls_entropy_func(&ctx, entropy, sizeof(entropy)) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100483 // Determine what should have happened with manual entropy internal logic
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100484
485 // Init accumulator
486 header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 TEST_ASSERT(mbedtls_md_setup(&accumulator, md_info, 0) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100488
489 // First run for updating write_seed
490 header[0] = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
492 TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
493 TEST_ASSERT(mbedtls_md_update(&accumulator,
494 read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
495 TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
498 TEST_ASSERT(mbedtls_md_update(&accumulator,
499 buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
502 check_seed) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100503
504 // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
505 header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
507 TEST_ASSERT(mbedtls_md_update(&accumulator,
508 empty, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100509
510 header[0] = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
512 TEST_ASSERT(mbedtls_md_update(&accumulator,
513 check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
514 TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100515
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
517 check_entropy) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100518
519 // Check result of both NV file and entropy received with the manual calculations
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 TEST_ASSERT(memcmp(check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
521 TEST_ASSERT(memcmp(check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100522
Gilles Peskinee39b9032019-06-12 19:31:29 +0200523exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 mbedtls_md_free(&accumulator);
525 mbedtls_entropy_free(&ctx);
Gilles Peskinee39b9032019-06-12 19:31:29 +0200526 mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
527 mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
Paul Bakkerffbfb4c2016-06-01 15:36:18 +0100528}
529/* END_CASE */
530
Hanno Beckerd4a872e2017-09-07 08:09:33 +0100531/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532void entropy_selftest(int result)
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200533{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 TEST_ASSERT(mbedtls_entropy_self_test(1) == result);
Manuel Pégourié-Gonnard2c25eb02014-05-30 10:38:18 +0200535}
536/* END_CASE */