interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omtls / tests / drv / ecdsa.om

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