From: Chenghai Huang huangchenghai2@huawei.com
Add the input pointer length check for the digest and cipher to ensure that the input data is valid.
Signed-off-by: Chenghai Huang huangchenghai2@huawei.com --- src/uadk_digest.c | 10 ++++++++++ src/uadk_prov_cipher.c | 28 ++++++++++++++++++++++++++-- src/uadk_prov_digest.c | 21 +++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/src/uadk_digest.c b/src/uadk_digest.c index f460fcf..073c171 100644 --- a/src/uadk_digest.c +++ b/src/uadk_digest.c @@ -749,6 +749,11 @@ static int uadk_e_digest_update(EVP_MD_CTX *ctx, const void *data, size_t data_l return 0; }
+ if (unlikely(data_len && !data)) { + fprintf(stderr, "data to be digest is NULL.\n"); + return 0; + } + if (unlikely(priv->switch_flag == UADK_DO_SOFT)) goto soft_update;
@@ -877,6 +882,11 @@ static int uadk_e_digest_final(EVP_MD_CTX *ctx, unsigned char *digest) return 0; }
+ if (unlikely(!digest)) { + fprintf(stderr, "the output buffer is NULL.\n"); + return 0; + } + digest_set_msg_state(priv, true); priv->req.in = priv->data; priv->req.out = priv->out; diff --git a/src/uadk_prov_cipher.c b/src/uadk_prov_cipher.c index fa79764..f6d1581 100644 --- a/src/uadk_prov_cipher.c +++ b/src/uadk_prov_cipher.c @@ -737,6 +737,9 @@ static int uadk_prov_cipher_cipher(void *vctx, unsigned char *out, size_t *outl, struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx; int ret;
+ if (!vctx || !in || !out || !outl) + return UADK_E_FAIL; + if (inl == 0) { *outl = 0; return 1; @@ -763,6 +766,9 @@ static int uadk_prov_cipher_block_final(void *vctx, unsigned char *out, int sw_final_len = 0; int ret;
+ if (!vctx || !out || !outl) + return UADK_E_FAIL; + if (priv->sw_cipher && priv->switch_flag == UADK_DO_SOFT) { goto do_soft; @@ -845,6 +851,9 @@ static int uadk_prov_cipher_block_update(void *vctx, unsigned char *out, struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx; int ret;
+ if (!vctx || !in || !out || !outl) + return UADK_E_FAIL; + if (inl == 0) { *outl = 0; return 1; @@ -869,6 +878,9 @@ static int uadk_prov_cipher_stream_update(void *vctx, unsigned char *out, struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx; int ret;
+ if (!vctx || !in || !out || !outl) + return UADK_E_FAIL; + if (inl == 0) { *outl = 0; return 1; @@ -916,6 +928,9 @@ static int uadk_prov_cipher_stream_final(void *vctx, unsigned char *out, struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx; int sw_final_len = 0;
+ if (!vctx || !out || !outl) + return UADK_E_FAIL; + if (priv->sw_cipher && priv->switch_flag == UADK_DO_SOFT) { if (!EVP_CipherFinal_ex(priv->sw_ctx, out, &sw_final_len)) { @@ -937,6 +952,9 @@ static int uadk_prov_cipher_einit(void *vctx, const unsigned char *key, size_t k struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx; int ret;
+ if (!vctx) + return UADK_E_FAIL; + priv->req.op_type = WD_CIPHER_ENCRYPTION; priv->enc = 1;
@@ -954,6 +972,9 @@ static int uadk_prov_cipher_dinit(void *vctx, const unsigned char *key, size_t k struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx; int ret;
+ if (!vctx) + return UADK_E_FAIL; + priv->req.op_type = WD_CIPHER_DECRYPTION; priv->enc = 0;
@@ -981,8 +1002,8 @@ static int uadk_prov_cipher_set_ctx_params(void *vctx, const OSSL_PARAM params[] const OSSL_PARAM *p; int ret = 1;
- if (params == NULL) - return 1; + if (!vctx) + return UADK_E_FAIL;
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_PADDING); if (p != NULL) { @@ -1018,6 +1039,9 @@ static int uadk_prov_cipher_get_ctx_params(void *vctx, OSSL_PARAM params[]) struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)vctx; OSSL_PARAM *p;
+ if (!vctx || !params) + return UADK_E_FAIL; + p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); if (p != NULL && !OSSL_PARAM_set_size_t(p, priv->keylen)) { ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); diff --git a/src/uadk_prov_digest.c b/src/uadk_prov_digest.c index 8cadbcf..33beedf 100644 --- a/src/uadk_prov_digest.c +++ b/src/uadk_prov_digest.c @@ -413,6 +413,11 @@ do_soft_digest:
static int uadk_digest_update(struct digest_priv_ctx *priv, const void *data, size_t data_len) { + if (!priv->data) { + fprintf(stderr, "failed to do digest update, data in CTX is NULL.\n"); + return UADK_DIGEST_FAIL; + } + if (unlikely(priv->switch_flag == UADK_DO_SOFT)) goto soft_update;
@@ -505,6 +510,11 @@ static int uadk_digest_final(struct digest_priv_ctx *priv, unsigned char *digest struct async_op op; int ret;
+ if (!priv->data) { + fprintf(stderr, "failed to do digest final, data in CTX is NULL.\n"); + return UADK_DIGEST_FAIL; + } + priv->req.has_next = DIGEST_END; priv->req.in = priv->data; priv->req.out = priv->out; @@ -556,6 +566,11 @@ static int uadk_digest_digest(struct digest_priv_ctx *priv, const void *data, si struct async_op op; int ret;
+ if (!priv->data) { + fprintf(stderr, "failed to do single digest, data in CTX is NULL.\n"); + return UADK_DIGEST_FAIL; + } + priv->req.has_next = DIGEST_END; priv->req.in = priv->data; priv->req.out = priv->out; @@ -734,6 +749,12 @@ static int uadk_prov_digest(void *dctx, const unsigned char *in, size_t inl, return UADK_DIGEST_FAIL; }
+ if (inl > BUF_LEN) { + fprintf(stderr, "data len(%lu) can not be processed in single digest.\n", + inl); + return UADK_DIGEST_FAIL; + } + if (outsz > 0) { ret = uadk_digest_digest(priv, in, inl, out); if (!ret)