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 / om / tls / gcmsiv.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note gcmsiv.om: AES-GCM-SIV (RFC 8452) for om/tls, module code GS. It reuses the AES block cipher |
| 3 | note and the masked GF(2^128) multiply of aesgcm.om, which must come before it in the unit. |
| 4 | note Library unit: no om2 line, no fn main, no mutable globals. Not part of the TLS client build; |
| 5 | note programs that need it compose `om2`, aesgcm.om, gcmsiv.om, then the program. |
| 6 | note |
| 7 | note API (return codes: 0 ok, 1 authentication failure (open only), 2 bad parameters): |
| 8 | note aes_gcmsiv_seal kn aad buf / aes_gcmsiv_open kn aad buf |
| 9 | note AEAD_AES_128_GCM_SIV and AEAD_AES_256_GCM_SIV (RFC 8452 section 6): kn = key-generating |
| 10 | note key (16 or 32 bytes) || nonce (12 bytes), so len kn is 28 or 44. |
| 11 | note seal: buf = plaintext || 16 bytes of room; becomes ciphertext || tag. The same key, nonce, |
| 12 | note aad and plaintext always give the same output (the scheme is deterministic by design). |
| 13 | note open: buf = ciphertext || tag. The ciphertext is decrypted in place, the tag is recomputed |
| 14 | note over the result and compared with an or-accumulated comparison of both 64-bit halves; on a |
| 15 | note mismatch buf[0 .. n) is zeroed before returning 1, so no unauthenticated plaintext is |
| 16 | note released (RFC 8452 section 5). |
| 17 | note 2 (nothing written) when len kn is not 28 or 44, len buf < 16, or the plaintext or aad is |
| 18 | note longer than 2^36 bytes (P_MAX, A_MAX). |
| 19 | note |
| 20 | note Construction (RFC 8452 sections 3 to 5): |
| 21 | note keys: AES(K, LE32(i) || nonce)[0 .. 8) for i = 0, 1 (authentication key) and 2, 3 (and 4, 5 |
| 22 | note for AES-256) (encryption key). |
| 23 | note POLYVAL through GHASH (RFC 8452 Appendix A): POLYVAL(H, X1 .. Xn) = |
| 24 | note ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X1) .. ByteReverse(Xn))), with |
| 25 | note gcm_mul of aesgcm.om as the GHASH multiply. Inputs: pad16(aad), pad16(plaintext), then the |
| 26 | note length block LE64(8 len aad) || LE64(8 len plaintext). |
| 27 | note tag: AES(encryption key, (S ^ nonce) with bit 7 of byte 15 cleared). |
| 28 | note CTR: the counter block is the tag with bit 7 of byte 15 set; bytes 0..3 count as a little-endian |
| 29 | note 32-bit integer that wraps at 2^32. |
| 30 | note |
| 31 | note Constant time on the same terms as aesgcm.om: no array is indexed by a secret and every branch |
| 32 | note and loop count depends only on lengths, the key size and the tag-check outcome. Secret |
| 33 | note workspaces (round keys, derived keys, H, state) are zeroed before the entry points return. |
| 34 | note |
| 35 | note Workspace k (ulong AGKN) cells, beyond those aes_setkey and gcm_mul use (0..120, 121..124): |
| 36 | note 125,126 POLYVAL result, then the masked value, then the tag; 127 bytes 4..7 of the counter |
| 37 | note block; 128 bytes 8..15 of the counter block; 129 the 32-bit counter (bytes 0..3, little-endian); |
| 38 | note 131,132 the message-authentication key; 133,134 the nonce (bytes 0..7, bytes 8..11 then zeros). |
| 39 | note 128-bit values are two big-endian halves (hi = bytes 0..7), as in aesgcm.om. |
| 40 | |
| 41 | note all ones except bit 7 of byte 15 of a 128-bit value (the low byte of its low half) |
| 42 | const GSMSK ulong 0xffffffffffffff7f |
| 43 | note 2^36: P_MAX and A_MAX of RFC 8452 section 6 |
| 44 | const GSMAX ulong 0x1000000000 |
| 45 | |
| 46 | note d[o .. o + 8) read big-endian |
| 47 | fn siv_be64 d view uchar * o ulong to ulong |
| 48 | x = ulong 0 |
| 49 | for i ulong 8 unroll |
| 50 | x = or shl x 8 ulong get d add o i |
| 51 | end |
| 52 | ret x |
| 53 | end |
| 54 | |
| 55 | note d[o .. o + 8) read little-endian |
| 56 | fn siv_le64 d view uchar * o ulong to ulong |
| 57 | x = ulong 0 |
| 58 | for i ulong 8 unroll |
| 59 | x = or shl x 8 ulong get d add o sub 7 i |
| 60 | end |
| 61 | ret x |
| 62 | end |
| 63 | |
| 64 | note x written big-endian to d[o .. o + 8) |
| 65 | fn siv_put8 d edit uchar * o ulong x ulong to void |
| 66 | for i ulong 8 unroll |
| 67 | put d add o i uchar and shr x sub 56 shl i 3 0xff |
| 68 | end |
| 69 | end |
| 70 | |
| 71 | note byte order of the low 32 bits of x reversed |
| 72 | fn siv_bsw32 x ulong to ulong inline |
| 73 | ret shr aes_bswap x 32 |
| 74 | end |
| 75 | |
| 76 | note the key-derivation blocks LE32(c + b) || nonce, b = 0..3, into s[150..158); the nonce is |
| 77 | note kn[kl .. kl + 12) |
| 78 | fn siv_dblk s edit ulong AGSN kn view uchar * kl ulong c ulong to void |
| 79 | h = siv_be64 kn kl |
| 80 | l = siv_be64 kn add kl 4 |
| 81 | for b ulong 4 unroll |
| 82 | put s add 150 mul b 2 or shl add c b 56 shr h 32 |
| 83 | put s add 151 mul b 2 l |
| 84 | end |
| 85 | end |
| 86 | |
| 87 | note derive_keys (RFC 8452 section 4) under the key-generating key kn[0 .. kl): the |
| 88 | note message-authentication key into k[131], k[132] and the message-encryption key into ek[0 .. kl) |
| 89 | fn siv_derive k edit ulong AGKN s edit ulong AGSN kn view uchar * kl ulong ek edit uchar 32 to void |
| 90 | aes_setkey k s part kn 0 kl |
| 91 | siv_dblk s kn kl 0 |
| 92 | aes_enc4 k s |
| 93 | put k 131 get s 150 |
| 94 | put k 132 get s 152 |
| 95 | siv_put8 ek 0 get s 154 |
| 96 | siv_put8 ek 8 get s 156 |
| 97 | if eq kl 32 |
| 98 | siv_dblk s kn kl 4 |
| 99 | aes_enc4 k s |
| 100 | siv_put8 ek 16 get s 150 |
| 101 | siv_put8 ek 24 get s 152 |
| 102 | end |
| 103 | end |
| 104 | |
| 105 | note the GHASH key for POLYVAL (RFC 8452 Appendix A): mulX_GHASH(ByteReverse(authentication key)) |
| 106 | note into k[121], k[122]; multiplying by x in GHASH's bit order is a right shift that folds R back |
| 107 | note in when the last bit falls out (the reduction is a mask, as in gcm_mul) |
| 108 | fn siv_hkey k edit ulong AGKN to void |
| 109 | h = aes_bswap get k 132 |
| 110 | l = aes_bswap get k 131 |
| 111 | m = sub 0 and l 1 |
| 112 | l = or shr l 1 shl h 63 |
| 113 | h = xor shr h 1 and m AGR |
| 114 | put k 121 h |
| 115 | put k 122 l |
| 116 | end |
| 117 | |
| 118 | note keys for kn = key(kl) || nonce(12): the derived keys, the message-encryption key schedule in |
| 119 | note k[0..121), the POLYVAL key and the nonce cells |
| 120 | fn siv_init k edit ulong AGKN s edit ulong AGSN kn view uchar * kl ulong to void |
| 121 | ek = new uchar 32 0 |
| 122 | siv_derive k s kn kl ek |
| 123 | aes_setkey k s part ek 0 kl |
| 124 | for i ulong 32 unroll |
| 125 | put ek i 0 |
| 126 | end |
| 127 | siv_hkey k |
| 128 | put k 133 siv_be64 kn kl |
| 129 | put k 134 shl siv_be64 kn add kl 4 32 |
| 130 | end |
| 131 | |
| 132 | note absorb the 16 bytes of d at o as one POLYVAL block: its byte reverse into the GHASH state |
| 133 | fn siv_ablk k edit ulong AGKN d view uchar * o ulong to void |
| 134 | put k 123 xor get k 123 siv_le64 d add o 8 |
| 135 | put k 124 xor get k 124 siv_le64 d o |
| 136 | gcm_mul k |
| 137 | end |
| 138 | |
| 139 | note the whole blocks of d (fewer than 2^20) |
| 140 | fn siv_absrun k edit ulong AGKN d view uchar * to void |
| 141 | for i ulong shr len d 4 |
| 142 | siv_ablk k d shl i 4 |
| 143 | end |
| 144 | end |
| 145 | |
| 146 | note POLYVAL update with d, the last partial block padded with zeros (runs of 8 MiB keep every |
| 147 | note rolled loop under its 1000000 passes) |
| 148 | fn siv_absorb k edit ulong AGKN d view uchar * to void |
| 149 | p = new uchar 16 0 |
| 150 | n = len d |
| 151 | q = ulong 0 |
| 152 | for a ulong shr n 23 |
| 153 | siv_absrun k part d q 0x800000 |
| 154 | q = add q 0x800000 |
| 155 | end |
| 156 | m = sub n and n 15 |
| 157 | siv_absrun k part d q sub m q |
| 158 | r = sub n m |
| 159 | if gt r 0 |
| 160 | for i ulong r |
| 161 | put p i get d add m i |
| 162 | end |
| 163 | siv_ablk k p 0 |
| 164 | for i ulong 16 unroll |
| 165 | put p i 0 |
| 166 | end |
| 167 | end |
| 168 | end |
| 169 | |
| 170 | note the length block LE64(8a) || LE64(8c), byte-reversed: high half 8c, low half 8a |
| 171 | fn siv_lens k edit ulong AGKN a ulong c ulong to void |
| 172 | put k 123 xor get k 123 shl c 3 |
| 173 | put k 124 xor get k 124 shl a 3 |
| 174 | gcm_mul k |
| 175 | end |
| 176 | |
| 177 | note S = POLYVAL(authentication key, pad16(aad) || pad16(d) || length block) into k[125], k[126] |
| 178 | note (the byte reverse of the GHASH state) |
| 179 | fn siv_poly k edit ulong AGKN aad view uchar * d view uchar * to void |
| 180 | put k 123 0 |
| 181 | put k 124 0 |
| 182 | siv_absorb k aad |
| 183 | siv_absorb k d |
| 184 | siv_lens k len aad len d |
| 185 | put k 125 aes_bswap get k 124 |
| 186 | put k 126 aes_bswap get k 123 |
| 187 | put k 123 0 |
| 188 | put k 124 0 |
| 189 | end |
| 190 | |
| 191 | note S ^ nonce, then bit 7 of byte 15 cleared, in k[125], k[126] |
| 192 | fn siv_mask k edit ulong AGKN to void |
| 193 | put k 125 xor get k 125 get k 133 |
| 194 | put k 126 xor get k 126 get k 134 |
| 195 | put k 126 and get k 126 GSMSK |
| 196 | end |
| 197 | |
| 198 | note the tag: k[125], k[126] encrypted with the message-encryption key, in place |
| 199 | fn siv_tag k edit ulong AGKN s edit ulong AGSN to void |
| 200 | put s 150 get k 125 |
| 201 | put s 151 get k 126 |
| 202 | aes_enc4 k s |
| 203 | put k 125 get s 150 |
| 204 | put k 126 get s 151 |
| 205 | end |
| 206 | |
| 207 | note the initial counter block from the tag halves th, tl: bit 7 of byte 15 set, bytes 0..3 as the |
| 208 | note little-endian 32-bit counter |
| 209 | fn siv_ctrset k edit ulong AGKN th ulong tl ulong to void |
| 210 | put k 127 and th 0xffffffff |
| 211 | put k 128 or tl 0x80 |
| 212 | put k 129 siv_bsw32 shr th 32 |
| 213 | end |
| 214 | |
| 215 | note the next four counter blocks into s[150..158); the counter wraps at 2^32 (only its low 32 bits |
| 216 | note reach the block) |
| 217 | fn siv_cblk k edit ulong AGKN s edit ulong AGSN to void |
| 218 | c = get k 129 |
| 219 | h = get k 127 |
| 220 | l = get k 128 |
| 221 | for b ulong 4 unroll |
| 222 | put s add 150 mul b 2 or shl siv_bsw32 add c b 32 h |
| 223 | put s add 151 mul b 2 l |
| 224 | end |
| 225 | put k 129 and add c 4 0xffffffff |
| 226 | end |
| 227 | |
| 228 | note CTR over d from the stored counter (len d below 2^25 + 64) |
| 229 | fn siv_ctrrun k edit ulong AGKN s edit ulong AGSN d edit uchar * to void |
| 230 | n = len d |
| 231 | for j ulong shr n 6 |
| 232 | siv_cblk k s |
| 233 | aes_enc4 k s |
| 234 | gcm_xor64 s d shl j 6 |
| 235 | end |
| 236 | r = and n 63 |
| 237 | if gt r 0 |
| 238 | siv_cblk k s |
| 239 | aes_enc4 k s |
| 240 | gcm_xorn s part d sub n r r |
| 241 | end |
| 242 | end |
| 243 | |
| 244 | fn siv_ctr k edit ulong AGKN s edit ulong AGSN d edit uchar * to void |
| 245 | n = len d |
| 246 | q = ulong 0 |
| 247 | for a ulong shr n 25 |
| 248 | siv_ctrrun k s part d q 0x2000000 |
| 249 | q = add q 0x2000000 |
| 250 | end |
| 251 | siv_ctrrun k s part d q sub n q |
| 252 | end |
| 253 | |
| 254 | fn siv_seal1 k edit ulong AGKN s edit ulong AGSN aad view uchar * buf edit uchar * to void |
| 255 | n = sub len buf 16 |
| 256 | siv_poly k aad view part buf 0 n |
| 257 | siv_mask k |
| 258 | siv_tag k s |
| 259 | siv_ctrset k get k 125 get k 126 |
| 260 | siv_ctr k s part buf 0 n |
| 261 | siv_put8 buf n get k 125 |
| 262 | siv_put8 buf add n 8 get k 126 |
| 263 | end |
| 264 | |
| 265 | fn siv_open1 k edit ulong AGKN s edit ulong AGSN aad view uchar * buf edit uchar * to ulong |
| 266 | n = sub len buf 16 |
| 267 | th = ulong 0 |
| 268 | tl = ulong 0 |
| 269 | for i ulong 8 unroll |
| 270 | th = or shl th 8 ulong get buf add n i |
| 271 | tl = or shl tl 8 ulong get buf add n add i 8 |
| 272 | end |
| 273 | siv_ctrset k th tl |
| 274 | siv_ctr k s part buf 0 n |
| 275 | siv_poly k aad view part buf 0 n |
| 276 | siv_mask k |
| 277 | siv_tag k s |
| 278 | d = or xor get k 125 th xor get k 126 tl |
| 279 | if ne d 0 |
| 280 | gcm_wipe part buf 0 n |
| 281 | ret 1 |
| 282 | end |
| 283 | ret 0 |
| 284 | end |
| 285 | |
| 286 | note 0 when the lengths are acceptable (see the API note), else 2 |
| 287 | fn siv_chk kb ulong ab ulong bl ulong to ulong |
| 288 | ok = ulong 0 |
| 289 | if eq kb 28 |
| 290 | ok = 1 |
| 291 | end |
| 292 | if eq kb 44 |
| 293 | ok = 1 |
| 294 | end |
| 295 | if eq ok 0 |
| 296 | ret 2 |
| 297 | end |
| 298 | if lt bl 16 |
| 299 | ret 2 |
| 300 | end |
| 301 | if gt sub bl 16 GSMAX |
| 302 | ret 2 |
| 303 | end |
| 304 | if gt ab GSMAX |
| 305 | ret 2 |
| 306 | end |
| 307 | ret 0 |
| 308 | end |
| 309 | |
| 310 | fn aes_gcmsiv_seal_core kn view uchar * aad view uchar * buf edit uchar * to ulong |
| 311 | k = new ulong AGKN 0 |
| 312 | s = new ulong AGSN 0 |
| 313 | if ne siv_chk len kn len aad len buf 0 |
| 314 | ret 2 |
| 315 | end |
| 316 | siv_init k s kn sub len kn 12 |
| 317 | siv_seal1 k s aad buf |
| 318 | gcm_clear k s |
| 319 | ret 0 |
| 320 | end |
| 321 | |
| 322 | fn aes_gcmsiv_seal kn view uchar * aad view uchar * buf edit uchar * to ulong |
| 323 | prev = dit 1 |
| 324 | r = aes_gcmsiv_seal_core kn aad buf |
| 325 | z = scrub 4096 |
| 326 | z = dit int prev |
| 327 | ret r |
| 328 | end |
| 329 | |
| 330 | fn aes_gcmsiv_open_core kn view uchar * aad view uchar * buf edit uchar * to ulong |
| 331 | k = new ulong AGKN 0 |
| 332 | s = new ulong AGSN 0 |
| 333 | if ne siv_chk len kn len aad len buf 0 |
| 334 | ret 2 |
| 335 | end |
| 336 | siv_init k s kn sub len kn 12 |
| 337 | r = siv_open1 k s aad buf |
| 338 | gcm_clear k s |
| 339 | ret r |
| 340 | end |
| 341 | |
| 342 | fn aes_gcmsiv_open kn view uchar * aad view uchar * buf edit uchar * to ulong |
| 343 | prev = dit 1 |
| 344 | r = aes_gcmsiv_open_core kn aad buf |
| 345 | z = scrub 4096 |
| 346 | z = dit int prev |
| 347 | ret r |
| 348 | end |