interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / sha512 / test / driver.om

389 lines · 6065 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: program-mode test driver for om/sha512, concatenated after sha512.om.
3note Reads vector records from stdin (at most 16 MiB), one per line, fields separated by one space:
4note H n MSG MD MD = SHA-512(MSG), MSG n bytes in hex ("-" when n is 0)
5note P n PRE MSG MD MD = SHA-512(PRE || MSG), PRE 64 bytes in hex, checks s512pre
6note M SEED MD0 .. MD99 NIST CAVP SHA-2 Monte Carlo chain (SHAVS section 6.4), 100 checkpoints
7note Lines that are empty or start with # are skipped; a trailing carriage return is allowed.
8note Prints "FAIL line L" (wrong digest) or "BAD line L" (malformed record) per failure, then
9note "sha512 driver: C checks, F failed". Exit 0 only when F is 0 and C is above 0; exit 1 otherwise;
10note exit 2 when the input cannot be read or exceeds 16 MiB.
11
12global inp uchar 16777216
13global st ulong 8
14global ob uchar 32
15global one uchar 1
16text tfail "FAIL line "
17text tbad "BAD line "
18text tsum "sha512 driver: "
19text tchk " checks, "
20text tfld " failed\n"
21text tnl "\n"
22text tio "sha512 driver: cannot read input or input over 16 MiB\n"
23
24note st: 0 input length, 1 cursor, 2 line number (1-based), 3 malformed flag
25
26note read all of stdin into inp; 0 ok, 2 read error or over capacity
27fn dslurp to int
28t = ulong 0
29while true bound 999999
30room = sub len inp t
31if eq room 0
32r = read 0 one 0 1
33if ne r 0
34ret 2
35end
36break
37end
38r = read 0 inp t room
39if lt r 0
40ret 2
41end
42if eq r 0
43break
44end
45t = add t ulong r
46end
47put st 0 t
48ret 0
49end
50
51note byte at the cursor, or 256 at the end of input
52fn dpeek to ulong
53c = get st 1
54if ge c get st 0
55ret 256
56end
57ret ulong get inp c
58end
59
60fn dadv to void
61put st 1 add get st 1 1
62end
63
64fn dbad to void
65put st 3 1
66end
67
68note the value of hex digit c, or 16 when c is not one
69fn dhexv c ulong to ulong
70if ge c 48
71if le c 57
72ret sub c 48
73end
74end
75if ge c 97
76if le c 102
77ret sub c 87
78end
79end
80if ge c 65
81if le c 70
82ret sub c 55
83end
84end
85ret 16
86end
87
88note one byte from two hex digits at the cursor (malformed flag on a bad digit)
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 a decimal number at the cursor (at most 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 n bytes of hex into a[0..n); n 0 is written "-"
139fn dhexm a edit uchar 65536 n ulong to void
140if eq n 0
141if ne dpeek 45
142dbad
143ret
144end
145dadv
146ret
147end
148for i ulong n
149put a i dbyte
150end
151end
152
153fn dhex64 a edit uchar 64 to void
154for i ulong 64
155put a i dbyte
156end
157end
158
159note end of record: an optional carriage return, then a newline or the end of input
160fn deor to void
161if eq dpeek 13
162dadv
163end
164c = dpeek
165if eq c 10
166dadv
167ret
168end
169if ne c 256
170dbad
171end
172end
173
174note skip the rest of the line, newline included
175fn dskip to void
176while true bound 999999
177c = dpeek
178if eq c 256
179ret
180end
181dadv
182if eq c 10
183ret
184end
185end
186end
187
188note 0 when a and b hold the same 64 bytes
189fn dcmp a view uchar 64 b view uchar 64 to ulong
190d = ulong 0
191for i ulong 64
192if ne get a i get b i
193d = 1
194end
195end
196ret d
197end
198
199note write v in decimal to stdout
200fn dprn v ulong to void
201x = v
202k = ulong 0
203while true bound 21
204put ob sub 31 k uchar add 48 rem x 10
205k = add k 1
206x = div x 10
207if eq x 0
208break
209end
210end
211r = write 1 ob sub 32 k k
212end
213
214fn dmsg bad ulong ln ulong to void
215if eq bad 1
216r = write 1 tbad 0 len tbad
217else
218r = write 1 tfail 0 len tfail
219end
220dprn ln
221r = write 1 tnl 0 1
222end
223
224note H record after the tag; 0 pass, 1 wrong digest, 2 malformed
225fn drech msg edit uchar 65536 dig edit uchar 64 ex edit uchar 64 to ulong
226dspace
227n = dnum
228if gt n 65536
229dbad
230ret 2
231end
232dspace
233dhexm msg n
234dspace
235dhex64 ex
236deor
237if eq get st 3 1
238ret 2
239end
240s512hash msg n dig
241ret dcmp dig ex
242end
243
244note P record after the tag
245fn drecp msg edit uchar 65536 dig edit uchar 64 ex edit uchar 64 pre edit uchar 64 to ulong
246dspace
247n = dnum
248if gt n 65536
249dbad
250ret 2
251end
252dspace
253dhex64 pre
254dspace
255dhexm msg n
256dspace
257dhex64 ex
258deor
259if eq get st 3 1
260ret 2
261end
262s512pre pre msg n dig
263ret dcmp dig ex
264end
265
266note M record after the tag: SHAVS Monte Carlo. For j = 0..99: MD0 = MD1 = MD2 = Seed; for i = 3..1002
267note MDi = SHA-512(MD(i-3) || MD(i-2) || MD(i-1)); checkpoint j is MD1002, which is also the next Seed.
268note msg[0..192) holds the three previous digests. Returns the number of failed checkpoints; the
269note malformed flag is set on a parse error.
270fn drecm msg edit uchar 65536 dig edit uchar 64 ex edit uchar 64 sd edit uchar 64 to ulong
271fl = ulong 0
272dspace
273dhex64 sd
274for j ulong 100
275for i ulong 64
276put msg i get sd i
277put msg add i 64 get sd i
278put msg add i 128 get sd i
279end
280for c ulong 1000
281s512hash msg 192 dig
282for i ulong 128
283put msg i get msg add i 64
284end
285for i ulong 64
286put msg add i 128 get dig i
287end
288end
289dspace
290dhex64 ex
291if ne dcmp dig ex 0
292fl = add fl 1
293end
294for i ulong 64
295put sd i get dig i
296end
297end
298deor
299ret fl
300end
301
302fn main to int
303msg = new uchar 65536 0
304dig = new uchar 64 0
305ex = new uchar 64 0
306pre = new uchar 64 0
307if ne dslurp 0
308r = write 2 tio 0 len tio
309ret 2
310end
311put st 1 0
312put st 2 0
313chk = ulong 0
314fl = ulong 0
315while lt get st 1 get st 0 bound 999999
316put st 2 add get st 2 1
317ln = get st 2
318put st 3 0
319put st 4 get st 1
320c = dpeek
321if eq c 72
322dadv
323res = drech msg dig ex
324chk = add chk 1
325if ne res 0
326fl = add fl 1
327dmsg get st 3 ln
328end
329else
330if eq c 80
331dadv
332res = drecp msg dig ex pre
333chk = add chk 1
334if ne res 0
335fl = add fl 1
336dmsg get st 3 ln
337end
338else
339if eq c 77
340dadv
341res = drecm msg dig ex pre
342chk = add chk 100
343fl = add fl res
344if eq get st 3 1
345fl = add fl 1
346dmsg 1 ln
347else
348if ne res 0
349dmsg 0 ln
350end
351end
352else
353if eq c 35
354dskip
355else
356if eq c 10
357dadv
358else
359if eq c 13
360dskip
361else
362chk = add chk 1
363fl = add fl 1
364dmsg 1 ln
365dskip
366end
367end
368end
369end
370end
371end
372if eq get st 3 1
373put st 1 get st 4
374dskip
375end
376end
377r = write 1 tsum 0 len tsum
378dprn chk
379r = write 1 tchk 0 len tchk
380dprn fl
381r = write 1 tfld 0 len tfld
382if ne fl 0
383ret 1
384end
385if eq chk 0
386ret 1
387end
388ret 0
389end