interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / crates / aienos-store-tool / store_tool.om

348 lines · 7650 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-store-tool/store_tool.om: Pure-Omega System Store v1
3note qualification tooling: CRC32C, Superblock v1 codec, sector tear closure,
4note deterministic fault injection, seeded garbage, and HMAC-SHA256 operator auth.
5note Converted from aienux crates/aienos-store-tool/src/main.rs.
6note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-store-tool/store_tool.om ...
7
8const ST_UNIT_BYTES ulong 4096
9const ST_CRC_OFFSET ulong 64
10const ST_VERSION_V1 ulong 1
11
12const ST_OK int 0
13const ST_ERR_BAD_MAGIC int -1
14const ST_ERR_UNSUP_VER int -2
15const ST_ERR_WRONG_SLOT int -3
16const ST_ERR_NONZERO_RES int -4
17const ST_ERR_BAD_CRC int -5
18const ST_ERR_AMBIGUOUS int -6
19
20const ST_CASE_BAD_CRC int 1
21const ST_CASE_WRONG_MAGIC int 2
22const ST_CASE_NONZERO_RES int 3
23const ST_CASE_WRONG_SLOT int 4
24const ST_CASE_REGION_MISMATCH int 5
25const ST_CASE_UNSUP_VER int 6
26const ST_CASE_SEEDED_GARBAGE int 7
27const ST_CASE_NEW_ROOT int 8
28
29text st_magic "AIENSTR1"
30text st_bad_magic "AIENBAD1"
31
32note Superblock decode outputs: 0=generation, 1=region_units, 2=slot_id
33global st_sb_out ulong 3
34
35note Tear closure outputs: 0=as_old, 1=as_new, 2=fail_mask
36global st_tear_out ulong 3
37
38fn st_put_u16_le buf edit uchar * off ulong v ulong to void
39put buf off uchar and v 0xff
40put buf add off 1 uchar and shr v 8 0xff
41end
42
43fn st_put_u32_le buf edit uchar * off ulong v ulong to void
44put buf off uchar and v 0xff
45put buf add off 1 uchar and shr v 8 0xff
46put buf add off 2 uchar and shr v 16 0xff
47put buf add off 3 uchar and shr v 24 0xff
48end
49
50fn st_put_u64_le buf edit uchar * off ulong v ulong to void
51for i ulong 8
52put buf add off i uchar and shr v mul i 8 0xff
53end
54end
55
56fn st_get_u16_le buf view uchar * off ulong to ulong
57ret or ulong get buf off shl ulong get buf add off 1 8
58end
59
60fn st_get_u32_le buf view uchar * off ulong to ulong
61b0 = ulong get buf off
62b1 = ulong get buf add off 1
63b2 = ulong get buf add off 2
64b3 = ulong get buf add off 3
65ret or or b0 shl b1 8 or shl b2 16 shl b3 24
66end
67
68fn st_get_u64_le buf view uchar * off ulong to ulong
69acc = ulong 0
70for i ulong 8
71acc = or acc shl ulong get buf add off i mul i 8
72end
73ret acc
74end
75
76note CRC32C (Castagnoli, reflected polynomial 0x82f63b78)
77fn st_crc32c buf view uchar * to uint
78crc = uint 0xffffffff
79for i ulong len buf bound 4096
80b = uint get buf i
81crc = xor crc b
82for bit ulong 8
83if ne and crc 1 0
84crc = xor shr crc 1 0x82f63b78
85else
86crc = shr crc 1
87end
88end
89end
90ret xor crc 0xffffffff
91end
92
93fn st_rechecksum unit edit uchar * to void
94for i ulong 4
95put unit add ST_CRC_OFFSET i 0
96end
97crc = st_crc32c unit
98st_put_u32_le unit ST_CRC_OFFSET ulong crc
99end
100
101note Encode a valid System Store v1 superblock into a 4096-byte unit
102fn st_sb_encode unit edit uchar * slot ulong gen ulong region ulong to void
103for i ulong ST_UNIT_BYTES
104put unit i 0
105end
106for i ulong 8
107put unit i get st_magic i
108end
109st_put_u16_le unit 8 ST_VERSION_V1
110st_put_u64_le unit 16 gen
111st_put_u32_le unit 44 slot
112st_put_u64_le unit 48 region
113st_rechecksum unit
114end
115
116note Decode and validate a System Store v1 superblock
117fn st_sb_decode unit view uchar * expected_slot ulong to int
118if lt len unit ST_UNIT_BYTES
119ret ST_ERR_BAD_MAGIC
120end
121for i ulong 8
122if ne get unit i get st_magic i
123ret ST_ERR_BAD_MAGIC
124end
125end
126ver = st_get_u16_le unit 8
127if ne ver ST_VERSION_V1
128ret ST_ERR_UNSUP_VER
129end
130slot = st_get_u32_le unit 44
131if ne slot expected_slot
132ret ST_ERR_WRONG_SLOT
133end
134for i ulong sub ST_UNIT_BYTES 68 bound 4096
135if ne get unit add 68 i 0
136ret ST_ERR_NONZERO_RES
137end
138end
139stored = uint st_get_u32_le unit ST_CRC_OFFSET
140tmp = new uchar 4096 0
141for i ulong ST_UNIT_BYTES
142put tmp i get unit i
143end
144for i ulong 4
145put tmp add ST_CRC_OFFSET i 0
146end
147calc = st_crc32c tmp
148if ne calc stored
149ret ST_ERR_BAD_CRC
150end
151put st_sb_out 0 st_get_u64_le unit 16
152put st_sb_out 1 st_get_u64_le unit 48
153put st_sb_out 2 slot
154ret ST_OK
155end
156
157note Exact xorshift64 seeded garbage (seed 0x4149454e4f530136)
158fn st_seeded_garbage dst edit uchar * to void
159state = ulong 0x4149454e4f530136
160chunks = div ST_UNIT_BYTES 8
161for c ulong chunks bound 512
162state = xor state shl state 13
163state = xor state shr state 7
164state = xor state shl state 17
165st_put_u64_le dst mul c 8 state
166end
167end
168
169fn st_units_eq a view uchar * b view uchar * to bool
170for i ulong ST_UNIT_BYTES
171if ne get a i get b i
172ret false
173end
174end
175ret true
176end
177
178global st_torn uchar 4096
179
180fn st_torn_eq_buf buf view uchar * to bool
181for i ulong ST_UNIT_BYTES
182if ne get st_torn i get buf i
183ret false
184end
185end
186ret true
187end
188
189fn st_copy_sector new_u view uchar * base ulong sector ulong to void
190for j ulong sector bound 4096
191idx = add base j
192put st_torn idx get new_u idx
193end
194end
195
196fn st_eval_tear old_u view uchar * new_u view uchar * sector ulong mask ulong to int
197for i ulong ST_UNIT_BYTES
198put st_torn i get old_u i
199end
200sectors = div ST_UNIT_BYTES sector
201for s ulong sectors bound 16
202if ne and mask shl ulong 1 s 0
203st_copy_sector new_u mul s sector sector
204end
205end
206if st_torn_eq_buf old_u
207ret 1
208end
209if st_torn_eq_buf new_u
210ret 2
211end
212ret 0
213end
214
215note Enumerate all sector-subset tears of new over old (sectors <= 16)
216fn st_tear_outcomes old_u view uchar * new_u view uchar * sector ulong to int
217sectors = div ST_UNIT_BYTES sector
218if or eq sectors 0 gt sectors 16
219ret -1
220end
221combos = shl ulong 1 sectors
222as_old = ulong 0
223as_new = ulong 0
224for mask ulong combos bound 65536
225res = st_eval_tear old_u new_u sector mask
226if eq res 1
227as_old = add as_old 1
228else
229if eq res 2
230as_new = add as_new 1
231else
232put st_tear_out 0 as_old
233put st_tear_out 1 as_new
234put st_tear_out 2 mask
235ret -1
236end
237end
238end
239put st_tear_out 0 as_old
240put st_tear_out 1 as_new
241put st_tear_out 2 0
242ret ST_OK
243end
244
245note Inject one deterministic fault case into a valid superblock copy
246fn st_inject_case src view uchar * dst edit uchar * slot ulong kase int to int
247if eq kase ST_CASE_SEEDED_GARBAGE
248st_seeded_garbage dst
249ret ST_ERR_BAD_MAGIC
250end
251for i ulong ST_UNIT_BYTES
252put dst i get src i
253end
254if eq kase ST_CASE_BAD_CRC
255put dst ST_CRC_OFFSET xor get dst ST_CRC_OFFSET 1
256ret ST_ERR_BAD_CRC
257end
258if eq kase ST_CASE_WRONG_MAGIC
259for i ulong 8
260put dst i get st_bad_magic i
261end
262st_rechecksum dst
263ret ST_ERR_BAD_MAGIC
264end
265if eq kase ST_CASE_NONZERO_RES
266put dst 200 0x42
267st_rechecksum dst
268ret ST_ERR_NONZERO_RES
269end
270if eq kase ST_CASE_WRONG_SLOT
271st_put_u32_le dst 44 sub 1 slot
272st_rechecksum dst
273ret ST_ERR_WRONG_SLOT
274end
275if eq kase ST_CASE_REGION_MISMATCH
276reg = st_get_u64_le dst 48
277st_put_u64_le dst 48 add reg 1
278st_rechecksum dst
279ret ST_OK
280end
281if eq kase ST_CASE_UNSUP_VER
282st_put_u16_le dst 8 2
283st_rechecksum dst
284ret ST_ERR_UNSUP_VER
285end
286if eq kase ST_CASE_NEW_ROOT
287ret ST_OK
288end
289ret ST_ERR_AMBIGUOUS
290end
291
292note Select inactive superblock slot: zero slot, else lower generation
293fn st_inactive_slot s0 view uchar * s1 view uchar * to int
294z0 = true
295for i ulong ST_UNIT_BYTES
296if ne get s0 i 0
297z0 = false
298break
299end
300end
301if z0
302ret 0
303end
304z1 = true
305for i ulong ST_UNIT_BYTES
306if ne get s1 i 0
307z1 = false
308break
309end
310end
311if z1
312ret 1
313end
314r0 = st_sb_decode s0 0
315g0 = get st_sb_out 0
316r1 = st_sb_decode s1 1
317g1 = get st_sb_out 0
318if or ne r0 ST_OK ne r1 ST_OK
319ret ST_ERR_AMBIGUOUS
320end
321if lt g0 g1
322ret 0
323end
324if lt g1 g0
325ret 1
326end
327ret ST_ERR_AMBIGUOUS
328end
329
330note Recovery Core HMAC-SHA256(key[32], challenge[32])
331fn st_operator_respond key view uchar * chal view uchar * resp edit uchar * to void
332ipad = new uchar 64 0x36
333opad = new uchar 64 0x5c
334for i ulong 32
335put ipad i xor get key i 0x36
336put opad i xor get key i 0x5c
337end
338ctx = new ulong 40 0
339sha256_init ctx
340sha256_update ctx ipad
341sha256_update ctx chal
342inner = new uchar 32 0
343sha256_final ctx inner
344sha256_init ctx
345sha256_update ctx opad
346sha256_update ctx inner
347sha256_final ctx resp
348end