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.om

261 lines · 5885 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-crypto/crypto.om: Pure-Omega AIENOS Cryptographic Primitives.
3note Converted from aienux crates/aienos-crypto/src/{lib,aes,polyval,aes_gcm_siv,sha256}.rs.
4note Constant-time GF(2^8) AES S-box, FIPS 197 AES-256 block cipher, RFC 8452 POLYVAL,
5note RFC 8452 AES-256-GCM-SIV AEAD, FIPS 180-4 SHA-256, and RFC 4231 HMAC-SHA256.
6note Compose with: si/directives/om2.om om/tls/sha2.om om/tls/aesgcm.om om/tls/gcmsiv.om aienux/crates/aienos-crypto/crypto.om ...
7
8const CR_OK int 0
9const CR_ERR_AUTH int -1
10const CR_ERR_PARAM int -2
11
12const CR_KEY_LEN ulong 32
13const CR_NONCE_LEN ulong 12
14const CR_TAG_LEN ulong 16
15const CR_BLOCK_LEN ulong 16
16
17note Wipe a byte buffer to zero
18fn cr_wipe buf edit uchar * to void
19n = len buf
20for i ulong n bound 65536
21put buf i 0
22end
23end
24
25note Constant-time equality for two byte slices of equal length
26fn cr_ct_eq a view uchar * b view uchar * to bool
27if ne len a len b
28ret false
29end
30diff = uchar 0
31n = len a
32for i ulong n bound 65536
33diff = or diff xor get a i get b i
34end
35ret eq diff 0
36end
37
38note --- Constant-time GF(2^8) AES S-box (matches aes.rs sub_bytes_ct) ---
39
40fn cr_xtime x uchar to uchar
41hi = shr x 7
42mask = sub uchar 0 hi
43ret xor shl x 1 and uchar 0x1b mask
44end
45
46fn cr_gf_mul a uchar b uchar to uchar
47acc = uchar 0
48cur = a
49for i ulong 8 unroll
50bit = and shr b uchar i 1
51mask = sub uchar 0 bit
52acc = xor acc and cur mask
53cur = cr_xtime cur
54end
55ret acc
56end
57
58fn cr_rotl8 x uchar k uchar to uchar
59rk = sub uchar 8 k
60ret or shl x k shr x rk
61end
62
63note S(b) = Affine(b^254) in GF(2^8), branch-free and table-free
64fn cr_sbox_ct x uchar to uchar
65x2 = cr_gf_mul x x
66x3 = cr_gf_mul x2 x
67t = cr_gf_mul x3 x3
68x12 = cr_gf_mul t t
69t = cr_gf_mul x12 x3
70t = cr_gf_mul t t
71t = cr_gf_mul t t
72t = cr_gf_mul t t
73t = cr_gf_mul t t
74t = cr_gf_mul t x12
75inv = cr_gf_mul t x2
76acc = xor inv cr_rotl8 inv 1
77acc = xor acc cr_rotl8 inv 2
78acc = xor acc cr_rotl8 inv 3
79acc = xor acc cr_rotl8 inv 4
80ret xor acc uchar 0x63
81end
82
83note Single-block FIPS 197 AES-256 encryption via om/tls/aesgcm.om bitsliced AES
84fn cr_aes256_encrypt_block key view uchar * blk edit uchar * to int
85if or ne len key 32 ne len blk 16
86ret CR_ERR_PARAM
87end
88k = new ulong AGKN 0
89s = new ulong AGSN 0
90aes_setkey k s key
91hi = siv_be64 view blk 0
92lo = siv_be64 view blk 8
93put s 150 hi
94put s 151 lo
95put s 152 0
96put s 153 0
97put s 154 0
98put s 155 0
99put s 156 0
100put s 157 0
101aes_enc4 k s
102siv_put8 blk 0 get s 150
103siv_put8 blk 8 get s 151
104gcm_clear k s
105ret CR_OK
106end
107
108note --- Constant-time POLYVAL (RFC 8452 Section 3, exact port of polyval.rs) ---
109
110fn cr_polyval_div_x v edit uchar 16 to void
111lsb = and get v 0 1
112carry = uchar 0
113for idx ulong 16 unroll
114i = sub 15 idx
115cur = get v i
116next_carry = and cur 1
117put v i or shr cur 1 shl carry 7
118carry = next_carry
119end
120mask = sub uchar 0 lsb
121put v 15 xor get v 15 and uchar 0xe1 mask
122end
123
124fn cr_polyval_xor_masked v edit uchar 16 b view uchar 16 mask uchar to void
125for j ulong 16 unroll
126put v j xor get v j and get b j mask
127end
128end
129
130note dot(a, b) = a * b * x^-128 mod (x^128 + x^127 + x^126 + x^121 + 1)
131fn cr_polyval_dot a view uchar 16 b view uchar 16 dst edit uchar 16 to int
132v = new uchar 16 0
133for i ulong 128 bound 128
134ab = get a div i 8
135sh = uchar rem i 8
136bit = and shr ab sh 1
137mask = sub uchar 0 bit
138cr_polyval_xor_masked v b mask
139cr_polyval_div_x v
140end
141for j ulong 16 unroll
142put dst j get v j
143put v j 0
144end
145ret CR_OK
146end
147
148fn cr_polyval_update_block h view uchar 16 acc edit uchar 16 blk view uchar 16 to int
149tmp = new uchar 16 0
150for i ulong 16 unroll
151put tmp i xor get acc i get blk i
152end
153rc = cr_polyval_dot view tmp h acc
154for i ulong 16 unroll
155put tmp i 0
156end
157ret rc
158end
159
160note --- AES-256-GCM-SIV (RFC 8452, wrapping om/tls/gcmsiv.om) ---
161
162fn cr_gcmsiv_derive_keys key view uchar 32 nonce view uchar 12 auth_out edit uchar 16 enc_out edit uchar 32 to int
163kn = new uchar 44 0
164for i ulong 32
165put kn i get key i
166end
167for i ulong 12
168put kn add 32 i get nonce i
169end
170k = new ulong AGKN 0
171s = new ulong AGSN 0
172ek = new uchar 32 0
173siv_derive k s view kn 32 ek
174siv_put8 auth_out 0 get k 131
175siv_put8 auth_out 8 get k 132
176for i ulong 32
177put enc_out i get ek i
178put ek i 0
179end
180for i ulong 44
181put kn i 0
182end
183gcm_clear k s
184ret CR_OK
185end
186
187note Pack key(32) || nonce(12) into kn(44)
188fn cr_gcmsiv_pack_kn key view uchar 32 nonce view uchar 12 kn edit uchar 44 to void
189for i ulong 32
190put kn i get key i
191end
192for i ulong 12
193put kn add 32 i get nonce i
194end
195end
196
197note Encrypt in-place buf = pt || 16B room using kn = key(32) || nonce(12)
198fn cr_gcmsiv_seal_kn kn view uchar 44 aad view uchar * buf edit uchar * to int
199if lt len buf 16
200ret CR_ERR_PARAM
201end
202rc = aes_gcmsiv_seal kn aad buf
203if ne rc 0
204ret CR_ERR_PARAM
205end
206ret CR_OK
207end
208
209note Decrypt in-place buf = ct || tag using kn = key(32) || nonce(12); zeroes buf on auth failure
210fn cr_gcmsiv_open_kn kn view uchar 44 aad view uchar * buf edit uchar * to int
211if lt len buf 16
212cr_wipe buf
213ret CR_ERR_AUTH
214end
215rc = aes_gcmsiv_open kn aad buf
216if ne rc 0
217cr_wipe buf
218ret CR_ERR_AUTH
219end
220ret CR_OK
221end
222
223note --- SHA-256 and HMAC-SHA256 (wrapping om/tls/sha2.om) ---
224
225fn cr_sha256 msg view uchar * digest edit uchar 32 to int
226dl = sha2_digest 256 msg digest
227if ne dl 32
228ret CR_ERR_PARAM
229end
230ret CR_OK
231end
232
233note Two-part HMAC-SHA256 for 32-byte key (matches sha256.rs hmac_sha256)
234fn cr_hmac_sha256_2 key view uchar 32 part1 view uchar * part2 view uchar * digest edit uchar 32 to int
235ipad = new uchar 64 0x36
236opad = new uchar 64 0x5c
237for i ulong 32
238kb = get key i
239put ipad i xor get ipad i kb
240put opad i xor get opad i kb
241end
242ctx = new ulong 40 0
243sha256_init ctx
244sha256_update ctx view ipad
245sha256_update ctx part1
246sha256_update ctx part2
247inner = new uchar 32 0
248sha256_final ctx inner
249sha256_init ctx
250sha256_update ctx view opad
251sha256_update ctx view inner
252sha256_final ctx digest
253for i ulong 64
254put ipad i 0
255put opad i 0
256end
257for i ulong 32
258put inner i 0
259end
260ret CR_OK
261end