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

807 lines · 14985 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: program-mode test driver for om/tls/aesgcm.om (concatenated after `om2` and the
3note library). Reads records from stdin (at most 16 MiB), one per line, fields separated by one space,
4note hex fields in lowercase or uppercase, "-" for an empty field:
5note B KEY PT CT FIPS 197 single block: AES-KEY(PT) = CT (16-byte blocks)
6note E KEY IV AAD PT CT TAG CAVP encrypt vector: seal(PT) = CT || TAG, open(CT || TAG)
7note = PT, and open with the last tag byte flipped returns 1 and
8note zeroes the data area (tamper control)
9note D KEY IV AAD CT TAG PT CAVP decrypt vector: open(CT || TAG) = PT; PT is FAIL when
10note open must return 1 and zero the data area
11note W TCID V|I KEY IV AAD MSG CT TAG Wycheproof: V as E; I: open returns 1 and zeroes the data
12note area, or, for an empty IV, seal and open both return 2
13note X CTL KIALEN BUFLEN WANT seal and open with ctl on zero arrays both return WANT
14note Y KNLEN AADLEN BUFLEN WANT aead_aes128gcm_seal/open on zero arrays both return WANT
15note T N seal then open N zero bytes (N <= 1048576, key/IV fixed);
16note prints "aesgcm speed bytes=N seal_ns=S open_ns=O seal_MBps=A
17note open_MBps=B" (MB = 10^6 bytes)
18note Every E, D and W vector whose shape is AES-128, 12-byte IV, 16-byte tag also runs through
19note aead_aes128gcm_seal/open (same expectations, tamper control included).
20note Lines that are empty or start with # are skipped; a trailing carriage return is allowed.
21note Prints "MISMATCH line L" (wrong result) or "BAD line L" (malformed record) per failure, then
22note "aesgcm driver: C checks, F failed". Exit 0 only when F is 0 and C is above 0; exit 1 otherwise;
23note exit 2 when the input cannot be read or exceeds 16 MiB.
24
25global inp uchar 16777216
26global st ulong 20
27global ob uchar 32
28global one uchar 1
29text tfail "MISMATCH line "
30text tbad "BAD line "
31text tsum "aesgcm driver: "
32text tchk " checks, "
33text tfld " failed\n"
34text tnl "\n"
35text tio "aesgcm driver: cannot read input or input over 16 MiB\n"
36text tspd "aesgcm speed bytes="
37text tsns " seal_ns="
38text tons " open_ns="
39text tsmb " seal_MBps="
40text tomb " open_MBps="
41
42note st: 0 input length, 1 cursor, 2 line number, 3 malformed flag, 4 line start,
43note 5 key bytes, 6 iv bytes, 7 aad bytes, 8 plaintext bytes, 9 ciphertext bytes, 10 tag bytes,
44note 11 expected-failure flag (D FAIL, W I), 12 checks, 13 failures, 14 key||nonce bytes for dacall
45
46fn dslurp to int
47t = ulong 0
48while true bound 999999
49room = sub len inp t
50if eq room 0
51r = read 0 one 0 1
52if ne r 0
53ret 2
54end
55break
56end
57r = read 0 inp t room
58if lt r 0
59ret 2
60end
61if eq r 0
62break
63end
64t = add t ulong r
65end
66put st 0 t
67ret 0
68end
69
70fn dpeek to ulong
71c = get st 1
72if ge c get st 0
73ret 256
74end
75ret ulong get inp c
76end
77
78fn dadv to void
79put st 1 add get st 1 1
80end
81
82fn dbad to void
83put st 3 1
84end
85
86fn dhexv c ulong to ulong
87if ge c 48
88if le c 57
89ret sub c 48
90end
91end
92if ge c 97
93if le c 102
94ret sub c 87
95end
96end
97if ge c 65
98if le c 70
99ret sub c 55
100end
101end
102ret 16
103end
104
105fn dspace to void
106if ne dpeek 32
107dbad
108ret
109end
110dadv
111end
112
113note a decimal number at the cursor (1 to 9 digits)
114fn dnum to ulong
115v = ulong 0
116k = ulong 0
117while true bound 10
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 9
133dbad
134end
135ret v
136end
137
138note a hex field at the cursor into a[o ..); the byte count ("-" is 0 bytes)
139fn dhexf a edit uchar 65536 o ulong to ulong
140if eq dpeek 45
141dadv
142ret 0
143end
144n = ulong 0
145while true bound 65537
146hi = dhexv dpeek
147if gt hi 15
148break
149end
150dadv
151lo = dhexv dpeek
152if gt lo 15
153dbad
154break
155end
156dadv
157if ge add o n 65536
158dbad
159break
160end
161put a add o n uchar or shl hi 4 lo
162n = add n 1
163end
164if eq n 0
165dbad
166end
167ret n
168end
169
170fn deor to void
171if eq dpeek 13
172dadv
173end
174c = dpeek
175if eq c 10
176dadv
177ret
178end
179if ne c 256
180dbad
181end
182end
183
184fn dskip to void
185while true bound 999999
186c = dpeek
187if eq c 256
188ret
189end
190dadv
191if eq c 10
192ret
193end
194end
195end
196
197note 0 when a[0..n) = b[0..n)
198fn dcmp a view uchar 65536 b view uchar 65536 n ulong to ulong
199d = ulong 0
200for i ulong n
201if ne get a i get b i
202d = 1
203end
204end
205ret d
206end
207
208note 0 when a[0..n) is all zero
209fn dzero a view uchar 65536 n ulong to ulong
210d = ulong 0
211for i ulong n
212if ne get a i 0
213d = 1
214end
215end
216ret d
217end
218
219fn dcopy a edit uchar 65536 b view uchar 65536 n ulong to void
220for i ulong n
221put a i get b i
222end
223end
224
225fn dprn v ulong to void
226x = v
227k = ulong 0
228while true bound 21
229put ob sub 31 k uchar add 48 rem x 10
230k = add k 1
231x = div x 10
232if eq x 0
233break
234end
235end
236r = write 1 ob sub 32 k k
237end
238
239fn dmsg bad ulong ln ulong to void
240if eq bad 1
241r = write 1 tbad 0 len tbad
242else
243r = write 1 tfail 0 len tfail
244end
245dprn ln
246r = write 1 tnl 0 1
247end
248
249note ctl of the current record: klen + 256 tlen + 65536 ivlen
250fn dctl to ulong
251ret or or get st 5 shl get st 10 8 shl get st 6 16
252end
253
254note key || iv || aad length
255fn dka to ulong
256ret add add get st 5 get st 6 get st 7
257end
258
259note data || tag length
260fn dbn to ulong
261ret add get st 8 get st 10
262end
263
264note 1 when the record has the TLS shape (AES-128, 12-byte IV, 16-byte tag)
265fn dtls to ulong
266if ne get st 5 16
267ret 0
268end
269if ne get st 6 12
270ret 0
271end
272if ne get st 10 16
273ret 0
274end
275ret 1
276end
277
278note seal pt into buf; 0 when it returns 0 and buf = ex (data || tag)
279fn dseal kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 to ulong
280n = dbn
281dcopy buf pt get st 8
282c = dctl
283a = dka
284r = aes_gcm_seal c part kia 0 a part buf 0 n
285if ne r 0
286ret 1
287end
288ret dcmp buf ex n
289end
290
291note open buf (data || tag); 0 when it returns 0 and buf[0..pl) = pt
292fn dopen kia view uchar 65536 pt view uchar 65536 buf edit uchar 65536 to ulong
293c = dctl
294a = dka
295n = dbn
296r = aes_gcm_open c part kia 0 a part buf 0 n
297if ne r 0
298ret 1
299end
300ret dcmp buf pt get st 8
301end
302
303note open buf; 0 when it returns want and, for want 1, the data area is zero
304fn dfail kia view uchar 65536 buf edit uchar 65536 want ulong to ulong
305c = dctl
306a = dka
307n = dbn
308r = aes_gcm_open c part kia 0 a part buf 0 n
309if ne r want
310ret 1
311end
312if eq want 1
313ret dzero buf get st 8
314end
315ret 0
316end
317
318note tamper control: ex with its last byte flipped must be refused (1, data area zero)
319fn dtamp kia view uchar 65536 ex view uchar 65536 buf edit uchar 65536 to ulong
320n = dbn
321dcopy buf ex n
322put buf sub n 1 xor get buf sub n 1 1
323ret dfail kia buf 1
324end
325
326note aad of the record (kia after key and iv) into aa
327fn daad kia view uchar 65536 aa edit uchar 65536 to void
328o = add get st 5 get st 6
329for i ulong get st 7
330put aa i get kia add o i
331end
332end
333
334note aead_aes128gcm_open (o 1) or _seal (o 0) on buf[0 .. dbn) with kn = kia[0 .. st 14) and aad
335note aa[0 .. st 7)
336fn dacall kia view uchar 65536 aa view uchar 65536 buf edit uchar 65536 o ulong to ulong
337kn = get st 14
338al = get st 7
339bl = dbn
340if eq o 0
341ret aead_aes128gcm_seal part kia 0 kn part aa 0 al part buf 0 bl
342end
343ret aead_aes128gcm_open part kia 0 kn part aa 0 al part buf 0 bl
344end
345
346note aead_aes128gcm_open on buf; 0 when it returns want and, for 0, buf = pt, for 1, zero data
347fn daopen kia view uchar 65536 pt view uchar 65536 buf edit uchar 65536 aa view uchar 65536 want ulong to ulong
348r = dacall kia aa buf 1
349if ne r want
350ret 1
351end
352if eq want 0
353ret dcmp buf pt get st 8
354end
355ret dzero buf get st 8
356end
357
358note the TLS-shaped encrypt checks through aead_aes128gcm_*: seal, open, tamper
359fn daseal kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong
360daad kia aa
361n = dbn
362dcopy buf pt get st 8
363f = ulong 0
364if ne dacall kia aa buf 0 0
365f = 1
366end
367f = or f dcmp buf ex n
368f = or f daopen kia pt buf aa 0
369dcopy buf ex n
370put buf sub n 1 xor get buf sub n 1 1
371f = or f daopen kia pt buf aa 1
372ret f
373end
374
375note an encrypt-direction vector (E, W V): seal, open, tamper, and the TLS path
376fn dvalid kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong
377if ne get st 8 get st 9
378dbad
379ret 2
380end
381f = dseal kia pt ex buf
382dcopy buf ex dbn
383f = or f dopen kia pt buf
384f = or f dtamp kia ex buf
385if eq dtls 1
386f = or f daseal kia pt ex buf aa
387end
388ret f
389end
390
391note a vector that must be refused (D FAIL, W I)
392fn dinval kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong
393n = dbn
394dcopy buf ex n
395if eq get st 6 0
396f = dfail kia buf 2
397dcopy buf ex n
398c = dctl
399a = dka
400if ne aes_gcm_seal c part kia 0 a part buf 0 n 2
401f = 1
402end
403ret f
404end
405f = dfail kia buf 1
406if eq dtls 1
407daad kia aa
408dcopy buf ex n
409f = or f daopen kia pt buf aa 1
410end
411ret f
412end
413
414note a decrypt vector with a plaintext (D)
415fn ddec kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong
416n = dbn
417dcopy buf ex n
418f = dopen kia pt buf
419if eq dtls 1
420daad kia aa
421dcopy buf ex n
422f = or f daopen kia pt buf aa 0
423end
424ret f
425end
426
427note key, iv and aad fields into kia
428fn dkia kia edit uchar 65536 to void
429put st 5 dhexf kia 0
430dspace
431put st 6 dhexf kia get st 5
432dspace
433put st 7 dhexf kia add get st 5 get st 6
434end
435
436note ct then tag fields into ex
437fn dcttag ex edit uchar 65536 to void
438put st 9 dhexf ex 0
439dspace
440put st 10 dhexf ex get st 9
441end
442
443note E record after the tag
444fn drece kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong
445dspace
446dkia kia
447dspace
448put st 8 dhexf pt 0
449dspace
450dcttag ex
451deor
452if eq get st 3 1
453ret 2
454end
455ret dvalid kia pt ex buf aa
456end
457
458note D record after the tag
459fn drecd kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong
460dspace
461dkia kia
462dspace
463dcttag ex
464dspace
465put st 8 get st 9
466put st 11 0
467if eq dpeek 70
468put st 11 1
469for i ulong 4
470dadv
471end
472else
473if ne dhexf pt 0 get st 9
474dbad
475end
476end
477deor
478if eq get st 3 1
479ret 2
480end
481if eq get st 11 1
482ret dinval kia pt ex buf aa
483end
484ret ddec kia pt ex buf aa
485end
486
487note W record after the tag
488fn drecw kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong
489dspace
490t = dnum
491dspace
492c = dpeek
493dadv
494put st 11 1
495if eq c 86
496put st 11 0
497else
498if ne c 73
499dbad
500end
501end
502dspace
503dkia kia
504dspace
505put st 8 dhexf pt 0
506dspace
507dcttag ex
508deor
509if eq get st 3 1
510ret 2
511end
512if eq get st 11 1
513ret dinval kia pt ex buf aa
514end
515ret dvalid kia pt ex buf aa
516end
517
518note one block pt through aes_setkey (key kia[0..kl)) and aes_enc4 (lane 0); 0 when it equals ex
519fn dblk kia view uchar 65536 pt edit uchar 65536 ex view uchar 65536 kl ulong to ulong
520k = new ulong AGKN 0
521s = new ulong AGSN 0
522aes_setkey k s part kia 0 kl
523h = ulong 0
524l = ulong 0
525for i ulong 8
526h = or shl h 8 ulong get pt i
527l = or shl l 8 ulong get pt add i 8
528end
529put s 150 h
530put s 151 l
531aes_enc4 k s
532h = get s 150
533l = get s 151
534for i ulong 8
535put pt sub 7 i uchar and h 0xff
536put pt sub 15 i uchar and l 0xff
537h = shr h 8
538l = shr l 8
539end
540ret dcmp pt ex 16
541end
542
543note B record after the tag
544fn drecb kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 to ulong
545dspace
546kl = dhexf kia 0
547dspace
548pl = dhexf pt 0
549dspace
550cl = dhexf ex 0
551deor
552if eq get st 3 1
553ret 2
554end
555if ne pl 16
556dbad
557ret 2
558end
559if ne cl 16
560dbad
561ret 2
562end
563if ne gcm_chk or kl 0x11000 add kl 1 16 0
564dbad
565ret 2
566end
567ret dblk kia pt ex kl
568end
569
570note X record: seal and open with ctl on zero arrays of the given lengths must both return want
571fn drecx kia edit uchar 65536 buf edit uchar 65536 to ulong
572dspace
573ctl = dnum
574dspace
575ka = dnum
576dspace
577bl = dnum
578dspace
579want = dnum
580deor
581if eq get st 3 1
582ret 2
583end
584if gt ka 65536
585dbad
586ret 2
587end
588if gt bl 65536
589dbad
590ret 2
591end
592for i ulong 65536
593put kia i 0
594put buf i 0
595end
596f = ulong 0
597if ne aes_gcm_seal ctl part kia 0 ka part buf 0 bl want
598f = 1
599end
600if ne aes_gcm_open ctl part kia 0 ka part buf 0 bl want
601f = 1
602end
603ret f
604end
605
606note Y record: aead_aes128gcm_seal and _open on zero arrays must both return want
607fn drecy kia edit uchar 65536 aa edit uchar 65536 buf edit uchar 65536 to ulong
608dspace
609kn = dnum
610dspace
611al = dnum
612dspace
613bl = dnum
614dspace
615want = dnum
616deor
617if eq get st 3 1
618ret 2
619end
620if gt kn 65536
621dbad
622ret 2
623end
624if gt al 65536
625dbad
626ret 2
627end
628if gt bl 65536
629dbad
630ret 2
631end
632for i ulong 65536
633put kia i 0
634put aa i 0
635put buf i 0
636end
637put st 14 kn
638put st 7 al
639put st 8 bl
640put st 10 0
641f = ulong 0
642if ne dacall kia aa buf 0 want
643f = 1
644end
645if ne dacall kia aa buf 1 want
646f = 1
647end
648put st 14 28
649ret f
650end
651
652note the speed line: n bytes, seal and open nanoseconds, and n * 1000 / ns (decimal MB/s, 10^6 bytes)
653fn dspd n ulong a ulong b ulong to void
654w = write 1 tspd 0 len tspd
655dprn n
656w = write 1 tsns 0 len tsns
657dprn a
658w = write 1 tons 0 len tons
659dprn b
660w = write 1 tsmb 0 len tsmb
661dprn div mul n 1000 or a 1
662w = write 1 tomb 0 len tomb
663dprn div mul n 1000 or b 1
664w = write 1 tnl 0 1
665end
666
667note T record: seal then open n bytes in place with a fixed key and 12-byte IV, timed
668fn drect kia edit uchar 65536 to ulong
669mbuf = new uchar 1048592 0
670dspace
671n = dnum
672deor
673if eq get st 3 1
674ret 2
675end
676if gt n 1048576
677dbad
678ret 2
679end
680for i ulong 28
681put kia i uchar i
682end
683ctl = ulong 0xc1010
684t0 = monons
685m = add n 16
686r = aes_gcm_seal ctl part kia 0 28 part mbuf 0 m
687t1 = monons
688r = or r aes_gcm_open ctl part kia 0 28 part mbuf 0 m
689t2 = monons
690dspd n ulong sub t1 t0 ulong sub t2 t1
691if ne r 0
692ret 1
693end
694ret dzbig part mbuf 0 n
695end
696
697note 0 when a is all zero (two loop levels: one rolled loop traps past 1000000 passes)
698fn dzbig a view uchar * to ulong
699n = len a
700d = ulong 0
701o = ulong 0
702for h ulong add shr n 16 1
703for l ulong 65536
704if lt o n
705d = or d ulong get a o
706end
707o = add o 1
708end
709end
710ret d
711end
712
713note one record at the cursor; adds to st 12 (checks) and st 13 (failures)
714fn drec kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to void
715c = dpeek
716res = ulong 0
717if eq c 35
718dskip
719ret
720end
721if eq c 10
722dadv
723ret
724end
725if eq c 13
726dskip
727ret
728end
729dadv
730if eq c 66
731res = drecb kia pt ex
732else
733if eq c 69
734res = drece kia pt ex buf aa
735else
736if eq c 68
737res = drecd kia pt ex buf aa
738else
739if eq c 87
740res = drecw kia pt ex buf aa
741else
742if eq c 88
743res = drecx kia buf
744else
745if eq c 89
746res = drecy kia aa buf
747else
748if eq c 84
749res = drect kia
750else
751dbad
752end
753end
754end
755end
756end
757end
758end
759put st 12 add get st 12 1
760if ne res 0
761put st 13 add get st 13 1
762if eq get st 3 1
763dmsg 1 get st 2
764else
765dmsg 0 get st 2
766end
767end
768end
769
770fn main to int
771kia = new uchar 65536 0
772pt = new uchar 65536 0
773ex = new uchar 65536 0
774buf = new uchar 65536 0
775aa = new uchar 65536 0
776if ne dslurp 0
777r = write 2 tio 0 len tio
778ret 2
779end
780put st 1 0
781put st 2 0
782put st 12 0
783put st 13 0
784put st 14 28
785while lt get st 1 get st 0 bound 999999
786put st 2 add get st 2 1
787put st 3 0
788put st 4 get st 1
789drec kia pt ex buf aa
790if eq get st 3 1
791put st 1 get st 4
792dskip
793end
794end
795r = write 1 tsum 0 len tsum
796dprn get st 12
797r = write 1 tchk 0 len tchk
798dprn get st 13
799r = write 1 tfld 0 len tfld
800if ne get st 13 0
801ret 1
802end
803if eq get st 12 0
804ret 1
805end
806ret 0
807end