2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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)
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;
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,
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,
43 static void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
44 const PROV_GCM_HW *hw, size_t ivlen_min)
47 ctx->mode = EVP_CIPH_GCM_MODE;
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;
54 ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
57 static void gcm_deinitctx(PROV_GCM_CTX *ctx)
59 OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
62 static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
63 const unsigned char *iv, size_t ivlen, int enc)
65 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
70 if (ivlen < ctx->ivlen_min || ivlen > sizeof(ctx->iv)) {
71 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
75 memcpy(ctx->iv, iv, ctx->ivlen);
76 ctx->iv_state = IV_STATE_BUFFERED;
80 if (keylen != ctx->keylen) {
81 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
84 return ctx->hw->setkey(ctx, key, ctx->keylen);
89 static int gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
90 const unsigned char *iv, size_t ivlen)
92 return gcm_init(vctx, key, keylen, iv, ivlen, 1);
95 static int gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
96 const unsigned char *iv, size_t ivlen)
98 return gcm_init(vctx, key, keylen, iv, ivlen, 0);
101 static int gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
103 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
107 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
109 if (!OSSL_PARAM_set_int(p, ctx->ivlen))
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);
118 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
120 if (ctx->iv_gen != 1 && ctx->iv_gen_rand != 1)
122 if (ctx->ivlen != (int)p->data_size) {
123 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
126 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)) {
127 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
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);
137 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
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);
144 if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
145 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
152 static int gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
154 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
159 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
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);
166 if (sz == 0 || ctx->enc) {
167 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
173 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
175 if (!OSSL_PARAM_get_size_t(p, &sz)) {
176 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
179 if (sz == 0 || sz > sizeof(ctx->iv)) {
180 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
186 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
188 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
189 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
192 sz = gcm_tls_init(ctx, p->data, p->data_size);
194 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
197 ctx->tls_aad_pad_sz = sz;
200 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
202 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
203 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
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);
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.
218 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
222 if (!OSSL_PARAM_get_int(p, &keylen)) {
223 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
226 /* The key length can not be modified for gcm mode */
227 if (keylen != (int)ctx->keylen)
234 static int gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
235 size_t outsize, const unsigned char *in,
238 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
241 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
245 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
246 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
252 static int gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
255 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
258 i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
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)
270 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
273 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
277 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
285 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
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).
292 static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
294 int sz = ctx->ivlen - offset;
296 /* Must be at least 96 bits */
297 if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
300 /* Use DRBG to generate random iv */
301 if (rand_bytes_ex(ctx->libctx, ctx->iv + offset, sz) <= 0)
303 ctx->iv_state = IV_STATE_BUFFERED;
304 ctx->iv_gen_rand = 1;
308 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
309 size_t *padlen, const unsigned char *in,
314 const PROV_GCM_HW *hw = ctx->hw;
316 if (ctx->tls_aad_len >= 0)
317 return gcm_tls_cipher(ctx, out, padlen, in, len);
319 if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
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.
328 if (ctx->iv_state == IV_STATE_UNINITIALISED) {
329 if (!ctx->enc || !gcm_iv_generate(ctx, 0))
333 if (ctx->iv_state == IV_STATE_BUFFERED) {
334 if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
336 ctx->iv_state = IV_STATE_COPIED;
340 /* The input is AAD if out is NULL */
342 if (!hw->aadupdate(ctx, in, len))
345 /* The input is ciphertext OR plaintext */
346 if (!hw->cipherupdate(ctx, in, len, out))
350 /* Finished when in == NULL */
351 if (!hw->cipherfinal(ctx, ctx->buf))
353 ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
364 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
369 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
372 /* Save the aad for later use. */
374 memcpy(buf, aad, aad_len);
375 dat->tls_aad_len = aad_len;
376 dat->tls_enc_records = 0;
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)
382 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
384 /* If decrypting correct for tag too. */
386 if (len < EVP_GCM_TLS_TAG_LEN)
388 len -= EVP_GCM_TLS_TAG_LEN;
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;
396 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
399 /* Special case: -1 length restores whole IV */
400 if (len == (size_t)-1) {
401 memcpy(ctx->iv, iv, ctx->ivlen);
403 ctx->iv_state = IV_STATE_BUFFERED;
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)
411 memcpy(ctx->iv, iv, len);
413 && rand_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len) <= 0)
416 ctx->iv_state = IV_STATE_BUFFERED;
420 /* increment counter (64-bit int) by 1 */
421 static void ctr64_inc(unsigned char *counter)
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
442 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
443 const unsigned char *in, size_t len)
445 int rv = 0, arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
447 unsigned char *tag = NULL;
452 /* Encrypt/decrypt must be performed in place */
453 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
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
462 if (ctx->enc && ++ctx->tls_enc_records == 0) {
463 ERR_raise(ERR_LIB_PROV, EVP_R_TOO_MANY_RECORDS);
467 if (ctx->iv_gen == 0)
470 * Set IV from start of buffer or generate IV and write to start of
474 if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
476 if (arg > ctx->ivlen)
478 memcpy(out, ctx->iv + ctx->ivlen - arg, arg);
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.
483 ctr64_inc(ctx->iv + ctx->ivlen - 8);
485 memcpy(ctx->iv + ctx->ivlen - arg, out, arg);
486 if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
489 ctx->iv_state = IV_STATE_COPIED;
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;
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)) {
500 OPENSSL_cleanse(out, len);
504 plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
510 ctx->iv_state = IV_STATE_FINISHED;
511 ctx->tls_aad_len = -1;
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[]) \
520 return cipher_default_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
521 flags, kbits, blkbits, ivbits); \
523 static OSSL_OP_cipher_newctx_fn alg##kbits##gcm_newctx; \
524 static void *alg##kbits##gcm_newctx(void *provctx) \
526 return alg##_gcm_newctx(provctx, kbits); \
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 }, \
551 static void *aes_gcm_newctx(void *provctx, size_t keybits)
553 PROV_AES_GCM_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
556 gcm_initctx(provctx, (PROV_GCM_CTX *)ctx, keybits,
557 PROV_AES_HW_gcm(keybits), 8);
561 static OSSL_OP_cipher_freectx_fn aes_gcm_freectx;
562 static void aes_gcm_freectx(void *vctx)
564 PROV_AES_GCM_CTX *ctx = (PROV_AES_GCM_CTX *)vctx;
566 gcm_deinitctx((PROV_GCM_CTX *)ctx);
567 OPENSSL_clear_free(ctx, sizeof(*ctx));
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);
577 #if !defined(OPENSSL_NO_ARIA) && !defined(FIPS_MODE)
579 static void *aria_gcm_newctx(void *provctx, size_t keybits)
581 PROV_ARIA_GCM_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
584 gcm_initctx(provctx, (PROV_GCM_CTX *)ctx, keybits,
585 PROV_ARIA_HW_gcm(keybits), 4);
589 static OSSL_OP_cipher_freectx_fn aria_gcm_freectx;
590 static void aria_gcm_freectx(void *vctx)
592 PROV_ARIA_GCM_CTX *ctx = (PROV_ARIA_GCM_CTX *)vctx;
594 gcm_deinitctx((PROV_GCM_CTX *)ctx);
595 OPENSSL_clear_free(ctx, sizeof(*ctx));
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);
605 #endif /* !defined(OPENSSL_NO_ARIA) && !defined(FIPS_MODE) */