use crate::codec::{Decode, Encode, ErrorKind, Reader, Result, Writer};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SecurityControl(pub u8);
impl SecurityControl {
#[must_use]
pub const fn new(suite: u8, authenticated: bool, encrypted: bool) -> Self {
let mut v = suite & 0x0F;
if authenticated {
v |= 0x10;
}
if encrypted {
v |= 0x20;
}
Self(v)
}
#[must_use]
pub const fn with_broadcast(self, broadcast: bool) -> Self {
Self(if broadcast { self.0 | 0x40 } else { self.0 & !0x40 })
}
#[must_use]
pub const fn suite(self) -> u8 {
self.0 & 0x0F
}
#[must_use]
pub const fn authenticated(self) -> bool {
self.0 & 0x10 != 0
}
#[must_use]
pub const fn encrypted(self) -> bool {
self.0 & 0x20 != 0
}
#[must_use]
pub const fn broadcast_key(self) -> bool {
self.0 & 0x40 != 0
}
#[must_use]
pub const fn compressed(self) -> bool {
self.0 & 0x80 != 0
}
#[must_use]
pub const fn is_plain(self) -> bool {
!self.authenticated() && !self.encrypted()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CipheredService<'a> {
pub security_control: SecurityControl,
pub invocation_counter: u32,
pub payload: &'a [u8],
}
impl Encode for CipheredService<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_length(5 + self.payload.len())?;
w.write_u8(self.security_control.0)?;
w.write_u32(self.invocation_counter)?;
w.write_bytes(self.payload)
}
}
impl<'a> Decode<'a> for CipheredService<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
let body = r.length_prefixed()?;
let mut br = Reader::with_base(body, r.offset() - body.len());
Ok(Self {
security_control: SecurityControl(br.u8()?),
invocation_counter: br.u32()?,
payload: br.take_rest(),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GeneralGloCiphering<'a> {
pub system_title: &'a [u8],
pub ciphered: CipheredService<'a>,
}
impl Encode for GeneralGloCiphering<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_length_prefixed(self.system_title)?;
self.ciphered.encode(w)
}
}
impl<'a> Decode<'a> for GeneralGloCiphering<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self { system_title: r.length_prefixed()?, ciphered: CipheredService::decode(r)? })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyInfo<'a> {
Identified {
key_id: u8,
},
Wrapped {
kek_id: u8,
ciphered_key: &'a [u8],
},
Agreed {
parameters: &'a [u8],
ciphered_key: &'a [u8],
},
}
impl Encode for KeyInfo<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
match self {
Self::Identified { key_id } => w.write_bytes(&[0, *key_id]),
Self::Wrapped { kek_id, ciphered_key } => {
w.write_bytes(&[1, *kek_id])?;
w.write_length_prefixed(ciphered_key)
}
Self::Agreed { parameters, ciphered_key } => {
w.write_u8(2)?;
w.write_length_prefixed(parameters)?;
w.write_length_prefixed(ciphered_key)
}
}
}
}
impl<'a> Decode<'a> for KeyInfo<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(match r.u8()? {
0 => Self::Identified { key_id: r.u8()? },
1 => Self::Wrapped { kek_id: r.u8()?, ciphered_key: r.length_prefixed()? },
2 => Self::Agreed { parameters: r.length_prefixed()?, ciphered_key: r.length_prefixed()? },
other => return Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GeneralCiphering<'a> {
pub transaction_id: &'a [u8],
pub originator_system_title: &'a [u8],
pub recipient_system_title: &'a [u8],
pub date_time: &'a [u8],
pub other_information: &'a [u8],
pub key_info: Option<KeyInfo<'a>>,
pub ciphered: CipheredService<'a>,
}
impl Encode for GeneralCiphering<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_length_prefixed(self.transaction_id)?;
w.write_length_prefixed(self.originator_system_title)?;
w.write_length_prefixed(self.recipient_system_title)?;
w.write_length_prefixed(self.date_time)?;
w.write_length_prefixed(self.other_information)?;
match &self.key_info {
None => w.write_u8(0)?,
Some(k) => {
w.write_u8(1)?;
k.encode(w)?;
}
}
self.ciphered.encode(w)
}
}
impl<'a> Decode<'a> for GeneralCiphering<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self {
transaction_id: r.length_prefixed()?,
originator_system_title: r.length_prefixed()?,
recipient_system_title: r.length_prefixed()?,
date_time: r.length_prefixed()?,
other_information: r.length_prefixed()?,
key_info: if r.u8()? == 0 { None } else { Some(KeyInfo::decode(r)?) },
ciphered: CipheredService::decode(r)?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GeneralSigning<'a> {
pub transaction_id: &'a [u8],
pub originator_system_title: &'a [u8],
pub recipient_system_title: &'a [u8],
pub date_time: &'a [u8],
pub other_information: &'a [u8],
pub content: &'a [u8],
pub signature: &'a [u8],
}
impl Encode for GeneralSigning<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_length_prefixed(self.transaction_id)?;
w.write_length_prefixed(self.originator_system_title)?;
w.write_length_prefixed(self.recipient_system_title)?;
w.write_length_prefixed(self.date_time)?;
w.write_length_prefixed(self.other_information)?;
w.write_length_prefixed(self.content)?;
w.write_length_prefixed(self.signature)
}
}
impl<'a> Decode<'a> for GeneralSigning<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self {
transaction_id: r.length_prefixed()?,
originator_system_title: r.length_prefixed()?,
recipient_system_title: r.length_prefixed()?,
date_time: r.length_prefixed()?,
other_information: r.length_prefixed()?,
content: r.length_prefixed()?,
signature: r.length_prefixed()?,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::SliceWriter;
#[test]
fn security_control_bits() {
let sc = SecurityControl::new(1, true, true);
assert_eq!(sc.suite(), 1);
assert!(sc.authenticated() && sc.encrypted());
assert!(!sc.broadcast_key() && !sc.compressed());
assert_eq!(sc.0, 0x31);
assert!(SecurityControl(0x00).is_plain());
assert!(SecurityControl(0x80).compressed());
assert!(SecurityControl(0x40).broadcast_key());
}
#[test]
fn a_ciphered_service_round_trips() {
let c = CipheredService {
security_control: SecurityControl::new(0, true, true),
invocation_counter: 0x0000_0001,
payload: &[0xAA, 0xBB, 0xCC],
};
let mut buf = [0u8; 32];
let mut w = SliceWriter::new(&mut buf);
c.encode(&mut w).unwrap();
assert_eq!(w.as_slice(), [0x08, 0x30, 0, 0, 0, 1, 0xAA, 0xBB, 0xCC]);
assert_eq!(CipheredService::from_bytes(w.as_slice()).unwrap(), c);
}
#[test]
fn general_glo_ciphering_names_the_originator() {
let g = GeneralGloCiphering {
system_title: b"MMM\x00\x00\xbcaN",
ciphered: CipheredService {
security_control: SecurityControl::new(0, true, true),
invocation_counter: 5,
payload: &[0x01],
},
};
let mut buf = [0u8; 64];
let mut w = SliceWriter::new(&mut buf);
g.encode(&mut w).unwrap();
assert_eq!(w.as_slice()[0], 8, "system title is length-prefixed");
assert_eq!(GeneralGloCiphering::from_bytes(w.as_slice()).unwrap(), g);
}
#[test]
fn key_info_variants_round_trip() {
for k in [
KeyInfo::Identified { key_id: 0 },
KeyInfo::Wrapped { kek_id: 0, ciphered_key: &[1, 2, 3] },
KeyInfo::Agreed { parameters: &[9], ciphered_key: &[8, 7] },
] {
let mut buf = [0u8; 32];
let mut w = SliceWriter::new(&mut buf);
k.encode(&mut w).unwrap();
assert_eq!(KeyInfo::from_bytes(w.as_slice()).unwrap(), k);
}
}
}