a3s_box_runtime/tee/
simulate.rs1pub const TEE_SIMULATE_ENV: &str = "A3S_TEE_SIMULATE";
9
10pub fn is_simulate_mode() -> bool {
12 std::env::var(TEE_SIMULATE_ENV)
13 .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
14 .unwrap_or(false)
15}
16
17pub const SIMULATED_REPORT_VERSION: u32 = 0xA3;
20
21pub const SIMULATED_CHIP_ID: [u8; 64] = [0xA3; 64];
23
24pub fn build_simulated_report(report_data: &[u8; 64]) -> Vec<u8> {
31 const SNP_REPORT_SIZE: usize = 1184;
32 let mut report = vec![0u8; SNP_REPORT_SIZE];
33
34 report[0x00..0x04].copy_from_slice(&SIMULATED_REPORT_VERSION.to_le_bytes());
36
37 report[0x04..0x08].copy_from_slice(&1u32.to_le_bytes());
39
40 report[0x08..0x10].copy_from_slice(&0u64.to_le_bytes());
42
43 report[0x38] = 3; report[0x39] = 0; report[0x3E] = 8; report[0x3F] = 115; report[0x50..0x90].copy_from_slice(report_data);
51
52 for i in 0..48 {
54 report[0x90 + i] = (i as u8).wrapping_mul(0xA3);
55 }
56
57 report[0x1A0..0x1E0].copy_from_slice(&SIMULATED_CHIP_ID);
59
60 report
63}
64
65pub fn is_simulated_report(report: &[u8]) -> bool {
67 if report.len() < 4 {
68 return false;
69 }
70 let version = u32::from_le_bytes(report[0x00..0x04].try_into().unwrap_or([0; 4]));
71 version == SIMULATED_REPORT_VERSION
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn test_is_simulate_mode_default() {
80 let _ = is_simulate_mode();
84 }
85
86 #[test]
87 fn test_build_simulated_report_size() {
88 let data = [0u8; 64];
89 let report = build_simulated_report(&data);
90 assert_eq!(report.len(), 1184);
91 }
92
93 #[test]
94 fn test_build_simulated_report_version() {
95 let data = [0u8; 64];
96 let report = build_simulated_report(&data);
97 let version = u32::from_le_bytes(report[0x00..0x04].try_into().unwrap());
98 assert_eq!(version, SIMULATED_REPORT_VERSION);
99 }
100
101 #[test]
102 fn test_build_simulated_report_contains_nonce() {
103 let mut data = [0u8; 64];
104 data[0] = 0xDE;
105 data[1] = 0xAD;
106 data[2] = 0xBE;
107 data[3] = 0xEF;
108 let report = build_simulated_report(&data);
109 assert_eq!(report[0x50], 0xDE);
110 assert_eq!(report[0x51], 0xAD);
111 assert_eq!(report[0x52], 0xBE);
112 assert_eq!(report[0x53], 0xEF);
113 }
114
115 #[test]
116 fn test_build_simulated_report_tcb() {
117 let data = [0u8; 64];
118 let report = build_simulated_report(&data);
119 assert_eq!(report[0x38], 3); assert_eq!(report[0x3E], 8); assert_eq!(report[0x3F], 115); }
123
124 #[test]
125 fn test_build_simulated_report_chip_id() {
126 let data = [0u8; 64];
127 let report = build_simulated_report(&data);
128 assert_eq!(&report[0x1A0..0x1E0], &SIMULATED_CHIP_ID);
129 }
130
131 #[test]
132 fn test_build_simulated_report_zero_signature() {
133 let data = [0u8; 64];
134 let report = build_simulated_report(&data);
135 assert!(report[0x2A0..].iter().all(|&b| b == 0));
137 }
138
139 #[test]
140 fn test_is_simulated_report_true() {
141 let data = [0u8; 64];
142 let report = build_simulated_report(&data);
143 assert!(is_simulated_report(&report));
144 }
145
146 #[test]
147 fn test_is_simulated_report_false() {
148 let mut report = vec![0u8; 1184];
149 report[0x00..0x04].copy_from_slice(&2u32.to_le_bytes()); assert!(!is_simulated_report(&report));
151 }
152
153 #[test]
154 fn test_is_simulated_report_too_short() {
155 assert!(!is_simulated_report(&[0u8; 2]));
156 }
157
158 #[test]
159 fn test_simulated_report_version_constant() {
160 assert_eq!(SIMULATED_REPORT_VERSION, 0xA3);
161 }
162}