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

510 lines · 9695 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: program-mode test driver for om/tls/drbg.om (run.sh writes om2, then sha2.om, hkdf.om,
3note drbg.om, this file). Reads records from stdin (at most 8 MiB), one per line, fields separated by
4note one space, byte strings in hex ("-" = empty):
5note D NB EI NONCE PS OP .. OP = RB
6note drbg_inst(EI || NONCE || PS), then each OP in order, then compare the output of the last
7note generate with RB (NB bytes). OP is one of
8note G AI drbg_gen of NB bytes with additional input AI
9note R AI EI drbg_reseed(EI || AI) (CAVP pr_false, ACVP reSeed)
10note P AI EI prediction resistance: drbg_reseed(EI || AI), then drbg_gen of NB bytes with
11note Null additional input (SP 800-90A 9.3.1 step 7; CAVP pr_true, ACVP PR)
12note Checks: RB matches (1), and the negative control: RB with one byte flipped must not match (1).
13note S the API checks below (one check each)
14note Lines that are empty or start with # are skipped. Prints "MISMATCH line L" (wrong result),
15note "STUCK line L" (a call returned 4, the continuous-test error state) or "BAD line L"
16note (malformed record), then "drbg driver: C checks, F failed". Exit 0 only when F is 0 and C > 0;
17note 1 otherwise; 2 when the input cannot be read or exceeds 8 MiB.
18
19global inp uchar 8388608
20global st ulong 8
21global ob uchar 32
22global one uchar 1
23text tfail "MISMATCH line "
24text tstuck "STUCK line "
25text tbad "BAD line "
26text tsum "drbg driver: "
27text tchk " checks, "
28text tfld " failed\n"
29text tnl "\n"
30text tio "drbg driver: cannot read input or input over 8 MiB\n"
31
32note st: 0 input length, 1 cursor, 2 line number, 3 malformed flag, 4 line start, 5 checks, 6 failed
33
34fn drslurp to int
35t = ulong 0
36while true bound 999999
37room = sub len inp t
38if eq room 0
39r = read 0 one 0 1
40if ne r 0
41ret 2
42end
43break
44end
45r = read 0 inp t room
46if lt r 0
47ret 2
48end
49if eq r 0
50break
51end
52t = add t ulong r
53end
54put st 0 t
55ret 0
56end
57
58fn drpeek to ulong
59c = get st 1
60if ge c get st 0
61ret 256
62end
63ret ulong get inp c
64end
65
66fn dradv to void
67put st 1 add get st 1 1
68end
69
70fn drbad to void
71put st 3 1
72end
73
74fn drhexv c ulong to ulong
75if ge c 48
76if le c 57
77ret sub c 48
78end
79end
80if ge c 97
81if le c 102
82ret sub c 87
83end
84end
85if ge c 65
86if le c 70
87ret sub c 55
88end
89end
90ret 16
91end
92
93fn drbyte to uchar
94hi = drhexv drpeek
95dradv
96lo = drhexv drpeek
97dradv
98if gt hi 15
99drbad
100ret 0
101end
102if gt lo 15
103drbad
104ret 0
105end
106ret uchar or shl hi 4 lo
107end
108
109fn drsp to void
110if ne drpeek 32
111drbad
112ret
113end
114dradv
115end
116
117note a decimal number (at most 9 digits) after one space
118fn drnum to ulong
119drsp
120v = ulong 0
121k = ulong 0
122while true bound 10
123c = drpeek
124if lt c 48
125break
126end
127if gt c 57
128break
129end
130v = add mul v 10 sub c 48
131k = add k 1
132dradv
133end
134if eq k 0
135drbad
136end
137if gt k 9
138drbad
139end
140ret v
141end
142
143note one space, then hex bytes into a[0..n) (or "-"); returns n
144fn drhex a edit uchar * to ulong
145drsp
146if eq drpeek 45
147dradv
148ret 0
149end
150n = ulong 0
151while true bound 999999
152if gt drhexv drpeek 15
153break
154end
155if ge n len a
156drbad
157ret n
158end
159put a n drbyte
160n = add n 1
161end
162if eq n 0
163drbad
164end
165ret n
166end
167
168fn dreor to void
169if eq drpeek 13
170dradv
171end
172c = drpeek
173if eq c 10
174dradv
175ret
176end
177if ne c 256
178drbad
179end
180end
181
182fn drskip to void
183while true bound 999999
184c = drpeek
185if eq c 256
186ret
187end
188dradv
189if eq c 10
190ret
191end
192end
193end
194
195fn drprn v ulong to void
196x = v
197k = ulong 0
198while true bound 21
199put ob sub 31 k uchar add 48 rem x 10
200k = add k 1
201x = div x 10
202if eq x 0
203break
204end
205end
206r = write 1 ob sub 32 k k
207end
208
209note kind 0 MISMATCH, 1 BAD, 2 STUCK
210fn drmsg kind ulong to void
211if eq kind 1
212r = write 1 tbad 0 len tbad
213else
214if eq kind 2
215r = write 1 tstuck 0 len tstuck
216else
217r = write 1 tfail 0 len tfail
218end
219end
220drprn get st 2
221r = write 1 tnl 0 1
222end
223
224note one check: ok 1 passes, anything else fails (and is reported)
225fn drchk ok ulong to void
226put st 5 add get st 5 1
227if ne ok 1
228put st 6 add get st 6 1
229drmsg 0
230end
231end
232
233note 1 when a[0..n) equals b[0..n) (n <= both lengths), else 0
234fn dreq a view uchar * b view uchar * n ulong to ulong
235if gt n len a
236ret 0
237end
238if gt n len b
239ret 0
240end
241d = uchar 0
242for i ulong n
243d = or d xor get a i get b i
244end
245if eq d 0
246ret 1
247end
248ret 0
249end
250
251note sm[n..) gets x; returns n + len x (0 and the record is malformed when it does not fit)
252fn drapp sm edit uchar * n ulong x view uchar * to ulong
253if gt add n len x len sm
254drbad
255ret 0
256end
257for i ulong len x
258put sm add n i get x i
259end
260ret add n len x
261end
262
263note seed material EI || NONCE || PS from the record into sm, then drbg_inst; returns its code
264fn drinit a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 s edit uchar 128 to ulong
265na = drhex a
266nn = drhex b
267n = drapp sm 0 part a 0 na
268n = drapp sm n part b 0 nn
269na = drhex a
270n = drapp sm n part a 0 na
271ret drbg_inst s part sm 0 n
272end
273
274note reseed OP fields "AI EI": drbg_reseed(EI || AI); returns its code
275fn drres a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 s edit uchar 128 to ulong
276na = drhex a
277nn = drhex b
278n = drapp sm 0 part b 0 nn
279n = drapp sm n part a 0 na
280ret drbg_reseed s part sm 0 n
281end
282
283note generate nb bytes into o with additional input a[0..na)
284fn drgen a edit uchar 4096 na ulong o edit uchar 65537 s edit uchar 128 nb ulong to ulong
285ret drbg_gen s part o 0 nb part a 0 na
286end
287
288note one OP (after its space): returns the call's code, plus 256 when it generated
289fn drop1 a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 s edit uchar 128 nb ulong to ulong
290c = drpeek
291dradv
292if eq c 71
293ret add 256 drgen a drhex a o s nb
294end
295if eq c 82
296ret drres a b sm s
297end
298if eq c 80
299rc = drres a b sm s
300if eq rc 0
301rc = drgen a 0 o s nb
302end
303ret add 256 rc
304end
305drbad
306ret 0
307end
308
309note the OPs up to and including "="; returns bit 0 a call failed, bit 1 a call returned 4 (the
310note continuous-test error state), bit 2 at least one generate ran
311fn drops a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 s edit uchar 128 nb ulong to ulong
312f = ulong 0
313while true bound 64
314drsp
315if eq drpeek 61
316dradv
317break
318end
319r = drop1 a b sm o s nb
320if ge r 256
321f = or f 4
322end
323r = and r 255
324if eq r 4
325f = or f 2
326end
327if ne r 0
328f = or f 1
329end
330if eq get st 3 1
331ret f
332end
333end
334ret f
335end
336
337note a D record (after the D); a, b scratch, sm seed material, o output, ex expected, s the DRBG state
338fn drrecd a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 ex edit uchar 65536 s edit uchar 128 to void
339nb = drnum
340if gt nb len o
341drbad
342ret
343end
344if eq nb 0
345drbad
346ret
347end
348f = ulong 0
349if ne drinit a b sm s 0
350f = 1
351end
352f = or f drops a b sm o s nb
353nx = drhex ex
354dreor
355if eq get st 3 1
356ret
357end
358if ne nx nb
359drbad
360ret
361end
362if eq and f 4 0
363drbad
364ret
365end
366if ne and f 2 0
367drmsg 2
368end
369ok = dreq part o 0 nb ex nb
370if ne and f 1 0
371ok = 0
372end
373drchk ok
374put ex 0 xor get ex 0 1
375drchk xor dreq part o 0 nb ex nb 1
376drbg_wipe s
377end
378
379note one check that got equals want
380fn drwant got ulong want ulong to void
381ok = ulong 0
382if eq got want
383ok = 1
384end
385drchk ok
386end
387
388note fixed seed material of n bytes (byte i = i + 1) for the API checks
389fn drseed sm edit uchar * n ulong to void
390for i ulong n
391put sm i uchar add i 1
392end
393end
394
395note S record: API checks (each is one check)
396fn drrecs sm edit uchar 8192 o edit uchar 65537 s edit uchar 128 to void
397dreor
398drbg_wipe s
399note generate and reseed before instantiate: 2
400drwant drbg_gen s part o 0 32 part sm 0 0 2
401drseed sm 64
402drwant drbg_reseed s part sm 0 32 2
403note seed material under 48 bytes is refused (3); 48 is accepted
404drwant drbg_inst s part sm 0 47 3
405drwant drbg_inst s part sm 0 48 0
406note reseed material under 32 bytes is refused (3)
407drwant drbg_reseed s part sm 0 31 3
408note request over 65536 bytes is refused (3); 65536 is accepted
409drwant drbg_gen s part o 0 65537 part sm 0 0 3
410drwant drbg_gen s part o 0 65536 part sm 0 0 0
411note reseed interval: limit 2 allows generates with counter 1 and 2, then 1 (reseed required) and
412note no output written; after a reseed generates work again
413drwant drbg_limit s 0 3
414drwant drbg_limit s add DRRSMX 1 3
415drwant drbg_inst s part sm 0 48 0
416drwant drbg_limit s 2 0
417drwant drbg_gen s part o 0 16 part sm 0 0 0
418drwant drbg_gen s part o 0 16 part sm 0 0 0
419for i ulong 16
420put o i 170
421end
422drwant drbg_gen s part o 0 16 part sm 0 0 1
423d = ulong 0
424for i ulong 16
425d = or d xor ulong get o i 170
426end
427drwant d 0
428drwant drbg_reseed s part sm 0 32 0
429drwant drbg_gen s part o 0 16 part sm 0 0 0
430note two generates give different output; the state after wipe is all zero and refuses
431drwant drbg_limit s DRRSMX 0
432drwant drbg_gen s part o 0 32 part sm 0 0 0
433drwant drbg_gen s part o 32 32 part sm 0 0 0
434for i ulong 32
435put sm add 4096 i get o i
436end
437drchk xor dreq part o 32 32 part sm 4096 32 32 1
438drbg_wipe s
439d = ulong 0
440for i ulong len s
441d = or d ulong get s i
442end
443drwant d 0
444drwant drbg_gen s part o 0 16 part sm 0 0 2
445end
446
447fn drrec a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 ex edit uchar 65536 s edit uchar 128 to void
448c = drpeek
449dradv
450if eq c 68
451drrecd a b sm o ex s
452ret
453end
454if eq c 83
455drrecs sm o s
456ret
457end
458drbad
459end
460
461fn main to int
462a = new uchar 4096 0
463b = new uchar 4096 0
464sm = new uchar 8192 0
465o = new uchar 65537 0
466ex = new uchar 65536 0
467s = new uchar 128 0
468if ne drslurp 0
469r = write 2 tio 0 len tio
470ret 2
471end
472put st 1 0
473put st 2 0
474put st 5 0
475put st 6 0
476while lt get st 1 get st 0 bound 999999
477put st 2 add get st 2 1
478put st 3 0
479put st 4 get st 1
480c = drpeek
481if eq c 35
482drskip
483else
484if eq c 10
485dradv
486else
487drrec a b sm o ex s
488if eq get st 3 1
489put st 5 add get st 5 1
490put st 6 add get st 6 1
491drmsg 1
492put st 1 get st 4
493drskip
494end
495end
496end
497end
498r = write 1 tsum 0 len tsum
499drprn get st 5
500r = write 1 tchk 0 len tchk
501drprn get st 6
502r = write 1 tfld 0 len tfld
503if ne get st 6 0
504ret 1
505end
506if eq get st 5 0
507ret 1
508end
509ret 0
510end