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

429 lines · 11260 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: OMEGA-CRYPTO-REUSE-1 gates 1 and 2 (issue #639), keystream reuse on synthetic inputs.
3note Built as `om2`, om/tls/chacha.om, om/tls/aesgcm.om, om/tls/gcmsiv.om, driver.om (om/tls/test/reuse/run.sh).
4note Stdin: lines; empty lines and lines starting with # are skipped; exactly one line
5note X HEX HEX = 160 hex digits, the 80 bytes of P1 XOR P2, computed outside this program
6note All inputs are synthetic (no real key, no real payload), 80 bytes so that ChaCha20 crosses a
7note 64-byte block and AES-GCM uses five counter blocks:
8note P1[i] = (7 i + 3) mod 256 P2[i] = (13 i + 101) mod 256 i = 0..79
9note K1[i] = (29 i + 17) mod 256 K2[i] = (31 i + 5) mod 256 synthetic keystreams
10note ChaCha20-Poly1305 kn = key || nonce, byte i = i (44 bytes); the distinct-nonce kn flips bit 0
11note of the last nonce byte. AES-128-GCM and AES-128-GCM-SIV kn byte i = (3 i + 1) mod 256
12note (28 bytes), same flip. Mode 2 derives its keystream from the tag, so a repeated nonce
13note does not disclose P1 XOR P2 (RFC 8452).
14note aad = "abcd". Tags (16 bytes) are never part of the reuse comparison.
15note The detector kreuse says "reuse" when C1 XOR C2 equals P1 XOR P2 over the ciphertext bytes:
16note with both plaintexts known, equal XORs mean the two keystreams were equal there. A primitive
17note API accepts a repeated nonce (RFC 8439 4, SP 800-38D 8): nothing here expects it to refuse.
18note Checks (numbered as printed):
19note 1 synthetic: (P1^K1) ^ (P2^K1) == X (independent expected bytes)
20note 2 synthetic: detector flags reuse for one keystream K1
21note 3 synthetic: detector does not flag K1 against K2 (distinct-keystream control)
22note then for m = 0 (ChaCha20-Poly1305, checks 4..11) and m = 1 (AES-128-GCM, checks 12..19):
23note +0 seal P1 and P2 under one kn, and P2 under the distinct-nonce kn: all three return 0
24note +1 ct1 ^ ct2 == X over the 80 ciphertext bytes (payload XOR disclosed)
25note +2 detector flags reuse for the repeated nonce
26note +3 detector does not flag the distinct nonce (negative control)
27note +4 the two tags under the repeated nonce differ (why tags are excluded)
28note +5 open(ct1) under the repeated nonce returns 0 and P1 (authentication still holds)
29note +6 ct2 with one ciphertext bit flipped: open returns 1 and zeroes the data part
30note +7 the distinct-nonce sealed P2 opened with the first kn returns 1 (wrong nonce refused)
31note then mode 2 (AES-128-GCM-SIV, checks 20..27):
32note 20 the three seals return 0
33note 21 mode-2 ciphertext of P1 differs from the mode-1 ciphertext of the same kn and P1
34note 22 ct1 ^ ct2 != X over the 80 ciphertext bytes (nonce misuse does not disclose XOR)
35note 23 detector does not flag the repeated nonce
36note 24 the two tags under the repeated nonce differ
37note 25 open(ct1) returns 0 and restores P1
38note 26 ct2 with one other ciphertext bit flipped: open returns 1 and zeroes the data
39note 27 the distinct-nonce sealed P2 opened with the first kn returns 1
40note Prints "MISMATCH check N" per failed check, "BAD input" for a malformed X line, then
41note "reuse driver: C checks, F failed". Exit 0 only when F is 0 and C is 27; 1 otherwise; 2 when
42note stdin cannot be read or is over 4096 bytes.
43
44const RLEN ulong 80
45const SLEN ulong 96
46note offsets in the work buffer w (one buffer keeps main within 16 live values)
47const OP1 ulong 0
48const OP2 ulong 96
49const OK1 ulong 192
50const OK2 ulong 288
51const OC1 ulong 384
52const OC2 ulong 480
53const OC3 ulong 576
54const OT ulong 672
55const OX ulong 768
56const ON1 ulong 864
57const ON2 ulong 928
58const OAD ulong 992
59const WLEN ulong 1024
60
61global inp uchar 4096
62global st ulong 4
63global ob uchar 32
64global one uchar 1
65text tmis "MISMATCH check "
66text tbad "BAD input\n"
67text tsum "reuse driver: "
68text tchk " checks, "
69text tfld " failed\n"
70text tnl "\n"
71text tio "reuse driver: cannot read input or input over 4096 bytes\n"
72
73note st: 0 input length, 1 checks, 2 failed
74
75fn dslurp to int
76t = ulong 0
77while true bound 4097
78room = sub len inp t
79if eq room 0
80r = read 0 one 0 1
81if ne r 0
82ret 2
83end
84break
85end
86r = read 0 inp t room
87if lt r 0
88ret 2
89end
90if eq r 0
91break
92end
93t = add t ulong r
94end
95put st 0 t
96ret 0
97end
98
99fn dhexv c ulong to ulong
100if ge c 48
101if le c 57
102ret sub c 48
103end
104end
105if ge c 97
106if le c 102
107ret sub c 87
108end
109end
110if ge c 65
111if le c 70
112ret sub c 55
113end
114end
115ret 16
116end
117
118note find the one "X " line and decode its 160 hex digits into x; 0 ok, 1 malformed or not one line
119fn dparse x edit uchar * to ulong
120n = get st 0
121p = ulong 0
122seen = ulong 0
123bad = ulong 0
124while lt p n bound 4097
125c = ulong get inp p
126q = p
127while lt q n bound 4097
128if eq get inp q 10
129break
130end
131q = add q 1
132end
133if eq c 88
134seen = add seen 1
135bad = or bad dxline x p q
136else
137if and ne c 35 ne q p
138bad = 1
139end
140end
141p = add q 1
142end
143if ne seen 1
144bad = 1
145end
146ret bad
147end
148
149note decode the X line inp[p .. q) into x: 0 ok, 1 malformed
150fn dxline x edit uchar * p ulong q ulong to ulong
151if ne sub q p 162
152ret 1
153end
154if ne get inp add p 1 32
155ret 1
156end
157bad = ulong 0
158for i ulong RLEN
159o = add p mul i 2
160hi = dhexv ulong get inp add o 2
161lo = dhexv ulong get inp add o 3
162if or gt hi 15 gt lo 15
163bad = 1
164else
165put x i uchar or shl hi 4 lo
166end
167end
168ret bad
169end
170
171fn dprn v ulong to void
172x = v
173k = ulong 0
174while true bound 21
175put ob sub 31 k uchar add 48 rem x 10
176k = add k 1
177x = div x 10
178if eq x 0
179break
180end
181end
182r = write 1 ob sub 32 k k
183end
184
185fn dchk ok bool id ulong to void
186put st 1 add get st 1 1
187if not ok
188put st 2 add get st 2 1
189r = write 1 tmis 0 len tmis
190dprn id
191r = write 1 tnl 0 1
192end
193end
194
195note the reuse detector over the work buffer: true when w[a..] ^ w[b..] == w[p..] ^ w[q..] over the
196note first n bytes (a, b ciphertexts; p, q the known plaintexts)
197fn kreuse w view uchar WLEN a ulong b ulong p ulong q ulong n ulong to bool
198acc = uchar 0
199for i ulong n
200c = xor get w add a i get w add b i
201acc = or acc xor c xor get w add p i get w add q i
202end
203ret not ne acc 0
204end
205
206note true when w[a..] ^ w[b..] == w[x..] over the first n bytes
207fn dxeq w view uchar WLEN a ulong b ulong x ulong n ulong to bool
208acc = uchar 0
209for i ulong n
210acc = or acc xor xor get w add a i get w add b i get w add x i
211end
212ret eq acc 0
213end
214
215note true when w[a..] and w[b..] differ somewhere in their first n bytes
216fn ddiff w view uchar WLEN a ulong b ulong n ulong to bool
217acc = uchar 0
218for i ulong n
219acc = or acc xor get w add a i get w add b i
220end
221ret ne acc 0
222end
223
224note copy w[s .. s + n) to w[d .. d + n)
225fn dmove w edit uchar WLEN d ulong s ulong n ulong to void
226for i ulong n
227put w add d i get w add s i
228end
229end
230
231note true when the first n bytes of a are 0
232fn dzero a view uchar * n ulong to bool
233acc = uchar 0
234for i ulong n
235acc = or acc get a i
236end
237ret eq acc 0
238end
239
240fn dcopy d edit uchar * s view uchar * to void
241for i ulong len s
242put d i get s i
243end
244end
245
246note synthetic plaintexts, keystreams, aad and the kn pair of mode m (0 ChaCha, 1 AES-GCM, 2 AES-GCM-SIV)
247fn dgen w edit uchar WLEN m ulong to ulong
248for i ulong SLEN
249put w add OP1 i 0
250put w add OP2 i 0
251end
252for i ulong RLEN
253put w add OP1 i uchar rem add mul 7 i 3 256
254put w add OP2 i uchar rem add mul 13 i 101 256
255put w add OK1 i uchar rem add mul 29 i 17 256
256put w add OK2 i uchar rem add mul 31 i 5 256
257end
258put w add OAD 0 97
259put w add OAD 1 98
260put w add OAD 2 99
261put w add OAD 3 100
262nl = ulong 44
263if eq m 1
264nl = 28
265end
266if eq m 2
267nl = 28
268end
269for i ulong nl
270v = uchar i
271if eq m 1
272v = uchar rem add mul 3 i 1 256
273end
274if eq m 2
275v = uchar rem add mul 3 i 1 256
276end
277put w add ON1 i v
278put w add ON2 i v
279end
280last = add ON2 sub nl 1
281put w last xor get w last 1
282ret nl
283end
284
285note seal / open w[o .. o + SLEN) in place under kn = w[k .. k + nl) and aad = w[OAD .. OAD + 4);
286note kn and aad are copied to local arrays first (one array may not be passed twice in a call)
287fn dseal m ulong w edit uchar WLEN k ulong nl ulong o ulong to ulong
288kb = new uchar 44 0
289ad = new uchar 4 0
290dcopy part kb 0 nl part w k nl
291dcopy ad part w OAD 4
292if eq m 2
293ret aes_gcmsiv_seal part kb 0 nl ad part w o SLEN
294end
295if eq m 1
296ret aead_aes128gcm_seal part kb 0 nl ad part w o SLEN
297end
298ret aead_chacha_seal part kb 0 nl ad part w o SLEN
299end
300
301fn dopen m ulong w edit uchar WLEN k ulong nl ulong o ulong to ulong
302kb = new uchar 44 0
303ad = new uchar 4 0
304dcopy part kb 0 nl part w k nl
305dcopy ad part w OAD 4
306if eq m 2
307ret aes_gcmsiv_open part kb 0 nl ad part w o SLEN
308end
309if eq m 1
310ret aead_aes128gcm_open part kb 0 nl ad part w o SLEN
311end
312ret aead_chacha_open part kb 0 nl ad part w o SLEN
313end
314
315note gate 1: one synthetic keystream reused, then two distinct ones
316fn dsynth w edit uchar WLEN to void
317for i ulong RLEN
318put w add OC1 i xor get w add OP1 i get w add OK1 i
319put w add OC2 i xor get w add OP2 i get w add OK1 i
320put w add OC3 i xor get w add OP2 i get w add OK2 i
321end
322dchk dxeq w OC1 OC2 OX RLEN 1
323dchk kreuse w OC1 OC2 OP1 OP2 RLEN 2
324dchk not kreuse w OC1 OC3 OP1 OP2 RLEN 3
325end
326
327note gate 2 for mode m, checks b .. b + 7
328fn daead w edit uchar WLEN m ulong b ulong to void
329nl = dgen w m
330dmove w OC1 OP1 SLEN
331dmove w OC2 OP2 SLEN
332dmove w OC3 OP2 SLEN
333r1 = dseal m w ON1 nl OC1
334r2 = dseal m w ON1 nl OC2
335r3 = dseal m w ON2 nl OC3
336dchk eq or or r1 r2 r3 0 b
337dchk dxeq w OC1 OC2 OX RLEN add b 1
338dchk kreuse w OC1 OC2 OP1 OP2 RLEN add b 2
339dchk not kreuse w OC1 OC3 OP1 OP2 RLEN add b 3
340dchk ddiff w add OC1 RLEN add OC2 RLEN 16 add b 4
341dmove w OT OC1 SLEN
342r4 = dopen m w ON1 nl OT
343dchk and eq r4 0 not ddiff w OT OP1 RLEN add b 5
344dmove w OT OC2 SLEN
345put w add OT 5 xor get w add OT 5 1
346r5 = dopen m w ON1 nl OT
347dchk and eq r5 1 dzero part w OT RLEN RLEN add b 6
348dmove w OT OC3 SLEN
349r6 = dopen m w ON1 nl OT
350dchk eq r6 1 add b 7
351end
352
353note gate 3: AES-128-GCM-SIV. Check 21 is the siv-as-other-mode site: only the first seal is mode 2.
354fn dsiv_seals w edit uchar WLEN nl ulong to void
355dmove w OC1 OP1 SLEN
356dmove w OC2 OP2 SLEN
357dmove w OC3 OP2 SLEN
358r1 = dseal 2 w ON1 nl OC1
359r2 = dseal 2 w ON1 nl OC2
360r3 = dseal 2 w ON2 nl OC3
361dchk eq or or r1 r2 r3 0 20
362end
363
364fn dsiv_notgcm w edit uchar WLEN nl ulong to void
365dmove w OT OC1 SLEN
366dmove w OC1 OP1 SLEN
367r1 = dseal 1 w ON1 nl OC1
368dchk ddiff w OT OC1 RLEN 21
369dmove w OC1 OP1 SLEN
370r2 = dseal 2 w ON1 nl OC1
371end
372
373fn dsiv_misuse w edit uchar WLEN to void
374dchk not dxeq w OC1 OC2 OX RLEN 22
375dchk not kreuse w OC1 OC2 OP1 OP2 RLEN 23
376dchk ddiff w add OC1 RLEN add OC2 RLEN 16 24
377end
378
379fn dsiv_auth w edit uchar WLEN nl ulong to void
380dmove w OT OC1 SLEN
381r4 = dopen 2 w ON1 nl OT
382dchk and eq r4 0 not ddiff w OT OP1 RLEN 25
383dmove w OT OC2 SLEN
384put w add OT 9 xor get w add OT 9 1
385r5 = dopen 2 w ON1 nl OT
386dchk and eq r5 1 dzero part w OT RLEN RLEN 26
387dmove w OT OC3 SLEN
388r6 = dopen 2 w ON1 nl OT
389dchk eq r6 1 27
390end
391
392fn dsiv w edit uchar WLEN to void
393nl = dgen w 2
394dsiv_seals w nl
395dsiv_notgcm w nl
396dsiv_misuse w
397dsiv_auth w nl
398end
399
400fn main to int
401w = new uchar WLEN 0
402if ne dslurp 0
403r = write 2 tio 0 len tio
404ret 2
405end
406put st 1 0
407put st 2 0
408if ne dparse part w OX RLEN 0
409r = write 1 tbad 0 len tbad
410put st 2 add get st 2 1
411end
412nl = dgen w 0
413dsynth w
414daead w 0 4
415daead w 1 12
416dsiv w
417r = write 1 tsum 0 len tsum
418dprn get st 1
419r = write 1 tchk 0 len tchk
420dprn get st 2
421r = write 1 tfld 0 len tfld
422if ne get st 2 0
423ret 1
424end
425if ne get st 1 27
426ret 1
427end
428ret 0
429end