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

585 lines · 14198 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-artifact/receipt.om: Pure-Omega Admission Receipt v0 (ADR 0014 s7)
3note 512-byte canonical encoder, strict fail-closed decoder, semantic validator,
4note nonce/identity builders, and Ed25519 signing/verification.
5note Converted from aienux crates/aienos-artifact/src/receipt.rs.
6note Compose after: 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
7
8const RCP_VERSION ulong 0
9const RCP_HEADER_SIZE ulong 96
10const RCP_SIZE ulong 512
11const RCP_SIG_OFFSET ulong 400
12const RCP_SIG_MSG_SIZE ulong 70
13
14note Presence flags (offset 16)
15const RCP_FLAG_MACHINE_ID ulong 1
16const RCP_FLAG_CONTEXT_ID ulong 2
17const RCP_FLAG_TIMESTAMP ulong 4
18const RCP_VALID_FLAGS ulong 7
19
20note Result flags (offset 72)
21const RCP_RES_READ_OK ulong 1
22const RCP_RES_WRITE_DENIED ulong 2
23const RCP_RES_RECLAIMED ulong 4
24const RCP_RES_CANARY_PASSED ulong 8
25const RCP_RES_FORGED_DENIED ulong 16
26const RCP_RES_MAPPED_BYTES_MATCH ulong 32
27const RCP_RES_EXECUTED_BYTES_MATCH ulong 64
28const RCP_RES_WX_SEALED ulong 128
29const RCP_VALID_RES_FLAGS ulong 255
30
31note ReceiptDecision enum values
32const RCP_DEC_ADMITTED ulong 1
33const RCP_DEC_REJECTED ulong 2
34const RCP_DEC_CANARY_FAILED ulong 3
35const RCP_DEC_DESTROYED ulong 4
36
37note QualificationTier enum values
38const RCP_TIER_QEMU ulong 1
39const RCP_TIER_MACHINE1 ulong 2
40const RCP_TIER_PI5 ulong 3
41
42note ExecutionStatusCode enum values
43const RCP_EXEC_NOT_RUN ulong 0
44const RCP_EXEC_EXITED ulong 1
45const RCP_EXEC_TIMEOUT ulong 2
46const RCP_EXEC_FAULT ulong 3
47const RCP_EXEC_BAD_SYSCALL ulong 4
48const RCP_EXEC_RESOURCE_OVERRUN ulong 5
49const RCP_EXEC_CANARY_FAILED ulong 6
50
51note Stage and loader reason bounds
52const RCP_MAX_REJECTION_STAGE ulong 9
53const RCP_LOADER_REASON_FIRST ulong 0x101
54const RCP_LOADER_REASON_LAST ulong 0x10a
55const RCP_MAX_ANCHORS ulong 8
56
57note Domain and label strings (domains get trailing NUL byte when hashed/framed)
58text rcp_magic_pfx "AIENRCP"
59text rcp_sig_dom_pfx "AIENOS-ADMISSION-RECEIPT-SIGNATURE-V1"
60text rcp_dig_dom_pfx "AIENOS-ADMISSION-RECEIPT-V1"
61text rcp_nonce_dom_pfx "AIENOS-ADMISSION-RECEIPT-NONCE-V1"
62text rcp_lbl_qemu "SEED-0B-QEMU"
63text rcp_lbl_machine1 "SEED-0B-MACHINE1"
64text rcp_lbl_pi5 "SEED-0B-PI5"
65
66note Working receipt record in globals
67note rcp_scalars slots:
68note 0=flags, 1=decision, 2=tier, 3=sequence, 4=observed_time_ns,
69note 5=execution_status, 6=result_flags, 7=rejection_stage, 8=rejection_reason,
70note 9=syscalls, 10=object_reads_ok, 11=denials, 12=frames_reserved,
71note 13=generation, 14=context_id
72global rcp_scalars ulong 15
73global rcp_exit_status int 1
74global rcp_nonce_buf uchar 16
75note 9 digests * 32 bytes = 288 bytes:
76note 0: artifact_id (offset 96)
77note 1: payload_digest (offset 128)
78note 2: artifact_signer_fingerprint (offset 160)
79note 3: policy_digest (offset 192)
80note 4: requested_capability_digest (offset 224)
81note 5: granted_capability_digest (offset 256)
82note 6: resource_envelope_digest (offset 288)
83note 7: verifier_identity (offset 320)
84note 8: machine_id_digest (offset 352)
85global rcp_digests uchar 288
86
87note Receipt trust anchor table (distinct from artifact anchors)
88global rcp_anchor_cnt ulong 1
89global rcp_anchor_pks uchar 256
90
91fn rcp_clear to void
92for i ulong 15
93put rcp_scalars i 0
94end
95put rcp_exit_status 0 0
96for i ulong 16
97put rcp_nonce_buf i 0
98end
99for i ulong 288
100put rcp_digests i 0
101end
102end
103
104fn rcp_set_digest idx ulong src view uchar * to void
105base = mul idx 32
106for i ulong 32
107put rcp_digests add base i get src i
108end
109end
110
111fn rcp_get_digest idx ulong dst edit uchar * to void
112base = mul idx 32
113for i ulong 32
114put dst i get rcp_digests add base i
115end
116end
117
118fn rcp_put_i32_le buf edit uchar * off ulong v int to void
119u = ulong 0
120if ge v 0
121u = ulong v
122else
123u = ulong add long v 4294967296
124end
125art_put_u32_le buf off u
126end
127
128fn rcp_get_i32_le buf view uchar * off ulong to int
129u = art_get_u32_le buf off
130if ge u 2147483648
131ret int sub long u 4294967296
132end
133ret int u
134end
135
136note Canonical 512-byte encoding with all-zero signature block (receipt::encode)
137fn rcp_encode dst edit uchar * to int
138if lt len dst RCP_SIZE
139ret ART_ERR_TRUNCATED
140end
141for i ulong 512
142put dst i 0
143end
144for i ulong 7
145put dst i get rcp_magic_pfx i
146end
147put dst 7 0
148art_put_u16_le dst 8 RCP_VERSION
149art_put_u16_le dst 10 RCP_HEADER_SIZE
150art_put_u32_le dst 12 RCP_SIZE
151art_put_u32_le dst 16 get rcp_scalars 0
152art_put_u16_le dst 20 get rcp_scalars 1
153art_put_u16_le dst 22 get rcp_scalars 2
154art_put_u64_le dst 24 get rcp_scalars 3
155for i ulong 16
156put dst add 32 i get rcp_nonce_buf i
157end
158art_put_u64_le dst 48 get rcp_scalars 4
159art_put_u32_le dst 64 get rcp_scalars 5
160rcp_put_i32_le dst 68 get rcp_exit_status 0
161art_put_u32_le dst 72 get rcp_scalars 6
162art_put_u16_le dst 76 get rcp_scalars 7
163art_put_u16_le dst 78 get rcp_scalars 8
164art_put_u32_le dst 80 get rcp_scalars 9
165art_put_u32_le dst 84 get rcp_scalars 10
166art_put_u32_le dst 88 get rcp_scalars 11
167art_put_u32_le dst 92 get rcp_scalars 12
168for i ulong 288
169put dst add 96 i get rcp_digests i
170end
171art_put_u64_le dst 384 get rcp_scalars 13
172art_put_u64_le dst 392 get rcp_scalars 14
173ret ART_OK
174end
175
176note SHA256("AIENOS-ADMISSION-RECEIPT-V1\0" || bytes[0..400])
177fn rcp_digest bytes view uchar * dst edit uchar * to void
178art_sha256_dom rcp_dig_dom_pfx part bytes 0 RCP_SIG_OFFSET dst
179end
180
181note Deterministic per-context nonce: first 16 bytes of SHA256(NONCE_DOMAIN || verifier_id || seq_le)
182fn rcp_nonce verifier_id view uchar * seq ulong dst edit uchar * to void
183ctx = new ulong 40 0
184nul = new uchar 1 0
185seqb = new uchar 8 0
186art_put_u64_le seqb 0 seq
187sha256_init ctx
188sha256_update ctx rcp_nonce_dom_pfx
189sha256_update ctx nul
190sha256_update ctx part verifier_id 0 32
191sha256_update ctx seqb
192dig = new uchar 32 0
193sha256_final ctx dig
194for i ulong 16
195put dst i get dig i
196end
197end
198
199note Canonical 45-byte verifier build identity (returns 45 on valid 40-char lowercase hex commit, else -1)
200fn rcp_verifier_identity_bytes commit view uchar * target ulong abi ulong feat uchar dst edit uchar 45 to int
201if ne len commit 40
202ret -1
203end
204for i ulong 40
205c = get commit i
206is_dig = and ge c 48 le c 57
207is_af = and ge c 97 le c 102
208if not or is_dig is_af
209ret -1
210end
211put dst i c
212end
213art_put_u16_le dst 40 target
214art_put_u16_le dst 42 abi
215put dst 44 feat
216ret 45
217end
218
219fn rcp_tier_label_copy tier ulong dst edit uchar * to ulong
220if eq tier RCP_TIER_QEMU
221n = len rcp_lbl_qemu
222for i ulong n bound 32
223put dst i get rcp_lbl_qemu i
224end
225ret n
226end
227if eq tier RCP_TIER_MACHINE1
228n = len rcp_lbl_machine1
229for i ulong n bound 32
230put dst i get rcp_lbl_machine1 i
231end
232ret n
233end
234if eq tier RCP_TIER_PI5
235n = len rcp_lbl_pi5
236for i ulong n bound 32
237put dst i get rcp_lbl_pi5 i
238end
239ret n
240end
241ret 0
242end
243
244fn rcp_valid_reason code ulong to bool
245if and ge code 1 le code 19
246ret true
247end
248if and ge code RCP_LOADER_REASON_FIRST le code RCP_LOADER_REASON_LAST
249ret true
250end
251ret false
252end
253
254fn rcp_machine_id_is_zero to bool
255for i ulong 32
256if ne get rcp_digests add 256 i 0
257ret false
258end
259end
260ret true
261end
262
263note s7.1 / s7.3 semantic validation of working receipt
264fn rcp_validate to int
265if eq and get rcp_scalars 0 RCP_FLAG_MACHINE_ID 0
266if not rcp_machine_id_is_zero
267ret ART_ERR_MALFORMED_RECEIPT
268end
269end
270if eq and get rcp_scalars 0 RCP_FLAG_CONTEXT_ID 0
271if or ne get rcp_scalars 13 0 ne get rcp_scalars 14 0
272ret ART_ERR_MALFORMED_RECEIPT
273end
274end
275if eq and get rcp_scalars 0 RCP_FLAG_TIMESTAMP 0
276if ne get rcp_scalars 4 0
277ret ART_ERR_MALFORMED_RECEIPT
278end
279end
280rst = get rcp_scalars 7
281rrs = get rcp_scalars 8
282if gt rst RCP_MAX_REJECTION_STAGE
283ret ART_ERR_MALFORMED_RECEIPT
284end
285if ne rrs 0
286if not rcp_valid_reason rrs
287ret ART_ERR_MALFORMED_RECEIPT
288end
289end
290dec = get rcp_scalars 1
291es = get rcp_scalars 5
292ex = get rcp_exit_status 0
293rf = get rcp_scalars 6
294if eq dec RCP_DEC_REJECTED
295if or eq rst 0 eq rrs 0
296ret ART_ERR_MALFORMED_RECEIPT
297end
298if or ne es RCP_EXEC_NOT_RUN ne ex 0
299ret ART_ERR_MALFORMED_RECEIPT
300end
301if or ne get rcp_scalars 9 0 ne get rcp_scalars 10 0
302ret ART_ERR_MALFORMED_RECEIPT
303end
304if ne get rcp_scalars 11 0
305ret ART_ERR_MALFORMED_RECEIPT
306end
307if ne and rf not RCP_RES_RECLAIMED 0
308ret ART_ERR_MALFORMED_RECEIPT
309end
310else
311if or ne rst 0 ne rrs 0
312ret ART_ERR_MALFORMED_RECEIPT
313end
314end
315if ne eq dec RCP_DEC_CANARY_FAILED eq es RCP_EXEC_CANARY_FAILED
316ret ART_ERR_MALFORMED_RECEIPT
317end
318if ne and rf RCP_RES_CANARY_PASSED 0
319if or ne es RCP_EXEC_EXITED ne ex 0
320ret ART_ERR_MALFORMED_RECEIPT
321end
322end
323vid = new uchar 32 0
324rcp_get_digest 7 vid
325exp_n = new uchar 16 0
326rcp_nonce vid get rcp_scalars 3 exp_n
327for i ulong 16
328if ne get rcp_nonce_buf i get exp_n i
329ret ART_ERR_MALFORMED_RECEIPT
330end
331end
332ret ART_OK
333end
334
335note Strict fail-closed decode of 512-byte receipt record (receipt::decode)
336fn rcp_decode_header bytes view uchar * to int
337n = len bytes
338if lt n RCP_SIZE
339ret ART_ERR_TRUNCATED
340end
341if gt n RCP_SIZE
342ret ART_ERR_WRONG_LENGTH
343end
344for i ulong 7
345if ne get bytes i get rcp_magic_pfx i
346ret ART_ERR_BAD_MAGIC
347end
348end
349if ne get bytes 7 0
350ret ART_ERR_BAD_MAGIC
351end
352if or ne art_get_u16_le bytes 8 RCP_VERSION ne art_get_u16_le bytes 10 RCP_HEADER_SIZE
353ret ART_ERR_UNSUPPORTED_VERSION
354end
355if ne art_get_u32_le bytes 12 RCP_SIZE
356ret ART_ERR_UNSUPPORTED_VERSION
357end
358fl = art_get_u32_le bytes 16
359rf = art_get_u32_le bytes 72
360if or ne and fl not RCP_VALID_FLAGS 0 ne and rf not RCP_VALID_RES_FLAGS 0
361ret ART_ERR_UNSUPPORTED_FLAGS
362end
363if or not art_all_zero bytes 56 8 not art_all_zero bytes 500 12
364ret ART_ERR_RESERVED_NON_ZERO
365end
366if not art_all_zero bytes 400 100
367if ne art_get_u16_le bytes 400 ART_SIG_ALG_ED25519
368ret ART_ERR_BAD_SIGNATURE_FORMAT
369end
370if not art_all_zero bytes 402 2
371ret ART_ERR_RESERVED_NON_ZERO
372end
373end
374ret ART_OK
375end
376
377fn rcp_decode bytes view uchar * to int
378rc = rcp_decode_header bytes
379if ne rc ART_OK
380ret rc
381end
382dec = art_get_u16_le bytes 20
383if or lt dec 1 gt dec 4
384ret ART_ERR_MALFORMED_RECEIPT
385end
386tier = art_get_u16_le bytes 22
387if or lt tier 1 gt tier 3
388ret ART_ERR_MALFORMED_RECEIPT
389end
390es = art_get_u32_le bytes 64
391if gt es 6
392ret ART_ERR_MALFORMED_RECEIPT
393end
394put rcp_scalars 0 art_get_u32_le bytes 16
395put rcp_scalars 1 dec
396put rcp_scalars 2 tier
397put rcp_scalars 3 art_get_u64_le bytes 24
398for i ulong 16
399put rcp_nonce_buf i get bytes add 32 i
400end
401put rcp_scalars 4 art_get_u64_le bytes 48
402put rcp_scalars 5 es
403put rcp_exit_status 0 rcp_get_i32_le bytes 68
404put rcp_scalars 6 art_get_u32_le bytes 72
405put rcp_scalars 7 art_get_u16_le bytes 76
406put rcp_scalars 8 art_get_u16_le bytes 78
407put rcp_scalars 9 art_get_u32_le bytes 80
408put rcp_scalars 10 art_get_u32_le bytes 84
409put rcp_scalars 11 art_get_u32_le bytes 88
410put rcp_scalars 12 art_get_u32_le bytes 92
411for i ulong 288
412put rcp_digests i get bytes add 96 i
413end
414put rcp_scalars 13 art_get_u64_le bytes 384
415put rcp_scalars 14 art_get_u64_le bytes 392
416ret rcp_validate
417end
418
419fn rcp_is_unsigned bytes view uchar * to bool
420ret art_all_zero bytes RCP_SIG_OFFSET 112
421end
422
423note Build 70-byte receipt signature message: "AIENOS-ADMISSION-RECEIPT-SIGNATURE-V1\0" || digest
424fn rcp_signature_message dig view uchar * msg_out edit uchar * to void
425for i ulong 37
426put msg_out i get rcp_sig_dom_pfx i
427end
428put msg_out 37 0
429for i ulong 32
430put msg_out add 38 i get dig i
431end
432end
433
434note Sign a valid unsigned 512-byte receipt in place using 32-byte Ed25519 seed
435fn rcp_sign bytes edit uchar * seed view uchar * to int
436if ne len bytes RCP_SIZE
437ret ART_ERR_WRONG_LENGTH
438end
439if not rcp_is_unsigned bytes
440ret ART_ERR_BAD_SIGNATURE_FORMAT
441end
442rc = rcp_decode bytes
443if ne rc ART_OK
444ret rc
445end
446dig = new uchar 32 0
447rcp_digest bytes dig
448msg = new uchar 70 0
449rcp_signature_message dig msg
450pk = new uchar 32 0
451art_ed25519_pubkey seed pk
452fp = new uchar 32 0
453art_signer_fingerprint pk fp
454sg = new uchar 64 0
455art_ed25519_sign seed msg sg
456art_put_u16_le bytes 400 ART_SIG_ALG_ED25519
457put bytes 402 0
458put bytes 403 0
459for i ulong 32
460put bytes add 404 i get fp i
461end
462for i ulong 64
463put bytes add 436 i get sg i
464end
465ret ART_OK
466end
467
468note Receipt trust anchor set (distinct from artifact trust anchors)
469fn rcp_anchors_clear to void
470put rcp_anchor_cnt 0 0
471end
472
473fn rcp_anchors_add pk view uchar * to int
474if ne len pk 32
475ret ART_ERR_BAD_SIGNATURE_FORMAT
476end
477cnt = get rcp_anchor_cnt 0
478if ge cnt RCP_MAX_ANCHORS
479ret ART_ERR_RESOURCE_LIMIT
480end
481base = mul cnt 32
482for i ulong 32
483put rcp_anchor_pks add base i get pk i
484end
485put rcp_anchor_cnt 0 add cnt 1
486ret ART_OK
487end
488
489note Populate RFC 8032 TEST 2 public key (Seed0bTestReceiptAnchorSet, TEST ONLY)
490fn rcp_seed0b_test_pubkey dst edit uchar * to void
491put dst 0 0x3d
492put dst 1 0x40
493put dst 2 0x17
494put dst 3 0xc3
495put dst 4 0xe8
496put dst 5 0x43
497put dst 6 0x89
498put dst 7 0x5a
499put dst 8 0x92
500put dst 9 0xb7
501put dst 10 0x0a
502put dst 11 0xa7
503put dst 12 0x4d
504put dst 13 0x1b
505put dst 14 0x7e
506put dst 15 0xbc
507put dst 16 0x9c
508put dst 17 0x98
509put dst 18 0x2c
510put dst 19 0xcf
511put dst 20 0x2e
512put dst 21 0xc4
513put dst 22 0x96
514put dst 23 0x8c
515put dst 24 0xc0
516put dst 25 0xcd
517put dst 26 0x55
518put dst 27 0xf1
519put dst 28 0x2a
520put dst 29 0xf4
521put dst 30 0x66
522put dst 31 0x0c
523end
524
525fn rcp_anchors_add_seed0b_test to int
526pk = new uchar 32 0
527rcp_seed0b_test_pubkey pk
528ret rcp_anchors_add pk
529end
530
531fn rcp_anchors_find fp view uchar * pk_out edit uchar * to bool
532cnt = get rcp_anchor_cnt 0
533cand = new uchar 32 0
534cfp = new uchar 32 0
535for idx ulong cnt bound 8
536base = mul idx 32
537for i ulong 32
538put cand i get rcp_anchor_pks add base i
539end
540art_signer_fingerprint cand cfp
541if art_bytes_eq fp cfp
542for i ulong 32
543put pk_out i get cand i
544end
545ret true
546end
547end
548ret false
549end
550
551note Decode and authenticate a signed 512-byte receipt record (receipt::verify)
552fn rcp_verify bytes view uchar * to int
553rc = rcp_decode bytes
554if ne rc ART_OK
555ret rc
556end
557if rcp_is_unsigned bytes
558ret ART_ERR_BAD_SIGNATURE_FORMAT
559end
560fp = new uchar 32 0
561for i ulong 32
562put fp i get bytes add 404 i
563end
564pk = new uchar 32 0
565if not rcp_anchors_find fp pk
566ret ART_ERR_UNTRUSTED_SIGNER
567end
568chk_fp = new uchar 32 0
569art_signer_fingerprint pk chk_fp
570if not art_bytes_eq chk_fp fp
571ret ART_ERR_UNTRUSTED_SIGNER
572end
573sg = new uchar 64 0
574for i ulong 64
575put sg i get bytes add 436 i
576end
577dig = new uchar 32 0
578rcp_digest bytes dig
579msg = new uchar 70 0
580rcp_signature_message dig msg
581if not art_ed25519_verify pk msg sg
582ret ART_ERR_BAD_SIGNATURE
583end
584ret ART_OK
585end