use crate::crypto::{
AEAD_MIC_LENGTH_BYTES, AEAD_NONCE_LENGTH_BYTES, SymmetricKey, aead_decrypt_in_place,
aead_encrypt_in_place, ct_eq, hmac,
};
use crate::error::{Result, bail};
pub const OVERHEAD: usize = AEAD_NONCE_LENGTH_BYTES + 4 + AEAD_MIC_LENGTH_BYTES;
pub const KEY_REFRESH_OFFSET: u32 = 1 << 31;
pub fn nonce(key: &SymmetricKey, counter: u32) -> Result<[u8; AEAD_NONCE_LENGTH_BYTES]> {
let digest = hmac(key.as_bytes(), &counter.to_le_bytes())?;
let mut out = [0u8; AEAD_NONCE_LENGTH_BYTES];
let Some(head) = digest.get(..AEAD_NONCE_LENGTH_BYTES) else {
bail!(BufferTooSmall)
};
out.copy_from_slice(head);
Ok(out)
}
pub fn encrypt(
key: &SymmetricKey,
counter: u32,
application_data: &[u8],
out: &mut [u8],
) -> Result<usize> {
let nonce = nonce(key, counter)?;
let plaintext_len = application_data.len().saturating_add(4);
let total = plaintext_len
.saturating_add(AEAD_NONCE_LENGTH_BYTES)
.saturating_add(AEAD_MIC_LENGTH_BYTES);
if out.len() < total {
bail!(BufferTooSmall)
}
let Some(head) = out.get_mut(..AEAD_NONCE_LENGTH_BYTES) else {
bail!(BufferTooSmall)
};
head.copy_from_slice(&nonce);
let body_end = AEAD_NONCE_LENGTH_BYTES.saturating_add(plaintext_len);
let Some(body) = out.get_mut(AEAD_NONCE_LENGTH_BYTES..body_end) else {
bail!(BufferTooSmall)
};
if body.len() < 4 {
bail!(BufferTooSmall)
}
let (counter_slot, data_slot) = body.split_at_mut(4);
counter_slot.copy_from_slice(&counter.to_le_bytes());
data_slot.copy_from_slice(application_data);
let mut tag = [0u8; AEAD_MIC_LENGTH_BYTES];
aead_encrypt_in_place(key, &nonce, &[], body, &mut tag)?;
let Some(tail) = out.get_mut(body_end..total) else {
bail!(BufferTooSmall)
};
tail.copy_from_slice(&tag);
Ok(total)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CheckIn<'a> {
pub counter: u32,
pub application_data: &'a [u8],
}
pub fn decrypt<'a>(key: &SymmetricKey, buf: &'a mut [u8]) -> Result<CheckIn<'a>> {
if buf.len() < OVERHEAD {
bail!(BtpMalformed)
}
let (head, rest) = buf.split_at_mut(AEAD_NONCE_LENGTH_BYTES);
let mut received_nonce = [0u8; AEAD_NONCE_LENGTH_BYTES];
received_nonce.copy_from_slice(head);
let body_len = rest.len().saturating_sub(AEAD_MIC_LENGTH_BYTES);
let (body, tag_bytes) = rest.split_at_mut(body_len);
let mut tag = [0u8; AEAD_MIC_LENGTH_BYTES];
let Some(tag_slice) = tag_bytes.get(..AEAD_MIC_LENGTH_BYTES) else {
bail!(BtpMalformed)
};
tag.copy_from_slice(tag_slice);
aead_decrypt_in_place(key, &received_nonce, &[], body, &tag)?;
let (Some(counter_bytes), Some(application_data)) = (body.get(..4), body.get(4..)) else {
bail!(BtpMalformed)
};
let mut counter_le = [0u8; 4];
counter_le.copy_from_slice(counter_bytes);
let counter = u32::from_le_bytes(counter_le);
let derived = nonce(key, counter)?;
if !ct_eq(&derived, &received_nonce) {
bail!(IntegrityCheckFailed)
}
Ok(CheckIn {
counter,
application_data,
})
}
#[derive(Debug, Clone)]
pub struct Registration {
key: SymmetricKey,
start: u32,
offset: u32,
}
impl Registration {
#[must_use]
pub const fn new(key: SymmetricKey, start_counter: u32) -> Self {
Self {
key,
start: start_counter,
offset: 0,
}
}
#[must_use]
pub const fn key(&self) -> &SymmetricKey {
&self.key
}
#[must_use]
pub const fn start_counter(&self) -> u32 {
self.start
}
#[must_use]
pub const fn offset(&self) -> u32 {
self.offset
}
#[must_use]
pub const fn is_fresh(&self, counter: u32) -> bool {
self.offset < counter.wrapping_sub(self.start)
}
#[must_use]
pub const fn needs_key_refresh(&self) -> bool {
self.offset >= KEY_REFRESH_OFFSET
}
pub fn accept<'a>(&mut self, buf: &'a mut [u8]) -> Result<CheckIn<'a>> {
let message = decrypt(&self.key, buf)?;
if !self.is_fresh(message.counter) {
bail!(DuplicateMessage)
}
self.offset = message.counter.wrapping_sub(self.start);
Ok(message)
}
pub fn refresh(&mut self, key: SymmetricKey, start_counter: u32) {
self.key = key;
self.start = start_counter;
self.offset = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn hex(text: &str) -> heapless::Vec<u8, 128> {
text.split(':')
.filter(|s| !s.is_empty())
.map(|byte| u8::from_str_radix(byte.trim(), 16).expect("hex"))
.collect()
}
fn key(text: &str) -> SymmetricKey {
SymmetricKey::from_slice(&hex(text)).expect("16 octets")
}
#[test]
fn appendix_f4_test_1() {
let k = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
assert_eq!(
nonce(&k, 0x0C).expect("nonce").as_slice(),
hex("45:80:d2:c6:f1:31:0d:c4:eb:64:f1:f8:e8").as_slice()
);
let expected = hex(
"45:80:d2:c6:f1:31:0d:c4:eb:64:f1:f8:e8:bd:c2:1f:b5:19:5d:74:7d:d2:87:9b:2b:0d:\
43:ce:5b:1c:56:50:78",
);
let mut out = [0u8; 64];
let n = encrypt(&k, 0x0C, b"", &mut out).expect("encrypt");
assert_eq!(&out[..n], expected.as_slice());
let mut registration = Registration::new(k, 0x0B);
let mut buf = expected.clone();
let message = registration.accept(&mut buf).expect("valid");
assert_eq!(message.counter, 0x0C);
assert!(message.application_data.is_empty());
assert_eq!(registration.offset(), 1);
}
#[test]
fn appendix_f4_test_2() {
let k = key("18:fd:bc:ea:ef:01:95:5b:0e:c8:75:ed:a3:ae:6e:e8");
assert_eq!(
nonce(&k, 0x0F).expect("nonce").as_slice(),
hex("9b:02:ed:21:ee:0c:7b:49:19:85:50:2e:37").as_slice()
);
let expected = hex(
"9b:02:ed:21:ee:0c:7b:49:19:85:50:2e:37:2d:bd:7b:3f:8b:4f:8e:3c:5a:d9:94:19:38:\
9f:41:a8:d6:09:93:8c:67:a8:6d:65",
);
let mut out = [0u8; 64];
let n = encrypt(&k, 0x0F, b"This", &mut out).expect("encrypt");
assert_eq!(&out[..n], expected.as_slice());
let mut registration = Registration::new(k, 0x0B);
let mut buf = expected;
let message = registration.accept(&mut buf).expect("valid");
assert_eq!(message.counter, 0x0F);
assert_eq!(message.application_data, b"This");
assert_eq!(registration.offset(), 4);
}
#[test]
fn appendix_f4_test_3_a_counter_at_the_starting_value_is_a_replay() {
let k = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
let expected = hex(
"aa:84:bc:60:88:6a:63:a8:47:5d:5d:be:b5:6d:63:5f:a9:52:85:ae:33:62:66:13:c7:63:\
6c:e3:e3:b2:a8:b1:3a:8c:89:be:f7:68:91:e8:e2:96",
);
let mut out = [0u8; 64];
let n = encrypt(&k, 0x0B, b"This is a", &mut out).expect("encrypt");
assert_eq!(&out[..n], expected.as_slice());
let mut buf = expected.clone();
let opened = decrypt(&k, &mut buf).expect("the cryptography is sound");
assert_eq!(opened.counter, 0x0B);
assert_eq!(opened.application_data, b"This is a");
let mut registration = Registration::new(k, 0x0B);
let mut buf = expected;
assert_eq!(
registration
.accept(&mut buf)
.map(|_| ())
.map_err(|e| e.code()),
Err(crate::ErrorCode::DuplicateMessage)
);
assert_eq!(
registration.offset(),
0,
"a replay does not move the window"
);
}
#[test]
fn appendix_f4_test_4_a_counter_behind_the_window_is_a_replay() {
let k = key("ca:67:d4:1f:f7:11:29:10:fd:d1:8a:1b:f9:9e:a9:74");
let expected = hex(
"7a:97:72:24:3c:97:c8:7d:5f:3a:31:c4:e6:db:bc:1a:a5:66:c4:43:c2:05:86:06:6b:42:\
7b:fc:aa:ad:78:da:4a:10:5a:13:42:ad:bf:3f:47:98:cd:81:b9:ef:97:bb:b7",
);
let mut out = [0u8; 80];
let n = encrypt(&k, 0x0B, b"This is a longer", &mut out).expect("encrypt");
assert_eq!(&out[..n], expected.as_slice());
let mut registration = Registration::new(k.clone(), 0x0B);
let mut ahead = [0u8; 80];
let n = encrypt(&k, 0x0F, b"", &mut ahead).expect("encrypt");
registration
.accept(&mut ahead[..n])
.expect("0x0F arrives first");
assert_eq!(registration.offset(), 4);
let mut buf = expected;
assert_eq!(
registration
.accept(&mut buf)
.map(|_| ())
.map_err(|e| e.code()),
Err(crate::ErrorCode::DuplicateMessage),
"§4.22.3.3 rejects it because the window has already passed it"
);
assert_eq!(registration.offset(), 4, "and the window did not move");
}
#[test]
fn a_counter_far_ahead_is_accepted_and_demands_a_new_key() {
let k = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
let mut registration = Registration::new(k.clone(), 0x0F);
let mut out = [0u8; 64];
let n = encrypt(&k, 0x0B, b"", &mut out).expect("encrypt");
registration
.accept(&mut out[..n])
.expect("arithmetically fresh");
assert_eq!(registration.offset(), 0xFFFF_FFFC);
assert!(registration.needs_key_refresh());
}
#[test]
fn appendix_f4_test_5_a_tampered_message_does_not_decrypt() {
let k = key("ca:67:d4:1f:f7:11:29:10:fd:d1:8a:1b:f9:9e:a9:74");
let mut buf = hex(
"f9:34:67:6e:a6:e0:70:7b:7a:d7:81:4f:f8:2e:5b:18:d1:9a:23:b2:e4:fa:df:82:92:53:\
51:7f:f3:c9:1d:8d:47:84:31:5a:1e:32:08:b8:ec:f6:11:8b:02:1a:5a:4c:d4:e9:d4:13:\
8d:ff:29:71",
);
assert_eq!(
decrypt(&k, &mut buf).map(|_| ()).map_err(|e| e.code()),
Err(crate::ErrorCode::IntegrityCheckFailed)
);
}
#[test]
fn appendix_f4_test_6_a_nonce_that_does_not_match_its_counter_is_refused() {
let k = key("ca:67:d4:1f:f7:11:29:10:fd:d1:8a:1b:f9:9e:a9:74");
let mut buf = hex(
"06:34:67:6e:a6:e0:70:7b:7a:d7:81:4f:f8:29:5b:18:d1:9a:23:b2:e4:fa:df:82:92:53:\
51:7f:f3:c9:1d:8d:47:84:2e:41:02:3c:03:ad:66:ac:4d:ca:72:47:e0:e4:c6:6b:d9:d3:\
99:13:e2:3d:82:32:b9:61:fa:92:26",
);
assert_eq!(
decrypt(&k, &mut buf).map(|_| ()).map_err(|e| e.code()),
Err(crate::ErrorCode::IntegrityCheckFailed),
"the AEAD succeeded; the nonce check is what refused it"
);
}
#[test]
fn the_window_survives_the_counter_wrapping() {
let k = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
let start = u32::MAX.saturating_sub(2);
let mut registration = Registration::new(k.clone(), start);
let mut out = [0u8; 64];
for counter in [u32::MAX - 1, u32::MAX, 0, 1, 2] {
let n = encrypt(&k, counter, b"", &mut out).expect("encrypt");
let mut buf = out;
let message = registration
.accept(&mut buf[..n])
.expect("a wrapped counter is still moving forwards");
assert_eq!(message.counter, counter);
}
assert_eq!(registration.offset(), 5);
let n = encrypt(&k, u32::MAX, b"", &mut out).expect("encrypt");
let mut buf = out;
assert_eq!(
registration
.accept(&mut buf[..n])
.map(|_| ())
.map_err(|e| e.code()),
Err(crate::ErrorCode::DuplicateMessage)
);
}
#[test]
fn a_key_must_be_refreshed_at_half_the_counter_space() {
let k = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
let mut registration = Registration::new(k.clone(), 0);
assert!(!registration.needs_key_refresh());
let mut out = [0u8; 64];
let n = encrypt(&k, KEY_REFRESH_OFFSET, b"", &mut out).expect("encrypt");
let mut buf = out;
registration.accept(&mut buf[..n]).expect("still valid");
assert!(
registration.needs_key_refresh(),
"2³¹ counter values have been used"
);
let fresh = key("18:fd:bc:ea:ef:01:95:5b:0e:c8:75:ed:a3:ae:6e:e8");
registration.refresh(fresh, 7);
assert!(!registration.needs_key_refresh());
assert_eq!(registration.offset(), 0);
assert_eq!(registration.start_counter(), 7);
}
#[test]
fn a_client_identifies_the_server_by_which_key_opens_the_message() {
let a = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
let b = key("18:fd:bc:ea:ef:01:95:5b:0e:c8:75:ed:a3:ae:6e:e8");
let mut out = [0u8; 64];
let n = encrypt(&b, 3, b"", &mut out).expect("encrypt");
let mut buf = out;
assert_eq!(
decrypt(&a, &mut buf[..n]).map(|_| ()).map_err(|e| e.code()),
Err(crate::ErrorCode::IntegrityCheckFailed),
"the wrong key is a failure to try the next one with, not a bad message"
);
let mut buf = out;
assert_eq!(decrypt(&b, &mut buf[..n]).expect("right key").counter, 3);
}
#[test]
fn a_message_shorter_than_its_own_overhead_is_refused() {
let k = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
for len in 0..OVERHEAD {
let mut buf = [0u8; OVERHEAD];
assert!(decrypt(&k, &mut buf[..len]).is_err(), "length {len}");
}
}
#[test]
fn a_buffer_too_small_for_the_message_is_refused_rather_than_truncated() {
let k = key("d9:0e:13:18:0d:00:ba:ad:d2:0c:f5:ed:49:13:d3:ff");
let mut out = [0u8; OVERHEAD];
encrypt(&k, 1, b"", &mut out).expect("exactly enough for no application data");
assert_eq!(
encrypt(&k, 1, b"x", &mut out).map_err(|e| e.code()),
Err(crate::ErrorCode::BufferTooSmall)
);
}
}