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