interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / tls / test / rsa / driver.om

329 lines · 7370 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: test driver for om/tls/rsa.om (program mode). Built as: om2 line, sha2.om, rsa.om,
3note this file. Reads a vector file on stdin, one case per line (see gen_vectors.py):
4note EXP MODE CTL KEY MSG DIG SIG ID
5note EXP 0..5 exact return code or n (any nonzero); MODE p (pkcs1, CTL = hash) or s (pss, CTL = ctl);
6note KEY / MSG / DIG / SIG hex ('-' empty; MSG '*' = use DIG verbatim, no hashing).
7note For every MSG the driver computes sha2_digest with the CTL hash and requires it to equal DIG
8note (the converter's hashlib digest); hs = that digest || SIG goes to the verify call.
9note Negative control: every case expected 0 that verifies is run again with one signature byte
10note flipped (bit 0 of the middle byte) and must be refused.
11note Optional argument: repeat count R (each verify runs R times; for timing).
12note Output: one MISMATCH line per wrong result, then
13note cases C verifies V mismatches X controls K ns T
14note Exit 0 only when at least one verify ran and nothing mismatched; 1 otherwise; 65 bad input.
15
16text rvtmis "MISMATCH line "
17text rvtgot " got "
18text rvtexp " want "
19text rvtdig " digest"
20text rvtctl " control"
21text rvtvfy " verify"
22text rvtcase "cases "
23text rvtver " verifies "
24text rvtmm " mismatches "
25text rvtcon " controls "
26text rvtns " ns "
27text rvtnl "\n"
28text rvtbad "driver: bad input\n"
29
30global rvin uchar 16777216
31global rvout uchar 32
32global rvarg uchar 32
33global rvcnt ulong 16
34global rvf ulong 16
35
36fn rv_bad to void
37r = write 2 rvtbad 0 len rvtbad
38exit 65
39end
40
41fn rv_num v ulong to void
42k = ulong 0
43x = v
44while true bound 30
45put rvout sub 31 k uchar add rem x 10 48
46k = add k 1
47x = div x 10
48if eq x 0
49break
50end
51end
52r = write 1 rvout sub 32 k k
53end
54
55fn rv_nib c ulong to ulong
56if and ge c 48 le c 57
57ret sub c 48
58end
59if and ge c 97 le c 102
60ret sub c 87
61end
62rv_bad
63ret 0
64end
65
66note end of the token starting at p (the next space or newline)
67fn rv_tok p ulong to ulong
68e = p
69while and ne get rvin e 32 ne get rvin e 10 bound 999999
70e = add e 1
71end
72ret e
73end
74
75note hex rvin[p..q) into d; '-' is empty; returns the byte count
76fn rv_hex d edit uchar * p ulong q ulong to ulong
77if and eq sub q p 1 eq get rvin p 45
78ret 0
79end
80h = sub q p
81if ne and h 1 0
82rv_bad
83end
84c = div h 2
85if gt c len d
86rv_bad
87end
88for i ulong c
89o = add p mul i 2
90hi = rv_nib ulong get rvin o
91lo = rv_nib ulong get rvin add o 1
92put d i uchar or shl hi 4 lo
93end
94ret c
95end
96
97fn rv_dec p ulong q ulong to ulong
98v = ulong 0
99for i ulong sub q p
100c = ulong get rvin add p i
101if or lt c 48 gt c 57
102rv_bad
103end
104v = add mul v 10 sub c 48
105end
106ret v
107end
108
109fn rv_slurp to void
110t = ulong 0
111while true bound 999999
112room = sub len rvin t
113if eq room 0
114rv_bad
115end
116r = read 0 rvin t room
117if lt r 0
118rv_bad
119end
120if eq r 0
121break
122end
123t = add t ulong r
124end
125put rvcnt 6 t
126end
127
128fn rv_reps to ulong
129if lt argcnt 2
130ret 1
131end
132l = argread 1 rvarg 0 20
133v = ulong 0
134for i ulong ulong l
135v = add mul v 10 sub ulong get rvarg i 48
136end
137ret v
138end
139
140note counters in rvcnt: 0 line, 1 cases, 2 verifies, 3 mismatches, 4 ns, 5 repeats, 6 input length,
141note 7 controls
142fn rv_bump i ulong v ulong to void
143put rvcnt i add get rvcnt i v
144end
145
146fn rv_mis tag view uchar * got ulong want ulong to void
147rv_bump 3 1
148r = write 1 rvtmis 0 len rvtmis
149rv_num get rvcnt 0
150r = write 1 tag 0 len tag
151r = write 1 rvtgot 0 len rvtgot
152rv_num got
153r = write 1 rvtexp 0 len rvtexp
154rv_num want
155r = write 1 rvtnl 0 1
156end
157
158note one verify call: mode (rvcnt cell 8) 112 'p' or 115 's'
159fn rv_call c ulong key view uchar * hs view uchar * hl ulong to ulong
160if eq get rvcnt 8 112
161ret rsa_verify_pkcs1 c key hs hl
162end
163ret rsa_verify_pss c key hs hl
164end
165
166note run the verify R times; returns the last result
167fn rv_run c ulong key view uchar * hs view uchar * hl ulong to ulong
168got = ulong 0
169for k ulong get rvcnt 5
170t0 = monons
171got = rv_call c key hs hl
172rv_bump 4 ulong sub monons t0
173rv_bump 2 1
174end
175ret got
176end
177
178note judge result got against expectation x (99 = any nonzero); 0 ok, 1 mismatch
179fn rv_judge got ulong x ulong to ulong
180if eq x 99
181if eq got 0
182ret 1
183end
184ret 0
185end
186if ne got x
187ret 1
188end
189ret 0
190end
191
192note digest of msg with alg into dc; must equal the converter digest dg (length dl); returns the
193note digest length to use (dl when alg is not a sha2 hash: dg is used as given)
194fn rv_digest alg ulong m view uchar * dg edit uchar 128 dl ulong to ulong
195dc = new uchar 64 0
196n = sha2_digest alg m dc
197if eq n 0
198ret dl
199end
200d = ulong 0
201if ne n dl
202d = 1
203end
204if eq n dl
205for i ulong n
206d = or d ulong xor get dc i get dg i
207end
208end
209if ne d 0
210rv_mis rvtdig n dl
211end
212ret dl
213end
214
215note token bounds of the 8 fields of the line at p into rvf (start, end pairs)
216fn rv_split p ulong to void
217for t ulong 8
218q = rv_tok p
219put rvf mul t 2 p
220put rvf add mul t 2 1 q
221p = add q 1
222end
223end
224
225note MSG field (unless '*'): hash it, cross-check DIG; returns the digest length for hs
226fn rv_msgdig msg edit uchar 4096 dg edit uchar 128 dl ulong to ulong
227p = get rvf 8
228q = get rvf 9
229if and eq sub q p 1 eq get rvin p 42
230ret dl
231end
232ml = rv_hex part msg 0 4096 p q
233alg = get rvcnt 10
234if eq get rvcnt 8 115
235alg = and alg 0xffff
236end
237ret rv_digest alg part msg 0 ml dg dl
238end
239
240note parse the split line: rvcnt 8 mode, 9 expectation, 10 ctl, 11 key length, 12 digest
241note length, 13 signature length; hs = digest || signature
242fn rv_parse key edit uchar 2048 msg edit uchar 4096 dg edit uchar 128 hs edit uchar 2200 to void
243x = ulong 99
244if ne get rvin get rvf 0 110
245x = rv_dec get rvf 0 get rvf 1
246end
247put rvcnt 9 x
248put rvcnt 8 ulong get rvin get rvf 2
249put rvcnt 10 rv_dec get rvf 4 get rvf 5
250put rvcnt 11 rv_hex part key 0 2048 get rvf 6 get rvf 7
251dl = rv_hex part dg 0 128 get rvf 10 get rvf 11
252dl = rv_msgdig msg dg dl
253for i ulong dl
254put hs i get dg i
255end
256put rvcnt 12 dl
257put rvcnt 13 rv_hex part hs dl sub 2200 dl get rvf 12 get rvf 13
258end
259
260note flipped-byte control: bit 0 of the middle signature byte flipped must be refused
261fn rv_ctrl key view uchar 2048 hs edit uchar 2200 to void
262dl = get rvcnt 12
263n = add dl get rvcnt 13
264fx = add dl div get rvcnt 13 2
265put hs fx xor get hs fx 1
266rv_bump 7 1
267cg = rv_call get rvcnt 10 part key 0 get rvcnt 11 part hs 0 n dl
268if eq cg 0
269rv_mis rvtctl cg 99
270end
271end
272
273note run the parsed case and judge it; cases expected valid that verify get the control
274fn rv_check key view uchar 2048 hs edit uchar 2200 to void
275dl = get rvcnt 12
276n = add dl get rvcnt 13
277x = get rvcnt 9
278got = rv_run get rvcnt 10 part key 0 get rvcnt 11 part hs 0 n dl
279if ne rv_judge got x 0
280rv_mis rvtvfy got x
281end
282if and eq x 0 and eq got 0 gt get rvcnt 13 0
283rv_ctrl key hs
284end
285end
286
287fn rv_line key edit uchar 2048 msg edit uchar 4096 dg edit uchar 128 hs edit uchar 2200 p ulong to void
288rv_bump 1 1
289rv_split p
290rv_parse key msg dg hs
291rv_check key hs
292end
293
294fn main to int
295key = new uchar 2048 0
296msg = new uchar 4096 0
297dg = new uchar 128 0
298hs = new uchar 2200 0
299rv_slurp
300put rvcnt 5 rv_reps
301tot = get rvcnt 6
302p = ulong 0
303while lt p tot bound 999999
304rv_bump 0 1
305e = p
306while ne get rvin e 10 bound 999999
307e = add e 1
308end
309if ne get rvin p 35
310rv_line key msg dg hs p
311end
312p = add e 1
313end
314r = write 1 rvtcase 0 len rvtcase
315rv_num get rvcnt 1
316r = write 1 rvtver 0 len rvtver
317rv_num get rvcnt 2
318r = write 1 rvtmm 0 len rvtmm
319rv_num get rvcnt 3
320r = write 1 rvtcon 0 len rvtcon
321rv_num get rvcnt 7
322r = write 1 rvtns 0 len rvtns
323rv_num get rvcnt 4
324r = write 1 rvtnl 0 1
325if or gt get rvcnt 3 0 eq get rvcnt 2 0
326ret 1
327end
328ret 0
329end