Skip to main content

qualia_core_db/net/qdnf/profiles/
cover.rs

1//! Cover traffic, padding classes and relay separation (E16.3).
2//!
3//! Reuses [`super::budget`] padding buckets, cell size and the P3 cover
4//! byte-rate. Cover that is mandatory (the `CoverTraffic` predicate) cannot
5//! be dropped for cost ([`QdnfError::Downgrade`]).
6//!
7//! # Observer / collusion assumptions (not anonymity)
8//!
9//! This profile assumes **one network observer** on the path. Approved
10//! relays are assumed **not colluding** with each other or that observer
11//! under P3. P4 replaces relays with [`ControlPredicate::IsolatedBearer`]
12//! because relay collusion is in-scope for compartmented missions.
13//!
14//! These are threat-model assumptions. They are **not** an anonymity claim.
15//! Byte padding alone does not prove resistance to timing correlation.
16//! [`correlation_resistance_measured`] is false until a separate measurement
17//! harness reports otherwise.
18//!
19//! Remote wipe and attestation cannot guarantee safety after endpoint
20//! compromise or seizure (E16.7).
21
22use super::budget::{
23    pad_to_next_bucket, record_bucket, P1_PADDING_BUCKET, P2_RECORD_BYTES, P3_CELL_BYTES,
24    P3_COVER_BYTES_PER_SEC,
25};
26use super::catalog::{catalog_entry, ControlPredicate, ControlSet, ProtectionProfile};
27use crate::net::qdnf::errors::QdnfError;
28
29/// Padded traffic class. `cover_bps` holds the P3 cover *byte* rate (4 KiB/s)
30/// when the class budgets cover; it is not a bits/s anonymity parameter.
31#[repr(C)]
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
33pub struct CoverClass {
34    pub pad_bucket: u32,
35    pub batch_max: u8,
36    pub cover_bps: u64,
37}
38
39impl CoverClass {
40    pub const NONE: Self = Self {
41        pad_bucket: 0,
42        batch_max: 1,
43        cover_bps: 0,
44    };
45}
46
47/// True when the named catalog profile itself lists `CoverTraffic`.
48/// Catalog P0–P4 do not: P3 cover is optional unless the predicate is added.
49pub fn cover_mandatory(profile: ProtectionProfile) -> bool {
50    catalog_entry(profile).contains_predicate(ControlPredicate::CoverTraffic)
51}
52
53/// Cover required by the selected control set (Recipe F / S13).
54pub fn cover_mandatory_controls(controls: &ControlSet) -> bool {
55    controls.contains_predicate(ControlPredicate::CoverTraffic)
56}
57
58/// Dropping cover because of cost. Mandatory cover is a downgrade, not a
59/// cheaper compatible profile.
60pub fn drop_cover_for_cost(profile: ProtectionProfile) -> Result<(), QdnfError> {
61    if cover_mandatory(profile) {
62        Err(QdnfError::Downgrade)
63    } else {
64        Ok(())
65    }
66}
67
68/// `true` when cover may drop for controls.
69pub fn drop_cover_for_controls(controls: &ControlSet) -> Result<(), QdnfError> {
70    if cover_mandatory_controls(controls) {
71        Err(QdnfError::Downgrade)
72    } else {
73        Ok(())
74    }
75}
76
77/// Cover class.
78pub fn cover_class(profile: ProtectionProfile) -> CoverClass {
79    let pad_bucket = record_bucket(profile) as u32;
80    match profile {
81        ProtectionProfile::P0 => CoverClass::NONE,
82        ProtectionProfile::P1 => CoverClass {
83            pad_bucket,
84            batch_max: 1,
85            cover_bps: 0,
86        },
87        ProtectionProfile::P2 => CoverClass {
88            pad_bucket,
89            batch_max: 4,
90            cover_bps: 0,
91        },
92        ProtectionProfile::P3 | ProtectionProfile::P4 => CoverClass {
93            pad_bucket,
94            batch_max: 8,
95            cover_bps: P3_COVER_BYTES_PER_SEC,
96        },
97    }
98}
99
100/// P3 lists ApprovedRelay. P4 uses IsolatedBearer instead of remote relays.
101pub fn approved_relay_required(profile: ProtectionProfile) -> bool {
102    catalog_entry(profile).contains_predicate(ControlPredicate::ApprovedRelay)
103}
104
105/// `CoverClass::isolated_bearer_required`.
106pub fn isolated_bearer_required(profile: ProtectionProfile) -> bool {
107    catalog_entry(profile).contains_predicate(ControlPredicate::IsolatedBearer)
108}
109
110/// Honest: no correlation-resistance campaign has been measured here.
111pub fn correlation_resistance_measured() -> bool {
112    false
113}
114
115/// Test oracle: observed cover/pad length for `real_len`.
116///
117/// Intra-bucket application lengths collapse to the pad bucket. Cover
118/// classes with a byte-rate (P3/P4) always emit the cell size when the
119/// application fits in one cell, so slack inside the cell is not a length
120/// oracle.
121pub fn cover_oracle_observed_bytes(
122    profile: ProtectionProfile,
123    real_len: u64,
124) -> Result<u64, QdnfError> {
125    let class = cover_class(profile);
126    if class.pad_bucket == 0 {
127        return Ok(real_len);
128    }
129    let padded = pad_to_next_bucket(real_len, class.pad_bucket as u64)?;
130    if class.cover_bps == 0 {
131        return Ok(padded);
132    }
133    if real_len <= class.pad_bucket as u64 {
134        Ok(class.pad_bucket as u64)
135    } else {
136        Ok(padded)
137    }
138}
139
140/// `CoverClass::p1_padding_bucket`.
141pub const fn p1_padding_bucket() -> u64 {
142    P1_PADDING_BUCKET
143}
144
145/// `CoverClass::p3_cell_bytes`.
146pub const fn p3_cell_bytes() -> u64 {
147    P3_CELL_BYTES
148}
149
150/// `CoverClass::p2_record_bytes`.
151pub const fn p2_record_bytes() -> u64 {
152    P2_RECORD_BYTES
153}
154
155#[cfg(test)]
156mod tests {
157    use super::*;
158    use crate::net::qdnf::profiles::budget::estimate_overhead;
159    use crate::net::qdnf::profiles::catalog::control_set_from_predicates;
160
161    #[test]
162    fn catalog_profiles_do_not_mandate_cover() {
163        assert!(!cover_mandatory(ProtectionProfile::P0));
164        assert!(!cover_mandatory(ProtectionProfile::P1));
165        assert!(!cover_mandatory(ProtectionProfile::P2));
166        assert!(!cover_mandatory(ProtectionProfile::P3));
167        assert!(!cover_mandatory(ProtectionProfile::P4));
168        assert_eq!(drop_cover_for_cost(ProtectionProfile::P3), Ok(()));
169    }
170
171    #[test]
172    fn mandatory_cover_cannot_be_dropped_for_cost() {
173        let controls = control_set_from_predicates(
174            ProtectionProfile::P3,
175            &[
176                ControlPredicate::Authenticity,
177                ControlPredicate::CoverTraffic,
178            ],
179        )
180        .unwrap();
181        assert!(cover_mandatory_controls(&controls));
182        assert_eq!(
183            drop_cover_for_controls(&controls),
184            Err(QdnfError::Downgrade)
185        );
186        assert_eq!(
187            estimate_overhead(ProtectionProfile::P3, &controls, 1, 1000, 0, true),
188            Err(QdnfError::Downgrade)
189        );
190    }
191
192    #[test]
193    fn cover_class_reuses_budget_buckets() {
194        let p1 = cover_class(ProtectionProfile::P1);
195        assert_eq!(p1.pad_bucket, P1_PADDING_BUCKET as u32);
196        assert_eq!(p1.batch_max, 1);
197        assert_eq!(p1.cover_bps, 0);
198        let p3 = cover_class(ProtectionProfile::P3);
199        assert_eq!(p3.pad_bucket, P3_CELL_BYTES as u32);
200        assert_eq!(p3.batch_max, 8);
201        assert_eq!(p3.cover_bps, P3_COVER_BYTES_PER_SEC);
202        let p4 = cover_class(ProtectionProfile::P4);
203        assert_eq!(p4.pad_bucket, p3.pad_bucket);
204        assert_eq!(p4.cover_bps, P3_COVER_BYTES_PER_SEC);
205    }
206
207    #[test]
208    fn p3_requires_approved_relay_p4_isolated() {
209        assert!(approved_relay_required(ProtectionProfile::P3));
210        assert!(!isolated_bearer_required(ProtectionProfile::P3));
211        assert!(!approved_relay_required(ProtectionProfile::P4));
212        assert!(isolated_bearer_required(ProtectionProfile::P4));
213    }
214
215    #[test]
216    fn correlation_resistance_is_unmeasured() {
217        assert!(!correlation_resistance_measured());
218    }
219
220    #[test]
221    fn cover_oracle_does_not_leak_real_length_class() {
222        let a = 17u64;
223        let b = 200u64;
224        assert_ne!(a, b);
225        let p1_a = cover_oracle_observed_bytes(ProtectionProfile::P1, a).unwrap();
226        let p1_b = cover_oracle_observed_bytes(ProtectionProfile::P1, b).unwrap();
227        assert_eq!(p1_a, p1_b);
228        assert_eq!(p1_a, P1_PADDING_BUCKET);
229        let p3_a = cover_oracle_observed_bytes(ProtectionProfile::P3, 1).unwrap();
230        let p3_b = cover_oracle_observed_bytes(ProtectionProfile::P3, 4000).unwrap();
231        assert_eq!(p3_a, p3_b);
232        assert_eq!(p3_a, P3_CELL_BYTES);
233        assert_ne!(
234            cover_oracle_observed_bytes(ProtectionProfile::P1, 17).unwrap(),
235            cover_oracle_observed_bytes(ProtectionProfile::P1, 300).unwrap()
236        );
237    }
238}