[PATCH 00/12] uadk_engine/uadk_provider: some bugfix

From: JiangShui Yang <yangjiangshui@h-partners.com> Chenghai Huang (1): uadk_provider: add reset for part of ctx after final Qi Tao (1): uadk_provider/rsa: bugfix for rsa digest interface Weili Qian (3): uadk_engine/sm2: set the default user id uadk_provider/sm2: delete duplicate applications sess uadk_engine/digest: small packet directly uses soft computing Wenkai Lin (2): uadk_engine: optimize for sec cipher and digest init uadk_engine/aead: fix for set key problem Zhushuai Yin (3): uadk_provider:Improved SM3 hmac performance uadk_engine:Fix DH algorithm shared key comparison failure uadk_provider/sm2:fix the issue of SM2 authentication failure lizhi (2): uadk_provider: clean up unused functions uadk_prov: fix a cleancode issue src/uadk_aead.c | 65 ++++++++++---------- src/uadk_async.c | 5 ++ src/uadk_async.h | 1 + src/uadk_cipher.c | 98 +++++++++++++----------------- src/uadk_dh.c | 8 ++- src/uadk_digest.c | 120 ++++++++++++++++++------------------- src/uadk_prov_der_writer.c | 69 --------------------- src/uadk_prov_der_writer.h | 4 -- src/uadk_prov_digest.c | 29 ++++++--- src/uadk_prov_ffc.c | 3 +- src/uadk_prov_hmac.c | 18 ++++-- src/uadk_prov_rsa.c | 107 +++++++++++++++++++++------------ src/uadk_prov_sm2.c | 108 +++++++++++++++++---------------- src/uadk_sm2.c | 95 +++++++++++++++-------------- 14 files changed, 356 insertions(+), 374 deletions(-) -- 2.43.0

From: Zhushuai Yin <yinzhushuai@huawei.com> Reduce the digest block size to prevent repeated application and release of large memory, thereby improving performance. Signed-off-by: Zhushuai Yin <yinzhushuai@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_digest.c | 2 +- src/uadk_prov_hmac.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uadk_prov_digest.c b/src/uadk_prov_digest.c index 208f381..39e589a 100644 --- a/src/uadk_prov_digest.c +++ b/src/uadk_prov_digest.c @@ -50,7 +50,7 @@ #define MD5_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT (8 * 1024) #define SHA_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT (512) #define MAX_DIGEST_LENGTH 64 -#define DIGEST_BLOCK_SIZE (512 * 1024) +#define DIGEST_BLOCK_SIZE 16384 #define ALG_NAME_SIZE 128 #define UADK_DIGEST_DEF_CTXS 1 diff --git a/src/uadk_prov_hmac.c b/src/uadk_prov_hmac.c index 9ad9168..7ee663a 100644 --- a/src/uadk_prov_hmac.c +++ b/src/uadk_prov_hmac.c @@ -41,7 +41,7 @@ #define MAX_DIGEST_LENGTH 64 #define MAX_KEY_LEN 144 -#define HMAC_BLOCK_SIZE (512 * 1024) +#define HMAC_BLOCK_SIZE 16384 #define ALG_NAME_SIZE 128 #define PARAMS_SIZE 2 -- 2.43.0

From: Weili Qian <qianweili@huawei.com> In tls handshake, client/server does not set a valid user id. so a default user id is set for signing process. When sess is empty, alloc sess to execute the business. Signed-off-by: Weili Qian <qianweili@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_sm2.c | 95 +++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/src/uadk_sm2.c b/src/uadk_sm2.c index 1294c4e..f538594 100644 --- a/src/uadk_sm2.c +++ b/src/uadk_sm2.c @@ -26,7 +26,9 @@ #include "uadk.h" #include "uadk_pkey.h" -#define GET_SIGNLEN 1 +#define GET_SIGNLEN 1 +#define SM2_DEFAULT_USERID "1234567812345678" +#define SM2_DEFAULT_USERID_LEN 16 enum { CTX_INIT_FAIL = -1, @@ -223,6 +225,26 @@ static int sm2_update_sess(struct sm2_ctx *smctx) return 0; } +static int sm2_ctx_check(struct sm2_ctx *smctx) +{ + if (!smctx) { + fprintf(stderr, "sm2 sign smctx is NULL\n"); + return UADK_DO_SOFT; + } + + if (!smctx->sess && sm2_update_sess(smctx)) { + fprintf(stderr, "sm2 sign sess is NULL\n"); + return UADK_DO_SOFT; + } + + if (smctx->init_status != CTX_INIT_SUCC) { + fprintf(stderr, "sm2 ctx init failed\n"); + return UADK_DO_SOFT; + } + + return 1; +} + static int update_public_key(EVP_PKEY_CTX *ctx) { struct sm2_ctx *smctx = EVP_PKEY_CTX_get_data(ctx); @@ -685,6 +707,7 @@ static int sm2_sign_check(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, EVP_PKEY *p_key = EVP_PKEY_CTX_get0_pkey(ctx); EC_KEY *ec = EVP_PKEY_get0(p_key); const int sig_sz = ECDSA_size(ec); + int ret; if (!siglen) { fprintf(stderr, "siglen is NULL\n"); @@ -702,15 +725,9 @@ static int sm2_sign_check(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, return GET_SIGNLEN; } - if (!smctx || !smctx->sess) { - fprintf(stderr, "smctx or sess NULL\n"); - return UADK_DO_SOFT; - } - - if (smctx->init_status != CTX_INIT_SUCC) { - fprintf(stderr, "sm2 ctx init failed\n"); + ret = sm2_ctx_check(smctx); + if (ret == UADK_DO_SOFT) return UADK_DO_SOFT; - } if (sig_sz <= 0) { fprintf(stderr, "sig_sz error\n"); @@ -813,16 +830,11 @@ static int sm2_verify_check(EVP_PKEY_CTX *ctx, size_t tbslen) { struct sm2_ctx *smctx = EVP_PKEY_CTX_get_data(ctx); + int ret; - if (!smctx || !smctx->sess) { - fprintf(stderr, "smctx or sess NULL\n"); - return UADK_DO_SOFT; - } - - if (smctx->init_status != CTX_INIT_SUCC) { - fprintf(stderr, "sm2 ctx init failed\n"); + ret = sm2_ctx_check(smctx); + if (ret == UADK_DO_SOFT) return UADK_DO_SOFT; - } if (tbslen > SM2_KEY_BYTES) return UADK_DO_SOFT; @@ -924,17 +936,11 @@ static int sm2_encrypt_check(EVP_PKEY_CTX *ctx, EVP_PKEY *p_key = EVP_PKEY_CTX_get0_pkey(ctx); EC_KEY *ec = EVP_PKEY_get0(p_key); const EVP_MD *md; - int c3_size; - - if (!smctx || !smctx->sess) { - fprintf(stderr, "smctx or sess NULL\n"); - return UADK_DO_SOFT; - } + int ret, c3_size; - if (smctx->init_status != CTX_INIT_SUCC) { - fprintf(stderr, "sm2 ctx init failed\n"); + ret = sm2_ctx_check(smctx); + if (ret == UADK_DO_SOFT) return UADK_DO_SOFT; - } md = (smctx->ctx.md == NULL) ? EVP_sm3() : smctx->ctx.md; c3_size = EVP_MD_size(md); @@ -1034,18 +1040,12 @@ static int sm2_decrypt_check(EVP_PKEY_CTX *ctx, const unsigned char *in, size_t inlen) { struct sm2_ctx *smctx = EVP_PKEY_CTX_get_data(ctx); + int ret, hash_size; const EVP_MD *md; - int hash_size; - - if (!smctx || !smctx->sess) { - fprintf(stderr, "smctx or sess NULL\n"); - return UADK_DO_SOFT; - } - if (smctx->init_status != CTX_INIT_SUCC) { - fprintf(stderr, "sm2 ctx init failed\n"); + ret = sm2_ctx_check(smctx); + if (ret == UADK_DO_SOFT) return UADK_DO_SOFT; - } md = (smctx->ctx.md == NULL) ? EVP_sm3() : smctx->ctx.md; hash_size = EVP_MD_size(md); @@ -1229,6 +1229,7 @@ static int sm2_init(EVP_PKEY_CTX *ctx) } smctx->init_status = CTX_INIT_SUCC; + end: EVP_PKEY_CTX_set_data(ctx, smctx); EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0); @@ -1601,13 +1602,14 @@ static int sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) } if (!smctx->ctx.id_set) { - /* - * An ID value must be set. The specifications are not clear whether a - * NULL is allowed. We only allow it if set explicitly for maximum - * flexibility. - */ - fprintf(stderr, "id not set\n"); - return 0; + /* If the id is not set, use the default value. */ + smctx->ctx.id = OPENSSL_memdup(SM2_DEFAULT_USERID, SM2_DEFAULT_USERID_LEN); + if (smctx->ctx.id == NULL) { + fprintf(stderr, "failed to memdup ctx id\n"); + return 0; + } + smctx->ctx.id_len = SM2_DEFAULT_USERID_LEN; + smctx->ctx.id_set = 1; } if (mdlen < 0) { @@ -1628,11 +1630,16 @@ static int sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) static int sm2_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src) # endif { - struct sm2_ctx *dctx, *sctx; + struct sm2_ctx *sctx = EVP_PKEY_CTX_get_data(src); + struct sm2_ctx *dctx; + + if (!sctx) { + fprintf(stderr, "src ctx is NULL\n"); + return 0; + } if (!sm2_init(dst)) return 0; - sctx = EVP_PKEY_CTX_get_data(src); dctx = EVP_PKEY_CTX_get_data(dst); if (sctx->ctx.gen_group != NULL) { dctx->ctx.gen_group = EC_GROUP_dup(sctx->ctx.gen_group); -- 2.43.0

