• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/entropy.h"
3#include "entropy_poll.h"
4#include "mbedtls/md.h"
5#include "string.h"
6
7typedef enum {
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
13typedef struct {
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;
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 */
25static int entropy_dummy_source(void *arg, unsigned char *output,
26                                size_t len, size_t *olen)
27{
28    entropy_dummy_context *context = arg;
29    ++context->calls;
30
31    switch (context->instruction) {
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:
39            return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
40    }
41
42    memset(output, 0x2a, *olen);
43    return 0;
44}
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 */
57static void entropy_clear_sources(mbedtls_entropy_context *ctx)
58{
59    ctx->source_count = 0;
60}
61
62#if defined(MBEDTLS_ENTROPY_NV_SEED)
63/*
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
68int buffer_nv_seed_read(unsigned char *buf, size_t buf_len)
69{
70    if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
71        return -1;
72    }
73
74    memcpy(buf, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE);
75    return 0;
76}
77
78int buffer_nv_seed_write(unsigned char *buf, size_t buf_len)
79{
80    if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
81        return -1;
82    }
83
84    memcpy(buffer_seed, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
85    return 0;
86}
87
88/*
89 * NV seed read/write helpers that fill the base seedfile
90 */
91static int write_nv_seed(unsigned char *buf, size_t buf_len)
92{
93    FILE *f;
94
95    if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
96        return -1;
97    }
98
99    if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "w")) == NULL) {
100        return -1;
101    }
102
103    if (fwrite(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) !=
104        MBEDTLS_ENTROPY_BLOCK_SIZE) {
105        return -1;
106    }
107
108    fclose(f);
109
110    return 0;
111}
112
113int read_nv_seed(unsigned char *buf, size_t buf_len)
114{
115    FILE *f;
116
117    if (buf_len != MBEDTLS_ENTROPY_BLOCK_SIZE) {
118        return -1;
119    }
120
121    if ((f = fopen(MBEDTLS_PLATFORM_STD_NV_SEED_FILE, "rb")) == NULL) {
122        return -1;
123    }
124
125    if (fread(buf, 1, MBEDTLS_ENTROPY_BLOCK_SIZE, f) !=
126        MBEDTLS_ENTROPY_BLOCK_SIZE) {
127        return -1;
128    }
129
130    fclose(f);
131
132    return 0;
133}
134#endif /* MBEDTLS_ENTROPY_NV_SEED */
135/* END_HEADER */
136
137/* BEGIN_DEPENDENCIES
138 * depends_on:MBEDTLS_ENTROPY_C
139 * END_DEPENDENCIES
140 */
141
142/* BEGIN_CASE */
143void entropy_init_free(int reinit)
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
151    mbedtls_entropy_init(&ctx);
152    mbedtls_entropy_free(&ctx);
153
154    if (reinit) {
155        mbedtls_entropy_init(&ctx);
156    }
157    mbedtls_entropy_free(&ctx);
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
165/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
166void entropy_seed_file(char *path, int ret)
167{
168    mbedtls_entropy_context ctx;
169
170    mbedtls_entropy_init(&ctx);
171
172    TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) == ret);
173    TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path) == ret);
174
175exit:
176    mbedtls_entropy_free(&ctx);
177}
178/* END_CASE */
179
180/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
181void entropy_write_base_seed_file(int ret)
182{
183    mbedtls_entropy_context ctx;
184
185    mbedtls_entropy_init(&ctx);
186
187    TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
188    TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
189
190exit:
191    mbedtls_entropy_free(&ctx);
192}
193/* END_CASE */
194
195/* BEGIN_CASE */
196void entropy_no_sources()
197{
198    mbedtls_entropy_context ctx;
199    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
200
201    mbedtls_entropy_init(&ctx);
202    entropy_clear_sources(&ctx);
203    TEST_EQUAL(mbedtls_entropy_func(&ctx, buf, sizeof(buf)),
204               MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED);
205
206exit:
207    mbedtls_entropy_free(&ctx);
208}
209/* END_CASE */
210
211/* BEGIN_CASE */
212void entropy_too_many_sources()
213{
214    mbedtls_entropy_context ctx;
215    size_t i;
216    entropy_dummy_context dummy = { DUMMY_REQUESTED_LENGTH, 0, 0 };
217
218    mbedtls_entropy_init(&ctx);
219
220    /*
221     * It's hard to tell precisely when the error will occur,
222     * since we don't know how many sources were automatically added.
223     */
224    for (i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++) {
225        (void) mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
226                                          16, MBEDTLS_ENTROPY_SOURCE_WEAK);
227    }
228
229    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
230                                           16, MBEDTLS_ENTROPY_SOURCE_WEAK)
231                == MBEDTLS_ERR_ENTROPY_MAX_SOURCES);
232
233exit:
234    mbedtls_entropy_free(&ctx);
235}
236/* END_CASE */
237
238/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
239void entropy_func_len(int len, int ret)
240{
241    mbedtls_entropy_context ctx;
242    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
243    unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
244    size_t i, j;
245
246    mbedtls_entropy_init(&ctx);
247
248    /*
249     * See comments in mbedtls_entropy_self_test()
250     */
251    for (i = 0; i < 8; i++) {
252        TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, len) == ret);
253        for (j = 0; j < sizeof(buf); j++) {
254            acc[j] |= buf[j];
255        }
256    }
257
258    if (ret == 0) {
259        for (j = 0; j < (size_t) len; j++) {
260            TEST_ASSERT(acc[j] != 0);
261        }
262    }
263
264    for (j = len; j < sizeof(buf); j++) {
265        TEST_ASSERT(acc[j] == 0);
266    }
267
268exit:
269    mbedtls_entropy_free(&ctx);
270}
271/* END_CASE */
272
273/* BEGIN_CASE */
274void entropy_source_fail(char *path)
275{
276    mbedtls_entropy_context ctx;
277    unsigned char buf[16];
278    entropy_dummy_context dummy = { DUMMY_FAIL, 0, 0 };
279
280    mbedtls_entropy_init(&ctx);
281
282    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
283                                           &dummy, 16,
284                                           MBEDTLS_ENTROPY_SOURCE_WEAK)
285                == 0);
286
287    TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, sizeof(buf))
288                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
289    TEST_ASSERT(mbedtls_entropy_gather(&ctx)
290                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
291#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
292    TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path)
293                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
294    TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path)
295                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
296#else
297    ((void) path);
298#endif
299
300exit:
301    mbedtls_entropy_free(&ctx);
302}
303/* END_CASE */
304
305/* BEGIN_CASE */
306void entropy_threshold(int threshold, int chunk_size, int result)
307{
308    mbedtls_entropy_context ctx;
309    entropy_dummy_context strong =
310    { DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0 };
311    entropy_dummy_context weak = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
312    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
313    int ret;
314
315    mbedtls_entropy_init(&ctx);
316    entropy_clear_sources(&ctx);
317
318    /* Set strong source that reaches its threshold immediately and
319     * a weak source whose threshold is a test parameter. */
320    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
321                                           &strong, 1,
322                                           MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
323    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
324                                           &weak, threshold,
325                                           MBEDTLS_ENTROPY_SOURCE_WEAK) == 0);
326
327    ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
328
329    if (result >= 0) {
330        TEST_ASSERT(ret == 0);
331#if defined(MBEDTLS_ENTROPY_NV_SEED)
332        /* If the NV seed functionality is enabled, there are two entropy
333         * updates: before and after updating the NV seed. */
334        result *= 2;
335#endif
336        TEST_ASSERT(weak.calls == (size_t) result);
337    } else {
338        TEST_ASSERT(ret == result);
339    }
340
341exit:
342    mbedtls_entropy_free(&ctx);
343}
344/* END_CASE */
345
346/* BEGIN_CASE */
347void entropy_calls(int strength1, int strength2,
348                   int threshold, int chunk_size,
349                   int result)
350{
351    /*
352     * if result >= 0: result = expected number of calls to source 1
353     * if result < 0: result = expected return code from mbedtls_entropy_func()
354     */
355
356    mbedtls_entropy_context ctx;
357    entropy_dummy_context dummy1 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
358    entropy_dummy_context dummy2 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
359    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
360    int ret;
361
362    mbedtls_entropy_init(&ctx);
363    entropy_clear_sources(&ctx);
364
365    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
366                                           &dummy1, threshold,
367                                           strength1) == 0);
368    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
369                                           &dummy2, threshold,
370                                           strength2) == 0);
371
372    ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
373
374    if (result >= 0) {
375        TEST_ASSERT(ret == 0);
376#if defined(MBEDTLS_ENTROPY_NV_SEED)
377        /* If the NV seed functionality is enabled, there are two entropy
378         * updates: before and after updating the NV seed. */
379        result *= 2;
380#endif
381        TEST_ASSERT(dummy1.calls == (size_t) result);
382    } else {
383        TEST_ASSERT(ret == result);
384    }
385
386exit:
387    mbedtls_entropy_free(&ctx);
388}
389/* END_CASE */
390
391/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
392void nv_seed_file_create()
393{
394    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
395
396    memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
397
398    TEST_ASSERT(write_nv_seed(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
399}
400/* END_CASE */
401
402/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
403void entropy_nv_seed_std_io()
404{
405    unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
406    unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
407
408    memset(io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE);
409    memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
410
411    mbedtls_platform_set_nv_seed(mbedtls_platform_std_nv_seed_read,
412                                 mbedtls_platform_std_nv_seed_write);
413
414    /* Check if platform NV read and write manipulate the same data */
415    TEST_ASSERT(write_nv_seed(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
416    TEST_ASSERT(mbedtls_nv_seed_read(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
417                MBEDTLS_ENTROPY_BLOCK_SIZE);
418
419    TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
420
421    memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
422
423    /* Check if platform NV write and raw read manipulate the same data */
424    TEST_ASSERT(mbedtls_nv_seed_write(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
425                MBEDTLS_ENTROPY_BLOCK_SIZE);
426    TEST_ASSERT(read_nv_seed(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
427
428    TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
429}
430/* END_CASE */
431
432/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
433void entropy_nv_seed(data_t *read_seed)
434{
435#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
436    const mbedtls_md_info_t *md_info =
437        mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
438#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
439    const mbedtls_md_info_t *md_info =
440        mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
441#else
442#error "Unsupported entropy accumulator"
443#endif
444    mbedtls_md_context_t accumulator;
445    mbedtls_entropy_context ctx;
446    int (*original_mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
447        mbedtls_nv_seed_read;
448    int (*original_mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
449        mbedtls_nv_seed_write;
450
451    unsigned char header[2];
452    unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
453    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
454    unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
455    unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
456    unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
457
458    memset(entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
459    memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
460    memset(empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
461    memset(check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE);
462    memset(check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE);
463
464    // Make sure we read/write NV seed from our buffers
465    mbedtls_platform_set_nv_seed(buffer_nv_seed_read, buffer_nv_seed_write);
466
467    mbedtls_md_init(&accumulator);
468    mbedtls_entropy_init(&ctx);
469    entropy_clear_sources(&ctx);
470
471    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, mbedtls_nv_seed_poll, NULL,
472                                           MBEDTLS_ENTROPY_BLOCK_SIZE,
473                                           MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
474
475    // Set the initial NV seed to read
476    TEST_ASSERT(read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE);
477    memcpy(buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE);
478
479    // Do an entropy run
480    TEST_ASSERT(mbedtls_entropy_func(&ctx, entropy, sizeof(entropy)) == 0);
481    // Determine what should have happened with manual entropy internal logic
482
483    // Init accumulator
484    header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
485    TEST_ASSERT(mbedtls_md_setup(&accumulator, md_info, 0) == 0);
486
487    // First run for updating write_seed
488    header[0] = 0;
489    TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
490    TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
491    TEST_ASSERT(mbedtls_md_update(&accumulator,
492                                  read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
493    TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
494
495    TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
496    TEST_ASSERT(mbedtls_md_update(&accumulator,
497                                  buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
498
499    TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
500                           check_seed) == 0);
501
502    // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
503    header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
504    TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
505    TEST_ASSERT(mbedtls_md_update(&accumulator,
506                                  empty, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
507
508    header[0] = 0;
509    TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
510    TEST_ASSERT(mbedtls_md_update(&accumulator,
511                                  check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
512    TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
513
514    TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
515                           check_entropy) == 0);
516
517    // Check result of both NV file and entropy received with the manual calculations
518    TEST_ASSERT(memcmp(check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
519    TEST_ASSERT(memcmp(check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
520
521exit:
522    mbedtls_md_free(&accumulator);
523    mbedtls_entropy_free(&ctx);
524    mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
525    mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
526}
527/* END_CASE */
528
529/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
530void entropy_selftest(int result)
531{
532    TEST_ASSERT(mbedtls_entropy_self_test(1) == result);
533}
534/* END_CASE */
535