ace_server/security_provider.rs
1// region: SecurityError
2
3/// Errors the server state machine may produce during SecurityAccess handling.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum SecurityError {
6 /// The supplied key does not match the generated seed.
7 InvalidKey,
8
9 /// Max failed attempts reached - lockout now active.
10 ExceededAttempts,
11
12 /// Lockout imte has not yet expired.
13 DelayNotExpired,
14}
15
16// endregion: SecurityError
17
18// region: SecurityProvider
19
20/// Provides seed generation and key validation for UDS SecurityAccess (0x27).
21///
22/// The seed/key algorithm is always application-psecific. This trait allows the server to delegate
23/// without knowing the algorithm.
24///
25/// # Security Levels
26///
27/// UDS security levels use odd bytes for Request Seed (0x01, 0x03, 0x05, ...) and the
28/// corresponding even byte for Send Key (0x02, 0x04, 0x06, ...). The `level` parameter is always
29/// the RequestSeed byte (odd).
30///
31/// # Simulation
32///
33/// In DST the implementation should derive seeds from the injected RNG so that the full exchange
34/// is reproducible across simulation runs.
35pub trait SecurityProvider {
36 /// Generates a seed for the given security level.
37 ///
38 /// Writes seed bytes into `buf` and returns the number of bytes written. On real hardware the
39 /// seed must be non-deterministic (hardware RNG). In simulation derive from the seeded
40 /// `ace_sim::rng::Rng`.
41 fn generate_seed(&mut self, level: u8, buf: &mut [u8]) -> Result<usize, SecurityError>;
42
43 /// Validates a key against the previously generated seed.
44 ///
45 /// Returns `Ok(())` if the key is correct.
46 fn validate_key(&self, level: u8, seed: &[u8], key: &[u8]) -> Result<(), SecurityError>;
47}
48
49// endregion: SecurityProvider