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