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-artifact-tool / artifact_tool_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-artifact-tool/artifact_tool_test.om: Behavioral self-test for aienos-artifact-tool. |
| 3 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-artifact-tool/artifact_tool.om aienux/crates/aienos-artifact-tool/artifact_tool_test.om |
| 4 | |
| 5 | note Golden artifact_id hex from main.rs::tests::deterministic_pack_uses_the_shared_parser_and_id |
| 6 | text t_golden_id "71976b5c38ec15f2307565ed3eea51fde4a5375970cd49338d6e16166238cf4b" |
| 7 | text t_data4 "DATA" |
| 8 | text t_bad_cmd "unknown" |
| 9 | text t_arg_x "x" |
| 10 | text t_ws_pre " \n\t" |
| 11 | |
| 12 | fn t_test_pack_and_golden_id to int |
| 13 | at_manifest_reset |
| 14 | at_manifest_set_entry 0 |
| 15 | idx = at_manifest_push_cap 3 7 1 1 4 |
| 16 | if lt idx 0 |
| 17 | ret 1 |
| 18 | end |
| 19 | at_manifest_set_cap_bounds ulong idx 4 0 4 |
| 20 | at_manifest_set_res_pages 1 1 1 1 |
| 21 | at_manifest_set_res_limits 2 16 1000 2000 16 |
| 22 | code = new uchar 4 0 |
| 23 | put code 0 0xc0 |
| 24 | put code 1 0x03 |
| 25 | put code 2 0x5f |
| 26 | put code 3 0xd6 |
| 27 | b1 = new uchar 512 0 |
| 28 | b2 = new uchar 512 0 |
| 29 | if ne at_pack code t_data4 b1 AT_OK |
| 30 | ret 2 |
| 31 | end |
| 32 | n1 = get at_pack_len 0 |
| 33 | if ne at_pack code t_data4 b2 AT_OK |
| 34 | ret 3 |
| 35 | end |
| 36 | n2 = get at_pack_len 0 |
| 37 | if ne n1 n2 |
| 38 | ret 4 |
| 39 | end |
| 40 | if not at_bytes_eq part b1 0 n1 part b2 0 n2 |
| 41 | ret 5 |
| 42 | end |
| 43 | hex_buf = new uchar 64 0 |
| 44 | if ne at_id_hex part b1 0 n1 hex_buf AT_OK |
| 45 | ret 6 |
| 46 | end |
| 47 | if not at_bytes_eq hex_buf t_golden_id |
| 48 | ret 7 |
| 49 | end |
| 50 | if ne at_inspect_artifact part b1 0 n1 AT_OK |
| 51 | ret 8 |
| 52 | end |
| 53 | if ne get at_art_meta 2 1 |
| 54 | ret 9 |
| 55 | end |
| 56 | if ne get at_art_meta 7 4 |
| 57 | ret 10 |
| 58 | end |
| 59 | if ne get at_art_meta 9 4 |
| 60 | ret 11 |
| 61 | end |
| 62 | ret 0 |
| 63 | end |
| 64 | |
| 65 | fn t_test_sign_and_verify to int |
| 66 | at_manifest_reset |
| 67 | at_manifest_set_res_pages 1 1 1 0 |
| 68 | at_manifest_set_res_limits 0 0 1 1 1 |
| 69 | code = new uchar 4 0 |
| 70 | put code 0 0xc0 |
| 71 | put code 1 0x03 |
| 72 | put code 2 0x5f |
| 73 | put code 3 0xd6 |
| 74 | buf = new uchar 512 0 |
| 75 | if ne at_pack code t_data4 buf AT_OK |
| 76 | ret 12 |
| 77 | end |
| 78 | n = get at_pack_len 0 |
| 79 | note Production mode refuses signing and fails closed on verify |
| 80 | if ne at_sign_artifact part buf 0 n false AT_ERR_PROD_SIGN_OFF |
| 81 | ret 13 |
| 82 | end |
| 83 | if ne at_sign_artifact part buf 0 n true AT_OK |
| 84 | ret 14 |
| 85 | end |
| 86 | if ne at_verify_artifact part buf 0 n true AT_OK |
| 87 | ret 15 |
| 88 | end |
| 89 | if ne at_verify_artifact part buf 0 n false AT_ERR_UNTRUSTED |
| 90 | ret 16 |
| 91 | end |
| 92 | note Mutate one payload byte -> BadSignature (18) |
| 93 | pay_off = get at_art_meta 4 |
| 94 | put buf pay_off xor get buf pay_off 1 |
| 95 | if ne at_verify_artifact part buf 0 n true AT_ERR_BAD_SIG |
| 96 | ret 17 |
| 97 | end |
| 98 | note Restore payload, mutate one signature byte -> BadSignature (18) |
| 99 | put buf pay_off xor get buf pay_off 1 |
| 100 | sig_off = get at_art_meta 6 |
| 101 | put buf add sig_off 40 xor get buf add sig_off 40 1 |
| 102 | if ne at_verify_artifact part buf 0 n true AT_ERR_BAD_SIG |
| 103 | ret 18 |
| 104 | end |
| 105 | ret 0 |
| 106 | end |
| 107 | |
| 108 | fn t_test_corpus_all_29 to int |
| 109 | base = new uchar 600 0 |
| 110 | if ne at_corpus_build_baseline base AT_OK |
| 111 | ret 20 |
| 112 | end |
| 113 | bn = get at_corpus_len 0 |
| 114 | if ne at_verify_artifact part base 0 bn true AT_OK |
| 115 | ret 21 |
| 116 | end |
| 117 | c1 = new uchar 600 0 |
| 118 | c2 = new uchar 600 0 |
| 119 | for i ulong AT_CORPUS_COUNT |
| 120 | cid = add i 1 |
| 121 | if ne at_corpus_build_case cid c1 AT_OK |
| 122 | ret 22 |
| 123 | end |
| 124 | n1 = get at_corpus_len 0 |
| 125 | if ne at_corpus_build_case cid c2 AT_OK |
| 126 | ret 23 |
| 127 | end |
| 128 | n2 = get at_corpus_len 0 |
| 129 | if ne n1 n2 |
| 130 | ret 24 |
| 131 | end |
| 132 | if not at_bytes_eq part c1 0 n1 part c2 0 n2 |
| 133 | ret 25 |
| 134 | end |
| 135 | note Every corpus case must differ from baseline |
| 136 | if at_bytes_eq part c1 0 n1 part base 0 bn |
| 137 | ret 26 |
| 138 | end |
| 139 | if ne at_corpus_expect cid AT_OK |
| 140 | ret 27 |
| 141 | end |
| 142 | ek = get at_corpus_exp 0 |
| 143 | ecode = int get at_corpus_exp 1 |
| 144 | vrc = at_verify_artifact part c1 0 n1 true |
| 145 | if eq ek AT_EXP_REJ_VERIFIED |
| 146 | if ne vrc ecode |
| 147 | ret add 30 int cid |
| 148 | end |
| 149 | else |
| 150 | if ne vrc AT_OK |
| 151 | ret add 60 int cid |
| 152 | end |
| 153 | end |
| 154 | end |
| 155 | note Invalid case IDs rejected |
| 156 | if ne at_corpus_build_case 0 c1 AT_ERR_BAD_CASE |
| 157 | ret 28 |
| 158 | end |
| 159 | if ne at_corpus_build_case 30 c1 AT_ERR_BAD_CASE |
| 160 | ret 29 |
| 161 | end |
| 162 | ret 0 |
| 163 | end |
| 164 | |
| 165 | fn t_stage_binding_rcp art view uchar * admitted bool to int |
| 166 | ver_id = new uchar 32 7 |
| 167 | nonce = new uchar 16 0 |
| 168 | tmp = new uchar 32 0 |
| 169 | pol = new uchar 32 1 |
| 170 | gr = new uchar 32 2 |
| 171 | if ne at_parse_and_identify art AT_OK |
| 172 | ret 90 |
| 173 | end |
| 174 | at_rcp_reset |
| 175 | put at_rcp_f 0 0 |
| 176 | if admitted |
| 177 | put at_rcp_f 1 AT_DEC_ADMITTED |
| 178 | put at_rcp_f 5 AT_EXEC_EXITED |
| 179 | put at_rcp_f 8 0 |
| 180 | put at_rcp_f 9 0 |
| 181 | else |
| 182 | put at_rcp_f 1 AT_DEC_REJECTED |
| 183 | put at_rcp_f 5 AT_EXEC_NOT_RUN |
| 184 | put at_rcp_f 8 3 |
| 185 | put at_rcp_f 9 17 |
| 186 | end |
| 187 | put at_rcp_f 2 AT_TIER_QEMU |
| 188 | put at_rcp_f 3 1 |
| 189 | put at_rcp_f 7 AT_RCP_RES_RECLAIMED |
| 190 | at_rcp_nonce ver_id 1 nonce |
| 191 | for i ulong 16 |
| 192 | put at_rcp_nonce_buf i get nonce i |
| 193 | end |
| 194 | for i ulong 32 |
| 195 | put tmp i get at_art_id i |
| 196 | end |
| 197 | at_rcp_set_digest 0 tmp |
| 198 | for i ulong 32 |
| 199 | put tmp i get at_art_payload_sha i |
| 200 | end |
| 201 | at_rcp_set_digest 1 tmp |
| 202 | for i ulong 32 |
| 203 | put tmp i get at_art_signer_fp i |
| 204 | end |
| 205 | at_rcp_set_digest 2 tmp |
| 206 | at_rcp_set_digest 3 pol |
| 207 | for i ulong 32 |
| 208 | put tmp i get at_art_req_caps_sha i |
| 209 | end |
| 210 | at_rcp_set_digest 4 tmp |
| 211 | if admitted |
| 212 | at_rcp_set_digest 5 gr |
| 213 | end |
| 214 | for i ulong 32 |
| 215 | put tmp i get at_art_res_env_sha i |
| 216 | end |
| 217 | at_rcp_set_digest 6 tmp |
| 218 | at_rcp_set_digest 7 ver_id |
| 219 | ret 0 |
| 220 | end |
| 221 | |
| 222 | fn t_test_receipt_check to int |
| 223 | art = new uchar 600 0 |
| 224 | if ne at_corpus_build_baseline art AT_OK |
| 225 | ret 91 |
| 226 | end |
| 227 | an = get at_corpus_len 0 |
| 228 | note 1. Valid admitted receipt binds supplied artifact |
| 229 | if ne t_stage_binding_rcp part art 0 an true 0 |
| 230 | ret 92 |
| 231 | end |
| 232 | rec = new uchar 512 0 |
| 233 | if ne at_rcp_encode rec AT_OK |
| 234 | ret 93 |
| 235 | end |
| 236 | if ne at_rcp_check rec part art 0 an AT_OK |
| 237 | ret 94 |
| 238 | end |
| 239 | note 2. Valid rejected receipt with zero signer_fp is allowed |
| 240 | if ne t_stage_binding_rcp part art 0 an false 0 |
| 241 | ret 95 |
| 242 | end |
| 243 | z32 = new uchar 32 0 |
| 244 | at_rcp_set_digest 2 z32 |
| 245 | if ne at_rcp_encode rec AT_OK |
| 246 | ret 96 |
| 247 | end |
| 248 | if ne at_rcp_check rec part art 0 an AT_OK |
| 249 | ret 97 |
| 250 | end |
| 251 | note 3. Wrong artifact_id rejected |
| 252 | if ne t_stage_binding_rcp part art 0 an true 0 |
| 253 | ret 98 |
| 254 | end |
| 255 | bad9 = new uchar 32 9 |
| 256 | at_rcp_set_digest 0 bad9 |
| 257 | if ne at_rcp_encode rec AT_OK |
| 258 | ret 99 |
| 259 | end |
| 260 | if ne at_rcp_check rec part art 0 an AT_ERR_RCP_BIND_MISMATCH |
| 261 | ret 100 |
| 262 | end |
| 263 | note 4. Zero payload_digest in admitted receipt rejected |
| 264 | if ne t_stage_binding_rcp part art 0 an true 0 |
| 265 | ret 101 |
| 266 | end |
| 267 | at_rcp_set_digest 1 z32 |
| 268 | if ne at_rcp_encode rec AT_OK |
| 269 | ret 102 |
| 270 | end |
| 271 | if ne at_rcp_check rec part art 0 an AT_ERR_RCP_BIND_MISMATCH |
| 272 | ret 103 |
| 273 | end |
| 274 | note 5. Admitted receipt without granted_capability_digest rejected |
| 275 | if ne t_stage_binding_rcp part art 0 an true 0 |
| 276 | ret 104 |
| 277 | end |
| 278 | at_rcp_set_digest 5 z32 |
| 279 | if ne at_rcp_encode rec AT_OK |
| 280 | ret 105 |
| 281 | end |
| 282 | if ne at_rcp_check rec part art 0 an AT_ERR_RCP_NO_GRANTS |
| 283 | ret 106 |
| 284 | end |
| 285 | note 6. Signed receipt rejected by kernel-emitted check |
| 286 | if ne t_stage_binding_rcp part art 0 an true 0 |
| 287 | ret 107 |
| 288 | end |
| 289 | if ne at_rcp_encode rec AT_OK |
| 290 | ret 108 |
| 291 | end |
| 292 | put rec 400 1 |
| 293 | if ne at_rcp_check rec part art 0 an AT_ERR_RCP_MUST_UNSIGNED |
| 294 | ret 109 |
| 295 | end |
| 296 | note 7. Mutated artifact fails binding check |
| 297 | put rec 400 0 |
| 298 | last_p = sub an 101 |
| 299 | put art last_p xor get art last_p 1 |
| 300 | if ne at_rcp_check rec part art 0 an AT_ERR_RCP_BIND_MISMATCH |
| 301 | ret 110 |
| 302 | end |
| 303 | note 8. Unparseable artifact: admitted rejected, rejected with zero fields accepted |
| 304 | put art 0 88 |
| 305 | if ne at_rcp_check rec part art 0 an AT_ERR_RCP_ADMIT_BAD_ART |
| 306 | ret 111 |
| 307 | end |
| 308 | at_rcp_reset |
| 309 | put at_rcp_f 1 AT_DEC_REJECTED |
| 310 | put at_rcp_f 2 AT_TIER_QEMU |
| 311 | put at_rcp_f 3 2 |
| 312 | put at_rcp_f 5 AT_EXEC_NOT_RUN |
| 313 | put at_rcp_f 8 1 |
| 314 | put at_rcp_f 9 1 |
| 315 | ver_id = new uchar 32 7 |
| 316 | nonce = new uchar 16 0 |
| 317 | at_rcp_nonce ver_id 2 nonce |
| 318 | for i ulong 16 |
| 319 | put at_rcp_nonce_buf i get nonce i |
| 320 | end |
| 321 | at_rcp_set_digest 7 ver_id |
| 322 | if ne at_rcp_encode rec AT_OK |
| 323 | ret 112 |
| 324 | end |
| 325 | if ne at_rcp_check rec part art 0 an AT_OK |
| 326 | ret 113 |
| 327 | end |
| 328 | ret 0 |
| 329 | end |
| 330 | |
| 331 | fn t_test_receipt_sign_verify_hex to int |
| 332 | art = new uchar 600 0 |
| 333 | if ne at_corpus_build_baseline art AT_OK |
| 334 | ret 120 |
| 335 | end |
| 336 | an = get at_corpus_len 0 |
| 337 | if ne t_stage_binding_rcp part art 0 an true 0 |
| 338 | ret 121 |
| 339 | end |
| 340 | rec = new uchar 512 0 |
| 341 | if ne at_rcp_encode rec AT_OK |
| 342 | ret 122 |
| 343 | end |
| 344 | note Inspect unsigned receipt |
| 345 | if ne at_rcp_inspect rec AT_OK |
| 346 | ret 123 |
| 347 | end |
| 348 | if ne get at_rcp_signed_flag 0 0 |
| 349 | ret 124 |
| 350 | end |
| 351 | note Unsigned receipt fails verify with BadSignatureFormat (15) |
| 352 | if ne at_rcp_verify rec true AT_ERR_BAD_SIG_FMT |
| 353 | ret 125 |
| 354 | end |
| 355 | note Production sign-test fails closed |
| 356 | if ne at_rcp_sign_test rec false AT_ERR_PROD_SIGN_OFF |
| 357 | ret 126 |
| 358 | end |
| 359 | note Test sign-test succeeds and verifies in test mode, fails closed in prod mode |
| 360 | if ne at_rcp_sign_test rec true AT_OK |
| 361 | ret 127 |
| 362 | end |
| 363 | if ne at_rcp_inspect rec AT_OK |
| 364 | ret 128 |
| 365 | end |
| 366 | if ne get at_rcp_signed_flag 0 1 |
| 367 | ret 129 |
| 368 | end |
| 369 | if ne at_rcp_verify rec true AT_OK |
| 370 | ret 130 |
| 371 | end |
| 372 | if ne at_rcp_verify rec false AT_ERR_UNTRUSTED |
| 373 | ret 131 |
| 374 | end |
| 375 | note Re-signing an already-signed receipt fails with BadSignatureFormat (15) |
| 376 | if ne at_rcp_sign_test rec true AT_ERR_BAD_SIG_FMT |
| 377 | ret 132 |
| 378 | end |
| 379 | note Roundtrip through from-hex with leading/trailing whitespace |
| 380 | hex_full = new uchar 1031 0 |
| 381 | for i ulong 4 |
| 382 | put hex_full i get t_ws_pre i |
| 383 | end |
| 384 | if ne at_hex_encode rec part hex_full 4 1024 AT_OK |
| 385 | ret 133 |
| 386 | end |
| 387 | put hex_full 1028 13 |
| 388 | put hex_full 1029 10 |
| 389 | put hex_full 1030 32 |
| 390 | dec_rec = new uchar 512 0 |
| 391 | if ne at_rcp_from_hex hex_full dec_rec AT_OK |
| 392 | ret 134 |
| 393 | end |
| 394 | if not at_bytes_eq rec dec_rec |
| 395 | ret 135 |
| 396 | end |
| 397 | note Uppercase hex rejected by from-hex |
| 398 | put hex_full 4 65 |
| 399 | if ne at_rcp_from_hex hex_full dec_rec AT_ERR_BAD_HEX |
| 400 | ret 136 |
| 401 | end |
| 402 | note Wrong length hex rejected |
| 403 | if ne at_rcp_from_hex part hex_full 4 100 dec_rec AT_ERR_BAD_HEX |
| 404 | ret 137 |
| 405 | end |
| 406 | note Tampered receipt signature rejected with BadSignature (18) |
| 407 | put rec 440 xor get rec 440 1 |
| 408 | if ne at_rcp_verify rec true AT_ERR_BAD_SIG |
| 409 | ret 138 |
| 410 | end |
| 411 | ret 0 |
| 412 | end |
| 413 | |
| 414 | fn t_test_cli_dispatch to int |
| 415 | if ne at_cli_dispatch 6 at_kw_pack t_arg_x false AT_CMD_PACK |
| 416 | ret 140 |
| 417 | end |
| 418 | if ne at_cli_dispatch 5 at_kw_pack t_arg_x false AT_CMD_INVALID |
| 419 | ret 141 |
| 420 | end |
| 421 | if ne at_cli_dispatch 3 at_kw_inspect t_arg_x false AT_CMD_INSPECT |
| 422 | ret 142 |
| 423 | end |
| 424 | if ne at_cli_dispatch 3 at_kw_id t_arg_x false AT_CMD_ID |
| 425 | ret 143 |
| 426 | end |
| 427 | if ne at_cli_dispatch 4 at_kw_sign t_arg_x false AT_CMD_SIGN |
| 428 | ret 144 |
| 429 | end |
| 430 | if ne at_cli_dispatch 3 at_kw_verify t_arg_x false AT_CMD_VERIFY |
| 431 | ret 145 |
| 432 | end |
| 433 | if ne at_cli_dispatch 3 at_kw_neg_corpus t_arg_x false AT_CMD_INVALID |
| 434 | ret 146 |
| 435 | end |
| 436 | if ne at_cli_dispatch 3 at_kw_neg_corpus t_arg_x true AT_CMD_NEG_CORPUS |
| 437 | ret 147 |
| 438 | end |
| 439 | if ne at_cli_dispatch 4 at_kw_receipt at_kw_inspect false AT_CMD_RCP_INSPECT |
| 440 | ret 148 |
| 441 | end |
| 442 | if ne at_cli_dispatch 5 at_kw_receipt at_kw_check false AT_CMD_RCP_CHECK |
| 443 | ret 149 |
| 444 | end |
| 445 | if ne at_cli_dispatch 5 at_kw_receipt at_kw_from_hex false AT_CMD_RCP_FROM_HEX |
| 446 | ret 150 |
| 447 | end |
| 448 | if ne at_cli_dispatch 5 at_kw_receipt at_kw_sign_test false AT_CMD_RCP_SIGN_TEST |
| 449 | ret 151 |
| 450 | end |
| 451 | if ne at_cli_dispatch 4 at_kw_receipt at_kw_verify false AT_CMD_RCP_VERIFY |
| 452 | ret 152 |
| 453 | end |
| 454 | if ne at_cli_dispatch 3 at_kw_receipt at_kw_verify false AT_CMD_INVALID |
| 455 | ret 153 |
| 456 | end |
| 457 | if ne at_cli_dispatch 3 t_bad_cmd t_arg_x true AT_CMD_INVALID |
| 458 | ret 154 |
| 459 | end |
| 460 | ret 0 |
| 461 | end |
| 462 | |
| 463 | fn main to int |
| 464 | rc = t_test_pack_and_golden_id |
| 465 | if ne rc 0 |
| 466 | ret rc |
| 467 | end |
| 468 | rc = t_test_sign_and_verify |
| 469 | if ne rc 0 |
| 470 | ret rc |
| 471 | end |
| 472 | rc = t_test_corpus_all_29 |
| 473 | if ne rc 0 |
| 474 | ret rc |
| 475 | end |
| 476 | rc = t_test_receipt_check |
| 477 | if ne rc 0 |
| 478 | ret rc |
| 479 | end |
| 480 | rc = t_test_receipt_sign_verify_hex |
| 481 | if ne rc 0 |
| 482 | ret rc |
| 483 | end |
| 484 | rc = t_test_cli_dispatch |
| 485 | if ne rc 0 |
| 486 | ret rc |
| 487 | end |
| 488 | ret 0 |
| 489 | end |