From: lizhi <lizhi206@huawei.com> Remove unused functions to reduce compilation warnings. Signed-off-by: lizhi <lizhi206@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_der_writer.c | 69 -------------------------------------- src/uadk_prov_der_writer.h | 4 --- 2 files changed, 73 deletions(-) diff --git a/src/uadk_prov_der_writer.c b/src/uadk_prov_der_writer.c index d20640a..affb860 100644 --- a/src/uadk_prov_der_writer.c +++ b/src/uadk_prov_der_writer.c @@ -120,16 +120,6 @@ int ossl_DER_w_precompiled(WPACKET *pkt, int tag, int_end_context(pkt, tag); } -int ossl_DER_w_boolean(WPACKET *pkt, int tag, int b) -{ - return int_start_context(pkt, tag) && - WPACKET_start_sub_packet(pkt) && - (!b || WPACKET_put_bytes_u8(pkt, 0xFF)) && - !WPACKET_close(pkt) && - !WPACKET_put_bytes_u8(pkt, DER_P_BOOLEAN) && - int_end_context(pkt, tag); -} - int ossl_DER_w_octet_string(WPACKET *pkt, int tag, const unsigned char *data, size_t data_n) { @@ -141,19 +131,6 @@ int ossl_DER_w_octet_string(WPACKET *pkt, int tag, int_end_context(pkt, tag); } -int ossl_DER_w_octet_string_uint32(WPACKET *pkt, int tag, uint32_t value) -{ - unsigned char tmp[4] = { 0, 0, 0, 0 }; - unsigned char *pbuf = tmp + (sizeof(tmp) - 1); - - while (value > 0) { - *pbuf-- = (value & 0xFF); - value >>= LOW_BIT_SIZE; - } - - return ossl_DER_w_octet_string(pkt, tag, tmp, sizeof(tmp)); -} - static int int_der_w_integer(WPACKET *pkt, int tag, int (*put_bytes)(WPACKET *pkt, const void *v, unsigned int *top_byte), @@ -195,52 +172,6 @@ int ossl_DER_w_uint32(WPACKET *pkt, int tag, uint32_t v) return int_der_w_integer(pkt, tag, int_put_bytes_uint32, &v); } -static BN_ULONG *bn_get_words(const BIGNUM *a) -{ - return a->d; -} - -static int int_put_bytes_bn(WPACKET *pkt, const void *v, - unsigned int *top_byte) -{ - unsigned char *p = NULL; - size_t n = BN_num_bytes(v); - - /* The BIGNUM limbs are in LE order */ - *top_byte = - ((bn_get_words(v)[(n - 1) / BN_BYTES]) - >> (BYTES_TO_BITS_OFFSET * ((n - 1) % BN_BYTES))) - & 0xFF; - - if (!WPACKET_allocate_bytes(pkt, n, &p)) - return 0; - - if (p != NULL) - BN_bn2bin(v, p); - - return 1; -} - -int ossl_DER_w_bn(WPACKET *pkt, int tag, const BIGNUM *v) -{ - if (v == NULL || BN_is_negative(v)) - return 0; - - if (BN_is_zero(v)) - return ossl_DER_w_uint32(pkt, tag, 0); - - return int_der_w_integer(pkt, tag, int_put_bytes_bn, v); -} - -int ossl_DER_w_null(WPACKET *pkt, int tag) -{ - return int_start_context(pkt, tag) && - WPACKET_start_sub_packet(pkt) && - WPACKET_close(pkt) && - WPACKET_put_bytes_u8(pkt, DER_P_NULL) && - int_end_context(pkt, tag); -} - /* Constructed things need a start and an end */ int ossl_DER_w_begin_sequence(WPACKET *pkt, int tag) { diff --git a/src/uadk_prov_der_writer.h b/src/uadk_prov_der_writer.h index d2a47ea..d8d0232 100644 --- a/src/uadk_prov_der_writer.h +++ b/src/uadk_prov_der_writer.h @@ -110,13 +110,9 @@ int ossl_DER_w_precompiled(WPACKET *pkt, int tag, const unsigned char *precompiled, size_t precompiled_n); -int ossl_DER_w_boolean(WPACKET *pkt, int tag, int b); int ossl_DER_w_uint32(WPACKET *pkt, int tag, uint32_t v); -int ossl_DER_w_bn(WPACKET *pkt, int tag, const BIGNUM *v); -int ossl_DER_w_null(WPACKET *pkt, int tag); int ossl_DER_w_octet_string(WPACKET *pkt, int tag, const unsigned char *data, size_t data_n); -int ossl_DER_w_octet_string_uint32(WPACKET *pkt, int tag, uint32_t value); /* * All constructors for constructed elements have a begin and a end function -- 2.43.0

Fixing function return values, multithreading resource release, memory leaks, code specifications, and other issues. Signed-off-by: Qi Tao <taoqi10@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_rsa.c | 107 ++++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 39 deletions(-) diff --git a/src/uadk_prov_rsa.c b/src/uadk_prov_rsa.c index 59cdc5d..4218416 100644 --- a/src/uadk_prov_rsa.c +++ b/src/uadk_prov_rsa.c @@ -64,8 +64,6 @@ #define GENCB_RETRY 3 #define PRIME_CHECK_BIT_NUM 4 -#define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1) - UADK_PKEY_KEYMGMT_DESCR(rsa, RSA); UADK_PKEY_SIGNATURE_DESCR(rsa, RSA); UADK_PKEY_ASYM_CIPHER_DESCR(rsa, RSA); @@ -2145,7 +2143,6 @@ static void uadk_signature_rsa_freectx(void *vprsactx) free_tbuf(priv); OPENSSL_clear_free(priv, sizeof(*priv)); - uadk_prov_destroy_rsa(); } static void *uadk_asym_cipher_rsa_newctx(void *provctx) @@ -2168,7 +2165,6 @@ static void uadk_asym_cipher_rsa_freectx(void *vprsactx) return; OPENSSL_free(priv); - uadk_prov_destroy_rsa(); } static int uadk_signature_rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) @@ -2221,30 +2217,31 @@ static int uadk_rsa_check_padding(const PROV_RSA_SIG_CTX *prsactx, switch (prsactx->pad_mode) { case RSA_NO_PADDING: ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE); - return 0; + return UADK_E_FAIL; case RSA_X931_PADDING: if (RSA_X931_hash_id(mdnid) == -1) { ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST); - return 0; + return UADK_E_FAIL; } break; case RSA_PKCS1_PSS_PADDING: - if (rsa_pss_restricted(prsactx)) + if (prsactx->min_saltlen != -1) { if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname)) || (mgf1_mdname != NULL && !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) { ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); - return 0; + return UADK_E_FAIL; } + } break; default: break; } - return 1; + return UADK_E_SUCCESS; } -int uadk_digest_md_to_nid(const EVP_MD *md, const OSSL_ITEM *it, size_t it_len) +static int uadk_digest_md_to_nid(const EVP_MD *md, const OSSL_ITEM *it, size_t it_len) { size_t i; @@ -2257,7 +2254,7 @@ int uadk_digest_md_to_nid(const EVP_MD *md, const OSSL_ITEM *it, size_t it_len) return NID_undef; } -int uadk_digest_get_approved_nid(const EVP_MD *md) +static int uadk_digest_get_approved_nid(const EVP_MD *md) { static const OSSL_ITEM name_to_nid[] = { { NID_sha1, OSSL_DIGEST_NAME_SHA1 }, @@ -2276,7 +2273,7 @@ int uadk_digest_get_approved_nid(const EVP_MD *md) return uadk_digest_md_to_nid(md, name_to_nid, OSSL_NELEM(name_to_nid)); } -int uadk_digest_rsa_sign_get_md_nid(OSSL_LIB_CTX *ctx, const EVP_MD *md, +static int uadk_digest_rsa_sign_get_md_nid(OSSL_LIB_CTX *ctx, const EVP_MD *md, int sha1_allowed) { return uadk_digest_get_approved_nid(md); @@ -2285,6 +2282,8 @@ int uadk_digest_rsa_sign_get_md_nid(OSSL_LIB_CTX *ctx, const EVP_MD *md, static int uadk_rsa_setup_md(PROV_RSA_SIG_CTX *ctx, const char *mdname, const char *mdprops) { + size_t mdname_len; + if (mdprops == NULL) mdprops = ctx->propq; @@ -2293,8 +2292,7 @@ static int uadk_rsa_setup_md(PROV_RSA_SIG_CTX *ctx, const char *mdname, int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN); int md_nid = uadk_digest_rsa_sign_get_md_nid(ctx->libctx, md, sha1_allowed); - size_t mdname_len = strlen(mdname); - + mdname_len = strlen(mdname); if (md == NULL || md_nid <= 0 || !uadk_rsa_check_padding(ctx, mdname, NULL, md_nid) || mdname_len >= sizeof(ctx->mdname)) { @@ -2307,24 +2305,32 @@ static int uadk_rsa_setup_md(PROV_RSA_SIG_CTX *ctx, const char *mdname, if (mdname_len >= sizeof(ctx->mdname)) ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, "%s exceeds name buffer length", mdname); - EVP_MD_free(md); + if (md) + EVP_MD_free(md); return 0; } if (!ctx->mgf1_md_set) { if (!EVP_MD_up_ref(md)) { - EVP_MD_free(md); + if (md) + EVP_MD_free(md); return 0; } - EVP_MD_free(ctx->mgf1_md); + if (ctx->mgf1_md) + EVP_MD_free(ctx->mgf1_md); ctx->mgf1_md = md; ctx->mgf1_mdnid = md_nid; OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname)); } - EVP_MD_CTX_free(ctx->mdctx); - EVP_MD_free(ctx->md); - ctx->mdctx = NULL; + if (ctx->mdctx) { + EVP_MD_CTX_free(ctx->mdctx); + ctx->mdctx = NULL; + } + + if (ctx->md) + EVP_MD_free(ctx->md); + ctx->md = md; ctx->mdnid = md_nid; OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); @@ -2361,8 +2367,11 @@ static int uadk_signature_rsa_digest_signverify_init(void *vprsactx, const char return 1; error: - EVP_MD_CTX_free(priv->mdctx); - priv->mdctx = NULL; + if (priv->mdctx) { + EVP_MD_CTX_free(priv->mdctx); + priv->mdctx = NULL; + } + return 0; } @@ -2426,7 +2435,7 @@ ENCODE_DIGESTINFO_SHA(sha3_512, 0x0a, SHA512_DIGEST_LENGTH); return digestinfo_##name##_der -const unsigned char *uadk_rsa_digestinfo_encoding(int md_nid, size_t *len) +static const unsigned char *uadk_rsa_digestinfo_encoding(int md_nid, size_t *len) { switch (md_nid) { MD_CASE(sha1); @@ -2506,7 +2515,6 @@ static int uadk_signature_rsa_digest_sign_final(void *vprsactx, unsigned char *s if (priv->mdctx == NULL) return UADK_E_FAIL; - priv->flag_allow_md = 1; rsasize = uadk_rsa_size(priv->rsa); /* @@ -2525,6 +2533,8 @@ static int uadk_signature_rsa_digest_sign_final(void *vprsactx, unsigned char *s return 1; } + priv->flag_allow_md = 1; + if (priv->pad_mode == RSA_PKCS1_PADDING) { /* Compute the encoded digest. */ if (priv->mdnid == NID_md5_sha1) { @@ -2544,18 +2554,22 @@ static int uadk_signature_rsa_digest_sign_final(void *vprsactx, unsigned char *s goto err; encoded = tmps; } + } else { + fprintf(stderr, "This padding mode is not supported\n"); + return UADK_E_FAIL; } ret = uadk_prov_rsa_private_sign(encoded_len, encoded, sig, priv->rsa, priv->pad_mode); if (ret == UADK_DO_SOFT || ret == UADK_E_FAIL) - goto exe_soft; + goto err; + OPENSSL_clear_free(tmps, encoded_len); return ret; err: OPENSSL_clear_free(tmps, encoded_len); -exe_soft: if (ret == UADK_DO_SOFT) - uadk_rsa_sw_sign(vprsactx, sig, siglen, sigsize, digest, dlen); + return uadk_rsa_sw_sign(vprsactx, sig, siglen, sigsize, digest, dlen); + return UADK_E_FAIL; } @@ -2584,7 +2598,7 @@ static int uadk_signature_rsa_digest_verify_final(void *vprsactx, const unsigned unsigned char *decrypt_buf = NULL, *encoded = NULL; size_t decrypt_len, encoded_len = 0; unsigned char digest[EVP_MAX_MD_SIZE]; - unsigned int dlen = 0, len; + unsigned int dlen = 0; int ret = UADK_E_FAIL; if (priv == NULL) @@ -2600,25 +2614,24 @@ static int uadk_signature_rsa_digest_verify_final(void *vprsactx, const unsigned if (!EVP_DigestFinal_ex(priv->mdctx, digest, &dlen)) return UADK_E_FAIL; - if (priv->pad_mode == RSA_PKCS1_PADDING) { if (siglen != (size_t)uadk_rsa_size(priv->rsa)) { ERR_raise(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH); - return 0; + return UADK_E_FAIL; } /* Recover the encoded digest. */ decrypt_buf = OPENSSL_malloc(siglen); if (decrypt_buf == NULL) { ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); - goto err; + return UADK_E_FAIL; } - len = uadk_prov_rsa_public_verify(siglen, sig, decrypt_buf, + ret = uadk_prov_rsa_public_verify(siglen, sig, decrypt_buf, priv->rsa, priv->pad_mode); - if (len <= 0) + if (ret <= 0) goto err; - decrypt_len = len; + decrypt_len = ret; if (priv->mdnid == NID_md5_sha1) { /* @@ -2628,34 +2641,50 @@ static int uadk_signature_rsa_digest_verify_final(void *vprsactx, const unsigned */ if (decrypt_len != SSL_SIG_LENGTH) { ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE); + ret = UADK_E_FAIL; goto err; } if (siglen != SSL_SIG_LENGTH) { ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH); + ret = UADK_E_FAIL; goto err; } - if (memcmp(decrypt_buf, sig, SSL_SIG_LENGTH) != 0) { + if (memcmp(decrypt_buf, digest, SSL_SIG_LENGTH) != 0) { ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE); + ret = UADK_E_FAIL; goto err; } } else { /* Construct the encoded digest and ensure it matches. */ - if (!encode_pkcs1(&encoded, &encoded_len, priv->mdnid, digest, dlen)) + if (!encode_pkcs1(&encoded, &encoded_len, priv->mdnid, digest, dlen)) { + ret = UADK_E_FAIL; goto err; + } if (encoded_len != decrypt_len || memcmp(encoded, decrypt_buf, encoded_len) != 0) { ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE); + ret = UADK_E_FAIL; goto err; } } - ret = 1; + ret = UADK_E_SUCCESS; + } else { + fprintf(stderr, "This padding mode is not supported\n"); + return UADK_E_FAIL; } + err: - OPENSSL_clear_free(encoded, encoded_len); - OPENSSL_clear_free(decrypt_buf, siglen); + if (encoded) + OPENSSL_clear_free(encoded, encoded_len); + if (decrypt_buf) + OPENSSL_clear_free(decrypt_buf, siglen); + + if (ret == UADK_DO_SOFT) + return uadk_rsa_sw_verify(vprsactx, sig, siglen, digest, dlen); + return ret; } -- 2.43.0

