pub const RECOVERY_EXPORT_MAGIC: u32 = 0x5245_5850; pub const RECOVERY_EXPORT_VERSION: u16 = 1; pub const DIAGNOSTIC_LOG_LIMIT: u32 = 64; pub const REDACTION_MASK_SENTINEL: u64 = 0x5245_4441_4354_4544; #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum DiagnosticOutcome { Success = 1, Failure = 2, } #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ExportPhase { Idle = 0, Refused = 1, Exported = 2, } #[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ExportError { InvalidMagic = 1, UnsupportedVersion = 2, ZeroBootEpoch = 3, ZeroActiveSlotId = 4, ZeroToolBinaryHash = 5, ZeroKernelImageHash = 6, MismatchedImageHash = 7, ZeroDiagnosticBounds = 8, DiagnosticBoundsExceeded = 9, PersonalSecretUnredacted = 10, InvalidRedactionSentinel = 11, ZeroLogDigest = 12, MissingOutcomeCoverage = 13, UnboundReferenceDigest = 14, AlreadyExported = 15, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct DiagnosticLogEntry { pub sequence: u32, pub outcome: DiagnosticOutcome, pub subsystem_code: u32, pub contains_personal_secret: bool, pub redacted_field_mask: u64, pub entry_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct DiagnosticExportRequest { pub magic: u32, pub version: u16, pub active_slot_id: u16, pub boot_epoch: u64, pub tool_binary_hash: u64, pub claimed_image_hash: u64, pub expected_image_hash: u64, pub entry_count: u32, pub max_entries_bound: u32, pub success_entry: DiagnosticLogEntry, pub failure_entry: DiagnosticLogEntry, pub reference_binding_digest: u64, } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct DiagnosticExportBundle { pub boot_epoch: u64, pub active_slot_id: u16, pub entry_count: u32, pub max_entries_bound: u32, pub redacted_entries: u32, pub tool_binary_hash: u64, pub verified_image_hash: u64, pub reference_binding_digest: u64, pub bundle_digest: u64, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ExportStatus { pub phase: ExportPhase, pub exported: bool, pub active_slot_id: u16, pub boot_epoch: u64, pub entry_count: u32, pub max_entries_bound: u32, pub redacted_entries: u32, pub reference_binding_digest: u64, pub bundle_digest: u64, pub secret_refusals: u64, pub image_hash_refusals: u64, pub reference_refusals: u64, pub rejected_total: u64, pub exported_total: u64, } pub const fn compute_reference_binding( boot_epoch: u64, active_slot_id: u16, tool_binary_hash: u64, image_hash: u64, max_entries_bound: u32, ) -> u64 { let mut h: u64 = 0xCBF2_9CE4_8422_2325; h = (h ^ boot_epoch).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (active_slot_id as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ tool_binary_hash).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ image_hash).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (max_entries_bound as u64)).wrapping_mul(0x0000_0100_0000_01B3); h } pub const fn compute_export_bundle_digest( reference_binding_digest: u64, success_digest: u64, failure_digest: u64, redacted_entries: u32, entry_count: u32, ) -> u64 { let mut h = reference_binding_digest ^ 0x9E37_79B9_7F4A_7C15; h = (h ^ success_digest).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ failure_digest).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (redacted_entries as u64)).wrapping_mul(0x0000_0100_0000_01B3); h = (h ^ (entry_count as u64)).wrapping_mul(0x0000_0100_0000_01B3); h } pub fn validate_log_redaction(entry: &DiagnosticLogEntry) -> Result { if entry.entry_digest == 0 { return Err(ExportError::ZeroLogDigest); } if entry.contains_personal_secret { return Err(ExportError::PersonalSecretUnredacted); } if entry.redacted_field_mask != REDACTION_MASK_SENTINEL { return Err(ExportError::InvalidRedactionSentinel); } Ok(true) } pub fn export_diagnostic_bundle( req: &DiagnosticExportRequest, ) -> Result { if req.magic != RECOVERY_EXPORT_MAGIC { return Err(ExportError::InvalidMagic); } if req.version != RECOVERY_EXPORT_VERSION { return Err(ExportError::UnsupportedVersion); } if req.boot_epoch == 0 { return Err(ExportError::ZeroBootEpoch); } if req.active_slot_id == 0 { return Err(ExportError::ZeroActiveSlotId); } if req.tool_binary_hash == 0 { return Err(ExportError::ZeroToolBinaryHash); } if req.expected_image_hash == 0 { return Err(ExportError::ZeroKernelImageHash); } if req.claimed_image_hash != req.expected_image_hash { return Err(ExportError::MismatchedImageHash); } if req.max_entries_bound == 0 || req.entry_count == 0 { return Err(ExportError::ZeroDiagnosticBounds); } if req.max_entries_bound > DIAGNOSTIC_LOG_LIMIT || req.entry_count > req.max_entries_bound { return Err(ExportError::DiagnosticBoundsExceeded); } if req.success_entry.outcome != DiagnosticOutcome::Success || req.failure_entry.outcome != DiagnosticOutcome::Failure { return Err(ExportError::MissingOutcomeCoverage); } let s_redacted = validate_log_redaction(&req.success_entry)?; let f_redacted = validate_log_redaction(&req.failure_entry)?; let expected_ref = compute_reference_binding( req.boot_epoch, req.active_slot_id, req.tool_binary_hash, req.expected_image_hash, req.max_entries_bound, ); if req.reference_binding_digest != expected_ref { return Err(ExportError::UnboundReferenceDigest); } let redacted_entries = (s_redacted as u32) + (f_redacted as u32); let bundle_digest = compute_export_bundle_digest( expected_ref, req.success_entry.entry_digest, req.failure_entry.entry_digest, redacted_entries, req.entry_count, ); Ok(DiagnosticExportBundle { boot_epoch: req.boot_epoch, active_slot_id: req.active_slot_id, entry_count: req.entry_count, max_entries_bound: req.max_entries_bound, redacted_entries, tool_binary_hash: req.tool_binary_hash, verified_image_hash: req.expected_image_hash, reference_binding_digest: expected_ref, bundle_digest, }) } pub struct RecoveryDiagnosticExporter { phase: ExportPhase, exported: bool, active_slot_id: u16, boot_epoch: u64, max_entries_bound: u32, committed: Option, secret_refusals: u64, image_hash_refusals: u64, reference_refusals: u64, rejected_total: u64, exported_total: u64, } impl RecoveryDiagnosticExporter { pub const fn new(boot_epoch: u64, active_slot_id: u16, max_entries_bound: u32) -> Self { Self { phase: ExportPhase::Idle, exported: false, active_slot_id, boot_epoch, max_entries_bound, committed: None, secret_refusals: 0, image_hash_refusals: 0, reference_refusals: 0, rejected_total: 0, exported_total: 0, } } pub fn export( &mut self, req: &DiagnosticExportRequest, ) -> Result { if self.exported { self.rejected_total += 1; return Err(ExportError::AlreadyExported); } match export_diagnostic_bundle(req) { Ok(bundle) => { self.phase = ExportPhase::Exported; self.exported = true; self.exported_total += 1; self.committed = Some(bundle); Ok(bundle) } Err(e) => { self.phase = ExportPhase::Refused; self.rejected_total += 1; match e { ExportError::PersonalSecretUnredacted | ExportError::InvalidRedactionSentinel => { self.secret_refusals += 1; } ExportError::MismatchedImageHash | ExportError::ZeroKernelImageHash => { self.image_hash_refusals += 1; } _ => { self.reference_refusals += 1; } } Err(e) } } } pub fn committed_bundle(&self) -> Option { self.committed } pub fn status(&self) -> ExportStatus { let (entry_count, redacted_entries, reference_binding_digest, bundle_digest) = match self.committed { Some(b) => ( b.entry_count, b.redacted_entries, b.reference_binding_digest, b.bundle_digest, ), None => (0, 0, 0, 0), }; ExportStatus { phase: self.phase, exported: self.exported, active_slot_id: self.active_slot_id, boot_epoch: self.boot_epoch, entry_count, max_entries_bound: self.max_entries_bound, redacted_entries, reference_binding_digest, bundle_digest, secret_refusals: self.secret_refusals, image_hash_refusals: self.image_hash_refusals, reference_refusals: self.reference_refusals, rejected_total: self.rejected_total, exported_total: self.exported_total, } } }