use super::{REPLAY_DETECTED, STALE_KEY, UNKNOWN_PATH_SECRET};
use crate::crypto::IntoNonce;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(test, derive(bolero_generator::TypeGenerator))]
pub enum Nonce {
UnknownPathSecret,
StaleKey {
min_key_id: u64,
},
ReplayDetected {
rejected_key_id: u64,
},
}
impl IntoNonce for Nonce {
#[inline]
fn into_nonce(self) -> [u8; 12] {
let mut nonce = [0; 12];
match self {
Self::UnknownPathSecret => {
nonce[0] = UNKNOWN_PATH_SECRET;
}
Self::StaleKey { min_key_id } => {
nonce[0] = STALE_KEY;
nonce[1..9].copy_from_slice(&min_key_id.to_be_bytes());
}
Self::ReplayDetected { rejected_key_id } => {
nonce[0] = REPLAY_DETECTED;
nonce[1..9].copy_from_slice(&rejected_key_id.to_be_bytes());
}
}
nonce
}
}
#[cfg(test)]
mod tests {
use super::*;
use bolero::check;
#[test]
#[cfg_attr(kani, kani::proof, kani::solver(cadical))]
fn nonce_uniqueness() {
check!().with_type::<(Nonce, Nonce)>().for_each(|(a, b)| {
if a == b {
assert_eq!(a.into_nonce(), b.into_nonce());
} else {
assert_ne!(a.into_nonce(), b.into_nonce());
}
});
}
}