use crate::error::Error;
use crate::payload_util::require_at_least;
use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeySet {
pub key_type: u8,
pub key: Vec<u8>,
}
impl KeySet {
pub fn scbk(key: [u8; 16]) -> Self {
Self {
key_type: 0x01,
key: key.to_vec(),
}
}
pub fn encode(&self) -> Result<Vec<u8>, Error> {
if self.key.len() > u8::MAX as usize {
return Err(Error::MalformedPayload {
code: 0x75,
reason: "KEYSET key length exceeds 255",
});
}
let mut out = Vec::with_capacity(2 + self.key.len());
out.push(self.key_type);
out.push(self.key.len() as u8);
out.extend_from_slice(&self.key);
Ok(out)
}
pub fn decode(data: &[u8]) -> Result<Self, Error> {
require_at_least(data, 2, 0x75)?;
let key_len = data[1] as usize;
if data.len() != 2 + key_len {
return Err(Error::MalformedPayload {
code: 0x75,
reason: "KEYSET key length disagrees with payload",
});
}
Ok(Self {
key_type: data[0],
key: data[2..2 + key_len].to_vec(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scbk_roundtrip() {
let key = [0xAAu8; 16];
let body = KeySet::scbk(key);
let bytes = body.encode().unwrap();
assert_eq!(bytes[0], 0x01);
assert_eq!(bytes[1], 16);
assert_eq!(&bytes[2..], &key);
assert_eq!(KeySet::decode(&bytes).unwrap(), body);
}
#[test]
fn decode_rejects_short() {
assert!(matches!(
KeySet::decode(&[0x01]),
Err(Error::PayloadTooShort { code: 0x75, .. })
));
}
#[test]
fn decode_rejects_length_mismatch() {
assert!(matches!(
KeySet::decode(&[0x01, 0x04, 0xDE, 0xAD]),
Err(Error::MalformedPayload { code: 0x75, .. })
));
}
}