1 /*
2  * Copyright (C) 2024 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_crypto_util_openssl.h"
20 #include "sm2_crypto_util.h"
21 #include "log.h"
22 #include "memory.h"
23 #include "cstring"
24 
25 using namespace std;
26 using namespace testing::ext;
27 
28 namespace {
29 class CryptoSm2UtilTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 
SetUp()37 void CryptoSm2UtilTest::SetUp() {}
TearDown()38 void CryptoSm2UtilTest::TearDown() {}
SetUpTestCase()39 void CryptoSm2UtilTest::SetUpTestCase() {}
TearDownTestCase()40 void CryptoSm2UtilTest::TearDownTestCase() {}
41 
42 static const char *g_sm2ModeC1C3C2 = "C1C3C2";
43 static const char *g_sm2ModeError = "C1C2C2";
44 static const int CORRECT_INPUT_LEN = 121;
45 static const int ERROR_INPUT_LEN = 12;
46 static const int INPUT_LEN_ZERO = 0;
47 static uint8_t g_mockCorrectInput[CORRECT_INPUT_LEN] = {
48     48, 119, 2, 33, 0, 183, 70, 70, 149, 188, 64, 6, 110, 236, 85, 149, 216, 224, 102, 95, 92, 41, 105, 232, 5,
49     248, 122, 21, 174, 43, 226, 221, 104, 82, 88, 153, 45, 2, 32, 96, 229, 78, 209, 233, 110, 5, 149, 91, 110,
50     109, 181, 17, 75, 109, 146, 128, 170, 113, 205, 158, 193, 156, 90, 110, 40, 18, 119, 247, 198, 93, 107, 4,
51     32, 87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115,
52     169, 125, 128, 42, 157, 31, 114, 198, 109, 244, 4, 14, 100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10,
53     65, 123
54 };
55 static HcfBlob g_correctInput = {
56     .data = g_mockCorrectInput,
57     .len = CORRECT_INPUT_LEN
58 };
59 static HcfBlob g_errorInput = {
60     .data = g_mockCorrectInput,
61     .len = ERROR_INPUT_LEN
62 };
63 static const int X_COORDINATE_LEN = 32;
64 static unsigned char g_xCoordinate[] = {
65     45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122, 248, 5, 232, 105, 41, 92, 95, 102, 224, 216, 149, 85, 236,
66     110, 6, 64, 188, 149, 70, 70, 183
67 };
68 static const int Y_COORDINATE_LEN = 32;
69 static unsigned char g_yCoordinate[] = {
70     107, 93, 198, 247, 119, 18, 40, 110, 90, 156, 193, 158, 205, 113, 170, 128, 146, 109, 75, 17, 181, 109, 110,
71     91, 149, 5, 110, 233, 209, 78, 229, 96
72 };
73 static const int HASH_DATA_LEN = 32;
74 static const int ERROR_HASH_DATA_LEN = 15;
75 static unsigned char g_hashData[] = {
76     87, 167, 167, 247, 88, 146, 203, 234, 83, 126, 117, 129, 52, 142, 82, 54, 152, 226, 201, 111, 143, 115, 169,
77     125, 128, 42, 157, 31, 114, 198, 109, 244
78 };
79 static const int CIPHER_TEXT_DATA_LEN = 14;
80 static unsigned char g_cipherTextData[] = {
81     100, 227, 78, 195, 249, 179, 43, 70, 242, 69, 169, 10, 65, 123
82 };
83 
ConstructCorrectSm2CipherTextSpec(Sm2CipherTextSpec ** spec)84 HcfResult ConstructCorrectSm2CipherTextSpec(Sm2CipherTextSpec **spec)
85 {
86     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
87     if (tempSpec == nullptr) {
88         return HCF_ERR_MALLOC;
89     }
90     tempSpec->xCoordinate.data = g_xCoordinate;
91     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
92     tempSpec->yCoordinate.data = g_yCoordinate;
93     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
94     tempSpec->cipherTextData.data = g_cipherTextData;
95     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
96     tempSpec->hashData.data = g_hashData;
97     tempSpec->hashData.len = HASH_DATA_LEN;
98     *spec = tempSpec;
99     return HCF_SUCCESS;
100 }
101 
ConstructMissYErrorSm2CipherTextSpec(Sm2CipherTextSpec ** spec)102 HcfResult ConstructMissYErrorSm2CipherTextSpec(Sm2CipherTextSpec **spec)
103 {
104     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
105     if (tempSpec == nullptr) {
106         return HCF_ERR_MALLOC;
107     }
108     tempSpec->xCoordinate.data = g_xCoordinate;
109     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
110     tempSpec->cipherTextData.data = g_cipherTextData;
111     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
112     tempSpec->hashData.data = g_hashData;
113     tempSpec->hashData.len = HASH_DATA_LEN;
114     *spec = tempSpec;
115     return HCF_SUCCESS;
116 }
117 
ConstructMissXErrorSm2CipherTextSpec(Sm2CipherTextSpec ** spec)118 HcfResult ConstructMissXErrorSm2CipherTextSpec(Sm2CipherTextSpec **spec)
119 {
120     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
121     if (tempSpec == nullptr) {
122         return HCF_ERR_MALLOC;
123     }
124     tempSpec->yCoordinate.data = g_yCoordinate;
125     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
126     tempSpec->cipherTextData.data = g_cipherTextData;
127     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
128     tempSpec->hashData.data = g_hashData;
129     tempSpec->hashData.len = HASH_DATA_LEN;
130     *spec = tempSpec;
131     return HCF_SUCCESS;
132 }
133 
ConstructMissHashDataErrorSm2CipherTextSpec(Sm2CipherTextSpec ** spec)134 HcfResult ConstructMissHashDataErrorSm2CipherTextSpec(Sm2CipherTextSpec **spec)
135 {
136     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
137     if (tempSpec == nullptr) {
138         return HCF_ERR_MALLOC;
139     }
140     tempSpec->xCoordinate.data = g_xCoordinate;
141     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
142     tempSpec->yCoordinate.data = g_yCoordinate;
143     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
144     tempSpec->cipherTextData.data = g_cipherTextData;
145     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
146     *spec = tempSpec;
147     return HCF_SUCCESS;
148 }
149 
ConstructMissCipherDataErrorSm2CipherTextSpec(Sm2CipherTextSpec ** spec)150 HcfResult ConstructMissCipherDataErrorSm2CipherTextSpec(Sm2CipherTextSpec **spec)
151 {
152     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
153     if (tempSpec == nullptr) {
154         return HCF_ERR_MALLOC;
155     }
156     tempSpec->xCoordinate.data = g_xCoordinate;
157     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
158     tempSpec->yCoordinate.data = g_yCoordinate;
159     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
160     tempSpec->hashData.data = g_hashData;
161     tempSpec->hashData.len = HASH_DATA_LEN;
162     *spec = tempSpec;
163     return HCF_SUCCESS;
164 }
165 
ConstructLenErrorSm2CipherTextSpec(Sm2CipherTextSpec ** spec)166 HcfResult ConstructLenErrorSm2CipherTextSpec(Sm2CipherTextSpec **spec)
167 {
168     // hashData.len != 32
169     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
170     if (tempSpec == nullptr) {
171         return HCF_ERR_MALLOC;
172     }
173     tempSpec->xCoordinate.data = g_xCoordinate;
174     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
175     tempSpec->yCoordinate.data = g_yCoordinate;
176     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
177     tempSpec->hashData.data = g_hashData;
178     tempSpec->hashData.len = ERROR_HASH_DATA_LEN;
179     tempSpec->cipherTextData.data = g_cipherTextData;
180     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
181     *spec = tempSpec;
182     return HCF_SUCCESS;
183 }
184 
ConstructLenZeroXSm2CipherTextSpec(Sm2CipherTextSpec ** spec)185 HcfResult ConstructLenZeroXSm2CipherTextSpec(Sm2CipherTextSpec **spec)
186 {
187     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
188     if (tempSpec == nullptr) {
189         return HCF_ERR_MALLOC;
190     }
191     tempSpec->xCoordinate.data = g_xCoordinate;
192     tempSpec->xCoordinate.len = INPUT_LEN_ZERO;
193     tempSpec->yCoordinate.data = g_yCoordinate;
194     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
195     tempSpec->cipherTextData.data = g_cipherTextData;
196     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
197     tempSpec->hashData.data = g_hashData;
198     tempSpec->hashData.len = HASH_DATA_LEN;
199     *spec = tempSpec;
200     return HCF_SUCCESS;
201 }
202 
ConstructLenZeroYSm2CipherTextSpec(Sm2CipherTextSpec ** spec)203 HcfResult ConstructLenZeroYSm2CipherTextSpec(Sm2CipherTextSpec **spec)
204 {
205     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
206     if (tempSpec == nullptr) {
207         return HCF_ERR_MALLOC;
208     }
209     tempSpec->xCoordinate.data = g_xCoordinate;
210     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
211     tempSpec->yCoordinate.data = g_yCoordinate;
212     tempSpec->yCoordinate.len = INPUT_LEN_ZERO;
213     tempSpec->cipherTextData.data = g_cipherTextData;
214     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
215     tempSpec->hashData.data = g_hashData;
216     tempSpec->hashData.len = HASH_DATA_LEN;
217     *spec = tempSpec;
218     return HCF_SUCCESS;
219 }
220 
ConstructLenZeroCipherDataSm2CipherTextSpec(Sm2CipherTextSpec ** spec)221 HcfResult ConstructLenZeroCipherDataSm2CipherTextSpec(Sm2CipherTextSpec **spec)
222 {
223     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
224     if (tempSpec == nullptr) {
225         return HCF_ERR_MALLOC;
226     }
227     tempSpec->xCoordinate.data = g_xCoordinate;
228     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
229     tempSpec->yCoordinate.data = g_yCoordinate;
230     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
231     tempSpec->cipherTextData.data = g_cipherTextData;
232     tempSpec->cipherTextData.len = INPUT_LEN_ZERO;
233     tempSpec->hashData.data = g_hashData;
234     tempSpec->hashData.len = HASH_DATA_LEN;
235     *spec = tempSpec;
236     return HCF_SUCCESS;
237 }
238 
ConstructLenZeroHashDataSm2CipherTextSpec(Sm2CipherTextSpec ** spec)239 HcfResult ConstructLenZeroHashDataSm2CipherTextSpec(Sm2CipherTextSpec **spec)
240 {
241     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
242     if (tempSpec == nullptr) {
243         return HCF_ERR_MALLOC;
244     }
245     tempSpec->xCoordinate.data = g_xCoordinate;
246     tempSpec->xCoordinate.len = X_COORDINATE_LEN;
247     tempSpec->yCoordinate.data = g_yCoordinate;
248     tempSpec->yCoordinate.len = Y_COORDINATE_LEN;
249     tempSpec->cipherTextData.data = g_cipherTextData;
250     tempSpec->cipherTextData.len = CIPHER_TEXT_DATA_LEN;
251     tempSpec->hashData.data = g_hashData;
252     tempSpec->hashData.len = INPUT_LEN_ZERO;
253     *spec = tempSpec;
254     return HCF_SUCCESS;
255 }
256 
ConstructMissErrorSm2CipherTextSpec(Sm2CipherTextSpec ** spec)257 HcfResult ConstructMissErrorSm2CipherTextSpec(Sm2CipherTextSpec **spec)
258 {
259     Sm2CipherTextSpec *tempSpec = static_cast<Sm2CipherTextSpec *>(HcfMalloc(sizeof(Sm2CipherTextSpec), 0));
260     if (tempSpec == nullptr) {
261         return HCF_ERR_MALLOC;
262     }
263     *spec = tempSpec;
264     return HCF_SUCCESS;
265 }
266 
267 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest001, TestSize.Level0)
268 {
269     // test HcfGenCipherTextBySpec success, case mode = C1C3C2
270     int res = 0;
271     Sm2CipherTextSpec *spec = nullptr;
272     res = ConstructCorrectSm2CipherTextSpec(&spec);
273     EXPECT_EQ(res, HCF_SUCCESS);
274     HcfBlob output = { .data = nullptr, .len = 0 };
275     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
276     EXPECT_EQ(res, HCF_SUCCESS);
277     res = memcmp(output.data, g_correctInput.data, g_correctInput.len);
278     HcfBlobDataFree(&output);
279     HcfFree(spec);
280     EXPECT_EQ(res, 0);
281 }
282 
283 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest002, TestSize.Level0)
284 {
285     // test HcfGenCipherTextBySpec success, case mode = null
286     int res = 0;
287     Sm2CipherTextSpec *spec = nullptr;
288     res = ConstructCorrectSm2CipherTextSpec(&spec);
289     EXPECT_EQ(res, HCF_SUCCESS);
290     HcfBlob output = { .data = nullptr, .len = 0 };
291     res = HcfGenCipherTextBySpec(spec, NULL, &output);
292     EXPECT_EQ(res, HCF_SUCCESS);
293     res = memcmp(output.data, g_correctInput.data, g_correctInput.len);
294     HcfBlobDataFree(&output);
295     HcfFree(spec);
296     EXPECT_EQ(res, 0);
297 }
298 
299 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest003, TestSize.Level0)
300 {
301     // test HcfGenCipherTextBySpec error, case mode = C1C2C2
302     Sm2CipherTextSpec *spec = nullptr;
303     HcfResult res = ConstructCorrectSm2CipherTextSpec(&spec);
304     EXPECT_EQ(res, HCF_SUCCESS);
305     HcfBlob output = { .data = nullptr, .len = 0 };
306     res = HcfGenCipherTextBySpec(spec, g_sm2ModeError, &output);
307     EXPECT_EQ(res, HCF_INVALID_PARAMS);
308     HcfFree(spec);
309 }
310 
311 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest004, TestSize.Level0)
312 {
313     // test HcfGenCipherTextBySpec error, case spec miss yCoordinate
314     Sm2CipherTextSpec *spec = nullptr;
315     HcfResult res = ConstructMissYErrorSm2CipherTextSpec(&spec);
316     EXPECT_EQ(res, HCF_SUCCESS);
317     HcfBlob output = { .data = nullptr, .len = 0 };
318     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
319     EXPECT_EQ(res, HCF_INVALID_PARAMS);
320     HcfFree(spec);
321 }
322 
323 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest005, TestSize.Level0)
324 {
325     // test HcfGenCipherTextBySpec error, case spec hashData.len != 32
326     Sm2CipherTextSpec *spec = nullptr;
327     HcfResult res = ConstructLenErrorSm2CipherTextSpec(&spec);
328     EXPECT_EQ(res, HCF_SUCCESS);
329     HcfBlob output = { .data = nullptr, .len = 0 };
330     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
331     EXPECT_EQ(res, HCF_INVALID_PARAMS);
332     HcfFree(spec);
333 }
334 
335 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest006, TestSize.Level0)
336 {
337     // test HcfGenCipherTextBySpec error, case spec miss xCoordinate
338     Sm2CipherTextSpec *spec = nullptr;
339     HcfResult res = ConstructMissXErrorSm2CipherTextSpec(&spec);
340     EXPECT_EQ(res, HCF_SUCCESS);
341     HcfBlob output = { .data = nullptr, .len = 0 };
342     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
343     EXPECT_EQ(res, HCF_INVALID_PARAMS);
344     HcfFree(spec);
345 }
346 
347 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest007, TestSize.Level0)
348 {
349     // test HcfGenCipherTextBySpec error, case spec miss hashData
350     Sm2CipherTextSpec *spec = nullptr;
351     HcfResult res = ConstructMissHashDataErrorSm2CipherTextSpec(&spec);
352     EXPECT_EQ(res, HCF_SUCCESS);
353     HcfBlob output = { .data = nullptr, .len = 0 };
354     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
355     EXPECT_EQ(res, HCF_INVALID_PARAMS);
356     HcfFree(spec);
357 }
358 
359 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest008, TestSize.Level0)
360 {
361     // test HcfGenCipherTextBySpec error, case spec miss cipherData
362     Sm2CipherTextSpec *spec = nullptr;
363     HcfResult res = ConstructMissCipherDataErrorSm2CipherTextSpec(&spec);
364     EXPECT_EQ(res, HCF_SUCCESS);
365     HcfBlob output = { .data = nullptr, .len = 0 };
366     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
367     EXPECT_EQ(res, HCF_INVALID_PARAMS);
368     HcfFree(spec);
369 }
370 
371 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest009, TestSize.Level0)
372 {
373     // test HcfGenCipherTextBySpec error, case output is null
374     Sm2CipherTextSpec *spec = nullptr;
375     HcfResult res = ConstructCorrectSm2CipherTextSpec(&spec);
376     EXPECT_EQ(res, HCF_SUCCESS);
377     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, NULL);
378     EXPECT_EQ(res, HCF_INVALID_PARAMS);
379     HcfFree(spec);
380 }
381 
382 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest010, TestSize.Level0)
383 {
384     // test HcfGenCipherTextBySpec error, case spec is null
385     HcfBlob output = { .data = nullptr, .len = 0 };
386     HcfResult res = HcfGenCipherTextBySpec(nullptr, g_sm2ModeC1C3C2, &output);
387     EXPECT_EQ(res, HCF_INVALID_PARAMS);
388 }
389 
390 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest011, TestSize.Level0)
391 {
392     // test HcfGenCipherTextBySpec error, case spec xCoordinate.len = 0
393     Sm2CipherTextSpec *spec = nullptr;
394     HcfResult res = ConstructLenZeroXSm2CipherTextSpec(&spec);
395     EXPECT_EQ(res, HCF_SUCCESS);
396     HcfBlob output = { .data = nullptr, .len = 0 };
397     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
398     EXPECT_EQ(res, HCF_INVALID_PARAMS);
399     HcfFree(spec);
400 }
401 
402 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest012, TestSize.Level0)
403 {
404     // test HcfGenCipherTextBySpec error, case spec yCoordinate.len = 0
405     Sm2CipherTextSpec *spec = nullptr;
406     HcfResult res = ConstructLenZeroYSm2CipherTextSpec(&spec);
407     EXPECT_EQ(res, HCF_SUCCESS);
408     HcfBlob output = { .data = nullptr, .len = 0 };
409     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
410     EXPECT_EQ(res, HCF_INVALID_PARAMS);
411     HcfFree(spec);
412 }
413 
414 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest013, TestSize.Level0)
415 {
416     // test HcfGenCipherTextBySpec error, case spec cipherTextData.len = 0
417     Sm2CipherTextSpec *spec = nullptr;
418     HcfResult res = ConstructLenZeroCipherDataSm2CipherTextSpec(&spec);
419     EXPECT_EQ(res, HCF_SUCCESS);
420     HcfBlob output = { .data = nullptr, .len = 0 };
421     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
422     EXPECT_EQ(res, HCF_INVALID_PARAMS);
423     HcfFree(spec);
424 }
425 
426 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest014, TestSize.Level0)
427 {
428     // test HcfGenCipherTextBySpec error, case spec hashData.len = 0
429     Sm2CipherTextSpec *spec = nullptr;
430     HcfResult res = ConstructLenZeroHashDataSm2CipherTextSpec(&spec);
431     EXPECT_EQ(res, HCF_SUCCESS);
432     HcfBlob output = { .data = nullptr, .len = 0 };
433     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
434     EXPECT_EQ(res, HCF_INVALID_PARAMS);
435     HcfFree(spec);
436 }
437 
438 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest015, TestSize.Level0)
439 {
440     // test HcfGenCipherTextBySpec error, case spec null construct
441     Sm2CipherTextSpec *spec = nullptr;
442     HcfResult res = ConstructMissErrorSm2CipherTextSpec(&spec);
443     EXPECT_EQ(res, HCF_SUCCESS);
444     HcfBlob output = { .data = nullptr, .len = 0 };
445     res = HcfGenCipherTextBySpec(spec, g_sm2ModeC1C3C2, &output);
446     EXPECT_EQ(res, HCF_INVALID_PARAMS);
447     HcfFree(spec);
448 }
449 
450 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest101, TestSize.Level0)
451 {
452     // test HcfGetCipherTextSpec success, case mode = C1C3C2
453     Sm2CipherTextSpec *spec = nullptr;
454     HcfResult res = HcfGetCipherTextSpec(&g_correctInput, g_sm2ModeC1C3C2, &spec);
455     EXPECT_EQ(res, HCF_SUCCESS);
456     DestroySm2CipherTextSpec(spec);
457 }
458 
459 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest102, TestSize.Level0)
460 {
461     // test HcfGenCipherTextBySpec success, case mode = null
462     Sm2CipherTextSpec *spec = nullptr;
463     HcfResult res = HcfGetCipherTextSpec(&g_correctInput, NULL, &spec);
464     EXPECT_EQ(res, HCF_SUCCESS);
465     DestroySm2CipherTextSpec(spec);
466 }
467 
468 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest103, TestSize.Level0)
469 {
470     // test HcfGenCipherTextBySpec error, case mode = C1C2C2
471     Sm2CipherTextSpec *spec = nullptr;
472     HcfResult res = HcfGetCipherTextSpec(&g_correctInput, g_sm2ModeError, &spec);
473     EXPECT_EQ(res, HCF_INVALID_PARAMS);
474 }
475 
476 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest104, TestSize.Level0)
477 {
478     // test HcfGetCipherTextSpec error, case input null
479     Sm2CipherTextSpec *spec = nullptr;
480     HcfResult res = HcfGetCipherTextSpec(NULL, g_sm2ModeC1C3C2, &spec);
481     EXPECT_EQ(res, HCF_INVALID_PARAMS);
482 }
483 
484 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest105, TestSize.Level0)
485 {
486     // test HcfGetCipherTextSpec error, case input error len
487     Sm2CipherTextSpec *spec = nullptr;
488     HcfResult res = HcfGetCipherTextSpec(&g_errorInput, g_sm2ModeC1C3C2, &spec);
489     EXPECT_EQ(res, HCF_ERR_CRYPTO_OPERATION);
490 }
491 
492 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest106, TestSize.Level0)
493 {
494     // test HcfGetCipherTextSpec error, case returnSpec is null
495     HcfResult res = HcfGetCipherTextSpec(&g_correctInput, g_sm2ModeC1C3C2, NULL);
496     EXPECT_EQ(res, HCF_INVALID_PARAMS);
497 }
498 
499 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest201, TestSize.Level0)
500 {
501     // test HcfSm2SpecToAsn1 success
502     int res = 0;
503     Sm2CipherTextSpec *spec = nullptr;
504     res = ConstructCorrectSm2CipherTextSpec(&spec);
505     EXPECT_EQ(res, HCF_SUCCESS);
506     HcfBlob output = { .data = nullptr, .len = 0 };
507     res = HcfSm2SpecToAsn1(spec, &output);
508     EXPECT_EQ(res, HCF_SUCCESS);
509     res = memcmp(output.data, g_correctInput.data, g_correctInput.len);
510     HcfBlobDataFree(&output);
511     HcfFree(spec);
512     EXPECT_EQ(res, 0);
513 }
514 
515 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest202, TestSize.Level0)
516 {
517     // test HcfSm2SpecToAsn1 success, case spec miss yCoordinate
518     Sm2CipherTextSpec *spec = nullptr;
519     HcfResult res = ConstructMissYErrorSm2CipherTextSpec(&spec);
520     EXPECT_EQ(res, HCF_SUCCESS);
521     HcfBlob output = { .data = nullptr, .len = 0 };
522     res = HcfSm2SpecToAsn1(spec, &output);
523     EXPECT_EQ(res, HCF_SUCCESS);
524     HcfFree(spec);
525 }
526 
527 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest203, TestSize.Level0)
528 {
529     // test HcfSm2SpecToAsn1 success, case spec miss xCoordinate
530     Sm2CipherTextSpec *spec = nullptr;
531     HcfResult res = ConstructMissXErrorSm2CipherTextSpec(&spec);
532     EXPECT_EQ(res, HCF_SUCCESS);
533     HcfBlob output = { .data = nullptr, .len = 0 };
534     res = HcfSm2SpecToAsn1(spec, &output);
535     EXPECT_EQ(res, HCF_SUCCESS);
536     HcfBlobDataFree(&output);
537     HcfFree(spec);
538 }
539 
540 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest204, TestSize.Level0)
541 {
542     // test HcfSm2SpecToAsn1 success, case spec miss hashData
543     Sm2CipherTextSpec *spec = nullptr;
544     HcfResult res = ConstructMissHashDataErrorSm2CipherTextSpec(&spec);
545     EXPECT_EQ(res, HCF_SUCCESS);
546     HcfBlob output = { .data = nullptr, .len = 0 };
547     res = HcfSm2SpecToAsn1(spec, &output);
548     EXPECT_EQ(res, HCF_SUCCESS);
549     HcfBlobDataFree(&output);
550     HcfFree(spec);
551 }
552 
553 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest205, TestSize.Level0)
554 {
555     // test HcfGenCipherTextBySpec success, case spec miss cipherData
556     Sm2CipherTextSpec *spec = nullptr;
557     HcfResult res = ConstructMissCipherDataErrorSm2CipherTextSpec(&spec);
558     EXPECT_EQ(res, HCF_SUCCESS);
559     HcfBlob output = { .data = nullptr, .len = 0 };
560     res = HcfSm2SpecToAsn1(spec, &output);
561     EXPECT_EQ(res, HCF_SUCCESS);
562     HcfBlobDataFree(&output);
563     HcfFree(spec);
564 }
565 
566 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest301, TestSize.Level0)
567 {
568     // test HcfAsn1ToSm2Spec success
569     Sm2CipherTextSpec *spec = nullptr;
570     HcfResult res = HcfAsn1ToSm2Spec(&g_correctInput, &spec);
571     EXPECT_EQ(res, HCF_SUCCESS);
572     DestroySm2CipherTextSpec(spec);
573 }
574 
575 HWTEST_F(CryptoSm2UtilTest, CryptoSm2UtilTest305, TestSize.Level0)
576 {
577     // test HcfAsn1ToSm2Spec error, case input error len
578     Sm2CipherTextSpec *spec = nullptr;
579     HcfResult res = HcfAsn1ToSm2Spec(&g_errorInput, &spec);
580     EXPECT_EQ(res, HCF_ERR_CRYPTO_OPERATION);
581 }
582 }