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
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 OPENSSL_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__)
49 OPENSSL_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|.
asm_ctx_from_ctx(const EVP_AEAD_CTX * ctx)54 static 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|.
64 extern 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|.
69 extern void aes256gcmsiv_aes_ks(
70 const uint8_t key[32], uint8_t out_expanded_key[16*15]);
71
aead_aes_gcm_siv_asm_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len)72 static 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
aead_aes_gcm_siv_asm_cleanup(EVP_AEAD_CTX * ctx)106 static 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|.
111 extern 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|.
116 extern 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|.
120 extern 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|.
126 extern 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|.
141 extern 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.
148 extern 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.
158 extern 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.
163 extern 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|.
170 extern 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.
176 extern 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|.
182 extern 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.
188 extern 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.
197 extern 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.
204 extern 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.
211 extern 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.
218 extern 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|.
gcm_siv_asm_polyval(uint8_t out_tag[16],const uint8_t * in,size_t in_len,const uint8_t * ad,size_t ad_len,const uint8_t auth_key[16],const uint8_t nonce[12])225 static 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|.
aead_aes_gcm_siv_asm_crypt_last_block(int is_128_bit,uint8_t * out,const uint8_t * in,size_t in_len,const uint8_t tag[16],const struct aead_aes_gcm_siv_asm_ctx * enc_key_expanded)288 static 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|.
aead_aes_gcm_siv_kdf(int is_128_bit,const struct aead_aes_gcm_siv_asm_ctx * gcm_siv_ctx,uint64_t out_record_auth_key[2],uint64_t out_record_enc_key[4],const uint8_t nonce[12])317 static 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
aead_aes_gcm_siv_asm_seal_scatter(const EVP_AEAD_CTX * ctx,uint8_t * out,uint8_t * out_tag,size_t * out_tag_len,size_t max_out_tag_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * extra_in,size_t extra_in_len,const uint8_t * ad,size_t ad_len)341 static 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
aead_aes_gcm_siv_asm_open(const EVP_AEAD_CTX * ctx,uint8_t * out,size_t * out_len,size_t max_out_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * ad,size_t ad_len)411 static 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
527 static 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
544 static 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
563 struct 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
572 OPENSSL_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__)
576 OPENSSL_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
aead_aes_gcm_siv_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len)581 static 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
aead_aes_gcm_siv_cleanup(EVP_AEAD_CTX * ctx)610 static 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.
gcm_siv_crypt(uint8_t * out,const uint8_t * in,size_t in_len,const uint8_t initial_counter[AES_BLOCK_SIZE],block128_f enc_block,const AES_KEY * key)619 static 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 #ifdef OPENSSL_BIGENDIAN
634 counter.w[0] = CRYPTO_bswap4(CRYPTO_bswap4(counter.w[0]) + 1);
635 #else
636 counter.w[0]++;
637 #endif
638
639 size_t todo = AES_BLOCK_SIZE;
640 if (in_len - done < todo) {
641 todo = in_len - done;
642 }
643
644 for (size_t i = 0; i < todo; i++) {
645 out[done + i] = keystream[i] ^ in[done + i];
646 }
647
648 done += todo;
649 }
650 }
651
652 // gcm_siv_polyval evaluates POLYVAL at |auth_key| on the given plaintext and
653 // AD. The result is written to |out_tag|.
gcm_siv_polyval(uint8_t out_tag[16],const uint8_t * in,size_t in_len,const uint8_t * ad,size_t ad_len,const uint8_t auth_key[16],const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN])654 static void gcm_siv_polyval(
655 uint8_t out_tag[16], const uint8_t *in, size_t in_len, const uint8_t *ad,
656 size_t ad_len, const uint8_t auth_key[16],
657 const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) {
658 struct polyval_ctx polyval_ctx;
659 CRYPTO_POLYVAL_init(&polyval_ctx, auth_key);
660
661 CRYPTO_POLYVAL_update_blocks(&polyval_ctx, ad, ad_len & ~15);
662
663 uint8_t scratch[16];
664 if (ad_len & 15) {
665 OPENSSL_memset(scratch, 0, sizeof(scratch));
666 OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15);
667 CRYPTO_POLYVAL_update_blocks(&polyval_ctx, scratch, sizeof(scratch));
668 }
669
670 CRYPTO_POLYVAL_update_blocks(&polyval_ctx, in, in_len & ~15);
671 if (in_len & 15) {
672 OPENSSL_memset(scratch, 0, sizeof(scratch));
673 OPENSSL_memcpy(scratch, &in[in_len & ~15], in_len & 15);
674 CRYPTO_POLYVAL_update_blocks(&polyval_ctx, scratch, sizeof(scratch));
675 }
676
677 union {
678 uint8_t c[16];
679 struct {
680 uint64_t ad;
681 uint64_t in;
682 } bitlens;
683 } length_block;
684
685 #ifdef OPENSSL_BIGENDIAN
686 length_block.bitlens.ad = CRYPTO_bswap8(ad_len * 8);
687 length_block.bitlens.in = CRYPTO_bswap8(in_len * 8);
688 #else
689 length_block.bitlens.ad = ad_len * 8;
690 length_block.bitlens.in = in_len * 8;
691 #endif
692 CRYPTO_POLYVAL_update_blocks(&polyval_ctx, length_block.c,
693 sizeof(length_block));
694
695 CRYPTO_POLYVAL_finish(&polyval_ctx, out_tag);
696 for (size_t i = 0; i < EVP_AEAD_AES_GCM_SIV_NONCE_LEN; i++) {
697 out_tag[i] ^= nonce[i];
698 }
699 out_tag[15] &= 0x7f;
700 }
701
702 // gcm_siv_record_keys contains the keys used for a specific GCM-SIV record.
703 struct gcm_siv_record_keys {
704 uint8_t auth_key[16];
705 union {
706 double align;
707 AES_KEY ks;
708 } enc_key;
709 block128_f enc_block;
710 };
711
712 // gcm_siv_keys calculates the keys for a specific GCM-SIV record with the
713 // given nonce and writes them to |*out_keys|.
gcm_siv_keys(const struct aead_aes_gcm_siv_ctx * gcm_siv_ctx,struct gcm_siv_record_keys * out_keys,const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN])714 static void gcm_siv_keys(
715 const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx,
716 struct gcm_siv_record_keys *out_keys,
717 const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) {
718 const AES_KEY *const key = &gcm_siv_ctx->ks.ks;
719 uint8_t key_material[(128 /* POLYVAL key */ + 256 /* max AES key */) / 8];
720 const size_t blocks_needed = gcm_siv_ctx->is_256 ? 6 : 4;
721
722 uint8_t counter[AES_BLOCK_SIZE];
723 OPENSSL_memset(counter, 0, AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN);
724 OPENSSL_memcpy(counter + AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN,
725 nonce, EVP_AEAD_AES_GCM_SIV_NONCE_LEN);
726 for (size_t i = 0; i < blocks_needed; i++) {
727 counter[0] = i;
728
729 uint8_t ciphertext[AES_BLOCK_SIZE];
730 gcm_siv_ctx->kgk_block(counter, ciphertext, key);
731 OPENSSL_memcpy(&key_material[i * 8], ciphertext, 8);
732 }
733
734 OPENSSL_memcpy(out_keys->auth_key, key_material, 16);
735 // Note the |ctr128_f| function uses a big-endian couner, while AES-GCM-SIV
736 // uses a little-endian counter. We ignore the return value and only use
737 // |block128_f|. This has a significant performance cost for the fallback
738 // bitsliced AES implementations (bsaes and aes_nohw).
739 //
740 // We currently do not consider AES-GCM-SIV to be performance-sensitive on
741 // client hardware. If this changes, we can write little-endian |ctr128_f|
742 // functions.
743 aes_ctr_set_key(&out_keys->enc_key.ks, NULL, &out_keys->enc_block,
744 key_material + 16, gcm_siv_ctx->is_256 ? 32 : 16);
745 }
746
aead_aes_gcm_siv_seal_scatter(const EVP_AEAD_CTX * ctx,uint8_t * out,uint8_t * out_tag,size_t * out_tag_len,size_t max_out_tag_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * extra_in,size_t extra_in_len,const uint8_t * ad,size_t ad_len)747 static int aead_aes_gcm_siv_seal_scatter(
748 const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
749 size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
750 size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
751 size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
752 const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx =
753 (struct aead_aes_gcm_siv_ctx *)&ctx->state;
754 const uint64_t in_len_64 = in_len;
755 const uint64_t ad_len_64 = ad_len;
756
757 if (in_len + EVP_AEAD_AES_GCM_SIV_TAG_LEN < in_len ||
758 in_len_64 > (UINT64_C(1) << 36) ||
759 ad_len_64 >= (UINT64_C(1) << 61)) {
760 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
761 return 0;
762 }
763
764 if (max_out_tag_len < EVP_AEAD_AES_GCM_SIV_TAG_LEN) {
765 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
766 return 0;
767 }
768
769 if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) {
770 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
771 return 0;
772 }
773
774 struct gcm_siv_record_keys keys;
775 gcm_siv_keys(gcm_siv_ctx, &keys, nonce);
776
777 uint8_t tag[16];
778 gcm_siv_polyval(tag, in, in_len, ad, ad_len, keys.auth_key, nonce);
779 keys.enc_block(tag, tag, &keys.enc_key.ks);
780
781 gcm_siv_crypt(out, in, in_len, tag, keys.enc_block, &keys.enc_key.ks);
782
783 OPENSSL_memcpy(out_tag, tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN);
784 *out_tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN;
785
786 return 1;
787 }
788
aead_aes_gcm_siv_open_gather(const EVP_AEAD_CTX * ctx,uint8_t * out,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * in_tag,size_t in_tag_len,const uint8_t * ad,size_t ad_len)789 static int aead_aes_gcm_siv_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
790 const uint8_t *nonce, size_t nonce_len,
791 const uint8_t *in, size_t in_len,
792 const uint8_t *in_tag,
793 size_t in_tag_len, const uint8_t *ad,
794 size_t ad_len) {
795 const uint64_t ad_len_64 = ad_len;
796 if (ad_len_64 >= (UINT64_C(1) << 61)) {
797 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
798 return 0;
799 }
800
801 const uint64_t in_len_64 = in_len;
802 if (in_tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN ||
803 in_len_64 > (UINT64_C(1) << 36) + AES_BLOCK_SIZE) {
804 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
805 return 0;
806 }
807
808 if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) {
809 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
810 return 0;
811 }
812
813 const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx =
814 (struct aead_aes_gcm_siv_ctx *)&ctx->state;
815
816 struct gcm_siv_record_keys keys;
817 gcm_siv_keys(gcm_siv_ctx, &keys, nonce);
818
819 gcm_siv_crypt(out, in, in_len, in_tag, keys.enc_block, &keys.enc_key.ks);
820
821 uint8_t expected_tag[EVP_AEAD_AES_GCM_SIV_TAG_LEN];
822 gcm_siv_polyval(expected_tag, out, in_len, ad, ad_len, keys.auth_key, nonce);
823 keys.enc_block(expected_tag, expected_tag, &keys.enc_key.ks);
824
825 if (CRYPTO_memcmp(expected_tag, in_tag, sizeof(expected_tag)) != 0) {
826 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
827 return 0;
828 }
829
830 return 1;
831 }
832
833 static const EVP_AEAD aead_aes_128_gcm_siv = {
834 16, // 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 static const EVP_AEAD aead_aes_256_gcm_siv = {
851 32, // key length
852 EVP_AEAD_AES_GCM_SIV_NONCE_LEN, // nonce length
853 EVP_AEAD_AES_GCM_SIV_TAG_LEN, // overhead
854 EVP_AEAD_AES_GCM_SIV_TAG_LEN, // max tag length
855 0, // seal_scatter_supports_extra_in
856
857 aead_aes_gcm_siv_init,
858 NULL /* init_with_direction */,
859 aead_aes_gcm_siv_cleanup,
860 NULL /* open */,
861 aead_aes_gcm_siv_seal_scatter,
862 aead_aes_gcm_siv_open_gather,
863 NULL /* get_iv */,
864 NULL /* tag_len */,
865 };
866
867 #if defined(AES_GCM_SIV_ASM)
868
avx_aesni_capable(void)869 static char avx_aesni_capable(void) {
870 const uint32_t ecx = OPENSSL_ia32cap_P[1];
871
872 return (ecx & (1 << (57 - 32))) != 0 /* AESNI */ &&
873 (ecx & (1 << 28)) != 0 /* AVX */;
874 }
875
EVP_aead_aes_128_gcm_siv(void)876 const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) {
877 if (avx_aesni_capable()) {
878 return &aead_aes_128_gcm_siv_asm;
879 }
880 return &aead_aes_128_gcm_siv;
881 }
882
EVP_aead_aes_256_gcm_siv(void)883 const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) {
884 if (avx_aesni_capable()) {
885 return &aead_aes_256_gcm_siv_asm;
886 }
887 return &aead_aes_256_gcm_siv;
888 }
889
890 #else
891
EVP_aead_aes_128_gcm_siv(void)892 const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) {
893 return &aead_aes_128_gcm_siv;
894 }
895
EVP_aead_aes_256_gcm_siv(void)896 const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) {
897 return &aead_aes_256_gcm_siv;
898 }
899
900 #endif // AES_GCM_SIV_ASM
901