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
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note ec.om: ECDSA signature VERIFICATION on P-256 and P-384 (FIPS 186-4 6.4, SEC 1 4.1.4), prefix ot_ec_. |
| 3 | note Public data only (certificates, CertificateVerify): variable time is acceptable; strictness is not optional. |
| 4 | note Representation: a workspace w (ulong 1024) of 64 slots of 16 words; slot s holds a number as little-endian |
| 5 | note 32-bit limbs in w[16s .. 16s+12). A modulus slot also keeps its limb count in word 15 and -m^-1 mod 2^32 in |
| 6 | note word 14. Field and scalar arithmetic is Montgomery multiplication (CIOS) modulo p or n; inverses by Fermat. |
| 7 | note Points are Jacobian (X, Y, Z) in three slots, Montgomery form mod p, infinity = Z zero; a = -3 doubling; |
| 8 | note general addition that falls back to doubling for P == Q and gives infinity for P == -Q. |
| 9 | note Curve constants (SEC 2 / FIPS 186-4 D.1.2): |
| 10 | note wolfram (WolframLanguageEvaluator, 2026-11-11, TlsPk2 and OmegaTls): for each curve, with a = -3 and affine |
| 11 | note double-and-add, p == 2^256-2^224+2^192+2^96-1 (resp. 2^384-2^128-2^96+2^32-1), PrimeQ[p], PrimeQ[n], |
| 12 | note Gy^2 == Gx^3 - 3 Gx + b mod p, n*G == infinity, BitLength[p] == BitLength[n] == 256 (384), n < p < 2n, |
| 13 | note -4a^3-27b^2 != 0 mod p: all True for both curves. p < 2n: x(R) mod n needs at most one subtraction. |
| 14 | note Limb bound: one CIOS step adds t_j + a_j b_i + carry <= (2^32-1) + (2^32-1)^2 + (2^32-1) = 2^64 - 1. |
| 15 | global ot_ec_v ulong 8 |
| 16 | global ot_ec_pub uchar 97 |
| 17 | text ot_ec_p256p "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff" |
| 18 | text ot_ec_p256b "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b" |
| 19 | text ot_ec_p256x "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296" |
| 20 | text ot_ec_p256y "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5" |
| 21 | text ot_ec_p256n "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551" |
| 22 | text ot_ec_p384p "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff" |
| 23 | text ot_ec_p384b "b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef" |
| 24 | text ot_ec_p384x "aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7" |
| 25 | text ot_ec_p384y "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f" |
| 26 | text ot_ec_p384n "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973" |
| 27 | note slots |
| 28 | const OT_EC_P ulong 0 |
| 29 | const OT_EC_N ulong 1 |
| 30 | const OT_EC_R2P ulong 2 |
| 31 | const OT_EC_R2N ulong 3 |
| 32 | const OT_EC_ONE ulong 4 |
| 33 | const OT_EC_PM2 ulong 5 |
| 34 | const OT_EC_NM2 ulong 6 |
| 35 | const OT_EC_B ulong 7 |
| 36 | const OT_EC_RP ulong 8 |
| 37 | const OT_EC_RN ulong 9 |
| 38 | const OT_EC_GX ulong 10 |
| 39 | const OT_EC_QX ulong 13 |
| 40 | const OT_EC_TX ulong 16 |
| 41 | const OT_EC_AX ulong 19 |
| 42 | const OT_EC_S1 ulong 22 |
| 43 | const OT_EC_S2 ulong 23 |
| 44 | const OT_EC_E ulong 24 |
| 45 | const OT_EC_W ulong 25 |
| 46 | const OT_EC_U1 ulong 26 |
| 47 | const OT_EC_U2 ulong 27 |
| 48 | const OT_EC_X ulong 28 |
| 49 | note 30..63: scratch for the point formulas |
| 50 | |
| 51 | note ======== limbs ======== |
| 52 | fn ot_ec_k w view ulong 1024 m ulong to ulong |
| 53 | ret get w add mul m 16 15 |
| 54 | end |
| 55 | fn ot_ec_zero w edit ulong 1024 s ulong to void |
| 56 | for i ulong 16 |
| 57 | put w add mul s 16 i 0 |
| 58 | end |
| 59 | end |
| 60 | fn ot_ec_copy w edit ulong 1024 d ulong s ulong to void |
| 61 | for i ulong 12 |
| 62 | put w add mul d 16 i get w add mul s 16 i |
| 63 | end |
| 64 | end |
| 65 | fn ot_ec_iszero w view ulong 1024 s ulong to bool |
| 66 | v = ulong 0 |
| 67 | for i ulong 12 |
| 68 | v = or v get w add mul s 16 i |
| 69 | end |
| 70 | ret eq v 0 |
| 71 | end |
| 72 | fn ot_ec_eq w view ulong 1024 a ulong b ulong to bool |
| 73 | for i ulong 12 |
| 74 | if ne get w add mul a 16 i get w add mul b 16 i |
| 75 | ret false |
| 76 | end |
| 77 | end |
| 78 | ret true |
| 79 | end |
| 80 | note a < modulus m (as integers, k limbs) |
| 81 | fn ot_ec_lt w view ulong 1024 a ulong m ulong to bool |
| 82 | k = ot_ec_k w m |
| 83 | for j ulong k |
| 84 | i = sub sub k 1 j |
| 85 | x = get w add mul a 16 i |
| 86 | y = get w add mul m 16 i |
| 87 | if lt x y |
| 88 | ret true |
| 89 | end |
| 90 | if gt x y |
| 91 | ret false |
| 92 | end |
| 93 | end |
| 94 | ret false |
| 95 | end |
| 96 | note big-endian bytes (at most 4k) into slot s; false if there are more bytes than the slot holds |
| 97 | fn ot_ec_import w edit ulong 1024 s ulong b view uchar * to bool |
| 98 | ot_ec_zero w s |
| 99 | n = len b |
| 100 | if gt n 48 |
| 101 | ret false |
| 102 | end |
| 103 | for i ulong n |
| 104 | j = shr i 2 |
| 105 | sh = mul and i 3 8 |
| 106 | v = shl ulong get b sub sub n 1 i sh |
| 107 | o = add mul s 16 j |
| 108 | put w o or get w o v |
| 109 | end |
| 110 | ret true |
| 111 | end |
| 112 | fn ot_ec_export w view ulong 1024 s ulong o edit uchar * to void |
| 113 | n = len o |
| 114 | for i ulong n |
| 115 | v = get w add mul s 16 shr i 2 |
| 116 | sh = mul and i 3 8 |
| 117 | by = uchar and shr v sh 255 |
| 118 | put o sub sub n 1 i by |
| 119 | end |
| 120 | end |
| 121 | fn ot_ec_hexv c uchar to ulong |
| 122 | if and ge c 48 le c 57 |
| 123 | ret ulong sub c 48 |
| 124 | end |
| 125 | ret add 10 ulong sub c 97 |
| 126 | end |
| 127 | note a constant from hex text into slot s |
| 128 | fn ot_ec_hex w edit ulong 1024 s ulong t view uchar * to void |
| 129 | b = new uchar 48 0 |
| 130 | n = div len t 2 |
| 131 | for i ulong n |
| 132 | hi = ot_ec_hexv get t mul i 2 |
| 133 | lo = ot_ec_hexv get t add mul i 2 1 |
| 134 | put b i uchar add mul hi 16 lo |
| 135 | end |
| 136 | ok = ot_ec_import w s part b 0 n |
| 137 | end |
| 138 | |
| 139 | note ======== modular arithmetic modulo the modulus slot m ======== |
| 140 | fn ot_ec_g w view ulong 1024 s ulong i ulong to ulong |
| 141 | ret get w add mul s 16 i |
| 142 | end |
| 143 | fn ot_ec_s w edit ulong 1024 s ulong i ulong v ulong to void |
| 144 | put w add mul s 16 i v |
| 145 | end |
| 146 | note a - m in place when a >= m or a has a carry word (c) |
| 147 | fn ot_ec_cond w edit ulong 1024 a ulong m ulong c ulong to void |
| 148 | if eq c 0 |
| 149 | if ot_ec_lt w a m |
| 150 | ret |
| 151 | end |
| 152 | end |
| 153 | k = ot_ec_k w m |
| 154 | br = ulong 0 |
| 155 | for i ulong k |
| 156 | x = sub ot_ec_g w a i ot_ec_g w m i |
| 157 | x = sub x br |
| 158 | ot_ec_s w a i and x 4294967295 |
| 159 | br = shr x 63 |
| 160 | end |
| 161 | end |
| 162 | fn ot_ec_add w edit ulong 1024 d ulong a ulong b ulong m ulong to void |
| 163 | k = ot_ec_k w m |
| 164 | c = ulong 0 |
| 165 | for i ulong k |
| 166 | x = add ot_ec_g w a i ot_ec_g w b i |
| 167 | x = add x c |
| 168 | ot_ec_s w d i and x 4294967295 |
| 169 | c = shr x 32 |
| 170 | end |
| 171 | ot_ec_cond w d m c |
| 172 | end |
| 173 | fn ot_ec_sub w edit ulong 1024 d ulong a ulong b ulong m ulong to void |
| 174 | k = ot_ec_k w m |
| 175 | br = ulong 0 |
| 176 | for i ulong k |
| 177 | x = sub ot_ec_g w a i ot_ec_g w b i |
| 178 | x = sub x br |
| 179 | ot_ec_s w d i and x 4294967295 |
| 180 | br = shr x 63 |
| 181 | end |
| 182 | if ne br 0 |
| 183 | ot_ec_addm w d m |
| 184 | end |
| 185 | end |
| 186 | fn ot_ec_addm w edit ulong 1024 d ulong m ulong to void |
| 187 | k = ot_ec_k w m |
| 188 | c = ulong 0 |
| 189 | for i ulong k |
| 190 | x = add ot_ec_g w d i ot_ec_g w m i |
| 191 | x = add x c |
| 192 | ot_ec_s w d i and x 4294967295 |
| 193 | c = shr x 32 |
| 194 | end |
| 195 | end |
| 196 | note t += a * bi over k limbs (t has k + 2 words) |
| 197 | fn ot_ec_row1 t edit ulong 16 w view ulong 1024 a ulong bi ulong k ulong to void |
| 198 | c = ulong 0 |
| 199 | for j ulong k |
| 200 | x = mul ot_ec_g w a j bi |
| 201 | x = add x get t j |
| 202 | x = add x c |
| 203 | put t j and x 4294967295 |
| 204 | c = shr x 32 |
| 205 | end |
| 206 | x = add get t k c |
| 207 | put t k and x 4294967295 |
| 208 | put t add k 1 add get t add k 1 shr x 32 |
| 209 | end |
| 210 | note t = (t + q m) / 2^32 with q = t0 * (-m^-1) mod 2^32 |
| 211 | fn ot_ec_row2 t edit ulong 16 w view ulong 1024 m ulong k ulong to void |
| 212 | q = and mul get t 0 ot_ec_g w m 14 4294967295 |
| 213 | x = add get t 0 mul q ot_ec_g w m 0 |
| 214 | c = shr x 32 |
| 215 | for j ulong sub k 1 |
| 216 | x = mul q ot_ec_g w m add j 1 |
| 217 | x = add x get t add j 1 |
| 218 | x = add x c |
| 219 | put t j and x 4294967295 |
| 220 | c = shr x 32 |
| 221 | end |
| 222 | x = add get t k c |
| 223 | put t sub k 1 and x 4294967295 |
| 224 | put t k add get t add k 1 shr x 32 |
| 225 | put t add k 1 0 |
| 226 | end |
| 227 | note d = a * b / R mod m (Montgomery, R = 2^(32k)); d may equal a or b |
| 228 | fn ot_ec_mul w edit ulong 1024 d ulong a ulong b ulong m ulong to void |
| 229 | t = new ulong 16 0 |
| 230 | k = ot_ec_k w m |
| 231 | for i ulong k |
| 232 | ot_ec_row1 t w a get w add mul b 16 i k |
| 233 | ot_ec_row2 t w m k |
| 234 | end |
| 235 | for i ulong 12 |
| 236 | v = ulong 0 |
| 237 | if lt i k |
| 238 | v = get t i |
| 239 | end |
| 240 | put w add mul d 16 i v |
| 241 | end |
| 242 | ot_ec_cond w d m get t k |
| 243 | end |
| 244 | note d = a ^ e (Montgomery form in and out; e an ordinary number in slot e, bits from the top) |
| 245 | fn ot_ec_pow w edit ulong 1024 d ulong a ulong e ulong m ulong r ulong to void |
| 246 | note r: slot holding R mod m (Montgomery one); uses slot 62 as the accumulator |
| 247 | ot_ec_copy w 62 r |
| 248 | k = ot_ec_k w m |
| 249 | for j ulong mul k 32 |
| 250 | i = sub sub mul k 32 1 j |
| 251 | ot_ec_mul w 62 62 62 m |
| 252 | if ot_ec_bit w e i |
| 253 | ot_ec_mul w 62 62 a m |
| 254 | end |
| 255 | end |
| 256 | ot_ec_copy w d 62 |
| 257 | end |
| 258 | |
| 259 | note ======== curve setup ======== |
| 260 | note modulus slot m from hex: limbs, k, -m^-1 mod 2^32 (Newton: v = v (2 - m0 v), 5 steps double the bits each time) |
| 261 | fn ot_ec_modulus w edit ulong 1024 m ulong t view uchar * k ulong to void |
| 262 | ot_ec_hex w m t |
| 263 | o = mul m 16 |
| 264 | put w add o 15 k |
| 265 | m0 = get w o |
| 266 | v = ulong 1 |
| 267 | for i ulong 5 |
| 268 | v = and mul v sub 2 mul m0 v 4294967295 |
| 269 | end |
| 270 | put w add o 14 and neg v 4294967295 |
| 271 | end |
| 272 | note slot d = 2^(32k e) mod m by doubling 1 (public, small): e = 1 gives R, e = 2 gives R^2 |
| 273 | fn ot_ec_rpow w edit ulong 1024 d ulong m ulong e ulong to void |
| 274 | ot_ec_zero w d |
| 275 | put w mul d 16 1 |
| 276 | k = ot_ec_k w m |
| 277 | for i ulong mul mul k 32 e |
| 278 | ot_ec_add w d d d m |
| 279 | end |
| 280 | end |
| 281 | note slot d = m - 2 |
| 282 | fn ot_ec_m2 w edit ulong 1024 d ulong m ulong to void |
| 283 | ot_ec_copy w d m |
| 284 | o = mul d 16 |
| 285 | x = sub get w o 2 |
| 286 | put w o x |
| 287 | note m is odd and > 2: no borrow beyond limb 0 for these moduli (low limb of p and n is >= 2) |
| 288 | end |
| 289 | fn ot_ec_setup w edit ulong 1024 curve ulong to bool |
| 290 | for i ulong 1024 |
| 291 | put w i 0 |
| 292 | end |
| 293 | if eq curve 1 |
| 294 | ot_ec_modulus w OT_EC_P ot_ec_p256p 8 |
| 295 | ot_ec_modulus w OT_EC_N ot_ec_p256n 8 |
| 296 | ot_ec_hex w OT_EC_B ot_ec_p256b |
| 297 | ot_ec_hex w OT_EC_GX ot_ec_p256x |
| 298 | ot_ec_hex w add OT_EC_GX 1 ot_ec_p256y |
| 299 | else |
| 300 | if ne curve 2 |
| 301 | ret false |
| 302 | end |
| 303 | ot_ec_modulus w OT_EC_P ot_ec_p384p 12 |
| 304 | ot_ec_modulus w OT_EC_N ot_ec_p384n 12 |
| 305 | ot_ec_hex w OT_EC_B ot_ec_p384b |
| 306 | ot_ec_hex w OT_EC_GX ot_ec_p384x |
| 307 | ot_ec_hex w add OT_EC_GX 1 ot_ec_p384y |
| 308 | end |
| 309 | ot_ec_rpow w OT_EC_RP OT_EC_P 1 |
| 310 | ot_ec_rpow w OT_EC_R2P OT_EC_P 2 |
| 311 | ot_ec_rpow w OT_EC_RN OT_EC_N 1 |
| 312 | ot_ec_rpow w OT_EC_R2N OT_EC_N 2 |
| 313 | ot_ec_zero w OT_EC_ONE |
| 314 | put w mul OT_EC_ONE 16 1 |
| 315 | ot_ec_m2 w OT_EC_PM2 OT_EC_P |
| 316 | ot_ec_m2 w OT_EC_NM2 OT_EC_N |
| 317 | ot_ec_mul w OT_EC_B OT_EC_B OT_EC_R2P OT_EC_P |
| 318 | ot_ec_tomont w OT_EC_GX |
| 319 | ret true |
| 320 | end |
| 321 | note the affine point at slots (s, s+1) into Montgomery Jacobian form (s, s+1, s+2), Z = 1 |
| 322 | fn ot_ec_tomont w edit ulong 1024 s ulong to void |
| 323 | ot_ec_mul w s s OT_EC_R2P OT_EC_P |
| 324 | ot_ec_mul w add s 1 add s 1 OT_EC_R2P OT_EC_P |
| 325 | ot_ec_copy w add s 2 OT_EC_RP |
| 326 | end |
| 327 | note y^2 == x^3 - 3x + b for the affine Montgomery point (s, s+1) |
| 328 | fn ot_ec_oncurve w edit ulong 1024 s ulong to bool |
| 329 | ot_ec_mul w 30 add s 1 add s 1 OT_EC_P |
| 330 | ot_ec_mul w 31 s s OT_EC_P |
| 331 | ot_ec_mul w 31 31 s OT_EC_P |
| 332 | ot_ec_sub w 31 31 s OT_EC_P |
| 333 | ot_ec_sub w 31 31 s OT_EC_P |
| 334 | ot_ec_sub w 31 31 s OT_EC_P |
| 335 | ot_ec_add w 31 31 OT_EC_B OT_EC_P |
| 336 | ret ot_ec_eq w 30 31 |
| 337 | end |
| 338 | |
| 339 | note ======== points (Jacobian, a = -3) ======== |
| 340 | note d = 2 s (d may be s). Formulas: delta = Z^2, gamma = Y^2, beta = X gamma, alpha = 3 (X - delta)(X + delta), |
| 341 | note X3 = alpha^2 - 8 beta, Z3 = (Y + Z)^2 - gamma - delta, Y3 = alpha (4 beta - X3) - 8 gamma^2 |
| 342 | fn ot_ec_dbl w edit ulong 1024 d ulong s ulong to void |
| 343 | p = OT_EC_P |
| 344 | if ot_ec_iszero w add s 2 |
| 345 | ot_ec_pcopy w d s |
| 346 | ret |
| 347 | end |
| 348 | ot_ec_mul w 40 add s 2 add s 2 p |
| 349 | ot_ec_mul w 41 add s 1 add s 1 p |
| 350 | ot_ec_mul w 42 s 41 p |
| 351 | ot_ec_sub w 43 s 40 p |
| 352 | ot_ec_add w 44 s 40 p |
| 353 | ot_ec_mul w 43 43 44 p |
| 354 | ot_ec_add w 44 43 43 p |
| 355 | ot_ec_add w 43 44 43 p |
| 356 | ot_ec_add w 45 add s 1 add s 2 p |
| 357 | ot_ec_mul w 45 45 45 p |
| 358 | ot_ec_sub w 45 45 41 p |
| 359 | ot_ec_sub w add d 2 45 40 p |
| 360 | ot_ec_add w 46 42 42 p |
| 361 | ot_ec_add w 46 46 46 p |
| 362 | ot_ec_add w 47 46 46 p |
| 363 | ot_ec_mul w 48 43 43 p |
| 364 | ot_ec_sub w d 48 47 p |
| 365 | ot_ec_sub w 46 46 d p |
| 366 | ot_ec_mul w 46 43 46 p |
| 367 | ot_ec_mul w 41 41 41 p |
| 368 | ot_ec_add w 41 41 41 p |
| 369 | ot_ec_add w 41 41 41 p |
| 370 | ot_ec_add w 41 41 41 p |
| 371 | ot_ec_sub w add d 1 46 41 p |
| 372 | end |
| 373 | fn ot_ec_pcopy w edit ulong 1024 d ulong s ulong to void |
| 374 | if ne d s |
| 375 | ot_ec_copy w d s |
| 376 | ot_ec_copy w add d 1 add s 1 |
| 377 | ot_ec_copy w add d 2 add s 2 |
| 378 | end |
| 379 | end |
| 380 | note d = a + b (d may be a). U1 = X1 Z2^2, U2 = X2 Z1^2, S1 = Y1 Z2^3, S2 = Y2 Z1^3, H = U2 - U1, R = S2 - S1; |
| 381 | note H = 0: R = 0 -> doubling, else infinity. X3 = R^2 - H^3 - 2 U1 H^2, Y3 = R (U1 H^2 - X3) - S1 H^3, Z3 = H Z1 Z2 |
| 382 | fn ot_ec_padd w edit ulong 1024 d ulong a ulong b ulong to void |
| 383 | p = OT_EC_P |
| 384 | if ot_ec_iszero w add a 2 |
| 385 | ot_ec_pcopy w d b |
| 386 | ret |
| 387 | end |
| 388 | if ot_ec_iszero w add b 2 |
| 389 | ot_ec_pcopy w d a |
| 390 | ret |
| 391 | end |
| 392 | ot_ec_mul w 50 add b 2 add b 2 p |
| 393 | ot_ec_mul w 51 add a 2 add a 2 p |
| 394 | ot_ec_mul w 52 a 50 p |
| 395 | ot_ec_mul w 53 b 51 p |
| 396 | ot_ec_mul w 54 add a 1 add b 2 p |
| 397 | ot_ec_mul w 54 54 50 p |
| 398 | ot_ec_mul w 55 add b 1 add a 2 p |
| 399 | ot_ec_mul w 55 55 51 p |
| 400 | ot_ec_sub w 56 53 52 p |
| 401 | ot_ec_sub w 57 55 54 p |
| 402 | if ot_ec_iszero w 56 |
| 403 | if ot_ec_iszero w 57 |
| 404 | ot_ec_dbl w d a |
| 405 | ret |
| 406 | end |
| 407 | ot_ec_zero w add d 2 |
| 408 | ret |
| 409 | end |
| 410 | ot_ec_padd2 w d a b |
| 411 | end |
| 412 | fn ot_ec_padd2 w edit ulong 1024 d ulong a ulong b ulong to void |
| 413 | p = OT_EC_P |
| 414 | ot_ec_mul w 58 56 56 p |
| 415 | ot_ec_mul w 59 58 56 p |
| 416 | ot_ec_mul w 52 52 58 p |
| 417 | ot_ec_mul w 60 add a 2 add b 2 p |
| 418 | ot_ec_mul w add d 2 60 56 p |
| 419 | ot_ec_mul w 61 57 57 p |
| 420 | ot_ec_sub w 61 61 59 p |
| 421 | ot_ec_sub w 61 61 52 p |
| 422 | ot_ec_sub w d 61 52 p |
| 423 | ot_ec_sub w 52 52 d p |
| 424 | ot_ec_mul w 52 57 52 p |
| 425 | ot_ec_mul w 54 54 59 p |
| 426 | ot_ec_sub w add d 1 52 54 p |
| 427 | end |
| 428 | |
| 429 | note ======== the API ======== |
| 430 | note load the public key: uncompressed SEC 1 point 04 || X || Y for the curve (1 P-256, 2 P-384); coordinates < p, |
| 431 | note on the curve (which also excludes infinity, which has no affine form here) |
| 432 | fn ot_ec_key curve ulong pub view uchar * to bool |
| 433 | put ot_ec_v 0 0 |
| 434 | w = new ulong 1024 0 |
| 435 | if not ot_ec_setup w curve |
| 436 | ret false |
| 437 | end |
| 438 | c = ulong 32 |
| 439 | if eq curve 2 |
| 440 | c = 48 |
| 441 | end |
| 442 | note the length first, then the first byte: `or` evaluates both sides, so they are two tests |
| 443 | if ne len pub add mul c 2 1 |
| 444 | ret false |
| 445 | end |
| 446 | if ne get pub 0 4 |
| 447 | ret false |
| 448 | end |
| 449 | if not ot_ec_import w OT_EC_QX part pub 1 c |
| 450 | ret false |
| 451 | end |
| 452 | if not ot_ec_import w add OT_EC_QX 1 part pub add 1 c c |
| 453 | ret false |
| 454 | end |
| 455 | if or not ot_ec_lt w OT_EC_QX OT_EC_P not ot_ec_lt w add OT_EC_QX 1 OT_EC_P |
| 456 | ret false |
| 457 | end |
| 458 | ot_ec_tomont w OT_EC_QX |
| 459 | if not ot_ec_oncurve w OT_EC_QX |
| 460 | ret false |
| 461 | end |
| 462 | for i ulong len pub |
| 463 | put ot_ec_pub i get pub i |
| 464 | end |
| 465 | put ot_ec_v 0 curve |
| 466 | ret true |
| 467 | end |
| 468 | note strict DER ECDSA-Sig-Value: SEQUENCE { INTEGER r, INTEGER s }, short-form lengths only (the largest legal |
| 469 | note value for these curves is below 128 bytes), minimal positive integers, nothing after the SEQUENCE |
| 470 | fn ot_ec_verify alg ulong digest view uchar 64 sig view uchar * to bool |
| 471 | n = len sig |
| 472 | if lt n 8 |
| 473 | ret false |
| 474 | end |
| 475 | if or ne get sig 0 48 ne add 2 ulong get sig 1 n |
| 476 | ret false |
| 477 | end |
| 478 | rs = new ulong 4 0 |
| 479 | if not ot_ec_derint sig 2 rs 0 |
| 480 | ret false |
| 481 | end |
| 482 | if not ot_ec_derint sig get rs 1 rs 2 |
| 483 | ret false |
| 484 | end |
| 485 | if ne get rs 3 n |
| 486 | ret false |
| 487 | end |
| 488 | raw = new uchar 96 0 |
| 489 | c = ulong 32 |
| 490 | if eq get ot_ec_v 0 2 |
| 491 | c = 48 |
| 492 | end |
| 493 | if not ot_ec_place sig get rs 0 sub get rs 1 get rs 0 raw c |
| 494 | ret false |
| 495 | end |
| 496 | if not ot_ec_place sig get rs 2 sub get rs 3 get rs 2 raw add shl c 8 c |
| 497 | ret false |
| 498 | end |
| 499 | ret ot_ec_verraw alg digest part raw 0 mul c 2 |
| 500 | end |
| 501 | note one INTEGER at sig[p]: rs[at] = content start, rs[at+1] = position after it; false when not strict DER |
| 502 | fn ot_ec_derint sig view uchar * p ulong rs edit ulong 4 o ulong to bool |
| 503 | n = len sig |
| 504 | if gt add p 2 n |
| 505 | ret false |
| 506 | end |
| 507 | if ne get sig p 2 |
| 508 | ret false |
| 509 | end |
| 510 | l = ulong get sig add p 1 |
| 511 | if or eq l 0 ge l 128 |
| 512 | ret false |
| 513 | end |
| 514 | s = add p 2 |
| 515 | if gt add s l n |
| 516 | ret false |
| 517 | end |
| 518 | if ne and get sig s 128 0 |
| 519 | ret false |
| 520 | end |
| 521 | if gt l 1 |
| 522 | if and eq get sig s 0 eq and get sig add s 1 128 0 |
| 523 | ret false |
| 524 | end |
| 525 | end |
| 526 | put rs o s |
| 527 | put rs add o 1 add s l |
| 528 | ret true |
| 529 | end |
| 530 | note copy the integer content sig[s, s+l) right-aligned into raw[o, o+c), dropping one leading zero; |
| 531 | note oc = 256 o + c (the parameter-slot limit) |
| 532 | fn ot_ec_place sig view uchar * s ulong l ulong raw edit uchar 96 oc ulong to bool |
| 533 | c = and oc 255 |
| 534 | a = s |
| 535 | m = l |
| 536 | if and gt m 1 eq get sig a 0 |
| 537 | a = add a 1 |
| 538 | m = sub m 1 |
| 539 | end |
| 540 | if gt m c |
| 541 | ret false |
| 542 | end |
| 543 | q = add shr oc 8 sub c m |
| 544 | for i ulong m |
| 545 | put raw add q i get sig add a i |
| 546 | end |
| 547 | ret true |
| 548 | end |
| 549 | note verify r || s (each the curve's byte length) against the loaded key |
| 550 | fn ot_ec_verraw alg ulong digest view uchar 64 rs view uchar * to bool |
| 551 | curve = get ot_ec_v 0 |
| 552 | if eq curve 0 |
| 553 | ret false |
| 554 | end |
| 555 | c = ulong 32 |
| 556 | if eq curve 2 |
| 557 | c = 48 |
| 558 | end |
| 559 | if ne len rs mul c 2 |
| 560 | ret false |
| 561 | end |
| 562 | hl = ulong 0 |
| 563 | if eq alg 0 |
| 564 | hl = 32 |
| 565 | end |
| 566 | if eq alg 1 |
| 567 | hl = 48 |
| 568 | end |
| 569 | if eq alg 2 |
| 570 | hl = 64 |
| 571 | end |
| 572 | if eq hl 0 |
| 573 | ret false |
| 574 | end |
| 575 | w = new ulong 1024 0 |
| 576 | ok = ot_ec_setup w curve |
| 577 | if not ot_ec_loadq w c |
| 578 | ret false |
| 579 | end |
| 580 | ok = ot_ec_import w OT_EC_S1 part rs 0 c |
| 581 | ok = ot_ec_import w OT_EC_S2 part rs c c |
| 582 | if or ot_ec_iszero w OT_EC_S1 ot_ec_iszero w OT_EC_S2 |
| 583 | ret false |
| 584 | end |
| 585 | if or not ot_ec_lt w OT_EC_S1 OT_EC_N not ot_ec_lt w OT_EC_S2 OT_EC_N |
| 586 | ret false |
| 587 | end |
| 588 | note e: the leftmost min(hash bits, order bits) bits of the digest; order bits are 8c here |
| 589 | m = hl |
| 590 | if gt m c |
| 591 | m = c |
| 592 | end |
| 593 | ok = ot_ec_import w OT_EC_E part digest 0 m |
| 594 | ot_ec_cond w OT_EC_E OT_EC_N 0 |
| 595 | ret ot_ec_check w |
| 596 | end |
| 597 | fn ot_ec_loadq w edit ulong 1024 c ulong to bool |
| 598 | q = new uchar 97 0 |
| 599 | for i ulong add mul c 2 1 |
| 600 | put q i get ot_ec_pub i |
| 601 | end |
| 602 | ok = ot_ec_import w OT_EC_QX part q 1 c |
| 603 | ok = ot_ec_import w add OT_EC_QX 1 part q add 1 c c |
| 604 | ot_ec_tomont w OT_EC_QX |
| 605 | ret true |
| 606 | end |
| 607 | note w = s^-1, u1 = e w, u2 = r w (mod n); R = u1 G + u2 Q; accept iff R is finite and x(R) mod n == r |
| 608 | fn ot_ec_check w edit ulong 1024 to bool |
| 609 | n = OT_EC_N |
| 610 | ot_ec_mul w OT_EC_W OT_EC_S2 OT_EC_R2N n |
| 611 | ot_ec_pow w OT_EC_W OT_EC_W OT_EC_NM2 n OT_EC_RN |
| 612 | ot_ec_mul w OT_EC_U1 OT_EC_E OT_EC_W n |
| 613 | ot_ec_mul w OT_EC_U2 OT_EC_S1 OT_EC_W n |
| 614 | ot_ec_padd w OT_EC_TX OT_EC_GX OT_EC_QX |
| 615 | ot_ec_zero w add OT_EC_AX 2 |
| 616 | k = ot_ec_k w n |
| 617 | for j ulong mul k 32 |
| 618 | i = sub sub mul k 32 1 j |
| 619 | ot_ec_dbl w OT_EC_AX OT_EC_AX |
| 620 | b1 = ot_ec_bit w OT_EC_U1 i |
| 621 | b2 = ot_ec_bit w OT_EC_U2 i |
| 622 | if and b1 b2 |
| 623 | ot_ec_padd w OT_EC_AX OT_EC_AX OT_EC_TX |
| 624 | else |
| 625 | if b1 |
| 626 | ot_ec_padd w OT_EC_AX OT_EC_AX OT_EC_GX |
| 627 | end |
| 628 | if b2 |
| 629 | ot_ec_padd w OT_EC_AX OT_EC_AX OT_EC_QX |
| 630 | end |
| 631 | end |
| 632 | end |
| 633 | if ot_ec_iszero w add OT_EC_AX 2 |
| 634 | ret false |
| 635 | end |
| 636 | p = OT_EC_P |
| 637 | ot_ec_pow w OT_EC_X add OT_EC_AX 2 OT_EC_PM2 p OT_EC_RP |
| 638 | ot_ec_mul w OT_EC_X OT_EC_X OT_EC_X p |
| 639 | ot_ec_mul w OT_EC_X OT_EC_X OT_EC_AX p |
| 640 | ot_ec_mul w OT_EC_X OT_EC_X OT_EC_ONE p |
| 641 | ot_ec_cond w OT_EC_X n 0 |
| 642 | ret ot_ec_eq w OT_EC_X OT_EC_S1 |
| 643 | end |
| 644 | fn ot_ec_bit w view ulong 1024 s ulong i ulong to bool |
| 645 | ret eq and shr get w add mul s 16 shr i 5 and i 31 1 1 |
| 646 | end |