2024-02-17 17:12:16 +01:00
|
|
|
use crate::canonicalization::canonicalize;
|
2024-03-17 14:31:01 +01:00
|
|
|
use crate::context::KeyContext;
|
2024-02-17 16:29:54 +01:00
|
|
|
use crate::ikm::InputKeyMaterial;
|
|
|
|
|
|
|
|
pub(crate) type KdfFunction = dyn Fn(&str, &[u8]) -> Vec<u8>;
|
|
|
|
|
2024-03-09 11:49:40 +01:00
|
|
|
pub(crate) fn derive_key(
|
|
|
|
ikm: &InputKeyMaterial,
|
|
|
|
ctx: &KeyContext,
|
|
|
|
time_period: Option<u64>,
|
|
|
|
) -> Vec<u8> {
|
2024-04-20 19:02:55 +02:00
|
|
|
let mut elems = ctx.get_ctx_elems(time_period);
|
|
|
|
elems.push(ikm.scheme.get_key_len().to_le_bytes().to_vec());
|
2024-03-09 16:09:34 +01:00
|
|
|
let key_context = canonicalize(&elems);
|
2024-02-17 16:29:54 +01:00
|
|
|
let kdf = ikm.scheme.get_kdf();
|
|
|
|
kdf(&key_context, &ikm.content)
|
|
|
|
}
|
|
|
|
|
2024-02-17 17:58:42 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::ikm::InputKeyMaterial;
|
2024-03-02 14:53:38 +01:00
|
|
|
use crate::KeyContext;
|
2024-03-09 16:27:23 +01:00
|
|
|
use std::num::NonZeroU64;
|
2024-02-17 17:58:42 +01:00
|
|
|
|
2024-03-09 16:27:23 +01:00
|
|
|
const TEST_RAW_IKM: &[u8] = &[
|
|
|
|
0x0a, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7b, 0x85, 0x27, 0xef, 0xf2, 0xbd, 0x58,
|
|
|
|
0x9f, 0x6e, 0xb1, 0x7b, 0x71, 0xc3, 0x1e, 0xf6, 0xfd, 0x7f, 0x90, 0xdb, 0xc6, 0x43, 0xea,
|
|
|
|
0xe9, 0x9c, 0xa3, 0xb5, 0xee, 0xcc, 0xb6, 0xb6, 0x28, 0x6a, 0xbd, 0xe4, 0xd0, 0x65, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x3d, 0x82, 0x6f, 0x8b, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
];
|
2024-02-17 17:58:42 +01:00
|
|
|
|
2024-03-09 16:27:23 +01:00
|
|
|
#[test]
|
2024-04-20 11:48:58 +02:00
|
|
|
#[cfg(feature = "chacha")]
|
2024-03-09 16:27:23 +01:00
|
|
|
fn derive_key_no_tp() {
|
|
|
|
let ikm = InputKeyMaterial::from_bytes(TEST_RAW_IKM).unwrap();
|
2024-03-02 14:53:38 +01:00
|
|
|
let ctx = KeyContext::from(["some", "context"]);
|
2024-02-17 17:58:42 +01:00
|
|
|
assert_eq!(
|
2024-03-02 14:53:38 +01:00
|
|
|
super::derive_key(&ikm, &ctx, None),
|
2024-02-17 17:58:42 +01:00
|
|
|
vec![
|
2024-04-20 19:02:55 +02:00
|
|
|
0xf9, 0x90, 0x22, 0x8, 0x9a, 0x63, 0x2, 0xc9, 0xd0, 0x7c, 0x71, 0x56, 0xa4, 0xc3,
|
|
|
|
0x8c, 0x4b, 0x1d, 0xe8, 0x56, 0xf2, 0xc3, 0xf6, 0xba, 0xc3, 0x4b, 0x8d, 0x85, 0x29,
|
|
|
|
0x4b, 0x5, 0x13, 0xc3
|
2024-02-17 17:58:42 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2024-03-09 16:27:23 +01:00
|
|
|
|
|
|
|
#[test]
|
2024-04-20 11:48:58 +02:00
|
|
|
#[cfg(feature = "chacha")]
|
2024-03-09 16:27:23 +01:00
|
|
|
fn derive_key_tp_0() {
|
|
|
|
let ikm = InputKeyMaterial::from_bytes(TEST_RAW_IKM).unwrap();
|
|
|
|
let ctx = KeyContext::from(["some", "context"]);
|
|
|
|
assert_eq!(
|
|
|
|
super::derive_key(&ikm, &ctx, Some(0)),
|
|
|
|
vec![
|
2024-04-20 19:02:55 +02:00
|
|
|
0xfe, 0x70, 0x65, 0x84, 0x79, 0x9a, 0xc0, 0xf1, 0x50, 0xb5, 0x72, 0x73, 0x16, 0xf4,
|
|
|
|
0x5b, 0x49, 0xb4, 0x46, 0xfa, 0x58, 0xa6, 0xb9, 0xf0, 0xc7, 0xec, 0x49, 0x87, 0x7e,
|
|
|
|
0x46, 0xd0, 0x9, 0xe8
|
2024-03-09 16:27:23 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-04-20 11:48:58 +02:00
|
|
|
#[cfg(feature = "chacha")]
|
2024-03-09 16:27:23 +01:00
|
|
|
fn derive_key_tp_42() {
|
|
|
|
let ikm = InputKeyMaterial::from_bytes(TEST_RAW_IKM).unwrap();
|
|
|
|
let ctx = KeyContext::from(["some", "context"]);
|
|
|
|
assert_eq!(
|
|
|
|
super::derive_key(&ikm, &ctx, Some(42)),
|
|
|
|
vec![
|
2024-04-20 19:02:55 +02:00
|
|
|
0x5c, 0x9b, 0x1c, 0xfa, 0x21, 0xac, 0xdb, 0x37, 0x4d, 0xee, 0x60, 0xf7, 0x6, 0x18,
|
|
|
|
0x85, 0xb5, 0x95, 0x2a, 0x6c, 0xd3, 0x43, 0x9, 0xcb, 0x1b, 0x7f, 0x9d, 0xdf, 0x58,
|
|
|
|
0xf9, 0x3e, 0x6e, 0x14
|
2024-03-09 16:27:23 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_time_period() {
|
|
|
|
let test_vec = &[
|
|
|
|
// (periodicity, timestamp, reference value)
|
|
|
|
(1, 0, 0),
|
|
|
|
(1, 1, 1),
|
|
|
|
(1, 2, 2),
|
|
|
|
(1, 35015, 35015),
|
|
|
|
(16_777_216, 0, 0),
|
|
|
|
(16_777_216, 16_777_215, 0),
|
|
|
|
(16_777_216, 16_777_216, 1),
|
|
|
|
(16_777_216, 1_709_994_382, 101),
|
|
|
|
];
|
|
|
|
let mut ctx = KeyContext::from([]);
|
|
|
|
for (p, ts, ref_val) in test_vec {
|
|
|
|
let p = NonZeroU64::new(*p).unwrap();
|
|
|
|
ctx.set_periodicity(p);
|
|
|
|
let tp = ctx.get_time_period(*ts);
|
|
|
|
assert_eq!(tp, Some(*ref_val));
|
|
|
|
}
|
|
|
|
}
|
2024-02-17 17:58:42 +01:00
|
|
|
}
|