Skip to main content

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