Skip to main content

osdp/command/
chlng.rs

1//! `osdp_CHLNG` (`0x76`) โ€” initiate Secure Channel session.
2//!
3//! # Spec: ยง6.17, Annex D.4
4//!
5//! Body is `RND.A` โ€” an 8-byte random challenge generated by the ACU.
6//! The packet must carry an SCB of type `SCS_11`.
7
8use crate::error::Error;
9use alloc::vec::Vec;
10
11/// `osdp_CHLNG` body.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct Chlng {
14    /// `RND.A` โ€” 8 random bytes from the ACU.
15    pub rnd_a: [u8; 8],
16}
17
18impl Chlng {
19    /// New with the given `RND.A`.
20    pub const fn new(rnd_a: [u8; 8]) -> Self {
21        Self { rnd_a }
22    }
23
24    /// Encode.
25    pub fn encode(&self) -> Result<Vec<u8>, Error> {
26        Ok(self.rnd_a.to_vec())
27    }
28
29    /// Decode.
30    pub fn decode(data: &[u8]) -> Result<Self, Error> {
31        if data.len() != 8 {
32            return Err(Error::MalformedPayload {
33                code: 0x76,
34                reason: "CHLNG requires 8-byte RND.A",
35            });
36        }
37        let mut rnd_a = [0u8; 8];
38        rnd_a.copy_from_slice(data);
39        Ok(Self { rnd_a })
40    }
41}