1 /*
2 * Copyright (C) 2023 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 <cstring>
18
19 #include "asy_key_generator.h"
20 #include "blob.h"
21 #include "sm2_openssl.h"
22 #include "memory.h"
23 #include "securec.h"
24 #include "signature.h"
25 #include "memory_mock.h"
26 #include "openssl_adapter_mock.h"
27
28 using namespace std;
29 using namespace testing::ext;
30
31 namespace {
32 class CryptoSm2SignTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38
39 static HcfKeyPair *sm2256KeyPair_;
40 };
41
42 HcfKeyPair *CryptoSm2SignTest::sm2256KeyPair_ = nullptr;
43
44 static const char *g_mockMessage = "hello world";
45 static HcfBlob g_mockInput = {
46 .data = (uint8_t *)g_mockMessage,
47 .len = 12
48 };
49
SetUp()50 void CryptoSm2SignTest::SetUp() {}
TearDown()51 void CryptoSm2SignTest::TearDown() {}
52
SetUpTestCase()53 void CryptoSm2SignTest::SetUpTestCase()
54 {
55 HcfAsyKeyGenerator *generator = nullptr;
56 int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
57 ASSERT_EQ(res, HCF_SUCCESS);
58 ASSERT_NE(generator, nullptr);
59
60 HcfKeyPair *keyPair = nullptr;
61 res = generator->generateKeyPair(generator, nullptr, &keyPair);
62 ASSERT_EQ(res, HCF_SUCCESS);
63 ASSERT_NE(keyPair, nullptr);
64
65 sm2256KeyPair_ = keyPair;
66
67 HcfObjDestroy(generator);
68 }
69
TearDownTestCase()70 void CryptoSm2SignTest::TearDownTestCase()
71 {
72 HcfObjDestroy(sm2256KeyPair_);
73 }
74
GetMockClass(void)75 static const char *GetMockClass(void)
76 {
77 return "HcfMock";
78 }
79
80 static HcfObjectBase obj = {
81 .getClass = GetMockClass,
82 .destroy = nullptr
83 };
84
85 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest001, TestSize.Level0)
86 {
87 HcfSign *sign = nullptr;
88 int32_t res = HcfSignCreate("SM2|SM3", &sign);
89
90 ASSERT_EQ(res, HCF_SUCCESS);
91 ASSERT_NE(sign, nullptr);
92
93 HcfObjDestroy(sign);
94 }
95
96 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest002, TestSize.Level0)
97 {
98 HcfSign *sign = nullptr;
99 int32_t res = HcfSignCreate(nullptr, &sign);
100
101 ASSERT_EQ(res, HCF_INVALID_PARAMS);
102 ASSERT_EQ(sign, nullptr);
103 }
104
105 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest003, TestSize.Level0)
106 {
107 HcfSign *sign = nullptr;
108 int32_t res = HcfSignCreate("ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD"
109 "ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD", &sign);
110
111 ASSERT_EQ(res, HCF_INVALID_PARAMS);
112 ASSERT_EQ(sign, nullptr);
113 }
114
115 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest004, TestSize.Level0)
116 {
117 HcfSign *sign = nullptr;
118 int32_t res = HcfSignCreate("SM5|SM3", &sign);
119
120 ASSERT_NE(res, HCF_SUCCESS);
121 ASSERT_EQ(sign, nullptr);
122 }
123
124 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest005, TestSize.Level0)
125 {
126 HcfSign *sign = nullptr;
127 int32_t res = HcfSignCreate("SM2|SM5", &sign);
128
129 ASSERT_NE(res, HCF_SUCCESS);
130 ASSERT_EQ(sign, nullptr);
131 }
132
133 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest006, TestSize.Level0)
134 {
135 HcfSign *sign = nullptr;
136 int32_t res = HcfSignCreate("SM2|MD5", &sign);
137
138 ASSERT_NE(res, HCF_SUCCESS);
139 ASSERT_EQ(sign, nullptr);
140 }
141
142 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest007, TestSize.Level0)
143 {
144 HcfSign *sign = nullptr;
145 int32_t res = HcfSignCreate("SM2SM3", &sign);
146
147 ASSERT_NE(res, HCF_SUCCESS);
148 ASSERT_EQ(sign, nullptr);
149 }
150
151 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest008, TestSize.Level0)
152 {
153 int32_t res = HcfSignCreate("SM2|SM3", nullptr);
154
155 ASSERT_EQ(res, HCF_INVALID_PARAMS);
156 }
157
158 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest009, TestSize.Level0)
159 {
160 HcfSign *sign = nullptr;
161 int32_t res = HcfSignCreate("SM2|SM3", &sign);
162
163 ASSERT_EQ(res, HCF_SUCCESS);
164 ASSERT_NE(sign, nullptr);
165
166 const char *className = sign->base.getClass();
167
168 ASSERT_NE(className, nullptr);
169
170 HcfObjDestroy(sign);
171 }
172
173 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest010, TestSize.Level0)
174 {
175 HcfSign *sign = nullptr;
176 int32_t res = HcfSignCreate("SM2|SM3", &sign);
177
178 ASSERT_EQ(res, HCF_SUCCESS);
179 ASSERT_NE(sign, nullptr);
180
181 sign->base.destroy((HcfObjectBase *)sign);
182 }
183
184 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest011, TestSize.Level0)
185 {
186 HcfSign *sign = nullptr;
187 int32_t res = HcfSignCreate("SM2|SM3", &sign);
188
189 ASSERT_EQ(res, HCF_SUCCESS);
190 ASSERT_NE(sign, nullptr);
191
192 sign->base.destroy(nullptr);
193
194 HcfObjDestroy(sign);
195 }
196
197 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest012, TestSize.Level0)
198 {
199 HcfSign *sign = nullptr;
200 int32_t res = HcfSignCreate("SM2|SM3", &sign);
201
202 ASSERT_EQ(res, HCF_SUCCESS);
203 ASSERT_NE(sign, nullptr);
204
205 sign->base.destroy(&obj);
206
207 HcfObjDestroy(sign);
208 }
209
210 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest013, TestSize.Level0)
211 {
212 HcfSign *sign = nullptr;
213 int32_t res = HcfSignCreate("SM2|SM3", &sign);
214
215 ASSERT_EQ(res, HCF_SUCCESS);
216 ASSERT_NE(sign, nullptr);
217
218 const char *algName = sign->getAlgoName(sign);
219
220 ASSERT_NE(algName, nullptr);
221
222 HcfObjDestroy(sign);
223 }
224
225 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest014, TestSize.Level0)
226 {
227 HcfSign *sign = nullptr;
228 int32_t res = HcfSignCreate("SM2|SM3", &sign);
229
230 ASSERT_EQ(res, HCF_SUCCESS);
231 ASSERT_NE(sign, nullptr);
232
233 const char *algName = sign->getAlgoName(nullptr);
234
235 ASSERT_EQ(algName, nullptr);
236
237 HcfObjDestroy(sign);
238 }
239
240 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest016, TestSize.Level0)
241 {
242 HcfSign *sign = nullptr;
243 int32_t res = HcfSignCreate("SM2|SM3", &sign);
244
245 ASSERT_EQ(res, HCF_SUCCESS);
246 ASSERT_NE(sign, nullptr);
247
248 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
249
250 ASSERT_EQ(res, HCF_SUCCESS);
251
252 HcfObjDestroy(sign);
253 }
254
255 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest017, TestSize.Level0)
256 {
257 HcfSign *sign = nullptr;
258 int32_t res = HcfSignCreate("SM2|SM3", &sign);
259
260 ASSERT_EQ(res, HCF_SUCCESS);
261 ASSERT_NE(sign, nullptr);
262
263 res = sign->init(nullptr, nullptr, sm2256KeyPair_->priKey);
264
265 ASSERT_EQ(res, HCF_INVALID_PARAMS);
266
267 HcfObjDestroy(sign);
268 }
269
270 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest019, TestSize.Level0)
271 {
272 HcfSign *sign = nullptr;
273 int32_t res = HcfSignCreate("SM2|SM3", &sign);
274
275 ASSERT_EQ(res, HCF_SUCCESS);
276 ASSERT_NE(sign, nullptr);
277
278 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
279
280 ASSERT_EQ(res, HCF_SUCCESS);
281
282 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
283
284 ASSERT_EQ(res, HCF_INVALID_PARAMS);
285
286 HcfObjDestroy(sign);
287 }
288
289 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest020, TestSize.Level0)
290 {
291 HcfSign *sign = nullptr;
292 int32_t res = HcfSignCreate("SM2|SM3", &sign);
293
294 ASSERT_EQ(res, HCF_SUCCESS);
295 ASSERT_NE(sign, nullptr);
296
297 res = sign->init(sign, nullptr, nullptr);
298
299 ASSERT_EQ(res, HCF_INVALID_PARAMS);
300
301 HcfObjDestroy(sign);
302 }
303
304 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest022, TestSize.Level0)
305 {
306 HcfSign *sign = nullptr;
307 int32_t res = HcfSignCreate("SM2|SM3", &sign);
308
309 ASSERT_EQ(res, HCF_SUCCESS);
310 ASSERT_NE(sign, nullptr);
311
312 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
313
314 ASSERT_EQ(res, HCF_SUCCESS);
315
316 res = sign->update(sign, &g_mockInput);
317
318 ASSERT_EQ(res, HCF_SUCCESS);
319
320 HcfObjDestroy(sign);
321 }
322
323 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest023, TestSize.Level0)
324 {
325 HcfSign *sign = nullptr;
326 int32_t res = HcfSignCreate("SM2|SM3", &sign);
327
328 ASSERT_EQ(res, HCF_SUCCESS);
329 ASSERT_NE(sign, nullptr);
330
331 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
332
333 ASSERT_EQ(res, HCF_SUCCESS);
334
335 res = sign->update(nullptr, &g_mockInput);
336
337 ASSERT_EQ(res, HCF_INVALID_PARAMS);
338
339 HcfObjDestroy(sign);
340 }
341
342 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest025, TestSize.Level0)
343 {
344 HcfSign *sign = nullptr;
345 int32_t res = HcfSignCreate("SM2|SM3", &sign);
346
347 ASSERT_EQ(res, HCF_SUCCESS);
348 ASSERT_NE(sign, nullptr);
349
350 res = sign->update(sign, &g_mockInput);
351
352 ASSERT_EQ(res, HCF_INVALID_PARAMS);
353
354 HcfObjDestroy(sign);
355 }
356
357 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest026, TestSize.Level0)
358 {
359 HcfSign *sign = nullptr;
360 int32_t res = HcfSignCreate("SM2|SM3", &sign);
361
362 ASSERT_EQ(res, HCF_SUCCESS);
363 ASSERT_NE(sign, nullptr);
364
365 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
366
367 ASSERT_EQ(res, HCF_SUCCESS);
368
369 res = sign->update(sign, nullptr);
370
371 ASSERT_EQ(res, HCF_INVALID_PARAMS);
372
373 HcfObjDestroy(sign);
374 }
375
376 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest027, TestSize.Level0)
377 {
378 HcfSign *sign = nullptr;
379 int32_t res = HcfSignCreate("SM2|SM3", &sign);
380
381 ASSERT_EQ(res, HCF_SUCCESS);
382 ASSERT_NE(sign, nullptr);
383
384 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
385
386 ASSERT_EQ(res, HCF_SUCCESS);
387
388 HcfBlob input = {
389 .data = nullptr,
390 .len = 1
391 };
392 res = sign->update(sign, &input);
393
394 ASSERT_EQ(res, HCF_INVALID_PARAMS);
395
396 HcfObjDestroy(sign);
397 }
398
399 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest028, TestSize.Level0)
400 {
401 HcfSign *sign = nullptr;
402 int32_t res = HcfSignCreate("SM2|SM3", &sign);
403
404 ASSERT_EQ(res, HCF_SUCCESS);
405 ASSERT_NE(sign, nullptr);
406
407 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
408
409 ASSERT_EQ(res, HCF_SUCCESS);
410
411 HcfBlob input = {
412 .data = (uint8_t *)g_mockMessage,
413 .len = 0
414 };
415 res = sign->update(sign, &input);
416
417 ASSERT_EQ(res, HCF_INVALID_PARAMS);
418
419 HcfObjDestroy(sign);
420 }
421
422 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest029, TestSize.Level0)
423 {
424 HcfSign *sign = nullptr;
425 int32_t res = HcfSignCreate("SM2|SM3", &sign);
426
427 ASSERT_EQ(res, HCF_SUCCESS);
428 ASSERT_NE(sign, nullptr);
429
430 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
431
432 ASSERT_EQ(res, HCF_SUCCESS);
433
434 res = sign->update(sign, &g_mockInput);
435
436 ASSERT_EQ(res, HCF_SUCCESS);
437
438 HcfBlob out = { .data = nullptr, .len = 0 };
439 res = sign->sign(sign, nullptr, &out);
440
441 ASSERT_EQ(res, HCF_SUCCESS);
442 ASSERT_NE(out.data, nullptr);
443 ASSERT_NE(out.len, (const unsigned int)0);
444
445 free(out.data);
446 HcfObjDestroy(sign);
447 }
448
449 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest030, TestSize.Level0)
450 {
451 HcfSign *sign = nullptr;
452 int32_t res = HcfSignCreate("SM2|SM3", &sign);
453
454 ASSERT_EQ(res, HCF_SUCCESS);
455 ASSERT_NE(sign, nullptr);
456
457 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
458
459 ASSERT_EQ(res, HCF_SUCCESS);
460
461 HcfBlob out = { .data = nullptr, .len = 0 };
462 res = sign->sign(sign, &g_mockInput, &out);
463
464 ASSERT_EQ(res, HCF_SUCCESS);
465 ASSERT_NE(out.data, nullptr);
466 ASSERT_NE(out.len, (const unsigned int)0);
467
468 free(out.data);
469 HcfObjDestroy(sign);
470 }
471
472 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest031, TestSize.Level0)
473 {
474 HcfSign *sign = nullptr;
475 int32_t res = HcfSignCreate("SM2|SM3", &sign);
476
477 ASSERT_EQ(res, HCF_SUCCESS);
478 ASSERT_NE(sign, nullptr);
479
480 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
481
482 ASSERT_EQ(res, HCF_SUCCESS);
483
484 res = sign->update(sign, &g_mockInput);
485
486 ASSERT_EQ(res, HCF_SUCCESS);
487
488 HcfBlob out = { .data = nullptr, .len = 0 };
489 res = sign->sign(nullptr, nullptr, &out);
490
491 ASSERT_EQ(res, HCF_INVALID_PARAMS);
492
493 HcfObjDestroy(sign);
494 }
495
496 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest033, TestSize.Level0)
497 {
498 HcfSign *sign = nullptr;
499 int32_t res = HcfSignCreate("SM2|SM3", &sign);
500
501 ASSERT_EQ(res, HCF_SUCCESS);
502 ASSERT_NE(sign, nullptr);
503
504 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
505
506 ASSERT_EQ(res, HCF_SUCCESS);
507
508 HcfBlob out = { .data = nullptr, .len = 0 };
509 res = sign->sign(sign, nullptr, &out);
510
511 ASSERT_EQ(res, HCF_INVALID_PARAMS);
512
513 HcfObjDestroy(sign);
514 }
515
516 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest034, TestSize.Level0)
517 {
518 HcfSign *sign = nullptr;
519 int32_t res = HcfSignCreate("SM2|SM3", &sign);
520
521 ASSERT_EQ(res, HCF_SUCCESS);
522 ASSERT_NE(sign, nullptr);
523
524 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
525
526 ASSERT_EQ(res, HCF_SUCCESS);
527
528 HcfBlob input = {
529 .data = nullptr,
530 .len = 1
531 };
532 HcfBlob out = { .data = nullptr, .len = 0 };
533 res = sign->sign(sign, &input, &out);
534
535 ASSERT_EQ(res, HCF_INVALID_PARAMS);
536
537 HcfObjDestroy(sign);
538 }
539
540 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest035, TestSize.Level0)
541 {
542 HcfSign *sign = nullptr;
543 int32_t res = HcfSignCreate("SM2|SM3", &sign);
544
545 ASSERT_EQ(res, HCF_SUCCESS);
546 ASSERT_NE(sign, nullptr);
547
548 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
549
550 ASSERT_EQ(res, HCF_SUCCESS);
551
552 HcfBlob input = {
553 .data = (uint8_t *)g_mockMessage,
554 .len = 0
555 };
556 HcfBlob out = { .data = nullptr, .len = 0 };
557 res = sign->sign(sign, &input, &out);
558
559 ASSERT_EQ(res, HCF_INVALID_PARAMS);
560
561 HcfObjDestroy(sign);
562 }
563
564 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest036, TestSize.Level0)
565 {
566 HcfSign *sign = nullptr;
567 int32_t res = HcfSignCreate("SM2|SM3", &sign);
568
569 ASSERT_EQ(res, HCF_SUCCESS);
570 ASSERT_NE(sign, nullptr);
571
572 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
573
574 ASSERT_EQ(res, HCF_SUCCESS);
575
576 res = sign->update(sign, &g_mockInput);
577
578 ASSERT_EQ(res, HCF_SUCCESS);
579
580 res = sign->sign(sign, nullptr, nullptr);
581
582 ASSERT_EQ(res, HCF_INVALID_PARAMS);
583
584 HcfObjDestroy(sign);
585 }
586
587 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest037, TestSize.Level0)
588 {
589 HcfSign *sign = nullptr;
590 int32_t res = HcfSignCreate("SM2|SM3", &sign);
591
592 ASSERT_EQ(res, HCF_SUCCESS);
593 ASSERT_NE(sign, nullptr);
594
595 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
596
597 ASSERT_EQ(res, HCF_SUCCESS);
598
599 const char *message = "hello world";
600 HcfBlob input = {
601 .data = (uint8_t *)message,
602 .len = 12
603 };
604 res = sign->update(sign, &input);
605
606 ASSERT_EQ(res, HCF_SUCCESS);
607
608 HcfBlob out = { .data = nullptr, .len = 0 };
609 res = sign->sign(sign, nullptr, &out);
610
611 ASSERT_EQ(res, HCF_SUCCESS);
612 ASSERT_NE(out.data, nullptr);
613 ASSERT_NE(out.len, (const unsigned int)0);
614
615 res = sign->update(sign, &input);
616
617 ASSERT_EQ(res, HCF_SUCCESS);
618
619 HcfBlob out2 = { .data = nullptr, .len = 0 };
620 res = sign->sign(sign, nullptr, &out2);
621
622 HcfVerify *verify = nullptr;
623 res = HcfVerifyCreate("SM2|SM3", &verify);
624 ASSERT_EQ(res, HCF_SUCCESS);
625 ASSERT_NE(verify, nullptr);
626
627 verify->init(verify, nullptr, sm2256KeyPair_->pubKey);
628 ASSERT_EQ(res, HCF_SUCCESS);
629
630 verify->update(verify, &input);
631 ASSERT_EQ(res, HCF_SUCCESS);
632
633 bool flag = verify->verify(verify, nullptr, &out);
634 ASSERT_EQ(flag, true);
635
636 verify->update(verify, &input);
637 ASSERT_EQ(res, HCF_SUCCESS);
638
639 flag = verify->verify(verify, nullptr, &out2);
640 ASSERT_EQ(flag, true);
641
642 free(out.data);
643 free(out2.data);
644 HcfObjDestroy(sign);
645 HcfObjDestroy(verify);
646 }
647
648 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest038, TestSize.Level0)
649 {
650 HcfSign *sign = nullptr;
651 int32_t res = HcfSignCreate("SM2|SM3", &sign);
652
653 ASSERT_EQ(res, HCF_SUCCESS);
654 ASSERT_NE(sign, nullptr);
655
656 res = sign->init(sign, nullptr, sm2256KeyPair_->priKey);
657
658 ASSERT_EQ(res, HCF_SUCCESS);
659
660 const char *message = "hello world";
661 HcfBlob input = {
662 .data = (uint8_t *)message,
663 .len = 12
664 };
665 res = sign->update(sign, &input);
666
667 ASSERT_EQ(res, HCF_SUCCESS);
668
669 res = sign->update(sign, &input);
670
671 ASSERT_EQ(res, HCF_SUCCESS);
672
673 HcfBlob out2 = { .data = nullptr, .len = 0 };
674 res = sign->sign(sign, nullptr, &out2);
675
676 free(out2.data);
677 HcfObjDestroy(sign);
678 }
679
680 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest039, TestSize.Level0)
681 {
682 HcfAsyKeyGenerator *generator = nullptr;
683 int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
684 ASSERT_EQ(res, HCF_SUCCESS);
685 ASSERT_NE(generator, nullptr);
686
687 HcfKeyPair *keyPair = nullptr;
688 res = generator->generateKeyPair(generator, nullptr, &keyPair);
689 ASSERT_EQ(res, HCF_SUCCESS);
690 ASSERT_NE(keyPair, nullptr);
691
692 HcfObjDestroy(generator);
693
694 HcfSign *sign = nullptr;
695 res = HcfSignCreate("SM2|SM3", &sign);
696 ASSERT_EQ(res, HCF_SUCCESS);
697 ASSERT_NE(sign, nullptr);
698
699 HcfParamsSpec params;
700 res = sign->init(sign, ¶ms, keyPair->priKey);
701 ASSERT_EQ(res, HCF_SUCCESS);
702
703 const char *message = "hello world";
704 HcfBlob input = {
705 .data = (uint8_t *)message,
706 .len = 12
707 };
708
709 HcfBlob out = { .data = nullptr, .len = 0 };
710 res = sign->sign(sign, &input, &out);
711 ASSERT_EQ(res, HCF_SUCCESS);
712 ASSERT_NE(out.data, nullptr);
713 ASSERT_NE(out.len, (const unsigned int)0);
714
715 free(out.data);
716 HcfObjDestroy(sign);
717 }
718
719 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest040, TestSize.Level0)
720 {
721 HcfAsyKeyGenerator *generator = nullptr;
722 int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
723 ASSERT_EQ(res, HCF_SUCCESS);
724 ASSERT_NE(generator, nullptr);
725
726 HcfKeyPair *keyPair = nullptr;
727 res = generator->generateKeyPair(generator, nullptr, &keyPair);
728 ASSERT_EQ(res, HCF_SUCCESS);
729 ASSERT_NE(keyPair, nullptr);
730
731 HcfObjDestroy(generator);
732
733 HcfSign *sign = nullptr;
734 res = HcfSignCreate("SM2|SM3", &sign);
735 ASSERT_EQ(res, HCF_SUCCESS);
736 ASSERT_NE(sign, nullptr);
737
738 HcfParamsSpec params;
739 res = sign->init(sign, ¶ms, keyPair->priKey);
740 ASSERT_EQ(res, HCF_SUCCESS);
741
742 const char *message = "hello world";
743 HcfBlob input = {
744 .data = (uint8_t *)message,
745 .len = 12
746 };
747 res = sign->update(sign, &input);
748 ASSERT_EQ(res, HCF_SUCCESS);
749
750 HcfBlob out = { .data = nullptr, .len = 0 };
751 res = sign->sign(sign, nullptr, &out);
752 ASSERT_EQ(res, HCF_SUCCESS);
753 ASSERT_NE(out.data, nullptr);
754 ASSERT_NE(out.len, (const unsigned int)0);
755
756 free(out.data);
757 HcfObjDestroy(sign);
758 }
759
760 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest041, TestSize.Level0)
761 {
762 HcfSignSpi *spiObj = nullptr;
763 int32_t res = HcfSignSpiSm2Create(nullptr, &spiObj);
764
765 ASSERT_EQ(res, HCF_INVALID_PARAMS);
766 ASSERT_EQ(spiObj, nullptr);
767 }
768
769 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest042, TestSize.Level0)
770 {
771 HcfSignatureParams params = {
772 .algo = HCF_ALG_SM2,
773 .md = HCF_OPENSSL_DIGEST_SM3,
774 };
775 int32_t res = HcfSignSpiSm2Create(¶ms, nullptr);
776
777 ASSERT_EQ(res, HCF_INVALID_PARAMS);
778 }
779
780 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest043, TestSize.Level0)
781 {
782 HcfSignatureParams params = {
783 .algo = HCF_ALG_SM2,
784 .md = HCF_OPENSSL_DIGEST_SM3,
785 };
786 HcfSignSpi *spiObj = nullptr;
787 int32_t res = HcfSignSpiSm2Create(¶ms, &spiObj);
788
789 ASSERT_EQ(res, HCF_SUCCESS);
790 ASSERT_NE(spiObj, nullptr);
791
792 res = spiObj->engineInit((HcfSignSpi *)&obj, nullptr, sm2256KeyPair_->priKey);
793 ASSERT_EQ(res, HCF_INVALID_PARAMS);
794
795 HcfObjDestroy(spiObj);
796 }
797
798 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest045, TestSize.Level0)
799 {
800 HcfSignatureParams params = {
801 .algo = HCF_ALG_SM2,
802 .md = HCF_OPENSSL_DIGEST_SM3,
803 };
804 HcfSignSpi *spiObj = nullptr;
805 int32_t res = HcfSignSpiSm2Create(¶ms, &spiObj);
806
807 ASSERT_EQ(res, HCF_SUCCESS);
808 ASSERT_NE(spiObj, nullptr);
809
810 const char *message = "hello world";
811 HcfBlob input = {
812 .data = (uint8_t *)message,
813 .len = 12
814 };
815 res = spiObj->engineUpdate(nullptr, &input);
816 ASSERT_EQ(res, HCF_INVALID_PARAMS);
817
818 HcfObjDestroy(spiObj);
819 }
820
821 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest047, TestSize.Level0)
822 {
823 HcfSignatureParams params = {
824 .algo = HCF_ALG_SM2,
825 .md = HCF_OPENSSL_DIGEST_SM3,
826 };
827 HcfSignSpi *spiObj = nullptr;
828 int32_t res = HcfSignSpiSm2Create(¶ms, &spiObj);
829
830 ASSERT_EQ(res, HCF_SUCCESS);
831 ASSERT_NE(spiObj, nullptr);
832
833 res = spiObj->engineUpdate(spiObj, nullptr);
834 ASSERT_EQ(res, HCF_INVALID_PARAMS);
835
836 HcfObjDestroy(spiObj);
837 }
838
839 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest049, TestSize.Level0)
840 {
841 HcfSignatureParams params = {
842 .algo = HCF_ALG_SM2,
843 .md = HCF_OPENSSL_DIGEST_SM3,
844 };
845 HcfSignSpi *spiObj = nullptr;
846 int32_t res = HcfSignSpiSm2Create(¶ms, &spiObj);
847
848 ASSERT_EQ(res, HCF_SUCCESS);
849 ASSERT_NE(spiObj, nullptr);
850
851 spiObj->base.destroy(nullptr);
852
853 HcfObjDestroy(spiObj);
854 }
855
MemoryMallocTestFunc(uint32_t mallocCount,HcfBlob * input)856 static void MemoryMallocTestFunc(uint32_t mallocCount, HcfBlob *input)
857 {
858 for (uint32_t i = 0; i < mallocCount; i++) {
859 ResetRecordMallocNum();
860 SetMockMallocIndex(i);
861 HcfAsyKeyGenerator *generator = nullptr;
862 int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
863 if (res != HCF_SUCCESS) {
864 continue;
865 }
866 HcfKeyPair *keyPair = nullptr;
867 res = generator->generateKeyPair(generator, nullptr, &keyPair);
868 HcfObjDestroy(generator);
869 if (res != HCF_SUCCESS) {
870 continue;
871 }
872 HcfSign *sign = nullptr;
873 res = HcfSignCreate("SM2|SM3", &sign);
874 if (res != HCF_SUCCESS) {
875 HcfObjDestroy(keyPair);
876 continue;
877 }
878 res = sign->init(sign, nullptr, keyPair->priKey);
879 if (res != HCF_SUCCESS) {
880 HcfObjDestroy(sign);
881 HcfObjDestroy(keyPair);
882 continue;
883 }
884 res = sign->update(sign, input);
885 if (res != HCF_SUCCESS) {
886 HcfObjDestroy(sign);
887 HcfObjDestroy(keyPair);
888 continue;
889 }
890 HcfBlob out = {
891 .data = nullptr,
892 .len = 0
893 };
894 res = sign->sign(sign, nullptr, &out);
895 HcfObjDestroy(sign);
896 HcfObjDestroy(keyPair);
897 if (res == HCF_SUCCESS) {
898 free(out.data);
899 }
900 }
901 }
902
903 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest051, TestSize.Level0)
904 {
905 StartRecordMallocNum();
906 HcfAsyKeyGenerator *generator = nullptr;
907 int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
908 ASSERT_EQ(res, HCF_SUCCESS);
909 ASSERT_NE(generator, nullptr);
910
911 HcfKeyPair *keyPair = nullptr;
912 res = generator->generateKeyPair(generator, nullptr, &keyPair);
913 ASSERT_EQ(res, HCF_SUCCESS);
914 ASSERT_NE(keyPair, nullptr);
915
916 HcfObjDestroy(generator);
917
918 HcfSign *sign = nullptr;
919 res = HcfSignCreate("SM2|SM3", &sign);
920 ASSERT_EQ(res, HCF_SUCCESS);
921 ASSERT_NE(sign, nullptr);
922
923 HcfParamsSpec params;
924 res = sign->init(sign, ¶ms, keyPair->priKey);
925 ASSERT_EQ(res, HCF_SUCCESS);
926
927 const char *message = "hello world";
928 HcfBlob input = {
929 .data = (uint8_t *)message,
930 .len = 12
931 };
932 res = sign->update(sign, &input);
933 ASSERT_EQ(res, HCF_SUCCESS);
934
935 HcfBlob out = { .data = nullptr, .len = 0 };
936 res = sign->sign(sign, nullptr, &out);
937 ASSERT_EQ(res, HCF_SUCCESS);
938 ASSERT_NE(out.data, nullptr);
939 ASSERT_NE(out.len, (const unsigned int)0);
940
941 free(out.data);
942 HcfObjDestroy(sign);
943
944 uint32_t mallocCount = GetMallocNum();
945 MemoryMallocTestFunc(mallocCount, &input);
946
947 EndRecordMallocNum();
948 }
949
OpensslMockTestFunc(uint32_t mallocCount,HcfBlob * input)950 static void OpensslMockTestFunc(uint32_t mallocCount, HcfBlob *input)
951 {
952 for (uint32_t i = 0; i < mallocCount; i++) {
953 ResetOpensslCallNum();
954 SetOpensslCallMockIndex(i);
955 HcfAsyKeyGenerator *generator = nullptr;
956 int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
957 if (res != HCF_SUCCESS) {
958 continue;
959 }
960 HcfKeyPair *keyPair = nullptr;
961 res = generator->generateKeyPair(generator, nullptr, &keyPair);
962 HcfObjDestroy(generator);
963 if (res != HCF_SUCCESS) {
964 continue;
965 }
966 HcfSign *sign = nullptr;
967 res = HcfSignCreate("SM2|SM3", &sign);
968 if (res != HCF_SUCCESS) {
969 HcfObjDestroy(keyPair);
970 continue;
971 }
972 res = sign->init(sign, nullptr, keyPair->priKey);
973 if (res != HCF_SUCCESS) {
974 HcfObjDestroy(sign);
975 HcfObjDestroy(keyPair);
976 continue;
977 }
978 res = sign->update(sign, input);
979 if (res != HCF_SUCCESS) {
980 HcfObjDestroy(sign);
981 HcfObjDestroy(keyPair);
982 continue;
983 }
984 HcfBlob out = {
985 .data = nullptr,
986 .len = 0
987 };
988 res = sign->sign(sign, input, &out);
989 HcfObjDestroy(sign);
990 HcfObjDestroy(keyPair);
991 if (res == HCF_SUCCESS) {
992 free(out.data);
993 }
994 }
995 }
996
997 HWTEST_F(CryptoSm2SignTest, CryptoSm2SignTest052, TestSize.Level0)
998 {
999 StartRecordOpensslCallNum();
1000 HcfAsyKeyGenerator *generator = nullptr;
1001 int32_t res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
1002 ASSERT_EQ(res, HCF_SUCCESS);
1003 ASSERT_NE(generator, nullptr);
1004
1005 HcfKeyPair *keyPair = nullptr;
1006 res = generator->generateKeyPair(generator, nullptr, &keyPair);
1007 ASSERT_EQ(res, HCF_SUCCESS);
1008 ASSERT_NE(keyPair, nullptr);
1009
1010 HcfObjDestroy(generator);
1011
1012 HcfSign *sign = nullptr;
1013 res = HcfSignCreate("SM2|SM3", &sign);
1014 ASSERT_EQ(res, HCF_SUCCESS);
1015 ASSERT_NE(sign, nullptr);
1016
1017 HcfParamsSpec params;
1018 res = sign->init(sign, ¶ms, keyPair->priKey);
1019 ASSERT_EQ(res, HCF_SUCCESS);
1020
1021 const char *message = "hello world";
1022 HcfBlob input = {
1023 .data = (uint8_t *)message,
1024 .len = 12
1025 };
1026 res = sign->update(sign, &input);
1027 ASSERT_EQ(res, HCF_SUCCESS);
1028
1029 HcfBlob out = { .data = nullptr, .len = 0 };
1030 res = sign->sign(sign, &input, &out);
1031 ASSERT_EQ(res, HCF_SUCCESS);
1032 ASSERT_NE(out.data, nullptr);
1033 ASSERT_NE(out.len, (const unsigned int)0);
1034
1035 free(out.data);
1036 HcfObjDestroy(sign);
1037
1038 uint32_t mallocCount = GetOpensslCallNum();
1039 OpensslMockTestFunc(mallocCount, &input);
1040
1041 EndRecordOpensslCallNum();
1042 }
1043 }
1044