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