1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "securec.h"
18 #include "blob.h"
19 #include "cipher_sm2_ecdsa_signature_openssl.h"
20 #include "sm2_crypto_util.h"
21 #include "sm2_crypto_params.h"
22 #include "log.h"
23 #include "memory.h"
24 #include "cstring"
25
26 using namespace std;
27 using namespace testing::ext;
28
29 namespace {
30 class CryptoSm2EcdsaSignature : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp();
35 void TearDown();
36 };
37
SetUp()38 void CryptoSm2EcdsaSignature::SetUp() {}
TearDown()39 void CryptoSm2EcdsaSignature::TearDown() {}
SetUpTestCase()40 void CryptoSm2EcdsaSignature::SetUpTestCase() {}
TearDownTestCase()41 void CryptoSm2EcdsaSignature::TearDownTestCase() {}
42
43 static unsigned char g_rCoordinate[] = {
44 107, 93, 198, 247, 119, 18, 40, 110, 90, 156, 193, 158, 205, 113, 170, 128, 146, 109, 75, 17, 181, 109, 110,
45 91, 149, 5, 110, 233, 209, 78, 229, 96
46 };
47
48 static unsigned char g_sCoordinate[] = {
49 45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122, 248, 5, 232, 105, 41, 92, 95, 102, 224, 216, 149, 85, 236,
50 110, 6, 64, 188, 149, 70, 70, 183
51 };
52
ConstructCorrectSm2Spec(Sm2EcSignatureDataSpec ** spec)53 HcfResult ConstructCorrectSm2Spec(Sm2EcSignatureDataSpec **spec)
54 {
55 Sm2EcSignatureDataSpec *tempSpec =
56 static_cast<Sm2EcSignatureDataSpec *>(HcfMalloc(sizeof(Sm2EcSignatureDataSpec), 0));
57 if (tempSpec == nullptr) {
58 return HCF_ERR_MALLOC;
59 }
60 tempSpec->rCoordinate.data = g_rCoordinate;
61 tempSpec->rCoordinate.len = sizeof(g_sCoordinate);
62 tempSpec->sCoordinate.data = g_sCoordinate;
63 tempSpec->sCoordinate.len = sizeof(g_sCoordinate);
64 *spec = tempSpec;
65 return HCF_SUCCESS;
66 }
67
68 HWTEST_F(CryptoSm2EcdsaSignature, HcfSm2SpecToDerDataAndBack, TestSize.Level0)
69 {
70 Sm2EcSignatureDataSpec *spec = nullptr;
71 ASSERT_EQ(ConstructCorrectSm2Spec(&spec), HCF_SUCCESS);
72
73 // Convert SM2 spec to DER data
74 HcfBlob derOutput = { .data = nullptr, .len = 0 };
75 EXPECT_EQ(HcfSm2SpecToDerData(spec, &derOutput), HCF_SUCCESS);
76 EXPECT_NE(derOutput.data, nullptr);
77 EXPECT_GT(derOutput.len, 0);
78
79 // Convert DER data back to SM2 spec
80 Sm2EcSignatureDataSpec *returnSpec = nullptr;
81 EXPECT_EQ(HcfDerDataToSm2Spec(&derOutput, &returnSpec), HCF_SUCCESS);
82 EXPECT_NE(returnSpec, nullptr);
83
84 // Validate the converted spec matches the original
85 EXPECT_EQ(returnSpec->rCoordinate.len, spec->rCoordinate.len);
86 EXPECT_EQ(memcmp(returnSpec->rCoordinate.data, spec->rCoordinate.data, spec->rCoordinate.len), 0);
87 EXPECT_EQ(returnSpec->sCoordinate.len, spec->sCoordinate.len);
88 EXPECT_EQ(memcmp(returnSpec->sCoordinate.data, spec->sCoordinate.data, spec->sCoordinate.len), 0);
89
90 // Free allocated resources
91 HcfBlobDataFree(&derOutput);
92 DestroySm2EcSignatureSpec(returnSpec);
93 HcfFree(spec);
94 }
95
96 HWTEST_F(CryptoSm2EcdsaSignature, HcfSm2SpecToDerData_NullInput, TestSize.Level0)
97 {
98 HcfBlob derOutput = { .data = nullptr, .len = 0 };
99 EXPECT_EQ(HcfSm2SpecToDerData(nullptr, &derOutput), HCF_ERR_PARAMETER_CHECK_FAILED);
100 }
101
102 HWTEST_F(CryptoSm2EcdsaSignature, HcfSm2SpecToDerData_NullOutput, TestSize.Level0)
103 {
104 Sm2EcSignatureDataSpec *spec = nullptr;
105 ASSERT_EQ(ConstructCorrectSm2Spec(&spec), HCF_SUCCESS);
106 EXPECT_EQ(HcfSm2SpecToDerData(spec, nullptr), HCF_ERR_PARAMETER_CHECK_FAILED);
107 HcfFree(spec);
108 }
109
110 HWTEST_F(CryptoSm2EcdsaSignature, HcfDerDataToSm2Spec_NullInput, TestSize.Level0)
111 {
112 Sm2EcSignatureDataSpec *returnSpec = nullptr;
113 EXPECT_EQ(HcfDerDataToSm2Spec(nullptr, &returnSpec), HCF_ERR_PARAMETER_CHECK_FAILED);
114 }
115
116 HWTEST_F(CryptoSm2EcdsaSignature, HcfDerDataToSm2Spec_NullOutput, TestSize.Level0)
117 {
118 HcfBlob derInput = { .data = g_rCoordinate, .len = sizeof(g_rCoordinate) };
119 EXPECT_EQ(HcfDerDataToSm2Spec(&derInput, nullptr), HCF_ERR_PARAMETER_CHECK_FAILED);
120 }
121
122 HWTEST_F(CryptoSm2EcdsaSignature, HcfDerDataToSm2Spec_InvalidData, TestSize.Level0)
123 {
124 unsigned char invalidData[] = { 0x00, 0x01, 0x02, 0x03 };
125 HcfBlob derInput = { .data = invalidData, .len = sizeof(invalidData) };
126 Sm2EcSignatureDataSpec *returnSpec = nullptr;
127 EXPECT_EQ(HcfDerDataToSm2Spec(&derInput, &returnSpec), HCF_ERR_CRYPTO_OPERATION);
128 }
129
130 }