Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/crates/aienos-kernel/sec_continuity.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / crates / aienos-kernel / sec_continuity.om` 2460 lines · 57651 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/crates/aienos-kernel/sec_continuity.om: 3 note Pure OM-2 port of crates/aienos-kernel Security, Admission, Artifact Loader, 4 note Continuity, Recovery, Budget, and Kernel Crypto subsystems. 5 note 6 note Library unit: no om2 header, no fn main, no globals, no structs. 7 note Compose with: si/directives/om2.om om/tls/sha2.om om/tls/aesgcm.om om/tls/gcmsiv.om 8 note Prefix: ksc_ / KSC_. 9 10 note ========================================================================= 11 note Constants and domain-separation texts 12 note ========================================================================= 13 14 const KSC_HKDF_OK ulong 0 15 const KSC_HKDF_ERR_TOO_LONG ulong 1 16 17 const KSC_ENV_VERSION ulong 1 18 const KSC_ENV_SUITE_GCMSIV ulong 1 19 const KSC_ENV_DEFAULT_CHUNK ulong 65536 20 const KSC_ENV_HDR_LEN ulong 64 21 const KSC_ENV_TAG_LEN ulong 16 22 const KSC_ENV_AAD_LEN ulong 111 23 const KSC_ENV_MAX_PT ulong `67108864` 24 25 const KSC_ENV_OK ulong 0 26 const KSC_ENV_ERR_SHORT ulong 1 27 const KSC_ENV_ERR_MAGIC ulong 2 28 const KSC_ENV_ERR_VER ulong 3 29 const KSC_ENV_ERR_SUITE ulong 4 30 const KSC_ENV_ERR_FLAGS ulong 5 31 const KSC_ENV_ERR_CHUNK ulong 6 32 const KSC_ENV_ERR_RSVD ulong 7 33 const KSC_ENV_ERR_LEN ulong 8 34 const KSC_ENV_ERR_AUTH ulong 9 35 const KSC_ENV_ERR_TOO_LARGE ulong 10 36 const KSC_ENV_ERR_OVERFLOW ulong 11 37 38 const KSC_KIND_AGENT_ROOT ulong 16 39 const KSC_KIND_MANIFEST ulong 17 40 const KSC_KIND_AGENT_STATE ulong 18 41 const KSC_KIND_CORTEX_WAL ulong 20 42 const KSC_KIND_KEYSLOT ulong 21 43 const KSC_KIND_SECURITY ulong 22 44 const KSC_KIND_MIGRATION ulong 23 45 46 const KSC_SEC_VER ulong 1 47 const KSC_SEC_MANIFEST_SIZE ulong 256 48 const KSC_MIG_MANIFEST_SIZE ulong 288 49 const KSC_KEYSLOT_HDR_SIZE ulong 128 50 const KSC_KEYSLOT_DESC_SIZE ulong 128 51 const KSC_KEYSLOT_MAX ulong 4 52 const KSC_KEYSLOT_MAX_ARENA ulong 32768 53 54 const KSC_SEC_OK ulong 0 55 const KSC_SEC_ERR_FORMAT ulong 1 56 const KSC_SEC_ERR_SHORT ulong 2 57 const KSC_SEC_ERR_MAGIC ulong 3 58 const KSC_SEC_ERR_VER ulong 4 59 const KSC_SEC_ERR_RSVD ulong 5 60 const KSC_SEC_ERR_AUTH ulong 6 61 const KSC_SEC_ERR_SLOT_RANGE ulong 7 62 const KSC_SEC_ERR_ARENA ulong 8 63 64 const KSC_ARB_VALID_RESUME ulong 0 65 const KSC_ARB_PREPARED_ADVANCE ulong 1 66 const KSC_ARB_ROLLBACK_STALE ulong 2 67 const KSC_ARB_ROLLBACK_DIGEST ulong 3 68 const KSC_ARB_INCONSISTENT_EPOCH ulong 4 69 70 const KSC_BMODE_ACTIVE ulong 0 71 const KSC_BMODE_IDLE ulong 1 72 const KSC_BMODE_URGENT ulong 2 73 74 const KSC_WSTATE_EMPTY ulong 0 75 const KSC_WSTATE_ACTIVE ulong 1 76 const KSC_WSTATE_QUARANTINED ulong 2 77 78 const KSC_BERR_OK ulong 0 79 const KSC_BERR_FULL ulong 1 80 const KSC_BERR_DUP ulong 2 81 const KSC_BERR_UNKNOWN ulong 3 82 const KSC_BERR_CPU ulong 4 83 const KSC_BERR_FRAMES ulong 5 84 const KSC_BERR_DMA ulong 6 85 const KSC_BERR_QUARANTINED ulong 7 86 const KSC_BERR_BAD_RELEASE ulong 8 87 88 const KSC_SLOT_A ulong 0 89 const KSC_SLOT_B ulong 1 90 const KSC_HEALTH_UNVERIFIED ulong 0 91 const KSC_HEALTH_CONFIRMED ulong 1 92 const KSC_HEALTH_FAILED ulong 2 93 94 const KSC_MOUNT_NONE ulong 0 95 const KSC_MOUNT_VALID ulong 1 96 const KSC_MOUNT_DEGRADED ulong 2 97 98 const KSC_PEER_ZERO ulong 0 99 const KSC_PEER_MALFORMED ulong 1 100 const KSC_PEER_GRAPH_BAD_NEWER ulong 2 101 const KSC_PEER_CLEAN ulong 3 102 103 const KSC_REASON_NONE ulong 0 104 const KSC_REASON_UNFORMATTED ulong 1 105 const KSC_REASON_MOUNT_ERR ulong 2 106 const KSC_REASON_UNPROVISIONED ulong 3 107 const KSC_REASON_CONFLICT ulong 4 108 const KSC_REASON_CORRUPT ulong 5 109 const KSC_REASON_DEGRADED ulong 6 110 111 const KSC_ACT_NONE ulong 0 112 const KSC_ACT_REPAIR_DEGRADED_PEER ulong 1 113 const KSC_ACT_PROVISION_IDENTITY ulong 2 114 115 const KSC_REC_OK ulong 0 116 const KSC_REC_ERR_NOT_APPLICABLE ulong 1 117 const KSC_REC_ERR_UNAUTHORISED ulong 2 118 119 const KSC_CONT_HDR_BYTES ulong 16 120 const KSC_CONT_BRANCH_BYTES ulong 80 121 const KSC_CONT_MAX_WAL_SEGS ulong 64 122 const KSC_CONT_MAX_BRANCHES ulong 256 123 const KSC_CONT_MAX_WAL_RECS ulong 64 124 const KSC_CONT_MAX_STMT_BYTES ulong 1024 125 126 const KSC_CERR_OK ulong 0 127 const KSC_CERR_BAD_MAGIC ulong 1 128 const KSC_CERR_BAD_VER ulong 2 129 const KSC_CERR_HDR_RSVD ulong 3 130 const KSC_CERR_BODY_LEN ulong 4 131 const KSC_CERR_TRUNCATED ulong 5 132 const KSC_CERR_RSVD ulong 6 133 const KSC_CERR_TRAILING ulong 7 134 const KSC_CERR_BAD_SRC ulong 8 135 const KSC_CERR_ZERO_AGENT ulong 9 136 const KSC_CERR_ROOT_BRANCH ulong 10 137 const KSC_CERR_TOO_MANY_WAL ulong 11 138 const KSC_CERR_SEQ_PREV ulong 12 139 const KSC_CERR_BRANCH_COUNT ulong 13 140 const KSC_CERR_NOT_SORTED ulong 14 141 const KSC_CERR_BAD_ROOT ulong 15 142 const KSC_CERR_NO_PARENT ulong 16 143 const KSC_CERR_LINEAGE ulong 17 144 const KSC_CERR_FORK_SUM ulong 18 145 const KSC_CERR_WAL_COUNT ulong 19 146 const KSC_CERR_EPISTEMIC ulong 20 147 const KSC_CERR_STMT_LEN ulong 21 148 149 const KSC_MAX_POLICY_RESOURCES ulong 16 150 const KSC_MAX_POLICY_SIGNERS ulong 8 151 const KSC_POLICY_HDR_SIZE ulong 64 152 const KSC_POLICY_REC_SIZE ulong 48 153 const KSC_POLICY_SIGNER_SIZE ulong 32 154 const KSC_MAX_CAPABILITIES ulong 16 155 const KSC_CAP_CHANNEL ulong 2 156 const KSC_CAP_OBJECT ulong 3 157 const KSC_RIGHT_READ ulong 1 158 const KSC_RIGHT_WRITE ulong 2 159 const KSC_VALID_RIGHTS ulong 63 160 161 const KSC_PAGE_SIZE ulong 4096 162 const KSC_MAX_CODE_PAGES ulong 64 163 const KSC_MAX_DATA_PAGES ulong 64 164 const KSC_MAX_STACK_PAGES ulong 16 165 const KSC_MAX_IPC_MSGS ulong 1024 166 const KSC_MAX_IPC_BYTES ulong `1048576` 167 const KSC_MAX_CPU_TICKS ulong `1000000000` 168 const KSC_MAX_ELAPSED_TICKS ulong `1000000000` 169 const KSC_MAX_SYSCALLS ulong 65536 170 const KSC_MAX_PAYLOAD_SIZE ulong `8388608` 171 const KSC_TARGET_AARCH64_LE ulong 1 172 const KSC_ABI_V1 ulong 1 173 174 const KSC_AERR_OK ulong 0 175 const KSC_AERR_WRONG_TARGET ulong 3 176 const KSC_AERR_WRONG_ABI ulong 4 177 const KSC_AERR_LEN_OVERFLOW ulong 5 178 const KSC_AERR_BAD_ENTRY ulong 12 179 const KSC_AERR_MALFORMED_CAP ulong 13 180 const KSC_AERR_RESOURCE_LIMIT ulong 14 181 const KSC_AERR_UNTRUSTED_SIGNER ulong 17 182 const KSC_AERR_RIGHTS_ESC ulong 19 183 184 const KSC_TABLE_FRAMES ulong 4 185 const KSC_AP_EL0 ulong 0x40 186 const KSC_AP_RO ulong 0x80 187 const KSC_PXN ulong 0x0020000000000000 188 const KSC_UXN ulong 0x0040000000000000 189 const KSC_ADDR_MASK ulong 0x0000fffffffff000 190 const KSC_DESC_TABLE ulong 3 191 192 const KSC_FAULT_CODE_WRITE ulong 1 193 const KSC_FAULT_EXEC_NX ulong 2 194 const KSC_FAULT_EXEC_UNMAPPED ulong 3 195 const KSC_FAULT_OTHER ulong 4 196 197 const KSC_RFLAG_READ_OK ulong 1 198 const KSC_RFLAG_WRITE_DENIED ulong 2 199 const KSC_RFLAG_FORGED_DENIED ulong 4 200 const KSC_RFLAG_CANARY_PASSED ulong 8 201 const KSC_RFLAG_MAPPED_MATCH ulong 16 202 const KSC_RFLAG_EXECUTED_MATCH ulong 32 203 const KSC_RFLAG_WX_SEALED ulong 64 204 const KSC_RFLAG_RECLAIMED ulong 128 205 206 text KSC_TXT_ENV_MAGIC "AIENENV1" 207 text KSC_TXT_CHUNK_AAD "AIENOS-M5-CHUNK-V1" 208 text KSC_TXT_KSL_MAGIC "AIENKSL1" 209 text KSC_TXT_SEC_MAGIC "AIENSEC1" 210 text KSC_TXT_MIG_MAGIC "AIENMIG1" 211 text KSC_TXT_ROOT_AUTH_DOM "AIENOS-M5-ROOT-AUTH-V1" 212 text KSC_TXT_SUBKEY_CORTEX "AIENOS/M5/CORTEX-V1" 213 text KSC_TXT_SUBKEY_AGENT "AIENOS/M5/AGENT-STATE-V1" 214 text KSC_TXT_SUBKEY_ARTIFACT "AIENOS/M5/ARTIFACT-V1" 215 text KSC_TXT_SUBKEY_ROOTAUTH "AIENOS/M5/ROOT-AUTH-V1" 216 text KSC_TXT_OPAUTH_DOM "AIENOS-RECOVERY-OPERATOR-AUTH-v1" 217 text KSC_TXT_CHALLENGE_DOM "AIENOS-RECOVERY-CHALLENGE-v1" 218 text KSC_TXT_ROOT_BRANCH "AIENOS_ROOT_BRANCH_v1:" 219 text KSC_TXT_CHILD_BRANCH "AIENOS_CHILD_BRANCH_v1:" 220 text KSC_TXT_MAGIC_ROOT "AIENROOT" 221 text KSC_TXT_MAGIC_MANI "AIENMANI" 222 text KSC_TXT_MAGIC_BRAN "AIENBRAN" 223 text KSC_TXT_MAGIC_CWAL "AIENCWAL" 224 text KSC_TXT_MAGIC_POL "AIENPOL" 225 text KSC_TXT_POL_DOM "AIENOS-ADMISSION-POLICY-V1" 226 text KSC_TXT_GRANTS_DOM "AIENOS-ADMISSION-CAP-GRANTS-V1" 227 228 note ========================================================================= 229 note 0. Common byte, endian, comparison, and SHA-256 helpers 230 note ========================================================================= 231 232 fn ksc_get_u16le b view uchar * off ulong to ulong 233 x = ulong get b off 234 y = ulong get b add off 1 235 ret or x shl y 8 236 end 237 238 fn ksc_get_u32le b view uchar * off ulong to ulong 239 x = ulong 0 240 for i ulong 4 unroll 241 x = or x shl ulong get b add off i mul i 8 242 end 243 ret x 244 end 245 246 fn ksc_get_u64le b view uchar * off ulong to ulong 247 x = ulong 0 248 for i ulong 8 unroll 249 x = or x shl ulong get b add off i mul i 8 250 end 251 ret x 252 end 253 254 fn ksc_get_u32be b view uchar * off ulong to ulong 255 x = ulong 0 256 for i ulong 4 unroll 257 x = or shl x 8 ulong get b add off i 258 end 259 ret x 260 end 261 262 fn ksc_put_u16le b edit uchar * off ulong v ulong to void 263 put b off uchar and v 0xff 264 put b add off 1 uchar and shr v 8 0xff 265 end 266 267 fn ksc_put_u32le b edit uchar * off ulong v ulong to void 268 for i ulong 4 unroll 269 put b add off i uchar and shr v mul i 8 0xff 270 end 271 end 272 273 fn ksc_put_u64le b edit uchar * off ulong v ulong to void 274 for i ulong 8 unroll 275 put b add off i uchar and shr v mul i 8 0xff 276 end 277 end 278 279 fn ksc_put_u32be b edit uchar * off ulong v ulong to void 280 for i ulong 4 unroll 281 put b add off i uchar and shr v sub 24 mul i 8 0xff 282 end 283 end 284 285 fn ksc_put_u64be b edit uchar * off ulong v ulong to void 286 for i ulong 8 unroll 287 put b add off i uchar and shr v sub 56 mul i 8 0xff 288 end 289 end 290 291 fn ksc_zeroize b edit uchar * to void 292 n = len b 293 for i ulong n bound `1048576` 294 put b i uchar 0 295 end 296 end 297 298 fn ksc_copy dst edit uchar * src view uchar * to void 299 n = len src 300 for i ulong n bound `1048576` 301 put dst i get src i 302 end 303 end 304 305 fn ksc_ct_eq a view uchar * b view uchar * to bool 306 la = len a 307 lb = len b 308 diff = xor la lb 309 m = la 310 if gt lb m 311 m = lb 312 end 313 for i ulong m bound `1048576` 314 xa = ulong 0 315 xb = ulong 0 316 if lt i la 317 xa = ulong get a i 318 end 319 if lt i lb 320 xb = ulong get b i 321 end 322 diff = or diff xor xa xb 323 end 324 ret eq diff 0 325 end 326 327 fn ksc_is_zero b view uchar * to bool 328 n = len b 329 acc = ulong 0 330 for i ulong n bound `1048576` 331 acc = or acc ulong get b i 332 end 333 ret eq acc 0 334 end 335 336 fn ksc_cmp_lex a view uchar * b view uchar * to int 337 n = len a 338 for i ulong n bound `1048576` 339 va = ulong get a i 340 vb = ulong get b i 341 if lt va vb 342 ret int -1 343 end 344 if gt va vb 345 ret int 1 346 end 347 end 348 ret int 0 349 end 350 351 fn ksc_sha256 msg view uchar * digest edit uchar 32 to void 352 r = sha2_digest ulong 256 msg digest 353 if eq r 0 354 trap 8 355 end 356 end 357 358 note ========================================================================= 359 note 1. Kernel Crypto: HMAC-SHA256, HKDF-SHA256, and M5 Streaming Envelope 360 note ========================================================================= 361 362 fn ksc_hmac_pack_opad hctx edit ulong 48 opad view uchar 64 to void 363 for w ulong 8 unroll 364 v = ulong 0 365 for j ulong 8 unroll 366 v = or v shl ulong get opad add mul w 8 j mul j 8 367 end 368 put hctx add 40 w v 369 end 370 end 371 372 fn ksc_hmac_unpack_opad hctx view ulong 48 opad edit uchar 64 to void 373 for w ulong 8 unroll 374 v = get hctx add 40 w 375 for j ulong 8 unroll 376 put opad add mul w 8 j uchar and shr v mul j 8 0xff 377 end 378 end 379 end 380 381 fn ksc_hmac_init hctx edit ulong 48 key view uchar * to void 382 kb = new uchar 64 0 383 ipad = new uchar 64 0x36 384 opad = new uchar 64 0x5c 385 kd = new uchar 32 0 386 kl = len key 387 if gt kl 64 388 ksc_sha256 key kd 389 ksc_copy part kb 0 32 kd 390 ksc_zeroize kd 391 else 392 if gt kl 0 393 ksc_copy part kb 0 kl key 394 end 395 end 396 for i ulong 64 unroll 397 kbi = ulong get kb i 398 put ipad i uchar xor ulong 0x36 kbi 399 put opad i uchar xor ulong 0x5c kbi 400 end 401 ksc_zeroize kb 402 sha256_init part hctx 0 40 403 sha256_update part hctx 0 40 ipad 404 ksc_zeroize ipad 405 ksc_hmac_pack_opad hctx opad 406 ksc_zeroize opad 407 end 408 409 fn ksc_hmac_update hctx edit ulong 48 msg view uchar * to void 410 if gt len msg 0 411 sha256_update part hctx 0 40 msg 412 end 413 end 414 415 fn ksc_hmac_final hctx edit ulong 48 tag edit uchar * to void 416 idig = new uchar 32 0 417 opad = new uchar 64 0 418 octx = new ulong 40 0 419 sha256_final part hctx 0 40 idig 420 ksc_hmac_unpack_opad hctx opad 421 sha256_init octx 422 sha256_update octx opad 423 sha256_update octx idig 424 sha256_final octx tag 425 ksc_zeroize idig 426 ksc_zeroize opad 427 for i ulong 48 unroll 428 put hctx i ulong 0 429 end 430 end 431 432 fn ksc_hmac_sha256 key view uchar * msg view uchar * tag edit uchar 32 to void 433 hctx = new ulong 48 0 434 ksc_hmac_init hctx key 435 ksc_hmac_update hctx msg 436 ksc_hmac_final hctx tag 437 end 438 439 fn ksc_hkdf_extract salt view uchar * ikm view uchar * prk edit uchar 32 to void 440 ksc_hmac_sha256 salt ikm prk 441 end 442 443 fn ksc_hkdf_expand prk view uchar * info view uchar * okm edit uchar * to ulong 444 prev = new uchar 33 0 445 hctx = new ulong 48 0 446 if gt len okm 8160 447 ret KSC_HKDF_ERR_TOO_LONG 448 end 449 written = ulong 0 450 c = ulong 0 451 while lt written len okm bound 256 452 c = add c 1 453 put prev 32 uchar and c 0xff 454 ksc_hmac_init hctx prk 455 if gt c 1 456 ksc_hmac_update hctx part prev 0 32 457 end 458 ksc_hmac_update hctx info 459 ksc_hmac_update hctx part prev 32 1 460 ksc_hmac_final hctx part prev 0 32 461 take = sub len okm written 462 if gt take 32 463 take = 32 464 end 465 for j ulong take bound 32 466 put okm add written j get prev j 467 end 468 written = add written take 469 end 470 ksc_zeroize prev 471 ret KSC_HKDF_OK 472 end 473 474 note M5 Authenticated Streaming Envelope (crypto/envelope.rs) 475 476 fn ksc_env_expected_len chunk_size ulong total_pt ulong res edit ulong * to ulong 477 if eq chunk_size 0 478 ret KSC_ENV_ERR_CHUNK 479 end 480 chunks = ulong 1 481 if gt total_pt 0 482 q = div total_pt chunk_size 483 r = rem total_pt chunk_size 484 chunks = q 485 if gt r 0 486 if eq chunks 0xffffffffffffffff 487 ret KSC_ENV_ERR_OVERFLOW 488 end 489 chunks = add chunks 1 490 end 491 end 492 if gt chunks div 0xffffffffffffffff KSC_ENV_TAG_LEN 493 ret KSC_ENV_ERR_OVERFLOW 494 end 495 tags = mul chunks KSC_ENV_TAG_LEN 496 if gt tags sub 0xffffffffffffffff total_pt 497 ret KSC_ENV_ERR_OVERFLOW 498 end 499 body = add tags total_pt 500 if gt body sub 0xffffffffffffffff KSC_ENV_HDR_LEN 501 ret KSC_ENV_ERR_OVERFLOW 502 end 503 put res 0 add body KSC_ENV_HDR_LEN 504 ret KSC_ENV_OK 505 end 506 507 note Decode and validate 64-byte envelope header. 508 note meta[0]=chunk_size, [1]=total_pt, [2]=key_epoch, [3]=chunk_count, [4]=expected_len 509 fn ksc_env_hdr_decode buf view uchar * meta edit ulong 8 to ulong 510 exp = new ulong 1 0 511 if lt len buf KSC_ENV_HDR_LEN 512 ret KSC_ENV_ERR_SHORT 513 end 514 if not ksc_ct_eq part buf 0 8 KSC_TXT_ENV_MAGIC 515 ret KSC_ENV_ERR_MAGIC 516 end 517 ver = ksc_get_u16le buf 8 518 if ne ver KSC_ENV_VERSION 519 ret KSC_ENV_ERR_VER 520 end 521 suite = ksc_get_u16le buf 10 522 if ne suite KSC_ENV_SUITE_GCMSIV 523 ret KSC_ENV_ERR_SUITE 524 end 525 flags = ksc_get_u32le buf 12 526 if ne flags 0 527 ret KSC_ENV_ERR_FLAGS 528 end 529 cs = ksc_get_u32le buf 16 530 if eq cs 0 531 ret KSC_ENV_ERR_CHUNK 532 end 533 if gt cs KSC_ENV_DEFAULT_CHUNK 534 ret KSC_ENV_ERR_CHUNK 535 end 536 rsvd = ksc_get_u32le buf 60 537 if ne rsvd 0 538 ret KSC_ENV_ERR_RSVD 539 end 540 tpt = ksc_get_u64le buf 20 541 ep = ksc_get_u64le buf 52 542 chunks = ulong 1 543 if gt tpt 0 544 chunks = div add tpt sub cs 1 cs 545 end 546 put meta 0 cs 547 put meta 1 tpt 548 put meta 2 ep 549 put meta 3 chunks 550 rc = ksc_env_expected_len cs tpt exp 551 if ne rc KSC_ENV_OK 552 put meta 4 ulong 0 553 else 554 put meta 4 get exp 0 555 end 556 ret KSC_ENV_OK 557 end 558 559 fn ksc_env_derive_nonce prefix view uchar * chunk_idx ulong nonce edit uchar * to void 560 ksc_copy part nonce 0 8 prefix 561 ksc_put_u32le nonce 8 chunk_idx 562 end 563 564 note meta[0]=object_kind, [1]=object_version, [2]=chunk_index, [3]=chunk_pt_len 565 fn ksc_env_compute_aad suuid view uchar * hdr view uchar * meta view ulong 8 aad edit uchar 111 to void 566 ksc_copy part aad 0 18 KSC_TXT_CHUNK_AAD 567 put aad 18 uchar 0 568 ksc_copy part aad 19 16 suuid 569 ksc_put_u16le aad 35 get meta 0 570 ksc_put_u16le aad 37 get meta 1 571 ksc_copy part aad 39 64 hdr 572 ksc_put_u32le aad 103 get meta 2 573 ksc_put_u32le aad 107 get meta 3 574 end 575 576 note Seal one chunk in place: cbuf has clen plaintext bytes followed by 16 tag bytes. 577 note cmeta: [kind, version, cidx, clen, ...] 578 fn ksc_env_seal_chunk params view uchar 72 hdr view uchar 64 cmeta view ulong 8 cbuf edit uchar * to ulong 579 kn = new uchar 44 0 580 aad = new uchar 111 0 581 ksc_copy part kn 0 32 part params 0 32 582 ksc_env_derive_nonce part params 64 8 get cmeta 2 part kn 32 12 583 ksc_env_compute_aad part params 32 16 hdr cmeta aad 584 rc = aes_gcmsiv_seal kn aad cbuf 585 ksc_zeroize kn 586 ret rc 587 end 588 589 fn ksc_env_prep_open phdr view uchar 112 cmeta view ulong 8 kn edit uchar 44 aad edit uchar 111 to void 590 for i ulong 32 unroll 591 put kn i get phdr i 592 end 593 for i ulong 8 unroll 594 put kn add 32 i get phdr add 92 i 595 end 596 ksc_put_u32le kn 40 get cmeta 2 597 ksc_copy part aad 0 18 KSC_TXT_CHUNK_AAD 598 put aad 18 uchar 0 599 for i ulong 16 unroll 600 put aad add 19 i get phdr add 32 i 601 end 602 ksc_put_u16le aad 35 get cmeta 0 603 ksc_put_u16le aad 37 get cmeta 1 604 for i ulong 64 unroll 605 put aad add 39 i get phdr add 48 i 606 end 607 ksc_put_u32le aad 103 get cmeta 2 608 ksc_put_u32le aad 107 get cmeta 3 609 end 610 611 fn ksc_copy_from_off dst edit uchar * src view uchar * soff ulong n ulong to void 612 for i ulong n bound 65552 613 put dst i get src add soff i 614 end 615 end 616 617 fn ksc_copy_to_off dst edit uchar * doff ulong src view uchar * n ulong to void 618 for i ulong n bound 65552 619 put dst add doff i get src i 620 end 621 end 622 623 note Open one chunk from env[epos..epos+clen+16) into pt_dst[ppos..ppos+clen). 624 note st: [kind, version, cidx, clen, chunk_size, epos, ppos, tpt] 625 fn ksc_env_open_chunk phdr view uchar 112 st view ulong 8 env view uchar * pt_dst edit uchar * to ulong 626 kn = new uchar 44 0 627 aad = new uchar 111 0 628 ctmp = new uchar 65552 0 629 ksc_env_prep_open phdr st kn aad 630 ctlen = add get st 3 16 631 ksc_copy_from_off ctmp env get st 5 ctlen 632 rc = aes_gcmsiv_open kn aad part ctmp 0 ctlen 633 ksc_zeroize kn 634 if ne rc 0 635 ksc_zeroize part ctmp 0 ctlen 636 ret KSC_ENV_ERR_AUTH 637 end 638 if gt get st 3 0 639 ksc_copy_to_off pt_dst get st 6 ctmp get st 3 640 end 641 ksc_zeroize part ctmp 0 ctlen 642 ret KSC_ENV_OK 643 end 644 645 fn ksc_env_build_hdr params view uchar 72 meta view ulong 4 cs ulong ptlen ulong hdr edit uchar 64 to void 646 ksc_copy part hdr 0 8 KSC_TXT_ENV_MAGIC 647 ksc_put_u16le hdr 8 KSC_ENV_VERSION 648 ksc_put_u16le hdr 10 KSC_ENV_SUITE_GCMSIV 649 ksc_put_u32le hdr 12 0 650 ksc_put_u32le hdr 16 cs 651 ksc_put_u64le hdr 20 ptlen 652 for i ulong 16 unroll 653 put hdr add 28 i get params add 48 i 654 end 655 for i ulong 8 unroll 656 put hdr add 44 i get params add 64 i 657 end 658 ksc_put_u64le hdr 52 get meta 2 659 ksc_put_u32le hdr 60 0 660 end 661 662 note Encrypt plaintext into envelope buffer dst. 663 note params: key32[0..32) || suuid[32..48) || env_id[48..64) || nonce_prefix[64..72) = 72 bytes. 664 note meta[0]=kind, [1]=version, [2]=key_epoch, [3]=chunk_size (0 => 65536). 665 note Returns total envelope length in dst, or 0 on error. 666 fn ksc_env_copy_chunk dst edit uchar * pt view uchar * st view ulong 8 to void 667 doff = get st 6 668 soff = get st 5 669 n = get st 3 670 for j ulong n bound 65536 671 put dst add doff j get pt add soff j 672 end 673 end 674 675 fn ksc_env_encrypt params view uchar 72 meta view ulong 4 pt view uchar * dst edit uchar * to ulong 676 hdr = new uchar 64 0 677 st = new ulong 8 0 678 cs = get meta 3 679 if eq cs 0 680 cs = KSC_ENV_DEFAULT_CHUNK 681 end 682 if gt cs KSC_ENV_DEFAULT_CHUNK 683 ret 0 684 end 685 if gt len pt KSC_ENV_MAX_PT 686 ret 0 687 end 688 if ne ksc_env_expected_len cs len pt part st 7 1 KSC_ENV_OK 689 ret 0 690 end 691 if lt len dst get st 7 692 ret 0 693 end 694 ksc_env_build_hdr params meta cs len pt hdr 695 for i ulong 64 unroll 696 put dst i get hdr i 697 end 698 put st 0 get meta 0 699 put st 1 get meta 1 700 put st 4 cs 701 put st 5 0 702 put st 6 64 703 if eq len pt 0 704 put st 2 0 705 put st 3 0 706 if ne ksc_env_seal_chunk params hdr st part dst 64 16 0 707 ret 0 708 end 709 ret 80 710 end 711 while lt get st 5 len pt bound `1048576` 712 clen = sub len pt get st 5 713 if gt clen get st 4 714 clen = get st 4 715 end 716 put st 3 clen 717 ksc_env_copy_chunk dst pt st 718 if ne ksc_env_seal_chunk params hdr st part dst get st 6 add clen 16 0 719 ret 0 720 end 721 put st 5 add get st 5 clen 722 put st 6 add get st 6 add clen 16 723 put st 2 add get st 2 1 724 end 725 ret get st 6 726 end 727 728 note Decrypt envelope env into pt_dst. 729 note params: key32[0..32) || suuid[32..48) = 48 bytes. 730 note meta[0]=kind, [1]=version; writes meta[2]=pt_len on success. 731 fn ksc_env_decrypt params view uchar 48 meta edit ulong 4 env view uchar * pt_dst edit uchar * to ulong 732 st = new ulong 8 0 733 phdr = new uchar 112 0 734 rc = ksc_env_hdr_decode env st 735 if ne rc KSC_ENV_OK 736 ret rc 737 end 738 if gt get st 1 KSC_ENV_MAX_PT 739 ret KSC_ENV_ERR_TOO_LARGE 740 end 741 if eq get st 4 0 742 ret KSC_ENV_ERR_OVERFLOW 743 end 744 if ne len env get st 4 745 ret KSC_ENV_ERR_LEN 746 end 747 if lt len pt_dst get st 1 748 ret KSC_ENV_ERR_LEN 749 end 750 put st 4 get st 0 751 put st 7 get st 1 752 put st 0 get meta 0 753 put st 1 get meta 1 754 put st 2 0 755 put st 3 0 756 put st 5 64 757 put st 6 0 758 for i ulong 48 unroll 759 put phdr i get params i 760 end 761 for i ulong 64 unroll 762 put phdr add 48 i get env i 763 end 764 if eq get st 7 0 765 if ne ksc_env_open_chunk phdr st env pt_dst KSC_ENV_OK 766 ret KSC_ENV_ERR_AUTH 767 end 768 put meta 2 0 769 ret KSC_ENV_OK 770 end 771 while lt get st 6 get st 7 bound `1048576` 772 clen = sub get st 7 get st 6 773 if gt clen get st 4 774 clen = get st 4 775 end 776 put st 3 clen 777 if ne ksc_env_open_chunk phdr st env pt_dst KSC_ENV_OK 778 for z ulong get st 6 bound `1048576` 779 put pt_dst z uchar 0 780 end 781 ret KSC_ENV_ERR_AUTH 782 end 783 put st 6 add get st 6 clen 784 put st 5 add get st 5 add clen 16 785 put st 2 add get st 2 1 786 end 787 put meta 2 get st 7 788 ret KSC_ENV_OK 789 end 790 791 note ========================================================================= 792 note 2. Security Subkeys, Manifests, KeySlot Wrap/Unwrap, Anti-Rollback 793 note ========================================================================= 794 795 note Derives k_cortex[0..32), k_agent[32..64), k_artifact[64..96), k_root_auth[96..128) 796 fn ksc_derive_subkeys k_vol view uchar 32 subkeys edit uchar 128 to void 797 r0 = ksc_hkdf_expand k_vol KSC_TXT_SUBKEY_CORTEX part subkeys 0 32 798 r1 = ksc_hkdf_expand k_vol KSC_TXT_SUBKEY_AGENT part subkeys 32 32 799 r2 = ksc_hkdf_expand k_vol KSC_TXT_SUBKEY_ARTIFACT part subkeys 64 32 800 r3 = ksc_hkdf_expand k_vol KSC_TXT_SUBKEY_ROOTAUTH part subkeys 96 32 801 if ne or or r0 r1 or r2 r3 0 802 trap 8 803 end 804 end 805 806 fn ksc_keyslot_wrap slot edit uchar 128 kek view uchar 32 k_vol view uchar 32 nonce view uchar 12 to ulong 807 kn = new uchar 44 0 808 aad = new uchar 4 0 809 ct = new uchar 48 0 810 put slot 1 uchar 1 811 ksc_copy part slot 44 12 nonce 812 ksc_copy part kn 0 32 kek 813 ksc_copy part kn 32 12 nonce 814 ksc_copy aad part slot 4 4 815 ksc_copy part ct 0 32 k_vol 816 rc = aes_gcmsiv_seal kn aad ct 817 if ne rc 0 818 ksc_zeroize ct 819 ret KSC_SEC_ERR_FORMAT 820 end 821 ksc_copy part slot 56 32 part ct 0 32 822 ksc_copy part slot 88 16 part ct 32 16 823 ksc_zeroize ct 824 ksc_zeroize kn 825 ret KSC_SEC_OK 826 end 827 828 fn ksc_keyslot_unwrap slot view uchar 128 kek view uchar 32 k_vol edit uchar 32 to ulong 829 kn = new uchar 44 0 830 aad = new uchar 4 0 831 ct = new uchar 48 0 832 stype = ulong get slot 0 833 if eq stype 0 834 ret KSC_SEC_ERR_FORMAT 835 end 836 if ne ulong get slot 1 1 837 ret KSC_SEC_ERR_FORMAT 838 end 839 ksc_copy part kn 0 32 kek 840 ksc_copy part kn 32 12 part slot 44 12 841 ksc_copy aad part slot 4 4 842 ksc_copy part ct 0 32 part slot 56 32 843 ksc_copy part ct 32 16 part slot 88 16 844 rc = aes_gcmsiv_open kn aad ct 845 ksc_zeroize kn 846 if ne rc 0 847 ksc_zeroize ct 848 ret KSC_SEC_ERR_AUTH 849 end 850 ksc_copy k_vol part ct 0 32 851 ksc_zeroize ct 852 ret KSC_SEC_OK 853 end 854 855 fn ksc_keyslot_desc_validate buf view uchar * to ulong 856 stype = ulong get buf 0 857 if gt stype 3 858 ret KSC_SEC_ERR_FORMAT 859 end 860 mkib = ksc_get_u32le buf 32 861 tcost = ksc_get_u32le buf 36 862 pcost = ksc_get_u32le buf 40 863 if gt mkib 131072 864 ret KSC_SEC_ERR_FORMAT 865 end 866 if gt tcost 10 867 ret KSC_SEC_ERR_FORMAT 868 end 869 if gt pcost 16 870 ret KSC_SEC_ERR_FORMAT 871 end 872 if not ksc_is_zero part buf 112 16 873 ret KSC_SEC_ERR_RSVD 874 end 875 ret KSC_SEC_OK 876 end 877 878 fn ksc_keyslot_manifest_validate buf view uchar * to ulong 879 fixed = add KSC_KEYSLOT_HDR_SIZE mul KSC_KEYSLOT_MAX KSC_KEYSLOT_DESC_SIZE 880 if lt len buf fixed 881 ret KSC_SEC_ERR_SHORT 882 end 883 if not ksc_ct_eq part buf 0 8 KSC_TXT_KSL_MAGIC 884 ret KSC_SEC_ERR_MAGIC 885 end 886 if ne ksc_get_u16le buf 8 KSC_SEC_VER 887 ret KSC_SEC_ERR_VER 888 end 889 if ne ksc_get_u16le buf 10 0 890 ret KSC_SEC_ERR_FORMAT 891 end 892 scnt = ksc_get_u32le buf 76 893 if gt scnt KSC_KEYSLOT_MAX 894 ret KSC_SEC_ERR_SLOT_RANGE 895 end 896 if not ksc_is_zero part buf 80 48 897 ret KSC_SEC_ERR_RSVD 898 end 899 for i ulong 4 unroll 900 off = add KSC_KEYSLOT_HDR_SIZE mul i KSC_KEYSLOT_DESC_SIZE 901 rc = ksc_keyslot_desc_validate part buf off 128 902 if ne rc KSC_SEC_OK 903 ret rc 904 end 905 end 906 arena = sub len buf fixed 907 if gt arena KSC_KEYSLOT_MAX_ARENA 908 ret KSC_SEC_ERR_ARENA 909 end 910 ret KSC_SEC_OK 911 end 912 913 fn ksc_sec_manifest_mac buf view uchar 256 k_root_auth view uchar 32 mac edit uchar 32 to void 914 hctx = new ulong 48 0 915 nul = new uchar 1 0 916 ksc_hmac_init hctx k_root_auth 917 ksc_hmac_update hctx KSC_TXT_ROOT_AUTH_DOM 918 ksc_hmac_update hctx nul 919 ksc_hmac_update hctx part buf 0 224 920 ksc_hmac_final hctx mac 921 end 922 923 fn ksc_sec_manifest_sign buf edit uchar 256 k_root_auth view uchar 32 to void 924 mac = new uchar 32 0 925 ksc_sec_manifest_mac buf k_root_auth mac 926 ksc_copy part buf 224 32 mac 927 ksc_zeroize mac 928 end 929 930 fn ksc_sec_manifest_verify buf view uchar 256 k_root_auth view uchar 32 to bool 931 exp = new uchar 32 0 932 ksc_sec_manifest_mac buf k_root_auth exp 933 ok = ksc_ct_eq part buf 224 32 exp 934 ksc_zeroize exp 935 ret ok 936 end 937 938 fn ksc_sec_manifest_validate buf view uchar * to ulong 939 if ne len buf KSC_SEC_MANIFEST_SIZE 940 ret KSC_SEC_ERR_SHORT 941 end 942 if not ksc_ct_eq part buf 0 8 KSC_TXT_SEC_MAGIC 943 ret KSC_SEC_ERR_MAGIC 944 end 945 if ne ksc_get_u16le buf 8 KSC_SEC_VER 946 ret KSC_SEC_ERR_VER 947 end 948 if ne ksc_get_u16le buf 10 0 949 ret KSC_SEC_ERR_FORMAT 950 end 951 if ne ksc_get_u32le buf 12 0 952 ret KSC_SEC_ERR_RSVD 953 end 954 ret KSC_SEC_OK 955 end 956 957 fn ksc_mig_manifest_validate buf view uchar * to ulong 958 if ne len buf KSC_MIG_MANIFEST_SIZE 959 ret KSC_SEC_ERR_SHORT 960 end 961 if not ksc_ct_eq part buf 0 8 KSC_TXT_MIG_MAGIC 962 ret KSC_SEC_ERR_MAGIC 963 end 964 if ne ksc_get_u16le buf 8 KSC_SEC_VER 965 ret KSC_SEC_ERR_VER 966 end 967 if ne ksc_get_u16le buf 10 0 968 ret KSC_SEC_ERR_FORMAT 969 end 970 if ne ksc_get_u32le buf 12 0 971 ret KSC_SEC_ERR_RSVD 972 end 973 ret KSC_SEC_OK 974 end 975 976 fn ksc_eval_anti_rollback has_anchor ulong a_ep ulong d_ep ulong a_dig view uchar 32 d_dig view uchar 32 to ulong 977 if eq has_anchor 0 978 ret KSC_ARB_PREPARED_ADVANCE 979 end 980 same = ksc_ct_eq d_dig a_dig 981 if eq d_ep a_ep 982 if same 983 ret KSC_ARB_VALID_RESUME 984 end 985 ret KSC_ARB_ROLLBACK_DIGEST 986 end 987 if lt d_ep a_ep 988 ret KSC_ARB_ROLLBACK_STALE 989 end 990 if lt a_ep 0xffffffffffffffff 991 if eq d_ep add a_ep 1 992 ret KSC_ARB_PREPARED_ADVANCE 993 end 994 end 995 ret KSC_ARB_INCONSISTENT_EPOCH 996 end 997 998 note ========================================================================= 999 note 3. Budget Manager & Watchdog Policy (budget.rs) 1000 note ========================================================================= 1001 1002 fn ksc_sat_add a ulong b ulong to ulong 1003 if gt a sub 0xffffffffffffffff b 1004 ret 0xffffffffffffffff 1005 end 1006 ret add a b 1007 end 1008 1009 fn ksc_budget_policy_allowance mode ulong base ulong to ulong 1010 whole = ulong 0 1011 rem_num = ulong 100 1012 if eq mode KSC_BMODE_IDLE 1013 rem_num = 800 1014 else 1015 if eq mode KSC_BMODE_URGENT 1016 whole = 1 1017 rem_num = 500 1018 end 1019 end 1020 p0 = ulong 0 1021 if eq whole 1 1022 p0 = base 1023 end 1024 q = div base 1000 1025 r = rem base 1000 1026 p1 = mul q rem_num 1027 p2 = div mul r rem_num 1000 1028 ret ksc_sat_add ksc_sat_add p0 p1 p2 1029 end 1030 1031 note BudgetManager layout in mgr edit ulong 132: 1032 note mgr[0] = mode (0=Active, 1=Idle, 2=Urgent), mgr[1] = cap (1..8) 1033 note slot s (0..cap-1) at base = 4 + s * 16: 1034 note +0 state, +1 id, +2 cpu_lim, +3 frames_lim, +4 dma_lim, +5 max_rt, 1035 note +6 forge, +7 cpu_used, +8 frames_used, +9 dma_used, +10 armed_at 1036 1037 fn ksc_budget_init mgr edit ulong 132 cap ulong to void 1038 for i ulong 132 bound 132 1039 put mgr i ulong 0 1040 end 1041 c = cap 1042 if gt c 8 1043 c = 8 1044 end 1045 put mgr 0 KSC_BMODE_ACTIVE 1046 put mgr 1 c 1047 end 1048 1049 fn ksc_budget_set_mode mgr edit ulong 132 mode ulong to void 1050 put mgr 0 mode 1051 end 1052 1053 fn ksc_budget_find mgr view ulong 132 wid ulong to ulong 1054 cap = get mgr 1 1055 for s ulong cap bound 8 1056 base = add 4 mul s 16 1057 if ne get mgr base KSC_WSTATE_EMPTY 1058 if eq get mgr add base 1 wid 1059 ret s 1060 end 1061 end 1062 end 1063 ret 99 1064 end 1065 1066 note env4: [cpu_lim, frames_lim, dma_lim, max_rt] 1067 fn ksc_budget_admit mgr edit ulong 132 wid ulong env4 view ulong 4 is_forge ulong now ulong to ulong 1068 if lt ksc_budget_find mgr wid 8 1069 ret KSC_BERR_DUP 1070 end 1071 cap = get mgr 1 1072 slot = ulong 99 1073 for s ulong cap bound 8 1074 base = add 4 mul s 16 1075 if eq get mgr base KSC_WSTATE_EMPTY 1076 slot = s 1077 break 1078 end 1079 end 1080 if eq slot 99 1081 ret KSC_BERR_FULL 1082 end 1083 b = add 4 mul slot 16 1084 put mgr b KSC_WSTATE_ACTIVE 1085 put mgr add b 1 wid 1086 put mgr add b 2 get env4 0 1087 put mgr add b 3 get env4 1 1088 put mgr add b 4 get env4 2 1089 put mgr add b 5 get env4 3 1090 put mgr add b 6 is_forge 1091 put mgr add b 7 0 1092 put mgr add b 8 0 1093 put mgr add b 9 0 1094 put mgr add b 10 now 1095 ret KSC_BERR_OK 1096 end 1097 1098 fn ksc_budget_remove mgr edit ulong 132 wid ulong to ulong 1099 s = ksc_budget_find mgr wid 1100 if ge s 8 1101 ret KSC_BERR_UNKNOWN 1102 end 1103 put mgr add 4 mul s 16 KSC_WSTATE_EMPTY 1104 ret KSC_BERR_OK 1105 end 1106 1107 fn ksc_budget_begin_period mgr edit ulong 132 to void 1108 cap = get mgr 1 1109 for s ulong cap bound 8 1110 b = add 4 mul s 16 1111 if eq get mgr b KSC_WSTATE_ACTIVE 1112 put mgr add b 7 0 1113 end 1114 end 1115 end 1116 1117 fn ksc_budget_cpu_allowance mgr view ulong 132 wid ulong res edit ulong 1 to ulong 1118 s = ksc_budget_find mgr wid 1119 if ge s 8 1120 ret KSC_BERR_UNKNOWN 1121 end 1122 b = add 4 mul s 16 1123 lim = get mgr add b 2 1124 if ne get mgr add b 6 0 1125 put res 0 ksc_budget_policy_allowance get mgr 0 lim 1126 else 1127 put res 0 lim 1128 end 1129 ret KSC_BERR_OK 1130 end 1131 1132 fn ksc_budget_state mgr view ulong 132 wid ulong res edit ulong 1 to ulong 1133 s = ksc_budget_find mgr wid 1134 if ge s 8 1135 ret KSC_BERR_UNKNOWN 1136 end 1137 put res 0 get mgr add 4 mul s 16 1138 ret KSC_BERR_OK 1139 end 1140 1141 fn ksc_budget_charge_cpu mgr edit ulong 132 wid ulong ticks ulong to ulong 1142 al = new ulong 1 0 1143 rc = ksc_budget_cpu_allowance mgr wid al 1144 if ne rc KSC_BERR_OK 1145 ret rc 1146 end 1147 s = ksc_budget_find mgr wid 1148 b = add 4 mul s 16 1149 if eq get mgr b KSC_WSTATE_QUARANTINED 1150 ret KSC_BERR_QUARANTINED 1151 end 1152 used = get mgr add b 7 1153 if gt ticks sub 0xffffffffffffffff used 1154 ret KSC_BERR_CPU 1155 end 1156 nxt = add used ticks 1157 if gt nxt get al 0 1158 ret KSC_BERR_CPU 1159 end 1160 put mgr add b 7 nxt 1161 ret KSC_BERR_OK 1162 end 1163 1164 fn ksc_budget_charge_frames mgr edit ulong 132 wid ulong frames ulong to ulong 1165 s = ksc_budget_find mgr wid 1166 if ge s 8 1167 ret KSC_BERR_UNKNOWN 1168 end 1169 b = add 4 mul s 16 1170 if eq get mgr b KSC_WSTATE_QUARANTINED 1171 ret KSC_BERR_QUARANTINED 1172 end 1173 used = get mgr add b 8 1174 if gt frames sub 0xffffffffffffffff used 1175 ret KSC_BERR_FRAMES 1176 end 1177 nxt = add used frames 1178 if gt nxt get mgr add b 3 1179 ret KSC_BERR_FRAMES 1180 end 1181 put mgr add b 8 nxt 1182 ret KSC_BERR_OK 1183 end 1184 1185 fn ksc_budget_release_frames mgr edit ulong 132 wid ulong frames ulong to ulong 1186 s = ksc_budget_find mgr wid 1187 if ge s 8 1188 ret KSC_BERR_UNKNOWN 1189 end 1190 b = add 4 mul s 16 1191 if eq get mgr b KSC_WSTATE_QUARANTINED 1192 ret KSC_BERR_QUARANTINED 1193 end 1194 used = get mgr add b 8 1195 if gt frames used 1196 ret KSC_BERR_BAD_RELEASE 1197 end 1198 put mgr add b 8 sub used frames 1199 ret KSC_BERR_OK 1200 end 1201 1202 fn ksc_budget_charge_dma mgr edit ulong 132 wid ulong bytes ulong to ulong 1203 s = ksc_budget_find mgr wid 1204 if ge s 8 1205 ret KSC_BERR_UNKNOWN 1206 end 1207 b = add 4 mul s 16 1208 if eq get mgr b KSC_WSTATE_QUARANTINED 1209 ret KSC_BERR_QUARANTINED 1210 end 1211 used = get mgr add b 9 1212 if gt bytes sub 0xffffffffffffffff used 1213 ret KSC_BERR_DMA 1214 end 1215 nxt = add used bytes 1216 if gt nxt get mgr add b 4 1217 ret KSC_BERR_DMA 1218 end 1219 put mgr add b 9 nxt 1220 ret KSC_BERR_OK 1221 end 1222 1223 fn ksc_budget_pet mgr edit ulong 132 wid ulong now ulong to ulong 1224 s = ksc_budget_find mgr wid 1225 if ge s 8 1226 ret KSC_BERR_UNKNOWN 1227 end 1228 b = add 4 mul s 16 1229 if eq get mgr b KSC_WSTATE_QUARANTINED 1230 ret KSC_BERR_QUARANTINED 1231 end 1232 put mgr add b 10 now 1233 ret KSC_BERR_OK 1234 end 1235 1236 note Quarantine overdue worlds using wrapping elapsed time; returns count expired. 1237 fn ksc_budget_check mgr edit ulong 132 now ulong expired edit ulong 8 to ulong 1238 cap = get mgr 1 1239 cnt = ulong 0 1240 for s ulong cap bound 8 1241 put expired s ulong 0 1242 b = add 4 mul s 16 1243 if eq get mgr b KSC_WSTATE_ACTIVE 1244 elapsed = sub now get mgr add b 10 1245 if ge elapsed get mgr add b 5 1246 put mgr b KSC_WSTATE_QUARANTINED 1247 put expired s get mgr add b 1 1248 cnt = add cnt 1 1249 end 1250 end 1251 end 1252 ret cnt 1253 end 1254 1255 fn ksc_budget_restore mgr edit ulong 132 wid ulong now ulong to ulong 1256 s = ksc_budget_find mgr wid 1257 if ge s 8 1258 ret KSC_BERR_UNKNOWN 1259 end 1260 b = add 4 mul s 16 1261 put mgr add b 7 0 1262 put mgr add b 8 0 1263 put mgr add b 9 0 1264 put mgr add b 10 now 1265 put mgr b KSC_WSTATE_ACTIVE 1266 ret KSC_BERR_OK 1267 end 1268 1269 note ========================================================================= 1270 note 4. Recovery Core, SlotManager, OperatorAuth, WalRecovery 1271 note ========================================================================= 1272 1273 note sm[0]=active_slot, [1]=slot_a_health, [2]=slot_b_health, [3]=attempts, [4]=max_attempts 1274 fn ksc_slotmgr_init sm edit ulong 5 to void 1275 put sm 0 KSC_SLOT_A 1276 put sm 1 KSC_HEALTH_CONFIRMED 1277 put sm 2 KSC_HEALTH_UNVERIFIED 1278 put sm 3 0 1279 put sm 4 3 1280 end 1281 1282 fn ksc_slotmgr_mark_healthy sm edit ulong 5 to void 1283 put sm 3 0 1284 if eq get sm 0 KSC_SLOT_A 1285 put sm 1 KSC_HEALTH_CONFIRMED 1286 else 1287 put sm 2 KSC_HEALTH_CONFIRMED 1288 end 1289 end 1290 1291 fn ksc_slotmgr_record_attempt sm edit ulong 5 to bool 1292 att = add get sm 3 1 1293 put sm 3 att 1294 if ge att get sm 4 1295 if eq get sm 0 KSC_SLOT_A 1296 put sm 1 KSC_HEALTH_FAILED 1297 put sm 0 KSC_SLOT_B 1298 else 1299 put sm 2 KSC_HEALTH_FAILED 1300 put sm 0 KSC_SLOT_A 1301 end 1302 put sm 3 0 1303 ret true 1304 end 1305 ret false 1306 end 1307 1308 fn ksc_opauth_expected_response key view uchar 32 ch view uchar 32 resp edit uchar 32 to void 1309 hctx = new ulong 48 0 1310 nul = new uchar 1 0 1311 ksc_hmac_init hctx key 1312 ksc_hmac_update hctx KSC_TXT_OPAUTH_DOM 1313 ksc_hmac_update hctx nul 1314 ksc_hmac_update hctx ch 1315 ksc_hmac_final hctx resp 1316 end 1317 1318 fn ksc_opauth_verify key view uchar 32 ch view uchar 32 resp view uchar 32 to bool 1319 exp = new uchar 32 0 1320 ksc_opauth_expected_response key ch exp 1321 ok = ksc_ct_eq exp resp 1322 ksc_zeroize exp 1323 ret ok 1324 end 1325 1326 fn ksc_wal_encode_entry payload view uchar * buf edit uchar * to ulong 1327 dig = new uchar 32 0 1328 plen = len payload 1329 tot = add 36 plen 1330 if lt len buf tot 1331 ret 0 1332 end 1333 ksc_sha256 payload dig 1334 ksc_put_u32be buf 0 plen 1335 ksc_copy part buf 4 32 dig 1336 if gt plen 0 1337 ksc_copy part buf 36 plen payload 1338 end 1339 ret tot 1340 end 1341 1342 note res[0]=valid_records, res[1]=truncated_valid_bytes 1343 fn ksc_wal_recover raw view uchar * res edit ulong 2 to void 1344 dig = new uchar 32 0 1345 off = ulong 0 1346 valid = ulong 0 1347 rlen = len raw 1348 while le add off 36 rlen bound `1048576` 1349 reclen = ksc_get_u32be raw off 1350 pstart = add off 36 1351 if gt reclen sub 0xffffffffffffffff pstart 1352 break 1353 end 1354 pend = add pstart reclen 1355 if gt pend rlen 1356 break 1357 end 1358 ksc_sha256 part raw pstart reclen dig 1359 if not ksc_ct_eq dig part raw add off 4 32 1360 break 1361 end 1362 valid = add valid 1 1363 off = pend 1364 end 1365 put res 0 valid 1366 put res 1 off 1367 end 1368 1369 fn ksc_recovery_applicable mstate ulong reason ulong pcond ulong has_cont ulong to ulong 1370 if eq mstate KSC_MOUNT_NONE 1371 ret KSC_ACT_NONE 1372 end 1373 if eq reason KSC_REASON_DEGRADED 1374 if eq pcond KSC_PEER_MALFORMED 1375 if ne has_cont 0 1376 ret KSC_ACT_REPAIR_DEGRADED_PEER 1377 end 1378 end 1379 ret KSC_ACT_NONE 1380 end 1381 if eq reason KSC_REASON_UNPROVISIONED 1382 if eq mstate KSC_MOUNT_VALID 1383 ret KSC_ACT_PROVISION_IDENTITY 1384 end 1385 end 1386 ret KSC_ACT_NONE 1387 end 1388 1389 fn ksc_recovery_challenge suuid view uchar 16 gen ulong action ulong sdig view uchar 32 ch edit uchar 32 to void 1390 sctx = new ulong 40 0 1391 nul = new uchar 1 0 1392 gb = new uchar 8 0 1393 ab = new uchar 1 0 1394 ksc_put_u64le gb 0 gen 1395 put ab 0 uchar and action 0xff 1396 sha256_init sctx 1397 sha256_update sctx KSC_TXT_CHALLENGE_DOM 1398 sha256_update sctx nul 1399 sha256_update sctx suuid 1400 sha256_update sctx gb 1401 sha256_update sctx ab 1402 sha256_update sctx sdig 1403 sha256_final sctx ch 1404 end 1405 1406 fn ksc_recovery_authorise applicable ulong req_act ulong key view uchar 32 ch view uchar 32 resp view uchar 32 to ulong 1407 if eq applicable KSC_ACT_NONE 1408 ret KSC_REC_ERR_NOT_APPLICABLE 1409 end 1410 if ne applicable req_act 1411 ret KSC_REC_ERR_NOT_APPLICABLE 1412 end 1413 if not ksc_opauth_verify key ch resp 1414 ret KSC_REC_ERR_UNAUTHORISED 1415 end 1416 ret KSC_REC_OK 1417 end 1418 1419 note ========================================================================= 1420 note 5. Continuity Objects, Branch Lineage, and State Transitions 1421 note ========================================================================= 1422 1423 fn ksc_root_branch_id agent view uchar * bid edit uchar 32 to void 1424 sctx = new ulong 40 0 1425 sha256_init sctx 1426 sha256_update sctx KSC_TXT_ROOT_BRANCH 1427 sha256_update sctx agent 1428 sha256_final sctx bid 1429 end 1430 1431 fn ksc_child_branch_id parent view uchar * idx ulong bid edit uchar 32 to void 1432 sctx = new ulong 40 0 1433 ib = new uchar 8 0 1434 ksc_put_u64be ib 0 idx 1435 sha256_init sctx 1436 sha256_update sctx KSC_TXT_CHILD_BRANCH 1437 sha256_update sctx parent 1438 sha256_update sctx ib 1439 sha256_final sctx bid 1440 end 1441 1442 fn ksc_cont_check_hdr buf view uchar * magic view uchar 8 to ulong 1443 if lt len buf KSC_CONT_HDR_BYTES 1444 ret KSC_CERR_TRUNCATED 1445 end 1446 if not ksc_ct_eq part buf 0 8 magic 1447 ret KSC_CERR_BAD_MAGIC 1448 end 1449 if ne ksc_get_u16le buf 8 0 1450 ret KSC_CERR_BAD_VER 1451 end 1452 if ne ksc_get_u16le buf 10 0 1453 ret KSC_CERR_HDR_RSVD 1454 end 1455 blen = ksc_get_u32le buf 12 1456 if ne blen sub len buf KSC_CONT_HDR_BYTES 1457 ret KSC_CERR_BODY_LEN 1458 end 1459 ret KSC_CERR_OK 1460 end 1461 1462 fn ksc_cont_write_hdr buf edit uchar * magic view uchar 8 body_len ulong to void 1463 ksc_copy part buf 0 8 magic 1464 ksc_put_u16le buf 8 0 1465 ksc_put_u16le buf 10 0 1466 ksc_put_u32le buf 12 body_len 1467 end 1468 1469 fn ksc_agent_root_encode agent view uchar 32 uuid view uchar 16 gen ulong src ulong buf edit uchar 112 to ulong 1470 rb = new uchar 32 0 1471 ksc_cont_write_hdr buf KSC_TXT_MAGIC_ROOT 96 1472 ksc_copy part buf 16 32 agent 1473 ksc_copy part buf 48 16 uuid 1474 ksc_root_branch_id agent rb 1475 ksc_copy part buf 64 32 rb 1476 ksc_put_u64le buf 96 gen 1477 put buf 104 uchar and src 0xff 1478 for i ulong 7 unroll 1479 put buf add 105 i uchar 0 1480 end 1481 ret 112 1482 end 1483 1484 note meta[0]=provisioned_generation, meta[1]=source 1485 fn ksc_agent_root_decode buf view uchar * meta edit ulong 2 to ulong 1486 rb = new uchar 32 0 1487 rc = ksc_cont_check_hdr buf KSC_TXT_MAGIC_ROOT 1488 if ne rc KSC_CERR_OK 1489 ret rc 1490 end 1491 if lt len buf 112 1492 ret KSC_CERR_TRUNCATED 1493 end 1494 if gt len buf 112 1495 ret KSC_CERR_TRAILING 1496 end 1497 src = ulong get buf 104 1498 if lt src 1 1499 ret KSC_CERR_BAD_SRC 1500 end 1501 if gt src 2 1502 ret KSC_CERR_BAD_SRC 1503 end 1504 if not ksc_is_zero part buf 105 7 1505 ret KSC_CERR_RSVD 1506 end 1507 if ksc_is_zero part buf 16 32 1508 ret KSC_CERR_ZERO_AGENT 1509 end 1510 ksc_root_branch_id part buf 16 32 rb 1511 if not ksc_ct_eq rb part buf 64 32 1512 ret KSC_CERR_ROOT_BRANCH 1513 end 1514 put meta 0 ksc_get_u64le buf 96 1515 put meta 1 src 1516 ret KSC_CERR_OK 1517 end 1518 1519 note meta[0]=sequence, [1]=incarnation, [2]=wal_count 1520 fn ksc_manifest_decode buf view uchar * meta edit ulong 3 to ulong 1521 rc = ksc_cont_check_hdr buf KSC_TXT_MAGIC_MANI 1522 if ne rc KSC_CERR_OK 1523 ret rc 1524 end 1525 if lt len buf 136 1526 ret KSC_CERR_TRUNCATED 1527 end 1528 seq = ksc_get_u64le buf 80 1529 inc = ksc_get_u64le buf 88 1530 wcnt = ksc_get_u16le buf 128 1531 if not ksc_is_zero part buf 130 6 1532 ret KSC_CERR_RSVD 1533 end 1534 if gt wcnt KSC_CONT_MAX_WAL_SEGS 1535 ret KSC_CERR_TOO_MANY_WAL 1536 end 1537 need = add 136 mul wcnt 32 1538 if lt len buf need 1539 ret KSC_CERR_TRUNCATED 1540 end 1541 if gt len buf need 1542 ret KSC_CERR_TRAILING 1543 end 1544 if eq seq 0 1545 ret KSC_CERR_SEQ_PREV 1546 end 1547 prev_zero = ksc_is_zero part buf 48 32 1548 if eq seq 1 1549 if not prev_zero 1550 ret KSC_CERR_SEQ_PREV 1551 end 1552 else 1553 if prev_zero 1554 ret KSC_CERR_SEQ_PREV 1555 end 1556 end 1557 put meta 0 seq 1558 put meta 1 inc 1559 put meta 2 wcnt 1560 ret KSC_CERR_OK 1561 end 1562 1563 fn ksc_state_find_branch buf view uchar * cnt ulong bid view uchar * to ulong 1564 lo = ulong 0 1565 hi = cnt 1566 while lt lo hi bound 16 1567 mid = add lo shr sub hi lo 1 1568 off = add 64 mul mid KSC_CONT_BRANCH_BYTES 1569 c = ksc_cmp_lex part buf off 32 bid 1570 if eq c int 0 1571 ret mid 1572 end 1573 if lt c int 0 1574 lo = add mid 1 1575 else 1576 hi = mid 1577 end 1578 end 1579 ret 9999 1580 end 1581 1582 fn ksc_state_check_lineage buf view uchar * cnt ulong idx ulong to ulong 1583 cbid = new uchar 32 0 1584 pid = new uchar 32 0 1585 off = add 64 mul idx KSC_CONT_BRANCH_BYTES 1586 ksc_copy pid part buf add off 32 32 1587 pidx = ksc_state_find_branch buf cnt pid 1588 if ge pidx cnt 1589 ret KSC_CERR_NO_PARENT 1590 end 1591 poff = add 64 mul pidx KSC_CONT_BRANCH_BYTES 1592 pdepth = ksc_get_u32le buf add poff 64 1593 if eq pdepth 0xffffffff 1594 ret KSC_CERR_LINEAGE 1595 end 1596 if ne ksc_get_u32le buf add off 64 add pdepth 1 1597 ret KSC_CERR_LINEAGE 1598 end 1599 lim = ksc_get_u64le buf add poff 72 1600 if gt lim KSC_CONT_MAX_BRANCHES 1601 lim = KSC_CONT_MAX_BRANCHES 1602 end 1603 for k ulong lim bound 256 1604 ksc_child_branch_id pid k cbid 1605 if ksc_ct_eq cbid part buf off 32 1606 ret KSC_CERR_OK 1607 end 1608 end 1609 ret KSC_CERR_LINEAGE 1610 end 1611 1612 fn ksc_state_check_sorted buf view uchar * cnt ulong to ulong 1613 prev_id = new uchar 32 0 1614 for i ulong cnt bound 256 1615 off = add 64 mul i KSC_CONT_BRANCH_BYTES 1616 if not ksc_is_zero part buf add off 68 4 1617 ret KSC_CERR_RSVD 1618 end 1619 if gt i 0 1620 if ge ksc_cmp_lex prev_id part buf off 32 int 0 1621 ret KSC_CERR_NOT_SORTED 1622 end 1623 end 1624 ksc_copy prev_id part buf off 32 1625 end 1626 ret KSC_CERR_OK 1627 end 1628 1629 fn ksc_agent_state_validate buf view uchar * to ulong 1630 rb = new uchar 32 0 1631 rc = ksc_cont_check_hdr buf KSC_TXT_MAGIC_BRAN 1632 if ne rc KSC_CERR_OK 1633 ret rc 1634 end 1635 if lt len buf 64 1636 ret KSC_CERR_TRUNCATED 1637 end 1638 cnt = ksc_get_u32le buf 56 1639 if not ksc_is_zero part buf 60 4 1640 ret KSC_CERR_RSVD 1641 end 1642 if eq cnt 0 1643 ret KSC_CERR_BRANCH_COUNT 1644 end 1645 if gt cnt KSC_CONT_MAX_BRANCHES 1646 ret KSC_CERR_BRANCH_COUNT 1647 end 1648 if lt len buf add 64 mul cnt KSC_CONT_BRANCH_BYTES 1649 ret KSC_CERR_TRUNCATED 1650 end 1651 if gt len buf add 64 mul cnt KSC_CONT_BRANCH_BYTES 1652 ret KSC_CERR_TRAILING 1653 end 1654 rc = ksc_state_check_sorted buf cnt 1655 if ne rc KSC_CERR_OK 1656 ret rc 1657 end 1658 ksc_root_branch_id part buf 16 32 rb 1659 roots = ulong 0 1660 children = ulong 0 1661 fsum = ulong 0 1662 fover = ulong 0 1663 for i ulong cnt bound 256 1664 off = add 64 mul i KSC_CONT_BRANCH_BYTES 1665 bf = ksc_get_u64le buf add off 72 1666 if gt bf sub 0xffffffffffffffff fsum 1667 fover = 1 1668 else 1669 fsum = add fsum bf 1670 end 1671 if ksc_is_zero part buf add off 32 32 1672 if not ksc_ct_eq part buf off 32 rb 1673 ret KSC_CERR_BAD_ROOT 1674 end 1675 if ne ksc_get_u32le buf add off 64 0 1676 ret KSC_CERR_BAD_ROOT 1677 end 1678 roots = add roots 1 1679 else 1680 rc = ksc_state_check_lineage buf cnt i 1681 if ne rc KSC_CERR_OK 1682 ret rc 1683 end 1684 children = add children 1 1685 end 1686 end 1687 if ne roots 1 1688 ret KSC_CERR_FORK_SUM 1689 end 1690 if ne fover 0 1691 ret KSC_CERR_FORK_SUM 1692 end 1693 if ne fsum children 1694 ret KSC_CERR_FORK_SUM 1695 end 1696 ret KSC_CERR_OK 1697 end 1698 1699 note Writes 144-byte genesis AgentState into buf; returns 144. 1700 fn ksc_agent_state_genesis agent view uchar 32 written_at ulong buf edit uchar * to ulong 1701 rb = new uchar 32 0 1702 if lt len buf 144 1703 ret 0 1704 end 1705 ksc_zeroize part buf 0 144 1706 ksc_cont_write_hdr buf KSC_TXT_MAGIC_BRAN 128 1707 ksc_copy part buf 16 32 agent 1708 ksc_put_u64le buf 48 written_at 1709 ksc_put_u32le buf 56 1 1710 ksc_root_branch_id agent rb 1711 ksc_copy part buf 64 32 rb 1712 ret 144 1713 end 1714 1715 fn ksc_state_insert_row buf edit uchar * cnt ulong ins ulong to void 1716 j = cnt 1717 while gt j ins bound 256 1718 soff = add 64 mul sub j 1 KSC_CONT_BRANCH_BYTES 1719 doff = add 64 mul j KSC_CONT_BRANCH_BYTES 1720 for b ulong 80 1721 put buf add doff b get buf add soff b 1722 end 1723 j = sub j 1 1724 end 1725 end 1726 1727 fn ksc_state_find_insert buf view uchar * cnt ulong cid view uchar 32 to ulong 1728 for i ulong cnt bound 256 1729 off = add 64 mul i KSC_CONT_BRANCH_BYTES 1730 if gt ksc_cmp_lex part buf off 32 cid int 0 1731 ret i 1732 end 1733 end 1734 ret cnt 1735 end 1736 1737 note Fork a child branch of parent in encoded AgentState buf (cur_len bytes). 1738 note Writes new child_id and returns new length (cur_len + 80), or 0 on error. 1739 fn ksc_agent_state_fork buf edit uchar * cur_len ulong parent view uchar 32 child_id edit uchar 32 to ulong 1740 cnt = ksc_get_u32le buf 56 1741 if ge cnt KSC_CONT_MAX_BRANCHES 1742 ret 0 1743 end 1744 if lt len buf add cur_len KSC_CONT_BRANCH_BYTES 1745 ret 0 1746 end 1747 pidx = ksc_state_find_branch part buf 0 cur_len cnt parent 1748 if ge pidx cnt 1749 ret 0 1750 end 1751 poff = add 64 mul pidx KSC_CONT_BRANCH_BYTES 1752 pdepth = ksc_get_u32le buf add poff 64 1753 if eq pdepth 0xffffffff 1754 ret 0 1755 end 1756 pforks = ksc_get_u64le buf add poff 72 1757 ksc_child_branch_id parent pforks child_id 1758 if lt ksc_state_find_branch part buf 0 cur_len cnt child_id cnt 1759 ret 0 1760 end 1761 ksc_put_u64le buf add poff 72 add pforks 1 1762 ins = ksc_state_find_insert buf cnt child_id 1763 ksc_state_insert_row buf cnt ins 1764 ioff = add 64 mul ins KSC_CONT_BRANCH_BYTES 1765 ksc_zeroize part buf ioff 80 1766 ksc_copy part buf ioff 32 child_id 1767 ksc_copy part buf add ioff 32 32 parent 1768 ksc_put_u32le buf add ioff 64 add pdepth 1 1769 ksc_put_u32le buf 56 add cnt 1 1770 ksc_put_u32le buf 12 sub add cur_len KSC_CONT_BRANCH_BYTES KSC_CONT_HDR_BYTES 1771 ret add cur_len KSC_CONT_BRANCH_BYTES 1772 end 1773 1774 note meta[0]=sequence, meta[1]=record_count 1775 fn ksc_wal_seg_decode buf view uchar * meta edit ulong 2 to ulong 1776 rc = ksc_cont_check_hdr buf KSC_TXT_MAGIC_CWAL 1777 if ne rc KSC_CERR_OK 1778 ret rc 1779 end 1780 if lt len buf 32 1781 ret KSC_CERR_TRUNCATED 1782 end 1783 seq = ksc_get_u64le buf 16 1784 rcnt = ksc_get_u32le buf 24 1785 if not ksc_is_zero part buf 28 4 1786 ret KSC_CERR_RSVD 1787 end 1788 if eq rcnt 0 1789 ret KSC_CERR_WAL_COUNT 1790 end 1791 if gt rcnt KSC_CONT_MAX_WAL_RECS 1792 ret KSC_CERR_WAL_COUNT 1793 end 1794 cur = ulong 32 1795 blen = len buf 1796 for i ulong rcnt bound 64 1797 if gt add cur 36 blen 1798 ret KSC_CERR_TRUNCATED 1799 end 1800 ep = ulong get buf cur 1801 if lt ep 1 1802 ret KSC_CERR_EPISTEMIC 1803 end 1804 if gt ep 6 1805 ret KSC_CERR_EPISTEMIC 1806 end 1807 if ne ulong get buf add cur 1 0 1808 ret KSC_CERR_RSVD 1809 end 1810 slen = ksc_get_u16le buf add cur 2 1811 if eq slen 0 1812 ret KSC_CERR_STMT_LEN 1813 end 1814 if gt slen KSC_CONT_MAX_STMT_BYTES 1815 ret KSC_CERR_STMT_LEN 1816 end 1817 cur = add cur add 36 slen 1818 if gt cur blen 1819 ret KSC_CERR_TRUNCATED 1820 end 1821 end 1822 if ne cur blen 1823 ret KSC_CERR_TRAILING 1824 end 1825 put meta 0 seq 1826 put meta 1 rcnt 1827 ret KSC_CERR_OK 1828 end 1829 1830 note ========================================================================= 1831 note 6. Admission Policy & Capability Attenuation (admission.rs) 1832 note ========================================================================= 1833 1834 fn ksc_cap_validate rec view uchar * to ulong 1835 if ne ksc_get_u16le rec 2 0 1836 ret KSC_AERR_MALFORMED_CAP 1837 end 1838 if ne ksc_get_u32le rec 20 0 1839 ret KSC_AERR_MALFORMED_CAP 1840 end 1841 rkind = ksc_get_u16le rec 0 1842 rid = ksc_get_u32le rec 4 1843 rights = ksc_get_u32le rec 8 1844 bkind = ksc_get_u32le rec 12 1845 mops = ksc_get_u32le rec 16 1846 mbytes = ksc_get_u64le rec 24 1847 boff = ksc_get_u64le rec 32 1848 blen = ksc_get_u64le rec 40 1849 if eq rid 0 1850 ret KSC_AERR_MALFORMED_CAP 1851 end 1852 if eq rights 0 1853 ret KSC_AERR_MALFORMED_CAP 1854 end 1855 if ne and rights xor KSC_VALID_RIGHTS 0xffffffff 0 1856 ret KSC_AERR_MALFORMED_CAP 1857 end 1858 if eq mops 0 1859 ret KSC_AERR_MALFORMED_CAP 1860 end 1861 if eq mbytes 0 1862 ret KSC_AERR_MALFORMED_CAP 1863 end 1864 if eq rkind KSC_CAP_OBJECT 1865 if ne bkind 1 1866 ret KSC_AERR_MALFORMED_CAP 1867 end 1868 if gt boff sub 0xffffffffffffffff blen 1869 ret KSC_AERR_LEN_OVERFLOW 1870 end 1871 if eq blen 0 1872 ret KSC_AERR_MALFORMED_CAP 1873 end 1874 if gt mbytes blen 1875 ret KSC_AERR_MALFORMED_CAP 1876 end 1877 ret KSC_AERR_OK 1878 end 1879 if eq rkind KSC_CAP_CHANNEL 1880 if ne bkind 2 1881 ret KSC_AERR_MALFORMED_CAP 1882 end 1883 if ne boff 0 1884 ret KSC_AERR_MALFORMED_CAP 1885 end 1886 if ne blen 0 1887 ret KSC_AERR_MALFORMED_CAP 1888 end 1889 ret KSC_AERR_OK 1890 end 1891 ret KSC_AERR_MALFORMED_CAP 1892 end 1893 1894 fn ksc_cap_is_attenuation grant view uchar * req view uchar * to bool 1895 if ne ksc_cap_validate grant KSC_AERR_OK 1896 ret false 1897 end 1898 if ne ksc_cap_validate req KSC_AERR_OK 1899 ret false 1900 end 1901 if ne ksc_get_u16le grant 0 ksc_get_u16le req 0 1902 ret false 1903 end 1904 if ne ksc_get_u32le grant 4 ksc_get_u32le req 4 1905 ret false 1906 end 1907 if ne ksc_get_u32le grant 12 ksc_get_u32le req 12 1908 ret false 1909 end 1910 gr = ksc_get_u32le grant 8 1911 rr = ksc_get_u32le req 8 1912 if ne and gr xor rr 0xffffffff 0 1913 ret false 1914 end 1915 if gt ksc_get_u32le grant 16 ksc_get_u32le req 16 1916 ret false 1917 end 1918 if gt ksc_get_u64le grant 24 ksc_get_u64le req 24 1919 ret false 1920 end 1921 bk = ksc_get_u32le grant 12 1922 if eq bk 1 1923 goff = ksc_get_u64le grant 32 1924 glen = ksc_get_u64le grant 40 1925 roff = ksc_get_u64le req 32 1926 rlen = ksc_get_u64le req 40 1927 if lt goff roff 1928 ret false 1929 end 1930 if gt add goff glen add roff rlen 1931 ret false 1932 end 1933 ret true 1934 end 1935 if eq bk 2 1936 if ne ksc_get_u64le grant 32 0 1937 ret false 1938 end 1939 if ne ksc_get_u64le grant 40 0 1940 ret false 1941 end 1942 ret true 1943 end 1944 ret false 1945 end 1946 1947 fn ksc_cap_intersect_range req view uchar * pol view uchar * res edit ulong 2 to ulong 1948 roff = ksc_get_u64le req 32 1949 rlen = ksc_get_u64le req 40 1950 poff = ksc_get_u64le pol 32 1951 plen = ksc_get_u64le pol 40 1952 if gt roff sub 0xffffffffffffffff rlen 1953 ret KSC_AERR_LEN_OVERFLOW 1954 end 1955 if gt poff sub 0xffffffffffffffff plen 1956 ret KSC_AERR_LEN_OVERFLOW 1957 end 1958 start = roff 1959 if gt poff start 1960 start = poff 1961 end 1962 finish = add roff rlen 1963 if lt add poff plen finish 1964 finish = add poff plen 1965 end 1966 if ge start finish 1967 ret 1 1968 end 1969 put res 0 start 1970 put res 1 sub finish start 1971 ret 0 1972 end 1973 1974 note Intersect request and policy rule into grant. 1975 note Returns 0 = granted, 1 = empty (no grant), >1 = ArtifactError. 1976 fn ksc_cap_intersect req view uchar * pol view uchar * grant edit uchar 48 to ulong 1977 rng = new ulong 2 0 1978 rights = and ksc_get_u32le req 8 ksc_get_u32le pol 8 1979 if eq rights 0 1980 ret 1 1981 end 1982 mops = ksc_get_u32le req 16 1983 if lt ksc_get_u32le pol 16 mops 1984 mops = ksc_get_u32le pol 16 1985 end 1986 mbytes = ksc_get_u64le req 24 1987 if lt ksc_get_u64le pol 24 mbytes 1988 mbytes = ksc_get_u64le pol 24 1989 end 1990 bkind = ksc_get_u32le req 12 1991 if eq bkind 1 1992 rrc = ksc_cap_intersect_range req pol rng 1993 if ne rrc 0 1994 ret rrc 1995 end 1996 if gt mbytes get rng 1 1997 mbytes = get rng 1 1998 end 1999 else 2000 if ne bkind 2 2001 ret KSC_AERR_MALFORMED_CAP 2002 end 2003 end 2004 if eq mops 0 2005 ret 1 2006 end 2007 if eq mbytes 0 2008 ret 1 2009 end 2010 ksc_zeroize grant 2011 ksc_put_u16le grant 0 ksc_get_u16le req 0 2012 ksc_put_u32le grant 4 ksc_get_u32le req 4 2013 ksc_put_u32le grant 8 rights 2014 ksc_put_u32le grant 12 bkind 2015 ksc_put_u32le grant 16 mops 2016 ksc_put_u64le grant 24 mbytes 2017 ksc_put_u64le grant 32 get rng 0 2018 ksc_put_u64le grant 40 get rng 1 2019 ret ksc_cap_validate grant 2020 end 2021 2022 note Limits/Resources array (9 ulongs): 2023 note [0]=code_pages, [1]=data_pages, [2]=stack_pages, [3]=max_caps, 2024 note [4]=ipc_msgs, [5]=ipc_bytes, [6]=cpu_ticks, [7]=elapsed_ticks, [8]=syscalls 2025 fn ksc_admission_limits_validate lim view ulong 9 to ulong 2026 if gt get lim 0 KSC_MAX_CODE_PAGES 2027 ret KSC_AERR_RESOURCE_LIMIT 2028 end 2029 if gt get lim 1 KSC_MAX_DATA_PAGES 2030 ret KSC_AERR_RESOURCE_LIMIT 2031 end 2032 if gt get lim 2 KSC_MAX_STACK_PAGES 2033 ret KSC_AERR_RESOURCE_LIMIT 2034 end 2035 if gt get lim 3 KSC_MAX_CAPABILITIES 2036 ret KSC_AERR_RESOURCE_LIMIT 2037 end 2038 if gt get lim 4 KSC_MAX_IPC_MSGS 2039 ret KSC_AERR_RESOURCE_LIMIT 2040 end 2041 if gt get lim 5 KSC_MAX_IPC_BYTES 2042 ret KSC_AERR_RESOURCE_LIMIT 2043 end 2044 if gt get lim 6 KSC_MAX_CPU_TICKS 2045 ret KSC_AERR_RESOURCE_LIMIT 2046 end 2047 if gt get lim 7 KSC_MAX_ELAPSED_TICKS 2048 ret KSC_AERR_RESOURCE_LIMIT 2049 end 2050 if gt get lim 8 KSC_MAX_SYSCALLS 2051 ret KSC_AERR_RESOURCE_LIMIT 2052 end 2053 ret KSC_AERR_OK 2054 end 2055 2056 fn ksc_admission_check_resources req view ulong 9 lim view ulong 9 avail view ulong 9 to ulong 2057 for i ulong 9 unroll 2058 r = get req i 2059 if gt r get lim i 2060 ret KSC_AERR_RESOURCE_LIMIT 2061 end 2062 if gt r get avail i 2063 ret KSC_AERR_RESOURCE_LIMIT 2064 end 2065 end 2066 if gt get req 0 `1048575` 2067 ret KSC_AERR_LEN_OVERFLOW 2068 end 2069 if gt get req 1 `1048575` 2070 ret KSC_AERR_LEN_OVERFLOW 2071 end 2072 if gt get req 2 `1048575` 2073 ret KSC_AERR_LEN_OVERFLOW 2074 end 2075 ret KSC_AERR_OK 2076 end 2077 2078 note AdmissionPolicy flat buffer: 1088 bytes = 64 hdr + 16*48 rules + 8*32 signers. 2079 note Header matches canonical_bytes() layout: 2080 note [0..8) "AIENPOL\0", [8..10) 0, [10..12) 64, [12..14) 48, [14..16) rcnt, 2081 note [16..60) limits, [60..62) scnt, [62..64) 0. 2082 fn ksc_policy_init pol edit uchar 1088 lim view ulong 9 to ulong 2083 rc = ksc_admission_limits_validate lim 2084 if ne rc KSC_AERR_OK 2085 ret rc 2086 end 2087 ksc_zeroize pol 2088 ksc_copy part pol 0 7 KSC_TXT_MAGIC_POL 2089 put pol 7 uchar 0 2090 ksc_put_u16le pol 10 KSC_POLICY_HDR_SIZE 2091 ksc_put_u16le pol 12 KSC_POLICY_REC_SIZE 2092 ksc_put_u32le pol 16 get lim 0 2093 ksc_put_u32le pol 20 get lim 1 2094 ksc_put_u32le pol 24 get lim 2 2095 ksc_put_u32le pol 28 get lim 3 2096 ksc_put_u32le pol 32 get lim 4 2097 ksc_put_u32le pol 36 get lim 5 2098 ksc_put_u64le pol 40 get lim 6 2099 ksc_put_u64le pol 48 get lim 7 2100 ksc_put_u32le pol 56 get lim 8 2101 ret KSC_AERR_OK 2102 end 2103 2104 fn ksc_cap_key rec view uchar * to ulong 2105 ret or shl ksc_get_u16le rec 0 32 ksc_get_u32le rec 4 2106 end 2107 2108 fn ksc_policy_add_rule pol edit uchar 1088 rule view uchar 48 to ulong 2109 rc = ksc_cap_validate rule 2110 if ne rc KSC_AERR_OK 2111 ret rc 2112 end 2113 rcnt = ksc_get_u16le pol 14 2114 key = ksc_cap_key rule 2115 ins = rcnt 2116 for i ulong rcnt bound 16 2117 off = add KSC_POLICY_HDR_SIZE mul i KSC_POLICY_REC_SIZE 2118 ckey = ksc_cap_key part pol off 48 2119 if eq ckey key 2120 ret KSC_AERR_MALFORMED_CAP 2121 end 2122 if gt ckey key 2123 ins = i 2124 break 2125 end 2126 end 2127 if eq rcnt KSC_MAX_POLICY_RESOURCES 2128 ret KSC_AERR_RESOURCE_LIMIT 2129 end 2130 j = rcnt 2131 while gt j ins bound 16 2132 soff = add KSC_POLICY_HDR_SIZE mul sub j 1 KSC_POLICY_REC_SIZE 2133 doff = add KSC_POLICY_HDR_SIZE mul j KSC_POLICY_REC_SIZE 2134 for b ulong 48 unroll 2135 put pol add doff b get pol add soff b 2136 end 2137 j = sub j 1 2138 end 2139 ioff = add KSC_POLICY_HDR_SIZE mul ins KSC_POLICY_REC_SIZE 2140 ksc_copy part pol ioff 48 rule 2141 ksc_put_u16le pol 14 add rcnt 1 2142 ret KSC_AERR_OK 2143 end 2144 2145 fn ksc_policy_allow_signer pol edit uchar 1088 fp view uchar 32 to ulong 2146 scnt = ksc_get_u16le pol 60 2147 sbase = add KSC_POLICY_HDR_SIZE mul KSC_MAX_POLICY_RESOURCES KSC_POLICY_REC_SIZE 2148 ins = scnt 2149 for i ulong scnt bound 8 2150 off = add sbase mul i KSC_POLICY_SIGNER_SIZE 2151 c = ksc_cmp_lex part pol off 32 fp 2152 if eq c int 0 2153 ret KSC_AERR_UNTRUSTED_SIGNER 2154 end 2155 if gt c int 0 2156 ins = i 2157 break 2158 end 2159 end 2160 if eq scnt KSC_MAX_POLICY_SIGNERS 2161 ret KSC_AERR_RESOURCE_LIMIT 2162 end 2163 j = scnt 2164 while gt j ins bound 8 2165 soff = add sbase mul sub j 1 KSC_POLICY_SIGNER_SIZE 2166 doff = add sbase mul j KSC_POLICY_SIGNER_SIZE 2167 for b ulong 32 unroll 2168 put pol add doff b get pol add soff b 2169 end 2170 j = sub j 1 2171 end 2172 ioff = add sbase mul ins KSC_POLICY_SIGNER_SIZE 2173 ksc_copy part pol ioff 32 fp 2174 ksc_put_u16le pol 60 add scnt 1 2175 ret KSC_AERR_OK 2176 end 2177 2178 fn ksc_policy_has_signer pol view uchar 1088 fp view uchar 32 to bool 2179 scnt = ksc_get_u16le pol 60 2180 sbase = add KSC_POLICY_HDR_SIZE mul KSC_MAX_POLICY_RESOURCES KSC_POLICY_REC_SIZE 2181 for i ulong scnt bound 8 2182 off = add sbase mul i KSC_POLICY_SIGNER_SIZE 2183 if ksc_ct_eq part pol off 32 fp 2184 ret true 2185 end 2186 end 2187 ret false 2188 end 2189 2190 note Canonical packed bytes (header + rcnt*48 + scnt*32) and SHA-256 policy digest 2191 fn ksc_policy_digest pol view uchar 1088 dig edit uchar 32 to ulong 2192 sctx = new ulong 40 0 2193 nul = new uchar 1 0 2194 rcnt = ksc_get_u16le pol 14 2195 scnt = ksc_get_u16le pol 60 2196 rbytes = mul rcnt KSC_POLICY_REC_SIZE 2197 sbase = add KSC_POLICY_HDR_SIZE mul KSC_MAX_POLICY_RESOURCES KSC_POLICY_REC_SIZE 2198 sbytes = mul scnt KSC_POLICY_SIGNER_SIZE 2199 sha256_init sctx 2200 sha256_update sctx KSC_TXT_POL_DOM 2201 sha256_update sctx nul 2202 sha256_update sctx part pol 0 add KSC_POLICY_HDR_SIZE rbytes 2203 if gt sbytes 0 2204 sha256_update sctx part pol sbase sbytes 2205 end 2206 sha256_final sctx dig 2207 ret add add KSC_POLICY_HDR_SIZE rbytes sbytes 2208 end 2209 2210 fn ksc_policy_find_rule pol view uchar 1088 req view uchar * to ulong 2211 rcnt = ksc_get_u16le pol 14 2212 key = ksc_cap_key req 2213 for i ulong rcnt bound 16 2214 off = add KSC_POLICY_HDR_SIZE mul i KSC_POLICY_REC_SIZE 2215 if eq ksc_cap_key part pol off 48 key 2216 ret i 2217 end 2218 end 2219 ret 99 2220 end 2221 2222 fn ksc_grants_digest grants view uchar 768 gcnt ulong gdig edit uchar 32 to void 2223 sctx = new ulong 40 0 2224 nul = new uchar 1 0 2225 sha256_init sctx 2226 sha256_update sctx KSC_TXT_GRANTS_DOM 2227 sha256_update sctx nul 2228 if gt gcnt 0 2229 sha256_update sctx part grants 0 mul gcnt 48 2230 end 2231 sha256_final sctx gdig 2232 end 2233 2234 fn ksc_policy_eval_one pol view uchar 1088 req view uchar * grec edit uchar 48 to ulong 2235 ridx = ksc_policy_find_rule pol req 2236 if ge ridx 16 2237 ret 1 2238 end 2239 poff = add KSC_POLICY_HDR_SIZE mul ridx KSC_POLICY_REC_SIZE 2240 irc = ksc_cap_intersect req part pol poff 48 grec 2241 if eq irc 0 2242 if not ksc_cap_is_attenuation grec req 2243 ret KSC_AERR_RIGHTS_ESC 2244 end 2245 if not ksc_cap_is_attenuation grec part pol poff 48 2246 ret KSC_AERR_RIGHTS_ESC 2247 end 2248 ret 0 2249 end 2250 ret irc 2251 end 2252 2253 note Evaluate requested capabilities against policy; writes granted records into grants[0..gcnt*48) 2254 note and granted_capability_digest into gdig. Returns gcnt (0..16), or >16 on error. 2255 fn ksc_policy_eval_caps pol view uchar 1088 reqs view uchar * rcnt ulong grants edit uchar 768 gdig edit uchar 32 to ulong 2256 grec = new uchar 48 0 2257 gcnt = ulong 0 2258 for i ulong rcnt bound 16 2259 irc = ksc_policy_eval_one pol part reqs mul i KSC_POLICY_REC_SIZE 48 grec 2260 if eq irc 0 2261 ksc_copy part grants mul gcnt 48 48 grec 2262 gcnt = add gcnt 1 2263 else 2264 if gt irc 1 2265 ret irc 2266 end 2267 end 2268 end 2269 ksc_grants_digest grants gcnt gdig 2270 ret gcnt 2271 end 2272 2273 note ========================================================================= 2274 note 7. Artifact Loader Layout, W^X Audit, Fault Classification, Receipt Flags 2275 note ========================================================================= 2276 2277 note lens[0]=code_len, [1]=data_len, [2]=data_mem_len, [3]=entry_offset 2278 note out_lay[0]=base, [1]=code_base, [2]=code_end, [3]=data_base, [4]=data_end, 2279 note [5]=stack_base, [6]=stack_top, [7]=entry_pc, [8]=content_frames, 2280 note [9]=total_frames, [10]=data_first, [11]=stack_first 2281 fn ksc_task_layout_init win_idx ulong res view ulong 9 lens view ulong 4 out_lay edit ulong 12 to ulong 2282 if eq win_idx 0 2283 ret KSC_AERR_RESOURCE_LIMIT 2284 end 2285 if ge win_idx 256 2286 ret KSC_AERR_RESOURCE_LIMIT 2287 end 2288 cp = get res 0 2289 dp = get res 1 2290 sp = get res 2 2291 if eq cp 0 2292 ret KSC_AERR_RESOURCE_LIMIT 2293 end 2294 if gt cp KSC_MAX_CODE_PAGES 2295 ret KSC_AERR_RESOURCE_LIMIT 2296 end 2297 if eq dp 0 2298 ret KSC_AERR_RESOURCE_LIMIT 2299 end 2300 if gt dp KSC_MAX_DATA_PAGES 2301 ret KSC_AERR_RESOURCE_LIMIT 2302 end 2303 if eq sp 0 2304 ret KSC_AERR_RESOURCE_LIMIT 2305 end 2306 if gt sp KSC_MAX_STACK_PAGES 2307 ret KSC_AERR_RESOURCE_LIMIT 2308 end 2309 if eq get lens 0 0 2310 ret KSC_AERR_RESOURCE_LIMIT 2311 end 2312 if gt get lens 0 mul cp KSC_PAGE_SIZE 2313 ret KSC_AERR_RESOURCE_LIMIT 2314 end 2315 if gt get lens 1 get lens 2 2316 ret KSC_AERR_RESOURCE_LIMIT 2317 end 2318 if gt get lens 2 mul dp KSC_PAGE_SIZE 2319 ret KSC_AERR_RESOURCE_LIMIT 2320 end 2321 if ne and get lens 3 3 0 2322 ret KSC_AERR_BAD_ENTRY 2323 end 2324 if gt add get lens 3 4 get lens 0 2325 ret KSC_AERR_BAD_ENTRY 2326 end 2327 dfirst = add 2 cp 2328 sfirst = add add dfirst dp 1 2329 if gt add add sfirst sp 1 512 2330 ret KSC_AERR_RESOURCE_LIMIT 2331 end 2332 base = shl win_idx 39 2333 put out_lay 0 base 2334 put out_lay 1 add base KSC_PAGE_SIZE 2335 put out_lay 2 add add base KSC_PAGE_SIZE mul cp KSC_PAGE_SIZE 2336 put out_lay 3 add base mul dfirst KSC_PAGE_SIZE 2337 put out_lay 4 add add base mul dfirst KSC_PAGE_SIZE mul dp KSC_PAGE_SIZE 2338 put out_lay 5 add base mul sfirst KSC_PAGE_SIZE 2339 put out_lay 6 add add base mul sfirst KSC_PAGE_SIZE mul sp KSC_PAGE_SIZE 2340 put out_lay 7 add add base KSC_PAGE_SIZE get lens 3 2341 put out_lay 8 add add cp dp sp 2342 put out_lay 9 add add add cp dp sp KSC_TABLE_FRAMES 2343 put out_lay 10 dfirst 2344 put out_lay 11 sfirst 2345 ret KSC_AERR_OK 2346 end 2347 2348 fn ksc_page_desc_is_wx desc ulong to bool 2349 if eq desc 0 2350 ret false 2351 end 2352 writable = eq and desc KSC_AP_RO 0 2353 if not writable 2354 ret false 2355 end 2356 if eq and desc KSC_UXN 0 2357 ret true 2358 end 2359 if eq and desc KSC_PXN 0 2360 ret true 2361 end 2362 ret false 2363 end 2364 2365 fn ksc_audit_code_alias leaf ulong expected_pa ulong to bool 2366 if ne and leaf 3 KSC_DESC_TABLE 2367 ret false 2368 end 2369 if ne and leaf KSC_ADDR_MASK expected_pa 2370 ret false 2371 end 2372 if eq and leaf KSC_AP_RO 0 2373 ret false 2374 end 2375 if eq and leaf KSC_PXN 0 2376 ret false 2377 end 2378 if eq and leaf KSC_UXN 0 2379 ret false 2380 end 2381 ret true 2382 end 2383 2384 note ranges[0]=code_base, [1]=code_end, [2]=data_base, [3]=data_end, [4]=stack_base, [5]=stack_end 2385 fn ksc_classify_fault esr ulong far ulong ranges view ulong 6 to ulong 2386 ec = and shr esr 26 0x3f 2387 status = and shr esr 2 0xf 2388 perm = eq status 3 2389 trans = eq status 1 2390 if eq ec 0x24 2391 if ne and esr shl ulong 1 6 0 2392 if perm 2393 if ge far get ranges 0 2394 if lt far get ranges 1 2395 ret KSC_FAULT_CODE_WRITE 2396 end 2397 end 2398 end 2399 end 2400 ret KSC_FAULT_OTHER 2401 end 2402 if eq ec 0x20 2403 if perm 2404 in_ds = false 2405 if ge far get ranges 2 2406 if lt far get ranges 3 2407 in_ds = true 2408 end 2409 end 2410 if ge far get ranges 4 2411 if lt far get ranges 5 2412 in_ds = true 2413 end 2414 end 2415 if in_ds 2416 ret KSC_FAULT_EXEC_NX 2417 end 2418 end 2419 if trans 2420 ret KSC_FAULT_EXEC_UNMAPPED 2421 end 2422 end 2423 ret KSC_FAULT_OTHER 2424 end 2425 2426 note Compute ADR 0014 §7 receipt result_flags bitmask. 2427 note obs[0]=admitted, [1]=reclaimed, [2]=reads_ok, [3]=write_denials, 2428 note [4]=forged_denials, [5]=exited_zero, [6]=mapped_match, 2429 note [7]=byte_chain, [8]=wx_sealed 2430 fn ksc_receipt_flags obs view ulong 9 to ulong 2431 flags = ulong 0 2432 if ne get obs 1 0 2433 flags = or flags KSC_RFLAG_RECLAIMED 2434 end 2435 if eq get obs 0 0 2436 ret flags 2437 end 2438 if gt get obs 2 0 2439 flags = or flags KSC_RFLAG_READ_OK 2440 end 2441 if gt get obs 3 0 2442 flags = or flags KSC_RFLAG_WRITE_DENIED 2443 end 2444 if gt get obs 4 0 2445 flags = or flags KSC_RFLAG_FORGED_DENIED 2446 end 2447 if ne get obs 5 0 2448 flags = or flags KSC_RFLAG_CANARY_PASSED 2449 end 2450 if ne get obs 6 0 2451 flags = or flags KSC_RFLAG_MAPPED_MATCH 2452 end 2453 if ne get obs 7 0 2454 flags = or flags KSC_RFLAG_EXECUTED_MATCH 2455 end 2456 if ne get obs 8 0 2457 flags = or flags KSC_RFLAG_WX_SEALED 2458 end 2459 ret flags 2460 end