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-artifact-tool / artifact_tool.om

2210 lines · 48738 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-artifact-tool/artifact_tool.om: Pure-Omega Binary Artifact v0
3note and Admission Receipt v0 qualification tool (pack, inspect, id, sign, verify,
4note negative-corpus H01..H29, receipt inspect/check/from-hex/sign-test/verify, CLI dispatch).
5note Converted from aienux crates/aienos-artifact-tool/src/{main,corpus,receipt_cmd}.rs
6note and crates/aienos-artifact/src/{format,capability,resource,canonical,id,signature,verify,receipt}.rs.
7note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-artifact-tool/artifact_tool.om ...
8
9note Binary Artifact v0 layout constants (ADR 0014)
10const AT_HEADER_SIZE ulong 128
11const AT_SEC_TABLE_OFF ulong 128
12const AT_SEC_COUNT ulong 2
13const AT_SEC_REC_SIZE ulong 32
14const AT_SEC_TABLE_SIZE ulong 64
15const AT_CAP_TABLE_OFF ulong 192
16const AT_CAP_REC_SIZE ulong 48
17const AT_MAX_CAPS ulong 16
18const AT_RES_ENV_SIZE ulong 48
19const AT_SIG_BLOCK_SIZE ulong 100
20const AT_SIG_ALG_ED25519 ulong 1
21const AT_TARGET_AARCH64_LE ulong 1
22const AT_ABI_V1 ulong 1
23const AT_PAGE_SIZE ulong 4096
24const AT_MAX_ARTIFACT_SIZE ulong 16777216
25const AT_MAX_PAYLOAD_SIZE ulong 8388608
26
27const AT_SEC_KIND_CODE ulong 1
28const AT_SEC_KIND_DATA ulong 2
29const AT_PERM_RX ulong 5
30const AT_PERM_RW ulong 3
31
32const AT_RES_KIND_CHANNEL ulong 2
33const AT_RES_KIND_OBJECT ulong 3
34const AT_VALID_RIGHTS ulong 63
35
36const AT_MAX_CODE_PAGES ulong 64
37const AT_MAX_DATA_PAGES ulong 64
38const AT_MAX_STACK_PAGES ulong 16
39const AT_MAX_IPC_MSGS ulong 1024
40const AT_MAX_IPC_BYTES ulong 1048576
41const AT_MAX_TICKS ulong 1000000000
42const AT_MAX_SYSCALLS ulong 65536
43const AT_U64_MAX ulong 0xffffffffffffffff
44
45note Stable fail-closed ArtifactError codes (ADR 0014 section 7.3)
46const AT_OK int 0
47const AT_ERR_BAD_MAGIC int 1
48const AT_ERR_UNSUP_VER int 2
49const AT_ERR_WRONG_TARGET int 3
50const AT_ERR_WRONG_ABI int 4
51const AT_ERR_LEN_OVERFLOW int 5
52const AT_ERR_TRUNCATED int 6
53const AT_ERR_WRONG_LEN int 7
54const AT_ERR_RESERVED_NZ int 8
55const AT_ERR_UNSUP_FLAGS int 9
56const AT_ERR_SEC_OVERLAP int 10
57const AT_ERR_BAD_SECTION int 11
58const AT_ERR_BAD_ENTRY int 12
59const AT_ERR_BAD_CAP int 13
60const AT_ERR_RES_LIMIT int 14
61const AT_ERR_BAD_SIG_FMT int 15
62const AT_ERR_DIGEST_MISMATCH int 16
63const AT_ERR_UNTRUSTED int 17
64const AT_ERR_BAD_SIG int 18
65const AT_ERR_RIGHTS_ESC int 19
66const AT_ERR_BAD_RECEIPT int 20
67
68note Tool-level negative error codes
69const AT_ERR_USAGE int -1
70const AT_ERR_BAD_HEX int -2
71const AT_ERR_BUF_SMALL int -3
72const AT_ERR_RCP_MUST_UNSIGNED int -4
73const AT_ERR_RCP_ADMIT_BAD_ART int -5
74const AT_ERR_RCP_NZ_BAD_ART int -6
75const AT_ERR_RCP_BIND_MISMATCH int -7
76const AT_ERR_RCP_NO_GRANTS int -8
77const AT_ERR_PROD_SIGN_OFF int -9
78const AT_ERR_PACK_BOUNDS int -10
79const AT_ERR_BAD_CASE int -11
80
81const AT_EXIT_TOOL_ERR int 2
82
83note Admission Receipt v0 constants (ADR 0014 section 7)
84const AT_RCP_SIZE ulong 512
85const AT_RCP_HDR_SIZE ulong 96
86const AT_RCP_SIG_OFF ulong 400
87const AT_RCP_VERSION ulong 0
88const AT_RCP_FLAG_MACHINE_ID ulong 1
89const AT_RCP_FLAG_CONTEXT_ID ulong 2
90const AT_RCP_FLAG_TIMESTAMP ulong 4
91const AT_RCP_VALID_FLAGS ulong 7
92
93const AT_RCP_RES_READ_OK ulong 1
94const AT_RCP_RES_WRITE_DENIED ulong 2
95const AT_RCP_RES_RECLAIMED ulong 4
96const AT_RCP_RES_CANARY_OK ulong 8
97const AT_RCP_RES_FORGED_DENIED ulong 16
98const AT_RCP_RES_MAPPED_MATCH ulong 32
99const AT_RCP_RES_EXEC_MATCH ulong 64
100const AT_RCP_RES_WX_SEALED ulong 128
101const AT_RCP_VALID_RES_FLAGS ulong 255
102
103const AT_DEC_ADMITTED ulong 1
104const AT_DEC_REJECTED ulong 2
105const AT_DEC_CANARY_FAILED ulong 3
106const AT_DEC_DESTROYED ulong 4
107
108const AT_TIER_QEMU ulong 1
109const AT_TIER_MACHINE1 ulong 2
110const AT_TIER_PI5 ulong 3
111
112const AT_EXEC_NOT_RUN ulong 0
113const AT_EXEC_EXITED ulong 1
114const AT_EXEC_TIMEOUT ulong 2
115const AT_EXEC_FAULT ulong 3
116const AT_EXEC_BAD_SYSCALL ulong 4
117const AT_EXEC_RES_OVERRUN ulong 5
118const AT_EXEC_CANARY_FAILED ulong 6
119
120const AT_MAX_REJ_STAGE ulong 9
121const AT_LOADER_REASON_FIRST ulong 257
122const AT_LOADER_REASON_LAST ulong 266
123
124note CLI command dispatch IDs
125const AT_CMD_INVALID int 0
126const AT_CMD_PACK int 1
127const AT_CMD_INSPECT int 2
128const AT_CMD_ID int 3
129const AT_CMD_SIGN int 4
130const AT_CMD_VERIFY int 5
131const AT_CMD_NEG_CORPUS int 6
132const AT_CMD_RCP_INSPECT int 10
133const AT_CMD_RCP_CHECK int 11
134const AT_CMD_RCP_FROM_HEX int 12
135const AT_CMD_RCP_SIGN_TEST int 13
136const AT_CMD_RCP_VERIFY int 14
137
138note Negative corpus constants (P2-7 H01..H29)
139const AT_CORPUS_COUNT ulong 29
140const AT_EXP_REJ_VERIFIED ulong 1
141const AT_EXP_REJ_AUTHORIZED ulong 2
142const AT_EXP_ADMITTED ulong 3
143
144const AT_CORPUS_EXEC_NONE ulong 0
145const AT_CORPUS_EXEC_EXIT0 ulong 1
146const AT_CORPUS_EXEC_NX ulong 2
147const AT_CORPUS_EXEC_BAD_SYS99 ulong 3
148const AT_CORPUS_EXEC_OVERRUN ulong 4
149
150note AArch64 instruction words for negative corpus payloads (corpus.rs)
151const AT_INSN_MOV_X0_0 ulong 0xd2800000
152const AT_INSN_MOV_X8_2 ulong 0xd2800048
153const AT_INSN_MOV_X8_6 ulong 0xd28000c8
154const AT_INSN_MOV_X8_99 ulong 0xd2800c68
155const AT_INSN_SVC_0 ulong 0xd4000001
156const AT_INSN_B_SELF ulong 0x14000000
157const AT_INSN_BR_X2 ulong 0xd61f0040
158const AT_INSN_SUB_X9_SP_16 ulong 0xd10043e9
159const AT_INSN_BR_X9 ulong 0xd61f0120
160
161note ASCII prefixes (trailing NUL byte appended explicitly where required)
162text at_art_magic "AIENART"
163text at_rcp_magic "AIENRCP"
164text at_dom_art_id "AIENOS-ARTIFACT-V1"
165text at_dom_art_sig "AIENOS-ARTIFACT-SIGNATURE-V1"
166text at_dom_req_caps "AIENOS-ARTIFACT-CAP-REQUESTS-V1"
167text at_dom_res_env "AIENOS-ARTIFACT-RESOURCES-V1"
168text at_dom_rcp_dig "AIENOS-ADMISSION-RECEIPT-V1"
169text at_dom_rcp_nonce "AIENOS-ADMISSION-RECEIPT-NONCE-V1"
170text at_dom_rcp_sig "AIENOS-ADMISSION-RECEIPT-SIGNATURE-V1"
171text at_hex_chars "0123456789abcdef"
172
173note CLI command keyword tables
174text at_kw_pack "pack"
175text at_kw_inspect "inspect"
176text at_kw_id "id"
177text at_kw_sign "sign"
178text at_kw_verify "verify"
179text at_kw_receipt "receipt"
180text at_kw_neg_corpus "negative-corpus"
181text at_kw_check "check"
182text at_kw_from_hex "from-hex"
183text at_kw_sign_test "sign-test"
184
185note Corpus static data tags
186text at_tag_baseline "BASELINE"
187text at_tag_largest "LARGEST!"
188text at_tag_datapage "DATAPAGE"
189text at_tag_stackpag "STACKPAG"
190text at_tag_badsyscl "BADSYSCL"
191text at_tag_overrun "OVERRUN!"
192text at_tag_ipcwants "IPCWANTS"
193text at_tag_writereq "WRITEREQ"
194text at_tag_rwrequst "RWREQUST"
195
196note Manifest staging globals for at_pack
197global at_man_entry ulong 1
198global at_man_cap_cnt ulong 1
199note 16 caps x 8 ulongs: kind, id, rights, bounds_kind, max_ops, max_bytes, byte_off, byte_len
200global at_man_caps ulong 128
201note 9 resource fields: code_p, data_p, stack_p, max_caps, ipc_m, ipc_b, cpu_t, elap_t, sys_c
202global at_man_res ulong 9
203global at_pack_len ulong 1
204
205note Parsed artifact view globals (populated by at_parse / at_parse_and_identify)
206note 0=total_len, 1=entry_off, 2=cap_cnt, 3=res_off, 4=pay_off, 5=pay_len, 6=sig_off,
207note 7=code_len, 8=data_off, 9=data_len, 10=data_mem_len, 11=sig_alg
208global at_art_meta ulong 12
209global at_art_id uchar 32
210global at_art_payload_sha uchar 32
211global at_art_signer_fp uchar 32
212global at_art_req_caps_sha uchar 32
213global at_art_res_env_sha uchar 32
214
215note Staged / decoded Admission Receipt v0 globals
216note 0=flags, 1=decision, 2=tier, 3=seq, 4=time_ns, 5=exec_st, 6=exit_st, 7=res_flags,
217note 8=rej_stage, 9=rej_reason, 10=syscalls, 11=reads_ok, 12=denials, 13=frames,
218note 14=generation, 15=context_id
219global at_rcp_f ulong 16
220global at_rcp_nonce_buf uchar 16
221note 9 digests x 32 bytes = 288 bytes:
222note 0=artifact_id, 1=payload, 2=signer, 3=policy, 4=req_caps, 5=grant_caps,
223note 6=res_env, 7=verifier_id, 8=machine_id
224global at_rcp_digests uchar 288
225global at_rcp_dig_out uchar 32
226global at_rcp_signed_flag ulong 1
227
228note Corpus build and expectation outputs
229global at_corpus_len ulong 1
230note 0=expect_kind, 1=expected_code, 2=expected_exec, 3=expected_caps
231global at_corpus_exp ulong 4
232
233note Little-endian integer helpers
234fn at_put_u16_le buf edit uchar * off ulong v ulong to void
235put buf off uchar and v 0xff
236put buf add off 1 uchar and shr v 8 0xff
237end
238
239fn at_put_u32_le buf edit uchar * off ulong v ulong to void
240put buf off uchar and v 0xff
241put buf add off 1 uchar and shr v 8 0xff
242put buf add off 2 uchar and shr v 16 0xff
243put buf add off 3 uchar and shr v 24 0xff
244end
245
246fn at_put_u64_le buf edit uchar * off ulong v ulong to void
247for i ulong 8
248put buf add off i uchar and shr v mul i 8 0xff
249end
250end
251
252fn at_get_u16_le buf view uchar * off ulong to ulong
253b0 = ulong get buf off
254b1 = ulong get buf add off 1
255ret or b0 shl b1 8
256end
257
258fn at_get_u32_le buf view uchar * off ulong to ulong
259b0 = ulong get buf off
260b1 = ulong get buf add off 1
261b2 = ulong get buf add off 2
262b3 = ulong get buf add off 3
263ret or or b0 shl b1 8 or shl b2 16 shl b3 24
264end
265
266fn at_get_u64_le buf view uchar * off ulong to ulong
267acc = ulong 0
268for i ulong 8
269b = ulong get buf add off i
270acc = or acc shl b mul i 8
271end
272ret acc
273end
274
275fn at_align_up_16 v ulong to ulong
276ret and add v 15 not ulong 15
277end
278
279fn at_bytes_zero buf view uchar * off ulong cnt ulong to bool
280for i ulong cnt bound 4096
281if ne get buf add off i 0
282ret false
283end
284end
285ret true
286end
287
288fn at_bytes_eq a view uchar * b view uchar * to bool
289if ne len a len b
290ret false
291end
292for i ulong len a bound 4096
293if ne get a i get b i
294ret false
295end
296end
297ret true
298end
299
300note Lowercase hex encoding (2 chars per byte)
301fn at_hex_encode src view uchar * dst edit uchar * to int
302n = len src
303if lt len dst mul n 2
304ret AT_ERR_BUF_SMALL
305end
306for i ulong n bound 1024
307b = ulong get src i
308hi = shr b 4
309lo = and b 0x0f
310put dst mul i 2 get at_hex_chars hi
311put dst add mul i 2 1 get at_hex_chars lo
312end
313ret AT_OK
314end
315
316fn at_hex_nibble ch uchar to int
317v = ulong ch
318if ge v 48
319if le v 57
320ret int sub v 48
321end
322end
323if ge v 97
324if le v 102
325ret int add sub v 97 10
326end
327end
328ret -1
329end
330
331fn at_is_ws ch uchar to bool
332if eq ch 32
333ret true
334end
335if eq ch 9
336ret true
337end
338if eq ch 10
339ret true
340end
341if eq ch 13
342ret true
343end
344ret false
345end
346
347fn at_rcp_decode_hex_body hex_in view uchar * start ulong dst edit uchar * to int
348for i ulong AT_RCP_SIZE
349pos = add start mul i 2
350hi = at_hex_nibble get hex_in pos
351lo = at_hex_nibble get hex_in add pos 1
352if lt hi 0
353ret AT_ERR_BAD_HEX
354end
355if lt lo 0
356ret AT_ERR_BAD_HEX
357end
358b = or shl ulong hi 4 ulong lo
359put dst i uchar b
360end
361ret AT_OK
362end
363
364note Strict lowercase hex decoder (matches receipt_cmd.rs::from_hex)
365fn at_rcp_from_hex hex_in view uchar * dst edit uchar * to int
366if lt len dst AT_RCP_SIZE
367ret AT_ERR_BUF_SMALL
368end
369n = len hex_in
370start = ulong 0
371while lt start n bound 4096
372if not at_is_ws get hex_in start
373break
374end
375start = add start 1
376end
377endp = n
378while gt endp start bound 4096
379if not at_is_ws get hex_in sub endp 1
380break
381end
382endp = sub endp 1
383end
384if ne sub endp start mul AT_RCP_SIZE 2
385ret AT_ERR_BAD_HEX
386end
387ret at_rcp_decode_hex_body hex_in start dst
388end
389
390note Domain-separated SHA-256 helper: SHA256(dom || 0x00 || payload)
391fn at_sha256_dom_nul dom view uchar * msg view uchar * dst edit uchar * to void
392ctx = new ulong 40 0
393z = new uchar 1 0
394sha256_init ctx
395sha256_update ctx dom
396sha256_update ctx z
397sha256_update ctx msg
398sha256_final ctx dst
399end
400
401note RFC 8032 TEST 1 seed (SEED-0B test-only artifact identity; never a production secret)
402fn at_fill_test_art_key dst edit uchar * to void
403put dst 0 0x9d
404put dst 1 0x61
405put dst 2 0xb1
406put dst 3 0x9d
407put dst 4 0xef
408put dst 5 0xfd
409put dst 6 0x5a
410put dst 7 0x60
411put dst 8 0xba
412put dst 9 0x84
413put dst 10 0x4a
414put dst 11 0xf4
415put dst 12 0x92
416put dst 13 0xec
417put dst 14 0x2c
418put dst 15 0xc4
419put dst 16 0x44
420put dst 17 0x49
421put dst 18 0xc5
422put dst 19 0x69
423put dst 20 0x7b
424put dst 21 0x32
425put dst 22 0x69
426put dst 23 0x19
427put dst 24 0x70
428put dst 25 0x3b
429put dst 26 0xac
430put dst 27 0x03
431put dst 28 0x1c
432put dst 29 0xae
433put dst 30 0x7f
434put dst 31 0x60
435end
436
437note RFC 8032 TEST 3 seed (untrusted signer for corpus H15)
438fn at_fill_untrusted_key dst edit uchar * to void
439put dst 0 0xc5
440put dst 1 0xaa
441put dst 2 0x8d
442put dst 3 0xf4
443put dst 4 0x3f
444put dst 5 0x9f
445put dst 6 0x83
446put dst 7 0x7b
447put dst 8 0xed
448put dst 9 0xb7
449put dst 10 0x44
450put dst 11 0x2f
451put dst 12 0x31
452put dst 13 0xdc
453put dst 14 0xb7
454put dst 15 0xb1
455put dst 16 0x66
456put dst 17 0xd3
457put dst 18 0x85
458put dst 19 0x35
459put dst 20 0x07
460put dst 21 0x6f
461put dst 22 0x09
462put dst 23 0x4b
463put dst 24 0x85
464put dst 25 0xce
465put dst 26 0x3a
466put dst 27 0x2e
467put dst 28 0x0b
468put dst 29 0x44
469put dst 30 0x58
470put dst 31 0xf7
471end
472
473note RFC 8032 TEST 2 seed (SEED-0B test-only receipt identity; distinct from artifact key)
474fn at_fill_test_rcp_key dst edit uchar * to void
475put dst 0 0x4c
476put dst 1 0xcd
477put dst 2 0x08
478put dst 3 0x9b
479put dst 4 0x28
480put dst 5 0xff
481put dst 6 0x96
482put dst 7 0xda
483put dst 8 0x9d
484put dst 9 0xb6
485put dst 10 0xc3
486put dst 11 0x46
487put dst 12 0xec
488put dst 13 0x11
489put dst 14 0x4e
490put dst 15 0x0f
491put dst 16 0x5b
492put dst 17 0x8a
493put dst 18 0x31
494put dst 19 0x9f
495put dst 20 0x35
496put dst 21 0xab
497put dst 22 0xa6
498put dst 23 0x24
499put dst 24 0xda
500put dst 25 0x8c
501put dst 26 0xf6
502put dst 27 0xed
503put dst 28 0x4f
504put dst 29 0xb8
505put dst 30 0xa6
506put dst 31 0xfb
507end
508
509note Signer fingerprint: SHA256(key[32])
510fn at_signer_fp key view uchar * fp_dst edit uchar * to void
511ctx = new ulong 40 0
512sha256_init ctx
513sha256_update ctx key
514sha256_final ctx fp_dst
515end
516
517note Deterministic 64-byte keyed SHA-256 signature over (dom || 0x00 || dig[32])
518fn at_compute_sig key view uchar 32 dom view uchar * dig view uchar 32 sig_dst edit uchar 64 to void
519ctx = new ulong 40 0
520z = new uchar 1 0
521tag = new uchar 1 1
522half = new uchar 32 0
523sha256_init ctx
524sha256_update ctx key
525sha256_update ctx dom
526sha256_update ctx z
527sha256_update ctx dig
528sha256_update ctx tag
529sha256_final ctx half
530for i ulong 32
531put sig_dst i get half i
532end
533put tag 0 2
534sha256_init ctx
535sha256_update ctx key
536sha256_update ctx dom
537sha256_update ctx z
538sha256_update ctx dig
539sha256_update ctx tag
540sha256_final ctx half
541for i ulong 32
542put sig_dst add 32 i get half i
543end
544end
545
546note Manifest staging helpers
547fn at_manifest_reset to void
548put at_man_entry 0 0
549put at_man_cap_cnt 0 0
550for i ulong 9
551put at_man_res i 0
552end
553end
554
555fn at_manifest_set_entry entry ulong to void
556put at_man_entry 0 entry
557end
558
559fn at_manifest_set_res_pages code_p ulong data_p ulong stack_p ulong max_caps ulong to void
560put at_man_res 0 code_p
561put at_man_res 1 data_p
562put at_man_res 2 stack_p
563put at_man_res 3 max_caps
564end
565
566fn at_manifest_set_res_limits ipc_m ulong ipc_b ulong cpu_t ulong elap_t ulong sys_c ulong to void
567put at_man_res 4 ipc_m
568put at_man_res 5 ipc_b
569put at_man_res 6 cpu_t
570put at_man_res 7 elap_t
571put at_man_res 8 sys_c
572end
573
574fn at_manifest_push_cap kind ulong rid ulong rights ulong bkind ulong max_ops ulong to int
575cnt = get at_man_cap_cnt 0
576if ge cnt AT_MAX_CAPS
577ret -1
578end
579base = mul cnt 8
580put at_man_caps base kind
581put at_man_caps add base 1 rid
582put at_man_caps add base 2 rights
583put at_man_caps add base 3 bkind
584put at_man_caps add base 4 max_ops
585put at_man_caps add base 5 0
586put at_man_caps add base 6 0
587put at_man_caps add base 7 0
588put at_man_cap_cnt 0 add cnt 1
589ret int cnt
590end
591
592fn at_manifest_set_cap_bounds idx ulong max_b ulong boff ulong blen ulong to void
593base = mul idx 8
594put at_man_caps add base 5 max_b
595put at_man_caps add base 6 boff
596put at_man_caps add base 7 blen
597end
598
599note Section and resource envelope encoders
600fn at_encode_section dst edit uchar * off ulong kind ulong perms ulong pay_rel ulong to void
601at_put_u16_le dst off kind
602at_put_u16_le dst add off 2 perms
603at_put_u32_le dst add off 8 pay_rel
604at_put_u32_le dst add off 20 AT_PAGE_SIZE
605end
606
607fn at_encode_resources dst edit uchar * off ulong to void
608at_put_u32_le dst off get at_man_res 0
609at_put_u32_le dst add off 4 get at_man_res 1
610at_put_u32_le dst add off 8 get at_man_res 2
611at_put_u16_le dst add off 12 get at_man_res 3
612at_put_u32_le dst add off 16 get at_man_res 4
613at_put_u32_le dst add off 20 get at_man_res 5
614at_put_u64_le dst add off 24 get at_man_res 6
615at_put_u64_le dst add off 32 get at_man_res 7
616at_put_u32_le dst add off 40 get at_man_res 8
617end
618
619fn at_encode_staged_caps dst edit uchar * cnt ulong to void
620for i ulong cnt bound 16
621base = mul i 8
622off = add AT_CAP_TABLE_OFF mul i AT_CAP_REC_SIZE
623at_put_u16_le dst off get at_man_caps base
624at_put_u32_le dst add off 4 get at_man_caps add base 1
625at_put_u32_le dst add off 8 get at_man_caps add base 2
626at_put_u32_le dst add off 12 get at_man_caps add base 3
627at_put_u32_le dst add off 16 get at_man_caps add base 4
628at_put_u64_le dst add off 24 get at_man_caps add base 5
629at_put_u64_le dst add off 32 get at_man_caps add base 6
630at_put_u64_le dst add off 40 get at_man_caps add base 7
631end
632end
633
634note Binary Artifact v0 strict parser (format.rs, capability.rs, resource.rs)
635fn at_parse_magic bytes view uchar * to bool
636for i ulong 7
637if ne get bytes i get at_art_magic i
638ret false
639end
640end
641if ne get bytes 7 0
642ret false
643end
644ret true
645end
646
647fn at_parse_header bytes view uchar * to int
648n = len bytes
649if lt n AT_HEADER_SIZE
650ret AT_ERR_TRUNCATED
651end
652if gt n AT_MAX_ARTIFACT_SIZE
653ret AT_ERR_RES_LIMIT
654end
655if not at_parse_magic bytes
656ret AT_ERR_BAD_MAGIC
657end
658if ne at_get_u16_le bytes 8 0
659ret AT_ERR_UNSUP_VER
660end
661if ne at_get_u16_le bytes 10 AT_HEADER_SIZE
662ret AT_ERR_UNSUP_VER
663end
664if ne at_get_u16_le bytes 12 AT_TARGET_AARCH64_LE
665ret AT_ERR_WRONG_TARGET
666end
667if ne at_get_u16_le bytes 14 AT_ABI_V1
668ret AT_ERR_WRONG_ABI
669end
670if ne at_get_u32_le bytes 16 0
671ret AT_ERR_UNSUP_FLAGS
672end
673total_len = at_get_u32_le bytes 20
674if ne total_len n
675ret AT_ERR_WRONG_LEN
676end
677put at_art_meta 0 total_len
678ret AT_OK
679end
680
681fn at_parse_header_tables bytes view uchar * to int
682if ne at_get_u32_le bytes 24 AT_SEC_TABLE_OFF
683ret AT_ERR_UNSUP_VER
684end
685if ne at_get_u16_le bytes 28 AT_SEC_COUNT
686ret AT_ERR_UNSUP_VER
687end
688if ne at_get_u16_le bytes 30 AT_SEC_REC_SIZE
689ret AT_ERR_UNSUP_VER
690end
691if ne at_get_u16_le bytes 32 0
692ret AT_ERR_UNSUP_VER
693end
694if ne at_get_u16_le bytes 34 0
695ret AT_ERR_UNSUP_VER
696end
697if ne at_get_u32_le bytes 40 AT_CAP_TABLE_OFF
698ret AT_ERR_UNSUP_VER
699end
700if ne at_get_u16_le bytes 46 AT_CAP_REC_SIZE
701ret AT_ERR_UNSUP_VER
702end
703if ne at_get_u16_le bytes 52 AT_RES_ENV_SIZE
704ret AT_ERR_UNSUP_VER
705end
706if ne at_get_u16_le bytes 54 0
707ret AT_ERR_UNSUP_VER
708end
709if not at_bytes_zero bytes 72 56
710ret AT_ERR_RESERVED_NZ
711end
712put at_art_meta 1 at_get_u32_le bytes 36
713ret AT_OK
714end
715
716fn at_parse_offsets_sig bytes view uchar * to int
717total_len = get at_art_meta 0
718pay_off = get at_art_meta 4
719pay_len = at_get_u32_le bytes 60
720if gt pay_len AT_MAX_PAYLOAD_SIZE
721ret AT_ERR_RES_LIMIT
722end
723sig_off = at_get_u32_le bytes 64
724sig_len = at_get_u16_le bytes 68
725sig_alg = at_get_u16_le bytes 70
726if ne sig_len AT_SIG_BLOCK_SIZE
727ret AT_ERR_BAD_SIG_FMT
728end
729if ne sig_alg AT_SIG_ALG_ED25519
730ret AT_ERR_BAD_SIG_FMT
731end
732if ne sig_off add pay_off pay_len
733ret AT_ERR_SEC_OVERLAP
734end
735if ne add sig_off AT_SIG_BLOCK_SIZE total_len
736ret AT_ERR_WRONG_LEN
737end
738put at_art_meta 5 pay_len
739put at_art_meta 6 sig_off
740put at_art_meta 11 sig_alg
741ret AT_OK
742end
743
744fn at_parse_offsets bytes view uchar * to int
745total_len = get at_art_meta 0
746cap_cnt = at_get_u16_le bytes 44
747if gt cap_cnt AT_MAX_CAPS
748ret AT_ERR_RES_LIMIT
749end
750cap_end = add AT_CAP_TABLE_OFF mul cap_cnt AT_CAP_REC_SIZE
751res_off = at_get_u32_le bytes 48
752if ne res_off cap_end
753ret AT_ERR_SEC_OVERLAP
754end
755res_end = add res_off AT_RES_ENV_SIZE
756exp_pay = at_align_up_16 res_end
757pay_off = at_get_u32_le bytes 56
758if ne pay_off exp_pay
759ret AT_ERR_SEC_OVERLAP
760end
761if gt pay_off total_len
762ret AT_ERR_TRUNCATED
763end
764if not at_bytes_zero bytes res_end sub pay_off res_end
765ret AT_ERR_RESERVED_NZ
766end
767put at_art_meta 2 cap_cnt
768put at_art_meta 3 res_off
769put at_art_meta 4 pay_off
770ret at_parse_offsets_sig bytes
771end
772
773fn at_parse_sections bytes view uchar * to int
774pay_len = get at_art_meta 5
775if ne at_get_u32_le bytes 132 0
776ret AT_ERR_RESERVED_NZ
777end
778if not at_bytes_zero bytes 152 8
779ret AT_ERR_RESERVED_NZ
780end
781if ne at_get_u32_le bytes 164 0
782ret AT_ERR_RESERVED_NZ
783end
784if not at_bytes_zero bytes 184 8
785ret AT_ERR_RESERVED_NZ
786end
787c_flen = at_get_u32_le bytes 140
788c_mlen = at_get_u32_le bytes 144
789if ne at_get_u16_le bytes 128 AT_SEC_KIND_CODE
790ret AT_ERR_BAD_SECTION
791end
792if ne at_get_u16_le bytes 130 AT_PERM_RX
793ret AT_ERR_BAD_SECTION
794end
795if ne at_get_u32_le bytes 136 0
796ret AT_ERR_BAD_SECTION
797end
798if eq c_flen 0
799ret AT_ERR_BAD_SECTION
800end
801if ne c_mlen c_flen
802ret AT_ERR_BAD_SECTION
803end
804if ne at_get_u32_le bytes 148 AT_PAGE_SIZE
805ret AT_ERR_BAD_SECTION
806end
807d_rel = at_get_u32_le bytes 168
808d_flen = at_get_u32_le bytes 172
809d_mlen = at_get_u32_le bytes 176
810if ne at_get_u16_le bytes 160 AT_SEC_KIND_DATA
811ret AT_ERR_BAD_SECTION
812end
813if ne at_get_u16_le bytes 162 AT_PERM_RW
814ret AT_ERR_BAD_SECTION
815end
816if ne d_rel c_flen
817ret AT_ERR_BAD_SECTION
818end
819if eq d_flen 0
820ret AT_ERR_BAD_SECTION
821end
822if lt d_mlen d_flen
823ret AT_ERR_BAD_SECTION
824end
825if ne at_get_u32_le bytes 180 AT_PAGE_SIZE
826ret AT_ERR_BAD_SECTION
827end
828if ne add c_flen d_flen pay_len
829ret AT_ERR_SEC_OVERLAP
830end
831put at_art_meta 7 c_flen
832put at_art_meta 8 d_rel
833put at_art_meta 9 d_flen
834put at_art_meta 10 d_mlen
835ret AT_OK
836end
837
838fn at_validate_cap_bounds bytes view uchar * off ulong kind ulong bkind ulong to int
839max_b = at_get_u64_le bytes add off 24
840boff = at_get_u64_le bytes add off 32
841blen = at_get_u64_le bytes add off 40
842if eq max_b 0
843ret AT_ERR_BAD_CAP
844end
845if eq kind AT_RES_KIND_OBJECT
846if ne bkind 1
847ret AT_ERR_BAD_CAP
848end
849if gt blen sub AT_U64_MAX boff
850ret AT_ERR_LEN_OVERFLOW
851end
852if eq blen 0
853ret AT_ERR_BAD_CAP
854end
855if gt max_b blen
856ret AT_ERR_BAD_CAP
857end
858ret AT_OK
859end
860if ne bkind 2
861ret AT_ERR_BAD_CAP
862end
863if ne boff 0
864ret AT_ERR_BAD_CAP
865end
866if ne blen 0
867ret AT_ERR_BAD_CAP
868end
869ret AT_OK
870end
871
872fn at_validate_one_cap bytes view uchar * off ulong to int
873if ne at_get_u16_le bytes add off 2 0
874ret AT_ERR_RESERVED_NZ
875end
876if ne at_get_u32_le bytes add off 20 0
877ret AT_ERR_RESERVED_NZ
878end
879kind = at_get_u16_le bytes off
880rid = at_get_u32_le bytes add off 4
881rights = at_get_u32_le bytes add off 8
882bkind = at_get_u32_le bytes add off 12
883max_ops = at_get_u32_le bytes add off 16
884if ne kind AT_RES_KIND_OBJECT
885if ne kind AT_RES_KIND_CHANNEL
886ret AT_ERR_BAD_CAP
887end
888end
889if eq rid 0
890ret AT_ERR_BAD_CAP
891end
892if eq rights 0
893ret AT_ERR_BAD_CAP
894end
895if ne and rights not AT_VALID_RIGHTS 0
896ret AT_ERR_BAD_CAP
897end
898if eq max_ops 0
899ret AT_ERR_BAD_CAP
900end
901ret at_validate_cap_bounds bytes off kind bkind
902end
903
904fn at_parse_caps bytes view uchar * to int
905cap_cnt = get at_art_meta 2
906has_prev = false
907prev_k = ulong 0
908prev_id = ulong 0
909for i ulong cap_cnt bound 16
910off = add AT_CAP_TABLE_OFF mul i AT_CAP_REC_SIZE
911rc = at_validate_one_cap bytes off
912if ne rc AT_OK
913ret rc
914end
915k = at_get_u16_le bytes off
916rid = at_get_u32_le bytes add off 4
917if has_prev
918if gt prev_k k
919ret AT_ERR_BAD_CAP
920end
921if eq prev_k k
922if ge prev_id rid
923ret AT_ERR_BAD_CAP
924end
925end
926end
927has_prev = true
928prev_k = k
929prev_id = rid
930end
931ret AT_OK
932end
933
934fn at_parse_res_limits bytes view uchar * res_off ulong to int
935if gt at_get_u32_le bytes add res_off 16 AT_MAX_IPC_MSGS
936ret AT_ERR_RES_LIMIT
937end
938if gt at_get_u32_le bytes add res_off 20 AT_MAX_IPC_BYTES
939ret AT_ERR_RES_LIMIT
940end
941cpu_t = at_get_u64_le bytes add res_off 24
942elap_t = at_get_u64_le bytes add res_off 32
943sys_c = at_get_u32_le bytes add res_off 40
944if eq cpu_t 0
945ret AT_ERR_RES_LIMIT
946end
947if gt cpu_t AT_MAX_TICKS
948ret AT_ERR_RES_LIMIT
949end
950if eq elap_t 0
951ret AT_ERR_RES_LIMIT
952end
953if gt elap_t AT_MAX_TICKS
954ret AT_ERR_RES_LIMIT
955end
956if eq sys_c 0
957ret AT_ERR_RES_LIMIT
958end
959if gt sys_c AT_MAX_SYSCALLS
960ret AT_ERR_RES_LIMIT
961end
962ret AT_OK
963end
964
965fn at_parse_resources bytes view uchar * to int
966res_off = get at_art_meta 3
967cap_cnt = get at_art_meta 2
968if ne at_get_u16_le bytes add res_off 14 0
969ret AT_ERR_RESERVED_NZ
970end
971if ne at_get_u32_le bytes add res_off 44 0
972ret AT_ERR_RESERVED_NZ
973end
974code_p = at_get_u32_le bytes res_off
975data_p = at_get_u32_le bytes add res_off 4
976stack_p = at_get_u32_le bytes add res_off 8
977max_c = at_get_u16_le bytes add res_off 12
978if eq code_p 0
979ret AT_ERR_RES_LIMIT
980end
981if gt code_p AT_MAX_CODE_PAGES
982ret AT_ERR_RES_LIMIT
983end
984if eq data_p 0
985ret AT_ERR_RES_LIMIT
986end
987if gt data_p AT_MAX_DATA_PAGES
988ret AT_ERR_RES_LIMIT
989end
990if eq stack_p 0
991ret AT_ERR_RES_LIMIT
992end
993if gt stack_p AT_MAX_STACK_PAGES
994ret AT_ERR_RES_LIMIT
995end
996if lt max_c cap_cnt
997ret AT_ERR_RES_LIMIT
998end
999if gt max_c AT_MAX_CAPS
1000ret AT_ERR_RES_LIMIT
1001end
1002if gt get at_art_meta 7 mul code_p AT_PAGE_SIZE
1003ret AT_ERR_RES_LIMIT
1004end
1005if gt get at_art_meta 10 mul data_p AT_PAGE_SIZE
1006ret AT_ERR_RES_LIMIT
1007end
1008ret at_parse_res_limits bytes res_off
1009end
1010
1011fn at_parse_entry_and_sig bytes view uchar * to int
1012entry_off = get at_art_meta 1
1013code_len = get at_art_meta 7
1014if ne and entry_off 3 0
1015ret AT_ERR_BAD_ENTRY
1016end
1017if gt add entry_off 4 code_len
1018ret AT_ERR_BAD_ENTRY
1019end
1020sig_off = get at_art_meta 6
1021if ne at_get_u16_le bytes sig_off get at_art_meta 11
1022ret AT_ERR_BAD_SIG_FMT
1023end
1024if ne get bytes add sig_off 2 0
1025ret AT_ERR_RESERVED_NZ
1026end
1027if ne get bytes add sig_off 3 0
1028ret AT_ERR_RESERVED_NZ
1029end
1030ret AT_OK
1031end
1032
1033fn at_parse bytes view uchar * to int
1034rc = at_parse_header bytes
1035if ne rc AT_OK
1036ret rc
1037end
1038rc = at_parse_header_tables bytes
1039if ne rc AT_OK
1040ret rc
1041end
1042rc = at_parse_offsets bytes
1043if ne rc AT_OK
1044ret rc
1045end
1046rc = at_parse_sections bytes
1047if ne rc AT_OK
1048ret rc
1049end
1050rc = at_parse_caps bytes
1051if ne rc AT_OK
1052ret rc
1053end
1054rc = at_parse_resources bytes
1055if ne rc AT_OK
1056ret rc
1057end
1058ret at_parse_entry_and_sig bytes
1059end
1060
1061note Parse artifact and compute all canonical identity digests
1062fn at_parse_and_identify bytes view uchar * to int
1063rc = at_parse bytes
1064if ne rc AT_OK
1065ret rc
1066end
1067sig_off = get at_art_meta 6
1068pay_off = get at_art_meta 4
1069pay_len = get at_art_meta 5
1070cap_len = mul get at_art_meta 2 AT_CAP_REC_SIZE
1071res_off = get at_art_meta 3
1072dig = new uchar 32 0
1073at_sha256_dom_nul at_dom_art_id part bytes 0 sig_off dig
1074for i ulong 32
1075put at_art_id i get dig i
1076end
1077ctx = new ulong 40 0
1078sha256_init ctx
1079sha256_update ctx part bytes pay_off pay_len
1080sha256_final ctx dig
1081for i ulong 32
1082put at_art_payload_sha i get dig i
1083end
1084at_sha256_dom_nul at_dom_req_caps part bytes AT_CAP_TABLE_OFF cap_len dig
1085for i ulong 32
1086put at_art_req_caps_sha i get dig i
1087end
1088at_sha256_dom_nul at_dom_res_env part bytes res_off AT_RES_ENV_SIZE dig
1089for i ulong 32
1090put at_art_res_env_sha i get dig i
1091end
1092for i ulong 32
1093put at_art_signer_fp i get bytes add add sig_off 4 i
1094end
1095ret AT_OK
1096end
1097
1098note Pack staged manifest + code + data into canonical Binary Artifact v0
1099fn at_pack_compute_layout clen ulong dlen ulong dst_len ulong to int
1100cap_cnt = get at_man_cap_cnt 0
1101if eq clen 0
1102ret AT_ERR_PACK_BOUNDS
1103end
1104if eq dlen 0
1105ret AT_ERR_PACK_BOUNDS
1106end
1107if gt cap_cnt AT_MAX_CAPS
1108ret AT_ERR_PACK_BOUNDS
1109end
1110res_off = add AT_CAP_TABLE_OFF mul cap_cnt AT_CAP_REC_SIZE
1111pay_off = at_align_up_16 add res_off AT_RES_ENV_SIZE
1112pay_len = add clen dlen
1113sig_off = add pay_off pay_len
1114total_len = add sig_off AT_SIG_BLOCK_SIZE
1115if gt total_len AT_MAX_ARTIFACT_SIZE
1116ret AT_ERR_RES_LIMIT
1117end
1118if gt pay_len AT_MAX_PAYLOAD_SIZE
1119ret AT_ERR_RES_LIMIT
1120end
1121if gt total_len dst_len
1122ret AT_ERR_BUF_SMALL
1123end
1124put at_art_meta 0 total_len
1125put at_art_meta 2 cap_cnt
1126put at_art_meta 3 res_off
1127put at_art_meta 4 pay_off
1128put at_art_meta 5 pay_len
1129put at_art_meta 6 sig_off
1130ret AT_OK
1131end
1132
1133fn at_pack_write_tables dst edit uchar * clen ulong dlen ulong to void
1134total_len = get at_art_meta 0
1135cap_cnt = get at_art_meta 2
1136res_off = get at_art_meta 3
1137for i ulong total_len bound 4096
1138put dst i 0
1139end
1140for i ulong 7
1141put dst i get at_art_magic i
1142end
1143at_put_u16_le dst 10 AT_HEADER_SIZE
1144at_put_u16_le dst 12 AT_TARGET_AARCH64_LE
1145at_put_u16_le dst 14 AT_ABI_V1
1146at_put_u32_le dst 20 total_len
1147at_put_u32_le dst 24 AT_SEC_TABLE_OFF
1148at_put_u16_le dst 28 AT_SEC_COUNT
1149at_put_u16_le dst 30 AT_SEC_REC_SIZE
1150at_put_u32_le dst 36 get at_man_entry 0
1151at_put_u32_le dst 40 AT_CAP_TABLE_OFF
1152at_put_u16_le dst 44 cap_cnt
1153at_put_u16_le dst 46 AT_CAP_REC_SIZE
1154at_put_u32_le dst 48 res_off
1155at_put_u16_le dst 52 AT_RES_ENV_SIZE
1156at_put_u32_le dst 56 get at_art_meta 4
1157at_put_u32_le dst 60 get at_art_meta 5
1158at_put_u32_le dst 64 get at_art_meta 6
1159at_put_u16_le dst 68 AT_SIG_BLOCK_SIZE
1160at_put_u16_le dst 70 AT_SIG_ALG_ED25519
1161at_encode_section dst 128 AT_SEC_KIND_CODE AT_PERM_RX 0
1162at_put_u32_le dst 140 clen
1163at_put_u32_le dst 144 clen
1164at_encode_section dst 160 AT_SEC_KIND_DATA AT_PERM_RW clen
1165at_put_u32_le dst 172 dlen
1166at_put_u32_le dst 176 mul get at_man_res 1 AT_PAGE_SIZE
1167at_encode_staged_caps dst cap_cnt
1168at_encode_resources dst res_off
1169end
1170
1171fn at_pack code view uchar * data view uchar * dst edit uchar * to int
1172clen = len code
1173dlen = len data
1174rc = at_pack_compute_layout clen dlen len dst
1175if ne rc AT_OK
1176ret rc
1177end
1178at_pack_write_tables dst clen dlen
1179pay_off = get at_art_meta 4
1180for i ulong clen bound 2048
1181put dst add pay_off i get code i
1182end
1183data_off = add pay_off clen
1184for i ulong dlen bound 2048
1185put dst add data_off i get data i
1186end
1187at_put_u16_le dst get at_art_meta 6 AT_SIG_ALG_ED25519
1188total_len = get at_art_meta 0
1189rc = at_parse part dst 0 total_len
1190if ne rc AT_OK
1191ret rc
1192end
1193put at_pack_len 0 total_len
1194ret AT_OK
1195end
1196
1197note Sign artifact in place with explicit 32-byte key
1198fn at_sign_with_key buf edit uchar * key view uchar 32 to int
1199rc = at_parse_and_identify buf
1200if ne rc AT_OK
1201ret rc
1202end
1203sig_off = get at_art_meta 6
1204fp = new uchar 32 0
1205id_cp = new uchar 32 0
1206sig = new uchar 64 0
1207for i ulong 32
1208put id_cp i get at_art_id i
1209end
1210at_signer_fp key fp
1211at_compute_sig key at_dom_art_sig id_cp sig
1212for i ulong 32
1213put buf add add sig_off 4 i get fp i
1214end
1215for i ulong 64
1216put buf add add sig_off 36 i get sig i
1217end
1218ret at_parse buf
1219end
1220
1221note Tool `sign` command: requires test_mode (SEED-0B test signing); production fails closed
1222fn at_sign_artifact buf edit uchar * test_mode bool to int
1223if not test_mode
1224ret AT_ERR_PROD_SIGN_OFF
1225end
1226key = new uchar 32 0
1227at_fill_test_art_key key
1228ret at_sign_with_key buf key
1229end
1230
1231note Tool `verify` command: parses, checks trust anchor (empty in production), verifies signature
1232fn at_verify_artifact bytes view uchar * test_mode bool to int
1233rc = at_parse_and_identify bytes
1234if ne rc AT_OK
1235ret rc
1236end
1237if not test_mode
1238ret AT_ERR_UNTRUSTED
1239end
1240key = new uchar 32 0
1241at_fill_test_art_key key
1242exp_fp = new uchar 32 0
1243at_signer_fp key exp_fp
1244for i ulong 32
1245if ne get at_art_signer_fp i get exp_fp i
1246ret AT_ERR_UNTRUSTED
1247end
1248end
1249id_cp = new uchar 32 0
1250for i ulong 32
1251put id_cp i get at_art_id i
1252end
1253exp_sig = new uchar 64 0
1254at_compute_sig key at_dom_art_sig id_cp exp_sig
1255sig_off = get at_art_meta 6
1256for i ulong 64
1257if ne get bytes add add sig_off 36 i get exp_sig i
1258ret AT_ERR_BAD_SIG
1259end
1260end
1261ret AT_OK
1262end
1263
1264note Tool `inspect` and `id` commands
1265fn at_inspect_artifact bytes view uchar * to int
1266ret at_parse_and_identify bytes
1267end
1268
1269fn at_id_hex bytes view uchar * hex_dst edit uchar * to int
1270rc = at_parse_and_identify bytes
1271if ne rc AT_OK
1272ret rc
1273end
1274id_cp = new uchar 32 0
1275for i ulong 32
1276put id_cp i get at_art_id i
1277end
1278ret at_hex_encode id_cp hex_dst
1279end
1280
1281note Admission Receipt v0 helpers (receipt.rs and receipt_cmd.rs)
1282fn at_rcp_digest rec view uchar * dst edit uchar * to void
1283at_sha256_dom_nul at_dom_rcp_dig part rec 0 AT_RCP_SIG_OFF dst
1284end
1285
1286fn at_rcp_nonce ver_id view uchar * seq ulong dst edit uchar * to void
1287ctx = new ulong 40 0
1288z = new uchar 1 0
1289seq_b = new uchar 8 0
1290dig = new uchar 32 0
1291at_put_u64_le seq_b 0 seq
1292sha256_init ctx
1293sha256_update ctx at_dom_rcp_nonce
1294sha256_update ctx z
1295sha256_update ctx ver_id
1296sha256_update ctx seq_b
1297sha256_final ctx dig
1298for i ulong 16
1299put dst i get dig i
1300end
1301end
1302
1303fn at_rcp_is_unsigned rec view uchar * to bool
1304ret at_bytes_zero rec AT_RCP_SIG_OFF sub AT_RCP_SIZE AT_RCP_SIG_OFF
1305end
1306
1307fn at_rcp_reset to void
1308for i ulong 16
1309put at_rcp_f i 0
1310end
1311for i ulong 16
1312put at_rcp_nonce_buf i 0
1313end
1314for i ulong 288
1315put at_rcp_digests i 0
1316end
1317end
1318
1319fn at_rcp_set_digest slot ulong val view uchar * to void
1320base = mul slot 32
1321for i ulong 32
1322put at_rcp_digests add base i get val i
1323end
1324end
1325
1326fn at_rcp_digest_is_zero slot ulong to bool
1327base = mul slot 32
1328for i ulong 32
1329if ne get at_rcp_digests add base i 0
1330ret false
1331end
1332end
1333ret true
1334end
1335
1336fn at_rcp_encode dst edit uchar * to int
1337if lt len dst AT_RCP_SIZE
1338ret AT_ERR_BUF_SMALL
1339end
1340for i ulong AT_RCP_SIZE
1341put dst i 0
1342end
1343for i ulong 7
1344put dst i get at_rcp_magic i
1345end
1346at_put_u16_le dst 8 AT_RCP_VERSION
1347at_put_u16_le dst 10 AT_RCP_HDR_SIZE
1348at_put_u32_le dst 12 AT_RCP_SIZE
1349at_put_u32_le dst 16 get at_rcp_f 0
1350at_put_u16_le dst 20 get at_rcp_f 1
1351at_put_u16_le dst 22 get at_rcp_f 2
1352at_put_u64_le dst 24 get at_rcp_f 3
1353for i ulong 16
1354put dst add 32 i get at_rcp_nonce_buf i
1355end
1356at_put_u64_le dst 48 get at_rcp_f 4
1357at_put_u32_le dst 64 get at_rcp_f 5
1358at_put_u32_le dst 68 get at_rcp_f 6
1359at_put_u32_le dst 72 get at_rcp_f 7
1360at_put_u16_le dst 76 get at_rcp_f 8
1361at_put_u16_le dst 78 get at_rcp_f 9
1362at_put_u32_le dst 80 get at_rcp_f 10
1363at_put_u32_le dst 84 get at_rcp_f 11
1364at_put_u32_le dst 88 get at_rcp_f 12
1365at_put_u32_le dst 92 get at_rcp_f 13
1366for i ulong 288
1367put dst add 96 i get at_rcp_digests i
1368end
1369at_put_u64_le dst 384 get at_rcp_f 14
1370at_put_u64_le dst 392 get at_rcp_f 15
1371ret AT_OK
1372end
1373
1374fn at_rcp_valid_reason code ulong to bool
1375if ge code 1
1376if le code 19
1377ret true
1378end
1379end
1380if ge code AT_LOADER_REASON_FIRST
1381if le code AT_LOADER_REASON_LAST
1382ret true
1383end
1384end
1385ret false
1386end
1387
1388fn at_rcp_validate_nonce to int
1389ver_id = new uchar 32 0
1390for i ulong 32
1391put ver_id i get at_rcp_digests add 224 i
1392end
1393exp_nonce = new uchar 16 0
1394at_rcp_nonce ver_id get at_rcp_f 3 exp_nonce
1395for i ulong 16
1396if ne get at_rcp_nonce_buf i get exp_nonce i
1397ret AT_ERR_BAD_RECEIPT
1398end
1399end
1400ret AT_OK
1401end
1402
1403fn at_rcp_validate_semantics to int
1404fl = get at_rcp_f 0
1405dec = get at_rcp_f 1
1406st = get at_rcp_f 5
1407ex = get at_rcp_f 6
1408rfl = get at_rcp_f 7
1409rstg = get at_rcp_f 8
1410rrsn = get at_rcp_f 9
1411if eq and fl AT_RCP_FLAG_MACHINE_ID 0
1412if not at_rcp_digest_is_zero 8
1413ret AT_ERR_BAD_RECEIPT
1414end
1415end
1416if eq and fl AT_RCP_FLAG_CONTEXT_ID 0
1417if ne get at_rcp_f 14 0
1418ret AT_ERR_BAD_RECEIPT
1419end
1420if ne get at_rcp_f 15 0
1421ret AT_ERR_BAD_RECEIPT
1422end
1423end
1424if eq and fl AT_RCP_FLAG_TIMESTAMP 0
1425if ne get at_rcp_f 4 0
1426ret AT_ERR_BAD_RECEIPT
1427end
1428end
1429if gt rstg AT_MAX_REJ_STAGE
1430ret AT_ERR_BAD_RECEIPT
1431end
1432if ne rrsn 0
1433if not at_rcp_valid_reason rrsn
1434ret AT_ERR_BAD_RECEIPT
1435end
1436end
1437if eq dec AT_DEC_REJECTED
1438if eq rstg 0
1439ret AT_ERR_BAD_RECEIPT
1440end
1441if eq rrsn 0
1442ret AT_ERR_BAD_RECEIPT
1443end
1444if ne st AT_EXEC_NOT_RUN
1445ret AT_ERR_BAD_RECEIPT
1446end
1447if ne ex 0
1448ret AT_ERR_BAD_RECEIPT
1449end
1450if ne get at_rcp_f 10 0
1451ret AT_ERR_BAD_RECEIPT
1452end
1453if ne get at_rcp_f 11 0
1454ret AT_ERR_BAD_RECEIPT
1455end
1456if ne get at_rcp_f 12 0
1457ret AT_ERR_BAD_RECEIPT
1458end
1459if ne and rfl not AT_RCP_RES_RECLAIMED 0
1460ret AT_ERR_BAD_RECEIPT
1461end
1462else
1463if ne rstg 0
1464ret AT_ERR_BAD_RECEIPT
1465end
1466if ne rrsn 0
1467ret AT_ERR_BAD_RECEIPT
1468end
1469end
1470dec_can = eq dec AT_DEC_CANARY_FAILED
1471st_can = eq st AT_EXEC_CANARY_FAILED
1472if ne dec_can st_can
1473ret AT_ERR_BAD_RECEIPT
1474end
1475if ne and rfl AT_RCP_RES_CANARY_OK 0
1476if ne st AT_EXEC_EXITED
1477ret AT_ERR_BAD_RECEIPT
1478end
1479if ne ex 0
1480ret AT_ERR_BAD_RECEIPT
1481end
1482end
1483ret at_rcp_validate_nonce
1484end
1485
1486fn at_rcp_decode rec view uchar * to int
1487n = len rec
1488if lt n AT_RCP_SIZE
1489ret AT_ERR_TRUNCATED
1490end
1491if gt n AT_RCP_SIZE
1492ret AT_ERR_WRONG_LEN
1493end
1494for i ulong 7
1495if ne get rec i get at_rcp_magic i
1496ret AT_ERR_BAD_MAGIC
1497end
1498end
1499if ne get rec 7 0
1500ret AT_ERR_BAD_MAGIC
1501end
1502if ne at_get_u16_le rec 8 AT_RCP_VERSION
1503ret AT_ERR_UNSUP_VER
1504end
1505if ne at_get_u16_le rec 10 AT_RCP_HDR_SIZE
1506ret AT_ERR_UNSUP_VER
1507end
1508if ne at_get_u32_le rec 12 AT_RCP_SIZE
1509ret AT_ERR_UNSUP_VER
1510end
1511fl = at_get_u32_le rec 16
1512rfl = at_get_u32_le rec 72
1513if ne and fl not AT_RCP_VALID_FLAGS 0
1514ret AT_ERR_UNSUP_FLAGS
1515end
1516if ne and rfl not AT_RCP_VALID_RES_FLAGS 0
1517ret AT_ERR_UNSUP_FLAGS
1518end
1519if not at_bytes_zero rec 56 8
1520ret AT_ERR_RESERVED_NZ
1521end
1522if not at_bytes_zero rec 500 12
1523ret AT_ERR_RESERVED_NZ
1524end
1525if not at_bytes_zero rec 400 100
1526if ne at_get_u16_le rec 400 AT_SIG_ALG_ED25519
1527ret AT_ERR_BAD_SIG_FMT
1528end
1529if not at_bytes_zero rec 402 2
1530ret AT_ERR_RESERVED_NZ
1531end
1532end
1533dec = at_get_u16_le rec 20
1534tier = at_get_u16_le rec 22
1535est = at_get_u32_le rec 64
1536if lt dec 1
1537ret AT_ERR_BAD_RECEIPT
1538end
1539if gt dec 4
1540ret AT_ERR_BAD_RECEIPT
1541end
1542if lt tier 1
1543ret AT_ERR_BAD_RECEIPT
1544end
1545if gt tier 3
1546ret AT_ERR_BAD_RECEIPT
1547end
1548if gt est 6
1549ret AT_ERR_BAD_RECEIPT
1550end
1551put at_rcp_f 0 fl
1552put at_rcp_f 1 dec
1553put at_rcp_f 2 tier
1554put at_rcp_f 3 at_get_u64_le rec 24
1555put at_rcp_f 4 at_get_u64_le rec 48
1556put at_rcp_f 5 est
1557put at_rcp_f 6 at_get_u32_le rec 68
1558put at_rcp_f 7 rfl
1559put at_rcp_f 8 at_get_u16_le rec 76
1560put at_rcp_f 9 at_get_u16_le rec 78
1561put at_rcp_f 10 at_get_u32_le rec 80
1562put at_rcp_f 11 at_get_u32_le rec 84
1563put at_rcp_f 12 at_get_u32_le rec 88
1564put at_rcp_f 13 at_get_u32_le rec 92
1565put at_rcp_f 14 at_get_u64_le rec 384
1566put at_rcp_f 15 at_get_u64_le rec 392
1567for i ulong 16
1568put at_rcp_nonce_buf i get rec add 32 i
1569end
1570for i ulong 288
1571put at_rcp_digests i get rec add 96 i
1572end
1573ret at_rcp_validate_semantics
1574end
1575
1576fn at_rcp_inspect rec view uchar * to int
1577rc = at_rcp_decode rec
1578if ne rc AT_OK
1579ret rc
1580end
1581dig = new uchar 32 0
1582at_rcp_digest rec dig
1583for i ulong 32
1584put at_rcp_dig_out i get dig i
1585end
1586if at_rcp_is_unsigned rec
1587put at_rcp_signed_flag 0 0
1588else
1589put at_rcp_signed_flag 0 1
1590end
1591ret AT_OK
1592end
1593
1594fn at_rcp_bind_match slot ulong want view uchar * zero_ok bool to bool
1595base = mul slot 32
1596same = true
1597for i ulong 32
1598if ne get at_rcp_digests add base i get want i
1599same = false
1600end
1601end
1602if same
1603ret true
1604end
1605if zero_ok
1606if at_rcp_digest_is_zero slot
1607ret true
1608end
1609end
1610ret false
1611end
1612
1613fn at_rcp_check_one_binding slot ulong zero_ok bool to bool
1614want = new uchar 32 0
1615if eq slot 0
1616for i ulong 32
1617put want i get at_art_id i
1618end
1619end
1620if eq slot 1
1621for i ulong 32
1622put want i get at_art_payload_sha i
1623end
1624end
1625if eq slot 2
1626for i ulong 32
1627put want i get at_art_signer_fp i
1628end
1629end
1630if eq slot 4
1631for i ulong 32
1632put want i get at_art_req_caps_sha i
1633end
1634end
1635if eq slot 6
1636for i ulong 32
1637put want i get at_art_res_env_sha i
1638end
1639end
1640ret at_rcp_bind_match slot want zero_ok
1641end
1642
1643note Tool `receipt check` command (receipt_cmd.rs::check)
1644fn at_rcp_check rec view uchar * art view uchar * to int
1645if not at_rcp_is_unsigned rec
1646ret AT_ERR_RCP_MUST_UNSIGNED
1647end
1648rc = at_rcp_decode rec
1649if ne rc AT_OK
1650ret rc
1651end
1652admitted = eq get at_rcp_f 1 AT_DEC_ADMITTED
1653arc = at_parse_and_identify art
1654if ne arc AT_OK
1655if admitted
1656ret AT_ERR_RCP_ADMIT_BAD_ART
1657end
1658if not at_rcp_digest_is_zero 0
1659ret AT_ERR_RCP_NZ_BAD_ART
1660end
1661if not at_rcp_digest_is_zero 1
1662ret AT_ERR_RCP_NZ_BAD_ART
1663end
1664if not at_rcp_digest_is_zero 2
1665ret AT_ERR_RCP_NZ_BAD_ART
1666end
1667if not at_rcp_digest_is_zero 4
1668ret AT_ERR_RCP_NZ_BAD_ART
1669end
1670if not at_rcp_digest_is_zero 6
1671ret AT_ERR_RCP_NZ_BAD_ART
1672end
1673else
1674z_ok = not admitted
1675if not at_rcp_check_one_binding 0 z_ok
1676ret AT_ERR_RCP_BIND_MISMATCH
1677end
1678if not at_rcp_check_one_binding 1 z_ok
1679ret AT_ERR_RCP_BIND_MISMATCH
1680end
1681if not at_rcp_check_one_binding 2 z_ok
1682ret AT_ERR_RCP_BIND_MISMATCH
1683end
1684if not at_rcp_check_one_binding 4 z_ok
1685ret AT_ERR_RCP_BIND_MISMATCH
1686end
1687if not at_rcp_check_one_binding 6 z_ok
1688ret AT_ERR_RCP_BIND_MISMATCH
1689end
1690if admitted
1691if at_rcp_digest_is_zero 5
1692ret AT_ERR_RCP_NO_GRANTS
1693end
1694end
1695end
1696dig = new uchar 32 0
1697at_rcp_digest rec dig
1698for i ulong 32
1699put at_rcp_dig_out i get dig i
1700end
1701ret AT_OK
1702end
1703
1704note Tool `receipt sign-test` command (receipt_cmd.rs::sign_test)
1705fn at_rcp_sign_test rec edit uchar * test_mode bool to int
1706if not test_mode
1707ret AT_ERR_PROD_SIGN_OFF
1708end
1709if not at_rcp_is_unsigned rec
1710ret AT_ERR_BAD_SIG_FMT
1711end
1712rc = at_rcp_decode rec
1713if ne rc AT_OK
1714ret rc
1715end
1716key = new uchar 32 0
1717at_fill_test_rcp_key key
1718fp = new uchar 32 0
1719at_signer_fp key fp
1720dig = new uchar 32 0
1721at_rcp_digest rec dig
1722sig = new uchar 64 0
1723at_compute_sig key at_dom_rcp_sig dig sig
1724at_put_u16_le rec 400 AT_SIG_ALG_ED25519
1725for i ulong 32
1726put rec add 404 i get fp i
1727end
1728for i ulong 64
1729put rec add 436 i get sig i
1730end
1731ret AT_OK
1732end
1733
1734note Tool `receipt verify` command (receipt_cmd.rs::verify_file)
1735fn at_rcp_verify rec view uchar * test_mode bool to int
1736rc = at_rcp_decode rec
1737if ne rc AT_OK
1738ret rc
1739end
1740if at_rcp_is_unsigned rec
1741ret AT_ERR_BAD_SIG_FMT
1742end
1743if not test_mode
1744ret AT_ERR_UNTRUSTED
1745end
1746key = new uchar 32 0
1747at_fill_test_rcp_key key
1748exp_fp = new uchar 32 0
1749at_signer_fp key exp_fp
1750for i ulong 32
1751if ne get rec add 404 i get exp_fp i
1752ret AT_ERR_UNTRUSTED
1753end
1754end
1755dig = new uchar 32 0
1756at_rcp_digest rec dig
1757exp_sig = new uchar 64 0
1758at_compute_sig key at_dom_rcp_sig dig exp_sig
1759for i ulong 64
1760if ne get rec add 436 i get exp_sig i
1761ret AT_ERR_BAD_SIG
1762end
1763end
1764for i ulong 32
1765put at_rcp_dig_out i get dig i
1766end
1767ret AT_OK
1768end
1769
1770note P2-7 Negative Corpus generator (corpus.rs: H01..H29)
1771fn at_corpus_stage_seed_cap rights ulong to void
1772idx = at_manifest_push_cap AT_RES_KIND_OBJECT 1 rights 1 4
1773at_manifest_set_cap_bounds ulong idx 32 0 32
1774end
1775
1776fn at_corpus_stage_res cp ulong dp ulong sp ulong mc ulong sc ulong to void
1777at_manifest_set_res_pages cp dp sp mc
1778at_manifest_set_res_limits 0 0 100000000 100000000 sc
1779end
1780
1781fn at_corpus_fill_exit0 buf edit uchar * to void
1782at_put_u32_le buf 0 AT_INSN_MOV_X0_0
1783at_put_u32_le buf 4 AT_INSN_MOV_X8_2
1784at_put_u32_le buf 8 AT_INSN_SVC_0
1785at_put_u32_le buf 12 AT_INSN_B_SELF
1786end
1787
1788fn at_corpus_pack_signed code view uchar * data view uchar * dst edit uchar * to int
1789rc = at_pack code data dst
1790if ne rc AT_OK
1791ret rc
1792end
1793n = get at_pack_len 0
1794rc = at_sign_artifact part dst 0 n true
1795if ne rc AT_OK
1796ret rc
1797end
1798put at_corpus_len 0 n
1799ret AT_OK
1800end
1801
1802fn at_corpus_build_baseline dst edit uchar * to int
1803at_manifest_reset
1804at_corpus_stage_seed_cap 1
1805at_corpus_stage_res 1 1 1 1 8
1806code = new uchar 16 0
1807at_corpus_fill_exit0 code
1808ret at_corpus_pack_signed code at_tag_baseline dst
1809end
1810
1811fn at_corpus_mutate_base cid ulong dst edit uchar * to int
1812ukey = new uchar 32 0
1813rc = at_corpus_build_baseline dst
1814if ne rc AT_OK
1815ret rc
1816end
1817n = get at_corpus_len 0
1818pay_off = at_get_u32_le dst 56
1819res_off = add AT_CAP_TABLE_OFF AT_CAP_REC_SIZE
1820sig_off = sub n AT_SIG_BLOCK_SIZE
1821if eq cid 1
1822put dst 0 88
1823ret AT_OK
1824end
1825if eq cid 2
1826at_put_u16_le dst 8 1
1827ret AT_OK
1828end
1829if eq cid 3
1830at_put_u16_le dst 12 2
1831ret AT_OK
1832end
1833if eq cid 4
1834at_put_u16_le dst 14 2
1835ret AT_OK
1836end
1837if eq cid 5
1838put at_corpus_len 0 sub n 10
1839ret AT_OK
1840end
1841if eq cid 6
1842if ge n len dst
1843ret AT_ERR_BUF_SMALL
1844end
1845put dst n 0
1846put at_corpus_len 0 add n 1
1847ret AT_OK
1848end
1849if eq cid 7
1850t = at_get_u32_le dst 20
1851at_put_u32_le dst 20 add t 1
1852ret AT_OK
1853end
1854if eq cid 8
1855at_put_u32_le dst 60 0xffffffff
1856ret AT_OK
1857end
1858if eq cid 9
1859at_put_u32_le dst 168 4
1860ret AT_OK
1861end
1862if eq cid 10
1863at_put_u32_le dst 36 0x1000
1864ret AT_OK
1865end
1866if eq cid 11
1867put dst pay_off xor get dst pay_off 1
1868ret AT_OK
1869end
1870if eq cid 12
1871at_put_u32_le dst add AT_CAP_TABLE_OFF 8 3
1872ret AT_OK
1873end
1874if eq cid 13
1875at_put_u32_le dst add res_off 8 2
1876ret AT_OK
1877end
1878if eq cid 14
1879p = add sig_off 40
1880put dst p xor get dst p 1
1881ret AT_OK
1882end
1883if eq cid 15
1884at_fill_untrusted_key ukey
1885ret at_sign_with_key part dst 0 n ukey
1886end
1887if eq cid 16
1888at_put_u16_le dst sig_off 2
1889ret AT_OK
1890end
1891if eq cid 17
1892put dst 100 1
1893ret AT_OK
1894end
1895if eq cid 18
1896at_put_u32_le dst add AT_CAP_TABLE_OFF 8 65
1897ret AT_OK
1898end
1899if eq cid 19
1900at_put_u16_le dst 44 17
1901ret AT_OK
1902end
1903if eq cid 20
1904at_put_u32_le dst res_off 65
1905ret AT_OK
1906end
1907if eq cid 22
1908at_put_u16_le dst 130 7
1909ret AT_OK
1910end
1911ret AT_ERR_BAD_CASE
1912end
1913
1914fn at_corpus_build_custom cid ulong dst edit uchar * to int
1915code = new uchar 16 0
1916c4 = new uchar 4 0
1917c8 = new uchar 8 0
1918c12 = new uchar 12 0
1919c32 = new uchar 32 0
1920if eq cid 21
1921at_manifest_reset
1922at_corpus_stage_res 64 64 16 0 4
1923at_corpus_fill_exit0 code
1924ret at_corpus_pack_signed code at_tag_largest dst
1925end
1926if eq cid 23
1927at_manifest_reset
1928at_corpus_stage_res 1 1 1 0 4
1929at_put_u32_le c4 0 AT_INSN_BR_X2
1930ret at_corpus_pack_signed c4 at_tag_datapage dst
1931end
1932if eq cid 24
1933at_manifest_reset
1934at_corpus_stage_res 1 1 1 0 4
1935at_put_u32_le c8 0 AT_INSN_SUB_X9_SP_16
1936at_put_u32_le c8 4 AT_INSN_BR_X9
1937ret at_corpus_pack_signed c8 at_tag_stackpag dst
1938end
1939if eq cid 25
1940at_manifest_reset
1941at_corpus_stage_res 1 1 1 0 4
1942at_put_u32_le c12 0 AT_INSN_MOV_X8_99
1943at_put_u32_le c12 4 AT_INSN_SVC_0
1944at_put_u32_le c12 8 AT_INSN_B_SELF
1945ret at_corpus_pack_signed c12 at_tag_badsyscl dst
1946end
1947if eq cid 26
1948at_manifest_reset
1949at_corpus_stage_res 1 1 1 0 2
1950at_put_u32_le c32 0 AT_INSN_MOV_X0_0
1951at_put_u32_le c32 4 AT_INSN_MOV_X8_6
1952at_put_u32_le c32 8 AT_INSN_SVC_0
1953at_put_u32_le c32 12 AT_INSN_SVC_0
1954at_put_u32_le c32 16 AT_INSN_SVC_0
1955at_put_u32_le c32 20 AT_INSN_MOV_X8_2
1956at_put_u32_le c32 24 AT_INSN_SVC_0
1957at_put_u32_le c32 28 AT_INSN_B_SELF
1958ret at_corpus_pack_signed c32 at_tag_overrun dst
1959end
1960if eq cid 27
1961at_manifest_reset
1962at_corpus_stage_seed_cap 1
1963at_manifest_set_res_pages 1 1 1 1
1964at_manifest_set_res_limits 1 64 100000000 100000000 8
1965at_corpus_fill_exit0 code
1966ret at_corpus_pack_signed code at_tag_ipcwants dst
1967end
1968if eq cid 28
1969at_manifest_reset
1970at_corpus_stage_seed_cap 2
1971at_corpus_stage_res 1 1 1 1 8
1972at_corpus_fill_exit0 code
1973ret at_corpus_pack_signed code at_tag_writereq dst
1974end
1975if eq cid 29
1976at_manifest_reset
1977at_corpus_stage_seed_cap 3
1978at_corpus_stage_res 1 1 1 1 8
1979at_corpus_fill_exit0 code
1980ret at_corpus_pack_signed code at_tag_rwrequst dst
1981end
1982ret AT_ERR_BAD_CASE
1983end
1984
1985fn at_corpus_build_case cid ulong dst edit uchar * to int
1986if lt cid 1
1987ret AT_ERR_BAD_CASE
1988end
1989if gt cid AT_CORPUS_COUNT
1990ret AT_ERR_BAD_CASE
1991end
1992if le cid 20
1993ret at_corpus_mutate_base cid dst
1994end
1995if eq cid 22
1996ret at_corpus_mutate_base cid dst
1997end
1998ret at_corpus_build_custom cid dst
1999end
2000
2001note Populate at_corpus_exp[0..4] = (expect_kind, code, exec_kind, caps) for H01..H29
2002fn at_corpus_set_exp k ulong code ulong ex ulong caps ulong to void
2003put at_corpus_exp 0 k
2004put at_corpus_exp 1 code
2005put at_corpus_exp 2 ex
2006put at_corpus_exp 3 caps
2007end
2008
2009fn at_corpus_expect cid ulong to int
2010if eq cid 1
2011at_corpus_set_exp AT_EXP_REJ_VERIFIED 1 0 0
2012ret AT_OK
2013end
2014if eq cid 2
2015at_corpus_set_exp AT_EXP_REJ_VERIFIED 2 0 0
2016ret AT_OK
2017end
2018if eq cid 3
2019at_corpus_set_exp AT_EXP_REJ_VERIFIED 3 0 0
2020ret AT_OK
2021end
2022if eq cid 4
2023at_corpus_set_exp AT_EXP_REJ_VERIFIED 4 0 0
2024ret AT_OK
2025end
2026if eq cid 5
2027at_corpus_set_exp AT_EXP_REJ_VERIFIED 7 0 0
2028ret AT_OK
2029end
2030if eq cid 6
2031at_corpus_set_exp AT_EXP_REJ_VERIFIED 7 0 0
2032ret AT_OK
2033end
2034if eq cid 7
2035at_corpus_set_exp AT_EXP_REJ_VERIFIED 7 0 0
2036ret AT_OK
2037end
2038if eq cid 8
2039at_corpus_set_exp AT_EXP_REJ_VERIFIED 14 0 0
2040ret AT_OK
2041end
2042if eq cid 9
2043at_corpus_set_exp AT_EXP_REJ_VERIFIED 11 0 0
2044ret AT_OK
2045end
2046if eq cid 10
2047at_corpus_set_exp AT_EXP_REJ_VERIFIED 12 0 0
2048ret AT_OK
2049end
2050if eq cid 11
2051at_corpus_set_exp AT_EXP_REJ_VERIFIED 18 0 0
2052ret AT_OK
2053end
2054if eq cid 12
2055at_corpus_set_exp AT_EXP_REJ_VERIFIED 18 0 0
2056ret AT_OK
2057end
2058if eq cid 13
2059at_corpus_set_exp AT_EXP_REJ_VERIFIED 18 0 0
2060ret AT_OK
2061end
2062if eq cid 14
2063at_corpus_set_exp AT_EXP_REJ_VERIFIED 18 0 0
2064ret AT_OK
2065end
2066if eq cid 15
2067at_corpus_set_exp AT_EXP_REJ_VERIFIED 17 0 0
2068ret AT_OK
2069end
2070if eq cid 16
2071at_corpus_set_exp AT_EXP_REJ_VERIFIED 15 0 0
2072ret AT_OK
2073end
2074if eq cid 17
2075at_corpus_set_exp AT_EXP_REJ_VERIFIED 8 0 0
2076ret AT_OK
2077end
2078if eq cid 18
2079at_corpus_set_exp AT_EXP_REJ_VERIFIED 13 0 0
2080ret AT_OK
2081end
2082if eq cid 19
2083at_corpus_set_exp AT_EXP_REJ_VERIFIED 14 0 0
2084ret AT_OK
2085end
2086if eq cid 20
2087at_corpus_set_exp AT_EXP_REJ_VERIFIED 14 0 0
2088ret AT_OK
2089end
2090if eq cid 21
2091at_corpus_set_exp AT_EXP_ADMITTED 0 AT_CORPUS_EXEC_EXIT0 0
2092ret AT_OK
2093end
2094if eq cid 22
2095at_corpus_set_exp AT_EXP_REJ_VERIFIED 11 0 0
2096ret AT_OK
2097end
2098if eq cid 23
2099at_corpus_set_exp AT_EXP_ADMITTED 0 AT_CORPUS_EXEC_NX 0
2100ret AT_OK
2101end
2102if eq cid 24
2103at_corpus_set_exp AT_EXP_ADMITTED 0 AT_CORPUS_EXEC_NX 0
2104ret AT_OK
2105end
2106if eq cid 25
2107at_corpus_set_exp AT_EXP_ADMITTED 0 AT_CORPUS_EXEC_BAD_SYS99 0
2108ret AT_OK
2109end
2110if eq cid 26
2111at_corpus_set_exp AT_EXP_ADMITTED 0 AT_CORPUS_EXEC_OVERRUN 0
2112ret AT_OK
2113end
2114if eq cid 27
2115at_corpus_set_exp AT_EXP_REJ_AUTHORIZED 14 0 0
2116ret AT_OK
2117end
2118if eq cid 28
2119at_corpus_set_exp AT_EXP_ADMITTED 0 AT_CORPUS_EXEC_EXIT0 0
2120ret AT_OK
2121end
2122if eq cid 29
2123at_corpus_set_exp AT_EXP_ADMITTED 0 AT_CORPUS_EXEC_EXIT0 1
2124ret AT_OK
2125end
2126ret AT_ERR_BAD_CASE
2127end
2128
2129note CLI argument parser and command dispatcher (main.rs::run + receipt_cmd.rs::run)
2130fn at_cli_dispatch_rcp argc ulong a2 view uchar * to int
2131if at_bytes_eq a2 at_kw_inspect
2132if eq argc 4
2133ret AT_CMD_RCP_INSPECT
2134end
2135ret AT_CMD_INVALID
2136end
2137if at_bytes_eq a2 at_kw_check
2138if eq argc 5
2139ret AT_CMD_RCP_CHECK
2140end
2141ret AT_CMD_INVALID
2142end
2143if at_bytes_eq a2 at_kw_from_hex
2144if eq argc 5
2145ret AT_CMD_RCP_FROM_HEX
2146end
2147ret AT_CMD_INVALID
2148end
2149if at_bytes_eq a2 at_kw_sign_test
2150if eq argc 5
2151ret AT_CMD_RCP_SIGN_TEST
2152end
2153ret AT_CMD_INVALID
2154end
2155if at_bytes_eq a2 at_kw_verify
2156if eq argc 4
2157ret AT_CMD_RCP_VERIFY
2158end
2159ret AT_CMD_INVALID
2160end
2161ret AT_CMD_INVALID
2162end
2163
2164fn at_cli_dispatch argc ulong a1 view uchar * a2 view uchar * test_mode bool to int
2165if lt argc 2
2166ret AT_CMD_INVALID
2167end
2168if at_bytes_eq a1 at_kw_pack
2169if eq argc 6
2170ret AT_CMD_PACK
2171end
2172ret AT_CMD_INVALID
2173end
2174if at_bytes_eq a1 at_kw_inspect
2175if eq argc 3
2176ret AT_CMD_INSPECT
2177end
2178ret AT_CMD_INVALID
2179end
2180if at_bytes_eq a1 at_kw_id
2181if eq argc 3
2182ret AT_CMD_ID
2183end
2184ret AT_CMD_INVALID
2185end
2186if at_bytes_eq a1 at_kw_sign
2187if eq argc 4
2188ret AT_CMD_SIGN
2189end
2190ret AT_CMD_INVALID
2191end
2192if at_bytes_eq a1 at_kw_verify
2193if eq argc 3
2194ret AT_CMD_VERIFY
2195end
2196ret AT_CMD_INVALID
2197end
2198if at_bytes_eq a1 at_kw_neg_corpus
2199if test_mode
2200if eq argc 3
2201ret AT_CMD_NEG_CORPUS
2202end
2203end
2204ret AT_CMD_INVALID
2205end
2206if at_bytes_eq a1 at_kw_receipt
2207ret at_cli_dispatch_rcp argc a2
2208end
2209ret AT_CMD_INVALID
2210end