components/
hmac.rs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Components for collections of HMACs.
//!
//! Usage
//! -----
//! ```rust
//!    let hmac = components::hmac::HmacComponent::new(
//!        board_kernel,
//!        chip.hmac,
//!    )
//!    .finalize(components::hmac_component_static!(
//!        lowrisc::hmac::Hmac,
//!        32
//!    ));
//! ```

use capsules_extra::hmac::HmacDriver;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil::digest;

#[macro_export]
macro_rules! hmac_component_static {
    ($A:ty, $L:expr $(,)?) => {{
        let hmac = kernel::static_buf!(capsules_extra::hmac::HmacDriver<'static, $A, $L>);

        let data_buffer = kernel::static_buf!([u8; 64]);
        let dest_buffer = kernel::static_buf!([u8; $L]);

        (hmac, data_buffer, dest_buffer)
    };};
}

pub type HmacComponentType<H, const L: usize> = capsules_extra::hmac::HmacDriver<'static, H, L>;

pub struct HmacComponent<A: 'static + digest::Digest<'static, L>, const L: usize> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    hmac: &'static A,
}

impl<A: 'static + digest::Digest<'static, L>, const L: usize> HmacComponent<A, L> {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        hmac: &'static A,
    ) -> HmacComponent<A, L> {
        HmacComponent {
            board_kernel,
            driver_num,
            hmac,
        }
    }
}

impl<
        A: kernel::hil::digest::HmacSha256
            + digest::HmacSha384
            + digest::HmacSha512
            + 'static
            + digest::Digest<'static, L>,
        const L: usize,
    > Component for HmacComponent<A, L>
{
    type StaticInput = (
        &'static mut MaybeUninit<HmacDriver<'static, A, L>>,
        &'static mut MaybeUninit<[u8; 64]>,
        &'static mut MaybeUninit<[u8; L]>,
    );
    type Output = &'static HmacDriver<'static, A, L>;

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

        let data_buffer = s.1.write([0; 64]);
        let dest_buffer = s.2.write([0; L]);

        let hmac = s.0.write(capsules_extra::hmac::HmacDriver::new(
            self.hmac,
            data_buffer,
            dest_buffer,
            self.board_kernel.create_grant(self.driver_num, &grant_cap),
        ));

        self.hmac.set_client(hmac);

        hmac
    }
}

#[macro_export]
macro_rules! hmac_sha256_software_component_static {
    ($S:ty $(,)?) => {{
        let hmac_sha256 =
            kernel::static_buf!(capsules_extra::hmac_sha256::HmacSha256Software<'static, $S>);

        let data_buffer = kernel::static_buf!([u8; 64]);
        let verify_buffer = kernel::static_buf!([u8; 32]);

        (hmac_sha256, data_buffer, verify_buffer)
    };};
}

pub type HmacSha256SoftwareComponentType<S> =
    capsules_extra::hmac_sha256::HmacSha256Software<'static, S>;

pub struct HmacSha256SoftwareComponent<
    S: digest::Sha256 + digest::DigestDataHash<'static, 32> + digest::Digest<'static, 32> + 'static,
> {
    sha_256: &'static S,
}

impl<S: digest::Sha256 + digest::DigestDataHash<'static, 32> + digest::Digest<'static, 32>>
    HmacSha256SoftwareComponent<S>
{
    pub fn new(sha_256: &'static S) -> HmacSha256SoftwareComponent<S> {
        HmacSha256SoftwareComponent { sha_256 }
    }
}

impl<
        S: digest::Sha256
            + digest::DigestDataHash<'static, 32>
            + digest::Digest<'static, 32>
            + 'static,
    > Component for HmacSha256SoftwareComponent<S>
{
    type StaticInput = (
        &'static mut MaybeUninit<capsules_extra::hmac_sha256::HmacSha256Software<'static, S>>,
        &'static mut MaybeUninit<[u8; 64]>,
        &'static mut MaybeUninit<[u8; 32]>,
    );
    type Output = &'static capsules_extra::hmac_sha256::HmacSha256Software<'static, S>;

    fn finalize(self, s: Self::StaticInput) -> Self::Output {
        let data_buffer = s.1.write([0; 64]);
        let verify_buffer = s.2.write([0; 32]);

        let hmac_sha256_sw =
            s.0.write(capsules_extra::hmac_sha256::HmacSha256Software::new(
                self.sha_256,
                data_buffer,
                verify_buffer,
            ));

        kernel::hil::digest::Digest::set_client(self.sha_256, hmac_sha256_sw);

        hmac_sha256_sw
    }
}