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

348 lines · 10439 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note gcmsiv.om: AES-GCM-SIV (RFC 8452) for om/tls, module code GS. It reuses the AES block cipher
3note and the masked GF(2^128) multiply of aesgcm.om, which must come before it in the unit.
4note Library unit: no om2 line, no fn main, no mutable globals. Not part of the TLS client build;
5note programs that need it compose `om2`, aesgcm.om, gcmsiv.om, then the program.
6note
7note API (return codes: 0 ok, 1 authentication failure (open only), 2 bad parameters):
8note aes_gcmsiv_seal kn aad buf / aes_gcmsiv_open kn aad buf
9note AEAD_AES_128_GCM_SIV and AEAD_AES_256_GCM_SIV (RFC 8452 section 6): kn = key-generating
10note key (16 or 32 bytes) || nonce (12 bytes), so len kn is 28 or 44.
11note seal: buf = plaintext || 16 bytes of room; becomes ciphertext || tag. The same key, nonce,
12note aad and plaintext always give the same output (the scheme is deterministic by design).
13note open: buf = ciphertext || tag. The ciphertext is decrypted in place, the tag is recomputed
14note over the result and compared with an or-accumulated comparison of both 64-bit halves; on a
15note mismatch buf[0 .. n) is zeroed before returning 1, so no unauthenticated plaintext is
16note released (RFC 8452 section 5).
17note 2 (nothing written) when len kn is not 28 or 44, len buf < 16, or the plaintext or aad is
18note longer than 2^36 bytes (P_MAX, A_MAX).
19note
20note Construction (RFC 8452 sections 3 to 5):
21note keys: AES(K, LE32(i) || nonce)[0 .. 8) for i = 0, 1 (authentication key) and 2, 3 (and 4, 5
22note for AES-256) (encryption key).
23note POLYVAL through GHASH (RFC 8452 Appendix A): POLYVAL(H, X1 .. Xn) =
24note ByteReverse(GHASH(mulX_GHASH(ByteReverse(H)), ByteReverse(X1) .. ByteReverse(Xn))), with
25note gcm_mul of aesgcm.om as the GHASH multiply. Inputs: pad16(aad), pad16(plaintext), then the
26note length block LE64(8 len aad) || LE64(8 len plaintext).
27note tag: AES(encryption key, (S ^ nonce) with bit 7 of byte 15 cleared).
28note CTR: the counter block is the tag with bit 7 of byte 15 set; bytes 0..3 count as a little-endian
29note 32-bit integer that wraps at 2^32.
30note
31note Constant time on the same terms as aesgcm.om: no array is indexed by a secret and every branch
32note and loop count depends only on lengths, the key size and the tag-check outcome. Secret
33note workspaces (round keys, derived keys, H, state) are zeroed before the entry points return.
34note
35note Workspace k (ulong AGKN) cells, beyond those aes_setkey and gcm_mul use (0..120, 121..124):
36note 125,126 POLYVAL result, then the masked value, then the tag; 127 bytes 4..7 of the counter
37note block; 128 bytes 8..15 of the counter block; 129 the 32-bit counter (bytes 0..3, little-endian);
38note 131,132 the message-authentication key; 133,134 the nonce (bytes 0..7, bytes 8..11 then zeros).
39note 128-bit values are two big-endian halves (hi = bytes 0..7), as in aesgcm.om.
40
41note all ones except bit 7 of byte 15 of a 128-bit value (the low byte of its low half)
42const GSMSK ulong 0xffffffffffffff7f
43note 2^36: P_MAX and A_MAX of RFC 8452 section 6
44const GSMAX ulong 0x1000000000
45
46note d[o .. o + 8) read big-endian
47fn siv_be64 d view uchar * o ulong to ulong
48x = ulong 0
49for i ulong 8 unroll
50x = or shl x 8 ulong get d add o i
51end
52ret x
53end
54
55note d[o .. o + 8) read little-endian
56fn siv_le64 d view uchar * o ulong to ulong
57x = ulong 0
58for i ulong 8 unroll
59x = or shl x 8 ulong get d add o sub 7 i
60end
61ret x
62end
63
64note x written big-endian to d[o .. o + 8)
65fn siv_put8 d edit uchar * o ulong x ulong to void
66for i ulong 8 unroll
67put d add o i uchar and shr x sub 56 shl i 3 0xff
68end
69end
70
71note byte order of the low 32 bits of x reversed
72fn siv_bsw32 x ulong to ulong inline
73ret shr aes_bswap x 32
74end
75
76note the key-derivation blocks LE32(c + b) || nonce, b = 0..3, into s[150..158); the nonce is
77note kn[kl .. kl + 12)
78fn siv_dblk s edit ulong AGSN kn view uchar * kl ulong c ulong to void
79h = siv_be64 kn kl
80l = siv_be64 kn add kl 4
81for b ulong 4 unroll
82put s add 150 mul b 2 or shl add c b 56 shr h 32
83put s add 151 mul b 2 l
84end
85end
86
87note derive_keys (RFC 8452 section 4) under the key-generating key kn[0 .. kl): the
88note message-authentication key into k[131], k[132] and the message-encryption key into ek[0 .. kl)
89fn siv_derive k edit ulong AGKN s edit ulong AGSN kn view uchar * kl ulong ek edit uchar 32 to void
90aes_setkey k s part kn 0 kl
91siv_dblk s kn kl 0
92aes_enc4 k s
93put k 131 get s 150
94put k 132 get s 152
95siv_put8 ek 0 get s 154
96siv_put8 ek 8 get s 156
97if eq kl 32
98siv_dblk s kn kl 4
99aes_enc4 k s
100siv_put8 ek 16 get s 150
101siv_put8 ek 24 get s 152
102end
103end
104
105note the GHASH key for POLYVAL (RFC 8452 Appendix A): mulX_GHASH(ByteReverse(authentication key))
106note into k[121], k[122]; multiplying by x in GHASH's bit order is a right shift that folds R back
107note in when the last bit falls out (the reduction is a mask, as in gcm_mul)
108fn siv_hkey k edit ulong AGKN to void
109h = aes_bswap get k 132
110l = aes_bswap get k 131
111m = sub 0 and l 1
112l = or shr l 1 shl h 63
113h = xor shr h 1 and m AGR
114put k 121 h
115put k 122 l
116end
117
118note keys for kn = key(kl) || nonce(12): the derived keys, the message-encryption key schedule in
119note k[0..121), the POLYVAL key and the nonce cells
120fn siv_init k edit ulong AGKN s edit ulong AGSN kn view uchar * kl ulong to void
121ek = new uchar 32 0
122siv_derive k s kn kl ek
123aes_setkey k s part ek 0 kl
124for i ulong 32 unroll
125put ek i 0
126end
127siv_hkey k
128put k 133 siv_be64 kn kl
129put k 134 shl siv_be64 kn add kl 4 32
130end
131
132note absorb the 16 bytes of d at o as one POLYVAL block: its byte reverse into the GHASH state
133fn siv_ablk k edit ulong AGKN d view uchar * o ulong to void
134put k 123 xor get k 123 siv_le64 d add o 8
135put k 124 xor get k 124 siv_le64 d o
136gcm_mul k
137end
138
139note the whole blocks of d (fewer than 2^20)
140fn siv_absrun k edit ulong AGKN d view uchar * to void
141for i ulong shr len d 4
142siv_ablk k d shl i 4
143end
144end
145
146note POLYVAL update with d, the last partial block padded with zeros (runs of 8 MiB keep every
147note rolled loop under its 1000000 passes)
148fn siv_absorb k edit ulong AGKN d view uchar * to void
149p = new uchar 16 0
150n = len d
151q = ulong 0
152for a ulong shr n 23
153siv_absrun k part d q 0x800000
154q = add q 0x800000
155end
156m = sub n and n 15
157siv_absrun k part d q sub m q
158r = sub n m
159if gt r 0
160for i ulong r
161put p i get d add m i
162end
163siv_ablk k p 0
164for i ulong 16 unroll
165put p i 0
166end
167end
168end
169
170note the length block LE64(8a) || LE64(8c), byte-reversed: high half 8c, low half 8a
171fn siv_lens k edit ulong AGKN a ulong c ulong to void
172put k 123 xor get k 123 shl c 3
173put k 124 xor get k 124 shl a 3
174gcm_mul k
175end
176
177note S = POLYVAL(authentication key, pad16(aad) || pad16(d) || length block) into k[125], k[126]
178note (the byte reverse of the GHASH state)
179fn siv_poly k edit ulong AGKN aad view uchar * d view uchar * to void
180put k 123 0
181put k 124 0
182siv_absorb k aad
183siv_absorb k d
184siv_lens k len aad len d
185put k 125 aes_bswap get k 124
186put k 126 aes_bswap get k 123
187put k 123 0
188put k 124 0
189end
190
191note S ^ nonce, then bit 7 of byte 15 cleared, in k[125], k[126]
192fn siv_mask k edit ulong AGKN to void
193put k 125 xor get k 125 get k 133
194put k 126 xor get k 126 get k 134
195put k 126 and get k 126 GSMSK
196end
197
198note the tag: k[125], k[126] encrypted with the message-encryption key, in place
199fn siv_tag k edit ulong AGKN s edit ulong AGSN to void
200put s 150 get k 125
201put s 151 get k 126
202aes_enc4 k s
203put k 125 get s 150
204put k 126 get s 151
205end
206
207note the initial counter block from the tag halves th, tl: bit 7 of byte 15 set, bytes 0..3 as the
208note little-endian 32-bit counter
209fn siv_ctrset k edit ulong AGKN th ulong tl ulong to void
210put k 127 and th 0xffffffff
211put k 128 or tl 0x80
212put k 129 siv_bsw32 shr th 32
213end
214
215note the next four counter blocks into s[150..158); the counter wraps at 2^32 (only its low 32 bits
216note reach the block)
217fn siv_cblk k edit ulong AGKN s edit ulong AGSN to void
218c = get k 129
219h = get k 127
220l = get k 128
221for b ulong 4 unroll
222put s add 150 mul b 2 or shl siv_bsw32 add c b 32 h
223put s add 151 mul b 2 l
224end
225put k 129 and add c 4 0xffffffff
226end
227
228note CTR over d from the stored counter (len d below 2^25 + 64)
229fn siv_ctrrun k edit ulong AGKN s edit ulong AGSN d edit uchar * to void
230n = len d
231for j ulong shr n 6
232siv_cblk k s
233aes_enc4 k s
234gcm_xor64 s d shl j 6
235end
236r = and n 63
237if gt r 0
238siv_cblk k s
239aes_enc4 k s
240gcm_xorn s part d sub n r r
241end
242end
243
244fn siv_ctr k edit ulong AGKN s edit ulong AGSN d edit uchar * to void
245n = len d
246q = ulong 0
247for a ulong shr n 25
248siv_ctrrun k s part d q 0x2000000
249q = add q 0x2000000
250end
251siv_ctrrun k s part d q sub n q
252end
253
254fn siv_seal1 k edit ulong AGKN s edit ulong AGSN aad view uchar * buf edit uchar * to void
255n = sub len buf 16
256siv_poly k aad view part buf 0 n
257siv_mask k
258siv_tag k s
259siv_ctrset k get k 125 get k 126
260siv_ctr k s part buf 0 n
261siv_put8 buf n get k 125
262siv_put8 buf add n 8 get k 126
263end
264
265fn siv_open1 k edit ulong AGKN s edit ulong AGSN aad view uchar * buf edit uchar * to ulong
266n = sub len buf 16
267th = ulong 0
268tl = ulong 0
269for i ulong 8 unroll
270th = or shl th 8 ulong get buf add n i
271tl = or shl tl 8 ulong get buf add n add i 8
272end
273siv_ctrset k th tl
274siv_ctr k s part buf 0 n
275siv_poly k aad view part buf 0 n
276siv_mask k
277siv_tag k s
278d = or xor get k 125 th xor get k 126 tl
279if ne d 0
280gcm_wipe part buf 0 n
281ret 1
282end
283ret 0
284end
285
286note 0 when the lengths are acceptable (see the API note), else 2
287fn siv_chk kb ulong ab ulong bl ulong to ulong
288ok = ulong 0
289if eq kb 28
290ok = 1
291end
292if eq kb 44
293ok = 1
294end
295if eq ok 0
296ret 2
297end
298if lt bl 16
299ret 2
300end
301if gt sub bl 16 GSMAX
302ret 2
303end
304if gt ab GSMAX
305ret 2
306end
307ret 0
308end
309
310fn aes_gcmsiv_seal_core kn view uchar * aad view uchar * buf edit uchar * to ulong
311k = new ulong AGKN 0
312s = new ulong AGSN 0
313if ne siv_chk len kn len aad len buf 0
314ret 2
315end
316siv_init k s kn sub len kn 12
317siv_seal1 k s aad buf
318gcm_clear k s
319ret 0
320end
321
322fn aes_gcmsiv_seal kn view uchar * aad view uchar * buf edit uchar * to ulong
323prev = dit 1
324r = aes_gcmsiv_seal_core kn aad buf
325z = scrub 4096
326z = dit int prev
327ret r
328end
329
330fn aes_gcmsiv_open_core kn view uchar * aad view uchar * buf edit uchar * to ulong
331k = new ulong AGKN 0
332s = new ulong AGSN 0
333if ne siv_chk len kn len aad len buf 0
334ret 2
335end
336siv_init k s kn sub len kn 12
337r = siv_open1 k s aad buf
338gcm_clear k s
339ret r
340end
341
342fn aes_gcmsiv_open kn view uchar * aad view uchar * buf edit uchar * to ulong
343prev = dit 1
344r = aes_gcmsiv_open_core kn aad buf
345z = scrub 4096
346z = dit int prev
347ret r
348end