• 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 CryptoAesEcbCipherTest : 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 /**
46  * @tc.name: CryptoAesEcbCipherTest.CryptoAesEcbCipherTest001
47  * @tc.desc: Verify whether the crypto framework is normal.
48  * @tc.type: FUNC
49  * @tc.require: I5QWEO
50  */
51 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest001, TestSize.Level0)
52 {
53     uint8_t cipherText[128] = {0};
54     int cipherTextLen = 128;
55 
56     HcfSymKeyGenerator *generator = nullptr;
57     HcfCipher *cipher = nullptr;
58     HcfSymKey *key = nullptr;
59 
60     int ret = HcfSymKeyGeneratorCreate("AES128", &generator);
61     ASSERT_EQ(ret, 0);
62 
63     ret = generator->generateSymKey(generator, &key);
64     ASSERT_EQ(ret, 0);
65 
66     ret = HcfCipherCreate("AES128|ECB|NoPadding", &cipher);
67     ASSERT_EQ(ret, 0);
68 
69     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
70     ASSERT_NE(ret, 0);
71 
72     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
73     ASSERT_NE(ret, 0);
74 
75     HcfObjDestroy((HcfObjectBase *)key);
76     HcfObjDestroy((HcfObjectBase *)cipher);
77     HcfObjDestroy((HcfObjectBase *)generator);
78 }
79 
80 /**
81  * @tc.name: CryptoAesEcbCipherTest.CryptoAesEcbCipherTest002
82  * @tc.desc: Verify AES128 cipher algorithm.
83  * @tc.type: FUNC
84  * @tc.require: I5QWEG
85  */
86 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest002, TestSize.Level0)
87 {
88     uint8_t cipherText[128] = {0};
89     int cipherTextLen = 128;
90 
91     HcfSymKeyGenerator *generator = nullptr;
92     HcfCipher *cipher = nullptr;
93     HcfSymKey *key = nullptr;
94 
95     int ret = HcfSymKeyGeneratorCreate("AES128", &generator);
96     ASSERT_EQ(ret, 0);
97 
98     ret = generator->generateSymKey(generator, &key);
99     ASSERT_EQ(ret, 0);
100 
101     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
102     ASSERT_EQ(ret, 0);
103 
104     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
105     ASSERT_EQ(ret, 0);
106 
107     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
108     ASSERT_EQ(ret, 0);
109 
110     HcfObjDestroy((HcfObjectBase *)key);
111     HcfObjDestroy((HcfObjectBase *)cipher);
112     HcfObjDestroy((HcfObjectBase *)generator);
113 }
114 
115 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest003, TestSize.Level0)
116 {
117     uint8_t cipherText[128] = {0};
118     int cipherTextLen = 128;
119 
120     HcfSymKeyGenerator *generator = nullptr;
121     HcfCipher *cipher = nullptr;
122     HcfSymKey *key = nullptr;
123     int ret = HcfSymKeyGeneratorCreate("AES128", &generator);
124     ASSERT_EQ(ret, 0);
125 
126     ret = generator->generateSymKey(generator, &key);
127     ASSERT_EQ(ret, 0);
128 
129     ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
130     ASSERT_EQ(ret, 0);
131 
132     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
133     ASSERT_EQ(ret, 0);
134 
135     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
136     ASSERT_EQ(ret, 0);
137 
138     HcfObjDestroy((HcfObjectBase *)key);
139     HcfObjDestroy((HcfObjectBase *)cipher);
140     HcfObjDestroy((HcfObjectBase *)generator);
141 }
142 
143 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest004, TestSize.Level0)
144 {
145     uint8_t cipherText[128] = {0};
146     int cipherTextLen = 128;
147 
148     HcfCipher *cipher = nullptr;
149     HcfSymKey *key = nullptr;
150     uint8_t codeCipherText[] = {
151         0xF5, 0x12, 0xA0, 0x33, 0xCD, 0xCF, 0x0D, 0x32,
152         0x3E, 0xFF, 0x80, 0x53, 0x89, 0xB6, 0xE4, 0xFE
153     };
154 
155     int ret = ConvertSymKey("AES128", &key);
156     ASSERT_EQ(ret, 0);
157 
158     ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
159     ASSERT_EQ(ret, 0);
160 
161     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
162     ASSERT_EQ(ret, 0);
163 
164     ret = memcmp(cipherText, codeCipherText, cipherTextLen);
165     ASSERT_EQ(ret, 0);
166 
167     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
168     ASSERT_EQ(ret, 0);
169 
170     HcfObjDestroy((HcfObjectBase *)key);
171     HcfObjDestroy((HcfObjectBase *)cipher);
172 }
173 
174 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest005, TestSize.Level0)
175 {
176     uint8_t cipherText[128] = {0};
177     int cipherTextLen = 128;
178 
179     HcfSymKeyGenerator *generator = nullptr;
180     HcfCipher *cipher = nullptr;
181     HcfSymKey *key = nullptr;
182 
183     int ret = HcfSymKeyGeneratorCreate("AES128", &generator);
184     ASSERT_EQ(ret, 0);
185 
186     ret = generator->generateSymKey(generator, &key);
187     ASSERT_EQ(ret, 0);
188 
189     ret = HcfCipherCreate("AES128|ECB|NoPadding", &cipher);
190     ASSERT_EQ(ret, 0);
191 
192     ret = AesNoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
193     ASSERT_NE(ret, 0);
194 
195     ret = AesNoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
196     ASSERT_NE(ret, 0);
197 
198     HcfObjDestroy((HcfObjectBase *)key);
199     HcfObjDestroy((HcfObjectBase *)cipher);
200     HcfObjDestroy((HcfObjectBase *)generator);
201 }
202 
203 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest006, TestSize.Level0)
204 {
205     uint8_t cipherText[128] = {0};
206     int cipherTextLen = 128;
207 
208     HcfSymKeyGenerator *generator = nullptr;
209     HcfCipher *cipher = nullptr;
210     HcfSymKey *key = nullptr;
211 
212     int ret = HcfSymKeyGeneratorCreate("AES128", &generator);
213     ASSERT_EQ(ret, 0);
214 
215     ret = generator->generateSymKey(generator, &key);
216     ASSERT_EQ(ret, 0);
217 
218     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
219     ASSERT_EQ(ret, 0);
220 
221     ret = AesNoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
222     ASSERT_EQ(ret, 0);
223 
224     ret = AesNoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
225     ASSERT_EQ(ret, 0);
226 
227     HcfObjDestroy((HcfObjectBase *)key);
228     HcfObjDestroy((HcfObjectBase *)cipher);
229     HcfObjDestroy((HcfObjectBase *)generator);
230 }
231 
232 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest007, TestSize.Level0)
233 {
234     uint8_t cipherText[128] = {0};
235     int cipherTextLen = 128;
236 
237     HcfSymKeyGenerator *generator = nullptr;
238     HcfCipher *cipher = nullptr;
239     HcfSymKey *key = nullptr;
240 
241     int ret = HcfSymKeyGeneratorCreate("AES128", &generator);
242     ASSERT_EQ(ret, 0);
243 
244     ret = generator->generateSymKey(generator, &key);
245     ASSERT_EQ(ret, 0);
246 
247     ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
248     ASSERT_EQ(ret, 0);
249 
250     ret = AesNoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
251     ASSERT_EQ(ret, 0);
252 
253     ret = AesNoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
254     ASSERT_EQ(ret, 0);
255 
256     HcfObjDestroy((HcfObjectBase *)key);
257     HcfObjDestroy((HcfObjectBase *)cipher);
258     HcfObjDestroy((HcfObjectBase *)generator);
259 }
260 
261 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest008, TestSize.Level0)
262 {
263     uint8_t cipherText[128] = {0};
264     int cipherTextLen = 128;
265 
266     HcfCipher *cipher = nullptr;
267     HcfSymKey *key = nullptr;
268     uint8_t codeCipherText[] = {
269         0xF5, 0x12, 0xA0, 0x33, 0xCD, 0xCF, 0x0D, 0x32,
270         0x3E, 0xFF, 0x80, 0x53, 0x89, 0xB6, 0xE4, 0xFE
271     };
272 
273     int ret = ConvertSymKey("AES128", &key);
274     ASSERT_EQ(ret, 0);
275 
276     ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
277     ASSERT_EQ(ret, 0);
278 
279     ret = AesNoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
280     ASSERT_EQ(ret, 0);
281 
282     ret = memcmp(cipherText, codeCipherText, cipherTextLen);
283     ASSERT_EQ(ret, 0);
284 
285     ret = AesNoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
286     ASSERT_EQ(ret, 0);
287 
288     HcfObjDestroy((HcfObjectBase *)key);
289     HcfObjDestroy((HcfObjectBase *)cipher);
290 }
291 
292 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest009, TestSize.Level0)
293 {
294     HcfCipher *cipher = nullptr;
295     HcfSymKey *key = nullptr;
296 
297     int ret = GeneratorFile("/data/test_aes.txt", 10 * FILE_BLOCK_SIZE);
298     ASSERT_EQ(ret, 0);
299 
300     ret = ConvertSymKey("AES128", &key);
301     ASSERT_EQ(ret, 0);
302 
303     ret = HcfCipherCreate("AES128|ECB|NoPadding", &cipher);
304     ASSERT_EQ(ret, 0);
305 
306     ret = AesMultiBlockEncrypt(cipher, key, nullptr);
307     ASSERT_EQ(ret, 0);
308 
309     ret = AesMultiBlockDecrypt(cipher, key, nullptr);
310     ASSERT_EQ(ret, 0);
311 
312     ret = CompareFileContent();
313     ASSERT_EQ(ret, 0);
314 
315     HcfObjDestroy((HcfObjectBase *)key);
316     HcfObjDestroy((HcfObjectBase *)cipher);
317 }
318 
319 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest010, TestSize.Level0)
320 {
321     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
322     int cipherTextLen = CIPHER_TEXT_LEN;
323     HcfCipher *cipher = nullptr;
324     HcfSymKey *key = nullptr;
325 
326     int ret = GenerateSymKey("AES192", &key);
327     ASSERT_EQ(ret, 0);
328 
329     ret = HcfCipherCreate("AES192|ECB|NoPadding", &cipher);
330     ASSERT_EQ(ret, 0);
331 
332     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
333     ASSERT_NE(ret, 0);
334 
335     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
336     ASSERT_NE(ret, 0);
337 
338     HcfObjDestroy(key);
339     HcfObjDestroy(cipher);
340 }
341 
342 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest011, TestSize.Level0)
343 {
344     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
345     int cipherTextLen = CIPHER_TEXT_LEN;
346     HcfCipher *cipher = nullptr;
347     HcfSymKey *key = nullptr;
348 
349     int ret = GenerateSymKey("AES192", &key);
350     ASSERT_EQ(ret, 0);
351 
352     ret = HcfCipherCreate("AES192|ECB|PKCS5", &cipher);
353     ASSERT_EQ(ret, 0);
354 
355     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
356     ASSERT_EQ(ret, 0);
357 
358     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
359     ASSERT_EQ(ret, 0);
360 
361     key->clearMem(key);
362     HcfObjDestroy(key);
363     HcfObjDestroy(cipher);
364 }
365 
366 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest012, TestSize.Level0)
367 {
368     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
369     int cipherTextLen = CIPHER_TEXT_LEN;
370     HcfCipher *cipher = nullptr;
371     HcfSymKey *key = nullptr;
372 
373     int ret = GenerateSymKey("AES256", &key);
374     ASSERT_EQ(ret, 0);
375 
376     ret = HcfCipherCreate("AES256|ECB|PKCS7", &cipher);
377     ASSERT_EQ(ret, 0);
378 
379     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
380     ASSERT_EQ(ret, 0);
381 
382     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
383     ASSERT_EQ(ret, 0);
384 
385     key->clearMem(key);
386     HcfObjDestroy(key);
387     HcfObjDestroy(cipher);
388 }
389 
390 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest013, TestSize.Level0)
391 {
392     HcfSymKeyGenerator *generator = nullptr;
393     HcfSymKey *key = nullptr;
394     HcfCipher *cipher = nullptr;
395 
396     int ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
397     ASSERT_EQ(ret, 0);
398 
399     ret = HcfSymKeyGeneratorCreate("AES128", &generator);
400     ASSERT_EQ(ret, 0);
401 
402     ret = generator->generateSymKey(reinterpret_cast<HcfSymKeyGenerator *>(cipher), &key);
403     ASSERT_NE(ret, 0);
404 
405     HcfObjDestroy(key);
406     HcfObjDestroy(generator);
407     HcfObjDestroy(cipher);
408 }
409 
410 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest014, TestSize.Level0)
411 {
412     HcfSymKeyGenerator *generator = nullptr;
413     HcfSymKey *key = nullptr;
414     uint8_t keyMaterial[] = {
415         0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
416         0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
417     };
418     HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN };
419     HcfCipher *cipher = nullptr;
420 
421     int ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
422     ASSERT_EQ(ret, 0);
423 
424     ret = HcfSymKeyGeneratorCreate("AES128", &generator);
425     ASSERT_EQ(ret, 0);
426 
427     ret = generator->convertSymKey(reinterpret_cast<HcfSymKeyGenerator *>(cipher), &keyTmpBlob, &key);
428     ASSERT_NE(ret, 0);
429 
430     HcfObjDestroy(key);
431     HcfObjDestroy(generator);
432     HcfObjDestroy(cipher);
433 }
434 
435 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest015, TestSize.Level0)
436 {
437     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
438     int cipherTextLen = CIPHER_TEXT_LEN;
439     HcfCipher *cipher = nullptr;
440     HcfSymKey *key = nullptr;
441 
442     int ret = GenerateSymKey("AES128", &key);
443     ASSERT_EQ(ret, 0);
444 
445     // allow input with more than one padding mode. It will pick the last PKCS5.
446     ret = HcfCipherCreate("AES128|ECB|NoPadding|PKCS5", &cipher);
447     ASSERT_EQ(ret, 0);
448 
449     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
450     ASSERT_EQ(ret, 0);
451 
452     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
453     ASSERT_EQ(ret, 0);
454 
455     HcfObjDestroy(key);
456     HcfObjDestroy(cipher);
457 }
458 
459 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest016, TestSize.Level0)
460 {
461     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
462     int cipherTextLen = CIPHER_TEXT_LEN;
463     HcfCipher *cipher = nullptr;
464     HcfSymKey *key = nullptr;
465 
466     int ret = GenerateSymKey("AES128", &key);
467     ASSERT_EQ(ret, 0);
468 
469     ret = HcfCipherCreate("AES256|ECB|PKCS5", &cipher);
470     ASSERT_EQ(ret, 0);
471 
472     // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key.
473     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
474     ASSERT_EQ(ret, 0);
475 
476     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
477     ASSERT_EQ(ret, 0);
478 
479     HcfObjDestroy(key);
480     HcfObjDestroy(cipher);
481 }
482 
483 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest017, TestSize.Level0)
484 {
485     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
486     int cipherTextLen = CIPHER_TEXT_LEN;
487     HcfCipher *cipher = nullptr;
488     HcfSymKey *key = nullptr;
489 
490     int ret = GenerateSymKey("AES256", &key);
491     ASSERT_EQ(ret, 0);
492 
493     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
494     ASSERT_EQ(ret, 0);
495 
496     ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
497     ASSERT_EQ(ret, 0);
498 
499     ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
500     ASSERT_EQ(ret, 0);
501 
502     HcfObjDestroy(key);
503     HcfObjDestroy(cipher);
504 }
505 
506 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest018, TestSize.Level0)
507 {
508     HcfCipher *cipher = nullptr;
509     HcfSymKey *key = nullptr;
510 
511     int ret = GenerateSymKey("AES128", &key);
512     ASSERT_EQ(ret, 0);
513 
514 
515     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
516     ASSERT_EQ(ret, 0);
517 
518 
519     ret = cipher->init(nullptr, ENCRYPT_MODE, &(key->key), nullptr);
520     ASSERT_NE(ret, 0);
521 
522     HcfObjDestroy(key);
523     HcfObjDestroy(cipher);
524 }
525 
526 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest019, TestSize.Level0)
527 {
528     HcfSymKeyGenerator *generator = nullptr;
529     HcfCipher *cipher = nullptr;
530 
531     int ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
532     ASSERT_EQ(ret, 0);
533 
534     ret = cipher->init(cipher, ENCRYPT_MODE, nullptr, nullptr);
535     ASSERT_NE(ret, 0);
536 
537     HcfObjDestroy(cipher);
538     HcfObjDestroy(generator);
539 }
540 
541 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest020, TestSize.Level0)
542 {
543     HcfSymKeyGenerator *generator = nullptr;
544     HcfSymKey *key = nullptr;
545     HcfCipher *cipher = nullptr;
546 
547     int ret = HcfSymKeyGeneratorCreate("AES128", &generator);
548     ASSERT_EQ(ret, 0);
549 
550     ret = generator->generateSymKey(generator, &key);
551     ASSERT_EQ(ret, 0);
552 
553     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
554     ASSERT_EQ(ret, 0);
555 
556     ret = cipher->init(reinterpret_cast<HcfCipher *>(generator), ENCRYPT_MODE, &(key->key), nullptr);
557     ASSERT_NE(ret, 0);
558 
559     HcfObjDestroy(key);
560     HcfObjDestroy(cipher);
561     HcfObjDestroy(generator);
562 }
563 
564 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest021, TestSize.Level0)
565 {
566     HcfCipher *cipher = nullptr;
567     HcfSymKey *key = nullptr;
568     HcfBlob input = { .data = nullptr, .len = 0 };
569     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
570     int cipherTextLen = CIPHER_TEXT_LEN;
571 
572     int ret = GenerateSymKey("AES128", &key);
573     ASSERT_EQ(ret, 0);
574 
575     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
576     ASSERT_EQ(ret, 0);
577 
578     ret = AesEncryptWithInput(cipher, key, &input, cipherText, &cipherTextLen);
579     ASSERT_EQ(ret, 0);
580 
581     ret = AesDecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen);
582     ASSERT_EQ(ret, 0);
583 
584     HcfObjDestroy(key);
585     HcfObjDestroy(cipher);
586 }
587 
588 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest022, TestSize.Level0)
589 {
590     HcfCipher *cipher = nullptr;
591     HcfSymKey *key = nullptr;
592     HcfBlob input = { .data = nullptr, .len = 0 };
593     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
594     int cipherTextLen = CIPHER_TEXT_LEN;
595 
596     int ret = GenerateSymKey("AES128", &key);
597     ASSERT_EQ(ret, 0);
598 
599     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
600     ASSERT_EQ(ret, 0);
601 
602     ret = AesEncryptWithInput(cipher, key, &input, cipherText, &cipherTextLen);
603     ASSERT_EQ(ret, 0);
604 
605     ret = AesDecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen);
606     ASSERT_EQ(ret, 0);
607 
608     HcfObjDestroy(key);
609     HcfObjDestroy(cipher);
610 }
611 
612 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest023, TestSize.Level0)
613 {
614     HcfCipher *cipher = nullptr;
615     HcfSymKey *key = nullptr;
616     HcfBlob input = { .data = nullptr, .len = PLAINTEXT_LEN };
617     HcfBlob output = { .data = nullptr, .len = 0 };
618 
619     int ret = GenerateSymKey("AES128", &key);
620     ASSERT_EQ(ret, 0);
621 
622     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
623     ASSERT_EQ(ret, 0);
624 
625     ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
626     ASSERT_EQ(ret, 0);
627 
628     ret = cipher->update(nullptr, &input, &output);
629     ASSERT_NE(ret, 0);
630 
631     HcfObjDestroy(key);
632     HcfObjDestroy(cipher);
633     if (output.data != nullptr) {
634         HcfFree(output.data);
635         output.data = nullptr;
636     }
637 }
638 
639 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest024, TestSize.Level0)
640 {
641     HcfCipher *cipher = nullptr;
642     HcfSymKey *key = nullptr;
643     HcfBlob input = { .data = nullptr, .len = PLAINTEXT_LEN };
644     HcfBlob output = { .data = nullptr, .len = 0 };
645 
646     int ret = GenerateSymKey("AES128", &key);
647     ASSERT_EQ(ret, 0);
648 
649     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
650     ASSERT_EQ(ret, 0);
651 
652     ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
653     ASSERT_EQ(ret, 0);
654 
655     ret = cipher->update(reinterpret_cast<HcfCipher *>(key), &input, &output);
656     ASSERT_NE(ret, 0);
657 
658     HcfObjDestroy(key);
659     HcfObjDestroy(cipher);
660     if (output.data != nullptr) {
661         HcfFree(output.data);
662         output.data = nullptr;
663     }
664 }
665 
666 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest025, TestSize.Level0)
667 {
668     HcfCipher *cipher = nullptr;
669     HcfSymKey *key = nullptr;
670     uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
671     int cipherTextLen = CIPHER_TEXT_LEN;
672 
673     int ret = GenerateSymKey("AES128", &key);
674     ASSERT_EQ(ret, 0);
675 
676     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
677     ASSERT_EQ(ret, 0);
678 
679     ret = AesNoUpdateEncWithInput(cipher, key, nullptr, cipherText, &cipherTextLen);
680     ASSERT_EQ(ret, 0);
681 
682     ret = AesDecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen);
683     ASSERT_EQ(ret, 0);
684 
685     HcfObjDestroy(key);
686     HcfObjDestroy(cipher);
687 }
688 
689 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest026, TestSize.Level0)
690 {
691     HcfCipher *cipher = nullptr;
692     HcfSymKey *key = nullptr;
693     uint8_t plainText[] = "this is test!";
694     HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN };
695 
696     int ret = GenerateSymKey("AES128", &key);
697     ASSERT_EQ(ret, 0);
698 
699     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
700     ASSERT_EQ(ret, 0);
701 
702     ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
703     ASSERT_EQ(ret, 0);
704 
705     ret = cipher->doFinal(cipher, &input, nullptr);
706     ASSERT_NE(ret, 0);
707 
708     HcfObjDestroy(key);
709     HcfObjDestroy(cipher);
710 }
711 
712 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest027, TestSize.Level0)
713 {
714     HcfCipher *cipher = nullptr;
715     HcfSymKey *key = nullptr;
716     uint8_t plainText[] = "this is test!";
717     HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN };
718     HcfBlob output = { .data = nullptr, .len = 0 };
719 
720     int ret = GenerateSymKey("AES128", &key);
721     ASSERT_EQ(ret, 0);
722 
723     ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
724     ASSERT_EQ(ret, 0);
725 
726     ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
727     ASSERT_EQ(ret, 0);
728 
729     ret = cipher->doFinal(reinterpret_cast<HcfCipher *>(key), &input, &output);
730     ASSERT_NE(ret, 0);
731 
732     HcfObjDestroy(key);
733     HcfObjDestroy(cipher);
734     if (output.data != nullptr) {
735         HcfFree(output.data);
736         output.data = nullptr;
737     }
738 }
739 
740 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest028, TestSize.Level0)
741 {
742     HcfSymKeyGeneratorSpi *generator = nullptr;
743     HcfSymKey *key = nullptr;
744     HcfCipher *cipher = nullptr;
745     SymKeyAttr attr = { .algo = HCF_ALG_AES, .keySize = AES_KEY_SIZE };
746 
747     int ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
748     ASSERT_EQ(ret, 0);
749 
750     ret = HcfSymKeyGeneratorSpiCreate(&attr, &generator);
751     ASSERT_EQ(ret, 0);
752 
753     ret = generator->engineGenerateSymmKey(reinterpret_cast<HcfSymKeyGeneratorSpi *>(cipher), &key);
754     ASSERT_NE(ret, 0);
755 
756     HcfObjDestroy(key);
757     HcfObjDestroy(generator);
758     HcfObjDestroy(cipher);
759 }
760 
761 HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest029, TestSize.Level0)
762 {
763     HcfSymKeyGeneratorSpi *generator = nullptr;
764     HcfSymKey *key = nullptr;
765     uint8_t keyMaterial[] = {
766         0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
767         0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
768     };
769     HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN };
770     HcfCipher *cipher = nullptr;
771     SymKeyAttr attr = { .algo = HCF_ALG_AES, .keySize = AES_KEY_SIZE };
772 
773     int ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
774     ASSERT_EQ(ret, 0);
775 
776     ret = HcfSymKeyGeneratorSpiCreate(&attr, &generator);
777     ASSERT_EQ(ret, 0);
778 
779     ret = generator->engineConvertSymmKey(reinterpret_cast<HcfSymKeyGeneratorSpi *>(cipher), &keyTmpBlob, &key);
780     ASSERT_NE(ret, 0);
781 
782     HcfObjDestroy(key);
783     HcfObjDestroy(generator);
784     HcfObjDestroy(cipher);
785 }
786 }