interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / ng / bench / kernels / chacha.om

461 lines · 12619 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note chacha.om: ChaCha20, Poly1305 and AEAD_CHACHA20_POLY1305 exactly as RFC 8439 (module CC).
4note OM-2 library unit (no om2 line, no main): the build writes `om2`, then the library files.
5note
6note API (0 = success; reject codes are documented per function):
7note chacha20_xor kn ctr buf kn = key(32) || nonce(12); buf ^= keystream from block counter ctr.
8note 2: len kn != 44, ctr above 2^32 - 1, or the last block would
9note need a counter above 2^32 - 1 (ctr + blocks > 2^32).
10note poly1305_mac kd m o o[0..16) = Poly1305(kd one-time key, m). 2: len kd != 32 or len o < 16.
11note aead_chacha_seal kn aad buf buf = plaintext || 16 bytes of tag room; encrypts in place and
12note writes the tag at buf[n-16..n). 2: len kn != 44, len buf < 16,
13note plaintext over the counter range (2^32 - 1 blocks).
14note aead_chacha_open kn aad buf buf = ciphertext || tag. 0: tag valid, buf[0..n-16) decrypted in
15note place. 1: tag invalid, buf[0..n-16) zeroed (no unauthenticated
16note plaintext is ever released). 2: bad lengths as for seal; buf untouched.
17note
18note Constant time: no branch, loop count or array index depends on key, nonce, plaintext, keystream,
19note Poly1305 accumulator or tag bytes; they depend only on lengths, the public counter and the
20note public verdict of the tag comparison (accumulated with or over all 16 bytes). Poly1305 uses
21note 26-bit limbs in ulong (products under 2^59) and a branch-free final reduction mod 2^130 - 5
22note (mask select) and addition of s mod 2^128. Compiler-level caveat: omc emits plain AArch64; we
23note rely on MUL/ADD/EOR/AND/ORR/shift being data-independent; the DIT bit is not set. Run-time bounds
24note checks compare public indices only. Keystream, ChaCha state, one-time key and Poly1305 state
25note arrays are zeroed before return.
26
27note little-endian 32-bit load of a[o .. o+4)
28fn chacha20_ld32 a view uchar * o ulong to uint inline
29x = uint get a add o 3
30x = or shl x 8 uint get a add o 2
31x = or shl x 8 uint get a add o 1
32ret or shl x 8 uint get a o
33end
34
35note RFC 8439 2.1 / 2.2: QUARTERROUND(a, b, c, d) on the state x; the four indices are the bytes
36note of q, a in bits 0..7 up to d in bits 24..31 (one parameter keeps the inlined body in 16 registers)
37fn chacha20_qr x edit uint 16 q ulong to void inline
38a = get x and q 15
39b = get x and shr q 8 15
40c = get x and shr q 16 15
41d = get x and shr q 24 15
42a = add a b
43d = rotl xor d a 16
44c = add c d
45b = rotl xor b c 12
46a = add a b
47d = rotl xor d a 8
48c = add c d
49b = rotl xor b c 7
50put x and q 15 a
51put x and shr q 8 15 b
52put x and shr q 16 15 c
53put x and shr q 24 15 d
54end
55
56note one column round and one diagonal round (RFC 8439 2.3); i is a constant in each unrolled copy,
57note so q folds to a number
58fn chacha20_dround x edit uint 16 to void
59for i ulong 4 unroll
60chacha20_qr x or or or i shl add i 4 8 shl add i 8 16 shl add i 12 24
61end
62for i ulong 4 unroll
63chacha20_qr x or or or i shl add and add i 1 3 4 8 shl add and add i 2 3 8 16 shl add and add i 3 3 12 24
64end
65end
66
67note RFC 8439 2.3 state: constants, key words, counter, nonce words (len kn = 44, ctr < 2^32)
68fn chacha20_init s edit uint 16 kn view uchar * ctr ulong to void
69put s 0 0x61707865
70put s 1 0x3320646e
71put s 2 0x79622d32
72put s 3 0x6b206574
73for i ulong 8 unroll
74put s add i 4 chacha20_ld32 kn mul i 4
75end
76put s 12 uint ctr
77for i ulong 3 unroll
78put s add i 13 chacha20_ld32 kn add mul i 4 32
79end
80end
81
82note ks = serialize(20 rounds(s) + s); w is the working state
83fn chacha20_block s view uint 16 w edit uint 16 ks edit uchar 64 to void
84x = uint 0
85for i ulong 16 unroll
86put w i get s i
87end
88for r ulong 10
89chacha20_dround w
90end
91for i ulong 16 unroll
92x = add get w i get s i
93put ks mul i 4 uchar and x 0xff
94put ks add mul i 4 1 uchar and shr x 8 0xff
95put ks add mul i 4 2 uchar and shr x 16 0xff
96put ks add mul i 4 3 uchar shr x 24
97end
98end
99
100note one keystream block of s into buf[o .. min(o + 64, len buf)); s[12] += 1; returns the new o
101fn chacha20_step s edit uint 16 w edit uint 16 ks edit uchar 64 buf edit uchar * o ulong to ulong
102chacha20_block s w ks
103k = sub len buf o
104if gt k 64
105k = 64
106end
107for i ulong k
108q = add o i
109put buf q xor get buf q get ks i
110end
111put s 12 add get s 12 1
112ret add o k
113end
114
115note buf ^= keystream blocks of s, s[12] counting up from its initial value; w, ks scratch
116fn chacha20_run s edit uint 16 w edit uint 16 ks edit uchar 64 buf edit uchar * to void
117n = len buf
118o = ulong 0
119for h ulong add shr n 22 1
120for l ulong 65536
121if ge o n
122break
123end
124o = chacha20_step s w ks buf o
125end
126end
127end
128
129note RFC 8439 2.4: buf ^= ChaCha20 keystream from block counter ctr
130fn chacha20_xor kn view uchar * ctr ulong buf edit uchar * to ulong
131s = new uint 16 0
132w = new uint 16 0
133ks = new uchar 64 0
134if ne len kn 44
135ret 2
136end
137if gt ctr 0xffffffff
138ret 2
139end
140if gt shr add len buf 63 6 sub 0x100000000 ctr
141ret 2
142end
143chacha20_init s kn ctr
144chacha20_run s w ks buf
145for i ulong 16 unroll
146put s i 0
147put w i 0
148end
149for i ulong 64 unroll
150put ks i 0
151end
152ret 0
153end
154
155note Poly1305 state p (ulong 24): 0..4 r limbs (26 bits), 5..8 5*r1..5*r4, 9..13 accumulator h
156note limbs, 14..18 scratch, 19..22 s as four 32-bit words.
157note Load the one-time key kd (32 bytes): r clamped as RFC 8439 2.5 (bytes 3, 7, 11, 15 and 15;
158note bytes 4, 8, 12 and 252), then cut into 26-bit limbs; s little-endian words; h = 0.
159fn poly1305_key p edit ulong 24 kd view uchar * to void
160c = new uchar 16 0
161for i ulong 16 unroll
162put c i get kd i
163end
164put c 3 and get c 3 15
165put c 7 and get c 7 15
166put c 11 and get c 11 15
167put c 15 and get c 15 15
168put c 4 and get c 4 252
169put c 8 and get c 8 252
170put c 12 and get c 12 252
171put p 0 and ulong chacha20_ld32 c 0 0x3ffffff
172put p 1 and shr ulong chacha20_ld32 c 3 2 0x3ffffff
173put p 2 and shr ulong chacha20_ld32 c 6 4 0x3ffffff
174put p 3 and shr ulong chacha20_ld32 c 9 6 0x3ffffff
175put p 4 shr ulong chacha20_ld32 c 12 8
176for i ulong 4 unroll
177put p add i 5 mul get p add i 1 5
178put p add i 19 ulong chacha20_ld32 kd add mul i 4 16
179end
180for i ulong 5 unroll
181put p add i 9 0
182end
183for i ulong 16 unroll
184put c i 0
185end
186end
187
188note h = (h + block b + hib * 2^128) * r mod 2^130 - 5 (partial: limbs below 2^26 + 2^6)
189fn poly1305_block p edit ulong 24 b view uchar 16 hib ulong to void
190h0 = add get p 9 and ulong chacha20_ld32 b 0 0x3ffffff
191h1 = add get p 10 and shr ulong chacha20_ld32 b 3 2 0x3ffffff
192h2 = add get p 11 and shr ulong chacha20_ld32 b 6 4 0x3ffffff
193h3 = add get p 12 and shr ulong chacha20_ld32 b 9 6 0x3ffffff
194h4 = add get p 13 or shr ulong chacha20_ld32 b 12 8 shl hib 24
195d = mul h0 get p 0
196d = add d mul h1 get p 8
197d = add d mul h2 get p 7
198d = add d mul h3 get p 6
199put p 14 add d mul h4 get p 5
200d = mul h0 get p 1
201d = add d mul h1 get p 0
202d = add d mul h2 get p 8
203d = add d mul h3 get p 7
204put p 15 add d mul h4 get p 6
205d = mul h0 get p 2
206d = add d mul h1 get p 1
207d = add d mul h2 get p 0
208d = add d mul h3 get p 8
209put p 16 add d mul h4 get p 7
210d = mul h0 get p 3
211d = add d mul h1 get p 2
212d = add d mul h2 get p 1
213d = add d mul h3 get p 0
214put p 17 add d mul h4 get p 8
215d = mul h0 get p 4
216d = add d mul h1 get p 3
217d = add d mul h2 get p 2
218d = add d mul h3 get p 1
219put p 18 add d mul h4 get p 0
220c = ulong 0
221for i ulong 5 unroll
222d = add get p add i 14 c
223put p add i 9 and d 0x3ffffff
224c = shr d 26
225end
226d = add get p 9 mul c 5
227put p 9 and d 0x3ffffff
228put p 10 add get p 10 shr d 26
229end
230
231note absorb m: whole 16-byte blocks with the 2^128 bit; a final partial block either RFC 8439 2.5
232note style (pad 1: append 0x01, zero fill, no 2^128 bit) or zero padded to a whole block as the
233note AEAD construction of 2.8 requires (pad 0). b is the caller's 16-byte block scratch.
234fn poly1305_feed p edit ulong 24 b edit uchar 16 m view uchar * pad ulong to void
235n = len m
236o = ulong 0
237for h ulong add shr n 20 1
238for l ulong 65536
239if gt add o 16 n
240break
241end
242for i ulong 16 unroll
243put b i get m add o i
244end
245poly1305_block p b 1
246o = add o 16
247end
248end
249r = sub n o
250if eq r 0
251ret
252end
253for i ulong 16 unroll
254put b i 0
255end
256for i ulong r
257put b i get m add o i
258end
259if eq pad 1
260put b r 1
261poly1305_block p b 0
262else
263poly1305_block p b 1
264end
265end
266
267note full carry, then h = h - (2^130 - 5) when that is not negative, by mask select (RFC 8439 2.5)
268fn poly1305_reduce p edit ulong 24 to void
269c = ulong 0
270d = ulong 0
271for i ulong 4 unroll
272d = add get p add i 10 c
273put p add i 10 and d 0x3ffffff
274c = shr d 26
275end
276d = add get p 9 mul c 5
277put p 9 and d 0x3ffffff
278put p 10 add get p 10 shr d 26
279c = 5
280for i ulong 4 unroll
281d = add get p add i 9 c
282put p add i 14 and d 0x3ffffff
283c = shr d 26
284end
285d = sub add get p 13 c 0x4000000
286put p 18 d
287m = sub shr d 63 1
288for i ulong 5 unroll
289put p add i 9 or and get p add i 9 not m and get p add i 14 m
290end
291end
292
293note o[0..16) = (h + s) mod 2^128, little-endian (h fully reduced, limbs below 2^26)
294fn poly1305_finish p edit ulong 24 o edit uchar * to void
295poly1305_reduce p
296a = add get p 9 shl get p 10 26
297f = add and a 0xffffffff get p 19
298put p 14 and f 0xffffffff
299a = add shr a 32 shl get p 11 20
300f = add add and a 0xffffffff get p 20 shr f 32
301put p 15 and f 0xffffffff
302a = add shr a 32 shl get p 12 14
303f = add add and a 0xffffffff get p 21 shr f 32
304put p 16 and f 0xffffffff
305a = add shr a 32 shl get p 13 8
306f = add add and a 0xffffffff get p 22 shr f 32
307put p 17 and f 0xffffffff
308x = ulong 0
309for i ulong 4 unroll
310x = get p add i 14
311put o mul i 4 uchar and x 0xff
312put o add mul i 4 1 uchar and shr x 8 0xff
313put o add mul i 4 2 uchar and shr x 16 0xff
314put o add mul i 4 3 uchar and shr x 24 0xff
315end
316end
317
318note RFC 8439 2.5: o[0..16) = Poly1305(kd, m)
319fn poly1305_mac kd view uchar * m view uchar * o edit uchar * to ulong
320p = new ulong 24 0
321b = new uchar 16 0
322if ne len kd 32
323ret 2
324end
325if lt len o 16
326ret 2
327end
328poly1305_key p kd
329poly1305_feed p b m 1
330poly1305_finish p o
331for i ulong 24 unroll
332put p i 0
333end
334for i ulong 16 unroll
335put b i 0
336end
337ret 0
338end
339
340note RFC 8439 2.8 tag over aad, pad16, buf[0..n-16), pad16, le64 len aad, le64 (n-16), keyed by the
341note one-time key pk; the tag replaces pk[0..16) and pk[16..32) is zeroed.
342fn aead_chacha_mac pk edit uchar 32 aad view uchar * buf view uchar * to void
343p = new ulong 24 0
344b = new uchar 16 0
345poly1305_key p pk
346poly1305_feed p b aad 0
347n = sub len buf 16
348poly1305_feed p b part buf 0 n 0
349a = len aad
350for i ulong 8 unroll
351put b i uchar and shr a mul i 8 0xff
352put b add i 8 uchar and shr n mul i 8 0xff
353end
354poly1305_block p b 1
355poly1305_finish p pk
356for i ulong 16 unroll
357put pk add i 16 0
358end
359for i ulong 24 unroll
360put p i 0
361end
362for i ulong 16 unroll
363put b i 0
364end
365end
366
367note 0 when kn and buf have AEAD lengths (len kn 44, len buf >= 16, ciphertext within the counter
368note range from block 1), else 2
369fn aead_chacha_lens kn view uchar * buf view uchar * to ulong
370if ne len kn 44
371ret 2
372end
373if lt len buf 16
374ret 2
375end
376if gt shr add sub len buf 16 63 6 0xffffffff
377ret 2
378end
379ret 0
380end
381
382note zero a, any length (two loop levels: one rolled loop traps past 1000000 passes)
383fn aead_chacha_zero a edit uchar * to void
384n = len a
385o = ulong 0
386for h ulong add shr n 20 1
387for l ulong 65536
388if ge o n
389break
390end
391k = sub n o
392if gt k 16
393k = 16
394end
395for i ulong k
396put a add o i 0
397end
398o = add o k
399end
400end
401end
402
403note RFC 8439 2.8 AEAD_CHACHA20_POLY1305 encryption, in place
404fn aead_chacha_seal kn view uchar * aad view uchar * buf edit uchar * to ulong
405pk = new uchar 32 0
406if ne aead_chacha_lens kn buf 0
407ret 2
408end
409m = sub len buf 16
410r = chacha20_xor kn 0 pk
411r = or r chacha20_xor kn 1 part buf 0 m
412aead_chacha_mac pk aad buf
413for i ulong 16 unroll
414put buf add m i get pk i
415end
416for i ulong 32 unroll
417put pk i 0
418end
419ret r
420end
421
422note RFC 8439 2.8 AEAD_CHACHA20_POLY1305 decryption, in place; the tag is checked before any
423note plaintext is produced, with a constant-time or-accumulated comparison
424fn aead_chacha_open kn view uchar * aad view uchar * buf edit uchar * to ulong
425pk = new uchar 32 0
426if ne aead_chacha_lens kn buf 0
427ret 2
428end
429m = sub len buf 16
430r = chacha20_xor kn 0 pk
431aead_chacha_mac pk aad buf
432d = uchar 0
433for i ulong 16 unroll
434d = or d xor get pk i get buf add m i
435end
436for i ulong 32 unroll
437put pk i 0
438end
439if ne d 0
440aead_chacha_zero part buf 0 m
441ret 1
442end
443ret or r chacha20_xor kn 1 part buf 0 m
444end
445note bench: ChaCha20 (om/tls/chacha.om chacha20_xor) over a 1 MiB buffer, 64 times in place
446fn main to int
447b = new uchar 1048576 0
448for i ulong 1048576
449put b i uchar and add mul i 31 7 255
450end
451kn = new uchar 44 0
452for i ulong 44
453put kn i uchar i
454end
455bad = ulong 0
456for r ulong 64
457bad = or bad chacha20_xor kn 1 b
458end
459w = write 1 b 0 1048576
460ret int bad
461end