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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#![cfg_attr(not(test), no_std)]
#[macro_use]
extern crate delog;
generate_macros!();
use core::time::Duration;
use trussed::{client, syscall, types::Message, Client as TrussedClient};
use ctap_types::heapless_bytes::Bytes;
pub use ctap_types::Error;
mod ctap1;
mod ctap2;
#[cfg(feature = "dispatch")]
mod dispatch;
pub mod constants;
pub mod credential;
pub mod state;
pub type Result<T> = core::result::Result<T, Error>;
pub trait TrussedRequirements:
client::Client
+ client::P256
+ client::Chacha8Poly1305
+ client::Aes256Cbc
+ client::Sha256
+ client::HmacSha256
+ client::Ed255 {
}
impl<T> TrussedRequirements for T where
T: client::Client
+ client::P256
+ client::Chacha8Poly1305
+ client::Aes256Cbc
+ client::Sha256
+ client::HmacSha256
+ client::Ed255 {
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Config {
pub max_msg_size: usize,
pub skip_up_timeout: Option<Duration>,
}
pub struct Authenticator<UP, T>
where
UP: UserPresence,
{
trussed: T,
state: state::State,
up: UP,
config: Config,
}
fn format_hex(data: &[u8], mut buffer: &mut [u8]) {
const HEX_CHARS: &[u8] = b"0123456789abcdef";
for byte in data.iter() {
buffer[0] = HEX_CHARS[(byte >> 4) as usize];
buffer[1] = HEX_CHARS[(byte & 0xf) as usize];
buffer = &mut buffer[2..];
}
}
#[inline]
#[allow(dead_code)]
pub(crate) fn msp() -> u32 {
0x2000_0000
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(i32)]
#[non_exhaustive]
pub enum SigningAlgorithm {
Ed25519 = -8,
P256 = -7,
}
impl core::convert::TryFrom<i32> for SigningAlgorithm {
type Error = Error;
fn try_from(alg: i32) -> Result<Self> {
Ok(match alg {
-7 => SigningAlgorithm::P256,
-8 => SigningAlgorithm::Ed25519,
_ => return Err(Error::UnsupportedAlgorithm),
})
}
}
pub trait UserPresence: Copy {
fn user_present<T: TrussedClient>(
self,
trussed: &mut T,
timeout_milliseconds: u32,
) -> Result<()>;
}
#[deprecated(note = "use `Silent` directly`")]
#[doc(hidden)]
pub type SilentAuthenticator = Silent;
#[derive(Copy, Clone)]
pub struct Silent {}
impl UserPresence for Silent {
fn user_present<T: TrussedClient>(self, _: &mut T, _: u32) -> Result<()> {
Ok(())
}
}
#[deprecated(note = "use `Conforming` directly")]
#[doc(hidden)]
pub type NonSilentAuthenticator = Conforming;
#[derive(Copy, Clone)]
pub struct Conforming {}
impl UserPresence for Conforming {
fn user_present<T: TrussedClient>(
self,
trussed: &mut T,
timeout_milliseconds: u32,
) -> Result<()> {
let result = syscall!(trussed.confirm_user_present(timeout_milliseconds)).result;
result.map_err(|err| match err {
trussed::types::consent::Error::TimedOut => Error::UserActionTimeout,
_ => Error::OperationDenied,
})
}
}
fn cbor_serialize_message<T: serde::Serialize>(
object: &T,
) -> core::result::Result<Message, ctap_types::serde::Error> {
trussed::cbor_serialize_bytes(object)
}
impl<UP, T> Authenticator<UP, T>
where
UP: UserPresence,
T: TrussedRequirements,
{
pub fn new(trussed: T, up: UP, config: Config) -> Self {
let state = state::State::new();
Self {
trussed,
state,
up,
config,
}
}
fn hash(&mut self, data: &[u8]) -> Bytes<32> {
let hash = syscall!(self.trussed.hash_sha256(data)).hash;
hash.to_bytes().expect("hash should fit")
}
fn skip_up_check(&mut self) -> bool {
if let Some(timeout) = self.config.skip_up_timeout.take() {
let uptime = syscall!(self.trussed.uptime()).uptime;
if uptime < timeout {
info_now!("skip up check directly after boot");
return true;
}
}
false
}
}
#[cfg(test)]
mod test {}