The openssl API d2i_SM2_Ciphertext() will clean the input param. When switching to soft computing, input data errors may occur. So pre-store the input data and free it after the hardware or software computing finished.
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- src/uadk_sm2.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/src/uadk_sm2.c b/src/uadk_sm2.c index f393641..aa56b5b 100644 --- a/src/uadk_sm2.c +++ b/src/uadk_sm2.c @@ -1026,7 +1026,7 @@ static int sm2_decrypt_check(EVP_PKEY_CTX *ctx, hash_size = EVP_MD_size(md); if (hash_size <= 0) { fprintf(stderr, "hash size = %d error\n", hash_size); - return 0; + return UADK_DO_SOFT; }
if (!out) { @@ -1102,6 +1102,7 @@ static int sm2_decrypt(EVP_PKEY_CTX *ctx, struct sm2_ciphertext *ctext_struct; struct wd_ecc_req req = {0}; struct wd_ecc_point c1; + unsigned char *in_soft; struct wd_dtb c2, c3; const EVP_MD *md; int ret; @@ -1112,10 +1113,18 @@ static int sm2_decrypt(EVP_PKEY_CTX *ctx,
md = (smctx->ctx.md == NULL) ? EVP_sm3() : smctx->ctx.md;
+ /* d2i_SM2_Ciphertext() will clean the in data, pre-save here */ + if (!inlen) + goto do_soft; + in_soft = malloc(inlen); + if (!in_soft) + goto do_soft; + memcpy(in_soft, in, inlen); + ctext_struct = d2i_SM2_Ciphertext(NULL, &in, inlen); if (!ctext_struct) { ret = UADK_DO_SOFT; - goto do_soft; + goto free_in_soft; }
ret = cipher_ber_to_bin(md, ctext_struct, &c1, &c2, &c3); @@ -1153,6 +1162,16 @@ free_c1: free(c1.x.data); free_ctext: SM2_Ciphertext_free(ctext_struct); +free_in_soft: + if (ret != UADK_DO_SOFT) { + free(in_soft); + return ret; + } + + fprintf(stderr, "switch to execute openssl software calculation.\n"); + ret = openssl_decrypt(ctx, out, outlen, in_soft, inlen); + free(in_soft); + return ret; do_soft: if (ret != UADK_DO_SOFT) return ret;