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, size_t ivlen_min)
32 ctx->mode = EVP_CIPH_GCM_MODE;
33 ctx->taglen = UNINITIALISED_SIZET;
34 ctx->tls_aad_len = UNINITIALISED_SIZET;
35 ctx->ivlen_min = ivlen_min;
36 ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
37 ctx->keylen = keybits / 8;
39 ctx->libctx = PROV_LIBCTX_OF(provctx);
42 static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
43 const unsigned char *iv, size_t ivlen, int enc)
45 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
47 if (!ossl_prov_is_running())
53 if (ivlen < ctx->ivlen_min || 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 return ctx->hw->setkey(ctx, key, ctx->keylen);
72 int ossl_gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
73 const unsigned char *iv, size_t ivlen)
75 return gcm_init(vctx, key, keylen, iv, ivlen, 1);
78 int ossl_gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
79 const unsigned char *iv, size_t ivlen)
81 return gcm_init(vctx, key, keylen, iv, ivlen, 0);
84 /* increment counter (64-bit int) by 1 */
85 static void ctr64_inc(unsigned char *counter)
100 static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen)
104 || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
106 if (olen == 0 || olen > ctx->ivlen)
108 memcpy(out, ctx->iv + ctx->ivlen - olen, olen);
110 * Invocation field will be at least 8 bytes in size and so no need
111 * to check wrap around or increment more than last 8 bytes.
113 ctr64_inc(ctx->iv + ctx->ivlen - 8);
114 ctx->iv_state = IV_STATE_COPIED;
118 static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl)
125 memcpy(ctx->iv + ctx->ivlen - inl, in, inl);
126 if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
128 ctx->iv_state = IV_STATE_COPIED;
132 int ossl_gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
134 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
138 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
139 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
140 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
143 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
144 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
145 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
148 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
150 size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen :
153 if (!OSSL_PARAM_set_size_t(p, taglen)) {
154 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
159 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
161 if (ctx->iv_state == IV_STATE_UNINITIALISED)
163 if (ctx->ivlen > p->data_size) {
164 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
167 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)
168 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) {
169 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
174 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
176 if (ctx->iv_state == IV_STATE_UNINITIALISED)
178 if (ctx->ivlen > p->data_size) {
179 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
182 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)
183 && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivlen)) {
184 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
189 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
190 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
191 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
194 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
198 || sz > EVP_GCM_TLS_TAG_LEN
200 || ctx->taglen == UNINITIALISED_SIZET) {
201 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
204 if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
205 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
209 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN);
212 || p->data_type != OSSL_PARAM_OCTET_STRING
213 || !getivgen(ctx, p->data, p->data_size))
219 int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
221 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
226 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
229 if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
230 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
233 if (sz == 0 || ctx->enc) {
234 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
240 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
242 if (!OSSL_PARAM_get_size_t(p, &sz)) {
243 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
246 if (sz == 0 || sz > sizeof(ctx->iv)) {
247 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
253 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
255 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
256 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
259 sz = gcm_tls_init(ctx, p->data, p->data_size);
261 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
264 ctx->tls_aad_pad_sz = sz;
267 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
269 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
270 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
273 if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
274 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
278 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV);
281 || p->data_type != OSSL_PARAM_OCTET_STRING
282 || !setivinv(ctx, p->data, p->data_size))
290 int ossl_gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
291 size_t outsize, const unsigned char *in, size_t inl)
293 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
301 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
305 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
306 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
312 int ossl_gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
315 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
318 if (!ossl_prov_is_running())
321 i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
329 int ossl_gcm_cipher(void *vctx,
330 unsigned char *out, size_t *outl, size_t outsize,
331 const unsigned char *in, size_t inl)
333 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
335 if (!ossl_prov_is_running())
339 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
343 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
351 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
353 * See also 8.2.2 RBG-based construction.
354 * Random construction consists of a free field (which can be NULL) and a
355 * random field which will use a DRBG that can return at least 96 bits of
356 * entropy strength. (The DRBG must be seeded by the FIPS module).
358 static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
360 int sz = ctx->ivlen - offset;
362 /* Must be at least 96 bits */
363 if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
366 /* Use DRBG to generate random iv */
367 if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz) <= 0)
369 ctx->iv_state = IV_STATE_BUFFERED;
370 ctx->iv_gen_rand = 1;
374 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
375 size_t *padlen, const unsigned char *in,
380 const PROV_GCM_HW *hw = ctx->hw;
382 if (ctx->tls_aad_len != UNINITIALISED_SIZET)
383 return gcm_tls_cipher(ctx, out, padlen, in, len);
385 if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
389 * FIPS requires generation of AES-GCM IV's inside the FIPS module.
390 * The IV can still be set externally (the security policy will state that
391 * this is not FIPS compliant). There are some applications
392 * where setting the IV externally is the only option available.
394 if (ctx->iv_state == IV_STATE_UNINITIALISED) {
395 if (!ctx->enc || !gcm_iv_generate(ctx, 0))
399 if (ctx->iv_state == IV_STATE_BUFFERED) {
400 if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
402 ctx->iv_state = IV_STATE_COPIED;
406 /* The input is AAD if out is NULL */
408 if (!hw->aadupdate(ctx, in, len))
411 /* The input is ciphertext OR plaintext */
412 if (!hw->cipherupdate(ctx, in, len, out))
416 /* The tag must be set before actually decrypting data */
417 if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET)
419 if (!hw->cipherfinal(ctx, ctx->buf))
421 ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
432 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
437 if (!ossl_prov_is_running() || aad_len != EVP_AEAD_TLS1_AAD_LEN)
440 /* Save the aad for later use. */
442 memcpy(buf, aad, aad_len);
443 dat->tls_aad_len = aad_len;
444 dat->tls_enc_records = 0;
446 len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
447 /* Correct length for explicit iv. */
448 if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
450 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
452 /* If decrypting correct for tag too. */
454 if (len < EVP_GCM_TLS_TAG_LEN)
456 len -= EVP_GCM_TLS_TAG_LEN;
458 buf[aad_len - 2] = (unsigned char)(len >> 8);
459 buf[aad_len - 1] = (unsigned char)(len & 0xff);
460 /* Extra padding: tag appended to record. */
461 return EVP_GCM_TLS_TAG_LEN;
464 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
467 /* Special case: -1 length restores whole IV */
468 if (len == (size_t)-1) {
469 memcpy(ctx->iv, iv, ctx->ivlen);
471 ctx->iv_state = IV_STATE_BUFFERED;
474 /* Fixed field must be at least 4 bytes and invocation field at least 8 */
475 if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
476 || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
479 memcpy(ctx->iv, iv, len);
481 && RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len) <= 0)
484 ctx->iv_state = IV_STATE_BUFFERED;
489 * Handle TLS GCM packet format. This consists of the last portion of the IV
490 * followed by the payload and finally the tag. On encrypt generate IV,
491 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
494 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
495 const unsigned char *in, size_t len)
498 size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
500 unsigned char *tag = NULL;
502 if (!ossl_prov_is_running() || !ctx->key_set)
505 /* Encrypt/decrypt must be performed in place */
506 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
510 * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
511 * Requirements from SP 800-38D". The requirements is for one party to the
512 * communication to fail after 2^64 - 1 keys. We do this on the encrypting
515 if (ctx->enc && ++ctx->tls_enc_records == 0) {
516 ERR_raise(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS);
521 * Set IV from start of buffer or generate IV and write to start of
525 if (!getivgen(ctx, out, arg))
528 if (!setivinv(ctx, out, arg))
532 /* Fix buffer and length to point to payload */
533 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
534 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
535 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
537 tag = ctx->enc ? out + len : (unsigned char *)in + len;
538 if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
539 EVP_GCM_TLS_TAG_LEN)) {
541 OPENSSL_cleanse(out, len);
545 plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
551 ctx->iv_state = IV_STATE_FINISHED;
552 ctx->tls_aad_len = UNINITIALISED_SIZET;