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