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 / chacha.om

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