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 / protocol / aien-protocols / ext / discovery_capsule.om

156 lines · 3744 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/aien-protocols/ext/discovery_capsule.om: pure-Omega port of aien-discovery-capsule
3note Implements recipient URI validation, capsule lifecycle state machine,
4note hop-limit & TTL routing enforcement, and canonical capsule digest verification.
5note Compose after si/directives/om2.om, om/tls/sha2.om, and protocol/aien-protocols/core/types.om.
6
7const DC_OK ulong 0
8const DC_ERR_RECIPIENT ulong 1
9const DC_ERR_TTL ulong 2
10const DC_ERR_HOPS ulong 3
11const DC_ERR_STATE ulong 4
12const DC_ERR_DIGEST ulong 5
13const DC_ERR_ID ulong 6
14
15const DC_STATE_DRAFT ulong 1
16const DC_STATE_SEALED ulong 2
17const DC_STATE_ROUTED ulong 3
18const DC_STATE_DELIVERED ulong 4
19const DC_STATE_EXPIRED ulong 5
20const DC_STATE_REVOKED ulong 6
21
22const DC_MAX_HOPS ulong 16
23const DC_MAX_TTL_S ulong 86400
24
25text dc_dom_cap "aien-dc:v1|"
26text dc_sep "|"
27text dc_pfx_did "did:"
28text dc_pfx_rad "rad:"
29text dc_pfx_node "node:"
30
31fn dc_state_valid s ulong to bool
32ret and ge s DC_STATE_DRAFT le s DC_STATE_REVOKED
33end
34
35fn dc_transition_check cur ulong nxt ulong to ulong
36if not dc_state_valid cur
37ret DC_ERR_STATE
38end
39if not dc_state_valid nxt
40ret DC_ERR_STATE
41end
42if eq cur DC_STATE_DRAFT
43if or eq nxt DC_STATE_SEALED eq nxt DC_STATE_REVOKED
44ret DC_OK
45end
46ret DC_ERR_STATE
47end
48if eq cur DC_STATE_SEALED
49if or eq nxt DC_STATE_ROUTED or eq nxt DC_STATE_EXPIRED eq nxt DC_STATE_REVOKED
50ret DC_OK
51end
52ret DC_ERR_STATE
53end
54if eq cur DC_STATE_ROUTED
55if or eq nxt DC_STATE_ROUTED or eq nxt DC_STATE_DELIVERED or eq nxt DC_STATE_EXPIRED eq nxt DC_STATE_REVOKED
56ret DC_OK
57end
58ret DC_ERR_STATE
59end
60ret DC_ERR_STATE
61end
62
63fn dc_validate_recipient recip view uchar * to ulong
64n = len recip
65if or lt n ulong 8 gt n ulong 128
66ret DC_ERR_RECIPIENT
67end
68i = ulong 0
69while lt i n bound 128
70b = get recip i
71if or lt b uchar 33 gt b uchar 126
72ret DC_ERR_RECIPIENT
73end
74i = add i ulong 1
75end
76has_pfx = false
77if pt_slice_eq part recip ulong 0 ulong 4 dc_pfx_did
78has_pfx = true
79end
80if pt_slice_eq part recip ulong 0 ulong 4 dc_pfx_rad
81has_pfx = true
82end
83if pt_slice_eq part recip ulong 0 ulong 5 dc_pfx_node
84has_pfx = true
85end
86if not has_pfx
87ret DC_ERR_RECIPIENT
88end
89ret DC_OK
90end
91
92fn dc_check_routing hops ulong max_hops ulong issued_s ulong ttl_s ulong now_s ulong to ulong
93if or eq max_hops ulong 0 gt max_hops DC_MAX_HOPS
94ret DC_ERR_HOPS
95end
96if gt hops max_hops
97ret DC_ERR_HOPS
98end
99if or eq ttl_s ulong 0 gt ttl_s DC_MAX_TTL_S
100ret DC_ERR_TTL
101end
102if lt now_s issued_s
103ret DC_ERR_TTL
104end
105elapsed = sub now_s issued_s
106if gt elapsed ttl_s
107ret DC_ERR_TTL
108end
109ret DC_OK
110end
111
112fn dc_hash_head ctx edit ulong 40 cap_id view uchar 16 recip view uchar * payload_dig view uchar 32 to void
113sha256_init ctx
114sha256_update ctx dc_dom_cap
115sha256_update ctx cap_id
116sha256_update ctx dc_sep
117sha256_update ctx recip
118sha256_update ctx dc_sep
119sha256_update ctx payload_dig
120end
121
122fn dc_hash_finish ctx edit ulong 40 ttl_s ulong max_hops ulong dst edit uchar 32 to void
123buf = new uchar 16 0
124p0 = pt_put_u64_le buf ulong 0 ttl_s
125p1 = pt_put_u64_le buf p0 max_hops
126sha256_update ctx part buf ulong 0 p1
127sha256_final ctx dst
128end
129
130fn dc_verify_capsule cap_id view uchar 16 recip view uchar * payload_dig view uchar 32 ttl_s ulong max_hops ulong to ulong
131if uuid_is_nil cap_id
132ret DC_ERR_ID
133end
134if ne dc_validate_recipient recip DC_OK
135ret DC_ERR_RECIPIENT
136end
137if digest_is_zero payload_dig
138ret DC_ERR_DIGEST
139end
140if or eq ttl_s ulong 0 gt ttl_s DC_MAX_TTL_S
141ret DC_ERR_TTL
142end
143if or eq max_hops ulong 0 gt max_hops DC_MAX_HOPS
144ret DC_ERR_HOPS
145end
146ret DC_OK
147end
148
149fn dc_verify_digest ctx edit ulong 40 ttl_s ulong max_hops ulong claimed view uchar 32 to ulong
150exp = new uchar 32 0
151dc_hash_finish ctx ttl_s max_hops exp
152if not digest_eq exp claimed
153ret DC_ERR_DIGEST
154end
155ret DC_OK
156end