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 /* Dispatch functions for gcm mode */
12 #include "cipher_local.h"
13 #include "internal/ciphers/cipher_gcm.h"
14 #include "internal/providercommonerr.h"
15 #include "crypto/rand.h"
16 #include "internal/provider_ctx.h"
18 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len);
19 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
21 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
22 const unsigned char *in, size_t len);
23 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
24 size_t *padlen, const unsigned char *in,
27 void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
28 const PROV_GCM_HW *hw, size_t ivlen_min)
31 ctx->mode = EVP_CIPH_GCM_MODE;
32 ctx->taglen = UNINITIALISED_SIZET;
33 ctx->tls_aad_len = UNINITIALISED_SIZET;
34 ctx->ivlen_min = ivlen_min;
35 ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
36 ctx->keylen = keybits / 8;
38 ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
41 void gcm_deinitctx(PROV_GCM_CTX *ctx)
43 OPENSSL_cleanse(ctx->iv, sizeof(ctx->iv));
46 static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
47 const unsigned char *iv, size_t ivlen, int enc)
49 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
54 if (ivlen < ctx->ivlen_min || ivlen > sizeof(ctx->iv)) {
55 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
59 memcpy(ctx->iv, iv, ctx->ivlen);
60 ctx->iv_state = IV_STATE_BUFFERED;
64 if (keylen != ctx->keylen) {
65 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
68 return ctx->hw->setkey(ctx, key, ctx->keylen);
73 int gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
74 const unsigned char *iv, size_t ivlen)
76 return gcm_init(vctx, key, keylen, iv, ivlen, 1);
79 int gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
80 const unsigned char *iv, size_t ivlen)
82 return gcm_init(vctx, key, keylen, iv, ivlen, 0);
85 int gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
87 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
91 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
92 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
93 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
96 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
97 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
98 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
101 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
103 size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen :
106 if (!OSSL_PARAM_set_size_t(p, taglen)) {
107 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
112 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
114 if (ctx->iv_gen != 1 && ctx->iv_gen_rand != 1)
116 if (ctx->ivlen != p->data_size) {
117 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
120 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)) {
121 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
126 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
127 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
128 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
131 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
135 || sz > EVP_GCM_TLS_TAG_LEN
137 || ctx->taglen == UNINITIALISED_SIZET) {
138 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
141 if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
142 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
150 int gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
152 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
157 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
160 if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
161 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
164 if (sz == 0 || ctx->enc) {
165 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
171 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
173 if (!OSSL_PARAM_get_size_t(p, &sz)) {
174 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
177 if (sz == 0 || sz > sizeof(ctx->iv)) {
178 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
184 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
186 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
187 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
190 sz = gcm_tls_init(ctx, p->data, p->data_size);
192 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
195 ctx->tls_aad_pad_sz = sz;
198 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
200 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
201 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
204 if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
205 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
211 * TODO(3.0) Temporary solution to address fuzz test crash, which will be
212 * reworked once the discussion in PR #9510 is resolved. i.e- We need a
213 * general solution for handling missing parameters inside set_params and
214 * get_params methods.
216 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
220 if (!OSSL_PARAM_get_size_t(p, &keylen)) {
221 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
224 /* The key length can not be modified for gcm mode */
225 if (keylen != ctx->keylen)
232 int gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
233 size_t outsize, const unsigned char *in, size_t inl)
235 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
238 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
242 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
243 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
249 int gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
252 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
255 i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
263 int gcm_cipher(void *vctx,
264 unsigned char *out, size_t *outl, size_t outsize,
265 const unsigned char *in, size_t inl)
267 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
270 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
274 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
282 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
284 * See also 8.2.2 RBG-based construction.
285 * Random construction consists of a free field (which can be NULL) and a
286 * random field which will use a DRBG that can return at least 96 bits of
287 * entropy strength. (The DRBG must be seeded by the FIPS module).
289 static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
291 int sz = ctx->ivlen - offset;
293 /* Must be at least 96 bits */
294 if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
297 /* Use DRBG to generate random iv */
298 if (rand_bytes_ex(ctx->libctx, ctx->iv + offset, sz) <= 0)
300 ctx->iv_state = IV_STATE_BUFFERED;
301 ctx->iv_gen_rand = 1;
305 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
306 size_t *padlen, const unsigned char *in,
311 const PROV_GCM_HW *hw = ctx->hw;
313 if (ctx->tls_aad_len != UNINITIALISED_SIZET)
314 return gcm_tls_cipher(ctx, out, padlen, in, len);
316 if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
320 * FIPS requires generation of AES-GCM IV's inside the FIPS module.
321 * The IV can still be set externally (the security policy will state that
322 * this is not FIPS compliant). There are some applications
323 * where setting the IV externally is the only option available.
325 if (ctx->iv_state == IV_STATE_UNINITIALISED) {
326 if (!ctx->enc || !gcm_iv_generate(ctx, 0))
330 if (ctx->iv_state == IV_STATE_BUFFERED) {
331 if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
333 ctx->iv_state = IV_STATE_COPIED;
337 /* The input is AAD if out is NULL */
339 if (!hw->aadupdate(ctx, in, len))
342 /* The input is ciphertext OR plaintext */
343 if (!hw->cipherupdate(ctx, in, len, out))
347 /* The tag must be set before actually decrypting data */
348 if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET)
350 if (!hw->cipherfinal(ctx, ctx->buf))
352 ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
363 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
368 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
371 /* Save the aad for later use. */
373 memcpy(buf, aad, aad_len);
374 dat->tls_aad_len = aad_len;
375 dat->tls_enc_records = 0;
377 len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
378 /* Correct length for explicit iv. */
379 if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
381 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
383 /* If decrypting correct for tag too. */
385 if (len < EVP_GCM_TLS_TAG_LEN)
387 len -= EVP_GCM_TLS_TAG_LEN;
389 buf[aad_len - 2] = (unsigned char)(len >> 8);
390 buf[aad_len - 1] = (unsigned char)(len & 0xff);
391 /* Extra padding: tag appended to record. */
392 return EVP_GCM_TLS_TAG_LEN;
395 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
398 /* Special case: -1 length restores whole IV */
399 if (len == (size_t)-1) {
400 memcpy(ctx->iv, iv, ctx->ivlen);
402 ctx->iv_state = IV_STATE_BUFFERED;
405 /* Fixed field must be at least 4 bytes and invocation field at least 8 */
406 if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
407 || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
410 memcpy(ctx->iv, iv, len);
412 && rand_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len) <= 0)
415 ctx->iv_state = IV_STATE_BUFFERED;
419 /* increment counter (64-bit int) by 1 */
420 static void ctr64_inc(unsigned char *counter)
436 * Handle TLS GCM packet format. This consists of the last portion of the IV
437 * followed by the payload and finally the tag. On encrypt generate IV,
438 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
441 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
442 const unsigned char *in, size_t len)
445 size_t 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 = UNINITIALISED_SIZET;