qualia_core_db/crypto/network/
malformed.rs1use crate::crypto::network::dual_sign::DualProof;
24use crate::crypto::network::errors::CryptoError;
25use crate::crypto::network::pq_handshake::transition;
26use crate::crypto::network::types::ML_KEM_768_CT_LEN;
27use crate::net::qdnf::crypto::handshake::HandshakeState;
28
29#[inline]
31pub fn reject_truncated(got: usize, need: usize) -> Result<(), CryptoError> {
32 if got < need {
33 Err(CryptoError::Truncated)
34 } else {
35 Ok(())
36 }
37}
38
39#[inline]
44pub fn reject_noncanonical_share(got: usize, need: usize) -> Result<(), CryptoError> {
45 if got != need {
46 Err(CryptoError::Malformed)
47 } else {
48 Ok(())
49 }
50}
51
52#[inline]
54pub fn reject_unknown_critical(critical_tag: u16) -> Result<(), CryptoError> {
55 if critical_tag != 0 {
56 Err(CryptoError::CriticalExtension)
57 } else {
58 Ok(())
59 }
60}
61
62#[inline]
63fn is_all_zero(bytes: &[u8]) -> bool {
64 let mut i = 0usize;
65 while i < bytes.len() {
66 if bytes[i] != 0 {
67 return false;
68 }
69 i += 1;
70 }
71 true
72}
73
74#[inline]
76pub fn reject_stripped_dual(proof: &DualProof) -> Result<(), CryptoError> {
77 if is_all_zero(&proof.mldsa_sig) || is_all_zero(&proof.ed25519_sig) {
78 Err(CryptoError::Unauthorized)
79 } else {
80 Ok(())
81 }
82}
83
84#[inline]
86pub fn reject_all_zero_dh(peer_pk: &[u8; 32]) -> Result<(), CryptoError> {
87 if is_all_zero(peer_pk) {
88 Err(CryptoError::CryptoFailure)
89 } else {
90 Ok(())
91 }
92}
93
94#[inline]
96pub fn reject_malformed_kem_ct(len: usize) -> Result<(), CryptoError> {
97 if len < ML_KEM_768_CT_LEN {
98 Err(CryptoError::Truncated)
99 } else if len > ML_KEM_768_CT_LEN {
100 Err(CryptoError::Malformed)
101 } else {
102 Ok(())
103 }
104}
105
106#[inline]
111pub fn reject_reordered_flight(
112 from: HandshakeState,
113 to: HandshakeState,
114) -> Result<(), CryptoError> {
115 transition(from, to).map(|_| ())
116}
117
118#[inline]
120pub fn fail_closed_on_malformed() -> bool {
121 true
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127 use crate::crypto::network::dual_sign::{sign_dual, verify_dual};
128 use crate::crypto::network::ed25519::public_from_seed;
129 use crate::crypto::network::mldsa::generate_keypair;
130 use crate::crypto::network::pq_handshake::{
131 application_data_allowed, decode_initiator_share, INITIATOR_SHARE_WIRE_LEN,
132 };
133 use crate::crypto::network::share_encoding::{
134 decode_responder_share, encode_responder_share, RESPONDER_SHARE_WIRE_LEN,
135 };
136 use crate::crypto::network::types::{
137 ED25519_SIG_LEN, ML_DSA_65_SIG_LEN, ML_KEM_768_CT_LEN, X25519_LEN,
138 };
139 use crate::crypto::network::x25519::X25519Secret;
140
141 #[test]
142 fn truncated_initiator_share_is_truncated() {
143 let need = INITIATOR_SHARE_WIRE_LEN;
144 let got = need - 1;
145 assert_eq!(reject_truncated(got, need), Err(CryptoError::Truncated));
146 let short = [0u8; INITIATOR_SHARE_WIRE_LEN - 1];
147 match decode_initiator_share(&short) {
148 Err(e) => assert_eq!(e, CryptoError::Truncated),
149 Ok(_) => panic!("truncated initiator share decoded"),
150 }
151 assert_eq!(reject_truncated(need, need), Ok(()));
152 }
153
154 #[test]
155 fn extra_trailing_byte_on_exact_share_is_malformed() {
156 let mut ct = [0u8; ML_KEM_768_CT_LEN];
157 let mut pk = [0u8; X25519_LEN];
158 ct[0] = 0xC1;
159 pk[0] = 0x11;
160 let mut wire = [0u8; RESPONDER_SHARE_WIRE_LEN];
161 let n = encode_responder_share(&ct, &pk, &mut wire).unwrap();
162 assert_eq!(n, RESPONDER_SHARE_WIRE_LEN);
163 assert_eq!(
164 reject_noncanonical_share(n, RESPONDER_SHARE_WIRE_LEN),
165 Ok(())
166 );
167
168 let mut extra = [0u8; RESPONDER_SHARE_WIRE_LEN + 1];
169 extra[..RESPONDER_SHARE_WIRE_LEN].copy_from_slice(&wire);
170 extra[RESPONDER_SHARE_WIRE_LEN] = 0x99;
171 assert_eq!(
172 reject_noncanonical_share(extra.len(), RESPONDER_SHARE_WIRE_LEN),
173 Err(CryptoError::Malformed)
174 );
175 assert_eq!(decode_responder_share(&extra), Err(CryptoError::Malformed));
176
177 let mut init_extra = [0u8; INITIATOR_SHARE_WIRE_LEN + 1];
178 init_extra[INITIATOR_SHARE_WIRE_LEN] = 0xAA;
179 match decode_initiator_share(&init_extra) {
180 Err(e) => assert_eq!(e, CryptoError::Malformed),
181 Ok(_) => panic!("oversize initiator share decoded"),
182 }
183 }
184
185 #[test]
186 fn unknown_critical_tag() {
187 assert_eq!(reject_unknown_critical(0), Ok(()));
188 assert_eq!(
189 reject_unknown_critical(0x99),
190 Err(CryptoError::CriticalExtension)
191 );
192 }
193
194 #[test]
195 fn stripped_ed25519_half_is_unauthorized() {
196 let mut proof = DualProof {
197 mldsa_sig: [0u8; ML_DSA_65_SIG_LEN],
198 ed25519_sig: [0u8; ED25519_SIG_LEN],
199 };
200 proof.mldsa_sig[0] = 0x01;
201 assert_eq!(reject_stripped_dual(&proof), Err(CryptoError::Unauthorized));
202
203 let (sk, pk) = generate_keypair().unwrap();
204 let seed = [7u8; 32];
205 let ed_pk = public_from_seed(&seed);
206 sign_dual(&sk, &seed, b"qdnf-malformed", b"qlink", &mut proof).unwrap();
207 assert_eq!(reject_stripped_dual(&proof), Ok(()));
208 assert!(verify_dual(&pk, &ed_pk, b"qdnf-malformed", b"qlink", &proof).is_ok());
209
210 proof.ed25519_sig = [0u8; ED25519_SIG_LEN];
211 assert_eq!(reject_stripped_dual(&proof), Err(CryptoError::Unauthorized));
212 assert_eq!(
213 verify_dual(&pk, &ed_pk, b"qdnf-malformed", b"qlink", &proof),
214 Err(CryptoError::Unauthorized)
215 );
216 }
217
218 #[test]
219 fn all_zero_dh_pk_is_crypto_failure() {
220 let zero = [0u8; 32];
221 assert_eq!(reject_all_zero_dh(&zero), Err(CryptoError::CryptoFailure));
222 let sk = X25519Secret::from_bytes([1u8; 32]);
223 assert_eq!(sk.diffie_hellman(&zero), Err(CryptoError::CryptoFailure));
224
225 let peer = X25519Secret::from_bytes([2u8; 32]).public();
226 assert_eq!(reject_all_zero_dh(&peer), Ok(()));
227 assert!(sk.diffie_hellman(&peer).is_ok());
228 }
229
230 #[test]
231 fn malformed_kem_ct_length() {
232 assert_eq!(reject_malformed_kem_ct(0), Err(CryptoError::Truncated));
233 assert_eq!(
234 reject_malformed_kem_ct(ML_KEM_768_CT_LEN + 1),
235 Err(CryptoError::Malformed)
236 );
237 assert_eq!(reject_malformed_kem_ct(ML_KEM_768_CT_LEN), Ok(()));
238 }
239
240 #[test]
241 fn idle_to_traffic_is_downgrade() {
242 assert_eq!(
243 reject_reordered_flight(HandshakeState::Idle, HandshakeState::Traffic),
244 Err(CryptoError::Downgrade)
245 );
246 assert_eq!(
247 transition(HandshakeState::Idle, HandshakeState::Traffic),
248 Err(CryptoError::Downgrade)
249 );
250 }
251
252 #[test]
253 fn proofs_to_reachability_is_unauthorized() {
254 assert_eq!(
255 reject_reordered_flight(HandshakeState::Proofs, HandshakeState::Reachability),
256 Err(CryptoError::Unauthorized)
257 );
258 assert_eq!(
259 transition(HandshakeState::Proofs, HandshakeState::Reachability),
260 Err(CryptoError::Unauthorized)
261 );
262 }
263
264 #[test]
265 fn fail_closed_on_malformed_is_true() {
266 assert!(fail_closed_on_malformed());
267 }
268
269 #[test]
270 fn zero_rtt_idle_application_data_forbidden() {
271 assert!(!application_data_allowed(HandshakeState::Idle));
272 }
273}