omega-principia
This project has not described itself yet.
Brought by drakestapleton
omega-principia / src / omsci_zenodo.om
| 1 | om2 |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note src/omsci_zenodo.om -- Pure-OM-2 Fail-Closed Zenodo DOI Publication Gatekeeper |
| 4 | note Portable macOS ARM64 & Linux AArch64 (zero C, zero gcc, zero libc, zero Python). |
| 5 | note |
| 6 | note Stdin format (677 bytes): |
| 7 | note bytes 0..3 : mode ("SBOX" = sandbox draft, "DRFT" = prod draft, "LIVE" = prod live DOI) |
| 8 | note byte 4 : explicit_publish_flag (uchar 48 '0' or uchar 49 '1') |
| 9 | note bytes 5..676 : 672-byte OMSC01 .omp binary capsule |
| 10 | note |
| 11 | note Fail-closed exit codes: |
| 12 | note 0 = AUTHORIZED |
| 13 | note 61 = ERR_TAMPER |
| 14 | note 62 = ERR_STAGE_ORDER |
| 15 | note 63 = ERR_NO_NEG_CTRL |
| 16 | note 64 = ERR_LEAN_HYGIENE |
| 17 | note 65 = ERR_CHAIN_BREAK |
| 18 | note 66 = ERR_UNSEALED_PUB |
| 19 | note 67 = ERR_NO_EXPLICIT_PUB (LIVE requested without explicit_publish_flag='1') |
| 20 | |
| 21 | const GF_P ulong 0xffffffff00000001 |
| 22 | const GF_EPS ulong 0xffffffff |
| 23 | const GENESIS_H ulong 0x4f4d534330310000 |
| 24 | const CAPSULE_LEN ulong 672 |
| 25 | const HDR_LEN ulong 32 |
| 26 | const FRAME_LEN ulong 64 |
| 27 | const NUM_STAGES ulong 10 |
| 28 | |
| 29 | const ERR_OK ulong 0 |
| 30 | const ERR_TAMPER ulong 61 |
| 31 | const ERR_STAGE_ORDER ulong 62 |
| 32 | const ERR_NO_NEG_CTRL ulong 63 |
| 33 | const ERR_LEAN_HYGIENE ulong 64 |
| 34 | const ERR_CHAIN_BREAK ulong 65 |
| 35 | const ERR_UNSEALED_PUB ulong 66 |
| 36 | const ERR_NO_EXPLICIT_PUB ulong 67 |
| 37 | |
| 38 | global cap_buf uchar 672 |
| 39 | global in_buf uchar 704 |
| 40 | global obuf uchar 1024 |
| 41 | global olen ulong 1 |
| 42 | |
| 43 | text s_sbox "AUTHORIZED=1\nENDPOINT=https://sandbox.zenodo.org/api/deposit/depositions\nACTION=DRAFT_PRERESERVE_DOI\nOMH08_X0=0x" |
| 44 | text s_drft "AUTHORIZED=1\nENDPOINT=https://zenodo.org/api/deposit/depositions\nACTION=DRAFT_PRERESERVE_DOI\nOMH08_X0=0x" |
| 45 | text s_live "AUTHORIZED=1\nENDPOINT=https://zenodo.org/api/deposit/depositions\nACTION=MINT_LIVE_DOI\nOMH08_X0=0x" |
| 46 | text s_rd "\nROOT_DIGEST=0x" |
| 47 | text s_nl "\n" |
| 48 | |
| 49 | fn oflush to void |
| 50 | n = get olen 0 |
| 51 | if gt n 0 |
| 52 | w = write 1 obuf 0 n |
| 53 | put olen 0 0 |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | fn oput c uchar to void |
| 58 | n = get olen 0 |
| 59 | if lt n 1024 |
| 60 | put obuf n c |
| 61 | put olen 0 add n 1 |
| 62 | end |
| 63 | end |
| 64 | |
| 65 | fn ostr s view uchar * to void |
| 66 | for i ulong len s |
| 67 | oput get s i |
| 68 | end |
| 69 | end |
| 70 | |
| 71 | fn ohex64 v ulong to void |
| 72 | for i ulong 16 |
| 73 | sh = mul sub 15 i 4 |
| 74 | nib = and shr v sh 15 |
| 75 | if lt nib 10 |
| 76 | oput uchar add nib 48 |
| 77 | else |
| 78 | oput uchar add sub nib 10 97 |
| 79 | end |
| 80 | end |
| 81 | end |
| 82 | |
| 83 | fn gf_norm x ulong to ulong |
| 84 | if ge x GF_P |
| 85 | ret sub x GF_P |
| 86 | end |
| 87 | ret x |
| 88 | end |
| 89 | |
| 90 | fn gf_add a ulong b ulong to ulong |
| 91 | aa = gf_norm a |
| 92 | bb = gf_norm b |
| 93 | s = add aa bb |
| 94 | if lt s aa |
| 95 | r = add s GF_EPS |
| 96 | ret gf_norm r |
| 97 | end |
| 98 | ret gf_norm s |
| 99 | end |
| 100 | |
| 101 | fn gf_reduce hi ulong lo ulong to ulong |
| 102 | hi_lo = and hi 0xffffffff |
| 103 | hi_hi = shr hi 32 |
| 104 | r1 = lo |
| 105 | if lt r1 hi_hi |
| 106 | r1 = add sub r1 hi_hi GF_P |
| 107 | else |
| 108 | r1 = sub r1 hi_hi |
| 109 | end |
| 110 | r1 = gf_norm r1 |
| 111 | t = mul hi_lo GF_EPS |
| 112 | ret gf_add r1 t |
| 113 | end |
| 114 | |
| 115 | fn gf_mul a ulong b ulong to ulong |
| 116 | aa = gf_norm a |
| 117 | bb = gf_norm b |
| 118 | p0 = mul and aa 0xffffffff and bb 0xffffffff |
| 119 | p1 = mul and aa 0xffffffff shr bb 32 |
| 120 | p2 = mul shr aa 32 and bb 0xffffffff |
| 121 | m_lo = add and p1 0xffffffff and p2 0xffffffff |
| 122 | m_hi = add shr p1 32 shr p2 32 |
| 123 | m_hi = add m_hi shr m_lo 32 |
| 124 | lo = add p0 shl and m_lo 0xffffffff 32 |
| 125 | hi = add mul shr aa 32 shr bb 32 m_hi |
| 126 | if lt lo p0 |
| 127 | hi = add hi 1 |
| 128 | end |
| 129 | ret gf_reduce hi lo |
| 130 | end |
| 131 | |
| 132 | fn mix64 a ulong b ulong to ulong |
| 133 | z = xor a add b 0x9e3779b97f4a7c15 |
| 134 | z = mul xor z shr z 30 0xbf58476d1ce4e5b9 |
| 135 | z = mul xor z shr z 27 0x94d049bb133111eb |
| 136 | z = xor z shr z 31 |
| 137 | ret z |
| 138 | end |
| 139 | |
| 140 | fn cap_get64 off ulong to ulong |
| 141 | acc = ulong 0 |
| 142 | for i ulong 8 |
| 143 | b = ulong get cap_buf add off i |
| 144 | acc = or acc shl b mul i 8 |
| 145 | end |
| 146 | ret acc |
| 147 | end |
| 148 | |
| 149 | fn cap_get32 off ulong to ulong |
| 150 | acc = ulong 0 |
| 151 | for i ulong 4 |
| 152 | b = ulong get cap_buf add off i |
| 153 | acc = or acc shl b mul i 8 |
| 154 | end |
| 155 | ret acc |
| 156 | end |
| 157 | |
| 158 | fn frame_hash_at off ulong to ulong |
| 159 | w0 = cap_get64 off |
| 160 | w1 = cap_get64 add off 8 |
| 161 | w2 = cap_get64 add off 16 |
| 162 | w3 = cap_get64 add off 24 |
| 163 | w4 = cap_get64 add off 32 |
| 164 | w5 = cap_get64 add off 40 |
| 165 | w6 = cap_get64 add off 48 |
| 166 | h = mix64 w0 w1 |
| 167 | h = mix64 h w2 |
| 168 | h = mix64 h w3 |
| 169 | h = mix64 h w4 |
| 170 | h = mix64 h w5 |
| 171 | h = mix64 h w6 |
| 172 | ret h |
| 173 | end |
| 174 | |
| 175 | fn verify_stage_frame s ulong prev ulong to ulong |
| 176 | off = add HDR_LEN mul s FRAME_LEN |
| 177 | if ne cap_get64 off s |
| 178 | ret ERR_STAGE_ORDER |
| 179 | end |
| 180 | if ne cap_get64 add off 8 prev |
| 181 | ret ERR_CHAIN_BREAK |
| 182 | end |
| 183 | if ne cap_get64 add off 16 add s 1 |
| 184 | ret ERR_STAGE_ORDER |
| 185 | end |
| 186 | if ne cap_get64 add off 56 frame_hash_at off |
| 187 | ret ERR_TAMPER |
| 188 | end |
| 189 | met = cap_get64 add off 24 |
| 190 | if eq s 6 |
| 191 | claims = and shr met 48 0xffff |
| 192 | nc = and shr met 16 0xffff |
| 193 | if eq nc 0 |
| 194 | ret ERR_NO_NEG_CTRL |
| 195 | end |
| 196 | if lt nc claims |
| 197 | ret ERR_NO_NEG_CTRL |
| 198 | end |
| 199 | end |
| 200 | if eq s 8 |
| 201 | leanm = cap_get64 add off 48 |
| 202 | if eq shr leanm 32 0 |
| 203 | ret ERR_LEAN_HYGIENE |
| 204 | end |
| 205 | if ne and shr leanm 16 0xffff 0 |
| 206 | ret ERR_LEAN_HYGIENE |
| 207 | end |
| 208 | if ne and shr leanm 8 0xff 0 |
| 209 | ret ERR_LEAN_HYGIENE |
| 210 | end |
| 211 | if ne and leanm 0xff 1 |
| 212 | ret ERR_LEAN_HYGIENE |
| 213 | end |
| 214 | end |
| 215 | ret ERR_OK |
| 216 | end |
| 217 | |
| 218 | fn verify_capsule to ulong |
| 219 | if ne get cap_buf 0 uchar 79 |
| 220 | ret ERR_TAMPER |
| 221 | end |
| 222 | if ne get cap_buf 1 uchar 77 |
| 223 | ret ERR_TAMPER |
| 224 | end |
| 225 | if ne get cap_buf 2 uchar 83 |
| 226 | ret ERR_TAMPER |
| 227 | end |
| 228 | if ne get cap_buf 3 uchar 67 |
| 229 | ret ERR_TAMPER |
| 230 | end |
| 231 | if ne get cap_buf 4 uchar 48 |
| 232 | ret ERR_TAMPER |
| 233 | end |
| 234 | if ne get cap_buf 5 uchar 49 |
| 235 | ret ERR_TAMPER |
| 236 | end |
| 237 | if ne get cap_buf 6 uchar 1 |
| 238 | ret ERR_TAMPER |
| 239 | end |
| 240 | if eq and cap_get32 8 16 0 |
| 241 | ret ERR_UNSEALED_PUB |
| 242 | end |
| 243 | if ne cap_get32 12 1023 |
| 244 | ret ERR_STAGE_ORDER |
| 245 | end |
| 246 | prev = GENESIS_H |
| 247 | x0 = ulong 1 |
| 248 | p1 = ulong 0x243f6a8885a308d3 |
| 249 | for s ulong NUM_STAGES |
| 250 | rc = verify_stage_frame s prev |
| 251 | if ne rc ERR_OK |
| 252 | ret rc |
| 253 | end |
| 254 | off = add HDR_LEN mul s FRAME_LEN |
| 255 | x0 = gf_add gf_mul x0 p1 gf_add cap_get64 add off 32 cap_get64 add off 40 |
| 256 | prev = cap_get64 add off 56 |
| 257 | end |
| 258 | if ne cap_get64 16 x0 |
| 259 | ret ERR_TAMPER |
| 260 | end |
| 261 | if ne cap_get64 24 prev |
| 262 | ret ERR_TAMPER |
| 263 | end |
| 264 | ret ERR_OK |
| 265 | end |
| 266 | |
| 267 | fn read_all_stdin to ulong |
| 268 | tot = ulong 0 |
| 269 | go = true |
| 270 | while go bound 704 |
| 271 | remn = sub 704 tot |
| 272 | if eq remn 0 |
| 273 | go = false |
| 274 | else |
| 275 | k = read 0 in_buf tot remn |
| 276 | if le k 0 |
| 277 | go = false |
| 278 | else |
| 279 | tot = add tot ulong k |
| 280 | end |
| 281 | end |
| 282 | end |
| 283 | ret tot |
| 284 | end |
| 285 | |
| 286 | fn is_mode c0 uchar c1 uchar c2 uchar c3 uchar to bool |
| 287 | if ne get in_buf 0 c0 |
| 288 | ret false |
| 289 | end |
| 290 | if ne get in_buf 1 c1 |
| 291 | ret false |
| 292 | end |
| 293 | if ne get in_buf 2 c2 |
| 294 | ret false |
| 295 | end |
| 296 | if ne get in_buf 3 c3 |
| 297 | ret false |
| 298 | end |
| 299 | ret true |
| 300 | end |
| 301 | |
| 302 | fn main to int |
| 303 | n = read_all_stdin |
| 304 | if lt n add 5 CAPSULE_LEN |
| 305 | ret int ERR_TAMPER |
| 306 | end |
| 307 | for i ulong CAPSULE_LEN |
| 308 | put cap_buf i get in_buf add 5 i |
| 309 | end |
| 310 | rc = verify_capsule |
| 311 | if ne rc ERR_OK |
| 312 | ret int rc |
| 313 | end |
| 314 | expl = get in_buf 4 |
| 315 | if is_mode uchar 83 uchar 66 uchar 79 uchar 88 |
| 316 | ostr s_sbox |
| 317 | ohex64 cap_get64 16 |
| 318 | ostr s_rd |
| 319 | ohex64 cap_get64 24 |
| 320 | ostr s_nl |
| 321 | oflush |
| 322 | ret 0 |
| 323 | end |
| 324 | if is_mode uchar 68 uchar 82 uchar 70 uchar 84 |
| 325 | ostr s_drft |
| 326 | ohex64 cap_get64 16 |
| 327 | ostr s_rd |
| 328 | ohex64 cap_get64 24 |
| 329 | ostr s_nl |
| 330 | oflush |
| 331 | ret 0 |
| 332 | end |
| 333 | if is_mode uchar 76 uchar 73 uchar 86 uchar 69 |
| 334 | if ne expl uchar 49 |
| 335 | ret int ERR_NO_EXPLICIT_PUB |
| 336 | end |
| 337 | ostr s_live |
| 338 | ohex64 cap_get64 16 |
| 339 | ostr s_rd |
| 340 | ohex64 cap_get64 24 |
| 341 | ostr s_nl |
| 342 | oflush |
| 343 | ret 0 |
| 344 | end |
| 345 | ret int ERR_TAMPER |
| 346 | end |