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 / core / provenance.om

183 lines · 4527 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/aien-protocols/core/provenance.om: pure-Omega port of aien-provenance
3note Implements compute_sha256, combine_digests, GrantPermissions bitflags,
4note SourceGrant unsigned digest, license/permission/expiration verification, and verify_all.
5note Compose after si/directives/om2.om, om/tls/sha2.om, and protocol/aien-protocols/core/types.om.
6
7const PERM_READ uint 1
8const PERM_EVALUATE uint 2
9const PERM_TRAIN uint 4
10const PERM_DISTILL uint 8
11const PERM_REDISTRIBUTE uint 16
12const PERM_COMMERCIAL uint 32
13const PERM_ALL_MASK uint 63
14
15const PROV_OK ulong 0
16const PROV_ERR_PERM_DENIED ulong 1
17const PROV_ERR_BAD_LICENSE ulong 2
18const PROV_ERR_EXPIRED ulong 3
19const PROV_ERR_MISSING_SIG ulong 4
20const PROV_ERR_BAD_SIG_FMT ulong 5
21const PROV_ERR_SIG_FAILED ulong 6
22const PROV_ERR_BAD_PERMS ulong 7
23
24text prov_exp_present "exp:present"
25text prov_exp_none "exp:none"
26
27note --- SHA-256 wrappers (backed by om/tls/sha2.om) ---
28
29fn prov_compute_sha256 msg view uchar * dst edit uchar 32 to void
30ctx = new ulong 40 0
31sha256_init ctx
32sha256_update ctx msg
33sha256_final ctx dst
34end
35
36fn prov_combine_digests d1 view uchar 32 d2 view uchar 32 dst edit uchar 32 to void
37ctx = new ulong 40 0
38sha256_init ctx
39sha256_update ctx d1
40sha256_update ctx d2
41sha256_final ctx dst
42end
43
44note --- GrantPermissions bitflag operations ---
45
46fn perm_is_valid bits uint to bool
47extra = and bits not PERM_ALL_MASK
48ret eq extra uint 0
49end
50
51fn perm_contains granted uint required uint to bool
52ret eq and granted required required
53end
54
55note --- SourceGrant deterministic unsigned digest ---
56note Matches aien-provenance::SourceGrant::compute_unsigned_digest byte-for-byte:
57note grant_id[16] || source_uri || license_spdx || perms_u32_le || issuer ||
58note ("exp:present" || exp_u64_le OR "exp:none")
59
60fn grant_hash_header ctx edit ulong 40 gid view uchar 16 uri view uchar * to void
61sha256_init ctx
62sha256_update ctx gid
63sha256_update ctx uri
64end
65
66fn grant_hash_policy ctx edit ulong 40 lic view uchar * perms uint issuer view uchar * to void
67pb = new uchar 4 0
68w = pt_put_u32_le pb ulong 0 perms
69if ne w ulong 4
70trap 8
71end
72sha256_update ctx lic
73sha256_update ctx pb
74sha256_update ctx issuer
75end
76
77fn grant_hash_finish ctx edit ulong 40 has_exp bool exp_sec ulong dst edit uchar 32 to void
78eb = new uchar 8 0
79if has_exp
80sha256_update ctx prov_exp_present
81w = pt_put_u64_le eb ulong 0 exp_sec
82if ne w ulong 8
83trap 8
84end
85sha256_update ctx eb
86else
87sha256_update ctx prov_exp_none
88end
89sha256_final ctx dst
90end
91
92note --- SourceGrant verification rules ---
93
94fn grant_verify_permission granted uint required uint to ulong
95if not perm_is_valid granted
96ret PROV_ERR_BAD_PERMS
97end
98if not perm_is_valid required
99ret PROV_ERR_BAD_PERMS
100end
101if perm_contains granted required
102ret PROV_OK
103end
104ret PROV_ERR_PERM_DENIED
105end
106
107fn grant_verify_validity has_exp bool exp_sec ulong now_sec ulong to ulong
108if has_exp
109if gt now_sec exp_sec
110ret PROV_ERR_EXPIRED
111end
112end
113ret PROV_OK
114end
115
116note allowed_csv is a comma-separated list of SPDX identifiers, e.g. "Apache-2.0,MIT"
117fn grant_verify_license_csv lic view uchar * allowed_csv view uchar * to ulong
118nl = len lic
119nc = len allowed_csv
120if eq nl ulong 0
121ret PROV_ERR_BAD_LICENSE
122end
123if eq nc ulong 0
124ret PROV_ERR_BAD_LICENSE
125end
126if gt nc ulong 4096
127ret PROV_ERR_BAD_LICENSE
128end
129start = ulong 0
130i = ulong 0
131while le i nc bound 4097
132at_sep = false
133if eq i nc
134at_sep = true
135else
136if eq get allowed_csv i uchar 44
137at_sep = true
138end
139end
140if at_sep
141seg_len = sub i start
142if gt seg_len ulong 0
143if pt_slice_eq_ascii_ci lic part allowed_csv start seg_len
144ret PROV_OK
145end
146end
147start = add i ulong 1
148end
149i = add i ulong 1
150end
151ret PROV_ERR_BAD_LICENSE
152end
153
154fn grant_verify_sig_present has_sig bool sig_len ulong to ulong
155if not has_sig
156ret PROV_ERR_MISSING_SIG
157end
158if ne sig_len ulong 64
159ret PROV_ERR_BAD_SIG_FMT
160end
161ret PROV_OK
162end
163
164note Complete policy verification order matches Rust SourceGrant::verify_all:
165note 1) license 2) permissions 3) expiration 4) signature presence/format (when required)
166fn grant_verify_policy lic view uchar * allowed_csv view uchar * granted uint required uint to ulong
167rc = grant_verify_license_csv lic allowed_csv
168if ne rc PROV_OK
169ret rc
170end
171ret grant_verify_permission granted required
172end
173
174fn grant_verify_window has_exp bool exp_sec ulong now_sec ulong req_sig bool has_sig bool sig_len ulong to ulong
175rc = grant_verify_validity has_exp exp_sec now_sec
176if ne rc PROV_OK
177ret rc
178end
179if req_sig
180ret grant_verify_sig_present has_sig sig_len
181end
182ret PROV_OK
183end