e2e-protection 0.4.0

End-to-End protection core with pluggable profiles. AUTOSAR profile family is optional via feature
Documentation
//! Profile 7m (Method-call style over Ethernet)
//!
//! **Intent (simplified)**:
//! - **Counter**: 32-bit
//! - **DataID**: 32-bit explicit
//! - **Meta**: 32-bit (2 bits Type, 2 bits Result, 28 bits SourceID)
//! - **CRC**: 64-bit CRC-64/ECMA
//! - **Length**: 32-bit
//!
//! **Frame layout (educational mapping)**:
//! ```text
//! [ payload ... | Length(4B) | Counter(4B) | DataID(4B) | Meta(4B) | CRC64(8B) ]
//! ```

use crc::{Crc, CRC_64_ECMA_182};

use crate::e2e::{CheckInfo, E2eError, E2eProfile};

const CRC64_ECMA: Crc<u64> = Crc::<u64>::new(&CRC_64_ECMA_182);

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct P7m {
    pub data_id: u32,
    pub msg_type: u8,
    pub msg_result: u8,
    pub source_id: u32,
}

impl E2eProfile for P7m {
    fn protect(&self, payload: &[u8], counter: u32) -> Result<Vec<u8>, E2eError> {
        let meta = ((self.msg_type as u32 & 0x3) << 30)
                 | ((self.msg_result as u32 & 0x3) << 28)
                 | (self.source_id & 0x0FFF_FFFF);

        let mut out = Vec::with_capacity(payload.len() + 24);
        out.extend_from_slice(payload);
        out.extend_from_slice(&(payload.len() as u32).to_be_bytes());
        out.extend_from_slice(&counter.to_be_bytes());
        out.extend_from_slice(&self.data_id.to_be_bytes());
        out.extend_from_slice(&meta.to_be_bytes());
        let crc = CRC64_ECMA.checksum(&out);
        out.extend_from_slice(&crc.to_be_bytes());
        Ok(out)
    }

    fn check(&self, frame: &[u8]) -> Result<CheckInfo, E2eError> {
        if frame.len() < 24 { return Err(E2eError::Length); }
        let crc_recv = u64::from_be_bytes(frame[frame.len()-8..].try_into().unwrap());
        let without_crc = &frame[..frame.len()-8];
        let want = CRC64_ECMA.checksum(without_crc);
        if want != crc_recv { return Err(E2eError::Crc); }

        // tail = last 16 bytes of without_crc
        let tail = &without_crc[without_crc.len()-16..];
        let len = u32::from_be_bytes(tail[0..4].try_into().unwrap()) as usize;
        let counter = u32::from_be_bytes(tail[4..8].try_into().unwrap());
        let data_id = u32::from_be_bytes(tail[8..12].try_into().unwrap());
        let meta = u32::from_be_bytes(tail[12..16].try_into().unwrap());

        if data_id != self.data_id { return Err(E2eError::DataId); }
        if (meta >> 30) != (self.msg_type & 0x3) as u32 { return Err(E2eError::DataId); }
        if ((meta >> 28) & 0x3) != (self.msg_result & 0x3) as u32 { return Err(E2eError::DataId); }
        if (meta & 0x0FFF_FFFF) != (self.source_id & 0x0FFF_FFFF) { return Err(E2eError::DataId); }

        let payload_len = without_crc.len() - 16;
        if len != payload_len { return Err(E2eError::Length); }

        Ok(CheckInfo { counter, ok: true })
    }

    fn payload_len(&self, frame: &[u8]) -> Option<usize> {
        frame.len().checked_sub(24)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{E2eProfile, E2eSession, CounterPolicy};

    #[test]
    fn p7m_roundtrip() {
        let mut tx = E2eSession::new(P7m{data_id:0x42, msg_type:2, msg_result:1, source_id:0x00AB_CDEF},
            CounterPolicy::Rollover{bits:32,step:1});
        let f = tx.wrap(&[0x55; 8]).unwrap();

        let mut rx = E2eSession::new(P7m{data_id:0x42, msg_type:2, msg_result:1, source_id:0x00AB_CDEF},
            CounterPolicy::Rollover{bits:32,step:1});
        let pl = rx.unwrap(&f).unwrap();
        assert_eq!(pl, &[0x55; 8]);
    }

    #[test]
    fn p7m_crc_detects_error() {
        let p = P7m{data_id:1, msg_type:0, msg_result:0, source_id:0};
        let mut f = p.protect(&[1,2,3,4,5], 123).unwrap();
        f[2] ^= 0x80;
        assert!(matches!(p.check(&f), Err(E2eError::Crc)));
    }
}