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 / protocol / interplane / v03 / aien_adapter.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/interplane/v03/aien_adapter.om: pure-Omega port of interplane-mv adapters/aien. |
| 3 | note Composes AIEN's existing gates and mints nothing: |
| 4 | note - SPECS enrollment: read_file (READ_FS), list_dir (READ_FS), write_file (WORLD_MUT), bash_eval (SPAWN|WORLD_MUT) |
| 5 | note - Fail-closed decide() when aegis gate is absent |
| 6 | note - HMAC-SHA256 ApprovalBinding under DeskKey (RFC 2104 / RFC 4231) |
| 7 | note - Two-phase approval spend (reserve / commit / release) with single-use grant replay refusal |
| 8 | note - Provenance bundle digest commitment |
| 9 | note Compose after si/directives/om2.om, om/tls/sha2.om, and protocol/interplane/v03/crossveil.om. |
| 10 | |
| 11 | const AIEN_CAP_READ_FILE ulong 1 |
| 12 | const AIEN_CAP_LIST_DIR ulong 2 |
| 13 | const AIEN_CAP_WRITE_FILE ulong 3 |
| 14 | const AIEN_CAP_BASH_EVAL ulong 4 |
| 15 | |
| 16 | const AIEN_EFF_READ_FS ulong 1 |
| 17 | const AIEN_EFF_WORLD_MUT ulong 2 |
| 18 | const AIEN_EFF_SPAWN_PROC ulong 4 |
| 19 | |
| 20 | const GRANT_UNUSED ulong 0 |
| 21 | const GRANT_RESERVED ulong 1 |
| 22 | const GRANT_SPENT ulong 2 |
| 23 | const GRANT_REVOKED ulong 3 |
| 24 | |
| 25 | const AIEN_OK ulong 0 |
| 26 | const AIEN_ERR_UNKNOWN_CAP ulong 1 |
| 27 | const AIEN_ERR_GATE_UNAVAIL ulong 2 |
| 28 | const AIEN_ERR_BAD_MAC ulong 3 |
| 29 | const AIEN_ERR_GRANT_STATE ulong 4 |
| 30 | const AIEN_ERR_GRANT_REPLAY ulong 5 |
| 31 | const AIEN_ERR_GRANT_REVOKED ulong 6 |
| 32 | |
| 33 | text aien_runtime_id "aien" |
| 34 | text aien_engine_effect "aien-mcp.EffectClassAuthority" |
| 35 | text aien_bind_ver "approval-binding/v1" |
| 36 | |
| 37 | note Capability enrollment (SPECS from adapters/aien/src/lib.rs) |
| 38 | fn aien_cap_effects cap_id ulong to ulong |
| 39 | if eq cap_id AIEN_CAP_READ_FILE |
| 40 | ret AIEN_EFF_READ_FS |
| 41 | end |
| 42 | if eq cap_id AIEN_CAP_LIST_DIR |
| 43 | ret AIEN_EFF_READ_FS |
| 44 | end |
| 45 | if eq cap_id AIEN_CAP_WRITE_FILE |
| 46 | ret AIEN_EFF_WORLD_MUT |
| 47 | end |
| 48 | if eq cap_id AIEN_CAP_BASH_EVAL |
| 49 | ret or AIEN_EFF_SPAWN_PROC AIEN_EFF_WORLD_MUT |
| 50 | end |
| 51 | ret ulong 0 |
| 52 | end |
| 53 | |
| 54 | note Fail-closed decide(): |
| 55 | note gate_linked: 0 -> always DENIED (UNAVAILABLE); 1 -> translates AIEN gate answer. |
| 56 | note gate_allow: 1 -> AUTHORIZED (for pure read_fs) or REQUIRES_APPROVAL (for world_mut/spawn unless approved_grant=1). |
| 57 | fn aien_decide cap_id ulong gate_linked ulong gate_allow ulong approved_grant ulong to ulong |
| 58 | eff = aien_cap_effects cap_id |
| 59 | if eq eff ulong 0 |
| 60 | ret CV_DEC_DENIED |
| 61 | end |
| 62 | if eq gate_linked ulong 0 |
| 63 | ret CV_DEC_DENIED |
| 64 | end |
| 65 | if eq gate_allow ulong 0 |
| 66 | ret CV_DEC_DENIED |
| 67 | end |
| 68 | note Pure read-only filesystem capability: AIEN gate Ok -> AUTHORIZED |
| 69 | if eq eff AIEN_EFF_READ_FS |
| 70 | ret CV_DEC_AUTHORIZED |
| 71 | end |
| 72 | note World mutation / process spawn requires an explicit AIEN-minted approval grant |
| 73 | if eq approved_grant ulong 1 |
| 74 | ret CV_DEC_AUTHORIZED |
| 75 | end |
| 76 | ret CV_DEC_REQUIRES_APPROVAL |
| 77 | end |
| 78 | |
| 79 | note --- RFC 2104 HMAC-SHA256 (for ApprovalBinding with DeskKey) --- |
| 80 | fn aien_hmac_prep_key key view uchar * kblk edit uchar 64 to void |
| 81 | kctx = new ulong 40 0 |
| 82 | kdig = new uchar 32 0 |
| 83 | klen = len key |
| 84 | if gt klen ulong 64 |
| 85 | sha256_init kctx |
| 86 | sha256_update kctx key |
| 87 | sha256_final kctx kdig |
| 88 | for i ulong 32 |
| 89 | put kblk i get kdig i |
| 90 | end |
| 91 | else |
| 92 | i = ulong 0 |
| 93 | while lt i klen bound 64 |
| 94 | put kblk i get key i |
| 95 | i = add i ulong 1 |
| 96 | end |
| 97 | end |
| 98 | end |
| 99 | |
| 100 | fn aien_hmac_inner kblk view uchar 64 msg view uchar * inner edit uchar 32 to void |
| 101 | ipad = new uchar 64 0 |
| 102 | ictx = new ulong 40 0 |
| 103 | for j ulong 64 |
| 104 | put ipad j xor get kblk j uchar 0x36 |
| 105 | end |
| 106 | sha256_init ictx |
| 107 | sha256_update ictx ipad |
| 108 | sha256_update ictx msg |
| 109 | sha256_final ictx inner |
| 110 | end |
| 111 | |
| 112 | fn aien_hmac_outer kblk view uchar 64 inner view uchar 32 mac edit uchar 32 to void |
| 113 | opad = new uchar 64 0 |
| 114 | octx = new ulong 40 0 |
| 115 | for j ulong 64 |
| 116 | put opad j xor get kblk j uchar 0x5c |
| 117 | end |
| 118 | sha256_init octx |
| 119 | sha256_update octx opad |
| 120 | sha256_update octx inner |
| 121 | sha256_final octx mac |
| 122 | end |
| 123 | |
| 124 | fn aien_hmac_sha256 key view uchar * msg view uchar * mac edit uchar 32 to void |
| 125 | kblk = new uchar 64 0 |
| 126 | inner = new uchar 32 0 |
| 127 | aien_hmac_prep_key key kblk |
| 128 | aien_hmac_inner kblk msg inner |
| 129 | aien_hmac_outer kblk inner mac |
| 130 | end |
| 131 | |
| 132 | fn aien_mac_eq a view uchar 32 b view uchar 32 to bool |
| 133 | diff = uchar 0 |
| 134 | for i ulong 32 |
| 135 | diff = or diff xor get a i get b i |
| 136 | end |
| 137 | ret eq diff uchar 0 |
| 138 | end |
| 139 | |
| 140 | note Two-phase single-use approval grant state machine (sovereign-core #260 / adapters/aien): |
| 141 | note UNUSED -> RESERVED (reserve) -> SPENT (commit) |
| 142 | note RESERVED -> UNUSED (release on aborted compose) |
| 143 | note UNUSED -> REVOKED (revoke before spend; spent grants cannot be un-spent) |
| 144 | fn aien_grant_reserve st edit ulong 1 to ulong |
| 145 | cur = get st 0 |
| 146 | if eq cur GRANT_SPENT |
| 147 | ret AIEN_ERR_GRANT_REPLAY |
| 148 | end |
| 149 | if eq cur GRANT_REVOKED |
| 150 | ret AIEN_ERR_GRANT_REVOKED |
| 151 | end |
| 152 | if ne cur GRANT_UNUSED |
| 153 | ret AIEN_ERR_GRANT_STATE |
| 154 | end |
| 155 | put st 0 GRANT_RESERVED |
| 156 | ret AIEN_OK |
| 157 | end |
| 158 | |
| 159 | fn aien_grant_commit st edit ulong 1 to ulong |
| 160 | cur = get st 0 |
| 161 | if eq cur GRANT_SPENT |
| 162 | ret AIEN_ERR_GRANT_REPLAY |
| 163 | end |
| 164 | if ne cur GRANT_RESERVED |
| 165 | ret AIEN_ERR_GRANT_STATE |
| 166 | end |
| 167 | put st 0 GRANT_SPENT |
| 168 | ret AIEN_OK |
| 169 | end |
| 170 | |
| 171 | fn aien_grant_release st edit ulong 1 to ulong |
| 172 | cur = get st 0 |
| 173 | if ne cur GRANT_RESERVED |
| 174 | ret AIEN_ERR_GRANT_STATE |
| 175 | end |
| 176 | put st 0 GRANT_UNUSED |
| 177 | ret AIEN_OK |
| 178 | end |
| 179 | |
| 180 | fn aien_grant_revoke st edit ulong 1 to ulong |
| 181 | cur = get st 0 |
| 182 | if eq cur GRANT_SPENT |
| 183 | ret AIEN_ERR_GRANT_REPLAY |
| 184 | end |
| 185 | put st 0 GRANT_REVOKED |
| 186 | ret AIEN_OK |
| 187 | end |