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 / acvp / acvp.om

793 lines · 15917 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note acvp.om: ACVP response engine for the om/tls library. Build (run.sh does this): the program is
3note acvpio.om || acvp.om || acvpmain.om, composed after the library by sh om/tls/build.sh OMC PROG OUT.
4note It computes, it does not grade: every record is answered with the library's own result and the
5note comparison against NIST's expectedResults.json happens afterwards (om2acvp.py). Exception: the
6note KDA validation (VAL) records, where the IUT itself must say whether a supplied DKM is right, and
7note the AES-GCM decrypt records, where the library's tag check decides; both decisions are made here.
8note Input on stdin (at most 16 MiB), one record per line, fields separated by one space, byte strings
9note in hex ("-" = empty), ID a decimal record number echoed as the first output field:
10note H ID alg MSG sha2_digest -> ID MD
11note M ID alg ver SEED SHA-2 Monte Carlo, ver 0 standard, 1 alternate (ACVP SHA
12note spec / ACVP-Server StandardSizeShaMct, AlternateSizeShaMct),
13note 100 outer rounds of 1000 -> ID MD1 .. MD100
14note L ID alg CONTENT NBYTES large-data test: CONTENT repeated to NBYTES bytes, streamed
15note through sha2_update in slices of whole CONTENT copies of at
16note most 512 KiB -> ID MD
17note T ID alg KEY MSG MACBYTES hmac_init/update/final, first MACBYTES bytes -> ID MAC
18note G ID KEY IV AAD PT TAGBYTES aes_gcm_seal -> ID RC CT TAG
19note D ID KEY IV AAD CT TAG aes_gcm_open -> ID RC PT (PT "-" unless RC 0)
20note E ID crv alg MSG PUB SIG hash MSG with alg, ecdsa_verify_raw crv (PUB = 04||X||Y,
21note SIG = r||s) -> ID RC
22note R ID alg KEY MSG SIG hash MSG, rsa_verify_pkcs1 (KEY = DER RSAPublicKey) -> ID RC
23note K ID alg SALT IKM N FI1 L1 .. FIN LN hkdf_extract once, hkdf_expand per (FI, L bytes)
24note -> ID DKM1 .. DKMN OK (1 when every hkdf_expand returned 0)
25note V ID alg SALT IKM N FI1 L1 DKM1 .. as K, then the DKMs are compared here -> ID OK (1/0)
26note S ID alg PSK DHE CH SH SF CF RFC 8446 7.1 key schedule from hkdf_extract/hkdf_expand and
27note sha2_digest ("-" PSK or DHE = Hash.length zero bytes)
28note -> ID CETS EEMS CHTS SHTS CATS SATS EMS RMS
29note N ID PSK DHE CH SH SF the same through tls.om's own tls_xtr/tls_drv (SHA-256, 32-byte
30note PSK and DHE only) -> ID RC CHTS SHTS CATS SATS
31note X ID PRIV PEER x25519_base, x25519_scalarmult -> ID RC1 PUB RC2 Z
32note B ID KEY PT AES ECB encryption (aes_setkey, aes_enc4) -> ID CT
33note C ID KEY PT AES ECB encrypt Monte Carlo (ACVP AES MCT)
34note -> ID (KEY PT CT) x 100
35note A malformed record prints "ID BAD" (or "0 BAD" before the ID is read). Exit 0 when no record
36note was malformed, 1 otherwise, 2 when stdin cannot be read or exceeds 16 MiB.
37
38text S13CET "c e traffic"
39text S13EEM "e exp master"
40text S13EXP "exp master"
41text S13RES "res master"
42
43note digest length of alg, 0 (and the record malformed) when alg is not 256, 384 or 512
44fn drdl alg ulong to ulong
45if eq alg 256
46ret 32
47end
48if eq alg 384
49ret 48
50end
51if eq alg 512
52ret 64
53end
54drbad
55ret 0
56end
57
58note ---- SHA-2 ----
59
60fn rech a edit uchar * to void
61dg = new uchar 64 0
62drid
63alg = drnum
64dl = drdl alg
65n = drhex a
66dreor
67if isbad
68ret
69end
70k = sha2_digest alg part a 0 n dg
71ohex part dg 0 k
72end
73
74note m = x0 || x1 || x2 (x slot j at 1024 j, length q[j]); ver q[4] 1: m cut or zero-padded to q[3]
75fn mcmsg m edit uchar * x view uchar 3072 q view ulong 8 to ulong
76n = ulong 0
77for j ulong 3
78for i ulong get q j
79put m n get x add mul j 1024 i
80n = add n 1
81end
82end
83if eq get q 4 1
84sl = get q 3
85while lt n sl bound 4096
86put m n 0
87n = add n 1
88end
89n = sl
90end
91ret n
92end
93
94note x0 = x1, x1 = x2, x2 = dg[0..dl)
95fn mcshift x edit uchar 3072 q edit ulong 8 dg view uchar 64 dl ulong to void
96for j ulong 2
97put q j get q add j 1
98for i ulong get q j
99put x add mul j 1024 i get x add mul add j 1 1024 i
100end
101end
102put q 2 dl
103for i ulong dl
104put x add 2048 i get dg i
105end
106end
107
108note x1 = x2 = x0, all of length n
109fn mcseed x edit uchar 3072 q edit ulong 8 n ulong to void
110for i ulong n
111put x add 1024 i get x i
112put x add 2048 i get x i
113end
114put q 0 n
115put q 1 n
116put q 2 n
117end
118
119fn recm a edit uchar * to void
120x = new uchar 3072 0
121q = new ulong 8 0
122dg = new uchar 64 0
123drid
124alg = drnum
125dl = drdl alg
126ver = drnum
127sl = drhex part x 0 1024
128dreor
129if gt ver 1
130drbad
131end
132if eq ver 0
133if ne sl dl
134drbad
135end
136end
137if isbad
138ret
139end
140put q 3 sl
141put q 4 ver
142mcseed x q sl
143for j ulong 100
144for c ulong 1000
145n = mcmsg a x q
146k = sha2_digest alg part a 0 n dg
147mcshift x q dg dl
148end
149ohex part dg 0 dl
150for i ulong dl
151put x i get dg i
152end
153mcseed x q dl
154end
155end
156
157fn recl a edit uchar * to void
158c = new ulong 40 0
159dg = new uchar 64 0
160drid
161alg = drnum
162dl = drdl alg
163cl = drhex part a 0 4096
164full = drnum
165dreor
166if eq cl 0
167drbad
168end
169if isbad
170ret
171end
172bn = mul div 524288 cl cl
173for i ulong sub bn cl
174put a add cl i get a i
175end
176sha2_init c alg
177q = ulong 0
178while lt q full bound 999999
179k = bn
180if gt k sub full q
181k = sub full q
182end
183sha2_update c part a 0 k
184q = add q k
185end
186k = sha2_fin c dg
187ohex part dg 0 k
188end
189
190note ---- HMAC ----
191
192fn rect a edit uchar * b edit uchar * to void
193c = new ulong 96 0
194dg = new uchar 64 0
195drid
196alg = drnum
197dl = drdl alg
198kl = drhex a
199ml = drhex b
200tl = drnum
201dreor
202if gt tl dl
203drbad
204end
205if isbad
206ret
207end
208hmac_init alg c part a 0 kl
209hmac_update c part b 0 ml
210k = hmac_final c dg
211ohex part dg 0 tl
212end
213
214note ---- AES-GCM ----
215
216note kia = KEY || IV || AAD into a; lengths into q[0..3)
217fn gckia a edit uchar * q edit ulong 8 to void
218put q 0 drhex a
219k = get q 0
220put q 1 drhex part a k sub len a k
221k = add k get q 1
222put q 2 drhex part a k sub len a k
223end
224
225fn recg a edit uchar * b edit uchar * to void
226q = new ulong 8 0
227drid
228gckia a q
229pl = drhex b
230tl = drnum
231dreor
232if gt tl 255
233drbad
234end
235if isbad
236ret
237end
238kia = add add get q 0 get q 1 get q 2
239ctl = add add get q 0 shl tl 8 shl get q 1 16
240rc = aes_gcm_seal ctl part a 0 kia part b 0 add pl tl
241ofld rc
242if ne rc 0
243ohex part b 0 0
244ohex part b 0 0
245ret
246end
247ohex part b 0 pl
248ohex part b pl tl
249end
250
251fn recd a edit uchar * b edit uchar * to void
252q = new ulong 8 0
253drid
254gckia a q
255cl = drhex b
256tl = drhex part b cl sub len b cl
257dreor
258if gt tl 255
259drbad
260end
261if isbad
262ret
263end
264kia = add add get q 0 get q 1 get q 2
265ctl = add add get q 0 shl tl 8 shl get q 1 16
266rc = aes_gcm_open ctl part a 0 kia part b 0 add cl tl
267ofld rc
268if ne rc 0
269ohex part b 0 0
270ret
271end
272ohex part b 0 cl
273end
274
275note ---- signatures ----
276
277fn rece a edit uchar * b edit uchar * to void
278drid
279crv = drnum
280alg = drnum
281dl = drdl alg
282ml = drhex a
283pl = drhex part a ml sub len a ml
284sl = drhex part b dl sub len b dl
285dreor
286if isbad
287ret
288end
289k = sha2_digest alg part a 0 ml part b 0 dl
290rc = ecdsa_verify_raw crv part a ml pl part b 0 add dl sl dl
291ofld rc
292end
293
294fn recr a edit uchar * b edit uchar * to void
295drid
296alg = drnum
297dl = drdl alg
298kl = drhex a
299ml = drhex part a kl sub len a kl
300sl = drhex part b dl sub len b dl
301dreor
302if isbad
303ret
304end
305k = sha2_digest alg part a kl ml part b 0 dl
306rc = rsa_verify_pkcs1 alg part a 0 kl part b 0 add dl sl dl
307ofld rc
308end
309
310note ---- KDA HKDF ----
311
312note b = PRK || FI: reads FI into b[dl..) and L, expands into c[0..L); returns rc, L in q[0]
313fn kdaexp alg ulong b edit uchar * c edit uchar * q edit ulong 8 to ulong
314dl = shr alg 3
315fl = drhex part b dl sub len b dl
316l = drnum
317put q 0 l
318if gt l len c
319drbad
320ret 9
321end
322if isbad
323ret 9
324end
325ret hkdf_expand alg part b 0 add dl fl dl part c 0 l
326end
327
328note 1 when a[0..n) equals b[0..n), else 0 (or-accumulated)
329fn dreq a view uchar * b view uchar * n ulong to ulong
330if gt n len a
331ret 0
332end
333if gt n len b
334ret 0
335end
336d = uchar 0
337for i ulong n
338d = or d xor get a i get b i
339end
340if eq d 0
341ret 1
342end
343ret 0
344end
345
346note per expansion: K prints the DKM, V reads the expected DKM and folds the comparison into ok;
347note both end with ok (1 when every expansion returned 0 and, for V, every DKM matched)
348fn kdaloop b edit uchar * c edit uchar * q edit ulong 8 to void
349e = new uchar 65536 0
350ok = ulong 1
351for j ulong get q 2
352rc = kdaexp get q 1 b c q
353if isbad
354ret
355end
356if eq get st 9 0
357ohex part c 0 get q 0
358else
359el = drhex e
360if ne el get q 0
361ok = 0
362end
363ok = and ok dreq c e get q 0
364end
365if ne rc 0
366ok = 0
367end
368end
369dreor
370if isbad
371ret
372end
373ofld ok
374end
375
376fn reck a edit uchar * b edit uchar * c edit uchar * to void
377q = new ulong 8 0
378drid
379alg = drnum
380dl = drdl alg
381sl = drhex a
382il = drhex part a sl sub len a sl
383n = drnum
384if isbad
385ret
386end
387k = hkdf_extract alg part a 0 add sl il sl b
388if ne k dl
389drbad
390ret
391end
392put q 1 alg
393put q 2 n
394kdaloop b c q
395end
396
397note ---- TLS 1.3 key schedule (ACVP TLS-v1.3-KDF-RFC8446) ----
398
399note w[0..dl) = HKDF-Extract(salt w[0..dl), ikm)
400fn s13x w edit uchar 256 dl ulong ikm view uchar * to void
401si = new uchar 4096 0
402p = new uchar 64 0
403for i ulong dl
404put si i get w i
405end
406for i ulong len ikm
407put si add dl i get ikm i
408end
409k = hkdf_extract shl dl 3 part si 0 add dl len ikm dl p
410for i ulong dl
411put w i get p i
412end
413end
414
415note p[dl..) = uint16 dl || uint8 len || "tls13 " || lab (HkdfLabel up to the context); returns its end
416fn s13lab p edit uchar 256 dl ulong lab view uchar * to ulong
417put p dl uchar shr dl 8
418put p add dl 1 uchar and dl 255
419ll = add 6 len lab
420put p add dl 2 uchar ll
421for i ulong 6
422put p add add dl 3 i get TLL13 i
423end
424for i ulong len lab
425put p add add dl 9 i get lab i
426end
427ret add add dl 3 ll
428end
429
430note o[0..dl) = HKDF-Expand(PRK p[0..dl), info p[dl..n), dl)
431fn s13e p view uchar 256 n ulong dl ulong o edit uchar 64 to ulong
432ret hkdf_expand shl dl 3 part p 0 n dl part o 0 dl
433end
434
435note o = HKDF-Expand-Label(w[0..dl), lab, w[64..64+dl), dl) (RFC 8446 7.1)
436fn s13d w view uchar 256 dl ulong lab view uchar * o edit uchar 64 to void
437p = new uchar 256 0
438for i ulong dl
439put p i get w i
440end
441c = s13lab p dl lab
442put p c uchar dl
443for i ulong dl
444put p add add c 1 i get w add 64 i
445end
446r = s13e p add add c 1 dl dl o
447end
448
449note w[64..) = transcript hash j (slot 64 j of h)
450fn s13c w edit uchar 256 h view uchar 320 j ulong dl ulong to void
451for i ulong dl
452put w add 64 i get h add mul j 64 i
453end
454end
455
456note w[0..dl) = o[0..dl)
457fn s13s w edit uchar 256 o view uchar 64 dl ulong to void
458for i ulong dl
459put w i get o i
460end
461end
462
463note h slot j = Hash(a[0..e_j)), e = 0, CH, CH||SH, CH||SH||SF, CH||SH||SF||CF
464fn s13h a view uchar * q view ulong 16 h edit uchar 320 to void
465d = new uchar 64 0
466alg = get q 1
467for j ulong 5
468k = sha2_digest alg part a 0 get q add 4 j d
469for i ulong k
470put h add mul j 64 i get d i
471end
472end
473end
474
475fn s13run a view uchar * b view uchar * q view ulong 16 to void
476w = new uchar 256 0
477h = new uchar 320 0
478o = new uchar 64 0
479z = new uchar 64 0
480dl = get q 2
481s13h a q h
482if eq get q 9 0
483s13x w dl part z 0 dl
484else
485s13x w dl part b 0 get q 9
486end
487s13c w h 1 dl
488s13d w dl S13CET o
489ohex part o 0 dl
490s13d w dl S13EEM o
491ohex part o 0 dl
492s13c w h 0 dl
493s13d w dl TLLDER o
494s13s w o dl
495if eq get q 10 0
496s13x w dl part z 0 dl
497else
498s13x w dl part b 1024 get q 10
499end
500s13c w h 2 dl
501s13d w dl TLLCHS o
502ohex part o 0 dl
503s13d w dl TLLSHS o
504ohex part o 0 dl
505s13c w h 0 dl
506s13d w dl TLLDER o
507s13s w o dl
508s13x w dl part z 0 dl
509s13c w h 3 dl
510s13d w dl TLLCAP o
511ohex part o 0 dl
512s13d w dl TLLSAP o
513ohex part o 0 dl
514s13d w dl S13EXP o
515ohex part o 0 dl
516s13c w h 4 dl
517s13d w dl S13RES o
518ohex part o 0 dl
519end
520
521note PSK into b[0..), DHE into b[1024..), CH SH SF CF back to back in a; q: 1 alg, 2 dl, 4..8 ends
522note (q 4 = 0), 9 PSK length, 10 DHE length
523fn s13in a edit uchar * b edit uchar * q edit ulong 16 nm ulong to void
524put q 9 drhex part b 0 1024
525put q 10 drhex part b 1024 1024
526put q 4 0
527e = ulong 0
528for j ulong nm
529k = drhex part a e sub len a e
530e = add e k
531put q add 5 j e
532end
533dreor
534end
535
536fn recs a edit uchar * b edit uchar * to void
537q = new ulong 16 0
538drid
539put q 1 drnum
540put q 2 drdl get q 1
541s13in a b q 4
542if isbad
543ret
544end
545s13run a b q
546end
547
548note ---- the same schedule through tls.om's tls_xtr / tls_drv (SHA-256 only) ----
549
550fn s13tls a view uchar * b view uchar * q view ulong 16 to void
551c = new uchar TLCSZ 0
552h = new uchar 32 0
553z = new uchar 32 0
554tls_h0 z
555for i ulong 32
556put c add TLTMP i get b i
557put c add TLSS i get b add 1024 i
558end
559r = tls_xtr c or TLHS shl TLTMP 32
560r = or r tls_drv c tls_sel TLHS TLHS 32 TLIDER z
561r = or r tls_xtr c or TLHS or shl TLHS 16 shl TLSS 32
562k = sha2_digest 256 part a 0 get q 6 h
563r = or r tls_drv c tls_sel TLCHS TLHS 32 TLICHS h
564r = or r tls_drv c tls_sel TLSHS TLHS 32 TLISHS h
565r = or r tls_drv c tls_sel TLMS TLHS 32 TLIDER z
566r = or r tls_xtr c or TLMS shl TLMS 16
567k = sha2_digest 256 part a 0 get q 7 h
568r = or r tls_drv c tls_sel TLCAP TLMS 32 TLICAP h
569r = or r tls_drv c tls_sel TLSAP TLMS 32 TLISAP h
570ofld r
571ohex part c TLCHS 32
572ohex part c TLSHS 32
573ohex part c TLCAP 32
574ohex part c TLSAP 32
575end
576
577fn recn a edit uchar * b edit uchar * to void
578q = new ulong 16 0
579drid
580put q 1 256
581put q 2 32
582s13in a b q 3
583if ne get q 9 32
584drbad
585end
586if ne get q 10 32
587drbad
588end
589if isbad
590ret
591end
592s13tls a b q
593end
594
595note ---- X25519 ----
596
597fn recx to void
598k = new uchar 32 0
599u = new uchar 32 0
600o = new uchar 32 0
601drid
602kl = drhex k
603ul = drhex u
604dreor
605if ne kl 32
606drbad
607end
608if ne ul 32
609drbad
610end
611if isbad
612ret
613end
614r = x25519_base o k
615ofld r
616ohex o
617r = x25519_scalarmult o k u
618ofld r
619ohex o
620end
621
622note ---- AES block cipher (aesgcm.om's aes_setkey / aes_enc4, encrypt direction only) ----
623
624note block b[o..o+16) into lane 0 of s (s[150] bytes 0..7, s[151] bytes 8..15, big-endian)
625fn aesld s edit ulong AGSN b view uchar * o ulong to void
626x = ulong 0
627y = ulong 0
628for i ulong 8
629x = or shl x 8 ulong get b add o i
630y = or shl y 8 ulong get b add add o 8 i
631end
632put s 150 x
633put s 151 y
634end
635
636fn aesst s view ulong AGSN b edit uchar * o ulong to void
637for i ulong 16
638v = get s add 150 shr i 3
639put b add o i uchar and shr v shl sub 7 and i 7 3 255
640end
641end
642
643note b[o..o+16) = AES-Encrypt(k, b[o..o+16))
644fn aesenc k view ulong AGKN s edit ulong AGSN b edit uchar * o ulong to void
645aesld s b o
646aes_enc4 k s
647aesst s b o
648end
649
650fn aeskl kl ulong to void
651if eq kl 16
652ret
653end
654if eq kl 24
655ret
656end
657if eq kl 32
658ret
659end
660drbad
661end
662
663note B ID KEY PT: ECB encryption of every 16-byte block -> ID CT
664fn recb a edit uchar * b edit uchar * to void
665k = new ulong AGKN 0
666s = new ulong AGSN 0
667drid
668kl = drhex a
669pl = drhex b
670dreor
671aeskl kl
672if ne and pl 15 0
673drbad
674end
675if isbad
676ret
677end
678aes_setkey k s part a 0 kl
679for i ulong shr pl 4
680aesenc k s b shl i 4
681end
682ohex part b 0 pl
683end
684
685note MCT key update (ACVP AES MCT, ECB): key ^= the last kl bytes of ct998 || ct999 (u = 32 bytes:
686note ct998 at 0, ct999 at 16)
687fn aesmku key edit uchar 32 kl ulong u view uchar 32 to void
688o = sub 32 kl
689for i ulong kl
690put key i xor get key i get u add o i
691end
692end
693
694note C ID KEY PT: ECB encrypt Monte Carlo, 100 rounds of 1000 -> ID (KEY PT CT) x 100
695fn recc a edit uchar * to void
696k = new ulong AGKN 0
697s = new ulong AGSN 0
698key = new uchar 32 0
699u = new uchar 32 0
700drid
701kl = drhex key
702pl = drhex part u 16 16
703dreor
704aeskl kl
705if ne pl 16
706drbad
707end
708if isbad
709ret
710end
711for r ulong 100
712ohex part key 0 kl
713ohex part u 16 16
714aes_setkey k s part key 0 kl
715for j ulong 1000
716for i ulong 16
717put u i get u add 16 i
718end
719aesenc k s u 16
720end
721ohex part u 16 16
722aesmku key kl u
723end
724end
725
726fn drrec a edit uchar * b edit uchar * c edit uchar * to void
727op = drpeek
728dradv
729if eq op 72
730rech a
731ret
732end
733if eq op 77
734recm a
735ret
736end
737if eq op 76
738recl a
739ret
740end
741if eq op 84
742rect a b
743ret
744end
745if eq op 71
746recg a b
747ret
748end
749if eq op 68
750recd a b
751ret
752end
753if eq op 69
754rece a b
755ret
756end
757if eq op 82
758recr a b
759ret
760end
761if eq op 75
762put st 9 0
763reck a b c
764ret
765end
766if eq op 86
767put st 9 1
768reck a b c
769ret
770end
771if eq op 83
772recs a b
773ret
774end
775if eq op 78
776recn a b
777ret
778end
779if eq op 66
780recb a b
781ret
782end
783if eq op 67
784recc a
785ret
786end
787if eq op 88
788recx
789ret
790end
791drbad
792end
793