interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / src / ct / aes.om

384 lines · 10755 bytes Raw Blame History
1note omtls AES-128/256 encryption constant-time core (FIPS 197), bitsliced S-box, and the GHASH multiply
2note (NIST SP 800-38D 6.3). Every loop unrolled, every index a constant, no branch, no table lookup.
3note State s (ulong 64): four 16-byte blocks, block q byte r + 4c at 16q + r + 4c, one byte value per word.
4note S-box: the 64 bytes become 8 bit-planes (plane j bit i = bit j of byte i) in slot 0 of the plane scratch w
5note (ulong 64: slot q = words 8q .. 8q+7; slot 7 is the multiplier's scratch). The GF(2^8) inverse is x^254
6note (0 -> 0): x2 = x x, x3 = x2 x, x6 = x3 x3, x12 = x6 x6, x15 = x12 x3, x30, x60, x120, x240 by squaring,
7note x252 = x240 x12, x254 = x252 x2; then the FIPS 197 5.1.1 affine map and back to bytes.
8note A product is shift-and-add over the planes: slot 7 holds A x^i (xtime on planes: x^8 = x^4 + x^3 + x + 1),
9note added into D under the plane mask b_i. Each fixed (A, B, D) is its own function so indices stay constants
10note (a slot passed as an argument would be a run-time index).
11note wolfram: 1/(x^6 + x^4 + x + 1) mod (x^8 + x^4 + x^3 + x + 1) over GF(2) = 0xca (FIPS 197 4.4 example).
12fn ot_aes_pk s view ulong 64 w edit ulong 64 to void
13for j ulong 8 unroll
14a = ulong 0
15for i ulong 64 unroll
16a = or a shl and shr get s i j 1 i
17end
18put w j a
19end
20end
21note Slot 7 times x on planes: (a0..a7) -> (a7, a0^a7, a1, a2^a7, a3^a7, a4, a5, a6).
22fn ot_aes_x7 w edit ulong 64 to void
23a3 = get w 59
24a7 = get w 63
25put w 63 get w 62
26put w 62 get w 61
27put w 61 get w 60
28put w 60 xor a3 a7
29put w 59 xor get w 58 a7
30put w 58 get w 57
31put w 57 xor get w 56 a7
32put w 56 a7
33end
34fn ot_aes_m001 w edit ulong 64 to void
35for k ulong 8 unroll
36put w add 56 k get w add 0 k
37put w add 8 k 0
38end
39for i ulong 8 unroll
40c = get w add 0 i
41for k ulong 8 unroll
42put w add 8 k xor get w add 8 k and get w add 56 k c
43end
44ot_aes_x7 w
45end
46end
47fn ot_aes_m102 w edit ulong 64 to void
48for k ulong 8 unroll
49put w add 56 k get w add 8 k
50put w add 16 k 0
51end
52for i ulong 8 unroll
53c = get w add 0 i
54for k ulong 8 unroll
55put w add 16 k xor get w add 16 k and get w add 56 k c
56end
57ot_aes_x7 w
58end
59end
60fn ot_aes_m223 w edit ulong 64 to void
61for k ulong 8 unroll
62put w add 56 k get w add 16 k
63put w add 24 k 0
64end
65for i ulong 8 unroll
66c = get w add 16 i
67for k ulong 8 unroll
68put w add 24 k xor get w add 24 k and get w add 56 k c
69end
70ot_aes_x7 w
71end
72end
73fn ot_aes_m334 w edit ulong 64 to void
74for k ulong 8 unroll
75put w add 56 k get w add 24 k
76put w add 32 k 0
77end
78for i ulong 8 unroll
79c = get w add 24 i
80for k ulong 8 unroll
81put w add 32 k xor get w add 32 k and get w add 56 k c
82end
83ot_aes_x7 w
84end
85end
86fn ot_aes_m425 w edit ulong 64 to void
87for k ulong 8 unroll
88put w add 56 k get w add 32 k
89put w add 40 k 0
90end
91for i ulong 8 unroll
92c = get w add 16 i
93for k ulong 8 unroll
94put w add 40 k xor get w add 40 k and get w add 56 k c
95end
96ot_aes_x7 w
97end
98end
99fn ot_aes_m556 w edit ulong 64 to void
100for k ulong 8 unroll
101put w add 56 k get w add 40 k
102put w add 48 k 0
103end
104for i ulong 8 unroll
105c = get w add 40 i
106for k ulong 8 unroll
107put w add 48 k xor get w add 48 k and get w add 56 k c
108end
109ot_aes_x7 w
110end
111end
112fn ot_aes_m663 w edit ulong 64 to void
113for k ulong 8 unroll
114put w add 56 k get w add 48 k
115put w add 24 k 0
116end
117for i ulong 8 unroll
118c = get w add 48 i
119for k ulong 8 unroll
120put w add 24 k xor get w add 24 k and get w add 56 k c
121end
122ot_aes_x7 w
123end
124end
125fn ot_aes_m336 w edit ulong 64 to void
126for k ulong 8 unroll
127put w add 56 k get w add 24 k
128put w add 48 k 0
129end
130for i ulong 8 unroll
131c = get w add 24 i
132for k ulong 8 unroll
133put w add 48 k xor get w add 48 k and get w add 56 k c
134end
135ot_aes_x7 w
136end
137end
138fn ot_aes_m346 w edit ulong 64 to void
139for k ulong 8 unroll
140put w add 56 k get w add 24 k
141put w add 48 k 0
142end
143for i ulong 8 unroll
144c = get w add 32 i
145for k ulong 8 unroll
146put w add 48 k xor get w add 48 k and get w add 56 k c
147end
148ot_aes_x7 w
149end
150end
151fn ot_aes_m610 w edit ulong 64 to void
152for k ulong 8 unroll
153put w add 56 k get w add 48 k
154put w add 0 k 0
155end
156for i ulong 8 unroll
157c = get w add 8 i
158for k ulong 8 unroll
159put w add 0 k xor get w add 0 k and get w add 56 k c
160end
161ot_aes_x7 w
162end
163end
164note FIPS 197 5.1.1 affine map of slot 0 into slot 1: b_k ^ b_(k+4) ^ b_(k+5) ^ b_(k+6) ^ b_(k+7) ^ (bit k of 0x63,
165note as an all-ones plane), then the planes of slot 1 back to the 64 bytes of s.
166fn ot_aes_af s edit ulong 64 w edit ulong 64 to void
167for k ulong 8 unroll
168v = xor xor get w k get w and add k 4 7 get w and add k 5 7
169v = xor xor v get w and add k 6 7 get w and add k 7 7
170put w add 8 k xor v neg and shr 99 k 1
171end
172for i ulong 64 unroll
173a = ulong 0
174for j ulong 8 unroll
175a = or a shl and shr get w add 8 j i 1 j
176end
177put s i a
178end
179end
180note SubBytes on all 64 bytes of s.
181fn ot_aes_sb s edit ulong 64 w edit ulong 64 to void
182ot_aes_pk s w
183ot_aes_m001 w
184ot_aes_m102 w
185ot_aes_m223 w
186ot_aes_m334 w
187ot_aes_m425 w
188ot_aes_m556 w
189ot_aes_m663 w
190ot_aes_m336 w
191ot_aes_m663 w
192ot_aes_m346 w
193ot_aes_m610 w
194ot_aes_af s w
195end
196note ShiftRows (FIPS 197 5.1.2): row r of each block rotated left by r columns.
197fn ot_aes_sr s edit ulong 64 to void
198for q ulong 4 unroll
199t = get s add mul q 16 1
200put s add mul q 16 1 get s add mul q 16 5
201put s add mul q 16 5 get s add mul q 16 9
202put s add mul q 16 9 get s add mul q 16 13
203put s add mul q 16 13 t
204t = get s add mul q 16 2
205put s add mul q 16 2 get s add mul q 16 10
206put s add mul q 16 10 t
207t = get s add mul q 16 6
208put s add mul q 16 6 get s add mul q 16 14
209put s add mul q 16 14 t
210t = get s add mul q 16 15
211put s add mul q 16 15 get s add mul q 16 11
212put s add mul q 16 11 get s add mul q 16 7
213put s add mul q 16 7 get s add mul q 16 3
214put s add mul q 16 3 t
215end
216end
217note xtime (FIPS 197 4.2.1): a * x mod p for a byte a in a ulong, the reduction selected by a mask.
218fn ot_aes_xt a ulong to ulong inline
219ret xor and shl a 1 255 and 27 neg shr a 7
220end
221note MixColumns (FIPS 197 5.1.3): b_r = a_r ^ t ^ xtime(a_r ^ a_(r+1)), t = a0 ^ a1 ^ a2 ^ a3.
222fn ot_aes_mc s edit ulong 64 to void
223for q ulong 4 unroll
224for c ulong 4 unroll
225a0 = get s add mul q 16 mul c 4
226a1 = get s add add mul q 16 mul c 4 1
227a2 = get s add add mul q 16 mul c 4 2
228a3 = get s add add mul q 16 mul c 4 3
229t = xor xor xor a0 a1 a2 a3
230put s add mul q 16 mul c 4 xor xor a0 t ot_aes_xt xor a0 a1
231put s add add mul q 16 mul c 4 1 xor xor a1 t ot_aes_xt xor a1 a2
232put s add add mul q 16 mul c 4 2 xor xor a2 t ot_aes_xt xor a2 a3
233put s add add mul q 16 mul c 4 3 xor xor a3 t ot_aes_xt xor a3 a0
234end
235end
236end
237note AES encryption of the 4 blocks of s with the round keys rk (16 bytes per round, 10 rounds).
238fn ot_aes_e128 rk view ulong 240 s edit ulong 64 w edit ulong 64 to void
239for q ulong 4 unroll
240for i ulong 16 unroll
241put s add mul q 16 i xor get s add mul q 16 i get rk i
242end
243end
244for r ulong 9 unroll
245ot_aes_sb s w
246ot_aes_sr s
247ot_aes_mc s
248for q ulong 4 unroll
249for i ulong 16 unroll
250put s add mul q 16 i xor get s add mul q 16 i get rk add mul add r 1 16 i
251end
252end
253end
254ot_aes_sb s w
255ot_aes_sr s
256for q ulong 4 unroll
257for i ulong 16 unroll
258put s add mul q 16 i xor get s add mul q 16 i get rk add 160 i
259end
260end
261end
262note AES encryption of the 4 blocks of s with the round keys rk (16 bytes per round, 14 rounds).
263fn ot_aes_e256 rk view ulong 240 s edit ulong 64 w edit ulong 64 to void
264for q ulong 4 unroll
265for i ulong 16 unroll
266put s add mul q 16 i xor get s add mul q 16 i get rk i
267end
268end
269for r ulong 13 unroll
270ot_aes_sb s w
271ot_aes_sr s
272ot_aes_mc s
273for q ulong 4 unroll
274for i ulong 16 unroll
275put s add mul q 16 i xor get s add mul q 16 i get rk add mul add r 1 16 i
276end
277end
278end
279ot_aes_sb s w
280ot_aes_sr s
281for q ulong 4 unroll
282for i ulong 16 unroll
283put s add mul q 16 i xor get s add mul q 16 i get rk add 224 i
284end
285end
286end
287note Rcon (FIPS 197 5.2) for round r = 0..9: x^r = 01 02 04 08 10 20 40 80 1b 36, as xor xor and shl 1 r 255 mul 27 and shr r 3 xor and r 1 1 mul 54 and shr r 3 and r 1.
288note Key expansion (FIPS 197 5.2): rk bytes 4i .. 4i+3 hold word w_i; SubWord through the bitsliced S-box on lanes
289note 0..3 of s. AES-128: Nk = 4, 44 words.
290fn ot_aes_k128 k view uchar 32 rk edit ulong 240 s edit ulong 64 w edit ulong 64 to void
291for i ulong 16 unroll
292put rk i ulong get k i
293end
294for r ulong 10 unroll
295for b ulong 4 unroll
296put s b get rk add add mul r 16 12 and add b 1 3
297end
298ot_aes_sb s w
299put s 0 xor get s 0 xor xor and shl 1 r 255 mul 27 and shr r 3 xor and r 1 1 mul 54 and shr r 3 and r 1
300for b ulong 4 unroll
301put rk add add mul r 16 16 b xor get rk add mul r 16 b get s b
302end
303for j ulong 3 unroll
304for b ulong 4 unroll
305put rk add add mul r 16 add 20 mul j 4 b xor get rk add add mul r 16 add 4 mul j 4 b get rk add add mul r 16 add 16 mul j 4 b
306end
307end
308end
309end
310note AES-256: Nk = 8, 60 words. Rounds r = 0..5 make words 8r+8 .. 8r+15 (word 8r+12 is SubWord only); the
311note last 4 words 56..59 follow with Rcon 0x40.
312fn ot_aes_k256 k view uchar 32 rk edit ulong 240 s edit ulong 64 w edit ulong 64 to void
313for i ulong 32 unroll
314put rk i ulong get k i
315end
316for r ulong 6 unroll
317for b ulong 4 unroll
318put s b get rk add add mul r 32 28 and add b 1 3
319end
320ot_aes_sb s w
321put s 0 xor get s 0 xor xor and shl 1 r 255 mul 27 and shr r 3 xor and r 1 1 mul 54 and shr r 3 and r 1
322for b ulong 4 unroll
323put rk add add mul r 32 32 b xor get rk add mul r 32 b get s b
324end
325for j ulong 3 unroll
326for b ulong 4 unroll
327put rk add add mul r 32 add 36 mul j 4 b xor get rk add add mul r 32 add 4 mul j 4 b get rk add add mul r 32 add 32 mul j 4 b
328end
329end
330for b ulong 4 unroll
331put s b get rk add add mul r 32 44 b
332end
333ot_aes_sb s w
334for b ulong 4 unroll
335put rk add add mul r 32 48 b xor get rk add add mul r 32 16 b get s b
336end
337for j ulong 3 unroll
338for b ulong 4 unroll
339put rk add add mul r 32 add 52 mul j 4 b xor get rk add add mul r 32 add 20 mul j 4 b get rk add add mul r 32 add 48 mul j 4 b
340end
341end
342end
343for b ulong 4 unroll
344put s b get rk add 220 and add b 1 3
345end
346ot_aes_sb s w
347put s 0 xor get s 0 64
348for b ulong 4 unroll
349put rk add 224 b xor get rk add 192 b get s b
350end
351for j ulong 3 unroll
352for b ulong 4 unroll
353put rk add add 228 mul j 4 b xor get rk add add 196 mul j 4 b get rk add add 224 mul j 4 b
354end
355end
356end
357note GHASH multiply (SP 800-38D 6.3, Algorithm 1): x = x * h in GF(2^128), x and h as two big-endian halves
358note (word 0 holds bits 0..63, bit 0 the most significant). Each of the 128 steps selects by mask, no branch.
359fn ot_gcm_mul x edit ulong 2 h view ulong 2 to void
360v0 = get h 0
361v1 = get h 1
362z0 = ulong 0
363z1 = ulong 0
364a = get x 0
365for i ulong 64 unroll
366m = neg and shr a sub 63 i 1
367z0 = xor z0 and v0 m
368z1 = xor z1 and v1 m
369r = neg and v1 1
370v1 = or shr v1 1 shl v0 63
371v0 = xor shr v0 1 and r 0xe100000000000000
372end
373a = get x 1
374for i ulong 64 unroll
375m = neg and shr a sub 63 i 1
376z0 = xor z0 and v0 m
377z1 = xor z1 and v1 m
378r = neg and v1 1
379v1 = or shr v1 1 shl v0 63
380v0 = xor shr v0 1 and r 0xe100000000000000
381end
382put x 0 z0
383put x 1 z1
384end