2 * Copyright 2019-2021 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 <openssl/rand.h>
13 #include <openssl/proverr.h>
14 #include "prov/ciphercommon.h"
15 #include "prov/ciphercommon_gcm.h"
16 #include "prov/providercommon.h"
17 #include "prov/provider_ctx.h"
19 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len);
20 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
22 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
23 const unsigned char *in, size_t len);
24 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
25 size_t *padlen, const unsigned char *in,
28 void ossl_gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
29 const PROV_GCM_HW *hw)
32 ctx->mode = EVP_CIPH_GCM_MODE;
33 ctx->taglen = UNINITIALISED_SIZET;
34 ctx->tls_aad_len = UNINITIALISED_SIZET;
35 ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
36 ctx->keylen = keybits / 8;
38 ctx->libctx = PROV_LIBCTX_OF(provctx);
41 static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
42 const unsigned char *iv, size_t ivlen,
43 const OSSL_PARAM params[], int enc)
45 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
47 if (!ossl_prov_is_running())
53 if (ivlen == 0 || ivlen > sizeof(ctx->iv)) {
54 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
58 memcpy(ctx->iv, iv, ivlen);
59 ctx->iv_state = IV_STATE_BUFFERED;
63 if (keylen != ctx->keylen) {
64 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
67 if (!ctx->hw->setkey(ctx, key, ctx->keylen))
70 return ossl_gcm_set_ctx_params(ctx, params);
73 int ossl_gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
74 const unsigned char *iv, size_t ivlen,
75 const OSSL_PARAM params[])
77 return gcm_init(vctx, key, keylen, iv, ivlen, params, 1);
80 int ossl_gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
81 const unsigned char *iv, size_t ivlen,
82 const OSSL_PARAM params[])
84 return gcm_init(vctx, key, keylen, iv, ivlen, params, 0);
87 /* increment counter (64-bit int) by 1 */
88 static void ctr64_inc(unsigned char *counter)
103 static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen)
107 || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
109 if (olen == 0 || olen > ctx->ivlen)
111 memcpy(out, ctx->iv + ctx->ivlen - olen, olen);
113 * Invocation field will be at least 8 bytes in size and so no need
114 * to check wrap around or increment more than last 8 bytes.
116 ctr64_inc(ctx->iv + ctx->ivlen - 8);
117 ctx->iv_state = IV_STATE_COPIED;
121 static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl)
128 memcpy(ctx->iv + ctx->ivlen - inl, in, inl);
129 if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
131 ctx->iv_state = IV_STATE_COPIED;
135 int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
137 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
141 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
142 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
143 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
146 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
147 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
148 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
151 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
153 size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen :
156 if (!OSSL_PARAM_set_size_t(p, taglen)) {
157 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
162 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
164 if (ctx->iv_state == IV_STATE_UNINITIALISED)
166 if (ctx->ivlen > p->data_size) {
167 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
170 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)
171 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) {
172 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
177 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
179 if (ctx->iv_state == IV_STATE_UNINITIALISED)
181 if (ctx->ivlen > p->data_size) {
182 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
185 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)
186 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) {
187 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
192 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
193 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
194 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
197 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
201 || sz > EVP_GCM_TLS_TAG_LEN
203 || ctx->taglen == UNINITIALISED_SIZET) {
204 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
207 if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
208 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
212 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN);
215 || p->data_type != OSSL_PARAM_OCTET_STRING
216 || !getivgen(ctx, p->data, p->data_size))
222 int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
224 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
232 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
235 if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
236 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
239 if (sz == 0 || ctx->enc) {
240 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
246 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
248 if (!OSSL_PARAM_get_size_t(p, &sz)) {
249 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
252 if (sz == 0 || sz > sizeof(ctx->iv)) {
253 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
259 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
261 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
262 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
265 sz = gcm_tls_init(ctx, p->data, p->data_size);
267 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
270 ctx->tls_aad_pad_sz = sz;
273 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
275 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
276 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
279 if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
280 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
284 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV);
287 || p->data_type != OSSL_PARAM_OCTET_STRING
288 || !setivinv(ctx, p->data, p->data_size))
296 int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
297 size_t outsize, const unsigned char *in, size_t inl)
299 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
307 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
311 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
312 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
318 int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
321 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
324 if (!ossl_prov_is_running())
327 i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
335 int ossl_gcm_cipher(void *vctx,
336 unsigned char *out, size_t *outl, size_t outsize,
337 const unsigned char *in, size_t inl)
339 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
341 if (!ossl_prov_is_running())
345 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
349 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
357 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
359 * See also 8.2.2 RBG-based construction.
360 * Random construction consists of a free field (which can be NULL) and a
361 * random field which will use a DRBG that can return at least 96 bits of
362 * entropy strength. (The DRBG must be seeded by the FIPS module).
364 static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
366 int sz = ctx->ivlen - offset;
368 /* Must be at least 96 bits */
369 if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
372 /* Use DRBG to generate random iv */
373 if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz, 0) <= 0)
375 ctx->iv_state = IV_STATE_BUFFERED;
376 ctx->iv_gen_rand = 1;
380 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
381 size_t *padlen, const unsigned char *in,
386 const PROV_GCM_HW *hw = ctx->hw;
388 if (ctx->tls_aad_len != UNINITIALISED_SIZET)
389 return gcm_tls_cipher(ctx, out, padlen, in, len);
391 if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
395 * FIPS requires generation of AES-GCM IV's inside the FIPS module.
396 * The IV can still be set externally (the security policy will state that
397 * this is not FIPS compliant). There are some applications
398 * where setting the IV externally is the only option available.
400 if (ctx->iv_state == IV_STATE_UNINITIALISED) {
401 if (!ctx->enc || !gcm_iv_generate(ctx, 0))
405 if (ctx->iv_state == IV_STATE_BUFFERED) {
406 if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
408 ctx->iv_state = IV_STATE_COPIED;
412 /* The input is AAD if out is NULL */
414 if (!hw->aadupdate(ctx, in, len))
417 /* The input is ciphertext OR plaintext */
418 if (!hw->cipherupdate(ctx, in, len, out))
422 /* The tag must be set before actually decrypting data */
423 if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET)
425 if (!hw->cipherfinal(ctx, ctx->buf))
427 ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
438 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
443 if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN)
446 /* Save the aad for later use. */
448 memcpy(buf, aad, aad_len);
449 dat->tls_aad_len = aad_len;
450 dat->tls_enc_records = 0;
452 len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
453 /* Correct length for explicit iv. */
454 if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
456 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
458 /* If decrypting correct for tag too. */
460 if (len < EVP_GCM_TLS_TAG_LEN)
462 len -= EVP_GCM_TLS_TAG_LEN;
464 buf[aad_len - 2] = (unsigned char)(len >> 8);
465 buf[aad_len - 1] = (unsigned char)(len & 0xff);
466 /* Extra padding: tag appended to record. */
467 return EVP_GCM_TLS_TAG_LEN;
470 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
473 /* Special case: -1 length restores whole IV */
474 if (len == (size_t)-1) {
475 memcpy(ctx->iv, iv, ctx->ivlen);
477 ctx->iv_state = IV_STATE_BUFFERED;
480 /* Fixed field must be at least 4 bytes and invocation field at least 8 */
481 if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
482 || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
485 memcpy(ctx->iv, iv, len);
487 && RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len, 0) <= 0)
490 ctx->iv_state = IV_STATE_BUFFERED;
495 * Handle TLS GCM packet format. This consists of the last portion of the IV
496 * followed by the payload and finally the tag. On encrypt generate IV,
497 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
500 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
501 const unsigned char *in, size_t len)
504 size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
506 unsigned char *tag = NULL;
508 if (!ossl_prov_is_running() || !ctx->key_set)
511 /* Encrypt/decrypt must be performed in place */
512 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
516 * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
517 * Requirements from SP 800-38D". The requirements is for one party to the
518 * communication to fail after 2^64 - 1 keys. We do this on the encrypting
521 if (ctx->enc && ++ctx->tls_enc_records == 0) {
522 ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS);
527 * Set IV from start of buffer or generate IV and write to start of
531 if (!getivgen(ctx, out, arg))
534 if (!setivinv(ctx, out, arg))
538 /* Fix buffer and length to point to payload */
539 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
540 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
541 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
543 tag = ctx->enc ? out + len : (unsigned char *)in + len;
544 if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
545 EVP_GCM_TLS_TAG_LEN)) {
547 OPENSSL_cleanse(out, len);
551 plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
557 ctx->iv_state = IV_STATE_FINISHED;
558 ctx->tls_aad_len = UNINITIALISED_SIZET;