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-crypto / crypto.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-crypto/crypto.om: Pure-Omega AIENOS Cryptographic Primitives. |
| 3 | note Converted from aienux crates/aienos-crypto/src/{lib,aes,polyval,aes_gcm_siv,sha256}.rs. |
| 4 | note Constant-time GF(2^8) AES S-box, FIPS 197 AES-256 block cipher, RFC 8452 POLYVAL, |
| 5 | note RFC 8452 AES-256-GCM-SIV AEAD, FIPS 180-4 SHA-256, and RFC 4231 HMAC-SHA256. |
| 6 | note Compose with: si/directives/om2.om om/tls/sha2.om om/tls/aesgcm.om om/tls/gcmsiv.om aienux/crates/aienos-crypto/crypto.om ... |
| 7 | |
| 8 | const CR_OK int 0 |
| 9 | const CR_ERR_AUTH int -1 |
| 10 | const CR_ERR_PARAM int -2 |
| 11 | |
| 12 | const CR_KEY_LEN ulong 32 |
| 13 | const CR_NONCE_LEN ulong 12 |
| 14 | const CR_TAG_LEN ulong 16 |
| 15 | const CR_BLOCK_LEN ulong 16 |
| 16 | |
| 17 | note Wipe a byte buffer to zero |
| 18 | fn cr_wipe buf edit uchar * to void |
| 19 | n = len buf |
| 20 | for i ulong n bound 65536 |
| 21 | put buf i 0 |
| 22 | end |
| 23 | end |
| 24 | |
| 25 | note Constant-time equality for two byte slices of equal length |
| 26 | fn cr_ct_eq a view uchar * b view uchar * to bool |
| 27 | if ne len a len b |
| 28 | ret false |
| 29 | end |
| 30 | diff = uchar 0 |
| 31 | n = len a |
| 32 | for i ulong n bound 65536 |
| 33 | diff = or diff xor get a i get b i |
| 34 | end |
| 35 | ret eq diff 0 |
| 36 | end |
| 37 | |
| 38 | note --- Constant-time GF(2^8) AES S-box (matches aes.rs sub_bytes_ct) --- |
| 39 | |
| 40 | fn cr_xtime x uchar to uchar |
| 41 | hi = shr x 7 |
| 42 | mask = sub uchar 0 hi |
| 43 | ret xor shl x 1 and uchar 0x1b mask |
| 44 | end |
| 45 | |
| 46 | fn cr_gf_mul a uchar b uchar to uchar |
| 47 | acc = uchar 0 |
| 48 | cur = a |
| 49 | for i ulong 8 unroll |
| 50 | bit = and shr b uchar i 1 |
| 51 | mask = sub uchar 0 bit |
| 52 | acc = xor acc and cur mask |
| 53 | cur = cr_xtime cur |
| 54 | end |
| 55 | ret acc |
| 56 | end |
| 57 | |
| 58 | fn cr_rotl8 x uchar k uchar to uchar |
| 59 | rk = sub uchar 8 k |
| 60 | ret or shl x k shr x rk |
| 61 | end |
| 62 | |
| 63 | note S(b) = Affine(b^254) in GF(2^8), branch-free and table-free |
| 64 | fn cr_sbox_ct x uchar to uchar |
| 65 | x2 = cr_gf_mul x x |
| 66 | x3 = cr_gf_mul x2 x |
| 67 | t = cr_gf_mul x3 x3 |
| 68 | x12 = cr_gf_mul t t |
| 69 | t = cr_gf_mul x12 x3 |
| 70 | t = cr_gf_mul t t |
| 71 | t = cr_gf_mul t t |
| 72 | t = cr_gf_mul t t |
| 73 | t = cr_gf_mul t t |
| 74 | t = cr_gf_mul t x12 |
| 75 | inv = cr_gf_mul t x2 |
| 76 | acc = xor inv cr_rotl8 inv 1 |
| 77 | acc = xor acc cr_rotl8 inv 2 |
| 78 | acc = xor acc cr_rotl8 inv 3 |
| 79 | acc = xor acc cr_rotl8 inv 4 |
| 80 | ret xor acc uchar 0x63 |
| 81 | end |
| 82 | |
| 83 | note Single-block FIPS 197 AES-256 encryption via om/tls/aesgcm.om bitsliced AES |
| 84 | fn cr_aes256_encrypt_block key view uchar * blk edit uchar * to int |
| 85 | if or ne len key 32 ne len blk 16 |
| 86 | ret CR_ERR_PARAM |
| 87 | end |
| 88 | k = new ulong AGKN 0 |
| 89 | s = new ulong AGSN 0 |
| 90 | aes_setkey k s key |
| 91 | hi = siv_be64 view blk 0 |
| 92 | lo = siv_be64 view blk 8 |
| 93 | put s 150 hi |
| 94 | put s 151 lo |
| 95 | put s 152 0 |
| 96 | put s 153 0 |
| 97 | put s 154 0 |
| 98 | put s 155 0 |
| 99 | put s 156 0 |
| 100 | put s 157 0 |
| 101 | aes_enc4 k s |
| 102 | siv_put8 blk 0 get s 150 |
| 103 | siv_put8 blk 8 get s 151 |
| 104 | gcm_clear k s |
| 105 | ret CR_OK |
| 106 | end |
| 107 | |
| 108 | note --- Constant-time POLYVAL (RFC 8452 Section 3, exact port of polyval.rs) --- |
| 109 | |
| 110 | fn cr_polyval_div_x v edit uchar 16 to void |
| 111 | lsb = and get v 0 1 |
| 112 | carry = uchar 0 |
| 113 | for idx ulong 16 unroll |
| 114 | i = sub 15 idx |
| 115 | cur = get v i |
| 116 | next_carry = and cur 1 |
| 117 | put v i or shr cur 1 shl carry 7 |
| 118 | carry = next_carry |
| 119 | end |
| 120 | mask = sub uchar 0 lsb |
| 121 | put v 15 xor get v 15 and uchar 0xe1 mask |
| 122 | end |
| 123 | |
| 124 | fn cr_polyval_xor_masked v edit uchar 16 b view uchar 16 mask uchar to void |
| 125 | for j ulong 16 unroll |
| 126 | put v j xor get v j and get b j mask |
| 127 | end |
| 128 | end |
| 129 | |
| 130 | note dot(a, b) = a * b * x^-128 mod (x^128 + x^127 + x^126 + x^121 + 1) |
| 131 | fn cr_polyval_dot a view uchar 16 b view uchar 16 dst edit uchar 16 to int |
| 132 | v = new uchar 16 0 |
| 133 | for i ulong 128 bound 128 |
| 134 | ab = get a div i 8 |
| 135 | sh = uchar rem i 8 |
| 136 | bit = and shr ab sh 1 |
| 137 | mask = sub uchar 0 bit |
| 138 | cr_polyval_xor_masked v b mask |
| 139 | cr_polyval_div_x v |
| 140 | end |
| 141 | for j ulong 16 unroll |
| 142 | put dst j get v j |
| 143 | put v j 0 |
| 144 | end |
| 145 | ret CR_OK |
| 146 | end |
| 147 | |
| 148 | fn cr_polyval_update_block h view uchar 16 acc edit uchar 16 blk view uchar 16 to int |
| 149 | tmp = new uchar 16 0 |
| 150 | for i ulong 16 unroll |
| 151 | put tmp i xor get acc i get blk i |
| 152 | end |
| 153 | rc = cr_polyval_dot view tmp h acc |
| 154 | for i ulong 16 unroll |
| 155 | put tmp i 0 |
| 156 | end |
| 157 | ret rc |
| 158 | end |
| 159 | |
| 160 | note --- AES-256-GCM-SIV (RFC 8452, wrapping om/tls/gcmsiv.om) --- |
| 161 | |
| 162 | fn cr_gcmsiv_derive_keys key view uchar 32 nonce view uchar 12 auth_out edit uchar 16 enc_out edit uchar 32 to int |
| 163 | kn = new uchar 44 0 |
| 164 | for i ulong 32 |
| 165 | put kn i get key i |
| 166 | end |
| 167 | for i ulong 12 |
| 168 | put kn add 32 i get nonce i |
| 169 | end |
| 170 | k = new ulong AGKN 0 |
| 171 | s = new ulong AGSN 0 |
| 172 | ek = new uchar 32 0 |
| 173 | siv_derive k s view kn 32 ek |
| 174 | siv_put8 auth_out 0 get k 131 |
| 175 | siv_put8 auth_out 8 get k 132 |
| 176 | for i ulong 32 |
| 177 | put enc_out i get ek i |
| 178 | put ek i 0 |
| 179 | end |
| 180 | for i ulong 44 |
| 181 | put kn i 0 |
| 182 | end |
| 183 | gcm_clear k s |
| 184 | ret CR_OK |
| 185 | end |
| 186 | |
| 187 | note Pack key(32) || nonce(12) into kn(44) |
| 188 | fn cr_gcmsiv_pack_kn key view uchar 32 nonce view uchar 12 kn edit uchar 44 to void |
| 189 | for i ulong 32 |
| 190 | put kn i get key i |
| 191 | end |
| 192 | for i ulong 12 |
| 193 | put kn add 32 i get nonce i |
| 194 | end |
| 195 | end |
| 196 | |
| 197 | note Encrypt in-place buf = pt || 16B room using kn = key(32) || nonce(12) |
| 198 | fn cr_gcmsiv_seal_kn kn view uchar 44 aad view uchar * buf edit uchar * to int |
| 199 | if lt len buf 16 |
| 200 | ret CR_ERR_PARAM |
| 201 | end |
| 202 | rc = aes_gcmsiv_seal kn aad buf |
| 203 | if ne rc 0 |
| 204 | ret CR_ERR_PARAM |
| 205 | end |
| 206 | ret CR_OK |
| 207 | end |
| 208 | |
| 209 | note Decrypt in-place buf = ct || tag using kn = key(32) || nonce(12); zeroes buf on auth failure |
| 210 | fn cr_gcmsiv_open_kn kn view uchar 44 aad view uchar * buf edit uchar * to int |
| 211 | if lt len buf 16 |
| 212 | cr_wipe buf |
| 213 | ret CR_ERR_AUTH |
| 214 | end |
| 215 | rc = aes_gcmsiv_open kn aad buf |
| 216 | if ne rc 0 |
| 217 | cr_wipe buf |
| 218 | ret CR_ERR_AUTH |
| 219 | end |
| 220 | ret CR_OK |
| 221 | end |
| 222 | |
| 223 | note --- SHA-256 and HMAC-SHA256 (wrapping om/tls/sha2.om) --- |
| 224 | |
| 225 | fn cr_sha256 msg view uchar * digest edit uchar 32 to int |
| 226 | dl = sha2_digest 256 msg digest |
| 227 | if ne dl 32 |
| 228 | ret CR_ERR_PARAM |
| 229 | end |
| 230 | ret CR_OK |
| 231 | end |
| 232 | |
| 233 | note Two-part HMAC-SHA256 for 32-byte key (matches sha256.rs hmac_sha256) |
| 234 | fn cr_hmac_sha256_2 key view uchar 32 part1 view uchar * part2 view uchar * digest edit uchar 32 to int |
| 235 | ipad = new uchar 64 0x36 |
| 236 | opad = new uchar 64 0x5c |
| 237 | for i ulong 32 |
| 238 | kb = get key i |
| 239 | put ipad i xor get ipad i kb |
| 240 | put opad i xor get opad i kb |
| 241 | end |
| 242 | ctx = new ulong 40 0 |
| 243 | sha256_init ctx |
| 244 | sha256_update ctx view ipad |
| 245 | sha256_update ctx part1 |
| 246 | sha256_update ctx part2 |
| 247 | inner = new uchar 32 0 |
| 248 | sha256_final ctx inner |
| 249 | sha256_init ctx |
| 250 | sha256_update ctx view opad |
| 251 | sha256_update ctx view inner |
| 252 | sha256_final ctx digest |
| 253 | for i ulong 64 |
| 254 | put ipad i 0 |
| 255 | put opad i 0 |
| 256 | end |
| 257 | for i ulong 32 |
| 258 | put inner i 0 |
| 259 | end |
| 260 | ret CR_OK |
| 261 | end |