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 "prov/ciphercommon.h"
13 #include "prov/cipher_gcm.h"
14 #include "prov/providercommonerr.h"
15 #include "crypto/rand.h"
16 #include "prov/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 static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
42 const unsigned char *iv, size_t ivlen, int enc)
44 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
49 if (ivlen < ctx->ivlen_min || ivlen > sizeof(ctx->iv)) {
50 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
54 memcpy(ctx->iv, iv, ivlen);
55 ctx->iv_state = IV_STATE_BUFFERED;
59 if (keylen != ctx->keylen) {
60 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
63 return ctx->hw->setkey(ctx, key, ctx->keylen);
68 int gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
69 const unsigned char *iv, size_t ivlen)
71 return gcm_init(vctx, key, keylen, iv, ivlen, 1);
74 int gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
75 const unsigned char *iv, size_t ivlen)
77 return gcm_init(vctx, key, keylen, iv, ivlen, 0);
80 int gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
82 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
86 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
87 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
88 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
91 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
92 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
93 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
96 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
98 size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen :
101 if (!OSSL_PARAM_set_size_t(p, taglen)) {
102 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
107 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
109 if (ctx->iv_gen != 1 && ctx->iv_gen_rand != 1)
111 if (ctx->ivlen != p->data_size) {
112 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
115 if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)) {
116 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
121 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
122 if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
123 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
126 p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
130 || sz > EVP_GCM_TLS_TAG_LEN
132 || ctx->taglen == UNINITIALISED_SIZET) {
133 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
136 if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
137 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
145 int gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
147 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
152 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
155 if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
156 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
159 if (sz == 0 || ctx->enc) {
160 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
166 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
168 if (!OSSL_PARAM_get_size_t(p, &sz)) {
169 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
172 if (sz == 0 || sz > sizeof(ctx->iv)) {
173 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
179 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
181 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
182 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
185 sz = gcm_tls_init(ctx, p->data, p->data_size);
187 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
190 ctx->tls_aad_pad_sz = sz;
193 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
195 if (p->data_type != OSSL_PARAM_OCTET_STRING) {
196 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
199 if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
200 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
208 int gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
209 size_t outsize, const unsigned char *in, size_t inl)
211 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
214 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
218 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
219 ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
225 int gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
228 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
231 i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
239 int gcm_cipher(void *vctx,
240 unsigned char *out, size_t *outl, size_t outsize,
241 const unsigned char *in, size_t inl)
243 PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
246 ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
250 if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
258 * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
260 * See also 8.2.2 RBG-based construction.
261 * Random construction consists of a free field (which can be NULL) and a
262 * random field which will use a DRBG that can return at least 96 bits of
263 * entropy strength. (The DRBG must be seeded by the FIPS module).
265 static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
267 int sz = ctx->ivlen - offset;
269 /* Must be at least 96 bits */
270 if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
273 /* Use DRBG to generate random iv */
274 if (rand_bytes_ex(ctx->libctx, ctx->iv + offset, sz) <= 0)
276 ctx->iv_state = IV_STATE_BUFFERED;
277 ctx->iv_gen_rand = 1;
281 static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
282 size_t *padlen, const unsigned char *in,
287 const PROV_GCM_HW *hw = ctx->hw;
289 if (ctx->tls_aad_len != UNINITIALISED_SIZET)
290 return gcm_tls_cipher(ctx, out, padlen, in, len);
292 if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
296 * FIPS requires generation of AES-GCM IV's inside the FIPS module.
297 * The IV can still be set externally (the security policy will state that
298 * this is not FIPS compliant). There are some applications
299 * where setting the IV externally is the only option available.
301 if (ctx->iv_state == IV_STATE_UNINITIALISED) {
302 if (!ctx->enc || !gcm_iv_generate(ctx, 0))
306 if (ctx->iv_state == IV_STATE_BUFFERED) {
307 if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
309 ctx->iv_state = IV_STATE_COPIED;
313 /* The input is AAD if out is NULL */
315 if (!hw->aadupdate(ctx, in, len))
318 /* The input is ciphertext OR plaintext */
319 if (!hw->cipherupdate(ctx, in, len, out))
323 /* The tag must be set before actually decrypting data */
324 if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET)
326 if (!hw->cipherfinal(ctx, ctx->buf))
328 ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
339 static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
344 if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
347 /* Save the aad for later use. */
349 memcpy(buf, aad, aad_len);
350 dat->tls_aad_len = aad_len;
351 dat->tls_enc_records = 0;
353 len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
354 /* Correct length for explicit iv. */
355 if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
357 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
359 /* If decrypting correct for tag too. */
361 if (len < EVP_GCM_TLS_TAG_LEN)
363 len -= EVP_GCM_TLS_TAG_LEN;
365 buf[aad_len - 2] = (unsigned char)(len >> 8);
366 buf[aad_len - 1] = (unsigned char)(len & 0xff);
367 /* Extra padding: tag appended to record. */
368 return EVP_GCM_TLS_TAG_LEN;
371 static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
374 /* Special case: -1 length restores whole IV */
375 if (len == (size_t)-1) {
376 memcpy(ctx->iv, iv, ctx->ivlen);
378 ctx->iv_state = IV_STATE_BUFFERED;
381 /* Fixed field must be at least 4 bytes and invocation field at least 8 */
382 if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
383 || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
386 memcpy(ctx->iv, iv, len);
388 && rand_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len) <= 0)
391 ctx->iv_state = IV_STATE_BUFFERED;
395 /* increment counter (64-bit int) by 1 */
396 static void ctr64_inc(unsigned char *counter)
412 * Handle TLS GCM packet format. This consists of the last portion of the IV
413 * followed by the payload and finally the tag. On encrypt generate IV,
414 * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
417 static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
418 const unsigned char *in, size_t len)
421 size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
423 unsigned char *tag = NULL;
428 /* Encrypt/decrypt must be performed in place */
429 if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
433 * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
434 * Requirements from SP 800-38D". The requirements is for one party to the
435 * communication to fail after 2^64 - 1 keys. We do this on the encrypting
438 if (ctx->enc && ++ctx->tls_enc_records == 0) {
439 ERR_raise(ERR_LIB_PROV, EVP_R_TOO_MANY_RECORDS);
443 if (ctx->iv_gen == 0)
446 * Set IV from start of buffer or generate IV and write to start of
450 if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
452 if (arg > ctx->ivlen)
454 memcpy(out, ctx->iv + ctx->ivlen - arg, arg);
456 * Invocation field will be at least 8 bytes in size and so no need
457 * to check wrap around or increment more than last 8 bytes.
459 ctr64_inc(ctx->iv + ctx->ivlen - 8);
461 memcpy(ctx->iv + ctx->ivlen - arg, out, arg);
462 if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
465 ctx->iv_state = IV_STATE_COPIED;
467 /* Fix buffer and length to point to payload */
468 in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
469 out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
470 len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
472 tag = ctx->enc ? out + len : (unsigned char *)in + len;
473 if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
474 EVP_GCM_TLS_TAG_LEN)) {
476 OPENSSL_cleanse(out, len);
480 plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
486 ctx->iv_state = IV_STATE_FINISHED;
487 ctx->tls_aad_len = UNINITIALISED_SIZET;