From: Weili Qian <qianweili@huawei.com> Currently, the sm2 sign and verify always use the sm3 algorithm for hash calculation, and hash algorithm is calculated by the uadk provider process calling the OpenSSL interface instead of the uadk. Therefore, there is no need to re-alloc session when updating the hash algorithm. Signed-off-by: Weili Qian <qianweili@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_sm2.c | 95 +++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/src/uadk_prov_sm2.c b/src/uadk_prov_sm2.c index c465a15..5499250 100644 --- a/src/uadk_prov_sm2.c +++ b/src/uadk_prov_sm2.c @@ -817,7 +817,8 @@ static void uadk_signature_sm2_freectx(void *vpsm2ctx) } if (smctx->sm2_pd) { - BN_free(smctx->sm2_pd->order); + if (smctx->sm2_pd->order) + BN_free(smctx->sm2_pd->order); OPENSSL_free(smctx->sm2_pd); } @@ -950,11 +951,25 @@ static int uadk_prov_sm2_update_sess(SM2_PROV_CTX *smctx) 0x53, 0xbb, 0xf4, 0x09, 0x39, 0xd5, 0x41, 0x23 }; struct wd_ecc_sess_setup setup = {0}; + struct sched_params params = {0}; handle_t sess; - BIGNUM *order; int type; - setup.alg = "sm2"; + if (smctx->sm2_pd == NULL) { + fprintf(stderr, "invalid: sm2 pd is NULL\n"); + return UADK_P_FAIL; + } + + /* order is free in freectx */ + if (smctx->sm2_pd->order == NULL) { + smctx->sm2_pd->order = BN_bin2bn((void *)sm2_order, sizeof(sm2_order), NULL); + if (smctx->sm2_pd->order == NULL) { + fprintf(stderr, "failed to BN_bin2bn order\n"); + return UADK_P_FAIL; + } + } + setup.rand.usr = (void *)smctx->sm2_pd->order; + if (smctx->sm2_md->md) { /* Set hash method */ setup.hash.cb = uadk_prov_compute_hash; @@ -967,14 +982,15 @@ static int uadk_prov_sm2_update_sess(SM2_PROV_CTX *smctx) } setup.hash.type = type; } + setup.alg = "sm2"; - order = BN_bin2bn((void *)sm2_order, sizeof(sm2_order), NULL); + /* Use the default numa parameters */ + params.numa_id = -1; + setup.sched_param = ¶ms; setup.rand.cb = uadk_prov_ecc_get_rand; - setup.rand.usr = (void *)order; sess = wd_ecc_alloc_sess(&setup); if (sess == (handle_t)0) { fprintf(stderr, "failed to alloc sess\n"); - BN_free(order); smctx->init_status = CTX_INIT_FAIL; return UADK_P_FAIL; } @@ -987,10 +1003,6 @@ static int uadk_prov_sm2_update_sess(SM2_PROV_CTX *smctx) smctx->sm2_pd->prikey = NULL; smctx->sm2_pd->pubkey = NULL; - if (smctx->sm2_pd->order) - BN_free(smctx->sm2_pd->order); - smctx->sm2_pd->order = order; - return UADK_P_SUCCESS; } @@ -1060,12 +1072,6 @@ static int uadk_signature_sm2_sign_init(void *vpsm2ctx, void *ec, psm2ctx->sm2_pctx->init_status = CTX_INIT_SUCC; - ret = uadk_prov_sm2_update_sess(psm2ctx->sm2_pctx); - if (ret == UADK_P_FAIL) { - fprintf(stderr, "failed to update sess in sign init\n"); - goto do_soft; - } - return UADK_P_SUCCESS; do_soft: @@ -1088,11 +1094,6 @@ static int uadk_prov_sm2_check_tbs_params(PROV_SM2_SIGN_CTX *psm2ctx, return UADK_P_FAIL; } - if (smctx->sess == (handle_t)0) { - fprintf(stderr, "invalid: smctx->sess is NULL\n"); - return UADK_P_FAIL; - } - if (smctx->init_status != CTX_INIT_SUCC) { fprintf(stderr, "sm2 ctx init did not init\n"); return UADK_P_FAIL; @@ -1303,6 +1304,15 @@ static int uadk_prov_sm2_sign(PROV_SM2_SIGN_CTX *psm2ctx, struct wd_dtb *s = NULL; int ret; + /* sess is free in uadk_signature_sm2_freectx() */ + if (smctx->sess == (handle_t)0) { + ret = uadk_prov_sm2_update_sess(smctx); + if (ret == UADK_P_FAIL) { + fprintf(stderr, "failed to alloc sess in sign\n"); + return ret; + } + } + ret = uadk_prov_sm2_sign_init_iot(smctx->sess, &req, (void *)tbs, tbslen); if (ret == UADK_P_FAIL) return ret; @@ -1475,6 +1485,15 @@ static int uadk_prov_sm2_verify(PROV_SM2_SIGN_CTX *psm2ctx, struct wd_dtb s = {0}; int ret; + /* sess is free in uadk_signature_sm2_freectx() */ + if (smctx->sess == (handle_t)0) { + ret = uadk_prov_sm2_update_sess(smctx); + if (ret == UADK_P_FAIL) { + fprintf(stderr, "failed to alloc sess in verify\n"); + return ret; + } + } + r.data = (void *)buf_r; s.data = (void *)buf_s; r.bsize = UADK_ECC_MAX_KEY_BYTES; @@ -2177,21 +2196,10 @@ static int uadk_prov_sm2_locate_id_digest(PROV_SM2_SIGN_CTX *psm2ctx, const OSS return UADK_P_SUCCESS; } -static int uadk_signature_sm2_set_ctx_params_sw(void *vpsm2ctx, const OSSL_PARAM params[]) -{ - if (uadk_get_sw_offload_state() && get_default_sm2_signature().set_ctx_params) { - fprintf(stderr, "switch to software sm2 set_ctx_params\n"); - return get_default_sm2_signature().set_ctx_params(vpsm2ctx, params); - } - - return UADK_P_FAIL; -} - static int uadk_signature_sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[]) { PROV_SM2_SIGN_CTX *psm2ctx = (PROV_SM2_SIGN_CTX *)vpsm2ctx; SM2_PROV_CTX *smctx; - int ret; /* * 'set_ctx_param' function can be called independently, @@ -2212,23 +2220,7 @@ static int uadk_signature_sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM pa return UADK_P_FAIL; } - ret = uadk_prov_sm2_locate_id_digest(psm2ctx, params); - if (ret == UADK_P_FAIL) - return ret; - - /* If not init, do not need to update session, just set the data before */ - if (smctx->init_status == CTX_INIT_SUCC) { - ret = uadk_prov_sm2_update_sess(smctx); - if (ret == UADK_P_FAIL) { - fprintf(stderr, "failed to update sess in set_ctx\n"); - goto do_soft; - } - } - - return UADK_P_SUCCESS; - -do_soft: - return uadk_signature_sm2_set_ctx_params_sw(vpsm2ctx, params); + return uadk_prov_sm2_locate_id_digest(psm2ctx, params); } static int uadk_signature_sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params) @@ -2457,10 +2449,13 @@ static void uadk_asym_cipher_sm2_freectx(void *vpsm2ctx) EVP_MD_free(smctx->sm2_md->md); OPENSSL_free(smctx->sm2_md); } + if (smctx->sm2_pd) { - BN_free(smctx->sm2_pd->order); + if (smctx->sm2_pd->order) + BN_free(smctx->sm2_pd->order); OPENSSL_free(smctx->sm2_pd); } + if (smctx->sess) wd_ecc_free_sess(smctx->sess); OPENSSL_free(smctx); -- 2.43.0

From: Zhushuai Yin <yinzhushuai@huawei.com> When the high-order bit of the returned shared secret key is 0, the high-order bit of the DH shared secret key return value is not padded with 0, and the returned size is also the value without the high-order bit, which does not match the expected result of the software calculation. Signed-off-by: Zhushuai Yin <yinzhushuai@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_dh.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/uadk_dh.c b/src/uadk_dh.c index 8fa6b4b..fcdc5cd 100644 --- a/src/uadk_dh.c +++ b/src/uadk_dh.c @@ -887,6 +887,7 @@ static int uadk_e_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, const BIGNUM *p = NULL; const BIGNUM *g = NULL; const BIGNUM *q = NULL; + __u16 len; int ret; if (!dh || !key || !pub_key || !DH_get0_priv_key(dh)) @@ -918,8 +919,11 @@ static int uadk_e_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, goto free_data; } - memcpy(key, dh_sess->req.pri, dh_sess->req.pri_bytes); - ret = dh_sess->req.pri_bytes; + len = dh_sess->req.pri_bytes < dh_sess->key_size ? + dh_sess->req.pri_bytes : dh_sess->key_size; + memset(key, 0, dh_sess->key_size - len); + memcpy(key + dh_sess->key_size - len, dh_sess->req.pri, len); + ret = dh_sess->key_size; dh_free_eng_session(dh_sess); return ret; -- 2.43.0

