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