xref: /aosp_15_r20/external/mbedtls/tests/suites/test_suite_entropy.function (revision 62c56f9862f102b96d72393aff6076c951fb8148)
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:!MBEDTLS_PSA_INJECT_ENTROPY
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    mbedtls_entropy_init(&ctx);
170
171    MD_PSA_INIT();
172
173    TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path) == ret);
174    TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path) == ret);
175
176exit:
177    mbedtls_entropy_free(&ctx);
178    MD_PSA_DONE();
179}
180/* END_CASE */
181
182/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
183void entropy_write_base_seed_file(int ret)
184{
185    mbedtls_entropy_context ctx;
186    mbedtls_entropy_init(&ctx);
187
188    MD_PSA_INIT();
189
190    TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
191    TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, MBEDTLS_PLATFORM_STD_NV_SEED_FILE) == ret);
192
193exit:
194    mbedtls_entropy_free(&ctx);
195    MD_PSA_DONE();
196}
197/* END_CASE */
198
199/* BEGIN_CASE */
200void entropy_no_sources()
201{
202    mbedtls_entropy_context ctx;
203    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
204
205    mbedtls_entropy_init(&ctx);
206    entropy_clear_sources(&ctx);
207    TEST_EQUAL(mbedtls_entropy_func(&ctx, buf, sizeof(buf)),
208               MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED);
209
210exit:
211    mbedtls_entropy_free(&ctx);
212}
213/* END_CASE */
214
215/* BEGIN_CASE */
216void entropy_too_many_sources()
217{
218    mbedtls_entropy_context ctx;
219    size_t i;
220    entropy_dummy_context dummy = { DUMMY_REQUESTED_LENGTH, 0, 0 };
221
222    mbedtls_entropy_init(&ctx);
223
224    /*
225     * It's hard to tell precisely when the error will occur,
226     * since we don't know how many sources were automatically added.
227     */
228    for (i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++) {
229        (void) mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
230                                          16, MBEDTLS_ENTROPY_SOURCE_WEAK);
231    }
232
233    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source, &dummy,
234                                           16, MBEDTLS_ENTROPY_SOURCE_WEAK)
235                == MBEDTLS_ERR_ENTROPY_MAX_SOURCES);
236
237exit:
238    mbedtls_entropy_free(&ctx);
239}
240/* END_CASE */
241
242/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG */
243void entropy_func_len(int len, int ret)
244{
245    mbedtls_entropy_context ctx;
246    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
247    unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 };
248    size_t i, j;
249
250    mbedtls_entropy_init(&ctx);
251
252    MD_PSA_INIT();
253
254    /*
255     * See comments in mbedtls_entropy_self_test()
256     */
257    for (i = 0; i < 8; i++) {
258        TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, len) == ret);
259        for (j = 0; j < sizeof(buf); j++) {
260            acc[j] |= buf[j];
261        }
262    }
263
264    if (ret == 0) {
265        for (j = 0; j < (size_t) len; j++) {
266            TEST_ASSERT(acc[j] != 0);
267        }
268    }
269
270    for (j = len; j < sizeof(buf); j++) {
271        TEST_ASSERT(acc[j] == 0);
272    }
273
274exit:
275    mbedtls_entropy_free(&ctx);
276    MD_PSA_DONE();
277}
278/* END_CASE */
279
280/* BEGIN_CASE */
281void entropy_source_fail(char *path)
282{
283    mbedtls_entropy_context ctx;
284    unsigned char buf[16];
285    entropy_dummy_context dummy = { DUMMY_FAIL, 0, 0 };
286
287    mbedtls_entropy_init(&ctx);
288
289    MD_PSA_INIT();
290
291    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
292                                           &dummy, 16,
293                                           MBEDTLS_ENTROPY_SOURCE_WEAK)
294                == 0);
295
296    TEST_ASSERT(mbedtls_entropy_func(&ctx, buf, sizeof(buf))
297                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
298    TEST_ASSERT(mbedtls_entropy_gather(&ctx)
299                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
300#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_NV_SEED)
301    TEST_ASSERT(mbedtls_entropy_write_seed_file(&ctx, path)
302                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
303    TEST_ASSERT(mbedtls_entropy_update_seed_file(&ctx, path)
304                == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
305#else
306    ((void) path);
307#endif
308
309exit:
310    mbedtls_entropy_free(&ctx);
311    MD_PSA_DONE();
312}
313/* END_CASE */
314
315/* BEGIN_CASE */
316void entropy_threshold(int threshold, int chunk_size, int result)
317{
318    mbedtls_entropy_context ctx;
319    entropy_dummy_context strong =
320    { DUMMY_CONSTANT_LENGTH, MBEDTLS_ENTROPY_BLOCK_SIZE, 0 };
321    entropy_dummy_context weak = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
322    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
323    int ret;
324
325    mbedtls_entropy_init(&ctx);
326    entropy_clear_sources(&ctx);
327
328    MD_PSA_INIT();
329
330    /* Set strong source that reaches its threshold immediately and
331     * a weak source whose threshold is a test parameter. */
332    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
333                                           &strong, 1,
334                                           MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
335    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
336                                           &weak, threshold,
337                                           MBEDTLS_ENTROPY_SOURCE_WEAK) == 0);
338
339    ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
340
341    if (result >= 0) {
342        TEST_ASSERT(ret == 0);
343#if defined(MBEDTLS_ENTROPY_NV_SEED)
344        /* If the NV seed functionality is enabled, there are two entropy
345         * updates: before and after updating the NV seed. */
346        result *= 2;
347#endif
348        TEST_ASSERT(weak.calls == (size_t) result);
349    } else {
350        TEST_ASSERT(ret == result);
351    }
352
353exit:
354    mbedtls_entropy_free(&ctx);
355    MD_PSA_DONE();
356}
357/* END_CASE */
358
359/* BEGIN_CASE */
360void entropy_calls(int strength1, int strength2,
361                   int threshold, int chunk_size,
362                   int result)
363{
364    /*
365     * if result >= 0: result = expected number of calls to source 1
366     * if result < 0: result = expected return code from mbedtls_entropy_func()
367     */
368
369    mbedtls_entropy_context ctx;
370    entropy_dummy_context dummy1 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
371    entropy_dummy_context dummy2 = { DUMMY_CONSTANT_LENGTH, chunk_size, 0 };
372    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 };
373    int ret;
374
375    mbedtls_entropy_init(&ctx);
376    entropy_clear_sources(&ctx);
377
378    MD_PSA_INIT();
379
380    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
381                                           &dummy1, threshold,
382                                           strength1) == 0);
383    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, entropy_dummy_source,
384                                           &dummy2, threshold,
385                                           strength2) == 0);
386
387    ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf));
388
389    if (result >= 0) {
390        TEST_ASSERT(ret == 0);
391#if defined(MBEDTLS_ENTROPY_NV_SEED)
392        /* If the NV seed functionality is enabled, there are two entropy
393         * updates: before and after updating the NV seed. */
394        result *= 2;
395#endif
396        TEST_ASSERT(dummy1.calls == (size_t) result);
397    } else {
398        TEST_ASSERT(ret == result);
399    }
400
401exit:
402    mbedtls_entropy_free(&ctx);
403    MD_PSA_DONE();
404}
405/* END_CASE */
406
407/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */
408void nv_seed_file_create()
409{
410    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
411
412    memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
413
414    TEST_ASSERT(write_nv_seed(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
415}
416/* END_CASE */
417
418/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO:MBEDTLS_PLATFORM_NV_SEED_ALT */
419void entropy_nv_seed_std_io()
420{
421    unsigned char io_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
422    unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
423
424    memset(io_seed, 1, MBEDTLS_ENTROPY_BLOCK_SIZE);
425    memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
426
427    mbedtls_platform_set_nv_seed(mbedtls_platform_std_nv_seed_read,
428                                 mbedtls_platform_std_nv_seed_write);
429
430    /* Check if platform NV read and write manipulate the same data */
431    TEST_ASSERT(write_nv_seed(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
432    TEST_ASSERT(mbedtls_nv_seed_read(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
433                MBEDTLS_ENTROPY_BLOCK_SIZE);
434
435    TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
436
437    memset(check_seed, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
438
439    /* Check if platform NV write and raw read manipulate the same data */
440    TEST_ASSERT(mbedtls_nv_seed_write(io_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) ==
441                MBEDTLS_ENTROPY_BLOCK_SIZE);
442    TEST_ASSERT(read_nv_seed(check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
443
444    TEST_ASSERT(memcmp(io_seed, check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
445}
446/* END_CASE */
447
448/* BEGIN_CASE depends_on:MBEDTLS_MD_C:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_PLATFORM_NV_SEED_ALT */
449void entropy_nv_seed(data_t *read_seed)
450{
451#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
452    const mbedtls_md_info_t *md_info =
453        mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
454#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
455    const mbedtls_md_info_t *md_info =
456        mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
457#else
458#error "Unsupported entropy accumulator"
459#endif
460    mbedtls_md_context_t accumulator;
461    mbedtls_entropy_context ctx;
462    int (*original_mbedtls_nv_seed_read)(unsigned char *buf, size_t buf_len) =
463        mbedtls_nv_seed_read;
464    int (*original_mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
465        mbedtls_nv_seed_write;
466
467    unsigned char header[2];
468    unsigned char entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
469    unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
470    unsigned char empty[MBEDTLS_ENTROPY_BLOCK_SIZE];
471    unsigned char check_seed[MBEDTLS_ENTROPY_BLOCK_SIZE];
472    unsigned char check_entropy[MBEDTLS_ENTROPY_BLOCK_SIZE];
473
474    memset(entropy, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
475    memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
476    memset(empty, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
477    memset(check_seed, 2, MBEDTLS_ENTROPY_BLOCK_SIZE);
478    memset(check_entropy, 3, MBEDTLS_ENTROPY_BLOCK_SIZE);
479
480    // Make sure we read/write NV seed from our buffers
481    mbedtls_platform_set_nv_seed(buffer_nv_seed_read, buffer_nv_seed_write);
482
483    mbedtls_md_init(&accumulator);
484    mbedtls_entropy_init(&ctx);
485    entropy_clear_sources(&ctx);
486
487    MD_PSA_INIT();
488
489    TEST_ASSERT(mbedtls_entropy_add_source(&ctx, mbedtls_nv_seed_poll, NULL,
490                                           MBEDTLS_ENTROPY_BLOCK_SIZE,
491                                           MBEDTLS_ENTROPY_SOURCE_STRONG) == 0);
492
493    // Set the initial NV seed to read
494    TEST_ASSERT(read_seed->len >= MBEDTLS_ENTROPY_BLOCK_SIZE);
495    memcpy(buffer_seed, read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE);
496
497    // Do an entropy run
498    TEST_ASSERT(mbedtls_entropy_func(&ctx, entropy, sizeof(entropy)) == 0);
499    // Determine what should have happened with manual entropy internal logic
500
501    // Init accumulator
502    header[1] = MBEDTLS_ENTROPY_BLOCK_SIZE;
503    TEST_ASSERT(mbedtls_md_setup(&accumulator, md_info, 0) == 0);
504
505    // First run for updating write_seed
506    header[0] = 0;
507    TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
508    TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
509    TEST_ASSERT(mbedtls_md_update(&accumulator,
510                                  read_seed->x, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
511    TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
512
513    TEST_ASSERT(mbedtls_md_starts(&accumulator) == 0);
514    TEST_ASSERT(mbedtls_md_update(&accumulator,
515                                  buf, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
516
517    TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
518                           check_seed) == 0);
519
520    // Second run for actual entropy (triggers mbedtls_entropy_update_nv_seed)
521    header[0] = MBEDTLS_ENTROPY_SOURCE_MANUAL;
522    TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
523    TEST_ASSERT(mbedtls_md_update(&accumulator,
524                                  empty, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
525
526    header[0] = 0;
527    TEST_ASSERT(mbedtls_md_update(&accumulator, header, 2) == 0);
528    TEST_ASSERT(mbedtls_md_update(&accumulator,
529                                  check_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
530    TEST_ASSERT(mbedtls_md_finish(&accumulator, buf) == 0);
531
532    TEST_ASSERT(mbedtls_md(md_info, buf, MBEDTLS_ENTROPY_BLOCK_SIZE,
533                           check_entropy) == 0);
534
535    // Check result of both NV file and entropy received with the manual calculations
536    TEST_ASSERT(memcmp(check_seed, buffer_seed, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
537    TEST_ASSERT(memcmp(check_entropy, entropy, MBEDTLS_ENTROPY_BLOCK_SIZE) == 0);
538
539exit:
540    mbedtls_md_free(&accumulator);
541    mbedtls_entropy_free(&ctx);
542    mbedtls_nv_seed_read = original_mbedtls_nv_seed_read;
543    mbedtls_nv_seed_write = original_mbedtls_nv_seed_write;
544    MD_PSA_DONE();
545}
546/* END_CASE */
547
548/* BEGIN_CASE depends_on:ENTROPY_HAVE_STRONG:MBEDTLS_SELF_TEST */
549void entropy_selftest(int result)
550{
551    MD_PSA_INIT();
552
553    TEST_ASSERT(mbedtls_entropy_self_test(1) == result);
554
555exit:
556    MD_PSA_DONE();
557}
558/* END_CASE */
559