Skip to main content

osdp/reply/
ccrypt.rs

1//! `osdp_CCRYPT` (`0x76`) — client cryptogram (PD response to `osdp_CHLNG`).
2//!
3//! # Spec: §7.16, Annex D.4
4//!
5//! Body: `cUID (8) || RND.B (8) || ClientCryptogram (16)`.
6
7use crate::error::Error;
8use alloc::vec::Vec;
9
10/// `osdp_CCRYPT` body.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct CCrypt {
13    /// PD client identifier (8 bytes).
14    pub cuid: [u8; 8],
15    /// `RND.B` from the PD.
16    pub rnd_b: [u8; 8],
17    /// Client cryptogram = AES_S-ENC( RND.A || RND.B ).
18    pub client_cryptogram: [u8; 16],
19}
20
21impl CCrypt {
22    /// Encode.
23    pub fn encode(&self) -> Result<Vec<u8>, Error> {
24        let mut out = Vec::with_capacity(32);
25        out.extend_from_slice(&self.cuid);
26        out.extend_from_slice(&self.rnd_b);
27        out.extend_from_slice(&self.client_cryptogram);
28        Ok(out)
29    }
30
31    /// Decode.
32    pub fn decode(data: &[u8]) -> Result<Self, Error> {
33        if data.len() != 32 {
34            return Err(Error::MalformedPayload {
35                code: 0x76,
36                reason: "CCRYPT requires 32 bytes",
37            });
38        }
39        let mut cuid = [0u8; 8];
40        cuid.copy_from_slice(&data[..8]);
41        let mut rnd_b = [0u8; 8];
42        rnd_b.copy_from_slice(&data[8..16]);
43        let mut client_cryptogram = [0u8; 16];
44        client_cryptogram.copy_from_slice(&data[16..32]);
45        Ok(Self {
46            cuid,
47            rnd_b,
48            client_cryptogram,
49        })
50    }
51}