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

194 lines · 3807 bytes Raw Blame History
1note omtls SHA-384 / SHA-512 driver layer (FIPS 180-4) and the one-shot ot_hash dispatch.
2note Context c (ulong 32): 0..7 chaining value, 8..23 the pending block as 16 big-endian words (bytes past the
3note pending count are zero), 24 bytes absorbed (mod 2^64), 25 digest length (48 SHA-384, 64 SHA-512), 26..31 zero.
4note Branches and indices here depend only on lengths and counts; message and state bytes only flow through
5note arithmetic and the CT core.
6fn ot_s5_ins c edit ulong 32 k ulong v uchar to void
7wi = add 8 shr k 3
8x = shl ulong v shl sub 7 and k 7 3
9put c wi or get c wi x
10end
11fn ot_s5_wd d view uchar * p ulong m edit ulong 16 to void
12for i ulong 16 unroll
13v = ulong 0
14for j ulong 8 unroll
15v = or shl v 8 ulong get d add p add mul i 8 j
16end
17put m i v
18end
19end
20fn ot_s5_z8 a edit ulong 8 to void
21for i ulong 8 unroll
22put a i 0
23end
24end
25fn ot_s5_z16 a edit ulong 16 to void
26for i ulong 16 unroll
27put a i 0
28end
29end
30fn ot_s5_z80 a edit ulong 80 to void
31for i ulong 80
32put a i 0
33end
34end
35note Hash the full pending block of c into h and clear it.
36fn ot_s5_flush c edit ulong 32 h edit ulong 8 m edit ulong 16 w edit ulong 80 to void
37for i ulong 16 unroll
38put m i get c add 8 i
39put c add 8 i 0
40end
41ot_s5_blk h m w
42end
43export fn ot_s5_init c edit ulong 32 is384 bool to void
44h = new ulong 8 0
45for i ulong 32 unroll
46put c i 0
47end
48if is384
49ot_s5_iv3 h
50put c 25 48
51else
52ot_s5_iv5 h
53put c 25 64
54end
55for i ulong 8 unroll
56put c i get h i
57end
58end
59export fn ot_s5_update c edit ulong 32 d view uchar * to void
60h = new ulong 8 0
61m = new ulong 16 0
62w = new ulong 80 0
63for i ulong 8 unroll
64put h i get c i
65end
66n = len d
67k = and get c 24 127
68p = ulong 0
69if gt k 0
70while and lt p n lt k 128 bound 128
71ot_s5_ins c k get d p
72k = add k 1
73p = add p 1
74end
75if eq k 128
76ot_s5_flush c h m w
77k = 0
78end
79end
80while ge sub n p 128 bound 9223372036854775807
81ot_s5_wd d p m
82ot_s5_blk h m w
83p = add p 128
84end
85while lt p n bound 128
86ot_s5_ins c k get d p
87k = add k 1
88p = add p 1
89end
90put c 24 add get c 24 n
91for i ulong 8 unroll
92put c i get h i
93end
94ot_s5_z8 h
95ot_s5_z16 m
96ot_s5_z80 w
97end
98note Pad (0x80, zeros, 128-bit big-endian bit length) and write the digest; c is not changed.
99export fn ot_s5_final c view ulong 32 o edit uchar 64 to void
100h = new ulong 8 0
101m = new ulong 16 0
102w = new ulong 80 0
103for i ulong 8 unroll
104put h i get c i
105end
106for i ulong 16 unroll
107put m i get c add 8 i
108end
109cnt = get c 24
110k = and cnt 127
111wi = shr k 3
112x = shl sub 7 and k 7 3
113x = shl ulong 128 x
114put m wi or get m wi x
115if gt k 111
116ot_s5_blk h m w
117ot_s5_z16 m
118end
119put m 14 shr cnt 61
120put m 15 shl cnt 3
121ot_s5_blk h m w
122for j ulong 8 unroll
123for i ulong 8 unroll
124put o add mul j 8 i ot_ct_lo8 shr get h j sub 56 mul i 8
125end
126end
127if eq get c 25 48
128for i ulong 16 unroll
129put o add 48 i 0
130end
131end
132ot_s5_z8 h
133ot_s5_z16 m
134ot_s5_z80 w
135end
136export fn ot_sha384 d view uchar * o edit uchar 48 to void
137c = new ulong 32 0
138t = new uchar 64 0
139ot_s5_init c true
140ot_s5_update c d
141ot_s5_final c t
142for i ulong 48 unroll
143put o i get t i
144end
145for i ulong 64 unroll
146put t i 0
147end
148for i ulong 32 unroll
149put c i 0
150end
151end
152export fn ot_sha512 d view uchar * o edit uchar 64 to void
153c = new ulong 32 0
154ot_s5_init c false
155ot_s5_update c d
156ot_s5_final c o
157for i ulong 32 unroll
158put c i 0
159end
160end
161note One-shot hash: alg 0 SHA-256 (vendored), 1 SHA-384, 2 SHA-512. Gives the digest length and zeroes o past it;
162note an unknown alg gives 0 with o all zero.
163export fn ot_hash alg ulong d view uchar * o edit uchar 64 to ulong
164t = new uchar 32 0
165c = new ulong 32 0
166if eq alg 0
167sha256_hash d t
168for i ulong 64 unroll
169put o i 0
170end
171for i ulong 32 unroll
172put o i get t i
173put t i 0
174end
175ret 32
176end
177if eq alg 1
178ot_s5_init c true
179ot_s5_update c d
180ot_s5_final c o
181for i ulong 32 unroll
182put c i 0
183end
184ret 48
185end
186if eq alg 2
187ot_sha512 d o
188ret 64
189end
190for i ulong 64 unroll
191put o i 0
192end
193ret 0
194end