1 /*
2 * Copyright (C) 2022 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_openssl.h"
22 #include "blob.h"
23 #include "cipher.h"
24 #include "detailed_iv_params.h"
25 #include "detailed_gcm_params.h"
26 #include "detailed_ccm_params.h"
27 #include "log.h"
28 #include "memory.h"
29 #include "sym_common_defines.h"
30 #include "sym_key_generator.h"
31
32 using namespace std;
33 using namespace testing::ext;
34
35 namespace {
36 const int32_t FILE_BLOCK_SIZE = 1024;
37 const int32_t RAND_MAX_NUM = 100;
38 const bool IS_DEBUG = false;
39 constexpr int32_t CIPHER_TEXT_LEN = 128;
40 constexpr int32_t KEY_MATERIAL_LEN = 16;
41 constexpr int32_t AES_IV_LEN = 16; // iv for CBC|CTR|OFB|CFB mode
42 constexpr int32_t GCM_IV_LEN = 12; // GCM
43 constexpr int32_t GCM_AAD_LEN = 8;
44 constexpr int32_t GCM_TAG_LEN = 16;
45 constexpr int32_t CCM_IV_LEN = 7; // CCM
46 constexpr int32_t CCM_AAD_LEN = 8;
47 constexpr int32_t CCM_TAG_LEN = 12;
48
49 class CryptoAesCipherTest : public testing::Test {
50 public:
51 static void SetUpTestCase();
52 static void TearDownTestCase();
53 void SetUp();
54 void TearDown();
55 };
56
SetUpTestCase()57 void CryptoAesCipherTest::SetUpTestCase() {}
58
TearDownTestCase()59 void CryptoAesCipherTest::TearDownTestCase() {}
60
SetUp()61 void CryptoAesCipherTest::SetUp() // add init here, this will be called before test.
62 {
63 }
64
TearDown()65 void CryptoAesCipherTest::TearDown() // add destroy here, this will be called when test case done.
66 {
67 }
68
PrintfHex(const char * tag,uint8_t * in,int inLen)69 static void PrintfHex(const char *tag, uint8_t *in, int inLen)
70 {
71 if (!IS_DEBUG) {
72 return;
73 }
74 printf("%s:\n", tag);
75 for (int i = 0; i < inLen; i++) {
76 printf("%02hhX", in[i]);
77 }
78 printf("\n");
79 }
80
GenerateSymKey(const char * algoName,HcfSymKey ** key)81 static int32_t GenerateSymKey(const char *algoName, HcfSymKey **key)
82 {
83 HcfSymKeyGenerator *generator = NULL;
84
85 int32_t ret = HcfSymKeyGeneratorCreate(algoName, &generator);
86 if (ret != 0) {
87 LOGE("HcfSymKeyGeneratorCreate failed!");
88 return ret;
89 }
90
91 ret = generator->generateSymKey(generator, key);
92 if (ret != 0) {
93 LOGE("generateSymKey failed!");
94 }
95 HcfObjDestroy((HcfObjectBase *)generator);
96 return ret;
97 }
98
ConvertSymKey(const char * algoName,HcfSymKey ** key)99 static int32_t ConvertSymKey(const char *algoName, HcfSymKey **key)
100 {
101 HcfSymKeyGenerator *generator = NULL;
102 uint8_t keyMaterial[] = {
103 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
104 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
105 };
106 HcfBlob keyTmpBlob = {.data = (uint8_t *)keyMaterial, .len = 16};
107
108 int32_t ret = HcfSymKeyGeneratorCreate(algoName, &generator);
109 if (ret != 0) {
110 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
111 return ret;
112 }
113
114 ret = generator->convertSymKey(generator, &keyTmpBlob, key);
115 if (ret != 0) {
116 LOGE("generateSymKey failed!");
117 }
118 PrintfHex("keybinary", keyTmpBlob.data, keyTmpBlob.len);
119 HcfObjDestroy((HcfObjectBase *)generator);
120 return ret;
121 }
122
123 /* just rand data fill file for test */
GeneratorFile(const char * fileName,int32_t genFileSize)124 static int32_t GeneratorFile(const char *fileName, int32_t genFileSize)
125 {
126 if (genFileSize == 0) {
127 return 0;
128 }
129 uint8_t buffer[FILE_BLOCK_SIZE] = {0};
130 std::ifstream file(fileName);
131
132 if (file.good()) {
133 file.close();
134 return 0;
135 }
136 ofstream outfile(fileName, ios::out|ios::binary|ios::app);
137 if (outfile.is_open()) {
138 while (genFileSize) {
139 for (uint32_t i = 0; i < FILE_BLOCK_SIZE; i++) {
140 buffer[i] = (rand() % RAND_MAX_NUM) + 1;
141 }
142 genFileSize -= FILE_BLOCK_SIZE;
143 outfile.write(reinterpret_cast<const char *>(buffer), FILE_BLOCK_SIZE);
144 }
145 outfile.close();
146 }
147 return 0;
148 }
149
CompareFileContent()150 static int32_t CompareFileContent()
151 {
152 int32_t ret = -1;
153 ifstream infile1, infile2;
154 infile1.open("/data/test_aes.txt", ios::in|ios::binary);
155 infile1.seekg (0, infile1.end);
156 uint32_t length1 = infile1.tellg();
157 infile1.seekg (0, infile1.beg);
158
159 infile2.open("/data/test_aes_new.txt", ios::in|ios::binary);
160 infile2.seekg (0, infile2.end);
161 uint32_t length2 = infile2.tellg();
162 infile2.seekg (0, infile2.beg);
163 if (length1 != length2) {
164 return ret;
165 }
166 uint8_t buffer1[FILE_BLOCK_SIZE] = {0};
167 uint8_t buffer2[FILE_BLOCK_SIZE] = {0};
168 for (uint32_t i = 0; i < length1 / FILE_BLOCK_SIZE; i++) {
169 infile1.read(reinterpret_cast<char *>(buffer1), FILE_BLOCK_SIZE);
170 infile2.read(reinterpret_cast<char *>(buffer2), FILE_BLOCK_SIZE);
171 ret = memcmp(buffer1, buffer2, FILE_BLOCK_SIZE);
172 if (ret != 0) {
173 goto clearup;
174 }
175 }
176 clearup:
177 infile1.close();
178 infile2.close();
179 return ret;
180 }
181
AesMultiBlockEncrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params)182 static int32_t AesMultiBlockEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params)
183 {
184 HcfBlob output = {};
185 ifstream infile;
186 ofstream outfile;
187 infile.open("/data/test_aes.txt", ios::in|ios::binary);
188 infile.seekg (0, infile.end);
189 uint32_t length = infile.tellg();
190 infile.seekg (0, infile.beg);
191 uint8_t buffer[1024] = {0};
192 outfile.open("/data/test_aes_enc.txt", ios::out|ios::binary);
193 HcfBlob input = {.data = (uint8_t *)buffer, .len = FILE_BLOCK_SIZE};
194 uint32_t count = length / FILE_BLOCK_SIZE;
195
196 int32_t ret = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)key, params);
197 if (ret != 0) {
198 LOGE("init failed! %d", ret);
199 goto clearup;
200 }
201 for (uint32_t i = 0; i < count; i++) {
202 infile.read(reinterpret_cast<char *>(buffer), FILE_BLOCK_SIZE);
203 ret = cipher->update(cipher, &input, &output);
204 if (ret != 0) {
205 LOGE("update failed!");
206 goto clearup;
207 }
208 if (output.data != nullptr && output.len > 0) {
209 outfile.write(reinterpret_cast<const char *>(output.data), output.len);
210 }
211 if (output.data != NULL) {
212 HcfFree(output.data);
213 output.data = NULL;
214 }
215 }
216 ret = cipher->doFinal(cipher, NULL, &output);
217 if (ret != 0) {
218 LOGE("doFinal failed!");
219 goto clearup;
220 }
221 if (output.data != nullptr && output.len > 0) {
222 outfile.write((const char *)output.data, output.len);
223 }
224
225 if (output.data != NULL) {
226 HcfFree(output.data);
227 output.data = NULL;
228 }
229 clearup:
230 outfile.close();
231 infile.close();
232
233 return ret;
234 }
235
AesMultiBlockDecrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params)236 static int32_t AesMultiBlockDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params)
237 {
238 HcfBlob output = {};
239 ifstream infile;
240 ofstream outfile;
241 infile.open("/data/test_aes_enc.txt", ios::in|ios::binary);
242 infile.seekg (0, infile.end);
243 uint32_t length = infile.tellg();
244 infile.seekg (0, infile.beg);
245 uint8_t buffer[1024] = {0};
246 outfile.open("/data/test_aes_new.txt", ios::out|ios::binary);
247 HcfBlob input = {.data = (uint8_t *)buffer, .len = FILE_BLOCK_SIZE};
248
249 uint32_t count = length / FILE_BLOCK_SIZE;
250 int32_t ret = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)key, params);
251 if (ret != 0) {
252 LOGE("init failed! %d", ret);
253 goto clearup;
254 }
255 for (uint32_t i = 0; i < count; i++) {
256 infile.read(reinterpret_cast<char *>(buffer), FILE_BLOCK_SIZE);
257 ret = cipher->update(cipher, &input, &output);
258 if (ret != 0) {
259 LOGE("update failed!");
260 goto clearup;
261 }
262 if (output.data != nullptr && output.len > 0) {
263 outfile.write(reinterpret_cast<const char *>(output.data), output.len);
264 }
265 if (output.data != NULL) {
266 HcfFree(output.data);
267 output.data = NULL;
268 }
269 }
270 ret = cipher->doFinal(cipher, NULL, &output);
271 if (ret != 0) {
272 LOGE("doFinal failed!");
273 goto clearup;
274 }
275 if (output.data != nullptr && output.len > 0) {
276 outfile.write((const char *)output.data, output.len);
277 }
278
279 if (output.data != NULL) {
280 HcfFree(output.data);
281 output.data = NULL;
282 }
283 clearup:
284 outfile.close();
285 infile.close();
286
287 return ret;
288 }
289
AesEncrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int * cipherTextLen)290 static int32_t AesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
291 uint8_t *cipherText, int *cipherTextLen)
292 {
293 uint8_t plainText[] = "this is test!";
294 HcfBlob input = {.data = (uint8_t *)plainText, .len = 13};
295 HcfBlob output = {};
296 int32_t maxLen = *cipherTextLen;
297 int32_t ret = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)key, params);
298 if (ret != 0) {
299 LOGE("init failed! %d", ret);
300 return ret;
301 }
302
303 ret = cipher->update(cipher, &input, &output);
304 if (ret != 0) {
305 LOGE("update failed!");
306 return ret;
307 }
308 *cipherTextLen = output.len;
309 if (output.data != nullptr) {
310 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
311 HcfBlobDataFree(&output);
312 return -1;
313 }
314 HcfBlobDataFree(&output);
315 }
316
317 ret = cipher->doFinal(cipher, NULL, &output);
318 if (ret != 0) {
319 LOGE("doFinal failed!");
320 return ret;
321 }
322 if (output.data != nullptr) {
323 if (memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len) != EOK) {
324 HcfBlobDataFree(&output);
325 return -1;
326 }
327 *cipherTextLen += output.len;
328 HcfBlobDataFree(&output);
329 }
330
331 PrintfHex("ciphertext", cipherText, *cipherTextLen);
332 return 0;
333 }
334
AesDecrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int cipherTextLen)335 static int32_t AesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
336 uint8_t *cipherText, int cipherTextLen)
337 {
338 uint8_t plainText[] = "this is test!";
339 HcfBlob input = {.data = (uint8_t *)cipherText, .len = cipherTextLen};
340 HcfBlob output = {};
341 int32_t maxLen = cipherTextLen;
342 int32_t ret = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)key, params);
343 if (ret != 0) {
344 LOGE("init failed! %d", ret);
345 return ret;
346 }
347
348 ret = cipher->update(cipher, &input, &output);
349 if (ret != 0) {
350 LOGE("update failed!");
351 return ret;
352 }
353 cipherTextLen = output.len;
354 if (output.data != nullptr) {
355 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
356 HcfBlobDataFree(&output);
357 return -1;
358 }
359 HcfBlobDataFree(&output);
360 }
361
362 ret = cipher->doFinal(cipher, NULL, &output);
363 if (ret != 0) {
364 LOGE("doFinal failed!");
365 return ret;
366 }
367 if (output.data != nullptr) {
368 if (memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len) != EOK) {
369 HcfBlobDataFree(&output);
370 return -1;
371 }
372 cipherTextLen += output.len;
373 HcfBlobDataFree(&output);
374 }
375
376 PrintfHex("plainText", cipherText, cipherTextLen);
377 if (cipherTextLen != sizeof(plainText) - 1) {
378 return -1;
379 }
380 return memcmp(cipherText, plainText, cipherTextLen);
381 }
382
AesNoUpdateEncrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int * cipherTextLen)383 static int32_t AesNoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
384 uint8_t *cipherText, int *cipherTextLen)
385 {
386 uint8_t plainText[] = "this is test!";
387 HcfBlob input = {.data = (uint8_t *)plainText, .len = 13};
388 HcfBlob output = {};
389 int32_t maxLen = *cipherTextLen;
390 int32_t ret = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)key, params);
391 if (ret != 0) {
392 LOGE("init failed! %d", ret);
393 return ret;
394 }
395
396 *cipherTextLen = 0;
397 ret = cipher->doFinal(cipher, &input, &output);
398 if (ret != 0) {
399 LOGE("doFinal failed!");
400 return ret;
401 }
402 if (output.data != nullptr) {
403 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
404 HcfBlobDataFree(&output);
405 return -1;
406 }
407 *cipherTextLen += output.len;
408 HcfBlobDataFree(&output);
409 }
410
411 PrintfHex("ciphertext", cipherText, *cipherTextLen);
412 return 0;
413 }
414
AesNoUpdateDecrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int cipherTextLen)415 static int32_t AesNoUpdateDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
416 uint8_t *cipherText, int cipherTextLen)
417 {
418 uint8_t plainText[] = "this is test!";
419 HcfBlob input = {.data = (uint8_t *)cipherText, .len = cipherTextLen};
420 HcfBlob output = {};
421 int32_t maxLen = cipherTextLen;
422 int32_t ret = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)key, params);
423 if (ret != 0) {
424 LOGE("init failed! %d", ret);
425 return ret;
426 }
427
428 cipherTextLen = 0;
429 ret = cipher->doFinal(cipher, &input, &output);
430 if (ret != 0) {
431 LOGE("doFinal failed!");
432 return ret;
433 }
434 if (output.data != nullptr) {
435 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
436 HcfBlobDataFree(&output);
437 return -1;
438 }
439 cipherTextLen += output.len;
440 HcfBlobDataFree(&output);
441 }
442
443 PrintfHex("plainText", cipherText, cipherTextLen);
444 if (cipherTextLen != sizeof(plainText) - 1) {
445 return -1;
446 }
447 return memcmp(cipherText, plainText, cipherTextLen);
448 }
449
450 /**
451 * @tc.name: CryptoAesCipherTest.CryptoAesCipherTest001
452 * @tc.desc: Verify whether the crypto framework is normal.
453 * @tc.type: FUNC
454 * @tc.require: I5QWEO
455 */
456 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest001, TestSize.Level0)
457 {
458 int ret = 0;
459 uint8_t cipherText[128] = {0};
460 int cipherTextLen = 128;
461
462 HcfSymKeyGenerator *generator = NULL;
463 HcfCipher *cipher = NULL;
464 HcfSymKey *key = NULL;
465
466 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
467 if (ret != 0) {
468 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
469 goto clearup;
470 }
471
472 ret = generator->generateSymKey(generator, &key);
473 if (ret != 0) {
474 LOGE("generateSymKey failed!");
475 goto clearup;
476 }
477
478 ret = HcfCipherCreate("AES128|ECB|NoPadding", &cipher);
479 if (ret != 0) {
480 LOGE("HcfCipherCreate failed!");
481 goto clearup;
482 }
483
484 ret = AesEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
485 if (ret != 0) {
486 LOGE("AesEncrypt failed! %d", ret);
487 goto clearup;
488 }
489
490 ret = AesDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
491 if (ret != 0) {
492 LOGE("AesDecrypt failed! %d", ret);
493 goto clearup;
494 }
495 clearup:
496 HcfObjDestroy((HcfObjectBase *)key);
497 HcfObjDestroy((HcfObjectBase *)cipher);
498 HcfObjDestroy((HcfObjectBase *)generator);
499 EXPECT_NE(ret, 0);
500 }
501
502 /**
503 * @tc.name: CryptoAesCipherTest.CryptoAesCipherTest002
504 * @tc.desc: Verify AES128 cipher algorithm.
505 * @tc.type: FUNC
506 * @tc.require: I5QWEG
507 */
508 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest002, TestSize.Level0)
509 {
510 int ret = 0;
511 uint8_t cipherText[128] = {0};
512 int cipherTextLen = 128;
513
514 HcfSymKeyGenerator *generator = NULL;
515 HcfCipher *cipher = NULL;
516 HcfSymKey *key = NULL;
517 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
518 if (ret != 0) {
519 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
520 goto clearup;
521 }
522
523 ret = generator->generateSymKey(generator, &key);
524 if (ret != 0) {
525 LOGE("generateSymKey failed!");
526 goto clearup;
527 }
528
529 ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
530 if (ret != 0) {
531 LOGE("HcfCipherCreate failed!");
532 goto clearup;
533 }
534
535 ret = AesEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
536 if (ret != 0) {
537 LOGE("AesEncrypt failed! %d", ret);
538 goto clearup;
539 }
540
541 ret = AesDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
542 if (ret != 0) {
543 LOGE("AesDecrypt failed! %d", ret);
544 goto clearup;
545 }
546 clearup:
547 HcfObjDestroy((HcfObjectBase *)key);
548 HcfObjDestroy((HcfObjectBase *)cipher);
549 HcfObjDestroy((HcfObjectBase *)generator);
550 EXPECT_EQ(ret, 0);
551 }
552
553 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest003, TestSize.Level0)
554 {
555 int ret = 0;
556 uint8_t cipherText[128] = {0};
557 int cipherTextLen = 128;
558
559 HcfSymKeyGenerator *generator = NULL;
560 HcfCipher *cipher = NULL;
561 HcfSymKey *key = NULL;
562 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
563 if (ret != 0) {
564 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
565 goto clearup;
566 }
567
568 ret = generator->generateSymKey(generator, &key);
569 if (ret != 0) {
570 LOGE("generateSymKey failed!");
571 goto clearup;
572 }
573
574 ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
575 if (ret != 0) {
576 LOGE("HcfCipherCreate failed!");
577 goto clearup;
578 }
579
580 ret = AesEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
581 if (ret != 0) {
582 LOGE("AesEncrypt failed! %d", ret);
583 goto clearup;
584 }
585
586 ret = AesDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
587 if (ret != 0) {
588 LOGE("AesDecrypt failed! %d", ret);
589 goto clearup;
590 }
591
592 clearup:
593 HcfObjDestroy((HcfObjectBase *)key);
594 HcfObjDestroy((HcfObjectBase *)cipher);
595 HcfObjDestroy((HcfObjectBase *)generator);
596 EXPECT_EQ(ret, 0);
597 }
598
599 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest004, TestSize.Level0)
600 {
601 int ret = 0;
602 uint8_t iv[16] = {0};
603 uint8_t cipherText[128] = {0};
604 int cipherTextLen = 128;
605
606 HcfIvParamsSpec ivSpec = {};
607 HcfSymKeyGenerator *generator = NULL;
608 HcfCipher *cipher = NULL;
609 HcfSymKey *key = NULL;
610 ivSpec.iv.data = iv;
611 ivSpec.iv.len = 16;
612
613 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
614 if (ret != 0) {
615 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
616 goto clearup;
617 }
618
619 ret = generator->generateSymKey(generator, &key);
620 if (ret != 0) {
621 LOGE("generateSymKey failed!");
622 goto clearup;
623 }
624
625 ret = HcfCipherCreate("AES128|CBC|NoPadding", &cipher);
626 if (ret != 0) {
627 LOGE("HcfCipherCreate failed!");
628 goto clearup;
629 }
630
631 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
632 if (ret != 0) {
633 LOGE("AesEncrypt failed! %d", ret);
634 goto clearup;
635 }
636
637 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
638 if (ret != 0) {
639 LOGE("AesDecrypt failed! %d", ret);
640 goto clearup;
641 }
642
643 clearup:
644 HcfObjDestroy((HcfObjectBase *)key);
645 HcfObjDestroy((HcfObjectBase *)cipher);
646 HcfObjDestroy((HcfObjectBase *)generator);
647 EXPECT_NE(ret, 0);
648 }
649
650 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest005, TestSize.Level0)
651 {
652 int ret = 0;
653 uint8_t iv[16] = {0};
654 uint8_t cipherText[128] = {0};
655 int cipherTextLen = 128;
656
657 HcfIvParamsSpec ivSpec = {};
658 HcfSymKeyGenerator *generator = NULL;
659 HcfCipher *cipher = NULL;
660 HcfSymKey *key = NULL;
661 ivSpec.iv.data = iv;
662 ivSpec.iv.len = 16;
663
664 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
665 if (ret != 0) {
666 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
667 goto clearup;
668 }
669
670 ret = generator->generateSymKey(generator, &key);
671 if (ret != 0) {
672 LOGE("generateSymKey failed!");
673 goto clearup;
674 }
675
676 ret = HcfCipherCreate("AES128|CBC|PKCS5", &cipher);
677 if (ret != 0) {
678 LOGE("HcfCipherCreate failed!");
679 goto clearup;
680 }
681
682 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
683 if (ret != 0) {
684 LOGE("AesEncrypt failed! %d", ret);
685 goto clearup;
686 }
687
688 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
689 if (ret != 0) {
690 LOGE("AesDecrypt failed! %d", ret);
691 goto clearup;
692 }
693
694 clearup:
695 HcfObjDestroy((HcfObjectBase *)key);
696 HcfObjDestroy((HcfObjectBase *)cipher);
697 HcfObjDestroy((HcfObjectBase *)generator);
698 EXPECT_EQ(ret, 0);
699 }
700
701 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest006, TestSize.Level0)
702 {
703 int ret = 0;
704 uint8_t iv[16] = {0};
705 uint8_t cipherText[128] = {0};
706 int cipherTextLen = 128;
707
708 HcfIvParamsSpec ivSpec = {};
709 HcfSymKeyGenerator *generator = NULL;
710 HcfCipher *cipher = NULL;
711 HcfSymKey *key = NULL;
712 ivSpec.iv.data = iv;
713 ivSpec.iv.len = 16;
714
715 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
716 if (ret != 0) {
717 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
718 goto clearup;
719 }
720
721 ret = generator->generateSymKey(generator, &key);
722 if (ret != 0) {
723 LOGE("generateSymKey failed!");
724 goto clearup;
725 }
726
727 ret = HcfCipherCreate("AES128|CBC|PKCS7", &cipher);
728 if (ret != 0) {
729 LOGE("HcfCipherCreate failed!");
730 goto clearup;
731 }
732
733 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
734 if (ret != 0) {
735 LOGE("AesEncrypt failed! %d", ret);
736 goto clearup;
737 }
738
739 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
740 if (ret != 0) {
741 LOGE("AesDecrypt failed! %d", ret);
742 goto clearup;
743 }
744
745 clearup:
746 HcfObjDestroy((HcfObjectBase *)key);
747 HcfObjDestroy((HcfObjectBase *)cipher);
748 HcfObjDestroy((HcfObjectBase *)generator);
749 EXPECT_EQ(ret, 0);
750 }
751
752 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest007, TestSize.Level0)
753 {
754 int ret = 0;
755 uint8_t iv[16] = {0};
756 uint8_t cipherText[128] = {0};
757 int cipherTextLen = 128;
758
759 HcfIvParamsSpec ivSpec = {};
760 HcfSymKeyGenerator *generator = NULL;
761 HcfCipher *cipher = NULL;
762 HcfSymKey *key = NULL;
763 ivSpec.iv.data = iv;
764 ivSpec.iv.len = 16;
765
766 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
767 if (ret != 0) {
768 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
769 goto clearup;
770 }
771
772 ret = generator->generateSymKey(generator, &key);
773 if (ret != 0) {
774 LOGE("generateSymKey failed!");
775 goto clearup;
776 }
777
778 ret = HcfCipherCreate("AES128|CTR|NoPadding", &cipher);
779 if (ret != 0) {
780 LOGE("HcfCipherCreate failed!");
781 goto clearup;
782 }
783
784 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
785 if (ret != 0) {
786 LOGE("AesEncrypt failed! %d", ret);
787 goto clearup;
788 }
789
790 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
791 if (ret != 0) {
792 LOGE("AesDecrypt failed! %d", ret);
793 goto clearup;
794 }
795
796 clearup:
797 HcfObjDestroy((HcfObjectBase *)key);
798 HcfObjDestroy((HcfObjectBase *)cipher);
799 HcfObjDestroy((HcfObjectBase *)generator);
800 EXPECT_EQ(ret, 0);
801 }
802
803 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest008, TestSize.Level0)
804 {
805 int ret = 0;
806 uint8_t iv[16] = {0};
807 uint8_t cipherText[128] = {0};
808 int cipherTextLen = 128;
809
810 HcfIvParamsSpec ivSpec = {};
811 HcfSymKeyGenerator *generator = NULL;
812 HcfCipher *cipher = NULL;
813 HcfSymKey *key = NULL;
814 ivSpec.iv.data = iv;
815 ivSpec.iv.len = 16;
816
817 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
818 if (ret != 0) {
819 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
820 goto clearup;
821 }
822
823 ret = generator->generateSymKey(generator, &key);
824 if (ret != 0) {
825 LOGE("generateSymKey failed!");
826 goto clearup;
827 }
828
829 ret = HcfCipherCreate("AES128|CTR|PKCS5", &cipher);
830 if (ret != 0) {
831 LOGE("HcfCipherCreate failed!");
832 goto clearup;
833 }
834
835 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
836 if (ret != 0) {
837 LOGE("AesEncrypt failed! %d", ret);
838 goto clearup;
839 }
840
841 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
842 if (ret != 0) {
843 LOGE("AesDecrypt failed! %d", ret);
844 goto clearup;
845 }
846
847 clearup:
848 HcfObjDestroy((HcfObjectBase *)key);
849 HcfObjDestroy((HcfObjectBase *)cipher);
850 HcfObjDestroy((HcfObjectBase *)generator);
851 EXPECT_EQ(ret, 0);
852 }
853
854 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest009, TestSize.Level0)
855 {
856 int ret = 0;
857 uint8_t iv[16] = {0};
858 uint8_t cipherText[128] = {0};
859 int cipherTextLen = 128;
860
861 HcfIvParamsSpec ivSpec = {};
862 HcfSymKeyGenerator *generator = NULL;
863 HcfCipher *cipher = NULL;
864 HcfSymKey *key = NULL;
865 ivSpec.iv.data = iv;
866 ivSpec.iv.len = 16;
867
868 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
869 if (ret != 0) {
870 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
871 goto clearup;
872 }
873
874 ret = generator->generateSymKey(generator, &key);
875 if (ret != 0) {
876 LOGE("generateSymKey failed!");
877 goto clearup;
878 }
879
880 ret = HcfCipherCreate("AES128|CTR|PKCS7", &cipher);
881 if (ret != 0) {
882 LOGE("HcfCipherCreate failed!");
883 goto clearup;
884 }
885
886 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
887 if (ret != 0) {
888 LOGE("AesEncrypt failed! %d", ret);
889 goto clearup;
890 }
891
892 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
893 if (ret != 0) {
894 LOGE("AesDecrypt failed! %d", ret);
895 goto clearup;
896 }
897
898 clearup:
899 HcfObjDestroy((HcfObjectBase *)key);
900 HcfObjDestroy((HcfObjectBase *)cipher);
901 HcfObjDestroy((HcfObjectBase *)generator);
902 EXPECT_EQ(ret, 0);
903 }
904
905 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest010, TestSize.Level0)
906 {
907 int ret = 0;
908 uint8_t iv[16] = {0};
909 uint8_t cipherText[128] = {0};
910 int cipherTextLen = 128;
911
912 HcfIvParamsSpec ivSpec = {};
913 HcfSymKeyGenerator *generator = NULL;
914 HcfCipher *cipher = NULL;
915 HcfSymKey *key = NULL;
916 ivSpec.iv.data = iv;
917 ivSpec.iv.len = 16;
918
919 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
920 if (ret != 0) {
921 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
922 goto clearup;
923 }
924
925 ret = generator->generateSymKey(generator, &key);
926 if (ret != 0) {
927 LOGE("generateSymKey failed!");
928 goto clearup;
929 }
930
931 ret = HcfCipherCreate("AES128|OFB|NoPadding", &cipher);
932 if (ret != 0) {
933 LOGE("HcfCipherCreate failed!");
934 goto clearup;
935 }
936
937 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
938 if (ret != 0) {
939 LOGE("AesEncrypt failed! %d", ret);
940 goto clearup;
941 }
942
943 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
944 if (ret != 0) {
945 LOGE("AesDecrypt failed! %d", ret);
946 goto clearup;
947 }
948
949 clearup:
950 HcfObjDestroy((HcfObjectBase *)key);
951 HcfObjDestroy((HcfObjectBase *)cipher);
952 HcfObjDestroy((HcfObjectBase *)generator);
953 EXPECT_EQ(ret, 0);
954 }
955
956 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest011, TestSize.Level0)
957 {
958 int ret = 0;
959 uint8_t iv[16] = {0};
960 uint8_t cipherText[128] = {0};
961 int cipherTextLen = 128;
962
963 HcfIvParamsSpec ivSpec = {};
964 HcfSymKeyGenerator *generator = NULL;
965 HcfCipher *cipher = NULL;
966 HcfSymKey *key = NULL;
967 ivSpec.iv.data = iv;
968 ivSpec.iv.len = 16;
969
970 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
971 if (ret != 0) {
972 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
973 goto clearup;
974 }
975
976 ret = generator->generateSymKey(generator, &key);
977 if (ret != 0) {
978 LOGE("generateSymKey failed!");
979 goto clearup;
980 }
981
982 ret = HcfCipherCreate("AES128|OFB|PKCS5", &cipher);
983 if (ret != 0) {
984 LOGE("HcfCipherCreate failed!");
985 goto clearup;
986 }
987
988 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
989 if (ret != 0) {
990 LOGE("AesEncrypt failed! %d", ret);
991 goto clearup;
992 }
993
994 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
995 if (ret != 0) {
996 LOGE("AesDecrypt failed! %d", ret);
997 goto clearup;
998 }
999
1000 clearup:
1001 HcfObjDestroy((HcfObjectBase *)key);
1002 HcfObjDestroy((HcfObjectBase *)cipher);
1003 HcfObjDestroy((HcfObjectBase *)generator);
1004 EXPECT_EQ(ret, 0);
1005 }
1006
1007 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest012, TestSize.Level0)
1008 {
1009 int ret = 0;
1010 uint8_t iv[16] = {0};
1011 uint8_t cipherText[128] = {0};
1012 int cipherTextLen = 128;
1013
1014 HcfIvParamsSpec ivSpec = {};
1015 HcfSymKeyGenerator *generator = NULL;
1016 HcfCipher *cipher = NULL;
1017 HcfSymKey *key = NULL;
1018 ivSpec.iv.data = iv;
1019 ivSpec.iv.len = 16;
1020
1021 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1022 if (ret != 0) {
1023 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1024 goto clearup;
1025 }
1026
1027 ret = generator->generateSymKey(generator, &key);
1028 if (ret != 0) {
1029 LOGE("generateSymKey failed!");
1030 goto clearup;
1031 }
1032
1033 ret = HcfCipherCreate("AES128|OFB|PKCS7", &cipher);
1034 if (ret != 0) {
1035 LOGE("HcfCipherCreate failed!");
1036 goto clearup;
1037 }
1038
1039 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1040 if (ret != 0) {
1041 LOGE("AesEncrypt failed! %d", ret);
1042 goto clearup;
1043 }
1044
1045 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1046 if (ret != 0) {
1047 LOGE("AesDecrypt failed! %d", ret);
1048 goto clearup;
1049 }
1050 clearup:
1051 HcfObjDestroy((HcfObjectBase *)key);
1052 HcfObjDestroy((HcfObjectBase *)cipher);
1053 HcfObjDestroy((HcfObjectBase *)generator);
1054 EXPECT_EQ(ret, 0);
1055 }
1056
1057 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest013, TestSize.Level0)
1058 {
1059 int ret = 0;
1060 uint8_t iv[16] = {0};
1061 uint8_t cipherText[128] = {0};
1062 int cipherTextLen = 128;
1063
1064 HcfIvParamsSpec ivSpec = {};
1065 HcfSymKeyGenerator *generator = NULL;
1066 HcfCipher *cipher = NULL;
1067 HcfSymKey *key = NULL;
1068 ivSpec.iv.data = iv;
1069 ivSpec.iv.len = 16;
1070
1071 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1072 if (ret != 0) {
1073 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1074 goto clearup;
1075 }
1076
1077 ret = generator->generateSymKey(generator, &key);
1078 if (ret != 0) {
1079 LOGE("generateSymKey failed!");
1080 goto clearup;
1081 }
1082
1083 ret = HcfCipherCreate("AES128|CFB|NoPadding", &cipher);
1084 if (ret != 0) {
1085 LOGE("HcfCipherCreate failed!");
1086 goto clearup;
1087 }
1088
1089 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1090 if (ret != 0) {
1091 LOGE("AesEncrypt failed! %d", ret);
1092 goto clearup;
1093 }
1094
1095 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1096 if (ret != 0) {
1097 LOGE("AesDecrypt failed! %d", ret);
1098 goto clearup;
1099 }
1100
1101 clearup:
1102 HcfObjDestroy((HcfObjectBase *)key);
1103 HcfObjDestroy((HcfObjectBase *)cipher);
1104 HcfObjDestroy((HcfObjectBase *)generator);
1105 EXPECT_EQ(ret, 0);
1106 }
1107
1108 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest014, TestSize.Level0)
1109 {
1110 int ret = 0;
1111 uint8_t iv[16] = {0};
1112 uint8_t cipherText[128] = {0};
1113 int cipherTextLen = 128;
1114
1115 HcfIvParamsSpec ivSpec = {};
1116 HcfSymKeyGenerator *generator = NULL;
1117 HcfCipher *cipher = NULL;
1118 HcfSymKey *key = NULL;
1119 ivSpec.iv.data = iv;
1120 ivSpec.iv.len = 16;
1121
1122 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1123 if (ret != 0) {
1124 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1125 goto clearup;
1126 }
1127
1128 ret = generator->generateSymKey(generator, &key);
1129 if (ret != 0) {
1130 LOGE("generateSymKey failed!");
1131 goto clearup;
1132 }
1133
1134 ret = HcfCipherCreate("AES128|CFB|PKCS5", &cipher);
1135 if (ret != 0) {
1136 LOGE("HcfCipherCreate failed!");
1137 goto clearup;
1138 }
1139
1140 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1141 if (ret != 0) {
1142 LOGE("AesEncrypt failed! %d", ret);
1143 goto clearup;
1144 }
1145
1146 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1147 if (ret != 0) {
1148 LOGE("AesDecrypt failed! %d", ret);
1149 goto clearup;
1150 }
1151
1152 clearup:
1153 HcfObjDestroy((HcfObjectBase *)key);
1154 HcfObjDestroy((HcfObjectBase *)cipher);
1155 HcfObjDestroy((HcfObjectBase *)generator);
1156 EXPECT_EQ(ret, 0);
1157 }
1158
1159 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest015, TestSize.Level0)
1160 {
1161 int ret = 0;
1162 uint8_t iv[16] = {0};
1163 uint8_t cipherText[128] = {0};
1164 int cipherTextLen = 128;
1165
1166 HcfIvParamsSpec ivSpec = {};
1167 HcfSymKeyGenerator *generator = NULL;
1168 HcfCipher *cipher = NULL;
1169 HcfSymKey *key = NULL;
1170 ivSpec.iv.data = iv;
1171 ivSpec.iv.len = 16;
1172
1173 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1174 if (ret != 0) {
1175 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1176 goto clearup;
1177 }
1178
1179 ret = generator->generateSymKey(generator, &key);
1180 if (ret != 0) {
1181 LOGE("generateSymKey failed!");
1182 goto clearup;
1183 }
1184
1185 ret = HcfCipherCreate("AES128|CFB|PKCS7", &cipher);
1186 if (ret != 0) {
1187 LOGE("HcfCipherCreate failed!");
1188 goto clearup;
1189 }
1190
1191 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1192 if (ret != 0) {
1193 LOGE("AesEncrypt failed! %d", ret);
1194 goto clearup;
1195 }
1196
1197 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1198 if (ret != 0) {
1199 LOGE("AesDecrypt failed! %d", ret);
1200 goto clearup;
1201 }
1202
1203 clearup:
1204 HcfObjDestroy((HcfObjectBase *)key);
1205 HcfObjDestroy((HcfObjectBase *)cipher);
1206 HcfObjDestroy((HcfObjectBase *)generator);
1207 EXPECT_EQ(ret, 0);
1208 }
1209
1210
1211 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest016, TestSize.Level0)
1212 {
1213 int ret = 0;
1214 uint8_t iv[16] = {0};
1215 uint8_t cipherText[128] = {0};
1216 int cipherTextLen = 128;
1217
1218 HcfIvParamsSpec ivSpec = {};
1219 HcfSymKeyGenerator *generator = NULL;
1220 HcfCipher *cipher = NULL;
1221 HcfSymKey *key = NULL;
1222 ivSpec.iv.data = iv;
1223 ivSpec.iv.len = 16;
1224
1225 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1226 if (ret != 0) {
1227 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1228 goto clearup;
1229 }
1230
1231 ret = generator->generateSymKey(generator, &key);
1232 if (ret != 0) {
1233 LOGE("generateSymKey failed!");
1234 goto clearup;
1235 }
1236
1237 ret = HcfCipherCreate("AES128|CFB1|NoPadding", &cipher);
1238 if (ret != 0) {
1239 LOGE("HcfCipherCreate failed!");
1240 goto clearup;
1241 }
1242
1243 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1244 if (ret != 0) {
1245 LOGE("AesEncrypt failed! %d", ret);
1246 goto clearup;
1247 }
1248
1249 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1250 if (ret != 0) {
1251 LOGE("AesDecrypt failed! %d", ret);
1252 goto clearup;
1253 }
1254
1255 clearup:
1256 HcfObjDestroy((HcfObjectBase *)key);
1257 HcfObjDestroy((HcfObjectBase *)cipher);
1258 HcfObjDestroy((HcfObjectBase *)generator);
1259 EXPECT_EQ(ret, 0);
1260 }
1261
1262 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest017, TestSize.Level0)
1263 {
1264 int ret = 0;
1265 uint8_t iv[16] = {0};
1266 uint8_t cipherText[128] = {0};
1267 int cipherTextLen = 128;
1268
1269 HcfIvParamsSpec ivSpec = {};
1270 HcfSymKeyGenerator *generator = NULL;
1271 HcfCipher *cipher = NULL;
1272 HcfSymKey *key = NULL;
1273 ivSpec.iv.data = iv;
1274 ivSpec.iv.len = 16;
1275
1276 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1277 if (ret != 0) {
1278 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1279 goto clearup;
1280 }
1281
1282 ret = generator->generateSymKey(generator, &key);
1283 if (ret != 0) {
1284 LOGE("generateSymKey failed!");
1285 goto clearup;
1286 }
1287
1288 ret = HcfCipherCreate("AES128|CFB1|PKCS5", &cipher);
1289 if (ret != 0) {
1290 LOGE("HcfCipherCreate failed!");
1291 goto clearup;
1292 }
1293
1294 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1295 if (ret != 0) {
1296 LOGE("AesEncrypt failed! %d", ret);
1297 goto clearup;
1298 }
1299
1300 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1301 if (ret != 0) {
1302 LOGE("AesDecrypt failed! %d", ret);
1303 goto clearup;
1304 }
1305
1306 clearup:
1307 HcfObjDestroy((HcfObjectBase *)key);
1308 HcfObjDestroy((HcfObjectBase *)cipher);
1309 HcfObjDestroy((HcfObjectBase *)generator);
1310 EXPECT_EQ(ret, 0);
1311 }
1312
1313 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest018, TestSize.Level0)
1314 {
1315 int ret = 0;
1316 uint8_t iv[16] = {0};
1317 uint8_t cipherText[128] = {0};
1318 int cipherTextLen = 128;
1319
1320 HcfIvParamsSpec ivSpec = {};
1321 HcfSymKeyGenerator *generator = NULL;
1322 HcfCipher *cipher = NULL;
1323 HcfSymKey *key = NULL;
1324 ivSpec.iv.data = iv;
1325 ivSpec.iv.len = 16;
1326
1327 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1328 if (ret != 0) {
1329 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1330 goto clearup;
1331 }
1332
1333 ret = generator->generateSymKey(generator, &key);
1334 if (ret != 0) {
1335 LOGE("generateSymKey failed!");
1336 }
1337
1338 ret = HcfCipherCreate("AES128|CFB1|PKCS7", &cipher); // CFB1/CFB8/CFB128 bit
1339 if (ret != 0) {
1340 LOGE("HcfCipherCreate failed!");
1341 goto clearup;
1342 }
1343
1344 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1345 if (ret != 0) {
1346 LOGE("AesEncrypt failed! %d", ret);
1347 goto clearup;
1348 }
1349
1350 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1351 if (ret != 0) {
1352 LOGE("AesDecrypt failed! %d", ret);
1353 goto clearup;
1354 }
1355
1356 clearup:
1357 HcfObjDestroy((HcfObjectBase *)key);
1358 HcfObjDestroy((HcfObjectBase *)cipher);
1359 HcfObjDestroy((HcfObjectBase *)generator);
1360 EXPECT_EQ(ret, 0);
1361 }
1362
1363 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest019, TestSize.Level0)
1364 {
1365 int ret = 0;
1366 uint8_t iv[16] = {0};
1367 uint8_t cipherText[128] = {0};
1368 int cipherTextLen = 128;
1369
1370 HcfIvParamsSpec ivSpec = {};
1371 HcfSymKeyGenerator *generator = NULL;
1372 HcfCipher *cipher = NULL;
1373 HcfSymKey *key = NULL;
1374 ivSpec.iv.data = iv;
1375 ivSpec.iv.len = 16;
1376
1377 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1378 if (ret != 0) {
1379 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1380 goto clearup;
1381 }
1382
1383 ret = generator->generateSymKey(generator, &key);
1384 if (ret != 0) {
1385 LOGE("generateSymKey failed!");
1386 }
1387
1388 ret = HcfCipherCreate("AES128|CFB8|NoPadding", &cipher); // CFB1/CFB8/CFB128 bit
1389 if (ret != 0) {
1390 LOGE("HcfCipherCreate failed!");
1391 goto clearup;
1392 }
1393
1394 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1395 if (ret != 0) {
1396 LOGE("AesEncrypt failed! %d", ret);
1397 goto clearup;
1398 }
1399
1400 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1401 if (ret != 0) {
1402 LOGE("AesDecrypt failed! %d", ret);
1403 goto clearup;
1404 }
1405
1406 clearup:
1407 HcfObjDestroy((HcfObjectBase *)key);
1408 HcfObjDestroy((HcfObjectBase *)cipher);
1409 HcfObjDestroy((HcfObjectBase *)generator);
1410 EXPECT_EQ(ret, 0);
1411 }
1412
1413 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest020, TestSize.Level0)
1414 {
1415 int ret = 0;
1416 uint8_t iv[16] = {0};
1417 uint8_t cipherText[128] = {0};
1418 int cipherTextLen = 128;
1419
1420 HcfIvParamsSpec ivSpec = {};
1421 HcfSymKeyGenerator *generator = NULL;
1422 HcfCipher *cipher = NULL;
1423 HcfSymKey *key = NULL;
1424 ivSpec.iv.data = iv;
1425 ivSpec.iv.len = 16;
1426
1427 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1428 if (ret != 0) {
1429 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1430 goto clearup;
1431 }
1432
1433 ret = generator->generateSymKey(generator, &key);
1434 if (ret != 0) {
1435 LOGE("generateSymKey failed!");
1436 }
1437
1438 ret = HcfCipherCreate("AES128|CFB8|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
1439 if (ret != 0) {
1440 LOGE("HcfCipherCreate failed!");
1441 goto clearup;
1442 }
1443
1444 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1445 if (ret != 0) {
1446 LOGE("AesEncrypt failed! %d", ret);
1447 goto clearup;
1448 }
1449
1450 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1451 if (ret != 0) {
1452 LOGE("AesDecrypt failed! %d", ret);
1453 goto clearup;
1454 }
1455
1456 clearup:
1457 HcfObjDestroy((HcfObjectBase *)key);
1458 HcfObjDestroy((HcfObjectBase *)cipher);
1459 HcfObjDestroy((HcfObjectBase *)generator);
1460 EXPECT_EQ(ret, 0);
1461 }
1462
1463 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest021, TestSize.Level0)
1464 {
1465 int ret = 0;
1466 uint8_t iv[16] = {0};
1467 uint8_t cipherText[128] = {0};
1468 int cipherTextLen = 128;
1469
1470 HcfIvParamsSpec ivSpec = {};
1471 HcfSymKeyGenerator *generator = NULL;
1472 HcfCipher *cipher = NULL;
1473 HcfSymKey *key = NULL;
1474 ivSpec.iv.data = iv;
1475 ivSpec.iv.len = 16;
1476
1477 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1478 if (ret != 0) {
1479 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1480 goto clearup;
1481 }
1482
1483 ret = generator->generateSymKey(generator, &key);
1484 if (ret != 0) {
1485 LOGE("generateSymKey failed!");
1486 }
1487
1488 ret = HcfCipherCreate("AES128|CFB8|PKCS7", &cipher); // CFB1/CFB8/CFB128 bit
1489 if (ret != 0) {
1490 LOGE("HcfCipherCreate failed!");
1491 goto clearup;
1492 }
1493
1494 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1495 if (ret != 0) {
1496 LOGE("AesEncrypt failed! %d", ret);
1497 goto clearup;
1498 }
1499
1500 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1501 if (ret != 0) {
1502 LOGE("AesDecrypt failed! %d", ret);
1503 goto clearup;
1504 }
1505
1506 clearup:
1507 HcfObjDestroy((HcfObjectBase *)key);
1508 HcfObjDestroy((HcfObjectBase *)cipher);
1509 HcfObjDestroy((HcfObjectBase *)generator);
1510 EXPECT_EQ(ret, 0);
1511 }
1512
1513 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest022, TestSize.Level0)
1514 {
1515 int ret = 0;
1516 uint8_t iv[16] = {0};
1517 uint8_t cipherText[128] = {0};
1518 int cipherTextLen = 128;
1519
1520 HcfIvParamsSpec ivSpec = {};
1521 HcfSymKeyGenerator *generator = NULL;
1522 HcfCipher *cipher = NULL;
1523 HcfSymKey *key = NULL;
1524 ivSpec.iv.data = iv;
1525 ivSpec.iv.len = 16;
1526
1527 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1528 if (ret != 0) {
1529 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1530 goto clearup;
1531 }
1532
1533 ret = generator->generateSymKey(generator, &key);
1534 if (ret != 0) {
1535 LOGE("generateSymKey failed!");
1536 }
1537
1538 ret = HcfCipherCreate("AES128|CFB128|NoPadding", &cipher); // CFB1/CFB8/CFB128 bit
1539 if (ret != 0) {
1540 LOGE("HcfCipherCreate failed!");
1541 goto clearup;
1542 }
1543
1544 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1545 if (ret != 0) {
1546 LOGE("AesEncrypt failed! %d", ret);
1547 goto clearup;
1548 }
1549
1550 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1551 if (ret != 0) {
1552 LOGE("AesDecrypt failed! %d", ret);
1553 goto clearup;
1554 }
1555
1556 clearup:
1557 HcfObjDestroy((HcfObjectBase *)key);
1558 HcfObjDestroy((HcfObjectBase *)cipher);
1559 HcfObjDestroy((HcfObjectBase *)generator);
1560 EXPECT_EQ(ret, 0);
1561 }
1562
1563 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest023, TestSize.Level0)
1564 {
1565 int ret = 0;
1566 uint8_t iv[16] = {0};
1567 uint8_t cipherText[128] = {0};
1568 int cipherTextLen = 128;
1569
1570 HcfIvParamsSpec ivSpec = {};
1571 HcfSymKeyGenerator *generator = NULL;
1572 HcfCipher *cipher = NULL;
1573 HcfSymKey *key = NULL;
1574 ivSpec.iv.data = iv;
1575 ivSpec.iv.len = 16;
1576
1577 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1578 if (ret != 0) {
1579 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1580 goto clearup;
1581 }
1582
1583 ret = generator->generateSymKey(generator, &key);
1584 if (ret != 0) {
1585 LOGE("generateSymKey failed!");
1586 }
1587
1588 ret = HcfCipherCreate("AES128|CFB128|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
1589 if (ret != 0) {
1590 LOGE("HcfCipherCreate failed!");
1591 goto clearup;
1592 }
1593
1594 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1595 if (ret != 0) {
1596 LOGE("AesEncrypt failed! %d", ret);
1597 goto clearup;
1598 }
1599
1600 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1601 if (ret != 0) {
1602 LOGE("AesDecrypt failed! %d", ret);
1603 goto clearup;
1604 }
1605
1606 clearup:
1607 HcfObjDestroy((HcfObjectBase *)key);
1608 HcfObjDestroy((HcfObjectBase *)cipher);
1609 HcfObjDestroy((HcfObjectBase *)generator);
1610 EXPECT_EQ(ret, 0);
1611 }
1612
1613 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest024, TestSize.Level0)
1614 {
1615 int ret = 0;
1616 uint8_t iv[16] = {0};
1617 uint8_t cipherText[128] = {0};
1618 int cipherTextLen = 128;
1619
1620 HcfIvParamsSpec ivSpec = {};
1621 HcfSymKeyGenerator *generator = NULL;
1622 HcfCipher *cipher = NULL;
1623 HcfSymKey *key = NULL;
1624 ivSpec.iv.data = iv;
1625 ivSpec.iv.len = 16;
1626
1627 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
1628 if (ret != 0) {
1629 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
1630 goto clearup;
1631 }
1632
1633 ret = generator->generateSymKey(generator, &key);
1634 if (ret != 0) {
1635 LOGE("generateSymKey failed!");
1636 }
1637
1638 ret = HcfCipherCreate("AES128|CFB128|PKCS7", &cipher); // CFB1/CFB8/CFB128 bit
1639 if (ret != 0) {
1640 LOGE("HcfCipherCreate failed!");
1641 goto clearup;
1642 }
1643
1644 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1645 if (ret != 0) {
1646 LOGE("AesEncrypt failed! %d", ret);
1647 goto clearup;
1648 }
1649
1650 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1651 if (ret != 0) {
1652 LOGE("AesDecrypt failed! %d", ret);
1653 goto clearup;
1654 }
1655
1656 clearup:
1657 HcfObjDestroy((HcfObjectBase *)key);
1658 HcfObjDestroy((HcfObjectBase *)cipher);
1659 HcfObjDestroy((HcfObjectBase *)generator);
1660 EXPECT_EQ(ret, 0);
1661 }
1662
1663 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest025, TestSize.Level0)
1664 {
1665 int ret = 0;
1666 uint8_t aad[8] = {0};
1667 uint8_t tag[16] = {0};
1668 uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
1669 uint8_t cipherText[128] = {0};
1670 int cipherTextLen = 128;
1671
1672 HcfCipher *cipher = NULL;
1673 HcfSymKey *key = NULL;
1674
1675 HcfGcmParamsSpec spec = {};
1676 spec.aad.data = aad;
1677 spec.aad.len = sizeof(aad);
1678 spec.tag.data = tag;
1679 spec.tag.len = sizeof(tag);
1680 spec.iv.data = iv;
1681 spec.iv.len = sizeof(iv);
1682
1683 ret = GenerateSymKey("AES128", &key);
1684 if (ret != 0) {
1685 LOGE("generateSymKey failed!");
1686 goto clearup;
1687 }
1688
1689 ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
1690 if (ret != 0) {
1691 LOGE("HcfCipherCreate failed!");
1692 goto clearup;
1693 }
1694
1695 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
1696 if (ret != 0) {
1697 LOGE("AesEncrypt failed, ret:%d!", ret);
1698 goto clearup;
1699 }
1700
1701 (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
1702 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
1703 cipherTextLen -= 16;
1704
1705 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
1706 if (ret != 0) {
1707 LOGE("AesDecrypt failed, ret:%d!", ret);
1708 goto clearup;
1709 }
1710
1711 clearup:
1712 HcfObjDestroy((HcfObjectBase *)key);
1713 HcfObjDestroy((HcfObjectBase *)cipher);
1714 EXPECT_EQ(ret, 0);
1715 }
1716
1717 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest026, TestSize.Level0)
1718 {
1719 int ret = 0;
1720 uint8_t aad[8] = {0};
1721 uint8_t tag[16] = {0};
1722 uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
1723 uint8_t cipherText[128] = {0};
1724 int cipherTextLen = 128;
1725
1726 HcfCipher *cipher = NULL;
1727 HcfSymKey *key = NULL;
1728
1729 HcfGcmParamsSpec spec = {};
1730 spec.aad.data = aad;
1731 spec.aad.len = sizeof(aad);
1732 spec.tag.data = tag;
1733 spec.tag.len = sizeof(tag);
1734 spec.iv.data = iv;
1735 spec.iv.len = sizeof(iv);
1736
1737 ret = GenerateSymKey("AES128", &key);
1738 if (ret != 0) {
1739 LOGE("generateSymKey failed!");
1740 goto clearup;
1741 }
1742
1743 ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
1744 if (ret != 0) {
1745 LOGE("HcfCipherCreate failed!");
1746 goto clearup;
1747 }
1748
1749 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
1750 if (ret != 0) {
1751 LOGE("AesEncrypt failed, ret:%d!", ret);
1752 goto clearup;
1753 }
1754
1755 (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
1756 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
1757 cipherTextLen -= 16;
1758
1759 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
1760 if (ret != 0) {
1761 LOGE("AesDecrypt failed, ret:%d!", ret);
1762 goto clearup;
1763 }
1764
1765 clearup:
1766 HcfObjDestroy((HcfObjectBase *)key);
1767 HcfObjDestroy((HcfObjectBase *)cipher);
1768 EXPECT_EQ(ret, 0);
1769 }
1770
1771 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest027, TestSize.Level0)
1772 {
1773 int ret = 0;
1774 uint8_t aad[8] = {0};
1775 uint8_t tag[16] = {0};
1776 uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
1777 uint8_t cipherText[128] = {0};
1778 int cipherTextLen = 128;
1779
1780 HcfCipher *cipher = NULL;
1781 HcfSymKey *key = NULL;
1782
1783 HcfGcmParamsSpec spec = {};
1784 spec.aad.data = aad;
1785 spec.aad.len = sizeof(aad);
1786 spec.tag.data = tag;
1787 spec.tag.len = sizeof(tag);
1788 spec.iv.data = iv;
1789 spec.iv.len = sizeof(iv);
1790
1791 ret = GenerateSymKey("AES128", &key);
1792 if (ret != 0) {
1793 LOGE("generateSymKey failed!");
1794 goto clearup;
1795 }
1796
1797 ret = HcfCipherCreate("AES128|GCM|PKCS7", &cipher);
1798 if (ret != 0) {
1799 LOGE("HcfCipherCreate failed!");
1800 goto clearup;
1801 }
1802
1803 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
1804 if (ret != 0) {
1805 LOGE("AesEncrypt failed, ret:%d!", ret);
1806 goto clearup;
1807 }
1808
1809 (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
1810 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
1811 cipherTextLen -= 16;
1812
1813 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
1814 if (ret != 0) {
1815 LOGE("AesDecrypt failed, ret:%d!", ret);
1816 goto clearup;
1817 }
1818
1819 clearup:
1820 HcfObjDestroy((HcfObjectBase *)key);
1821 HcfObjDestroy((HcfObjectBase *)cipher);
1822 EXPECT_EQ(ret, 0);
1823 }
1824
1825 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest028, TestSize.Level0)
1826 {
1827 int ret = 0;
1828 uint8_t aad[8] = {0};
1829 uint8_t tag[12] = {0};
1830 uint8_t iv[7] = {0};
1831 uint8_t cipherText[128] = {0};
1832 int cipherTextLen = 128;
1833
1834 HcfCipher *cipher = NULL;
1835 HcfSymKey *key = NULL;
1836 HcfCcmParamsSpec spec = {};
1837 spec.aad.data = aad;
1838 spec.aad.len = sizeof(aad);
1839 spec.tag.data = tag;
1840 spec.tag.len = sizeof(tag);
1841 spec.iv.data = iv;
1842 spec.iv.len = sizeof(iv);
1843
1844 ret = GenerateSymKey("AES128", &key);
1845 if (ret != 0) {
1846 LOGE("generateSymKey failed!");
1847 goto clearup;
1848 }
1849
1850 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
1851 if (ret != 0) {
1852 LOGE("HcfCipherCreate failed!");
1853 goto clearup;
1854 }
1855
1856 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
1857 if (ret != 0) {
1858 LOGE("AesEncrypt failed!");
1859 goto clearup;
1860 }
1861
1862 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
1863 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
1864 cipherTextLen -= 12;
1865
1866 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
1867 if (ret != 0) {
1868 LOGE("AesDecrypt failed!");
1869 goto clearup;
1870 }
1871
1872 clearup:
1873 HcfObjDestroy((HcfObjectBase *)key);
1874 HcfObjDestroy((HcfObjectBase *)cipher);
1875 EXPECT_EQ(ret, 0);
1876 }
1877
1878 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest029, TestSize.Level0)
1879 {
1880 int ret = 0;
1881 uint8_t aad[8] = {0};
1882 uint8_t tag[12] = {0};
1883 uint8_t iv[7] = {0};
1884 uint8_t cipherText[128] = {0};
1885 int cipherTextLen = 128;
1886
1887 HcfCipher *cipher = NULL;
1888 HcfSymKey *key = NULL;
1889 HcfCcmParamsSpec spec = {};
1890 spec.aad.data = aad;
1891 spec.aad.len = sizeof(aad);
1892 spec.tag.data = tag;
1893 spec.tag.len = sizeof(tag);
1894 spec.iv.data = iv;
1895 spec.iv.len = sizeof(iv);
1896
1897 ret = GenerateSymKey("AES128", &key);
1898 if (ret != 0) {
1899 LOGE("generateSymKey failed!");
1900 goto clearup;
1901 }
1902
1903 ret = HcfCipherCreate("AES128|CCM|PKCS5", &cipher);
1904 if (ret != 0) {
1905 LOGE("HcfCipherCreate failed!");
1906 goto clearup;
1907 }
1908
1909 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
1910 if (ret != 0) {
1911 LOGE("AesEncrypt failed!");
1912 goto clearup;
1913 }
1914
1915 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
1916 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
1917 cipherTextLen -= 12;
1918
1919 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
1920 if (ret != 0) {
1921 LOGE("AesDecrypt failed!");
1922 goto clearup;
1923 }
1924
1925 clearup:
1926 HcfObjDestroy((HcfObjectBase *)key);
1927 HcfObjDestroy((HcfObjectBase *)cipher);
1928 EXPECT_EQ(ret, 0);
1929 }
1930
1931 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest030, TestSize.Level0)
1932 {
1933 int ret = 0;
1934 uint8_t aad[8] = {0};
1935 uint8_t tag[12] = {0};
1936 uint8_t iv[7] = {0};
1937 uint8_t cipherText[128] = {0};
1938 int cipherTextLen = 128;
1939
1940 HcfCipher *cipher = NULL;
1941 HcfSymKey *key = NULL;
1942 HcfCcmParamsSpec spec = {};
1943 spec.aad.data = aad;
1944 spec.aad.len = sizeof(aad);
1945 spec.tag.data = tag;
1946 spec.tag.len = sizeof(tag);
1947 spec.iv.data = iv;
1948 spec.iv.len = sizeof(iv);
1949
1950 ret = GenerateSymKey("AES128", &key);
1951 if (ret != 0) {
1952 LOGE("generateSymKey failed!");
1953 goto clearup;
1954 }
1955
1956 ret = HcfCipherCreate("AES128|CCM|PKCS7", &cipher);
1957 if (ret != 0) {
1958 LOGE("HcfCipherCreate failed!");
1959 goto clearup;
1960 }
1961
1962 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
1963 if (ret != 0) {
1964 LOGE("AesEncrypt failed!");
1965 goto clearup;
1966 }
1967
1968 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
1969 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
1970 cipherTextLen -= 12;
1971
1972 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
1973 if (ret != 0) {
1974 LOGE("AesDecrypt failed!");
1975 goto clearup;
1976 }
1977
1978 clearup:
1979 HcfObjDestroy((HcfObjectBase *)key);
1980 HcfObjDestroy((HcfObjectBase *)cipher);
1981 EXPECT_EQ(ret, 0);
1982 }
1983
1984 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest031, TestSize.Level0)
1985 {
1986 int ret = 0;
1987 uint8_t cipherText[128] = {0};
1988 int cipherTextLen = 128;
1989
1990 HcfCipher *cipher = NULL;
1991 HcfSymKey *key = NULL;
1992 uint8_t codeCipherText[] = {
1993 0xF5, 0x12, 0xA0, 0x33, 0xCD, 0xCF, 0x0D, 0x32,
1994 0x3E, 0xFF, 0x80, 0x53, 0x89, 0xB6, 0xE4, 0xFE
1995 };
1996
1997 ret = ConvertSymKey("AES128", &key);
1998 if (ret != 0) {
1999 LOGE("generateSymKey failed!");
2000 goto clearup;
2001 }
2002
2003 ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
2004 if (ret != 0) {
2005 LOGE("HcfCipherCreate failed!");
2006 goto clearup;
2007 }
2008
2009 ret = AesEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
2010 if (ret != 0) {
2011 LOGE("AesEncrypt failed! %d", ret);
2012 goto clearup;
2013 }
2014
2015 ret = memcmp(cipherText, codeCipherText, cipherTextLen);
2016 if (ret != 0) {
2017 LOGE("cipherText compare failed!");
2018 goto clearup;
2019 }
2020
2021 ret = AesDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
2022 if (ret != 0) {
2023 LOGE("AesDecrypt failed! %d", ret);
2024 goto clearup;
2025 }
2026
2027 clearup:
2028 HcfObjDestroy((HcfObjectBase *)key);
2029 HcfObjDestroy((HcfObjectBase *)cipher);
2030 EXPECT_EQ(ret, 0);
2031 }
2032
2033 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest032, TestSize.Level0)
2034 {
2035 int ret = 0;
2036 uint8_t cipherText[128] = {0};
2037 int cipherTextLen = 128;
2038
2039 HcfSymKeyGenerator *generator = NULL;
2040 HcfCipher *cipher = NULL;
2041 HcfSymKey *key = NULL;
2042
2043 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2044 if (ret != 0) {
2045 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2046 goto clearup;
2047 }
2048
2049 ret = generator->generateSymKey(generator, &key);
2050 if (ret != 0) {
2051 LOGE("generateSymKey failed!");
2052 goto clearup;
2053 }
2054
2055 ret = HcfCipherCreate("AES128|ECB|NoPadding", &cipher);
2056 if (ret != 0) {
2057 LOGE("HcfCipherCreate failed!");
2058 goto clearup;
2059 }
2060
2061 ret = AesNoUpdateEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
2062 if (ret != 0) {
2063 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2064 goto clearup;
2065 }
2066
2067 ret = AesNoUpdateDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
2068 if (ret != 0) {
2069 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2070 goto clearup;
2071 }
2072 clearup:
2073 HcfObjDestroy((HcfObjectBase *)key);
2074 HcfObjDestroy((HcfObjectBase *)cipher);
2075 HcfObjDestroy((HcfObjectBase *)generator);
2076 EXPECT_NE(ret, 0);
2077 }
2078
2079 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest033, TestSize.Level0)
2080 {
2081 int ret = 0;
2082 uint8_t cipherText[128] = {0};
2083 int cipherTextLen = 128;
2084
2085 HcfSymKeyGenerator *generator = NULL;
2086 HcfCipher *cipher = NULL;
2087 HcfSymKey *key = NULL;
2088 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2089 if (ret != 0) {
2090 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2091 goto clearup;
2092 }
2093
2094 ret = generator->generateSymKey(generator, &key);
2095 if (ret != 0) {
2096 LOGE("generateSymKey failed!");
2097 goto clearup;
2098 }
2099
2100 ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher);
2101 if (ret != 0) {
2102 LOGE("HcfCipherCreate failed!");
2103 goto clearup;
2104 }
2105
2106 ret = AesNoUpdateEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
2107 if (ret != 0) {
2108 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2109 goto clearup;
2110 }
2111
2112 ret = AesNoUpdateDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
2113 if (ret != 0) {
2114 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2115 goto clearup;
2116 }
2117 clearup:
2118 HcfObjDestroy((HcfObjectBase *)key);
2119 HcfObjDestroy((HcfObjectBase *)cipher);
2120 HcfObjDestroy((HcfObjectBase *)generator);
2121 EXPECT_EQ(ret, 0);
2122 }
2123
2124 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest034, TestSize.Level0)
2125 {
2126 int ret = 0;
2127 uint8_t cipherText[128] = {0};
2128 int cipherTextLen = 128;
2129
2130 HcfSymKeyGenerator *generator = NULL;
2131 HcfCipher *cipher = NULL;
2132 HcfSymKey *key = NULL;
2133 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2134 if (ret != 0) {
2135 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2136 goto clearup;
2137 }
2138
2139 ret = generator->generateSymKey(generator, &key);
2140 if (ret != 0) {
2141 LOGE("generateSymKey failed!");
2142 goto clearup;
2143 }
2144
2145 ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
2146 if (ret != 0) {
2147 LOGE("HcfCipherCreate failed!");
2148 goto clearup;
2149 }
2150
2151 ret = AesNoUpdateEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
2152 if (ret != 0) {
2153 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2154 goto clearup;
2155 }
2156
2157 ret = AesNoUpdateDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
2158 if (ret != 0) {
2159 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2160 goto clearup;
2161 }
2162
2163 clearup:
2164 HcfObjDestroy((HcfObjectBase *)key);
2165 HcfObjDestroy((HcfObjectBase *)cipher);
2166 HcfObjDestroy((HcfObjectBase *)generator);
2167 EXPECT_EQ(ret, 0);
2168 }
2169
2170 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest035, TestSize.Level0)
2171 {
2172 int ret = 0;
2173 uint8_t iv[16] = {0};
2174 uint8_t cipherText[128] = {0};
2175 int cipherTextLen = 128;
2176
2177 HcfIvParamsSpec ivSpec = {};
2178 HcfSymKeyGenerator *generator = NULL;
2179 HcfCipher *cipher = NULL;
2180 HcfSymKey *key = NULL;
2181 ivSpec.iv.data = iv;
2182 ivSpec.iv.len = 16;
2183
2184 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2185 if (ret != 0) {
2186 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2187 goto clearup;
2188 }
2189
2190 ret = generator->generateSymKey(generator, &key);
2191 if (ret != 0) {
2192 LOGE("generateSymKey failed!");
2193 goto clearup;
2194 }
2195
2196 ret = HcfCipherCreate("AES128|CBC|NoPadding", &cipher);
2197 if (ret != 0) {
2198 LOGE("HcfCipherCreate failed!");
2199 goto clearup;
2200 }
2201
2202 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2203 if (ret != 0) {
2204 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2205 goto clearup;
2206 }
2207
2208 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2209 if (ret != 0) {
2210 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2211 goto clearup;
2212 }
2213
2214 clearup:
2215 HcfObjDestroy((HcfObjectBase *)key);
2216 HcfObjDestroy((HcfObjectBase *)cipher);
2217 HcfObjDestroy((HcfObjectBase *)generator);
2218 EXPECT_NE(ret, 0);
2219 }
2220
2221 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest036, TestSize.Level0)
2222 {
2223 int ret = 0;
2224 uint8_t iv[16] = {0};
2225 uint8_t cipherText[128] = {0};
2226 int cipherTextLen = 128;
2227
2228 HcfIvParamsSpec ivSpec = {};
2229 HcfSymKeyGenerator *generator = NULL;
2230 HcfCipher *cipher = NULL;
2231 HcfSymKey *key = NULL;
2232 ivSpec.iv.data = iv;
2233 ivSpec.iv.len = 16;
2234
2235 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2236 if (ret != 0) {
2237 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2238 goto clearup;
2239 }
2240
2241 ret = generator->generateSymKey(generator, &key);
2242 if (ret != 0) {
2243 LOGE("generateSymKey failed!");
2244 goto clearup;
2245 }
2246
2247 ret = HcfCipherCreate("AES128|CBC|PKCS5", &cipher);
2248 if (ret != 0) {
2249 LOGE("HcfCipherCreate failed!");
2250 goto clearup;
2251 }
2252
2253 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2254 if (ret != 0) {
2255 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2256 goto clearup;
2257 }
2258
2259 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2260 if (ret != 0) {
2261 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2262 goto clearup;
2263 }
2264
2265 clearup:
2266 HcfObjDestroy((HcfObjectBase *)key);
2267 HcfObjDestroy((HcfObjectBase *)cipher);
2268 HcfObjDestroy((HcfObjectBase *)generator);
2269 EXPECT_EQ(ret, 0);
2270 }
2271
2272 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest037, TestSize.Level0)
2273 {
2274 int ret = 0;
2275 uint8_t iv[16] = {0};
2276 uint8_t cipherText[128] = {0};
2277 int cipherTextLen = 128;
2278
2279 HcfIvParamsSpec ivSpec = {};
2280 HcfSymKeyGenerator *generator = NULL;
2281 HcfCipher *cipher = NULL;
2282 HcfSymKey *key = NULL;
2283 ivSpec.iv.data = iv;
2284 ivSpec.iv.len = 16;
2285
2286 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2287 if (ret != 0) {
2288 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2289 goto clearup;
2290 }
2291
2292 ret = generator->generateSymKey(generator, &key);
2293 if (ret != 0) {
2294 LOGE("generateSymKey failed!");
2295 goto clearup;
2296 }
2297
2298 ret = HcfCipherCreate("AES128|CBC|PKCS7", &cipher);
2299 if (ret != 0) {
2300 LOGE("HcfCipherCreate failed!");
2301 goto clearup;
2302 }
2303
2304 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2305 if (ret != 0) {
2306 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2307 goto clearup;
2308 }
2309
2310 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2311 if (ret != 0) {
2312 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2313 goto clearup;
2314 }
2315
2316 clearup:
2317 HcfObjDestroy((HcfObjectBase *)key);
2318 HcfObjDestroy((HcfObjectBase *)cipher);
2319 HcfObjDestroy((HcfObjectBase *)generator);
2320 EXPECT_EQ(ret, 0);
2321 }
2322
2323 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest038, TestSize.Level0)
2324 {
2325 int ret = 0;
2326 uint8_t iv[16] = {0};
2327 uint8_t cipherText[128] = {0};
2328 int cipherTextLen = 128;
2329
2330 HcfIvParamsSpec ivSpec = {};
2331 HcfSymKeyGenerator *generator = NULL;
2332 HcfCipher *cipher = NULL;
2333 HcfSymKey *key = NULL;
2334 ivSpec.iv.data = iv;
2335 ivSpec.iv.len = 16;
2336
2337 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2338 if (ret != 0) {
2339 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2340 goto clearup;
2341 }
2342
2343 ret = generator->generateSymKey(generator, &key);
2344 if (ret != 0) {
2345 LOGE("generateSymKey failed!");
2346 goto clearup;
2347 }
2348
2349 ret = HcfCipherCreate("AES128|CTR|NoPadding", &cipher);
2350 if (ret != 0) {
2351 LOGE("HcfCipherCreate failed!");
2352 goto clearup;
2353 }
2354
2355 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2356 if (ret != 0) {
2357 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2358 goto clearup;
2359 }
2360
2361 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2362 if (ret != 0) {
2363 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2364 goto clearup;
2365 }
2366
2367 clearup:
2368 HcfObjDestroy((HcfObjectBase *)key);
2369 HcfObjDestroy((HcfObjectBase *)cipher);
2370 HcfObjDestroy((HcfObjectBase *)generator);
2371 EXPECT_EQ(ret, 0);
2372 }
2373
2374 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest039, TestSize.Level0)
2375 {
2376 int ret = 0;
2377 uint8_t iv[16] = {0};
2378 uint8_t cipherText[128] = {0};
2379 int cipherTextLen = 128;
2380
2381 HcfIvParamsSpec ivSpec = {};
2382 HcfSymKeyGenerator *generator = NULL;
2383 HcfCipher *cipher = NULL;
2384 HcfSymKey *key = NULL;
2385 ivSpec.iv.data = iv;
2386 ivSpec.iv.len = 16;
2387
2388 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2389 if (ret != 0) {
2390 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2391 goto clearup;
2392 }
2393
2394 ret = generator->generateSymKey(generator, &key);
2395 if (ret != 0) {
2396 LOGE("generateSymKey failed!");
2397 goto clearup;
2398 }
2399
2400 ret = HcfCipherCreate("AES128|CTR|PKCS5", &cipher);
2401 if (ret != 0) {
2402 LOGE("HcfCipherCreate failed!");
2403 goto clearup;
2404 }
2405
2406 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2407 if (ret != 0) {
2408 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2409 goto clearup;
2410 }
2411
2412 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2413 if (ret != 0) {
2414 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2415 goto clearup;
2416 }
2417
2418 clearup:
2419 HcfObjDestroy((HcfObjectBase *)key);
2420 HcfObjDestroy((HcfObjectBase *)cipher);
2421 HcfObjDestroy((HcfObjectBase *)generator);
2422 EXPECT_EQ(ret, 0);
2423 }
2424
2425 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest040, TestSize.Level0)
2426 {
2427 int ret = 0;
2428 uint8_t iv[16] = {0};
2429 uint8_t cipherText[128] = {0};
2430 int cipherTextLen = 128;
2431
2432 HcfIvParamsSpec ivSpec = {};
2433 HcfSymKeyGenerator *generator = NULL;
2434 HcfCipher *cipher = NULL;
2435 HcfSymKey *key = NULL;
2436 ivSpec.iv.data = iv;
2437 ivSpec.iv.len = 16;
2438
2439 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2440 if (ret != 0) {
2441 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2442 goto clearup;
2443 }
2444
2445 ret = generator->generateSymKey(generator, &key);
2446 if (ret != 0) {
2447 LOGE("generateSymKey failed!");
2448 goto clearup;
2449 }
2450
2451 ret = HcfCipherCreate("AES128|CTR|PKCS7", &cipher);
2452 if (ret != 0) {
2453 LOGE("HcfCipherCreate failed!");
2454 goto clearup;
2455 }
2456
2457 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2458 if (ret != 0) {
2459 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2460 goto clearup;
2461 }
2462
2463 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2464 if (ret != 0) {
2465 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2466 goto clearup;
2467 }
2468
2469 clearup:
2470 HcfObjDestroy((HcfObjectBase *)key);
2471 HcfObjDestroy((HcfObjectBase *)cipher);
2472 HcfObjDestroy((HcfObjectBase *)generator);
2473 EXPECT_EQ(ret, 0);
2474 }
2475
2476 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest041, TestSize.Level0)
2477 {
2478 int ret = 0;
2479 uint8_t iv[16] = {0};
2480 uint8_t cipherText[128] = {0};
2481 int cipherTextLen = 128;
2482
2483 HcfIvParamsSpec ivSpec = {};
2484 HcfSymKeyGenerator *generator = NULL;
2485 HcfCipher *cipher = NULL;
2486 HcfSymKey *key = NULL;
2487 ivSpec.iv.data = iv;
2488 ivSpec.iv.len = 16;
2489
2490 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2491 if (ret != 0) {
2492 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2493 goto clearup;
2494 }
2495
2496 ret = generator->generateSymKey(generator, &key);
2497 if (ret != 0) {
2498 LOGE("generateSymKey failed!");
2499 goto clearup;
2500 }
2501
2502 ret = HcfCipherCreate("AES128|OFB|NoPadding", &cipher);
2503 if (ret != 0) {
2504 LOGE("HcfCipherCreate failed!");
2505 goto clearup;
2506 }
2507
2508 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2509 if (ret != 0) {
2510 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2511 goto clearup;
2512 }
2513
2514 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2515 if (ret != 0) {
2516 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2517 goto clearup;
2518 }
2519
2520 clearup:
2521 HcfObjDestroy((HcfObjectBase *)key);
2522 HcfObjDestroy((HcfObjectBase *)cipher);
2523 HcfObjDestroy((HcfObjectBase *)generator);
2524 EXPECT_EQ(ret, 0);
2525 }
2526
2527 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest042, TestSize.Level0)
2528 {
2529 int ret = 0;
2530 uint8_t iv[16] = {0};
2531 uint8_t cipherText[128] = {0};
2532 int cipherTextLen = 128;
2533
2534 HcfIvParamsSpec ivSpec = {};
2535 HcfSymKeyGenerator *generator = NULL;
2536 HcfCipher *cipher = NULL;
2537 HcfSymKey *key = NULL;
2538 ivSpec.iv.data = iv;
2539 ivSpec.iv.len = 16;
2540
2541 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2542 if (ret != 0) {
2543 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2544 goto clearup;
2545 }
2546
2547 ret = generator->generateSymKey(generator, &key);
2548 if (ret != 0) {
2549 LOGE("generateSymKey failed!");
2550 goto clearup;
2551 }
2552
2553 ret = HcfCipherCreate("AES128|OFB|PKCS5", &cipher);
2554 if (ret != 0) {
2555 LOGE("HcfCipherCreate failed!");
2556 goto clearup;
2557 }
2558
2559 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2560 if (ret != 0) {
2561 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2562 goto clearup;
2563 }
2564
2565 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2566 if (ret != 0) {
2567 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2568 goto clearup;
2569 }
2570
2571 clearup:
2572 HcfObjDestroy((HcfObjectBase *)key);
2573 HcfObjDestroy((HcfObjectBase *)cipher);
2574 HcfObjDestroy((HcfObjectBase *)generator);
2575 EXPECT_EQ(ret, 0);
2576 }
2577
2578 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest043, TestSize.Level0)
2579 {
2580 int ret = 0;
2581 uint8_t iv[16] = {0};
2582 uint8_t cipherText[128] = {0};
2583 int cipherTextLen = 128;
2584
2585 HcfIvParamsSpec ivSpec = {};
2586 HcfSymKeyGenerator *generator = NULL;
2587 HcfCipher *cipher = NULL;
2588 HcfSymKey *key = NULL;
2589 ivSpec.iv.data = iv;
2590 ivSpec.iv.len = 16;
2591
2592 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2593 if (ret != 0) {
2594 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2595 goto clearup;
2596 }
2597
2598 ret = generator->generateSymKey(generator, &key);
2599 if (ret != 0) {
2600 LOGE("generateSymKey failed!");
2601 goto clearup;
2602 }
2603
2604 ret = HcfCipherCreate("AES128|OFB|PKCS7", &cipher);
2605 if (ret != 0) {
2606 LOGE("HcfCipherCreate failed!");
2607 goto clearup;
2608 }
2609
2610 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2611 if (ret != 0) {
2612 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2613 goto clearup;
2614 }
2615
2616 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2617 if (ret != 0) {
2618 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2619 goto clearup;
2620 }
2621 clearup:
2622 HcfObjDestroy((HcfObjectBase *)key);
2623 HcfObjDestroy((HcfObjectBase *)cipher);
2624 HcfObjDestroy((HcfObjectBase *)generator);
2625 EXPECT_EQ(ret, 0);
2626 }
2627
2628 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest044, TestSize.Level0)
2629 {
2630 int ret = 0;
2631 uint8_t iv[16] = {0};
2632 uint8_t cipherText[128] = {0};
2633 int cipherTextLen = 128;
2634
2635 HcfIvParamsSpec ivSpec = {};
2636 HcfSymKeyGenerator *generator = NULL;
2637 HcfCipher *cipher = NULL;
2638 HcfSymKey *key = NULL;
2639 ivSpec.iv.data = iv;
2640 ivSpec.iv.len = 16;
2641
2642 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2643 if (ret != 0) {
2644 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2645 goto clearup;
2646 }
2647
2648 ret = generator->generateSymKey(generator, &key);
2649 if (ret != 0) {
2650 LOGE("generateSymKey failed!");
2651 goto clearup;
2652 }
2653
2654 ret = HcfCipherCreate("AES128|CFB|NoPadding", &cipher);
2655 if (ret != 0) {
2656 LOGE("HcfCipherCreate failed!");
2657 goto clearup;
2658 }
2659
2660 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2661 if (ret != 0) {
2662 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2663 goto clearup;
2664 }
2665
2666 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2667 if (ret != 0) {
2668 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2669 goto clearup;
2670 }
2671
2672 clearup:
2673 HcfObjDestroy((HcfObjectBase *)key);
2674 HcfObjDestroy((HcfObjectBase *)cipher);
2675 HcfObjDestroy((HcfObjectBase *)generator);
2676 EXPECT_EQ(ret, 0);
2677 }
2678
2679 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest045, TestSize.Level0)
2680 {
2681 int ret = 0;
2682 uint8_t iv[16] = {0};
2683 uint8_t cipherText[128] = {0};
2684 int cipherTextLen = 128;
2685
2686 HcfIvParamsSpec ivSpec = {};
2687 HcfSymKeyGenerator *generator = NULL;
2688 HcfCipher *cipher = NULL;
2689 HcfSymKey *key = NULL;
2690 ivSpec.iv.data = iv;
2691 ivSpec.iv.len = 16;
2692
2693 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2694 if (ret != 0) {
2695 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2696 goto clearup;
2697 }
2698
2699 ret = generator->generateSymKey(generator, &key);
2700 if (ret != 0) {
2701 LOGE("generateSymKey failed!");
2702 goto clearup;
2703 }
2704
2705 ret = HcfCipherCreate("AES128|CFB|PKCS5", &cipher);
2706 if (ret != 0) {
2707 LOGE("HcfCipherCreate failed!");
2708 goto clearup;
2709 }
2710
2711 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2712 if (ret != 0) {
2713 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2714 goto clearup;
2715 }
2716
2717 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2718 if (ret != 0) {
2719 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2720 goto clearup;
2721 }
2722
2723 clearup:
2724 HcfObjDestroy((HcfObjectBase *)key);
2725 HcfObjDestroy((HcfObjectBase *)cipher);
2726 HcfObjDestroy((HcfObjectBase *)generator);
2727 EXPECT_EQ(ret, 0);
2728 }
2729
2730 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest046, TestSize.Level0)
2731 {
2732 int ret = 0;
2733 uint8_t iv[16] = {0};
2734 uint8_t cipherText[128] = {0};
2735 int cipherTextLen = 128;
2736
2737 HcfIvParamsSpec ivSpec = {};
2738 HcfSymKeyGenerator *generator = NULL;
2739 HcfCipher *cipher = NULL;
2740 HcfSymKey *key = NULL;
2741 ivSpec.iv.data = iv;
2742 ivSpec.iv.len = 16;
2743
2744 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2745 if (ret != 0) {
2746 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2747 goto clearup;
2748 }
2749
2750 ret = generator->generateSymKey(generator, &key);
2751 if (ret != 0) {
2752 LOGE("generateSymKey failed!");
2753 goto clearup;
2754 }
2755
2756 ret = HcfCipherCreate("AES128|CFB|PKCS7", &cipher);
2757 if (ret != 0) {
2758 LOGE("HcfCipherCreate failed!");
2759 goto clearup;
2760 }
2761
2762 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2763 if (ret != 0) {
2764 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2765 goto clearup;
2766 }
2767
2768 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2769 if (ret != 0) {
2770 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2771 goto clearup;
2772 }
2773
2774 clearup:
2775 HcfObjDestroy((HcfObjectBase *)key);
2776 HcfObjDestroy((HcfObjectBase *)cipher);
2777 HcfObjDestroy((HcfObjectBase *)generator);
2778 EXPECT_EQ(ret, 0);
2779 }
2780
2781
2782 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest047, TestSize.Level0)
2783 {
2784 int ret = 0;
2785 uint8_t iv[16] = {0};
2786 uint8_t cipherText[128] = {0};
2787 int cipherTextLen = 128;
2788
2789 HcfIvParamsSpec ivSpec = {};
2790 HcfSymKeyGenerator *generator = NULL;
2791 HcfCipher *cipher = NULL;
2792 HcfSymKey *key = NULL;
2793 ivSpec.iv.data = iv;
2794 ivSpec.iv.len = 16;
2795
2796 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2797 if (ret != 0) {
2798 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2799 goto clearup;
2800 }
2801
2802 ret = generator->generateSymKey(generator, &key);
2803 if (ret != 0) {
2804 LOGE("generateSymKey failed!");
2805 goto clearup;
2806 }
2807
2808 ret = HcfCipherCreate("AES128|CFB1|NoPadding", &cipher);
2809 if (ret != 0) {
2810 LOGE("HcfCipherCreate failed!");
2811 goto clearup;
2812 }
2813
2814 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2815 if (ret != 0) {
2816 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2817 goto clearup;
2818 }
2819
2820 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2821 if (ret != 0) {
2822 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2823 goto clearup;
2824 }
2825
2826 clearup:
2827 HcfObjDestroy((HcfObjectBase *)key);
2828 HcfObjDestroy((HcfObjectBase *)cipher);
2829 HcfObjDestroy((HcfObjectBase *)generator);
2830 EXPECT_EQ(ret, 0);
2831 }
2832
2833 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest048, TestSize.Level0)
2834 {
2835 int ret = 0;
2836 uint8_t iv[16] = {0};
2837 uint8_t cipherText[128] = {0};
2838 int cipherTextLen = 128;
2839
2840 HcfIvParamsSpec ivSpec = {};
2841 HcfSymKeyGenerator *generator = NULL;
2842 HcfCipher *cipher = NULL;
2843 HcfSymKey *key = NULL;
2844 ivSpec.iv.data = iv;
2845 ivSpec.iv.len = 16;
2846
2847 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2848 if (ret != 0) {
2849 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2850 goto clearup;
2851 }
2852
2853 ret = generator->generateSymKey(generator, &key);
2854 if (ret != 0) {
2855 LOGE("generateSymKey failed!");
2856 goto clearup;
2857 }
2858
2859 ret = HcfCipherCreate("AES128|CFB1|PKCS5", &cipher);
2860 if (ret != 0) {
2861 LOGE("HcfCipherCreate failed!");
2862 goto clearup;
2863 }
2864
2865 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2866 if (ret != 0) {
2867 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2868 goto clearup;
2869 }
2870
2871 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2872 if (ret != 0) {
2873 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2874 goto clearup;
2875 }
2876
2877 clearup:
2878 HcfObjDestroy((HcfObjectBase *)key);
2879 HcfObjDestroy((HcfObjectBase *)cipher);
2880 HcfObjDestroy((HcfObjectBase *)generator);
2881 EXPECT_EQ(ret, 0);
2882 }
2883
2884 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest049, TestSize.Level0)
2885 {
2886 int ret = 0;
2887 uint8_t iv[16] = {0};
2888 uint8_t cipherText[128] = {0};
2889 int cipherTextLen = 128;
2890
2891 HcfIvParamsSpec ivSpec = {};
2892 HcfSymKeyGenerator *generator = NULL;
2893 HcfCipher *cipher = NULL;
2894 HcfSymKey *key = NULL;
2895 ivSpec.iv.data = iv;
2896 ivSpec.iv.len = 16;
2897
2898 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2899 if (ret != 0) {
2900 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2901 goto clearup;
2902 }
2903
2904 ret = generator->generateSymKey(generator, &key);
2905 if (ret != 0) {
2906 LOGE("generateSymKey failed!");
2907 }
2908
2909 ret = HcfCipherCreate("AES128|CFB1|PKCS7", &cipher); // CFB1/CFB8/CFB128 bit
2910 if (ret != 0) {
2911 LOGE("HcfCipherCreate failed!");
2912 goto clearup;
2913 }
2914
2915 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2916 if (ret != 0) {
2917 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2918 goto clearup;
2919 }
2920
2921 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2922 if (ret != 0) {
2923 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2924 goto clearup;
2925 }
2926
2927 clearup:
2928 HcfObjDestroy((HcfObjectBase *)key);
2929 HcfObjDestroy((HcfObjectBase *)cipher);
2930 HcfObjDestroy((HcfObjectBase *)generator);
2931 EXPECT_EQ(ret, 0);
2932 }
2933
2934 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest050, TestSize.Level0)
2935 {
2936 int ret = 0;
2937 uint8_t iv[16] = {0};
2938 uint8_t cipherText[128] = {0};
2939 int cipherTextLen = 128;
2940
2941 HcfIvParamsSpec ivSpec = {};
2942 HcfSymKeyGenerator *generator = NULL;
2943 HcfCipher *cipher = NULL;
2944 HcfSymKey *key = NULL;
2945 ivSpec.iv.data = iv;
2946 ivSpec.iv.len = 16;
2947
2948 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2949 if (ret != 0) {
2950 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
2951 goto clearup;
2952 }
2953
2954 ret = generator->generateSymKey(generator, &key);
2955 if (ret != 0) {
2956 LOGE("generateSymKey failed!");
2957 }
2958
2959 ret = HcfCipherCreate("AES128|CFB8|NoPadding", &cipher); // CFB1/CFB8/CFB128 bit
2960 if (ret != 0) {
2961 LOGE("HcfCipherCreate failed!");
2962 goto clearup;
2963 }
2964
2965 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
2966 if (ret != 0) {
2967 LOGE("AesNoUpdateEncrypt failed! %d", ret);
2968 goto clearup;
2969 }
2970
2971 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
2972 if (ret != 0) {
2973 LOGE("AesNoUpdateDecrypt failed! %d", ret);
2974 goto clearup;
2975 }
2976
2977 clearup:
2978 HcfObjDestroy((HcfObjectBase *)key);
2979 HcfObjDestroy((HcfObjectBase *)cipher);
2980 HcfObjDestroy((HcfObjectBase *)generator);
2981 EXPECT_EQ(ret, 0);
2982 }
2983
2984 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest051, TestSize.Level0)
2985 {
2986 int ret = 0;
2987 uint8_t iv[16] = {0};
2988 uint8_t cipherText[128] = {0};
2989 int cipherTextLen = 128;
2990
2991 HcfIvParamsSpec ivSpec = {};
2992 HcfSymKeyGenerator *generator = NULL;
2993 HcfCipher *cipher = NULL;
2994 HcfSymKey *key = NULL;
2995 ivSpec.iv.data = iv;
2996 ivSpec.iv.len = 16;
2997
2998 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
2999 if (ret != 0) {
3000 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
3001 goto clearup;
3002 }
3003
3004 ret = generator->generateSymKey(generator, &key);
3005 if (ret != 0) {
3006 LOGE("generateSymKey failed!");
3007 }
3008
3009 ret = HcfCipherCreate("AES128|CFB8|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
3010 if (ret != 0) {
3011 LOGE("HcfCipherCreate failed!");
3012 goto clearup;
3013 }
3014
3015 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
3016 if (ret != 0) {
3017 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3018 goto clearup;
3019 }
3020
3021 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
3022 if (ret != 0) {
3023 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3024 goto clearup;
3025 }
3026
3027 clearup:
3028 HcfObjDestroy((HcfObjectBase *)key);
3029 HcfObjDestroy((HcfObjectBase *)cipher);
3030 HcfObjDestroy((HcfObjectBase *)generator);
3031 EXPECT_EQ(ret, 0);
3032 }
3033
3034 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest052, TestSize.Level0)
3035 {
3036 int ret = 0;
3037 uint8_t iv[16] = {0};
3038 uint8_t cipherText[128] = {0};
3039 int cipherTextLen = 128;
3040
3041 HcfIvParamsSpec ivSpec = {};
3042 HcfSymKeyGenerator *generator = NULL;
3043 HcfCipher *cipher = NULL;
3044 HcfSymKey *key = NULL;
3045 ivSpec.iv.data = iv;
3046 ivSpec.iv.len = 16;
3047
3048 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
3049 if (ret != 0) {
3050 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
3051 goto clearup;
3052 }
3053
3054 ret = generator->generateSymKey(generator, &key);
3055 if (ret != 0) {
3056 LOGE("generateSymKey failed!");
3057 }
3058
3059 ret = HcfCipherCreate("AES128|CFB8|PKCS7", &cipher); // CFB1/CFB8/CFB128 bit
3060 if (ret != 0) {
3061 LOGE("HcfCipherCreate failed!");
3062 goto clearup;
3063 }
3064 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
3065 if (ret != 0) {
3066 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3067 goto clearup;
3068 }
3069
3070 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
3071 if (ret != 0) {
3072 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3073 goto clearup;
3074 }
3075
3076 clearup:
3077 HcfObjDestroy((HcfObjectBase *)key);
3078 HcfObjDestroy((HcfObjectBase *)cipher);
3079 HcfObjDestroy((HcfObjectBase *)generator);
3080 EXPECT_EQ(ret, 0);
3081 }
3082
3083 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest053, TestSize.Level0)
3084 {
3085 int ret = 0;
3086 uint8_t iv[16] = {0};
3087 uint8_t cipherText[128] = {0};
3088 int cipherTextLen = 128;
3089
3090 HcfIvParamsSpec ivSpec = {};
3091 HcfSymKeyGenerator *generator = NULL;
3092 HcfCipher *cipher = NULL;
3093 HcfSymKey *key = NULL;
3094 ivSpec.iv.data = iv;
3095 ivSpec.iv.len = 16;
3096
3097 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
3098 if (ret != 0) {
3099 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
3100 goto clearup;
3101 }
3102
3103 ret = generator->generateSymKey(generator, &key);
3104 if (ret != 0) {
3105 LOGE("generateSymKey failed!");
3106 }
3107
3108 ret = HcfCipherCreate("AES128|CFB128|NoPadding", &cipher); // CFB1/CFB8/CFB128 bit
3109 if (ret != 0) {
3110 LOGE("HcfCipherCreate failed!");
3111 goto clearup;
3112 }
3113 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
3114 if (ret != 0) {
3115 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3116 goto clearup;
3117 }
3118
3119 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
3120 if (ret != 0) {
3121 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3122 goto clearup;
3123 }
3124
3125 clearup:
3126 HcfObjDestroy((HcfObjectBase *)key);
3127 HcfObjDestroy((HcfObjectBase *)cipher);
3128 HcfObjDestroy((HcfObjectBase *)generator);
3129 EXPECT_EQ(ret, 0);
3130 }
3131
3132 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest054, TestSize.Level0)
3133 {
3134 int ret = 0;
3135 uint8_t iv[16] = {0};
3136 uint8_t cipherText[128] = {0};
3137 int cipherTextLen = 128;
3138
3139 HcfIvParamsSpec ivSpec = {};
3140 HcfSymKeyGenerator *generator = NULL;
3141 HcfCipher *cipher = NULL;
3142 HcfSymKey *key = NULL;
3143 ivSpec.iv.data = iv;
3144 ivSpec.iv.len = 16;
3145
3146 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
3147 if (ret != 0) {
3148 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
3149 goto clearup;
3150 }
3151
3152 ret = generator->generateSymKey(generator, &key);
3153 if (ret != 0) {
3154 LOGE("generateSymKey failed!");
3155 }
3156
3157 ret = HcfCipherCreate("AES128|CFB128|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
3158 if (ret != 0) {
3159 LOGE("HcfCipherCreate failed!");
3160 goto clearup;
3161 }
3162 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
3163 if (ret != 0) {
3164 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3165 goto clearup;
3166 }
3167
3168 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
3169 if (ret != 0) {
3170 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3171 goto clearup;
3172 }
3173
3174 clearup:
3175 HcfObjDestroy((HcfObjectBase *)key);
3176 HcfObjDestroy((HcfObjectBase *)cipher);
3177 HcfObjDestroy((HcfObjectBase *)generator);
3178 EXPECT_EQ(ret, 0);
3179 }
3180
3181 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest055, TestSize.Level0)
3182 {
3183 int ret = 0;
3184 uint8_t iv[16] = {0};
3185 uint8_t cipherText[128] = {0};
3186 int cipherTextLen = 128;
3187
3188 HcfIvParamsSpec ivSpec = {};
3189 HcfSymKeyGenerator *generator = NULL;
3190 HcfCipher *cipher = NULL;
3191 HcfSymKey *key = NULL;
3192 ivSpec.iv.data = iv;
3193 ivSpec.iv.len = 16;
3194
3195 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
3196 if (ret != 0) {
3197 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
3198 goto clearup;
3199 }
3200
3201 ret = generator->generateSymKey(generator, &key);
3202 if (ret != 0) {
3203 LOGE("generateSymKey failed!");
3204 }
3205
3206 ret = HcfCipherCreate("AES128|CFB128|PKCS7", &cipher); // CFB1/CFB8/CFB128 bit
3207 if (ret != 0) {
3208 LOGE("HcfCipherCreate failed!");
3209 goto clearup;
3210 }
3211 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
3212 if (ret != 0) {
3213 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3214 goto clearup;
3215 }
3216
3217 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
3218 if (ret != 0) {
3219 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3220 goto clearup;
3221 }
3222
3223 clearup:
3224 HcfObjDestroy((HcfObjectBase *)key);
3225 HcfObjDestroy((HcfObjectBase *)cipher);
3226 HcfObjDestroy((HcfObjectBase *)generator);
3227 EXPECT_EQ(ret, 0);
3228 }
3229
3230 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest056, TestSize.Level0)
3231 {
3232 int ret = 0;
3233 uint8_t aad[8] = {0};
3234 uint8_t tag[16] = {0};
3235 uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
3236 uint8_t cipherText[128] = {0};
3237 int cipherTextLen = 128;
3238
3239 HcfCipher *cipher = NULL;
3240 HcfSymKey *key = NULL;
3241
3242 HcfGcmParamsSpec spec = {};
3243 spec.aad.data = aad;
3244 spec.aad.len = sizeof(aad);
3245 spec.tag.data = tag;
3246 spec.tag.len = sizeof(tag);
3247 spec.iv.data = iv;
3248 spec.iv.len = sizeof(iv);
3249
3250 ret = GenerateSymKey("AES128", &key);
3251 if (ret != 0) {
3252 LOGE("GenerateSymKey failed!");
3253 goto clearup;
3254 }
3255
3256 ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher);
3257 if (ret != 0) {
3258 LOGE("HcfCipherCreate failed!");
3259 goto clearup;
3260 }
3261
3262 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
3263 if (ret != 0) {
3264 LOGE("AesNoUpdateEncrypt failed, ret:%d!", ret);
3265 goto clearup;
3266 }
3267
3268 (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
3269 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
3270 cipherTextLen -= 16;
3271
3272 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
3273 if (ret != 0) {
3274 LOGE("AesNoUpdateDecrypt failed, ret:%d!", ret);
3275 goto clearup;
3276 }
3277
3278 clearup:
3279 HcfObjDestroy((HcfObjectBase *)key);
3280 HcfObjDestroy((HcfObjectBase *)cipher);
3281 EXPECT_EQ(ret, 0);
3282 }
3283
3284 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest057, TestSize.Level0)
3285 {
3286 int ret = 0;
3287 uint8_t aad[8] = {0};
3288 uint8_t tag[16] = {0};
3289 uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
3290 uint8_t cipherText[128] = {0};
3291 int cipherTextLen = 128;
3292
3293 HcfCipher *cipher = NULL;
3294 HcfSymKey *key = NULL;
3295
3296 HcfGcmParamsSpec spec = {};
3297 spec.aad.data = aad;
3298 spec.aad.len = sizeof(aad);
3299 spec.tag.data = tag;
3300 spec.tag.len = sizeof(tag);
3301 spec.iv.data = iv;
3302 spec.iv.len = sizeof(iv);
3303
3304 ret = GenerateSymKey("AES128", &key);
3305 if (ret != 0) {
3306 LOGE("GenerateSymKey failed!");
3307 goto clearup;
3308 }
3309
3310 ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher);
3311 if (ret != 0) {
3312 LOGE("HcfCipherCreate failed!");
3313 goto clearup;
3314 }
3315
3316 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
3317 if (ret != 0) {
3318 LOGE("AesNoUpdateEncrypt failed, ret:%d!", ret);
3319 goto clearup;
3320 }
3321
3322 (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
3323 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
3324 cipherTextLen -= 16;
3325
3326 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
3327 if (ret != 0) {
3328 LOGE("AesNoUpdateDecrypt failed, ret:%d!", ret);
3329 goto clearup;
3330 }
3331
3332 clearup:
3333 HcfObjDestroy((HcfObjectBase *)key);
3334 HcfObjDestroy((HcfObjectBase *)cipher);
3335 EXPECT_EQ(ret, 0);
3336 }
3337
3338 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest058, TestSize.Level0)
3339 {
3340 int ret = 0;
3341 uint8_t aad[8] = {0};
3342 uint8_t tag[16] = {0};
3343 uint8_t iv[12] = {0}; // openssl only support nonce 12 bytes, tag 16bytes
3344 uint8_t cipherText[128] = {0};
3345 int cipherTextLen = 128;
3346
3347 HcfCipher *cipher = NULL;
3348 HcfSymKey *key = NULL;
3349
3350 HcfGcmParamsSpec spec = {};
3351 spec.aad.data = aad;
3352 spec.aad.len = sizeof(aad);
3353 spec.tag.data = tag;
3354 spec.tag.len = sizeof(tag);
3355 spec.iv.data = iv;
3356 spec.iv.len = sizeof(iv);
3357
3358 ret = GenerateSymKey("AES128", &key);
3359 if (ret != 0) {
3360 LOGE("GenerateSymKey failed!");
3361 goto clearup;
3362 }
3363
3364 ret = HcfCipherCreate("AES128|GCM|PKCS7", &cipher);
3365 if (ret != 0) {
3366 LOGE("HcfCipherCreate failed!");
3367 goto clearup;
3368 }
3369
3370 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
3371 if (ret != 0) {
3372 LOGE("AesNoUpdateEncrypt failed, ret:%d!", ret);
3373 goto clearup;
3374 }
3375
3376 (void)memcpy_s(spec.tag.data, 16, cipherText + cipherTextLen - 16, 16);
3377 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
3378 cipherTextLen -= 16;
3379
3380 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
3381 if (ret != 0) {
3382 LOGE("AesNoUpdateDecrypt failed, ret:%d!", ret);
3383 goto clearup;
3384 }
3385
3386 clearup:
3387 HcfObjDestroy((HcfObjectBase *)key);
3388 HcfObjDestroy((HcfObjectBase *)cipher);
3389 EXPECT_EQ(ret, 0);
3390 }
3391
3392 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest059, TestSize.Level0)
3393 {
3394 int ret = 0;
3395 uint8_t aad[8] = {0};
3396 uint8_t tag[12] = {0};
3397 uint8_t iv[7] = {0};
3398 uint8_t cipherText[128] = {0};
3399 int cipherTextLen = 128;
3400
3401 HcfCipher *cipher = NULL;
3402 HcfSymKey *key = NULL;
3403 HcfCcmParamsSpec spec = {};
3404 spec.aad.data = aad;
3405 spec.aad.len = sizeof(aad);
3406 spec.tag.data = tag;
3407 spec.tag.len = sizeof(tag);
3408 spec.iv.data = iv;
3409 spec.iv.len = sizeof(iv);
3410
3411 ret = GenerateSymKey("AES128", &key);
3412 if (ret != 0) {
3413 LOGE("GenerateSymKey failed!");
3414 goto clearup;
3415 }
3416
3417 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher);
3418 if (ret != 0) {
3419 LOGE("HcfCipherCreate failed!");
3420 goto clearup;
3421 }
3422
3423 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
3424 if (ret != 0) {
3425 LOGE("AesNoUpdateEncrypt failed!");
3426 goto clearup;
3427 }
3428
3429 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
3430 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
3431 cipherTextLen -= 12;
3432
3433 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
3434 if (ret != 0) {
3435 LOGE("AesNoUpdateDecrypt failed!");
3436 goto clearup;
3437 }
3438
3439 clearup:
3440 HcfObjDestroy((HcfObjectBase *)key);
3441 HcfObjDestroy((HcfObjectBase *)cipher);
3442 EXPECT_EQ(ret, 0);
3443 }
3444
3445 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest060, TestSize.Level0)
3446 {
3447 int ret = 0;
3448 uint8_t aad[8] = {0};
3449 uint8_t tag[12] = {0};
3450 uint8_t iv[7] = {0};
3451 uint8_t cipherText[128] = {0};
3452 int cipherTextLen = 128;
3453
3454 HcfCipher *cipher = NULL;
3455 HcfSymKey *key = NULL;
3456 HcfCcmParamsSpec spec = {};
3457 spec.aad.data = aad;
3458 spec.aad.len = sizeof(aad);
3459 spec.tag.data = tag;
3460 spec.tag.len = sizeof(tag);
3461 spec.iv.data = iv;
3462 spec.iv.len = sizeof(iv);
3463
3464 ret = GenerateSymKey("AES128", &key);
3465 if (ret != 0) {
3466 LOGE("GenerateSymKey failed!");
3467 goto clearup;
3468 }
3469
3470 ret = HcfCipherCreate("AES128|CCM|PKCS5", &cipher);
3471 if (ret != 0) {
3472 LOGE("HcfCipherCreate failed!");
3473 goto clearup;
3474 }
3475
3476 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
3477 if (ret != 0) {
3478 LOGE("AesNoUpdateEncrypt failed!");
3479 goto clearup;
3480 }
3481
3482 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
3483 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
3484 cipherTextLen -= 12;
3485
3486 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
3487 if (ret != 0) {
3488 LOGE("AesNoUpdateDecrypt failed!");
3489 goto clearup;
3490 }
3491
3492 clearup:
3493 HcfObjDestroy((HcfObjectBase *)key);
3494 HcfObjDestroy((HcfObjectBase *)cipher);
3495 EXPECT_EQ(ret, 0);
3496 }
3497
3498
3499 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest061, TestSize.Level0)
3500 {
3501 int ret = 0;
3502 uint8_t aad[8] = {0};
3503 uint8_t tag[12] = {0};
3504 uint8_t iv[7] = {0};
3505 uint8_t cipherText[128] = {0};
3506 int cipherTextLen = 128;
3507
3508 HcfCipher *cipher = NULL;
3509 HcfSymKey *key = NULL;
3510 HcfCcmParamsSpec spec = {};
3511 spec.aad.data = aad;
3512 spec.aad.len = sizeof(aad);
3513 spec.tag.data = tag;
3514 spec.tag.len = sizeof(tag);
3515 spec.iv.data = iv;
3516 spec.iv.len = sizeof(iv);
3517
3518 ret = GenerateSymKey("AES128", &key);
3519 if (ret != 0) {
3520 LOGE("GenerateSymKey failed!");
3521 goto clearup;
3522 }
3523
3524 ret = HcfCipherCreate("AES128|CCM|PKCS7", &cipher);
3525 if (ret != 0) {
3526 LOGE("HcfCipherCreate failed!");
3527 goto clearup;
3528 }
3529
3530 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen);
3531 if (ret != 0) {
3532 LOGE("AesNoUpdateEncrypt failed!");
3533 goto clearup;
3534 }
3535
3536 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12);
3537 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
3538 cipherTextLen -= 12;
3539
3540 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen);
3541 if (ret != 0) {
3542 LOGE("AesNoUpdateDecrypt failed!");
3543 goto clearup;
3544 }
3545
3546 clearup:
3547 HcfObjDestroy((HcfObjectBase *)key);
3548 HcfObjDestroy((HcfObjectBase *)cipher);
3549 EXPECT_EQ(ret, 0);
3550 }
3551
3552 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest062, TestSize.Level0)
3553 {
3554 int ret = 0;
3555 uint8_t cipherText[128] = {0};
3556 int cipherTextLen = 128;
3557
3558 HcfCipher *cipher = NULL;
3559 HcfSymKey *key = NULL;
3560 uint8_t codeCipherText[] = {
3561 0xF5, 0x12, 0xA0, 0x33, 0xCD, 0xCF, 0x0D, 0x32,
3562 0x3E, 0xFF, 0x80, 0x53, 0x89, 0xB6, 0xE4, 0xFE
3563 };
3564
3565 ret = ConvertSymKey("AES128", &key);
3566 if (ret != 0) {
3567 LOGE("ConvertSymKey failed!");
3568 goto clearup;
3569 }
3570
3571 ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher);
3572 if (ret != 0) {
3573 LOGE("HcfCipherCreate failed!");
3574 goto clearup;
3575 }
3576
3577 ret = AesNoUpdateEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
3578 if (ret != 0) {
3579 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3580 goto clearup;
3581 }
3582
3583 ret = memcmp(cipherText, codeCipherText, cipherTextLen);
3584 if (ret != 0) {
3585 LOGE("cipherText compare failed!");
3586 goto clearup;
3587 }
3588
3589 ret = AesNoUpdateDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
3590 if (ret != 0) {
3591 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3592 goto clearup;
3593 }
3594
3595 clearup:
3596 HcfObjDestroy((HcfObjectBase *)key);
3597 HcfObjDestroy((HcfObjectBase *)cipher);
3598 EXPECT_EQ(ret, 0);
3599 }
3600
3601 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest063, TestSize.Level0)
3602 {
3603 int ret = 0;
3604 HcfCipher *cipher = NULL;
3605 HcfSymKey *key = NULL;
3606
3607 ret = GeneratorFile("/data/test_aes.txt", 10 * FILE_BLOCK_SIZE);
3608 if (ret != 0) {
3609 LOGE("GeneratorFile failed!");
3610 goto clearup;
3611 }
3612 ret = ConvertSymKey("AES128", &key);
3613 if (ret != 0) {
3614 LOGE("ConvertSymKey failed!");
3615 goto clearup;
3616 }
3617
3618 ret = HcfCipherCreate("AES128|ECB|NoPadding", &cipher);
3619 if (ret != 0) {
3620 LOGE("HcfCipherCreate failed!");
3621 goto clearup;
3622 }
3623
3624 ret = AesMultiBlockEncrypt(cipher, key, NULL);
3625 if (ret != 0) {
3626 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3627 goto clearup;
3628 }
3629
3630 ret = AesMultiBlockDecrypt(cipher, key, NULL);
3631 if (ret != 0) {
3632 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3633 goto clearup;
3634 }
3635 ret = CompareFileContent();
3636 if (ret != 0) {
3637 LOGE("CompareFileContent failed!");
3638 goto clearup;
3639 }
3640
3641 clearup:
3642 HcfObjDestroy((HcfObjectBase *)key);
3643 HcfObjDestroy((HcfObjectBase *)cipher);
3644 EXPECT_EQ(ret, 0);
3645 }
3646
3647 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest064, TestSize.Level0)
3648 {
3649 int ret = 0;
3650 HcfIvParamsSpec ivSpec = {};
3651 uint8_t iv[16] = {0};
3652 ivSpec.iv.data = iv;
3653 ivSpec.iv.len = 16;
3654
3655 HcfCipher *cipher = NULL;
3656 HcfSymKey *key = NULL;
3657
3658 ret = GeneratorFile("/data/test_aes.txt", 10 * FILE_BLOCK_SIZE);
3659 if (ret != 0) {
3660 LOGE("GeneratorFile failed!");
3661 goto clearup;
3662 }
3663
3664 ret = ConvertSymKey("AES128", &key);
3665 if (ret != 0) {
3666 LOGE("ConvertSymKey failed!");
3667 goto clearup;
3668 }
3669
3670 ret = HcfCipherCreate("AES128|CBC|NoPadding", &cipher);
3671 if (ret != 0) {
3672 LOGE("HcfCipherCreate failed!");
3673 goto clearup;
3674 }
3675
3676 ret = AesMultiBlockEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3677 if (ret != 0) {
3678 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3679 goto clearup;
3680 }
3681
3682 ret = AesMultiBlockDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3683 if (ret != 0) {
3684 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3685 goto clearup;
3686 }
3687 ret = CompareFileContent();
3688 if (ret != 0) {
3689 LOGE("CompareFileContent failed!");
3690 goto clearup;
3691 }
3692
3693 clearup:
3694 HcfObjDestroy((HcfObjectBase *)key);
3695 HcfObjDestroy((HcfObjectBase *)cipher);
3696 EXPECT_EQ(ret, 0);
3697 }
3698
3699 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest065, TestSize.Level0)
3700 {
3701 int ret = 0;
3702 uint8_t iv[16] = {0};
3703
3704 HcfIvParamsSpec ivSpec = {};
3705 HcfCipher *cipher = NULL;
3706 HcfSymKey *key = NULL;
3707 ivSpec.iv.data = iv;
3708 ivSpec.iv.len = 16;
3709
3710 ret = ConvertSymKey("AES128", &key);
3711 if (ret != 0) {
3712 LOGE("ConvertSymKey failed!");
3713 goto clearup;
3714 }
3715
3716 ret = HcfCipherCreate("AES128|CTR|NoPadding", &cipher);
3717 if (ret != 0) {
3718 LOGE("HcfCipherCreate failed!");
3719 goto clearup;
3720 }
3721 ret = GeneratorFile("/data/test_aes.txt", 10 * FILE_BLOCK_SIZE);
3722 if (ret != 0) {
3723 LOGE("GeneratorFile failed!");
3724 goto clearup;
3725 }
3726
3727 ret = AesMultiBlockEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3728 if (ret != 0) {
3729 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3730 goto clearup;
3731 }
3732
3733 ret = AesMultiBlockDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3734 if (ret != 0) {
3735 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3736 goto clearup;
3737 }
3738 ret = CompareFileContent();
3739 if (ret != 0) {
3740 LOGE("CompareFileContent failed!");
3741 goto clearup;
3742 }
3743
3744 clearup:
3745 HcfObjDestroy((HcfObjectBase *)key);
3746 HcfObjDestroy((HcfObjectBase *)cipher);
3747 EXPECT_EQ(ret, 0);
3748 }
3749
3750 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest066, TestSize.Level0)
3751 {
3752 int ret = 0;
3753 uint8_t iv[16] = {0};
3754
3755 HcfIvParamsSpec ivSpec = {};
3756 HcfCipher *cipher = NULL;
3757 HcfSymKey *key = NULL;
3758 ivSpec.iv.data = iv;
3759 ivSpec.iv.len = 16;
3760
3761 ret = GenerateSymKey("AES128", &key);
3762 if (ret != 0) {
3763 LOGE("GenerateSymKey failed!");
3764 goto clearup;
3765 }
3766
3767 ret = HcfCipherCreate("AES128|OFB|NoPadding", &cipher);
3768 if (ret != 0) {
3769 LOGE("HcfCipherCreate failed!");
3770 goto clearup;
3771 }
3772 ret = GeneratorFile("/data/test_aes.txt", 10 * FILE_BLOCK_SIZE);
3773 if (ret != 0) {
3774 LOGE("GeneratorFile failed!");
3775 goto clearup;
3776 }
3777
3778 ret = AesMultiBlockEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3779 if (ret != 0) {
3780 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3781 goto clearup;
3782 }
3783
3784 ret = AesMultiBlockDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3785 if (ret != 0) {
3786 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3787 goto clearup;
3788 }
3789 ret = CompareFileContent();
3790 if (ret != 0) {
3791 LOGE("CompareFileContent failed!");
3792 goto clearup;
3793 }
3794
3795 clearup:
3796 HcfObjDestroy((HcfObjectBase *)key);
3797 HcfObjDestroy((HcfObjectBase *)cipher);
3798 EXPECT_EQ(ret, 0);
3799 }
3800
3801 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest067, TestSize.Level0)
3802 {
3803 int ret = 0;
3804 uint8_t iv[16] = {0};
3805
3806 HcfIvParamsSpec ivSpec = {};
3807 HcfCipher *cipher = NULL;
3808 HcfSymKey *key = NULL;
3809 ivSpec.iv.data = iv;
3810 ivSpec.iv.len = 16;
3811
3812 ret = GenerateSymKey("AES128", &key);
3813 if (ret != 0) {
3814 LOGE("GenerateSymKey failed!");
3815 goto clearup;
3816 }
3817
3818 ret = HcfCipherCreate("AES128|CFB|NoPadding", &cipher);
3819 if (ret != 0) {
3820 LOGE("HcfCipherCreate failed!");
3821 goto clearup;
3822 }
3823 ret = GeneratorFile("/data/test_aes.txt", 10 * FILE_BLOCK_SIZE);
3824 if (ret != 0) {
3825 LOGE("GeneratorFile failed!");
3826 goto clearup;
3827 }
3828
3829 ret = AesMultiBlockEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3830 if (ret != 0) {
3831 LOGE("AesNoUpdateEncrypt failed! %d", ret);
3832 goto clearup;
3833 }
3834
3835 ret = AesMultiBlockDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec);
3836 if (ret != 0) {
3837 LOGE("AesNoUpdateDecrypt failed! %d", ret);
3838 goto clearup;
3839 }
3840 ret = CompareFileContent();
3841 if (ret != 0) {
3842 LOGE("CompareFileContent failed!");
3843 goto clearup;
3844 }
3845
3846 clearup:
3847 HcfObjDestroy((HcfObjectBase *)key);
3848 HcfObjDestroy((HcfObjectBase *)cipher);
3849 EXPECT_EQ(ret, 0);
3850 }
3851
3852 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest068, TestSize.Level0)
3853 {
3854 int ret = 0;
3855 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
3856 int cipherTextLen = CIPHER_TEXT_LEN;
3857 HcfCipher *cipher = nullptr;
3858 HcfSymKey *key = nullptr;
3859
3860 ret = GenerateSymKey("AES192", &key);
3861 if (ret != 0) {
3862 LOGE("GenerateSymKey failed!");
3863 goto clearup;
3864 }
3865
3866 ret = HcfCipherCreate("AES192|ECB|NoPadding", &cipher);
3867 if (ret != 0) {
3868 LOGE("HcfCipherCreate failed!");
3869 goto clearup;
3870 }
3871
3872 ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
3873 if (ret != 0) {
3874 LOGE("AesEncrypt failed! %d", ret);
3875 goto clearup;
3876 }
3877
3878 ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
3879 if (ret != 0) {
3880 LOGE("AesDecrypt failed! %d", ret);
3881 }
3882 clearup:
3883 HcfObjDestroy(key);
3884 HcfObjDestroy(cipher);
3885 EXPECT_NE(ret, 0);
3886 }
3887
3888 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest069, TestSize.Level0)
3889 {
3890 int ret = 0;
3891 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
3892 int cipherTextLen = CIPHER_TEXT_LEN;
3893 HcfCipher *cipher = nullptr;
3894 HcfSymKey *key = nullptr;
3895
3896 ret = GenerateSymKey("AES192", &key);
3897 if (ret != 0) {
3898 LOGE("GenerateSymKey failed!");
3899 goto clearup;
3900 }
3901
3902 ret = HcfCipherCreate("AES192|ECB|PKCS5", &cipher);
3903 if (ret != 0) {
3904 LOGE("HcfCipherCreate failed!");
3905 goto clearup;
3906 }
3907
3908 ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
3909 if (ret != 0) {
3910 LOGE("AesEncrypt failed! %d", ret);
3911 goto clearup;
3912 }
3913
3914 ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
3915 if (ret != 0) {
3916 LOGE("AesDecrypt failed! %d", ret);
3917 }
3918 key->clearMem(key);
3919 clearup:
3920 HcfObjDestroy(key);
3921 HcfObjDestroy(cipher);
3922 EXPECT_EQ(ret, 0);
3923 }
3924
3925 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest070, TestSize.Level0)
3926 {
3927 int ret = 0;
3928 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
3929 int cipherTextLen = CIPHER_TEXT_LEN;
3930 HcfCipher *cipher = nullptr;
3931 HcfSymKey *key = nullptr;
3932
3933 ret = GenerateSymKey("AES256", &key);
3934 if (ret != 0) {
3935 LOGE("GenerateSymKey failed!");
3936 goto clearup;
3937 }
3938
3939 ret = HcfCipherCreate("AES256|ECB|PKCS7", &cipher);
3940 if (ret != 0) {
3941 LOGE("HcfCipherCreate failed!");
3942 goto clearup;
3943 }
3944
3945 ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen);
3946 if (ret != 0) {
3947 LOGE("AesEncrypt failed! %d", ret);
3948 goto clearup;
3949 }
3950
3951 ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen);
3952 if (ret != 0) {
3953 LOGE("AesDecrypt failed! %d", ret);
3954 }
3955 key->clearMem(key);
3956 clearup:
3957 HcfObjDestroy(key);
3958 HcfObjDestroy(cipher);
3959 EXPECT_EQ(ret, 0);
3960 }
3961
3962 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest071, TestSize.Level0)
3963 {
3964 int ret = 0;
3965 uint8_t iv[AES_IV_LEN] = { 0 };
3966 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
3967 int cipherTextLen = CIPHER_TEXT_LEN;
3968
3969 HcfIvParamsSpec ivSpec = {};
3970 HcfCipher *cipher = nullptr;
3971 HcfSymKey *key = nullptr;
3972 ivSpec.iv.data = iv;
3973 ivSpec.iv.len = AES_IV_LEN;
3974
3975 ret = GenerateSymKey("AES192", &key);
3976 if (ret != 0) {
3977 LOGE("GenerateSymKey failed!");
3978 goto clearup;
3979 }
3980
3981 ret = HcfCipherCreate("AES192|CBC|PKCS5", &cipher);
3982 if (ret != 0) {
3983 LOGE("HcfCipherCreate failed!");
3984 goto clearup;
3985 }
3986
3987 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
3988 if (ret != 0) {
3989 LOGE("AesEncrypt failed! %d", ret);
3990 goto clearup;
3991 }
3992
3993 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
3994 if (ret != 0) {
3995 LOGE("AesDecrypt failed! %d", ret);
3996 }
3997
3998 clearup:
3999 HcfObjDestroy(key);
4000 HcfObjDestroy(cipher);
4001 EXPECT_EQ(ret, 0);
4002 }
4003
4004 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest072, TestSize.Level0)
4005 {
4006 int ret = 0;
4007 uint8_t iv[AES_IV_LEN] = { 0 };
4008 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4009 int cipherTextLen = CIPHER_TEXT_LEN;
4010
4011 HcfIvParamsSpec ivSpec = {};
4012 HcfCipher *cipher = nullptr;
4013 HcfSymKey *key = nullptr;
4014 ivSpec.iv.data = iv;
4015 ivSpec.iv.len = AES_IV_LEN;
4016
4017 ret = GenerateSymKey("AES256", &key);
4018 if (ret != 0) {
4019 LOGE("GenerateSymKey failed!");
4020 goto clearup;
4021 }
4022
4023 ret = HcfCipherCreate("AES256|CBC|PKCS5", &cipher);
4024 if (ret != 0) {
4025 LOGE("HcfCipherCreate failed!");
4026 goto clearup;
4027 }
4028
4029 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4030 if (ret != 0) {
4031 LOGE("AesEncrypt failed! %d", ret);
4032 goto clearup;
4033 }
4034
4035 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4036 if (ret != 0) {
4037 LOGE("AesDecrypt failed! %d", ret);
4038 }
4039
4040 clearup:
4041 HcfObjDestroy(key);
4042 HcfObjDestroy(cipher);
4043 EXPECT_EQ(ret, 0);
4044 }
4045
4046 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest073, TestSize.Level0)
4047 {
4048 int ret = 0;
4049 uint8_t iv[AES_IV_LEN] = { 0 };
4050 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4051 int cipherTextLen = CIPHER_TEXT_LEN;
4052
4053 HcfIvParamsSpec ivSpec = {};
4054 HcfCipher *cipher = nullptr;
4055 HcfSymKey *key = nullptr;
4056 ivSpec.iv.data = iv;
4057 ivSpec.iv.len = AES_IV_LEN;
4058
4059 ret = GenerateSymKey("AES192", &key);
4060 if (ret != 0) {
4061 LOGE("GenerateSymKey failed!");
4062 goto clearup;
4063 }
4064
4065 ret = HcfCipherCreate("AES192|CTR|PKCS5", &cipher);
4066 if (ret != 0) {
4067 LOGE("HcfCipherCreate failed!");
4068 goto clearup;
4069 }
4070
4071 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4072 if (ret != 0) {
4073 LOGE("AesEncrypt failed! %d", ret);
4074 goto clearup;
4075 }
4076
4077 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4078 if (ret != 0) {
4079 LOGE("AesDecrypt failed! %d", ret);
4080 }
4081
4082 clearup:
4083 HcfObjDestroy(key);
4084 HcfObjDestroy(cipher);
4085 EXPECT_EQ(ret, 0);
4086 }
4087
4088 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest074, TestSize.Level0)
4089 {
4090 int ret = 0;
4091 uint8_t iv[AES_IV_LEN] = { 0 };
4092 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4093 int cipherTextLen = CIPHER_TEXT_LEN;
4094
4095 HcfIvParamsSpec ivSpec = {};
4096 HcfCipher *cipher = nullptr;
4097 HcfSymKey *key = nullptr;
4098 ivSpec.iv.data = iv;
4099 ivSpec.iv.len = AES_IV_LEN;
4100
4101 ret = GenerateSymKey("AES256", &key);
4102 if (ret != 0) {
4103 LOGE("GenerateSymKey failed!");
4104 goto clearup;
4105 }
4106
4107 ret = HcfCipherCreate("AES256|CTR|PKCS5", &cipher);
4108 if (ret != 0) {
4109 LOGE("HcfCipherCreate failed!");
4110 goto clearup;
4111 }
4112
4113 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4114 if (ret != 0) {
4115 LOGE("AesEncrypt failed! %d", ret);
4116 goto clearup;
4117 }
4118
4119 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4120 if (ret != 0) {
4121 LOGE("AesDecrypt failed! %d", ret);
4122 }
4123
4124 clearup:
4125 HcfObjDestroy(key);
4126 HcfObjDestroy(cipher);
4127 EXPECT_EQ(ret, 0);
4128 }
4129
4130 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest075, TestSize.Level0)
4131 {
4132 int ret = 0;
4133 uint8_t iv[AES_IV_LEN] = { 0 };
4134 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4135 int cipherTextLen = CIPHER_TEXT_LEN;
4136
4137 HcfIvParamsSpec ivSpec = {};
4138 HcfCipher *cipher = nullptr;
4139 HcfSymKey *key = nullptr;
4140 ivSpec.iv.data = iv;
4141 ivSpec.iv.len = AES_IV_LEN;
4142
4143 ret = GenerateSymKey("AES192", &key);
4144 if (ret != 0) {
4145 LOGE("GenerateSymKey failed!");
4146 goto clearup;
4147 }
4148
4149 ret = HcfCipherCreate("AES192|OFB|PKCS5", &cipher);
4150 if (ret != 0) {
4151 LOGE("HcfCipherCreate failed!");
4152 goto clearup;
4153 }
4154
4155 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4156 if (ret != 0) {
4157 LOGE("AesEncrypt failed! %d", ret);
4158 goto clearup;
4159 }
4160
4161 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4162 if (ret != 0) {
4163 LOGE("AesDecrypt failed! %d", ret);
4164 }
4165
4166 clearup:
4167 HcfObjDestroy(key);
4168 HcfObjDestroy(cipher);
4169 EXPECT_EQ(ret, 0);
4170 }
4171
4172 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest076, TestSize.Level0)
4173 {
4174 int ret = 0;
4175 uint8_t iv[AES_IV_LEN] = { 0 };
4176 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4177 int cipherTextLen = CIPHER_TEXT_LEN;
4178
4179 HcfIvParamsSpec ivSpec = {};
4180 HcfCipher *cipher = nullptr;
4181 HcfSymKey *key = nullptr;
4182 ivSpec.iv.data = iv;
4183 ivSpec.iv.len = AES_IV_LEN;
4184
4185 ret = GenerateSymKey("AES256", &key);
4186 if (ret != 0) {
4187 LOGE("GenerateSymKey failed!");
4188 goto clearup;
4189 }
4190
4191 ret = HcfCipherCreate("AES256|OFB|PKCS5", &cipher);
4192 if (ret != 0) {
4193 LOGE("HcfCipherCreate failed!");
4194 goto clearup;
4195 }
4196
4197 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4198 if (ret != 0) {
4199 LOGE("AesEncrypt failed! %d", ret);
4200 goto clearup;
4201 }
4202
4203 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4204 if (ret != 0) {
4205 LOGE("AesDecrypt failed! %d", ret);
4206 }
4207
4208 clearup:
4209 HcfObjDestroy(key);
4210 HcfObjDestroy(cipher);
4211 EXPECT_EQ(ret, 0);
4212 }
4213
4214 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest077, TestSize.Level0)
4215 {
4216 int ret = 0;
4217 uint8_t iv[AES_IV_LEN] = { 0 };
4218 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4219 int cipherTextLen = CIPHER_TEXT_LEN;
4220
4221 HcfIvParamsSpec ivSpec = {};
4222 HcfCipher *cipher = nullptr;
4223 HcfSymKey *key = nullptr;
4224 ivSpec.iv.data = iv;
4225 ivSpec.iv.len = AES_IV_LEN;
4226
4227 ret = GenerateSymKey("AES192", &key);
4228 if (ret != 0) {
4229 LOGE("GenerateSymKey failed!");
4230 goto clearup;
4231 }
4232
4233 ret = HcfCipherCreate("AES192|CFB|PKCS5", &cipher);
4234 if (ret != 0) {
4235 LOGE("HcfCipherCreate failed!");
4236 goto clearup;
4237 }
4238
4239 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4240 if (ret != 0) {
4241 LOGE("AesEncrypt failed! %d", ret);
4242 goto clearup;
4243 }
4244
4245 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4246 if (ret != 0) {
4247 LOGE("AesDecrypt failed! %d", ret);
4248 }
4249
4250 clearup:
4251 HcfObjDestroy(key);
4252 HcfObjDestroy(cipher);
4253 EXPECT_EQ(ret, 0);
4254 }
4255
4256 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest078, TestSize.Level0)
4257 {
4258 int ret = 0;
4259 uint8_t iv[AES_IV_LEN] = { 0 };
4260 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4261 int cipherTextLen = CIPHER_TEXT_LEN;
4262
4263 HcfIvParamsSpec ivSpec = {};
4264 HcfCipher *cipher = nullptr;
4265 HcfSymKey *key = nullptr;
4266 ivSpec.iv.data = iv;
4267 ivSpec.iv.len = AES_IV_LEN;
4268
4269 ret = GenerateSymKey("AES256", &key);
4270 if (ret != 0) {
4271 LOGE("GenerateSymKey failed!");
4272 goto clearup;
4273 }
4274
4275 ret = HcfCipherCreate("AES256|CFB|PKCS5", &cipher);
4276 if (ret != 0) {
4277 LOGE("HcfCipherCreate failed!");
4278 goto clearup;
4279 }
4280
4281 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4282 if (ret != 0) {
4283 LOGE("AesEncrypt failed! %d", ret);
4284 goto clearup;
4285 }
4286
4287 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4288 if (ret != 0) {
4289 LOGE("AesDecrypt failed! %d", ret);
4290 }
4291
4292 clearup:
4293 HcfObjDestroy(key);
4294 HcfObjDestroy(cipher);
4295 EXPECT_EQ(ret, 0);
4296 }
4297
4298 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest079, TestSize.Level0)
4299 {
4300 int ret = 0;
4301 uint8_t iv[AES_IV_LEN] = { 0 };
4302 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4303 int cipherTextLen = CIPHER_TEXT_LEN;
4304
4305 HcfIvParamsSpec ivSpec = {};
4306 HcfCipher *cipher = nullptr;
4307 HcfSymKey *key = nullptr;
4308 ivSpec.iv.data = iv;
4309 ivSpec.iv.len = AES_IV_LEN;
4310
4311 ret = GenerateSymKey("AES192", &key);
4312 if (ret != 0) {
4313 LOGE("GenerateSymKey failed!");
4314 goto clearup;
4315 }
4316
4317 ret = HcfCipherCreate("AES192|CFB1|PKCS5", &cipher);
4318 if (ret != 0) {
4319 LOGE("HcfCipherCreate failed!");
4320 goto clearup;
4321 }
4322
4323 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4324 if (ret != 0) {
4325 LOGE("AesEncrypt failed! %d", ret);
4326 goto clearup;
4327 }
4328
4329 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4330 if (ret != 0) {
4331 LOGE("AesDecrypt failed! %d", ret);
4332 }
4333
4334 clearup:
4335 HcfObjDestroy(key);
4336 HcfObjDestroy(cipher);
4337 EXPECT_EQ(ret, 0);
4338 }
4339
4340
4341 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest080, TestSize.Level0)
4342 {
4343 int ret = 0;
4344 uint8_t iv[AES_IV_LEN] = { 0 };
4345 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4346 int cipherTextLen = CIPHER_TEXT_LEN;
4347
4348 HcfIvParamsSpec ivSpec = {};
4349 HcfCipher *cipher = nullptr;
4350 HcfSymKey *key = nullptr;
4351 ivSpec.iv.data = iv;
4352 ivSpec.iv.len = AES_IV_LEN;
4353
4354 ret = GenerateSymKey("AES256", &key);
4355 if (ret != 0) {
4356 LOGE("GenerateSymKey failed!");
4357 goto clearup;
4358 }
4359
4360 ret = HcfCipherCreate("AES256|CFB1|PKCS5", &cipher);
4361 if (ret != 0) {
4362 LOGE("HcfCipherCreate failed!");
4363 goto clearup;
4364 }
4365
4366 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4367 if (ret != 0) {
4368 LOGE("AesEncrypt failed! %d", ret);
4369 goto clearup;
4370 }
4371
4372 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4373 if (ret != 0) {
4374 LOGE("AesDecrypt failed! %d", ret);
4375 }
4376
4377 clearup:
4378 HcfObjDestroy(key);
4379 HcfObjDestroy(cipher);
4380 EXPECT_EQ(ret, 0);
4381 }
4382
4383 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest081, TestSize.Level0)
4384 {
4385 int ret = 0;
4386 uint8_t iv[AES_IV_LEN] = { 0 };
4387 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4388 int cipherTextLen = CIPHER_TEXT_LEN;
4389
4390 HcfIvParamsSpec ivSpec = {};
4391 HcfCipher *cipher = nullptr;
4392 HcfSymKey *key = nullptr;
4393 ivSpec.iv.data = iv;
4394 ivSpec.iv.len = AES_IV_LEN;
4395
4396 ret = GenerateSymKey("AES192", &key);
4397 if (ret != 0) {
4398 LOGE("GenerateSymKey failed!");
4399 goto clearup;
4400 }
4401
4402 ret = HcfCipherCreate("AES192|CFB8|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
4403 if (ret != 0) {
4404 LOGE("HcfCipherCreate failed!");
4405 goto clearup;
4406 }
4407
4408 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4409 if (ret != 0) {
4410 LOGE("AesEncrypt failed! %d", ret);
4411 goto clearup;
4412 }
4413
4414 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4415 if (ret != 0) {
4416 LOGE("AesDecrypt failed! %d", ret);
4417 }
4418
4419 clearup:
4420 HcfObjDestroy(key);
4421 HcfObjDestroy(cipher);
4422 EXPECT_EQ(ret, 0);
4423 }
4424
4425 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest082, TestSize.Level0)
4426 {
4427 int ret = 0;
4428 uint8_t iv[AES_IV_LEN] = { 0 };
4429 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4430 int cipherTextLen = CIPHER_TEXT_LEN;
4431
4432 HcfIvParamsSpec ivSpec = {};
4433 HcfCipher *cipher = nullptr;
4434 HcfSymKey *key = nullptr;
4435 ivSpec.iv.data = iv;
4436 ivSpec.iv.len = AES_IV_LEN;
4437
4438 ret = GenerateSymKey("AES256", &key);
4439 if (ret != 0) {
4440 LOGE("GenerateSymKey failed!");
4441 goto clearup;
4442 }
4443
4444 ret = HcfCipherCreate("AES256|CFB8|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
4445 if (ret != 0) {
4446 LOGE("HcfCipherCreate failed!");
4447 goto clearup;
4448 }
4449
4450 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4451 if (ret != 0) {
4452 LOGE("AesEncrypt failed! %d", ret);
4453 goto clearup;
4454 }
4455
4456 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4457 if (ret != 0) {
4458 LOGE("AesDecrypt failed! %d", ret);
4459 }
4460
4461 clearup:
4462 HcfObjDestroy(key);
4463 HcfObjDestroy(cipher);
4464 EXPECT_EQ(ret, 0);
4465 }
4466
4467 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest083, TestSize.Level0)
4468 {
4469 int ret = 0;
4470 uint8_t iv[AES_IV_LEN] = { 0 };
4471 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4472 int cipherTextLen = CIPHER_TEXT_LEN;
4473
4474 HcfIvParamsSpec ivSpec = {};
4475 HcfCipher *cipher = nullptr;
4476 HcfSymKey *key = nullptr;
4477 ivSpec.iv.data = iv;
4478 ivSpec.iv.len = AES_IV_LEN;
4479
4480 ret = GenerateSymKey("AES192", &key);
4481 if (ret != 0) {
4482 LOGE("GenerateSymKey failed!");
4483 goto clearup;
4484 }
4485
4486 ret = HcfCipherCreate("AES192|CFB128|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
4487 if (ret != 0) {
4488 LOGE("HcfCipherCreate failed!");
4489 goto clearup;
4490 }
4491
4492 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4493 if (ret != 0) {
4494 LOGE("AesEncrypt failed! %d", ret);
4495 goto clearup;
4496 }
4497
4498 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4499 if (ret != 0) {
4500 LOGE("AesDecrypt failed! %d", ret);
4501 }
4502
4503 clearup:
4504 HcfObjDestroy(key);
4505 HcfObjDestroy(cipher);
4506 EXPECT_EQ(ret, 0);
4507 }
4508
4509 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest084, TestSize.Level0)
4510 {
4511 int ret = 0;
4512 uint8_t iv[AES_IV_LEN] = { 0 };
4513 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4514 int cipherTextLen = CIPHER_TEXT_LEN;
4515
4516 HcfIvParamsSpec ivSpec = {};
4517 HcfCipher *cipher = nullptr;
4518 HcfSymKey *key = nullptr;
4519 ivSpec.iv.data = iv;
4520 ivSpec.iv.len = AES_IV_LEN;
4521
4522 ret = GenerateSymKey("AES256", &key);
4523 if (ret != 0) {
4524 LOGE("GenerateSymKey failed!");
4525 goto clearup;
4526 }
4527
4528 ret = HcfCipherCreate("AES256|CFB128|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit
4529 if (ret != 0) {
4530 LOGE("HcfCipherCreate failed!");
4531 goto clearup;
4532 }
4533
4534 ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
4535 if (ret != 0) {
4536 LOGE("AesEncrypt failed! %d", ret);
4537 goto clearup;
4538 }
4539
4540 ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
4541 if (ret != 0) {
4542 LOGE("AesDecrypt failed! %d", ret);
4543 }
4544
4545 clearup:
4546 HcfObjDestroy(key);
4547 HcfObjDestroy(cipher);
4548 EXPECT_EQ(ret, 0);
4549 }
4550
4551 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest085, TestSize.Level0)
4552 {
4553 int ret = 0;
4554 uint8_t aad[GCM_AAD_LEN] = { 0 };
4555 uint8_t tag[GCM_TAG_LEN] = { 0 };
4556 uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes
4557 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4558 int cipherTextLen = CIPHER_TEXT_LEN;
4559
4560 HcfCipher *cipher = nullptr;
4561 HcfSymKey *key = nullptr;
4562
4563 HcfGcmParamsSpec spec = {};
4564 spec.aad.data = aad;
4565 spec.aad.len = sizeof(aad);
4566 spec.tag.data = tag;
4567 spec.tag.len = sizeof(tag);
4568 spec.iv.data = iv;
4569 spec.iv.len = sizeof(iv);
4570
4571 ret = GenerateSymKey("AES192", &key);
4572 if (ret != 0) {
4573 LOGE("generateSymKey failed!");
4574 goto clearup;
4575 }
4576
4577 ret = HcfCipherCreate("AES192|GCM|PKCS5", &cipher);
4578 if (ret != 0) {
4579 LOGE("HcfCipherCreate failed!");
4580 goto clearup;
4581 }
4582
4583 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
4584 if (ret != 0) {
4585 LOGE("AesEncrypt failed, ret:%d!", ret);
4586 goto clearup;
4587 }
4588
4589 (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
4590 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
4591 cipherTextLen -= GCM_TAG_LEN;
4592
4593 ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
4594 if (ret != 0) {
4595 LOGE("AesDecrypt failed, ret:%d!", ret);
4596 }
4597
4598 clearup:
4599 HcfObjDestroy(key);
4600 HcfObjDestroy(cipher);
4601 EXPECT_EQ(ret, 0);
4602 }
4603
4604 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest086, TestSize.Level0)
4605 {
4606 int ret = 0;
4607 uint8_t aad[GCM_AAD_LEN] = { 0 };
4608 uint8_t tag[GCM_TAG_LEN] = { 0 };
4609 uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes
4610 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4611 int cipherTextLen = CIPHER_TEXT_LEN;
4612
4613 HcfCipher *cipher = nullptr;
4614 HcfSymKey *key = nullptr;
4615
4616 HcfGcmParamsSpec spec = {};
4617 spec.aad.data = aad;
4618 spec.aad.len = sizeof(aad);
4619 spec.tag.data = tag;
4620 spec.tag.len = sizeof(tag);
4621 spec.iv.data = iv;
4622 spec.iv.len = sizeof(iv);
4623
4624 ret = GenerateSymKey("AES256", &key);
4625 if (ret != 0) {
4626 LOGE("generateSymKey failed!");
4627 goto clearup;
4628 }
4629
4630 ret = HcfCipherCreate("AES256|GCM|PKCS5", &cipher);
4631 if (ret != 0) {
4632 LOGE("HcfCipherCreate failed!");
4633 goto clearup;
4634 }
4635
4636 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
4637 if (ret != 0) {
4638 LOGE("AesEncrypt failed, ret:%d!", ret);
4639 goto clearup;
4640 }
4641
4642 (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN);
4643 PrintfHex("gcm tag", spec.tag.data, spec.tag.len);
4644 cipherTextLen -= GCM_TAG_LEN;
4645
4646 ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
4647 if (ret != 0) {
4648 LOGE("AesDecrypt failed, ret:%d!", ret);
4649 }
4650
4651 clearup:
4652 HcfObjDestroy(key);
4653 HcfObjDestroy(cipher);
4654 EXPECT_EQ(ret, 0);
4655 }
4656
4657 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest087, TestSize.Level0)
4658 {
4659 int ret = 0;
4660 uint8_t aad[CCM_AAD_LEN] = { 0 };
4661 uint8_t tag[CCM_TAG_LEN] = { 0 };
4662 uint8_t iv[CCM_IV_LEN] = { 0 };
4663 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4664 int cipherTextLen = CIPHER_TEXT_LEN;
4665
4666 HcfCipher *cipher = nullptr;
4667 HcfSymKey *key = nullptr;
4668 HcfCcmParamsSpec spec = {};
4669 spec.aad.data = aad;
4670 spec.aad.len = sizeof(aad);
4671 spec.tag.data = tag;
4672 spec.tag.len = sizeof(tag);
4673 spec.iv.data = iv;
4674 spec.iv.len = sizeof(iv);
4675
4676 ret = GenerateSymKey("AES192", &key);
4677 if (ret != 0) {
4678 LOGE("generateSymKey failed!");
4679 goto clearup;
4680 }
4681
4682 ret = HcfCipherCreate("AES192|CCM|PKCS5", &cipher);
4683 if (ret != 0) {
4684 LOGE("HcfCipherCreate failed!");
4685 goto clearup;
4686 }
4687
4688 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
4689 if (ret != 0) {
4690 LOGE("AesEncrypt failed!");
4691 goto clearup;
4692 }
4693
4694 (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN);
4695 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
4696 cipherTextLen -= CCM_TAG_LEN;
4697
4698 ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
4699 if (ret != 0) {
4700 LOGE("AesDecrypt failed!");
4701 }
4702
4703 clearup:
4704 HcfObjDestroy(key);
4705 HcfObjDestroy(cipher);
4706 EXPECT_EQ(ret, 0);
4707 }
4708
4709 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest088, TestSize.Level0)
4710 {
4711 int ret = 0;
4712 uint8_t aad[CCM_AAD_LEN] = { 0 };
4713 uint8_t tag[CCM_TAG_LEN] = { 0 };
4714 uint8_t iv[CCM_IV_LEN] = { 0 };
4715 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
4716 int cipherTextLen = CIPHER_TEXT_LEN;
4717
4718 HcfCipher *cipher = nullptr;
4719 HcfSymKey *key = nullptr;
4720 HcfCcmParamsSpec spec = {};
4721 spec.aad.data = aad;
4722 spec.aad.len = sizeof(aad);
4723 spec.tag.data = tag;
4724 spec.tag.len = sizeof(tag);
4725 spec.iv.data = iv;
4726 spec.iv.len = sizeof(iv);
4727
4728 ret = GenerateSymKey("AES256", &key);
4729 if (ret != 0) {
4730 LOGE("generateSymKey failed!");
4731 goto clearup;
4732 }
4733
4734 ret = HcfCipherCreate("AES256|CCM|PKCS5", &cipher);
4735 if (ret != 0) {
4736 LOGE("HcfCipherCreate failed!");
4737 goto clearup;
4738 }
4739
4740 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen);
4741 if (ret != 0) {
4742 LOGE("AesEncrypt failed!");
4743 goto clearup;
4744 }
4745
4746 (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN);
4747 PrintfHex("ccm tag", spec.tag.data, spec.tag.len);
4748 cipherTextLen -= CCM_TAG_LEN;
4749
4750 ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen);
4751 if (ret != 0) {
4752 LOGE("AesDecrypt failed!");
4753 }
4754
4755 clearup:
4756 HcfObjDestroy(key);
4757 HcfObjDestroy(cipher);
4758 EXPECT_EQ(ret, 0);
4759 }
4760
4761 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest089, TestSize.Level0)
4762 {
4763 int ret = 0;
4764 HcfCipher *cipher = nullptr;
4765 const char *cipherName = "AES128|CFB|NoPadding";
4766 const char *retAlgo = nullptr;
4767 ret = HcfCipherCreate(cipherName, &cipher);
4768 if (ret != 0) {
4769 LOGE("HcfCipherCreate failed!");
4770 goto clearup;
4771 }
4772
4773 retAlgo = cipher->getAlgorithm(cipher);
4774 if (retAlgo == nullptr) {
4775 LOGE("cipher getAlgorithm failed!");
4776 ret = HCF_ERR_CRYPTO_OPERATION;
4777 goto clearup;
4778 }
4779
4780 ret = strcmp(retAlgo, cipherName);
4781 if (ret != 0) {
4782 LOGE("cipher getAlgorithm failed!");
4783 }
4784 clearup:
4785 HcfObjDestroy(cipher);
4786 EXPECT_EQ(ret, 0);
4787 }
4788
4789 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest090, TestSize.Level0)
4790 {
4791 int ret = 0;
4792 HcfCipher *cipher = nullptr;
4793 const char *cipherName = "AES128|CFB|NoPadding";
4794 const char *retAlgo = nullptr;
4795 ret = HcfCipherCreate(cipherName, &cipher);
4796 if (ret != 0) {
4797 LOGE("HcfCipherCreate failed!");
4798 goto clearup;
4799 }
4800
4801 retAlgo = cipher->getAlgorithm(nullptr);
4802 if (retAlgo == nullptr) {
4803 LOGE("cipher getAlgorithm failed!");
4804 ret = HCF_ERR_CRYPTO_OPERATION;
4805 }
4806
4807 clearup:
4808 HcfObjDestroy(cipher);
4809 EXPECT_NE(ret, 0);
4810 }
4811
4812 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest091, TestSize.Level0)
4813 {
4814 int ret = 0;
4815 HcfCipher *cipher = nullptr;
4816 HcfSymKeyGenerator *generator = nullptr;
4817 const char *cipherName = "AES128|CFB|NoPadding";
4818 const char *retAlgo = nullptr;
4819
4820 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
4821 if (ret != 0) {
4822 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
4823 goto clearup;
4824 }
4825
4826 ret = HcfCipherCreate(cipherName, &cipher);
4827 if (ret != 0) {
4828 LOGE("HcfCipherCreate failed!");
4829 goto clearup;
4830 }
4831
4832 retAlgo = cipher->getAlgorithm(reinterpret_cast<HcfCipher *>(generator));
4833 if (retAlgo == nullptr) {
4834 LOGE("cipher getAlgorithm failed!");
4835 ret = HCF_ERR_CRYPTO_OPERATION;
4836 }
4837
4838 clearup:
4839 HcfObjDestroy(generator);
4840 HcfObjDestroy(cipher);
4841 EXPECT_NE(ret, 0);
4842 }
4843
4844 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest092, TestSize.Level0)
4845 {
4846 int ret = 0;
4847 HcfSymKeyGenerator *generator = nullptr;
4848 HcfSymKey *key = nullptr;
4849 const char *inputAlgoName = "AES128";
4850 const char *generatorAlgoName = nullptr;
4851
4852 ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator);
4853 if (ret != 0) {
4854 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
4855 goto clearup;
4856 }
4857 ret = generator->generateSymKey(generator, &key);
4858 if (ret != 0) {
4859 LOGE("generateSymKey failed!");
4860 goto clearup;
4861 }
4862
4863 // generator getAlgoName
4864 generatorAlgoName = generator->getAlgoName(generator);
4865 if (generatorAlgoName == nullptr) {
4866 LOGE("generator getAlgoName returns nullptr.");
4867 ret = HCF_ERR_CRYPTO_OPERATION;
4868 goto clearup;
4869 }
4870
4871 ret = strcmp(generatorAlgoName, inputAlgoName);
4872 if (ret != 0) {
4873 LOGE("generator getAlgoName failed!");
4874 }
4875 clearup:
4876 HcfObjDestroy(key);
4877 HcfObjDestroy(generator);
4878 EXPECT_EQ(ret, 0);
4879 }
4880
4881 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest093, TestSize.Level0)
4882 {
4883 int ret = 0;
4884 HcfSymKeyGenerator *generator = nullptr;
4885 const char *generatorAlgoName = nullptr;
4886 const char *inputAlgoName = "AES128";
4887
4888 ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator);
4889 if (ret != 0) {
4890 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
4891 goto clearup;
4892 }
4893
4894 // generator getAlgoName
4895 generatorAlgoName = generator->getAlgoName(nullptr);
4896 if (generatorAlgoName == nullptr) {
4897 LOGE("generator getAlgoName failed!");
4898 ret = HCF_ERR_CRYPTO_OPERATION;
4899 }
4900
4901 clearup:
4902 HcfObjDestroy(generator);
4903 EXPECT_NE(ret, 0);
4904 }
4905
4906 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest094, TestSize.Level0)
4907 {
4908 int ret = 0;
4909 HcfSymKeyGenerator *generator = nullptr;
4910 HcfSymKey *key = nullptr;
4911 const char *generatorAlgoName = nullptr;
4912 const char *inputAlgoName = "AES128";
4913
4914 ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator);
4915 if (ret != 0) {
4916 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
4917 goto clearup;
4918 }
4919 ret = generator->generateSymKey(generator, &key);
4920 if (ret != 0) {
4921 LOGE("generateSymKey failed!");
4922 goto clearup;
4923 }
4924
4925 // generator getAlgoName
4926 generatorAlgoName = generator->getAlgoName(reinterpret_cast<HcfSymKeyGenerator *>(key));
4927 if (generatorAlgoName == nullptr) {
4928 LOGE("generator getAlgoName failed!");
4929 ret = HCF_ERR_CRYPTO_OPERATION;
4930 }
4931
4932 clearup:
4933 HcfObjDestroy(key);
4934 HcfObjDestroy(generator);
4935 EXPECT_NE(ret, 0);
4936 }
4937
4938 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest095, TestSize.Level0)
4939 {
4940 int ret = 0;
4941 HcfSymKey *key = nullptr;
4942 const char *inputAlgoName = "AES128";
4943 const char *keyAlgoName = nullptr;
4944
4945 ret = GenerateSymKey(inputAlgoName, &key);
4946 if (ret != 0) {
4947 LOGE("GenerateSymKey failed!");
4948 goto clearup;
4949 }
4950
4951 // key getAlgorithm
4952 keyAlgoName = key->key.getAlgorithm(&(key->key));
4953 if (keyAlgoName == nullptr) {
4954 LOGE("key getAlgorithm returns nullptr.");
4955 ret = HCF_ERR_CRYPTO_OPERATION;
4956 goto clearup;
4957 }
4958
4959 ret = strcmp(keyAlgoName, inputAlgoName);
4960 if (ret != 0) {
4961 LOGE("key getAlgorithm failed!");
4962 }
4963 clearup:
4964 HcfObjDestroy(key);
4965 EXPECT_EQ(ret, 0);
4966 }
4967
4968 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest096, TestSize.Level0)
4969 {
4970 int ret = 0;
4971 HcfSymKey *key = nullptr;
4972 const char *inputAlgoName = "AES128";
4973 const char *keyAlgoName = nullptr;
4974
4975 ret = GenerateSymKey(inputAlgoName, &key);
4976 if (ret != 0) {
4977 LOGE("GenerateSymKey failed!");
4978 goto clearup;
4979 }
4980
4981 // key getAlgorithm
4982 keyAlgoName = key->key.getAlgorithm(nullptr);
4983 if (keyAlgoName == nullptr) {
4984 LOGE("key getAlgorithm returns nullptr.");
4985 ret = HCF_ERR_CRYPTO_OPERATION;
4986 }
4987
4988 clearup:
4989 HcfObjDestroy(key);
4990 EXPECT_NE(ret, 0);
4991 }
4992
4993 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest097, TestSize.Level0)
4994 {
4995 int ret = 0;
4996 HcfSymKeyGenerator *generator = nullptr;
4997 HcfSymKey *key = nullptr;
4998 const char *inputAlgoName = "AES128";
4999 const char *keyAlgoName = nullptr;
5000
5001 ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator);
5002 if (ret != 0) {
5003 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
5004 goto clearup;
5005 }
5006 ret = generator->generateSymKey(generator, &key);
5007 if (ret != 0) {
5008 LOGE("generateSymKey failed!");
5009 goto clearup;
5010 }
5011
5012 // key getAlgorithm
5013 keyAlgoName = key->key.getAlgorithm(reinterpret_cast<HcfKey *>(generator));
5014 if (keyAlgoName == nullptr) {
5015 LOGE("key getAlgorithm returns nullptr.");
5016 ret = HCF_ERR_CRYPTO_OPERATION;
5017 }
5018
5019 clearup:
5020 HcfObjDestroy(key);
5021 HcfObjDestroy(generator);
5022 EXPECT_NE(ret, 0);
5023 }
5024
5025 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest098, TestSize.Level0)
5026 {
5027 int ret = 0;
5028 HcfSymKey *key = nullptr;
5029 const char *keyFormat = "PKCS#8";
5030 const char *retFormat = nullptr;
5031
5032 ret = GenerateSymKey("AES128", &key);
5033 if (ret != 0) {
5034 LOGE("GenerateSymKey failed!");
5035 goto clearup;
5036 }
5037
5038 // key GetFormat
5039 retFormat = key->key.getFormat(&(key->key));
5040 if (retFormat == nullptr) {
5041 LOGE("key GetFormat returns nullptr.");
5042 ret = HCF_ERR_CRYPTO_OPERATION;
5043 goto clearup;
5044 }
5045
5046 ret = strcmp(retFormat, keyFormat);
5047 if (ret != 0) {
5048 LOGE("key GetFormat failed!");
5049 }
5050
5051 clearup:
5052 HcfObjDestroy(key);
5053 EXPECT_EQ(ret, 0);
5054 }
5055
5056 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest099, TestSize.Level0)
5057 {
5058 int ret = 0;
5059 HcfSymKey *key = nullptr;
5060 const char *retFormat = nullptr;
5061
5062 ret = GenerateSymKey("AES128", &key);
5063 if (ret != 0) {
5064 LOGE("GenerateSymKey failed!");
5065 goto clearup;
5066 }
5067
5068 // key getFormat
5069 retFormat = key->key.getFormat(nullptr);
5070 if (retFormat == nullptr) {
5071 LOGE("key GetFormat returns nullptr.");
5072 ret = HCF_ERR_CRYPTO_OPERATION;
5073 }
5074
5075 clearup:
5076 HcfObjDestroy(key);
5077 EXPECT_NE(ret, 0);
5078 }
5079
5080 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest100, TestSize.Level0)
5081 {
5082 int ret = 0;
5083 HcfSymKeyGenerator *generator = nullptr;
5084 HcfSymKey *key = nullptr;
5085 const char *retFormat = nullptr;
5086
5087 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
5088 if (ret != 0) {
5089 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
5090 goto clearup;
5091 }
5092 ret = generator->generateSymKey(generator, &key);
5093 if (ret != 0) {
5094 LOGE("generateSymKey failed!");
5095 goto clearup;
5096 }
5097
5098 // key getFormat
5099 retFormat = key->key.getFormat(reinterpret_cast<HcfKey *>(generator));
5100 if (retFormat == nullptr) {
5101 LOGE("key GetFormat returns nullptr.");
5102 ret = HCF_ERR_CRYPTO_OPERATION;
5103 }
5104
5105 clearup:
5106 HcfObjDestroy(key);
5107 HcfObjDestroy(generator);
5108 EXPECT_NE(ret, 0);
5109 }
5110
5111 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest101, TestSize.Level0)
5112 {
5113 int ret = 0;
5114 HcfSymKeyGenerator *generator = nullptr;
5115 HcfSymKey *key = nullptr;
5116 HcfBlob encodedBlob = { 0 };
5117 uint8_t keyMaterial[] = {
5118 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
5119 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
5120 };
5121 HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN };
5122
5123 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
5124 if (ret != 0) {
5125 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
5126 goto clearup;
5127 }
5128 ret = generator->convertSymKey(generator, &keyTmpBlob, &key);
5129 if (ret != 0) {
5130 LOGE("generateSymKey failed!");
5131 goto clearup;
5132 }
5133
5134 // key getEncoded
5135 ret = key->key.getEncoded(&(key->key), &encodedBlob);
5136 if (ret != 0) {
5137 LOGE("key GetEncoded failed.");
5138 goto clearup;
5139 }
5140
5141 if (encodedBlob.len != keyTmpBlob.len) {
5142 LOGE("key GetEncoded failed!");
5143 ret = HCF_ERR_CRYPTO_OPERATION;
5144 goto clearup;
5145 }
5146 ret = memcmp(encodedBlob.data, keyTmpBlob.data, keyTmpBlob.len);
5147
5148 clearup:
5149 HcfObjDestroy(key);
5150 HcfObjDestroy(generator);
5151 if (encodedBlob.data != nullptr) {
5152 HcfFree(encodedBlob.data);
5153 encodedBlob.data = nullptr;
5154 }
5155 EXPECT_EQ(ret, 0);
5156 }
5157
5158 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest102, TestSize.Level0)
5159 {
5160 int ret = 0;
5161 HcfSymKeyGenerator *generator = nullptr;
5162 HcfSymKey *key = nullptr;
5163 HcfBlob encodedBlob = { 0 };
5164 uint8_t keyMaterial[] = {
5165 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
5166 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
5167 };
5168 HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN };
5169
5170 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
5171 if (ret != 0) {
5172 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
5173 goto clearup;
5174 }
5175 ret = generator->convertSymKey(generator, &keyTmpBlob, &key);
5176 if (ret != 0) {
5177 LOGE("generateSymKey failed!");
5178 goto clearup;
5179 }
5180
5181 // key getEncoded
5182 ret = key->key.getEncoded(nullptr, &encodedBlob);
5183 if (ret != 0) {
5184 LOGE("key GetEncoded failed.");
5185 }
5186
5187 clearup:
5188 HcfObjDestroy(key);
5189 HcfObjDestroy(generator);
5190 if (encodedBlob.data != nullptr) {
5191 HcfFree(encodedBlob.data);
5192 encodedBlob.data = nullptr;
5193 }
5194 EXPECT_NE(ret, 0);
5195 }
5196
5197 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest103, TestSize.Level0)
5198 {
5199 int ret = 0;
5200 HcfSymKeyGenerator *generator = nullptr;
5201 HcfSymKey *key = nullptr;
5202 HcfBlob encodedBlob = { 0 };
5203 uint8_t keyMaterial[] = {
5204 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
5205 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
5206 };
5207 HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN };
5208
5209 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
5210 if (ret != 0) {
5211 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
5212 goto clearup;
5213 }
5214 ret = generator->convertSymKey(generator, &keyTmpBlob, &key);
5215 if (ret != 0) {
5216 LOGE("generateSymKey failed!");
5217 goto clearup;
5218 }
5219
5220 // key getEncoded
5221 ret = key->key.getEncoded(reinterpret_cast<HcfKey *>(generator), &encodedBlob);
5222 if (ret != 0) {
5223 LOGE("key GetEncoded failed.");
5224 }
5225
5226 clearup:
5227 HcfObjDestroy(key);
5228 HcfObjDestroy(generator);
5229 if (encodedBlob.data != nullptr) {
5230 HcfFree(encodedBlob.data);
5231 encodedBlob.data = nullptr;
5232 }
5233 EXPECT_NE(ret, 0);
5234 }
5235
5236 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest104, TestSize.Level0)
5237 {
5238 int ret = 0;
5239 HcfSymKeyGenerator *generator = nullptr;
5240 HcfSymKey *key = nullptr;
5241 HcfBlob encodedBlob = { 0 };
5242 uint8_t keyMaterial[] = {
5243 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
5244 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
5245 };
5246 HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN };
5247 SymKeyImpl *impl = nullptr;
5248 size_t tmpLen = 0;
5249
5250 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
5251 if (ret != 0) {
5252 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
5253 goto clearup;
5254 }
5255 ret = generator->convertSymKey(generator, &keyTmpBlob, &key);
5256 if (ret != 0) {
5257 LOGE("generateSymKey failed!");
5258 goto clearup;
5259 }
5260 impl = reinterpret_cast<SymKeyImpl *>(key);
5261 tmpLen = impl->keyMaterial.len;
5262 impl->keyMaterial.len = 0;
5263
5264 // key getEncoded
5265 ret = key->key.getEncoded(&(key->key), &encodedBlob);
5266 impl->keyMaterial.len = tmpLen;
5267 if (ret != 0) {
5268 LOGE("key GetEncoded failed.");
5269 }
5270
5271 clearup:
5272 HcfObjDestroy(key);
5273 HcfObjDestroy(generator);
5274 if (encodedBlob.data != nullptr) {
5275 HcfFree(encodedBlob.data);
5276 encodedBlob.data = nullptr;
5277 }
5278 EXPECT_NE(ret, 0);
5279 }
5280
5281 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest105, TestSize.Level0)
5282 {
5283 int ret = 0;
5284 HcfSymKeyGenerator *generator = nullptr;
5285 HcfSymKey *key = nullptr;
5286 HcfBlob encodedBlob = { 0 };
5287 uint8_t keyMaterial[] = {
5288 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
5289 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c
5290 };
5291 HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN };
5292
5293 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
5294 if (ret != 0) {
5295 LOGE("HcfSymKeyGeneratorCreate failed!%d", ret);
5296 goto clearup;
5297 }
5298 ret = generator->convertSymKey(generator, &keyTmpBlob, &key);
5299 if (ret != 0) {
5300 LOGE("generateSymKey failed!");
5301 goto clearup;
5302 }
5303
5304 key->clearMem(nullptr);
5305
5306 ret = key->key.getEncoded(&(key->key), &encodedBlob);
5307 if (ret != 0) {
5308 LOGE("key GetEncoded failed.");
5309 goto clearup;
5310 }
5311 if ((encodedBlob.data != nullptr) && (encodedBlob.data[0] != '\0')) {
5312 LOGE("clearMem failed!");
5313 ret = HCF_ERR_CRYPTO_OPERATION;
5314 }
5315
5316 clearup:
5317 HcfObjDestroy(key);
5318 HcfObjDestroy(generator);
5319 if (encodedBlob.data != nullptr) {
5320 HcfFree(encodedBlob.data);
5321 encodedBlob.data = nullptr;
5322 }
5323 EXPECT_NE(ret, 0);
5324 }
5325
5326 HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest106, TestSize.Level0)
5327 {
5328 int ret = 0;
5329 HcfSymKeyGenerator *generator = nullptr;
5330
5331 ret = HcfSymKeyGeneratorCreate("RSA128", &generator);
5332 if (ret != 0) {
5333 LOGE("HcfSymKeyGeneratorCreate failed! Should not select RSA for symKey generator.");
5334 }
5335
5336 HcfObjDestroy(generator);
5337 EXPECT_NE(ret, 0);
5338 }
5339 }