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 / main.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/aien-protocols/core/main.om: CLI entrypoint for aien-protocols core |
| 3 | note Compose with: |
| 4 | note si/directives/om2.om om/tls/sha2.om protocol/aien-protocols/core/types.om |
| 5 | note protocol/aien-protocols/core/provenance.om protocol/aien-protocols/core/action_event.om |
| 6 | note protocol/aien-protocols/core/main.om |
| 7 | |
| 8 | text cmd_uuid "check-uuid" |
| 9 | text cmd_digest "check-digest" |
| 10 | text cmd_sha "sha256" |
| 11 | text cmd_lic "check-license" |
| 12 | text cmd_trans "check-transition" |
| 13 | |
| 14 | text msg_ok "OK\n" |
| 15 | text msg_invalid "INVALID\n" |
| 16 | text msg_denied "DENIED\n" |
| 17 | text msg_nl "\n" |
| 18 | text msg_usage "Usage: aien-protocols-core <check-uuid|check-digest|sha256|check-license|check-transition> ...\n" |
| 19 | |
| 20 | fn parse_u64_dec s view uchar * to long |
| 21 | n = len s |
| 22 | if eq n ulong 0 |
| 23 | ret long -1 |
| 24 | end |
| 25 | if gt n ulong 18 |
| 26 | ret long -1 |
| 27 | end |
| 28 | acc = ulong 0 |
| 29 | i = ulong 0 |
| 30 | while lt i n bound 18 |
| 31 | b = get s i |
| 32 | if or lt b uchar 48 gt b uchar 57 |
| 33 | ret long -1 |
| 34 | end |
| 35 | d = sub ulong b ulong 48 |
| 36 | acc = add mul acc ulong 10 d |
| 37 | i = add i ulong 1 |
| 38 | end |
| 39 | ret long acc |
| 40 | end |
| 41 | |
| 42 | fn run_check_uuid to int |
| 43 | arg = new uchar 128 0 |
| 44 | k = argread ulong 2 arg ulong 0 ulong 128 |
| 45 | if le k long 0 |
| 46 | w = pt_write_all int 1 msg_invalid |
| 47 | if w |
| 48 | ret int 1 |
| 49 | end |
| 50 | ret int 1 |
| 51 | end |
| 52 | u = new uchar 16 0 |
| 53 | rc = uuid_from_str part arg 0 ulong k u |
| 54 | if ne rc PT_OK |
| 55 | w = pt_write_all int 1 msg_invalid |
| 56 | if w |
| 57 | ret int 1 |
| 58 | end |
| 59 | ret int 1 |
| 60 | end |
| 61 | if not uuid_is_v4 u |
| 62 | w = pt_write_all int 1 msg_invalid |
| 63 | if w |
| 64 | ret int 1 |
| 65 | end |
| 66 | ret int 1 |
| 67 | end |
| 68 | w = pt_write_all int 1 msg_ok |
| 69 | if not w |
| 70 | ret int 1 |
| 71 | end |
| 72 | ret int 0 |
| 73 | end |
| 74 | |
| 75 | fn run_check_digest to int |
| 76 | arg = new uchar 128 0 |
| 77 | k = argread ulong 2 arg ulong 0 ulong 128 |
| 78 | if le k long 0 |
| 79 | w = pt_write_all int 1 msg_invalid |
| 80 | if w |
| 81 | ret int 1 |
| 82 | end |
| 83 | ret int 1 |
| 84 | end |
| 85 | d = new uchar 32 0 |
| 86 | rc = digest_from_hex part arg 0 ulong k d |
| 87 | if ne rc PT_OK |
| 88 | w = pt_write_all int 1 msg_invalid |
| 89 | if w |
| 90 | ret int 1 |
| 91 | end |
| 92 | ret int 1 |
| 93 | end |
| 94 | fmt = new uchar 71 0 |
| 95 | fl = digest_format_sha256 d fmt |
| 96 | w1 = pt_write_all int 1 part fmt 0 fl |
| 97 | w2 = pt_write_all int 1 msg_nl |
| 98 | if and w1 w2 |
| 99 | ret int 0 |
| 100 | end |
| 101 | ret int 1 |
| 102 | end |
| 103 | |
| 104 | fn run_sha256 to int |
| 105 | arg = new uchar 4096 0 |
| 106 | k = argread ulong 2 arg ulong 0 ulong 4096 |
| 107 | if lt k long 0 |
| 108 | ret int 1 |
| 109 | end |
| 110 | d = new uchar 32 0 |
| 111 | prov_compute_sha256 part arg 0 ulong k d |
| 112 | fmt = new uchar 71 0 |
| 113 | fl = digest_format_sha256 d fmt |
| 114 | w1 = pt_write_all int 1 part fmt 0 fl |
| 115 | w2 = pt_write_all int 1 msg_nl |
| 116 | if and w1 w2 |
| 117 | ret int 0 |
| 118 | end |
| 119 | ret int 1 |
| 120 | end |
| 121 | |
| 122 | fn run_check_license to int |
| 123 | lic = new uchar 128 0 |
| 124 | csv = new uchar 512 0 |
| 125 | kl = argread ulong 2 lic ulong 0 ulong 128 |
| 126 | kc = argread ulong 3 csv ulong 0 ulong 512 |
| 127 | if or le kl long 0 le kc long 0 |
| 128 | w = pt_write_all int 1 msg_denied |
| 129 | if w |
| 130 | ret int 1 |
| 131 | end |
| 132 | ret int 1 |
| 133 | end |
| 134 | rc = grant_verify_license_csv part lic 0 ulong kl part csv 0 ulong kc |
| 135 | if ne rc PROV_OK |
| 136 | w = pt_write_all int 1 msg_denied |
| 137 | if w |
| 138 | ret int 1 |
| 139 | end |
| 140 | ret int 1 |
| 141 | end |
| 142 | w = pt_write_all int 1 msg_ok |
| 143 | if not w |
| 144 | ret int 1 |
| 145 | end |
| 146 | ret int 0 |
| 147 | end |
| 148 | |
| 149 | fn run_check_transition to int |
| 150 | a1 = new uchar 32 0 |
| 151 | a2 = new uchar 32 0 |
| 152 | k1 = argread ulong 2 a1 ulong 0 ulong 32 |
| 153 | k2 = argread ulong 3 a2 ulong 0 ulong 32 |
| 154 | if or le k1 long 0 le k2 long 0 |
| 155 | w = pt_write_all int 1 msg_denied |
| 156 | if w |
| 157 | ret int 1 |
| 158 | end |
| 159 | ret int 1 |
| 160 | end |
| 161 | v1 = parse_u64_dec part a1 0 ulong k1 |
| 162 | v2 = parse_u64_dec part a2 0 ulong k2 |
| 163 | if or lt v1 long 0 lt v2 long 0 |
| 164 | w = pt_write_all int 1 msg_denied |
| 165 | if w |
| 166 | ret int 1 |
| 167 | end |
| 168 | ret int 1 |
| 169 | end |
| 170 | rc = exec_transition_check ulong v1 ulong v2 |
| 171 | if ne rc AE_OK |
| 172 | w = pt_write_all int 1 msg_denied |
| 173 | if w |
| 174 | ret int 1 |
| 175 | end |
| 176 | ret int 1 |
| 177 | end |
| 178 | w = pt_write_all int 1 msg_ok |
| 179 | if not w |
| 180 | ret int 1 |
| 181 | end |
| 182 | ret int 0 |
| 183 | end |
| 184 | |
| 185 | fn main to int |
| 186 | argc = argcnt |
| 187 | if lt argc long 3 |
| 188 | w = pt_write_all int 2 msg_usage |
| 189 | if w |
| 190 | ret int 2 |
| 191 | end |
| 192 | ret int 2 |
| 193 | end |
| 194 | cmd = new uchar 64 0 |
| 195 | kc = argread ulong 1 cmd ulong 0 ulong 64 |
| 196 | if le kc long 0 |
| 197 | ret int 2 |
| 198 | end |
| 199 | if pt_slice_eq part cmd 0 ulong kc cmd_uuid |
| 200 | ret run_check_uuid |
| 201 | end |
| 202 | if pt_slice_eq part cmd 0 ulong kc cmd_digest |
| 203 | ret run_check_digest |
| 204 | end |
| 205 | if pt_slice_eq part cmd 0 ulong kc cmd_sha |
| 206 | ret run_sha256 |
| 207 | end |
| 208 | if pt_slice_eq part cmd 0 ulong kc cmd_lic |
| 209 | if lt argc long 4 |
| 210 | w = pt_write_all int 2 msg_usage |
| 211 | if w |
| 212 | ret int 2 |
| 213 | end |
| 214 | ret int 2 |
| 215 | end |
| 216 | ret run_check_license |
| 217 | end |
| 218 | if pt_slice_eq part cmd 0 ulong kc cmd_trans |
| 219 | if lt argc long 4 |
| 220 | w = pt_write_all int 2 msg_usage |
| 221 | if w |
| 222 | ret int 2 |
| 223 | end |
| 224 | ret int 2 |
| 225 | end |
| 226 | ret run_check_transition |
| 227 | end |
| 228 | w = pt_write_all int 2 msg_usage |
| 229 | if w |
| 230 | ret int 2 |
| 231 | end |
| 232 | ret int 2 |
| 233 | end |