1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Component for random number generator using `Entropy32ToRandom`.
//!
//! This provides one Component, RngComponent, which implements a userspace
//! syscall interface to the RNG peripheral (TRNG).
//!
//! Usage
//! -----
//! ```rust
//! let rng = components::rng::RngComponent::new(board_kernel, &sam4l::trng::TRNG)
//!     .finalize(rng_component_static!());
//! ```

// Author: Hudson Ayers <hayers@cs.stanford.edu>
// Last modified: 07/12/2019

use capsules_core::rng;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil::entropy::Entropy32;
use kernel::hil::rng::Rng;

#[macro_export]
macro_rules! rng_component_static {
    ($E: ty $(,)?) => {{
        let etr = kernel::static_buf!(capsules_core::rng::Entropy32ToRandom<'static, $E>);
        let rng = kernel::static_buf!(
            capsules_core::rng::RngDriver<
                'static,
                capsules_core::rng::Entropy32ToRandom<'static, $E>,
            >
        );

        (etr, rng)
    };};
}

pub type RngComponentType<E> =
    rng::RngDriver<'static, capsules_core::rng::Entropy32ToRandom<'static, E>>;

pub struct RngComponent<E: Entropy32<'static> + 'static> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    trng: &'static E,
}

impl<E: Entropy32<'static>> RngComponent<E> {
    pub fn new(board_kernel: &'static kernel::Kernel, driver_num: usize, trng: &'static E) -> Self {
        Self {
            board_kernel: board_kernel,
            driver_num: driver_num,
            trng: trng,
        }
    }
}

impl<E: Entropy32<'static>> Component for RngComponent<E> {
    type StaticInput = (
        &'static mut MaybeUninit<capsules_core::rng::Entropy32ToRandom<'static, E>>,
        &'static mut MaybeUninit<
            capsules_core::rng::RngDriver<
                'static,
                capsules_core::rng::Entropy32ToRandom<'static, E>,
            >,
        >,
    );
    type Output =
        &'static rng::RngDriver<'static, capsules_core::rng::Entropy32ToRandom<'static, E>>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        let entropy_to_random = static_buffer
            .0
            .write(rng::Entropy32ToRandom::new(self.trng));
        let rng = static_buffer.1.write(rng::RngDriver::new(
            entropy_to_random,
            self.board_kernel.create_grant(self.driver_num, &grant_cap),
        ));
        self.trng.set_client(entropy_to_random);
        entropy_to_random.set_client(rng);

        rng
    }
}