components/
sha.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//! Components for collections of SHA.
6//!
7//! Usage
8//! -----
9//! ```rust
10//!    let sha = components::sha::ShaComponent::new(
11//!        board_kernel,
12//!        chip.sha,
13//!    )
14//!    .finalize(components::sha_component_static!(
15//!        lowrisc::sha::Sha,
16//!        32,
17//!    ));
18//! ```
19
20use capsules_extra::sha::ShaDriver;
21use core::mem::MaybeUninit;
22use kernel::capabilities;
23use kernel::component::Component;
24use kernel::create_capability;
25use kernel::hil::digest;
26
27// Setup static space for the objects.
28#[macro_export]
29macro_rules! sha_component_static {
30    ($A:ty, $L:expr$(,)?) => {{
31        let sha_driver = kernel::static_buf!(
32            capsules_extra::sha::ShaDriver<
33                'static,
34                capsules_core::virtualizers::virtual_sha::VirtualMuxSha<'static, $A, $L>,
35                $L,
36            >
37        );
38
39        let data_buffer = kernel::static_buf!([u8; 64]);
40        let dest_buffer = kernel::static_buf!([u8; $L]);
41
42        (sha_driver, data_buffer, dest_buffer)
43    };};
44}
45
46pub struct ShaComponent<A: 'static + digest::Digest<'static, L>, const L: usize> {
47    board_kernel: &'static kernel::Kernel,
48    driver_num: usize,
49    sha: &'static A,
50}
51
52impl<A: 'static + digest::Digest<'static, L>, const L: usize> ShaComponent<A, L> {
53    pub fn new(
54        board_kernel: &'static kernel::Kernel,
55        driver_num: usize,
56        sha: &'static A,
57    ) -> ShaComponent<A, L> {
58        ShaComponent {
59            board_kernel,
60            driver_num,
61            sha,
62        }
63    }
64}
65
66impl<
67        A: kernel::hil::digest::Sha256
68            + digest::Sha384
69            + digest::Sha512
70            + 'static
71            + digest::Digest<'static, L>,
72        const L: usize,
73    > Component for ShaComponent<A, L>
74{
75    type StaticInput = (
76        &'static mut MaybeUninit<ShaDriver<'static, A, L>>,
77        &'static mut MaybeUninit<[u8; 64]>,
78        &'static mut MaybeUninit<[u8; L]>,
79    );
80
81    type Output = &'static ShaDriver<'static, A, L>;
82
83    fn finalize(self, s: Self::StaticInput) -> Self::Output {
84        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
85
86        let data_buffer = s.1.write([0; 64]);
87        let dest_buffer = s.2.write([0; L]);
88
89        let sha = s.0.write(capsules_extra::sha::ShaDriver::new(
90            self.sha,
91            data_buffer,
92            dest_buffer,
93            self.board_kernel.create_grant(self.driver_num, &grant_cap),
94        ));
95
96        self.sha.set_client(sha);
97
98        sha
99    }
100}
101
102#[macro_export]
103macro_rules! sha_software_256_component_static {
104    ($(,)?) => {{
105        kernel::static_buf!(capsules_extra::sha256::Sha256Software<'static>)
106    };};
107}
108
109pub struct ShaSoftware256Component {}
110
111impl ShaSoftware256Component {
112    pub fn new() -> ShaSoftware256Component {
113        ShaSoftware256Component {}
114    }
115}
116
117impl Component for ShaSoftware256Component {
118    type StaticInput = &'static mut MaybeUninit<capsules_extra::sha256::Sha256Software<'static>>;
119
120    type Output = &'static capsules_extra::sha256::Sha256Software<'static>;
121
122    fn finalize(self, s: Self::StaticInput) -> Self::Output {
123        let sha_256_sw = s.write(capsules_extra::sha256::Sha256Software::new());
124
125        kernel::deferred_call::DeferredCallClient::register(sha_256_sw);
126
127        sha_256_sw
128    }
129}