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 / seed / authority / arboris-signer.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note One supervised ARBSTP02 request on stdin and one signature on stdout. |
| 3 | note FD3 supplies 136 trusted bytes: claim words 0..7, arena and kernel |
| 4 | note SHA-256s, and the eight dispatch-control bytes. FD4 supplies the pinned |
| 5 | note world public key. FD5 is a connected socket to its dedicated SSH agent. |
| 6 | note Cwd is the supervisor-owned world receipt directory. Only public data |
| 7 | note enters this process. Compose sha2.om, sha512.om and ed25519.om first. |
| 8 | text as_domain "ARBSTP02" |
| 9 | text as_algorithm "ssh-ed25519" |
| 10 | text as_hex "0123456789abcdef" |
| 11 | text as_here "." |
| 12 | note Linux AArch64 open flags: CLOEXEC, NOFOLLOW, plus NONBLOCK, |
| 13 | note WRONLY|CREAT|EXCL or DIRECTORY for the three corresponding opens. |
| 14 | const as_read_flags int 559104 |
| 15 | const as_create_flags int 557249 |
| 16 | const as_dir_flags int 573440 |
| 17 | |
| 18 | fn as_read fd int buf edit uchar * to bool |
| 19 | pos = ulong 0 |
| 20 | while lt pos len buf |
| 21 | count = read fd buf pos sub len buf pos |
| 22 | if ne count neg long 4 |
| 23 | if le count 0 |
| 24 | ret false |
| 25 | end |
| 26 | pos = add pos ulong count |
| 27 | end |
| 28 | end |
| 29 | ret true |
| 30 | end |
| 31 | |
| 32 | fn as_write fd int buf view uchar * to bool |
| 33 | pos = ulong 0 |
| 34 | while lt pos len buf |
| 35 | count = write fd buf pos sub len buf pos |
| 36 | if ne count neg long 4 |
| 37 | if le count 0 |
| 38 | ret false |
| 39 | end |
| 40 | pos = add pos ulong count |
| 41 | end |
| 42 | end |
| 43 | ret true |
| 44 | end |
| 45 | |
| 46 | fn as_equal a view uchar * b view uchar * to bool |
| 47 | if ne len a len b |
| 48 | ret false |
| 49 | end |
| 50 | for i ulong len a |
| 51 | if ne get a i get b i |
| 52 | ret false |
| 53 | end |
| 54 | end |
| 55 | ret true |
| 56 | end |
| 57 | |
| 58 | fn as_allowed claim view uchar 65536 policy view uchar 136 to bool |
| 59 | if not as_equal part claim 0 8 as_domain |
| 60 | ret false |
| 61 | end |
| 62 | if not as_equal part claim 0 128 part policy 0 128 |
| 63 | ret false |
| 64 | end |
| 65 | ret as_equal part claim 176 8 part policy 128 8 |
| 66 | end |
| 67 | |
| 68 | fn as_agent pk view uchar 32 claim view uchar 65536 sig edit uchar 64 to bool |
| 69 | request = new uchar 252 0 |
| 70 | put request 3 248 |
| 71 | put request 4 13 |
| 72 | put request 8 51 |
| 73 | put request 12 11 |
| 74 | for i ulong 11 |
| 75 | put request add 13 i get as_algorithm i |
| 76 | end |
| 77 | put request 27 32 |
| 78 | for i ulong 32 |
| 79 | put request add 28 i get pk i |
| 80 | end |
| 81 | put request 63 184 |
| 82 | for i ulong 184 |
| 83 | put request add 64 i get claim i |
| 84 | end |
| 85 | if not as_write 5 request |
| 86 | ret false |
| 87 | end |
| 88 | response = new uchar 92 0 |
| 89 | if not as_read 5 part response 0 4 |
| 90 | ret false |
| 91 | end |
| 92 | if or ne get response 3 88 ne or get response 0 or get response 1 get response 2 0 |
| 93 | ret false |
| 94 | end |
| 95 | if not as_read 5 part response 4 88 |
| 96 | ret false |
| 97 | end |
| 98 | put request 3 88 |
| 99 | put request 4 14 |
| 100 | put request 8 83 |
| 101 | put request 27 64 |
| 102 | if not as_equal part response 0 28 part request 0 28 |
| 103 | ret false |
| 104 | end |
| 105 | for i ulong 64 |
| 106 | put sig i get response add 28 i |
| 107 | end |
| 108 | ret eq edverify pk claim 184 sig 0 1 |
| 109 | end |
| 110 | |
| 111 | fn as_name bytes view uchar * name edit uchar * to void |
| 112 | for i ulong len bytes |
| 113 | put name mul i 2 get as_hex shr get bytes i 4 |
| 114 | put name add mul i 2 1 get as_hex and get bytes i 15 |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | fn as_existing name view uchar * receipt view uchar 248 to bool |
| 119 | fd = fopen name 0 len name as_read_flags 0 |
| 120 | if lt fd 0 |
| 121 | ret false |
| 122 | end |
| 123 | data = new uchar 248 0 |
| 124 | extra = new uchar 1 0 |
| 125 | ok = as_read int fd data |
| 126 | if ok |
| 127 | ok = eq read int fd extra 0 1 0 |
| 128 | end |
| 129 | if ne fclose int fd 0 |
| 130 | ret false |
| 131 | end |
| 132 | ret and ok as_equal data receipt |
| 133 | end |
| 134 | |
| 135 | fn as_record receipt view uchar 248 to bool |
| 136 | nonce = new uchar 16 0 |
| 137 | if ne random nonce 0 16 0 16 |
| 138 | ret false |
| 139 | end |
| 140 | paths = new uchar 103 0 |
| 141 | put paths 0 2 |
| 142 | put paths 2 33 |
| 143 | put paths 4 46 |
| 144 | as_name nonce part paths 5 32 |
| 145 | put paths 37 64 |
| 146 | hash = new uchar 32 0 |
| 147 | ctx = new ulong 40 0 |
| 148 | sha256_init ctx |
| 149 | sha256_update ctx receipt |
| 150 | sha256_final ctx hash |
| 151 | as_name hash part paths 39 64 |
| 152 | tmp = fopen paths 4 33 as_create_flags 384 |
| 153 | if lt tmp 0 |
| 154 | ret false |
| 155 | end |
| 156 | ok = as_write int tmp receipt |
| 157 | if ok |
| 158 | ok = eq fsync int tmp 0 |
| 159 | end |
| 160 | if ne fclose int tmp 0 |
| 161 | ok = false |
| 162 | end |
| 163 | if ok |
| 164 | rc = renames paths 0 len paths 1 |
| 165 | ok = eq rc 0 |
| 166 | if eq rc neg long 17 |
| 167 | ok = as_existing part paths 39 64 receipt |
| 168 | end |
| 169 | if ne rc 0 |
| 170 | ignored = unlink paths 4 33 |
| 171 | end |
| 172 | else |
| 173 | ignored = unlink paths 4 33 |
| 174 | end |
| 175 | if not ok |
| 176 | ret false |
| 177 | end |
| 178 | dirfd = fopen as_here 0 1 as_dir_flags 0 |
| 179 | if lt dirfd 0 |
| 180 | ret false |
| 181 | end |
| 182 | ok = eq fsync int dirfd 0 |
| 183 | ret and eq fclose int dirfd 0 ok |
| 184 | end |
| 185 | |
| 186 | fn main to int |
| 187 | policy = new uchar 136 0 |
| 188 | pk = new uchar 32 0 |
| 189 | claim = new uchar 65536 0 |
| 190 | sig = new uchar 64 0 |
| 191 | if not as_read 3 policy |
| 192 | ret 1 |
| 193 | end |
| 194 | if not as_read 4 pk |
| 195 | ret 1 |
| 196 | end |
| 197 | if not as_read 0 part claim 0 184 |
| 198 | ret 2 |
| 199 | end |
| 200 | if not as_allowed claim policy |
| 201 | ret 3 |
| 202 | end |
| 203 | if not as_agent pk claim sig |
| 204 | ret 4 |
| 205 | end |
| 206 | receipt = new uchar 248 0 |
| 207 | for i ulong 184 |
| 208 | put receipt i get claim i |
| 209 | end |
| 210 | for i ulong 64 |
| 211 | put receipt add 184 i get sig i |
| 212 | end |
| 213 | if not as_record receipt |
| 214 | ret 5 |
| 215 | end |
| 216 | if not as_write 1 sig |
| 217 | ret 6 |
| 218 | end |
| 219 | ret 0 |
| 220 | end |