interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / crates / aienos-kernel / sec_continuity.om

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