interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / tests / drv / aesgcm.om

332 lines · 7562 bytes Raw Blame History
1note omtls test driver aesgcm: AES-128/256 (FIPS 197 C.1/C.3 block vectors and an S-box check of all 256 inputs
2note against a direct GF(2^8) reference), AES-GCM through ot_ae_seal / ot_ae_open (alg 1, 2) on the NIST CAVP
3note gcmEncryptExtIV / gcmDecrypt files (the subset with 96-bit IV, 128-bit tag, 128/256-bit keys; other groups
4note are skipped and not counted) and Wycheproof "W TCID V|I KEY IV AAD MSG CT TAG" (cases with a 12-byte IV and
5note a 16- or 32-byte key; other IV and key sizes are skipped and not counted). Usage: aesgcm FILE...
6note A line "S" in any file (tests/vectors/aesgcm/sbox-check.txt) runs the S-box check as one case.
7text ot_syg_area "aesgcm"
8text ot_syg_b "B"
9text ot_syg_w "W"
10text ot_syg_s "S"
11text ot_syg_cnt "Count"
12text ot_syg_key "Key"
13text ot_syg_iv "IV"
14text ot_syg_pt "PT"
15text ot_syg_aad "AAD"
16text ot_syg_ct "CT"
17text ot_syg_tag "Tag"
18text ot_syg_fail "FAIL"
19text ot_syg_kl "[Keylen"
20text ot_syg_ivl "[IVlen"
21text ot_syg_tl "[Taglen"
22note Reference S-box (public, slow, test only): inverse by search, then the FIPS 197 5.1.1 affine map.
23fn ot_syg_gm a ulong b ulong to ulong
24r = ulong 0
25x = a
26y = b
27for i ulong 8
28if ne and y 1 0
29r = xor r x
30end
31x = shl x 1
32if ne and x 256 0
33x = xor x 283
34end
35y = shr y 1
36end
37ret r
38end
39fn ot_syg_ref v ulong to ulong
40inv = ulong 0
41for c ulong 256
42if eq ot_syg_gm v c 1
43inv = c
44end
45end
46r = ulong 99
47for k ulong 8
48bit = xor shr inv k shr inv and add k 4 7
49bit = xor bit shr inv and add k 5 7
50bit = xor bit shr inv and add k 6 7
51bit = and xor bit shr inv and add k 7 7 1
52r = xor r shl bit k
53end
54ret r
55end
56fn ot_syg_sbox to bool
57s = new ulong 64 0
58w = new ulong 64 0
59ok = true
60for q ulong 4
61for i ulong 64
62put s i add mul q 64 i
63end
64ot_aes_sb s w
65for i ulong 64
66if ne get s i ot_syg_ref add mul q 64 i
67ok = false
68end
69end
70end
71ret ok
72end
73note B KEY PT CT (AES-192 records are skipped: st 9 = 1 means not counted).
74fn ot_syg_blk b view uchar * st edit ulong 10 to bool
75k = new uchar 32 0
76p = new uchar 16 0
77c = new uchar 16 0
78o = new uchar 16 0
79kl = ot_sy_nhex b st k
80pl = ot_sy_nhex b st p
81cl = ot_sy_nhex b st c
82if eq kl 24
83put st 9 1
84ret true
85end
86if or ne pl 16 ne cl 16
87ret false
88end
89if eq kl 16
90ot_aes_enc1 false k p o
91else
92if ne kl 32
93ret false
94end
95ot_aes_enc1 true k p o
96end
97ret ot_sy_eq o c 16
98end
99note The tail of a (from 49152) holds the record's key (64 bytes), IV (1024) and tag (64); aad stays below 49152.
100note kn = key || IV (AES-128 keys use bytes 0..15).
101fn ot_syg_kn a view uchar 65536 kn edit uchar 44 to void
102for i ulong 32
103put kn i get a add 49152 i
104end
105for i ulong 12
106put kn add 32 i get a add 49216 i
107end
108end
109note W TCID V|I KEY IV AAD MSG CT TAG. g: 0 key, 1 iv, 2 aad, 3 msg, 4 ct, 5 tag lengths, 6 valid.
110fn ot_syg_wy b view uchar * st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool
111g = new long 8 0
112n = ot_sy_tok b st
113n = ot_sy_tok b st
114put g 6 0
115if eq get b get st 6 86
116put g 6 1
117end
118put g 0 ot_sy_nhex b st part a 49152 64
119put g 1 ot_sy_nhex b st part a 49216 1024
120put g 2 ot_sy_nhex b st part a 0 49152
121put g 3 ot_sy_nhex b st p
122put g 4 ot_sy_nhex b st part c 0 65520
123if lt get g 4 0
124ret false
125end
126cl = ulong get g 4
127put g 5 ot_sy_nhex b st part c cl 16
128put g 7 get g 6
129sk = and ne get g 0 16 ne get g 0 32
130if or sk ne get g 1 12
131put st 9 1
132ret true
133end
134ret ot_syg_go a p c g
135end
136note Seal/open one AES-GCM case: g as above; g 7 1 adds the derived negative checks.
137fn ot_syg_go a edit uchar 65536 p view uchar 65536 c view uchar 65536 g view long 8 to bool
138kn = new uchar 44 0
139f = new ulong 8 0
140bad = or lt get g 2 0 lt get g 3 0
141bad = or bad or lt get g 4 0 ne get g 5 16
142if bad
143ret false
144end
145ot_syg_kn a kn
146put f 0 ulong get g 2
147put f 1 ulong get g 3
148put f 2 add ulong get g 4 16
149put f 3 ulong get g 6
150put f 4 ulong get g 7
151alg = ulong 1
152if eq get g 0 32
153alg = 2
154end
155ret ot_sye_run alg kn a p c f
156end
157note CAVP state, kept as 8-byte little-endian values in a from 51200 (test metadata only): 0 Keylen, 1 IVlen,
158note 2 Taglen (bits), 3 PT seen, 4 Tag seen, 5 key, 6 iv, 7 pt, 8 aad, 9 ct, 10 tag byte lengths, 11 FAIL record.
159fn ot_syg_cg a view uchar 65536 i ulong to long
160v = ulong 0
161o = add 51200 mul i 8
162for j ulong 8
163x = ulong get a add o j
164v = or v shl x mul j 8
165end
166ret bitcast long v
167end
168fn ot_syg_cp a edit uchar 65536 i ulong v long to void
169u = bitcast ulong v
170o = add 51200 mul i 8
171for j ulong 8
172x = and shr u mul j 8 255
173put a add o j uchar x
174end
175end
176note The value after "NAME =" as hex into o; an empty value is 0 bytes.
177fn ot_syg_val b view uchar * st edit ulong 10 o edit uchar * to long
178n = ot_sy_tok b st
179r = ot_sy_nhex b st o
180if eq r -3
181ret 0
182end
183ret r
184end
185fn ot_syg_hdr b view uchar * st edit ulong 10 a edit uchar 65536 to void
186k = ulong 9
187if ot_sy_is b st ot_syg_kl
188k = 0
189end
190if ot_sy_is b st ot_syg_ivl
191k = 1
192end
193if ot_sy_is b st ot_syg_tl
194k = 2
195end
196n = ot_sy_tok b st
197v = long ot_sy_ndec b st
198if lt k 3
199ot_syg_cp a k v
200end
201end
202note Evaluate a complete CAVP record (subset: 128/256-bit key, 96-bit IV, 128-bit tag; else not counted).
203fn ot_syg_cav st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool
204g = new long 8 0
205kl = ot_syg_cg a 0
206if or ne ot_syg_cg a 1 96 or ne ot_syg_cg a 2 128 and ne kl 128 ne kl 256
207put st 9 1
208ret true
209end
210put g 0 ot_syg_cg a 5
211put g 1 ot_syg_cg a 6
212put g 2 ot_syg_cg a 8
213put g 4 ot_syg_cg a 9
214put g 3 get g 4
215if eq ot_syg_cg a 11 0
216put g 3 ot_syg_cg a 7
217put g 6 1
218end
219put g 5 ot_syg_cg a 10
220if or lt get g 4 0 gt get g 4 65520
221ret false
222end
223cl = ulong get g 4
224for i ulong 16
225put c add cl i get a add 50240 i
226end
227ret ot_syg_go a p c g
228end
229note One line. Gives true when it closed a case; st 9 = 1 for a case that is not counted.
230fn ot_syg_line b view uchar * st edit ulong 10 a edit uchar 65536 p edit uchar 65536 c edit uchar 65536 to bool
231if ot_sy_is b st ot_syg_s
232ret ot_syg_sbox
233end
234if ot_sy_is b st ot_syg_b
235ret ot_syg_blk b st
236end
237if ot_sy_is b st ot_syg_w
238ret ot_syg_wy b st a p c
239end
240put st 9 1
241if eq get b get st 6 91
242ot_syg_hdr b st a
243ret true
244end
245if ot_sy_is b st ot_syg_cnt
246ot_syg_cp a 3 0
247ot_syg_cp a 4 0
248ot_syg_cp a 11 0
249ret true
250end
251if ot_sy_is b st ot_syg_key
252ot_syg_cp a 5 ot_syg_val b st part a 49152 64
253ret true
254end
255if ot_sy_is b st ot_syg_iv
256ot_syg_cp a 6 ot_syg_val b st part a 49216 1024
257ret true
258end
259if ot_sy_is b st ot_syg_aad
260ot_syg_cp a 8 ot_syg_val b st part a 0 49152
261ret true
262end
263if ot_sy_is b st ot_syg_ct
264ot_syg_cp a 9 ot_syg_val b st part c 0 65520
265ret true
266end
267if ot_sy_is b st ot_syg_pt
268ot_syg_cp a 7 ot_syg_val b st p
269ot_syg_cp a 3 1
270if eq ot_syg_cg a 4 1
271put st 9 0
272ret ot_syg_cav st a p c
273end
274ret true
275end
276if ot_sy_is b st ot_syg_tag
277ot_syg_cp a 10 ot_syg_val b st part a 50240 64
278ot_syg_cp a 4 1
279if eq ot_syg_cg a 3 1
280put st 9 0
281ret ot_syg_cav st a p c
282end
283ret true
284end
285if ot_sy_is b st ot_syg_fail
286ot_syg_cp a 11 1
287put st 9 0
288ret ot_syg_cav st a p c
289end
290ret true
291end
292fn ot_syg_file fd int b edit uchar * r edit ulong 4 to void
293st = new ulong 10 0
294a = new uchar 65536 0
295p = new uchar 65536 0
296c = new uchar 65536 0
297while ot_sy_line fd b st bound 9223372036854775807
298n = ot_sy_tok b st
299if gt n 0
300if ne get b get st 6 35
301put st 9 0
302ok = ot_syg_line b st a p c
303if eq get st 9 0
304ot_sy_res r ok ot_syg_area st
305end
306end
307end
308end
309if eq get st 2 2
310put r 1 add get r 1 1
311end
312end
313fn main to int
314b = new uchar 1048576 0
315pth = new uchar 4096 0
316r = new ulong 4 0
317st = new ulong 10 0
318na = argcnt
319i = ulong 1
320while lt i ulong na bound 4096
321fd = ot_sy_open i pth
322if lt fd 0
323put r 1 add get r 1 1
324ot_sy_bad ot_syg_area i st
325else
326ot_syg_file fd b r
327cl = fclose fd
328end
329i = add i 1
330end
331ret ot_sy_done ot_syg_area get r 0 get r 1
332end