• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     uint8_t aad[8] = {0};
48     uint8_t tag[16] = {0};
49     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
50     uint8_t cipherText[128] = {0};
51     int cipherTextLen = 128;
52 
53     HcfCipher *cipher = nullptr;
54     HcfSymKey *key = nullptr;
55 
56     HcfGcmParamsSpec 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     int ret = GenerateSymKey("AES128", &key);
65     ASSERT_EQ(ret, 0);
66 
67     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
68     ASSERT_EQ(ret, 0);
69 
70     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
71     ASSERT_EQ(ret, 0);
72 
73     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
74     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
75     cipherTextLen -= 16;
76 
77     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
78     ASSERT_EQ(ret, 0);
79 
80     HcfObjDestroy((HcfObjectBase *)key);
81     HcfObjDestroy((HcfObjectBase *)cipher);
82 }
83 
84 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest002, TestSize.Level0)
85 {
86     uint8_t aad[8] = {0};
87     uint8_t tag[16] = {0};
88     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
89     uint8_t cipherText[128] = {0};
90     int cipherTextLen = 128;
91 
92     HcfCipher *cipher = nullptr;
93     HcfSymKey *key = nullptr;
94 
95     HcfGcmParamsSpec spec = {};
96     spec.aad.data = aad;
97     spec.aad.len = sizeof(aad);
98     spec.tag.data = tag;
99     spec.tag.len = sizeof(tag);
100     spec.iv.data = iv;
101     spec.iv.len = sizeof(iv);
102 
103     int ret = GenerateSymKey("AES128", &key);
104     ASSERT_EQ(ret, 0);
105 
106     ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
107     ASSERT_EQ(ret, 0);
108 
109     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
110     ASSERT_EQ(ret, 0);
111 
112     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
113     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
114     cipherTextLen -= 16;
115 
116     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
117     ASSERT_EQ(ret, 0);
118 
119     HcfObjDestroy((HcfObjectBase *)key);
120     HcfObjDestroy((HcfObjectBase *)cipher);
121 }
122 
123 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest003, TestSize.Level0)
124 {
125     uint8_t aad[8] = {0};
126     uint8_t tag[16] = {0};
127     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
128     uint8_t cipherText[128] = {0};
129     int cipherTextLen = 128;
130 
131     HcfCipher *cipher = nullptr;
132     HcfSymKey *key = nullptr;
133 
134     HcfGcmParamsSpec spec = {};
135     spec.aad.data = aad;
136     spec.aad.len = sizeof(aad);
137     spec.tag.data = tag;
138     spec.tag.len = sizeof(tag);
139     spec.iv.data = iv;
140     spec.iv.len = sizeof(iv);
141 
142     int ret = GenerateSymKey("AES128", &key);
143     ASSERT_EQ(ret, 0);
144 
145     ret = HcfCipherCreate("AES128|GCM|PKCS7", &cipher);
146     ASSERT_EQ(ret, 0);
147 
148     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
149     ASSERT_EQ(ret, 0);
150 
151     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
152     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
153     cipherTextLen -= 16;
154 
155     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
156     ASSERT_EQ(ret, 0);
157 
158     HcfObjDestroy((HcfObjectBase *)key);
159     HcfObjDestroy((HcfObjectBase *)cipher);
160 }
161 
162 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest004, TestSize.Level0)
163 {
164     uint8_t aad[8] = {0};
165     uint8_t tag[16] = {0};
166     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
167     uint8_t cipherText[128] = {0};
168     int cipherTextLen = 128;
169 
170     HcfCipher *cipher = nullptr;
171     HcfSymKey *key = nullptr;
172 
173     HcfGcmParamsSpec spec = {};
174     spec.aad.data = aad;
175     spec.aad.len = sizeof(aad);
176     spec.tag.data = tag;
177     spec.tag.len = sizeof(tag);
178     spec.iv.data = iv;
179     spec.iv.len = sizeof(iv);
180 
181     int ret = GenerateSymKey("AES128", &key);
182     ASSERT_EQ(ret, 0);
183 
184     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
185     ASSERT_EQ(ret, 0);
186 
187     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
188     ASSERT_EQ(ret, 0);
189 
190     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
191     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
192     cipherTextLen -= 16;
193 
194     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
195     ASSERT_EQ(ret, 0);
196 
197     HcfObjDestroy((HcfObjectBase *)key);
198     HcfObjDestroy((HcfObjectBase *)cipher);
199 }
200 
201 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest005, TestSize.Level0)
202 {
203     uint8_t aad[8] = {0};
204     uint8_t tag[16] = {0};
205     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
206     uint8_t cipherText[128] = {0};
207     int cipherTextLen = 128;
208 
209     HcfCipher *cipher = nullptr;
210     HcfSymKey *key = nullptr;
211 
212     HcfGcmParamsSpec spec = {};
213     spec.aad.data = aad;
214     spec.aad.len = sizeof(aad);
215     spec.tag.data = tag;
216     spec.tag.len = sizeof(tag);
217     spec.iv.data = iv;
218     spec.iv.len = sizeof(iv);
219 
220     int ret = GenerateSymKey("AES128", &key);
221     ASSERT_EQ(ret, 0);
222 
223     ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
224     ASSERT_EQ(ret, 0);
225 
226     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
227     ASSERT_EQ(ret, 0);
228 
229     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
230     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
231     cipherTextLen -= 16;
232 
233     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
234     ASSERT_EQ(ret, 0);
235 
236     HcfObjDestroy((HcfObjectBase *)key);
237     HcfObjDestroy((HcfObjectBase *)cipher);
238 }
239 
240 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest006, TestSize.Level0)
241 {
242     uint8_t aad[8] = {0};
243     uint8_t tag[16] = {0};
244     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
245     uint8_t cipherText[128] = {0};
246     int cipherTextLen = 128;
247 
248     HcfCipher *cipher = nullptr;
249     HcfSymKey *key = nullptr;
250 
251     HcfGcmParamsSpec spec = {};
252     spec.aad.data = aad;
253     spec.aad.len = sizeof(aad);
254     spec.tag.data = tag;
255     spec.tag.len = sizeof(tag);
256     spec.iv.data = iv;
257     spec.iv.len = sizeof(iv);
258 
259     int ret = GenerateSymKey("AES128", &key);
260     ASSERT_EQ(ret, 0);
261 
262     ret = HcfCipherCreate("AES128|GCM|PKCS7", &cipher);
263     ASSERT_EQ(ret, 0);
264 
265     ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
266     ASSERT_EQ(ret, 0);
267 
268     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
269     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
270     cipherTextLen -= 16;
271 
272     ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
273     ASSERT_EQ(ret, 0);
274 
275     HcfObjDestroy((HcfObjectBase *)key);
276     HcfObjDestroy((HcfObjectBase *)cipher);
277 }
278 
279 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest007, TestSize.Level0)
280 {
281     uint8_t aad[GCM_AAD_LEN] = { 0 };
282     uint8_t tag[GCM_TAG_LEN] = { 0 };
283     uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes
284     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
285     int cipherTextLen = CIPHER_TEXT_LEN;
286 
287     HcfCipher *cipher = nullptr;
288     HcfSymKey *key = nullptr;
289 
290     HcfGcmParamsSpec spec = {};
291     spec.aad.data = aad;
292     spec.aad.len = sizeof(aad);
293     spec.tag.data = tag;
294     spec.tag.len = sizeof(tag);
295     spec.iv.data = iv;
296     spec.iv.len = sizeof(iv);
297 
298     int ret = GenerateSymKey("AES192", &key);
299     ASSERT_EQ(ret, 0);
300 
301     ret = HcfCipherCreate("AES192|GCM|PKCS5", &cipher);
302     ASSERT_EQ(ret, 0);
303 
304     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
305     ASSERT_EQ(ret, 0);
306 
307     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
308     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
309     cipherTextLen -= GCM_TAG_LEN;
310 
311     ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
312     ASSERT_EQ(ret, 0);
313 
314     HcfObjDestroy(key);
315     HcfObjDestroy(cipher);
316 }
317 
318 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest008, TestSize.Level0)
319 {
320     uint8_t aad[GCM_AAD_LEN] = { 0 };
321     uint8_t tag[GCM_TAG_LEN] = { 0 };
322     uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes
323     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
324     int cipherTextLen = CIPHER_TEXT_LEN;
325 
326     HcfCipher *cipher = nullptr;
327     HcfSymKey *key = nullptr;
328 
329     HcfGcmParamsSpec spec = {};
330     spec.aad.data = aad;
331     spec.aad.len = sizeof(aad);
332     spec.tag.data = tag;
333     spec.tag.len = sizeof(tag);
334     spec.iv.data = iv;
335     spec.iv.len = sizeof(iv);
336 
337     int ret = GenerateSymKey("AES256", &key);
338     ASSERT_EQ(ret, 0);
339 
340     ret = HcfCipherCreate("AES256|GCM|PKCS5", &cipher);
341     ASSERT_EQ(ret, 0);
342 
343     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
344     ASSERT_EQ(ret, 0);
345 
346     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
347     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
348     cipherTextLen -= GCM_TAG_LEN;
349 
350     ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
351     ASSERT_EQ(ret, 0);
352 
353     HcfObjDestroy(key);
354     HcfObjDestroy(cipher);
355 }
356 
357 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest009, TestSize.Level0)
358 {
359     int ret = 0;
360     HcfCipher *cipher = nullptr;
361 
362     ret = HcfCipherCreate("RSA128|GCM|NoPadding", &cipher);
363     if (ret != 0) {
364         LOGE("HcfCipherCreate failed! Should not select RSA for GCM generator.");
365     }
366 
367     HcfObjDestroy(cipher);
368     EXPECT_NE(ret, 0);
369 }
370 
371 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest010, TestSize.Level0)
372 {
373     int ret = 0;
374     HcfCipher *cipher = nullptr;
375 
376     // not allow '|' without content, because findAbility will fail for "" input
377     ret = HcfCipherCreate("AES128|GCM|", &cipher);
378     if (ret != 0) {
379         LOGE("HcfCipherCreate failed! Should select padding mode for AES generator.");
380     }
381 
382     HcfObjDestroy(cipher);
383     EXPECT_NE(ret, 0);
384 }
385 
386 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest011, TestSize.Level0)
387 {
388     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
389     int cipherTextLen = CIPHER_TEXT_LEN;
390     HcfCipher *cipher = nullptr;
391     HcfSymKey *key = nullptr;
392 
393     int ret = GenerateSymKey("AES128", &key);
394     ASSERT_EQ(ret, 0);
395 
396     ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
397     ASSERT_EQ(ret, 0);
398 
399     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
400     ASSERT_NE(ret, 0);
401 
402     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
403     ASSERT_NE(ret, 0);
404 
405     HcfObjDestroy(key);
406     HcfObjDestroy(cipher);
407 }
408 
409 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest012, TestSize.Level0)
410 {
411     uint8_t aad[GCM_AAD_LEN] = { 0 };
412     uint8_t tag[GCM_TAG_LEN] = { 0 };
413     uint8_t iv[GCM_IV_LEN] = { 0 };
414     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
415     int cipherTextLen = CIPHER_TEXT_LEN;
416 
417     HcfCipher *cipher = nullptr;
418     HcfSymKey *key = nullptr;
419 
420     HcfGcmParamsSpec spec = {};
421     spec.aad.data = nullptr;
422     spec.aad.len = sizeof(aad);
423     spec.tag.data = tag;
424     spec.tag.len = sizeof(tag);
425     spec.iv.data = iv;
426     spec.iv.len = sizeof(iv);
427 
428     int ret = GenerateSymKey("AES128", &key);
429     ASSERT_EQ(ret, 0);
430 
431     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
432     ASSERT_EQ(ret, 0);
433 
434     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
435     ASSERT_EQ(ret, 0);
436 
437     HcfObjDestroy(key);
438     HcfObjDestroy(cipher);
439 }
440 
441 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest013, TestSize.Level0)
442 {
443     uint8_t aad[GCM_AAD_LEN] = { 0 };
444     uint8_t tag[GCM_TAG_LEN] = { 0 };
445     uint8_t iv[GCM_IV_LEN] = { 0 };
446     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
447     int cipherTextLen = CIPHER_TEXT_LEN;
448 
449     HcfCipher *cipher = nullptr;
450     HcfSymKey *key = nullptr;
451 
452     HcfGcmParamsSpec spec = {};
453     spec.aad.data = aad;
454     spec.aad.len = sizeof(aad);
455     spec.tag.data = tag;
456     spec.tag.len = sizeof(tag);
457     spec.iv.data = nullptr;
458     spec.iv.len = sizeof(iv);
459 
460     int ret = GenerateSymKey("AES128", &key);
461     ASSERT_EQ(ret, 0);
462 
463     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
464     ASSERT_EQ(ret, 0);
465 
466     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
467     ASSERT_NE(ret, 0);
468 
469     HcfObjDestroy(key);
470     HcfObjDestroy(cipher);
471 }
472 
473 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest014, TestSize.Level0)
474 {
475     uint8_t aad[GCM_AAD_LEN] = { 0 };
476     uint8_t tag[GCM_TAG_LEN] = { 0 };
477     uint8_t iv[GCM_IV_LEN] = { 0 };
478     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
479     int cipherTextLen = CIPHER_TEXT_LEN;
480 
481     HcfCipher *cipher = nullptr;
482     HcfSymKey *key = nullptr;
483 
484     HcfGcmParamsSpec spec = {};
485     spec.aad.data = aad;
486     spec.aad.len = sizeof(aad);
487     spec.tag.data = nullptr;
488     spec.tag.len = sizeof(tag);
489     spec.iv.data = iv;
490     spec.iv.len = sizeof(iv);
491 
492     int ret = GenerateSymKey("AES128", &key);
493     ASSERT_EQ(ret, 0);
494 
495     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
496     ASSERT_EQ(ret, 0);
497 
498     ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
499     ASSERT_NE(ret, 0);
500 
501     HcfObjDestroy(key);
502     HcfObjDestroy(cipher);
503 }
504 
505 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest015, TestSize.Level0)
506 {
507     uint8_t tag[GCM_TAG_LEN] = {0};
508     uint8_t iv[GCM_IV_LEN] = {0};
509     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
510     int cipherTextLen = CIPHER_TEXT_LEN;
511 
512     HcfCipher *cipher = nullptr;
513     HcfSymKey *key = nullptr;
514 
515     HcfGcmParamsSpec spec = {};
516     spec.aad.data = nullptr;
517     spec.aad.len = 0;
518     spec.tag.data = tag;
519     spec.tag.len = sizeof(tag);
520     spec.iv.data = iv;
521     spec.iv.len = sizeof(iv);
522 
523     int ret = GenerateSymKey("AES128", &key);
524     ASSERT_EQ(ret, 0);
525 
526     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
527     ASSERT_EQ(ret, 0);
528 
529     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
530     ASSERT_EQ(ret, 0);
531 
532     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
533     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
534     cipherTextLen -= GCM_TAG_LEN;
535 
536     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
537     ASSERT_EQ(ret, 0);
538 
539     HcfObjDestroy((HcfObjectBase *)key);
540     HcfObjDestroy((HcfObjectBase *)cipher);
541 }
542 
543 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest016, TestSize.Level0)
544 {
545     uint8_t aad[GCM_AAD_LONG_LEN] = { 0 };
546     uint8_t tag[GCM_TAG_LEN] = {0};
547     uint8_t iv[GCM_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 
554     HcfGcmParamsSpec spec = {};
555     spec.aad.data = aad;
556     spec.aad.len = sizeof(aad);
557     spec.tag.data = tag;
558     spec.tag.len = sizeof(tag);
559     spec.iv.data = iv;
560     spec.iv.len = sizeof(iv);
561 
562     int ret = GenerateSymKey("AES128", &key);
563     ASSERT_EQ(ret, 0);
564 
565     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
566     ASSERT_EQ(ret, 0);
567 
568     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
569     ASSERT_EQ(ret, 0);
570 
571     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
572     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
573     cipherTextLen -= GCM_TAG_LEN;
574 
575     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
576     ASSERT_EQ(ret, 0);
577 
578     HcfObjDestroy((HcfObjectBase *)key);
579     HcfObjDestroy((HcfObjectBase *)cipher);
580 }
581 
582 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest017, TestSize.Level0)
583 {
584     uint8_t aad[GCM_AAD_LONG_LEN] = { 0 };
585     uint8_t tag[GCM_TAG_LEN] = {0};
586     uint8_t iv[GCM_IV_LONG_LEN] = {0};
587     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
588     int cipherTextLen = CIPHER_TEXT_LEN;
589 
590     HcfCipher *cipher = nullptr;
591     HcfSymKey *key = nullptr;
592 
593     HcfGcmParamsSpec spec = {};
594     spec.aad.data = aad;
595     spec.aad.len = sizeof(aad);
596     spec.tag.data = tag;
597     spec.tag.len = sizeof(tag);
598     spec.iv.data = iv;
599     spec.iv.len = sizeof(iv);
600 
601     int ret = GenerateSymKey("AES128", &key);
602     ASSERT_EQ(ret, 0);
603 
604     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
605     ASSERT_EQ(ret, 0);
606 
607     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
608     ASSERT_EQ(ret, 0);
609 
610     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
611     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
612     cipherTextLen -= GCM_TAG_LEN;
613 
614     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
615     ASSERT_EQ(ret, 0);
616 
617     HcfObjDestroy((HcfObjectBase *)key);
618     HcfObjDestroy((HcfObjectBase *)cipher);
619 }
620 
621 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest018, TestSize.Level0)
622 {
623     uint8_t aad[GCM_AAD_SHORT_LEN] = { 0 };
624     uint8_t tag[GCM_TAG_LEN] = {0};
625     // openssl only support ivLen [9, 16];
626     uint8_t iv[GCM_IV_SHORT_LEN] = {0};
627     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
628     int cipherTextLen = CIPHER_TEXT_LEN;
629 
630     HcfCipher *cipher = nullptr;
631     HcfSymKey *key = nullptr;
632 
633     HcfGcmParamsSpec spec = {};
634     spec.aad.data = aad;
635     spec.aad.len = sizeof(aad);
636     spec.tag.data = tag;
637     spec.tag.len = sizeof(tag);
638     spec.iv.data = iv;
639     spec.iv.len = sizeof(iv);
640 
641     int ret = GenerateSymKey("AES128", &key);
642     ASSERT_EQ(ret, 0);
643 
644     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
645     ASSERT_EQ(ret, 0);
646 
647     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
648     ASSERT_EQ(ret, 0);
649 
650     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
651     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
652     cipherTextLen -= GCM_TAG_LEN;
653 
654     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
655     ASSERT_EQ(ret, 0);
656 
657     HcfObjDestroy((HcfObjectBase *)key);
658     HcfObjDestroy((HcfObjectBase *)cipher);
659 }
660 
661 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest019, TestSize.Level0)
662 {
663     uint8_t tag[GCM_TAG_LEN] = {0};
664     uint8_t iv[GCM_IV_LONG_LEN] = {0};
665     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
666     int cipherTextLen = CIPHER_TEXT_LEN;
667 
668     HcfCipher *cipher = nullptr;
669     HcfSymKey *key = nullptr;
670 
671     HcfGcmParamsSpec spec = {};
672     spec.aad.data = nullptr;
673     spec.aad.len = 0;
674     spec.tag.data = tag;
675     spec.tag.len = sizeof(tag);
676     spec.iv.data = iv;
677     spec.iv.len = sizeof(iv);
678 
679     int ret = GenerateSymKey("AES128", &key);
680     ASSERT_EQ(ret, 0);
681 
682     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
683     ASSERT_EQ(ret, 0);
684 
685     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
686     ASSERT_EQ(ret, 0);
687 
688     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
689     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
690     cipherTextLen -= GCM_TAG_LEN;
691 
692     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
693     ASSERT_EQ(ret, 0);
694 
695     HcfObjDestroy((HcfObjectBase *)key);
696     HcfObjDestroy((HcfObjectBase *)cipher);
697 }
698 
699 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest020, TestSize.Level0)
700 {
701     int ret = 0;
702     uint8_t aad[8] = {0};
703     uint8_t tag[16] = {0};
704     uint8_t iv[128] = {0}; // openssl support iv max 128 bytes
705     uint8_t cipherText[128] = {0};
706     int cipherTextLen = 128;
707 
708     HcfCipher *cipher = nullptr;
709     HcfSymKey *key = nullptr;
710 
711     HcfGcmParamsSpec spec = {};
712     spec.aad.data = aad;
713     spec.aad.len = sizeof(aad);
714     spec.tag.data = tag;
715     spec.tag.len = sizeof(tag);
716     spec.iv.data = iv;
717     spec.iv.len = sizeof(iv);
718 
719     ret = GenerateSymKey("AES128", &key);
720     if (ret != 0) {
721         LOGE("generateSymKey failed!");
722         HcfObjDestroy((HcfObjectBase *)key);
723     }
724 
725     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
726     if (ret != 0) {
727         LOGE("HcfCipherCreate failed!");
728         HcfObjDestroy((HcfObjectBase *)key);
729         HcfObjDestroy((HcfObjectBase *)cipher);
730     }
731 
732     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
733     EXPECT_EQ(ret, 0);
734 
735     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
736     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
737     cipherTextLen -= 16;
738 
739     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
740     EXPECT_EQ(ret, 0);
741     HcfObjDestroy((HcfObjectBase *)key);
742     HcfObjDestroy((HcfObjectBase *)cipher);
743 }
744 
745 HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest021, TestSize.Level0)
746 {
747     int ret = 0;
748     uint8_t aad[8] = {0};
749     uint8_t tag[16] = {0};
750     uint8_t iv[129] = {0};
751     uint8_t cipherText[128] = {0};
752     int cipherTextLen = 128;
753 
754     HcfCipher *cipher = nullptr;
755     HcfSymKey *key = nullptr;
756 
757     HcfGcmParamsSpec spec = {};
758     spec.aad.data = aad;
759     spec.aad.len = sizeof(aad);
760     spec.tag.data = tag;
761     spec.tag.len = sizeof(tag);
762     spec.iv.data = iv;
763     spec.iv.len = sizeof(iv);
764 
765     ret = GenerateSymKey("AES128", &key);
766     if (ret != 0) {
767         LOGE("generateSymKey failed!");
768         HcfObjDestroy((HcfObjectBase *)key);
769     }
770 
771     ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
772     if (ret != 0) {
773         LOGE("HcfCipherCreate failed!");
774         HcfObjDestroy((HcfObjectBase *)key);
775         HcfObjDestroy((HcfObjectBase *)cipher);
776     }
777 
778     ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
779     EXPECT_NE(ret, 0);
780 
781     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
782     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
783     cipherTextLen -= 16;
784 
785     ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
786     EXPECT_NE(ret, 0);
787     HcfObjDestroy((HcfObjectBase *)key);
788     HcfObjDestroy((HcfObjectBase *)cipher);
789 }
790 }