From: lizhi <lizhi206@huawei.com> Replace magic number 3 with HASH_FIRST_BYTES macro. Signed-off-by: lizhi <lizhi206@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_ffc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/uadk_prov_ffc.c b/src/uadk_prov_ffc.c index 7719039..88059e4 100644 --- a/src/uadk_prov_ffc.c +++ b/src/uadk_prov_ffc.c @@ -21,6 +21,7 @@ #define BN_DEF(lo, hi) lo, hi #endif +#define HASH_FIRST_BYTES 3 /* DH parameters from RFC3526 */ # ifndef FIPS_MODULE @@ -1510,7 +1511,7 @@ static int generate_canonical_g(BN_CTX *ctx, BN_MONT_CTX *mont, !EVP_DigestUpdate(mctx, seed, seedlen) || !EVP_DigestUpdate(mctx, ggen, sizeof(ggen)) || /* Hash the first three bytes of md, corresponds to 'index || counter' */ - !EVP_DigestUpdate(mctx, md, 3) || + !EVP_DigestUpdate(mctx, md, HASH_FIRST_BYTES) || !EVP_DigestFinal_ex(mctx, md, NULL) || (BN_bin2bn(md, mdsize, w) == NULL) || !BN_mod_exp_mont(g, w, e, p, ctx, mont)) -- 2.43.0

