1 /*
2  * Copyright (C) 2022-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 <fstream>
18 #include <iostream>
19 #include "securec.h"
20 #include "aes_openssl.h"
21 #include "aes_common.h"
22 #include "blob.h"
23 #include "cipher.h"
24 #include "detailed_iv_params.h"
25 #include "detailed_gcm_params.h"
26 #include "detailed_ccm_params.h"
27 #include "log.h"
28 #include "memory.h"
29 #include "sym_common_defines.h"
30 #include "sym_key_generator.h"
31 #include "sm4_common.h"
32 #include "sm4_openssl.h"
33 
34 using namespace std;
35 using namespace testing::ext;
36 
37 namespace {
38 class CryptoSM4CipherTest : public testing::Test {
39 public:
SetUpTestCase()40     static void SetUpTestCase() {};
TearDownTestCase()41     static void TearDownTestCase() {};
SetUp()42     void SetUp() {};
TearDown()43     void TearDown() {};
44 };
45 
46 HcfObjectBase obj = {
47     .getClass = GetMockClass,
48     .destroy = nullptr
49 };
50 
51 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest004, TestSize.Level0)
52 {
53     int ret = 0;
54     uint8_t cipherText[128] = {0};
55     int cipherTextLen = 128;
56 
57     HcfSymKeyGenerator *generator = nullptr;
58     HcfCipher *cipher = nullptr;
59     HcfSymKey *key = nullptr;
60 
61 
62     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
63     if (ret != 0) {
64         LOGE("HcfSymKeyGeneratorCreate failed!");
65         goto clearup;
66     }
67 
68     ret = generator->generateSymKey(generator, &key);
69     if (ret != 0) {
70         LOGE("generateSymKey failed!");
71         goto clearup;
72     }
73 
74     ret = HcfCipherCreate("SM4_128|CBC|NoPadding", &cipher);
75     if (ret != 0) {
76         LOGE("HcfCipherCreate failed!");
77         goto clearup;
78     }
79 
80     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
81     if (ret != 0) {
82         LOGE("Sm4Encrypt failed! ");
83         goto clearup;
84     }
85 
86     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
87     if (ret != 0) {
88         LOGE("Sm4Decrypt failed! ");
89         goto clearup;
90     }
91 
92 clearup:
93     HcfObjDestroy((HcfObjectBase *)key);
94     HcfObjDestroy((HcfObjectBase *)cipher);
95     HcfObjDestroy((HcfObjectBase *)generator);
96     EXPECT_NE(ret, 0);
97 }
98 
99 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest005, TestSize.Level0)
100 {
101     int ret = 0;
102     uint8_t cipherText[128] = {0};
103     int cipherTextLen = 128;
104 
105     HcfSymKeyGenerator *generator = nullptr;
106     HcfCipher *cipher = nullptr;
107     HcfSymKey *key = nullptr;
108 
109 
110     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
111     if (ret != 0) {
112         LOGE("HcfSymKeyGeneratorCreate failed!");
113         goto clearup;
114     }
115 
116     ret = generator->generateSymKey(generator, &key);
117     if (ret != 0) {
118         LOGE("generateSymKey failed!");
119         goto clearup;
120     }
121 
122     ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher);
123     if (ret != 0) {
124         LOGE("HcfCipherCreate failed!");
125         goto clearup;
126     }
127 
128     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
129     if (ret != 0) {
130         LOGE("Sm4Encrypt failed! ");
131         goto clearup;
132     }
133     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
134     if (ret != 0) {
135         LOGE("Sm4Decrypt failed! ");
136         goto clearup;
137     }
138 
139 clearup:
140     HcfObjDestroy((HcfObjectBase *)key);
141     HcfObjDestroy((HcfObjectBase *)cipher);
142     HcfObjDestroy((HcfObjectBase *)generator);
143     EXPECT_EQ(ret, 0);
144 }
145 
146 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest006, TestSize.Level0)
147 {
148     int ret = 0;
149     uint8_t cipherText[128] = {0};
150     int cipherTextLen = 128;
151 
152     HcfSymKeyGenerator *generator = nullptr;
153     HcfCipher *cipher = nullptr;
154     HcfSymKey *key = nullptr;
155 
156     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
157     if (ret != 0) {
158         LOGE("HcfSymKeyGeneratorCreate failed!");
159         goto clearup;
160     }
161 
162     ret = generator->generateSymKey(generator, &key);
163     if (ret != 0) {
164         LOGE("generateSymKey failed!");
165         goto clearup;
166     }
167 
168     ret = HcfCipherCreate("SM4_128|CBC|PKCS7", &cipher);
169     if (ret != 0) {
170         LOGE("HcfCipherCreate failed!");
171         goto clearup;
172     }
173     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
174     if (ret != 0) {
175         LOGE("Sm4Encrypt failed! ");
176         goto clearup;
177     }
178 
179     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
180     if (ret != 0) {
181         LOGE("Sm4Decrypt failed! ");
182         goto clearup;
183     }
184 
185 clearup:
186     HcfObjDestroy((HcfObjectBase *)key);
187     HcfObjDestroy((HcfObjectBase *)cipher);
188     HcfObjDestroy((HcfObjectBase *)generator);
189     EXPECT_EQ(ret, 0);
190 }
191 
192 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest007, TestSize.Level0)
193 {
194     int ret = 0;
195     uint8_t cipherText[128] = {0};
196     int cipherTextLen = 128;
197 
198     HcfSymKeyGenerator *generator = nullptr;
199     HcfCipher *cipher = nullptr;
200     HcfSymKey *key = nullptr;
201 
202 
203     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
204     if (ret != 0) {
205         LOGE("HcfSymKeyGeneratorCreate failed!");
206         goto clearup;
207     }
208 
209     ret = generator->generateSymKey(generator, &key);
210     if (ret != 0) {
211         LOGE("generateSymKey failed!");
212         goto clearup;
213     }
214 
215     ret = HcfCipherCreate("SM4_128|OFB|NoPadding", &cipher);
216     if (ret != 0) {
217         LOGE("HcfCipherCreate failed!");
218         goto clearup;
219     }
220 
221     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
222     if (ret != 0) {
223         LOGE("Sm4Encrypt failed! ");
224         goto clearup;
225     }
226 
227     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
228     if (ret != 0) {
229         LOGE("Sm4Decrypt failed! ");
230         goto clearup;
231     }
232 
233 
234 clearup:
235     HcfObjDestroy((HcfObjectBase *)key);
236     HcfObjDestroy((HcfObjectBase *)cipher);
237     HcfObjDestroy((HcfObjectBase *)generator);
238     EXPECT_EQ(ret, 0);
239 }
240 
241 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest008, TestSize.Level0)
242 {
243     int ret = 0;
244     uint8_t cipherText[128] = {0};
245     int cipherTextLen = 128;
246 
247     HcfSymKeyGenerator *generator = nullptr;
248     HcfCipher *cipher = nullptr;
249     HcfSymKey *key = nullptr;
250 
251     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
252     if (ret != 0) {
253         LOGE("HcfSymKeyGeneratorCreate failed!");
254         goto clearup;
255     }
256 
257     ret = generator->generateSymKey(generator, &key);
258     if (ret != 0) {
259         LOGE("generateSymKey failed!");
260         goto clearup;
261     }
262 
263     ret = HcfCipherCreate("SM4_128|OFB|PKCS5", &cipher);
264     if (ret != 0) {
265         LOGE("HcfCipherCreate failed!");
266         goto clearup;
267     }
268     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
269     if (ret != 0) {
270         LOGE("Sm4Encrypt failed! ");
271         goto clearup;
272     }
273 
274     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
275     if (ret != 0) {
276         LOGE("Sm4Decrypt failed! ");
277         goto clearup;
278     }
279 
280 clearup:
281     HcfObjDestroy((HcfObjectBase *)key);
282     HcfObjDestroy((HcfObjectBase *)cipher);
283     HcfObjDestroy((HcfObjectBase *)generator);
284     EXPECT_EQ(ret, 0);
285 }
286 
287 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest009, TestSize.Level0)
288 {
289     int ret = 0;
290     uint8_t cipherText[128] = {0};
291     int cipherTextLen = 128;
292 
293     HcfSymKeyGenerator *generator = nullptr;
294     HcfCipher *cipher = nullptr;
295     HcfSymKey *key = nullptr;
296 
297     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
298     if (ret != 0) {
299         LOGE("HcfSymKeyGeneratorCreate failed!");
300         goto clearup;
301     }
302 
303     ret = generator->generateSymKey(generator, &key);
304     if (ret != 0) {
305         LOGE("generateSymKey failed!");
306         goto clearup;
307     }
308 
309     ret = HcfCipherCreate("SM4_128|OFB|PKCS7", &cipher);
310     if (ret != 0) {
311         LOGE("HcfCipherCreate failed!");
312         goto clearup;
313     }
314 
315     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
316     if (ret != 0) {
317         LOGE("Sm4Encrypt failed! ");
318         goto clearup;
319     }
320 
321     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
322     if (ret != 0) {
323         LOGE("Sm4Decrypt failed! ");
324         goto clearup;
325     }
326 
327 
328 clearup:
329     HcfObjDestroy((HcfObjectBase *)key);
330     HcfObjDestroy((HcfObjectBase *)cipher);
331     HcfObjDestroy((HcfObjectBase *)generator);
332     EXPECT_EQ(ret, 0);
333 }
334 
335 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest016, TestSize.Level0)
336 {
337     int ret = 0;
338     uint8_t cipherText[128] = {0};
339     int cipherTextLen = 128;
340 
341     HcfSymKeyGenerator *generator = nullptr;
342     HcfCipher *cipher = nullptr;
343     HcfSymKey *key = nullptr;
344 
345 
346     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
347     if (ret != 0) {
348         LOGE("HcfSymKeyGeneratorCreate failed!");
349         goto clearup;
350     }
351 
352     ret = generator->generateSymKey(generator, &key);
353     if (ret != 0) {
354         LOGE("generateSymKey failed!");
355         goto clearup;
356     }
357 
358     ret = HcfCipherCreate("SM4_128|CTR|NoPadding", &cipher);
359     if (ret != 0) {
360         LOGE("HcfCipherCreate failed!");
361         goto clearup;
362     }
363 
364     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
365     if (ret != 0) {
366         LOGE("Sm4Encrypt failed! ");
367         goto clearup;
368     }
369 
370     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
371     if (ret != 0) {
372         LOGE("Sm4Decrypt failed! ");
373         goto clearup;
374     }
375 
376 clearup:
377     HcfObjDestroy((HcfObjectBase *)key);
378     HcfObjDestroy((HcfObjectBase *)cipher);
379     HcfObjDestroy((HcfObjectBase *)generator);
380     EXPECT_EQ(ret, 0);
381 }
382 
383 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest017, TestSize.Level0)
384 {
385     int ret = 0;
386     uint8_t cipherText[128] = {0};
387     int cipherTextLen = 128;
388 
389     HcfSymKeyGenerator *generator = nullptr;
390     HcfCipher *cipher = nullptr;
391     HcfSymKey *key = nullptr;
392 
393     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
394     if (ret != 0) {
395         LOGE("HcfSymKeyGeneratorCreate failed!");
396         goto clearup;
397     }
398 
399     ret = generator->generateSymKey(generator, &key);
400     if (ret != 0) {
401         LOGE("generateSymKey failed!");
402         goto clearup;
403     }
404 
405     ret = HcfCipherCreate("SM4_128|CTR|PKCS5", &cipher);
406     if (ret != 0) {
407         LOGE("HcfCipherCreate failed!");
408         goto clearup;
409     }
410 
411     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
412     if (ret != 0) {
413         LOGE("Sm4Encrypt failed! ");
414         goto clearup;
415     }
416 
417     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
418     if (ret != 0) {
419         LOGE("Sm4Decrypt failed! ");
420         goto clearup;
421     }
422 
423 clearup:
424     HcfObjDestroy((HcfObjectBase *)key);
425     HcfObjDestroy((HcfObjectBase *)cipher);
426     HcfObjDestroy((HcfObjectBase *)generator);
427     EXPECT_EQ(ret, 0);
428 }
429 
430 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest018, TestSize.Level0)
431 {
432     int ret = 0;
433     uint8_t cipherText[128] = {0};
434     int cipherTextLen = 128;
435 
436     HcfSymKeyGenerator *generator = nullptr;
437     HcfCipher *cipher = nullptr;
438     HcfSymKey *key = nullptr;
439 
440     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
441     if (ret != 0) {
442         LOGE("HcfSymKeyGeneratorCreate failed!");
443         goto clearup;
444     }
445 
446     ret = generator->generateSymKey(generator, &key);
447     if (ret != 0) {
448         LOGE("generateSymKey failed!");
449         goto clearup;
450     }
451 
452     ret = HcfCipherCreate("SM4_128|CTR|PKCS7", &cipher);
453     if (ret != 0) {
454         LOGE("HcfCipherCreate failed!");
455         goto clearup;
456     }
457 
458     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
459     if (ret != 0) {
460         LOGE("Sm4Encrypt failed! ");
461         goto clearup;
462     }
463 
464     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
465     if (ret != 0) {
466         LOGE("Sm4Decrypt failed! ");
467         goto clearup;
468     }
469 
470 clearup:
471     HcfObjDestroy((HcfObjectBase *)key);
472     HcfObjDestroy((HcfObjectBase *)cipher);
473     HcfObjDestroy((HcfObjectBase *)generator);
474     EXPECT_EQ(ret, 0);
475 }
476 
477 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest022, TestSize.Level0)
478 {
479     int ret = 0;
480     uint8_t cipherText[128] = {0};
481     int cipherTextLen = 128;
482 
483     HcfSymKeyGenerator *generator = nullptr;
484     HcfCipher *cipher = nullptr;
485     HcfSymKey *key = nullptr;
486 
487 
488     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
489     if (ret != 0) {
490         LOGE("HcfSymKeyGeneratorCreate failed!");
491         goto clearup;
492     }
493 
494     ret = generator->generateSymKey(generator, &key);
495     if (ret != 0) {
496         LOGE("generateSymKey failed!");
497         goto clearup;
498     }
499 
500     ret = HcfCipherCreate("SM4_128|CBC|NoPadding", &cipher);
501     if (ret != 0) {
502         LOGE("HcfCipherCreate failed!");
503         goto clearup;
504     }
505 
506     ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
507     if (ret != 0) {
508         LOGE("Sm4NoUpdateEncrypt failed! ");
509         goto clearup;
510     }
511 
512     ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
513     if (ret != 0) {
514         LOGE("Sm4NoUpdateDecrypt failed! ");
515         goto clearup;
516     }
517 
518 clearup:
519     HcfObjDestroy((HcfObjectBase *)key);
520     HcfObjDestroy((HcfObjectBase *)cipher);
521     HcfObjDestroy((HcfObjectBase *)generator);
522     EXPECT_NE(ret, 0);
523 }
524 
525 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest023, TestSize.Level0)
526 {
527     int ret = 0;
528     uint8_t cipherText[128] = {0};
529     int cipherTextLen = 128;
530 
531     HcfSymKeyGenerator *generator = nullptr;
532     HcfCipher *cipher = nullptr;
533     HcfSymKey *key = nullptr;
534 
535     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
536     if (ret != 0) {
537         LOGE("HcfSymKeyGeneratorCreate failed!");
538         goto clearup;
539     }
540 
541     ret = generator->generateSymKey(generator, &key);
542     if (ret != 0) {
543         LOGE("generateSymKey failed!");
544         goto clearup;
545     }
546 
547     ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher);
548     if (ret != 0) {
549         LOGE("HcfCipherCreate failed!");
550         goto clearup;
551     }
552 
553     ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
554     if (ret != 0) {
555         LOGE("Sm4NoUpdateEncrypt failed! ");
556         goto clearup;
557     }
558 
559     ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
560     if (ret != 0) {
561         LOGE("Sm4NoUpdateDecrypt failed! ");
562         goto clearup;
563     }
564 
565 clearup:
566     HcfObjDestroy((HcfObjectBase *)key);
567     HcfObjDestroy((HcfObjectBase *)cipher);
568     HcfObjDestroy((HcfObjectBase *)generator);
569     EXPECT_EQ(ret, 0);
570 }
571 
572 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest024, TestSize.Level0)
573 {
574     int ret = 0;
575     uint8_t cipherText[128] = {0};
576     int cipherTextLen = 128;
577 
578     HcfSymKeyGenerator *generator = nullptr;
579     HcfCipher *cipher = nullptr;
580     HcfSymKey *key = nullptr;
581 
582 
583     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
584     if (ret != 0) {
585         LOGE("HcfSymKeyGeneratorCreate failed!");
586         goto clearup;
587     }
588 
589     ret = generator->generateSymKey(generator, &key);
590     if (ret != 0) {
591         LOGE("generateSymKey failed!");
592         goto clearup;
593     }
594 
595     ret = HcfCipherCreate("SM4_128|CBC|PKCS7", &cipher);
596     if (ret != 0) {
597         LOGE("HcfCipherCreate failed!");
598         goto clearup;
599     }
600 
601     ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
602     if (ret != 0) {
603         LOGE("Sm4NoUpdateEncrypt failed! ");
604         goto clearup;
605     }
606 
607     ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
608     if (ret != 0) {
609         LOGE("Sm4NoUpdateDecrypt failed! ");
610         goto clearup;
611     }
612 
613 clearup:
614     HcfObjDestroy((HcfObjectBase *)key);
615     HcfObjDestroy((HcfObjectBase *)cipher);
616     HcfObjDestroy((HcfObjectBase *)generator);
617     EXPECT_EQ(ret, 0);
618 }
619 
620 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest025, TestSize.Level0)
621 {
622     int ret = 0;
623     uint8_t cipherText[128] = {0};
624     int cipherTextLen = 128;
625 
626     HcfSymKeyGenerator *generator = nullptr;
627     HcfCipher *cipher = nullptr;
628     HcfSymKey *key = nullptr;
629 
630 
631     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
632     if (ret != 0) {
633         LOGE("HcfSymKeyGeneratorCreate failed!");
634         goto clearup;
635     }
636 
637     ret = generator->generateSymKey(generator, &key);
638     if (ret != 0) {
639         LOGE("generateSymKey failed!");
640         goto clearup;
641     }
642 
643     ret = HcfCipherCreate("SM4_128|OFB|NoPadding", &cipher);
644     if (ret != 0) {
645         LOGE("HcfCipherCreate failed!");
646         goto clearup;
647     }
648 
649     ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
650     if (ret != 0) {
651         LOGE("Sm4NoUpdateEncrypt failed! ");
652         goto clearup;
653     }
654 
655     ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
656     if (ret != 0) {
657         LOGE("Sm4NoUpdateDecrypt failed! ");
658         goto clearup;
659     }
660 
661 
662 clearup:
663     HcfObjDestroy((HcfObjectBase *)key);
664     HcfObjDestroy((HcfObjectBase *)cipher);
665     HcfObjDestroy((HcfObjectBase *)generator);
666     EXPECT_EQ(ret, 0);
667 }
668 
669 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest026, TestSize.Level0)
670 {
671     int ret = 0;
672     uint8_t cipherText[128] = {0};
673     int cipherTextLen = 128;
674 
675     HcfSymKeyGenerator *generator = nullptr;
676     HcfCipher *cipher = nullptr;
677     HcfSymKey *key = nullptr;
678 
679 
680     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
681     if (ret != 0) {
682         LOGE("HcfSymKeyGeneratorCreate failed!");
683         goto clearup;
684     }
685 
686     ret = generator->generateSymKey(generator, &key);
687     if (ret != 0) {
688         LOGE("generateSymKey failed!");
689         goto clearup;
690     }
691 
692     ret = HcfCipherCreate("SM4_128|OFB|PKCS5", &cipher);
693     if (ret != 0) {
694         LOGE("HcfCipherCreate failed!");
695         goto clearup;
696     }
697 
698     ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
699     if (ret != 0) {
700         LOGE("Sm4NoUpdateEncrypt failed! ");
701         goto clearup;
702     }
703 
704     ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
705     if (ret != 0) {
706         LOGE("Sm4NoUpdateDecrypt failed! ");
707         goto clearup;
708     }
709 
710 clearup:
711     HcfObjDestroy((HcfObjectBase *)key);
712     HcfObjDestroy((HcfObjectBase *)cipher);
713     HcfObjDestroy((HcfObjectBase *)generator);
714     EXPECT_EQ(ret, 0);
715 }
716 
717 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest027, TestSize.Level0)
718 {
719     int ret = 0;
720     uint8_t cipherText[128] = {0};
721     int cipherTextLen = 128;
722 
723     HcfSymKeyGenerator *generator = nullptr;
724     HcfCipher *cipher = nullptr;
725     HcfSymKey *key = nullptr;
726 
727     ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
728     if (ret != 0) {
729         LOGE("HcfSymKeyGeneratorCreate failed!");
730         goto clearup;
731     }
732 
733     ret = generator->generateSymKey(generator, &key);
734     if (ret != 0) {
735         LOGE("generateSymKey failed!");
736         goto clearup;
737     }
738 
739     ret = HcfCipherCreate("SM4_128|OFB|PKCS7", &cipher);
740     if (ret != 0) {
741         LOGE("HcfCipherCreate failed!");
742         goto clearup;
743     }
744 
745     ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
746     if (ret != 0) {
747         LOGE("Sm4NoUpdateEncrypt failed! ");
748         goto clearup;
749     }
750 
751     ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
752     if (ret != 0) {
753         LOGE("Sm4NoUpdateDecrypt failed! ");
754         goto clearup;
755     }
756 
757 
758 clearup:
759     HcfObjDestroy((HcfObjectBase *)key);
760     HcfObjDestroy((HcfObjectBase *)cipher);
761     HcfObjDestroy((HcfObjectBase *)generator);
762     EXPECT_EQ(ret, 0);
763 }
764 
765 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest044, TestSize.Level0)
766 {
767     int ret = 0;
768     HcfCipher *cipher = nullptr;
769 
770     ret = HcfCipherCreate("", &cipher);
771     if (ret != 0) {
772         LOGE("HcfCipherCreate failed!");
773     }
774 
775     HcfObjDestroy(cipher);
776     EXPECT_NE(ret, 0);
777 }
778 
779 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest045, TestSize.Level0)
780 {
781     int ret = 0;
782     HcfCipher *cipher = nullptr;
783 
784     ret = HcfCipherCreate(nullptr, &cipher);
785     if (ret != 0) {
786         LOGE("HcfCipherCreate failed!");
787     }
788 
789     HcfObjDestroy(cipher);
790     EXPECT_NE(ret, 0);
791 }
792 
793 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest052, TestSize.Level0)
794 {
795     int ret = 0;
796     HcfCipher *cipher = nullptr;
797 
798     ret = HcfCipherCreate("SM4_128|CCC|NoPadding", &cipher);
799     if (ret != 0) {
800         LOGE("HcfCipherCreate failed! Should not select CCC for SM4 generator.");
801     }
802 
803     HcfObjDestroy(cipher);
804     EXPECT_NE(ret, 0);
805 }
806 
807 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest055, TestSize.Level0)
808 {
809     int ret = 0;
810     uint8_t iv[AES_IV_LEN] = { 0 };
811     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
812     int cipherTextLen = CIPHER_TEXT_LEN;
813 
814     HcfIvParamsSpec ivSpec = {};
815     HcfCipher *cipher = nullptr;
816     HcfSymKey *key = nullptr;
817     ivSpec.iv.data = iv;
818     ivSpec.iv.len = AES_IV_LEN;
819 
820     ret = GenerateSymKeyForSm4("SM4_128", &key);
821     if (ret != 0) {
822         LOGE("GenerateSymKeyForSm4 failed!");
823         goto CLEAR_UP;
824     }
825 
826     ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher);
827     if (ret != 0) {
828         LOGE("HcfCipherCreate failed!");
829         goto CLEAR_UP;
830     }
831 
832     ret = Sm4Encrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
833     if (ret != 0) {
834         LOGE("AesEncrypt failed! %d", ret);
835         goto CLEAR_UP;
836     }
837 
838     ret = Sm4Decrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
839     if (ret != 0) {
840         LOGE("AesDecrypt failed! %d", ret);
841     }
842 
843 CLEAR_UP:
844     HcfObjDestroy(key);
845     HcfObjDestroy(cipher);
846     EXPECT_EQ(ret, 0);
847 }
848 
849 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest057, TestSize.Level0)
850 {
851     int ret = 0;
852     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
853     int cipherTextLen = CIPHER_TEXT_LEN;
854     HcfCipher *cipher = nullptr;
855     HcfSymKey *key = nullptr;
856 
857     ret = GenerateSymKeyForSm4("SM4_128", &key);
858     if (ret != 0) {
859         LOGE("GenerateSymKeyForSm4 failed!");
860         goto CLEAR_UP;
861     }
862 
863     // allow input without encryption mode. It will use default aes128ecb.
864     ret = HcfCipherCreate("SM4_128|PKCS5", &cipher);
865     if (ret != 0) {
866         LOGE("HcfCipherCreate failed!");
867         goto CLEAR_UP;
868     }
869 
870     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
871     if (ret != 0) {
872         LOGE("AesEncrypt failed! %d", ret);
873         goto CLEAR_UP;
874     }
875 
876     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
877     if (ret != 0) {
878         LOGE("AesDecrypt failed! %d", ret);
879     }
880 CLEAR_UP:
881     HcfObjDestroy(key);
882     HcfObjDestroy(cipher);
883     EXPECT_EQ(ret, 0);
884 }
885 
886 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest060, TestSize.Level0)
887 {
888     int ret = 0;
889     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
890     int cipherTextLen = CIPHER_TEXT_LEN;
891     HcfCipher *cipher = nullptr;
892     HcfSymKey *key = nullptr;
893 
894     ret = GenerateSymKeyForSm4("SM4_128", &key);
895     if (ret != 0) {
896         LOGE("GenerateSymKeyForSm4 failed!");
897         goto CLEAR_UP;
898     }
899 
900     // allow input without encryption mode. It will pick the last PKCS5, and use default aes128ecb.
901     ret = HcfCipherCreate("SM4_128|NoPadding|PKCS5", &cipher);
902     if (ret != 0) {
903         LOGE("HcfCipherCreate failed!");
904         goto CLEAR_UP;
905     }
906 
907     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
908     if (ret != 0) {
909         LOGE("AesEncrypt failed! %d", ret);
910         goto CLEAR_UP;
911     }
912 
913     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
914     if (ret != 0) {
915         LOGE("AesDecrypt failed! %d", ret);
916     }
917 CLEAR_UP:
918     HcfObjDestroy(key);
919     HcfObjDestroy(cipher);
920     EXPECT_EQ(ret, 0);
921 }
922 
923 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest062, TestSize.Level0)
924 {
925     HcfResult ret = HCF_SUCCESS;
926 
927     ret = HcfCipherSm4GeneratorSpiCreate(nullptr, nullptr);
928     EXPECT_NE(ret, HCF_SUCCESS);
929 
930     HcfCipherGeneratorSpi *cipher = nullptr;
931     CipherAttr params = {
932         .algo = HCF_ALG_SM4,
933         .mode = HCF_ALG_MODE_ECB,
934         .paddingMode = HCF_ALG_PADDING_PKCS5,
935     };
936     ret = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
937     EXPECT_EQ(ret, HCF_SUCCESS);
938 
939     ret = cipher->init(nullptr, ENCRYPT_MODE, nullptr, nullptr);
940     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
941 
942     ret = cipher->update(nullptr, nullptr, nullptr);
943     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
944 
945     ret = cipher->doFinal(nullptr, nullptr, nullptr);
946     EXPECT_EQ(ret, HCF_INVALID_PARAMS);
947 
948     HcfBlob dataArray = { .data = nullptr, .len = 0 };
949     ret = cipher->getCipherSpecString(nullptr, OAEP_MGF1_MD_STR, nullptr);
950     EXPECT_EQ(ret, HCF_NOT_SUPPORT);
951 
952     ret = cipher->getCipherSpecUint8Array(nullptr, OAEP_MGF1_MD_STR, &dataArray);
953     EXPECT_EQ(ret, HCF_NOT_SUPPORT);
954 
955     HcfBlob dataUint8 = { .data = nullptr, .len = 0 };
956     ret = cipher->setCipherSpecUint8Array(nullptr, OAEP_MGF1_MD_STR, dataUint8);
957     EXPECT_EQ(ret, HCF_NOT_SUPPORT);
958 
959     (void)cipher->base.destroy(nullptr);
960 
961     HcfObjDestroy(cipher);
962     HcfBlobDataFree(&dataArray);
963 }
964 
965 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest063, TestSize.Level0)
966 {
967     HcfResult res = HCF_SUCCESS;
968     HcfCipherGeneratorSpi *cipher = nullptr;
969     CipherAttr params = {
970         .algo = HCF_ALG_SM4,
971         .md = HCF_OPENSSL_DIGEST_SM3,
972     };
973     res = HcfCipherSm4GeneratorSpiCreate(&params, nullptr);
974     EXPECT_NE(res, HCF_SUCCESS);
975     HcfObjDestroy(cipher);
976 }
977 
978 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest064, TestSize.Level0)
979 {
980     int retkey = 0;
981     HcfResult res = HCF_SUCCESS;
982     HcfCipherGeneratorSpi *cipher = nullptr;
983     HcfSymKey *key = nullptr;
984     CipherAttr params = {
985         .algo = HCF_ALG_SM4,
986         .md = HCF_OPENSSL_DIGEST_SM3,
987     };
988     retkey = GenerateSymKeyForSm4("SM4_128", &key);
989     EXPECT_EQ(retkey, 0);
990     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
991     EXPECT_EQ(res, HCF_SUCCESS);
992     res = cipher->init((HcfCipherGeneratorSpi *)(&obj), ENCRYPT_MODE, (HcfKey *)key, nullptr);
993     ASSERT_EQ(res, HCF_INVALID_PARAMS);
994 
995     HcfObjDestroy(cipher);
996 }
997 
998 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest065, TestSize.Level0)
999 {
1000     int retkey = 0;
1001     HcfResult res = HCF_SUCCESS;
1002     HcfCipherGeneratorSpi *cipher = nullptr;
1003     HcfSymKey *key = nullptr;
1004     CipherAttr params = {
1005         .algo = HCF_ALG_SM4,
1006         .md = HCF_OPENSSL_DIGEST_SM3,
1007     };
1008     retkey = GenerateSymKeyForSm4("SM4_128", &key);
1009     EXPECT_EQ(retkey, 0);
1010     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1011     EXPECT_EQ(res, HCF_SUCCESS);
1012     res = cipher->init(nullptr, ENCRYPT_MODE, (HcfKey *)key, nullptr);
1013     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1014 
1015     HcfObjDestroy(cipher);
1016 }
1017 
1018 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest066, TestSize.Level0)
1019 {
1020     HcfResult res = HCF_SUCCESS;
1021     HcfCipherGeneratorSpi *cipher = nullptr;
1022     CipherAttr params = {
1023         .algo = HCF_ALG_SM4,
1024         .md = HCF_OPENSSL_DIGEST_SM3,
1025     };
1026     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1027     EXPECT_EQ(res, HCF_SUCCESS);
1028     res = cipher->init(cipher, ENCRYPT_MODE, nullptr, nullptr);
1029     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1030 
1031     HcfObjDestroy(cipher);
1032 }
1033 
1034 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest067, TestSize.Level0)
1035 {
1036     int retkey = 0;
1037     HcfResult res = HCF_SUCCESS;
1038     HcfCipherGeneratorSpi *cipher = nullptr;
1039     HcfSymKey *key = nullptr;
1040     CipherAttr params = {
1041         .algo = HCF_ALG_SM4,
1042         .md = HCF_OPENSSL_DIGEST_SM3,
1043     };
1044     retkey = GenerateSymKeyForSm4("SM4_128", &key);
1045     EXPECT_EQ(retkey, 0);
1046     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1047     EXPECT_EQ(res, HCF_SUCCESS);
1048     cipher->base.destroy(nullptr);
1049 
1050     HcfObjDestroy(cipher);
1051 }
1052 
1053 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest069, TestSize.Level0)
1054 {
1055     int retkey = 0;
1056     HcfResult res = HCF_SUCCESS;
1057     HcfCipherGeneratorSpi *cipher = nullptr;
1058     HcfSymKey *key = nullptr;
1059     CipherAttr params = {
1060         .algo = HCF_ALG_SM4,
1061         .md = HCF_OPENSSL_DIGEST_SM3,
1062     };
1063 
1064     uint8_t plan[] = "12312123123";
1065     HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)};
1066 
1067     retkey = GenerateSymKeyForSm4("SM4_128", &key);
1068     EXPECT_EQ(retkey, 0);
1069     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1070     EXPECT_EQ(res, HCF_SUCCESS);
1071     HcfBlob blob;
1072     res = cipher->update(nullptr, &input, &blob);
1073     EXPECT_NE(res, 0);
1074 
1075     HcfObjDestroy(cipher);
1076 }
1077 
1078 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest070, TestSize.Level0)
1079 {
1080     int retkey = 0;
1081     HcfResult res = HCF_SUCCESS;
1082     HcfCipherGeneratorSpi *cipher = nullptr;
1083     HcfSymKey *key = nullptr;
1084     CipherAttr params = {
1085         .algo = HCF_ALG_SM4,
1086         .md = HCF_OPENSSL_DIGEST_SM3,
1087     };
1088 
1089     retkey = GenerateSymKeyForSm4("SM4_128", &key);
1090     EXPECT_EQ(retkey, 0);
1091     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1092     EXPECT_EQ(res, HCF_SUCCESS);
1093     HcfBlob blob;
1094     res = cipher->update(cipher, nullptr, &blob);
1095     EXPECT_NE(res, 0);
1096 
1097     HcfObjDestroy(cipher);
1098 }
1099 
1100 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest071, TestSize.Level0)
1101 {
1102     int retkey = 0;
1103     HcfResult res = HCF_SUCCESS;
1104     HcfCipherGeneratorSpi *cipher = nullptr;
1105     HcfSymKey *key = nullptr;
1106     CipherAttr params = {
1107         .algo = HCF_ALG_SM4,
1108         .md = HCF_OPENSSL_DIGEST_SM3,
1109     };
1110 
1111     uint8_t plan[] = "12312123123";
1112     HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)};
1113 
1114     retkey = GenerateSymKeyForSm4("SM4_128", &key);
1115     EXPECT_EQ(retkey, 0);
1116     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1117     EXPECT_EQ(res, HCF_SUCCESS);
1118     res = cipher->update(cipher, &input, nullptr);
1119     EXPECT_NE(res, 0);
1120 
1121     HcfObjDestroy(cipher);
1122 }
1123 
1124 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest073, TestSize.Level0)
1125 {
1126     HcfResult res = HCF_SUCCESS;
1127     HcfCipherGeneratorSpi *cipher = nullptr;
1128     CipherAttr params = {
1129         .algo = HCF_ALG_SM2,
1130         .md = HCF_OPENSSL_DIGEST_SM3,
1131     };
1132     uint8_t plan[] = "12312123123";
1133     HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)};
1134     HcfBlob out = { .data = nullptr, .len = 0 };
1135     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1136     EXPECT_EQ(res, HCF_SUCCESS);
1137     res = cipher->doFinal(nullptr, &input, &out);
1138     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1139 
1140     HcfObjDestroy(cipher);
1141 }
1142 
1143 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest074, TestSize.Level0)
1144 {
1145     HcfResult res = HCF_SUCCESS;
1146     HcfCipherGeneratorSpi *cipher = nullptr;
1147     CipherAttr params = {
1148         .algo = HCF_ALG_SM2,
1149         .md = HCF_OPENSSL_DIGEST_SM3,
1150     };
1151     uint8_t plan[] = "12312123123";
1152     HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)};
1153 
1154     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1155     EXPECT_EQ(res, HCF_SUCCESS);
1156     res = cipher->doFinal(cipher, &input, nullptr);
1157     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1158 
1159     HcfObjDestroy(cipher);
1160 }
1161 
1162 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest075, TestSize.Level0)
1163 {
1164     HcfResult res = HCF_SUCCESS;
1165     HcfCipherGeneratorSpi *cipher = nullptr;
1166     CipherAttr params = {
1167         .algo = HCF_ALG_SM2,
1168         .md = HCF_OPENSSL_DIGEST_SM3,
1169     };
1170     HcfBlob input = {
1171         .data = nullptr,
1172         .len = 12
1173     };
1174     HcfBlob out = { .data = nullptr, .len = 0 };
1175     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1176     EXPECT_EQ(res, HCF_SUCCESS);
1177     res = cipher->doFinal(cipher, &input, &out);
1178     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1179 
1180     HcfObjDestroy(cipher);
1181 }
1182 
1183 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest077, TestSize.Level0)
1184 {
1185     HcfResult res = HCF_SUCCESS;
1186     HcfCipherGeneratorSpi *cipher = nullptr;
1187     CipherAttr params = {
1188         .algo = HCF_ALG_SM2,
1189         .md = HCF_OPENSSL_DIGEST_SM3,
1190     };
1191     uint8_t plan[] = "12312123123";
1192     HcfBlob input = {
1193         .data = (uint8_t *)plan,
1194         .len = -1
1195     };
1196     HcfBlob out = { .data = nullptr, .len = 0 };
1197     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1198     EXPECT_EQ(res, HCF_SUCCESS);
1199     res = cipher->doFinal(cipher, &input, &out);
1200     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1201 
1202     HcfObjDestroy(cipher);
1203 }
1204 
1205 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest078, TestSize.Level0)
1206 {
1207     HcfResult res = HCF_SUCCESS;
1208     HcfCipherGeneratorSpi *cipher = nullptr;
1209     CipherAttr params = {
1210         .algo = HCF_ALG_SM2,
1211         .md = HCF_OPENSSL_DIGEST_SM3,
1212     };
1213     HcfBlob input = {
1214         .data = nullptr,
1215         .len = 12
1216     };
1217     HcfBlob out = { .data = nullptr, .len = 0 };
1218     res = HcfCipherSm4GeneratorSpiCreate(&params, &cipher);
1219     EXPECT_EQ(res, HCF_SUCCESS);
1220     res = cipher->update(cipher, &input, &out);
1221     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1222 
1223     HcfObjDestroy(cipher);
1224 }
1225 
1226 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest079, TestSize.Level0)
1227 {
1228     int ret = 0;
1229     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
1230     int cipherTextLen = CIPHER_TEXT_LEN;
1231     HcfCipher *cipher = nullptr;
1232     HcfSymKey *key = nullptr;
1233 
1234     ret = GenerateSymKeyForSm4("AES256", &key);
1235     if (ret != 0) {
1236         LOGE("GenerateSymKeyForSm4 failed!");
1237         goto CLEAR_UP;
1238     }
1239 
1240     ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher);
1241     if (ret != 0) {
1242         LOGE("HcfCipherCreate failed!");
1243         goto CLEAR_UP;
1244     }
1245 
1246     // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key.
1247     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
1248     if (ret != 0) {
1249         LOGE("AesEncrypt failed! %d", ret);
1250         goto CLEAR_UP;
1251     }
1252 
1253     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
1254     if (ret != 0) {
1255         LOGE("AesDecrypt failed! %d", ret);
1256     }
1257 
1258 CLEAR_UP:
1259     HcfObjDestroy(key);
1260     HcfObjDestroy(cipher);
1261     EXPECT_NE(ret, 0);
1262 }
1263 
1264 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest080, TestSize.Level0)
1265 {
1266     int ret = 0;
1267     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
1268     int cipherTextLen = CIPHER_TEXT_LEN;
1269     HcfCipher *cipher = nullptr;
1270     HcfSymKey *key = nullptr;
1271 
1272     ret = GenerateSymKeyForSm4("AES256", &key);
1273     if (ret != 0) {
1274         LOGE("GenerateSymKeyForSm4 failed!");
1275         goto CLEAR_UP;
1276     }
1277 
1278     ret = HcfCipherCreate("SM4_128|CTR|PKCS5", &cipher);
1279     if (ret != 0) {
1280         LOGE("HcfCipherCreate failed!");
1281         goto CLEAR_UP;
1282     }
1283 
1284     // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key.
1285     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
1286     if (ret != 0) {
1287         LOGE("AesEncrypt failed! %d", ret);
1288         goto CLEAR_UP;
1289     }
1290 
1291     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
1292     if (ret != 0) {
1293         LOGE("AesDecrypt failed! %d", ret);
1294     }
1295 
1296 CLEAR_UP:
1297     HcfObjDestroy(key);
1298     HcfObjDestroy(cipher);
1299     EXPECT_NE(ret, 0);
1300 }
1301 
1302 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest081, TestSize.Level0)
1303 {
1304     int ret = 0;
1305     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
1306     int cipherTextLen = CIPHER_TEXT_LEN;
1307     HcfCipher *cipher = nullptr;
1308     HcfSymKey *key = nullptr;
1309 
1310     ret = GenerateSymKeyForSm4("AES256", &key);
1311     if (ret != 0) {
1312         LOGE("GenerateSymKeyForSm4 failed!");
1313         goto CLEAR_UP;
1314     }
1315 
1316     ret = HcfCipherCreate("SM4_128|OFB|PKCS5", &cipher);
1317     if (ret != 0) {
1318         LOGE("HcfCipherCreate failed!");
1319         goto CLEAR_UP;
1320     }
1321 
1322     // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key.
1323     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
1324     if (ret != 0) {
1325         LOGE("AesEncrypt failed! %d", ret);
1326         goto CLEAR_UP;
1327     }
1328 
1329     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
1330     if (ret != 0) {
1331         LOGE("AesDecrypt failed! %d", ret);
1332     }
1333 
1334 CLEAR_UP:
1335     HcfObjDestroy(key);
1336     HcfObjDestroy(cipher);
1337     EXPECT_NE(ret, 0);
1338 }
1339 }