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

455 lines · 11405 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note omtls RSA signature verification, RFC 8017: RSAVP1 (5.2.2), RSASSA-PKCS1-v1_5 verify (8.2.2),
3note RSASSA-PSS verify (8.1.2 with EMSA-PSS-VERIFY 9.1.2) and MGF1 (B.2.1). Every input is public, so the
4note code is variable time; it is strict and never traps on hostile input.
5note One key at a time: ot_rsa_key (or ot_rsa_parsekey) loads it into the globals below, then every verify
6note uses it. A failed load leaves no key, and every verify then gives false.
7note Moduli up to 16384 bits: src/bn.om holds 512 limbs on the stack because the current compiler has no
8note run-time allocation; this is a size limit of the implementation, not a policy. Larger keys are refused.
9note Hash alg ids as ot_hash: 0 SHA-256, 1 SHA-384, 2 SHA-512.
10note ot_rsa_minbits: smallest accepted modulus in bits; 0 means the default 2048. Only tests lower it.
11global ot_rsa_minbits ulong 1
12note Loaded key: Montgomery context (src/bn.om layout), R^2 mod n, public exponent bytes, and
13note ot_rsa_kv: 0 loaded (1), 1 modulus bytes, 2 modulus bits, 3 exponent bytes.
14global ot_rsa_kc ulong 514
15global ot_rsa_kr ulong 512
16global ot_rsa_ke uchar 2048
17global ot_rsa_kv ulong 4
18fn ot_rsa_hl alg ulong to ulong
19if eq alg 0
20ret 32
21end
22if eq alg 1
23ret 48
24end
25if eq alg 2
26ret 64
27end
28ret 0
29end
30note Index of the first nonzero byte of a (len a when there is none).
31fn ot_rsa_lz a view uchar * to ulong
32i = ulong 0
33while lt i len a bound 9223372036854775807
34if ne get a i 0
35ret i
36end
37i = add i 1
38end
39ret i
40end
41note Bit length of the big-endian number in a whose first nonzero byte is at z.
42fn ot_rsa_bits a view uchar * z ulong to ulong
43v = get a z
44b = mul sub sub len a z 1 8
45while ne v 0 bound 9
46b = add b 1
47v = shr v 1
48end
49ret b
50end
51fn ot_rsa_set n view uchar * e view uchar * nb ulong to bool
52nn = new ulong 512 0
53ee = new ulong 512 0
54cc = new ulong 514 0
55rr = new ulong 512 0
56ok = ot_bn_import nn n
57ok = ot_bn_import ee e
58if ge ot_bn_cmp ee nn 512 0
59ret false
60end
61if not ot_bn_ctx cc nn
62ret false
63end
64ot_bn_rr rr cc
65for i ulong 514
66put ot_rsa_kc i get cc i
67end
68for i ulong 512
69put ot_rsa_kr i get rr i
70end
71for i ulong 2048
72put ot_rsa_ke i 0
73end
74for i ulong len e
75put ot_rsa_ke i get e i
76end
77put ot_rsa_kv 1 len n
78put ot_rsa_kv 2 nb
79put ot_rsa_kv 3 len e
80put ot_rsa_kv 0 1
81ret true
82end
83note n and e big-endian, leading zero bytes allowed. Refused: n zero, even, under the minimum size or over
84note 16384 bits; e even, below 3, not below n, or 2^32 or more.
85note The e < 2^32 limit contains untrusted input: a key comes from a peer's certificate chain before the peer is
86note authenticated, and an exponent as long as a 16384-bit modulus would cost about 24k Montgomery products per
87note verify. Deployed RSA keys use e = 3 or 65537; a larger e is a defined refusal (false), never a trap.
88fn ot_rsa_load n view uchar * e view uchar * to bool
89zn = ot_rsa_lz n
90ze = ot_rsa_lz e
91if or eq zn len n eq ze len e
92ret false
93end
94nl = sub len n zn
95el = sub len e ze
96if or gt nl 2048 gt el nl
97ret false
98end
99if gt el 4
100ret false
101end
102mb = get ot_rsa_minbits 0
103if eq mb 0
104mb = ulong 2048
105end
106nb = ot_rsa_bits n zn
107if lt nb mb
108ret false
109end
110if eq and get e sub len e 1 1 0
111ret false
112end
113if eq el 1
114if lt get e sub len e 1 3
115ret false
116end
117end
118ret ot_rsa_set view part n zn nl view part e ze el nb
119end
120export fn ot_rsa_key n view uchar * e view uchar * to bool
121put ot_rsa_kv 0 0
122ret ot_rsa_load n e
123end
124note DER tag and length at st 0 (strict: definite, minimal, at most 3 length bytes). Gives the content
125note length with st 0 at the content; any fault sets st 1 to 1.
126fn ot_rsa_tl d view uchar * st edit ulong 4 tag uchar to ulong
127p = get st 0
128if gt add p 2 len d
129put st 1 1
130ret 0
131end
132if ne get d p tag
133put st 1 1
134ret 0
135end
136b = get d add p 1
137p = add p 2
138if lt b 128
139put st 0 p
140ret ulong b
141end
142nb = ulong and b 127
143if or eq nb 0 gt nb 3
144put st 1 1
145ret 0
146end
147if gt add p nb len d
148put st 1 1
149ret 0
150end
151v = ulong 0
152for i ulong nb
153v = or shl v 8 ulong get d add p i
154end
155if or eq get d p 0 lt v 128
156put st 1 1
157ret 0
158end
159put st 0 add p nb
160ret v
161end
162note A DER INTEGER content d p .. p+n: positive and minimal.
163fn ot_rsa_dint d view uchar * p ulong n ulong to bool
164if eq n 0
165ret false
166end
167if ge get d p 128
168ret false
169end
170if gt n 1
171if eq get d p 0
172if lt get d add p 1 128
173ret false
174end
175end
176end
177ret true
178end
179note RSAPublicKey (RFC 8017 A.1.1) DER: SEQUENCE { INTEGER n, INTEGER e }, nothing after it. Loads the key
180note as ot_rsa_key does; false (and no key) for any other encoding or a refused key.
181export fn ot_rsa_parsekey der view uchar * to bool
182put ot_rsa_kv 0 0
183st = new ulong 4 0
184ee = new uchar 2048 0
185sl = ot_rsa_tl der st 48
186if or ne get st 1 0 ne add get st 0 sl len der
187ret false
188end
189nl = ot_rsa_tl der st 2
190np = get st 0
191if or ne get st 1 0 gt add np nl len der
192ret false
193end
194put st 0 add np nl
195el = ot_rsa_tl der st 2
196ep = get st 0
197if or ne get st 1 0 ne add ep el len der
198ret false
199end
200if or not ot_rsa_dint der np nl not ot_rsa_dint der ep el
201ret false
202end
203if gt el 2048
204ret false
205end
206for i ulong el
207put ee i get der add ep i
208end
209ret ot_rsa_load view part der np nl view part ee 0 el
210end
211note RSAVP1: em (exactly the modulus length) = sig^e mod n. False when no key is loaded, sig is not exactly
212note the modulus length, or sig >= n.
213fn ot_rsa_vp1 sig view uchar * em edit uchar * to bool
214k = get ot_rsa_kv 1
215if or eq get ot_rsa_kv 0 0 ne len sig k
216ret false
217end
218if ne len em k
219ret false
220end
221cc = new ulong 514 0
222rr = new ulong 512 0
223ee = new uchar 2048 0
224ss = new ulong 512 0
225yy = new ulong 512 0
226for i ulong 514
227put cc i get ot_rsa_kc i
228end
229for i ulong 512
230put rr i get ot_rsa_kr i
231end
232el = get ot_rsa_kv 3
233for i ulong el
234put ee i get ot_rsa_ke i
235end
236ok = ot_bn_import ss sig
237if ot_bn_ge ss cc
238ret false
239end
240ot_bn_pow yy ss view part ee 0 el cc rr
241ret ot_bn_export yy em
242end
243note DigestInfo DER prefix (RFC 8017 9.2 note 1), NULL parameters present, 19 bytes at o p:
244note 30 L 30 0d 06 09 60 86 48 01 65 03 04 02 H 05 00 04 HL with L 31/41/51, H 01/02/03, HL 20/30/40.
245fn ot_rsa_di o edit uchar 2048 p ulong alg ulong to void
246a = uchar alg
247put o p 48
248put o add p 1 add 49 mul a 16
249put o add p 2 48
250put o add p 3 13
251put o add p 4 6
252put o add p 5 9
253put o add p 6 96
254put o add p 7 134
255put o add p 8 72
256put o add p 9 1
257put o add p 10 101
258put o add p 11 3
259put o add p 12 4
260put o add p 13 2
261put o add p 14 add a 1
262put o add p 15 5
263put o add p 16 0
264put o add p 17 4
265put o add p 18 add 32 mul a 16
266end
267note EMSA-PKCS1-v1_5 (9.2) of digest into o 0 .. k: 00 01 FF..FF 00 DigestInfo digest.
268fn ot_rsa_emp o edit uchar 2048 k ulong alg ulong digest view uchar 64 to void
269hl = ot_rsa_hl alg
270t = add 19 hl
271put o 0 0
272put o 1 1
273for i ulong sub sub k t 3
274put o add i 2 255
275end
276put o sub sub k t 1 0
277ot_rsa_di o sub k t alg
278for i ulong hl
279put o add sub k hl i get digest i
280end
281end
282fn ot_rsa_same a view uchar 2048 b view uchar 2048 s ulong k ulong to bool
283v = uchar 0
284for i ulong sub k s
285v = or v xor get a add s i get b add s i
286end
287ret eq v 0
288end
289note RSASSA-PKCS1-v1_5 verify: the expected block is rebuilt from the digest and compared whole; the
290note signature's DigestInfo is never parsed, so the form without NULL parameters is refused.
291export fn ot_rsa_pkcs1 sig view uchar * alg ulong digest view uchar 64 to bool
292hl = ot_rsa_hl alg
293k = get ot_rsa_kv 1
294if or eq hl 0 lt k add hl 30
295ret false
296end
297em = new uchar 2048 0
298ex = new uchar 2048 0
299if not ot_rsa_vp1 sig part em 0 k
300ret false
301end
302ot_rsa_emp ex k alg digest
303ret ot_rsa_same em ex 0 k
304end
305note One hash of a || b (b: 4 counter bytes) into o.
306fn ot_rsa_h2 alg ulong a view uchar * b view uchar 4 o edit uchar 64 to void
307c5 = new ulong 32 0
308c2 = new uchar 104 0
309t = new uchar 32 0
310if eq alg 0
311sha256_init c2
312sha256_update c2 a
313sha256_update c2 part b 0 4
314sha256_final c2 t
315for i ulong 32 unroll
316put o i get t i
317end
318else
319ot_s5_init c5 eq alg 1
320ot_s5_update c5 a
321ot_s5_update c5 part b 0 4
322ot_s5_final c5 o
323end
324end
325note MGF1 (RFC 8017 B.2.1) with hash alg: mask = the first len mask bytes of Hash(seed || C) for the 4-byte
326note big-endian counters C = 0, 1, ... An unknown alg gives an all-zero mask.
327export fn ot_rsa_mgf1 alg ulong seed view uchar * mask edit uchar * to void
328hl = ot_rsa_hl alg
329o = new uchar 64 0
330cb = new uchar 4 0
331for i ulong len mask bound 9223372036854775807
332put mask i 0
333end
334p = ulong 0
335c = ulong 0
336while and ne hl 0 lt p len mask bound 9223372036854775807
337put cb 0 uchar and shr c 24 255
338put cb 1 uchar and shr c 16 255
339put cb 2 uchar and shr c 8 255
340put cb 3 uchar and c 255
341ot_rsa_h2 alg seed cb o
342for i ulong hl
343if lt add p i len mask
344put mask add p i get o i
345end
346end
347p = add p hl
348c = add c 1
349end
350end
351note PSS geometry g: 0 hLen, 1 emLen, 2 bytes before EM in the k-byte block, 3 DB length, 4 zero top bits of
352note EM, 5 sLen. False when emLen < hLen + sLen + 2 (9.1.2 step 3).
353fn ot_rsa_geo g edit ulong 8 alg ulong slen ulong to bool
354hl = ot_rsa_hl alg
355k = get ot_rsa_kv 1
356eb = sub get ot_rsa_kv 2 1
357el = shr add eb 7 3
358if or eq hl 0 gt slen k
359ret false
360end
361if lt el add add hl slen 2
362ret false
363end
364put g 0 hl
365put g 1 el
366put g 2 sub k el
367put g 3 sub sub el hl 1
368put g 4 sub mul el 8 eb
369put g 5 slen
370ret true
371end
372note M' = 00 x 8 || mHash || salt (salt: the last sLen bytes of DB).
373fn ot_rsa_mp mp edit uchar 2128 db view uchar 2048 g view ulong 8 digest view uchar 64 to void
374hl = get g 0
375for i ulong hl
376put mp add 8 i get digest i
377end
378s0 = sub get g 3 get g 5
379b0 = add 8 hl
380for i ulong get g 5
381put mp add b0 i get db add s0 i
382end
383end
384fn ot_rsa_hcmp h view uchar 64 em view uchar 2048 p ulong n ulong to bool
385v = uchar 0
386for i ulong n
387v = or v xor get h i get em add p i
388end
389ret eq v 0
390end
391note Steps 12 to 14: H' = Hash(M') against H.
392fn ot_rsa_hck db view uchar 2048 em view uchar 2048 g view ulong 8 digest view uchar 64 alg ulong to bool
393mp = new uchar 2128 0
394h = new uchar 64 0
395ot_rsa_mp mp db g digest
396r = ot_hash alg view part mp 0 add add 8 get g 0 get g 5 h
397ret ot_rsa_hcmp h em add get g 2 get g 3 get g 0
398end
399note Steps 8 to 11: DB = maskedDB xor dbMask, top bits cleared, then PS zeros and the 01 separator.
400fn ot_rsa_dbck db edit uchar 2048 em view uchar 2048 g view ulong 8 digest view uchar 64 alg ulong to bool
401off = get g 2
402dl = get g 3
403for i ulong dl
404put db i xor get db i get em add off i
405end
406put db 0 uchar and ulong get db 0 shr 255 get g 4
407ps = sub sub dl get g 5 1
408for i ulong ps
409if ne get db i 0
410ret false
411end
412end
413if ne get db ps 1
414ret false
415end
416ret ot_rsa_hck db em g digest alg
417end
418note EMSA-PSS-VERIFY (9.1.2) on the k-byte RSAVP1 output em; EM is its last emLen bytes, and any byte
419note before EM must be zero (emLen < k when modBits - 1 is a multiple of 8).
420fn ot_rsa_emsa em view uchar 2048 alg ulong digest view uchar 64 slen ulong to bool
421g = new ulong 8 0
422db = new uchar 2048 0
423if not ot_rsa_geo g alg slen
424ret false
425end
426off = get g 2
427el = get g 1
428if ne off 0
429if ne get em 0 0
430ret false
431end
432end
433if ne get em sub add off el 1 188
434ret false
435end
436if ne shr ulong get em off sub 8 get g 4 0
437ret false
438end
439dl = get g 3
440ot_rsa_mgf1 alg view part em add off dl get g 0 part db 0 dl
441ret ot_rsa_dbck db em g digest alg
442end
443note RSASSA-PSS verify with MGF1 over the same hash and salt length exactly slen (TLS 1.3: slen = hLen).
444export fn ot_rsa_pss sig view uchar * alg ulong digest view uchar 64 slen ulong to bool
445hl = ot_rsa_hl alg
446k = get ot_rsa_kv 1
447if or eq hl 0 gt slen k
448ret false
449end
450em = new uchar 2048 0
451if not ot_rsa_vp1 sig part em 0 k
452ret false
453end
454ret ot_rsa_emsa em alg digest slen
455end