1// Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <assert.h>
16#include <limits.h>
17#include <string.h>
18
19#include <openssl/aead.h>
20#include <openssl/aes.h>
21#include <openssl/cipher.h>
22#include <openssl/err.h>
23#include <openssl/mem.h>
24#include <openssl/nid.h>
25
26#include "../../internal.h"
27#include "../aes/internal.h"
28#include "../bcm_interface.h"
29#include "../delocate.h"
30#include "../service_indicator/internal.h"
31#include "internal.h"
32
33
34#define AES_GCM_NONCE_LENGTH 12
35
36typedef struct {
37  union {
38    double align;
39    AES_KEY ks;
40  } ks;
41  block128_f block;
42  union {
43    cbc128_f cbc;
44    ctr128_f ctr;
45  } stream;
46} EVP_AES_KEY;
47
48typedef struct {
49  GCM128_KEY key;
50  GCM128_CONTEXT gcm;
51  int key_set;  // Set if key initialised
52  int iv_set;   // Set if an iv is set
53  uint8_t *iv;  // Temporary IV store
54  int ivlen;    // IV length
55  int taglen;
56  int iv_gen;  // It is OK to generate IVs
57  ctr128_f ctr;
58} EVP_AES_GCM_CTX;
59
60static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
61                        const uint8_t *iv, int enc) {
62  int ret;
63  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
64  const int mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
65
66  if (mode == EVP_CIPH_CTR_MODE) {
67    switch (ctx->key_len) {
68      case 16:
69        boringssl_fips_inc_counter(fips_counter_evp_aes_128_ctr);
70        break;
71
72      case 32:
73        boringssl_fips_inc_counter(fips_counter_evp_aes_256_ctr);
74        break;
75    }
76  }
77
78  if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
79    if (hwaes_capable()) {
80      ret = aes_hw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
81      dat->block = aes_hw_decrypt;
82      dat->stream.cbc = NULL;
83      if (mode == EVP_CIPH_CBC_MODE) {
84        dat->stream.cbc = aes_hw_cbc_encrypt;
85      }
86    } else if (bsaes_capable() && mode == EVP_CIPH_CBC_MODE) {
87      assert(vpaes_capable());
88      ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
89      if (ret == 0) {
90        vpaes_decrypt_key_to_bsaes(&dat->ks.ks, &dat->ks.ks);
91      }
92      // If |dat->stream.cbc| is provided, |dat->block| is never used.
93      dat->block = NULL;
94      dat->stream.cbc = bsaes_cbc_encrypt;
95    } else if (vpaes_capable()) {
96      ret = vpaes_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
97      dat->block = vpaes_decrypt;
98      dat->stream.cbc = NULL;
99#if defined(VPAES_CBC)
100      if (mode == EVP_CIPH_CBC_MODE) {
101        dat->stream.cbc = vpaes_cbc_encrypt;
102      }
103#endif
104    } else {
105      ret = aes_nohw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
106      dat->block = aes_nohw_decrypt;
107      dat->stream.cbc = NULL;
108      if (mode == EVP_CIPH_CBC_MODE) {
109        dat->stream.cbc = aes_nohw_cbc_encrypt;
110      }
111    }
112  } else if (hwaes_capable()) {
113    ret = aes_hw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
114    dat->block = aes_hw_encrypt;
115    dat->stream.cbc = NULL;
116    if (mode == EVP_CIPH_CBC_MODE) {
117      dat->stream.cbc = aes_hw_cbc_encrypt;
118    } else if (mode == EVP_CIPH_CTR_MODE) {
119      dat->stream.ctr = aes_hw_ctr32_encrypt_blocks;
120    }
121  } else if (vpaes_capable()) {
122    ret = vpaes_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
123    dat->block = vpaes_encrypt;
124    dat->stream.cbc = NULL;
125#if defined(VPAES_CBC)
126    if (mode == EVP_CIPH_CBC_MODE) {
127      dat->stream.cbc = vpaes_cbc_encrypt;
128    }
129#endif
130    if (mode == EVP_CIPH_CTR_MODE) {
131#if defined(BSAES)
132      assert(bsaes_capable());
133      dat->stream.ctr = vpaes_ctr32_encrypt_blocks_with_bsaes;
134#else
135      dat->stream.ctr = vpaes_ctr32_encrypt_blocks;
136#endif
137    }
138  } else {
139    ret = aes_nohw_set_encrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
140    dat->block = aes_nohw_encrypt;
141    dat->stream.cbc = NULL;
142    if (mode == EVP_CIPH_CBC_MODE) {
143      dat->stream.cbc = aes_nohw_cbc_encrypt;
144    } else if (mode == EVP_CIPH_CTR_MODE) {
145      dat->stream.ctr = aes_nohw_ctr32_encrypt_blocks;
146    }
147  }
148
149  if (ret < 0) {
150    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_AES_KEY_SETUP_FAILED);
151    return 0;
152  }
153
154  return 1;
155}
156
157static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
158                          size_t len) {
159  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
160
161  if (dat->stream.cbc) {
162    (*dat->stream.cbc)(in, out, len, &dat->ks.ks, ctx->iv, ctx->encrypt);
163  } else if (ctx->encrypt) {
164    CRYPTO_cbc128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
165  } else {
166    CRYPTO_cbc128_decrypt(in, out, len, &dat->ks.ks, ctx->iv, dat->block);
167  }
168
169  return 1;
170}
171
172static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
173                          size_t len) {
174  size_t bl = ctx->cipher->block_size;
175  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
176
177  if (len < bl) {
178    return 1;
179  }
180
181  len -= bl;
182  for (size_t i = 0; i <= len; i += bl) {
183    (*dat->block)(in + i, out + i, &dat->ks.ks);
184  }
185
186  return 1;
187}
188
189static int aes_ctr_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
190                          size_t len) {
191  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
192  CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks.ks, ctx->iv, ctx->buf,
193                              &ctx->num, dat->stream.ctr);
194  return 1;
195}
196
197static int aes_ofb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
198                          size_t len) {
199  EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
200
201  CRYPTO_ofb128_encrypt(in, out, len, &dat->ks.ks, ctx->iv, &ctx->num,
202                        dat->block);
203  return 1;
204}
205
206static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
207                            const uint8_t *iv, int enc) {
208  EVP_AES_GCM_CTX *gctx = reinterpret_cast<EVP_AES_GCM_CTX *>(ctx->cipher_data);
209  if (!iv && !key) {
210    return 1;
211  }
212
213  // We must configure first the key, then the IV, but the caller may pass both
214  // together, or separately in either order.
215  if (key) {
216    OPENSSL_memset(&gctx->gcm, 0, sizeof(gctx->gcm));
217    CRYPTO_gcm128_init_aes_key(&gctx->key, key, ctx->key_len);
218    // Use the IV if specified. Otherwise, use the saved IV, if any.
219    if (iv == NULL && gctx->iv_set) {
220      iv = gctx->iv;
221    }
222    if (iv) {
223      CRYPTO_gcm128_init_ctx(&gctx->key, &gctx->gcm, iv, gctx->ivlen);
224      gctx->iv_set = 1;
225    }
226    gctx->key_set = 1;
227  } else {
228    if (gctx->key_set) {
229      CRYPTO_gcm128_init_ctx(&gctx->key, &gctx->gcm, iv, gctx->ivlen);
230    } else {
231      // The caller specified the IV before the key. Save the IV for later.
232      OPENSSL_memcpy(gctx->iv, iv, gctx->ivlen);
233    }
234    gctx->iv_set = 1;
235    gctx->iv_gen = 0;
236  }
237  return 1;
238}
239
240static void aes_gcm_cleanup(EVP_CIPHER_CTX *c) {
241  EVP_AES_GCM_CTX *gctx = reinterpret_cast<EVP_AES_GCM_CTX *>(c->cipher_data);
242  OPENSSL_cleanse(&gctx->key, sizeof(gctx->key));
243  OPENSSL_cleanse(&gctx->gcm, sizeof(gctx->gcm));
244  if (gctx->iv != c->iv) {
245    OPENSSL_free(gctx->iv);
246  }
247}
248
249static int aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
250  EVP_AES_GCM_CTX *gctx = reinterpret_cast<EVP_AES_GCM_CTX *>(c->cipher_data);
251  switch (type) {
252    case EVP_CTRL_INIT:
253      gctx->key_set = 0;
254      gctx->iv_set = 0;
255      gctx->ivlen = c->cipher->iv_len;
256      gctx->iv = c->iv;
257      gctx->taglen = -1;
258      gctx->iv_gen = 0;
259      return 1;
260
261    case EVP_CTRL_AEAD_SET_IVLEN:
262      if (arg <= 0) {
263        return 0;
264      }
265
266      // Allocate memory for IV if needed
267      if (arg > EVP_MAX_IV_LENGTH && arg > gctx->ivlen) {
268        if (gctx->iv != c->iv) {
269          OPENSSL_free(gctx->iv);
270        }
271        gctx->iv = reinterpret_cast<uint8_t *>(OPENSSL_malloc(arg));
272        if (!gctx->iv) {
273          return 0;
274        }
275      }
276      gctx->ivlen = arg;
277      return 1;
278
279    case EVP_CTRL_GET_IVLEN:
280      *(int *)ptr = gctx->ivlen;
281      return 1;
282
283    case EVP_CTRL_AEAD_SET_TAG:
284      if (arg <= 0 || arg > 16 || c->encrypt) {
285        return 0;
286      }
287      OPENSSL_memcpy(c->buf, ptr, arg);
288      gctx->taglen = arg;
289      return 1;
290
291    case EVP_CTRL_AEAD_GET_TAG:
292      if (arg <= 0 || arg > 16 || !c->encrypt || gctx->taglen < 0) {
293        return 0;
294      }
295      OPENSSL_memcpy(ptr, c->buf, arg);
296      return 1;
297
298    case EVP_CTRL_AEAD_SET_IV_FIXED:
299      // Special case: -1 length restores whole IV
300      if (arg == -1) {
301        OPENSSL_memcpy(gctx->iv, ptr, gctx->ivlen);
302        gctx->iv_gen = 1;
303        return 1;
304      }
305      // Fixed field must be at least 4 bytes and invocation field
306      // at least 8.
307      if (arg < 4 || (gctx->ivlen - arg) < 8) {
308        return 0;
309      }
310      OPENSSL_memcpy(gctx->iv, ptr, arg);
311      if (c->encrypt) {
312        // |BCM_rand_bytes| calls within the fipsmodule should be wrapped with
313        // state lock functions to avoid updating the service indicator with the
314        // DRBG functions.
315        FIPS_service_indicator_lock_state();
316        BCM_rand_bytes(gctx->iv + arg, gctx->ivlen - arg);
317        FIPS_service_indicator_unlock_state();
318      }
319      gctx->iv_gen = 1;
320      return 1;
321
322    case EVP_CTRL_GCM_IV_GEN: {
323      if (gctx->iv_gen == 0 || gctx->key_set == 0) {
324        return 0;
325      }
326      CRYPTO_gcm128_init_ctx(&gctx->key, &gctx->gcm, gctx->iv, gctx->ivlen);
327      if (arg <= 0 || arg > gctx->ivlen) {
328        arg = gctx->ivlen;
329      }
330      OPENSSL_memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
331      // Invocation field will be at least 8 bytes in size, so no need to check
332      // wrap around or increment more than last 8 bytes.
333      uint8_t *ctr = gctx->iv + gctx->ivlen - 8;
334      CRYPTO_store_u64_be(ctr, CRYPTO_load_u64_be(ctr) + 1);
335      gctx->iv_set = 1;
336      return 1;
337    }
338
339    case EVP_CTRL_GCM_SET_IV_INV:
340      if (gctx->iv_gen == 0 || gctx->key_set == 0 || c->encrypt) {
341        return 0;
342      }
343      OPENSSL_memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
344      CRYPTO_gcm128_init_ctx(&gctx->key, &gctx->gcm, gctx->iv, gctx->ivlen);
345      gctx->iv_set = 1;
346      return 1;
347
348    case EVP_CTRL_COPY: {
349      EVP_CIPHER_CTX *out = reinterpret_cast<EVP_CIPHER_CTX *>(ptr);
350      EVP_AES_GCM_CTX *gctx_out =
351          reinterpret_cast<EVP_AES_GCM_CTX *>(out->cipher_data);
352      if (gctx->iv == c->iv) {
353        gctx_out->iv = out->iv;
354      } else {
355        gctx_out->iv =
356            reinterpret_cast<uint8_t *>(OPENSSL_memdup(gctx->iv, gctx->ivlen));
357        if (!gctx_out->iv) {
358          return 0;
359        }
360      }
361      return 1;
362    }
363
364    default:
365      return -1;
366  }
367}
368
369static int aes_gcm_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
370                          size_t len) {
371  EVP_AES_GCM_CTX *gctx = reinterpret_cast<EVP_AES_GCM_CTX *>(ctx->cipher_data);
372
373  // If not set up, return error
374  if (!gctx->key_set) {
375    return -1;
376  }
377  if (!gctx->iv_set) {
378    return -1;
379  }
380
381  if (len > INT_MAX) {
382    // This function signature can only express up to |INT_MAX| bytes encrypted.
383    //
384    // TODO(https://crbug.com/boringssl/494): Make the internal |EVP_CIPHER|
385    // calling convention |size_t|-clean.
386    return -1;
387  }
388
389  if (in) {
390    if (out == NULL) {
391      if (!CRYPTO_gcm128_aad(&gctx->key, &gctx->gcm, in, len)) {
392        return -1;
393      }
394    } else if (ctx->encrypt) {
395      if (!CRYPTO_gcm128_encrypt(&gctx->key, &gctx->gcm, in, out, len)) {
396        return -1;
397      }
398    } else {
399      if (!CRYPTO_gcm128_decrypt(&gctx->key, &gctx->gcm, in, out, len)) {
400        return -1;
401      }
402    }
403    return (int)len;
404  } else {
405    if (!ctx->encrypt) {
406      if (gctx->taglen < 0 || !CRYPTO_gcm128_finish(&gctx->key, &gctx->gcm,
407                                                    ctx->buf, gctx->taglen)) {
408        return -1;
409      }
410      gctx->iv_set = 0;
411      return 0;
412    }
413    CRYPTO_gcm128_tag(&gctx->key, &gctx->gcm, ctx->buf, 16);
414    gctx->taglen = 16;
415    // Don't reuse the IV
416    gctx->iv_set = 0;
417    return 0;
418  }
419}
420
421DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_128_cbc) {
422  memset(out, 0, sizeof(EVP_CIPHER));
423
424  out->nid = NID_aes_128_cbc;
425  out->block_size = 16;
426  out->key_len = 16;
427  out->iv_len = 16;
428  out->ctx_size = sizeof(EVP_AES_KEY);
429  out->flags = EVP_CIPH_CBC_MODE;
430  out->init = aes_init_key;
431  out->cipher = aes_cbc_cipher;
432}
433
434DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_128_ctr) {
435  memset(out, 0, sizeof(EVP_CIPHER));
436
437  out->nid = NID_aes_128_ctr;
438  out->block_size = 1;
439  out->key_len = 16;
440  out->iv_len = 16;
441  out->ctx_size = sizeof(EVP_AES_KEY);
442  out->flags = EVP_CIPH_CTR_MODE;
443  out->init = aes_init_key;
444  out->cipher = aes_ctr_cipher;
445}
446
447DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_ecb_generic) {
448  memset(out, 0, sizeof(EVP_CIPHER));
449
450  out->nid = NID_aes_128_ecb;
451  out->block_size = 16;
452  out->key_len = 16;
453  out->ctx_size = sizeof(EVP_AES_KEY);
454  out->flags = EVP_CIPH_ECB_MODE;
455  out->init = aes_init_key;
456  out->cipher = aes_ecb_cipher;
457}
458
459DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_128_ofb) {
460  memset(out, 0, sizeof(EVP_CIPHER));
461
462  out->nid = NID_aes_128_ofb128;
463  out->block_size = 1;
464  out->key_len = 16;
465  out->iv_len = 16;
466  out->ctx_size = sizeof(EVP_AES_KEY);
467  out->flags = EVP_CIPH_OFB_MODE;
468  out->init = aes_init_key;
469  out->cipher = aes_ofb_cipher;
470}
471
472DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_128_gcm) {
473  memset(out, 0, sizeof(EVP_CIPHER));
474
475  out->nid = NID_aes_128_gcm;
476  out->block_size = 1;
477  out->key_len = 16;
478  out->iv_len = AES_GCM_NONCE_LENGTH;
479  out->ctx_size = sizeof(EVP_AES_GCM_CTX);
480  out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
481               EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
482               EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
483  out->init = aes_gcm_init_key;
484  out->cipher = aes_gcm_cipher;
485  out->cleanup = aes_gcm_cleanup;
486  out->ctrl = aes_gcm_ctrl;
487}
488
489DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_192_cbc) {
490  memset(out, 0, sizeof(EVP_CIPHER));
491
492  out->nid = NID_aes_192_cbc;
493  out->block_size = 16;
494  out->key_len = 24;
495  out->iv_len = 16;
496  out->ctx_size = sizeof(EVP_AES_KEY);
497  out->flags = EVP_CIPH_CBC_MODE;
498  out->init = aes_init_key;
499  out->cipher = aes_cbc_cipher;
500}
501
502DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_192_ctr) {
503  memset(out, 0, sizeof(EVP_CIPHER));
504
505  out->nid = NID_aes_192_ctr;
506  out->block_size = 1;
507  out->key_len = 24;
508  out->iv_len = 16;
509  out->ctx_size = sizeof(EVP_AES_KEY);
510  out->flags = EVP_CIPH_CTR_MODE;
511  out->init = aes_init_key;
512  out->cipher = aes_ctr_cipher;
513}
514
515DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_ecb_generic) {
516  memset(out, 0, sizeof(EVP_CIPHER));
517
518  out->nid = NID_aes_192_ecb;
519  out->block_size = 16;
520  out->key_len = 24;
521  out->ctx_size = sizeof(EVP_AES_KEY);
522  out->flags = EVP_CIPH_ECB_MODE;
523  out->init = aes_init_key;
524  out->cipher = aes_ecb_cipher;
525}
526
527DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_192_ofb) {
528  memset(out, 0, sizeof(EVP_CIPHER));
529
530  out->nid = NID_aes_192_ofb128;
531  out->block_size = 1;
532  out->key_len = 24;
533  out->iv_len = 16;
534  out->ctx_size = sizeof(EVP_AES_KEY);
535  out->flags = EVP_CIPH_OFB_MODE;
536  out->init = aes_init_key;
537  out->cipher = aes_ofb_cipher;
538}
539
540DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_192_gcm) {
541  memset(out, 0, sizeof(EVP_CIPHER));
542
543  out->nid = NID_aes_192_gcm;
544  out->block_size = 1;
545  out->key_len = 24;
546  out->iv_len = AES_GCM_NONCE_LENGTH;
547  out->ctx_size = sizeof(EVP_AES_GCM_CTX);
548  out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
549               EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
550               EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
551  out->init = aes_gcm_init_key;
552  out->cipher = aes_gcm_cipher;
553  out->cleanup = aes_gcm_cleanup;
554  out->ctrl = aes_gcm_ctrl;
555}
556
557DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_256_cbc) {
558  memset(out, 0, sizeof(EVP_CIPHER));
559
560  out->nid = NID_aes_256_cbc;
561  out->block_size = 16;
562  out->key_len = 32;
563  out->iv_len = 16;
564  out->ctx_size = sizeof(EVP_AES_KEY);
565  out->flags = EVP_CIPH_CBC_MODE;
566  out->init = aes_init_key;
567  out->cipher = aes_cbc_cipher;
568}
569
570DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_256_ctr) {
571  memset(out, 0, sizeof(EVP_CIPHER));
572
573  out->nid = NID_aes_256_ctr;
574  out->block_size = 1;
575  out->key_len = 32;
576  out->iv_len = 16;
577  out->ctx_size = sizeof(EVP_AES_KEY);
578  out->flags = EVP_CIPH_CTR_MODE;
579  out->init = aes_init_key;
580  out->cipher = aes_ctr_cipher;
581}
582
583DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_ecb_generic) {
584  memset(out, 0, sizeof(EVP_CIPHER));
585
586  out->nid = NID_aes_256_ecb;
587  out->block_size = 16;
588  out->key_len = 32;
589  out->ctx_size = sizeof(EVP_AES_KEY);
590  out->flags = EVP_CIPH_ECB_MODE;
591  out->init = aes_init_key;
592  out->cipher = aes_ecb_cipher;
593}
594
595DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_256_ofb) {
596  memset(out, 0, sizeof(EVP_CIPHER));
597
598  out->nid = NID_aes_256_ofb128;
599  out->block_size = 1;
600  out->key_len = 32;
601  out->iv_len = 16;
602  out->ctx_size = sizeof(EVP_AES_KEY);
603  out->flags = EVP_CIPH_OFB_MODE;
604  out->init = aes_init_key;
605  out->cipher = aes_ofb_cipher;
606}
607
608DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_aes_256_gcm) {
609  memset(out, 0, sizeof(EVP_CIPHER));
610
611  out->nid = NID_aes_256_gcm;
612  out->block_size = 1;
613  out->key_len = 32;
614  out->iv_len = AES_GCM_NONCE_LENGTH;
615  out->ctx_size = sizeof(EVP_AES_GCM_CTX);
616  out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
617               EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
618               EVP_CIPH_CTRL_INIT | EVP_CIPH_FLAG_AEAD_CIPHER;
619  out->init = aes_gcm_init_key;
620  out->cipher = aes_gcm_cipher;
621  out->cleanup = aes_gcm_cleanup;
622  out->ctrl = aes_gcm_ctrl;
623}
624
625#if defined(HWAES_ECB)
626
627static int aes_hw_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
628                             const uint8_t *in, size_t len) {
629  size_t bl = ctx->cipher->block_size;
630
631  if (len < bl) {
632    return 1;
633  }
634
635  aes_hw_ecb_encrypt(in, out, len,
636                     reinterpret_cast<const AES_KEY *>(ctx->cipher_data),
637                     ctx->encrypt);
638
639  return 1;
640}
641
642DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_128_ecb) {
643  memset(out, 0, sizeof(EVP_CIPHER));
644
645  out->nid = NID_aes_128_ecb;
646  out->block_size = 16;
647  out->key_len = 16;
648  out->ctx_size = sizeof(EVP_AES_KEY);
649  out->flags = EVP_CIPH_ECB_MODE;
650  out->init = aes_init_key;
651  out->cipher = aes_hw_ecb_cipher;
652}
653
654DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_192_ecb) {
655  memset(out, 0, sizeof(EVP_CIPHER));
656
657  out->nid = NID_aes_192_ecb;
658  out->block_size = 16;
659  out->key_len = 24;
660  out->ctx_size = sizeof(EVP_AES_KEY);
661  out->flags = EVP_CIPH_ECB_MODE;
662  out->init = aes_init_key;
663  out->cipher = aes_hw_ecb_cipher;
664}
665
666DEFINE_LOCAL_DATA(EVP_CIPHER, aes_hw_256_ecb) {
667  memset(out, 0, sizeof(EVP_CIPHER));
668
669  out->nid = NID_aes_256_ecb;
670  out->block_size = 16;
671  out->key_len = 32;
672  out->ctx_size = sizeof(EVP_AES_KEY);
673  out->flags = EVP_CIPH_ECB_MODE;
674  out->init = aes_init_key;
675  out->cipher = aes_hw_ecb_cipher;
676}
677
678#define EVP_ECB_CIPHER_FUNCTION(keybits)            \
679  const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
680    if (hwaes_capable()) {                          \
681      return aes_hw_##keybits##_ecb();              \
682    }                                               \
683    return aes_##keybits##_ecb_generic();           \
684  }
685
686#else
687
688#define EVP_ECB_CIPHER_FUNCTION(keybits)            \
689  const EVP_CIPHER *EVP_aes_##keybits##_ecb(void) { \
690    return aes_##keybits##_ecb_generic();           \
691  }
692
693#endif  // HWAES_ECB
694
695EVP_ECB_CIPHER_FUNCTION(128)
696EVP_ECB_CIPHER_FUNCTION(192)
697EVP_ECB_CIPHER_FUNCTION(256)
698
699
700#define EVP_AEAD_AES_GCM_TAG_LEN 16
701
702namespace {
703struct aead_aes_gcm_ctx {
704  GCM128_KEY key;
705};
706}  // namespace
707
708static int aead_aes_gcm_init_impl(struct aead_aes_gcm_ctx *gcm_ctx,
709                                  size_t *out_tag_len, const uint8_t *key,
710                                  size_t key_len, size_t tag_len) {
711  const size_t key_bits = key_len * 8;
712  if (key_bits != 128 && key_bits != 192 && key_bits != 256) {
713    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
714    return 0;  // EVP_AEAD_CTX_init should catch this.
715  }
716
717  if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
718    tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
719  }
720
721  if (tag_len > EVP_AEAD_AES_GCM_TAG_LEN) {
722    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
723    return 0;
724  }
725
726  CRYPTO_gcm128_init_aes_key(&gcm_ctx->key, key, key_len);
727  *out_tag_len = tag_len;
728  return 1;
729}
730
731static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
732                  sizeof(struct aead_aes_gcm_ctx),
733              "AEAD state is too small");
734static_assert(alignof(union evp_aead_ctx_st_state) >=
735                  alignof(struct aead_aes_gcm_ctx),
736              "AEAD state has insufficient alignment");
737
738static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
739                             size_t key_len, size_t requested_tag_len) {
740  struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *)&ctx->state;
741
742  size_t actual_tag_len;
743  if (!aead_aes_gcm_init_impl(gcm_ctx, &actual_tag_len, key, key_len,
744                              requested_tag_len)) {
745    return 0;
746  }
747
748  ctx->tag_len = actual_tag_len;
749  return 1;
750}
751
752static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) {}
753
754static int aead_aes_gcm_seal_scatter_impl(
755    const struct aead_aes_gcm_ctx *gcm_ctx, uint8_t *out, uint8_t *out_tag,
756    size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
757    size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
758    size_t extra_in_len, const uint8_t *ad, size_t ad_len, size_t tag_len) {
759  if (extra_in_len + tag_len < tag_len) {
760    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
761    return 0;
762  }
763  if (max_out_tag_len < extra_in_len + tag_len) {
764    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
765    return 0;
766  }
767  if (nonce_len == 0) {
768    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
769    return 0;
770  }
771
772  const GCM128_KEY *key = &gcm_ctx->key;
773  GCM128_CONTEXT gcm;
774  CRYPTO_gcm128_init_ctx(key, &gcm, nonce, nonce_len);
775
776  if (ad_len > 0 && !CRYPTO_gcm128_aad(key, &gcm, ad, ad_len)) {
777    return 0;
778  }
779
780  if (!CRYPTO_gcm128_encrypt(key, &gcm, in, out, in_len)) {
781    return 0;
782  }
783
784  if (extra_in_len > 0 &&
785      !CRYPTO_gcm128_encrypt(key, &gcm, extra_in, out_tag, extra_in_len)) {
786    return 0;
787  }
788
789  CRYPTO_gcm128_tag(key, &gcm, out_tag + extra_in_len, tag_len);
790  *out_tag_len = tag_len + extra_in_len;
791
792  return 1;
793}
794
795static int aead_aes_gcm_seal_scatter(
796    const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
797    size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
798    size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
799    size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
800  const struct aead_aes_gcm_ctx *gcm_ctx =
801      (const struct aead_aes_gcm_ctx *)&ctx->state;
802  return aead_aes_gcm_seal_scatter_impl(
803      gcm_ctx, out, out_tag, out_tag_len, max_out_tag_len, nonce, nonce_len, in,
804      in_len, extra_in, extra_in_len, ad, ad_len, ctx->tag_len);
805}
806
807static int aead_aes_gcm_open_gather_impl(const struct aead_aes_gcm_ctx *gcm_ctx,
808                                         uint8_t *out, const uint8_t *nonce,
809                                         size_t nonce_len, const uint8_t *in,
810                                         size_t in_len, const uint8_t *in_tag,
811                                         size_t in_tag_len, const uint8_t *ad,
812                                         size_t ad_len, size_t tag_len) {
813  uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN];
814
815  if (nonce_len == 0) {
816    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
817    return 0;
818  }
819
820  if (in_tag_len != tag_len) {
821    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
822    return 0;
823  }
824
825  const GCM128_KEY *key = &gcm_ctx->key;
826  GCM128_CONTEXT gcm;
827  CRYPTO_gcm128_init_ctx(key, &gcm, nonce, nonce_len);
828
829  if (!CRYPTO_gcm128_aad(key, &gcm, ad, ad_len)) {
830    return 0;
831  }
832
833  if (!CRYPTO_gcm128_decrypt(key, &gcm, in, out, in_len)) {
834    return 0;
835  }
836
837  CRYPTO_gcm128_tag(key, &gcm, tag, tag_len);
838  if (CRYPTO_memcmp(tag, in_tag, tag_len) != 0) {
839    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
840    return 0;
841  }
842
843  return 1;
844}
845
846static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
847                                    const uint8_t *nonce, size_t nonce_len,
848                                    const uint8_t *in, size_t in_len,
849                                    const uint8_t *in_tag, size_t in_tag_len,
850                                    const uint8_t *ad, size_t ad_len) {
851  struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *)&ctx->state;
852  if (!aead_aes_gcm_open_gather_impl(gcm_ctx, out, nonce, nonce_len, in, in_len,
853                                     in_tag, in_tag_len, ad, ad_len,
854                                     ctx->tag_len)) {
855    return 0;
856  }
857
858  AEAD_GCM_verify_service_indicator(ctx);
859  return 1;
860}
861
862DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm) {
863  memset(out, 0, sizeof(EVP_AEAD));
864
865  out->key_len = 16;
866  out->nonce_len = AES_GCM_NONCE_LENGTH;
867  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
868  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
869  out->seal_scatter_supports_extra_in = 1;
870
871  out->init = aead_aes_gcm_init;
872  out->cleanup = aead_aes_gcm_cleanup;
873  out->seal_scatter = aead_aes_gcm_seal_scatter;
874  out->open_gather = aead_aes_gcm_open_gather;
875}
876
877DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_192_gcm) {
878  memset(out, 0, sizeof(EVP_AEAD));
879
880  out->key_len = 24;
881  out->nonce_len = AES_GCM_NONCE_LENGTH;
882  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
883  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
884  out->seal_scatter_supports_extra_in = 1;
885
886  out->init = aead_aes_gcm_init;
887  out->cleanup = aead_aes_gcm_cleanup;
888  out->seal_scatter = aead_aes_gcm_seal_scatter;
889  out->open_gather = aead_aes_gcm_open_gather;
890}
891
892DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) {
893  memset(out, 0, sizeof(EVP_AEAD));
894
895  out->key_len = 32;
896  out->nonce_len = AES_GCM_NONCE_LENGTH;
897  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
898  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
899  out->seal_scatter_supports_extra_in = 1;
900
901  out->init = aead_aes_gcm_init;
902  out->cleanup = aead_aes_gcm_cleanup;
903  out->seal_scatter = aead_aes_gcm_seal_scatter;
904  out->open_gather = aead_aes_gcm_open_gather;
905}
906
907static int aead_aes_gcm_init_randnonce(EVP_AEAD_CTX *ctx, const uint8_t *key,
908                                       size_t key_len,
909                                       size_t requested_tag_len) {
910  if (requested_tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH) {
911    if (requested_tag_len < AES_GCM_NONCE_LENGTH) {
912      OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
913      return 0;
914    }
915    requested_tag_len -= AES_GCM_NONCE_LENGTH;
916  }
917
918  if (!aead_aes_gcm_init(ctx, key, key_len, requested_tag_len)) {
919    return 0;
920  }
921
922  ctx->tag_len += AES_GCM_NONCE_LENGTH;
923  return 1;
924}
925
926static int aead_aes_gcm_seal_scatter_randnonce(
927    const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
928    size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *external_nonce,
929    size_t external_nonce_len, const uint8_t *in, size_t in_len,
930    const uint8_t *extra_in, size_t extra_in_len, const uint8_t *ad,
931    size_t ad_len) {
932  if (external_nonce_len != 0) {
933    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
934    return 0;
935  }
936
937  uint8_t nonce[AES_GCM_NONCE_LENGTH];
938  if (max_out_tag_len < sizeof(nonce)) {
939    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
940    return 0;
941  }
942
943  // |BCM_rand_bytes| calls within the fipsmodule should be wrapped with state
944  // lock functions to avoid updating the service indicator with the DRBG
945  // functions.
946  FIPS_service_indicator_lock_state();
947  BCM_rand_bytes(nonce, sizeof(nonce));
948  FIPS_service_indicator_unlock_state();
949
950  const struct aead_aes_gcm_ctx *gcm_ctx =
951      (const struct aead_aes_gcm_ctx *)&ctx->state;
952  if (!aead_aes_gcm_seal_scatter_impl(gcm_ctx, out, out_tag, out_tag_len,
953                                      max_out_tag_len - AES_GCM_NONCE_LENGTH,
954                                      nonce, sizeof(nonce), in, in_len,
955                                      extra_in, extra_in_len, ad, ad_len,
956                                      ctx->tag_len - AES_GCM_NONCE_LENGTH)) {
957    return 0;
958  }
959
960  assert(*out_tag_len + sizeof(nonce) <= max_out_tag_len);
961  memcpy(out_tag + *out_tag_len, nonce, sizeof(nonce));
962  *out_tag_len += sizeof(nonce);
963
964  AEAD_GCM_verify_service_indicator(ctx);
965  return 1;
966}
967
968static int aead_aes_gcm_open_gather_randnonce(
969    const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *external_nonce,
970    size_t external_nonce_len, const uint8_t *in, size_t in_len,
971    const uint8_t *in_tag, size_t in_tag_len, const uint8_t *ad,
972    size_t ad_len) {
973  if (external_nonce_len != 0) {
974    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
975    return 0;
976  }
977
978  if (in_tag_len < AES_GCM_NONCE_LENGTH) {
979    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
980    return 0;
981  }
982  const uint8_t *nonce = in_tag + in_tag_len - AES_GCM_NONCE_LENGTH;
983
984  const struct aead_aes_gcm_ctx *gcm_ctx =
985      (const struct aead_aes_gcm_ctx *)&ctx->state;
986  if (!aead_aes_gcm_open_gather_impl(
987          gcm_ctx, out, nonce, AES_GCM_NONCE_LENGTH, in, in_len, in_tag,
988          in_tag_len - AES_GCM_NONCE_LENGTH, ad, ad_len,
989          ctx->tag_len - AES_GCM_NONCE_LENGTH)) {
990    return 0;
991  }
992
993  AEAD_GCM_verify_service_indicator(ctx);
994  return 1;
995}
996
997DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_randnonce) {
998  memset(out, 0, sizeof(EVP_AEAD));
999
1000  out->key_len = 16;
1001  out->nonce_len = 0;
1002  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1003  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1004  out->seal_scatter_supports_extra_in = 1;
1005
1006  out->init = aead_aes_gcm_init_randnonce;
1007  out->cleanup = aead_aes_gcm_cleanup;
1008  out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
1009  out->open_gather = aead_aes_gcm_open_gather_randnonce;
1010}
1011
1012DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_randnonce) {
1013  memset(out, 0, sizeof(EVP_AEAD));
1014
1015  out->key_len = 32;
1016  out->nonce_len = 0;
1017  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1018  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
1019  out->seal_scatter_supports_extra_in = 1;
1020
1021  out->init = aead_aes_gcm_init_randnonce;
1022  out->cleanup = aead_aes_gcm_cleanup;
1023  out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
1024  out->open_gather = aead_aes_gcm_open_gather_randnonce;
1025}
1026
1027namespace {
1028struct aead_aes_gcm_tls12_ctx {
1029  struct aead_aes_gcm_ctx gcm_ctx;
1030  uint64_t min_next_nonce;
1031};
1032}  // namespace
1033
1034static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1035                  sizeof(struct aead_aes_gcm_tls12_ctx),
1036              "AEAD state is too small");
1037static_assert(alignof(union evp_aead_ctx_st_state) >=
1038                  alignof(struct aead_aes_gcm_tls12_ctx),
1039              "AEAD state has insufficient alignment");
1040
1041static int aead_aes_gcm_tls12_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1042                                   size_t key_len, size_t requested_tag_len) {
1043  struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1044      (struct aead_aes_gcm_tls12_ctx *)&ctx->state;
1045
1046  gcm_ctx->min_next_nonce = 0;
1047
1048  size_t actual_tag_len;
1049  if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1050                              requested_tag_len)) {
1051    return 0;
1052  }
1053
1054  ctx->tag_len = actual_tag_len;
1055  return 1;
1056}
1057
1058static int aead_aes_gcm_tls12_seal_scatter(
1059    const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1060    size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1061    size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1062    size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1063  struct aead_aes_gcm_tls12_ctx *gcm_ctx =
1064      (struct aead_aes_gcm_tls12_ctx *)&ctx->state;
1065
1066  if (nonce_len != AES_GCM_NONCE_LENGTH) {
1067    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1068    return 0;
1069  }
1070
1071  // The given nonces must be strictly monotonically increasing.
1072  uint64_t given_counter =
1073      CRYPTO_load_u64_be(nonce + nonce_len - sizeof(uint64_t));
1074  if (given_counter == UINT64_MAX || given_counter < gcm_ctx->min_next_nonce) {
1075    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
1076    return 0;
1077  }
1078
1079  gcm_ctx->min_next_nonce = given_counter + 1;
1080
1081  if (!aead_aes_gcm_seal_scatter(ctx, out, out_tag, out_tag_len,
1082                                 max_out_tag_len, nonce, nonce_len, in, in_len,
1083                                 extra_in, extra_in_len, ad, ad_len)) {
1084    return 0;
1085  }
1086
1087  AEAD_GCM_verify_service_indicator(ctx);
1088  return 1;
1089}
1090
1091DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls12) {
1092  memset(out, 0, sizeof(EVP_AEAD));
1093
1094  out->key_len = 16;
1095  out->nonce_len = AES_GCM_NONCE_LENGTH;
1096  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1097  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1098  out->seal_scatter_supports_extra_in = 1;
1099
1100  out->init = aead_aes_gcm_tls12_init;
1101  out->cleanup = aead_aes_gcm_cleanup;
1102  out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1103  out->open_gather = aead_aes_gcm_open_gather;
1104}
1105
1106DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls12) {
1107  memset(out, 0, sizeof(EVP_AEAD));
1108
1109  out->key_len = 32;
1110  out->nonce_len = AES_GCM_NONCE_LENGTH;
1111  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1112  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1113  out->seal_scatter_supports_extra_in = 1;
1114
1115  out->init = aead_aes_gcm_tls12_init;
1116  out->cleanup = aead_aes_gcm_cleanup;
1117  out->seal_scatter = aead_aes_gcm_tls12_seal_scatter;
1118  out->open_gather = aead_aes_gcm_open_gather;
1119}
1120
1121namespace {
1122struct aead_aes_gcm_tls13_ctx {
1123  struct aead_aes_gcm_ctx gcm_ctx;
1124  uint64_t min_next_nonce;
1125  uint64_t mask;
1126};
1127}  // namespace
1128
1129static_assert(sizeof(((EVP_AEAD_CTX *)NULL)->state) >=
1130                  sizeof(struct aead_aes_gcm_tls13_ctx),
1131              "AEAD state is too small");
1132static_assert(alignof(union evp_aead_ctx_st_state) >=
1133                  alignof(struct aead_aes_gcm_tls13_ctx),
1134              "AEAD state has insufficient alignment");
1135
1136static int aead_aes_gcm_tls13_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
1137                                   size_t key_len, size_t requested_tag_len) {
1138  struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1139      (struct aead_aes_gcm_tls13_ctx *)&ctx->state;
1140
1141  gcm_ctx->min_next_nonce = 0;
1142
1143  size_t actual_tag_len;
1144  if (!aead_aes_gcm_init_impl(&gcm_ctx->gcm_ctx, &actual_tag_len, key, key_len,
1145                              requested_tag_len)) {
1146    return 0;
1147  }
1148
1149  ctx->tag_len = actual_tag_len;
1150  return 1;
1151}
1152
1153static int aead_aes_gcm_tls13_seal_scatter(
1154    const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
1155    size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
1156    size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
1157    size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
1158  struct aead_aes_gcm_tls13_ctx *gcm_ctx =
1159      (struct aead_aes_gcm_tls13_ctx *)&ctx->state;
1160
1161  if (nonce_len != AES_GCM_NONCE_LENGTH) {
1162    OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
1163    return 0;
1164  }
1165
1166  // The given nonces must be strictly monotonically increasing. See
1167  // https://tools.ietf.org/html/rfc8446#section-5.3 for details of the TLS 1.3
1168  // nonce construction.
1169  uint64_t given_counter =
1170      CRYPTO_load_u64_be(nonce + nonce_len - sizeof(uint64_t));
1171
1172  if (gcm_ctx->min_next_nonce == 0) {
1173    // In the first call the sequence number will be zero and therefore the
1174    // given nonce will be 0 ^ mask = mask.
1175    gcm_ctx->mask = given_counter;
1176    gcm_ctx->min_next_nonce = 1;
1177  } else {
1178    given_counter ^= gcm_ctx->mask;
1179    if (given_counter == UINT64_MAX ||
1180        given_counter < gcm_ctx->min_next_nonce) {
1181      OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE);
1182      return 0;
1183    }
1184    gcm_ctx->min_next_nonce = given_counter + 1;
1185  }
1186
1187  if (!aead_aes_gcm_seal_scatter(ctx, out, out_tag, out_tag_len,
1188                                 max_out_tag_len, nonce, nonce_len, in, in_len,
1189                                 extra_in, extra_in_len, ad, ad_len)) {
1190    return 0;
1191  }
1192
1193  AEAD_GCM_verify_service_indicator(ctx);
1194  return 1;
1195}
1196
1197DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls13) {
1198  memset(out, 0, sizeof(EVP_AEAD));
1199
1200  out->key_len = 16;
1201  out->nonce_len = AES_GCM_NONCE_LENGTH;
1202  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1203  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1204  out->seal_scatter_supports_extra_in = 1;
1205
1206  out->init = aead_aes_gcm_tls13_init;
1207  out->cleanup = aead_aes_gcm_cleanup;
1208  out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1209  out->open_gather = aead_aes_gcm_open_gather;
1210}
1211
1212DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls13) {
1213  memset(out, 0, sizeof(EVP_AEAD));
1214
1215  out->key_len = 32;
1216  out->nonce_len = AES_GCM_NONCE_LENGTH;
1217  out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
1218  out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
1219  out->seal_scatter_supports_extra_in = 1;
1220
1221  out->init = aead_aes_gcm_tls13_init;
1222  out->cleanup = aead_aes_gcm_cleanup;
1223  out->seal_scatter = aead_aes_gcm_tls13_seal_scatter;
1224  out->open_gather = aead_aes_gcm_open_gather;
1225}
1226
1227int EVP_has_aes_hardware(void) {
1228#if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
1229  return hwaes_capable() && crypto_gcm_clmul_enabled();
1230#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
1231  return hwaes_capable() && CRYPTO_is_ARMv8_PMULL_capable();
1232#else
1233  return 0;
1234#endif
1235}
1236