uadk_engine/0070-sm2-fixup-switching-soft-sm2-decrypt-problem.patch
2023-11-29 16:35:32 +08:00

72 lines
2.1 KiB
Diff

From 9fd7d907e9c087c92a76da137c8e557edbb7f07f Mon Sep 17 00:00:00 2001
From: Weili Qian <qianweili@huawei.com>
Date: Sat, 25 Nov 2023 16:13:21 +0800
Subject: [PATCH 70/82] sm2: fixup switching soft sm2 decrypt problem
The openssl API d2i_SM2_Ciphertext() will change the 'in' address and
clean the data. If still use 'in' when switching to soft computing, input
data errors may occur. So pre-store the address to 'in_soft' and use it
in software computing.
Signed-off-by: Weili Qian <qianweili@huawei.com>
Signed-off-by: Zhiqi Song <songzhiqi1@huawei.com>
---
src/uadk_sm2.c | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/src/uadk_sm2.c b/src/uadk_sm2.c
index 84bda98..8421931 100644
--- a/src/uadk_sm2.c
+++ b/src/uadk_sm2.c
@@ -922,7 +922,7 @@ static int sm2_encrypt_check(EVP_PKEY_CTX *ctx,
c3_size = EVP_MD_size(md);
if (c3_size <= 0) {
fprintf(stderr, "c3 size error\n");
- return 0;
+ return UADK_E_INVALID;
}
if (!out) {
@@ -1033,7 +1033,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_E_INVALID;
}
if (!out) {
@@ -1107,6 +1107,7 @@ static int sm2_decrypt(EVP_PKEY_CTX *ctx,
{
struct sm2_ctx *smctx = EVP_PKEY_CTX_get_data(ctx);
struct sm2_ciphertext *ctext_struct;
+ const unsigned char *in_soft = in;
struct wd_ecc_req req = {0};
struct wd_ecc_point c1;
struct wd_dtb c2, c3;
@@ -1120,10 +1121,8 @@ static int sm2_decrypt(EVP_PKEY_CTX *ctx,
md = (smctx->ctx.md == NULL) ? EVP_sm3() : smctx->ctx.md;
ctext_struct = d2i_SM2_Ciphertext(NULL, &in, inlen);
- if (!ctext_struct) {
- ret = UADK_DO_SOFT;
- goto do_soft;
- }
+ if (!ctext_struct)
+ return 0;
ret = cipher_ber_to_bin(md, ctext_struct, &c1, &c2, &c3);
if (ret) {
@@ -1165,7 +1164,7 @@ do_soft:
return ret;
fprintf(stderr, "switch to execute openssl software calculation.\n");
- return openssl_decrypt(ctx, out, outlen, in, inlen);
+ return openssl_decrypt(ctx, out, outlen, in_soft, inlen);
}
static void sm2_cleanup(EVP_PKEY_CTX *ctx)
--
2.25.1