• 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 "sm4_common.h"
22 #include "sm4_openssl.h"
23 #include "aes_common.h"
24 #include "aes_openssl.h"
25 #include "blob.h"
26 #include "cipher.h"
27 #include "detailed_iv_params.h"
28 #include "detailed_gcm_params.h"
29 #include "detailed_ccm_params.h"
30 #include "log.h"
31 #include "memory.h"
32 #include "sym_common_defines.h"
33 #include "sym_key_generator.h"
34 
35 using namespace std;
36 using namespace testing::ext;
37 
38 namespace {
39 class CryptoSM4GcmCipherTest : public testing::Test {
40 public:
SetUpTestCase()41     static void SetUpTestCase() {};
TearDownTestCase()42     static void TearDownTestCase() {};
SetUp()43     void SetUp() {};
TearDown()44     void TearDown() {};
45 };
46 
47 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest001, TestSize.Level0)
48 {
49     int ret = 0;
50     uint8_t aad[8] = {0};
51     uint8_t tag[16] = {0};
52     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
53     uint8_t cipherText[128] = {0};
54     int cipherTextLen = 128;
55 
56     HcfCipher *cipher = nullptr;
57     HcfSymKey *key = nullptr;
58 
59     HcfGcmParamsSpec spec = {};
60     spec.aad.data = aad;
61     spec.aad.len = sizeof(aad);
62     spec.tag.data = tag;
63     spec.tag.len = sizeof(tag);
64     spec.iv.data = iv;
65     spec.iv.len = sizeof(iv);
66 
67     ret = GenerateSymKey("SM4_128", &key);
68     ASSERT_EQ(ret, 0);
69 
70     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
71     ASSERT_EQ(ret, 0);
72 
73     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
74     ASSERT_EQ(ret, 0);
75 
76     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
77     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
78     cipherTextLen -= 16;
79 
80     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
81     ASSERT_EQ(ret, 0);
82 
83     HcfObjDestroy((HcfObjectBase *)key);
84     HcfObjDestroy((HcfObjectBase *)cipher);
85 }
86 
87 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest002, TestSize.Level0)
88 {
89     int ret = 0;
90     uint8_t aad[8] = {0};
91     uint8_t tag[16] = {0};
92     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
93     uint8_t cipherText[128] = {0};
94     int cipherTextLen = 128;
95 
96     HcfCipher *cipher = nullptr;
97     HcfSymKey *key = nullptr;
98 
99     HcfGcmParamsSpec spec = {};
100     spec.aad.data = aad;
101     spec.aad.len = sizeof(aad);
102     spec.tag.data = tag;
103     spec.tag.len = sizeof(tag);
104     spec.iv.data = iv;
105     spec.iv.len = sizeof(iv);
106 
107     ret = GenerateSymKey("SM4_128", &key);
108     ASSERT_EQ(ret, 0);
109 
110     ret = HcfCipherCreate("SM4_128|GCM|PKCS5", &cipher);
111     ASSERT_EQ(ret, 0);
112 
113     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
114     ASSERT_EQ(ret, 0);
115 
116     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
117     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
118     cipherTextLen -= 16;
119 
120     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
121     ASSERT_EQ(ret, 0);
122 
123     HcfObjDestroy((HcfObjectBase *)key);
124     HcfObjDestroy((HcfObjectBase *)cipher);
125 }
126 
127 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest003, TestSize.Level0)
128 {
129     int ret = 0;
130     uint8_t aad[8] = {0};
131     uint8_t tag[16] = {0};
132     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
133     uint8_t cipherText[128] = {0};
134     int cipherTextLen = 128;
135 
136     HcfCipher *cipher = nullptr;
137     HcfSymKey *key = nullptr;
138 
139     HcfGcmParamsSpec spec = {};
140     spec.aad.data = aad;
141     spec.aad.len = sizeof(aad);
142     spec.tag.data = tag;
143     spec.tag.len = sizeof(tag);
144     spec.iv.data = iv;
145     spec.iv.len = sizeof(iv);
146 
147     ret = GenerateSymKey("SM4_128", &key);
148     ASSERT_EQ(ret, 0);
149 
150     ret = HcfCipherCreate("SM4_128|GCM|PKCS7", &cipher);
151     ASSERT_EQ(ret, 0);
152 
153     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
154     ASSERT_EQ(ret, 0);
155 
156     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
157     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
158     cipherTextLen -= 16;
159 
160     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
161     ASSERT_EQ(ret, 0);
162 
163     HcfObjDestroy((HcfObjectBase *)key);
164     HcfObjDestroy((HcfObjectBase *)cipher);
165 }
166 
167 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest004, TestSize.Level0)
168 {
169     int ret = 0;
170     uint8_t aad[8] = {0};
171     uint8_t tag[16] = {0};
172     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
173     uint8_t cipherText[128] = {0};
174     int cipherTextLen = 128;
175 
176     HcfCipher *cipher = nullptr;
177     HcfSymKey *key = nullptr;
178 
179     HcfGcmParamsSpec spec = {};
180     spec.aad.data = aad;
181     spec.aad.len = sizeof(aad);
182     spec.tag.data = tag;
183     spec.tag.len = sizeof(tag);
184     spec.iv.data = iv;
185     spec.iv.len = sizeof(iv);
186 
187     ret = GenerateSymKey("SM4_128", &key);
188     ASSERT_EQ(ret, 0);
189 
190     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
191     ASSERT_EQ(ret, 0);
192 
193     ret = Sm4NoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
194     ASSERT_EQ(ret, 0);
195 
196     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
197     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
198     cipherTextLen -= 16;
199 
200     ret = Sm4NoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
201     ASSERT_EQ(ret, 0);
202 
203     HcfObjDestroy((HcfObjectBase *)key);
204     HcfObjDestroy((HcfObjectBase *)cipher);
205 }
206 
207 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest005, 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("SM4_128", &key);
228     ASSERT_EQ(ret, 0);
229 
230     ret = HcfCipherCreate("SM4_128|GCM|PKCS5", &cipher);
231     ASSERT_EQ(ret, 0);
232 
233     ret = Sm4NoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
234     ASSERT_EQ(ret, 0);
235 
236     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
237     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
238     cipherTextLen -= 16;
239 
240     ret = Sm4NoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
241     ASSERT_EQ(ret, 0);
242 
243     HcfObjDestroy((HcfObjectBase *)key);
244     HcfObjDestroy((HcfObjectBase *)cipher);
245 }
246 
247 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest006, TestSize.Level0)
248 {
249     int ret = 0;
250     uint8_t aad[8] = {0};
251     uint8_t tag[16] = {0};
252     uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
253     uint8_t cipherText[128] = {0};
254     int cipherTextLen = 128;
255 
256     HcfCipher *cipher = nullptr;
257     HcfSymKey *key = nullptr;
258 
259     HcfGcmParamsSpec spec = {};
260     spec.aad.data = aad;
261     spec.aad.len = sizeof(aad);
262     spec.tag.data = tag;
263     spec.tag.len = sizeof(tag);
264     spec.iv.data = iv;
265     spec.iv.len = sizeof(iv);
266 
267     ret = GenerateSymKey("SM4_128", &key);
268     ASSERT_EQ(ret, 0);
269 
270     ret = HcfCipherCreate("SM4_128|GCM|PKCS7", &cipher);
271     ASSERT_EQ(ret, 0);
272 
273     ret = Sm4NoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
274     ASSERT_EQ(ret, 0);
275 
276     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
277     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
278     cipherTextLen -= 16;
279 
280     ret = Sm4NoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
281     ASSERT_EQ(ret, 0);
282 
283     HcfObjDestroy((HcfObjectBase *)key);
284     HcfObjDestroy((HcfObjectBase *)cipher);
285 }
286 
287 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest009, TestSize.Level0)
288 {
289     int ret = 0;
290     HcfCipher *cipher = nullptr;
291 
292     ret = HcfCipherCreate("RSA128|GCM|NoPadding", &cipher);
293     if (ret != 0) {
294         LOGE("HcfCipherCreate failed! Should not select RSA for GCM generator.");
295     }
296 
297     HcfObjDestroy(cipher);
298     EXPECT_NE(ret, 0);
299 }
300 
301 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest010, TestSize.Level0)
302 {
303     int ret = 0;
304     HcfCipher *cipher = nullptr;
305 
306     // not allow '|' without content, because findAbility will fail for "" input
307     ret = HcfCipherCreate("SM4_128|GCM|", &cipher);
308     if (ret != 0) {
309         LOGE("HcfCipherCreate failed! Should select padding mode for SM4_128 generator.");
310     }
311 
312     HcfObjDestroy(cipher);
313     EXPECT_NE(ret, 0);
314 }
315 
316 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest011, TestSize.Level0)
317 {
318     int ret = 0;
319     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
320     int cipherTextLen = CIPHER_TEXT_LEN;
321     HcfCipher *cipher = nullptr;
322     HcfSymKey *key = nullptr;
323 
324     ret = GenerateSymKey("SM4_128", &key);
325     ASSERT_EQ(ret, 0);
326 
327     // CBC, CTR, OFB, CFB enc/dec success,
328     // GCM, CCM enc/dec failed with params set to nullptr.
329     ret = HcfCipherCreate("SM4_128|GCM|PKCS5", &cipher);
330     ASSERT_EQ(ret, 0);
331 
332     ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
333     ASSERT_NE(ret, 0);
334 
335     ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen);
336     ASSERT_NE(ret, 0);
337 
338     HcfObjDestroy(key);
339     HcfObjDestroy(cipher);
340 }
341 
342 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest012, TestSize.Level0)
343 {
344     int ret = 0;
345     uint8_t aad[GCM_AAD_LEN] = { 0 };
346     uint8_t tag[GCM_TAG_LEN] = { 0 };
347     uint8_t iv[GCM_IV_LEN] = { 0 };
348     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
349     int cipherTextLen = CIPHER_TEXT_LEN;
350 
351     HcfCipher *cipher = nullptr;
352     HcfSymKey *key = nullptr;
353 
354     HcfGcmParamsSpec spec = {};
355     spec.aad.data = nullptr;
356     spec.aad.len = sizeof(aad);
357     spec.tag.data = tag;
358     spec.tag.len = sizeof(tag);
359     spec.iv.data = iv;
360     spec.iv.len = sizeof(iv);
361 
362     ret = GenerateSymKey("SM4_128", &key);
363     ASSERT_EQ(ret, 0);
364 
365     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
366     ASSERT_EQ(ret, 0);
367 
368     ret = Sm4Encrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
369     ASSERT_EQ(ret, 0);
370 
371     HcfObjDestroy(key);
372     HcfObjDestroy(cipher);
373 }
374 
375 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest013, TestSize.Level0)
376 {
377     int ret = 0;
378     uint8_t aad[GCM_AAD_LEN] = { 0 };
379     uint8_t tag[GCM_TAG_LEN] = { 0 };
380     uint8_t iv[GCM_IV_LEN] = { 0 };
381     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
382     int cipherTextLen = CIPHER_TEXT_LEN;
383 
384     HcfCipher *cipher = nullptr;
385     HcfSymKey *key = nullptr;
386 
387     HcfGcmParamsSpec spec = {};
388     spec.aad.data = aad;
389     spec.aad.len = sizeof(aad);
390     spec.tag.data = tag;
391     spec.tag.len = sizeof(tag);
392     spec.iv.data = nullptr;
393     spec.iv.len = sizeof(iv);
394 
395     ret = GenerateSymKey("SM4_128", &key);
396     ASSERT_EQ(ret, 0);
397 
398     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
399     ASSERT_EQ(ret, 0);
400 
401     ret = Sm4Encrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
402     ASSERT_NE(ret, 0);
403 
404     HcfObjDestroy(key);
405     HcfObjDestroy(cipher);
406 }
407 
408 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest014, TestSize.Level0)
409 {
410     int ret = 0;
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 = aad;
422     spec.aad.len = sizeof(aad);
423     spec.tag.data = nullptr;
424     spec.tag.len = sizeof(tag);
425     spec.iv.data = iv;
426     spec.iv.len = sizeof(iv);
427 
428     ret = GenerateSymKey("SM4_128", &key);
429     ASSERT_EQ(ret, 0);
430 
431     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
432     ASSERT_EQ(ret, 0);
433 
434     ret = Sm4Encrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
435     ASSERT_NE(ret, 0);
436 
437     HcfObjDestroy(key);
438     HcfObjDestroy(cipher);
439 }
440 
441 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest015, TestSize.Level0)
442 {
443     int ret = 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 = nullptr;
454     spec.aad.len = 0;
455     spec.tag.data = tag;
456     spec.tag.len = sizeof(tag);
457     spec.iv.data = iv;
458     spec.iv.len = sizeof(iv);
459 
460     ret = GenerateSymKey("SM4_128", &key);
461     ASSERT_EQ(ret, 0);
462 
463     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
464     ASSERT_EQ(ret, 0);
465 
466     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
467     ASSERT_EQ(ret, 0);
468 
469     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
470     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
471     cipherTextLen -= GCM_TAG_LEN;
472 
473     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
474     ASSERT_EQ(ret, 0);
475 
476     HcfObjDestroy((HcfObjectBase *)key);
477     HcfObjDestroy((HcfObjectBase *)cipher);
478 }
479 
480 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest016, TestSize.Level0)
481 {
482     int ret = 0;
483     uint8_t aad[GCM_AAD_LONG_LEN] = { 0 };
484     uint8_t tag[GCM_TAG_LEN] = {0};
485     uint8_t iv[GCM_IV_LEN] = {0};
486     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
487     int cipherTextLen = CIPHER_TEXT_LEN;
488 
489     HcfCipher *cipher = nullptr;
490     HcfSymKey *key = nullptr;
491 
492     HcfGcmParamsSpec spec = {};
493     spec.aad.data = aad;
494     spec.aad.len = sizeof(aad);
495     spec.tag.data = tag;
496     spec.tag.len = sizeof(tag);
497     spec.iv.data = iv;
498     spec.iv.len = sizeof(iv);
499 
500     ret = GenerateSymKey("SM4_128", &key);
501     ASSERT_EQ(ret, 0);
502 
503     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
504     ASSERT_EQ(ret, 0);
505 
506     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
507     ASSERT_EQ(ret, 0);
508 
509     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
510     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
511     cipherTextLen -= GCM_TAG_LEN;
512 
513     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
514     ASSERT_EQ(ret, 0);
515 
516     HcfObjDestroy((HcfObjectBase *)key);
517     HcfObjDestroy((HcfObjectBase *)cipher);
518 }
519 
520 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest017, TestSize.Level0)
521 {
522     int ret = 0;
523     uint8_t aad[GCM_AAD_LONG_LEN] = { 0 };
524     uint8_t tag[GCM_TAG_LEN] = {0};
525     uint8_t iv[GCM_IV_LONG_LEN] = {0};
526     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
527     int cipherTextLen = CIPHER_TEXT_LEN;
528 
529     HcfCipher *cipher = nullptr;
530     HcfSymKey *key = nullptr;
531 
532     HcfGcmParamsSpec spec = {};
533     spec.aad.data = aad;
534     spec.aad.len = sizeof(aad);
535     spec.tag.data = tag;
536     spec.tag.len = sizeof(tag);
537     spec.iv.data = iv;
538     spec.iv.len = sizeof(iv);
539 
540     ret = GenerateSymKey("SM4_128", &key);
541     ASSERT_EQ(ret, 0);
542 
543     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
544     ASSERT_EQ(ret, 0);
545 
546     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
547     ASSERT_EQ(ret, 0);
548 
549     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
550     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
551     cipherTextLen -= GCM_TAG_LEN;
552 
553     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
554     ASSERT_EQ(ret, 0);
555 
556     HcfObjDestroy((HcfObjectBase *)key);
557     HcfObjDestroy((HcfObjectBase *)cipher);
558 }
559 
560 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest018, TestSize.Level0)
561 {
562     int ret = 0;
563     uint8_t aad[GCM_AAD_SHORT_LEN] = { 0 };
564     uint8_t tag[GCM_TAG_LEN] = {0};
565     // openssl only support ivLen [9, 16];
566     uint8_t iv[GCM_IV_SHORT_LEN] = {0};
567     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
568     int cipherTextLen = CIPHER_TEXT_LEN;
569 
570     HcfCipher *cipher = nullptr;
571     HcfSymKey *key = nullptr;
572 
573     HcfGcmParamsSpec spec = {};
574     spec.aad.data = aad;
575     spec.aad.len = sizeof(aad);
576     spec.tag.data = tag;
577     spec.tag.len = sizeof(tag);
578     spec.iv.data = iv;
579     spec.iv.len = sizeof(iv);
580 
581     ret = GenerateSymKey("SM4_128", &key);
582     ASSERT_EQ(ret, 0);
583 
584     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
585     ASSERT_EQ(ret, 0);
586 
587     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
588     ASSERT_EQ(ret, 0);
589 
590     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
591     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
592     cipherTextLen -= GCM_TAG_LEN;
593 
594     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
595     ASSERT_EQ(ret, 0);
596 
597     HcfObjDestroy((HcfObjectBase *)key);
598     HcfObjDestroy((HcfObjectBase *)cipher);
599 }
600 
601 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest019, TestSize.Level0)
602 {
603     int ret = 0;
604     uint8_t tag[GCM_TAG_LEN] = {0};
605     uint8_t iv[GCM_IV_LONG_LEN] = {0};
606     uint8_t cipherText[CIPHER_TEXT_LEN] = {0};
607     int cipherTextLen = CIPHER_TEXT_LEN;
608 
609     HcfCipher *cipher = nullptr;
610     HcfSymKey *key = nullptr;
611 
612     HcfGcmParamsSpec spec = {};
613     spec.aad.data = nullptr;
614     spec.aad.len = 0;
615     spec.tag.data = tag;
616     spec.tag.len = sizeof(tag);
617     spec.iv.data = iv;
618     spec.iv.len = sizeof(iv);
619 
620     ret = GenerateSymKey("SM4_128", &key);
621     ASSERT_EQ(ret, 0);
622 
623     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
624     ASSERT_EQ(ret, 0);
625 
626     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
627     ASSERT_EQ(ret, 0);
628 
629     (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
630     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
631     cipherTextLen -= GCM_TAG_LEN;
632 
633     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
634     ASSERT_EQ(ret, 0);
635 
636     HcfObjDestroy((HcfObjectBase *)key);
637     HcfObjDestroy((HcfObjectBase *)cipher);
638 }
639 
640 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest020, TestSize.Level0)
641 {
642     int ret = 0;
643     uint8_t aad[8] = {0};
644     uint8_t tag[16] = {0};
645     uint8_t iv[128] = {0}; // openssl support iv max 128 bytes
646     uint8_t cipherText[128] = {0};
647     int cipherTextLen = 128;
648 
649     HcfCipher *cipher = nullptr;
650     HcfSymKey *key = nullptr;
651 
652     HcfGcmParamsSpec spec = {};
653     spec.aad.data = aad;
654     spec.aad.len = sizeof(aad);
655     spec.tag.data = tag;
656     spec.tag.len = sizeof(tag);
657     spec.iv.data = iv;
658     spec.iv.len = sizeof(iv);
659 
660     ret = GenerateSymKey("SM4_128", &key);
661     ASSERT_EQ(ret, 0);
662 
663     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
664     ASSERT_EQ(ret, 0);
665 
666     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
667     ASSERT_EQ(ret, 0);
668 
669     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
670     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
671     cipherTextLen -= 16;
672 
673     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
674     ASSERT_EQ(ret, 0);
675 
676     HcfObjDestroy((HcfObjectBase *)key);
677     HcfObjDestroy((HcfObjectBase *)cipher);
678 }
679 
680 HWTEST_F(CryptoSM4GcmCipherTest, CryptoSM4GcmCipherTest021, TestSize.Level0)
681 {
682     int ret = 0;
683     uint8_t aad[8] = {0};
684     uint8_t tag[16] = {0};
685     uint8_t iv[129] = {0};
686     uint8_t cipherText[128] = {0};
687     int cipherTextLen = 128;
688 
689     HcfCipher *cipher = nullptr;
690     HcfSymKey *key = nullptr;
691 
692     HcfGcmParamsSpec spec = {};
693     spec.aad.data = aad;
694     spec.aad.len = sizeof(aad);
695     spec.tag.data = tag;
696     spec.tag.len = sizeof(tag);
697     spec.iv.data = iv;
698     spec.iv.len = sizeof(iv);
699 
700     ret = GenerateSymKey("SM4_128", &key);
701     ASSERT_EQ(ret, 0);
702 
703     ret = HcfCipherCreate("SM4_128|GCM|NoPadding", &cipher);
704     ASSERT_EQ(ret, 0);
705 
706     ret = Sm4Encrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
707     ASSERT_NE(ret, 0);
708 
709     (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
710     PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
711     cipherTextLen -= 16;
712 
713     ret = Sm4Decrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
714     ASSERT_NE(ret, 0);
715 
716     HcfObjDestroy((HcfObjectBase *)key);
717     HcfObjDestroy((HcfObjectBase *)cipher);
718 }
719 }