Skip to main content

qualia_core_db/net/qdnf/crypto/handshake/
failures.rs

1//! Integrated handshake failure gate: entropy, 0-RTT/downgrade, and complete-path
2//! classification via `crypto::network::malformed` (not duplicated here).
3
4use crate::crypto::network::errors::CryptoError;
5use crate::crypto::network::pq_handshake::application_data_allowed;
6
7use super::HandshakeState;
8
9/// Qualified profile: 0-RTT application data is never admitted.
10pub fn admit_early_application_data() -> Result<(), CryptoError> {
11    debug_assert!(super::FORBIDDEN_ZERO_RTT);
12    Err(CryptoError::Downgrade)
13}
14
15/// Application data is admitted only after the handshake reaches `Traffic`.
16pub fn qualified_application_data(state: HandshakeState) -> Result<(), CryptoError> {
17    if application_data_allowed(state) {
18        Ok(())
19    } else {
20        Err(CryptoError::Downgrade)
21    }
22}
23
24/// Production application-data gate. Same outcome as [`qualified_application_data`].
25#[inline]
26pub fn qualified_handshake_gate(state: HandshakeState) -> Result<(), CryptoError> {
27    qualified_application_data(state)
28}
29
30/// Fill 32 bytes from the OS CSPRNG. All-zero output or getrandom failure is
31/// [`CryptoError::EntropyFailure`]. Never panics.
32pub fn entropy_fill(out: &mut [u8; 32]) -> Result<(), CryptoError> {
33    match getrandom::fill(out.as_mut_slice()) {
34        Ok(()) => {
35            if out.iter().all(|b| *b == 0) {
36                *out = [0u8; 32];
37                Err(CryptoError::EntropyFailure)
38            } else {
39                Ok(())
40            }
41        }
42        Err(_) => {
43            *out = [0u8; 32];
44            Err(CryptoError::EntropyFailure)
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52    use crate::crypto::network::kem::MlKem768Secret;
53    use crate::crypto::network::malformed::reject_all_zero_dh;
54    use crate::net::qdnf::crypto::handshake::{initiator_share, responder_complete};
55
56    #[test]
57    fn admit_early_application_data_is_downgrade() {
58        assert_eq!(admit_early_application_data(), Err(CryptoError::Downgrade));
59    }
60
61    #[test]
62    fn all_zero_dh_already_rejected() {
63        let (_sk, pk) = MlKem768Secret::generate().unwrap();
64        let i_x = [11u8; 32];
65        let mut ishare = initiator_share(&i_x, &pk).unwrap();
66        ishare.x25519_pk = [0u8; 32];
67        assert_eq!(
68            reject_all_zero_dh(&ishare.x25519_pk),
69            Err(CryptoError::CryptoFailure)
70        );
71        match responder_complete(&ishare, &[12u8; 32]) {
72            Err(e) => assert_eq!(e, CryptoError::CryptoFailure),
73            Ok(_) => panic!("all-zero peer DH public was accepted"),
74        }
75        assert!(initiator_share(&[0u8; 32], &pk).is_err());
76    }
77
78    #[test]
79    fn qualified_application_data_idle_is_downgrade_traffic_is_ok() {
80        assert_eq!(
81            qualified_application_data(HandshakeState::Idle),
82            Err(CryptoError::Downgrade)
83        );
84        assert_eq!(
85            qualified_handshake_gate(HandshakeState::Idle),
86            Err(CryptoError::Downgrade)
87        );
88        assert_eq!(qualified_application_data(HandshakeState::Traffic), Ok(()));
89        assert_eq!(qualified_handshake_gate(HandshakeState::Traffic), Ok(()));
90        assert_eq!(
91            qualified_application_data(HandshakeState::Finished),
92            Err(CryptoError::Downgrade)
93        );
94    }
95
96    #[test]
97    fn entropy_fill_produces_non_all_zero_or_entropy_failure() {
98        let mut out = [0u8; 32];
99        match entropy_fill(&mut out) {
100            Ok(()) => {
101                assert!(out.iter().any(|b| *b != 0));
102            }
103            Err(e) => {
104                assert_eq!(e, CryptoError::EntropyFailure);
105            }
106        }
107    }
108}