7644f81f21feaa52caec3ad9fef1b41d7c6872fb
[openssl.git] / providers / common / ciphers / gcm.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/evp.h>
11 #include <openssl/params.h>
12 #include <openssl/core_numbers.h>
13 #include <openssl/core_names.h>
14 #include "internal/rand_int.h"
15 #include "internal/provider_algs.h"
16 #include "internal/provider_ctx.h"
17 #include "internal/providercommonerr.h"
18 #include "ciphers_locl.h"
19
20 /* TODO(3.0) Figure out what flags are really needed */
21 #define AEAD_GCM_FLAGS (EVP_CIPH_FLAG_AEAD_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1 \
22                        | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER      \
23                        | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT        \
24                        | EVP_CIPH_CUSTOM_COPY)
25
26 static OSSL_OP_cipher_encrypt_init_fn gcm_einit;
27 static OSSL_OP_cipher_decrypt_init_fn gcm_dinit;
28 static OSSL_OP_cipher_get_ctx_params_fn gcm_get_ctx_params;
29 static OSSL_OP_cipher_set_ctx_params_fn gcm_set_ctx_params;
30 static OSSL_OP_cipher_cipher_fn gcm_cipher;
31 static OSSL_OP_cipher_update_fn gcm_stream_update;
32 static OSSL_OP_cipher_final_fn gcm_stream_final;
33
34 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len);
35 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
36                                 size_t len);
37 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
38                           const unsigned char *in, size_t len);
39 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
40                                size_t *padlen, const unsigned char *in,
41                                size_t len);
42
43 static void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
44                         const PROV_GCM_HW *hw, size_t ivlen_min)
45 {
46     ctx->pad = 1;
47     ctx->mode = EVP_CIPH_GCM_MODE;
48     ctx->taglen = -1;
49     ctx->tls_aad_len = -1;
50     ctx->ivlen_min = ivlen_min;
51     ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
52     ctx->keylen = keybits / 8;
53     ctx->hw = hw;
54     ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
55 }
56
57 static void gcm_deinitctx(PROV_GCM_CTX *ctx)
58 {
59     OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
60 }
61
62 static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
63                     const unsigned char *iv, size_t ivlen, int enc)
64 {
65     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
66
67     ctx->enc = enc;
68
69     if (iv != NULL) {
70         if (ivlen < ctx->ivlen_min || ivlen > sizeof(ctx->iv)) {
71             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
72             return 0;
73         }
74         ctx->ivlen = ivlen;
75         memcpy(ctx->iv, iv, ctx->ivlen);
76         ctx->iv_state = IV_STATE_BUFFERED;
77     }
78
79     if (key != NULL) {
80         if (keylen != ctx->keylen) {
81             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
82             return 0;
83         }
84         return ctx->hw->setkey(ctx, key, ctx->keylen);
85     }
86     return 1;
87 }
88
89 static int gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
90                      const unsigned char *iv, size_t ivlen)
91 {
92     return gcm_init(vctx, key, keylen, iv, ivlen, 1);
93 }
94
95 static int gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
96                      const unsigned char *iv, size_t ivlen)
97 {
98     return gcm_init(vctx, key, keylen, iv, ivlen, 0);
99 }
100
101 static int gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
102 {
103     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
104     OSSL_PARAM *p;
105     size_t sz;
106
107     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
108     if (p != NULL) {
109         if (!OSSL_PARAM_set_int(p, ctx->ivlen))
110             return 0;
111     }
112     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
113     if (p != NULL && !OSSL_PARAM_set_int(p, ctx->keylen)) {
114         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
115         return 0;
116     }
117
118     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
119     if (p != NULL) {
120         if (ctx->iv_gen != 1 && ctx->iv_gen_rand != 1)
121             return 0;
122         if (ctx->ivlen != (int)p->data_size) {
123             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
124             return 0;
125         }
126         if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)) {
127             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
128             return 0;
129         }
130     }
131
132     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
133     if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
134         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
135         return 0;
136     }
137     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
138     if (p != NULL) {
139         sz = p->data_size;
140         if (sz == 0 || sz > EVP_GCM_TLS_TAG_LEN || !ctx->enc || ctx->taglen < 0) {
141             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
142             return 0;
143         }
144         if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
145             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
146             return 0;
147         }
148     }
149     return 1;
150 }
151
152 static int gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
153 {
154     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
155     const OSSL_PARAM *p;
156     size_t sz;
157     void *vp;
158
159     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
160     if (p != NULL) {
161         vp = ctx->buf;
162         if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
163             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
164             return 0;
165         }
166         if (sz == 0 || ctx->enc) {
167             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
168             return 0;
169         }
170         ctx->taglen = sz;
171     }
172
173     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
174     if (p != NULL) {
175         if (!OSSL_PARAM_get_size_t(p, &sz)) {
176             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
177             return 0;
178         }
179         if (sz == 0 || sz > sizeof(ctx->iv)) {
180             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
181             return 0;
182         }
183         ctx->ivlen = sz;
184     }
185
186     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
187     if (p != NULL) {
188         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
189             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
190             return 0;
191         }
192         sz = gcm_tls_init(ctx, p->data, p->data_size);
193         if (sz == 0) {
194             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
195             return 0;
196         }
197         ctx->tls_aad_pad_sz = sz;
198     }
199
200     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
201     if (p != NULL) {
202         if (p->data_type != OSSL_PARAM_OCTET_STRING) {
203             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
204             return 0;
205         }
206         if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
207             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
208             return 0;
209         }
210     }
211
212     /*
213      * TODO(3.0) Temporary solution to address fuzz test crash, which will be
214      * reworked once the discussion in PR #9510 is resolved. i.e- We need a
215      * general solution for handling missing parameters inside set_params and
216      * get_params methods.
217      */
218     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
219     if (p != NULL) {
220         int keylen;
221
222         if (!OSSL_PARAM_get_int(p, &keylen)) {
223             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
224             return 0;
225         }
226         /* The key length can not be modified for gcm mode */
227         if (keylen != (int)ctx->keylen)
228             return 0;
229     }
230
231     return 1;
232 }
233
234 static int gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
235                              size_t outsize, const unsigned char *in,
236                              size_t inl)
237 {
238     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
239
240     if (outsize < inl) {
241         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
242         return -1;
243     }
244
245     if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
246         ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
247         return -1;
248     }
249     return 1;
250 }
251
252 static int gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
253                             size_t outsize)
254 {
255     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
256     int i;
257
258     i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
259     if (i <= 0)
260         return 0;
261
262     *outl = 0;
263     return 1;
264 }
265
266 static int gcm_cipher(void *vctx,
267                       unsigned char *out, size_t *outl, size_t outsize,
268                       const unsigned char *in, size_t inl)
269 {
270     PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
271
272     if (outsize < inl) {
273         ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
274         return -1;
275     }
276
277     if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
278         return -1;
279
280     *outl = inl;
281     return 1;
282 }
283
284 /*
285  * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
286  *
287  * See also 8.2.2 RBG-based construction.
288  * Random construction consists of a free field (which can be NULL) and a
289  * random field which will use a DRBG that can return at least 96 bits of
290  * entropy strength. (The DRBG must be seeded by the FIPS module).
291  */
292 static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
293 {
294     int sz = ctx->ivlen - offset;
295
296     /* Must be at least 96 bits */
297     if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
298         return 0;
299
300     /* Use DRBG to generate random iv */
301     if (rand_bytes_ex(ctx->libctx, ctx->iv + offset, sz) <= 0)
302         return 0;
303     ctx->iv_state = IV_STATE_BUFFERED;
304     ctx->iv_gen_rand = 1;
305     return 1;
306 }
307
308 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
309                                size_t *padlen, const unsigned char *in,
310                                size_t len)
311 {
312     size_t olen = 0;
313     int rv = 0;
314     const PROV_GCM_HW *hw = ctx->hw;
315
316     if (ctx->tls_aad_len >= 0)
317         return gcm_tls_cipher(ctx, out, padlen, in, len);
318
319     if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
320         goto err;
321
322     /*
323      * FIPS requires generation of AES-GCM IV's inside the FIPS module.
324      * The IV can still be set externally (the security policy will state that
325      * this is not FIPS compliant). There are some applications
326      * where setting the IV externally is the only option available.
327      */
328     if (ctx->iv_state == IV_STATE_UNINITIALISED) {
329         if (!ctx->enc || !gcm_iv_generate(ctx, 0))
330             goto err;
331     }
332
333     if (ctx->iv_state == IV_STATE_BUFFERED) {
334         if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
335             goto err;
336         ctx->iv_state = IV_STATE_COPIED;
337     }
338
339     if (in != NULL) {
340         /*  The input is AAD if out is NULL */
341         if (out == NULL) {
342             if (!hw->aadupdate(ctx, in, len))
343                 goto err;
344         } else {
345             /* The input is ciphertext OR plaintext */
346             if (!hw->cipherupdate(ctx, in, len, out))
347                 goto err;
348         }
349     } else {
350         /* Finished when in == NULL */
351         if (!hw->cipherfinal(ctx, ctx->buf))
352             goto err;
353         ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
354         goto finish;
355     }
356     olen = len;
357 finish:
358     rv = 1;
359 err:
360     *padlen = olen;
361     return rv;
362 }
363
364 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
365 {
366     unsigned char *buf;
367     size_t len;
368
369     if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
370        return 0;
371
372     /* Save the aad for later use. */
373     buf = dat->buf;
374     memcpy(buf, aad, aad_len);
375     dat->tls_aad_len = aad_len;
376     dat->tls_enc_records = 0;
377
378     len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
379     /* Correct length for explicit iv. */
380     if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
381         return 0;
382     len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
383
384     /* If decrypting correct for tag too. */
385     if (!dat->enc) {
386         if (len < EVP_GCM_TLS_TAG_LEN)
387             return 0;
388         len -= EVP_GCM_TLS_TAG_LEN;
389     }
390     buf[aad_len - 2] = (unsigned char)(len >> 8);
391     buf[aad_len - 1] = (unsigned char)(len & 0xff);
392     /* Extra padding: tag appended to record. */
393     return EVP_GCM_TLS_TAG_LEN;
394 }
395
396 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
397                                 size_t len)
398 {
399     /* Special case: -1 length restores whole IV */
400     if (len == (size_t)-1) {
401         memcpy(ctx->iv, iv, ctx->ivlen);
402         ctx->iv_gen = 1;
403         ctx->iv_state = IV_STATE_BUFFERED;
404         return 1;
405     }
406     /* Fixed field must be at least 4 bytes and invocation field at least 8 */
407     if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
408         || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
409             return 0;
410     if (len > 0)
411         memcpy(ctx->iv, iv, len);
412     if (ctx->enc
413         && rand_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len) <= 0)
414             return 0;
415     ctx->iv_gen = 1;
416     ctx->iv_state = IV_STATE_BUFFERED;
417     return 1;
418 }
419
420 /* increment counter (64-bit int) by 1 */
421 static void ctr64_inc(unsigned char *counter)
422 {
423     int n = 8;
424     unsigned char c;
425
426     do {
427         --n;
428         c = counter[n];
429         ++c;
430         counter[n] = c;
431         if (c > 0)
432             return;
433     } while (n > 0);
434 }
435
436 /*
437  * Handle TLS GCM packet format. This consists of the last portion of the IV
438  * followed by the payload and finally the tag. On encrypt generate IV,
439  * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
440  * and verify tag.
441  */
442 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
443                           const unsigned char *in, size_t len)
444 {
445     int rv = 0, arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
446     size_t plen = 0;
447     unsigned char *tag = NULL;
448
449     if (!ctx->key_set)
450         goto err;
451
452     /* Encrypt/decrypt must be performed in place */
453     if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
454         goto err;
455
456     /*
457      * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
458      * Requirements from SP 800-38D".  The requirements is for one party to the
459      * communication to fail after 2^64 - 1 keys.  We do this on the encrypting
460      * side only.
461      */
462     if (ctx->enc && ++ctx->tls_enc_records == 0) {
463         ERR_raise(ERR_LIB_PROV, EVP_R_TOO_MANY_RECORDS);
464         goto err;
465     }
466
467     if (ctx->iv_gen == 0)
468         goto err;
469     /*
470      * Set IV from start of buffer or generate IV and write to start of
471      * buffer.
472      */
473     if (ctx->enc) {
474         if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
475             goto err;
476         if (arg > ctx->ivlen)
477             arg = ctx->ivlen;
478         memcpy(out, ctx->iv + ctx->ivlen - arg, arg);
479         /*
480          * Invocation field will be at least 8 bytes in size and so no need
481          * to check wrap around or increment more than last 8 bytes.
482          */
483         ctr64_inc(ctx->iv + ctx->ivlen - 8);
484     } else {
485         memcpy(ctx->iv + ctx->ivlen - arg, out, arg);
486         if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
487             goto err;
488     }
489     ctx->iv_state = IV_STATE_COPIED;
490
491     /* Fix buffer and length to point to payload */
492     in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
493     out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
494     len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
495
496     tag = ctx->enc ? out + len : (unsigned char *)in + len;
497     if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
498                           EVP_GCM_TLS_TAG_LEN)) {
499         if (!ctx->enc)
500             OPENSSL_cleanse(out, len);
501         goto err;
502     }
503     if (ctx->enc)
504         plen =  len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
505     else
506         plen = len;
507
508     rv = 1;
509 err:
510     ctx->iv_state = IV_STATE_FINISHED;
511     ctx->tls_aad_len = -1;
512     *padlen = plen;
513     return rv;
514 }
515
516 #define IMPLEMENT_cipher(alg, lcmode, UCMODE, flags, kbits, blkbits, ivbits)   \
517     static OSSL_OP_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params; \
518     static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])      \
519     {                                                                          \
520         return cipher_default_get_params(params, EVP_CIPH_##UCMODE##_MODE,     \
521                                          flags, kbits, blkbits, ivbits);       \
522     }                                                                          \
523     static OSSL_OP_cipher_newctx_fn alg##kbits##gcm_newctx;                    \
524     static void *alg##kbits##gcm_newctx(void *provctx)                         \
525     {                                                                          \
526         return alg##_gcm_newctx(provctx, kbits);                               \
527     }                                                                          \
528     const OSSL_DISPATCH alg##kbits##gcm_functions[] = {                        \
529         { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))gcm_einit },          \
530         { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))gcm_dinit },          \
531         { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))gcm_stream_update },        \
532         { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))gcm_stream_final },          \
533         { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))gcm_cipher },               \
534         { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void)) alg##kbits##gcm_newctx },  \
535         { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_gcm_freectx },      \
536         { OSSL_FUNC_CIPHER_GET_PARAMS,                                         \
537             (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },          \
538         { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                     \
539             (void (*)(void))gcm_get_ctx_params },                              \
540         { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                     \
541             (void (*)(void))gcm_set_ctx_params },                              \
542         { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                    \
543                 (void (*)(void))cipher_default_gettable_params },              \
544         { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                \
545                 (void (*)(void))cipher_aead_gettable_ctx_params },             \
546         { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                \
547                 (void (*)(void))cipher_aead_settable_ctx_params },             \
548         { 0, NULL }                                                            \
549     }
550
551 static void *aes_gcm_newctx(void *provctx, size_t keybits)
552 {
553     PROV_AES_GCM_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
554
555     if (ctx != NULL)
556         gcm_initctx(provctx, (PROV_GCM_CTX *)ctx, keybits,
557                     PROV_AES_HW_gcm(keybits), 8);
558     return ctx;
559 }
560
561 static OSSL_OP_cipher_freectx_fn aes_gcm_freectx;
562 static void aes_gcm_freectx(void *vctx)
563 {
564     PROV_AES_GCM_CTX *ctx = (PROV_AES_GCM_CTX *)vctx;
565
566     gcm_deinitctx((PROV_GCM_CTX *)ctx);
567     OPENSSL_clear_free(ctx,  sizeof(*ctx));
568 }
569
570 /* aes128gcm_functions */
571 IMPLEMENT_cipher(aes, gcm, GCM, AEAD_GCM_FLAGS, 128, 8, 96);
572 /* aes192gcm_functions */
573 IMPLEMENT_cipher(aes, gcm, GCM, AEAD_GCM_FLAGS, 192, 8, 96);
574 /* aes256gcm_functions */
575 IMPLEMENT_cipher(aes, gcm, GCM, AEAD_GCM_FLAGS, 256, 8, 96);
576
577 #if !defined(OPENSSL_NO_ARIA) && !defined(FIPS_MODE)
578
579 static void *aria_gcm_newctx(void *provctx, size_t keybits)
580 {
581     PROV_ARIA_GCM_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
582
583     if (ctx != NULL)
584         gcm_initctx(provctx, (PROV_GCM_CTX *)ctx, keybits,
585                     PROV_ARIA_HW_gcm(keybits), 4);
586     return ctx;
587 }
588
589 static OSSL_OP_cipher_freectx_fn aria_gcm_freectx;
590 static void aria_gcm_freectx(void *vctx)
591 {
592     PROV_ARIA_GCM_CTX *ctx = (PROV_ARIA_GCM_CTX *)vctx;
593
594     gcm_deinitctx((PROV_GCM_CTX *)ctx);
595     OPENSSL_clear_free(ctx,  sizeof(*ctx));
596 }
597
598 /* aria128gcm_functions */
599 IMPLEMENT_cipher(aria, gcm, GCM, AEAD_GCM_FLAGS, 128, 8, 96);
600 /* aria192gcm_functions */
601 IMPLEMENT_cipher(aria, gcm, GCM, AEAD_GCM_FLAGS, 192, 8, 96);
602 /* aria256gcm_functions */
603 IMPLEMENT_cipher(aria, gcm, GCM, AEAD_GCM_FLAGS, 256, 8, 96);
604
605 #endif /* !defined(OPENSSL_NO_ARIA) && !defined(FIPS_MODE) */