-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathkey.rs
More file actions
178 lines (160 loc) · 5.18 KB
/
key.rs
File metadata and controls
178 lines (160 loc) · 5.18 KB
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
use heapless::Vec;
use serde::{Deserialize, Serialize};
use serde_indexed::{DeserializeIndexed, SerializeIndexed};
use zeroize::Zeroize;
pub use crate::Bytes;
use crate::{
Error,
config::{MAX_KEY_MATERIAL_LENGTH, MAX_SERIALIZED_KEY_LENGTH},
};
pub type Material = Vec<u8, {MAX_KEY_MATERIAL_LENGTH}>;
pub type SerializedKeyBytes = Vec<u8, {MAX_SERIALIZED_KEY_LENGTH}>;
// We don't implement serde to make sure nobody inadvertently still uses it
// Should we use references here only?
// #[derive(Clone, Debug, DeserializeIndexed, Eq, PartialEq, SerializeIndexed)]
/// A key object in Trussed.
///
/// Follows Sophie Schmieg's [dictum][dictum] that
/// "A key should always be considered to be the raw key material alongside its parameter choices."
///
/// [dictum]: https://twitter.com/SchmiegSophie/status/1264567198091079681
#[derive(Clone, Debug, /*DeserializeIndexed,*/ Eq, PartialEq, /*SerializeIndexed,*/ Zeroize)]
pub struct Key {
pub flags: Flags,
pub kind: Kind,
pub material: Material,
}
#[derive(Clone, Debug, /*DeserializeIndexed,*/ Eq, PartialEq, /*SerializeIndexed,*/ Zeroize)]
pub struct Info {
pub flags: Flags,
pub kind: Kind,
}
impl Info {
pub fn with_local_flag(mut self) -> Self {
self.flags |= Flags::LOCAL;
self
}
}
impl From<Kind> for Info {
fn from(kind: Kind) -> Self {
Self { flags: Default::default(), kind }
}
}
// TODO: How to store/check?
// TODO: Fix variant indices to keep storage stable!!
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Zeroize)]
#[repr(u16)]
pub enum Kind {
/// some bytes of entropy, needs a KDF applied,
/// the parameter is the length of the key
Shared(usize),
/// entropic bytes, suitable for use as symmetric secret (e.g., AES),
/// the parameter is the length of the key (e.g. 16 for AES).
Symmetric(usize),
/// 32B symmetric key + nonce, the parameter is the length of the nonce in bytes
Symmetric32Nonce(usize),
Ed255,
P256,
X255,
P384,
P521,
Rsa2k,
Rsa3k,
Rsa4k,
Ed448,
X448,
}
bitflags::bitflags! {
#[derive(DeserializeIndexed, SerializeIndexed, Zeroize)]
/// All non-used bits are RFU.
///
/// In particular, top bit is intended to be used to accomodate breaking format changes,
/// i.e., if `flags >> 32 != 0`, then the format is different.
pub struct Flags: u16 {
const LOCAL = 1 << 0;
const SENSITIVE = 1 << 1;
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
/// A key can either be public, of secret.
///
/// The secret case also applies to private keys for asymmetric algorithms.
pub enum Secrecy {
// Private,
Public,
Secret,
}
impl Key {
pub fn serialize(&self) -> SerializedKeyBytes {
let mut buffer = SerializedKeyBytes::new();
// big-endian here to ensure the first bit is enough to check compatibility
// on breaking format change
buffer.extend_from_slice(&self.flags.bits().to_be_bytes()).unwrap();
buffer.extend_from_slice(&(self.kind.code()).to_be_bytes()).unwrap();
// can't fail, since MAX_SERIALIZED_KEY_LENGTH is defined as MAX_KEY_MATERIAL_LENGTH + 4
buffer.extend_from_slice(&self.material).unwrap();
buffer
}
pub fn try_deserialize(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() < 4 {
return Err(Error::InvalidSerializedKey);
}
let (info, material) = bytes.split_at(4);
let flags_bits = u16::from_be_bytes([info[0], info[1]]);
let flags = Flags::from_bits(flags_bits).ok_or(Error::InvalidSerializedKey)?;
let kind_bits = u16::from_be_bytes([info[2], info[3]]);
let kind = Kind::try_from(kind_bits, material.len()).map_err(|_| Error::InvalidSerializedKey)?;
Ok(Key {
flags,
kind,
material: Material::from_slice(material).map_err(|_| Error::InvalidSerializedKey)?,
})
}
}
impl Default for Flags {
/// This implements "safe" defaults
/// - no claim on local generation
/// - default sensitive
fn default() -> Self {
Flags::SENSITIVE
}
}
impl Kind {
pub fn code(self) -> u16 {
match self {
Kind::Shared(_) => 1,
Kind::Symmetric(_) => 2,
Kind::Symmetric32Nonce(_) => 3,
Kind::Ed255 => 4,
Kind::P256 => 5,
Kind::X255 => 6,
// following PIV and our extensions
Kind::P384 => 0x14,
Kind::P521 => 0x15,
Kind::Rsa2k => 0x7,
Kind::Rsa3k => 0xE0,
Kind::Rsa4k => 0xE1,
Kind::Ed448 => 0xE4,
Kind::X448 => 0xE5,
}
}
pub fn try_from(code: u16, length: usize) -> Result<Self, Error> {
use Kind::*;
Ok(match code {
1 => Shared(length),
2 => Symmetric(length),
3 => Symmetric32Nonce(length - 32),
4 => Ed255,
5 => P256,
6 => X255,
0x14 => P384,
0x15 => P521,
0x7 => Rsa2k,
0xE0 => Rsa3k,
0xE1 => Rsa4k,
0xE4 => Ed448,
0xE5 => X448,
_ => return Err(Error::InvalidSerializedKey),
})
}
}