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 / si / seed-challenge / test.om
| 1 | om2 |
| 2 | note si/seed-challenge/test.om: Standalone OM-2 behavioral test suite for seed-challenge verifier. |
| 3 | note Tests positive verification (SHA-256 KATs, full seed0->hex1->asm2->om4->omc ladder pin table + log) |
| 4 | note and negative/refusal controls (flipped rung byte, forged stage2 digest, uppercase/short digest, |
| 5 | note missing rung chain link, injected FAIL/NOT_RUN line, non-aarch64 host, forged CHALLENGE footer). |
| 6 | |
| 7 | global expbuf uchar 16384 |
| 8 | global logbuf uchar 32768 |
| 9 | global pinbuf uchar 64 |
| 10 | global omcsha uchar 64 |
| 11 | global patbuf uchar 512 |
| 12 | global decbuf uchar 32 |
| 13 | global rungseen ulong 8 |
| 14 | global gmeta ulong 12 |
| 15 | global shastate uint 8 |
| 16 | global shak uint 64 |
| 17 | global shaw uint 64 |
| 18 | global shablk uchar 64 |
| 19 | global shadig uchar 32 |
| 20 | global shahex uchar 64 |
| 21 | global testbuf uchar 128 |
| 22 | |
| 23 | const m_elen ulong 0 |
| 24 | const m_llen ulong 1 |
| 25 | const m_plen ulong 2 |
| 26 | const m_olen ulong 3 |
| 27 | const m_bad ulong 4 |
| 28 | const m_patlen ulong 5 |
| 29 | const m_expe ulong 6 |
| 30 | |
| 31 | const r_seed0 ulong 0 |
| 32 | const r_hex1 ulong 1 |
| 33 | const r_asm2 ulong 2 |
| 34 | const r_om4 ulong 3 |
| 35 | const r_omc ulong 4 |
| 36 | const r_pin ulong 5 |
| 37 | const r_incnt ulong 6 |
| 38 | const r_rcnt ulong 7 |
| 39 | |
| 40 | text khex "0123456789abcdef" |
| 41 | text t_pin "pin " |
| 42 | text t_input "input " |
| 43 | text t_rung "rung " |
| 44 | text t_seed0 "seed0" |
| 45 | text t_hex1 "hex1" |
| 46 | text t_asm2 "asm2" |
| 47 | text t_om4 "om4" |
| 48 | text t_omc "omc" |
| 49 | text t_host "host " |
| 50 | text t_a64 "aarch64" |
| 51 | text t_pass_in "PASS input " |
| 52 | text t_pass_rg "PASS rung " |
| 53 | text t_st1 "stage1 " |
| 54 | text t_st2 "stage2 " |
| 55 | text t_st3 "stage3 " |
| 56 | text t_fail "FAIL " |
| 57 | text t_notrun "NOT_RUN " |
| 58 | text t_ch_pfx "CHALLENGE " |
| 59 | text t_ch_pass "CHALLENGE PASS pin=" |
| 60 | text t_ch_omc " omc=" |
| 61 | |
| 62 | note Test vectors (real omega-mv seed/challenge/expected-v1.txt and DGX Spark log) |
| 63 | text sha_empty "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 64 | text msg_abc "abc" |
| 65 | text sha_abc "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" |
| 66 | text hex_short "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015a" |
| 67 | text hex_upper "BA7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" |
| 68 | text hex_badch "ga7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" |
| 69 | text pin_ok "369dbc96daa1684db2d7c2c7085bbf16b3fd77ee" |
| 70 | |
| 71 | text exp_v1_a "# seed-chain v1\npin 369dbc96daa1684db2d7c2c7085bbf16b3fd77ee\ninput seed/rung0/seed0.hex baaf081fe6055a768485c5d25d7435681ce00b04cfa80b26c48169f78944e14d 7903\n" |
| 72 | text exp_v1_b "rung seed0 aca00cf3677236809f0f223437398c6bb429e4c65f13d58762bcf96d13799474 408\nrung hex1 920e1756aec399c7c74f5cedd1676abb91b844ea75448e14aa50dcb0bbc7ba5d 776\n" |
| 73 | text exp_v1_c "rung asm2 6988abe072fac3cdef2240d5ad13a3ecd8fb576fa09112d344b9e7c2315969c4 2464\nrung om4 d0da40e45f1fcc45d9a39c9c52c9d745b545e076970729e51c12d76ef519efab 14224\nrung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f 325524\n" |
| 74 | text exp_no_asm2 "rung om4 d0da40e45f1fcc45d9a39c9c52c9d745b545e076970729e51c12d76ef519efab 14224\nrung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f 325524\n" |
| 75 | text exp_bad_hex "pin 369dbc96daa1684db2d7c2c7085bbf16b3fd77ee\nrung seed0 ACA00cf3677236809f0f223437398c6bb429e4c65f13d58762bcf96d13799474 408\n" |
| 76 | |
| 77 | text log_v1_a "omega seed-chain bootstrap challenge\nhost Linux 7.0.0-1019-nvidia aarch64\npin 369dbc96daa1684db2d7c2c7085bbf16b3fd77ee\n" |
| 78 | text log_v1_b "PASS input seed/rung0/seed0.hex baaf081fe6055a768485c5d25d7435681ce00b04cfa80b26c48169f78944e14d 7903\nPASS rung seed0 aca00cf3677236809f0f223437398c6bb429e4c65f13d58762bcf96d13799474 408 219ms\nPASS rung hex1 920e1756aec399c7c74f5cedd1676abb91b844ea75448e14aa50dcb0bbc7ba5d 776 4ms\n" |
| 79 | text log_v1_c "PASS rung asm2 6988abe072fac3cdef2240d5ad13a3ecd8fb576fa09112d344b9e7c2315969c4 2464 1ms\nPASS rung om4 d0da40e45f1fcc45d9a39c9c52c9d745b545e076970729e51c12d76ef519efab 14224 9ms\n" |
| 80 | text log_v1_d "stage1 PASS rung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f 325524 6ms\nstage2 PASS rung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f 325524 12ms\nstage3 PASS rung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f 325524 13ms\n" |
| 81 | text log_v1_e "CHALLENGE PASS pin=369dbc96daa1684db2d7c2c7085bbf16b3fd77ee omc=2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f\n" |
| 82 | text log_bad_st2 "stage1 PASS rung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f 325524 6ms\nstage2 PASS rung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029e 325524 12ms\nstage3 PASS rung omc 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f 325524 13ms\n" |
| 83 | text log_x86_host "omega seed-chain bootstrap challenge\nhost Linux 6.8.0-generic x86_64\npin 369dbc96daa1684db2d7c2c7085bbf16b3fd77ee\n" |
| 84 | text log_inj_fail "FAIL rung hex1 mismatch\n" |
| 85 | text log_bad_foot "CHALLENGE PASS pin=369dbc96daa1684db2d7c2c7085bbf16b3fd77ee omc=0000000000000000000000000000000000000000000000000000000000000000\n" |
| 86 | |
| 87 | text p_sha_kat "PASS sha256_kat_empty_and_abc\n" |
| 88 | text p_flip_byte "PASS neg_flipped_rung_byte_caught\n" |
| 89 | text p_hex_fmt "PASS digest_format_validation_pos_and_neg\n" |
| 90 | text p_valid_v1 "PASS valid_v1_ladder_log_accepted\n" |
| 91 | text p_neg_st2 "PASS neg_forged_stage2_digest_refused\n" |
| 92 | text p_neg_chain "PASS neg_missing_asm2_rung_refused\n" |
| 93 | text p_neg_fail "PASS neg_injected_fail_line_refused\n" |
| 94 | text p_neg_host "PASS neg_non_aarch64_host_refused\n" |
| 95 | text p_neg_exp "PASS neg_malformed_expected_digest_refused\n" |
| 96 | text p_neg_foot "PASS neg_forged_challenge_footer_refused\n" |
| 97 | text p_all "SEED-CHALLENGE TEST SUITE PASS (10/10)\n" |
| 98 | text f_msg "FAIL test case " |
| 99 | text t_nl "\n" |
| 100 | |
| 101 | fn write_all fd int src view uchar * off ulong n ulong to void |
| 102 | p = ulong 0 |
| 103 | while lt p n bound 4096 |
| 104 | k = write fd src add off p sub n p |
| 105 | if le k 0 |
| 106 | exit 2 |
| 107 | end |
| 108 | p = add p ulong k |
| 109 | end |
| 110 | end |
| 111 | |
| 112 | fn wstr fd int s view uchar * to void |
| 113 | write_all fd s 0 len s |
| 114 | end |
| 115 | |
| 116 | fn wdec fd int v ulong to void |
| 117 | i = ulong 32 |
| 118 | n = v |
| 119 | while true bound 24 |
| 120 | i = sub i 1 |
| 121 | put decbuf i uchar add rem n 10 48 |
| 122 | n = div n 10 |
| 123 | if eq n 0 |
| 124 | break |
| 125 | end |
| 126 | end |
| 127 | p = i |
| 128 | while lt p 32 bound 32 |
| 129 | k = write fd decbuf p sub 32 p |
| 130 | if le k 0 |
| 131 | exit 2 |
| 132 | end |
| 133 | p = add p ulong k |
| 134 | end |
| 135 | end |
| 136 | |
| 137 | fn fail_case id ulong to void |
| 138 | wstr 2 f_msg |
| 139 | wdec 2 id |
| 140 | wstr 2 t_nl |
| 141 | exit 1 |
| 142 | end |
| 143 | |
| 144 | fn is_hex_ch c uchar to bool |
| 145 | if and ge c 48 le c 57 |
| 146 | ret true |
| 147 | end |
| 148 | if and ge c 97 le c 102 |
| 149 | ret true |
| 150 | end |
| 151 | ret false |
| 152 | end |
| 153 | |
| 154 | fn is_dig_ch c uchar to bool |
| 155 | ret and ge c 48 le c 57 |
| 156 | end |
| 157 | |
| 158 | fn is_hex_exp off ulong n ulong to bool |
| 159 | if eq n 0 |
| 160 | ret false |
| 161 | end |
| 162 | if gt add off n 16384 |
| 163 | ret false |
| 164 | end |
| 165 | i = ulong 0 |
| 166 | while lt i n bound 128 |
| 167 | if not is_hex_ch get expbuf add off i |
| 168 | ret false |
| 169 | end |
| 170 | i = add i 1 |
| 171 | end |
| 172 | ret true |
| 173 | end |
| 174 | |
| 175 | fn is_dec_exp off ulong n ulong to bool |
| 176 | if eq n 0 |
| 177 | ret false |
| 178 | end |
| 179 | if gt n 20 |
| 180 | ret false |
| 181 | end |
| 182 | if gt add off n 16384 |
| 183 | ret false |
| 184 | end |
| 185 | nz = false |
| 186 | i = ulong 0 |
| 187 | while lt i n bound 32 |
| 188 | c = get expbuf add off i |
| 189 | if not is_dig_ch c |
| 190 | ret false |
| 191 | end |
| 192 | if ne c 48 |
| 193 | nz = true |
| 194 | end |
| 195 | i = add i 1 |
| 196 | end |
| 197 | ret nz |
| 198 | end |
| 199 | |
| 200 | fn is_hex_view s view uchar * n ulong to bool |
| 201 | if ne len s n |
| 202 | ret false |
| 203 | end |
| 204 | i = ulong 0 |
| 205 | while lt i n bound 128 |
| 206 | if not is_hex_ch get s i |
| 207 | ret false |
| 208 | end |
| 209 | i = add i 1 |
| 210 | end |
| 211 | ret true |
| 212 | end |
| 213 | |
| 214 | fn sha_kinit to void |
| 215 | put shak 0 0x428a2f98 |
| 216 | put shak 1 0x71374491 |
| 217 | put shak 2 0xb5c0fbcf |
| 218 | put shak 3 0xe9b5dba5 |
| 219 | put shak 4 0x3956c25b |
| 220 | put shak 5 0x59f111f1 |
| 221 | put shak 6 0x923f82a4 |
| 222 | put shak 7 0xab1c5ed5 |
| 223 | put shak 8 0xd807aa98 |
| 224 | put shak 9 0x12835b01 |
| 225 | put shak 10 0x243185be |
| 226 | put shak 11 0x550c7dc3 |
| 227 | put shak 12 0x72be5d74 |
| 228 | put shak 13 0x80deb1fe |
| 229 | put shak 14 0x9bdc06a7 |
| 230 | put shak 15 0xc19bf174 |
| 231 | put shak 16 0xe49b69c1 |
| 232 | put shak 17 0xefbe4786 |
| 233 | put shak 18 0x0fc19dc6 |
| 234 | put shak 19 0x240ca1cc |
| 235 | put shak 20 0x2de92c6f |
| 236 | put shak 21 0x4a7484aa |
| 237 | put shak 22 0x5cb0a9dc |
| 238 | put shak 23 0x76f988da |
| 239 | put shak 24 0x983e5152 |
| 240 | put shak 25 0xa831c66d |
| 241 | put shak 26 0xb00327c8 |
| 242 | put shak 27 0xbf597fc7 |
| 243 | put shak 28 0xc6e00bf3 |
| 244 | put shak 29 0xd5a79147 |
| 245 | put shak 30 0x06ca6351 |
| 246 | put shak 31 0x14292967 |
| 247 | put shak 32 0x27b70a85 |
| 248 | put shak 33 0x2e1b2138 |
| 249 | put shak 34 0x4d2c6dfc |
| 250 | put shak 35 0x53380d13 |
| 251 | put shak 36 0x650a7354 |
| 252 | put shak 37 0x766a0abb |
| 253 | put shak 38 0x81c2c92e |
| 254 | put shak 39 0x92722c85 |
| 255 | put shak 40 0xa2bfe8a1 |
| 256 | put shak 41 0xa81a664b |
| 257 | put shak 42 0xc24b8b70 |
| 258 | put shak 43 0xc76c51a3 |
| 259 | put shak 44 0xd192e819 |
| 260 | put shak 45 0xd6990624 |
| 261 | put shak 46 0xf40e3585 |
| 262 | put shak 47 0x106aa070 |
| 263 | put shak 48 0x19a4c116 |
| 264 | put shak 49 0x1e376c08 |
| 265 | put shak 50 0x2748774c |
| 266 | put shak 51 0x34b0bcb5 |
| 267 | put shak 52 0x391c0cb3 |
| 268 | put shak 53 0x4ed8aa4a |
| 269 | put shak 54 0x5b9cca4f |
| 270 | put shak 55 0x682e6ff3 |
| 271 | put shak 56 0x748f82ee |
| 272 | put shak 57 0x78a5636f |
| 273 | put shak 58 0x84c87814 |
| 274 | put shak 59 0x8cc70208 |
| 275 | put shak 60 0x90befffa |
| 276 | put shak 61 0xa4506ceb |
| 277 | put shak 62 0xbef9a3f7 |
| 278 | put shak 63 0xc67178f2 |
| 279 | end |
| 280 | |
| 281 | fn sha_iv to void |
| 282 | put shastate 0 0x6a09e667 |
| 283 | put shastate 1 0xbb67ae85 |
| 284 | put shastate 2 0x3c6ef372 |
| 285 | put shastate 3 0xa54ff53a |
| 286 | put shastate 4 0x510e527f |
| 287 | put shastate 5 0x9b05688c |
| 288 | put shastate 6 0x1f83d9ab |
| 289 | put shastate 7 0x5be0cd19 |
| 290 | end |
| 291 | |
| 292 | fn sha_sched to void |
| 293 | for i ulong 16 |
| 294 | q = mul i 4 |
| 295 | x = uint get shablk q |
| 296 | x = or shl x 8 uint get shablk add q 1 |
| 297 | x = or shl x 8 uint get shablk add q 2 |
| 298 | put shaw i or shl x 8 uint get shablk add q 3 |
| 299 | end |
| 300 | for i ulong 48 |
| 301 | x = get shaw add i 1 |
| 302 | y = get shaw add i 14 |
| 303 | s0 = xor xor rotr x 7 rotr x 18 shr x 3 |
| 304 | s1 = xor xor rotr y 17 rotr y 19 shr y 10 |
| 305 | put shaw add i 16 add add get shaw i get shaw add i 9 add s0 s1 |
| 306 | end |
| 307 | end |
| 308 | |
| 309 | fn sha_compress to void |
| 310 | sha_sched |
| 311 | a = get shastate 0 |
| 312 | b = get shastate 1 |
| 313 | c = get shastate 2 |
| 314 | d = get shastate 3 |
| 315 | e = get shastate 4 |
| 316 | f = get shastate 5 |
| 317 | g = get shastate 6 |
| 318 | h = get shastate 7 |
| 319 | t = uint 0 |
| 320 | for i ulong 64 |
| 321 | h = add h get shak i |
| 322 | h = add h get shaw i |
| 323 | t = xor rotr e 6 rotr e 11 |
| 324 | t = xor t rotr e 25 |
| 325 | h = add h t |
| 326 | h = add h xor g and e xor f g |
| 327 | d = add d h |
| 328 | t = xor rotr a 2 rotr a 13 |
| 329 | t = xor t rotr a 22 |
| 330 | h = add h t |
| 331 | h = add h or and a b and c or a b |
| 332 | t = h |
| 333 | h = g |
| 334 | g = f |
| 335 | f = e |
| 336 | e = d |
| 337 | d = c |
| 338 | c = b |
| 339 | b = a |
| 340 | a = t |
| 341 | end |
| 342 | put shastate 0 add get shastate 0 a |
| 343 | put shastate 1 add get shastate 1 b |
| 344 | put shastate 2 add get shastate 2 c |
| 345 | put shastate 3 add get shastate 3 d |
| 346 | put shastate 4 add get shastate 4 e |
| 347 | put shastate 5 add get shastate 5 f |
| 348 | put shastate 6 add get shastate 6 g |
| 349 | put shastate 7 add get shastate 7 h |
| 350 | end |
| 351 | |
| 352 | fn sha256_bytes src view uchar * dlen ulong to void |
| 353 | sha_kinit |
| 354 | sha_iv |
| 355 | off = ulong 0 |
| 356 | while ge sub dlen off 64 bound 65536 |
| 357 | for j ulong 64 |
| 358 | put shablk j get src add off j |
| 359 | end |
| 360 | sha_compress |
| 361 | off = add off 64 |
| 362 | end |
| 363 | remn = sub dlen off |
| 364 | for j ulong 64 |
| 365 | if lt j remn |
| 366 | put shablk j get src add off j |
| 367 | else |
| 368 | put shablk j 0 |
| 369 | end |
| 370 | end |
| 371 | put shablk remn 0x80 |
| 372 | if gt remn 55 |
| 373 | sha_compress |
| 374 | for j ulong 64 |
| 375 | put shablk j 0 |
| 376 | end |
| 377 | end |
| 378 | bits = mul dlen 8 |
| 379 | put shablk 56 uchar and shr bits 56 255 |
| 380 | put shablk 57 uchar and shr bits 48 255 |
| 381 | put shablk 58 uchar and shr bits 40 255 |
| 382 | put shablk 59 uchar and shr bits 32 255 |
| 383 | put shablk 60 uchar and shr bits 24 255 |
| 384 | put shablk 61 uchar and shr bits 16 255 |
| 385 | put shablk 62 uchar and shr bits 8 255 |
| 386 | put shablk 63 uchar and bits 255 |
| 387 | sha_compress |
| 388 | for i ulong 8 |
| 389 | w = get shastate i |
| 390 | q = mul i 4 |
| 391 | put shadig q uchar and shr w 24 255 |
| 392 | put shadig add q 1 uchar and shr w 16 255 |
| 393 | put shadig add q 2 uchar and shr w 8 255 |
| 394 | put shadig add q 3 uchar and w 255 |
| 395 | end |
| 396 | for i ulong 32 |
| 397 | bv = ulong get shadig i |
| 398 | put shahex mul i 2 get khex shr bv 4 |
| 399 | put shahex add mul i 2 1 get khex and bv 15 |
| 400 | end |
| 401 | end |
| 402 | |
| 403 | fn verify_rung_bytes src view uchar * dlen ulong exphex view uchar * to bool |
| 404 | if not is_hex_view exphex 64 |
| 405 | ret false |
| 406 | end |
| 407 | sha256_bytes src dlen |
| 408 | for i ulong 64 |
| 409 | if ne get shahex i get exphex i |
| 410 | ret false |
| 411 | end |
| 412 | end |
| 413 | ret true |
| 414 | end |
| 415 | |
| 416 | fn exp_reset to void |
| 417 | put gmeta m_elen 0 |
| 418 | end |
| 419 | |
| 420 | fn exp_append s view uchar * to void |
| 421 | tot = get gmeta m_elen |
| 422 | n = len s |
| 423 | i = ulong 0 |
| 424 | while lt i n bound 4096 |
| 425 | put expbuf add tot i get s i |
| 426 | i = add i 1 |
| 427 | end |
| 428 | put gmeta m_elen add tot n |
| 429 | end |
| 430 | |
| 431 | fn log_reset to void |
| 432 | put gmeta m_llen 0 |
| 433 | end |
| 434 | |
| 435 | fn log_append s view uchar * to void |
| 436 | tot = get gmeta m_llen |
| 437 | n = len s |
| 438 | i = ulong 0 |
| 439 | while lt i n bound 4096 |
| 440 | put logbuf add tot i get s i |
| 441 | i = add i 1 |
| 442 | end |
| 443 | put gmeta m_llen add tot n |
| 444 | end |
| 445 | |
| 446 | fn pat_clear to void |
| 447 | put gmeta m_patlen 0 |
| 448 | end |
| 449 | |
| 450 | fn pat_push_ch c uchar to void |
| 451 | n = get gmeta m_patlen |
| 452 | if lt n 512 |
| 453 | put patbuf n c |
| 454 | put gmeta m_patlen add n 1 |
| 455 | end |
| 456 | end |
| 457 | |
| 458 | fn pat_push_lit s view uchar * to void |
| 459 | sl = len s |
| 460 | i = ulong 0 |
| 461 | while lt i sl bound 512 |
| 462 | pat_push_ch get s i |
| 463 | i = add i 1 |
| 464 | end |
| 465 | end |
| 466 | |
| 467 | fn pat_push_exp off ulong n ulong to void |
| 468 | i = ulong 0 |
| 469 | while lt i n bound 512 |
| 470 | if lt add off i 16384 |
| 471 | pat_push_ch get expbuf add off i |
| 472 | end |
| 473 | i = add i 1 |
| 474 | end |
| 475 | end |
| 476 | |
| 477 | fn pat_push_pin to void |
| 478 | n = get gmeta m_plen |
| 479 | i = ulong 0 |
| 480 | while lt i n bound 64 |
| 481 | pat_push_ch get pinbuf i |
| 482 | i = add i 1 |
| 483 | end |
| 484 | end |
| 485 | |
| 486 | fn pat_push_omc to void |
| 487 | n = get gmeta m_olen |
| 488 | i = ulong 0 |
| 489 | while lt i n bound 64 |
| 490 | pat_push_ch get omcsha i |
| 491 | i = add i 1 |
| 492 | end |
| 493 | end |
| 494 | |
| 495 | fn exp_eq off ulong n ulong lit view uchar * to bool |
| 496 | if ne n len lit |
| 497 | ret false |
| 498 | end |
| 499 | i = ulong 0 |
| 500 | while lt i n bound 512 |
| 501 | if ne get expbuf add off i get lit i |
| 502 | ret false |
| 503 | end |
| 504 | i = add i 1 |
| 505 | end |
| 506 | ret true |
| 507 | end |
| 508 | |
| 509 | fn exp_starts off ulong elen ulong pfx view uchar * to bool |
| 510 | pl = len pfx |
| 511 | if lt elen pl |
| 512 | ret false |
| 513 | end |
| 514 | i = ulong 0 |
| 515 | while lt i pl bound 512 |
| 516 | if ne get expbuf add off i get pfx i |
| 517 | ret false |
| 518 | end |
| 519 | i = add i 1 |
| 520 | end |
| 521 | ret true |
| 522 | end |
| 523 | |
| 524 | fn log_starts off ulong llen ulong pfx view uchar * to bool |
| 525 | pl = len pfx |
| 526 | if lt llen pl |
| 527 | ret false |
| 528 | end |
| 529 | i = ulong 0 |
| 530 | while lt i pl bound 512 |
| 531 | if ne get logbuf add off i get pfx i |
| 532 | ret false |
| 533 | end |
| 534 | i = add i 1 |
| 535 | end |
| 536 | ret true |
| 537 | end |
| 538 | |
| 539 | fn log_contains off ulong llen ulong needle view uchar * to bool |
| 540 | nl = len needle |
| 541 | if gt nl llen |
| 542 | ret false |
| 543 | end |
| 544 | lim = add sub llen nl 1 |
| 545 | i = ulong 0 |
| 546 | while lt i lim bound 1024 |
| 547 | if log_starts add off i sub llen i needle |
| 548 | ret true |
| 549 | end |
| 550 | i = add i 1 |
| 551 | end |
| 552 | ret false |
| 553 | end |
| 554 | |
| 555 | fn log_line_matches_pat off ulong llen ulong allow_ms bool to bool |
| 556 | pl = get gmeta m_patlen |
| 557 | if lt llen pl |
| 558 | ret false |
| 559 | end |
| 560 | i = ulong 0 |
| 561 | while lt i pl bound 512 |
| 562 | if ne get logbuf add off i get patbuf i |
| 563 | ret false |
| 564 | end |
| 565 | i = add i 1 |
| 566 | end |
| 567 | if eq llen pl |
| 568 | ret true |
| 569 | end |
| 570 | if not allow_ms |
| 571 | ret false |
| 572 | end |
| 573 | if lt sub llen pl 4 |
| 574 | ret false |
| 575 | end |
| 576 | if ne get logbuf add off pl 32 |
| 577 | ret false |
| 578 | end |
| 579 | if ne get logbuf add off sub llen 2 109 |
| 580 | ret false |
| 581 | end |
| 582 | if ne get logbuf add off sub llen 1 115 |
| 583 | ret false |
| 584 | end |
| 585 | pos = add pl 1 |
| 586 | endpos = sub llen 2 |
| 587 | if ge pos endpos |
| 588 | ret false |
| 589 | end |
| 590 | while lt pos endpos bound 32 |
| 591 | if not is_dig_ch get logbuf add off pos |
| 592 | ret false |
| 593 | end |
| 594 | pos = add pos 1 |
| 595 | end |
| 596 | ret true |
| 597 | end |
| 598 | |
| 599 | fn log_has_pat allow_ms bool to bool |
| 600 | tot = get gmeta m_llen |
| 601 | pos = ulong 0 |
| 602 | while lt pos tot bound 32768 |
| 603 | ls = pos |
| 604 | while lt pos tot bound 32768 |
| 605 | if eq get logbuf pos 10 |
| 606 | break |
| 607 | end |
| 608 | pos = add pos 1 |
| 609 | end |
| 610 | ll = sub pos ls |
| 611 | if log_line_matches_pat ls ll allow_ms |
| 612 | ret true |
| 613 | end |
| 614 | if lt pos tot |
| 615 | pos = add pos 1 |
| 616 | end |
| 617 | end |
| 618 | ret false |
| 619 | end |
| 620 | |
| 621 | fn log_has_aarch64_host to bool |
| 622 | tot = get gmeta m_llen |
| 623 | pos = ulong 0 |
| 624 | while lt pos tot bound 32768 |
| 625 | ls = pos |
| 626 | while lt pos tot bound 32768 |
| 627 | if eq get logbuf pos 10 |
| 628 | break |
| 629 | end |
| 630 | pos = add pos 1 |
| 631 | end |
| 632 | ll = sub pos ls |
| 633 | if log_starts ls ll t_host |
| 634 | if log_contains ls ll t_a64 |
| 635 | ret true |
| 636 | end |
| 637 | end |
| 638 | if lt pos tot |
| 639 | pos = add pos 1 |
| 640 | end |
| 641 | end |
| 642 | ret false |
| 643 | end |
| 644 | |
| 645 | fn log_has_fail_or_notrun to bool |
| 646 | tot = get gmeta m_llen |
| 647 | pos = ulong 0 |
| 648 | while lt pos tot bound 32768 |
| 649 | ls = pos |
| 650 | while lt pos tot bound 32768 |
| 651 | if eq get logbuf pos 10 |
| 652 | break |
| 653 | end |
| 654 | pos = add pos 1 |
| 655 | end |
| 656 | ll = sub pos ls |
| 657 | off = ls |
| 658 | remn = ll |
| 659 | if log_starts ls ll t_st1 |
| 660 | off = add ls 7 |
| 661 | remn = sub ll 7 |
| 662 | else |
| 663 | if log_starts ls ll t_st2 |
| 664 | off = add ls 7 |
| 665 | remn = sub ll 7 |
| 666 | else |
| 667 | if log_starts ls ll t_st3 |
| 668 | off = add ls 7 |
| 669 | remn = sub ll 7 |
| 670 | end |
| 671 | end |
| 672 | end |
| 673 | if log_starts off remn t_fail |
| 674 | ret true |
| 675 | end |
| 676 | if log_starts off remn t_notrun |
| 677 | ret true |
| 678 | end |
| 679 | if lt pos tot |
| 680 | pos = add pos 1 |
| 681 | end |
| 682 | end |
| 683 | ret false |
| 684 | end |
| 685 | |
| 686 | fn log_check_last_challenge to bool |
| 687 | pat_clear |
| 688 | pat_push_lit t_ch_pass |
| 689 | pat_push_pin |
| 690 | pat_push_lit t_ch_omc |
| 691 | pat_push_omc |
| 692 | tot = get gmeta m_llen |
| 693 | pos = ulong 0 |
| 694 | found = false |
| 695 | matched = false |
| 696 | while lt pos tot bound 32768 |
| 697 | ls = pos |
| 698 | while lt pos tot bound 32768 |
| 699 | if eq get logbuf pos 10 |
| 700 | break |
| 701 | end |
| 702 | pos = add pos 1 |
| 703 | end |
| 704 | ll = sub pos ls |
| 705 | if log_starts ls ll t_ch_pfx |
| 706 | found = true |
| 707 | matched = log_line_matches_pat ls ll false |
| 708 | end |
| 709 | if lt pos tot |
| 710 | pos = add pos 1 |
| 711 | end |
| 712 | end |
| 713 | if not found |
| 714 | ret false |
| 715 | end |
| 716 | ret matched |
| 717 | end |
| 718 | |
| 719 | fn find_space_exp pos ulong lim ulong to ulong |
| 720 | p = pos |
| 721 | while lt p lim bound 512 |
| 722 | if eq get expbuf p 32 |
| 723 | ret p |
| 724 | end |
| 725 | p = add p 1 |
| 726 | end |
| 727 | ret lim |
| 728 | end |
| 729 | |
| 730 | fn identify_rung p1 ulong nlen ulong to ulong |
| 731 | if exp_eq p1 nlen t_seed0 |
| 732 | ret r_seed0 |
| 733 | end |
| 734 | if exp_eq p1 nlen t_hex1 |
| 735 | ret r_hex1 |
| 736 | end |
| 737 | if exp_eq p1 nlen t_asm2 |
| 738 | ret r_asm2 |
| 739 | end |
| 740 | if exp_eq p1 nlen t_om4 |
| 741 | ret r_om4 |
| 742 | end |
| 743 | if exp_eq p1 nlen t_omc |
| 744 | ret r_omc |
| 745 | end |
| 746 | ret 99 |
| 747 | end |
| 748 | |
| 749 | fn check_omc_stages p1 ulong remn ulong to void |
| 750 | pat_clear |
| 751 | pat_push_lit t_st1 |
| 752 | pat_push_lit t_pass_rg |
| 753 | pat_push_exp p1 remn |
| 754 | ok = log_has_pat true |
| 755 | pat_clear |
| 756 | pat_push_lit t_st2 |
| 757 | pat_push_lit t_pass_rg |
| 758 | pat_push_exp p1 remn |
| 759 | if not log_has_pat true |
| 760 | ok = false |
| 761 | end |
| 762 | pat_clear |
| 763 | pat_push_lit t_st3 |
| 764 | pat_push_lit t_pass_rg |
| 765 | pat_push_exp p1 remn |
| 766 | if not log_has_pat true |
| 767 | ok = false |
| 768 | end |
| 769 | if not ok |
| 770 | put gmeta m_bad add get gmeta m_bad 1 |
| 771 | end |
| 772 | end |
| 773 | |
| 774 | fn check_input_line ls ulong le_pos ulong to void |
| 775 | p1 = add ls 6 |
| 776 | s1 = find_space_exp p1 le_pos |
| 777 | if or eq s1 p1 ge s1 le_pos |
| 778 | put gmeta m_expe add get gmeta m_expe 1 |
| 779 | ret |
| 780 | end |
| 781 | p2 = add s1 1 |
| 782 | s2 = find_space_exp p2 le_pos |
| 783 | if or ne sub s2 p2 64 ge s2 le_pos |
| 784 | put gmeta m_expe add get gmeta m_expe 1 |
| 785 | ret |
| 786 | end |
| 787 | if not is_hex_exp p2 64 |
| 788 | put gmeta m_expe add get gmeta m_expe 1 |
| 789 | ret |
| 790 | end |
| 791 | p3 = add s2 1 |
| 792 | if not is_dec_exp p3 sub le_pos p3 |
| 793 | put gmeta m_expe add get gmeta m_expe 1 |
| 794 | ret |
| 795 | end |
| 796 | pat_clear |
| 797 | pat_push_lit t_pass_in |
| 798 | pat_push_exp p1 sub le_pos p1 |
| 799 | if not log_has_pat false |
| 800 | put gmeta m_bad add get gmeta m_bad 1 |
| 801 | end |
| 802 | end |
| 803 | |
| 804 | fn check_rung_line ls ulong le_pos ulong to void |
| 805 | p1 = add ls 5 |
| 806 | s1 = find_space_exp p1 le_pos |
| 807 | nlen = sub s1 p1 |
| 808 | if or eq nlen 0 ge s1 le_pos |
| 809 | put gmeta m_expe add get gmeta m_expe 1 |
| 810 | ret |
| 811 | end |
| 812 | p2 = add s1 1 |
| 813 | s2 = find_space_exp p2 le_pos |
| 814 | if or ne sub s2 p2 64 ge s2 le_pos |
| 815 | put gmeta m_expe add get gmeta m_expe 1 |
| 816 | ret |
| 817 | end |
| 818 | if not is_hex_exp p2 64 |
| 819 | put gmeta m_expe add get gmeta m_expe 1 |
| 820 | ret |
| 821 | end |
| 822 | p3 = add s2 1 |
| 823 | if not is_dec_exp p3 sub le_pos p3 |
| 824 | put gmeta m_expe add get gmeta m_expe 1 |
| 825 | ret |
| 826 | end |
| 827 | ridx = identify_rung p1 nlen |
| 828 | if eq ridx 99 |
| 829 | put gmeta m_expe add get gmeta m_expe 1 |
| 830 | ret |
| 831 | end |
| 832 | put rungseen ridx 1 |
| 833 | if eq ridx r_omc |
| 834 | for i ulong 64 |
| 835 | put omcsha i get expbuf add p2 i |
| 836 | end |
| 837 | put gmeta m_olen 64 |
| 838 | check_omc_stages p1 sub le_pos p1 |
| 839 | else |
| 840 | pat_clear |
| 841 | pat_push_lit t_pass_rg |
| 842 | pat_push_exp p1 sub le_pos p1 |
| 843 | if not log_has_pat true |
| 844 | put gmeta m_bad add get gmeta m_bad 1 |
| 845 | end |
| 846 | end |
| 847 | end |
| 848 | |
| 849 | fn verify_loaded to int |
| 850 | for i ulong 8 |
| 851 | put rungseen i 0 |
| 852 | end |
| 853 | put gmeta m_plen 0 |
| 854 | put gmeta m_olen 0 |
| 855 | put gmeta m_bad 0 |
| 856 | put gmeta m_expe 0 |
| 857 | etot = get gmeta m_elen |
| 858 | pos = ulong 0 |
| 859 | while lt pos etot bound 16384 |
| 860 | ls = pos |
| 861 | while lt pos etot bound 16384 |
| 862 | if eq get expbuf pos 10 |
| 863 | break |
| 864 | end |
| 865 | pos = add pos 1 |
| 866 | end |
| 867 | ll = sub pos ls |
| 868 | if gt ll 0 |
| 869 | if ne get expbuf ls 35 |
| 870 | if exp_starts ls ll t_pin |
| 871 | plen = sub ll 4 |
| 872 | if and eq plen 40 is_hex_exp add ls 4 40 |
| 873 | for j ulong 40 |
| 874 | put pinbuf j get expbuf add add ls 4 j |
| 875 | end |
| 876 | put gmeta m_plen 40 |
| 877 | put rungseen r_pin 1 |
| 878 | else |
| 879 | put gmeta m_expe add get gmeta m_expe 1 |
| 880 | end |
| 881 | else |
| 882 | if exp_starts ls ll t_input |
| 883 | check_input_line ls pos |
| 884 | else |
| 885 | if exp_starts ls ll t_rung |
| 886 | check_rung_line ls pos |
| 887 | else |
| 888 | put gmeta m_expe add get gmeta m_expe 1 |
| 889 | end |
| 890 | end |
| 891 | end |
| 892 | end |
| 893 | end |
| 894 | if lt pos etot |
| 895 | pos = add pos 1 |
| 896 | end |
| 897 | end |
| 898 | if gt get gmeta m_expe 0 |
| 899 | ret 2 |
| 900 | end |
| 901 | if eq get rungseen r_pin 0 |
| 902 | ret 2 |
| 903 | end |
| 904 | chain_ok = true |
| 905 | for r ulong 5 |
| 906 | if eq get rungseen r 0 |
| 907 | chain_ok = false |
| 908 | end |
| 909 | end |
| 910 | if not chain_ok |
| 911 | put gmeta m_bad add get gmeta m_bad 1 |
| 912 | end |
| 913 | pat_clear |
| 914 | pat_push_lit t_pin |
| 915 | pat_push_pin |
| 916 | if not log_has_pat false |
| 917 | put gmeta m_bad add get gmeta m_bad 1 |
| 918 | end |
| 919 | if not log_has_aarch64_host |
| 920 | put gmeta m_bad add get gmeta m_bad 1 |
| 921 | end |
| 922 | if log_has_fail_or_notrun |
| 923 | put gmeta m_bad add get gmeta m_bad 1 |
| 924 | end |
| 925 | if not log_check_last_challenge |
| 926 | put gmeta m_bad add get gmeta m_bad 1 |
| 927 | end |
| 928 | if eq get gmeta m_bad 0 |
| 929 | ret 0 |
| 930 | end |
| 931 | ret 1 |
| 932 | end |
| 933 | |
| 934 | fn load_valid_exp to void |
| 935 | exp_reset |
| 936 | exp_append exp_v1_a |
| 937 | exp_append exp_v1_b |
| 938 | exp_append exp_v1_c |
| 939 | end |
| 940 | |
| 941 | fn load_valid_log to void |
| 942 | log_reset |
| 943 | log_append log_v1_a |
| 944 | log_append log_v1_b |
| 945 | log_append log_v1_c |
| 946 | log_append log_v1_d |
| 947 | log_append log_v1_e |
| 948 | end |
| 949 | |
| 950 | fn test_sha_and_flip to void |
| 951 | note Test 1: SHA-256 KATs (empty and "abc") |
| 952 | empty_arr = new uchar 4 0 |
| 953 | if not verify_rung_bytes empty_arr 0 sha_empty |
| 954 | fail_case 1 |
| 955 | end |
| 956 | if not verify_rung_bytes msg_abc 3 sha_abc |
| 957 | fail_case 1 |
| 958 | end |
| 959 | wstr 1 p_sha_kat |
| 960 | note Test 2: negative control - flip 1 bit in rung bytes ("abc" -> "bbc") must fail |
| 961 | rung_buf = new uchar 8 0 |
| 962 | put rung_buf 0 97 |
| 963 | put rung_buf 1 98 |
| 964 | put rung_buf 2 99 |
| 965 | if not verify_rung_bytes rung_buf 3 sha_abc |
| 966 | fail_case 2 |
| 967 | end |
| 968 | put rung_buf 0 98 |
| 969 | if verify_rung_bytes rung_buf 3 sha_abc |
| 970 | fail_case 2 |
| 971 | end |
| 972 | wstr 1 p_flip_byte |
| 973 | end |
| 974 | |
| 975 | fn test_hex_formats to void |
| 976 | note Test 3: strict 64-hex and 40-hex lowercase format validation |
| 977 | if not is_hex_view sha_abc 64 |
| 978 | fail_case 3 |
| 979 | end |
| 980 | if not is_hex_view pin_ok 40 |
| 981 | fail_case 3 |
| 982 | end |
| 983 | if is_hex_view hex_short 64 |
| 984 | fail_case 3 |
| 985 | end |
| 986 | if is_hex_view hex_upper 64 |
| 987 | fail_case 3 |
| 988 | end |
| 989 | if is_hex_view hex_badch 64 |
| 990 | fail_case 3 |
| 991 | end |
| 992 | wstr 1 p_hex_fmt |
| 993 | end |
| 994 | |
| 995 | fn main to int |
| 996 | test_sha_and_flip |
| 997 | test_hex_formats |
| 998 | |
| 999 | note Test 4: positive full v1 ladder + DGX Spark log |
| 1000 | load_valid_exp |
| 1001 | load_valid_log |
| 1002 | if ne verify_loaded 0 |
| 1003 | fail_case 4 |
| 1004 | end |
| 1005 | wstr 1 p_valid_v1 |
| 1006 | |
| 1007 | note Test 5: negative control - forged stage2 omc digest |
| 1008 | load_valid_exp |
| 1009 | log_reset |
| 1010 | log_append log_v1_a |
| 1011 | log_append log_v1_b |
| 1012 | log_append log_v1_c |
| 1013 | log_append log_bad_st2 |
| 1014 | log_append log_v1_e |
| 1015 | if ne verify_loaded 1 |
| 1016 | fail_case 5 |
| 1017 | end |
| 1018 | wstr 1 p_neg_st2 |
| 1019 | |
| 1020 | note Test 6: negative control - incomplete rung chain (missing asm2) |
| 1021 | exp_reset |
| 1022 | exp_append exp_v1_a |
| 1023 | exp_append exp_v1_b |
| 1024 | exp_append exp_no_asm2 |
| 1025 | load_valid_log |
| 1026 | if ne verify_loaded 1 |
| 1027 | fail_case 6 |
| 1028 | end |
| 1029 | wstr 1 p_neg_chain |
| 1030 | |
| 1031 | note Test 7: negative control - injected FAIL line in log |
| 1032 | load_valid_exp |
| 1033 | load_valid_log |
| 1034 | log_append log_inj_fail |
| 1035 | if ne verify_loaded 1 |
| 1036 | fail_case 7 |
| 1037 | end |
| 1038 | wstr 1 p_neg_fail |
| 1039 | |
| 1040 | note Test 8: negative control - non-aarch64 host line |
| 1041 | load_valid_exp |
| 1042 | log_reset |
| 1043 | log_append log_x86_host |
| 1044 | log_append log_v1_b |
| 1045 | log_append log_v1_c |
| 1046 | log_append log_v1_d |
| 1047 | log_append log_v1_e |
| 1048 | if ne verify_loaded 1 |
| 1049 | fail_case 8 |
| 1050 | end |
| 1051 | wstr 1 p_neg_host |
| 1052 | |
| 1053 | note Test 9: negative control - malformed expected digest (uppercase hex) |
| 1054 | exp_reset |
| 1055 | exp_append exp_bad_hex |
| 1056 | load_valid_log |
| 1057 | if ne verify_loaded 2 |
| 1058 | fail_case 9 |
| 1059 | end |
| 1060 | wstr 1 p_neg_exp |
| 1061 | |
| 1062 | note Test 10: negative control - forged final CHALLENGE footer |
| 1063 | load_valid_exp |
| 1064 | log_reset |
| 1065 | log_append log_v1_a |
| 1066 | log_append log_v1_b |
| 1067 | log_append log_v1_c |
| 1068 | log_append log_v1_d |
| 1069 | log_append log_bad_foot |
| 1070 | if ne verify_loaded 1 |
| 1071 | fail_case 10 |
| 1072 | end |
| 1073 | wstr 1 p_neg_foot |
| 1074 | |
| 1075 | wstr 1 p_all |
| 1076 | ret 0 |
| 1077 | end |