/src/boringssl/crypto/cipher_extra/e_aesgcmsiv.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2017, Google Inc. |
2 | | * |
3 | | * Permission to use, copy, modify, and/or distribute this software for any |
4 | | * purpose with or without fee is hereby granted, provided that the above |
5 | | * copyright notice and this permission notice appear in all copies. |
6 | | * |
7 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
8 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
10 | | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
14 | | |
15 | | #include <openssl/aead.h> |
16 | | |
17 | | #include <assert.h> |
18 | | |
19 | | #include <openssl/cipher.h> |
20 | | #include <openssl/crypto.h> |
21 | | #include <openssl/err.h> |
22 | | |
23 | | #include "../fipsmodule/cipher/internal.h" |
24 | | #include "../internal.h" |
25 | | |
26 | | |
27 | 0 | #define EVP_AEAD_AES_GCM_SIV_NONCE_LEN 12 |
28 | 0 | #define EVP_AEAD_AES_GCM_SIV_TAG_LEN 16 |
29 | | |
30 | | // TODO(davidben): AES-GCM-SIV assembly is not correct for Windows. It must save |
31 | | // and restore xmm6 through xmm15. |
32 | | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ |
33 | | !defined(OPENSSL_WINDOWS) |
34 | | #define AES_GCM_SIV_ASM |
35 | | |
36 | | // Optimised AES-GCM-SIV |
37 | | |
38 | | struct aead_aes_gcm_siv_asm_ctx { |
39 | | alignas(16) uint8_t key[16*15]; |
40 | | int is_128_bit; |
41 | | }; |
42 | | |
43 | | // The assembly code assumes 8-byte alignment of the EVP_AEAD_CTX's state, and |
44 | | // aligns to 16 bytes itself. |
45 | | static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) + 8 >= |
46 | | sizeof(struct aead_aes_gcm_siv_asm_ctx), |
47 | | "AEAD state is too small"); |
48 | | static_assert(alignof(union evp_aead_ctx_st_state) >= 8, |
49 | | "AEAD state has insufficient alignment"); |
50 | | |
51 | | // asm_ctx_from_ctx returns a 16-byte aligned context pointer from |ctx|. |
52 | | static struct aead_aes_gcm_siv_asm_ctx *asm_ctx_from_ctx( |
53 | 0 | const EVP_AEAD_CTX *ctx) { |
54 | | // ctx->state must already be 8-byte aligned. Thus, at most, we may need to |
55 | | // add eight to align it to 16 bytes. |
56 | 0 | const uintptr_t offset = ((uintptr_t)&ctx->state) & 8; |
57 | 0 | return (struct aead_aes_gcm_siv_asm_ctx *)(&ctx->state.opaque[offset]); |
58 | 0 | } |
59 | | |
60 | | // aes128gcmsiv_aes_ks writes an AES-128 key schedule for |key| to |
61 | | // |out_expanded_key|. |
62 | | extern void aes128gcmsiv_aes_ks( |
63 | | const uint8_t key[16], uint8_t out_expanded_key[16*15]); |
64 | | |
65 | | // aes256gcmsiv_aes_ks writes an AES-256 key schedule for |key| to |
66 | | // |out_expanded_key|. |
67 | | extern void aes256gcmsiv_aes_ks( |
68 | | const uint8_t key[32], uint8_t out_expanded_key[16*15]); |
69 | | |
70 | | static int aead_aes_gcm_siv_asm_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
71 | 0 | size_t key_len, size_t tag_len) { |
72 | 0 | const size_t key_bits = key_len * 8; |
73 | |
|
74 | 0 | if (key_bits != 128 && key_bits != 256) { |
75 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
76 | 0 | return 0; // EVP_AEAD_CTX_init should catch this. |
77 | 0 | } |
78 | | |
79 | 0 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
80 | 0 | tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
81 | 0 | } |
82 | |
|
83 | 0 | if (tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
84 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
85 | 0 | return 0; |
86 | 0 | } |
87 | | |
88 | 0 | struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx = asm_ctx_from_ctx(ctx); |
89 | 0 | assert((((uintptr_t)gcm_siv_ctx) & 15) == 0); |
90 | | |
91 | 0 | if (key_bits == 128) { |
92 | 0 | aes128gcmsiv_aes_ks(key, &gcm_siv_ctx->key[0]); |
93 | 0 | gcm_siv_ctx->is_128_bit = 1; |
94 | 0 | } else { |
95 | 0 | aes256gcmsiv_aes_ks(key, &gcm_siv_ctx->key[0]); |
96 | 0 | gcm_siv_ctx->is_128_bit = 0; |
97 | 0 | } |
98 | |
|
99 | 0 | ctx->tag_len = tag_len; |
100 | |
|
101 | 0 | return 1; |
102 | 0 | } |
103 | | |
104 | 0 | static void aead_aes_gcm_siv_asm_cleanup(EVP_AEAD_CTX *ctx) {} |
105 | | |
106 | | // aesgcmsiv_polyval_horner updates the POLYVAL value in |in_out_poly| to |
107 | | // include a number (|in_blocks|) of 16-byte blocks of data from |in|, given |
108 | | // the POLYVAL key in |key|. |
109 | | extern void aesgcmsiv_polyval_horner(const uint8_t in_out_poly[16], |
110 | | const uint8_t key[16], const uint8_t *in, |
111 | | size_t in_blocks); |
112 | | |
113 | | // aesgcmsiv_htable_init writes powers 1..8 of |auth_key| to |out_htable|. |
114 | | extern void aesgcmsiv_htable_init(uint8_t out_htable[16 * 8], |
115 | | const uint8_t auth_key[16]); |
116 | | |
117 | | // aesgcmsiv_htable6_init writes powers 1..6 of |auth_key| to |out_htable|. |
118 | | extern void aesgcmsiv_htable6_init(uint8_t out_htable[16 * 6], |
119 | | const uint8_t auth_key[16]); |
120 | | |
121 | | // aesgcmsiv_htable_polyval updates the POLYVAL value in |in_out_poly| to |
122 | | // include |in_len| bytes of data from |in|. (Where |in_len| must be a multiple |
123 | | // of 16.) It uses the precomputed powers of the key given in |htable|. |
124 | | extern void aesgcmsiv_htable_polyval(const uint8_t htable[16 * 8], |
125 | | const uint8_t *in, size_t in_len, |
126 | | uint8_t in_out_poly[16]); |
127 | | |
128 | | // aes128gcmsiv_dec decrypts |in_len| & ~15 bytes from |out| and writes them to |
129 | | // |in|. |in| and |out| may be equal, but must not otherwise alias. |
130 | | // |
131 | | // |in_out_calculated_tag_and_scratch|, on entry, must contain: |
132 | | // 1. The current value of the calculated tag, which will be updated during |
133 | | // decryption and written back to the beginning of this buffer on exit. |
134 | | // 2. The claimed tag, which is needed to derive counter values. |
135 | | // |
136 | | // While decrypting, the whole of |in_out_calculated_tag_and_scratch| may be |
137 | | // used for other purposes. In order to decrypt and update the POLYVAL value, it |
138 | | // uses the expanded key from |key| and the table of powers in |htable|. |
139 | | extern void aes128gcmsiv_dec(const uint8_t *in, uint8_t *out, |
140 | | uint8_t in_out_calculated_tag_and_scratch[16 * 8], |
141 | | const uint8_t htable[16 * 6], |
142 | | const struct aead_aes_gcm_siv_asm_ctx *key, |
143 | | size_t in_len); |
144 | | |
145 | | // aes256gcmsiv_dec acts like |aes128gcmsiv_dec|, but for AES-256. |
146 | | extern void aes256gcmsiv_dec(const uint8_t *in, uint8_t *out, |
147 | | uint8_t in_out_calculated_tag_and_scratch[16 * 8], |
148 | | const uint8_t htable[16 * 6], |
149 | | const struct aead_aes_gcm_siv_asm_ctx *key, |
150 | | size_t in_len); |
151 | | |
152 | | // aes128gcmsiv_kdf performs the AES-GCM-SIV KDF given the expanded key from |
153 | | // |key_schedule| and the nonce in |nonce|. Note that, while only 12 bytes of |
154 | | // the nonce are used, 16 bytes are read and so the value must be |
155 | | // right-padded. |
156 | | extern void aes128gcmsiv_kdf(const uint8_t nonce[16], |
157 | | uint64_t out_key_material[8], |
158 | | const uint8_t *key_schedule); |
159 | | |
160 | | // aes256gcmsiv_kdf acts like |aes128gcmsiv_kdf|, but for AES-256. |
161 | | extern void aes256gcmsiv_kdf(const uint8_t nonce[16], |
162 | | uint64_t out_key_material[12], |
163 | | const uint8_t *key_schedule); |
164 | | |
165 | | // aes128gcmsiv_aes_ks_enc_x1 performs a key expansion of the AES-128 key in |
166 | | // |key|, writes the expanded key to |out_expanded_key| and encrypts a single |
167 | | // block from |in| to |out|. |
168 | | extern void aes128gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], |
169 | | uint8_t out_expanded_key[16 * 15], |
170 | | const uint64_t key[2]); |
171 | | |
172 | | // aes256gcmsiv_aes_ks_enc_x1 acts like |aes128gcmsiv_aes_ks_enc_x1|, but for |
173 | | // AES-256. |
174 | | extern void aes256gcmsiv_aes_ks_enc_x1(const uint8_t in[16], uint8_t out[16], |
175 | | uint8_t out_expanded_key[16 * 15], |
176 | | const uint64_t key[4]); |
177 | | |
178 | | // aes128gcmsiv_ecb_enc_block encrypts a single block from |in| to |out| using |
179 | | // the expanded key in |expanded_key|. |
180 | | extern void aes128gcmsiv_ecb_enc_block( |
181 | | const uint8_t in[16], uint8_t out[16], |
182 | | const struct aead_aes_gcm_siv_asm_ctx *expanded_key); |
183 | | |
184 | | // aes256gcmsiv_ecb_enc_block acts like |aes128gcmsiv_ecb_enc_block|, but for |
185 | | // AES-256. |
186 | | extern void aes256gcmsiv_ecb_enc_block( |
187 | | const uint8_t in[16], uint8_t out[16], |
188 | | const struct aead_aes_gcm_siv_asm_ctx *expanded_key); |
189 | | |
190 | | // aes128gcmsiv_enc_msg_x4 encrypts |in_len| bytes from |in| to |out| using the |
191 | | // expanded key from |key|. (The value of |in_len| must be a multiple of 16.) |
192 | | // The |in| and |out| buffers may be equal but must not otherwise overlap. The |
193 | | // initial counter is constructed from the given |tag| as required by |
194 | | // AES-GCM-SIV. |
195 | | extern void aes128gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, |
196 | | const uint8_t *tag, |
197 | | const struct aead_aes_gcm_siv_asm_ctx *key, |
198 | | size_t in_len); |
199 | | |
200 | | // aes256gcmsiv_enc_msg_x4 acts like |aes128gcmsiv_enc_msg_x4|, but for |
201 | | // AES-256. |
202 | | extern void aes256gcmsiv_enc_msg_x4(const uint8_t *in, uint8_t *out, |
203 | | const uint8_t *tag, |
204 | | const struct aead_aes_gcm_siv_asm_ctx *key, |
205 | | size_t in_len); |
206 | | |
207 | | // aes128gcmsiv_enc_msg_x8 acts like |aes128gcmsiv_enc_msg_x4|, but is |
208 | | // optimised for longer messages. |
209 | | extern void aes128gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, |
210 | | const uint8_t *tag, |
211 | | const struct aead_aes_gcm_siv_asm_ctx *key, |
212 | | size_t in_len); |
213 | | |
214 | | // aes256gcmsiv_enc_msg_x8 acts like |aes256gcmsiv_enc_msg_x4|, but is |
215 | | // optimised for longer messages. |
216 | | extern void aes256gcmsiv_enc_msg_x8(const uint8_t *in, uint8_t *out, |
217 | | const uint8_t *tag, |
218 | | const struct aead_aes_gcm_siv_asm_ctx *key, |
219 | | size_t in_len); |
220 | | |
221 | | // gcm_siv_asm_polyval evaluates POLYVAL at |auth_key| on the given plaintext |
222 | | // and AD. The result is written to |out_tag|. |
223 | | static void gcm_siv_asm_polyval(uint8_t out_tag[16], const uint8_t *in, |
224 | | size_t in_len, const uint8_t *ad, size_t ad_len, |
225 | | const uint8_t auth_key[16], |
226 | 0 | const uint8_t nonce[12]) { |
227 | 0 | OPENSSL_memset(out_tag, 0, 16); |
228 | 0 | const size_t ad_blocks = ad_len / 16; |
229 | 0 | const size_t in_blocks = in_len / 16; |
230 | 0 | int htable_init = 0; |
231 | 0 | alignas(16) uint8_t htable[16*8]; |
232 | |
|
233 | 0 | if (ad_blocks > 8 || in_blocks > 8) { |
234 | 0 | htable_init = 1; |
235 | 0 | aesgcmsiv_htable_init(htable, auth_key); |
236 | 0 | } |
237 | |
|
238 | 0 | if (htable_init) { |
239 | 0 | aesgcmsiv_htable_polyval(htable, ad, ad_len & ~15, out_tag); |
240 | 0 | } else { |
241 | 0 | aesgcmsiv_polyval_horner(out_tag, auth_key, ad, ad_blocks); |
242 | 0 | } |
243 | |
|
244 | 0 | uint8_t scratch[16]; |
245 | 0 | if (ad_len & 15) { |
246 | 0 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
247 | 0 | OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15); |
248 | 0 | aesgcmsiv_polyval_horner(out_tag, auth_key, scratch, 1); |
249 | 0 | } |
250 | |
|
251 | 0 | if (htable_init) { |
252 | 0 | aesgcmsiv_htable_polyval(htable, in, in_len & ~15, out_tag); |
253 | 0 | } else { |
254 | 0 | aesgcmsiv_polyval_horner(out_tag, auth_key, in, in_blocks); |
255 | 0 | } |
256 | |
|
257 | 0 | if (in_len & 15) { |
258 | 0 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
259 | 0 | OPENSSL_memcpy(scratch, &in[in_len & ~15], in_len & 15); |
260 | 0 | aesgcmsiv_polyval_horner(out_tag, auth_key, scratch, 1); |
261 | 0 | } |
262 | |
|
263 | 0 | uint8_t length_block[16]; |
264 | 0 | CRYPTO_store_u64_le(length_block, ad_len * 8); |
265 | 0 | CRYPTO_store_u64_le(length_block + 8, in_len * 8); |
266 | 0 | aesgcmsiv_polyval_horner(out_tag, auth_key, length_block, 1); |
267 | |
|
268 | 0 | for (size_t i = 0; i < 12; i++) { |
269 | 0 | out_tag[i] ^= nonce[i]; |
270 | 0 | } |
271 | |
|
272 | 0 | out_tag[15] &= 0x7f; |
273 | 0 | } |
274 | | |
275 | | // aead_aes_gcm_siv_asm_crypt_last_block handles the encryption/decryption |
276 | | // (same thing in CTR mode) of the final block of a plaintext/ciphertext. It |
277 | | // writes |in_len| & 15 bytes to |out| + |in_len|, based on an initial counter |
278 | | // derived from |tag|. |
279 | | static void aead_aes_gcm_siv_asm_crypt_last_block( |
280 | | int is_128_bit, uint8_t *out, const uint8_t *in, size_t in_len, |
281 | | const uint8_t tag[16], |
282 | 0 | const struct aead_aes_gcm_siv_asm_ctx *enc_key_expanded) { |
283 | 0 | alignas(16) uint8_t counter[16]; |
284 | 0 | OPENSSL_memcpy(&counter, tag, sizeof(counter)); |
285 | 0 | counter[15] |= 0x80; |
286 | 0 | CRYPTO_store_u32_le(counter, CRYPTO_load_u32_le(counter) + in_len / 16); |
287 | |
|
288 | 0 | if (is_128_bit) { |
289 | 0 | aes128gcmsiv_ecb_enc_block(counter, counter, enc_key_expanded); |
290 | 0 | } else { |
291 | 0 | aes256gcmsiv_ecb_enc_block(counter, counter, enc_key_expanded); |
292 | 0 | } |
293 | |
|
294 | 0 | const size_t last_bytes_offset = in_len & ~15; |
295 | 0 | const size_t last_bytes_len = in_len & 15; |
296 | 0 | uint8_t *last_bytes_out = &out[last_bytes_offset]; |
297 | 0 | const uint8_t *last_bytes_in = &in[last_bytes_offset]; |
298 | 0 | for (size_t i = 0; i < last_bytes_len; i++) { |
299 | 0 | last_bytes_out[i] = last_bytes_in[i] ^ counter[i]; |
300 | 0 | } |
301 | 0 | } |
302 | | |
303 | | // aead_aes_gcm_siv_kdf calculates the record encryption and authentication |
304 | | // keys given the |nonce|. |
305 | | static void aead_aes_gcm_siv_kdf( |
306 | | int is_128_bit, const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx, |
307 | | uint64_t out_record_auth_key[2], uint64_t out_record_enc_key[4], |
308 | 0 | const uint8_t nonce[12]) { |
309 | 0 | alignas(16) uint8_t padded_nonce[16]; |
310 | 0 | OPENSSL_memcpy(padded_nonce, nonce, 12); |
311 | |
|
312 | 0 | alignas(16) uint64_t key_material[12]; |
313 | 0 | if (is_128_bit) { |
314 | 0 | aes128gcmsiv_kdf(padded_nonce, key_material, &gcm_siv_ctx->key[0]); |
315 | 0 | out_record_enc_key[0] = key_material[4]; |
316 | 0 | out_record_enc_key[1] = key_material[6]; |
317 | 0 | } else { |
318 | 0 | aes256gcmsiv_kdf(padded_nonce, key_material, &gcm_siv_ctx->key[0]); |
319 | 0 | out_record_enc_key[0] = key_material[4]; |
320 | 0 | out_record_enc_key[1] = key_material[6]; |
321 | 0 | out_record_enc_key[2] = key_material[8]; |
322 | 0 | out_record_enc_key[3] = key_material[10]; |
323 | 0 | } |
324 | |
|
325 | 0 | out_record_auth_key[0] = key_material[0]; |
326 | 0 | out_record_auth_key[1] = key_material[2]; |
327 | 0 | } |
328 | | |
329 | | static int aead_aes_gcm_siv_asm_seal_scatter( |
330 | | const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, |
331 | | size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce, |
332 | | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in, |
333 | 0 | size_t extra_in_len, const uint8_t *ad, size_t ad_len) { |
334 | 0 | const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx = asm_ctx_from_ctx(ctx); |
335 | 0 | const uint64_t in_len_64 = in_len; |
336 | 0 | const uint64_t ad_len_64 = ad_len; |
337 | |
|
338 | 0 | if (in_len_64 > (UINT64_C(1) << 36) || |
339 | 0 | ad_len_64 >= (UINT64_C(1) << 61)) { |
340 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
341 | 0 | return 0; |
342 | 0 | } |
343 | | |
344 | 0 | if (max_out_tag_len < EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
345 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
346 | 0 | return 0; |
347 | 0 | } |
348 | | |
349 | 0 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
350 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
351 | 0 | return 0; |
352 | 0 | } |
353 | | |
354 | 0 | alignas(16) uint64_t record_auth_key[2]; |
355 | 0 | alignas(16) uint64_t record_enc_key[4]; |
356 | 0 | aead_aes_gcm_siv_kdf(gcm_siv_ctx->is_128_bit, gcm_siv_ctx, record_auth_key, |
357 | 0 | record_enc_key, nonce); |
358 | |
|
359 | 0 | alignas(16) uint8_t tag[16] = {0}; |
360 | 0 | gcm_siv_asm_polyval(tag, in, in_len, ad, ad_len, |
361 | 0 | (const uint8_t *)record_auth_key, nonce); |
362 | |
|
363 | 0 | struct aead_aes_gcm_siv_asm_ctx enc_key_expanded; |
364 | |
|
365 | 0 | if (gcm_siv_ctx->is_128_bit) { |
366 | 0 | aes128gcmsiv_aes_ks_enc_x1(tag, tag, &enc_key_expanded.key[0], |
367 | 0 | record_enc_key); |
368 | |
|
369 | 0 | if (in_len < 128) { |
370 | 0 | aes128gcmsiv_enc_msg_x4(in, out, tag, &enc_key_expanded, in_len & ~15); |
371 | 0 | } else { |
372 | 0 | aes128gcmsiv_enc_msg_x8(in, out, tag, &enc_key_expanded, in_len & ~15); |
373 | 0 | } |
374 | 0 | } else { |
375 | 0 | aes256gcmsiv_aes_ks_enc_x1(tag, tag, &enc_key_expanded.key[0], |
376 | 0 | record_enc_key); |
377 | |
|
378 | 0 | if (in_len < 128) { |
379 | 0 | aes256gcmsiv_enc_msg_x4(in, out, tag, &enc_key_expanded, in_len & ~15); |
380 | 0 | } else { |
381 | 0 | aes256gcmsiv_enc_msg_x8(in, out, tag, &enc_key_expanded, in_len & ~15); |
382 | 0 | } |
383 | 0 | } |
384 | |
|
385 | 0 | if (in_len & 15) { |
386 | 0 | aead_aes_gcm_siv_asm_crypt_last_block(gcm_siv_ctx->is_128_bit, out, in, |
387 | 0 | in_len, tag, &enc_key_expanded); |
388 | 0 | } |
389 | |
|
390 | 0 | OPENSSL_memcpy(out_tag, tag, sizeof(tag)); |
391 | 0 | *out_tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
392 | |
|
393 | 0 | return 1; |
394 | 0 | } |
395 | | |
396 | | static int aead_aes_gcm_siv_asm_open_gather( |
397 | | const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce, |
398 | | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag, |
399 | 0 | size_t in_tag_len, const uint8_t *ad, size_t ad_len) { |
400 | 0 | const uint64_t ad_len_64 = ad_len; |
401 | 0 | if (ad_len_64 >= (UINT64_C(1) << 61)) { |
402 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
403 | 0 | return 0; |
404 | 0 | } |
405 | | |
406 | 0 | const uint64_t in_len_64 = in_len; |
407 | 0 | if (in_len_64 > UINT64_C(1) << 36 || |
408 | 0 | in_tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
409 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
410 | 0 | return 0; |
411 | 0 | } |
412 | | |
413 | 0 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
414 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
415 | 0 | return 0; |
416 | 0 | } |
417 | | |
418 | 0 | const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx = asm_ctx_from_ctx(ctx); |
419 | |
|
420 | 0 | alignas(16) uint64_t record_auth_key[2]; |
421 | 0 | alignas(16) uint64_t record_enc_key[4]; |
422 | 0 | aead_aes_gcm_siv_kdf(gcm_siv_ctx->is_128_bit, gcm_siv_ctx, record_auth_key, |
423 | 0 | record_enc_key, nonce); |
424 | |
|
425 | 0 | struct aead_aes_gcm_siv_asm_ctx expanded_key; |
426 | 0 | if (gcm_siv_ctx->is_128_bit) { |
427 | 0 | aes128gcmsiv_aes_ks((const uint8_t *) record_enc_key, &expanded_key.key[0]); |
428 | 0 | } else { |
429 | 0 | aes256gcmsiv_aes_ks((const uint8_t *) record_enc_key, &expanded_key.key[0]); |
430 | 0 | } |
431 | | // calculated_tag is 16*8 bytes, rather than 16 bytes, because |
432 | | // aes[128|256]gcmsiv_dec uses the extra as scratch space. |
433 | 0 | alignas(16) uint8_t calculated_tag[16 * 8] = {0}; |
434 | |
|
435 | 0 | OPENSSL_memset(calculated_tag, 0, EVP_AEAD_AES_GCM_SIV_TAG_LEN); |
436 | 0 | const size_t ad_blocks = ad_len / 16; |
437 | 0 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, ad, |
438 | 0 | ad_blocks); |
439 | |
|
440 | 0 | uint8_t scratch[16]; |
441 | 0 | if (ad_len & 15) { |
442 | 0 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
443 | 0 | OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15); |
444 | 0 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, |
445 | 0 | scratch, 1); |
446 | 0 | } |
447 | |
|
448 | 0 | alignas(16) uint8_t htable[16 * 6]; |
449 | 0 | aesgcmsiv_htable6_init(htable, (const uint8_t *)record_auth_key); |
450 | | |
451 | | // aes[128|256]gcmsiv_dec needs access to the claimed tag. So it's put into |
452 | | // its scratch space. |
453 | 0 | memcpy(calculated_tag + 16, in_tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN); |
454 | 0 | if (gcm_siv_ctx->is_128_bit) { |
455 | 0 | aes128gcmsiv_dec(in, out, calculated_tag, htable, &expanded_key, in_len); |
456 | 0 | } else { |
457 | 0 | aes256gcmsiv_dec(in, out, calculated_tag, htable, &expanded_key, in_len); |
458 | 0 | } |
459 | |
|
460 | 0 | if (in_len & 15) { |
461 | 0 | aead_aes_gcm_siv_asm_crypt_last_block(gcm_siv_ctx->is_128_bit, out, in, |
462 | 0 | in_len, in_tag, &expanded_key); |
463 | 0 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
464 | 0 | OPENSSL_memcpy(scratch, out + (in_len & ~15), in_len & 15); |
465 | 0 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, |
466 | 0 | scratch, 1); |
467 | 0 | } |
468 | |
|
469 | 0 | uint8_t length_block[16]; |
470 | 0 | CRYPTO_store_u64_le(length_block, ad_len * 8); |
471 | 0 | CRYPTO_store_u64_le(length_block + 8, in_len * 8); |
472 | 0 | aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, |
473 | 0 | length_block, 1); |
474 | |
|
475 | 0 | for (size_t i = 0; i < 12; i++) { |
476 | 0 | calculated_tag[i] ^= nonce[i]; |
477 | 0 | } |
478 | |
|
479 | 0 | calculated_tag[15] &= 0x7f; |
480 | |
|
481 | 0 | if (gcm_siv_ctx->is_128_bit) { |
482 | 0 | aes128gcmsiv_ecb_enc_block(calculated_tag, calculated_tag, &expanded_key); |
483 | 0 | } else { |
484 | 0 | aes256gcmsiv_ecb_enc_block(calculated_tag, calculated_tag, &expanded_key); |
485 | 0 | } |
486 | |
|
487 | 0 | if (CRYPTO_memcmp(calculated_tag, in_tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN) != |
488 | 0 | 0) { |
489 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
490 | 0 | return 0; |
491 | 0 | } |
492 | | |
493 | 0 | return 1; |
494 | 0 | } |
495 | | |
496 | | static const EVP_AEAD aead_aes_128_gcm_siv_asm = { |
497 | | 16, // key length |
498 | | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
499 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
500 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
501 | | 0, // seal_scatter_supports_extra_in |
502 | | |
503 | | aead_aes_gcm_siv_asm_init, |
504 | | NULL /* init_with_direction */, |
505 | | aead_aes_gcm_siv_asm_cleanup, |
506 | | NULL /* open */, |
507 | | aead_aes_gcm_siv_asm_seal_scatter, |
508 | | aead_aes_gcm_siv_asm_open_gather, |
509 | | NULL /* get_iv */, |
510 | | NULL /* tag_len */, |
511 | | }; |
512 | | |
513 | | static const EVP_AEAD aead_aes_256_gcm_siv_asm = { |
514 | | 32, // key length |
515 | | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
516 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
517 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
518 | | 0, // seal_scatter_supports_extra_in |
519 | | |
520 | | aead_aes_gcm_siv_asm_init, |
521 | | NULL /* init_with_direction */, |
522 | | aead_aes_gcm_siv_asm_cleanup, |
523 | | NULL /* open */, |
524 | | aead_aes_gcm_siv_asm_seal_scatter, |
525 | | aead_aes_gcm_siv_asm_open_gather, |
526 | | NULL /* get_iv */, |
527 | | NULL /* tag_len */, |
528 | | }; |
529 | | |
530 | | #endif // X86_64 && !NO_ASM && !WINDOWS |
531 | | |
532 | | struct aead_aes_gcm_siv_ctx { |
533 | | union { |
534 | | double align; |
535 | | AES_KEY ks; |
536 | | } ks; |
537 | | block128_f kgk_block; |
538 | | unsigned is_256:1; |
539 | | }; |
540 | | |
541 | | static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >= |
542 | | sizeof(struct aead_aes_gcm_siv_ctx), |
543 | | "AEAD state is too small"); |
544 | | static_assert(alignof(union evp_aead_ctx_st_state) >= |
545 | | alignof(struct aead_aes_gcm_siv_ctx), |
546 | | "AEAD state has insufficient alignment"); |
547 | | |
548 | | static int aead_aes_gcm_siv_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
549 | 0 | size_t key_len, size_t tag_len) { |
550 | 0 | const size_t key_bits = key_len * 8; |
551 | |
|
552 | 0 | if (key_bits != 128 && key_bits != 256) { |
553 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH); |
554 | 0 | return 0; // EVP_AEAD_CTX_init should catch this. |
555 | 0 | } |
556 | | |
557 | 0 | if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) { |
558 | 0 | tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
559 | 0 | } |
560 | 0 | if (tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
561 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE); |
562 | 0 | return 0; |
563 | 0 | } |
564 | | |
565 | 0 | struct aead_aes_gcm_siv_ctx *gcm_siv_ctx = |
566 | 0 | (struct aead_aes_gcm_siv_ctx *)&ctx->state; |
567 | 0 | OPENSSL_memset(gcm_siv_ctx, 0, sizeof(struct aead_aes_gcm_siv_ctx)); |
568 | |
|
569 | 0 | aes_ctr_set_key(&gcm_siv_ctx->ks.ks, NULL, &gcm_siv_ctx->kgk_block, key, |
570 | 0 | key_len); |
571 | 0 | gcm_siv_ctx->is_256 = (key_len == 32); |
572 | 0 | ctx->tag_len = tag_len; |
573 | |
|
574 | 0 | return 1; |
575 | 0 | } |
576 | | |
577 | 0 | static void aead_aes_gcm_siv_cleanup(EVP_AEAD_CTX *ctx) {} |
578 | | |
579 | | // gcm_siv_crypt encrypts (or decrypts—it's the same thing) |in_len| bytes from |
580 | | // |in| to |out|, using the block function |enc_block| with |key| in counter |
581 | | // mode, starting at |initial_counter|. This differs from the traditional |
582 | | // counter mode code in that the counter is handled little-endian, only the |
583 | | // first four bytes are used and the GCM-SIV tweak to the final byte is |
584 | | // applied. The |in| and |out| pointers may be equal but otherwise must not |
585 | | // alias. |
586 | | static void gcm_siv_crypt(uint8_t *out, const uint8_t *in, size_t in_len, |
587 | | const uint8_t initial_counter[AES_BLOCK_SIZE], |
588 | 0 | block128_f enc_block, const AES_KEY *key) { |
589 | 0 | uint8_t counter[16]; |
590 | |
|
591 | 0 | OPENSSL_memcpy(counter, initial_counter, AES_BLOCK_SIZE); |
592 | 0 | counter[15] |= 0x80; |
593 | |
|
594 | 0 | for (size_t done = 0; done < in_len;) { |
595 | 0 | uint8_t keystream[AES_BLOCK_SIZE]; |
596 | 0 | enc_block(counter, keystream, key); |
597 | 0 | CRYPTO_store_u32_le(counter, CRYPTO_load_u32_le(counter) + 1); |
598 | |
|
599 | 0 | size_t todo = AES_BLOCK_SIZE; |
600 | 0 | if (in_len - done < todo) { |
601 | 0 | todo = in_len - done; |
602 | 0 | } |
603 | |
|
604 | 0 | for (size_t i = 0; i < todo; i++) { |
605 | 0 | out[done + i] = keystream[i] ^ in[done + i]; |
606 | 0 | } |
607 | |
|
608 | 0 | done += todo; |
609 | 0 | } |
610 | 0 | } |
611 | | |
612 | | // gcm_siv_polyval evaluates POLYVAL at |auth_key| on the given plaintext and |
613 | | // AD. The result is written to |out_tag|. |
614 | | static void gcm_siv_polyval( |
615 | | uint8_t out_tag[16], const uint8_t *in, size_t in_len, const uint8_t *ad, |
616 | | size_t ad_len, const uint8_t auth_key[16], |
617 | 0 | const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) { |
618 | 0 | struct polyval_ctx polyval_ctx; |
619 | 0 | CRYPTO_POLYVAL_init(&polyval_ctx, auth_key); |
620 | |
|
621 | 0 | CRYPTO_POLYVAL_update_blocks(&polyval_ctx, ad, ad_len & ~15); |
622 | |
|
623 | 0 | uint8_t scratch[16]; |
624 | 0 | if (ad_len & 15) { |
625 | 0 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
626 | 0 | OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15); |
627 | 0 | CRYPTO_POLYVAL_update_blocks(&polyval_ctx, scratch, sizeof(scratch)); |
628 | 0 | } |
629 | |
|
630 | 0 | CRYPTO_POLYVAL_update_blocks(&polyval_ctx, in, in_len & ~15); |
631 | 0 | if (in_len & 15) { |
632 | 0 | OPENSSL_memset(scratch, 0, sizeof(scratch)); |
633 | 0 | OPENSSL_memcpy(scratch, &in[in_len & ~15], in_len & 15); |
634 | 0 | CRYPTO_POLYVAL_update_blocks(&polyval_ctx, scratch, sizeof(scratch)); |
635 | 0 | } |
636 | |
|
637 | 0 | uint8_t length_block[16]; |
638 | 0 | CRYPTO_store_u64_le(length_block, ((uint64_t) ad_len) * 8); |
639 | 0 | CRYPTO_store_u64_le(length_block + 8, ((uint64_t) in_len) * 8); |
640 | 0 | CRYPTO_POLYVAL_update_blocks(&polyval_ctx, length_block, |
641 | 0 | sizeof(length_block)); |
642 | |
|
643 | 0 | CRYPTO_POLYVAL_finish(&polyval_ctx, out_tag); |
644 | 0 | for (size_t i = 0; i < EVP_AEAD_AES_GCM_SIV_NONCE_LEN; i++) { |
645 | 0 | out_tag[i] ^= nonce[i]; |
646 | 0 | } |
647 | 0 | out_tag[15] &= 0x7f; |
648 | 0 | } |
649 | | |
650 | | // gcm_siv_record_keys contains the keys used for a specific GCM-SIV record. |
651 | | struct gcm_siv_record_keys { |
652 | | uint8_t auth_key[16]; |
653 | | union { |
654 | | double align; |
655 | | AES_KEY ks; |
656 | | } enc_key; |
657 | | block128_f enc_block; |
658 | | }; |
659 | | |
660 | | // gcm_siv_keys calculates the keys for a specific GCM-SIV record with the |
661 | | // given nonce and writes them to |*out_keys|. |
662 | | static void gcm_siv_keys( |
663 | | const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx, |
664 | | struct gcm_siv_record_keys *out_keys, |
665 | 0 | const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) { |
666 | 0 | const AES_KEY *const key = &gcm_siv_ctx->ks.ks; |
667 | 0 | uint8_t key_material[(128 /* POLYVAL key */ + 256 /* max AES key */) / 8]; |
668 | 0 | const size_t blocks_needed = gcm_siv_ctx->is_256 ? 6 : 4; |
669 | |
|
670 | 0 | uint8_t counter[AES_BLOCK_SIZE]; |
671 | 0 | OPENSSL_memset(counter, 0, AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN); |
672 | 0 | OPENSSL_memcpy(counter + AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN, |
673 | 0 | nonce, EVP_AEAD_AES_GCM_SIV_NONCE_LEN); |
674 | 0 | for (size_t i = 0; i < blocks_needed; i++) { |
675 | 0 | counter[0] = i; |
676 | |
|
677 | 0 | uint8_t ciphertext[AES_BLOCK_SIZE]; |
678 | 0 | gcm_siv_ctx->kgk_block(counter, ciphertext, key); |
679 | 0 | OPENSSL_memcpy(&key_material[i * 8], ciphertext, 8); |
680 | 0 | } |
681 | |
|
682 | 0 | OPENSSL_memcpy(out_keys->auth_key, key_material, 16); |
683 | | // Note the |ctr128_f| function uses a big-endian couner, while AES-GCM-SIV |
684 | | // uses a little-endian counter. We ignore the return value and only use |
685 | | // |block128_f|. This has a significant performance cost for the fallback |
686 | | // bitsliced AES implementations (bsaes and aes_nohw). |
687 | | // |
688 | | // We currently do not consider AES-GCM-SIV to be performance-sensitive on |
689 | | // client hardware. If this changes, we can write little-endian |ctr128_f| |
690 | | // functions. |
691 | 0 | aes_ctr_set_key(&out_keys->enc_key.ks, NULL, &out_keys->enc_block, |
692 | 0 | key_material + 16, gcm_siv_ctx->is_256 ? 32 : 16); |
693 | 0 | } |
694 | | |
695 | | static int aead_aes_gcm_siv_seal_scatter( |
696 | | const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, |
697 | | size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce, |
698 | | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in, |
699 | 0 | size_t extra_in_len, const uint8_t *ad, size_t ad_len) { |
700 | 0 | const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx = |
701 | 0 | (struct aead_aes_gcm_siv_ctx *)&ctx->state; |
702 | 0 | const uint64_t in_len_64 = in_len; |
703 | 0 | const uint64_t ad_len_64 = ad_len; |
704 | |
|
705 | 0 | if (in_len + EVP_AEAD_AES_GCM_SIV_TAG_LEN < in_len || |
706 | 0 | in_len_64 > (UINT64_C(1) << 36) || |
707 | 0 | ad_len_64 >= (UINT64_C(1) << 61)) { |
708 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
709 | 0 | return 0; |
710 | 0 | } |
711 | | |
712 | 0 | if (max_out_tag_len < EVP_AEAD_AES_GCM_SIV_TAG_LEN) { |
713 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
714 | 0 | return 0; |
715 | 0 | } |
716 | | |
717 | 0 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
718 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
719 | 0 | return 0; |
720 | 0 | } |
721 | | |
722 | 0 | struct gcm_siv_record_keys keys; |
723 | 0 | gcm_siv_keys(gcm_siv_ctx, &keys, nonce); |
724 | |
|
725 | 0 | uint8_t tag[16]; |
726 | 0 | gcm_siv_polyval(tag, in, in_len, ad, ad_len, keys.auth_key, nonce); |
727 | 0 | keys.enc_block(tag, tag, &keys.enc_key.ks); |
728 | |
|
729 | 0 | gcm_siv_crypt(out, in, in_len, tag, keys.enc_block, &keys.enc_key.ks); |
730 | |
|
731 | 0 | OPENSSL_memcpy(out_tag, tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN); |
732 | 0 | *out_tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN; |
733 | |
|
734 | 0 | return 1; |
735 | 0 | } |
736 | | |
737 | | static int aead_aes_gcm_siv_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out, |
738 | | const uint8_t *nonce, size_t nonce_len, |
739 | | const uint8_t *in, size_t in_len, |
740 | | const uint8_t *in_tag, |
741 | | size_t in_tag_len, const uint8_t *ad, |
742 | 0 | size_t ad_len) { |
743 | 0 | const uint64_t ad_len_64 = ad_len; |
744 | 0 | if (ad_len_64 >= (UINT64_C(1) << 61)) { |
745 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
746 | 0 | return 0; |
747 | 0 | } |
748 | | |
749 | 0 | const uint64_t in_len_64 = in_len; |
750 | 0 | if (in_tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN || |
751 | 0 | in_len_64 > (UINT64_C(1) << 36) + AES_BLOCK_SIZE) { |
752 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
753 | 0 | return 0; |
754 | 0 | } |
755 | | |
756 | 0 | if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) { |
757 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
758 | 0 | return 0; |
759 | 0 | } |
760 | | |
761 | 0 | const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx = |
762 | 0 | (struct aead_aes_gcm_siv_ctx *)&ctx->state; |
763 | |
|
764 | 0 | struct gcm_siv_record_keys keys; |
765 | 0 | gcm_siv_keys(gcm_siv_ctx, &keys, nonce); |
766 | |
|
767 | 0 | gcm_siv_crypt(out, in, in_len, in_tag, keys.enc_block, &keys.enc_key.ks); |
768 | |
|
769 | 0 | uint8_t expected_tag[EVP_AEAD_AES_GCM_SIV_TAG_LEN]; |
770 | 0 | gcm_siv_polyval(expected_tag, out, in_len, ad, ad_len, keys.auth_key, nonce); |
771 | 0 | keys.enc_block(expected_tag, expected_tag, &keys.enc_key.ks); |
772 | |
|
773 | 0 | if (CRYPTO_memcmp(expected_tag, in_tag, sizeof(expected_tag)) != 0) { |
774 | 0 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
775 | 0 | return 0; |
776 | 0 | } |
777 | | |
778 | 0 | return 1; |
779 | 0 | } |
780 | | |
781 | | static const EVP_AEAD aead_aes_128_gcm_siv = { |
782 | | 16, // key length |
783 | | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
784 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
785 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
786 | | 0, // seal_scatter_supports_extra_in |
787 | | |
788 | | aead_aes_gcm_siv_init, |
789 | | NULL /* init_with_direction */, |
790 | | aead_aes_gcm_siv_cleanup, |
791 | | NULL /* open */, |
792 | | aead_aes_gcm_siv_seal_scatter, |
793 | | aead_aes_gcm_siv_open_gather, |
794 | | NULL /* get_iv */, |
795 | | NULL /* tag_len */, |
796 | | }; |
797 | | |
798 | | static const EVP_AEAD aead_aes_256_gcm_siv = { |
799 | | 32, // key length |
800 | | EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length |
801 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead |
802 | | EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length |
803 | | 0, // seal_scatter_supports_extra_in |
804 | | |
805 | | aead_aes_gcm_siv_init, |
806 | | NULL /* init_with_direction */, |
807 | | aead_aes_gcm_siv_cleanup, |
808 | | NULL /* open */, |
809 | | aead_aes_gcm_siv_seal_scatter, |
810 | | aead_aes_gcm_siv_open_gather, |
811 | | NULL /* get_iv */, |
812 | | NULL /* tag_len */, |
813 | | }; |
814 | | |
815 | | #if defined(AES_GCM_SIV_ASM) |
816 | | |
817 | | const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) { |
818 | | if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { |
819 | | return &aead_aes_128_gcm_siv_asm; |
820 | | } |
821 | | return &aead_aes_128_gcm_siv; |
822 | | } |
823 | | |
824 | | const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) { |
825 | | if (CRYPTO_is_AVX_capable() && CRYPTO_is_AESNI_capable()) { |
826 | | return &aead_aes_256_gcm_siv_asm; |
827 | | } |
828 | | return &aead_aes_256_gcm_siv; |
829 | | } |
830 | | |
831 | | #else |
832 | | |
833 | 1 | const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) { |
834 | 1 | return &aead_aes_128_gcm_siv; |
835 | 1 | } |
836 | | |
837 | 1 | const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) { |
838 | 1 | return &aead_aes_256_gcm_siv; |
839 | 1 | } |
840 | | |
841 | | #endif // AES_GCM_SIV_ASM |