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

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