1 /*
2  * Copyright (c) 2023 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 "hks_batch_test.h"
17 #include "hks_aes_cipher_test_common.h"
18 #include "hks_access_control_test_common.h"
19 #include "hks_test_adapt_for_de.h"
20 
21 #include <gtest/gtest.h>
22 #ifdef L2_STANDARD
23 #include "file_ex.h"
24 #endif
25 #include "hks_log.h"
26 
27 using namespace testing::ext;
28 namespace Unittest::BatchTest {
29 class HksBatchTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32 
33     static void TearDownTestCase(void);
34 
35     void SetUp();
36 
37     void TearDown();
38 };
39 
SetUpTestCase(void)40 void HksBatchTest::SetUpTestCase(void)
41 {
42 }
43 
TearDownTestCase(void)44 void HksBatchTest::TearDownTestCase(void)
45 {
46 }
47 
SetUp()48 void HksBatchTest::SetUp()
49 {
50     EXPECT_EQ(HksInitialize(), 0);
51 }
52 
TearDown()53 void HksBatchTest::TearDown()
54 {
55 }
56 
57 static struct HksParam g_genParams001[] = {
58     {
59         .tag = HKS_TAG_ALGORITHM,
60         .uint32Param = HKS_ALG_AES
61     }, {
62         .tag = HKS_TAG_PURPOSE,
63         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
64     }, {
65         .tag = HKS_TAG_KEY_SIZE,
66         .uint32Param = HKS_AES_KEY_SIZE_128
67     }, {
68         .tag = HKS_TAG_PADDING,
69         .uint32Param = HKS_PADDING_NONE
70     }, {
71         .tag = HKS_TAG_BLOCK_MODE,
72         .uint32Param = HKS_MODE_GCM
73     }, {
74         .tag = HKS_TAG_KEY_AUTH_PURPOSE,
75         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
76     }, {
77         .tag = HKS_TAG_BATCH_PURPOSE,
78         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
79     }
80 };
81 static struct HksParam g_encryptParams001[] = {
82     {
83         .tag = HKS_TAG_ALGORITHM,
84         .uint32Param = HKS_ALG_AES
85     }, {
86         .tag = HKS_TAG_PURPOSE,
87         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
88     }, {
89         .tag = HKS_TAG_KEY_SIZE,
90         .uint32Param = HKS_AES_KEY_SIZE_128
91     }, {
92         .tag = HKS_TAG_DIGEST,
93         .uint32Param = HKS_DIGEST_NONE
94     }, {
95         .tag = HKS_TAG_PADDING,
96         .uint32Param = HKS_PADDING_NONE
97     }, {
98         .tag = HKS_TAG_BLOCK_MODE,
99         .uint32Param = HKS_MODE_GCM
100     }, {
101         .tag = HKS_TAG_IV,
102         .blob = {
103             .size = AesCipher::IV_SIZE,
104             .data = (uint8_t *)AesCipher::IV
105         }
106     }, {
107         .tag = HKS_TAG_ASSOCIATED_DATA,
108         .blob = {
109             .size = AesCipher::AAD_SIZE,
110             .data = (uint8_t *)AesCipher::AAD
111         }
112     }, {
113         .tag = HKS_TAG_AE_TAG,
114         .blob = {
115             .size = AesCipher::AEAD_SIZE,
116             .data = (uint8_t *)AesCipher::AEAD
117         }
118     }
119 };
120 static struct HksParam g_decryptParams001[] = {
121     {
122         .tag = HKS_TAG_ALGORITHM,
123         .uint32Param = HKS_ALG_AES
124     }, {
125         .tag = HKS_TAG_PURPOSE,
126         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
127     }, {
128         .tag = HKS_TAG_KEY_SIZE,
129         .uint32Param = HKS_AES_KEY_SIZE_128
130     }, {
131         .tag = HKS_TAG_PADDING,
132         .uint32Param = HKS_PADDING_NONE
133     }, {
134         .tag = HKS_TAG_BLOCK_MODE,
135         .uint32Param = HKS_MODE_GCM
136     }, {
137         .tag = HKS_TAG_DIGEST,
138         .uint32Param = HKS_DIGEST_NONE
139     }, {
140         .tag = HKS_TAG_ASSOCIATED_DATA,
141         .blob = {
142             .size = AesCipher::AAD_SIZE,
143             .data = (uint8_t *)AesCipher::AAD
144         }
145     }, {
146         .tag = HKS_TAG_NONCE,
147         .blob = {
148             .size = AesCipher::NONCE_SIZE,
149             .data = (uint8_t *)AesCipher::NONCE
150         }
151     }, {
152         .tag = HKS_TAG_AE_TAG,
153         .blob = {
154             .size = AesCipher::AEAD_SIZE,
155             .data = (uint8_t *)AesCipher::AEAD
156         }
157     }, {
158         .tag = HKS_TAG_IS_BATCH_OPERATION,
159         .boolParam = true
160     }, {
161         .tag = HKS_TAG_BATCH_OPERATION_TIMEOUT,
162         .uint32Param = 600
163     }, {
164         .tag = HKS_TAG_BATCH_PURPOSE,
165         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
166     }
167 };
168 
169 static struct HksParam g_genParams002[] = {
170     {
171         .tag = HKS_TAG_ALGORITHM,
172         .uint32Param = HKS_ALG_AES
173     }, {
174         .tag = HKS_TAG_PURPOSE,
175         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
176     }, {
177         .tag = HKS_TAG_KEY_SIZE,
178         .uint32Param = HKS_AES_KEY_SIZE_128
179     }, {
180         .tag = HKS_TAG_PADDING,
181         .uint32Param = HKS_PADDING_NONE
182     }, {
183         .tag = HKS_TAG_BLOCK_MODE,
184         .uint32Param = HKS_MODE_GCM
185     }, {
186         .tag = HKS_TAG_KEY_AUTH_PURPOSE,
187         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
188     }, {
189         .tag = HKS_TAG_BATCH_PURPOSE,
190         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
191     }
192 };
193 static struct HksParam g_encryptParams002[] = {
194     {
195         .tag = HKS_TAG_ALGORITHM,
196         .uint32Param = HKS_ALG_AES
197     }, {
198         .tag = HKS_TAG_PURPOSE,
199         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
200     }, {
201         .tag = HKS_TAG_KEY_SIZE,
202         .uint32Param = HKS_AES_KEY_SIZE_128
203     }, {
204         .tag = HKS_TAG_DIGEST,
205         .uint32Param = HKS_DIGEST_NONE
206     }, {
207         .tag = HKS_TAG_PADDING,
208         .uint32Param = HKS_PADDING_NONE
209     }, {
210         .tag = HKS_TAG_BLOCK_MODE,
211         .uint32Param = HKS_MODE_GCM
212     }, {
213         .tag = HKS_TAG_IV,
214         .blob = {
215             .size = AesCipher::IV_SIZE,
216             .data = (uint8_t *)AesCipher::IV
217         }
218     }, {
219         .tag = HKS_TAG_ASSOCIATED_DATA,
220         .blob = {
221             .size = AesCipher::AAD_SIZE,
222             .data = (uint8_t *)AesCipher::AAD
223         }
224     }, {
225         .tag = HKS_TAG_AE_TAG,
226         .blob = {
227             .size = AesCipher::AEAD_SIZE,
228             .data = (uint8_t *)AesCipher::AEAD
229         }
230     }
231 };
232 static struct HksParam g_decryptParams002[] = {
233     {
234         .tag = HKS_TAG_ALGORITHM,
235         .uint32Param = HKS_ALG_AES
236     }, {
237         .tag = HKS_TAG_PURPOSE,
238         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
239     }, {
240         .tag = HKS_TAG_KEY_SIZE,
241         .uint32Param = HKS_AES_KEY_SIZE_128
242     }, {
243         .tag = HKS_TAG_PADDING,
244         .uint32Param = HKS_PADDING_NONE
245     }, {
246         .tag = HKS_TAG_BLOCK_MODE,
247         .uint32Param = HKS_MODE_GCM
248     }, {
249         .tag = HKS_TAG_DIGEST,
250         .uint32Param = HKS_DIGEST_NONE
251     }, {
252         .tag = HKS_TAG_ASSOCIATED_DATA,
253         .blob = {
254             .size = AesCipher::AAD_SIZE,
255             .data = (uint8_t *)AesCipher::AAD
256         }
257     }, {
258         .tag = HKS_TAG_NONCE,
259         .blob = {
260             .size = AesCipher::NONCE_SIZE,
261             .data = (uint8_t *)AesCipher::NONCE
262         }
263     }, {
264         .tag = HKS_TAG_AE_TAG,
265         .blob = {
266             .size = AesCipher::AEAD_SIZE,
267             .data = (uint8_t *)AesCipher::AEAD
268         }
269     }, {
270         .tag = HKS_TAG_IS_BATCH_OPERATION,
271         .boolParam = true
272     }, {
273         .tag = HKS_TAG_BATCH_OPERATION_TIMEOUT,
274         .uint32Param = 0
275     }, {
276         .tag = HKS_TAG_BATCH_PURPOSE,
277         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
278     }
279 };
280 
281 static struct HksParam g_genParams003[] = {
282     {
283         .tag = HKS_TAG_ALGORITHM,
284         .uint32Param = HKS_ALG_AES
285     }, {
286         .tag = HKS_TAG_PURPOSE,
287         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
288     }, {
289         .tag = HKS_TAG_KEY_SIZE,
290         .uint32Param = HKS_AES_KEY_SIZE_128
291     }, {
292         .tag = HKS_TAG_PADDING,
293         .uint32Param = HKS_PADDING_NONE
294     }, {
295         .tag = HKS_TAG_BLOCK_MODE,
296         .uint32Param = HKS_MODE_GCM
297     }, {
298         .tag = HKS_TAG_KEY_AUTH_PURPOSE,
299         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
300     }, {
301         .tag = HKS_TAG_BATCH_PURPOSE,
302         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
303     }
304 };
305 
306 static struct HksParam g_encryptParams003[] = {
307     {
308         .tag = HKS_TAG_ALGORITHM,
309         .uint32Param = HKS_ALG_AES
310     }, {
311         .tag = HKS_TAG_PURPOSE,
312         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
313     }, {
314         .tag = HKS_TAG_KEY_SIZE,
315         .uint32Param = HKS_AES_KEY_SIZE_128
316     }, {
317         .tag = HKS_TAG_DIGEST,
318         .uint32Param = HKS_DIGEST_NONE
319     }, {
320         .tag = HKS_TAG_PADDING,
321         .uint32Param = HKS_PADDING_NONE
322     }, {
323         .tag = HKS_TAG_BLOCK_MODE,
324         .uint32Param = HKS_MODE_GCM
325     }, {
326         .tag = HKS_TAG_IV,
327         .blob = {
328             .size = AesCipher::IV_SIZE,
329             .data = (uint8_t *)AesCipher::IV
330         }
331     }, {
332         .tag = HKS_TAG_ASSOCIATED_DATA,
333         .blob = {
334             .size = AesCipher::AAD_SIZE,
335             .data = (uint8_t *)AesCipher::AAD
336         }
337     }, {
338         .tag = HKS_TAG_AE_TAG,
339         .blob = {
340             .size = AesCipher::AEAD_SIZE,
341             .data = (uint8_t *)AesCipher::AEAD
342         }
343     }
344 };
345 
346 static struct HksParam g_decryptNormalParams003[] = {
347     {
348         .tag = HKS_TAG_ALGORITHM,
349         .uint32Param = HKS_ALG_AES
350     }, {
351         .tag = HKS_TAG_PURPOSE,
352         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
353     }, {
354         .tag = HKS_TAG_KEY_SIZE,
355         .uint32Param = HKS_AES_KEY_SIZE_128
356     }, {
357         .tag = HKS_TAG_PADDING,
358         .uint32Param = HKS_PADDING_NONE
359     }, {
360         .tag = HKS_TAG_BLOCK_MODE,
361         .uint32Param = HKS_MODE_GCM
362     }, {
363         .tag = HKS_TAG_DIGEST,
364         .uint32Param = HKS_DIGEST_NONE
365     }, {
366         .tag = HKS_TAG_ASSOCIATED_DATA,
367         .blob = {
368             .size = AesCipher::AAD_SIZE,
369             .data = (uint8_t *)AesCipher::AAD
370         }
371     }, {
372         .tag = HKS_TAG_NONCE,
373         .blob = {
374             .size = AesCipher::NONCE_SIZE,
375             .data = (uint8_t *)AesCipher::NONCE
376         }
377     }, {
378         .tag = HKS_TAG_AE_TAG,
379         .blob = {
380             .size = AesCipher::AEAD_SIZE,
381             .data = (uint8_t *)AesCipher::AEAD
382         }
383     }
384 };
385 
386 static struct HksParam g_decryptBatchParams003[] = {
387     {
388         .tag = HKS_TAG_ALGORITHM,
389         .uint32Param = HKS_ALG_AES
390     }, {
391         .tag = HKS_TAG_PURPOSE,
392         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
393     }, {
394         .tag = HKS_TAG_KEY_SIZE,
395         .uint32Param = HKS_AES_KEY_SIZE_128
396     }, {
397         .tag = HKS_TAG_PADDING,
398         .uint32Param = HKS_PADDING_NONE
399     }, {
400         .tag = HKS_TAG_BLOCK_MODE,
401         .uint32Param = HKS_MODE_GCM
402     }, {
403         .tag = HKS_TAG_DIGEST,
404         .uint32Param = HKS_DIGEST_NONE
405     }, {
406         .tag = HKS_TAG_ASSOCIATED_DATA,
407         .blob = {
408             .size = AesCipher::AAD_SIZE,
409             .data = (uint8_t *)AesCipher::AAD
410         }
411     }, {
412         .tag = HKS_TAG_NONCE,
413         .blob = {
414             .size = AesCipher::NONCE_SIZE,
415             .data = (uint8_t *)AesCipher::NONCE
416         }
417     }, {
418         .tag = HKS_TAG_AE_TAG,
419         .blob = {
420             .size = AesCipher::AEAD_SIZE,
421             .data = (uint8_t *)AesCipher::AEAD
422         }
423     }, {
424         .tag = HKS_TAG_IS_BATCH_OPERATION,
425         .boolParam = true
426     }, {
427         .tag = HKS_TAG_BATCH_OPERATION_TIMEOUT,
428         .uint32Param = 600
429     }, {
430         .tag = HKS_TAG_BATCH_PURPOSE,
431         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
432     }
433 };
434 
435 static struct HksParam g_genParams004[] = {
436     {
437         .tag = HKS_TAG_ALGORITHM,
438         .uint32Param = HKS_ALG_AES
439     }, {
440         .tag = HKS_TAG_PURPOSE,
441         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
442     }, {
443         .tag = HKS_TAG_KEY_SIZE,
444         .uint32Param = HKS_AES_KEY_SIZE_128
445     }, {
446         .tag = HKS_TAG_PADDING,
447         .uint32Param = HKS_PADDING_NONE
448     }, {
449         .tag = HKS_TAG_BLOCK_MODE,
450         .uint32Param = HKS_MODE_GCM
451     }, {
452         .tag = HKS_TAG_KEY_AUTH_PURPOSE,
453         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
454     }, {
455         .tag = HKS_TAG_BATCH_PURPOSE,
456         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT | HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY
457     }
458 };
459 static struct HksParam g_encryptParams004[] = {
460     {
461         .tag = HKS_TAG_ALGORITHM,
462         .uint32Param = HKS_ALG_AES
463     }, {
464         .tag = HKS_TAG_PURPOSE,
465         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
466     }, {
467         .tag = HKS_TAG_KEY_SIZE,
468         .uint32Param = HKS_AES_KEY_SIZE_128
469     }, {
470         .tag = HKS_TAG_DIGEST,
471         .uint32Param = HKS_DIGEST_NONE
472     }, {
473         .tag = HKS_TAG_PADDING,
474         .uint32Param = HKS_PADDING_NONE
475     }, {
476         .tag = HKS_TAG_BLOCK_MODE,
477         .uint32Param = HKS_MODE_GCM
478     }, {
479         .tag = HKS_TAG_IV,
480         .blob = {
481             .size = AesCipher::IV_SIZE,
482             .data = (uint8_t *)AesCipher::IV
483         }
484     }, {
485         .tag = HKS_TAG_ASSOCIATED_DATA,
486         .blob = {
487             .size = AesCipher::AAD_SIZE,
488             .data = (uint8_t *)AesCipher::AAD
489         }
490     }, {
491         .tag = HKS_TAG_AE_TAG,
492         .blob = {
493             .size = AesCipher::AEAD_SIZE,
494             .data = (uint8_t *)AesCipher::AEAD
495         }
496     }
497 };
498 static struct HksParam g_decryptParams004[] = {
499     {
500         .tag = HKS_TAG_ALGORITHM,
501         .uint32Param = HKS_ALG_AES
502     }, {
503         .tag = HKS_TAG_PURPOSE,
504         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
505     }, {
506         .tag = HKS_TAG_KEY_SIZE,
507         .uint32Param = HKS_AES_KEY_SIZE_128
508     }, {
509         .tag = HKS_TAG_PADDING,
510         .uint32Param = HKS_PADDING_NONE
511     }, {
512         .tag = HKS_TAG_BLOCK_MODE,
513         .uint32Param = HKS_MODE_GCM
514     }, {
515         .tag = HKS_TAG_DIGEST,
516         .uint32Param = HKS_DIGEST_NONE
517     }, {
518         .tag = HKS_TAG_ASSOCIATED_DATA,
519         .blob = {
520             .size = AesCipher::AAD_SIZE,
521             .data = (uint8_t *)AesCipher::AAD
522         }
523     }, {
524         .tag = HKS_TAG_NONCE,
525         .blob = {
526             .size = AesCipher::NONCE_SIZE,
527             .data = (uint8_t *)AesCipher::NONCE
528         }
529     }, {
530         .tag = HKS_TAG_AE_TAG,
531         .blob = {
532             .size = AesCipher::AEAD_SIZE,
533             .data = (uint8_t *)AesCipher::AEAD
534         }
535     }, {
536         .tag = HKS_TAG_IS_BATCH_OPERATION,
537         .boolParam = true
538     }, {
539         .tag = HKS_TAG_BATCH_OPERATION_TIMEOUT,
540         .uint32Param = 600
541     }, {
542         .tag = HKS_TAG_BATCH_PURPOSE,
543         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
544     }
545 };
546 
547 static struct HksParam g_genParams005[] = {
548     {
549         .tag = HKS_TAG_ALGORITHM,
550         .uint32Param = HKS_ALG_AES
551     }, {
552         .tag = HKS_TAG_PURPOSE,
553         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
554     }, {
555         .tag = HKS_TAG_KEY_SIZE,
556         .uint32Param = HKS_AES_KEY_SIZE_128
557     }, {
558         .tag = HKS_TAG_PADDING,
559         .uint32Param = HKS_PADDING_NONE
560     }, {
561         .tag = HKS_TAG_BLOCK_MODE,
562         .uint32Param = HKS_MODE_GCM
563     }, {
564         .tag = HKS_TAG_KEY_AUTH_PURPOSE,
565         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
566     }, {
567         .tag = HKS_TAG_BATCH_PURPOSE,
568         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
569     }
570 };
571 static struct HksParam g_encryptParams005[] = {
572     {
573         .tag = HKS_TAG_ALGORITHM,
574         .uint32Param = HKS_ALG_AES
575     }, {
576         .tag = HKS_TAG_PURPOSE,
577         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
578     }, {
579         .tag = HKS_TAG_KEY_SIZE,
580         .uint32Param = HKS_AES_KEY_SIZE_128
581     }, {
582         .tag = HKS_TAG_DIGEST,
583         .uint32Param = HKS_DIGEST_NONE
584     }, {
585         .tag = HKS_TAG_PADDING,
586         .uint32Param = HKS_PADDING_NONE
587     }, {
588         .tag = HKS_TAG_BLOCK_MODE,
589         .uint32Param = HKS_MODE_GCM
590     }, {
591         .tag = HKS_TAG_IV,
592         .blob = {
593             .size = AesCipher::IV_SIZE,
594             .data = (uint8_t *)AesCipher::IV
595         }
596     }, {
597         .tag = HKS_TAG_ASSOCIATED_DATA,
598         .blob = {
599             .size = AesCipher::AAD_SIZE,
600             .data = (uint8_t *)AesCipher::AAD
601         }
602     }, {
603         .tag = HKS_TAG_AE_TAG,
604         .blob = {
605             .size = AesCipher::AEAD_SIZE,
606             .data = (uint8_t *)AesCipher::AEAD
607         }
608     }
609 };
610 static struct HksParam g_decryptParams005[] = {
611     {
612         .tag = HKS_TAG_ALGORITHM,
613         .uint32Param = HKS_ALG_AES
614     }, {
615         .tag = HKS_TAG_PURPOSE,
616         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
617     }, {
618         .tag = HKS_TAG_KEY_SIZE,
619         .uint32Param = HKS_AES_KEY_SIZE_128
620     }, {
621         .tag = HKS_TAG_PADDING,
622         .uint32Param = HKS_PADDING_NONE
623     }, {
624         .tag = HKS_TAG_BLOCK_MODE,
625         .uint32Param = HKS_MODE_GCM
626     }, {
627         .tag = HKS_TAG_DIGEST,
628         .uint32Param = HKS_DIGEST_NONE
629     }, {
630         .tag = HKS_TAG_ASSOCIATED_DATA,
631         .blob = {
632             .size = AesCipher::AAD_SIZE,
633             .data = (uint8_t *)AesCipher::AAD
634         }
635     }, {
636         .tag = HKS_TAG_NONCE,
637         .blob = {
638             .size = AesCipher::NONCE_SIZE,
639             .data = (uint8_t *)AesCipher::NONCE
640         }
641     }, {
642         .tag = HKS_TAG_AE_TAG,
643         .blob = {
644             .size = AesCipher::AEAD_SIZE,
645             .data = (uint8_t *)AesCipher::AEAD
646         }
647     }, {
648         .tag = HKS_TAG_IS_BATCH_OPERATION,
649         .boolParam = true
650     }, {
651         .tag = HKS_TAG_BATCH_OPERATION_TIMEOUT,
652         .uint32Param = 600
653     }, {
654         .tag = HKS_TAG_BATCH_PURPOSE,
655         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
656     }
657 };
658 
659 static struct HksParam g_genParams006[] = {
660     {
661         .tag = HKS_TAG_ALGORITHM,
662         .uint32Param = HKS_ALG_AES
663     }, {
664         .tag = HKS_TAG_PURPOSE,
665         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
666     }, {
667         .tag = HKS_TAG_KEY_SIZE,
668         .uint32Param = HKS_AES_KEY_SIZE_128
669     }, {
670         .tag = HKS_TAG_PADDING,
671         .uint32Param = HKS_PADDING_NONE
672     }, {
673         .tag = HKS_TAG_BLOCK_MODE,
674         .uint32Param = HKS_MODE_GCM
675     }, {
676         .tag = HKS_TAG_KEY_AUTH_PURPOSE,
677         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
678     }, {
679         .tag = HKS_TAG_BATCH_PURPOSE,
680         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
681     }
682 };
683 static struct HksParam g_encryptParams006[] = {
684     {
685         .tag = HKS_TAG_ALGORITHM,
686         .uint32Param = HKS_ALG_AES
687     }, {
688         .tag = HKS_TAG_PURPOSE,
689         .uint32Param = HKS_KEY_PURPOSE_ENCRYPT
690     }, {
691         .tag = HKS_TAG_KEY_SIZE,
692         .uint32Param = HKS_AES_KEY_SIZE_128
693     }, {
694         .tag = HKS_TAG_DIGEST,
695         .uint32Param = HKS_DIGEST_NONE
696     }, {
697         .tag = HKS_TAG_PADDING,
698         .uint32Param = HKS_PADDING_NONE
699     }, {
700         .tag = HKS_TAG_BLOCK_MODE,
701         .uint32Param = HKS_MODE_GCM
702     }, {
703         .tag = HKS_TAG_IV,
704         .blob = {
705             .size = AesCipher::IV_SIZE,
706             .data = (uint8_t *)AesCipher::IV
707         }
708     }, {
709         .tag = HKS_TAG_ASSOCIATED_DATA,
710         .blob = {
711             .size = AesCipher::AAD_SIZE,
712             .data = (uint8_t *)AesCipher::AAD
713         }
714     }, {
715         .tag = HKS_TAG_AE_TAG,
716         .blob = {
717             .size = AesCipher::AEAD_SIZE,
718             .data = (uint8_t *)AesCipher::AEAD
719         }
720     }
721 };
722 static struct HksParam g_decryptParams006[] = {
723     {
724         .tag = HKS_TAG_ALGORITHM,
725         .uint32Param = HKS_ALG_AES
726     }, {
727         .tag = HKS_TAG_PURPOSE,
728         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
729     }, {
730         .tag = HKS_TAG_KEY_SIZE,
731         .uint32Param = HKS_AES_KEY_SIZE_128
732     }, {
733         .tag = HKS_TAG_PADDING,
734         .uint32Param = HKS_PADDING_NONE
735     }, {
736         .tag = HKS_TAG_BLOCK_MODE,
737         .uint32Param = HKS_MODE_GCM
738     }, {
739         .tag = HKS_TAG_DIGEST,
740         .uint32Param = HKS_DIGEST_NONE
741     }, {
742         .tag = HKS_TAG_ASSOCIATED_DATA,
743         .blob = {
744             .size = AesCipher::AAD_SIZE,
745             .data = (uint8_t *)AesCipher::AAD
746         }
747     }, {
748         .tag = HKS_TAG_NONCE,
749         .blob = {
750             .size = AesCipher::NONCE_SIZE,
751             .data = (uint8_t *)AesCipher::NONCE
752         }
753     }, {
754         .tag = HKS_TAG_AE_TAG,
755         .blob = {
756             .size = AesCipher::AEAD_SIZE,
757             .data = (uint8_t *)AesCipher::AEAD
758         }
759     }, {
760         .tag = HKS_TAG_BATCH_OPERATION_TIMEOUT,
761         .uint32Param = 600
762     }, {
763         .tag = HKS_TAG_BATCH_PURPOSE,
764         .uint32Param = HKS_KEY_PURPOSE_DECRYPT
765     }
766 };
767 
768 
769 #ifdef L2_STANDARD
770 /**
771  * @tc.name: HksBatchTest.HksBatchTest001
772  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-GCM pad-NONE size-128.
773  * @tc.type: FUNC
774  */
775 HWTEST_F(HksBatchTest, HksBatchTest001, TestSize.Level0)
776 {
777     char tmpKeyAlias[] = "HksBatchTest001";
778     struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
779 
780     struct HksParamSet *genParamSet = nullptr;
781     int32_t ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(HksParam));
782     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
783 
784     struct HksParamSet *encryptParamSet = nullptr;
785     ret = InitParamSet(&encryptParamSet, g_encryptParams001, sizeof(g_encryptParams001) / sizeof(HksParam));
786     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(encrypt) failed.";
787 
788     struct HksParamSet *decryptParamSet = nullptr;
789     ret = InitParamSet(&decryptParamSet, g_decryptParams001, sizeof(g_decryptParams001) / sizeof(HksParam));
790     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(decrypt) failed.";
791 
792     ret = AesCipher::HksAesCipherTestCaseGcm4(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, false);
793     EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
794 
795     HksFreeParamSet(&genParamSet);
796     HksFreeParamSet(&encryptParamSet);
797     HksFreeParamSet(&decryptParamSet);
798 }
799 
800 /**
801  * @tc.name: HksBatchTest.HksBatchTest002
802  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-GCM pad-NONE size-128.
803  * @tc.type: FUNC
804  */
805 HWTEST_F(HksBatchTest, HksBatchTest002, TestSize.Level0)
806 {
807     char tmpKeyAlias[] = "HksBatchTest002";
808     struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
809 
810     struct HksParamSet *genParamSet = nullptr;
811     int32_t ret = InitParamSet(&genParamSet, g_genParams002, sizeof(g_genParams002) / sizeof(HksParam));
812     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
813 
814     struct HksParamSet *encryptParamSet = nullptr;
815     ret = InitParamSet(&encryptParamSet, g_encryptParams002, sizeof(g_encryptParams002) / sizeof(HksParam));
816     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(encrypt) failed.";
817 
818     struct HksParamSet *decryptParamSet = nullptr;
819     ret = InitParamSet(&decryptParamSet, g_decryptParams002, sizeof(g_decryptParams002) / sizeof(HksParam));
820     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(decrypt) failed.";
821 
822     ret = AesCipher::HksAesCipherTestCaseGcm4(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, true);
823     EXPECT_EQ(ret, HKS_ERROR_INVALID_TIME_OUT) << "this case failed.";
824 
825     HksFreeParamSet(&genParamSet);
826     HksFreeParamSet(&encryptParamSet);
827     HksFreeParamSet(&decryptParamSet);
828 }
829 
InitParamSetForHksBatchTest003(struct HksParamSet ** genParamSet,struct HksParamSet ** encryptParamSet,struct HksParamSet ** decNormaLParamSet,struct HksParamSet ** decryptBatchParamSet)830 static int32_t InitParamSetForHksBatchTest003(struct HksParamSet **genParamSet, struct HksParamSet **encryptParamSet,
831     struct HksParamSet **decNormaLParamSet, struct HksParamSet **decryptBatchParamSet)
832 {
833     int32_t ret = InitParamSet(genParamSet, g_genParams003,
834         sizeof(g_genParams003) / sizeof(HksParam));
835     if (ret != HKS_SUCCESS) {
836         return ret;
837     }
838 
839     ret = InitParamSet(encryptParamSet, g_encryptParams003,
840         sizeof(g_encryptParams003) / sizeof(HksParam));
841     if (ret != HKS_SUCCESS) {
842         return ret;
843     }
844 
845     ret = InitParamSet(decNormaLParamSet, g_decryptNormalParams003,
846         sizeof(g_decryptNormalParams003) / sizeof(HksParam));
847     if (ret != HKS_SUCCESS) {
848         return ret;
849     }
850 
851     ret = InitParamSet(decryptBatchParamSet, g_decryptBatchParams003,
852         sizeof(g_decryptBatchParams003) / sizeof(HksParam));
853     if (ret != HKS_SUCCESS) {
854         return ret;
855     }
856 
857     return HKS_SUCCESS;
858 }
859 
860 /**
861  * @tc.name: HksBatchTest.HksBatchTest003
862  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-GCM pad-NONE size-128.
863  * @tc.type: FUNC
864  */
865 HWTEST_F(HksBatchTest, HksBatchTest003, TestSize.Level0)
866 {
867     char tmpKeyAlias[] = "HksBatchTest003";
868     struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
869 
870     struct HksParamSet *genParamSet = nullptr;
871     struct HksParamSet *encryptParamSet = nullptr;
872     struct HksParamSet *decNormaLParamSet = nullptr;
873     struct HksParamSet *decryptBatchParamSet = nullptr;
874     int32_t ret = InitParamSetForHksBatchTest003(&genParamSet, &encryptParamSet, &decNormaLParamSet,
875         &decryptBatchParamSet);
876     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
877 
878     /* 1. Generate Key */
879     ret = HksGenerateKeyForDe(&keyAlias, genParamSet, nullptr);
880     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
881 
882     static const std::string tmp_inData1 = "Hks_string1";
883     struct HksBlob inData1 = { tmp_inData1.length(),
884         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(tmp_inData1.c_str())) };
885 
886     static const std::string tmp_inData2 = "Hks_string2";
887     struct HksBlob inData2 = { tmp_inData2.length(),
888         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(tmp_inData2.c_str())) };
889 
890     uint8_t cipher1[AesCipher::AES_COMMON_SIZE] = {0};
891     struct HksBlob cipherText1 = { AesCipher::AES_COMMON_SIZE, cipher1 };
892 
893     uint8_t cipher2[AesCipher::AES_COMMON_SIZE] = {0};
894     struct HksBlob cipherText2 = { AesCipher::AES_COMMON_SIZE, cipher2 };
895 
896     ret = AesCipher::HksAesEncryptThreeStage(&keyAlias, encryptParamSet, &inData1, &cipherText1);
897     EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
898 
899     ret = AesCipher::HksAesEncryptThreeStage(&keyAlias, encryptParamSet, &inData2, &cipherText2);
900     EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
901 
902     uint8_t plainNormal1[AesCipher::AES_COMMON_SIZE] = {0};
903     struct HksBlob plainTextNormal1 = { AesCipher::AES_COMMON_SIZE, plainNormal1 };
904 
905     uint8_t plainNormal2[AesCipher::AES_COMMON_SIZE] = {0};
906     struct HksBlob plainTextNormal2 = { AesCipher::AES_COMMON_SIZE, plainNormal2 };
907 
908     ret = AesCipher::HksAesDecryptThreeStage(&keyAlias, decNormaLParamSet, &inData1, &cipherText1, &plainTextNormal1);
909     EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
910 
911     ret = AesCipher::HksAesDecryptThreeStage(&keyAlias, decNormaLParamSet, &inData2, &cipherText2, &plainTextNormal2);
912     EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
913 
914     uint8_t plainBathc1[AesCipher::AES_COMMON_SIZE] = {0};
915     struct HksBlob plainTextBatch1 = { AesCipher::AES_COMMON_SIZE, plainBathc1 };
916 
917     uint8_t plainBatch2[AesCipher::AES_COMMON_SIZE] = {0};
918     struct HksBlob plainTextBatch2 = { AesCipher::AES_COMMON_SIZE, plainBatch2 };
919 
920     ret = AesCipher::HksAesDecryptForBatch(&keyAlias, decryptBatchParamSet, &inData1, &cipherText1, &plainTextBatch1,
921         &inData2, &cipherText2, &plainTextBatch2);
922     EXPECT_EQ(ret, HKS_SUCCESS) << "this case failed.";
923 
924     ret = HksDeleteKeyForDe(&keyAlias, genParamSet);
925     EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
926 
927     HksFreeParamSet(&genParamSet);
928     HksFreeParamSet(&encryptParamSet);
929     HksFreeParamSet(&decNormaLParamSet);
930     HksFreeParamSet(&decryptBatchParamSet);
931 }
932 
933 /**
934  * @tc.name: HksBatchTest.HksBatchTest004
935  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-GCM pad-NONE size-128.
936  * @tc.type: FUNC
937  */
938 HWTEST_F(HksBatchTest, HksBatchTest004, TestSize.Level0)
939 {
940     char tmpKeyAlias[] = "HksBatchTest004";
941     struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
942 
943     struct HksParamSet *genParamSet = nullptr;
944     int32_t ret = InitParamSet(&genParamSet, g_genParams004, sizeof(g_genParams004) / sizeof(HksParam));
945     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
946 
947     struct HksParamSet *encryptParamSet = nullptr;
948     ret = InitParamSet(&encryptParamSet, g_encryptParams004, sizeof(g_encryptParams004) / sizeof(HksParam));
949     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(encrypt) failed.";
950 
951     struct HksParamSet *decryptParamSet = nullptr;
952     ret = InitParamSet(&decryptParamSet, g_decryptParams004, sizeof(g_decryptParams004) / sizeof(HksParam));
953     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(decrypt) failed.";
954 
955     ret = AesCipher::HksAesCipherTestCaseGcm4(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, false);
956     EXPECT_EQ(ret, HKS_ERROR_INVALID_PURPOSE) << "this case failed.";
957 
958     HksFreeParamSet(&genParamSet);
959     HksFreeParamSet(&encryptParamSet);
960     HksFreeParamSet(&decryptParamSet);
961 }
962 
963 /**
964  * @tc.name: HksBatchTest.HksBatchTest005
965  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-GCM pad-NONE size-128.
966  * @tc.type: FUNC
967  */
968 HWTEST_F(HksBatchTest, HksBatchTest005, TestSize.Level0)
969 {
970     char tmpKeyAlias[] = "HksBatchTest005";
971     struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
972 
973     struct HksParamSet *genParamSet = nullptr;
974     int32_t ret = InitParamSet(&genParamSet, g_genParams005, sizeof(g_genParams005) / sizeof(HksParam));
975     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
976 
977     struct HksParamSet *encryptParamSet = nullptr;
978     ret = InitParamSet(&encryptParamSet, g_encryptParams005, sizeof(g_encryptParams005) / sizeof(HksParam));
979     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(encrypt) failed.";
980 
981     struct HksParamSet *decryptParamSet = nullptr;
982     ret = InitParamSet(&decryptParamSet, g_decryptParams005, sizeof(g_decryptParams005) / sizeof(HksParam));
983     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(decrypt) failed.";
984 
985     ret = AesCipher::HksAesCipherTestCaseGcm4(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, false);
986     EXPECT_EQ(ret, HKS_ERROR_INVALID_PURPOSE) << "this case failed.";
987 
988     HksFreeParamSet(&genParamSet);
989     HksFreeParamSet(&encryptParamSet);
990     HksFreeParamSet(&decryptParamSet);
991 }
992 
993 /**
994  * @tc.name: HksBatchTest.HksBatchTest006
995  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-GCM pad-NONE size-128.
996  * @tc.type: FUNC
997  */
998 HWTEST_F(HksBatchTest, HksBatchTest006, TestSize.Level0)
999 {
1000     char tmpKeyAlias[] = "HksBatchTest006";
1001     struct HksBlob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
1002 
1003     struct HksParamSet *genParamSet = nullptr;
1004     int32_t ret = InitParamSet(&genParamSet, g_genParams006, sizeof(g_genParams006) / sizeof(HksParam));
1005     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(gen) failed.";
1006 
1007     struct HksParamSet *encryptParamSet = nullptr;
1008     ret = InitParamSet(&encryptParamSet, g_encryptParams006, sizeof(g_encryptParams006) / sizeof(HksParam));
1009     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(encrypt) failed.";
1010 
1011     struct HksParamSet *decryptParamSet = nullptr;
1012     ret = InitParamSet(&decryptParamSet, g_decryptParams006, sizeof(g_decryptParams006) / sizeof(HksParam));
1013     EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet(decrypt) failed.";
1014 
1015     ret = AesCipher::HksAesCipherTestCaseGcm4(&keyAlias, genParamSet, encryptParamSet, decryptParamSet, false);
1016     EXPECT_EQ(ret, HKS_ERROR_NOT_SUPPORTED) << "this case failed.";
1017 
1018     HksFreeParamSet(&genParamSet);
1019     HksFreeParamSet(&encryptParamSet);
1020     HksFreeParamSet(&decryptParamSet);
1021 }
1022 #endif
1023 
1024 } // namespace Unittest::BatchTest