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 / aienux / crates / aienos-crypto / crypto_test.om

285 lines · 6177 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-crypto/crypto_test.om: Behavioral self-test for aienos-crypto.
3note Verifies FIPS 197 S-box & AES-256 C.2, RFC 8452 POLYVAL & AES-256-GCM-SIV Appendix C,
4note FIPS 180-4 SHA-256, and RFC 4231 HMAC-SHA256 Case 1.
5note Compose with: si/directives/om2.om om/tls/sha2.om om/tls/aesgcm.om om/tls/gcmsiv.om aienux/crates/aienos-crypto/crypto.om aienux/crates/aienos-crypto/crypto_test.om
6
7text crt_abc "abc"
8text crt_hi "Hi "
9text crt_there "There"
10
11fn crt_hex_nib c uchar to uchar
12if and ge c 48 le c 57
13ret sub c 48
14end
15if and ge c 97 le c 102
16ret add sub c 97 10
17end
18if and ge c 65 le c 70
19ret add sub c 65 10
20end
21ret 0
22end
23
24fn crt_from_hex src view uchar * dst edit uchar * to void
25n = div len src 2
26for i ulong n bound 256
27hi = crt_hex_nib get src mul i 2
28lo = crt_hex_nib get src add mul i 2 1
29put dst i or shl hi 4 lo
30end
31end
32
33text crt_aes_exp "8ea2b7ca516745bfeafc49904b496089"
34text crt_pv_a "66e94bd4ef8a2c3b884cfa59ca342b2e"
35text crt_pv_b "ff000000000000000000000000000000"
36text crt_pv_exp "ebe563401e7e91ea3ad6426b8140c394"
37text crt_pv_h "25629347589242761d31f826ba4b757b"
38text crt_pv_x1 "4f4f95668c83dfb6401762bb2d01a262"
39text crt_pv_x2 "d1a24ddd2721d006bbe45f20d3c9f362"
40text crt_pv_wexp "f7a3b47b846119fae5b7866cf5e5b77e"
41text crt_siv_k "0100000000000000000000000000000000000000000000000000000000000000"
42text crt_siv_n "030000000000000000000000"
43text crt_siv_pt1 "01000000000000000000000000000000"
44text crt_siv_exp1 "85a01b63025ba19b7fd3ddfc033b3e76c9eac6fa700942702e90862383c6c366"
45text crt_siv_pt2 "0200000000000000"
46text crt_siv_aad2 "01"
47text crt_siv_exp2 "1de22967237a813291213f267e3b452f02d01ae33e4ec854"
48text crt_sha_empty "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
49text crt_sha_abc "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
50text crt_hmac_exp "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
51
52fn crt_test_sbox_and_aes to int
53note Spot-check key FIPS 197 S-box entries and bijection across all 256 inputs
54if ne cr_sbox_ct 0x00 0x63
55ret 1
56end
57if ne cr_sbox_ct 0x01 0x7c
58ret 2
59end
60if ne cr_sbox_ct 0x53 0xed
61ret 3
62end
63if ne cr_sbox_ct 0x52 0x00
64ret 4
65end
66if ne cr_sbox_ct 0xff 0x16
67ret 5
68end
69seen = new uchar 256 0
70for b ulong 256
71s = ulong cr_sbox_ct uchar b
72if ne get seen s 0
73ret 6
74end
75put seen s 1
76end
77
78note NIST FIPS 197 Appendix C.2 (AES-256)
79key = new uchar 32 0
80for i ulong 32
81put key i uchar i
82end
83blk = new uchar 16 0
84for i ulong 16
85put blk i uchar mul i 0x11
86end
87exp = new uchar 16 0
88crt_from_hex crt_aes_exp exp
89if ne cr_aes256_encrypt_block view key blk CR_OK
90ret 7
91end
92if not cr_ct_eq view blk view exp
93ret 8
94end
95ret 0
96end
97
98fn crt_test_polyval to int
99note RFC 8452 Section 7 dot test vector
100a = new uchar 16 0
101b = new uchar 16 0
102exp = new uchar 16 0
103got = new uchar 16 0
104crt_from_hex crt_pv_a a
105crt_from_hex crt_pv_b b
106crt_from_hex crt_pv_exp exp
107if ne cr_polyval_dot view a view b got CR_OK
108ret 10
109end
110if not cr_ct_eq view got view exp
111ret 11
112end
113
114note RFC 8452 Appendix A worked example (2 blocks)
115h = new uchar 16 0
116x1 = new uchar 16 0
117x2 = new uchar 16 0
118wexp = new uchar 16 0
119acc = new uchar 16 0
120crt_from_hex crt_pv_h h
121crt_from_hex crt_pv_x1 x1
122crt_from_hex crt_pv_x2 x2
123crt_from_hex crt_pv_wexp wexp
124if ne cr_polyval_update_block view h acc view x1 CR_OK
125ret 12
126end
127if ne cr_polyval_update_block view h acc view x2 CR_OK
128ret 13
129end
130if not cr_ct_eq view acc view wexp
131ret 14
132end
133ret 0
134end
135
136fn crt_test_gcmsiv_vec2 kn view uchar 44 to int
137note RFC 8452 Appendix C: AES-256-GCM-SIV 8-byte PT, 1-byte AAD
138pt2 = new uchar 8 0
139aad2 = new uchar 1 0
140exp2 = new uchar 24 0
141ct2 = new uchar 24 0
142crt_from_hex crt_siv_pt2 pt2
143crt_from_hex crt_siv_aad2 aad2
144crt_from_hex crt_siv_exp2 exp2
145for i ulong 8
146put ct2 i get pt2 i
147end
148if ne cr_gcmsiv_seal_kn kn view aad2 ct2 CR_OK
149ret 28
150end
151if not cr_ct_eq view ct2 view exp2
152ret 29
153end
154if ne cr_gcmsiv_open_kn kn view aad2 ct2 CR_OK
155ret 30
156end
157if not cr_ct_eq view part ct2 0 8 view pt2
158ret 31
159end
160
161note Tamper detection + mandatory zeroing of output buffer on failure
162for i ulong 24
163put ct2 i get exp2 i
164end
165put ct2 0 xor get ct2 0 1
166if ne cr_gcmsiv_open_kn kn view aad2 ct2 CR_ERR_AUTH
167ret 32
168end
169for i ulong 8
170if ne get ct2 i 0
171ret 33
172end
173end
174ret 0
175end
176
177fn crt_test_gcmsiv to int
178key = new uchar 32 0
179nonce = new uchar 12 0
180crt_from_hex crt_siv_k key
181crt_from_hex crt_siv_n nonce
182
183note Key derivation determinism check
184ak1 = new uchar 16 0
185ek1 = new uchar 32 0
186ak2 = new uchar 16 0
187ek2 = new uchar 32 0
188if ne cr_gcmsiv_derive_keys view key view nonce ak1 ek1 CR_OK
189ret 20
190end
191if ne cr_gcmsiv_derive_keys view key view nonce ak2 ek2 CR_OK
192ret 21
193end
194if not cr_ct_eq view ak1 view ak2
195ret 22
196end
197if not cr_ct_eq view ek1 view ek2
198ret 23
199end
200
201note RFC 8452 Appendix C: AES-256-GCM-SIV 16-byte PT, 0-byte AAD
202kn = new uchar 44 0
203cr_gcmsiv_pack_kn view key view nonce kn
204pt1 = new uchar 16 0
205exp1 = new uchar 32 0
206ct1 = new uchar 32 0
207empty = new uchar 1 0
208crt_from_hex crt_siv_pt1 pt1
209crt_from_hex crt_siv_exp1 exp1
210for i ulong 16
211put ct1 i get pt1 i
212end
213if ne cr_gcmsiv_seal_kn view kn view part empty 0 0 ct1 CR_OK
214ret 24
215end
216if not cr_ct_eq view ct1 view exp1
217ret 25
218end
219if ne cr_gcmsiv_open_kn view kn view part empty 0 0 ct1 CR_OK
220ret 26
221end
222if not cr_ct_eq view part ct1 0 16 view pt1
223ret 27
224end
225
226ret crt_test_gcmsiv_vec2 view kn
227end
228
229fn crt_test_sha_hmac to int
230empty = new uchar 1 0
231dig = new uchar 32 0
232exp = new uchar 32 0
233
234note SHA-256("")
235crt_from_hex crt_sha_empty exp
236if ne cr_sha256 view part empty 0 0 dig CR_OK
237ret 40
238end
239if not cr_ct_eq view dig view exp
240ret 41
241end
242
243note SHA-256("abc")
244crt_from_hex crt_sha_abc exp
245if ne cr_sha256 crt_abc dig CR_OK
246ret 42
247end
248if not cr_ct_eq view dig view exp
249ret 43
250end
251
252note RFC 4231 Case 1 HMAC-SHA256
253hkey = new uchar 32 0
254for i ulong 20
255put hkey i 0x0b
256end
257crt_from_hex crt_hmac_exp exp
258if ne cr_hmac_sha256_2 view hkey crt_hi crt_there dig CR_OK
259ret 44
260end
261if not cr_ct_eq view dig view exp
262ret 45
263end
264ret 0
265end
266
267fn main to int
268rc = crt_test_sbox_and_aes
269if ne rc 0
270ret rc
271end
272rc = crt_test_polyval
273if ne rc 0
274ret rc
275end
276rc = crt_test_gcmsiv
277if ne rc 0
278ret rc
279end
280rc = crt_test_sha_hmac
281if ne rc 0
282ret rc
283end
284ret 0
285end