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 / chacha / driver.om

527 lines · 8525 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: program-mode test driver for om/tls/chacha.om, built as `om2`, chacha.om, driver.om.
3note Reads vector records from stdin (at most 16 MiB), one per line, fields separated by one space;
4note hex fields are lowercase or uppercase hex, "-" for empty:
5note Q IA IB IC ID STATE WANT chacha20_qr on 16 words (each 8 hex digits, big-endian as RFC prints)
6note C KN CTR IN OUT CODE chacha20_xor KN CTR on a copy of IN; CODE 0: result == OUT;
7note CODE 2: refused with 2 and the buffer untouched
8note P KEY MSG TAG CODE poly1305_mac; CODE 0: tag == TAG; CODE 2: refused with 2
9note A KN AAD PT CTTAG CODE CODE 0: seal(PT) == CTTAG and open(CTTAG) == 0 with PT;
10note CODE 1: open(CTTAG) == 1 and the ciphertext part zeroed;
11note CODE 2: seal and open both refuse with 2 and leave the buffer alone
12note Lines that are empty or start with # are skipped. Prints "MISMATCH line L" (wrong result) or
13note "BAD line L" (malformed record) per failure, then "chacha driver: C checks, F failed".
14note Exit 0 only when F is 0 and C is above 0; exit 1 otherwise; 2 when stdin cannot be read.
15
16global inp uchar 16777216
17global st ulong 12
18global ob uchar 32
19global one uchar 1
20text tfail "MISMATCH line "
21text tbad "BAD line "
22text tsum "chacha driver: "
23text tchk " checks, "
24text tfld " failed\n"
25text tnl "\n"
26text tio "chacha driver: cannot read input or input over 16 MiB\n"
27
28note st: 0 input length, 1 cursor, 2 line number, 3 malformed flag, 4 line start
29
30fn dslurp to int
31t = ulong 0
32while true bound 999999
33room = sub len inp t
34if eq room 0
35r = read 0 one 0 1
36if ne r 0
37ret 2
38end
39break
40end
41r = read 0 inp t room
42if lt r 0
43ret 2
44end
45if eq r 0
46break
47end
48t = add t ulong r
49end
50put st 0 t
51ret 0
52end
53
54fn dpeek to ulong
55c = get st 1
56if ge c get st 0
57ret 256
58end
59ret ulong get inp c
60end
61
62fn dadv to void
63put st 1 add get st 1 1
64end
65
66fn dbad to void
67put st 3 1
68end
69
70fn dhexv c ulong to ulong
71if ge c 48
72if le c 57
73ret sub c 48
74end
75end
76if ge c 97
77if le c 102
78ret sub c 87
79end
80end
81if ge c 65
82if le c 70
83ret sub c 55
84end
85end
86ret 16
87end
88
89fn dbyte to uchar
90hi = dhexv dpeek
91dadv
92lo = dhexv dpeek
93dadv
94if gt hi 15
95dbad
96ret 0
97end
98if gt lo 15
99dbad
100ret 0
101end
102ret uchar or shl hi 4 lo
103end
104
105fn dspace to void
106if ne dpeek 32
107dbad
108ret
109end
110dadv
111end
112
113note decimal number at the cursor (1 to 19 digits)
114fn dnum to ulong
115v = ulong 0
116k = ulong 0
117while true bound 21
118c = dpeek
119if lt c 48
120break
121end
122if gt c 57
123break
124end
125v = add mul v 10 sub c 48
126k = add k 1
127dadv
128end
129if eq k 0
130dbad
131end
132if gt k 19
133dbad
134end
135ret v
136end
137
138note hex field into a; returns the byte count ("-" is 0 bytes)
139fn dhexf a edit uchar * to ulong
140if eq dpeek 45
141dadv
142ret 0
143end
144n = ulong 0
145while true bound 999999
146c = dpeek
147if eq c 32
148break
149end
150if eq c 10
151break
152end
153if eq c 13
154break
155end
156if eq c 256
157break
158end
159if ge n len a
160dbad
161ret n
162end
163put a n dbyte
164n = add n 1
165if eq get st 3 1
166ret n
167end
168end
169ret n
170end
171
172fn deor to void
173if eq dpeek 13
174dadv
175end
176c = dpeek
177if eq c 10
178dadv
179ret
180end
181if ne c 256
182dbad
183end
184end
185
186fn dskip to void
187while true bound 999999
188c = dpeek
189if eq c 256
190ret
191end
192dadv
193if eq c 10
194ret
195end
196end
197end
198
199note 0 when a and b have the same length and bytes
200fn dsame a view uchar * b view uchar * to ulong
201if ne len a len b
202ret 1
203end
204d = ulong 0
205for i ulong len a
206if ne get a i get b i
207d = 1
208end
209end
210ret d
211end
212
213note 0 when every byte of a is 0
214fn dzero a view uchar * to ulong
215d = ulong 0
216for i ulong len a
217if ne get a i 0
218d = 1
219end
220end
221ret d
222end
223
224fn dcopy d edit uchar * s view uchar * to void
225for i ulong len s
226put d i get s i
227end
228end
229
230fn dprn v ulong to void
231x = v
232k = ulong 0
233while true bound 21
234put ob sub 31 k uchar add 48 rem x 10
235k = add k 1
236x = div x 10
237if eq x 0
238break
239end
240end
241r = write 1 ob sub 32 k k
242end
243
244fn dmsg bad ulong ln ulong to void
245if eq bad 1
246r = write 1 tbad 0 len tbad
247else
248r = write 1 tfail 0 len tfail
249end
250dprn ln
251r = write 1 tnl 0 1
252end
253
254note 16 big-endian words from 64 bytes
255fn dwords w edit uint 16 b view uchar * to void
256for i ulong 16
257q = mul i 4
258x = uint get b q
259x = or shl x 8 uint get b add q 1
260x = or shl x 8 uint get b add q 2
261put w i or shl x 8 uint get b add q 3
262end
263end
264
265note the library quarter round, called (not inlined) so drecq keeps its names in registers
266fn dqr x edit uint 16 q ulong to void
267chacha20_qr x q
268end
269
270note Q record; 0 pass, 1 mismatch, 2 malformed
271fn drecq b edit uchar 65552 e edit uchar 65552 to ulong
272x = new uint 16 0
273y = new uint 16 0
274dspace
275ia = dnum
276dspace
277ib = dnum
278dspace
279ic = dnum
280dspace
281id = dnum
282dspace
283nb = dhexf b
284dspace
285nx = dhexf e
286deor
287if eq get st 3 1
288ret 2
289end
290if ne nb 64
291ret 2
292end
293if ne nx 64
294ret 2
295end
296if gt or or or ia ib ic id 15
297ret 2
298end
299dwords x b
300dwords y e
301dqr x or or or ia shl ib 8 shl ic 16 shl id 24
302d = ulong 0
303for i ulong 16
304if ne get x i get y i
305d = 1
306end
307end
308ret d
309end
310
311note C record
312fn drecc kn edit uchar 64 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong
313dspace
314put st 5 dhexf kn
315dspace
316ctr = dnum
317dspace
318nb = dhexf b
319dspace
320put st 8 dhexf e
321dspace
322put st 9 dnum
323deor
324if eq get st 3 1
325ret 2
326end
327dcopy w part b 0 nb
328r = chacha20_xor part kn 0 get st 5 ctr part w 0 nb
329if ne r get st 9
330ret 1
331end
332if eq r 0
333ret dsame part w 0 nb part e 0 get st 8
334end
335ret dsame part w 0 nb part b 0 nb
336end
337
338note poly1305_mac on the parsed key (st 5 bytes) and message (st 7 bytes) into w[0..16)
339fn dpmac kn edit uchar 64 b edit uchar 65552 w edit uchar 65552 to ulong
340nk = get st 5
341nb = get st 7
342ret poly1305_mac part kn 0 nk part b 0 nb part w 0 16
343end
344
345note P record
346fn drecp kn edit uchar 64 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong
347dspace
348put st 5 dhexf kn
349dspace
350put st 7 dhexf b
351dspace
352put st 8 dhexf e
353dspace
354put st 9 dnum
355deor
356if eq get st 3 1
357ret 2
358end
359r = dpmac kn b w
360if ne r get st 9
361ret 1
362end
363if eq r 0
364ret dsame part w 0 16 part e 0 get st 8
365end
366ret 0
367end
368
369note aead_chacha_seal / open on the parsed key (st 5 bytes), aad (st 6 bytes) and w[0..n)
370fn dseal kn edit uchar 64 a edit uchar 65552 w edit uchar 65552 n ulong to ulong
371nk = get st 5
372na = get st 6
373ret aead_chacha_seal part kn 0 nk part a 0 na part w 0 n
374end
375
376fn dopen kn edit uchar 64 a edit uchar 65552 w edit uchar 65552 n ulong to ulong
377nk = get st 5
378na = get st 6
379ret aead_chacha_open part kn 0 nk part a 0 na part w 0 n
380end
381
382note A record, CODE 0; lengths in st 5 key, 6 aad, 7 plaintext, 8 ciphertext with tag
383fn drecav kn edit uchar 64 a edit uchar 65552 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong
384np = get st 7
385nc = get st 8
386if ne nc add np 16
387ret 1
388end
389dcopy w part b 0 np
390r = dseal kn a w nc
391if ne r 0
392ret 1
393end
394if ne dsame part w 0 nc part e 0 nc 0
395ret 1
396end
397dcopy w part e 0 nc
398r = dopen kn a w nc
399if ne r 0
400ret 1
401end
402ret dsame part w 0 np part b 0 np
403end
404
405note A record, CODE 1 or 2 (st 9)
406fn drecai kn edit uchar 64 a edit uchar 65552 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong
407code = get st 9
408nc = get st 8
409dcopy w part e 0 nc
410r = dopen kn a w nc
411if ne r code
412ret 1
413end
414if eq code 1
415ret dzero part w 0 sub nc 16
416end
417if ne dsame part w 0 nc part e 0 nc 0
418ret 1
419end
420np = get st 7
421dcopy w part b 0 np
422r = dseal kn a w add np 16
423if ne r 2
424ret 1
425end
426ret dsame part w 0 np part b 0 np
427end
428
429note A record
430fn dreca kn edit uchar 64 a edit uchar 65552 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong
431dspace
432put st 5 dhexf kn
433dspace
434put st 6 dhexf a
435dspace
436put st 7 dhexf b
437dspace
438put st 8 dhexf e
439dspace
440put st 9 dnum
441deor
442if eq get st 3 1
443ret 2
444end
445if gt get st 9 2
446ret 2
447end
448if gt get st 7 65536
449ret 2
450end
451if eq get st 9 0
452ret drecav kn a b e w
453end
454if and eq get st 9 1 lt get st 8 16
455ret 2
456end
457ret drecai kn a b e w
458end
459
460fn main to int
461kn = new uchar 64 0
462a = new uchar 65552 0
463b = new uchar 65552 0
464e = new uchar 65552 0
465w = new uchar 65552 0
466if ne dslurp 0
467r = write 2 tio 0 len tio
468ret 2
469end
470put st 1 0
471put st 2 0
472chk = ulong 0
473fl = ulong 0
474res = ulong 0
475while lt get st 1 get st 0 bound 999999
476put st 2 add get st 2 1
477ln = get st 2
478put st 3 0
479put st 4 get st 1
480c = dpeek
481if eq c 35
482dskip
483continue
484end
485if eq c 10
486dadv
487continue
488end
489dadv
490res = 2
491if eq c 81
492res = drecq b e
493end
494if eq c 67
495res = drecc kn b e w
496end
497if eq c 80
498res = drecp kn b e w
499end
500if eq c 65
501res = dreca kn a b e w
502end
503chk = add chk 1
504if ne res 0
505fl = add fl 1
506if eq res 2
507dmsg 1 ln
508put st 1 get st 4
509dskip
510else
511dmsg 0 ln
512end
513end
514end
515r = write 1 tsum 0 len tsum
516dprn chk
517r = write 1 tchk 0 len tchk
518dprn fl
519r = write 1 tfld 0 len tfld
520if ne fl 0
521ret 1
522end
523if eq chk 0
524ret 1
525end
526ret 0
527end