omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / aienux / crates / aienos-evidence / evidence.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-evidence/evidence.om: Pure-Omega AIENOS Config A Evidence, |
| 3 | note UTC time formatting, PE32+ AArch64 EFI check, Canonical JSON checksum, |
| 4 | note Bundle Verification, and Native-Boot Rollback Verdict. |
| 5 | note Converted from aienux crates/aienos-evidence/src/{time,pe,canon,verify,rollback,capture,http,lib,main}.rs. |
| 6 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-evidence/evidence.om ... |
| 7 | |
| 8 | const EV_OK int 0 |
| 9 | const EV_ERR_BAD_DOS int -1 |
| 10 | const EV_ERR_BAD_PE int -2 |
| 11 | const EV_ERR_BAD_MACHINE int -3 |
| 12 | const EV_ERR_BAD_MAGIC int -4 |
| 13 | const EV_ERR_BAD_SUBSYS int -5 |
| 14 | const EV_ERR_TRUNCATED int -6 |
| 15 | |
| 16 | const EV_MACHINE_ARM64 ulong 0xaa64 |
| 17 | const EV_PE32_PLUS ulong 0x20b |
| 18 | const EV_SUBSYS_EFI_APP ulong 10 |
| 19 | |
| 20 | const EV_MIN_OUTPUT_TOKENS ulong 32 |
| 21 | const EV_MIN_SAMPLES ulong 3 |
| 22 | |
| 23 | const EV_VERDICT_PASS int 0 |
| 24 | const EV_VERDICT_FAIL int 1 |
| 25 | const EV_VERDICT_UNPARSEABLE int 2 |
| 26 | const EV_VERDICT_BLOCKED int 3 |
| 27 | |
| 28 | const EV_REC_DOC_ONLY uchar 0 |
| 29 | const EV_REC_VERIFIED uchar 1 |
| 30 | |
| 31 | text ev_hex_digits "0123456789abcdef" |
| 32 | |
| 33 | note Structured Config A bundle fields for pure-Omega verification |
| 34 | note 0=schema_ok, 1=host_ok, 2=uefi_present, 3=gpu_ok, 4=cpu_cores, 5=cpu_part_sum, |
| 35 | note 6=mem_total_kb, 7=block_devs, 8=model_sha_ok, 9=model_size, |
| 36 | note 10=repo_cnt, 11=repo_commits_ok, 12=repo_any_dirty, |
| 37 | note 13=sample_cnt, 14=samples_complete, |
| 38 | note 15=rec_status, 16=rec_rollback_tested |
| 39 | global ev_bundle ulong 17 |
| 40 | global ev_bundle_sum uchar 32 |
| 41 | |
| 42 | note Verification report: 0=errors, 1=m0_open |
| 43 | global ev_report ulong 2 |
| 44 | |
| 45 | note Rollback pre/post capture state |
| 46 | note 0=valid_shape, 1=secure_boot_off, 2=boot_order_match, 3=boot_current_match, |
| 47 | note 4=boot_next_cleared, 5=root_ok, 6=esp_ok |
| 48 | global ev_rb_state ulong 7 |
| 49 | |
| 50 | fn ev_u16_le buf view uchar * off ulong to ulong |
| 51 | b0 = ulong get buf off |
| 52 | b1 = ulong get buf add off 1 |
| 53 | ret or b0 shl b1 8 |
| 54 | end |
| 55 | |
| 56 | fn ev_u32_le buf view uchar * off ulong to ulong |
| 57 | b0 = ulong get buf off |
| 58 | b1 = ulong get buf add off 1 |
| 59 | b2 = ulong get buf add off 2 |
| 60 | b3 = ulong get buf add off 3 |
| 61 | ret or or b0 shl b1 8 or shl b2 16 shl b3 24 |
| 62 | end |
| 63 | |
| 64 | fn ev_put_u64_be buf edit uchar * off ulong v ulong to void |
| 65 | for i ulong 8 |
| 66 | sh = mul sub 7 i 8 |
| 67 | put buf add off i uchar and shr v sh 0xff |
| 68 | end |
| 69 | end |
| 70 | |
| 71 | note Scratch for civil date: 0=year, 1=month, 2=day |
| 72 | global ev_ymd ulong 3 |
| 73 | |
| 74 | note Howard Hinnant civil date from days since 1970-01-01 |
| 75 | fn ev_civil_date days ulong to void |
| 76 | z = add days 719468 |
| 77 | era = div z 146097 |
| 78 | doe = rem z 146097 |
| 79 | tmp = sub doe div doe 1460 |
| 80 | tmp = add tmp div doe 36524 |
| 81 | tmp = sub tmp div doe 146096 |
| 82 | yoe = div tmp 365 |
| 83 | tmp = add mul 365 yoe div yoe 4 |
| 84 | tmp = sub tmp div yoe 100 |
| 85 | doy = sub doe tmp |
| 86 | mp = div add mul 5 doy 2 153 |
| 87 | tmp = div add mul 153 mp 2 5 |
| 88 | put ev_ymd 2 add sub doy tmp 1 |
| 89 | mon = ulong 0 |
| 90 | if lt mp 10 |
| 91 | mon = add mp 3 |
| 92 | else |
| 93 | mon = sub mp 9 |
| 94 | end |
| 95 | put ev_ymd 1 mon |
| 96 | yr = add yoe mul era 400 |
| 97 | if le mon 2 |
| 98 | yr = add yr 1 |
| 99 | end |
| 100 | put ev_ymd 0 yr |
| 101 | end |
| 102 | |
| 103 | fn ev_put_2d dst edit uchar * off ulong v ulong to void |
| 104 | put dst off uchar add 48 rem div v 10 10 |
| 105 | put dst add off 1 uchar add 48 rem v 10 |
| 106 | end |
| 107 | |
| 108 | fn ev_iso_utc secs ulong dst edit uchar * to int |
| 109 | if lt len dst 20 |
| 110 | ret EV_ERR_TRUNCATED |
| 111 | end |
| 112 | ev_civil_date div secs 86400 |
| 113 | yr = get ev_ymd 0 |
| 114 | ev_put_2d dst 0 div yr 100 |
| 115 | ev_put_2d dst 2 rem yr 100 |
| 116 | put dst 4 uchar 45 |
| 117 | ev_put_2d dst 5 get ev_ymd 1 |
| 118 | put dst 7 uchar 45 |
| 119 | ev_put_2d dst 8 get ev_ymd 2 |
| 120 | put dst 10 uchar 84 |
| 121 | rem_s = rem secs 86400 |
| 122 | ev_put_2d dst 11 div rem_s 3600 |
| 123 | put dst 13 uchar 58 |
| 124 | ev_put_2d dst 14 div rem rem_s 3600 60 |
| 125 | put dst 16 uchar 58 |
| 126 | ev_put_2d dst 17 rem rem_s 60 |
| 127 | put dst 19 uchar 90 |
| 128 | ret EV_OK |
| 129 | end |
| 130 | |
| 131 | note Structural check that bytes form an AArch64 PE32+ EFI application |
| 132 | fn ev_verify_efi_app data view uchar * to int |
| 133 | if lt len data 256 |
| 134 | ret EV_ERR_BAD_DOS |
| 135 | end |
| 136 | if or ne get data 0 77 ne get data 1 90 |
| 137 | ret EV_ERR_BAD_DOS |
| 138 | end |
| 139 | pe = ev_u32_le data 60 |
| 140 | if gt add pe 96 len data |
| 141 | ret EV_ERR_TRUNCATED |
| 142 | end |
| 143 | if or ne get data pe 80 ne get data add pe 1 69 |
| 144 | ret EV_ERR_BAD_PE |
| 145 | end |
| 146 | if or ne get data add pe 2 0 ne get data add pe 3 0 |
| 147 | ret EV_ERR_BAD_PE |
| 148 | end |
| 149 | machine = ev_u16_le data add pe 4 |
| 150 | if ne machine EV_MACHINE_ARM64 |
| 151 | ret EV_ERR_BAD_MACHINE |
| 152 | end |
| 153 | opt = add pe 24 |
| 154 | magic = ev_u16_le data opt |
| 155 | if ne magic EV_PE32_PLUS |
| 156 | ret EV_ERR_BAD_MAGIC |
| 157 | end |
| 158 | subsys = ev_u16_le data add opt 68 |
| 159 | if ne subsys EV_SUBSYS_EFI_APP |
| 160 | ret EV_ERR_BAD_SUBSYS |
| 161 | end |
| 162 | ret EV_OK |
| 163 | end |
| 164 | |
| 165 | fn ev_is_lower_hex buf view uchar * expected_len ulong to bool |
| 166 | if ne len buf expected_len |
| 167 | ret false |
| 168 | end |
| 169 | for i ulong expected_len bound 128 |
| 170 | b = get buf i |
| 171 | is_dig = and ge b 48 le b 57 |
| 172 | is_af = and ge b 97 le b 102 |
| 173 | if not or is_dig is_af |
| 174 | ret false |
| 175 | end |
| 176 | end |
| 177 | ret true |
| 178 | end |
| 179 | |
| 180 | fn ev_hex_encode src view uchar * dst edit uchar * to void |
| 181 | n = len src |
| 182 | for i ulong n bound 64 |
| 183 | b = ulong get src i |
| 184 | hi = shr b 4 |
| 185 | lo = and b 15 |
| 186 | put dst mul i 2 get ev_hex_digits hi |
| 187 | put dst add mul i 2 1 get ev_hex_digits lo |
| 188 | end |
| 189 | end |
| 190 | |
| 191 | note Canonical bundle checksum over the 17 structured bundle fields |
| 192 | fn ev_bundle_checksum dst edit uchar * to void |
| 193 | raw = new uchar 136 0 |
| 194 | for i ulong 17 |
| 195 | ev_put_u64_be raw mul i 8 get ev_bundle i |
| 196 | end |
| 197 | dlen = sha2_digest 256 raw dst |
| 198 | if ne dlen 32 |
| 199 | trap 8 |
| 200 | end |
| 201 | end |
| 202 | |
| 203 | fn ev_bundle_seal to void |
| 204 | sum = new uchar 32 0 |
| 205 | ev_bundle_checksum sum |
| 206 | for i ulong 32 |
| 207 | put ev_bundle_sum i get sum i |
| 208 | end |
| 209 | end |
| 210 | |
| 211 | fn ev_bundle_checksum_ok to bool |
| 212 | sum = new uchar 32 0 |
| 213 | ev_bundle_checksum sum |
| 214 | for i ulong 32 |
| 215 | if ne get sum i get ev_bundle_sum i |
| 216 | ret false |
| 217 | end |
| 218 | end |
| 219 | ret true |
| 220 | end |
| 221 | |
| 222 | fn ev_bundle_verify to void |
| 223 | errs = ulong 0 |
| 224 | m0 = ulong 0 |
| 225 | if not ev_bundle_checksum_ok |
| 226 | errs = add errs 1 |
| 227 | end |
| 228 | if eq get ev_bundle 0 0 |
| 229 | errs = add errs 1 |
| 230 | end |
| 231 | if eq get ev_bundle 1 0 |
| 232 | errs = add errs 1 |
| 233 | end |
| 234 | if eq get ev_bundle 2 0 |
| 235 | errs = add errs 1 |
| 236 | end |
| 237 | if eq get ev_bundle 3 0 |
| 238 | errs = add errs 1 |
| 239 | end |
| 240 | cores = get ev_bundle 4 |
| 241 | psum = get ev_bundle 5 |
| 242 | if or eq cores 0 ne cores psum |
| 243 | errs = add errs 1 |
| 244 | end |
| 245 | if eq get ev_bundle 6 0 |
| 246 | errs = add errs 1 |
| 247 | end |
| 248 | if eq get ev_bundle 7 0 |
| 249 | errs = add errs 1 |
| 250 | end |
| 251 | if or eq get ev_bundle 8 0 eq get ev_bundle 9 0 |
| 252 | errs = add errs 1 |
| 253 | end |
| 254 | if eq get ev_bundle 10 0 |
| 255 | errs = add errs 1 |
| 256 | else |
| 257 | if eq get ev_bundle 11 0 |
| 258 | errs = add errs 1 |
| 259 | end |
| 260 | if ne get ev_bundle 12 0 |
| 261 | errs = add errs 1 |
| 262 | end |
| 263 | end |
| 264 | if or lt get ev_bundle 13 EV_MIN_SAMPLES eq get ev_bundle 14 0 |
| 265 | errs = add errs 1 |
| 266 | end |
| 267 | if ne get ev_bundle 15 ulong EV_REC_VERIFIED |
| 268 | m0 = add m0 1 |
| 269 | end |
| 270 | if eq get ev_bundle 16 0 |
| 271 | m0 = add m0 1 |
| 272 | end |
| 273 | put ev_report 0 errs |
| 274 | put ev_report 1 m0 |
| 275 | end |
| 276 | |
| 277 | note Native-boot rollback verdict evaluation |
| 278 | fn ev_rollback_verdict to int |
| 279 | if eq get ev_rb_state 0 0 |
| 280 | ret EV_VERDICT_UNPARSEABLE |
| 281 | end |
| 282 | if eq get ev_rb_state 1 0 |
| 283 | ret EV_VERDICT_BLOCKED |
| 284 | end |
| 285 | if eq get ev_rb_state 2 0 |
| 286 | ret EV_VERDICT_FAIL |
| 287 | end |
| 288 | if eq get ev_rb_state 3 0 |
| 289 | ret EV_VERDICT_FAIL |
| 290 | end |
| 291 | if eq get ev_rb_state 4 0 |
| 292 | ret EV_VERDICT_FAIL |
| 293 | end |
| 294 | if or eq get ev_rb_state 5 0 eq get ev_rb_state 6 0 |
| 295 | ret EV_VERDICT_FAIL |
| 296 | end |
| 297 | ret EV_VERDICT_PASS |
| 298 | end |