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 / aienux / allen / tls-crypto.om

628 lines · 11254 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-or-later
2note Allen bounded TLS cryptography. Concatenate vendor/sha256-core.om before this unit.
3note SHA256 reuses the existing first-party core and its provenance/license unchanged.
4note X25519 follows RFC7748 sections 5/6 and native/net/x25519.c's Montgomery ladder.
5note Unsigned radix-65536 field representation follows the first-party omnet field bounds;
6note no verification-only branches, allocations, or point routines are reused.
7note ChaCha20/Poly1305 are implemented from RFC8439 sections 2.1/2.3/2.5/2.8 equations.
8note uint arithmetic wraps modulo 2^32 per OMEGESE 9.3; narrowing conversions are masked.
9note No secret-indexed accesses or secret-dependent loop counts. Compiler/machine timing is unaudited.
10note Single caller, nonreentrant; all input/output arrays are disjoint fixed globals, no caller aliases.
11note Caller owns key/nonce uniqueness and entropy. Public calls preserve key/private input until tcwipe.
12note tcsha/tchmac preserve Tcbuf; tcopen verifies the full tag BEFORE touching ciphertext.
13note All public bool functions reject invalid lengths. HMAC accepts keys of 0..32 bytes only.
14note HKDF-Extract/Expand RFC5869 is composed by tls.om using tchmac.
15note Scratch globals are wiped after calls. tcwipe also clears caller-visible secrets and plaintext.
16
17global Tcbuf uchar 65536
18global Tckey uchar 32
19global Tciv uchar 12
20global Tcaad uchar 32
21global Tctag uchar 16
22global Tcdigest uchar 32
23global Tcxpriv uchar 32
24global Tcxpub uchar 32
25global Tcxpeer uchar 32
26global Tcxshare uchar 32
27
28global Tchi uchar 32
29global Tcxf ulong 176
30global Tcxt ulong 32
31global Tcxsc uchar 32
32global Tcxres uchar 32
33global Tccs uint 16
34global Tccw uint 16
35global Tccblk uchar 64
36global Tcmkey uchar 32
37global Tcmblk uchar 16
38global Tcmtag uchar 16
39global Tcmr ulong 10
40global Tcmh ulong 10
41global Tcmt ulong 10
42global Tcmg ulong 10
43
44fn tcshwipe to void
45for i ulong 8
46put H i 0
47put Vv i 0
48end
49for i ulong 64
50put W i 0
51put Hb i 0
52end
53put Hs 0 0
54put Hs 1 0
55for i ulong 32
56put Hash i 0
57put Tchi i 0
58end
59end
60
61fn tcsha n ulong to bool
62for i ulong 32
63put Tcdigest i 0
64end
65tcshwipe
66if gt n 65536
67ret false
68end
69constants
70hinit
71for i ulong n
72hbyte get Tcbuf i
73end
74finish
75for i ulong 32
76put Tcdigest i get Hash i
77end
78tcshwipe
79ret true
80end
81
82fn tchmac keylen ulong n ulong to bool
83for i ulong 32
84put Tcdigest i 0
85end
86tcshwipe
87if or gt keylen 32 gt n 65536
88ret false
89end
90constants
91hinit
92for i ulong 64
93v = uchar 54
94if lt i keylen
95v = xor v get Tckey i
96end
97hbyte v
98end
99for i ulong n
100hbyte get Tcbuf i
101end
102finish
103for i ulong 32
104put Tchi i get Hash i
105end
106hinit
107for i ulong 64
108v = uchar 92
109if lt i keylen
110v = xor v get Tckey i
111end
112hbyte v
113end
114for i ulong 32
115hbyte get Tchi i
116end
117finish
118for i ulong 32
119put Tcdigest i get Hash i
120end
121tcshwipe
122ret true
123end
124
125note Field slots: x0,a16,b32,c48,d64,e80,f96,k112,inverse128,canonical144,subtract160.
126note A mul input limb is below 2^21; folded convolution below 2^52, so ulong never overflows.
127note Mul gives limbs <=65535 except limb0<=65573. Sub adds 8p, keeping intermediates positive.
128fn tcxwipe to void
129for i ulong 176
130put Tcxf i 0
131end
132for i ulong 32
133put Tcxt i 0
134put Tcxsc i 0
135put Tcxres i 0
136end
137end
138
139fn tcfcopy o ulong a ulong to void
140for i ulong 16
141put Tcxf add o i get Tcxf add a i
142end
143end
144
145fn tcfadd o ulong a ulong b ulong to void
146for i ulong 16
147v = add get Tcxf add a i get Tcxf add b i
148put Tcxf add o i v
149end
150end
151
152fn tcfprime i ulong to ulong
153if eq i 0
154ret 65517
155end
156if eq i 15
157ret 32767
158end
159ret 65535
160end
161
162fn tcfsub o ulong a ulong b ulong to void
163for i ulong 16
164v = add get Tcxf add a i mul tcfprime i 8
165put Tcxf add o i sub v get Tcxf add b i
166end
167end
168
169fn tcfcarry o ulong to void
170for i ulong 15
171p = add o i
172v = get Tcxf p
173put Tcxf add p 1 add get Tcxf add p 1 shr v 16
174put Tcxf p and v 65535
175end
176v = get Tcxf add o 15
177put Tcxf add o 15 and v 65535
178put Tcxf o add get Tcxf o mul shr v 16 38
179end
180
181fn tcfmul o ulong a ulong b ulong to void
182for i ulong 32
183put Tcxt i 0
184end
185for i ulong 16
186x = get Tcxf add a i
187for j ulong 16
188p = add i j
189v = mul x get Tcxf add b j
190put Tcxt p add get Tcxt p v
191end
192end
193for i ulong 15
194v = add get Tcxt i mul 38 get Tcxt add i 16
195put Tcxf add o i v
196end
197put Tcxf add o 15 get Tcxt 15
198tcfcarry o
199tcfcarry o
200end
201
202fn tcfswap a ulong b ulong bit ulong to void
203mask = sub ulong 0 bit
204for i ulong 16
205x = get Tcxf add a i
206y = get Tcxf add b i
207v = and mask xor x y
208put Tcxf add a i xor x v
209put Tcxf add b i xor y v
210end
211end
212
213fn tcflad bit ulong to void
214tcfswap 16 32 bit
215tcfswap 48 64 bit
216tcfadd 80 16 48
217tcfsub 16 16 48
218tcfadd 48 32 64
219tcfsub 32 32 64
220tcfmul 64 80 80
221tcfmul 96 16 16
222tcfmul 16 48 16
223tcfmul 48 32 80
224tcfadd 80 16 48
225tcfsub 16 16 48
226tcfmul 32 16 16
227tcfsub 48 64 96
228tcfmul 16 48 112
229tcfadd 16 16 64
230tcfmul 48 48 16
231tcfmul 16 64 96
232tcfmul 64 32 0
233tcfmul 32 80 80
234tcfswap 16 32 bit
235tcfswap 48 64 bit
236end
237
238fn tcfinv to void
239tcfcopy 128 48
240for i ulong 254
241j = sub 253 i
242tcfmul 128 128 128
243note Branch uses fixed public exponent p-2, not any secret bit.
244if and ne j 2 ne j 4
245tcfmul 128 128 48
246end
247end
248tcfmul 16 16 128
249end
250
251fn tcfpack to void
252tcfcopy 144 16
253tcfcarry 144
254tcfcarry 144
255tcfcarry 144
256for j ulong 2
257borrow = ulong 0
258for i ulong 16
259v = add get Tcxf add 144 i 65536
260v = sub sub v tcfprime i borrow
261put Tcxf add 160 i and v 65535
262borrow = sub 1 shr v 16
263end
264tcfswap 144 160 sub 1 borrow
265end
266for i ulong 16
267v = get Tcxf add 144 i
268put Tcxres mul i 2 uchar and v 255
269put Tcxres add mul i 2 1 uchar and shr v 8 255
270end
271end
272
273fn tcxrun base bool to bool
274tcxwipe
275for i ulong 32
276put Tcxsc i get Tcxpriv i
277end
278put Tcxsc 0 and get Tcxsc 0 248
279put Tcxsc 31 or and get Tcxsc 31 127 64
280if base
281put Tcxf 0 9
282else
283for i ulong 16
284v = ulong get Tcxpeer mul i 2
285v = or v shl ulong get Tcxpeer add mul i 2 1 8
286put Tcxf i v
287end
288put Tcxf 15 and get Tcxf 15 32767
289end
290tcfcopy 32 0
291put Tcxf 16 1
292put Tcxf 64 1
293put Tcxf 112 56129
294put Tcxf 113 1
295for i ulong 255
296j = sub 254 i
297v = ulong get Tcxsc div j 8
298tcflad and shr v rem j 8 1
299end
300tcfinv
301tcfpack
302acc = uchar 0
303for i ulong 32
304acc = or acc get Tcxres i
305end
306ret ne acc 0
307end
308
309fn tcx25519 to bool
310ok = tcxrun false
311for i ulong 32
312put Tcxshare i get Tcxres i
313end
314tcxwipe
315ret ok
316end
317
318fn tcxpublic to bool
319ok = tcxrun true
320for i ulong 32
321put Tcxpub i get Tcxres i
322end
323tcxwipe
324ret ok
325end
326
327fn tccwipe to void
328for i ulong 16
329put Tccs i 0
330put Tccw i 0
331put Tcmblk i 0
332put Tcmtag i 0
333end
334for i ulong 64
335put Tccblk i 0
336end
337for i ulong 32
338put Tcmkey i 0
339end
340for i ulong 10
341put Tcmr i 0
342put Tcmh i 0
343put Tcmt i 0
344put Tcmg i 0
345end
346end
347
348fn tclrot x uint n uint to uint
349ret or shl x n shr x sub 32 n
350end
351
352fn tccqr a ulong b ulong c ulong d ulong to void
353put Tccw a add get Tccw a get Tccw b
354put Tccw d tclrot xor get Tccw d get Tccw a 16
355put Tccw c add get Tccw c get Tccw d
356put Tccw b tclrot xor get Tccw b get Tccw c 12
357put Tccw a add get Tccw a get Tccw b
358put Tccw d tclrot xor get Tccw d get Tccw a 8
359put Tccw c add get Tccw c get Tccw d
360put Tccw b tclrot xor get Tccw b get Tccw c 7
361end
362
363fn tccinit to void
364put Tccs 0 1634760805
365put Tccs 1 857760878
366put Tccs 2 2036477234
367put Tccs 3 1797285236
368for i ulong 8
369v = uint 0
370for j ulong 4
371b = uint get Tckey add mul i 4 j
372v = or v shl b mul j 8
373end
374put Tccs add i 4 v
375end
376for i ulong 3
377v = uint 0
378for j ulong 4
379b = uint get Tciv add mul i 4 j
380v = or v shl b mul j 8
381end
382put Tccs add i 13 v
383end
384end
385
386fn tccgen ctr uint to void
387put Tccs 12 ctr
388for i ulong 16
389put Tccw i get Tccs i
390end
391for i ulong 10
392tccqr 0 4 8 12
393tccqr 1 5 9 13
394tccqr 2 6 10 14
395tccqr 3 7 11 15
396tccqr 0 5 10 15
397tccqr 1 6 11 12
398tccqr 2 7 8 13
399tccqr 3 4 9 14
400end
401for i ulong 16
402v = add get Tccw i get Tccs i
403for j ulong 4
404put Tccblk add mul i 4 j uchar and shr v mul j 8 255
405end
406end
407end
408
409note Standalone block entry for primitive qualification; AEAD initializes the key only once.
410fn tccblock ctr uint to void
411tccinit
412tccgen ctr
413end
414
415note At most 1024 payload blocks: counters 1..1024, never 0 or a wrapped uint.
416fn tccxor n ulong to void
417p = ulong 0
418ctr = uint 1
419while lt p n bound 1024
420tccgen ctr
421m = sub n p
422if gt m 64
423m = 64
424end
425for i ulong m
426v = xor get Tcbuf add p i get Tccblk i
427put Tcbuf add p i v
428end
429p = add p m
430ctr = add ctr 1
431end
432end
433
434note Poly1305 radix 2^13: 10 limbs, B^10 = 5 mod p. Folded products <2^40.
435note Tcmkey is the one-time key, Tcmblk a block, Tcmh the accumulator. No secret tables.
436fn tcminit to void
437for i ulong 10
438put Tcmr i 0
439put Tcmh i 0
440end
441for i ulong 16
442v = ulong get Tcmkey i
443if eq rem i 4 3
444v = and v 15
445end
446if and ne i 0 eq rem i 4 0
447v = and v 252
448end
449j = div mul i 8 13
450v = shl v rem mul i 8 13
451put Tcmr j or get Tcmr j and v 8191
452if lt j 9
453put Tcmr add j 1 or get Tcmr add j 1 shr v 13
454end
455end
456end
457
458fn tcmcarry to void
459for i ulong 9
460v = get Tcmh i
461put Tcmh i and v 8191
462put Tcmh add i 1 add get Tcmh add i 1 shr v 13
463end
464v = get Tcmh 9
465put Tcmh 9 and v 8191
466put Tcmh 0 add get Tcmh 0 mul shr v 13 5
467end
468
469fn tcmblock n ulong to void
470for i ulong n
471j = div mul i 8 13
472v = shl ulong get Tcmblk i rem mul i 8 13
473put Tcmh j add get Tcmh j and v 8191
474if lt j 9
475put Tcmh add j 1 add get Tcmh add j 1 shr v 13
476end
477end
478top = div mul n 8 13
479v = shl ulong 1 rem mul n 8 13
480put Tcmh top add get Tcmh top v
481for i ulong 10
482put Tcmt i 0
483end
484for i ulong 10
485for j ulong 10
486p = add i j
487v = mul get Tcmh i get Tcmr j
488if ge p 10
489p = sub p 10
490v = mul v 5
491end
492put Tcmt p add get Tcmt p v
493end
494end
495for i ulong 10
496put Tcmh i get Tcmt i
497end
498tcmcarry
499tcmcarry
500tcmcarry
501end
502
503fn tcmfinish to void
504note h is normalized below B^10; h+5 overflows exactly when h >= p.
505v = ulong 5
506for i ulong 10
507v = add v get Tcmh i
508put Tcmg i and v 8191
509v = shr v 13
510end
511mask = sub ulong 0 v
512for i ulong 10
513v = xor get Tcmg i get Tcmh i
514put Tcmh i xor get Tcmh i and mask v
515end
516carry = ulong 0
517for i ulong 16
518j = div mul i 8 13
519s = rem mul i 8 13
520v = shr get Tcmh j s
521if lt j 9
522v = or v shl get Tcmh add j 1 sub 13 s
523end
524v = add and v 255 carry
525v = add v ulong get Tcmkey add i 16
526put Tcmtag i uchar and v 255
527carry = shr v 8
528end
529end
530
531fn tcmauth n ulong aadlen ulong to void
532tccgen 0
533for i ulong 32
534put Tcmkey i get Tccblk i
535end
536tcminit
537p = ulong 0
538while lt p aadlen bound 2
539for i ulong 16
540put Tcmblk i 0
541if lt add p i aadlen
542put Tcmblk i get Tcaad add p i
543end
544end
545tcmblock 16
546p = add p 16
547end
548p = 0
549while lt p n bound 4096
550for i ulong 16
551put Tcmblk i 0
552if lt add p i n
553put Tcmblk i get Tcbuf add p i
554end
555end
556tcmblock 16
557p = add p 16
558end
559for i ulong 8
560put Tcmblk i uchar and shr aadlen mul i 8 255
561put Tcmblk add i 8 uchar and shr n mul i 8 255
562end
563tcmblock 16
564tcmfinish
565end
566
567fn tcseal n ulong aadlen ulong to bool
568tccwipe
569for i ulong 16
570put Tctag i 0
571end
572if or gt n 65536 gt aadlen 32
573ret false
574end
575tccinit
576tccxor n
577tcmauth n aadlen
578for i ulong 16
579put Tctag i get Tcmtag i
580end
581tccwipe
582ret true
583end
584
585fn tcopen n ulong aadlen ulong to bool
586tccwipe
587if or gt n 65536 gt aadlen 32
588ret false
589end
590tccinit
591tcmauth n aadlen
592v = uchar 0
593for i ulong 16
594v = or v xor get Tctag i get Tcmtag i
595end
596note Only the final accumulated authentication result branches; no partial tag comparison.
597if ne v 0
598tccwipe
599ret false
600end
601tccxor n
602tccwipe
603ret true
604end
605
606fn tcwipe to void
607tcshwipe
608tcxwipe
609tccwipe
610for i ulong 65536
611put Tcbuf i 0
612end
613for i ulong 32
614put Tckey i 0
615put Tcaad i 0
616put Tcdigest i 0
617put Tcxpriv i 0
618put Tcxpub i 0
619put Tcxpeer i 0
620put Tcxshare i 0
621end
622for i ulong 12
623put Tciv i 0
624end
625for i ulong 16
626put Tctag i 0
627end
628end