/*
- * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
-#include <openssl/engine.h>
+#ifndef FIPS_MODULE
+# include <openssl/engine.h>
+#endif
#include <openssl/params.h>
#include <openssl/core_names.h>
#include "internal/cryptlib.h"
#include "internal/provider.h"
#include "internal/core.h"
+#include "internal/safe_math.h"
#include "crypto/evp.h"
#include "evp_local.h"
+OSSL_SAFE_MATH_SIGNED(int, int)
+
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
{
if (ctx == NULL)
if (ctx->fetched_cipher != NULL)
EVP_CIPHER_free(ctx->fetched_cipher);
memset(ctx, 0, sizeof(*ctx));
+ ctx->iv_len = -1;
return 1;
ENGINE_finish(ctx->engine);
#endif
memset(ctx, 0, sizeof(*ctx));
+ ctx->iv_len = -1;
return 1;
}
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
{
- return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
+ EVP_CIPHER_CTX *ctx;
+
+ ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
+ if (ctx == NULL)
+ return NULL;
+
+ ctx->iv_len = -1;
+ return ctx;
}
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
ENGINE *tmpimpl = NULL;
#endif
+
/*
* enc == 1 means we are encrypting.
* enc == 0 means we are decrypting.
return 0;
}
EVP_CIPHER_free(ctx->fetched_cipher);
+ /* Coverity false positive, the reference counting is confusing it */
+ /* coverity[use_after_free] */
ctx->fetched_cipher = (EVP_CIPHER *)cipher;
}
ctx->cipher = cipher;
return ctx->cipher->einit(ctx->algctx,
key,
key == NULL ? 0
- : EVP_CIPHER_CTX_key_length(ctx),
+ : EVP_CIPHER_CTX_get_key_length(ctx),
iv,
iv == NULL ? 0
- : EVP_CIPHER_CTX_iv_length(ctx),
+ : EVP_CIPHER_CTX_get_iv_length(ctx),
params);
}
return ctx->cipher->dinit(ctx->algctx,
key,
key == NULL ? 0
- : EVP_CIPHER_CTX_key_length(ctx),
+ : EVP_CIPHER_CTX_get_key_length(ctx),
iv,
iv == NULL ? 0
- : EVP_CIPHER_CTX_iv_length(ctx),
+ : EVP_CIPHER_CTX_get_iv_length(ctx),
params);
/* Code below to be removed when legacy support is dropped. */
/* Preserve wrap enable flag, zero everything else */
ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
- if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
+ if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL) <= 0) {
ctx->cipher = NULL;
ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
return 0;
|| ctx->cipher->block_size == 16);
if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
- && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
+ && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
return 0;
}
- if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
+ if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
& EVP_CIPH_CUSTOM_IV) == 0) {
- switch (EVP_CIPHER_CTX_mode(ctx)) {
+ switch (EVP_CIPHER_CTX_get_mode(ctx)) {
case EVP_CIPH_STREAM_CIPHER:
case EVP_CIPH_ECB_MODE:
/* fall-through */
case EVP_CIPH_CBC_MODE:
- n = EVP_CIPHER_CTX_iv_length(ctx);
- if (!ossl_assert(n >= 0 && n <= (int)sizeof(ctx->iv)))
- return 0;
- if (iv)
- memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
- memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
+ n = EVP_CIPHER_CTX_get_iv_length(ctx);
+ if (n < 0 || n > (int)sizeof(ctx->iv)) {
+ ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
+ return 0;
+ }
+ if (iv != NULL)
+ memcpy(ctx->oiv, iv, n);
+ memcpy(ctx->iv, ctx->oiv, n);
break;
case EVP_CIPH_CTR_MODE:
ctx->num = 0;
/* Don't reuse IV for CTR mode */
- if (iv)
- memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
+ if (iv != NULL) {
+ n = EVP_CIPHER_CTX_get_iv_length(ctx);
+ if (n <= 0 || n > (int)sizeof(ctx->iv)) {
+ ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
+ return 0;
+ }
+ memcpy(ctx->iv, iv, n);
+ }
break;
default:
}
}
- if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
+ if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
if (!ctx->cipher->init(ctx, key, iv, enc))
return 0;
}
int i, j, bl, cmpl = inl;
if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
- cmpl = (cmpl + 7) / 8;
+ cmpl = safe_div_round_up_int(cmpl, 8, NULL);
bl = ctx->cipher->block_size;
const unsigned char *in, int inl)
{
int ret;
- size_t soutl;
+ size_t soutl, inl_ = (size_t)inl;
int blocksize;
if (outl != NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
return 0;
}
+
ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
- inl + (blocksize == 1 ? 0 : blocksize), in,
- (size_t)inl);
+ inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
+ in, inl_);
if (ret) {
if (soutl > INT_MAX) {
if (ctx->cipher->prov == NULL)
goto legacy;
- blocksize = EVP_CIPHER_CTX_block_size(ctx);
+ blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
{
int fix_len, cmpl = inl, ret;
unsigned int b;
- size_t soutl;
+ size_t soutl, inl_ = (size_t)inl;
int blocksize;
if (outl != NULL) {
if (ctx->cipher->prov == NULL)
goto legacy;
- blocksize = EVP_CIPHER_CTX_block_size(ctx);
+ blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
if (ctx->cipher->cupdate == NULL || blocksize < 1) {
ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
return 0;
}
ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
- inl + (blocksize == 1 ? 0 : blocksize), in,
- (size_t)inl);
+ inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
+ in, inl_);
if (ret) {
if (soutl > INT_MAX) {
b = ctx->cipher->block_size;
if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
- cmpl = (cmpl + 7) / 8;
+ cmpl = safe_div_round_up_int(cmpl, 8, NULL);
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
if (ctx->cipher->prov == NULL)
goto legacy;
- blocksize = EVP_CIPHER_CTX_block_size(ctx);
+ blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
if (c->cipher->prov != NULL) {
int ok;
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
- size_t len = keylen;
+ size_t len;
- if (EVP_CIPHER_CTX_key_length(c) == keylen)
+ if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
return 1;
/* Check the cipher actually understands this parameter */
if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
- OSSL_CIPHER_PARAM_KEYLEN) == NULL)
+ OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
+ ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
return 0;
+ }
params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
+ if (!OSSL_PARAM_set_int(params, keylen))
+ return 0;
ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
-
- return ok > 0 ? 1 : 0;
+ if (ok <= 0)
+ return 0;
+ c->key_len = keylen;
+ return 1;
}
/* Code below to be removed when legacy support is dropped. */
*/
if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
- if (EVP_CIPHER_CTX_key_length(c) == keylen)
+ if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
return 1;
if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
c->key_len = keylen;
switch (type) {
case EVP_CTRL_SET_KEY_LENGTH:
params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
+ ctx->key_len = -1;
break;
case EVP_CTRL_RAND_KEY: /* Used by DES */
set_params = 0;
if (arg < 0)
return 0;
params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
+ ctx->iv_len = -1;
break;
case EVP_CTRL_CCM_SET_L:
if (arg < 2 || arg > 8)
return 0;
sz = 15 - arg;
params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
+ ctx->iv_len = -1;
break;
case EVP_CTRL_AEAD_SET_IV_FIXED:
params[0] = OSSL_PARAM_construct_octet_string(
int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
{
- if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
- return ctx->cipher->set_ctx_params(ctx->algctx, params);
- return 0;
+ int r = 0;
+ const OSSL_PARAM *p;
+
+ if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
+ r = ctx->cipher->set_ctx_params(ctx->algctx, params);
+ if (r > 0) {
+ p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
+ if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
+ r = 0;
+ ctx->key_len = -1;
+ }
+ }
+ if (r > 0) {
+ p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
+ if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
+ r = 0;
+ ctx->iv_len = -1;
+ }
+ }
+ }
+ return r;
}
int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
{
if (cipher != NULL && cipher->gettable_params != NULL)
return cipher->gettable_params(
- ossl_provider_ctx(EVP_CIPHER_provider(cipher)));
+ ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
return NULL;
}
void *provctx;
if (cipher != NULL && cipher->settable_ctx_params != NULL) {
- provctx = ossl_provider_ctx(EVP_CIPHER_provider(cipher));
+ provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
return cipher->settable_ctx_params(NULL, provctx);
}
return NULL;
void *provctx;
if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
- provctx = ossl_provider_ctx(EVP_CIPHER_provider(cipher));
+ provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
return cipher->gettable_ctx_params(NULL, provctx);
}
return NULL;
void *alg;
if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
- alg = ossl_provider_ctx(EVP_CIPHER_provider(cctx->cipher));
+ alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
}
return NULL;
void *provctx;
if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
- provctx = ossl_provider_ctx(EVP_CIPHER_provider(cctx->cipher));
+ provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
}
return NULL;
if (cipher == NULL)
return NULL;
- prov = EVP_CIPHER_provider(cipher);
+ prov = EVP_CIPHER_get0_provider(cipher);
return ossl_provider_libctx(prov);
}
#endif
int kl;
OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
- kl = EVP_CIPHER_CTX_key_length(ctx);
- if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl) <= 0)
+ kl = EVP_CIPHER_CTX_get_key_length(ctx);
+ if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
return 0;
return 1;
}
#endif /* FIPS_MODULE */
}
+EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
+{
+ EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
+
+ if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
+ EVP_CIPHER_CTX_free(out);
+ out = NULL;
+ }
+ return out;
+}
+
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
{
if ((in == NULL) || (in->cipher == NULL)) {
return;
if (legacy_method == NULL)
return;
- nid = EVP_CIPHER_nid(legacy_method);
+ nid = EVP_CIPHER_get_nid(legacy_method);
if (*legacy_nid != NID_undef && *legacy_nid != nid) {
*legacy_nid = -1;
return;
{
evp_generic_do_all(libctx, OSSL_OP_CIPHER,
(void (*)(void *, void *))fn, arg,
- evp_cipher_from_algorithm, evp_cipher_free);
+ evp_cipher_from_algorithm, evp_cipher_up_ref,
+ evp_cipher_free);
}