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

1011 lines · 25408 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-artifact/artifact.om: Pure-Omega Binary Artifact v0 parser,
3note capability/resource codecs, canonical domain-separated digests, identity, and
4note Ed25519 signature verification.
5note Converted from aienux crates/aienos-artifact/src/{lib,error,id,canonical,capability,resource,format,signature,verify}.rs.
6note Compose with: si/directives/om2.om om/tls/sha2.om protocol/omnet/om/sha512/sha512.om protocol/omnet/om/ed25519/ed25519.om aienux/crates/aienos-artifact/artifact.om aienux/crates/aienos-artifact/receipt.om ...
7
8note Stable fail-closed error codes matching ArtifactError repr(u16)
9const ART_OK int 0
10const ART_ERR_BAD_MAGIC int 1
11const ART_ERR_UNSUPPORTED_VERSION int 2
12const ART_ERR_WRONG_TARGET int 3
13const ART_ERR_WRONG_ABI int 4
14const ART_ERR_LENGTH_OVERFLOW int 5
15const ART_ERR_TRUNCATED int 6
16const ART_ERR_WRONG_LENGTH int 7
17const ART_ERR_RESERVED_NON_ZERO int 8
18const ART_ERR_UNSUPPORTED_FLAGS int 9
19const ART_ERR_SECTION_OVERLAP int 10
20const ART_ERR_BAD_SECTION int 11
21const ART_ERR_BAD_ENTRY_POINT int 12
22const ART_ERR_MALFORMED_CAPABILITY int 13
23const ART_ERR_RESOURCE_LIMIT int 14
24const ART_ERR_BAD_SIGNATURE_FORMAT int 15
25const ART_ERR_DIGEST_MISMATCH int 16
26const ART_ERR_UNTRUSTED_SIGNER int 17
27const ART_ERR_BAD_SIGNATURE int 18
28const ART_ERR_RIGHTS_ESCALATION int 19
29const ART_ERR_MALFORMED_RECEIPT int 20
30
31note Format constants (format.rs)
32const ART_FORMAT_VERSION ulong 0
33const ART_HEADER_SIZE ulong 128
34const ART_SECTION_TABLE_OFFSET ulong 128
35const ART_SECTION_COUNT ulong 2
36const ART_SECTION_RECORD_SIZE ulong 32
37const ART_SECTION_TABLE_SIZE ulong 64
38const ART_CAPABILITY_TABLE_OFFSET ulong 192
39const ART_SIGNATURE_BLOCK_SIZE ulong 100
40const ART_SIG_ALG_ED25519 ulong 1
41const ART_MAX_ARTIFACT_SIZE ulong 16777216
42const ART_MAX_PAYLOAD_SIZE ulong 8388608
43const ART_TARGET_AARCH64_LE ulong 1
44const ART_ABI_V1 ulong 1
45const ART_SECTION_KIND_CODE ulong 1
46const ART_SECTION_KIND_DATA ulong 2
47const ART_PERM_RX ulong 5
48const ART_PERM_RW ulong 3
49
50note Capability constants (capability.rs)
51const ART_CAP_RECORD_SIZE ulong 48
52const ART_MAX_CAPABILITIES ulong 16
53const ART_RESOURCE_KIND_CHANNEL ulong 2
54const ART_RESOURCE_KIND_OBJECT ulong 3
55const ART_RIGHT_READ ulong 1
56const ART_RIGHT_WRITE ulong 2
57const ART_RIGHT_MAP ulong 4
58const ART_RIGHT_GRANT ulong 8
59const ART_RIGHT_DERIVE ulong 16
60const ART_RIGHT_REVOKE ulong 32
61const ART_VALID_RIGHTS ulong 63
62
63note Resource envelope constants (resource.rs)
64const ART_RESOURCE_ENVELOPE_SIZE ulong 48
65const ART_PAGE_SIZE ulong 4096
66const ART_MAX_CODE_PAGES ulong 64
67const ART_MAX_DATA_PAGES ulong 64
68const ART_MAX_STACK_PAGES ulong 16
69const ART_MAX_IPC_MESSAGES ulong 1024
70const ART_MAX_IPC_BYTES ulong 1048576
71const ART_MAX_CPU_TICKS ulong 1000000000
72const ART_MAX_ELAPSED_TICKS ulong 1000000000
73const ART_MAX_SYSCALLS ulong 65536
74
75note Signature and trust tier constants (signature.rs)
76const ART_SIG_SIZE ulong 64
77const ART_PUBKEY_SIZE ulong 32
78const ART_SIG_MSG_SIZE ulong 61
79const ART_TRUST_TIER_NONE ulong 0
80const ART_TRUST_TIER_PRODUCTION ulong 1
81const ART_TRUST_TIER_SEED0B_QUAL ulong 2
82const ART_MAX_ANCHORS ulong 8
83
84note Domain strings (each followed by a trailing NUL byte when hashed/framed)
85text art_magic_pfx "AIENART"
86text art_id_dom_pfx "AIENOS-ARTIFACT-V1"
87text art_req_dom_pfx "AIENOS-ARTIFACT-CAP-REQUESTS-V1"
88text art_grant_dom_pfx "AIENOS-ADMISSION-CAP-GRANTS-V1"
89text art_res_dom_pfx "AIENOS-ARTIFACT-RESOURCES-V1"
90text art_pol_dom_pfx "AIENOS-ADMISSION-POLICY-V1"
91text art_mach_dom_pfx "AIENOS-MACHINE-ID-V1"
92text art_ver_dom_pfx "AIENOS-VERIFIER-IDENTITY-V1"
93text art_sig_dom_pfx "AIENOS-ARTIFACT-SIGNATURE-V1"
94
95note Parsed artifact state
96note art_meta slots:
97note 0=target_arch, 1=abi_version, 2=entry_offset, 3=sig_alg,
98note 4=cap_cnt, 5=cap_off, 6=cap_bytes_len, 7=res_off,
99note 8=payload_off, 9=payload_len, 10=sig_off, 11=verified_tier
100global art_meta ulong 12
101note Two sections, 6 ulongs each: kind, perm, poff, flen, mlen, align
102global art_sections ulong 12
103note Up to 16 capabilities, 8 ulongs each:
104note 0=resource_kind, 1=resource_id, 2=rights, 3=bounds_kind,
105note 4=max_operations, 5=max_bytes, 6=byte_offset, 7=byte_length
106global art_caps ulong 128
107note Resource envelope, 9 ulongs:
108note 0=code_pages, 1=data_pages, 2=stack_pages, 3=max_capabilities,
109note 4=ipc_messages, 5=ipc_bytes, 6=cpu_ticks, 7=elapsed_ticks, 8=syscall_count
110global art_res ulong 9
111global art_signer_fp uchar 32
112global art_sig uchar 64
113global art_id uchar 32
114global art_payload_dig uchar 32
115
116note Artifact trust anchor table (distinct from receipt anchors)
117global art_anchor_cnt ulong 1
118global art_anchor_pks uchar 256
119global art_anchor_tiers ulong 8
120
121note Little-endian byte helpers
122fn art_get_u16_le buf view uchar * off ulong to ulong
123b0 = ulong get buf off
124b1 = ulong get buf add off 1
125ret or b0 shl b1 8
126end
127
128fn art_get_u32_le buf view uchar * off ulong to ulong
129b0 = ulong get buf off
130b1 = ulong get buf add off 1
131b2 = ulong get buf add off 2
132b3 = ulong get buf add off 3
133ret or or b0 shl b1 8 or shl b2 16 shl b3 24
134end
135
136fn art_get_u64_le buf view uchar * off ulong to ulong
137acc = ulong 0
138for i ulong 8
139b = ulong get buf add off i
140acc = or acc shl b mul i 8
141end
142ret acc
143end
144
145fn art_put_u16_le buf edit uchar * off ulong v ulong to void
146put buf off uchar and v 0xff
147put buf add off 1 uchar and shr v 8 0xff
148end
149
150fn art_put_u32_le buf edit uchar * off ulong v ulong to void
151put buf off uchar and v 0xff
152put buf add off 1 uchar and shr v 8 0xff
153put buf add off 2 uchar and shr v 16 0xff
154put buf add off 3 uchar and shr v 24 0xff
155end
156
157fn art_put_u64_le buf edit uchar * off ulong v ulong to void
158for i ulong 8
159sh = mul i 8
160put buf add off i uchar and shr v sh 0xff
161end
162end
163
164fn art_all_zero buf view uchar * off ulong n ulong to bool
165for i ulong n bound 1024
166if ne get buf add off i 0
167ret false
168end
169end
170ret true
171end
172
173fn art_bytes_eq a view uchar * b view uchar * to bool
174if ne len a len b
175ret false
176end
177n = len a
178for i ulong n bound 4096
179if ne get a i get b i
180ret false
181end
182end
183ret true
184end
185
186note Domain-separated SHA-256: SHA256(dom_pfx || 0x00 || data)
187fn art_sha256_dom dom view uchar * data view uchar * dst edit uchar * to void
188ctx = new ulong 40 0
189nul = new uchar 1 0
190sha256_init ctx
191sha256_update ctx dom
192sha256_update ctx nul
193sha256_update ctx data
194sha256_final ctx dst
195end
196
197note Canonical digests (canonical.rs, id.rs)
198fn art_id_from_unsigned_bytes data view uchar * dst edit uchar * to void
199art_sha256_dom art_id_dom_pfx data dst
200end
201
202fn art_compute_id bytes view uchar * dst edit uchar * to void
203so = get art_meta 10
204art_id_from_unsigned_bytes part bytes 0 so dst
205end
206
207fn art_payload_digest_slice payload view uchar * dst edit uchar * to void
208dlen = sha2_digest 256 payload dst
209if ne dlen 32
210trap 8
211end
212end
213
214fn art_requested_cap_digest data view uchar * dst edit uchar * to void
215art_sha256_dom art_req_dom_pfx data dst
216end
217
218fn art_granted_cap_digest data view uchar * dst edit uchar * to void
219art_sha256_dom art_grant_dom_pfx data dst
220end
221
222fn art_resource_envelope_digest data view uchar * dst edit uchar * to void
223art_sha256_dom art_res_dom_pfx data dst
224end
225
226fn art_policy_digest data view uchar * dst edit uchar * to void
227art_sha256_dom art_pol_dom_pfx data dst
228end
229
230fn art_machine_id_digest data view uchar * dst edit uchar * to void
231art_sha256_dom art_mach_dom_pfx data dst
232end
233
234fn art_verifier_identity_digest data view uchar * dst edit uchar * to void
235art_sha256_dom art_ver_dom_pfx data dst
236end
237
238fn art_signer_fingerprint pk view uchar * dst edit uchar * to void
239dlen = sha2_digest 256 pk dst
240if ne dlen 32
241trap 8
242end
243end
244
245note CapabilityRequest encode / validate / decode / attenuation (capability.rs)
246fn art_cap_encode cap view ulong * dst edit uchar * to void
247for i ulong 48
248put dst i 0
249end
250art_put_u16_le dst 0 get cap 0
251art_put_u32_le dst 4 get cap 1
252art_put_u32_le dst 8 get cap 2
253art_put_u32_le dst 12 get cap 3
254art_put_u32_le dst 16 get cap 4
255art_put_u64_le dst 24 get cap 5
256art_put_u64_le dst 32 get cap 6
257art_put_u64_le dst 40 get cap 7
258end
259
260fn art_cap_validate cap view ulong * to int
261rk = get cap 0
262rid = get cap 1
263rts = get cap 2
264bk = get cap 3
265mops = get cap 4
266mb = get cap 5
267if and ne rk ART_RESOURCE_KIND_OBJECT ne rk ART_RESOURCE_KIND_CHANNEL
268ret ART_ERR_MALFORMED_CAPABILITY
269end
270if or eq rid 0 eq rts 0
271ret ART_ERR_MALFORMED_CAPABILITY
272end
273if ne and rts not ART_VALID_RIGHTS 0
274ret ART_ERR_MALFORMED_CAPABILITY
275end
276if or eq mops 0 eq mb 0
277ret ART_ERR_MALFORMED_CAPABILITY
278end
279boff = get cap 6
280blen = get cap 7
281if and eq rk ART_RESOURCE_KIND_OBJECT eq bk 1
282bend = add boff blen
283if lt bend boff
284ret ART_ERR_LENGTH_OVERFLOW
285end
286if or eq blen 0 gt mb blen
287ret ART_ERR_MALFORMED_CAPABILITY
288end
289ret ART_OK
290end
291if and eq rk ART_RESOURCE_KIND_CHANNEL eq bk 2
292if or ne boff 0 ne blen 0
293ret ART_ERR_MALFORMED_CAPABILITY
294end
295ret ART_OK
296end
297ret ART_ERR_MALFORMED_CAPABILITY
298end
299
300fn art_cap_decode bytes view uchar * cap edit ulong * to int
301n = len bytes
302if lt n ART_CAP_RECORD_SIZE
303ret ART_ERR_TRUNCATED
304end
305if gt n ART_CAP_RECORD_SIZE
306ret ART_ERR_WRONG_LENGTH
307end
308if or ne art_get_u16_le bytes 2 0 ne art_get_u32_le bytes 20 0
309ret ART_ERR_RESERVED_NON_ZERO
310end
311put cap 0 art_get_u16_le bytes 0
312put cap 1 art_get_u32_le bytes 4
313put cap 2 art_get_u32_le bytes 8
314put cap 3 art_get_u32_le bytes 12
315put cap 4 art_get_u32_le bytes 16
316put cap 5 art_get_u64_le bytes 24
317put cap 6 art_get_u64_le bytes 32
318put cap 7 art_get_u64_le bytes 40
319ret art_cap_validate cap
320end
321
322fn art_cap_is_attenuation_of cand view ulong * req view ulong * to bool
323if ne art_cap_validate cand ART_OK
324ret false
325end
326if ne art_cap_validate req ART_OK
327ret false
328end
329if or ne get cand 0 get req 0 ne get cand 1 get req 1
330ret false
331end
332if ne get cand 3 get req 3
333ret false
334end
335if ne and get cand 2 not get req 2 0
336ret false
337end
338if or gt get cand 4 get req 4 gt get cand 5 get req 5
339ret false
340end
341bk = get cand 3
342if eq bk 1
343co = get cand 6
344cl = get cand 7
345ce = add co cl
346if lt ce co
347ret false
348end
349ro = get req 6
350rl = get req 7
351re = add ro rl
352if lt re ro
353ret false
354end
355ret and ge co ro le ce re
356end
357if eq bk 2
358ret and eq get cand 6 0 eq get cand 7 0
359end
360ret false
361end
362
363note ResourceEnvelope validate / decode / capacity (resource.rs)
364fn art_res_validate env view ulong * req_caps ulong to int
365cp = get env 0
366dp = get env 1
367sp = get env 2
368mc = get env 3
369if or eq cp 0 gt cp ART_MAX_CODE_PAGES
370ret ART_ERR_RESOURCE_LIMIT
371end
372if or eq dp 0 gt dp ART_MAX_DATA_PAGES
373ret ART_ERR_RESOURCE_LIMIT
374end
375if or eq sp 0 gt sp ART_MAX_STACK_PAGES
376ret ART_ERR_RESOURCE_LIMIT
377end
378if or lt mc req_caps gt mc ART_MAX_CAPABILITIES
379ret ART_ERR_RESOURCE_LIMIT
380end
381if or gt get env 4 ART_MAX_IPC_MESSAGES gt get env 5 ART_MAX_IPC_BYTES
382ret ART_ERR_RESOURCE_LIMIT
383end
384ct = get env 6
385et = get env 7
386sc = get env 8
387if or eq ct 0 gt ct ART_MAX_CPU_TICKS
388ret ART_ERR_RESOURCE_LIMIT
389end
390if or eq et 0 gt et ART_MAX_ELAPSED_TICKS
391ret ART_ERR_RESOURCE_LIMIT
392end
393if or eq sc 0 gt sc ART_MAX_SYSCALLS
394ret ART_ERR_RESOURCE_LIMIT
395end
396ret ART_OK
397end
398
399fn art_res_decode bytes view uchar * req_caps ulong env edit ulong * to int
400n = len bytes
401if lt n ART_RESOURCE_ENVELOPE_SIZE
402ret ART_ERR_TRUNCATED
403end
404if gt n ART_RESOURCE_ENVELOPE_SIZE
405ret ART_ERR_WRONG_LENGTH
406end
407if or ne art_get_u16_le bytes 14 0 ne art_get_u32_le bytes 44 0
408ret ART_ERR_RESERVED_NON_ZERO
409end
410put env 0 art_get_u32_le bytes 0
411put env 1 art_get_u32_le bytes 4
412put env 2 art_get_u32_le bytes 8
413put env 3 art_get_u16_le bytes 12
414put env 4 art_get_u32_le bytes 16
415put env 5 art_get_u32_le bytes 20
416put env 6 art_get_u64_le bytes 24
417put env 7 art_get_u64_le bytes 32
418put env 8 art_get_u32_le bytes 40
419ret art_res_validate env req_caps
420end
421
422fn art_res_code_cap_bytes env view ulong * to ulong
423ret mul get env 0 ART_PAGE_SIZE
424end
425
426fn art_res_data_cap_bytes env view ulong * to ulong
427ret mul get env 1 ART_PAGE_SIZE
428end
429
430note SignatureBlock decode (signature.rs)
431fn art_sig_block_decode bytes view uchar * fp_out edit uchar * sig_out edit uchar * to int
432n = len bytes
433if lt n ART_SIGNATURE_BLOCK_SIZE
434ret ART_ERR_TRUNCATED
435end
436if gt n ART_SIGNATURE_BLOCK_SIZE
437ret ART_ERR_WRONG_LENGTH
438end
439alg = art_get_u16_le bytes 0
440if ne alg ART_SIG_ALG_ED25519
441ret ART_ERR_BAD_SIGNATURE_FORMAT
442end
443if or ne get bytes 2 0 ne get bytes 3 0
444ret ART_ERR_RESERVED_NON_ZERO
445end
446for i ulong 32
447put fp_out i get bytes add 4 i
448end
449for i ulong 64
450put sig_out i get bytes add 36 i
451end
452ret ART_OK
453end
454
455note Section decode and validation (format.rs)
456fn art_decode_section bytes view uchar * off ulong slot ulong to int
457if ne art_get_u32_le bytes add off 4 0
458ret ART_ERR_RESERVED_NON_ZERO
459end
460if not art_all_zero bytes add off 24 8
461ret ART_ERR_RESERVED_NON_ZERO
462end
463base = mul slot 6
464put art_sections base art_get_u16_le bytes off
465put art_sections add base 1 art_get_u16_le bytes add off 2
466put art_sections add base 2 art_get_u32_le bytes add off 8
467put art_sections add base 3 art_get_u32_le bytes add off 12
468put art_sections add base 4 art_get_u32_le bytes add off 16
469put art_sections add base 5 art_get_u32_le bytes add off 20
470ret ART_OK
471end
472
473fn art_validate_sections payload_len ulong to int
474cf = get art_sections 3
475if or ne get art_sections 0 ART_SECTION_KIND_CODE ne get art_sections 1 ART_PERM_RX
476ret ART_ERR_BAD_SECTION
477end
478if or ne get art_sections 2 0 eq cf 0
479ret ART_ERR_BAD_SECTION
480end
481if or ne get art_sections 4 cf ne get art_sections 5 ART_PAGE_SIZE
482ret ART_ERR_BAD_SECTION
483end
484df = get art_sections 9
485if or ne get art_sections 6 ART_SECTION_KIND_DATA ne get art_sections 7 ART_PERM_RW
486ret ART_ERR_BAD_SECTION
487end
488if or ne get art_sections 8 cf eq df 0
489ret ART_ERR_BAD_SECTION
490end
491if or lt get art_sections 10 df ne get art_sections 11 ART_PAGE_SIZE
492ret ART_ERR_BAD_SECTION
493end
494end_p = add cf df
495if lt end_p cf
496ret ART_ERR_LENGTH_OVERFLOW
497end
498if ne end_p payload_len
499ret ART_ERR_SECTION_OVERLAP
500end
501ret ART_OK
502end
503
504note Header validation helper for art_parse (keeps register pressure low)
505fn art_parse_header bytes view uchar * to int
506n = len bytes
507if lt n ART_HEADER_SIZE
508ret ART_ERR_TRUNCATED
509end
510if gt n ART_MAX_ARTIFACT_SIZE
511ret ART_ERR_RESOURCE_LIMIT
512end
513for i ulong 7
514if ne get bytes i get art_magic_pfx i
515ret ART_ERR_BAD_MAGIC
516end
517end
518if ne get bytes 7 0
519ret ART_ERR_BAD_MAGIC
520end
521if or ne art_get_u16_le bytes 8 ART_FORMAT_VERSION ne art_get_u16_le bytes 10 ART_HEADER_SIZE
522ret ART_ERR_UNSUPPORTED_VERSION
523end
524ta = art_get_u16_le bytes 12
525if ne ta ART_TARGET_AARCH64_LE
526ret ART_ERR_WRONG_TARGET
527end
528av = art_get_u16_le bytes 14
529if ne av ART_ABI_V1
530ret ART_ERR_WRONG_ABI
531end
532if ne art_get_u32_le bytes 16 0
533ret ART_ERR_UNSUPPORTED_FLAGS
534end
535tlen = art_get_u32_le bytes 20
536if ne tlen n
537ret ART_ERR_WRONG_LENGTH
538end
539if or ne art_get_u32_le bytes 24 ART_SECTION_TABLE_OFFSET ne art_get_u16_le bytes 28 ART_SECTION_COUNT
540ret ART_ERR_UNSUPPORTED_VERSION
541end
542if or ne art_get_u16_le bytes 30 ART_SECTION_RECORD_SIZE ne art_get_u16_le bytes 32 0
543ret ART_ERR_UNSUPPORTED_VERSION
544end
545if ne art_get_u16_le bytes 34 0
546ret ART_ERR_UNSUPPORTED_VERSION
547end
548if or ne art_get_u32_le bytes 40 ART_CAPABILITY_TABLE_OFFSET ne art_get_u16_le bytes 46 ART_CAP_RECORD_SIZE
549ret ART_ERR_UNSUPPORTED_VERSION
550end
551if or ne art_get_u16_le bytes 52 ART_RESOURCE_ENVELOPE_SIZE ne art_get_u16_le bytes 54 0
552ret ART_ERR_UNSUPPORTED_VERSION
553end
554if not art_all_zero bytes 72 56
555ret ART_ERR_RESERVED_NON_ZERO
556end
557put art_meta 0 ta
558put art_meta 1 av
559put art_meta 2 art_get_u32_le bytes 36
560ret ART_OK
561end
562
563note Layout offsets check helper for art_parse
564fn art_parse_layout bytes view uchar * to int
565n = len bytes
566cc = art_get_u16_le bytes 44
567if gt cc ART_MAX_CAPABILITIES
568ret ART_ERR_RESOURCE_LIMIT
569end
570clen = mul cc ART_CAP_RECORD_SIZE
571roff = art_get_u32_le bytes 48
572if ne roff add ART_CAPABILITY_TABLE_OFFSET clen
573ret ART_ERR_SECTION_OVERLAP
574end
575rend = add roff ART_RESOURCE_ENVELOPE_SIZE
576poff = art_get_u32_le bytes 56
577if ne poff and add rend 15 not ulong 15
578ret ART_ERR_SECTION_OVERLAP
579end
580if gt poff n
581ret ART_ERR_TRUNCATED
582end
583if not art_all_zero bytes rend sub poff rend
584ret ART_ERR_RESERVED_NON_ZERO
585end
586plen = art_get_u32_le bytes 60
587if gt plen ART_MAX_PAYLOAD_SIZE
588ret ART_ERR_RESOURCE_LIMIT
589end
590soff = art_get_u32_le bytes 64
591if or ne art_get_u16_le bytes 68 ART_SIGNATURE_BLOCK_SIZE ne art_get_u16_le bytes 70 ART_SIG_ALG_ED25519
592ret ART_ERR_BAD_SIGNATURE_FORMAT
593end
594if ne soff add poff plen
595ret ART_ERR_SECTION_OVERLAP
596end
597if ne add soff ART_SIGNATURE_BLOCK_SIZE n
598ret ART_ERR_WRONG_LENGTH
599end
600put art_meta 3 ART_SIG_ALG_ED25519
601put art_meta 4 cc
602put art_meta 5 ART_CAPABILITY_TABLE_OFFSET
603put art_meta 6 clen
604put art_meta 7 roff
605put art_meta 8 poff
606put art_meta 9 plen
607put art_meta 10 soff
608put art_meta 11 ART_TRUST_TIER_NONE
609ret ART_OK
610end
611
612note Parse capability table into art_caps with strict (resource_kind, resource_id) ordering
613fn art_parse_caps bytes view uchar * cc ulong to int
614prev_key = ulong 0
615tmp = new ulong 8 0
616for idx ulong cc bound 16
617rc = art_cap_decode part bytes add ART_CAPABILITY_TABLE_OFFSET mul idx ART_CAP_RECORD_SIZE ART_CAP_RECORD_SIZE tmp
618if ne rc ART_OK
619ret rc
620end
621key = or shl get tmp 0 32 get tmp 1
622if le key prev_key
623ret ART_ERR_MALFORMED_CAPABILITY
624end
625prev_key = key
626for f ulong 8
627put art_caps add mul idx 8 f get tmp f
628end
629end
630ret ART_OK
631end
632
633note Strict fail-closed Binary Artifact v0 parser (Artifact::parse)
634fn art_parse bytes view uchar * to int
635rc = art_parse_header bytes
636if ne rc ART_OK
637ret rc
638end
639rc = art_parse_layout bytes
640if ne rc ART_OK
641ret rc
642end
643rc = art_decode_section bytes 128 0
644if ne rc ART_OK
645ret rc
646end
647rc = art_decode_section bytes 160 1
648if ne rc ART_OK
649ret rc
650end
651rc = art_validate_sections get art_meta 9
652if ne rc ART_OK
653ret rc
654end
655rc = art_parse_caps bytes get art_meta 4
656if ne rc ART_OK
657ret rc
658end
659env = new ulong 9 0
660rc = art_res_decode part bytes get art_meta 7 ART_RESOURCE_ENVELOPE_SIZE get art_meta 4 env
661if ne rc ART_OK
662ret rc
663end
664for i ulong 9
665put art_res i get env i
666end
667if or gt get art_sections 4 art_res_code_cap_bytes env gt get art_sections 10 art_res_data_cap_bytes env
668ret ART_ERR_RESOURCE_LIMIT
669end
670if ne and get art_meta 2 3 0
671ret ART_ERR_BAD_ENTRY_POINT
672end
673if gt add get art_meta 2 4 0xffffffff
674ret ART_ERR_LENGTH_OVERFLOW
675end
676if gt add get art_meta 2 4 get art_sections 3
677ret ART_ERR_BAD_ENTRY_POINT
678end
679fp = new uchar 32 0
680sg = new uchar 64 0
681rc = art_sig_block_decode part bytes get art_meta 10 ART_SIGNATURE_BLOCK_SIZE fp sg
682if ne rc ART_OK
683ret rc
684end
685for i ulong 32
686put art_signer_fp i get fp i
687end
688for i ulong 64
689put art_sig i get sg i
690end
691ret ART_OK
692end
693
694note Parse and compute ArtifactId + payload_digest (verify.rs: parse_and_identify)
695fn art_parse_and_identify bytes view uchar * to int
696if gt len bytes ART_MAX_ARTIFACT_SIZE
697ret ART_ERR_RESOURCE_LIMIT
698end
699rc = art_parse bytes
700if ne rc ART_OK
701ret rc
702end
703id_buf = new uchar 32 0
704art_compute_id bytes id_buf
705for i ulong 32
706put art_id i get id_buf i
707end
708pd_buf = new uchar 32 0
709poff = get art_meta 8
710plen = get art_meta 9
711art_payload_digest_slice part bytes poff plen pd_buf
712for i ulong 32
713put art_payload_dig i get pd_buf i
714end
715ret ART_OK
716end
717
718note Ed25519 scalar multiplication [s]B (256 bits) for RFC 8032 keygen and signing
719fn art_ed_smul_b w edit ulong 1024 s view uchar 32 to void
720edfcpy w edq edzero 64
721edfcpy w add edq 16 edone 16
722edfcpy w add edq 32 edone 16
723for j ulong 256
724i = sub 255 j
725edpdbl w edq
726if eq edbit s i 1
727edpadd w edq edbp
728end
729end
730end
731
732note Compute S = (r + k * a) mod L (32-byte little-endian scalars)
733fn art_ed_muladd_l r view uchar 32 k view uchar 32 a view uchar 32 dst edit uchar 32 to void
734x = new long 64 0
735for i ulong 32
736put x i long get r i
737end
738for i ulong 32
739ki = long get k i
740for j ulong 32
741idx = add i j
742put x idx add get x idx mul ki long get a j
743end
744end
745carry = long 0
746for i ulong 64
747v = add get x i carry
748put x i and v 255
749carry = shr v 8
750end
751edmodl x
752for i ulong 32
753put dst i uchar get x i
754end
755end
756
757note Derive RFC 8032 Ed25519 public key from 32-byte seed
758fn art_ed25519_pubkey seed view uchar * pk_out edit uchar * to void
759h = new uchar 64 0
760dlen = sha2_digest 512 part seed 0 32 h
761if ne dlen 64
762trap 8
763end
764a = new uchar 32 0
765for i ulong 32
766put a i get h i
767end
768put a 0 and get a 0 248
769put a 31 or and get a 31 127 64
770w = new ulong 1024 0
771edinit w
772art_ed_smul_b w a
773pk32 = new uchar 32 0
774edpenc w edq pk32
775for i ulong 32
776put pk_out i get pk32 i
777end
778end
779
780note Reduce 64-byte SHA-512 digest modulo L into 32-byte scalar
781fn art_ed_reduce64 hd view uchar 64 dst edit uchar 32 to void
782x = new long 64 0
783for i ulong 64
784put x i long get hd i
785end
786edmodl x
787for i ulong 32
788put dst i uchar get x i
789end
790end
791
792note Helper: compute r = SHA512(prefix || msg) mod L and R = encode([r]B)
793fn art_ed_sign_step1 pfx view uchar 32 msg view uchar * r_out edit uchar 32 rb_out edit uchar 32 to void
794ctx = new ulong 40 0
795sha512_init ctx
796sha512_update ctx pfx
797sha512_update ctx msg
798hd = new uchar 64 0
799sha512_final ctx hd
800art_ed_reduce64 hd r_out
801w = new ulong 1024 0
802edinit w
803art_ed_smul_b w r_out
804edpenc w edq rb_out
805end
806
807note Helper: compute k = SHA512(R || A || msg) mod L
808fn art_ed_sign_step2 rb view uchar 32 pk view uchar 32 msg view uchar * k_out edit uchar 32 to void
809ctx = new ulong 40 0
810sha512_init ctx
811sha512_update ctx rb
812sha512_update ctx pk
813sha512_update ctx msg
814hd = new uchar 64 0
815sha512_final ctx hd
816art_ed_reduce64 hd k_out
817end
818
819note Helper: expand 32-byte seed into clamped scalar a, 32-byte prefix, and 32-byte public key
820fn art_ed_expand seed view uchar * a_out edit uchar 32 pfx_out edit uchar 32 pk_out edit uchar 32 to void
821h = new uchar 64 0
822dlen = sha2_digest 512 part seed 0 32 h
823if ne dlen 64
824trap 8
825end
826for i ulong 32
827put a_out i get h i
828put pfx_out i get h add 32 i
829end
830put a_out 0 and get a_out 0 248
831put a_out 31 or and get a_out 31 127 64
832w = new ulong 1024 0
833edinit w
834art_ed_smul_b w a_out
835edpenc w edq pk_out
836end
837
838note RFC 8032 pure Ed25519 signing (seed[0..32], msg, sig_out[0..64])
839fn art_ed25519_sign seed view uchar * msg view uchar * sig_out edit uchar * to void
840a = new uchar 32 0
841pfx = new uchar 32 0
842pk = new uchar 32 0
843art_ed_expand seed a pfx pk
844r = new uchar 32 0
845rb = new uchar 32 0
846art_ed_sign_step1 pfx msg r rb
847k = new uchar 32 0
848art_ed_sign_step2 rb pk msg k
849art_ed_muladd_l r k a pfx
850for i ulong 32
851put sig_out i get rb i
852put sig_out add 32 i get pfx i
853end
854end
855
856note RFC 8032 Ed25519 strict verification (mode 0) via protocol/omnet/om/ed25519/ed25519.om
857fn art_ed25519_verify pk view uchar * msg view uchar * sig view uchar * to bool
858if or ne len pk 32 ne len sig 64
859ret false
860end
861mlen = len msg
862if gt mlen 65536
863ret false
864end
865pk32 = new uchar 32 0
866for i ulong 32
867put pk32 i get pk i
868end
869sig64 = new uchar 64 0
870for i ulong 64
871put sig64 i get sig i
872end
873mbuf = new uchar 65536 0
874for i ulong mlen bound 65536
875put mbuf i get msg i
876end
877ret eq edverify pk32 mbuf mlen sig64 0 1
878end
879
880note Build 61-byte artifact signature message: "AIENOS-ARTIFACT-SIGNATURE-V1\0" || artifact_id
881fn art_signature_message aid view uchar * msg_out edit uchar * to void
882for i ulong 28
883put msg_out i get art_sig_dom_pfx i
884end
885put msg_out 28 0
886for i ulong 32
887put msg_out add 29 i get aid i
888end
889end
890
891note Trust anchor management (EmptyTrustAnchorSet / ProductionTrustAnchorSet / Seed0bTestAnchorSet)
892fn art_anchors_clear to void
893put art_anchor_cnt 0 0
894end
895
896fn art_anchors_add pk view uchar * tier ulong to int
897if ne len pk 32
898ret ART_ERR_BAD_SIGNATURE_FORMAT
899end
900cnt = get art_anchor_cnt 0
901if ge cnt ART_MAX_ANCHORS
902ret ART_ERR_RESOURCE_LIMIT
903end
904base = mul cnt 32
905for i ulong 32
906put art_anchor_pks add base i get pk i
907end
908put art_anchor_tiers cnt tier
909put art_anchor_cnt 0 add cnt 1
910ret ART_OK
911end
912
913note Populate RFC 8032 TEST 1 public key (Seed0bTestAnchorSet, TEST ONLY)
914fn art_seed0b_test_pubkey dst edit uchar * to void
915put dst 0 0xd7
916put dst 1 0x5a
917put dst 2 0x98
918put dst 3 0x01
919put dst 4 0x82
920put dst 5 0xb1
921put dst 6 0x0a
922put dst 7 0xb7
923put dst 8 0xd5
924put dst 9 0x4b
925put dst 10 0xfe
926put dst 11 0xd3
927put dst 12 0xc9
928put dst 13 0x64
929put dst 14 0x07
930put dst 15 0x3a
931put dst 16 0x0e
932put dst 17 0xe1
933put dst 18 0x72
934put dst 19 0xf3
935put dst 20 0xda
936put dst 21 0xa6
937put dst 22 0x23
938put dst 23 0x25
939put dst 24 0xaf
940put dst 25 0x02
941put dst 26 0x1a
942put dst 27 0x68
943put dst 28 0xf7
944put dst 29 0x07
945put dst 30 0x51
946put dst 31 0x1a
947end
948
949fn art_anchors_add_seed0b_test to int
950pk = new uchar 32 0
951art_seed0b_test_pubkey pk
952ret art_anchors_add pk ART_TRUST_TIER_SEED0B_QUAL
953end
954
955note Find trusted anchor by SHA-256 fingerprint; writes 32-byte pk to pk_out and returns tier (or 0)
956fn art_anchors_find fp view uchar * pk_out edit uchar * to ulong
957cnt = get art_anchor_cnt 0
958cand = new uchar 32 0
959cfp = new uchar 32 0
960for idx ulong cnt bound 8
961base = mul idx 32
962for i ulong 32
963put cand i get art_anchor_pks add base i
964end
965art_signer_fingerprint cand cfp
966if art_bytes_eq fp cfp
967for i ulong 32
968put pk_out i get cand i
969end
970ret get art_anchor_tiers idx
971end
972end
973ret ART_TRUST_TIER_NONE
974end
975
976note ConfiguredArtifactVerifier::verify
977fn art_verify bytes view uchar * to int
978rc = art_parse_and_identify bytes
979if ne rc ART_OK
980ret rc
981end
982fp = new uchar 32 0
983for i ulong 32
984put fp i get art_signer_fp i
985end
986pk = new uchar 32 0
987tier = art_anchors_find fp pk
988if eq tier ART_TRUST_TIER_NONE
989ret ART_ERR_UNTRUSTED_SIGNER
990end
991chk_fp = new uchar 32 0
992art_signer_fingerprint pk chk_fp
993if not art_bytes_eq chk_fp fp
994ret ART_ERR_UNTRUSTED_SIGNER
995end
996aid = new uchar 32 0
997for i ulong 32
998put aid i get art_id i
999end
1000msg = new uchar 61 0
1001art_signature_message aid msg
1002sg = new uchar 64 0
1003for i ulong 64
1004put sg i get art_sig i
1005end
1006if not art_ed25519_verify pk msg sg
1007ret ART_ERR_BAD_SIGNATURE
1008end
1009put art_meta 11 tier
1010ret ART_OK
1011end