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