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
omtls / tests / drv / ecdsa.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tests/drv/ecdsa.om: ECDSA P-256/P-384 verification against vector files (Wycheproof and NIST CAVP SigVer, |
| 3 | note converted to lines "NAME CURVEBITS ENC RC HASHBITS PUB SIG MSG DIGEST", ENC d = DER, r = raw r||s, RC the expected |
| 4 | note result code: 0 the signature is valid, 1 it must be refused). Every line: the digest must equal ot_hash(MSG) |
| 5 | note (the hash path), then ot_ec_key + verify must accept exactly when RC is 0. |
| 6 | note Edge cases, from the first valid DER case of each curve: r = 0, s = 0, r = n, s = n, r = n + 1 (refused), |
| 7 | note (r, n - s) (accepted: ECDSA signatures are malleable this way, and TLS does not rely on uniqueness), the public |
| 8 | note key with its last byte changed (off the curve: refused) and with x = p (refused). |
| 9 | note usage: build/drv_ecdsa FILE... prints failures, then "PASS ecdsa cases=N" (exit 0) or "FAIL ecdsa ..." (exit 1) |
| 10 | global ed_f uchar 4194304 |
| 11 | global ed_v ulong 16 |
| 12 | global ed_dg uchar 64 |
| 13 | global ed_pb uchar 128 |
| 14 | global ed_sb uchar 128 |
| 15 | text ed_pass "PASS ecdsa cases=" |
| 16 | text ed_failt "FAIL ecdsa pass=" |
| 17 | text ed_ff " fail=" |
| 18 | text ed_bad "FAIL case " |
| 19 | text ed_edge "FAIL edge case " |
| 20 | text ed_nl "\n" |
| 21 | note wolfram (WolframLanguageEvaluator, 2026-11-11): P-256 p mod 4 = 3, JacobiSymbol[b, p] = 1, y = PowerMod[b, |
| 22 | note (p+1)/4, p] satisfies y^2 = b mod p: so (0, y) is ON the curve, and (p, y) is the same point with x out of range |
| 23 | text ed_y0 "66485c780e2f83d72433bd5d84a06bb6541c2af31dae871728bf856a174f93f4" |
| 24 | |
| 25 | fn ed_say t view uchar * to void |
| 26 | k = write 1 t 0 len t |
| 27 | end |
| 28 | fn ed_num n ulong to void |
| 29 | d = new uchar 24 0 |
| 30 | v = n |
| 31 | i = ulong 24 |
| 32 | while true bound 30 |
| 33 | i = sub i 1 |
| 34 | put d i uchar add 48 rem v 10 |
| 35 | v = div v 10 |
| 36 | if eq v 0 |
| 37 | break |
| 38 | end |
| 39 | end |
| 40 | k = write 1 d i sub 24 i |
| 41 | end |
| 42 | fn ed_hv c uchar to ulong |
| 43 | if and ge c 48 le c 57 |
| 44 | ret ulong sub c 48 |
| 45 | end |
| 46 | if and ge c 97 le c 102 |
| 47 | ret add 10 ulong sub c 97 |
| 48 | end |
| 49 | ret 0 |
| 50 | end |
| 51 | note hex field ed_f[s, e) into b; byte count |
| 52 | fn ed_hex s ulong e ulong b edit uchar 65536 to ulong |
| 53 | n = div sub e s 2 |
| 54 | for i ulong n |
| 55 | put b i uchar add mul ed_hv get ed_f add s mul i 2 16 ed_hv get ed_f add add s mul i 2 1 |
| 56 | end |
| 57 | ret n |
| 58 | end |
| 59 | fn ed_dec s ulong e ulong to ulong |
| 60 | v = ulong 0 |
| 61 | for i ulong sub e s |
| 62 | v = add mul v 10 ulong sub get ed_f add s i 48 |
| 63 | end |
| 64 | ret v |
| 65 | end |
| 66 | fn ed_count ok bool to void |
| 67 | if ok |
| 68 | put ed_v 0 add get ed_v 0 1 |
| 69 | else |
| 70 | put ed_v 1 add get ed_v 1 1 |
| 71 | end |
| 72 | end |
| 73 | note one line ed_f[s, e): fields into t (start, end pairs) |
| 74 | fn ed_line s ulong e ulong to void |
| 75 | t = new ulong 20 0 |
| 76 | k = ulong 0 |
| 77 | p = s |
| 78 | while lt p e bound 9223372036854775807 |
| 79 | while lt p e bound 9223372036854775807 |
| 80 | if ne get ed_f p 32 |
| 81 | break |
| 82 | end |
| 83 | p = add p 1 |
| 84 | end |
| 85 | if ge p e |
| 86 | break |
| 87 | end |
| 88 | q = p |
| 89 | while lt q e bound 9223372036854775807 |
| 90 | if eq get ed_f q 32 |
| 91 | break |
| 92 | end |
| 93 | q = add q 1 |
| 94 | end |
| 95 | if lt k 10 |
| 96 | put t mul k 2 p |
| 97 | put t add mul k 2 1 q |
| 98 | end |
| 99 | k = add k 1 |
| 100 | p = q |
| 101 | end |
| 102 | if ne k 9 |
| 103 | ret |
| 104 | end |
| 105 | ok = ed_case t |
| 106 | if not ok |
| 107 | ed_say ed_bad |
| 108 | z = write 1 ed_f get t 0 sub get t 1 get t 0 |
| 109 | ed_say ed_nl |
| 110 | end |
| 111 | ed_count ok |
| 112 | if gt get ed_v 12 0 |
| 113 | ed_runedges |
| 114 | end |
| 115 | end |
| 116 | note remember the first valid DER case of a curve for the edge cases (run after its own count) |
| 117 | fn ed_keep curve ulong alg ulong pub view uchar * sig view uchar * to void |
| 118 | for i ulong len pub |
| 119 | put ed_pb i get pub i |
| 120 | end |
| 121 | for i ulong len sig |
| 122 | put ed_sb i get sig i |
| 123 | end |
| 124 | put ed_v 10 len pub |
| 125 | put ed_v 11 len sig |
| 126 | put ed_v 12 curve |
| 127 | put ed_v 13 alg |
| 128 | end |
| 129 | fn ed_runedges to void |
| 130 | p = new uchar 128 0 |
| 131 | s = new uchar 128 0 |
| 132 | for i ulong 128 |
| 133 | put p i get ed_pb i |
| 134 | put s i get ed_sb i |
| 135 | end |
| 136 | c = get ed_v 12 |
| 137 | put ed_v 12 0 |
| 138 | ed_edges c part p 0 get ed_v 10 part s 0 get ed_v 11 get ed_v 13 |
| 139 | end |
| 140 | fn ed_case t view ulong 20 to bool |
| 141 | dg = new uchar 64 0 |
| 142 | curve = ulong 1 |
| 143 | if eq ed_dec get t 2 get t 3 384 |
| 144 | curve = 2 |
| 145 | end |
| 146 | hb = ed_dec get t 8 get t 9 |
| 147 | alg = ulong 0 |
| 148 | if eq hb 384 |
| 149 | alg = 1 |
| 150 | end |
| 151 | if eq hb 512 |
| 152 | alg = 2 |
| 153 | end |
| 154 | if not ed_digest t alg dg |
| 155 | ret false |
| 156 | end |
| 157 | for i ulong 64 |
| 158 | put ed_dg i get dg i |
| 159 | end |
| 160 | valid = eq get ed_f get t 6 48 |
| 161 | got = ed_check t curve alg |
| 162 | ret eq got valid |
| 163 | end |
| 164 | note load the key and verify (DER or raw by the ENC field) with the digest in ed_dg |
| 165 | fn ed_check t view ulong 20 curve ulong alg ulong to bool |
| 166 | pub = new uchar 65536 0 |
| 167 | sig = new uchar 65536 0 |
| 168 | dg = new uchar 64 0 |
| 169 | for i ulong 64 |
| 170 | put dg i get ed_dg i |
| 171 | end |
| 172 | raw = eq get ed_f get t 4 114 |
| 173 | pn = ed_hex get t 10 get t 11 pub |
| 174 | sn = ed_hex get t 12 get t 13 sig |
| 175 | if not ot_ec_key curve part pub 0 pn |
| 176 | ret false |
| 177 | end |
| 178 | if raw |
| 179 | ret ot_ec_verraw alg dg part sig 0 sn |
| 180 | end |
| 181 | got = ot_ec_verify alg dg part sig 0 sn |
| 182 | if and got eq get ed_v add 3 curve 0 |
| 183 | put ed_v add 3 curve 1 |
| 184 | ed_keep curve alg part pub 0 pn part sig 0 sn |
| 185 | end |
| 186 | ret got |
| 187 | end |
| 188 | note the hash path: ot_hash(MSG) must equal DIGEST; the digest for verification into dg |
| 189 | fn ed_digest t view ulong 20 alg ulong dg edit uchar 64 to bool |
| 190 | m = new uchar 65536 0 |
| 191 | d = new uchar 65536 0 |
| 192 | mn = ed_hex get t 14 get t 15 m |
| 193 | dn = ed_hex get t 16 get t 17 d |
| 194 | h = new uchar 64 0 |
| 195 | hl = ot_hash alg part m 0 mn h |
| 196 | if ne hl dn |
| 197 | ret false |
| 198 | end |
| 199 | for i ulong hl |
| 200 | if ne get h i get d i |
| 201 | ret false |
| 202 | end |
| 203 | put dg i get h i |
| 204 | end |
| 205 | ret true |
| 206 | end |
| 207 | |
| 208 | note ---- edge cases from one valid DER signature (digest in ed_dg) ---- |
| 209 | fn ed_c curve ulong to ulong |
| 210 | if eq curve 2 |
| 211 | ret 48 |
| 212 | end |
| 213 | ret 32 |
| 214 | end |
| 215 | fn ed_edges curve ulong pub view uchar * sig view uchar * alg ulong to void |
| 216 | c = ed_c curve |
| 217 | raw = new uchar 96 0 |
| 218 | rs = new ulong 4 0 |
| 219 | ok = ot_ec_derint sig 2 rs 0 |
| 220 | ok = ot_ec_derint sig get rs 1 rs 2 |
| 221 | ok = ot_ec_place sig get rs 0 sub get rs 1 get rs 0 raw c |
| 222 | ok = ot_ec_place sig get rs 2 sub get rs 3 get rs 2 raw add shl c 8 c |
| 223 | for k ulong 6 |
| 224 | ed_edge1 curve pub raw alg k |
| 225 | end |
| 226 | ed_edgekey curve pub |
| 227 | end |
| 228 | note kind 0 r = 0, 1 s = 0, 2 r = n, 3 s = n, 4 r = n + 1, 5 s = n - s (valid) |
| 229 | fn ed_edge1 curve ulong pub view uchar * raw view uchar 96 alg ulong kind ulong to void |
| 230 | c = ed_c curve |
| 231 | w = new ulong 1024 0 |
| 232 | ok = ot_ec_setup w curve |
| 233 | x = new uchar 96 0 |
| 234 | for i ulong mul c 2 |
| 235 | put x i get raw i |
| 236 | end |
| 237 | o = ulong 0 |
| 238 | if or eq kind 1 or eq kind 3 eq kind 5 |
| 239 | o = c |
| 240 | end |
| 241 | if lt kind 2 |
| 242 | for i ulong c |
| 243 | put x add o i 0 |
| 244 | end |
| 245 | end |
| 246 | if or eq kind 2 eq kind 3 |
| 247 | ot_ec_export w OT_EC_N part x o c |
| 248 | end |
| 249 | if eq kind 4 |
| 250 | ot_ec_add w OT_EC_S1 OT_EC_N OT_EC_ONE OT_EC_P |
| 251 | ot_ec_export w OT_EC_S1 part x o c |
| 252 | end |
| 253 | if eq kind 5 |
| 254 | ok = ot_ec_import w OT_EC_S2 part x c c |
| 255 | ot_ec_sub w OT_EC_S2 OT_EC_N OT_EC_S2 OT_EC_N |
| 256 | ot_ec_export w OT_EC_S2 part x c c |
| 257 | end |
| 258 | ed_edgecheck curve pub x alg kind |
| 259 | end |
| 260 | fn ed_edgecheck curve ulong pub view uchar * x view uchar 96 alg ulong kind ulong to void |
| 261 | c = ed_c curve |
| 262 | dg = new uchar 64 0 |
| 263 | for i ulong 64 |
| 264 | put dg i get ed_dg i |
| 265 | end |
| 266 | ok = ot_ec_key curve pub |
| 267 | got = ot_ec_verraw alg dg part x 0 mul c 2 |
| 268 | want = eq kind 5 |
| 269 | if ne got want |
| 270 | ed_say ed_edge |
| 271 | ed_num kind |
| 272 | ed_say ed_nl |
| 273 | end |
| 274 | ed_count eq got want |
| 275 | end |
| 276 | fn ed_edgekey curve ulong pub view uchar * to void |
| 277 | k = new uchar 97 0 |
| 278 | n = len pub |
| 279 | for i ulong n |
| 280 | put k i get pub i |
| 281 | end |
| 282 | put k sub n 1 xor get k sub n 1 1 |
| 283 | a = ot_ec_key curve part k 0 n |
| 284 | w = new ulong 1024 0 |
| 285 | ok = ot_ec_setup w curve |
| 286 | c = div sub n 1 2 |
| 287 | ot_ec_export w OT_EC_P part k 1 c |
| 288 | b = ot_ec_key curve part k 0 n |
| 289 | if or a b |
| 290 | ed_say ed_edge |
| 291 | ed_num 6 |
| 292 | ed_say ed_nl |
| 293 | end |
| 294 | ed_count not or a b |
| 295 | note an empty and a one-byte public key: a defined refusal, never a trap (review finding OMTLS-EC-EMPTY-POINT-TRAP) |
| 296 | e0 = ot_ec_key curve part k 0 0 |
| 297 | e1 = ot_ec_key curve part k 0 1 |
| 298 | ed_count not or e0 e1 |
| 299 | if eq curve 1 |
| 300 | ed_xrange |
| 301 | end |
| 302 | end |
| 303 | note P-256 only: (0, y0) is a valid public point (accepted) and (p, y0) must be refused by the coordinate range check |
| 304 | fn ed_xrange to void |
| 305 | k = new uchar 97 0 |
| 306 | put k 0 4 |
| 307 | for i ulong 32 |
| 308 | put k add 33 i uchar add mul ed_hv get ed_y0 mul i 2 16 ed_hv get ed_y0 add mul i 2 1 |
| 309 | end |
| 310 | a = ot_ec_key 1 part k 0 65 |
| 311 | w = new ulong 1024 0 |
| 312 | ok = ot_ec_setup w 1 |
| 313 | ot_ec_export w OT_EC_P part k 1 32 |
| 314 | b = ot_ec_key 1 part k 0 65 |
| 315 | good = and a not b |
| 316 | if not good |
| 317 | ed_say ed_edge |
| 318 | ed_num 7 |
| 319 | ed_say ed_nl |
| 320 | end |
| 321 | ed_count good |
| 322 | end |
| 323 | |
| 324 | fn ed_file i ulong to bool |
| 325 | pb = new uchar 4096 0 |
| 326 | k = argread i pb 0 4096 |
| 327 | fd = fopen pb 0 ulong k 0 0 |
| 328 | if lt fd 0 |
| 329 | ret false |
| 330 | end |
| 331 | n = ulong 0 |
| 332 | while lt n len ed_f bound 100000 |
| 333 | r = read int fd ed_f n sub len ed_f n |
| 334 | if le r 0 |
| 335 | break |
| 336 | end |
| 337 | n = add n ulong r |
| 338 | end |
| 339 | z = fclose int fd |
| 340 | p = ulong 0 |
| 341 | while lt p n bound 9223372036854775807 |
| 342 | q = p |
| 343 | while lt q n bound 9223372036854775807 |
| 344 | if eq get ed_f q 10 |
| 345 | break |
| 346 | end |
| 347 | q = add q 1 |
| 348 | end |
| 349 | if gt q p |
| 350 | if ne get ed_f p 35 |
| 351 | ed_line p q |
| 352 | end |
| 353 | end |
| 354 | p = add q 1 |
| 355 | end |
| 356 | ret true |
| 357 | end |
| 358 | fn main to int |
| 359 | for i ulong sub ulong argcnt 1 |
| 360 | if not ed_file add i 1 |
| 361 | ret 2 |
| 362 | end |
| 363 | end |
| 364 | f = get ed_v 1 |
| 365 | p = get ed_v 0 |
| 366 | if eq f 0 |
| 367 | ed_say ed_pass |
| 368 | ed_num p |
| 369 | ed_say ed_nl |
| 370 | ret 0 |
| 371 | end |
| 372 | ed_say ed_failt |
| 373 | ed_num p |
| 374 | ed_say ed_ff |
| 375 | ed_num f |
| 376 | ed_say ed_nl |
| 377 | ret 1 |
| 378 | end |