summaryrefslogtreecommitdiffstats
path: root/providers
diff options
context:
space:
mode:
authorTomas Mraz <tomas@openssl.org>2023-11-01 16:54:58 +0100
committerTomas Mraz <tomas@openssl.org>2023-11-23 17:09:56 +0100
commit9cda00baa322b84de3261b610033c4c0e0cb0808 (patch)
tree93f0b89a46541c61669902c62618c327327240b4 /providers
parent68055d010738e8a34d9eead71ef08eb1e8875ce4 (diff)
update/final: Return error if key is not set
Also make sure the key is not set if the key length is changed on the context after the key was set previously. Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (cherry picked from commit 3a95d1e41abf2e8eb0f6f07003bac844950bfaae) Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com> (Merged from https://github.com/openssl/openssl/pull/22613) (cherry picked from commit 29f7a75ce39b4061bd0398f571aa45b883ef5f07)
Diffstat (limited to 'providers')
-rw-r--r--providers/implementations/ciphers/cipher_des.c1
-rw-r--r--providers/implementations/ciphers/cipher_tdes_common.c1
-rw-r--r--providers/implementations/ciphers/ciphercommon.c33
-rw-r--r--providers/implementations/include/prov/ciphercommon.h1
4 files changed, 35 insertions, 1 deletions
diff --git a/providers/implementations/ciphers/cipher_des.c b/providers/implementations/ciphers/cipher_des.c
index c6d13466f7..b8bd47c740 100644
--- a/providers/implementations/ciphers/cipher_des.c
+++ b/providers/implementations/ciphers/cipher_des.c
@@ -98,6 +98,7 @@ static int des_init(void *vctx, const unsigned char *key, size_t keylen,
}
if (!ctx->hw->init(ctx, key, keylen))
return 0;
+ ctx->key_set = 1;
}
return ossl_cipher_generic_set_ctx_params(ctx, params);
}
diff --git a/providers/implementations/ciphers/cipher_tdes_common.c b/providers/implementations/ciphers/cipher_tdes_common.c
index af2f5b9841..cd11f2185d 100644
--- a/providers/implementations/ciphers/cipher_tdes_common.c
+++ b/providers/implementations/ciphers/cipher_tdes_common.c
@@ -92,6 +92,7 @@ static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
}
if (!ctx->hw->init(ctx, key, ctx->keylen))
return 0;
+ ctx->key_set = 1;
}
return ossl_cipher_generic_set_ctx_params(ctx, params);
}
diff --git a/providers/implementations/ciphers/ciphercommon.c b/providers/implementations/ciphers/ciphercommon.c
index fa383165d8..7ad3eb0a1f 100644
--- a/providers/implementations/ciphers/ciphercommon.c
+++ b/providers/implementations/ciphers/ciphercommon.c
@@ -128,7 +128,10 @@ int ossl_cipher_var_keylen_set_ctx_params(void *vctx, const OSSL_PARAM params[])
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
return 0;
}
- ctx->keylen = keylen;
+ if (ctx->keylen != keylen) {
+ ctx->keylen = keylen;
+ ctx->key_set = 0;
+ }
}
return 1;
}
@@ -217,6 +220,7 @@ static int cipher_generic_init_internal(PROV_CIPHER_CTX *ctx,
}
if (!ctx->hw->init(ctx, key, ctx->keylen))
return 0;
+ ctx->key_set = 1;
}
return ossl_cipher_generic_set_ctx_params(ctx, params);
}
@@ -249,6 +253,11 @@ int ossl_cipher_generic_block_update(void *vctx, unsigned char *out,
size_t blksz = ctx->blocksize;
size_t nextblocks;
+ if (!ctx->key_set) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
+ return 0;
+ }
+
if (ctx->tlsversion > 0) {
/*
* Each update call corresponds to a TLS record and is individually
@@ -390,6 +399,11 @@ int ossl_cipher_generic_block_final(void *vctx, unsigned char *out,
if (!ossl_prov_is_running())
return 0;
+ if (!ctx->key_set) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
+ return 0;
+ }
+
if (ctx->tlsversion > 0) {
/* We never finalize TLS, so this is an error */
ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
@@ -456,6 +470,11 @@ int ossl_cipher_generic_stream_update(void *vctx, unsigned char *out,
{
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
+ if (!ctx->key_set) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
+ return 0;
+ }
+
if (inl == 0) {
*outl = 0;
return 1;
@@ -510,9 +529,16 @@ int ossl_cipher_generic_stream_update(void *vctx, unsigned char *out,
int ossl_cipher_generic_stream_final(void *vctx, unsigned char *out,
size_t *outl, size_t outsize)
{
+ PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
+
if (!ossl_prov_is_running())
return 0;
+ if (!ctx->key_set) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
+ return 0;
+ }
+
*outl = 0;
return 1;
}
@@ -526,6 +552,11 @@ int ossl_cipher_generic_cipher(void *vctx, unsigned char *out, size_t *outl,
if (!ossl_prov_is_running())
return 0;
+ if (!ctx->key_set) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
+ return 0;
+ }
+
if (outsize < inl) {
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
return 0;
diff --git a/providers/implementations/include/prov/ciphercommon.h b/providers/implementations/include/prov/ciphercommon.h
index 8153872cba..383b759304 100644
--- a/providers/implementations/include/prov/ciphercommon.h
+++ b/providers/implementations/include/prov/ciphercommon.h
@@ -58,6 +58,7 @@ struct prov_cipher_ctx_st {
unsigned int pad : 1; /* Whether padding should be used or not */
unsigned int enc : 1; /* Set to 1 for encrypt, or 0 otherwise */
unsigned int iv_set : 1; /* Set when the iv is copied to the iv/oiv buffers */
+ unsigned int key_set : 1; /* Set when key is set on the context */
unsigned int updated : 1; /* Set to 1 during update for one shot ciphers */
unsigned int variable_keylength : 1;
unsigned int inverse_cipher : 1; /* set to 1 to use inverse cipher */