From: Zhushuai Yin <yinzhushuai@huawei.com> In the software calculation of SM2, if there is no ID (noid), a default ID will be used. However, in the hardware calculation, the default ID is not used, and an empty value is directly passed, leading to a mismatch between the signature and authentication data, resulting in failure. It is necessary to modify the driver to use the default ID in the case of no ID (noid). Signed-off-by: Zhushuai Yin <yinzhushuai@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_sm2.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/uadk_prov_sm2.c b/src/uadk_prov_sm2.c index 5499250..9a71ed2 100644 --- a/src/uadk_prov_sm2.c +++ b/src/uadk_prov_sm2.c @@ -32,6 +32,8 @@ #define SM2_KEY_BYTES 32 #define SM2_GET_SIGNLEN 1 #define SM3_DIGEST_LENGTH 32 +#define SM2_DEFAULT_USERID "1234567812345678" +#define SM2_DEFAULT_USERID_LEN 16 static pthread_mutex_t sign_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t asym_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -1855,6 +1857,17 @@ static int sm2_sig_compute_z_digest(PROV_SM2_SIGN_CTX *psm2ctx) return UADK_P_FAIL; } + /* if id is not set, use default id */ + if (psm2ctx->id == NULL) { + /* psm2ctx id will be freed in uadk_signature_sm2_freectx, not here */ + psm2ctx->id = OPENSSL_memdup(SM2_DEFAULT_USERID, SM2_DEFAULT_USERID_LEN); + if (psm2ctx->id == NULL) { + fprintf(stderr, "failed to memdup psm2ctx id\n"); + goto free_z; + } + psm2ctx->id_len = SM2_DEFAULT_USERID_LEN; + } + /* get hashed prefix 'z' of tbs message */ ret = uadk_prov_sm2_compute_z_digest(z, smctx->sm2_md->md, psm2ctx->id, psm2ctx->id_len, psm2ctx->key); -- 2.43.0

