Download src/main.rs from Sahek/ubda-engine: direct link, hf CLI and curl.
- Browser
- Download file 7.34 kB
-
https://huggingface.co/datasets/Sahek/ubda-engine/resolve/main/src/main.rs
- Command line
-
hf download hf://datasets/Sahek/ubda-engine/src/main.rs
-
curl -L -o main.rs https://huggingface.co/datasets/Sahek/ubda-engine/resolve/main/src/main.rs
7.34 kB
| mod bte_interface; | |
| mod hardware_authorizer; | |
| mod key_broker; | |
| mod replay_store; | |
| mod state_machine; | |
| mod types; | |
| use bte_interface::BehavioralTrustEngine; | |
| use hardware_authorizer::{KeyAuthority, MockHardwareAuthorizer}; | |
| use key_broker::KeyBroker; | |
| use state_machine::SystemStateMachine; | |
| use types::*; | |
| fn main() { | |
| println!("=== UBDA V1.2-alpha Architectural Refactor Integration Test ==="); | |
| let mut state_machine = SystemStateMachine::new(0.70); | |
| let mock_authorizer = MockHardwareAuthorizer::new(); | |
| let mut key_broker = KeyBroker::new(&mock_authorizer); | |
| let bte = BehavioralTrustEngine::new(0.70); | |
| let session_id = "sess_ubda_v1_alpha"; | |
| let now = chrono::Utc::now().timestamp(); | |
| // 1. Establish State T0 -> T1 -> T2 | |
| println!("\n[1/7] Transitioning State: T0 -> T1 -> T2..."); | |
| state_machine.handle_device_authenticated().unwrap(); | |
| let telemetry = BehavioralTelemetry { | |
| anomaly_score: 0.10, | |
| cadence_entropy: 0.90, | |
| spatial_risk_factor: 0.02, | |
| timestamp: now, | |
| }; | |
| let assertion = | |
| bte.process_telemetry(session_id, telemetry.clone(), state_machine.current_state()); | |
| state_machine | |
| .handle_behavioral_assertion(&telemetry) | |
| .unwrap(); | |
| assert_eq!( | |
| state_machine.current_state(), | |
| TrustState::BehavioralContinuity | |
| ); | |
| println!(" -> Active State: T2 (BehavioralContinuity)"); | |
| // 2. Reject Insufficient Classification (T2 requesting D2 data) | |
| println!("\n[2/7] Testing Classification Policy Enforcement (T2 requesting D2 data)..."); | |
| let d2_issuance = mock_authorizer.issue_dac( | |
| &assertion, | |
| "financial_records.enc".to_string(), | |
| DataClassification::D2, // Requires T3 | |
| AccessOperation::Read, | |
| now, | |
| 300, | |
| 2001, | |
| ); | |
| match d2_issuance { | |
| Err(hardware_authorizer::AuthorizerError::InsufficientTrustState { .. }) => { | |
| println!(" -> PASS: Authorizer blocked D2 issuance under T2 trust level."); | |
| } | |
| _ => panic!(" -> FAIL: D2 capability improperly issued under T2 state!"), | |
| } | |
| // 3. Valid D1 Capability Cycle | |
| println!("\n[3/7] Issuing & Consuming Valid D1 Capability..."); | |
| let valid_dac = mock_authorizer | |
| .issue_dac( | |
| &assertion, | |
| "personal_document.enc".to_string(), | |
| DataClassification::D1, // Requires T2 | |
| AccessOperation::Read, | |
| now, | |
| 300, | |
| 2002, | |
| ) | |
| .expect("D1 DAC issuance failed"); | |
| let key = key_broker | |
| .execute_key_use( | |
| &valid_dac, | |
| session_id, | |
| AccessOperation::Read, | |
| now, | |
| state_machine.current_state(), | |
| ) | |
| .expect("Key derivation failed"); | |
| println!( | |
| " -> PASS: Derived Ephemeral Key (Digest: {:?})", | |
| &key.key_bytes[0..4] | |
| ); | |
| // 4. Anti-Replay Enforcement | |
| println!("\n[4/7] Testing Replay Defense..."); | |
| let replay_result = key_broker.execute_key_use( | |
| &valid_dac, | |
| session_id, | |
| AccessOperation::Read, | |
| now + 1, | |
| state_machine.current_state(), | |
| ); | |
| match replay_result { | |
| Err(key_broker::KeyBrokerError::ReplayDetected) => { | |
| println!(" -> PASS: Key Broker blocked consumed DAC replay attempt."); | |
| } | |
| _ => panic!(" -> FAIL: Replayed DAC was accepted!"), | |
| } | |
| // 5. Signature Tamper Verification | |
| println!("\n[5/7] Testing Signature Integrity Verification..."); | |
| let mut tampered_dac = mock_authorizer | |
| .issue_dac( | |
| &assertion, | |
| "personal_document_2.enc".to_string(), | |
| DataClassification::D1, | |
| AccessOperation::Read, | |
| now, | |
| 300, | |
| 2003, | |
| ) | |
| .unwrap(); | |
| // Field modification by attacker | |
| tampered_dac.required_state = TrustState::CriticalElevation; | |
| let tamper_result = key_broker.execute_key_use( | |
| &tampered_dac, | |
| session_id, | |
| AccessOperation::Read, | |
| now, | |
| TrustState::CriticalElevation, | |
| ); | |
| match tamper_result { | |
| Err(key_broker::KeyBrokerError::InvalidSignature) => { | |
| println!( | |
| " -> PASS: Key Broker detected field modification via canonical verification." | |
| ); | |
| } | |
| _ => panic!(" -> FAIL: Tampered capability signature validated!"), | |
| } | |
| // 6. Full Elevation to T3/T4 and Issuing High-Classification Capabilities | |
| println!("\n[6/7] Elevating T2 -> T3 -> T4 and Issuing D2/D3 Capabilities..."); | |
| state_machine.handle_step_up_auth().unwrap(); | |
| assert_eq!(state_machine.current_state(), TrustState::HighAssurance); | |
| println!(" -> Active State: T3 (HighAssurance)"); | |
| let d2_assertion = | |
| bte.process_telemetry(session_id, telemetry.clone(), state_machine.current_state()); | |
| let d2_dac = mock_authorizer | |
| .issue_dac( | |
| &d2_assertion, | |
| "financial_records.enc".to_string(), | |
| DataClassification::D2, | |
| AccessOperation::Read, | |
| now, | |
| 300, | |
| 2004, | |
| ) | |
| .expect("D2 DAC issuance failed at T3"); | |
| key_broker | |
| .execute_key_use( | |
| &d2_dac, | |
| session_id, | |
| AccessOperation::Read, | |
| now, | |
| state_machine.current_state(), | |
| ) | |
| .expect("D2 key derivation failed at T3"); | |
| println!(" -> PASS: Issued & consumed D2 capability under T3 trust level."); | |
| state_machine.handle_critical_elevation().unwrap(); | |
| assert_eq!(state_machine.current_state(), TrustState::CriticalElevation); | |
| println!(" -> Active State: T4 (CriticalElevation)"); | |
| let d3_assertion = | |
| bte.process_telemetry(session_id, telemetry.clone(), state_machine.current_state()); | |
| let d3_dac = mock_authorizer | |
| .issue_dac( | |
| &d3_assertion, | |
| "root_signing_key.enc".to_string(), | |
| DataClassification::D3, | |
| AccessOperation::Read, | |
| now, | |
| 300, | |
| 2005, | |
| ) | |
| .expect("D3 DAC issuance failed at T4"); | |
| key_broker | |
| .execute_key_use( | |
| &d3_dac, | |
| session_id, | |
| AccessOperation::Read, | |
| now, | |
| state_machine.current_state(), | |
| ) | |
| .expect("D3 key derivation failed at T4"); | |
| println!(" -> PASS: Issued & consumed D3 (master key) capability under T4 trust level."); | |
| // 7. Hard Anomaly Lockout & Controlled Recovery | |
| println!("\n[7/7] Injecting Hard Anomaly Interrupt (T_-1 Lockout)..."); | |
| state_machine.trigger_compromise(); | |
| assert_eq!(state_machine.current_state(), TrustState::Compromised); | |
| assert!(state_machine.handle_device_authenticated().is_err()); | |
| println!(" -> PASS: Escalation blocked in T_-1 state."); | |
| state_machine.handle_hardware_recovery_reset().unwrap(); | |
| assert_eq!(state_machine.current_state(), TrustState::Unauthenticated); | |
| println!(" -> PASS: System performed clean hardware recovery to T0 state."); | |
| println!("\n=== ALL UBDA V1.2-alpha PROTOCOL TESTS COMPLETED ==="); | |
| } | |