interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / tls / aesgcm.om

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