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