Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/om/tls/aesgcm.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / tls / aesgcm.om` 793 lines ยท 21881 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aesgcm.om: AES (FIPS 197) and AES-GCM (NIST SP 800-38D) for om/tls, module code AG. 3 note Library unit: no om2 line, no fn main, no mutable globals. The build writes `om2`, the om/tls 4 note library files, then a program. 5 note 6 note API (return codes: 0 ok, 1 authentication failure (open only), 2 bad parameters): 7 note aes_gcm_seal ctl kia buf / aes_gcm_open ctl kia buf 8 note kia = key || iv || aad. ctl = klen + 256 * tlen + 65536 * ivlen: 9 note bits 0..7 klen (16, 24 or 32 bytes), bits 8..15 tlen (tag bytes, 1..16), 10 note bits 16..63 ivlen (IV bytes, >= 1). aad is the rest of kia (len kia - klen - ivlen bytes). 11 note seal: buf = plaintext || tlen bytes of room; becomes ciphertext || tag. 12 note open: buf = ciphertext || tag; on success buf[0..n) becomes the plaintext; on a tag 13 note mismatch buf[0..n) is zeroed and 1 is returned. The tag is checked before any plaintext 14 note is produced, with an or-accumulated comparison over all tlen bytes. 15 note 2 (nothing written) when klen is not 16/24/32, tlen is 0 or above 16, ivlen is 0, 16 note klen + ivlen > len kia, len buf < tlen, or the data part exceeds 2^36 - 32 bytes 17 note ((2^32 - 2) counter blocks, SP 800-38D 5.2.1.1). 18 note aead_aes128gcm_seal kn aad buf / aead_aes128gcm_open kn aad buf 19 note TLS 1.3 AEAD_AES_128_GCM (RFC 5116 5.1): kn = key(16) || nonce(12), 16-byte tag, the same 20 note in-place buffer and return codes as aead_chacha_*; 2 when len kn is not 28 or len buf < 16. 21 note 22 note Constant time. AES is bitsliced (BearSSL aes_ct64 layout by T. Pornin, MIT licence: four 23 note blocks in eight 64-bit planes, plane j holding bit j of every state byte). SubBytes is the 24 note Boyar-Peralta depth-16 straight-line program for the AES S-box (J. Boyar, R. Peralta, "A Small 25 note Depth-16 Circuit for the AES S-Box", SEC 2012, IFIP AICT 376, pp. 287-298, doi 26 note 10.1007/978-3-642-30436-1_24; preprint IACR eprint 2011/332). The paper's circuit has 128 gates; 27 note this is the authors' later 125-gate depth-16 version (34 AND, 91 XOR/XNOR), transcribed gate 28 note for gate from https://www.cs.yale.edu/homes/peralta/CircuitStuff/AESDEPTH16SIZE125 (sha256 29 note `b72f635d5a58e06b44d4040b95901da27418ec5a037f9123392155e96966f162`, fetched 2026-10-26). The 30 note key schedule's SubWord uses the same circuit. GHASH is 31 note the bit-serial multiply of SP 800-38D Algorithm 1 with every conditional replaced by an 32 note all-ones/all-zeros mask. No array is indexed by key, data, H or any other secret; every 33 note branch and loop count depends only on lengths, the key size and the tag-check outcome. 34 note Caveat: omc emits plain AArch64; this relies on LDR/STR/EOR/AND/ORR/shift/ROR/SUB being 35 note data-independent in time. No DIT bit is set. Secret workspaces (round keys, H, state) are 36 note zeroed before the entry points return. 37 note 38 note Workspace k (ulong AGKN) cells: 0..119 round keys (round r, plane j at 8r + j); 120 number of 39 note rounds; 121,122 H; 123,124 GHASH accumulator Y; 125,126 E(K, J0); 127,128 J0; 129 next counter 40 note (32 bits); 130 tag length. 128-bit values are two big-endian halves (hi = bytes 0..7). 41 note Workspace s (ulong AGSN) cells: 0..7 bit planes; 8..147 S-box gates (gate Ti in cell 7 + i); 42 note 150..157 the four blocks of aes_enc4 (block b: hi at 150 + 2b, lo at 151 + 2b). 43 44 const AGKN ulong 136 45 const AGSN ulong 160 46 note GHASH reduction R = `11100001` || 0^120 (SP 800-38D 6.3), high half 47 const AGR ulong 0xe100000000000000 48 49 note 8 x 8 bit transpose of s[0..8) inside every byte position (BearSSL br_aes_ct64_ortho); 50 note its own inverse 51 fn aes_ortho s edit ulong AGSN to void 52 a = ulong 0 53 b = ulong 0 54 for i ulong 4 unroll 55 a = get s mul i 2 56 b = get s add mul i 2 1 57 put s mul i 2 or and a 0x5555555555555555 shl and b 0x5555555555555555 1 58 put s add mul i 2 1 or and shr a 1 0x5555555555555555 and b 0xaaaaaaaaaaaaaaaa 59 end 60 for i ulong 4 unroll 61 a = get s add and i 1 mul shr i 1 4 62 b = get s add add and i 1 mul shr i 1 4 2 63 put s add and i 1 mul shr i 1 4 or and a 0x3333333333333333 shl and b 0x3333333333333333 2 64 put s add add and i 1 mul shr i 1 4 2 or and shr a 2 0x3333333333333333 and b 0xcccccccccccccccc 65 end 66 for i ulong 4 unroll 67 a = get s i 68 b = get s add i 4 69 put s i or and a 0x0f0f0f0f0f0f0f0f shl and b 0x0f0f0f0f0f0f0f0f 4 70 put s add i 4 or and shr a 4 0x0f0f0f0f0f0f0f0f and b 0xf0f0f0f0f0f0f0f0 71 end 72 end 73 74 note SubBytes on all 64 bytes of the planes: the Boyar-Peralta depth-16 circuit, one gate a line. 75 note Inputs U0 (most significant bit) .. U7 are planes s[7] .. s[0]; gate Ti is s[7 + i]; outputs 76 note S0 .. S7 go to s[7] .. s[0]. "+" is xor, "x" is and, "#" (xnor) is not xor. Every input is 77 note read before the first output is written. 78 fn aes_sbox s edit ulong AGSN to void 79 put s 8 xor get s 1 get s 3 80 put s 9 xor get s 4 get s 7 81 put s 10 xor get s 6 get s 5 82 put s 11 xor get s 0 get s 10 83 put s 12 xor get s 8 get s 9 84 put s 13 xor get s 6 get s 2 85 put s 14 xor get s 7 get s 1 86 put s 15 xor get s 8 get s 13 87 put s 16 xor get s 1 get s 11 88 put s 17 xor get s 4 get s 11 89 put s 18 xor get s 0 get s 12 90 put s 19 xor get s 12 get s 13 91 put s 20 xor get s 5 get s 2 92 put s 21 xor get s 10 get s 12 93 put s 22 xor get s 2 get s 14 94 put s 23 xor get s 7 get s 2 95 put s 24 xor get s 0 get s 15 96 put s 25 xor get s 1 get s 2 97 put s 26 xor get s 9 get s 25 98 put s 27 xor get s 11 get s 22 99 put s 28 xor get s 8 get s 20 100 put s 29 xor get s 7 get s 11 101 put s 46 xor get s 28 get s 12 102 put s 47 xor get s 28 get s 14 103 put s 48 xor get s 14 get s 26 104 put s 49 xor get s 23 get s 21 105 put s 50 xor get s 29 get s 24 106 put s 51 and get s 26 get s 12 107 put s 52 and get s 27 get s 18 108 put s 53 xor get s 19 get s 51 109 put s 54 and get s 17 get s 0 110 put s 55 xor get s 54 get s 51 111 put s 56 and get s 14 get s 28 112 put s 57 and get s 16 get s 11 113 put s 58 xor get s 47 get s 56 114 put s 59 and get s 29 get s 24 115 put s 60 xor get s 59 get s 56 116 put s 61 and get s 9 get s 15 117 put s 62 and get s 48 get s 46 118 put s 63 xor get s 62 get s 61 119 put s 64 and get s 23 get s 21 120 put s 65 xor get s 64 get s 61 121 put s 66 xor get s 53 get s 52 122 put s 67 xor get s 55 get s 49 123 put s 68 xor get s 58 get s 57 124 put s 69 xor get s 60 get s 65 125 put s 70 xor get s 66 get s 63 126 put s 71 xor get s 67 get s 65 127 put s 72 xor get s 68 get s 63 128 put s 73 xor get s 69 get s 50 129 put s 74 xor get s 72 get s 73 130 put s 75 and get s 72 get s 70 131 put s 76 xor get s 71 get s 75 132 put s 77 xor get s 70 get s 71 133 put s 78 xor get s 73 get s 75 134 put s 79 and get s 78 get s 77 135 put s 80 and get s 76 get s 74 136 put s 81 and get s 70 get s 73 137 put s 82 and get s 77 get s 81 138 put s 83 xor get s 77 get s 75 139 put s 84 and get s 71 get s 72 140 put s 85 and get s 74 get s 84 141 put s 86 xor get s 74 get s 75 142 put s 87 xor get s 71 get s 79 143 put s 88 xor get s 82 get s 83 144 put s 89 xor get s 73 get s 80 145 put s 90 xor get s 85 get s 86 146 put s 91 xor get s 88 get s 90 147 put s 92 xor get s 87 get s 89 148 put s 93 xor get s 87 get s 88 149 put s 94 xor get s 89 get s 90 150 put s 95 xor get s 92 get s 91 151 put s 96 and get s 94 get s 12 152 put s 97 and get s 90 get s 18 153 put s 98 and get s 89 get s 0 154 put s 99 and get s 93 get s 28 155 put s 100 and get s 88 get s 11 156 put s 101 and get s 87 get s 24 157 put s 102 and get s 92 get s 15 158 put s 103 and get s 95 get s 46 159 put s 104 and get s 91 get s 21 160 put s 105 and get s 94 get s 26 161 put s 106 and get s 90 get s 27 162 put s 107 and get s 89 get s 17 163 put s 108 and get s 93 get s 14 164 put s 109 and get s 88 get s 16 165 put s 110 and get s 87 get s 29 166 put s 111 and get s 92 get s 9 167 put s 112 and get s 95 get s 48 168 put s 113 and get s 91 get s 23 169 put s 114 xor get s 111 get s 112 170 put s 115 xor get s 100 get s 106 171 put s 116 xor get s 103 get s 114 172 put s 117 xor get s 105 get s 115 173 put s 118 xor get s 98 get s 108 174 put s 119 xor get s 96 get s 99 175 put s 120 xor get s 114 get s 119 176 put s 121 xor get s 97 get s 117 177 put s 122 xor get s 96 get s 102 178 put s 123 xor get s 101 get s 109 179 put s 124 xor get s 104 get s 110 180 put s 125 xor get s 98 get s 121 181 put s 126 xor get s 118 get s 124 182 put s 127 xor get s 107 get s 115 183 put s 128 xor get s 99 get s 102 184 put s 129 xor get s 117 get s 128 185 put s 130 xor get s 113 get s 126 186 put s 131 xor get s 111 get s 122 187 put s 132 xor get s 118 get s 123 188 put s 7 xor get s 116 get s 129 189 put s 5 not xor get s 130 get s 131 190 put s 135 xor get s 101 get s 114 191 put s 4 xor get s 120 get s 121 192 put s 3 xor get s 125 get s 135 193 put s 138 xor get s 100 get s 108 194 put s 139 xor get s 119 get s 127 195 put s 0 not xor get s 120 get s 132 196 put s 141 xor get s 104 get s 123 197 put s 142 xor get s 138 get s 141 198 put s 143 xor get s 100 get s 122 199 put s 1 not xor get s 116 get s 142 200 put s 145 xor get s 126 get s 139 201 put s 2 xor get s 116 get s 145 202 put s 147 xor get s 121 get s 143 203 put s 6 not xor get s 116 get s 147 204 end 205 206 note ShiftRows: row r of every plane is the 16 bits at 16r (column c at 16r + 4c, block in the low 207 note two bits); row r rotates left by r columns 208 fn aes_shrows s edit ulong AGSN to void 209 x = ulong 0 210 y = ulong 0 211 for j ulong 8 unroll 212 x = get s j 213 y = and x 0xffff 214 y = or y shr and x 0xfff00000 4 215 y = or y shl and x 0xf0000 12 216 y = or y shr and x 0xff0000000000 8 217 y = or y shl and x 0xff00000000 8 218 y = or y shr and x 0xf000000000000000 12 219 y = or y shl and x 0xfff000000000000 4 220 put s j y 221 end 222 end 223 224 note MixColumns: with r = q rotated right 16 (the next row) and u = q xor r, out = xtime(u) xor r 225 note xor rotr32(u); xtime moves plane 7 into planes 0, 1, 3, 4. u goes to s[8..16). 226 fn aes_mixcol s edit ulong AGSN to void 227 x = ulong 0 228 for j ulong 8 unroll 229 x = get s j 230 put s add 8 j xor x rotr x 16 231 end 232 put s 0 xor xor get s 15 rotr get s 0 16 rotr get s 8 32 233 put s 1 xor xor xor get s 8 get s 15 rotr get s 1 16 rotr get s 9 32 234 put s 2 xor xor get s 9 rotr get s 2 16 rotr get s 10 32 235 put s 3 xor xor xor get s 10 get s 15 rotr get s 3 16 rotr get s 11 32 236 put s 4 xor xor xor get s 11 get s 15 rotr get s 4 16 rotr get s 12 32 237 put s 5 xor xor get s 12 rotr get s 5 16 rotr get s 13 32 238 put s 6 xor xor get s 13 rotr get s 6 16 rotr get s 14 32 239 put s 7 xor xor get s 14 rotr get s 7 16 rotr get s 15 32 240 end 241 242 note AddRoundKey with round r 243 fn aes_ark k view ulong AGKN s edit ulong AGSN r ulong to void 244 o = shl r 3 245 for j ulong 8 unroll 246 put s j xor get s j get k add o j 247 end 248 end 249 250 note bytes 0..3 of x to bytes 0, 2, 4, 6 (br_aes_ct64_interleave_in) 251 fn aes_spread x ulong to ulong inline 252 y = and or x shl x 16 0x0000ffff0000ffff 253 ret and or y shl y 8 0x00ff00ff00ff00ff 254 end 255 256 note the inverse of aes_spread on bytes 0, 2, 4, 6 of x 257 fn aes_unspr x ulong to ulong inline 258 y = and x 0x00ff00ff00ff00ff 259 y = and or y shr y 8 0x0000ffff0000ffff 260 ret and or y shr y 16 0xffffffff 261 end 262 263 fn aes_bswap x ulong to ulong 264 y = or and shr x 8 0x00ff00ff00ff00ff shl and x 0x00ff00ff00ff00ff 8 265 y = or and shr y 16 0x0000ffff0000ffff shl and y 0x0000ffff0000ffff 16 266 ret rotr y 32 267 end 268 269 note the four big-endian blocks s[150..158) into bit planes s[0..8): block b's little-endian words 270 note w0..w3 give s[b] = spread(w0) | spread(w2) << 8 and s[b + 4] = spread(w1) | spread(w3) << 8 271 fn aes_inblk s edit ulong AGSN to void 272 x = ulong 0 273 y = ulong 0 274 for b ulong 4 unroll 275 x = aes_bswap get s add 150 mul b 2 276 y = aes_bswap get s add 151 mul b 2 277 put s b or aes_spread and x 0xffffffff shl aes_spread and y 0xffffffff 8 278 put s add b 4 or aes_spread shr x 32 shl aes_spread shr y 32 8 279 end 280 aes_ortho s 281 end 282 283 note bit planes s[0..8) back to the four big-endian blocks s[150..158) 284 fn aes_outblk s edit ulong AGSN to void 285 aes_ortho s 286 x = ulong 0 287 y = ulong 0 288 for b ulong 4 unroll 289 x = get s b 290 y = get s add b 4 291 put s add 150 mul b 2 aes_bswap or aes_unspr x shl aes_unspr y 32 292 put s add 151 mul b 2 aes_bswap or aes_unspr shr x 8 shl aes_unspr shr y 8 32 293 end 294 end 295 296 note encrypt the four blocks s[150..158) in place with the round keys in k 297 fn aes_enc4 k view ulong AGKN s edit ulong AGSN to void 298 aes_inblk s 299 nr = get k 120 300 aes_ark k s 0 301 for r ulong sub nr 1 302 aes_sbox s 303 aes_shrows s 304 aes_mixcol s 305 aes_ark k s add r 1 306 end 307 aes_sbox s 308 aes_shrows s 309 aes_ark k s nr 310 aes_outblk s 311 end 312 313 note SubWord of the 32-bit little-endian word x through the bitsliced S-box 314 fn aes_subw s edit ulong AGSN x ulong to ulong 315 for j ulong 8 unroll 316 put s j 0 317 end 318 put s 0 x 319 aes_ortho s 320 aes_sbox s 321 aes_ortho s 322 ret and get s 0 0xffffffff 323 end 324 325 note FIPS 197 5.2 key expansion of w[0 .. nk) into w[nk .. 4 (nk + 7)); words are little-endian as 326 note loaded from the key bytes, so RotWord is a right rotation by 8 and Rcon is in the low byte. 327 note The branches depend on the word index and the key size only. 328 fn aes_expand w edit ulong 60 s edit ulong AGSN nk ulong to void 329 rc = ulong 1 330 j = ulong 0 331 t = get w sub nk 1 332 for c ulong sub shl add nk 7 2 nk 333 if eq j 0 334 t = xor aes_subw s or shr t 8 and shl t 24 0xff000000 rc 335 rc = shl rc 1 336 if gt rc 255 337 rc = xor rc 0x11b 338 end 339 else 340 if gt nk 6 341 if eq j 4 342 t = aes_subw s t 343 end 344 end 345 end 346 t = xor t get w c 347 put w add c nk t 348 j = add j 1 349 if eq j nk 350 j = 0 351 end 352 end 353 end 354 355 note the key schedule into k: number of rounds, then each round key as bit planes in 356 note k[8r .. 8r + 8). len key is 16, 24 or 32 (checked by the caller). 357 fn aes_setkey k edit ulong AGKN s edit ulong AGSN key view uchar * to void 358 w = new ulong 60 0 359 nk = shr len key 2 360 nr = add nk 6 361 put k 120 nr 362 x = ulong 0 363 q = ulong 0 364 for i ulong nk 365 q = shl i 2 366 x = or ulong get key q shl ulong get key add q 1 8 367 x = or x shl ulong get key add q 2 16 368 put w i or x shl ulong get key add q 3 24 369 end 370 aes_expand w s nk 371 aes_rkeys k s w 372 for i ulong 60 373 put w i 0 374 end 375 end 376 377 note round key r (words w[4r .. 4r + 4)) as bit planes in k[8r .. 8r + 8): the same block in all 378 note four lanes, through aes_inblk 379 fn aes_rkeys k edit ulong AGKN s edit ulong AGSN w view ulong 60 to void 380 q = ulong 0 381 x = ulong 0 382 for r ulong add get k 120 1 383 q = shl r 2 384 x = or get w q shl get w add q 1 32 385 put s 150 aes_bswap x 386 x = or get w add q 2 shl get w add q 3 32 387 put s 151 aes_bswap x 388 for b ulong 3 unroll 389 put s add 152 mul b 2 get s 150 390 put s add 153 mul b 2 get s 151 391 end 392 aes_inblk s 393 for i ulong 8 unroll 394 put k add shl r 3 i get s i 395 end 396 end 397 end 398 399 note Y = Y * H in GF(2^128) (SP 800-38D 6.3 Algorithm 1): Z ^= V where bit i of Y is set, 400 note V = V >> 1, xor R where V's last bit was set; both conditions are masks 401 fn gcm_mul k edit ulong AGKN to void 402 x = get k 123 403 vh = get k 121 404 vl = get k 122 405 zh = ulong 0 406 zl = ulong 0 407 m = ulong 0 408 for i ulong 64 unroll 409 m = sub 0 and shr x sub 63 i 1 410 zh = xor zh and vh m 411 zl = xor zl and vl m 412 m = sub 0 and vl 1 413 vl = or shr vl 1 shl vh 63 414 vh = xor shr vh 1 and m AGR 415 end 416 x = get k 124 417 for i ulong 64 unroll 418 m = sub 0 and shr x sub 63 i 1 419 zh = xor zh and vh m 420 zl = xor zl and vl m 421 m = sub 0 and vl 1 422 vl = or shr vl 1 shl vh 63 423 vh = xor shr vh 1 and m AGR 424 end 425 put k 123 zh 426 put k 124 zl 427 end 428 429 note absorb the 16 bytes of d at o into Y 430 fn gcm_ablk k edit ulong AGKN d view uchar * o ulong to void 431 h = ulong 0 432 l = ulong 0 433 for i ulong 8 unroll 434 h = or shl h 8 ulong get d add o i 435 l = or shl l 8 ulong get d add o add i 8 436 end 437 put k 123 xor get k 123 h 438 put k 124 xor get k 124 l 439 gcm_mul k 440 end 441 442 note the whole blocks of d (fewer than 2^20) 443 fn gcm_absrun k edit ulong AGKN d view uchar * to void 444 for i ulong shr len d 4 445 gcm_ablk k d shl i 4 446 end 447 end 448 449 note GHASH update with d, the last partial block padded with zeros (runs of 8 MiB keep every 450 note rolled loop under its `1000000` passes) 451 fn gcm_absorb k edit ulong AGKN d view uchar * to void 452 p = new uchar 16 0 453 n = len d 454 q = ulong 0 455 for a ulong shr n 23 456 gcm_absrun k part d q 0x800000 457 q = add q 0x800000 458 end 459 m = sub n and n 15 460 gcm_absrun k part d q sub m q 461 r = sub n m 462 if gt r 0 463 for i ulong r 464 put p i get d add m i 465 end 466 gcm_ablk k p 0 467 end 468 end 469 470 note the length block [8a]_64 || [8c]_64 471 fn gcm_lens k edit ulong AGKN a ulong c ulong to void 472 put k 123 xor get k 123 shl a 3 473 put k 124 xor get k 124 shl c 3 474 gcm_mul k 475 end 476 477 note the next four counter blocks J0[0..12) || inc32 into s[150..158) 478 fn gcm_cblk k edit ulong AGKN s edit ulong AGSN to void 479 c = get k 129 480 h = get k 127 481 l = and get k 128 0xffffffff00000000 482 for b ulong 4 unroll 483 put s add 150 mul b 2 h 484 put s add 151 mul b 2 or l and add c b 0xffffffff 485 end 486 put k 129 and add c 4 0xffffffff 487 end 488 489 note d[o .. o + 64) ^= the 64 key stream bytes in s[150..158) 490 fn gcm_xor64 s view ulong AGSN d edit uchar * o ulong to void 491 x = ulong 0 492 q = ulong 0 493 for w ulong 8 unroll 494 x = get s add 150 w 495 for p ulong 8 unroll 496 q = add o add shl w 3 p 497 put d q xor get d q uchar and shr x sub 56 shl p 3 0xff 498 end 499 end 500 end 501 502 note GCTR over d from the stored counter (len d below 2^25 + 64) 503 fn gcm_ctrrun k edit ulong AGKN s edit ulong AGSN d edit uchar * to void 504 n = len d 505 for j ulong shr n 6 506 gcm_cblk k s 507 aes_enc4 k s 508 gcm_xor64 s d shl j 6 509 end 510 r = and n 63 511 if gt r 0 512 gcm_cblk k s 513 aes_enc4 k s 514 gcm_xorn s part d sub n r r 515 end 516 end 517 518 note d[0 .. len d) ^= the first len d (below 64) key stream bytes in s[150..158) 519 fn gcm_xorn s view ulong AGSN d edit uchar * to void 520 x = ulong 0 521 z = ulong 0 522 for i ulong len d 523 x = get s add 150 shr i 3 524 z = sub 56 shl and i 7 3 525 put d i xor get d i uchar and shr x z 0xff 526 end 527 end 528 529 fn gcm_ctr k edit ulong AGKN s edit ulong AGSN d edit uchar * to void 530 n = len d 531 q = ulong 0 532 for a ulong shr n 25 533 gcm_ctrrun k s part d q 0x2000000 534 q = add q 0x2000000 535 end 536 gcm_ctrrun k s part d q sub n q 537 end 538 539 note key schedule, H = E(K, 0^128), J0 (SP 800-38D 7.1 step 2), E(K, J0) and the first counter. 540 note key = kia[0 .. kl), iv = kia[kl .. kl + il). 541 fn gcm_init k edit ulong AGKN s edit ulong AGSN kia view uchar * kl ulong il ulong to void 542 aes_setkey k s part kia 0 kl 543 h = ulong 0 544 l = ulong 1 545 if eq il 12 546 for i ulong 8 unroll 547 h = or shl h 8 ulong get kia add kl i 548 end 549 l = ulong 0 550 for i ulong 4 unroll 551 l = or shl l 8 ulong get kia add add kl 8 i 552 end 553 l = or shl l 32 1 554 end 555 for i ulong 8 unroll 556 put s add 150 i 0 557 end 558 put s 152 h 559 put s 153 l 560 aes_enc4 k s 561 put k 121 get s 150 562 put k 122 get s 151 563 put k 125 get s 152 564 put k 126 get s 153 565 if ne il 12 566 put k 123 0 567 put k 124 0 568 gcm_absorb k part kia kl il 569 gcm_lens k 0 il 570 h = get k 123 571 l = get k 124 572 put s 150 h 573 put s 151 l 574 aes_enc4 k s 575 put k 125 get s 150 576 put k 126 get s 151 577 end 578 put k 123 0 579 put k 124 0 580 put k 127 h 581 put k 128 l 582 put k 129 and add l 1 0xffffffff 583 end 584 585 note the tag Y ^ E(K, J0) into k[123], k[124] after the length block of a aad bytes and c data bytes 586 fn gcm_tag k edit ulong AGKN a ulong c ulong to void 587 gcm_lens k a c 588 put k 123 xor get k 123 get k 125 589 put k 124 xor get k 124 get k 126 590 end 591 592 note byte i (0..15) of the tag in k[123], k[124] 593 fn gcm_tbyte k view ulong AGKN i ulong to uchar 594 x = get k 123 595 if ge i 8 596 x = get k 124 597 end 598 z = sub 56 shl and i 7 3 599 ret uchar and shr x z 0xff 600 end 601 602 fn gcm_seal1 k edit ulong AGKN s edit ulong AGSN aad view uchar * buf edit uchar * to void 603 t = get k 130 604 n = sub len buf t 605 gcm_ctr k s part buf 0 n 606 gcm_absorb k aad 607 gcm_absorb k view part buf 0 n 608 gcm_tag k len aad n 609 for i ulong t 610 put buf add n i gcm_tbyte k i 611 end 612 end 613 614 fn gcm_open1 k edit ulong AGKN s edit ulong AGSN aad view uchar * buf edit uchar * to ulong 615 t = get k 130 616 n = sub len buf t 617 gcm_absorb k aad 618 gcm_absorb k view part buf 0 n 619 gcm_tag k len aad n 620 d = uchar 0 621 for i ulong t 622 d = or d xor get buf add n i gcm_tbyte k i 623 end 624 if ne d 0 625 gcm_wipe part buf 0 n 626 ret 1 627 end 628 gcm_ctr k s part buf 0 n 629 ret 0 630 end 631 632 note zero d, any length (two loop levels keep each under `1000000` passes) 633 fn gcm_wipe d edit uchar * to void 634 n = len d 635 for a ulong shr n 19 636 for b ulong 0x80000 637 put d add shl a 19 b 0 638 end 639 end 640 m = and n 0x7ffff 641 o = sub n m 642 for b ulong m 643 put d add o b 0 644 end 645 end 646 647 fn gcm_clear k edit ulong AGKN s edit ulong AGSN to void 648 for i ulong AGKN 649 put k i 0 650 end 651 for i ulong AGSN 652 put s i 0 653 end 654 end 655 656 note 0 when ctl and the lengths are acceptable (see the API note), else 2 657 fn gcm_chk ctl ulong ka ulong bl ulong to ulong 658 kl = and ctl 0xff 659 tl = and shr ctl 8 0xff 660 il = shr ctl 16 661 ok = ulong 0 662 if eq kl 16 663 ok = 1 664 end 665 if eq kl 24 666 ok = 1 667 end 668 if eq kl 32 669 ok = 1 670 end 671 if eq ok 0 672 ret 2 673 end 674 if eq tl 0 675 ret 2 676 end 677 if gt tl 16 678 ret 2 679 end 680 if eq il 0 681 ret 2 682 end 683 if gt kl ka 684 ret 2 685 end 686 if gt il sub ka kl 687 ret 2 688 end 689 if gt tl bl 690 ret 2 691 end 692 if gt sub bl tl 0xfffffffe0 693 ret 2 694 end 695 ret 0 696 end 697 698 fn aes_gcm_seal_core ctl ulong kia view uchar * buf edit uchar * to ulong 699 k = new ulong AGKN 0 700 s = new ulong AGSN 0 701 if ne gcm_chk ctl len kia len buf 0 702 ret 2 703 end 704 kl = and ctl 0xff 705 il = shr ctl 16 706 put k 130 and shr ctl 8 0xff 707 gcm_init k s kia kl il 708 q = add kl il 709 gcm_seal1 k s part kia q sub len kia q buf 710 gcm_clear k s 711 ret 0 712 end 713 714 fn aes_gcm_seal ctl ulong kia view uchar * buf edit uchar * to ulong 715 prev = dit 1 716 r = aes_gcm_seal_core ctl kia buf 717 z = scrub 4096 718 z = dit int prev 719 ret r 720 end 721 722 fn aes_gcm_open_core ctl ulong kia view uchar * buf edit uchar * to ulong 723 k = new ulong AGKN 0 724 s = new ulong AGSN 0 725 if ne gcm_chk ctl len kia len buf 0 726 ret 2 727 end 728 kl = and ctl 0xff 729 il = shr ctl 16 730 put k 130 and shr ctl 8 0xff 731 gcm_init k s kia kl il 732 q = add kl il 733 r = gcm_open1 k s part kia q sub len kia q buf 734 gcm_clear k s 735 ret r 736 end 737 738 fn aes_gcm_open ctl ulong kia view uchar * buf edit uchar * to ulong 739 prev = dit 1 740 r = aes_gcm_open_core ctl kia buf 741 z = scrub 4096 742 z = dit int prev 743 ret r 744 end 745 746 note TLS 1.3 AEAD_AES_128_GCM: kn = key(16) || nonce(12), 16-byte tag 747 fn aead_aes128gcm_seal_core kn view uchar * aad view uchar * buf edit uchar * to ulong 748 k = new ulong AGKN 0 749 s = new ulong AGSN 0 750 if ne gcm_chk 0xc1010 28 len buf 0 751 ret 2 752 end 753 if ne len kn 28 754 ret 2 755 end 756 put k 130 16 757 gcm_init k s kn 16 12 758 gcm_seal1 k s aad buf 759 gcm_clear k s 760 ret 0 761 end 762 763 fn aead_aes128gcm_seal kn view uchar * aad view uchar * buf edit uchar * to ulong 764 prev = dit 1 765 r = aead_aes128gcm_seal_core kn aad buf 766 z = scrub 4096 767 z = dit int prev 768 ret r 769 end 770 771 fn aead_aes128gcm_open_core kn view uchar * aad view uchar * buf edit uchar * to ulong 772 k = new ulong AGKN 0 773 s = new ulong AGSN 0 774 if ne gcm_chk 0xc1010 28 len buf 0 775 ret 2 776 end 777 if ne len kn 28 778 ret 2 779 end 780 put k 130 16 781 gcm_init k s kn 16 12 782 r = gcm_open1 k s aad buf 783 gcm_clear k s 784 ret r 785 end 786 787 fn aead_aes128gcm_open kn view uchar * aad view uchar * buf edit uchar * to ulong 788 prev = dit 1 789 r = aead_aes128gcm_open_core kn aad buf 790 z = scrub 4096 791 z = dit int prev 792 ret r 793 end