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 CryptoAesGcmCipherTest : 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(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest001, TestSize.Level0)
46 {
47     int ret = 0;
48     uint8_t aad[8] = {0};
49     uint8_t tag[16] = {0};
50     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
51     uint8_t cipherText[128] = {0};
52     int cipherTextLen = 128;
53 
54     HcfCipher *cipher = nullptr;
55     HcfSymKey *key = nullptr;
56 
57     HcfGcmParamsSpec spec = {};
58     spec.aad.data = aad;
59     spec.aad.len = sizeof(aad);
60     spec.tag.data = tag;
61     spec.tag.len = sizeof(tag);
62     spec.iv.data = iv;
63     spec.iv.len = sizeof(iv);
64 
65     ret = GenerateSymKey("AES128", &key);
66     if (ret != 0) {
67         LOGE("generateSymKey failed!");
68         goto CLEAR_UP;
69     }
70 
71     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
72     if (ret != 0) {
73         LOGE("HcfCipherCreate failed!");
74         goto CLEAR_UP;
75     }
76 
77     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
78     if (ret != 0) {
79         LOGE("AesEncrypt failed, ret:%d!", ret);
80         goto CLEAR_UP;
81     }
82 
83     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
84     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
85     cipherTextLen -= 16;
86 
87     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
88     if (ret != 0) {
89         LOGE("AesDecrypt failed, ret:%d!", ret);
90         goto CLEAR_UP;
91     }
92 
93 CLEAR_UP:
94     HcfObjDestroy((HcfObjectBase *)key);
95     HcfObjDestroy((HcfObjectBase *)cipher);
96     EXPECT_EQ(ret, 0);
97 }
98 
99 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest002, TestSize.Level0)
100 {
101     int ret = 0;
102     uint8_t aad[8] = {0};
103     uint8_t tag[16] = {0};
104     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
105     uint8_t cipherText[128] = {0};
106     int cipherTextLen = 128;
107 
108     HcfCipher *cipher = nullptr;
109     HcfSymKey *key = nullptr;
110 
111     HcfGcmParamsSpec spec = {};
112     spec.aad.data = aad;
113     spec.aad.len = sizeof(aad);
114     spec.tag.data = tag;
115     spec.tag.len = sizeof(tag);
116     spec.iv.data = iv;
117     spec.iv.len = sizeof(iv);
118 
119     ret = GenerateSymKey("AES128", &key);
120     if (ret != 0) {
121         LOGE("generateSymKey failed!");
122         goto CLEAR_UP;
123     }
124 
125     ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
126     if (ret != 0) {
127         LOGE("HcfCipherCreate failed!");
128         goto CLEAR_UP;
129     }
130 
131     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
132     if (ret != 0) {
133         LOGE("AesEncrypt failed, ret:%d!", ret);
134         goto CLEAR_UP;
135     }
136 
137     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
138     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
139     cipherTextLen -= 16;
140 
141     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
142     if (ret != 0) {
143         LOGE("AesDecrypt failed, ret:%d!", ret);
144         goto CLEAR_UP;
145     }
146 
147 CLEAR_UP:
148     HcfObjDestroy((HcfObjectBase *)key);
149     HcfObjDestroy((HcfObjectBase *)cipher);
150     EXPECT_EQ(ret, 0);
151 }
152 
153 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest003, TestSize.Level0)
154 {
155     int ret = 0;
156     uint8_t aad[8] = {0};
157     uint8_t tag[16] = {0};
158     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
159     uint8_t cipherText[128] = {0};
160     int cipherTextLen = 128;
161 
162     HcfCipher *cipher = nullptr;
163     HcfSymKey *key = nullptr;
164 
165     HcfGcmParamsSpec spec = {};
166     spec.aad.data = aad;
167     spec.aad.len = sizeof(aad);
168     spec.tag.data = tag;
169     spec.tag.len = sizeof(tag);
170     spec.iv.data = iv;
171     spec.iv.len = sizeof(iv);
172 
173     ret = GenerateSymKey("AES128", &key);
174     if (ret != 0) {
175         LOGE("generateSymKey failed!");
176         goto CLEAR_UP;
177     }
178 
179     ret = HcfCipherCreate("AES128|GCM|PKCS7", &cipher);
180     if (ret != 0) {
181         LOGE("HcfCipherCreate failed!");
182         goto CLEAR_UP;
183     }
184 
185     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
186     if (ret != 0) {
187         LOGE("AesEncrypt failed, ret:%d!", ret);
188         goto CLEAR_UP;
189     }
190 
191     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
192     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
193     cipherTextLen -= 16;
194 
195     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
196     if (ret != 0) {
197         LOGE("AesDecrypt failed, ret:%d!", ret);
198         goto CLEAR_UP;
199     }
200 
201 CLEAR_UP:
202     HcfObjDestroy((HcfObjectBase *)key);
203     HcfObjDestroy((HcfObjectBase *)cipher);
204     EXPECT_EQ(ret, 0);
205 }
206 
207 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest004, TestSize.Level0)
208 {
209     int ret = 0;
210     uint8_t aad[8] = {0};
211     uint8_t tag[16] = {0};
212     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
213     uint8_t cipherText[128] = {0};
214     int cipherTextLen = 128;
215 
216     HcfCipher *cipher = nullptr;
217     HcfSymKey *key = nullptr;
218 
219     HcfGcmParamsSpec spec = {};
220     spec.aad.data = aad;
221     spec.aad.len = sizeof(aad);
222     spec.tag.data = tag;
223     spec.tag.len = sizeof(tag);
224     spec.iv.data = iv;
225     spec.iv.len = sizeof(iv);
226 
227     ret = GenerateSymKey("AES128", &key);
228     if (ret != 0) {
229         LOGE("GenerateSymKey failed!");
230         goto CLEAR_UP;
231     }
232 
233     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
234     if (ret != 0) {
235         LOGE("HcfCipherCreate failed!");
236         goto CLEAR_UP;
237     }
238 
239     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
240     if (ret != 0) {
241         LOGE("AesNoUpdateEncrypt failed, ret:%d!", ret);
242         goto CLEAR_UP;
243     }
244 
245     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
246     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
247     cipherTextLen -= 16;
248 
249     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
250     if (ret != 0) {
251         LOGE("AesNoUpdateDecrypt failed, ret:%d!", ret);
252         goto CLEAR_UP;
253     }
254 
255 CLEAR_UP:
256     HcfObjDestroy((HcfObjectBase *)key);
257     HcfObjDestroy((HcfObjectBase *)cipher);
258     EXPECT_EQ(ret, 0);
259 }
260 
261 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest005, TestSize.Level0)
262 {
263     int ret = 0;
264     uint8_t aad[8] = {0};
265     uint8_t tag[16] = {0};
266     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
267     uint8_t cipherText[128] = {0};
268     int cipherTextLen = 128;
269 
270     HcfCipher *cipher = nullptr;
271     HcfSymKey *key = nullptr;
272 
273     HcfGcmParamsSpec spec = {};
274     spec.aad.data = aad;
275     spec.aad.len = sizeof(aad);
276     spec.tag.data = tag;
277     spec.tag.len = sizeof(tag);
278     spec.iv.data = iv;
279     spec.iv.len = sizeof(iv);
280 
281     ret = GenerateSymKey("AES128", &key);
282     if (ret != 0) {
283         LOGE("GenerateSymKey failed!");
284         goto CLEAR_UP;
285     }
286 
287     ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
288     if (ret != 0) {
289         LOGE("HcfCipherCreate failed!");
290         goto CLEAR_UP;
291     }
292 
293     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
294     if (ret != 0) {
295         LOGE("AesNoUpdateEncrypt failed, ret:%d!", ret);
296         goto CLEAR_UP;
297     }
298 
299     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
300     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
301     cipherTextLen -= 16;
302 
303     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
304     if (ret != 0) {
305         LOGE("AesNoUpdateDecrypt failed, ret:%d!", ret);
306         goto CLEAR_UP;
307     }
308 
309 CLEAR_UP:
310     HcfObjDestroy((HcfObjectBase *)key);
311     HcfObjDestroy((HcfObjectBase *)cipher);
312     EXPECT_EQ(ret, 0);
313 }
314 
315 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest006, TestSize.Level0)
316 {
317     int ret = 0;
318     uint8_t aad[8] = {0};
319     uint8_t tag[16] = {0};
320     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
321     uint8_t cipherText[128] = {0};
322     int cipherTextLen = 128;
323 
324     HcfCipher *cipher = nullptr;
325     HcfSymKey *key = nullptr;
326 
327     HcfGcmParamsSpec spec = {};
328     spec.aad.data = aad;
329     spec.aad.len = sizeof(aad);
330     spec.tag.data = tag;
331     spec.tag.len = sizeof(tag);
332     spec.iv.data = iv;
333     spec.iv.len = sizeof(iv);
334 
335     ret = GenerateSymKey("AES128", &key);
336     if (ret != 0) {
337         LOGE("GenerateSymKey failed!");
338         goto CLEAR_UP;
339     }
340 
341     ret = HcfCipherCreate("AES128|GCM|PKCS7", &cipher);
342     if (ret != 0) {
343         LOGE("HcfCipherCreate failed!");
344         goto CLEAR_UP;
345     }
346 
347     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
348     if (ret != 0) {
349         LOGE("AesNoUpdateEncrypt failed, ret:%d!", ret);
350         goto CLEAR_UP;
351     }
352 
353     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
354     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
355     cipherTextLen -= 16;
356 
357     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
358     if (ret != 0) {
359         LOGE("AesNoUpdateDecrypt failed, ret:%d!", ret);
360         goto CLEAR_UP;
361     }
362 
363 CLEAR_UP:
364     HcfObjDestroy((HcfObjectBase *)key);
365     HcfObjDestroy((HcfObjectBase *)cipher);
366     EXPECT_EQ(ret, 0);
367 }
368 
369 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest007, TestSize.Level0)
370 {
371     int ret = 0;
372     uint8_t aad[GCM_AAD_LEN] = { 0 };
373     uint8_t tag[GCM_TAG_LEN] = { 0 };
374     uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes
375     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
376     int cipherTextLen = CIPHER_TEXT_LEN;
377 
378     HcfCipher *cipher = nullptr;
379     HcfSymKey *key = nullptr;
380 
381     HcfGcmParamsSpec spec = {};
382     spec.aad.data = aad;
383     spec.aad.len = sizeof(aad);
384     spec.tag.data = tag;
385     spec.tag.len = sizeof(tag);
386     spec.iv.data = iv;
387     spec.iv.len = sizeof(iv);
388 
389     ret = GenerateSymKey("AES192", &key);
390     if (ret != 0) {
391         LOGE("generateSymKey failed!");
392         goto CLEAR_UP;
393     }
394 
395     ret = HcfCipherCreate("AES192|GCM|PKCS5", &cipher);
396     if (ret != 0) {
397         LOGE("HcfCipherCreate failed!");
398         goto CLEAR_UP;
399     }
400 
401     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
402     if (ret != 0) {
403         LOGE("AesEncrypt failed, ret:%d!", ret);
404         goto CLEAR_UP;
405     }
406 
407     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
408     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
409     cipherTextLen -= GCM_TAG_LEN;
410 
411     ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
412     if (ret != 0) {
413         LOGE("AesDecrypt failed, ret:%d!", ret);
414     }
415 
416 CLEAR_UP:
417     HcfObjDestroy(key);
418     HcfObjDestroy(cipher);
419     EXPECT_EQ(ret, 0);
420 }
421 
422 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest008, TestSize.Level0)
423 {
424     int ret = 0;
425     uint8_t aad[GCM_AAD_LEN] = { 0 };
426     uint8_t tag[GCM_TAG_LEN] = { 0 };
427     uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes
428     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
429     int cipherTextLen = CIPHER_TEXT_LEN;
430 
431     HcfCipher *cipher = nullptr;
432     HcfSymKey *key = nullptr;
433 
434     HcfGcmParamsSpec spec = {};
435     spec.aad.data = aad;
436     spec.aad.len = sizeof(aad);
437     spec.tag.data = tag;
438     spec.tag.len = sizeof(tag);
439     spec.iv.data = iv;
440     spec.iv.len = sizeof(iv);
441 
442     ret = GenerateSymKey("AES256", &key);
443     if (ret != 0) {
444         LOGE("generateSymKey failed!");
445         goto CLEAR_UP;
446     }
447 
448     ret = HcfCipherCreate("AES256|GCM|PKCS5", &cipher);
449     if (ret != 0) {
450         LOGE("HcfCipherCreate failed!");
451         goto CLEAR_UP;
452     }
453 
454     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
455     if (ret != 0) {
456         LOGE("AesEncrypt failed, ret:%d!", ret);
457         goto CLEAR_UP;
458     }
459 
460     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
461     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
462     cipherTextLen -= GCM_TAG_LEN;
463 
464     ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
465     if (ret != 0) {
466         LOGE("AesDecrypt failed, ret:%d!", ret);
467     }
468 
469 CLEAR_UP:
470     HcfObjDestroy(key);
471     HcfObjDestroy(cipher);
472     EXPECT_EQ(ret, 0);
473 }
474 
475 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest009, TestSize.Level0)
476 {
477     int ret = 0;
478     HcfCipher *cipher = nullptr;
479 
480     ret = HcfCipherCreate("RSA128|GCM|NoPadding", &cipher);
481     if (ret != 0) {
482         LOGE("HcfCipherCreate failed! Should not select RSA for GCM generator.");
483     }
484 
485     HcfObjDestroy(cipher);
486     EXPECT_NE(ret, 0);
487 }
488 
489 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest010, TestSize.Level0)
490 {
491     int ret = 0;
492     HcfCipher *cipher = nullptr;
493 
494     // not allow '|' without content, because findAbility will fail for "" input
495     ret = HcfCipherCreate("AES128|GCM|", &cipher);
496     if (ret != 0) {
497         LOGE("HcfCipherCreate failed! Should select padding mode for AES generator.");
498     }
499 
500     HcfObjDestroy(cipher);
501     EXPECT_NE(ret, 0);
502 }
503 
504 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest011, TestSize.Level0)
505 {
506     int ret = 0;
507     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
508     int cipherTextLen = CIPHER_TEXT_LEN;
509     HcfCipher *cipher = nullptr;
510     HcfSymKey *key = nullptr;
511 
512     ret = GenerateSymKey("AES128", &key);
513     if (ret != 0) {
514         LOGE("GenerateSymKey failed!");
515         goto CLEAR_UP;
516     }
517 
518     // CBC, CTR, OFB, CFB enc/dec success,
519     // GCM, CCM enc/dec failed with params set to nullptr.
520     ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
521     if (ret != 0) {
522         LOGE("HcfCipherCreate failed!");
523         goto CLEAR_UP;
524     }
525 
526     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
527     if (ret != 0) {
528         LOGE("AesEncrypt failed! %d", ret);
529         goto CLEAR_UP;
530     }
531 
532     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
533     if (ret != 0) {
534         LOGE("AesDecrypt failed! %d", ret);
535     }
536 
537 CLEAR_UP:
538     HcfObjDestroy(key);
539     HcfObjDestroy(cipher);
540     EXPECT_NE(ret, 0);
541 }
542 
543 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest012, TestSize.Level0)
544 {
545     int ret = 0;
546     uint8_t aad[GCM_AAD_LEN] = { 0 };
547     uint8_t tag[GCM_TAG_LEN] = { 0 };
548     uint8_t iv[GCM_IV_LEN] = { 0 };
549     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
550     int cipherTextLen = CIPHER_TEXT_LEN;
551 
552     HcfCipher *cipher = nullptr;
553     HcfSymKey *key = nullptr;
554 
555     HcfGcmParamsSpec spec = {};
556     spec.aad.data = nullptr;
557     spec.aad.len = sizeof(aad);
558     spec.tag.data = tag;
559     spec.tag.len = sizeof(tag);
560     spec.iv.data = iv;
561     spec.iv.len = sizeof(iv);
562 
563     ret = GenerateSymKey("AES128", &key);
564     if (ret != 0) {
565         LOGE("generateSymKey failed!");
566         goto CLEAR_UP;
567     }
568 
569     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
570     if (ret != 0) {
571         LOGE("HcfCipherCreate failed!");
572         goto CLEAR_UP;
573     }
574 
575     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
576     if (ret != 0) {
577         LOGE("AesEncrypt failed, ret:%d!", ret);
578     }
579 
580 // now support gcm no aad.
581 CLEAR_UP:
582     HcfObjDestroy(key);
583     HcfObjDestroy(cipher);
584     EXPECT_EQ(ret, 0);
585 }
586 
587 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest013, TestSize.Level0)
588 {
589     int ret = 0;
590     uint8_t aad[GCM_AAD_LEN] = { 0 };
591     uint8_t tag[GCM_TAG_LEN] = { 0 };
592     uint8_t iv[GCM_IV_LEN] = { 0 };
593     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
594     int cipherTextLen = CIPHER_TEXT_LEN;
595 
596     HcfCipher *cipher = nullptr;
597     HcfSymKey *key = nullptr;
598 
599     HcfGcmParamsSpec spec = {};
600     spec.aad.data = aad;
601     spec.aad.len = sizeof(aad);
602     spec.tag.data = tag;
603     spec.tag.len = sizeof(tag);
604     spec.iv.data = nullptr;
605     spec.iv.len = sizeof(iv);
606 
607     ret = GenerateSymKey("AES128", &key);
608     if (ret != 0) {
609         LOGE("generateSymKey failed!");
610         goto CLEAR_UP;
611     }
612 
613     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
614     if (ret != 0) {
615         LOGE("HcfCipherCreate failed!");
616         goto CLEAR_UP;
617     }
618 
619     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
620     if (ret != 0) {
621         LOGE("AesEncrypt failed, ret:%d!", ret);
622     }
623 
624 CLEAR_UP:
625     HcfObjDestroy(key);
626     HcfObjDestroy(cipher);
627     EXPECT_NE(ret, 0);
628 }
629 
630 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest014, TestSize.Level0)
631 {
632     int ret = 0;
633     uint8_t aad[GCM_AAD_LEN] = { 0 };
634     uint8_t tag[GCM_TAG_LEN] = { 0 };
635     uint8_t iv[GCM_IV_LEN] = { 0 };
636     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
637     int cipherTextLen = CIPHER_TEXT_LEN;
638 
639     HcfCipher *cipher = nullptr;
640     HcfSymKey *key = nullptr;
641 
642     HcfGcmParamsSpec spec = {};
643     spec.aad.data = aad;
644     spec.aad.len = sizeof(aad);
645     spec.tag.data = nullptr;
646     spec.tag.len = sizeof(tag);
647     spec.iv.data = iv;
648     spec.iv.len = sizeof(iv);
649 
650     ret = GenerateSymKey("AES128", &key);
651     if (ret != 0) {
652         LOGE("generateSymKey failed!");
653         goto CLEAR_UP;
654     }
655 
656     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
657     if (ret != 0) {
658         LOGE("HcfCipherCreate failed!");
659         goto CLEAR_UP;
660     }
661 
662     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
663     if (ret != 0) {
664         LOGE("AesEncrypt failed, ret:%d!", ret);
665     }
666 
667 CLEAR_UP:
668     HcfObjDestroy(key);
669     HcfObjDestroy(cipher);
670     EXPECT_NE(ret, 0);
671 }
672 
673 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest015, TestSize.Level0)
674 {
675     int ret = 0;
676     uint8_t tag[GCM_TAG_LEN] = {0};
677     uint8_t iv[GCM_IV_LEN] = {0};
678     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
679     int cipherTextLen = CIPHER_TEXT_LEN;
680 
681     HcfCipher *cipher = nullptr;
682     HcfSymKey *key = nullptr;
683 
684     HcfGcmParamsSpec spec = {};
685     spec.aad.data = nullptr;
686     spec.aad.len = 0;
687     spec.tag.data = tag;
688     spec.tag.len = sizeof(tag);
689     spec.iv.data = iv;
690     spec.iv.len = sizeof(iv);
691 
692     ret = GenerateSymKey("AES128", &key);
693     if (ret != 0) {
694         LOGE("generateSymKey failed!");
695         goto CLEAR_UP;
696     }
697 
698     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
699     if (ret != 0) {
700         LOGE("HcfCipherCreate failed!");
701         goto CLEAR_UP;
702     }
703 
704     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
705     if (ret != 0) {
706         LOGE("AesEncrypt failed, ret:%d!", ret);
707         goto CLEAR_UP;
708     }
709 
710     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
711     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
712     cipherTextLen -= GCM_TAG_LEN;
713 
714     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
715     if (ret != 0) {
716         LOGE("AesDecrypt failed, ret:%d!", ret);
717         goto CLEAR_UP;
718     }
719 
720 CLEAR_UP:
721     HcfObjDestroy((HcfObjectBase *)key);
722     HcfObjDestroy((HcfObjectBase *)cipher);
723     EXPECT_EQ(ret, 0);
724 }
725 
726 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest016, TestSize.Level0)
727 {
728     int ret = 0;
729     uint8_t aad[GCM_AAD_LONG_LEN] = { 0 };
730     uint8_t tag[GCM_TAG_LEN] = {0};
731     uint8_t iv[GCM_IV_LEN] = {0};
732     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
733     int cipherTextLen = CIPHER_TEXT_LEN;
734 
735     HcfCipher *cipher = nullptr;
736     HcfSymKey *key = nullptr;
737 
738     HcfGcmParamsSpec spec = {};
739     spec.aad.data = aad;
740     spec.aad.len = sizeof(aad);
741     spec.tag.data = tag;
742     spec.tag.len = sizeof(tag);
743     spec.iv.data = iv;
744     spec.iv.len = sizeof(iv);
745 
746     ret = GenerateSymKey("AES128", &key);
747     if (ret != 0) {
748         LOGE("generateSymKey failed!");
749         goto CLEAR_UP;
750     }
751 
752     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
753     if (ret != 0) {
754         LOGE("HcfCipherCreate failed!");
755         goto CLEAR_UP;
756     }
757 
758     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
759     if (ret != 0) {
760         LOGE("AesEncrypt failed, ret:%d!", ret);
761         goto CLEAR_UP;
762     }
763 
764     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
765     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
766     cipherTextLen -= GCM_TAG_LEN;
767 
768     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
769     if (ret != 0) {
770         LOGE("AesDecrypt failed, ret:%d!", ret);
771         goto CLEAR_UP;
772     }
773 
774 CLEAR_UP:
775     HcfObjDestroy((HcfObjectBase *)key);
776     HcfObjDestroy((HcfObjectBase *)cipher);
777     EXPECT_EQ(ret, 0);
778 }
779 
780 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest017, TestSize.Level0)
781 {
782     int ret = 0;
783     uint8_t aad[GCM_AAD_LONG_LEN] = { 0 };
784     uint8_t tag[GCM_TAG_LEN] = {0};
785     uint8_t iv[GCM_IV_LONG_LEN] = {0};
786     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
787     int cipherTextLen = CIPHER_TEXT_LEN;
788 
789     HcfCipher *cipher = nullptr;
790     HcfSymKey *key = nullptr;
791 
792     HcfGcmParamsSpec spec = {};
793     spec.aad.data = aad;
794     spec.aad.len = sizeof(aad);
795     spec.tag.data = tag;
796     spec.tag.len = sizeof(tag);
797     spec.iv.data = iv;
798     spec.iv.len = sizeof(iv);
799 
800     ret = GenerateSymKey("AES128", &key);
801     if (ret != 0) {
802         LOGE("generateSymKey failed!");
803         goto CLEAR_UP;
804     }
805 
806     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
807     if (ret != 0) {
808         LOGE("HcfCipherCreate failed!");
809         goto CLEAR_UP;
810     }
811 
812     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
813     if (ret != 0) {
814         LOGE("AesEncrypt failed, ret:%d!", ret);
815         goto CLEAR_UP;
816     }
817 
818     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
819     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
820     cipherTextLen -= GCM_TAG_LEN;
821 
822     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
823     if (ret != 0) {
824         LOGE("AesDecrypt failed, ret:%d!", ret);
825         goto CLEAR_UP;
826     }
827 
828 CLEAR_UP:
829     HcfObjDestroy((HcfObjectBase *)key);
830     HcfObjDestroy((HcfObjectBase *)cipher);
831     EXPECT_EQ(ret, 0);
832 }
833 
834 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest018, TestSize.Level0)
835 {
836     int ret = 0;
837     uint8_t aad[GCM_AAD_SHORT_LEN] = { 0 };
838     uint8_t tag[GCM_TAG_LEN] = {0};
839     // openssl only support ivLen [9, 16];
840     uint8_t iv[GCM_IV_SHORT_LEN] = {0};
841     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
842     int cipherTextLen = CIPHER_TEXT_LEN;
843 
844     HcfCipher *cipher = nullptr;
845     HcfSymKey *key = nullptr;
846 
847     HcfGcmParamsSpec spec = {};
848     spec.aad.data = aad;
849     spec.aad.len = sizeof(aad);
850     spec.tag.data = tag;
851     spec.tag.len = sizeof(tag);
852     spec.iv.data = iv;
853     spec.iv.len = sizeof(iv);
854 
855     ret = GenerateSymKey("AES128", &key);
856     if (ret != 0) {
857         LOGE("generateSymKey failed!");
858         goto CLEAR_UP;
859     }
860 
861     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
862     if (ret != 0) {
863         LOGE("HcfCipherCreate failed!");
864         goto CLEAR_UP;
865     }
866 
867     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
868     if (ret != 0) {
869         LOGE("AesEncrypt failed, ret:%d!", ret);
870         goto CLEAR_UP;
871     }
872 
873     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
874     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
875     cipherTextLen -= GCM_TAG_LEN;
876 
877     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
878     if (ret != 0) {
879         LOGE("AesDecrypt failed, ret:%d!", ret);
880         goto CLEAR_UP;
881     }
882 
883 CLEAR_UP:
884     HcfObjDestroy((HcfObjectBase *)key);
885     HcfObjDestroy((HcfObjectBase *)cipher);
886     EXPECT_EQ(ret, 0);
887 }
888 
889 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest019, TestSize.Level0)
890 {
891     int ret = 0;
892     uint8_t tag[GCM_TAG_LEN] = {0};
893     uint8_t iv[GCM_IV_LONG_LEN] = {0};
894     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
895     int cipherTextLen = CIPHER_TEXT_LEN;
896 
897     HcfCipher *cipher = nullptr;
898     HcfSymKey *key = nullptr;
899 
900     HcfGcmParamsSpec spec = {};
901     spec.aad.data = nullptr;
902     spec.aad.len = 0;
903     spec.tag.data = tag;
904     spec.tag.len = sizeof(tag);
905     spec.iv.data = iv;
906     spec.iv.len = sizeof(iv);
907 
908     ret = GenerateSymKey("AES128", &key);
909     if (ret != 0) {
910         LOGE("generateSymKey failed!");
911         goto CLEAR_UP;
912     }
913 
914     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
915     if (ret != 0) {
916         LOGE("HcfCipherCreate failed!");
917         goto CLEAR_UP;
918     }
919 
920     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
921     if (ret != 0) {
922         LOGE("AesEncrypt failed, ret:%d!", ret);
923         goto CLEAR_UP;
924     }
925 
926     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
927     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
928     cipherTextLen -= GCM_TAG_LEN;
929 
930     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
931     if (ret != 0) {
932         LOGE("AesDecrypt failed, ret:%d!", ret);
933         goto CLEAR_UP;
934     }
935 
936 CLEAR_UP:
937     HcfObjDestroy((HcfObjectBase *)key);
938     HcfObjDestroy((HcfObjectBase *)cipher);
939     EXPECT_EQ(ret, 0);
940 }
941 
942 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest020, TestSize.Level0)
943 {
944     int ret = 0;
945     uint8_t aad[8] = {0};
946     uint8_t tag[16] = {0};
947     uint8_t iv[128] = {0}; // openssl support iv max 128 bytes
948     uint8_t cipherText[128] = {0};
949     int cipherTextLen = 128;
950 
951     HcfCipher *cipher = nullptr;
952     HcfSymKey *key = nullptr;
953 
954     HcfGcmParamsSpec spec = {};
955     spec.aad.data = aad;
956     spec.aad.len = sizeof(aad);
957     spec.tag.data = tag;
958     spec.tag.len = sizeof(tag);
959     spec.iv.data = iv;
960     spec.iv.len = sizeof(iv);
961 
962     ret = GenerateSymKey("AES128", &key);
963     if (ret != 0) {
964         LOGE("generateSymKey failed!");
965         HcfObjDestroy((HcfObjectBase *)key);
966     }
967 
968     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
969     if (ret != 0) {
970         LOGE("HcfCipherCreate failed!");
971         HcfObjDestroy((HcfObjectBase *)key);
972         HcfObjDestroy((HcfObjectBase *)cipher);
973     }
974 
975     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
976     EXPECT_EQ(ret, 0);
977 
978     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
979     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
980     cipherTextLen -= 16;
981 
982     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
983     EXPECT_EQ(ret, 0);
984     HcfObjDestroy((HcfObjectBase *)key);
985     HcfObjDestroy((HcfObjectBase *)cipher);
986 }
987 
988 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest021, TestSize.Level0)
989 {
990     int ret = 0;
991     uint8_t aad[8] = {0};
992     uint8_t tag[16] = {0};
993     uint8_t iv[129] = {0};
994     uint8_t cipherText[128] = {0};
995     int cipherTextLen = 128;
996 
997     HcfCipher *cipher = nullptr;
998     HcfSymKey *key = nullptr;
999 
1000     HcfGcmParamsSpec spec = {};
1001     spec.aad.data = aad;
1002     spec.aad.len = sizeof(aad);
1003     spec.tag.data = tag;
1004     spec.tag.len = sizeof(tag);
1005     spec.iv.data = iv;
1006     spec.iv.len = sizeof(iv);
1007 
1008     ret = GenerateSymKey("AES128", &key);
1009     if (ret != 0) {
1010         LOGE("generateSymKey failed!");
1011         HcfObjDestroy((HcfObjectBase *)key);
1012     }
1013 
1014     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
1015     if (ret != 0) {
1016         LOGE("HcfCipherCreate failed!");
1017         HcfObjDestroy((HcfObjectBase *)key);
1018         HcfObjDestroy((HcfObjectBase *)cipher);
1019     }
1020 
1021     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
1022     EXPECT_NE(ret, 0);
1023 
1024     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
1025     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
1026     cipherTextLen -= 16;
1027 
1028     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
1029     EXPECT_NE(ret, 0);
1030     HcfObjDestroy((HcfObjectBase *)key);
1031     HcfObjDestroy((HcfObjectBase *)cipher);
1032 }
1033 }