nx_trust_state_test.nx source
↩ module page · 131 lines · 6396 B
1// nx_trust_state_test.nx -- KAT for trust-state classifier.
2
3import "nx_syscalls.nx"
4import "nx_etg.nx"
5import "nx_trust_state.nx"
6
7func main() -> i64 {
8 // ----- T1 Trust state sealed-enum completeness -----
9 var i: i64 = 0
10 while i < NX_TRUST_N {
11 if nx_trust_is_valid(i) != 1 { return 1 + i }
12 i = i + 1
13 }
14 if nx_trust_is_valid(0 - 1) != 0 { return 100 }
15 if nx_trust_is_valid(NX_TRUST_N) != 0 { return 101 }
16
17 // ----- T2 Hardware band sealed-enum -----
18 var b: i64 = 0
19 while b < NX_HW_BAND_N {
20 if nx_hw_band_is_valid(b) != 1 { return 110 + b }
21 b = b + 1
22 }
23 if nx_hw_band_is_valid(NX_HW_BAND_N) != 0 { return 115 }
24
25 // ----- T3 Production-capable threshold -----
26 if nx_trust_is_production_capable(NX_TRUST_UNWRITTEN) != 0 { return 120 }
27 if nx_trust_is_production_capable(NX_TRUST_WRITTEN_UNTESTED) != 0 { return 121 }
28 if nx_trust_is_production_capable(NX_TRUST_LAB_TESTED_QEMU) != 0 { return 122 }
29 if nx_trust_is_production_capable(NX_TRUST_LAB_TESTED_NATIVE) != 0 { return 123 }
30 if nx_trust_is_production_capable(NX_TRUST_CROSS_ARCH_TESTED) != 0 { return 124 }
31 if nx_trust_is_production_capable(NX_TRUST_SUBOPTIMAL_HARDWARE_TESTED) != 1 { return 125 }
32 if nx_trust_is_production_capable(NX_TRUST_BARELY_FUNCTIONAL_TESTED) != 1 { return 126 }
33 if nx_trust_is_production_capable(NX_TRUST_WORLD_CLASS_TESTED) != 1 { return 127 }
34 if nx_trust_is_production_capable(NX_TRUST_RANGE_TESTED) != 1 { return 128 }
35 if nx_trust_is_production_capable(NX_TRUST_TIME_PROVEN) != 1 { return 129 }
36
37 // ----- T4 Fully-proven threshold -----
38 if nx_trust_is_fully_proven(NX_TRUST_CROSS_ARCH_TESTED) != 0 { return 130 }
39 if nx_trust_is_fully_proven(NX_TRUST_WORLD_CLASS_TESTED) != 0 { return 131 }
40 if nx_trust_is_fully_proven(NX_TRUST_RANGE_TESTED) != 1 { return 132 }
41 if nx_trust_is_fully_proven(NX_TRUST_TIME_PROVEN) != 1 { return 133 }
42
43 // ----- T5 NxTrustRecord init defaults to WRITTEN_UNTESTED -----
44 let r_buf: *u8 = sys_mmap(128)
45 let r: *NxTrustRecord = r_buf as *NxTrustRecord
46 nx_trust_record_init(r, 0xCAFE)
47 if r.primitive_id_hash != 0xCAFE { return 140 }
48 if r.trust_state != NX_TRUST_WRITTEN_UNTESTED { return 141 }
49 if r.bands_passed_bits != 0 { return 142 }
50
51 // ----- T6 Band escalation: world-class only -> WORLD_CLASS_TESTED -----
52 nx_trust_record_band_pass(r, NX_HW_BAND_WORLD_CLASS)
53 if r.trust_state != NX_TRUST_WORLD_CLASS_TESTED { return 150 }
54 // bits: 1 << 3 = 8
55 if r.bands_passed_bits != 8 { return 151 }
56
57 // ----- T7 Adding suboptimal escalates to higher state (band priority) -----
58 // World-class alone is "upper band confirmed" but NOT range; adding
59 // BARELY_FUNCTIONAL gives barely-functional + world-class = bits 2|8 = 10
60 nx_trust_record_band_pass(r, NX_HW_BAND_BARELY_FUNCTIONAL)
61 if r.bands_passed_bits != 10 { return 160 }
62 // trust_state stays WORLD_CLASS_TESTED until all three bands pass
63 // (BARELY_FUNCTIONAL_TESTED is index 6; WORLD_CLASS_TESTED is index 7;
64 // band-pass for BARELY_FUNCTIONAL escalates to BARELY_FUNCTIONAL_TESTED
65 // only if state was lower; current state 7 > 6 so no escalation)
66 if r.trust_state != NX_TRUST_WORLD_CLASS_TESTED { return 161 }
67
68 // ----- T8 All three bands -> RANGE_TESTED -----
69 nx_trust_record_band_pass(r, NX_HW_BAND_SUBOPTIMAL)
70 if r.bands_passed_bits != 14 { return 170 } // 2|4|8 = 14
71 if r.trust_state != NX_TRUST_RANGE_TESTED { return 171 }
72
73 // ----- T9 Re-pass same band is no-op -----
74 let rc_dup: i64 = nx_trust_record_band_pass(r, NX_HW_BAND_WORLD_CLASS)
75 if rc_dup != 0 { return 180 } // returns 0 for "already recorded"
76 if r.bands_passed_bits != 14 { return 181 }
77
78 // ----- T10 Invalid band rejected -----
79 let rc_bad: i64 = nx_trust_record_band_pass(r, NX_HW_BAND_NONE)
80 if rc_bad != -1 { return 190 }
81 let rc_bad2: i64 = nx_trust_record_band_pass(r, 9999)
82 if rc_bad2 != -1 { return 191 }
83
84 // ----- T11 nx_trust_record_set_state CROSS_ARCH escalation -----
85 let r2_buf: *u8 = sys_mmap(128)
86 let r2: *NxTrustRecord = r2_buf as *NxTrustRecord
87 nx_trust_record_init(r2, 0xBEEF)
88 let rc_s1: i64 = nx_trust_record_set_state(r2, NX_TRUST_LAB_TESTED_QEMU)
89 if rc_s1 != 0 { return 200 }
90 if r2.trust_state != NX_TRUST_LAB_TESTED_QEMU { return 201 }
91 nx_trust_record_set_state(r2, NX_TRUST_CROSS_ARCH_TESTED)
92 if r2.trust_state != NX_TRUST_CROSS_ARCH_TESTED { return 202 }
93
94 // ----- T12 RANGE_TESTED rejected without all bands -----
95 let rc_skip: i64 = nx_trust_record_set_state(r2, NX_TRUST_RANGE_TESTED)
96 if rc_skip != -2 { return 210 }
97 if r2.trust_state != NX_TRUST_CROSS_ARCH_TESTED { return 211 }
98
99 // ----- T13 TIME_PROVEN not externally settable -----
100 let rc_tp: i64 = nx_trust_record_set_state(r2, NX_TRUST_TIME_PROVEN)
101 if rc_tp != -3 { return 220 }
102
103 // ----- T14 Outcome mapping -----
104 nx_trust_record_init(r2, 0xBEEF)
105 // WRITTEN_UNTESTED -> FALSIFIED (no production capability)
106 if nx_trust_to_etg_outcome(r2) != NX_ETG_OUTCOME_FALSIFIED { return 230 }
107 nx_trust_record_set_state(r2, NX_TRUST_CROSS_ARCH_TESTED)
108 // CROSS_ARCH_TESTED still below SUBOPTIMAL -> FALSIFIED
109 if nx_trust_to_etg_outcome(r2) != NX_ETG_OUTCOME_FALSIFIED { return 231 }
110 nx_trust_record_band_pass(r2, NX_HW_BAND_SUBOPTIMAL)
111 // SUBOPTIMAL_HARDWARE_TESTED -> INCONCLUSIVE
112 if nx_trust_to_etg_outcome(r2) != NX_ETG_OUTCOME_INCONCLUSIVE { return 232 }
113 nx_trust_record_band_pass(r2, NX_HW_BAND_BARELY_FUNCTIONAL)
114 nx_trust_record_band_pass(r2, NX_HW_BAND_WORLD_CLASS)
115 // RANGE_TESTED -> CONFIRMED
116 if nx_trust_to_etg_outcome(r2) != NX_ETG_OUTCOME_CONFIRMED { return 233 }
117
118 // ----- T15 nx_trust_attest emits valid NxEtgEntry -----
119 let e_buf: *u8 = sys_mmap(128)
120 let e: *NxEtgEntry = e_buf as *NxEtgEntry
121 let rc_a: i64 = nx_trust_attest(r2, e, 0xC0DEC0DE, 1, 20260520)
122 if rc_a != 0 { return 240 }
123 if e.silicon_serial_hash != 0xC0DEC0DE { return 241 }
124 if e.outcome != NX_ETG_OUTCOME_CONFIRMED { return 242 }
125 if e.claim_value != 14 { return 243 } // bands_passed_bits all set
126 if e.measurement_value != NX_TRUST_RANGE_TESTED { return 244 }
127 if e.attestation_hash == 0 { return 245 }
128 if r2.attestation_hash != e.attestation_hash { return 246 }
129
130 return 0
131}