
A soft ctx is created only when the soft switch is turned on. Otherwise, it is meaningless. Signed-off-by: Qi Tao <taoqi10@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_aead.c | 29 ++++++++++++++++++----------- src/uadk_prov_cipher.c | 27 ++++++++++++++++++--------- src/uadk_prov_digest.c | 29 ++++++++++++++++++----------- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/uadk_prov_aead.c b/src/uadk_prov_aead.c index dbbd844..3965f60 100644 --- a/src/uadk_prov_aead.c +++ b/src/uadk_prov_aead.c @@ -153,7 +153,7 @@ static void uadk_aead_mutex_infork(void) pthread_mutex_unlock(&aead_mutex); } -static int uadk_fetch_sw_aead(struct aead_priv_ctx *priv) +static int uadk_create_aead_soft_ctx(struct aead_priv_ctx *priv) { if (priv->sw_aead) return UADK_AEAD_SUCCESS; @@ -172,12 +172,24 @@ static int uadk_fetch_sw_aead(struct aead_priv_ctx *priv) break; } - if (unlikely(priv->sw_aead == NULL)) { + if (unlikely(!priv->sw_aead)) { fprintf(stderr, "aead failed to fetch\n"); return UADK_AEAD_FAIL; } + priv->sw_ctx = EVP_CIPHER_CTX_new(); + if (!priv->sw_ctx) { + fprintf(stderr, "EVP_AEAD_CTX_new failed.\n"); + goto free; + } + return UADK_AEAD_SUCCESS; + +free: + EVP_CIPHER_free(priv->sw_aead); + priv->sw_aead = NULL; + + return UADK_AEAD_FAIL; } static int uadk_prov_aead_soft_init(struct aead_priv_ctx *priv, const unsigned char *key, @@ -185,7 +197,7 @@ static int uadk_prov_aead_soft_init(struct aead_priv_ctx *priv, const unsigned c { int ret; - if (!priv->sw_aead || !priv->sw_ctx) + if (!priv->sw_aead) return UADK_AEAD_FAIL; if (priv->req.op_type == WD_CIPHER_ENCRYPTION_DIGEST) @@ -208,7 +220,7 @@ static int uadk_aead_soft_update(struct aead_priv_ctx *priv, unsigned char *out, { int ret; - if (!priv->sw_aead || !priv->sw_ctx) + if (!priv->sw_aead) return UADK_AEAD_FAIL; if (priv->req.op_type == WD_CIPHER_ENCRYPTION_DIGEST) @@ -230,7 +242,7 @@ static int uadk_aead_soft_final(struct aead_priv_ctx *priv, unsigned char *diges { int ret; - if (!priv->sw_aead || !priv->sw_ctx) + if (!priv->sw_aead) goto error; if (priv->req.op_type == WD_CIPHER_ENCRYPTION_DIGEST) { @@ -834,7 +846,7 @@ static int uadk_prov_aead_init(struct aead_priv_ctx *priv, const unsigned char * priv->stream_switch_flag = 0; if (uadk_get_sw_offload_state()) - uadk_fetch_sw_aead(priv); + uadk_create_aead_soft_ctx(priv); ret = uadk_prov_aead_dev_init(priv); if (unlikely(ret < 0)) { @@ -1118,9 +1130,7 @@ static void *uadk_prov_aead_dupctx(void *ctx) fprintf(stderr, "EVP_CIPHER_CTX_dup failed in ctx copy.\n"); goto free_data; } - } - if (dst_ctx->sw_aead) { ret = EVP_CIPHER_up_ref(dst_ctx->sw_aead); if (!ret) goto free_dup; @@ -1176,9 +1186,6 @@ static void *uadk_##nm##_newctx(void *provctx) \ ctx->ivlen = iv_len; \ ctx->nid = e_nid; \ ctx->taglen = tag_len; \ - ctx->sw_ctx = EVP_CIPHER_CTX_new(); \ - if (ctx->sw_ctx == NULL) \ - fprintf(stderr, "EVP_AEAD_CTX_new failed.\n"); \ strncpy(ctx->alg_name, #algnm, ALG_NAME_SIZE - 1); \ \ return ctx; \ diff --git a/src/uadk_prov_cipher.c b/src/uadk_prov_cipher.c index 7e94fbc..6d3e2c1 100644 --- a/src/uadk_prov_cipher.c +++ b/src/uadk_prov_cipher.c @@ -202,7 +202,7 @@ static int ossl_cipher_cbc_cts_mode_name2id(const char *name) return -1; } -static int uadk_fetch_sw_cipher(struct cipher_priv_ctx *priv) +static int uadk_create_cipher_soft_ctx(struct cipher_priv_ctx *priv) { if (priv->sw_cipher) return UADK_P_SUCCESS; @@ -293,19 +293,31 @@ static int uadk_fetch_sw_cipher(struct cipher_priv_ctx *priv) break; } - if (unlikely(priv->sw_cipher == NULL)) { + if (unlikely(!priv->sw_cipher)) { fprintf(stderr, "cipher failed to fetch\n"); return UADK_P_FAIL; } + priv->sw_ctx = EVP_CIPHER_CTX_new(); + if (!priv->sw_ctx) { + fprintf(stderr, "EVP_CIPHER_CTX_new failed.\n"); + goto free; + } + return UADK_P_SUCCESS; + +free: + EVP_CIPHER_free(priv->sw_cipher); + priv->sw_cipher = NULL; + + return UADK_P_FAIL; } static int uadk_prov_cipher_sw_init(struct cipher_priv_ctx *priv, const unsigned char *key, const unsigned char *iv) { - if (!priv->sw_cipher || !priv->sw_ctx) + if (!priv->sw_cipher) return UADK_P_FAIL; if (!EVP_CipherInit_ex2(priv->sw_ctx, priv->sw_cipher, key, iv, @@ -322,7 +334,7 @@ static int uadk_prov_cipher_sw_init(struct cipher_priv_ctx *priv, static int uadk_prov_cipher_soft_update(struct cipher_priv_ctx *priv, unsigned char *out, int *outl, const unsigned char *in, size_t len) { - if (!priv->sw_cipher || !priv->sw_ctx) + if (!priv->sw_cipher) return UADK_P_FAIL; if (!EVP_CipherInit_ex2(priv->sw_ctx, priv->sw_cipher, priv->key, priv->iv, @@ -346,7 +358,7 @@ static int uadk_prov_cipher_soft_final(struct cipher_priv_ctx *priv, unsigned ch { int sw_final_len = 0; - if (!priv->sw_cipher || !priv->sw_ctx) + if (!priv->sw_cipher) return UADK_P_FAIL; if (!EVP_CipherFinal_ex(priv->sw_ctx, out, &sw_final_len)) { @@ -428,7 +440,7 @@ static int uadk_prov_cipher_init(struct cipher_priv_ctx *priv, priv->switch_threshold = SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT; if (uadk_get_sw_offload_state()) - uadk_fetch_sw_cipher(priv); + uadk_create_cipher_soft_ctx(priv); ret = uadk_prov_cipher_dev_init(priv); if (unlikely(ret <= 0)) { @@ -1351,9 +1363,6 @@ static void *uadk_##nm##_newctx(void *provctx) \ ctx->ivlen = iv_len; \ ctx->nid = e_nid; \ ctx->cts_mode = WD_CIPHER_CBC_CS1; \ - ctx->sw_ctx = EVP_CIPHER_CTX_new(); \ - if (ctx->sw_ctx == NULL) \ - fprintf(stderr, "EVP_CIPHER_CTX_new failed.\n"); \ strncpy(ctx->alg_name, #algnm, ALG_NAME_SIZE - 1); \ if (strcmp(#typ, "block") == 0) \ ctx->pad = 1; \ diff --git a/src/uadk_prov_digest.c b/src/uadk_prov_digest.c index a13d075..98e25e1 100644 --- a/src/uadk_prov_digest.c +++ b/src/uadk_prov_digest.c @@ -130,7 +130,7 @@ static EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in) return out; } -static int uadk_digests_soft_md(struct digest_priv_ctx *priv) +static int uadk_create_digest_soft_ctx(struct digest_priv_ctx *priv) { if (priv->soft_md) return UADK_DIGEST_SUCCESS; @@ -169,17 +169,29 @@ static int uadk_digests_soft_md(struct digest_priv_ctx *priv) break; } - if (unlikely(priv->soft_md == NULL)) { + if (unlikely(!priv->soft_md)) { fprintf(stderr, "digest failed to fetch\n"); return UADK_DIGEST_FAIL; } + priv->soft_ctx = EVP_MD_CTX_new(); + if (!priv->soft_ctx) { + fprintf(stderr, "EVP_MD_CTX_new failed.\n"); + goto free; + } + return UADK_DIGEST_SUCCESS; + +free: + EVP_MD_free(priv->soft_md); + priv->soft_md = NULL; + + return UADK_DIGEST_FAIL; } static int uadk_digest_soft_init(struct digest_priv_ctx *priv) { - if (!priv->soft_md || !priv->soft_ctx) + if (!priv->soft_md) return UADK_DIGEST_FAIL; if (!EVP_DigestInit_ex(priv->soft_ctx, priv->soft_md, NULL)) { @@ -195,7 +207,7 @@ static int uadk_digest_soft_init(struct digest_priv_ctx *priv) static int uadk_digest_soft_update(struct digest_priv_ctx *priv, const void *data, size_t len) { - if (priv->soft_md == NULL) + if (!priv->soft_md) return UADK_DIGEST_FAIL; if (!EVP_DigestUpdate(priv->soft_ctx, data, len)) { @@ -212,7 +224,7 @@ static int uadk_digest_soft_final(struct digest_priv_ctx *priv, unsigned char *d { unsigned int digest_length; - if (priv->soft_md == NULL) + if (!priv->soft_md) return UADK_DIGEST_FAIL; if (!EVP_DigestFinal_ex(priv->soft_ctx, digest, &digest_length)) { @@ -837,9 +849,7 @@ static void *uadk_prov_dupctx(void *dctx) fprintf(stderr, "EVP_MD_CTX_new failed in ctx copy.\n"); goto free_data; } - } - if (dst_ctx->soft_md) { ret = EVP_MD_up_ref(dst_ctx->soft_md); if (!ret) goto free_dup; @@ -868,7 +878,7 @@ static int uadk_prov_init(void *dctx, const OSSL_PARAM params[]) } if (uadk_get_sw_offload_state()) - uadk_digests_soft_md(priv); + uadk_create_digest_soft_ctx(priv); ret = uadk_get_digest_info(priv); if (unlikely(!ret)) @@ -981,9 +991,6 @@ static void *uadk_##name##_newctx(void *provctx) \ ctx->blk_size = blksize; \ ctx->md_size = mdsize; \ ctx->e_nid = nid; \ - ctx->soft_ctx = EVP_MD_CTX_new(); \ - if (ctx->soft_ctx == NULL) \ - fprintf(stderr, "EVP_MD_CTX_new failed.\n"); \ strncpy(ctx->alg_name, #name, ALG_NAME_SIZE - 1); \ ptr = strchr(ctx->alg_name, '_'); \ if (ptr != NULL) \ -- 2.33.0