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 / atlas / lib.om

617 lines · 13978 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/atlas/lib.om: Pure-Omega Atlas Stage-0 Boot Verification Engine.
3note Faithful OM-2 library (no om2 line, no fn main) derived from upstream atlas
4note (CONTRACT-QEMU-VIRT-AARCH64-M1, atlas.s, sha256_clean.c, machine_contract.json).
5note Compose with: si/directives/om2.om aienux/atlas/lib.om <main.om|test.om>
6
7struct AtlasHeader magic uint ver uint stage_len ulong entry_off ulong epoch ulong end
8
9struct AtlasBootDesc ram_base ulong ram_size ulong uart_base ulong payload_base ulong payload_size ulong entry_addr ulong desc_addr ulong stack_top ulong cookie ulong epoch ulong verdict uint halted uint end
10
11note Contract constants (CONTRACT-QEMU-VIRT-AARCH64-M1)
12const ATLAS_MAGIC_LE uint 0x534c5441
13const ATLAS_HDR_VER uint 1
14const ATLAS_HDR_SIZE ulong 64
15const ATLAS_RAM_BASE ulong 0x40000000
16const ATLAS_RAM_SIZE ulong 0x08000000
17const ATLAS_UART_BASE ulong 0x09000000
18const ATLAS_STACK_BASE ulong 0x401f0000
19const ATLAS_STACK_TOP ulong 0x401fc000
20const ATLAS_GUARD_GAP ulong 8192
21const ATLAS_DESC_ADDR ulong 0x401fe000
22const ATLAS_DESC_SIZE ulong 64
23const ATLAS_PAYLOAD_BASE ulong 0x40200000
24const ATLAS_MAX_STAGE ulong 0x04000000
25const ATLAS_COOKIE ulong 0x5048595349435330
26
27note Verification status / fail-closed refusal codes
28const ATLAS_OK uint 0
29const ATLAS_ERR_SHORT_HDR uint 1
30const ATLAS_ERR_MAGIC uint 2
31const ATLAS_ERR_VERSION uint 3
32const ATLAS_ERR_STAGE_LEN uint 4
33const ATLAS_ERR_ALIGN uint 5
34const ATLAS_ERR_ENTRY_OOB uint 6
35const ATLAS_ERR_TRUNCATED uint 7
36const ATLAS_ERR_ROLLBACK uint 8
37const ATLAS_ERR_MEMMAP uint 9
38const ATLAS_ERR_DIGEST uint 10
39
40note ---- Self-contained FIPS 180-4 SHA-256 (matching upstream sha256_clean.c) ----
41
42fn atlas_sha256_k k edit uint 64 to void
43put k 0 0x428a2f98
44put k 1 0x71374491
45put k 2 0xb5c0fbcf
46put k 3 0xe9b5dba5
47put k 4 0x3956c25b
48put k 5 0x59f111f1
49put k 6 0x923f82a4
50put k 7 0xab1c5ed5
51put k 8 0xd807aa98
52put k 9 0x12835b01
53put k 10 0x243185be
54put k 11 0x550c7dc3
55put k 12 0x72be5d74
56put k 13 0x80deb1fe
57put k 14 0x9bdc06a7
58put k 15 0xc19bf174
59put k 16 0xe49b69c1
60put k 17 0xefbe4786
61put k 18 0x0fc19dc6
62put k 19 0x240ca1cc
63put k 20 0x2de92c6f
64put k 21 0x4a7484aa
65put k 22 0x5cb0a9dc
66put k 23 0x76f988da
67put k 24 0x983e5152
68put k 25 0xa831c66d
69put k 26 0xb00327c8
70put k 27 0xbf597fc7
71put k 28 0xc6e00bf3
72put k 29 0xd5a79147
73put k 30 0x06ca6351
74put k 31 0x14292967
75put k 32 0x27b70a85
76put k 33 0x2e1b2138
77put k 34 0x4d2c6dfc
78put k 35 0x53380d13
79put k 36 0x650a7354
80put k 37 0x766a0abb
81put k 38 0x81c2c92e
82put k 39 0x92722c85
83put k 40 0xa2bfe8a1
84put k 41 0xa81a664b
85put k 42 0xc24b8b70
86put k 43 0xc76c51a3
87put k 44 0xd192e819
88put k 45 0xd6990624
89put k 46 0xf40e3585
90put k 47 0x106aa070
91put k 48 0x19a4c116
92put k 49 0x1e376c08
93put k 50 0x2748774c
94put k 51 0x34b0bcb5
95put k 52 0x391c0cb3
96put k 53 0x4ed8aa4a
97put k 54 0x5b9cca4f
98put k 55 0x682e6ff3
99put k 56 0x748f82ee
100put k 57 0x78a5636f
101put k 58 0x84c87814
102put k 59 0x8cc70208
103put k 60 0x90befffa
104put k 61 0xa4506ceb
105put k 62 0xbef9a3f7
106put k 63 0xc67178f2
107end
108
109fn atlas_be32 buf view uchar * off ulong to uint
110b0 = shl uint get buf off 24
111b1 = shl uint get buf add off 1 16
112b2 = shl uint get buf add off 2 8
113b3 = uint get buf add off 3
114ret or or b0 b1 or b2 b3
115end
116
117fn atlas_s0 x uint to uint
118ret xor xor rotr x 7 rotr x 18 shr x 3
119end
120
121fn atlas_s1 x uint to uint
122ret xor xor rotr x 17 rotr x 19 shr x 10
123end
124
125fn atlas_cap0 x uint to uint
126ret xor xor rotr x 2 rotr x 13 rotr x 22
127end
128
129fn atlas_cap1 x uint to uint
130ret xor xor rotr x 6 rotr x 11 rotr x 25
131end
132
133fn atlas_ch x uint y uint z uint to uint
134ret xor and x y and not x z
135end
136
137fn atlas_maj x uint y uint z uint to uint
138ret xor xor and x y and x z and y z
139end
140
141fn atlas_sha256_sched w edit uint 64 blk view uchar * boff ulong to void
142for i ulong 16
143put w i atlas_be32 blk add boff mul i 4
144end
145i = ulong 16
146while lt i 64 bound 64
147t0 = atlas_s0 get w sub i 15
148t1 = atlas_s1 get w sub i 2
149val = add add get w sub i 16 t0 add get w sub i 7 t1
150put w i val
151i = add i 1
152end
153end
154
155fn atlas_sha256_compress st edit uint 8 w view uint 64 k view uint 64 to void
156v = new uint 8 0
157for i ulong 8
158put v i get st i
159end
160for i ulong 64
161t1 = add get v 7 atlas_cap1 get v 4
162t1 = add t1 atlas_ch get v 4 get v 5 get v 6
163t1 = add t1 get k i
164t1 = add t1 get w i
165t2 = add atlas_cap0 get v 0 atlas_maj get v 0 get v 1 get v 2
166put v 7 get v 6
167put v 6 get v 5
168put v 5 get v 4
169put v 4 add get v 3 t1
170put v 3 get v 2
171put v 2 get v 1
172put v 1 get v 0
173put v 0 add t1 t2
174end
175for i ulong 8
176put st i add get st i get v i
177put v i 0
178end
179end
180
181fn atlas_sha256_block st edit uint 8 k view uint 64 blk view uchar * boff ulong to void
182w = new uint 64 0
183atlas_sha256_sched w blk boff
184atlas_sha256_compress st w k
185for i ulong 64
186put w i 0
187end
188end
189
190fn atlas_put_be64 buf edit uchar 128 off ulong v ulong to void
191for j ulong 8
192sh = mul sub 7 j 8
193b = uchar and shr v sh 0xff
194put buf add off j b
195end
196end
197
198fn atlas_sha256_pad_fill pad edit uchar 128 msg view uchar * off ulong to ulong
199n = len msg
200remn = sub n off
201i = ulong 0
202while lt i remn bound 64
203put pad i get msg add off i
204i = add i 1
205end
206put pad remn uchar 0x80
207pblks = ulong 1
208if ge remn 56
209pblks = ulong 2
210end
211atlas_put_be64 pad sub mul pblks 64 8 shl n 3
212ret pblks
213end
214
215fn atlas_sha256_pad_final st edit uint 8 k view uint 64 msg view uchar * off ulong to void
216pad = new uchar 128 0
217pblks = atlas_sha256_pad_fill pad msg off
218atlas_sha256_block st k pad 0
219if eq pblks 2
220atlas_sha256_block st k pad 64
221end
222for j ulong 128
223put pad j 0
224end
225end
226
227fn atlas_sha256_emit st edit uint 8 digest edit uchar 32 to void
228for j ulong 8
229wd = get st j
230bo = mul j 4
231put digest bo uchar and shr wd 24 0xff
232put digest add bo 1 uchar and shr wd 16 0xff
233put digest add bo 2 uchar and shr wd 8 0xff
234put digest add bo 3 uchar and wd 0xff
235put st j 0
236end
237end
238
239fn atlas_sha256_compute msg view uchar * digest edit uchar 32 to void
240st = new uint 8 0
241k = new uint 64 0
242atlas_sha256_k k
243put st 0 0x6a09e667
244put st 1 0xbb67ae85
245put st 2 0x3c6ef372
246put st 3 0xa54ff53a
247put st 4 0x510e527f
248put st 5 0x9b05688c
249put st 6 0x1f83d9ab
250put st 7 0x5be0cd19
251off = ulong 0
252remn = len msg
253note Each iteration consumes 64 bytes; bound 1048576 supports up to 64 MiB stages.
254while ge remn 64 bound 1048576
255atlas_sha256_block st k msg off
256off = add off 64
257remn = sub remn 64
258end
259atlas_sha256_pad_final st k msg off
260atlas_sha256_emit st digest
261end
262
263note ---- Constant-time 32-byte digest comparison (zero early exit, zero secret branches) ----
264
265fn atlas_ct_eq32 a view uchar * b view uchar * to bool
266if ne len a 32
267ret false
268end
269if ne len b 32
270ret false
271end
272acc = uint 0
273for i ulong 32
274da = uint get a i
275db = uint get b i
276acc = or acc xor da db
277end
278ret eq acc 0
279end
280
281note ---- Little-endian wire encoding/decoding ----
282
283fn atlas_le32 buf view uchar * off ulong to uint
284b0 = uint get buf off
285b1 = shl uint get buf add off 1 8
286b2 = shl uint get buf add off 2 16
287b3 = shl uint get buf add off 3 24
288ret or or b0 b1 or b2 b3
289end
290
291fn atlas_le64 buf view uchar * off ulong to ulong
292lo = ulong atlas_le32 buf off
293hi = ulong atlas_le32 buf add off 4
294ret or lo shl hi 32
295end
296
297fn atlas_put_le32 buf edit uchar * off ulong v uint to void
298put buf off uchar and v 0xff
299put buf add off 1 uchar and shr v 8 0xff
300put buf add off 2 uchar and shr v 16 0xff
301put buf add off 3 uchar and shr v 24 0xff
302end
303
304fn atlas_put_le64 buf edit uchar * off ulong v ulong to void
305for i ulong 8
306put buf add off i uchar and shr v mul i 8 0xff
307end
308end
309
310note ---- Atlas Header encode/parse ----
311
312fn atlas_header_encode buf edit uchar * hdr view AtlasHeader exp view uchar 32 to bool
313if lt len buf ATLAS_HDR_SIZE
314ret false
315end
316atlas_put_le32 buf 0 get hdr magic
317atlas_put_le32 buf 4 get hdr ver
318atlas_put_le64 buf 8 get hdr stage_len
319atlas_put_le64 buf 16 get hdr entry_off
320atlas_put_le64 buf 24 get hdr epoch
321for i ulong 32
322put buf add 32 i get exp i
323end
324ret true
325end
326
327fn atlas_header_parse buf view uchar * hdr edit AtlasHeader exp edit uchar 32 to uint
328if lt len buf ATLAS_HDR_SIZE
329ret ATLAS_ERR_SHORT_HDR
330end
331put hdr magic atlas_le32 buf 0
332put hdr ver atlas_le32 buf 4
333put hdr stage_len atlas_le64 buf 8
334put hdr entry_off atlas_le64 buf 16
335put hdr epoch atlas_le64 buf 24
336for i ulong 32
337put exp i get buf add 32 i
338end
339if ne get hdr magic ATLAS_MAGIC_LE
340ret ATLAS_ERR_MAGIC
341end
342if ne get hdr ver ATLAS_HDR_VER
343ret ATLAS_ERR_VERSION
344end
345ret ATLAS_OK
346end
347
348note ---- Memory map & disjointness proof check (CONTRACT-QEMU-VIRT-AARCH64-M1) ----
349
350fn atlas_add_overflow a ulong b ulong to bool
351s = add a b
352ret lt s a
353end
354
355fn atlas_check_memmap stack_base ulong stack_top ulong desc_addr ulong payload_base ulong stage_len ulong to bool
356if ge stack_base stack_top
357ret false
358end
359if ne and stack_top 15 0
360ret false
361end
362if ne and desc_addr 63 0
363ret false
364end
365if ne and payload_base 63 0
366ret false
367end
368if atlas_add_overflow stack_top ATLAS_GUARD_GAP
369ret false
370end
371if gt add stack_top ATLAS_GUARD_GAP desc_addr
372ret false
373end
374if atlas_add_overflow desc_addr ATLAS_DESC_SIZE
375ret false
376end
377if gt add desc_addr ATLAS_DESC_SIZE payload_base
378ret false
379end
380if lt payload_base ATLAS_RAM_BASE
381ret false
382end
383if atlas_add_overflow payload_base stage_len
384ret false
385end
386if atlas_add_overflow ATLAS_RAM_BASE ATLAS_RAM_SIZE
387ret false
388end
389if gt add payload_base stage_len add ATLAS_RAM_BASE ATLAS_RAM_SIZE
390ret false
391end
392ret true
393end
394
395note ---- Fail-closed descriptor zeroing & full stage-0 verification ----
396
397fn atlas_desc_halt desc edit AtlasBootDesc code uint to void
398put desc ram_base 0
399put desc ram_size 0
400put desc uart_base 0
401put desc payload_base 0
402put desc payload_size 0
403put desc entry_addr 0
404put desc desc_addr 0
405put desc stack_top 0
406put desc cookie 0
407put desc epoch 0
408put desc verdict code
409put desc halted 1
410end
411
412fn atlas_validate_hdr hdr view AtlasHeader img_len ulong min_epoch ulong to uint
413slen = get hdr stage_len
414eoff = get hdr entry_off
415ep = get hdr epoch
416if eq slen 0
417ret ATLAS_ERR_STAGE_LEN
418end
419if gt slen ATLAS_MAX_STAGE
420ret ATLAS_ERR_STAGE_LEN
421end
422if ne and slen 3 0
423ret ATLAS_ERR_ALIGN
424end
425if ne and eoff 3 0
426ret ATLAS_ERR_ALIGN
427end
428if ge eoff slen
429ret ATLAS_ERR_ENTRY_OOB
430end
431if atlas_add_overflow ATLAS_HDR_SIZE slen
432ret ATLAS_ERR_STAGE_LEN
433end
434if lt img_len add ATLAS_HDR_SIZE slen
435ret ATLAS_ERR_TRUNCATED
436end
437if lt ep min_epoch
438ret ATLAS_ERR_ROLLBACK
439end
440if not atlas_check_memmap ATLAS_STACK_BASE ATLAS_STACK_TOP ATLAS_DESC_ADDR ATLAS_PAYLOAD_BASE slen
441ret ATLAS_ERR_MEMMAP
442end
443ret ATLAS_OK
444end
445
446fn atlas_desc_fill desc edit AtlasBootDesc hdr view AtlasHeader to void
447put desc ram_base ATLAS_RAM_BASE
448put desc ram_size ATLAS_RAM_SIZE
449put desc uart_base ATLAS_UART_BASE
450put desc payload_base ATLAS_PAYLOAD_BASE
451put desc payload_size get hdr stage_len
452put desc entry_addr add ATLAS_PAYLOAD_BASE get hdr entry_off
453put desc desc_addr ATLAS_DESC_ADDR
454put desc stack_top ATLAS_STACK_TOP
455put desc cookie ATLAS_COOKIE
456put desc epoch get hdr epoch
457put desc verdict ATLAS_OK
458put desc halted 0
459end
460
461fn atlas_verify_image img view uchar * min_epoch ulong hdr edit AtlasHeader desc edit AtlasBootDesc comp edit uchar 32 to uint
462exp = new uchar 32 0
463atlas_desc_halt desc ATLAS_ERR_SHORT_HDR
464for i ulong 32
465put comp i 0
466end
467rc = atlas_header_parse img hdr exp
468if ne rc ATLAS_OK
469atlas_desc_halt desc rc
470ret rc
471end
472rc = atlas_validate_hdr hdr len img min_epoch
473if ne rc ATLAS_OK
474atlas_desc_halt desc rc
475ret rc
476end
477atlas_sha256_compute part img ATLAS_HDR_SIZE get hdr stage_len comp
478if not atlas_ct_eq32 comp exp
479atlas_desc_halt desc ATLAS_ERR_DIGEST
480ret ATLAS_ERR_DIGEST
481end
482atlas_desc_fill desc hdr
483ret ATLAS_OK
484end
485
486note ---- Machine Boot Descriptor 64-byte wire emitter (matches atlas.s offsets) ----
487note +0: ram_base, +8: ram_size, +16: uart_base, +24: payload_base,
488note +32: payload_size, +40: entry_addr, +48: epoch, +56: cookie
489
490fn atlas_emit_desc_bytes desc view AtlasBootDesc dst edit uchar 64 to bool
491for i ulong 64
492put dst i 0
493end
494if ne get desc verdict ATLAS_OK
495ret false
496end
497if ne get desc halted 0
498ret false
499end
500if ne get desc cookie ATLAS_COOKIE
501ret false
502end
503atlas_put_le64 dst 0 get desc ram_base
504atlas_put_le64 dst 8 get desc ram_size
505atlas_put_le64 dst 16 get desc uart_base
506atlas_put_le64 dst 24 get desc payload_base
507atlas_put_le64 dst 32 get desc payload_size
508atlas_put_le64 dst 40 get desc entry_addr
509atlas_put_le64 dst 48 get desc epoch
510atlas_put_le64 dst 56 get desc cookie
511ret true
512end
513
514note ---- I/O and hex formatting helpers ----
515
516fn atlas_write_all fd int buf view uchar * to bool
517n = len buf
518off = ulong 0
519while lt off n bound 65536
520k = write fd buf off sub n off
521if le k 0
522ret false
523end
524off = add off ulong k
525end
526ret true
527end
528
529fn atlas_hex_nibble v uchar to uchar
530x = and v 15
531if lt x 10
532ret add 48 x
533end
534ret add 87 x
535end
536
537fn atlas_parse_hex_nibble c uchar to int
538if ge c 48
539if le c 57
540ret int sub c 48
541end
542end
543if ge c 97
544if le c 102
545ret add 10 int sub c 97
546end
547end
548if ge c 65
549if le c 70
550ret add 10 int sub c 65
551end
552end
553ret -1
554end
555
556fn atlas_parse_hex_bytes hex view uchar * dst edit uchar * to bool
557hlen = len hex
558if ne and hlen 1 0
559ret false
560end
561need = shr hlen 1
562if lt len dst need
563ret false
564end
565i = ulong 0
566while lt i need bound 65536
567hi = atlas_parse_hex_nibble get hex mul i 2
568lo = atlas_parse_hex_nibble get hex add mul i 2 1
569if lt hi 0
570ret false
571end
572if lt lo 0
573ret false
574end
575put dst i uchar or shl hi 4 lo
576i = add i 1
577end
578ret true
579end
580
581fn atlas_write_hex64 fd int v ulong to bool
582buf = new uchar 18 0
583put buf 0 uchar 48
584put buf 1 uchar 120
585for i ulong 16
586sh = mul sub 15 i 4
587nib = uchar and shr v sh 15
588put buf add 2 i atlas_hex_nibble nib
589end
590ret atlas_write_all fd buf
591end
592
593fn atlas_write_digest fd int d view uchar 32 to bool
594buf = new uchar 64 0
595for i ulong 32
596b = get d i
597put buf mul i 2 atlas_hex_nibble shr b 4
598put buf add mul i 2 1 atlas_hex_nibble and b 15
599end
600ret atlas_write_all fd buf
601end
602
603fn atlas_write_u64 fd int v ulong to bool
604buf = new uchar 24 0
605if eq v 0
606put buf 0 uchar 48
607ret atlas_write_all fd part buf 0 1
608end
609cur = v
610pos = ulong 24
611while gt cur 0 bound 24
612pos = sub pos 1
613put buf pos add uchar 48 uchar rem cur 10
614cur = div cur 10
615end
616ret atlas_write_all fd part buf pos sub 24 pos
617end