interplane.Get Omega
Read in your language

omega-principia

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

omega-principia / src / omsci_zenodo.om

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