acex_server/security_provider.rs
1// region: SecurityError
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5pub struct SeedGenerationError;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9pub struct InvalidKeyError;
10
11// endregion: SecurityError
12
13// region: SecurityProvider
14
15/// Provides seed generation and key validation for UDS SecurityAccess (0x27).
16///
17/// The seed/key algorithm is always application-psecific. This trait allows the server to delegate
18/// without knowing the algorithm.
19///
20/// # Security Levels
21///
22/// UDS security levels use odd bytes for Request Seed (0x01, 0x03, 0x05, ...) and the
23/// corresponding even byte for Send Key (0x02, 0x04, 0x06, ...). The `level` parameter is always
24/// the RequestSeed byte (odd).
25///
26/// # Simulation
27///
28/// In DST the implementation should derive seeds from the injected RNG so that the full exchange
29/// is reproducible across simulation runs.
30pub trait SecurityProvider: Clone {
31 /// Generates a seed for the given security level.
32 ///
33 /// Writes seed bytes into `buf` and returns the number of bytes written. On real hardware the
34 /// seed must be non-deterministic (hardware RNG). In simulation derive from the seeded
35 /// `acex_sim::rng::Rng`.
36 fn generate_seed(&mut self, level: u8, buf: &mut [u8]) -> Result<usize, SeedGenerationError>;
37
38 /// Validates a key against the previously generated seed.
39 ///
40 /// Returns `Ok(())` if the key is correct.
41 fn validate_key(&mut self, level: u8, seed: &[u8], key: &[u8]) -> Result<(), InvalidKeyError>;
42}
43
44// endregion: SecurityProvider