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