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-kernel / sec_continuity_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-kernel/sec_continuity_test.om: |
| 3 | note Behavioral test suite for sec_continuity.om. |
| 4 | note Returns 0 on pass, distinct non-zero exit code on every failure. |
| 5 | |
| 6 | text T_HI "Hi There" |
| 7 | text T_JEFE "Jefe" |
| 8 | text T_JEFE_MSG "what do ya want for nothing?" |
| 9 | text T_RFC4231_C6 "Test Using Larger Than Block-Size Key - Hash Key First" |
| 10 | text T_ENV_PT "Sovereign AIENOS M5 Encrypted State" |
| 11 | text T_ENV_CONF "Confidential branch checkpoint" |
| 12 | text T_WAL_E1 "boot_start" |
| 13 | text T_WAL_E2 "cortex_mount" |
| 14 | text T_WAL_E3 "corrupted_entry_here" |
| 15 | text T_WAL_CP "checkpoint" |
| 16 | text T_SAME "same" |
| 17 | text T_SAME_B "same" |
| 18 | text T_SAMF "samf" |
| 19 | text T_SAME2 "same!" |
| 20 | |
| 21 | fn t_fill b edit uchar * v uchar to void |
| 22 | n = len b |
| 23 | for i ulong n bound 1048576 |
| 24 | put b i v |
| 25 | end |
| 26 | end |
| 27 | |
| 28 | fn t_hex_nibble c uchar to ulong |
| 29 | v = ulong c |
| 30 | if ge v 48 |
| 31 | if le v 57 |
| 32 | ret sub v 48 |
| 33 | end |
| 34 | end |
| 35 | if ge v 97 |
| 36 | if le v 102 |
| 37 | ret add 10 sub v 97 |
| 38 | end |
| 39 | end |
| 40 | trap 8 |
| 41 | end |
| 42 | |
| 43 | fn t_from_hex h view uchar * dst edit uchar * to void |
| 44 | n = div len h 2 |
| 45 | for i ulong n bound 4096 |
| 46 | hi = t_hex_nibble get h mul i 2 |
| 47 | lo = t_hex_nibble get h add mul i 2 1 |
| 48 | put dst i uchar or shl hi 4 lo |
| 49 | end |
| 50 | end |
| 51 | |
| 52 | text H_HMAC1 "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" |
| 53 | text H_HMAC2 "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" |
| 54 | text H_HMAC6 "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54" |
| 55 | text H_HKDF1_PRK "077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5" |
| 56 | text H_HKDF1_OKM "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865" |
| 57 | text H_HKDF2_PRK "19ef24a32c717b167f33a91d6f648bdf96596776afdb6377ac434c1c293ccb04" |
| 58 | text H_HKDF2_OKM "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8" |
| 59 | text H_OPAUTH_KAT "f6162da5f879e9bd4defc7108c463b5d164f4db68ae991781bf798fc14907aaa" |
| 60 | |
| 61 | note 1. Test HMAC-SHA256 & constant_time_eq |
| 62 | fn test_hmac to int |
| 63 | k20 = new uchar 20 0x0b |
| 64 | k131 = new uchar 131 0xaa |
| 65 | tag = new uchar 32 0 |
| 66 | exp = new uchar 32 0 |
| 67 | ksc_hmac_sha256 k20 T_HI tag |
| 68 | t_from_hex H_HMAC1 exp |
| 69 | if not ksc_ct_eq tag exp |
| 70 | ret 1 |
| 71 | end |
| 72 | ksc_hmac_sha256 T_JEFE T_JEFE_MSG tag |
| 73 | t_from_hex H_HMAC2 exp |
| 74 | if not ksc_ct_eq tag exp |
| 75 | ret 2 |
| 76 | end |
| 77 | ksc_hmac_sha256 k131 T_RFC4231_C6 tag |
| 78 | t_from_hex H_HMAC6 exp |
| 79 | if not ksc_ct_eq tag exp |
| 80 | ret 3 |
| 81 | end |
| 82 | if not ksc_ct_eq T_SAME T_SAME_B |
| 83 | ret 4 |
| 84 | end |
| 85 | if ksc_ct_eq T_SAME T_SAMF |
| 86 | ret 5 |
| 87 | end |
| 88 | if ksc_ct_eq T_SAME T_SAME2 |
| 89 | ret 6 |
| 90 | end |
| 91 | ret 0 |
| 92 | end |
| 93 | |
| 94 | note 2. Test HKDF-SHA256 |
| 95 | fn test_hkdf to int |
| 96 | ikm = new uchar 22 0x0b |
| 97 | salt1 = new uchar 13 0 |
| 98 | info1 = new uchar 10 0 |
| 99 | prk = new uchar 32 0 |
| 100 | exp_prk = new uchar 32 0 |
| 101 | okm = new uchar 42 0 |
| 102 | exp_okm = new uchar 42 0 |
| 103 | too_long = new uchar 8161 0 |
| 104 | for i ulong 13 unroll |
| 105 | put salt1 i uchar and i 0xff |
| 106 | end |
| 107 | for i ulong 10 unroll |
| 108 | put info1 i uchar and add 0xf0 i 0xff |
| 109 | end |
| 110 | ksc_hkdf_extract salt1 ikm prk |
| 111 | t_from_hex H_HKDF1_PRK exp_prk |
| 112 | if not ksc_ct_eq prk exp_prk |
| 113 | ret 10 |
| 114 | end |
| 115 | if ne ksc_hkdf_expand prk info1 okm KSC_HKDF_OK |
| 116 | ret 11 |
| 117 | end |
| 118 | t_from_hex H_HKDF1_OKM exp_okm |
| 119 | if not ksc_ct_eq okm exp_okm |
| 120 | ret 12 |
| 121 | end |
| 122 | ksc_hkdf_extract part salt1 0 0 ikm prk |
| 123 | t_from_hex H_HKDF2_PRK exp_prk |
| 124 | if not ksc_ct_eq prk exp_prk |
| 125 | ret 13 |
| 126 | end |
| 127 | if ne ksc_hkdf_expand prk part info1 0 0 okm KSC_HKDF_OK |
| 128 | ret 14 |
| 129 | end |
| 130 | t_from_hex H_HKDF2_OKM exp_okm |
| 131 | if not ksc_ct_eq okm exp_okm |
| 132 | ret 15 |
| 133 | end |
| 134 | if ne ksc_hkdf_expand prk part info1 0 0 too_long KSC_HKDF_ERR_TOO_LONG |
| 135 | ret 16 |
| 136 | end |
| 137 | ret 0 |
| 138 | end |
| 139 | |
| 140 | note 3. Test M5 Streaming Envelope (small, empty, multi-chunk, tamper, hostile headers) |
| 141 | fn test_envelope to int |
| 142 | params = new uchar 72 0 |
| 143 | dparams = new uchar 48 0 |
| 144 | meta = new ulong 4 0 |
| 145 | dmeta = new ulong 4 0 |
| 146 | env = new uchar 256 0 |
| 147 | dec = new uchar 256 0 |
| 148 | exp = new ulong 1 0 |
| 149 | t_fill part params 0 32 uchar 0x42 |
| 150 | t_fill part params 32 16 uchar 0x11 |
| 151 | t_fill part params 48 16 uchar 0x22 |
| 152 | t_fill part params 64 8 uchar 0x33 |
| 153 | ksc_copy dparams part params 0 48 |
| 154 | put meta 0 16 |
| 155 | put meta 1 1 |
| 156 | put meta 2 100 |
| 157 | put meta 3 0 |
| 158 | elen = ksc_env_encrypt params meta T_ENV_PT env |
| 159 | if ne elen add 64 add len T_ENV_PT 16 |
| 160 | ret 20 |
| 161 | end |
| 162 | put dmeta 0 16 |
| 163 | put dmeta 1 1 |
| 164 | rc = ksc_env_decrypt dparams dmeta part env 0 elen dec |
| 165 | if ne rc KSC_ENV_OK |
| 166 | ret 21 |
| 167 | end |
| 168 | if ne get dmeta 2 len T_ENV_PT |
| 169 | ret 22 |
| 170 | end |
| 171 | if not ksc_ct_eq part dec 0 len T_ENV_PT T_ENV_PT |
| 172 | ret 23 |
| 173 | end |
| 174 | note Cross-store UUID mismatch |
| 175 | put dparams 32 uchar 0x99 |
| 176 | if ne ksc_env_decrypt dparams dmeta part env 0 elen dec KSC_ENV_ERR_AUTH |
| 177 | ret 24 |
| 178 | end |
| 179 | put dparams 32 uchar 0x11 |
| 180 | note Mismatched object kind |
| 181 | put dmeta 0 21 |
| 182 | if ne ksc_env_decrypt dparams dmeta part env 0 elen dec KSC_ENV_ERR_AUTH |
| 183 | ret 25 |
| 184 | end |
| 185 | put dmeta 0 16 |
| 186 | note Bit-flip in ciphertext |
| 187 | put env 69 uchar xor ulong get env 69 1 |
| 188 | if ne ksc_env_decrypt dparams dmeta part env 0 elen dec KSC_ENV_ERR_AUTH |
| 189 | ret 26 |
| 190 | end |
| 191 | note Empty plaintext roundtrip |
| 192 | elen0 = ksc_env_encrypt params meta part T_ENV_PT 0 0 env |
| 193 | if ne elen0 80 |
| 194 | ret 27 |
| 195 | end |
| 196 | if ne ksc_env_decrypt dparams dmeta part env 0 80 dec KSC_ENV_OK |
| 197 | ret 28 |
| 198 | end |
| 199 | if ne get dmeta 2 0 |
| 200 | ret 29 |
| 201 | end |
| 202 | note Hostile header checks |
| 203 | ksc_put_u64le env 20 0xffffffffffffffff |
| 204 | if ne ksc_env_decrypt dparams dmeta part env 0 80 dec KSC_ENV_ERR_TOO_LARGE |
| 205 | ret 30 |
| 206 | end |
| 207 | ksc_put_u64le env 20 KSC_ENV_MAX_PT |
| 208 | if ne ksc_env_decrypt dparams dmeta part env 0 80 dec KSC_ENV_ERR_LEN |
| 209 | ret 31 |
| 210 | end |
| 211 | note Expected length overflow checks |
| 212 | if ne ksc_env_expected_len 1 0xffffffffffffffff exp KSC_ENV_ERR_OVERFLOW |
| 213 | ret 32 |
| 214 | end |
| 215 | if ne ksc_env_expected_len 0 1 exp KSC_ENV_ERR_CHUNK |
| 216 | ret 33 |
| 217 | end |
| 218 | ret 0 |
| 219 | end |
| 220 | |
| 221 | note 3b. Test multi-chunk envelope & late-chunk tamper wipe |
| 222 | fn test_envelope_multichunk to int |
| 223 | params = new uchar 72 0 |
| 224 | dparams = new uchar 48 0 |
| 225 | meta = new ulong 4 0 |
| 226 | dmeta = new ulong 4 0 |
| 227 | pt = new uchar 200 0 |
| 228 | env = new uchar 400 0 |
| 229 | dec = new uchar 200 0 |
| 230 | t_fill part params 0 32 uchar 0xaa |
| 231 | t_fill part params 32 16 uchar 0xbb |
| 232 | t_fill part params 48 16 uchar 0xcc |
| 233 | t_fill part params 64 8 uchar 0xdd |
| 234 | ksc_copy dparams part params 0 48 |
| 235 | for i ulong 200 bound 200 |
| 236 | put pt i uchar and rem i 251 0xff |
| 237 | end |
| 238 | note Use chunk_size = 64 => 4 chunks (64, 64, 64, 8) |
| 239 | put meta 0 18 |
| 240 | put meta 1 1 |
| 241 | put meta 2 42 |
| 242 | put meta 3 64 |
| 243 | elen = ksc_env_encrypt params meta pt env |
| 244 | if ne elen ulong 328 |
| 245 | ret 34 |
| 246 | end |
| 247 | put dmeta 0 18 |
| 248 | put dmeta 1 1 |
| 249 | if ne ksc_env_decrypt dparams dmeta part env 0 elen dec KSC_ENV_OK |
| 250 | ret 35 |
| 251 | end |
| 252 | if not ksc_ct_eq dec pt |
| 253 | ret 36 |
| 254 | end |
| 255 | note Corrupt last chunk tag -> must reject AND wipe dec |
| 256 | put env sub elen 1 uchar xor ulong get env sub elen 1 1 |
| 257 | if ne ksc_env_decrypt dparams dmeta part env 0 elen dec KSC_ENV_ERR_AUTH |
| 258 | ret 37 |
| 259 | end |
| 260 | if not ksc_is_zero part dec 0 192 |
| 261 | ret 38 |
| 262 | end |
| 263 | ret 0 |
| 264 | end |
| 265 | |
| 266 | note 4a. Test Security Subkeys and KeySlots |
| 267 | fn test_security_keys to int |
| 268 | kvol = new uchar 32 0x5a |
| 269 | subk = new uchar 128 0 |
| 270 | slot = new uchar 128 0 |
| 271 | kek = new uchar 32 0x88 |
| 272 | wkek = new uchar 32 0x99 |
| 273 | nonce = new uchar 12 0x33 |
| 274 | unw = new uchar 32 0 |
| 275 | ksc_derive_subkeys kvol subk |
| 276 | ksc_copy unw part subk 0 32 |
| 277 | if ksc_ct_eq unw part subk 32 32 |
| 278 | ret 40 |
| 279 | end |
| 280 | if ksc_ct_eq unw part subk 64 32 |
| 281 | ret 41 |
| 282 | end |
| 283 | if ksc_ct_eq unw part subk 96 32 |
| 284 | ret 42 |
| 285 | end |
| 286 | if ksc_ct_eq unw kvol |
| 287 | ret 43 |
| 288 | end |
| 289 | put slot 0 uchar 3 |
| 290 | ksc_put_u32le slot 4 0 |
| 291 | ksc_put_u64le slot 8 1 |
| 292 | if ne ksc_keyslot_wrap slot kek kvol nonce KSC_SEC_OK |
| 293 | ret 44 |
| 294 | end |
| 295 | if ne ksc_keyslot_desc_validate slot KSC_SEC_OK |
| 296 | ret 45 |
| 297 | end |
| 298 | if ne ksc_keyslot_unwrap slot kek unw KSC_SEC_OK |
| 299 | ret 46 |
| 300 | end |
| 301 | if not ksc_ct_eq unw kvol |
| 302 | ret 47 |
| 303 | end |
| 304 | if ne ksc_keyslot_unwrap slot wkek unw KSC_SEC_ERR_AUTH |
| 305 | ret 48 |
| 306 | end |
| 307 | ret 0 |
| 308 | end |
| 309 | |
| 310 | note 4b. Test SecurityManifest, MigrationManifest, Anti-Rollback |
| 311 | fn test_security_manifests to int |
| 312 | sec = new uchar 256 0 |
| 313 | mig = new uchar 288 0 |
| 314 | kauth = new uchar 32 0x44 |
| 315 | wkauth = new uchar 32 0x55 |
| 316 | d1 = new uchar 32 0xaa |
| 317 | d1b = new uchar 32 0xaa |
| 318 | d2 = new uchar 32 0xbb |
| 319 | ksc_copy part sec 0 8 KSC_TXT_SEC_MAGIC |
| 320 | ksc_put_u16le sec 8 1 |
| 321 | ksc_put_u64le sec 32 10 |
| 322 | ksc_put_u64le sec 40 1 |
| 323 | ksc_put_u64le sec 48 5 |
| 324 | ksc_sec_manifest_sign sec kauth |
| 325 | if ne ksc_sec_manifest_validate sec KSC_SEC_OK |
| 326 | ret 49 |
| 327 | end |
| 328 | if not ksc_sec_manifest_verify sec kauth |
| 329 | ret 50 |
| 330 | end |
| 331 | if ksc_sec_manifest_verify sec wkauth |
| 332 | ret 51 |
| 333 | end |
| 334 | ksc_copy part mig 0 8 KSC_TXT_MIG_MAGIC |
| 335 | ksc_put_u16le mig 8 1 |
| 336 | if ne ksc_mig_manifest_validate mig KSC_SEC_OK |
| 337 | ret 52 |
| 338 | end |
| 339 | if ne ksc_eval_anti_rollback 1 10 10 d1 d1b KSC_ARB_VALID_RESUME |
| 340 | ret 53 |
| 341 | end |
| 342 | if ne ksc_eval_anti_rollback 1 10 11 d1 d2 KSC_ARB_PREPARED_ADVANCE |
| 343 | ret 54 |
| 344 | end |
| 345 | if ne ksc_eval_anti_rollback 1 10 9 d1 d1b KSC_ARB_ROLLBACK_STALE |
| 346 | ret 55 |
| 347 | end |
| 348 | if ne ksc_eval_anti_rollback 1 10 10 d1 d2 KSC_ARB_ROLLBACK_DIGEST |
| 349 | ret 56 |
| 350 | end |
| 351 | if ne ksc_eval_anti_rollback 1 10 12 d1 d2 KSC_ARB_INCONSISTENT_EPOCH |
| 352 | ret 57 |
| 353 | end |
| 354 | if ne ksc_eval_anti_rollback 0 0 1 d1 d2 KSC_ARB_PREPARED_ADVANCE |
| 355 | ret 58 |
| 356 | end |
| 357 | ret 0 |
| 358 | end |
| 359 | |
| 360 | note 5. Test BudgetManager & Watchdog Policy |
| 361 | fn test_budget to int |
| 362 | mgr = new ulong 132 0 |
| 363 | env = new ulong 4 0 |
| 364 | exp = new ulong 8 0 |
| 365 | al = new ulong 1 0 |
| 366 | st = new ulong 1 0 |
| 367 | put env 0 100 |
| 368 | put env 1 4 |
| 369 | put env 2 32 |
| 370 | put env 3 10 |
| 371 | ksc_budget_init mgr 2 |
| 372 | if ne ksc_budget_admit mgr 7 env 0 0 KSC_BERR_OK |
| 373 | ret 60 |
| 374 | end |
| 375 | if ne ksc_budget_charge_cpu mgr 7 101 KSC_BERR_CPU |
| 376 | ret 61 |
| 377 | end |
| 378 | if ne ksc_budget_charge_frames mgr 7 5 KSC_BERR_FRAMES |
| 379 | ret 62 |
| 380 | end |
| 381 | if ne ksc_budget_charge_dma mgr 7 33 KSC_BERR_DMA |
| 382 | ret 63 |
| 383 | end |
| 384 | if ne ksc_budget_charge_frames mgr 7 4 KSC_BERR_OK |
| 385 | ret 64 |
| 386 | end |
| 387 | if ne ksc_budget_release_frames mgr 7 2 KSC_BERR_OK |
| 388 | ret 65 |
| 389 | end |
| 390 | if ne ksc_budget_charge_frames mgr 7 2 KSC_BERR_OK |
| 391 | ret 66 |
| 392 | end |
| 393 | if ne ksc_budget_release_frames mgr 7 5 KSC_BERR_BAD_RELEASE |
| 394 | ret 67 |
| 395 | end |
| 396 | note Watchdog pet, expiry, sticky quarantine, restore |
| 397 | if ne ksc_budget_check mgr 9 exp 0 |
| 398 | ret 68 |
| 399 | end |
| 400 | if ne ksc_budget_pet mgr 7 8 KSC_BERR_OK |
| 401 | ret 69 |
| 402 | end |
| 403 | if ne ksc_budget_check mgr 17 exp 0 |
| 404 | ret 70 |
| 405 | end |
| 406 | if ne ksc_budget_check mgr 18 exp 1 |
| 407 | ret 71 |
| 408 | end |
| 409 | if ne get exp 0 7 |
| 410 | ret 72 |
| 411 | end |
| 412 | if ne ksc_budget_charge_cpu mgr 7 1 KSC_BERR_QUARANTINED |
| 413 | ret 73 |
| 414 | end |
| 415 | if ne ksc_budget_pet mgr 7 20 KSC_BERR_QUARANTINED |
| 416 | ret 74 |
| 417 | end |
| 418 | if ne ksc_budget_restore mgr 7 20 KSC_BERR_OK |
| 419 | ret 75 |
| 420 | end |
| 421 | if ne ksc_budget_state mgr 7 st KSC_BERR_OK |
| 422 | ret 76 |
| 423 | end |
| 424 | if ne get st 0 KSC_WSTATE_ACTIVE |
| 425 | ret 77 |
| 426 | end |
| 427 | note Forge policy scaling |
| 428 | if ne ksc_budget_admit mgr 1 env 1 0 KSC_BERR_OK |
| 429 | ret 78 |
| 430 | end |
| 431 | if ne ksc_budget_cpu_allowance mgr 1 al KSC_BERR_OK |
| 432 | ret 79 |
| 433 | end |
| 434 | if ne get al 0 10 |
| 435 | ret 80 |
| 436 | end |
| 437 | ksc_budget_set_mode mgr KSC_BMODE_IDLE |
| 438 | if ne ksc_budget_cpu_allowance mgr 1 al KSC_BERR_OK |
| 439 | ret 81 |
| 440 | end |
| 441 | if ne get al 0 80 |
| 442 | ret 82 |
| 443 | end |
| 444 | ksc_budget_set_mode mgr KSC_BMODE_URGENT |
| 445 | if ne ksc_budget_cpu_allowance mgr 1 al KSC_BERR_OK |
| 446 | ret 83 |
| 447 | end |
| 448 | if ne get al 0 150 |
| 449 | ret 84 |
| 450 | end |
| 451 | note Counter wrap regression: start = u64::MAX - 4, max_rt = 10 |
| 452 | ksc_budget_init mgr 4 |
| 453 | start = sub ulong 0xffffffffffffffff 4 |
| 454 | if ne ksc_budget_admit mgr 1 env 0 start KSC_BERR_OK |
| 455 | ret 85 |
| 456 | end |
| 457 | if ne ksc_budget_check mgr 0xffffffffffffffff exp 0 |
| 458 | ret 86 |
| 459 | end |
| 460 | if ne ksc_budget_check mgr add start 9 exp 0 |
| 461 | ret 87 |
| 462 | end |
| 463 | if ne ksc_budget_check mgr add start 10 exp 1 |
| 464 | ret 88 |
| 465 | end |
| 466 | note Overflow safety at u64::MAX |
| 467 | ksc_budget_init mgr 1 |
| 468 | put env 0 0xffffffffffffffff |
| 469 | put env 1 0xffffffffffffffff |
| 470 | put env 2 0xffffffffffffffff |
| 471 | put env 3 0xffffffffffffffff |
| 472 | if ne ksc_budget_admit mgr 1 env 0 0xfffffffffffffffe KSC_BERR_OK |
| 473 | ret 89 |
| 474 | end |
| 475 | if ne ksc_budget_charge_cpu mgr 1 0xffffffffffffffff KSC_BERR_OK |
| 476 | ret 90 |
| 477 | end |
| 478 | if ne ksc_budget_charge_cpu mgr 1 1 KSC_BERR_CPU |
| 479 | ret 91 |
| 480 | end |
| 481 | if ne ksc_budget_check mgr 0xffffffffffffffff exp 0 |
| 482 | ret 92 |
| 483 | end |
| 484 | ret 0 |
| 485 | end |
| 486 | |
| 487 | note 6a. Test OperatorAuth and SlotManager |
| 488 | fn test_recovery_auth to int |
| 489 | key = new uchar 32 0 |
| 490 | ch = new uchar 32 0 |
| 491 | kat = new uchar 32 0 |
| 492 | resp = new uchar 32 0 |
| 493 | bad = new uchar 32 0 |
| 494 | sm = new ulong 5 0 |
| 495 | for i ulong 32 unroll |
| 496 | put key i uchar and i 0xff |
| 497 | put ch i uchar and add 0xa0 i 0xff |
| 498 | end |
| 499 | t_from_hex H_OPAUTH_KAT kat |
| 500 | ksc_opauth_expected_response key ch resp |
| 501 | if not ksc_ct_eq resp kat |
| 502 | ret 100 |
| 503 | end |
| 504 | if not ksc_opauth_verify key ch kat |
| 505 | ret 101 |
| 506 | end |
| 507 | ksc_copy bad kat |
| 508 | put bad 17 uchar xor ulong get bad 17 4 |
| 509 | if ksc_opauth_verify key ch bad |
| 510 | ret 102 |
| 511 | end |
| 512 | ksc_hmac_sha256 key ch bad |
| 513 | if ksc_opauth_verify key ch bad |
| 514 | ret 103 |
| 515 | end |
| 516 | ksc_zeroize bad |
| 517 | if ksc_opauth_verify key ch bad |
| 518 | ret 104 |
| 519 | end |
| 520 | ksc_slotmgr_init sm |
| 521 | if ksc_slotmgr_record_attempt sm |
| 522 | ret 105 |
| 523 | end |
| 524 | if ksc_slotmgr_record_attempt sm |
| 525 | ret 106 |
| 526 | end |
| 527 | if not ksc_slotmgr_record_attempt sm |
| 528 | ret 107 |
| 529 | end |
| 530 | if ne get sm 0 KSC_SLOT_B |
| 531 | ret 108 |
| 532 | end |
| 533 | if ne get sm 1 KSC_HEALTH_FAILED |
| 534 | ret 109 |
| 535 | end |
| 536 | ksc_slotmgr_mark_healthy sm |
| 537 | if ne get sm 2 KSC_HEALTH_CONFIRMED |
| 538 | ret 110 |
| 539 | end |
| 540 | ret 0 |
| 541 | end |
| 542 | |
| 543 | note 6b. Test WalRecovery truncation & overflow protection |
| 544 | fn test_recovery_wal to int |
| 545 | wbuf = new uchar 512 0 |
| 546 | wres = new ulong 2 0 |
| 547 | n1 = ksc_wal_encode_entry T_WAL_E1 wbuf |
| 548 | n2 = ksc_wal_encode_entry T_WAL_E2 part wbuf n1 sub 512 n1 |
| 549 | off2 = add n1 n2 |
| 550 | ksc_wal_recover part wbuf 0 off2 wres |
| 551 | if ne get wres 0 2 |
| 552 | ret 111 |
| 553 | end |
| 554 | if ne get wres 1 off2 |
| 555 | ret 112 |
| 556 | end |
| 557 | n3 = ksc_wal_encode_entry T_WAL_E3 part wbuf off2 sub 512 off2 |
| 558 | put wbuf add off2 40 uchar xor ulong get wbuf add off2 40 0xff |
| 559 | ksc_wal_recover part wbuf 0 add off2 n3 wres |
| 560 | if ne get wres 0 2 |
| 561 | ret 113 |
| 562 | end |
| 563 | if ne get wres 1 off2 |
| 564 | ret 114 |
| 565 | end |
| 566 | ksc_put_u32be wbuf 0 0xffffffff |
| 567 | ksc_wal_recover part wbuf 0 100 wres |
| 568 | if ne get wres 0 0 |
| 569 | ret 115 |
| 570 | end |
| 571 | ret 0 |
| 572 | end |
| 573 | |
| 574 | note 6b. Test Recovery Core applicability & challenge binding |
| 575 | fn test_recovery_core to int |
| 576 | key = new uchar 32 0x0f |
| 577 | resp = new uchar 32 0 |
| 578 | suuid = new uchar 16 0x5a |
| 579 | sdig1 = new uchar 32 0x33 |
| 580 | sdig2 = new uchar 32 0x44 |
| 581 | ch1 = new uchar 32 0 |
| 582 | ch2 = new uchar 32 0 |
| 583 | ch3 = new uchar 32 0 |
| 584 | if ne ksc_recovery_applicable KSC_MOUNT_DEGRADED KSC_REASON_DEGRADED KSC_PEER_MALFORMED 1 KSC_ACT_REPAIR_DEGRADED_PEER |
| 585 | ret 116 |
| 586 | end |
| 587 | if ne ksc_recovery_applicable KSC_MOUNT_DEGRADED KSC_REASON_DEGRADED KSC_PEER_MALFORMED 0 KSC_ACT_NONE |
| 588 | ret 117 |
| 589 | end |
| 590 | if ne ksc_recovery_applicable KSC_MOUNT_DEGRADED KSC_REASON_DEGRADED KSC_PEER_GRAPH_BAD_NEWER 1 KSC_ACT_NONE |
| 591 | ret 118 |
| 592 | end |
| 593 | if ne ksc_recovery_applicable KSC_MOUNT_VALID KSC_REASON_UNPROVISIONED KSC_PEER_ZERO 0 KSC_ACT_PROVISION_IDENTITY |
| 594 | ret 119 |
| 595 | end |
| 596 | ksc_recovery_challenge suuid 7 KSC_ACT_REPAIR_DEGRADED_PEER sdig1 ch1 |
| 597 | ksc_recovery_challenge suuid 7 KSC_ACT_REPAIR_DEGRADED_PEER sdig2 ch2 |
| 598 | ksc_recovery_challenge suuid 7 KSC_ACT_PROVISION_IDENTITY sdig1 ch3 |
| 599 | if ksc_ct_eq ch1 ch2 |
| 600 | ret 120 |
| 601 | end |
| 602 | if ksc_ct_eq ch1 ch3 |
| 603 | ret 121 |
| 604 | end |
| 605 | ksc_opauth_expected_response key ch1 resp |
| 606 | if ne ksc_recovery_authorise KSC_ACT_REPAIR_DEGRADED_PEER KSC_ACT_REPAIR_DEGRADED_PEER key ch1 resp KSC_REC_OK |
| 607 | ret 122 |
| 608 | end |
| 609 | if ne ksc_recovery_authorise KSC_ACT_REPAIR_DEGRADED_PEER KSC_ACT_PROVISION_IDENTITY key ch1 resp KSC_REC_ERR_NOT_APPLICABLE |
| 610 | ret 123 |
| 611 | end |
| 612 | if ne ksc_recovery_authorise KSC_ACT_REPAIR_DEGRADED_PEER KSC_ACT_REPAIR_DEGRADED_PEER key ch2 resp KSC_REC_ERR_UNAUTHORISED |
| 613 | ret 124 |
| 614 | end |
| 615 | ret 0 |
| 616 | end |
| 617 | |
| 618 | note 7a. Test Continuity AgentRoot, Manifest, CortexWalSegment |
| 619 | fn test_continuity_objs to int |
| 620 | agent = new uchar 32 0x11 |
| 621 | uuid = new uchar 16 0x5a |
| 622 | root_buf = new uchar 112 0 |
| 623 | rmeta = new ulong 2 0 |
| 624 | mani = new uchar 168 0 |
| 625 | mmeta = new ulong 3 0 |
| 626 | wseg = new uchar 96 0 |
| 627 | wmeta = new ulong 2 0 |
| 628 | if ne ksc_agent_root_encode agent uuid 2 1 root_buf 112 |
| 629 | ret 130 |
| 630 | end |
| 631 | if ne ksc_agent_root_decode root_buf rmeta KSC_CERR_OK |
| 632 | ret 131 |
| 633 | end |
| 634 | if ne get rmeta 0 2 |
| 635 | ret 132 |
| 636 | end |
| 637 | put root_buf 64 uchar xor ulong get root_buf 64 1 |
| 638 | if ne ksc_agent_root_decode root_buf rmeta KSC_CERR_ROOT_BRANCH |
| 639 | ret 133 |
| 640 | end |
| 641 | ksc_cont_write_hdr mani KSC_TXT_MAGIC_MANI 120 |
| 642 | put mani 16 uchar 0xa1 |
| 643 | ksc_put_u64le mani 80 1 |
| 644 | ksc_put_u64le mani 88 1 |
| 645 | if ne ksc_manifest_decode part mani 0 136 mmeta KSC_CERR_OK |
| 646 | ret 134 |
| 647 | end |
| 648 | put mani 48 uchar 0xd4 |
| 649 | if ne ksc_manifest_decode part mani 0 136 mmeta KSC_CERR_SEQ_PREV |
| 650 | ret 135 |
| 651 | end |
| 652 | ksc_put_u64le mani 80 2 |
| 653 | if ne ksc_manifest_decode part mani 0 136 mmeta KSC_CERR_OK |
| 654 | ret 136 |
| 655 | end |
| 656 | ksc_cont_write_hdr wseg KSC_TXT_MAGIC_CWAL 56 |
| 657 | ksc_put_u64le wseg 16 3 |
| 658 | ksc_put_u32le wseg 24 1 |
| 659 | put wseg 32 uchar 6 |
| 660 | ksc_put_u16le wseg 34 4 |
| 661 | put wseg 68 uchar 65 |
| 662 | put wseg 69 uchar 66 |
| 663 | put wseg 70 uchar 67 |
| 664 | put wseg 71 uchar 68 |
| 665 | if ne ksc_wal_seg_decode part wseg 0 72 wmeta KSC_CERR_OK |
| 666 | ret 144 |
| 667 | end |
| 668 | put wseg 32 uchar 7 |
| 669 | if ne ksc_wal_seg_decode part wseg 0 72 wmeta KSC_CERR_EPISTEMIC |
| 670 | ret 145 |
| 671 | end |
| 672 | ret 0 |
| 673 | end |
| 674 | |
| 675 | note 7b. Test Continuity AgentState branch table & hostile fork sum |
| 676 | fn test_continuity_branches to int |
| 677 | agent = new uchar 32 0x11 |
| 678 | st_buf = new uchar 512 0 |
| 679 | rb = new uchar 32 0 |
| 680 | c0 = new uchar 32 0 |
| 681 | c1 = new uchar 32 0 |
| 682 | c2 = new uchar 32 0 |
| 683 | slen = ksc_agent_state_genesis agent 1 st_buf |
| 684 | if ne slen 144 |
| 685 | ret 137 |
| 686 | end |
| 687 | if ne ksc_agent_state_validate part st_buf 0 slen KSC_CERR_OK |
| 688 | ret 138 |
| 689 | end |
| 690 | ksc_root_branch_id agent rb |
| 691 | slen = ksc_agent_state_fork st_buf slen rb c0 |
| 692 | if ne slen 224 |
| 693 | ret 139 |
| 694 | end |
| 695 | if ne ksc_agent_state_validate part st_buf 0 slen KSC_CERR_OK |
| 696 | ret 140 |
| 697 | end |
| 698 | ri = ksc_state_find_branch part st_buf 0 slen 2 rb |
| 699 | ci = sub 1 ri |
| 700 | roff = add add 64 mul ri 80 72 |
| 701 | coff = add add 64 mul ci 80 72 |
| 702 | ksc_put_u64le st_buf roff 0xffffffffffffffff |
| 703 | ksc_put_u64le st_buf coff 2 |
| 704 | if ne ksc_agent_state_validate part st_buf 0 slen KSC_CERR_FORK_SUM |
| 705 | ret 141 |
| 706 | end |
| 707 | ksc_put_u64le st_buf roff 1 |
| 708 | ksc_put_u64le st_buf coff 0 |
| 709 | slen = ksc_agent_state_fork st_buf slen rb c1 |
| 710 | slen = ksc_agent_state_fork st_buf slen c0 c2 |
| 711 | if ne slen 384 |
| 712 | ret 142 |
| 713 | end |
| 714 | if ne ksc_agent_state_validate part st_buf 0 slen KSC_CERR_OK |
| 715 | ret 143 |
| 716 | end |
| 717 | ret 0 |
| 718 | end |
| 719 | |
| 720 | fn test_admission_res_check to int |
| 721 | lim = new ulong 9 0 |
| 722 | req = new ulong 9 0 |
| 723 | avail = new ulong 9 0 |
| 724 | put lim 0 1 |
| 725 | put lim 1 1 |
| 726 | put lim 2 1 |
| 727 | put lim 3 1 |
| 728 | put lim 6 10 |
| 729 | put lim 7 20 |
| 730 | put lim 8 16 |
| 731 | for i ulong 9 unroll |
| 732 | put req i get lim i |
| 733 | put avail i get lim i |
| 734 | end |
| 735 | if ne ksc_admission_check_resources req lim avail KSC_AERR_OK |
| 736 | ret 165 |
| 737 | end |
| 738 | put avail 2 0 |
| 739 | if ne ksc_admission_check_resources req lim avail KSC_AERR_RESOURCE_LIMIT |
| 740 | ret 166 |
| 741 | end |
| 742 | ret 0 |
| 743 | end |
| 744 | |
| 745 | note 8a. Test Admission Policy canonical digest |
| 746 | fn test_admission to int |
| 747 | lim = new ulong 9 0 |
| 748 | pol1 = new uchar 1088 0 |
| 749 | pol2 = new uchar 1088 0 |
| 750 | r1 = new uchar 48 0 |
| 751 | r2 = new uchar 48 0 |
| 752 | fp1 = new uchar 32 0x45 |
| 753 | fp2 = new uchar 32 0x99 |
| 754 | pd1 = new uchar 32 0 |
| 755 | pd2 = new uchar 32 0 |
| 756 | put lim 0 1 |
| 757 | put lim 1 1 |
| 758 | put lim 2 1 |
| 759 | put lim 3 1 |
| 760 | put lim 6 10 |
| 761 | put lim 7 20 |
| 762 | put lim 8 16 |
| 763 | if ne ksc_policy_init pol1 lim KSC_AERR_OK |
| 764 | ret 150 |
| 765 | end |
| 766 | if ne ksc_policy_init pol2 lim KSC_AERR_OK |
| 767 | ret 151 |
| 768 | end |
| 769 | ksc_put_u16le r1 0 KSC_CAP_OBJECT |
| 770 | ksc_put_u32le r1 4 7 |
| 771 | ksc_put_u32le r1 8 KSC_RIGHT_READ |
| 772 | ksc_put_u32le r1 12 1 |
| 773 | ksc_put_u32le r1 16 4 |
| 774 | ksc_put_u64le r1 24 4 |
| 775 | ksc_put_u64le r1 32 0 |
| 776 | ksc_put_u64le r1 40 4 |
| 777 | ksc_copy r2 r1 |
| 778 | ksc_put_u32le r2 4 9 |
| 779 | if ne ksc_policy_add_rule pol1 r1 KSC_AERR_OK |
| 780 | ret 152 |
| 781 | end |
| 782 | if ne ksc_policy_add_rule pol1 r2 KSC_AERR_OK |
| 783 | ret 153 |
| 784 | end |
| 785 | if ne ksc_policy_add_rule pol2 r2 KSC_AERR_OK |
| 786 | ret 154 |
| 787 | end |
| 788 | if ne ksc_policy_add_rule pol2 r1 KSC_AERR_OK |
| 789 | ret 155 |
| 790 | end |
| 791 | if ne ksc_policy_allow_signer pol1 fp1 KSC_AERR_OK |
| 792 | ret 156 |
| 793 | end |
| 794 | if ne ksc_policy_allow_signer pol2 fp1 KSC_AERR_OK |
| 795 | ret 157 |
| 796 | end |
| 797 | if ne ksc_policy_digest pol1 pd1 ulong 192 |
| 798 | ret 158 |
| 799 | end |
| 800 | if ne ksc_policy_digest pol2 pd2 ulong 192 |
| 801 | ret 159 |
| 802 | end |
| 803 | if not ksc_ct_eq pd1 pd2 |
| 804 | ret 160 |
| 805 | end |
| 806 | if ne ksc_policy_allow_signer pol2 fp2 KSC_AERR_OK |
| 807 | ret 161 |
| 808 | end |
| 809 | if eq ksc_policy_digest pol2 pd2 0 |
| 810 | ret 162 |
| 811 | end |
| 812 | if ksc_ct_eq pd1 pd2 |
| 813 | ret 162 |
| 814 | end |
| 815 | ret test_admission_caps pol1 r1 |
| 816 | end |
| 817 | |
| 818 | fn test_admission_caps pol1 view uchar 1088 r1 view uchar 48 to int |
| 819 | req_rw = new uchar 48 0 |
| 820 | grants = new uchar 768 0 |
| 821 | gdig = new uchar 32 0 |
| 822 | ksc_copy req_rw r1 |
| 823 | ksc_put_u32le req_rw 8 or KSC_RIGHT_READ KSC_RIGHT_WRITE |
| 824 | if ne ksc_policy_eval_caps pol1 req_rw 1 grants gdig 1 |
| 825 | ret 163 |
| 826 | end |
| 827 | if ne ksc_get_u32le grants 8 KSC_RIGHT_READ |
| 828 | ret 164 |
| 829 | end |
| 830 | if not ksc_cap_is_attenuation part grants 0 48 req_rw |
| 831 | ret 165 |
| 832 | end |
| 833 | ret test_admission_res_check |
| 834 | end |
| 835 | |
| 836 | note 8b. Test Artifact Loader Layout, W^X Audit, Fault Classification, Receipt Flags |
| 837 | fn test_loader to int |
| 838 | lim = new ulong 9 0 |
| 839 | lens = new ulong 4 0 |
| 840 | lay = new ulong 12 0 |
| 841 | ranges = new ulong 6 0 |
| 842 | obs = new ulong 9 0 |
| 843 | put lim 0 1 |
| 844 | put lim 1 1 |
| 845 | put lim 2 1 |
| 846 | put lens 0 64 |
| 847 | put lens 1 27 |
| 848 | put lens 2 4096 |
| 849 | put lens 3 4 |
| 850 | if ne ksc_task_layout_init 1 lim lens lay KSC_AERR_OK |
| 851 | ret 167 |
| 852 | end |
| 853 | base = shl ulong 1 39 |
| 854 | if ne get lay 1 add base 4096 |
| 855 | ret 168 |
| 856 | end |
| 857 | if ne get lay 7 add add base 4096 4 |
| 858 | ret 169 |
| 859 | end |
| 860 | if ne get lay 9 7 |
| 861 | ret 170 |
| 862 | end |
| 863 | d = or KSC_AP_EL0 KSC_DESC_TABLE |
| 864 | if not ksc_page_desc_is_wx d |
| 865 | ret 171 |
| 866 | end |
| 867 | d = or or KSC_AP_EL0 KSC_AP_RO or KSC_PXN KSC_DESC_TABLE |
| 868 | if ksc_page_desc_is_wx d |
| 869 | ret 172 |
| 870 | end |
| 871 | d = or or 0x40010000 KSC_AP_RO or KSC_PXN or KSC_UXN KSC_DESC_TABLE |
| 872 | if not ksc_audit_code_alias d 0x40010000 |
| 873 | ret 173 |
| 874 | end |
| 875 | d = xor d KSC_AP_RO |
| 876 | if ksc_audit_code_alias d 0x40010000 |
| 877 | ret 174 |
| 878 | end |
| 879 | put ranges 0 get lay 1 |
| 880 | put ranges 1 get lay 2 |
| 881 | put ranges 2 get lay 3 |
| 882 | put ranges 3 get lay 4 |
| 883 | put ranges 4 get lay 5 |
| 884 | put ranges 5 get lay 6 |
| 885 | d = or or shl ulong 0x24 26 shl ulong 1 6 shl ulong 3 2 |
| 886 | if ne ksc_classify_fault d get lay 1 ranges KSC_FAULT_CODE_WRITE |
| 887 | ret 175 |
| 888 | end |
| 889 | d = or shl ulong 0x20 26 shl ulong 3 2 |
| 890 | if ne ksc_classify_fault d get lay 3 ranges KSC_FAULT_EXEC_NX |
| 891 | ret 176 |
| 892 | end |
| 893 | d = or shl ulong 0x20 26 shl ulong 1 2 |
| 894 | if ne ksc_classify_fault d 0x1000 ranges KSC_FAULT_EXEC_UNMAPPED |
| 895 | ret 177 |
| 896 | end |
| 897 | for i ulong 9 unroll |
| 898 | put obs i 1 |
| 899 | end |
| 900 | if ne ksc_receipt_flags obs 255 |
| 901 | ret 178 |
| 902 | end |
| 903 | ret 0 |
| 904 | end |
| 905 | |
| 906 | fn main to int |
| 907 | r = test_hmac |
| 908 | if ne r int 0 |
| 909 | ret r |
| 910 | end |
| 911 | r = test_hkdf |
| 912 | if ne r int 0 |
| 913 | ret r |
| 914 | end |
| 915 | r = test_envelope |
| 916 | if ne r int 0 |
| 917 | ret r |
| 918 | end |
| 919 | r = test_envelope_multichunk |
| 920 | if ne r int 0 |
| 921 | ret r |
| 922 | end |
| 923 | r = test_security_keys |
| 924 | if ne r int 0 |
| 925 | ret r |
| 926 | end |
| 927 | r = test_security_manifests |
| 928 | if ne r int 0 |
| 929 | ret r |
| 930 | end |
| 931 | r = test_budget |
| 932 | if ne r int 0 |
| 933 | ret r |
| 934 | end |
| 935 | r = test_recovery_auth |
| 936 | if ne r int 0 |
| 937 | ret r |
| 938 | end |
| 939 | r = test_recovery_wal |
| 940 | if ne r int 0 |
| 941 | ret r |
| 942 | end |
| 943 | r = test_recovery_core |
| 944 | if ne r int 0 |
| 945 | ret r |
| 946 | end |
| 947 | r = test_continuity_objs |
| 948 | if ne r int 0 |
| 949 | ret r |
| 950 | end |
| 951 | r = test_continuity_branches |
| 952 | if ne r int 0 |
| 953 | ret r |
| 954 | end |
| 955 | r = test_admission |
| 956 | if ne r int 0 |
| 957 | ret r |
| 958 | end |
| 959 | r = test_loader |
| 960 | if ne r int 0 |
| 961 | ret r |
| 962 | end |
| 963 | ret 0 |
| 964 | end |