components/
ctap.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
// 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 CTAP HID over USB support.
//!
//! This provides a component for using the CTAP driver. This allows for
//! Client to Authenticator Protocol Authentication.
//!
//! Usage
//! -----
//! ```rust
//! static STRINGS: &'static [&str; 3] = &[
//!     "XYZ Corp.",     // Manufacturer
//!     "FIDO Key",      // Product
//!     "Serial No. 5",  // Serial number
//! ];
//!
//!     let (ctap, ctap_driver) = components::ctap::CtapComponent::new(
//!         &earlgrey::usbdev::USB,
//!         0x1337, // My important company
//!         0x0DEC, // My device name
//!         strings,
//!         board_kernel,
//!         ctap_send_buffer,
//!         ctap_recv_buffer,
//!     )
//!     .finalize(components::ctap_component_static!(lowrisc::usbdev::Usb));
//!
//!     ctap.enable();
//!     ctap.attach();
//! ```

use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil;

// Setup static space for the objects.
#[macro_export]
macro_rules! ctap_component_static {
    ($U:ty $(,)?) => {{
        let hid = kernel::static_buf!(capsules_extra::usb::ctap::CtapHid<'static, $U>);
        let driver = kernel::static_buf!(
            capsules_extra::usb_hid_driver::UsbHidDriver<
                'static,
                capsules_extra::usb::usb_hid_driver::UsbHidDriver<'static, $U>,
            >
        );
        let send_buffer = kernel::static_buf!([u8; 64]);
        let recv_buffer = kernel::static_buf!([u8; 64]);

        (hid, driver, send_buffer, recv_buffer)
    };};
}

pub struct CtapComponent<U: 'static + hil::usb::UsbController<'static>> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    usb: &'static U,
    vendor_id: u16,
    product_id: u16,
    strings: &'static [&'static str; 3],
}

impl<U: 'static + hil::usb::UsbController<'static>> CtapComponent<U> {
    pub fn new(
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
        usb: &'static U,
        vendor_id: u16,
        product_id: u16,
        strings: &'static [&'static str; 3],
    ) -> CtapComponent<U> {
        CtapComponent {
            board_kernel,
            driver_num,
            usb,
            vendor_id,
            product_id,
            strings,
        }
    }
}

impl<U: 'static + hil::usb::UsbController<'static>> Component for CtapComponent<U> {
    type StaticInput = (
        &'static mut MaybeUninit<capsules_extra::usb::ctap::CtapHid<'static, U>>,
        &'static mut MaybeUninit<
            capsules_extra::usb_hid_driver::UsbHidDriver<
                'static,
                capsules_extra::usb::ctap::CtapHid<'static, U>,
            >,
        >,
        &'static mut MaybeUninit<[u8; 64]>,
        &'static mut MaybeUninit<[u8; 64]>,
    );
    type Output = (
        &'static capsules_extra::usb::ctap::CtapHid<'static, U>,
        &'static capsules_extra::usb_hid_driver::UsbHidDriver<
            'static,
            capsules_extra::usb::ctap::CtapHid<'static, U>,
        >,
    );

    fn finalize(self, s: Self::StaticInput) -> Self::Output {
        let ctap = s.0.write(capsules_extra::usb::ctap::CtapHid::new(
            self.usb,
            self.vendor_id,
            self.product_id,
            self.strings,
        ));
        self.usb.set_client(ctap);

        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        let send_buffer = s.2.write([0; 64]);
        let recv_buffer = s.3.write([0; 64]);

        let ctap_driver = s.1.write(capsules_extra::usb_hid_driver::UsbHidDriver::new(
            ctap,
            send_buffer,
            recv_buffer,
            self.board_kernel.create_grant(self.driver_num, &grant_cap),
        ));

        ctap.set_client(ctap_driver);

        (ctap, ctap_driver)
    }
}