• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   const struct aead_aes_gcm_siv_asm_ctx *gcm_siv_ctx = asm_ctx_from_ctx(ctx);
430   const size_t plaintext_len = in_len - EVP_AEAD_AES_GCM_SIV_TAG_LEN;
431   const uint8_t *const given_tag = in + plaintext_len;
432 
433   if (max_out_len < plaintext_len) {
434     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
435     return 0;
436   }
437 
438   alignas(16) uint64_t record_auth_key[2];
439   alignas(16) uint64_t record_enc_key[4];
440   aead_aes_gcm_siv_kdf(gcm_siv_ctx->is_128_bit, gcm_siv_ctx, record_auth_key,
441                        record_enc_key, nonce);
442 
443   struct aead_aes_gcm_siv_asm_ctx expanded_key;
444   if (gcm_siv_ctx->is_128_bit) {
445     aes128gcmsiv_aes_ks((const uint8_t *) record_enc_key, &expanded_key.key[0]);
446   } else {
447     aes256gcmsiv_aes_ks((const uint8_t *) record_enc_key, &expanded_key.key[0]);
448   }
449   // calculated_tag is 16*8 bytes, rather than 16 bytes, because
450   // aes[128|256]gcmsiv_dec uses the extra as scratch space.
451   alignas(16) uint8_t calculated_tag[16 * 8] = {0};
452 
453   OPENSSL_memset(calculated_tag, 0, EVP_AEAD_AES_GCM_SIV_TAG_LEN);
454   const size_t ad_blocks = ad_len / 16;
455   aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key, ad,
456                            ad_blocks);
457 
458   uint8_t scratch[16];
459   if (ad_len & 15) {
460     OPENSSL_memset(scratch, 0, sizeof(scratch));
461     OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15);
462     aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key,
463                              scratch, 1);
464   }
465 
466   alignas(16) uint8_t htable[16 * 6];
467   aesgcmsiv_htable6_init(htable, (const uint8_t *)record_auth_key);
468 
469   if (gcm_siv_ctx->is_128_bit) {
470     aes128gcmsiv_dec(in, out, calculated_tag, htable, &expanded_key,
471                      plaintext_len);
472   } else {
473     aes256gcmsiv_dec(in, out, calculated_tag, htable, &expanded_key,
474                      plaintext_len);
475   }
476 
477   if (plaintext_len & 15) {
478     aead_aes_gcm_siv_asm_crypt_last_block(gcm_siv_ctx->is_128_bit, out, in,
479                                           plaintext_len, given_tag,
480                                           &expanded_key);
481     OPENSSL_memset(scratch, 0, sizeof(scratch));
482     OPENSSL_memcpy(scratch, out + (plaintext_len & ~15), plaintext_len & 15);
483     aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key,
484                              scratch, 1);
485   }
486 
487   union {
488     uint8_t c[16];
489     struct {
490       uint64_t ad;
491       uint64_t in;
492     } bitlens;
493   } length_block;
494 
495   length_block.bitlens.ad = ad_len * 8;
496   length_block.bitlens.in = plaintext_len * 8;
497   aesgcmsiv_polyval_horner(calculated_tag, (const uint8_t *)record_auth_key,
498                            length_block.c, 1);
499 
500   for (size_t i = 0; i < 12; i++) {
501     calculated_tag[i] ^= nonce[i];
502   }
503 
504   calculated_tag[15] &= 0x7f;
505 
506   if (gcm_siv_ctx->is_128_bit) {
507     aes128gcmsiv_ecb_enc_block(calculated_tag, calculated_tag, &expanded_key);
508   } else {
509     aes256gcmsiv_ecb_enc_block(calculated_tag, calculated_tag, &expanded_key);
510   }
511 
512   if (CRYPTO_memcmp(calculated_tag, given_tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN) !=
513       0) {
514     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
515     return 0;
516   }
517 
518   *out_len = in_len - EVP_AEAD_AES_GCM_SIV_TAG_LEN;
519   return 1;
520 }
521 
522 static const EVP_AEAD aead_aes_128_gcm_siv_asm = {
523     16,                              // key length
524     EVP_AEAD_AES_GCM_SIV_NONCE_LEN,  // nonce length
525     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // overhead
526     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // max tag length
527     0,                               // seal_scatter_supports_extra_in
528 
529     aead_aes_gcm_siv_asm_init,
530     NULL /* init_with_direction */,
531     aead_aes_gcm_siv_asm_cleanup,
532     aead_aes_gcm_siv_asm_open,
533     aead_aes_gcm_siv_asm_seal_scatter,
534     NULL /* open_gather */,
535     NULL /* get_iv */,
536     NULL /* tag_len */,
537 };
538 
539 static const EVP_AEAD aead_aes_256_gcm_siv_asm = {
540     32,                              // key length
541     EVP_AEAD_AES_GCM_SIV_NONCE_LEN,  // nonce length
542     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // overhead
543     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // max tag length
544     0,                               // seal_scatter_supports_extra_in
545 
546     aead_aes_gcm_siv_asm_init,
547     NULL /* init_with_direction */,
548     aead_aes_gcm_siv_asm_cleanup,
549     aead_aes_gcm_siv_asm_open,
550     aead_aes_gcm_siv_asm_seal_scatter,
551     NULL /* open_gather */,
552     NULL /* get_iv */,
553     NULL /* tag_len */,
554 };
555 
556 #endif  // X86_64 && !NO_ASM && !WINDOWS
557 
558 struct aead_aes_gcm_siv_ctx {
559   union {
560     double align;
561     AES_KEY ks;
562   } ks;
563   block128_f kgk_block;
564   unsigned is_256:1;
565 };
566 
567 OPENSSL_STATIC_ASSERT(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
568                           sizeof(struct aead_aes_gcm_siv_ctx),
569                       "AEAD state is too small");
570 #if defined(__GNUC__) || defined(__clang__)
571 OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
572                           alignof(struct aead_aes_gcm_siv_ctx),
573                       "AEAD state has insufficient alignment");
574 #endif
575 
aead_aes_gcm_siv_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len)576 static int aead_aes_gcm_siv_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
577                                  size_t key_len, size_t tag_len) {
578   const size_t key_bits = key_len * 8;
579 
580   if (key_bits != 128 && key_bits != 256) {
581     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
582     return 0;  // EVP_AEAD_CTX_init should catch this.
583   }
584 
585   if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
586     tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN;
587   }
588   if (tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN) {
589     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
590     return 0;
591   }
592 
593   struct aead_aes_gcm_siv_ctx *gcm_siv_ctx =
594       (struct aead_aes_gcm_siv_ctx *)&ctx->state;
595   OPENSSL_memset(gcm_siv_ctx, 0, sizeof(struct aead_aes_gcm_siv_ctx));
596 
597   aes_ctr_set_key(&gcm_siv_ctx->ks.ks, NULL, &gcm_siv_ctx->kgk_block, key,
598                   key_len);
599   gcm_siv_ctx->is_256 = (key_len == 32);
600   ctx->tag_len = tag_len;
601 
602   return 1;
603 }
604 
aead_aes_gcm_siv_cleanup(EVP_AEAD_CTX * ctx)605 static void aead_aes_gcm_siv_cleanup(EVP_AEAD_CTX *ctx) {}
606 
607 // gcm_siv_crypt encrypts (or decrypts—it's the same thing) |in_len| bytes from
608 // |in| to |out|, using the block function |enc_block| with |key| in counter
609 // mode, starting at |initial_counter|. This differs from the traditional
610 // counter mode code in that the counter is handled little-endian, only the
611 // first four bytes are used and the GCM-SIV tweak to the final byte is
612 // applied. The |in| and |out| pointers may be equal but otherwise must not
613 // 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)614 static void gcm_siv_crypt(uint8_t *out, const uint8_t *in, size_t in_len,
615                           const uint8_t initial_counter[AES_BLOCK_SIZE],
616                           block128_f enc_block, const AES_KEY *key) {
617   union {
618     uint32_t w[4];
619     uint8_t c[16];
620   } counter;
621 
622   OPENSSL_memcpy(counter.c, initial_counter, AES_BLOCK_SIZE);
623   counter.c[15] |= 0x80;
624 
625   for (size_t done = 0; done < in_len;) {
626     uint8_t keystream[AES_BLOCK_SIZE];
627     enc_block(counter.c, keystream, key);
628     counter.w[0]++;
629 
630     size_t todo = AES_BLOCK_SIZE;
631     if (in_len - done < todo) {
632       todo = in_len - done;
633     }
634 
635     for (size_t i = 0; i < todo; i++) {
636       out[done + i] = keystream[i] ^ in[done + i];
637     }
638 
639     done += todo;
640   }
641 }
642 
643 // gcm_siv_polyval evaluates POLYVAL at |auth_key| on the given plaintext and
644 // 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])645 static void gcm_siv_polyval(
646     uint8_t out_tag[16], const uint8_t *in, size_t in_len, const uint8_t *ad,
647     size_t ad_len, const uint8_t auth_key[16],
648     const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) {
649   struct polyval_ctx polyval_ctx;
650   CRYPTO_POLYVAL_init(&polyval_ctx, auth_key);
651 
652   CRYPTO_POLYVAL_update_blocks(&polyval_ctx, ad, ad_len & ~15);
653 
654   uint8_t scratch[16];
655   if (ad_len & 15) {
656     OPENSSL_memset(scratch, 0, sizeof(scratch));
657     OPENSSL_memcpy(scratch, &ad[ad_len & ~15], ad_len & 15);
658     CRYPTO_POLYVAL_update_blocks(&polyval_ctx, scratch, sizeof(scratch));
659   }
660 
661   CRYPTO_POLYVAL_update_blocks(&polyval_ctx, in, in_len & ~15);
662   if (in_len & 15) {
663     OPENSSL_memset(scratch, 0, sizeof(scratch));
664     OPENSSL_memcpy(scratch, &in[in_len & ~15], in_len & 15);
665     CRYPTO_POLYVAL_update_blocks(&polyval_ctx, scratch, sizeof(scratch));
666   }
667 
668   union {
669     uint8_t c[16];
670     struct {
671       uint64_t ad;
672       uint64_t in;
673     } bitlens;
674   } length_block;
675 
676   length_block.bitlens.ad = ad_len * 8;
677   length_block.bitlens.in = in_len * 8;
678   CRYPTO_POLYVAL_update_blocks(&polyval_ctx, length_block.c,
679                                sizeof(length_block));
680 
681   CRYPTO_POLYVAL_finish(&polyval_ctx, out_tag);
682   for (size_t i = 0; i < EVP_AEAD_AES_GCM_SIV_NONCE_LEN; i++) {
683     out_tag[i] ^= nonce[i];
684   }
685   out_tag[15] &= 0x7f;
686 }
687 
688 // gcm_siv_record_keys contains the keys used for a specific GCM-SIV record.
689 struct gcm_siv_record_keys {
690   uint8_t auth_key[16];
691   union {
692     double align;
693     AES_KEY ks;
694   } enc_key;
695   block128_f enc_block;
696 };
697 
698 // gcm_siv_keys calculates the keys for a specific GCM-SIV record with the
699 // 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])700 static void gcm_siv_keys(
701     const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx,
702     struct gcm_siv_record_keys *out_keys,
703     const uint8_t nonce[EVP_AEAD_AES_GCM_SIV_NONCE_LEN]) {
704   const AES_KEY *const key = &gcm_siv_ctx->ks.ks;
705   uint8_t key_material[(128 /* POLYVAL key */ + 256 /* max AES key */) / 8];
706   const size_t blocks_needed = gcm_siv_ctx->is_256 ? 6 : 4;
707 
708   uint8_t counter[AES_BLOCK_SIZE];
709   OPENSSL_memset(counter, 0, AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN);
710   OPENSSL_memcpy(counter + AES_BLOCK_SIZE - EVP_AEAD_AES_GCM_SIV_NONCE_LEN,
711                  nonce, EVP_AEAD_AES_GCM_SIV_NONCE_LEN);
712   for (size_t i = 0; i < blocks_needed; i++) {
713     counter[0] = i;
714 
715     uint8_t ciphertext[AES_BLOCK_SIZE];
716     gcm_siv_ctx->kgk_block(counter, ciphertext, key);
717     OPENSSL_memcpy(&key_material[i * 8], ciphertext, 8);
718   }
719 
720   OPENSSL_memcpy(out_keys->auth_key, key_material, 16);
721   aes_ctr_set_key(&out_keys->enc_key.ks, NULL, &out_keys->enc_block,
722                   key_material + 16, gcm_siv_ctx->is_256 ? 32 : 16);
723 }
724 
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)725 static int aead_aes_gcm_siv_seal_scatter(
726     const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
727     size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
728     size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
729     size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
730   const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx =
731       (struct aead_aes_gcm_siv_ctx *)&ctx->state;
732   const uint64_t in_len_64 = in_len;
733   const uint64_t ad_len_64 = ad_len;
734 
735   if (in_len + EVP_AEAD_AES_GCM_SIV_TAG_LEN < in_len ||
736       in_len_64 > (UINT64_C(1) << 36) ||
737       ad_len_64 >= (UINT64_C(1) << 61)) {
738     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
739     return 0;
740   }
741 
742   if (max_out_tag_len < EVP_AEAD_AES_GCM_SIV_TAG_LEN) {
743     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
744     return 0;
745   }
746 
747   if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) {
748     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
749     return 0;
750   }
751 
752   struct gcm_siv_record_keys keys;
753   gcm_siv_keys(gcm_siv_ctx, &keys, nonce);
754 
755   uint8_t tag[16];
756   gcm_siv_polyval(tag, in, in_len, ad, ad_len, keys.auth_key, nonce);
757   keys.enc_block(tag, tag, &keys.enc_key.ks);
758 
759   gcm_siv_crypt(out, in, in_len, tag, keys.enc_block, &keys.enc_key.ks);
760 
761   OPENSSL_memcpy(out_tag, tag, EVP_AEAD_AES_GCM_SIV_TAG_LEN);
762   *out_tag_len = EVP_AEAD_AES_GCM_SIV_TAG_LEN;
763 
764   return 1;
765 }
766 
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)767 static int aead_aes_gcm_siv_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
768                                         const uint8_t *nonce, size_t nonce_len,
769                                         const uint8_t *in, size_t in_len,
770                                         const uint8_t *in_tag,
771                                         size_t in_tag_len, const uint8_t *ad,
772                                         size_t ad_len) {
773   const uint64_t ad_len_64 = ad_len;
774   if (ad_len_64 >= (UINT64_C(1) << 61)) {
775     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
776     return 0;
777   }
778 
779   const uint64_t in_len_64 = in_len;
780   if (in_tag_len != EVP_AEAD_AES_GCM_SIV_TAG_LEN ||
781       in_len_64 > (UINT64_C(1) << 36) + AES_BLOCK_SIZE) {
782     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
783     return 0;
784   }
785 
786   if (nonce_len != EVP_AEAD_AES_GCM_SIV_NONCE_LEN) {
787     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
788     return 0;
789   }
790 
791   const struct aead_aes_gcm_siv_ctx *gcm_siv_ctx =
792       (struct aead_aes_gcm_siv_ctx *)&ctx->state;
793 
794   struct gcm_siv_record_keys keys;
795   gcm_siv_keys(gcm_siv_ctx, &keys, nonce);
796 
797   gcm_siv_crypt(out, in, in_len, in_tag, keys.enc_block, &keys.enc_key.ks);
798 
799   uint8_t expected_tag[EVP_AEAD_AES_GCM_SIV_TAG_LEN];
800   gcm_siv_polyval(expected_tag, out, in_len, ad, ad_len, keys.auth_key, nonce);
801   keys.enc_block(expected_tag, expected_tag, &keys.enc_key.ks);
802 
803   if (CRYPTO_memcmp(expected_tag, in_tag, sizeof(expected_tag)) != 0) {
804     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
805     return 0;
806   }
807 
808   return 1;
809 }
810 
811 static const EVP_AEAD aead_aes_128_gcm_siv = {
812     16,                              // key length
813     EVP_AEAD_AES_GCM_SIV_NONCE_LEN,  // nonce length
814     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // overhead
815     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // max tag length
816     0,                               // seal_scatter_supports_extra_in
817 
818     aead_aes_gcm_siv_init,
819     NULL /* init_with_direction */,
820     aead_aes_gcm_siv_cleanup,
821     NULL /* open */,
822     aead_aes_gcm_siv_seal_scatter,
823     aead_aes_gcm_siv_open_gather,
824     NULL /* get_iv */,
825     NULL /* tag_len */,
826 };
827 
828 static const EVP_AEAD aead_aes_256_gcm_siv = {
829     32,                              // key length
830     EVP_AEAD_AES_GCM_SIV_NONCE_LEN,  // nonce length
831     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // overhead
832     EVP_AEAD_AES_GCM_SIV_TAG_LEN,    // max tag length
833     0,                               // seal_scatter_supports_extra_in
834 
835     aead_aes_gcm_siv_init,
836     NULL /* init_with_direction */,
837     aead_aes_gcm_siv_cleanup,
838     NULL /* open */,
839     aead_aes_gcm_siv_seal_scatter,
840     aead_aes_gcm_siv_open_gather,
841     NULL /* get_iv */,
842     NULL /* tag_len */,
843 };
844 
845 #if defined(AES_GCM_SIV_ASM)
846 
avx_aesni_capable(void)847 static char avx_aesni_capable(void) {
848   const uint32_t ecx = OPENSSL_ia32cap_P[1];
849 
850   return (ecx & (1 << (57 - 32))) != 0 /* AESNI */ &&
851          (ecx & (1 << 28)) != 0 /* AVX */;
852 }
853 
EVP_aead_aes_128_gcm_siv(void)854 const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) {
855   if (avx_aesni_capable()) {
856     return &aead_aes_128_gcm_siv_asm;
857   }
858   return &aead_aes_128_gcm_siv;
859 }
860 
EVP_aead_aes_256_gcm_siv(void)861 const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) {
862   if (avx_aesni_capable()) {
863     return &aead_aes_256_gcm_siv_asm;
864   }
865   return &aead_aes_256_gcm_siv;
866 }
867 
868 #else
869 
EVP_aead_aes_128_gcm_siv(void)870 const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void) {
871   return &aead_aes_128_gcm_siv;
872 }
873 
EVP_aead_aes_256_gcm_siv(void)874 const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void) {
875   return &aead_aes_256_gcm_siv;
876 }
877 
878 #endif  // AES_GCM_SIV_ASM
879