imix/test/
rng_test.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! This tests an underlying 32-bit entropy generator and the library
6//! transformations between 8-bit and 32-bit entropy. To run this test,
7//! add this line to the imix boot sequence:
8//! ```
9//!     test::rng_test::run_entropy32();
10//! ```
11//! This test takes a 32-bit entropy generator, puts its output into a
12//! 32-8 conversion to be an 8-bit generator, puts that output into an
13//! 8-to-32 conversion to be a 32-bit generator again, and makes this final
14//! 32-bit entropy source be the tested RNG. This therefore tests not only
15//! the underlying entropy source but also the conversion library.
16//!
17//! The expected output is a series of random numbers that should be
18//! different on each invocation. Rigorous entropy tests are outside
19//! the scope of this test.
20
21use capsules_core::rng;
22use capsules_core::test::rng::TestRng;
23use kernel::hil::entropy::{Entropy32, Entropy8};
24use kernel::hil::rng::Rng;
25use kernel::static_init;
26use sam4l::trng::Trng;
27
28pub unsafe fn run_entropy32(trng: &'static Trng) {
29    let t = static_init_test_entropy32(trng);
30    t.run();
31}
32
33unsafe fn static_init_test_entropy32(trng: &'static Trng) -> &'static TestRng<'static> {
34    let e1 = static_init!(
35        rng::Entropy32To8<'static, Trng>,
36        rng::Entropy32To8::new(trng)
37    );
38    trng.set_client(e1);
39    let e2 = static_init!(
40        rng::Entropy8To32<'static, rng::Entropy32To8<'static, Trng>>,
41        rng::Entropy8To32::new(e1)
42    );
43    e1.set_client(e2);
44    let er = static_init!(
45        rng::Entropy32ToRandom<
46            'static,
47            rng::Entropy8To32<'static, rng::Entropy32To8<'static, Trng>>,
48        >,
49        rng::Entropy32ToRandom::new(e2)
50    );
51    e2.set_client(er);
52    let test = static_init!(TestRng<'static>, TestRng::new(er));
53    er.set_client(test);
54    test
55}