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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use std::{
array::TryFromSliceError,
convert::TryInto,
fmt::{self, Debug, Formatter},
ops::Deref,
};
use aes::{Aes128, Aes192, Aes256};
use cipher::KeyInit;
use hmac::Hmac;
use rand::{rngs::OsRng, RngCore};
use sha1::Sha1;
use crate::{
packet::SeqNumber,
settings::{KeySettings, KeySize, Passphrase},
};
use super::wrap;
#[derive(Clone, Eq, PartialEq)]
pub struct Salt([u8; 16]);
impl Salt {
pub fn new_random() -> Self {
let mut salt = [0; 16];
OsRng.fill_bytes(&mut salt[..]);
Self(salt)
}
pub fn try_from(bytes: &[u8]) -> Result<Salt, TryFromSliceError> {
Ok(Salt(bytes[..].try_into()?))
}
pub fn generate_strean_iv_for(&self, seq_number: SeqNumber) -> StreamInitializationVector {
let salt = self.0;
let mut out = [0; 16];
out[0..14].copy_from_slice(&salt[..14]);
for (i, b) in seq_number.0.to_be_bytes().iter().enumerate() {
out[i + 10] ^= *b;
}
StreamInitializationVector(out)
}
pub fn as_slice(&self) -> &[u8] {
&self.0
}
}
impl Debug for Salt {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "Salt(0x{})", hex::encode_upper(self.0))
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct StreamInitializationVector([u8; 16]);
impl StreamInitializationVector {
pub fn try_from(slice: &[u8]) -> Result<Self, TryFromSliceError> {
Ok(StreamInitializationVector(slice[..].try_into()?))
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
impl Debug for StreamInitializationVector {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "StreamIV(0x{})", hex::encode_upper(self.0))
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct WrapInitializationVector([u8; 8]);
impl WrapInitializationVector {
pub fn try_from(slice: &[u8]) -> Result<Self, TryFromSliceError> {
Ok(WrapInitializationVector(slice[..].try_into()?))
}
}
impl Debug for WrapInitializationVector {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "KeyIV(0x{})", hex::encode_upper(self.0))
}
}
#[derive(Clone, Eq, PartialEq)]
pub enum EncryptionKey {
Bytes16([u8; 16]),
Bytes24([u8; 24]),
Bytes32([u8; 32]),
}
impl EncryptionKey {
pub fn new_random(size: KeySize) -> Self {
use EncryptionKey::*;
fn new_key<const N: usize>() -> [u8; N] {
let mut key = [0u8; N];
OsRng.fill_bytes(&mut key[..]);
key
}
match size {
KeySize::AES128 => Bytes16(new_key()),
KeySize::AES192 => Bytes24(new_key()),
KeySize::AES256 => Bytes32(new_key()),
KeySize::Unspecified => Bytes16(new_key()),
}
}
pub fn try_from(bytes: &[u8]) -> Result<EncryptionKey, TryFromSliceError> {
use EncryptionKey::*;
match bytes.len() {
16 => Ok(Bytes16(bytes[..].try_into()?)),
24 => Ok(Bytes24(bytes[..].try_into()?)),
_ => Ok(Bytes32(bytes[..].try_into()?)),
}
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
use EncryptionKey::*;
match self {
Bytes16(key) => key.len(),
Bytes24(key) => key.len(),
Bytes32(key) => key.len(),
}
}
pub fn as_bytes(&self) -> &[u8] {
use EncryptionKey::*;
match self {
Bytes16(key) => &key[..],
Bytes24(key) => &key[..],
Bytes32(key) => &key[..],
}
}
}
impl fmt::Debug for EncryptionKey {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use EncryptionKey::*;
match self {
Bytes16(_) => f.debug_struct("EncryptionKey::Bytes16"),
Bytes24(_) => f.debug_struct("EncryptionKey::Bytes24"),
Bytes32(_) => f.debug_struct("EncryptionKey::Bytes32"),
}
.finish()
}
}
#[derive(Clone, Eq, PartialEq)]
pub struct KeyEncryptionKey(EncryptionKey);
impl KeyEncryptionKey {
pub fn new(key_settings: &KeySettings, salt: &Salt) -> Self {
let key_size = key_settings.key_size;
let passphrase = &key_settings.passphrase;
fn calculate_pbkdf2(passphrase: &Passphrase, salt: &Salt, key: &mut [u8]) {
let salt = salt.0;
const ROUNDS: u32 = 2048;
let salt_len = usize::min(8, salt.len());
pbkdf2::pbkdf2::<Hmac<Sha1>>(
passphrase.as_bytes(),
&salt[salt.len() - salt_len..], ROUNDS,
&mut *key,
).unwrap();
}
fn new_key<const N: usize>(passphrase: &Passphrase, salt: &Salt) -> [u8; N] {
let mut key = [0u8; N];
calculate_pbkdf2(passphrase, salt, &mut key);
key
}
use EncryptionKey::*;
let key = match key_size {
KeySize::AES128 => Bytes16(new_key(passphrase, salt)),
KeySize::AES192 => Bytes24(new_key(passphrase, salt)),
KeySize::AES256 => Bytes32(new_key(passphrase, salt)),
KeySize::Unspecified => Bytes16(new_key(passphrase, salt)),
};
KeyEncryptionKey(key)
}
pub fn encrypt_wrapped_keys(&self, keys: &[u8]) -> Vec<u8> {
let mut encrypted_keys = vec![0; keys.len() + 8];
use EncryptionKey::*;
match &self.0 {
Bytes16(key) => wrap::aes_wrap(
&Aes128::new(key[..].into()),
None,
&mut encrypted_keys,
keys,
),
Bytes24(key) => wrap::aes_wrap(
&Aes192::new(key[..].into()),
None,
&mut encrypted_keys,
keys,
),
Bytes32(key) => wrap::aes_wrap(
&Aes256::new(key[..].into()),
None,
&mut encrypted_keys,
keys,
),
}
encrypted_keys
}
pub fn decrypt_wrapped_keys(
&self,
wrapped_keys: &[u8],
) -> Result<Vec<u8>, WrapInitializationVector> {
use EncryptionKey::*;
let mut keys = vec![0; wrapped_keys.len() - 8];
let mut iv = [0; 8];
match &self.0 {
Bytes16(key) => wrap::aes_unwrap(
&Aes128::new(key[..].into()),
&mut iv,
&mut keys,
wrapped_keys,
),
Bytes24(key) => wrap::aes_unwrap(
&Aes192::new(key[..].into()),
&mut iv,
&mut keys,
wrapped_keys,
),
Bytes32(key) => wrap::aes_unwrap(
&Aes256::new(key[..].into()),
&mut iv,
&mut keys,
wrapped_keys,
),
}
if iv != wrap::DEFAULT_IV {
return Err(WrapInitializationVector(iv));
}
Ok(keys)
}
}
impl Deref for KeyEncryptionKey {
type Target = EncryptionKey;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Debug for KeyEncryptionKey {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use EncryptionKey::*;
match &self.0 {
Bytes16(_) => f.debug_struct("KeyEncryptionKey::Bytes16"),
Bytes24(_) => f.debug_struct("KeyEncryptionKey::Bytes24"),
Bytes32(_) => f.debug_struct("KeyEncryptionKey::Bytes32"),
}
.finish()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn kek_generate() {
let key_settings = KeySettings {
key_size: KeySize::AES128,
passphrase: "password123".into(),
};
let expected_kek = &hex::decode(b"08F2758F41E4244D00057C9CEBEB95FC").unwrap()[..];
let salt =
Salt::try_from(&hex::decode(b"7D59759C2B1A3F0B06C7028790C81C7D").unwrap()[..]).unwrap();
let kek = KeyEncryptionKey::new(&key_settings, &salt);
assert_eq!(kek.0.as_bytes(), expected_kek);
assert_eq!(format!("{kek:?}"), "KeyEncryptionKey::Bytes16");
assert_eq!(format!("{:?}", kek.deref()), "EncryptionKey::Bytes16");
assert_ne!(Salt::new_random(), Salt::new_random());
}
#[test]
fn generate_iv() {
let salt =
Salt::try_from(&hex::decode(b"87647f8a2361fb1a9e692de576985949").unwrap()[..]).unwrap();
let expected_iv = StreamInitializationVector::try_from(
&hex::decode(b"87647f8a2361fb1a9e6907af1b810000").unwrap()[..],
)
.unwrap();
let iv = salt.generate_strean_iv_for(SeqNumber(709520665));
assert_eq!(iv, expected_iv);
assert_eq!(
format!("{iv:?}"),
"StreamIV(0x87647F8A2361FB1A9E6907AF1B810000)"
);
assert_eq!(
format!("{salt:?}"),
"Salt(0x87647F8A2361FB1A9E692DE576985949)"
);
assert_ne!(Salt::new_random(), Salt::new_random());
}
}