From: Chenghai Huang <huangchenghai2@huawei.com> Add reset for part of ctx after final in hmac and digest, because user may call update again without alloc a new ctx. Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_prov_digest.c | 27 ++++++++++++++++++++------- src/uadk_prov_hmac.c | 16 +++++++++++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/uadk_prov_digest.c b/src/uadk_prov_digest.c index 39e589a..457aa9a 100644 --- a/src/uadk_prov_digest.c +++ b/src/uadk_prov_digest.c @@ -277,6 +277,13 @@ out: return ret; } +static void uadk_digest_reset(struct digest_priv_ctx *priv) +{ + priv->state = SEC_DIGEST_INIT; + priv->last_update_bufflen = 0; + priv->total_data_len = 0; +} + static int uadk_digest_poll(void *ctx) { __u64 rx_cnt = 0; @@ -685,7 +692,7 @@ sync_err: if (priv->state == SEC_DIGEST_INIT) { ret = uadk_digest_soft_work(priv, priv->req.in_bytes, digest); } else { - ret = 0; + ret = UADK_DIGEST_FAIL; fprintf(stderr, "do sec digest final failed.\n"); } clear: @@ -914,7 +921,7 @@ static int uadk_prov_final(void *dctx, unsigned char *out, size_t *outl, size_t outsz) { struct digest_priv_ctx *priv = (struct digest_priv_ctx *)dctx; - int ret; + int ret = UADK_DIGEST_SUCCESS; if (!dctx || !out) { fprintf(stderr, "CTX or output data is NULL.\n"); @@ -924,20 +931,23 @@ static int uadk_prov_final(void *dctx, unsigned char *out, if (outsz > 0) { ret = uadk_digest_final(priv, out); if (!ret) - return ret; + goto reset_ctx; } if (outl) *outl = priv->md_size; - return UADK_DIGEST_SUCCESS; +reset_ctx: + uadk_digest_reset(priv); + + return ret; } static int uadk_prov_digest(void *dctx, const unsigned char *in, size_t inl, unsigned char *out, size_t *outl, size_t outsz) { struct digest_priv_ctx *priv = (struct digest_priv_ctx *)dctx; - int ret; + int ret = UADK_DIGEST_SUCCESS; if (!dctx || !in || !out) { fprintf(stderr, "CTX or input or output data is NULL.\n"); @@ -953,13 +963,16 @@ static int uadk_prov_digest(void *dctx, const unsigned char *in, size_t inl, if (outsz > 0) { ret = uadk_digest_digest(priv, in, inl, out); if (!ret) - return ret; + goto reset_ctx; } if (unlikely(outl != NULL)) *outl = priv->md_size; - return UADK_DIGEST_SUCCESS; +reset_ctx: + uadk_digest_reset(priv); + + return ret; } void uadk_prov_destroy_digest(void) diff --git a/src/uadk_prov_hmac.c b/src/uadk_prov_hmac.c index 7ee663a..0558bc2 100644 --- a/src/uadk_prov_hmac.c +++ b/src/uadk_prov_hmac.c @@ -288,6 +288,13 @@ static OSSL_FUNC_mac_settable_ctx_params_fn uadk_prov_hmac_settable_ctx_params; static OSSL_FUNC_mac_set_ctx_params_fn uadk_prov_hmac_set_ctx_params; +static void uadk_hmac_reset(struct hmac_priv_ctx *priv) +{ + priv->state = SEC_DIGEST_INIT; + priv->last_update_bufflen = 0; + priv->total_data_len = 0; +} + static int uadk_hmac_poll(void *ctx) { __u64 rx_cnt = 0; @@ -956,7 +963,7 @@ static int uadk_prov_hmac_final(void *hctx, unsigned char *out, size_t *outl, size_t outsize) { struct hmac_priv_ctx *priv = (struct hmac_priv_ctx *)hctx; - int ret; + int ret = UADK_HMAC_SUCCESS; if (!hctx) { fprintf(stderr, "hmac CTX or output data is NULL.\n"); @@ -966,13 +973,16 @@ static int uadk_prov_hmac_final(void *hctx, unsigned char *out, size_t *outl, if (out && outsize > 0) { ret = uadk_hmac_final(priv, out); if (!ret) - return ret; + goto reset_ctx; } if (outl) *outl = priv->out_len; - return UADK_HMAC_SUCCESS; +reset_ctx: + uadk_hmac_reset(priv); + + return ret; } void uadk_prov_destroy_hmac(void) -- 2.43.0

From: Weili Qian <qianweili@huawei.com> In small packet scenarios, the hash algorithm will switch to software computation. In asynchronous situations, if the eventfd is getted before switching to software computation, it will lead to performance degradation. Therefore, for small packets, directly switch to software computation. Signed-off-by: Weili Qian <qianweili@huawei.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_async.c | 5 +++++ src/uadk_async.h | 1 + src/uadk_digest.c | 49 ++++++++++++++++++++++------------------------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/uadk_async.c b/src/uadk_async.c index 8adc964..ead9893 100644 --- a/src/uadk_async.c +++ b/src/uadk_async.c @@ -47,6 +47,11 @@ static void async_fd_cleanup(ASYNC_WAIT_CTX *ctx, const void *key, close(readfd); } +ASYNC_JOB *async_get_async_job(void) +{ + return ASYNC_get_current_job(); +} + int async_setup_async_event_notification(struct async_op *op) { ASYNC_WAIT_CTX *waitctx; diff --git a/src/uadk_async.h b/src/uadk_async.h index 23f017e..4aebf05 100644 --- a/src/uadk_async.h +++ b/src/uadk_async.h @@ -88,4 +88,5 @@ int async_wake_job(ASYNC_JOB *job); void async_free_poll_task(int id, bool is_cb); int async_get_free_task(int *id); void async_poll_task_free(void); +ASYNC_JOB *async_get_async_job(void); #endif diff --git a/src/uadk_digest.c b/src/uadk_digest.c index 60691a6..92068b7 100644 --- a/src/uadk_digest.c +++ b/src/uadk_digest.c @@ -815,10 +815,6 @@ static int do_digest_sync(struct digest_priv_ctx *priv) { int ret; - if (priv->req.in_bytes <= priv->switch_threshold && - priv->state == SEC_DIGEST_INIT) - return 0; - ret = wd_do_digest_sync(priv->sess, &priv->req); if (ret) { fprintf(stderr, "do sec digest sync failed, switch to soft digest.\n"); @@ -835,18 +831,6 @@ static int do_digest_async(struct digest_priv_ctx *priv, struct async_op *op) int cnt = 0; int idx; - if (unlikely(priv->switch_flag == UADK_DO_SOFT)) { - fprintf(stderr, "async cipher init failed.\n"); - return ret; - } - - if (priv->req.in_bytes <= priv->switch_threshold && - priv->state == SEC_DIGEST_INIT) { - /* hw v2 does not support in_bytes=0 refer digest_bd2_type_check - * so switch to sw - */ - return 0; - } cb_param = malloc(sizeof(struct uadk_e_cb_info)); if (!cb_param) { fprintf(stderr, "failed to alloc cb_param.\n"); @@ -892,7 +876,7 @@ static int uadk_e_digest_final(EVP_MD_CTX *ctx, unsigned char *digest) { struct digest_priv_ctx *priv = (struct digest_priv_ctx *)EVP_MD_CTX_md_data(ctx); - struct async_op *op; + struct async_op *op = NULL; int ret = 1; if (unlikely(!priv)) { @@ -917,6 +901,24 @@ static int uadk_e_digest_final(EVP_MD_CTX *ctx, unsigned char *digest) if (priv->e_nid == NID_sha384) priv->req.out_bytes = WD_DIGEST_SHA384_LEN; + if (unlikely(priv->switch_flag == UADK_DO_SOFT)) { + if (async_get_async_job()) + goto hw_err; + + /* Synchronous, only the synchronous mode supports soft computing */ + ret = digest_soft_final(priv, digest); + digest_soft_cleanup(priv); + return ret; + } + + if (priv->req.in_bytes <= priv->switch_threshold && + priv->state == SEC_DIGEST_INIT) + /* + * hw v2 does not support in_bytes=0 refer digest_bd2_type_check + * so switch to sw. + */ + return uadk_e_digest_soft_work(priv, priv->req.in_bytes, digest); + op = malloc(sizeof(struct async_op)); if (!op) return 0; @@ -929,13 +931,6 @@ static int uadk_e_digest_final(EVP_MD_CTX *ctx, unsigned char *digest) } if (!op->job) { - /* Synchronous, only the synchronous mode supports soft computing */ - if (unlikely(priv->switch_flag == UADK_DO_SOFT)) { - ret = digest_soft_final(priv, digest); - digest_soft_cleanup(priv); - goto clear; - } - ret = do_digest_sync(priv); if (!ret) goto hw_err; @@ -957,8 +952,10 @@ hw_err: fprintf(stderr, "do sec digest stream mode failed.\n"); } clear: - (void)async_clear_async_event_notification(); - free(op); + if (op) { + (void)async_clear_async_event_notification(); + free(op); + } return ret; } -- 2.43.0

From: Wenkai Lin <linwenkai6@hisilicon.com> When the nid is not changed, the original algorithm information can be used, which reduces repeated calls. Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_cipher.c | 98 ++++++++++++++++++++--------------------------- src/uadk_digest.c | 75 ++++++++++++++++++------------------ 2 files changed, 79 insertions(+), 94 deletions(-) diff --git a/src/uadk_cipher.c b/src/uadk_cipher.c index b55f6ae..f7facc9 100644 --- a/src/uadk_cipher.c +++ b/src/uadk_cipher.c @@ -65,6 +65,8 @@ struct cipher_priv_ctx { size_t switch_threshold; bool update_iv; struct sched_params sched_param; + int nid; + const EVP_CIPHER *sw_cipher; }; struct cipher_info { @@ -171,8 +173,7 @@ static int uadk_e_cipher_sw_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, { /* Real implementation: Openssl soft arithmetic key initialization function */ struct cipher_priv_ctx *priv; - const EVP_CIPHER *sw_cipher; - int ret, nid, sw_size; + int ret, sw_size; if (unlikely(key == NULL)) { fprintf(stderr, "uadk engine init parameter key is NULL.\n"); @@ -185,14 +186,7 @@ static int uadk_e_cipher_sw_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, return 0; } - nid = EVP_CIPHER_CTX_nid(ctx); - sw_cipher = sec_ciphers_get_cipher_sw_impl(nid); - if (unlikely(sw_cipher == NULL)) { - fprintf(stderr, "get openssl software cipher failed, nid = %d.\n", nid); - return 0; - } - - sw_size = EVP_CIPHER_impl_ctx_size(sw_cipher); + sw_size = EVP_CIPHER_impl_ctx_size(priv->sw_cipher); if (unlikely(sw_size == 0)) { fprintf(stderr, "get openssl software cipher ctx size failed.\n"); return 0; @@ -209,7 +203,7 @@ static int uadk_e_cipher_sw_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, iv = EVP_CIPHER_CTX_iv_noconst(ctx); EVP_CIPHER_CTX_set_cipher_data(ctx, priv->sw_ctx_data); - ret = EVP_CIPHER_meth_get_init(sw_cipher)(ctx, key, iv, enc); + ret = EVP_CIPHER_meth_get_init(priv->sw_cipher)(ctx, key, iv, enc); EVP_CIPHER_CTX_set_cipher_data(ctx, priv); if (unlikely(ret != 1)) { fprintf(stderr, "failed init openssl soft work key.\n"); @@ -225,9 +219,8 @@ static int uadk_e_cipher_soft_work(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, size_t inl) { struct cipher_priv_ctx *priv; - const EVP_CIPHER *sw_cipher; unsigned char *iv; - int ret, nid; + int ret; priv = (struct cipher_priv_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); if (unlikely(priv == NULL)) { @@ -245,15 +238,9 @@ static int uadk_e_cipher_soft_work(EVP_CIPHER_CTX *ctx, unsigned char *out, memcpy(iv, priv->iv, EVP_CIPHER_CTX_iv_length(ctx)); priv->update_iv = true; } - sw_cipher = sec_ciphers_get_cipher_sw_impl(EVP_CIPHER_CTX_nid(ctx)); - if (unlikely(sw_cipher == NULL)) { - nid = EVP_CIPHER_CTX_nid(ctx); - fprintf(stderr, "get openssl software cipher failed, nid = %d.\n", nid); - return 0; - } EVP_CIPHER_CTX_set_cipher_data(ctx, priv->sw_ctx_data); - ret = EVP_CIPHER_meth_get_do_cipher(sw_cipher)(ctx, out, in, inl); + ret = EVP_CIPHER_meth_get_do_cipher(priv->sw_cipher)(ctx, out, in, inl); if (unlikely(ret != 1)) { fprintf(stderr, "OpenSSL do cipher failed.\n"); return 0; @@ -475,11 +462,21 @@ err_unlock: return 0; } -static void cipher_priv_ctx_setup(struct cipher_priv_ctx *priv, - enum wd_cipher_alg alg, enum wd_cipher_mode mode) +static bool is_cipher_nid_found(struct cipher_priv_ctx *priv, int nid) { - priv->setup.alg = alg; - priv->setup.mode = mode; + __u32 cipher_counts = ARRAY_SIZE(cipher_info_table); + __u32 i; + + for (i = 0; i < cipher_counts; i++) { + if (nid == cipher_info_table[i].nid) { + priv->setup.alg = cipher_info_table[i].alg; + priv->setup.mode = cipher_info_table[i].mode; + return true; + } + } + + fprintf(stderr, "failed to find the cipher nid!\n"); + return false; } static int uadk_e_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, @@ -487,9 +484,7 @@ static int uadk_e_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, { struct cipher_priv_ctx *priv = (struct cipher_priv_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); - __u32 cipher_counts = ARRAY_SIZE(cipher_info_table); int nid, ret; - __u32 i; if (unlikely(!priv)) { fprintf(stderr, "priv get from cipher ctx is NULL.\n"); @@ -501,7 +496,6 @@ static int uadk_e_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, return 0; } - nid = EVP_CIPHER_CTX_nid(ctx); priv->req.op_type = enc ? WD_CIPHER_ENCRYPTION : WD_CIPHER_DECRYPTION; if (iv) @@ -509,17 +503,18 @@ static int uadk_e_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, else memcpy(priv->iv, EVP_CIPHER_CTX_iv_noconst(ctx), EVP_CIPHER_CTX_iv_length(ctx)); - for (i = 0; i < cipher_counts; i++) { - if (nid == cipher_info_table[i].nid) { - cipher_priv_ctx_setup(priv, cipher_info_table[i].alg, - cipher_info_table[i].mode); - break; - } - } + nid = EVP_CIPHER_CTX_nid(ctx); + if (nid != priv->nid) { + ret = is_cipher_nid_found(priv, nid); + if (!ret) + return 0; - if (i == cipher_counts) { - fprintf(stderr, "failed to setup the private ctx.\n"); - return 0; + priv->sw_cipher = sec_ciphers_get_cipher_sw_impl(nid); + if (unlikely(priv->sw_cipher == NULL)) { + fprintf(stderr, "get openssl software cipher failed, nid = %d.\n", nid); + return 0; + } + priv->nid = nid; } ret = uadk_e_cipher_sw_init(ctx, key, iv, enc); @@ -544,6 +539,9 @@ static int uadk_e_cipher_cleanup(EVP_CIPHER_CTX *ctx) priv->sess = 0; } + priv->nid = NID_undef; + priv->sw_cipher = NULL; + return 1; } @@ -644,10 +642,8 @@ static int uadk_e_cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int numa_node, void static void uadk_e_ctx_init(EVP_CIPHER_CTX *ctx, struct cipher_priv_ctx *priv) { - __u32 cipher_counts = ARRAY_SIZE(cipher_info_table); struct sched_params *para; - int nid, ret, type; - __u32 i; + int nid, ret, type = 0; priv->req.iv_bytes = EVP_CIPHER_CTX_iv_length(ctx); priv->req.iv = priv->iv; @@ -667,10 +663,8 @@ static void uadk_e_ctx_init(EVP_CIPHER_CTX *ctx, struct cipher_priv_ctx *priv) * the cipher algorithm does not distinguish between * encryption and decryption queues */ - type = priv->req.op_type; - ret = uadk_e_is_env_enabled("cipher"); - if (ret) - type = 0; + if (priv->req.op_type) + type = uadk_e_is_env_enabled("cipher") ? 0 : priv->req.op_type; /* Use the default numa parameters */ if (priv->setup.sched_param != &priv->sched_param) @@ -681,18 +675,10 @@ static void uadk_e_ctx_init(EVP_CIPHER_CTX *ctx, struct cipher_priv_ctx *priv) if (!priv->sess) { nid = EVP_CIPHER_CTX_nid(ctx); - - for (i = 0; i < cipher_counts; i++) { - if (nid == cipher_info_table[i].nid) { - cipher_priv_ctx_setup(priv, cipher_info_table[i].alg, - cipher_info_table[i].mode); - break; - } - } - - if (i == cipher_counts) { - fprintf(stderr, "failed to setup the private ctx.\n"); - return; + if (nid != priv->nid) { + ret = is_cipher_nid_found(priv, nid); + if (!ret) + return; } priv->sess = wd_cipher_alloc_sess(&priv->setup); diff --git a/src/uadk_digest.c b/src/uadk_digest.c index 92068b7..408eac3 100644 --- a/src/uadk_digest.c +++ b/src/uadk_digest.c @@ -140,6 +140,7 @@ struct digest_priv_ctx { bool is_stream_copy; size_t total_data_len; struct sched_params sched_param; + __u32 out_bytes; }; struct digest_info { @@ -544,7 +545,7 @@ static void digest_priv_ctx_setup(struct digest_priv_ctx *priv, priv->setup.alg = alg; priv->setup.mode = mode; priv->req.out_buf_bytes = MAX_DIGEST_LENGTH; - priv->req.out_bytes = out_len; + priv->out_bytes = out_len; } static void digest_priv_ctx_reset(struct digest_priv_ctx *priv) @@ -574,23 +575,46 @@ static int uadk_e_digest_ctrl(EVP_MD_CTX *ctx, int cmd, int numa_node, void *p2) return 1; } +static bool is_digest_nid_found(struct digest_priv_ctx *priv, int nid) + +{ + __u32 counts = ARRAY_SIZE(digest_info_table); + __u32 i; + + for (i = 0; i < counts; i++) { + if (nid == digest_info_table[i].nid) { + digest_priv_ctx_setup(priv, digest_info_table[i].alg, + digest_info_table[i].mode, + digest_info_table[i].out_len); + return true; + } + } + + fprintf(stderr, "failed to find the digest nid!\n"); + return false; +} + static int uadk_e_digest_init(EVP_MD_CTX *ctx) { struct digest_priv_ctx *priv = (struct digest_priv_ctx *) EVP_MD_CTX_md_data(ctx); - __u32 digest_counts = ARRAY_SIZE(digest_info_table); - __u32 i; - int ret; + int ret, nid; if (unlikely(!priv)) { fprintf(stderr, "priv get from digest ctx is NULL.\n"); return 0; } - priv->e_nid = EVP_MD_nid(EVP_MD_CTX_md(ctx)); - digest_priv_ctx_reset(priv); + nid = EVP_MD_nid(EVP_MD_CTX_md(ctx)); + if (nid != priv->e_nid) { + ret = is_digest_nid_found(priv, nid); + if (!ret) + return 0; + priv->e_nid = nid; + } + ret = uadk_e_init_digest(); if (unlikely(!ret)) { priv->switch_flag = UADK_DO_SOFT; @@ -598,19 +622,6 @@ static int uadk_e_digest_init(EVP_MD_CTX *ctx) return digest_soft_init(priv); } - for (i = 0; i < digest_counts; i++) { - if (priv->e_nid == digest_info_table[i].nid) { - digest_priv_ctx_setup(priv, digest_info_table[i].alg, - digest_info_table[i].mode, digest_info_table[i].out_len); - break; - } - } - - if (unlikely(i == digest_counts)) { - fprintf(stderr, "failed to setup the private ctx.\n"); - return 0; - } - /* Use the default numa parameters */ if (priv->setup.sched_param != &priv->sched_param) uadk_e_digest_ctrl(ctx, 0, -1, NULL); @@ -635,22 +646,15 @@ out: return 0; } -static void digest_update_out_length(EVP_MD_CTX *ctx) +static void digest_update_out_length(struct digest_priv_ctx *priv) { - struct digest_priv_ctx *priv = - (struct digest_priv_ctx *)EVP_MD_CTX_md_data(ctx); - - if (unlikely(!priv)) { - fprintf(stderr, "priv get from digest ctx is NULL.\n"); - return; - } - /* Sha224 and Sha384 need full length mac buffer as doing long hash */ if (priv->e_nid == NID_sha224) priv->req.out_bytes = WD_DIGEST_SHA224_FULL_LEN; - - if (priv->e_nid == NID_sha384) + else if (priv->e_nid == NID_sha384) priv->req.out_bytes = WD_DIGEST_SHA384_FULL_LEN; + else + priv->req.out_bytes = priv->out_bytes; } static void digest_set_msg_state(struct digest_priv_ctx *priv, bool is_end) @@ -676,7 +680,7 @@ static int digest_update_inner(EVP_MD_CTX *ctx, const void *data, size_t data_le return 0; } - digest_update_out_length(ctx); + digest_update_out_length(priv); digest_set_msg_state(priv, false); do { @@ -893,13 +897,7 @@ static int uadk_e_digest_final(EVP_MD_CTX *ctx, unsigned char *digest) priv->req.in = priv->data; priv->req.out = priv->out; priv->req.in_bytes = priv->last_update_bufflen; - priv->e_nid = EVP_MD_nid(EVP_MD_CTX_md(ctx)); - - if (priv->e_nid == NID_sha224) - priv->req.out_bytes = WD_DIGEST_SHA224_LEN; - - if (priv->e_nid == NID_sha384) - priv->req.out_bytes = WD_DIGEST_SHA384_LEN; + priv->req.out_bytes = priv->out_bytes; if (unlikely(priv->switch_flag == UADK_DO_SOFT)) { if (async_get_async_job()) @@ -978,6 +976,7 @@ static int uadk_e_digest_cleanup(EVP_MD_CTX *ctx) } digest_soft_cleanup(priv); + priv->e_nid = NID_undef; return 1; } -- 2.43.0

From: Wenkai Lin <linwenkai6@hisilicon.com> The AEAD algorithm needs to dynamically set the cipher key, so separate it from the function that applies for resources. Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com> Signed-off-by: JiangShui Yang <yangjiangshui@h-partners.com> --- src/uadk_aead.c | 65 +++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/src/uadk_aead.c b/src/uadk_aead.c index 0cb44c0..ef511c2 100644 --- a/src/uadk_aead.c +++ b/src/uadk_aead.c @@ -272,8 +272,10 @@ static int uadk_e_init_aead_cipher(void) } static int uadk_e_ctx_init(struct aead_priv_ctx *priv, const unsigned char *ckey, - int ckey_len, struct wd_aead_sess_setup *setup) + int ckey_len) { + struct wd_aead_sess_setup setup = {0}; + struct sched_params params = {0}; int ret; ret = uadk_e_init_aead_cipher(); @@ -281,7 +283,15 @@ static int uadk_e_ctx_init(struct aead_priv_ctx *priv, const unsigned char *ckey return UADK_E_FAIL; if (!priv->sess) { - priv->sess = wd_aead_alloc_sess(setup); + params.type = priv->req.op_type; + if (params.type && uadk_e_is_env_enabled("aead")) + params.type = 0; + params.numa_id = g_aead_engine.numa_id; + setup.sched_param = ¶ms; + setup.calg = WD_CIPHER_AES; + setup.cmode = WD_CIPHER_GCM; + + priv->sess = wd_aead_alloc_sess(&setup); if (!priv->sess) { fprintf(stderr, "uadk engine failed to alloc aead session!\n"); return UADK_E_FAIL; @@ -292,12 +302,6 @@ static int uadk_e_ctx_init(struct aead_priv_ctx *priv, const unsigned char *ckey goto out; } - ret = wd_aead_set_ckey(priv->sess, ckey, ckey_len); - if (ret) { - fprintf(stderr, "uadk engine failed to set ckey!\n"); - goto out; - } - /* Memory needs to be reserved for both input and output. */ priv->data = malloc(AEAD_BLOCK_SIZE << 1); if (unlikely(!priv->data)) { @@ -306,7 +310,17 @@ static int uadk_e_ctx_init(struct aead_priv_ctx *priv, const unsigned char *ckey } } + ret = wd_aead_set_ckey(priv->sess, ckey, ckey_len); + if (ret) { + fprintf(stderr, "uadk engine failed to set ckey!\n"); + goto free_data; + } + return UADK_E_SUCCESS; + +free_data: + if (priv->data) + free(priv->data); out: wd_aead_free_sess(priv->sess); priv->sess = 0; @@ -316,8 +330,6 @@ out: static int uadk_e_aes_gcm_init(EVP_CIPHER_CTX *ctx, const unsigned char *ckey, const unsigned char *iv, int enc) { - struct wd_aead_sess_setup setup; - struct sched_params params = {0}; struct aead_priv_ctx *priv; int ret, ckey_len; @@ -327,49 +339,32 @@ static int uadk_e_aes_gcm_init(EVP_CIPHER_CTX *ctx, const unsigned char *ckey, return UADK_E_FAIL; } - if (unlikely(!ckey)) - return UADK_E_SUCCESS; - - if (iv) + if (iv) { memcpy(priv->iv, iv, AES_GCM_IV_LEN); - - setup.calg = WD_CIPHER_AES; - setup.cmode = WD_CIPHER_GCM; - setup.dalg = 0; - setup.dmode = 0; + priv->req.iv = priv->iv; + priv->req.iv_bytes = AES_GCM_IV_LEN; + memset(priv->iv + AES_GCM_IV_LEN, 0, AES_GCM_CTR_LEN); + } priv->req.assoc_bytes = 0; priv->req.out_bytes = 0; priv->req.data_fmt = WD_FLAT_BUF; - - priv->req.iv = priv->iv; - priv->req.iv_bytes = AES_GCM_IV_LEN; - memset(priv->iv + AES_GCM_IV_LEN, 0, AES_GCM_CTR_LEN); - priv->req.mac = priv->mac; priv->req.mac_bytes = AES_GCM_TAG_LEN; - priv->taglen = 0; priv->is_req_tag_set = false; priv->mode = UNINIT_STREAM; - priv->data = NULL; if (enc) priv->req.op_type = WD_CIPHER_ENCRYPTION_DIGEST; else priv->req.op_type = WD_CIPHER_DECRYPTION_DIGEST; - params.type = priv->req.op_type; - ret = uadk_e_is_env_enabled("aead"); - if (ret) - params.type = 0; - params.numa_id = g_aead_engine.numa_id; - setup.sched_param = ¶ms; + if (!ckey) + return UADK_E_SUCCESS; ckey_len = EVP_CIPHER_CTX_key_length(ctx); - ret = uadk_e_ctx_init(priv, ckey, ckey_len, &setup); - - return ret; + return uadk_e_ctx_init(priv, ckey, ckey_len); } static int uadk_e_aes_gcm_cleanup(EVP_CIPHER_CTX *ctx) -- 2.43.0
participants (1)
-
Qi Tao