1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include <fstream>
18 #include <iostream>
19 #include "securec.h"
20 
21 #include "aes_common.h"
22 #include "aes_openssl.h"
23 #include "blob.h"
24 #include "cipher.h"
25 #include "detailed_iv_params.h"
26 #include "detailed_gcm_params.h"
27 #include "detailed_ccm_params.h"
28 #include "log.h"
29 #include "memory.h"
30 #include "sym_common_defines.h"
31 #include "sym_key_generator.h"
32 
33 using namespace std;
34 using namespace testing::ext;
35 
36 namespace {
37 class CryptoAesCcmCipherTest : public testing::Test {
38 public:
SetUpTestCase()39     static void SetUpTestCase() {};
TearDownTestCase()40     static void TearDownTestCase() {};
SetUp()41     void SetUp() {};
TearDown()42     void TearDown() {};
43 };
44 
45 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest001, TestSize.Level0)
46 {
47     int ret = 0;
48     uint8_t aad[8] = {0};
49     uint8_t tag[12] = {0};
50     uint8_t iv[7] = {0};
51     uint8_t cipherText[128] = {0};
52     int cipherTextLen = 128;
53 
54     HcfCipher *cipher = nullptr;
55     HcfSymKey *key = nullptr;
56     HcfCcmParamsSpec spec = {};
57     spec.aad.data = aad;
58     spec.aad.len = sizeof(aad);
59     spec.tag.data = tag;
60     spec.tag.len = sizeof(tag);
61     spec.iv.data = iv;
62     spec.iv.len = sizeof(iv);
63 
64     ret = GenerateSymKey("AES128", &key);
65     if (ret != 0) {
66         LOGE("generateSymKey failed!");
67         goto CLEAR_UP;
68     }
69 
70     ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
71     if (ret != 0) {
72         LOGE("HcfCipherCreate failed!");
73         goto CLEAR_UP;
74     }
75 
76     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
77     if (ret != 0) {
78         LOGE("AesEncrypt failed!");
79         goto CLEAR_UP;
80     }
81 
82     (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
83     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
84     cipherTextLen -= 12;
85 
86     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
87     if (ret != 0) {
88         LOGE("AesDecrypt failed!");
89         goto CLEAR_UP;
90     }
91 
92 CLEAR_UP:
93     HcfObjDestroy((HcfObjectBase *)key);
94     HcfObjDestroy((HcfObjectBase *)cipher);
95     EXPECT_EQ(ret, 0);
96 }
97 
98 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest002, TestSize.Level0)
99 {
100     int ret = 0;
101     uint8_t aad[8] = {0};
102     uint8_t tag[12] = {0};
103     uint8_t iv[7] = {0};
104     uint8_t cipherText[128] = {0};
105     int cipherTextLen = 128;
106 
107     HcfCipher *cipher = nullptr;
108     HcfSymKey *key = nullptr;
109     HcfCcmParamsSpec spec = {};
110     spec.aad.data = aad;
111     spec.aad.len = sizeof(aad);
112     spec.tag.data = tag;
113     spec.tag.len = sizeof(tag);
114     spec.iv.data = iv;
115     spec.iv.len = sizeof(iv);
116 
117     ret = GenerateSymKey("AES128", &key);
118     if (ret != 0) {
119         LOGE("generateSymKey failed!");
120         goto CLEAR_UP;
121     }
122 
123     ret = HcfCipherCreate("AES128|CCM|PKCS5", &cipher);
124     if (ret != 0) {
125         LOGE("HcfCipherCreate failed!");
126         goto CLEAR_UP;
127     }
128 
129     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
130     if (ret != 0) {
131         LOGE("AesEncrypt failed!");
132         goto CLEAR_UP;
133     }
134 
135     (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
136     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
137     cipherTextLen -= 12;
138 
139     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
140     if (ret != 0) {
141         LOGE("AesDecrypt failed!");
142         goto CLEAR_UP;
143     }
144 
145 CLEAR_UP:
146     HcfObjDestroy((HcfObjectBase *)key);
147     HcfObjDestroy((HcfObjectBase *)cipher);
148     EXPECT_EQ(ret, 0);
149 }
150 
151 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest003, TestSize.Level0)
152 {
153     int ret = 0;
154     uint8_t aad[8] = {0};
155     uint8_t tag[12] = {0};
156     uint8_t iv[7] = {0};
157     uint8_t cipherText[128] = {0};
158     int cipherTextLen = 128;
159 
160     HcfCipher *cipher = nullptr;
161     HcfSymKey *key = nullptr;
162     HcfCcmParamsSpec spec = {};
163     spec.aad.data = aad;
164     spec.aad.len = sizeof(aad);
165     spec.tag.data = tag;
166     spec.tag.len = sizeof(tag);
167     spec.iv.data = iv;
168     spec.iv.len = sizeof(iv);
169 
170     ret = GenerateSymKey("AES128", &key);
171     if (ret != 0) {
172         LOGE("generateSymKey failed!");
173         goto CLEAR_UP;
174     }
175 
176     ret = HcfCipherCreate("AES128|CCM|PKCS7", &cipher);
177     if (ret != 0) {
178         LOGE("HcfCipherCreate failed!");
179         goto CLEAR_UP;
180     }
181 
182     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
183     if (ret != 0) {
184         LOGE("AesEncrypt failed!");
185         goto CLEAR_UP;
186     }
187 
188     (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
189     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
190     cipherTextLen -= 12;
191 
192     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
193     if (ret != 0) {
194         LOGE("AesDecrypt failed!");
195         goto CLEAR_UP;
196     }
197 
198 CLEAR_UP:
199     HcfObjDestroy((HcfObjectBase *)key);
200     HcfObjDestroy((HcfObjectBase *)cipher);
201     EXPECT_EQ(ret, 0);
202 }
203 
204 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest004, TestSize.Level0)
205 {
206     int ret = 0;
207     uint8_t aad[8] = {0};
208     uint8_t tag[12] = {0};
209     uint8_t iv[7] = {0};
210     uint8_t cipherText[128] = {0};
211     int cipherTextLen = 128;
212 
213     HcfCipher *cipher = nullptr;
214     HcfSymKey *key = nullptr;
215     HcfCcmParamsSpec spec = {};
216     spec.aad.data = aad;
217     spec.aad.len = sizeof(aad);
218     spec.tag.data = tag;
219     spec.tag.len = sizeof(tag);
220     spec.iv.data = iv;
221     spec.iv.len = sizeof(iv);
222 
223     ret = GenerateSymKey("AES128", &key);
224     if (ret != 0) {
225         LOGE("GenerateSymKey failed!");
226         goto CLEAR_UP;
227     }
228 
229     ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
230     if (ret != 0) {
231         LOGE("HcfCipherCreate failed!");
232         goto CLEAR_UP;
233     }
234 
235     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
236     if (ret != 0) {
237         LOGE("AesNoUpdateEncrypt failed!");
238         goto CLEAR_UP;
239     }
240 
241     (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
242     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
243     cipherTextLen -= 12;
244 
245     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
246     if (ret != 0) {
247         LOGE("AesNoUpdateDecrypt failed!");
248         goto CLEAR_UP;
249     }
250 
251 CLEAR_UP:
252     HcfObjDestroy((HcfObjectBase *)key);
253     HcfObjDestroy((HcfObjectBase *)cipher);
254     EXPECT_EQ(ret, 0);
255 }
256 
257 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest005, TestSize.Level0)
258 {
259     int ret = 0;
260     uint8_t aad[8] = {0};
261     uint8_t tag[12] = {0};
262     uint8_t iv[7] = {0};
263     uint8_t cipherText[128] = {0};
264     int cipherTextLen = 128;
265 
266     HcfCipher *cipher = nullptr;
267     HcfSymKey *key = nullptr;
268     HcfCcmParamsSpec spec = {};
269     spec.aad.data = aad;
270     spec.aad.len = sizeof(aad);
271     spec.tag.data = tag;
272     spec.tag.len = sizeof(tag);
273     spec.iv.data = iv;
274     spec.iv.len = sizeof(iv);
275 
276     ret = GenerateSymKey("AES128", &key);
277     if (ret != 0) {
278         LOGE("GenerateSymKey failed!");
279         goto CLEAR_UP;
280     }
281 
282     ret = HcfCipherCreate("AES128|CCM|PKCS5", &cipher);
283     if (ret != 0) {
284         LOGE("HcfCipherCreate failed!");
285         goto CLEAR_UP;
286     }
287 
288     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
289     if (ret != 0) {
290         LOGE("AesNoUpdateEncrypt failed!");
291         goto CLEAR_UP;
292     }
293 
294     (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
295     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
296     cipherTextLen -= 12;
297 
298     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
299     if (ret != 0) {
300         LOGE("AesNoUpdateDecrypt failed!");
301         goto CLEAR_UP;
302     }
303 
304 CLEAR_UP:
305     HcfObjDestroy((HcfObjectBase *)key);
306     HcfObjDestroy((HcfObjectBase *)cipher);
307     EXPECT_EQ(ret, 0);
308 }
309 
310 
311 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest006, TestSize.Level0)
312 {
313     int ret = 0;
314     uint8_t aad[8] = {0};
315     uint8_t tag[12] = {0};
316     uint8_t iv[7] = {0};
317     uint8_t cipherText[128] = {0};
318     int cipherTextLen = 128;
319 
320     HcfCipher *cipher = nullptr;
321     HcfSymKey *key = nullptr;
322     HcfCcmParamsSpec spec = {};
323     spec.aad.data = aad;
324     spec.aad.len = sizeof(aad);
325     spec.tag.data = tag;
326     spec.tag.len = sizeof(tag);
327     spec.iv.data = iv;
328     spec.iv.len = sizeof(iv);
329 
330     ret = GenerateSymKey("AES128", &key);
331     if (ret != 0) {
332         LOGE("GenerateSymKey failed!");
333         goto CLEAR_UP;
334     }
335 
336     ret = HcfCipherCreate("AES128|CCM|PKCS7", &cipher);
337     if (ret != 0) {
338         LOGE("HcfCipherCreate failed!");
339         goto CLEAR_UP;
340     }
341 
342     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
343     if (ret != 0) {
344         LOGE("AesNoUpdateEncrypt failed!");
345         goto CLEAR_UP;
346     }
347 
348     (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
349     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
350     cipherTextLen -= 12;
351 
352     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
353     if (ret != 0) {
354         LOGE("AesNoUpdateDecrypt failed!");
355         goto CLEAR_UP;
356     }
357 
358 CLEAR_UP:
359     HcfObjDestroy((HcfObjectBase *)key);
360     HcfObjDestroy((HcfObjectBase *)cipher);
361     EXPECT_EQ(ret, 0);
362 }
363 
364 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest007, TestSize.Level0)
365 {
366     int ret = 0;
367     uint8_t aad[CCM_AAD_LEN] = { 0 };
368     uint8_t tag[CCM_TAG_LEN] = { 0 };
369     uint8_t iv[CCM_IV_LEN] = { 0 };
370     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
371     int cipherTextLen = CIPHER_TEXT_LEN;
372 
373     HcfCipher *cipher = nullptr;
374     HcfSymKey *key = nullptr;
375     HcfCcmParamsSpec spec = {};
376     spec.aad.data = aad;
377     spec.aad.len = sizeof(aad);
378     spec.tag.data = tag;
379     spec.tag.len = sizeof(tag);
380     spec.iv.data = iv;
381     spec.iv.len = sizeof(iv);
382 
383     ret = GenerateSymKey("AES192", &key);
384     if (ret != 0) {
385         LOGE("generateSymKey failed!");
386         goto CLEAR_UP;
387     }
388 
389     ret = HcfCipherCreate("AES192|CCM|PKCS5", &cipher);
390     if (ret != 0) {
391         LOGE("HcfCipherCreate failed!");
392         goto CLEAR_UP;
393     }
394 
395     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
396     if (ret != 0) {
397         LOGE("AesEncrypt failed!");
398         goto CLEAR_UP;
399     }
400 
401     (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN);
402     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
403     cipherTextLen -= CCM_TAG_LEN;
404 
405     ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
406     if (ret != 0) {
407         LOGE("AesDecrypt failed!");
408     }
409 
410 CLEAR_UP:
411     HcfObjDestroy(key);
412     HcfObjDestroy(cipher);
413     EXPECT_EQ(ret, 0);
414 }
415 
416 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest008, TestSize.Level0)
417 {
418     int ret = 0;
419     uint8_t aad[CCM_AAD_LEN] = { 0 };
420     uint8_t tag[CCM_TAG_LEN] = { 0 };
421     uint8_t iv[CCM_IV_LEN] = { 0 };
422     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
423     int cipherTextLen = CIPHER_TEXT_LEN;
424 
425     HcfCipher *cipher = nullptr;
426     HcfSymKey *key = nullptr;
427     HcfCcmParamsSpec spec = {};
428     spec.aad.data = aad;
429     spec.aad.len = sizeof(aad);
430     spec.tag.data = tag;
431     spec.tag.len = sizeof(tag);
432     spec.iv.data = iv;
433     spec.iv.len = sizeof(iv);
434 
435     ret = GenerateSymKey("AES256", &key);
436     if (ret != 0) {
437         LOGE("generateSymKey failed!");
438         goto CLEAR_UP;
439     }
440 
441     ret = HcfCipherCreate("AES256|CCM|PKCS5", &cipher);
442     if (ret != 0) {
443         LOGE("HcfCipherCreate failed!");
444         goto CLEAR_UP;
445     }
446 
447     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
448     if (ret != 0) {
449         LOGE("AesEncrypt failed!");
450         goto CLEAR_UP;
451     }
452 
453     (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN);
454     PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
455     cipherTextLen -= CCM_TAG_LEN;
456 
457     ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
458     if (ret != 0) {
459         LOGE("AesDecrypt failed!");
460     }
461 
462 CLEAR_UP:
463     HcfObjDestroy(key);
464     HcfObjDestroy(cipher);
465     EXPECT_EQ(ret, 0);
466 }
467 
468 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest009, TestSize.Level0)
469 {
470     int ret = 0;
471     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
472     int cipherTextLen = CIPHER_TEXT_LEN;
473 
474     HcfCipher *cipher = nullptr;
475     HcfSymKey *key = nullptr;
476 
477     ret = GenerateSymKey("AES128", &key);
478     if (ret != 0) {
479         LOGE("generateSymKey failed!");
480         goto CLEAR_UP;
481     }
482 
483     ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
484     if (ret != 0) {
485         LOGE("HcfCipherCreate failed!");
486         goto CLEAR_UP;
487     }
488 
489     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
490     if (ret != 0) {
491         LOGE("AesEncrypt failed!");
492     }
493 
494 CLEAR_UP:
495     HcfObjDestroy(key);
496     HcfObjDestroy(cipher);
497     EXPECT_NE(ret, 0);
498 }
499 
500 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest010, TestSize.Level0)
501 {
502     int ret = 0;
503     uint8_t aad[CCM_AAD_LEN] = { 0 };
504     uint8_t tag[CCM_TAG_LEN] = { 0 };
505     uint8_t iv[CCM_IV_LEN] = { 0 };
506     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
507     int cipherTextLen = CIPHER_TEXT_LEN;
508 
509     HcfCipher *cipher = nullptr;
510     HcfSymKey *key = nullptr;
511     HcfCcmParamsSpec spec = {};
512     spec.aad.data = nullptr;
513     spec.aad.len = sizeof(aad);
514     spec.tag.data = tag;
515     spec.tag.len = sizeof(tag);
516     spec.iv.data = iv;
517     spec.iv.len = sizeof(iv);
518 
519     ret = GenerateSymKey("AES128", &key);
520     if (ret != 0) {
521         LOGE("generateSymKey failed!");
522         goto CLEAR_UP;
523     }
524 
525     ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
526     if (ret != 0) {
527         LOGE("HcfCipherCreate failed!");
528         goto CLEAR_UP;
529     }
530 
531     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
532     if (ret != 0) {
533         LOGE("AesEncrypt failed!");
534     }
535 
536 CLEAR_UP:
537     HcfObjDestroy(key);
538     HcfObjDestroy(cipher);
539     EXPECT_NE(ret, 0);
540 }
541 
542 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest011, TestSize.Level0)
543 {
544     int ret = 0;
545     uint8_t aad[CCM_AAD_LEN] = { 0 };
546     uint8_t tag[CCM_TAG_LEN] = { 0 };
547     uint8_t iv[CCM_IV_LEN] = { 0 };
548     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
549     int cipherTextLen = CIPHER_TEXT_LEN;
550 
551     HcfCipher *cipher = nullptr;
552     HcfSymKey *key = nullptr;
553     HcfCcmParamsSpec spec = {};
554     spec.aad.data = aad;
555     spec.aad.len = sizeof(aad);
556     spec.tag.data = tag;
557     spec.tag.len = sizeof(tag);
558     spec.iv.data = nullptr;
559     spec.iv.len = sizeof(iv);
560 
561     ret = GenerateSymKey("AES128", &key);
562     if (ret != 0) {
563         LOGE("generateSymKey failed!");
564         goto CLEAR_UP;
565     }
566 
567     ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
568     if (ret != 0) {
569         LOGE("HcfCipherCreate failed!");
570         goto CLEAR_UP;
571     }
572 
573     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
574     if (ret != 0) {
575         LOGE("AesEncrypt failed!");
576     }
577 
578 CLEAR_UP:
579     HcfObjDestroy(key);
580     HcfObjDestroy(cipher);
581     EXPECT_NE(ret, 0);
582 }
583 
584 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest012, TestSize.Level0)
585 {
586     int ret = 0;
587     uint8_t aad[CCM_AAD_LEN] = { 0 };
588     uint8_t tag[CCM_TAG_LEN] = { 0 };
589     uint8_t iv[CCM_IV_LEN] = { 0 };
590     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
591     int cipherTextLen = CIPHER_TEXT_LEN;
592 
593     HcfCipher *cipher = nullptr;
594     HcfSymKey *key = nullptr;
595     HcfCcmParamsSpec spec = {};
596     spec.aad.data = aad;
597     spec.aad.len = sizeof(aad);
598     spec.tag.data = nullptr;
599     spec.tag.len = sizeof(tag);
600     spec.iv.data = iv;
601     spec.iv.len = sizeof(iv);
602 
603     ret = GenerateSymKey("AES128", &key);
604     if (ret != 0) {
605         LOGE("generateSymKey failed!");
606         goto CLEAR_UP;
607     }
608 
609     ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
610     if (ret != 0) {
611         LOGE("HcfCipherCreate failed!");
612         goto CLEAR_UP;
613     }
614 
615     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
616     if (ret != 0) {
617         LOGE("AesEncrypt failed!");
618     }
619 
620 CLEAR_UP:
621     HcfObjDestroy(key);
622     HcfObjDestroy(cipher);
623     EXPECT_NE(ret, 0);
624 }
625 
626 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest013, TestSize.Level0)
627 {
628     int ret = 0;
629     HcfCipher *cipher = nullptr;
630     HcfSymKey *key = nullptr;
631     HcfSymKeyGenerator *generator = nullptr;
632 
633     ret = HcfSymKeyGeneratorCreate("AES128", &generator);
634     if (ret != 0) {
635         LOGE("HcfSymKeyGeneratorCreate failed!");
636         goto CLEAR_UP;
637     }
638     EXPECT_EQ(ret, HCF_SUCCESS);
639     ret = generator->generateSymKey(generator, &key);
640     if (ret != 0) {
641         LOGE("generateSymKey failed!");
642         goto CLEAR_UP;
643     }
644     generator->base.destroy(nullptr);
645 
646     ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
647     EXPECT_EQ(ret, HCF_SUCCESS);
648     ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast<HcfKey *>(key), nullptr);
649     EXPECT_NE(ret, HCF_SUCCESS);
650     goto CLEAR_UP;
651 CLEAR_UP:
652     HcfObjDestroy(key);
653     HcfObjDestroy(cipher);
654     HcfObjDestroy(generator);
655 }
656 }