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

642 lines · 15538 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/rsa.om (module RS): RSA signature verification, RFC 8017.
3note Library unit for OM-2: no om2 line, no fn main; needs om/tls/sha2.om (sha2_digest) in the unit.
4note
5note API
6note rsa_verify_pkcs1 hsh key hs hlen RSASSA-PKCS1-v1_5 verify (8.2.2), EMSA-PKCS1-v1_5 (9.2):
7note the whole k-byte block 00 01 FF..FF 00 DigestInfo H is built and compared byte for byte;
8note the block is never parsed. DigestInfo prefixes verbatim from RFC 8017 9.2 note 1.
9note rsa_verify_pss ctl key hs hlen RSASSA-PSS verify (8.1.2), EMSA-PSS-VERIFY (9.1.2), MGF1 (B.2.1).
10note hsh: 256, 384 or 512 (SHA-256/384/512). hs = digest (hlen bytes) || signature (the rest).
11note key: DER RSAPublicKey SEQUENCE { INTEGER n, INTEGER e }, strict DER (definite minimal lengths
12note of 1..3 octets, minimal positive INTEGERs, no trailing bytes). Not an SPKI.
13note ctl (PSS): bits 0..15 message hash (256|384|512), bits 16..31 MGF1 hash (256|384|512),
14note bits 32..63 salt length in bytes (exact). TLS 1.3 rsa_pss_rsae_sha256 =
15note 256 | 256 << 16 | 32 << 32 = 0x0000002001000100.
16note Accepted keys: modulus 1024..8192 bits and odd; e odd, e >= 3, bit length of e <= bit length
17note of n. A minimum such as 2048 bits is caller policy (X.509/TLS layer): x509.om accepts
18note server-presented RSA keys (leaf and chain) only with 2048..4096 bits and e odd in
19note 3..2^32-1 (x509_rsaok), and anchors with >= 2048 bits.
20note Return codes: 0 valid, 1 mismatch (signature does not verify), 2 bad key encoding,
21note 3 unsupported size or exponent, 4 signature length != modulus length or s >= n, 5 unsupported
22note hash (or hlen != digest length of the hash). Checked in the order 5, 2/3, 4, 1.
23note hs shorter than the digest length is code 4. All workspace is local (the caller passes no
24note buffer sizes), so these entry points have no caller-contract traps.
25note Untrusted inputs (key, hs, ctl) never trap: every index is range-checked before use (tested by
26note test/rsa/vectors/fuzz.txt: key truncations, header substitutions, signature lengths, ctl values).
27note Timing: all inputs are public (signature verification), so the code is variable time on purpose:
28note early exits, length-dependent loop counts, a square-and-multiply driven by the bits of e, and
29note value-dependent final subtractions in Montgomery reduction. It handles no secrets; nothing is
30note constant time and nothing needs to be. Cost grows with bits(e) * s^2: an e as long as n
31note (allowed, see above) is a CPU cost the caller accepts by passing such a key; x509.om bounds
32note e to 32 bits for server-presented keys, so only a trust anchor can carry a longer e.
33note Arithmetic: 32-bit limbs in ulong cells, little-endian limb order; Montgomery multiplication
34note (CIOS) with n0' = -n^-1 mod 2^32 (Newton iteration from x = n, 3 -> 48 correct bits in 5 steps)
35note and R^2 mod n (64*s modular doublings of 1), both computed at run time from the key. The
36note workspace is a local array of RSWN cells (12.2 KiB) plus a RSEM-byte block per call.
37
38const RSWN ulong 1564
39const RSEM ulong 1024
40const RSMASK ulong 0xffffffff
41note limb regions of the workspace, 258 cells each (256 limbs of an 8192-bit number + 2)
42const RSON ulong 0
43const RSOT ulong 258
44const RSOX ulong 516
45const RSOA ulong 774
46const RSOR ulong 1032
47const RSOG ulong 1290
48note scalar cells of the workspace
49const RSCI ulong 1548
50const RSCS ulong 1549
51const RSCK ulong 1550
52const RSCB ulong 1551
53const RSCNO ulong 1552
54const RSCNL ulong 1553
55const RSCEO ulong 1554
56const RSCEL ulong 1555
57const RSCP ulong 1556
58const RSCL ulong 1557
59const RSCH ulong 1558
60const RSCM ulong 1559
61const RSCSL ulong 1560
62note DigestInfo DER prefixes, RFC 8017 section 9.2 note 1, bytes 0..7, 8..15, 16..18
63note SHA-256: 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20
64const RSD2A ulong 0x3031300d06096086
65const RSD2B ulong 0x4801650304020105
66const RSD2C ulong 0x000420
67note SHA-384: 30 41 30 0d 06 09 60 86 48 01 65 03 04 02 02 05 00 04 30
68const RSD3A ulong 0x3041300d06096086
69const RSD3B ulong 0x4801650304020205
70const RSD3C ulong 0x000430
71note SHA-512: 30 51 30 0d 06 09 60 86 48 01 65 03 04 02 03 05 00 04 40
72const RSD5A ulong 0x3051300d06096086
73const RSD5B ulong 0x4801650304020305
74const RSD5C ulong 0x000440
75
76note T[0..s+1] += A[ao..] * bi (one CIOS multiply row)
77fn bn_mac w edit ulong RSWN s ulong ao ulong bi ulong to void
78cy = ulong 0
79x = ulong 0
80for j ulong s
81x = add add get w add RSOT j mul get w add ao j bi cy
82put w add RSOT j and x RSMASK
83cy = shr x 32
84end
85x = add get w add RSOT s cy
86put w add RSOT s and x RSMASK
87put w add RSOT add s 1 add get w add RSOT add s 1 shr x 32
88end
89
90note T = (T + m * N) / 2^32 with m = T[0] * n0' mod 2^32 (one CIOS reduction row)
91fn bn_red w edit ulong RSWN s ulong to void
92m = and mul get w RSOT get w RSCI RSMASK
93x = add get w RSOT mul m get w RSON
94c = shr x 32
95for j ulong sub s 1
96q = add j 1
97u = mul m get w add RSON q
98x = add add get w add RSOT q u c
99put w add RSOT j and x RSMASK
100c = shr x 32
101end
102x = add get w add RSOT s c
103put w add RSOT sub s 1 and x RSMASK
104put w add RSOT s add get w add RSOT add s 1 shr x 32
105put w add RSOT add s 1 0
106end
107
108note 1 when A >= B (s limbs each), else 0
109fn bn_cmp w view ulong RSWN s ulong ao ulong bo ulong to ulong
110j = s
111while gt j 0 bound 999
112j = sub j 1
113a = get w add ao j
114b = get w add bo j
115if gt a b
116ret 1
117end
118if lt a b
119ret 0
120end
121end
122ret 1
123end
124
125note R = A - B over s limbs (final borrow dropped)
126fn bn_sub w edit ulong RSWN s ulong ao ulong bo ulong ro ulong to void
127bw = ulong 0
128for j ulong s
129x = sub sub get w add ao j get w add bo j bw
130put w add ro j and x RSMASK
131bw = shr x 63
132end
133end
134
135note R = T mod N given T < 2N (T has s + 1 limbs)
136fn bn_fin w edit ulong RSWN s ulong ro ulong to void
137hi = get w add RSOT s
138if eq hi 0
139hi = bn_cmp w s RSOT RSON
140end
141if ne hi 0
142bn_sub w s RSOT RSON ro
143ret
144end
145for j ulong s
146put w add ro j get w add RSOT j
147end
148end
149
150note R = A * B / 2^(32 s) mod N; R may be A or B
151fn bn_mont w edit ulong RSWN s ulong ao ulong bo ulong ro ulong to void
152for j ulong add s 2
153put w add RSOT j 0
154end
155for i ulong s
156bn_mac w s ao get w add bo i
157bn_red w s
158end
159bn_fin w s ro
160end
161
162note region ro = big-endian bytes src (len src <= 4 s), upper limbs zero
163fn bn_load w edit ulong RSWN ro ulong s ulong src view uchar * to void
164for j ulong add s 2
165put w add ro j 0
166end
167n = len src
168for j ulong n
169b = ulong get src sub sub n 1 j
170q = add ro shr j 2
171v = shl b mul and j 3 8
172put w q or get w q v
173end
174end
175
176note em[0..k) = region ro as k big-endian bytes
177fn bn_store w view ulong RSWN ro ulong em edit uchar RSEM k ulong to void
178for j ulong k
179v = shr get w add ro shr j 2 mul and j 3 8
180put em sub sub k 1 j uchar and v 255
181end
182end
183
184note region ro = 2 * region ro over s limbs; returns the carry out
185fn bn_dbl w edit ulong RSWN s ulong ro ulong to ulong
186c = ulong 0
187for j ulong s
188x = or shl get w add ro j 1 c
189put w add ro j and x RSMASK
190c = shr x 32
191end
192ret c
193end
194
195note n0' = -N[0]^-1 mod 2^32: Newton x = x (2 - n x), from x = n (correct to 3 bits; 5 steps > 32 bits)
196fn bn_n0 w edit ulong RSWN to void
197n = get w RSON
198x = n
199for i ulong 5
200x = and mul x sub 2 and mul n x RSMASK RSMASK
201end
202put w RSCI and sub 0 x RSMASK
203end
204
205note region R = R^2 mod N = 2^(64 s) mod N, by 64 s modular doublings of 1
206fn bn_r2 w edit ulong RSWN s ulong to void
207for j ulong add s 2
208put w add RSOR j 0
209end
210put w RSOR 1
211for i ulong mul s 64
212c = bn_dbl w s RSOR
213if eq c 0
214c = bn_cmp w s RSOR RSON
215end
216if ne c 0
217bn_sub w s RSOR RSON RSOR
218end
219end
220end
221
222note region A = G^e mod N (G the signature, e from key bytes), left-to-right binary
223fn rsa_pow w edit ulong RSWN key view uchar * to void
224s = get w RSCS
225bn_n0 w
226bn_r2 w s
227bn_mont w s RSOG RSOR RSOX
228for j ulong s
229put w add RSOA j get w add RSOX j
230end
231eo = get w RSCEO
232el = get w RSCEL
233st = ulong 0
234for i ulong el
235b = ulong get key add eo i
236for t ulong 8
237bit = and shr b sub 7 t 1
238if ne st 0
239bn_mont w s RSOA RSOA RSOA
240if ne bit 0
241bn_mont w s RSOA RSOX RSOA
242end
243end
244if ne bit 0
245st = 1
246end
247end
248end
249for j ulong add s 2
250put w add RSOR j 0
251end
252put w RSOR 1
253bn_mont w s RSOA RSOR RSOA
254end
255
256note DER tag and length at key[p]; content offset and length to RSCP / RSCL; 0 or 2
257fn rsa_tlv w edit ulong RSWN key view uchar * p ulong tag ulong to ulong
258n = len key
259if gt add p 2 n
260ret 2
261end
262if ne ulong get key p tag
263ret 2
264end
265b = ulong get key add p 1
266h = ulong 2
267l = b
268if eq b 0x81
269if gt add p 3 n
270ret 2
271end
272l = ulong get key add p 2
273if lt l 0x80
274ret 2
275end
276h = 3
277end
278if eq b 0x82
279if gt add p 4 n
280ret 2
281end
282l = or shl ulong get key add p 2 8 ulong get key add p 3
283if lt l 0x100
284ret 2
285end
286h = 4
287end
288if and ge b 0x80 eq h 2
289ret 2
290end
291c = add p h
292if gt l sub n c
293ret 2
294end
295put w RSCP c
296put w RSCL l
297ret 0
298end
299
300note DER INTEGER at key[p], minimal and nonnegative; RSCP / RSCL = magnitude without the sign octet
301fn rsa_int w edit ulong RSWN key view uchar * p ulong to ulong
302r = rsa_tlv w key p 2
303if ne r 0
304ret r
305end
306c = get w RSCP
307l = get w RSCL
308if eq l 0
309ret 2
310end
311b = get key c
312if ge b 0x80
313ret 2
314end
315if and eq b 0 gt l 1
316if lt get key add c 1 0x80
317ret 2
318end
319put w RSCP add c 1
320put w RSCL sub l 1
321end
322ret 0
323end
324
325note bit length of the big-endian magnitude key[o..o+l) (first byte nonzero unless the value is 0)
326fn rsa_bits key view uchar * o ulong l ulong to ulong
327if eq l 0
328ret 0
329end
330b = ulong get key o
331nb = ulong 0
332while ne b 0 bound 9
333nb = add nb 1
334b = shr b 1
335end
336if eq nb 0
337ret 0
338end
339ret add mul sub l 1 8 nb
340end
341
342note parse and check the RSAPublicKey; fills RSCNO RSCNL RSCEO RSCEL RSCB RSCK RSCS
343fn rsa_key w edit ulong RSWN key view uchar * to ulong
344r = rsa_tlv w key 0 0x30
345if ne r 0
346ret r
347end
348if ne add get w RSCP get w RSCL len key
349ret 2
350end
351r = rsa_int w key get w RSCP
352if ne r 0
353ret r
354end
355no = get w RSCP
356nl = get w RSCL
357r = rsa_int w key add no nl
358if ne r 0
359ret r
360end
361eo = get w RSCP
362el = get w RSCL
363if ne add eo el len key
364ret 2
365end
366mb = rsa_bits key no nl
367if or lt mb 1024 gt mb 8192
368ret 3
369end
370if eq and get key sub add no nl 1 1 0
371ret 3
372end
373eb = rsa_bits key eo el
374if lt eb 2
375ret 3
376end
377if eq and get key sub add eo el 1 1 0
378ret 3
379end
380if gt eb mb
381ret 3
382end
383put w RSCNO no
384put w RSCNL nl
385put w RSCEO eo
386put w RSCEL el
387put w RSCB mb
388put w RSCK div add mb 7 8
389put w RSCS div add mb 31 32
390ret 0
391end
392
393note RSAVP1: key checks, length and range checks, em[0..k) = sg^e mod n as k bytes
394fn rsa_core w edit ulong RSWN key view uchar * sg view uchar * em edit uchar RSEM to ulong
395r = rsa_key w key
396if ne r 0
397ret r
398end
399k = get w RSCK
400if ne len sg k
401ret 4
402end
403s = get w RSCS
404bn_load w RSON s part key get w RSCNO get w RSCNL
405bn_load w RSOG s sg
406if ne bn_cmp w s RSOG RSON 0
407ret 4
408end
409rsa_pow w key
410bn_store w RSOA em k
411ret 0
412end
413
414note digest length of SHA-256/384/512, 0 for anything else
415fn rsa_hlen h ulong to ulong
416if eq h 256
417ret 32
418end
419if eq h 384
420ret 48
421end
422if eq h 512
423ret 64
424end
425ret 0
426end
427
428note DigestInfo prefix byte i (0..18) for hash h
429fn rsa_dibyte h ulong i ulong to ulong
430x = RSD2A
431y = RSD2B
432z = RSD2C
433if eq h 384
434x = RSD3A
435y = RSD3B
436z = RSD3C
437end
438if eq h 512
439x = RSD5A
440y = RSD5B
441z = RSD5C
442end
443if lt i 8
444ret and shr x mul sub 7 i 8 255
445end
446if lt i 16
447ret and shr y mul sub 15 i 8 255
448end
449ret and shr z mul sub 18 i 8 255
450end
451
452note EMSA-PKCS1-v1_5 comparison of the whole block em[0..k) against 00 01 FF.. 00 DigestInfo dg
453fn rsa_pkcs1_chk em view uchar RSEM k ulong h ulong dg view uchar * to ulong
454hl = len dg
455t = add 19 hl
456if lt k add t 11
457ret 1
458end
459d = ulong get em 0
460d = or d xor ulong get em 1 1
461ps = sub sub k t 3
462for i ulong ps
463d = or d xor ulong get em add 2 i 255
464end
465d = or d ulong get em add 2 ps
466for i ulong 19
467d = or d xor ulong get em add add 3 ps i rsa_dibyte h i
468end
469for i ulong hl
470d = or d xor ulong get em add sub k hl i ulong get dg i
471end
472if ne d 0
473ret 1
474end
475ret 0
476end
477
478fn rsa_verify_pkcs1 hsh ulong key view uchar * hs view uchar * hlen ulong to ulong
479hl = rsa_hlen hsh
480if or eq hl 0 ne hl hlen
481ret 5
482end
483if lt len hs hl
484ret 4
485end
486w = new ulong RSWN 0
487em = new uchar RSEM 0
488r = rsa_core w key part hs hl sub len hs hl em
489if ne r 0
490ret r
491end
492ret rsa_pkcs1_chk em get w RSCK hsh part hs 0 hl
493end
494
495note MGF1 counter C as 4 big-endian bytes after the seed (RFC 8017 B.2.1 step 3.A, I2OSP(counter, 4))
496fn rsa_ctr sd edit uchar 68 hl ulong ct ulong to void
497put sd hl uchar and shr ct 24 255
498put sd add hl 1 uchar and shr ct 16 255
499put sd add hl 2 uchar and shr ct 8 255
500put sd add hl 3 uchar and ct 255
501end
502
503note em[p..min(p+n, e)) ^= hb[0..); returns p + n
504fn rsa_xor em edit uchar RSEM hb view uchar 64 p ulong e ulong n ulong to ulong
505for i ulong n
506if lt p e
507put em p xor get em p get hb i
508end
509p = add p 1
510end
511ret p
512end
513
514note MGF1 seed: sd[0..hl) = em[p..p+hl) for the digest length hl of hash h; returns hl
515fn rsa_seed sd edit uchar 68 em view uchar RSEM p ulong h ulong to ulong
516hl = rsa_hlen h
517for i ulong hl
518put sd i get em add p i
519end
520ret hl
521end
522
523note MGF1 (RFC 8017 B.2.1): em[o..o+db) ^= MGF1(seed = em[o+db..o+db+hl), db) with the RSCM hash
524fn rsa_mgf1 w view ulong RSWN em edit uchar RSEM o ulong db ulong to void
525sd = new uchar 68 0
526hb = new uchar 64 0
527db = add o db
528hl = rsa_seed sd em db get w RSCH
529ct = ulong 0
530while lt o db bound 999
531rsa_ctr sd hl ct
532n = sha2_digest get w RSCM part sd 0 add hl 4 hb
533o = rsa_xor em hb o db n
534ct = add ct 1
535end
536end
537
538note d[0..len s) = s
539fn rsa_cpy d edit uchar * s view uchar * to void
540for i ulong len s
541put d i get s i
542end
543end
544
545note 0 when a and b (same length) are equal, else 1
546fn rsa_neq a view uchar * b view uchar * to ulong
547d = ulong 0
548for i ulong len a
549d = or d ulong xor get a i get b i
550end
551if ne d 0
552ret 1
553end
554ret 0
555end
556
557note M' = 0^8 || mHash || salt into mp (zero on entry); salt = the sl bytes before H at the end of em
558fn rsa_mprime w view ulong RSWN em view uchar RSEM mh view uchar * mp edit uchar 1096 to ulong
559hl = len mh
560sl = get w RSCSL
561hp = sub sub get w RSCK 1 hl
562rsa_cpy part mp 8 hl mh
563rsa_cpy part mp add 8 hl sl part em sub hp sl sl
564ret add add 8 hl sl
565end
566
567note EMSA-PSS-VERIFY steps 12-14: H' = Hash(M') compared with H = em[k-1-hl..k-1)
568fn rsa_pss_hash w view ulong RSWN em view uchar RSEM mh view uchar * to ulong
569mp = new uchar 1096 0
570hb = new uchar 64 0
571n = rsa_mprime w em mh mp
572r = sha2_digest get w RSCH part mp 0 n hb
573hl = len mh
574hp = sub sub get w RSCK 1 hl
575ret rsa_neq part hb 0 hl part em hp hl
576end
577
578note DB prefix p = PS || 0x01 (len p >= 1): 0 when PS is all zero and the last byte is 1, else 1
579fn rsa_ps p view uchar * to ulong
580n = sub len p 1
581d = xor ulong get p n 1
582for i ulong n
583d = or d ulong get p i
584end
585if ne d 0
586ret 1
587end
588ret 0
589end
590
591note EMSA-PSS-VERIFY (RFC 8017 9.1.2) on em[0..k), emBits = modBits - 1
592fn rsa_pss_chk w edit ulong RSWN em edit uchar RSEM mh view uchar * to ulong
593k = get w RSCK
594eb = sub get w RSCB 1
595el = div add eb 7 8
596hl = len mh
597sl = get w RSCSL
598if lt el add add hl sl 2
599ret 1
600end
601o = sub k el
602if and eq o 1 ne get em 0 0
603ret 1
604end
605if ne get em sub k 1 0xbc
606ret 1
607end
608db = sub sub el hl 1
609tm = shr ulong 255 sub mul el 8 eb
610if ne and ulong get em o xor tm 255 0
611ret 1
612end
613rsa_mgf1 w em o db
614put em o uchar and ulong get em o tm
615if ne rsa_ps part em o sub db sl 0
616ret 1
617end
618ret rsa_pss_hash w em mh
619end
620
621fn rsa_verify_pss ctl ulong key view uchar * hs view uchar * hlen ulong to ulong
622hsh = and ctl 0xffff
623mg = and shr ctl 16 0xffff
624sl = shr ctl 32
625hl = rsa_hlen hsh
626if or or eq hl 0 eq rsa_hlen mg 0 ne hl hlen
627ret 5
628end
629if lt len hs hl
630ret 4
631end
632w = new ulong RSWN 0
633em = new uchar RSEM 0
634r = rsa_core w key part hs hl sub len hs hl em
635if ne r 0
636ret r
637end
638put w RSCH hsh
639put w RSCM mg
640put w RSCSL sl
641ret rsa_pss_chk w em part hs 0 hl
642end