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 "securec.h"
18
19 #include "sym_key_generator.h"
20 #include "cipher.h"
21 #include "log.h"
22 #include "memory.h"
23 #include "detailed_iv_params.h"
24 #include "detailed_gcm_params.h"
25 #include "detailed_ccm_params.h"
26 #include "aes_openssl.h"
27
28 using namespace std;
29 using namespace testing::ext;
30
31 namespace {
32 constexpr int32_t CIPHER_TEXT_LEN = 128;
33 constexpr int32_t DES_IV_LEN = 8;
34 constexpr int32_t PLAINTEXT_LEN = 13;
35
36 class Crypto3DesCipherTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase()44 void Crypto3DesCipherTest::SetUpTestCase() {}
TearDownTestCase()45 void Crypto3DesCipherTest::TearDownTestCase() {}
46
SetUp()47 void Crypto3DesCipherTest::SetUp() // add init here, this will be called before test.
48 {
49 }
50
TearDown()51 void Crypto3DesCipherTest::TearDown() // add destroy here, this will be called when test case done.
52 {
53 }
54
GenerateDesSymKey(HcfSymKey ** key)55 static int32_t GenerateDesSymKey(HcfSymKey **key)
56 {
57 HcfSymKeyGenerator *generator = nullptr;
58
59 int32_t ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
60 if (ret != 0) {
61 LOGE("HcfSymKeyGeneratorCreate failed!");
62 return ret;
63 }
64
65 ret = generator->generateSymKey(generator, key);
66 if (ret != 0) {
67 LOGE("generateSymKey failed!");
68 }
69 HcfObjDestroy(generator);
70 return ret;
71 }
72
DesEncrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int * cipherTextLen)73 static int32_t DesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
74 uint8_t *cipherText, int *cipherTextLen)
75 {
76 uint8_t plainText[] = "this is test!";
77 HcfBlob input = {.data = (uint8_t *)plainText, .len = 13};
78 HcfBlob output = {};
79 int32_t maxLen = *cipherTextLen;
80 int32_t ret = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)key, params);
81 if (ret != 0) {
82 LOGE("init failed! %d", ret);
83 return ret;
84 }
85
86 ret = cipher->update(cipher, &input, &output);
87 if (ret != 0) {
88 LOGE("update failed!");
89 return ret;
90 }
91 *cipherTextLen = output.len;
92 if (output.data != nullptr) {
93 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
94 HcfBlobDataFree(&output);
95 return -1;
96 }
97 HcfBlobDataFree(&output);
98 }
99
100 ret = cipher->doFinal(cipher, NULL, &output);
101 if (ret != 0) {
102 LOGE("doFinal failed!");
103 return ret;
104 }
105 if (output.data != nullptr) {
106 if (memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len) != EOK) {
107 HcfBlobDataFree(&output);
108 return -1;
109 }
110 *cipherTextLen += output.len;
111 HcfBlobDataFree(&output);
112 }
113 return 0;
114 }
115
DesDecrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int cipherTextLen)116 static int32_t DesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
117 uint8_t *cipherText, int cipherTextLen)
118 {
119 uint8_t plainText[] = "this is test!";
120 HcfBlob input = {.data = (uint8_t *)cipherText, .len = cipherTextLen};
121 HcfBlob output = {};
122 int32_t maxLen = cipherTextLen;
123 int32_t ret = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)key, params);
124 if (ret != 0) {
125 LOGE("init failed! %d", ret);
126 return ret;
127 }
128
129 ret = cipher->update(cipher, &input, &output);
130 if (ret != 0) {
131 LOGE("update failed!");
132 return ret;
133 }
134 cipherTextLen = output.len;
135 if (output.data != nullptr) {
136 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
137 HcfBlobDataFree(&output);
138 return -1;
139 }
140 HcfBlobDataFree(&output);
141 }
142
143 ret = cipher->doFinal(cipher, NULL, &output);
144 if (ret != 0) {
145 LOGE("doFinal failed!");
146 return ret;
147 }
148 if (output.data != nullptr) {
149 if (memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len) != EOK) {
150 HcfBlobDataFree(&output);
151 return -1;
152 }
153 cipherTextLen += output.len;
154 HcfBlobDataFree(&output);
155 }
156
157 if (cipherTextLen != sizeof(plainText) - 1) {
158 return -1;
159 }
160 return memcmp(cipherText, plainText, cipherTextLen);
161 }
162
DesNoUpdateEncrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int * cipherTextLen)163 static int32_t DesNoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
164 uint8_t *cipherText, int *cipherTextLen)
165 {
166 uint8_t plainText[] = "this is test!";
167 HcfBlob input = {.data = (uint8_t *)plainText, .len = 13};
168 HcfBlob output = {};
169 int32_t maxLen = *cipherTextLen;
170 int32_t ret = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)key, params);
171 if (ret != 0) {
172 LOGE("init failed! %d", ret);
173 return ret;
174 }
175
176 *cipherTextLen = 0;
177 ret = cipher->doFinal(cipher, &input, &output);
178 if (ret != 0) {
179 LOGE("doFinal failed!");
180 return ret;
181 }
182 if (output.data != nullptr) {
183 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
184 HcfBlobDataFree(&output);
185 return -1;
186 }
187 *cipherTextLen += output.len;
188 HcfBlobDataFree(&output);
189 }
190 return 0;
191 }
192
DesNoUpdateDecrypt(HcfCipher * cipher,HcfSymKey * key,HcfParamsSpec * params,uint8_t * cipherText,int cipherTextLen)193 static int32_t DesNoUpdateDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params,
194 uint8_t *cipherText, int cipherTextLen)
195 {
196 uint8_t plainText[] = "this is test!";
197 HcfBlob input = {.data = (uint8_t *)cipherText, .len = cipherTextLen};
198 HcfBlob output = {};
199 int32_t maxLen = cipherTextLen;
200 int32_t ret = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)key, params);
201 if (ret != 0) {
202 LOGE("init failed! %d", ret);
203 return ret;
204 }
205
206 cipherTextLen = 0;
207 ret = cipher->doFinal(cipher, &input, &output);
208 if (ret != 0) {
209 LOGE("doFinal failed!");
210 return ret;
211 }
212 if (output.data != nullptr) {
213 if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) {
214 HcfBlobDataFree(&output);
215 return -1;
216 }
217 cipherTextLen += output.len;
218 HcfBlobDataFree(&output);
219 }
220
221 if (cipherTextLen != sizeof(plainText) - 1) {
222 return -1;
223 }
224 return memcmp(cipherText, plainText, cipherTextLen);
225 }
226
227 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest001, TestSize.Level0)
228 {
229 int ret = 0;
230 uint8_t cipherText[128] = {0};
231 int cipherTextLen = 128;
232
233 HcfSymKeyGenerator *generator = NULL;
234 HcfCipher *cipher = NULL;
235 HcfSymKey *key = NULL;
236
237 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
238 if (ret != 0) {
239 LOGE("HcfSymKeyGeneratorCreate failed!");
240 goto clearup;
241 }
242
243 ret = generator->generateSymKey(generator, &key);
244 if (ret != 0) {
245 LOGE("generateSymKey failed!");
246 goto clearup;
247 }
248
249 ret = HcfCipherCreate("3DES192|ECB|NoPadding", &cipher);
250 if (ret != 0) {
251 LOGE("HcfCipherCreate failed!");
252 goto clearup;
253 }
254
255 ret = DesEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
256 if (ret != 0) {
257 LOGE("DesEncrypt failed! %d", ret);
258 goto clearup;
259 }
260
261 ret = DesDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
262 if (ret != 0) {
263 LOGE("DesDecrypt failed! %d", ret);
264 goto clearup;
265 }
266
267 clearup:
268 HcfObjDestroy((HcfObjectBase *)key);
269 HcfObjDestroy((HcfObjectBase *)cipher);
270 HcfObjDestroy((HcfObjectBase *)generator);
271 EXPECT_NE(ret, 0);
272 }
273
274 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest002, TestSize.Level0)
275 {
276 int ret = 0;
277 uint8_t cipherText[128] = {0};
278 int cipherTextLen = 128;
279
280 HcfSymKeyGenerator *generator = NULL;
281 HcfCipher *cipher = NULL;
282 HcfSymKey *key = NULL;
283
284 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
285 if (ret != 0) {
286 LOGE("HcfSymKeyGeneratorCreate failed!");
287 goto clearup;
288 }
289
290 ret = generator->generateSymKey(generator, &key);
291 if (ret != 0) {
292 LOGE("generateSymKey failed!");
293 goto clearup;
294 }
295
296 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
297 if (ret != 0) {
298 LOGE("HcfCipherCreate failed!");
299 goto clearup;
300 }
301
302 ret = DesEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
303 if (ret != 0) {
304 LOGE("DesEncrypt failed! %d", ret);
305 goto clearup;
306 }
307
308 ret = DesDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
309 if (ret != 0) {
310 LOGE("DesDecrypt failed! %d", ret);
311 goto clearup;
312 }
313
314
315 clearup:
316 HcfObjDestroy((HcfObjectBase *)key);
317 HcfObjDestroy((HcfObjectBase *)cipher);
318 HcfObjDestroy((HcfObjectBase *)generator);
319 EXPECT_EQ(ret, 0);
320 }
321
322 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest003, TestSize.Level0)
323 {
324 int ret = 0;
325 uint8_t cipherText[128] = {0};
326 int cipherTextLen = 128;
327
328 HcfSymKeyGenerator *generator = NULL;
329 HcfCipher *cipher = NULL;
330 HcfSymKey *key = NULL;
331
332 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
333 if (ret != 0) {
334 LOGE("HcfSymKeyGeneratorCreate failed!");
335 goto clearup;
336 }
337
338 ret = generator->generateSymKey(generator, &key);
339 if (ret != 0) {
340 LOGE("generateSymKey failed!");
341 goto clearup;
342 }
343
344 ret = HcfCipherCreate("3DES192|ECB|PKCS7", &cipher);
345 if (ret != 0) {
346 LOGE("HcfCipherCreate failed!");
347 goto clearup;
348 }
349
350 ret = DesEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
351 if (ret != 0) {
352 LOGE("DesEncrypt failed! %d", ret);
353 goto clearup;
354 }
355
356 ret = DesDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
357 if (ret != 0) {
358 LOGE("DesDecrypt failed! %d", ret);
359 goto clearup;
360 }
361
362 clearup:
363 HcfObjDestroy((HcfObjectBase *)key);
364 HcfObjDestroy((HcfObjectBase *)cipher);
365 HcfObjDestroy((HcfObjectBase *)generator);
366 EXPECT_EQ(ret, 0);
367 }
368
369
370 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest004, TestSize.Level0)
371 {
372 int ret = 0;
373 uint8_t cipherText[128] = {0};
374 int cipherTextLen = 128;
375
376 HcfSymKeyGenerator *generator = NULL;
377 HcfCipher *cipher = NULL;
378 HcfSymKey *key = NULL;
379 uint8_t iv[8] = {0};
380 HcfIvParamsSpec ivSpec = {};
381 ivSpec.iv.data = iv;
382 ivSpec.iv.len = 8;
383
384 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
385 if (ret != 0) {
386 LOGE("HcfSymKeyGeneratorCreate failed!");
387 goto clearup;
388 }
389
390 ret = generator->generateSymKey(generator, &key);
391 if (ret != 0) {
392 LOGE("generateSymKey failed!");
393 goto clearup;
394 }
395
396 ret = HcfCipherCreate("3DES192|CBC|NoPadding", &cipher);
397 if (ret != 0) {
398 LOGE("HcfCipherCreate failed!");
399 goto clearup;
400 }
401
402 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
403 if (ret != 0) {
404 LOGE("DesEncrypt failed! %d", ret);
405 goto clearup;
406 }
407
408 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
409 if (ret != 0) {
410 LOGE("DesDecrypt failed! %d", ret);
411 goto clearup;
412 }
413
414 clearup:
415 HcfObjDestroy((HcfObjectBase *)key);
416 HcfObjDestroy((HcfObjectBase *)cipher);
417 HcfObjDestroy((HcfObjectBase *)generator);
418 EXPECT_NE(ret, 0);
419 }
420
421 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest005, TestSize.Level0)
422 {
423 int ret = 0;
424 uint8_t cipherText[128] = {0};
425 int cipherTextLen = 128;
426
427 HcfSymKeyGenerator *generator = NULL;
428 HcfCipher *cipher = NULL;
429 HcfSymKey *key = NULL;
430 uint8_t iv[8] = {0};
431 HcfIvParamsSpec ivSpec = {};
432 ivSpec.iv.data = iv;
433 ivSpec.iv.len = 8;
434
435 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
436 if (ret != 0) {
437 LOGE("HcfSymKeyGeneratorCreate failed!");
438 goto clearup;
439 }
440
441 ret = generator->generateSymKey(generator, &key);
442 if (ret != 0) {
443 LOGE("generateSymKey failed!");
444 goto clearup;
445 }
446
447 ret = HcfCipherCreate("3DES192|CBC|PKCS5", &cipher);
448 if (ret != 0) {
449 LOGE("HcfCipherCreate failed!");
450 goto clearup;
451 }
452
453 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
454 if (ret != 0) {
455 LOGE("DesEncrypt failed! %d", ret);
456 goto clearup;
457 }
458 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
459 if (ret != 0) {
460 LOGE("DesDecrypt failed! %d", ret);
461 goto clearup;
462 }
463
464 clearup:
465 HcfObjDestroy((HcfObjectBase *)key);
466 HcfObjDestroy((HcfObjectBase *)cipher);
467 HcfObjDestroy((HcfObjectBase *)generator);
468 EXPECT_EQ(ret, 0);
469 }
470
471 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest006, TestSize.Level0)
472 {
473 int ret = 0;
474 uint8_t cipherText[128] = {0};
475 int cipherTextLen = 128;
476
477 HcfSymKeyGenerator *generator = NULL;
478 HcfCipher *cipher = NULL;
479 HcfSymKey *key = NULL;
480 uint8_t iv[8] = {0};
481 HcfIvParamsSpec ivSpec = {};
482 ivSpec.iv.data = iv;
483 ivSpec.iv.len = 8;
484
485 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
486 if (ret != 0) {
487 LOGE("HcfSymKeyGeneratorCreate failed!");
488 goto clearup;
489 }
490
491 ret = generator->generateSymKey(generator, &key);
492 if (ret != 0) {
493 LOGE("generateSymKey failed!");
494 goto clearup;
495 }
496
497 ret = HcfCipherCreate("3DES192|CBC|PKCS7", &cipher);
498 if (ret != 0) {
499 LOGE("HcfCipherCreate failed!");
500 goto clearup;
501 }
502 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
503 if (ret != 0) {
504 LOGE("DesEncrypt failed! %d", ret);
505 goto clearup;
506 }
507
508 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
509 if (ret != 0) {
510 LOGE("DesDecrypt failed! %d", ret);
511 goto clearup;
512 }
513
514 clearup:
515 HcfObjDestroy((HcfObjectBase *)key);
516 HcfObjDestroy((HcfObjectBase *)cipher);
517 HcfObjDestroy((HcfObjectBase *)generator);
518 EXPECT_EQ(ret, 0);
519 }
520
521 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest007, TestSize.Level0)
522 {
523 int ret = 0;
524 uint8_t cipherText[128] = {0};
525 int cipherTextLen = 128;
526
527 HcfSymKeyGenerator *generator = NULL;
528 HcfCipher *cipher = NULL;
529 HcfSymKey *key = NULL;
530 uint8_t iv[8] = {0};
531 HcfIvParamsSpec ivSpec = {};
532 ivSpec.iv.data = iv;
533 ivSpec.iv.len = 8;
534
535 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
536 if (ret != 0) {
537 LOGE("HcfSymKeyGeneratorCreate failed!");
538 goto clearup;
539 }
540
541 ret = generator->generateSymKey(generator, &key);
542 if (ret != 0) {
543 LOGE("generateSymKey failed!");
544 goto clearup;
545 }
546
547 ret = HcfCipherCreate("3DES192|OFB|NoPadding", &cipher);
548 if (ret != 0) {
549 LOGE("HcfCipherCreate failed!");
550 goto clearup;
551 }
552
553 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
554 if (ret != 0) {
555 LOGE("DesEncrypt failed! %d", ret);
556 goto clearup;
557 }
558
559 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
560 if (ret != 0) {
561 LOGE("DesDecrypt failed! %d", ret);
562 goto clearup;
563 }
564
565
566 clearup:
567 HcfObjDestroy((HcfObjectBase *)key);
568 HcfObjDestroy((HcfObjectBase *)cipher);
569 HcfObjDestroy((HcfObjectBase *)generator);
570 EXPECT_EQ(ret, 0);
571 }
572
573 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest008, TestSize.Level0)
574 {
575 int ret = 0;
576 uint8_t cipherText[128] = {0};
577 int cipherTextLen = 128;
578
579 HcfSymKeyGenerator *generator = NULL;
580 HcfCipher *cipher = NULL;
581 HcfSymKey *key = NULL;
582 uint8_t iv[8] = {0};
583 HcfIvParamsSpec ivSpec = {};
584 ivSpec.iv.data = iv;
585 ivSpec.iv.len = 8;
586
587 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
588 if (ret != 0) {
589 LOGE("HcfSymKeyGeneratorCreate failed!");
590 goto clearup;
591 }
592
593 ret = generator->generateSymKey(generator, &key);
594 if (ret != 0) {
595 LOGE("generateSymKey failed!");
596 goto clearup;
597 }
598
599 ret = HcfCipherCreate("3DES192|OFB|PKCS5", &cipher);
600 if (ret != 0) {
601 LOGE("HcfCipherCreate failed!");
602 goto clearup;
603 }
604 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
605 if (ret != 0) {
606 LOGE("DesEncrypt failed! %d", ret);
607 goto clearup;
608 }
609
610 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
611 if (ret != 0) {
612 LOGE("DesDecrypt failed! %d", ret);
613 goto clearup;
614 }
615
616 clearup:
617 HcfObjDestroy((HcfObjectBase *)key);
618 HcfObjDestroy((HcfObjectBase *)cipher);
619 HcfObjDestroy((HcfObjectBase *)generator);
620 EXPECT_EQ(ret, 0);
621 }
622
623 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest009, TestSize.Level0)
624 {
625 int ret = 0;
626 uint8_t cipherText[128] = {0};
627 int cipherTextLen = 128;
628
629 HcfSymKeyGenerator *generator = NULL;
630 HcfCipher *cipher = NULL;
631 HcfSymKey *key = NULL;
632 uint8_t iv[8] = {0};
633 HcfIvParamsSpec ivSpec = {};
634 ivSpec.iv.data = iv;
635 ivSpec.iv.len = 8;
636
637 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
638 if (ret != 0) {
639 LOGE("HcfSymKeyGeneratorCreate failed!");
640 goto clearup;
641 }
642
643 ret = generator->generateSymKey(generator, &key);
644 if (ret != 0) {
645 LOGE("generateSymKey failed!");
646 goto clearup;
647 }
648
649 ret = HcfCipherCreate("3DES192|OFB|PKCS7", &cipher);
650 if (ret != 0) {
651 LOGE("HcfCipherCreate failed!");
652 goto clearup;
653 }
654
655 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
656 if (ret != 0) {
657 LOGE("DesEncrypt failed! %d", ret);
658 goto clearup;
659 }
660
661 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
662 if (ret != 0) {
663 LOGE("DesDecrypt failed! %d", ret);
664 goto clearup;
665 }
666
667
668 clearup:
669 HcfObjDestroy((HcfObjectBase *)key);
670 HcfObjDestroy((HcfObjectBase *)cipher);
671 HcfObjDestroy((HcfObjectBase *)generator);
672 EXPECT_EQ(ret, 0);
673 }
674
675 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest010, TestSize.Level0)
676 {
677 int ret = 0;
678 uint8_t cipherText[128] = {0};
679 int cipherTextLen = 128;
680
681 HcfSymKeyGenerator *generator = NULL;
682 HcfCipher *cipher = NULL;
683 HcfSymKey *key = NULL;
684 uint8_t iv[8] = {0};
685 HcfIvParamsSpec ivSpec = {};
686 ivSpec.iv.data = iv;
687 ivSpec.iv.len = 8;
688
689 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
690 if (ret != 0) {
691 LOGE("HcfSymKeyGeneratorCreate failed!");
692 goto clearup;
693 }
694
695 ret = generator->generateSymKey(generator, &key);
696 if (ret != 0) {
697 LOGE("generateSymKey failed!");
698 goto clearup;
699 }
700
701 ret = HcfCipherCreate("3DES192|CFB|NoPadding", &cipher);
702 if (ret != 0) {
703 LOGE("HcfCipherCreate failed!");
704 goto clearup;
705 }
706
707 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
708 if (ret != 0) {
709 LOGE("DesEncrypt failed! %d", ret);
710 goto clearup;
711 }
712
713 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
714 if (ret != 0) {
715 LOGE("DesDecrypt failed! %d", ret);
716 goto clearup;
717 }
718
719 clearup:
720 HcfObjDestroy((HcfObjectBase *)key);
721 HcfObjDestroy((HcfObjectBase *)cipher);
722 HcfObjDestroy((HcfObjectBase *)generator);
723 EXPECT_EQ(ret, 0);
724 }
725
726 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest011, TestSize.Level0)
727 {
728 int ret = 0;
729 uint8_t cipherText[128] = {0};
730 int cipherTextLen = 128;
731
732 HcfSymKeyGenerator *generator = NULL;
733 HcfCipher *cipher = NULL;
734 HcfSymKey *key = NULL;
735 uint8_t iv[8] = {0};
736 HcfIvParamsSpec ivSpec = {};
737 ivSpec.iv.data = iv;
738 ivSpec.iv.len = 8;
739
740 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
741 if (ret != 0) {
742 LOGE("HcfSymKeyGeneratorCreate failed!");
743 goto clearup;
744 }
745
746 ret = generator->generateSymKey(generator, &key);
747 if (ret != 0) {
748 LOGE("generateSymKey failed!");
749 goto clearup;
750 }
751
752 ret = HcfCipherCreate("3DES192|CFB|PKCS5", &cipher);
753 if (ret != 0) {
754 LOGE("HcfCipherCreate failed!");
755 goto clearup;
756 }
757
758 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
759 if (ret != 0) {
760 LOGE("DesEncrypt failed! %d", ret);
761 goto clearup;
762 }
763
764 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
765 if (ret != 0) {
766 LOGE("DesDecrypt failed! %d", ret);
767 goto clearup;
768 }
769
770 clearup:
771 HcfObjDestroy((HcfObjectBase *)key);
772 HcfObjDestroy((HcfObjectBase *)cipher);
773 HcfObjDestroy((HcfObjectBase *)generator);
774 EXPECT_EQ(ret, 0);
775 }
776
777 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest012, TestSize.Level0)
778 {
779 int ret = 0;
780 uint8_t cipherText[128] = {0};
781 int cipherTextLen = 128;
782
783 HcfSymKeyGenerator *generator = NULL;
784 HcfCipher *cipher = NULL;
785 HcfSymKey *key = NULL;
786 uint8_t iv[8] = {0};
787 HcfIvParamsSpec ivSpec = {};
788 ivSpec.iv.data = iv;
789 ivSpec.iv.len = 8;
790
791 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
792 if (ret != 0) {
793 LOGE("HcfSymKeyGeneratorCreate failed!");
794 goto clearup;
795 }
796
797 ret = generator->generateSymKey(generator, &key);
798 if (ret != 0) {
799 LOGE("generateSymKey failed!");
800 goto clearup;
801 }
802
803 ret = HcfCipherCreate("3DES192|CFB|PKCS7", &cipher);
804 if (ret != 0) {
805 LOGE("HcfCipherCreate failed!");
806 goto clearup;
807 }
808
809 ret = DesEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
810 if (ret != 0) {
811 LOGE("DesEncrypt failed! %d", ret);
812 goto clearup;
813 }
814
815 ret = DesDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
816 if (ret != 0) {
817 LOGE("DesDecrypt failed! %d", ret);
818 goto clearup;
819 }
820
821 clearup:
822 HcfObjDestroy((HcfObjectBase *)key);
823 HcfObjDestroy((HcfObjectBase *)cipher);
824 HcfObjDestroy((HcfObjectBase *)generator);
825 EXPECT_EQ(ret, 0);
826 }
827
828 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest013, TestSize.Level0)
829 {
830 int ret = 0;
831 uint8_t cipherText[128] = {0};
832 int cipherTextLen = 128;
833
834 HcfSymKeyGenerator *generator = NULL;
835 HcfCipher *cipher = NULL;
836 HcfSymKey *key = NULL;
837
838 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
839 if (ret != 0) {
840 LOGE("HcfSymKeyGeneratorCreate failed!");
841 goto clearup;
842 }
843
844 ret = generator->generateSymKey(generator, &key);
845 if (ret != 0) {
846 LOGE("generateSymKey failed!");
847 goto clearup;
848 }
849
850 ret = HcfCipherCreate("3DES192|ECB|NoPadding", &cipher);
851 if (ret != 0) {
852 LOGE("HcfCipherCreate failed!");
853 goto clearup;
854 }
855
856 ret = DesNoUpdateEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
857 if (ret != 0) {
858 LOGE("DesNoUpdateEncrypt failed! %d", ret);
859 goto clearup;
860 }
861
862 ret = DesNoUpdateDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
863 if (ret != 0) {
864 LOGE("DesNoUpdateDecrypt failed! %d", ret);
865 goto clearup;
866 }
867
868 clearup:
869 HcfObjDestroy((HcfObjectBase *)key);
870 HcfObjDestroy((HcfObjectBase *)cipher);
871 HcfObjDestroy((HcfObjectBase *)generator);
872 EXPECT_NE(ret, 0);
873 }
874
875 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest014, TestSize.Level0)
876 {
877 int ret = 0;
878 uint8_t cipherText[128] = {0};
879 int cipherTextLen = 128;
880
881 HcfSymKeyGenerator *generator = NULL;
882 HcfCipher *cipher = NULL;
883 HcfSymKey *key = NULL;
884
885 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
886 if (ret != 0) {
887 LOGE("HcfSymKeyGeneratorCreate failed!");
888 goto clearup;
889 }
890
891 ret = generator->generateSymKey(generator, &key);
892 if (ret != 0) {
893 LOGE("generateSymKey failed!");
894 goto clearup;
895 }
896
897 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
898 if (ret != 0) {
899 LOGE("HcfCipherCreate failed!");
900 goto clearup;
901 }
902
903 ret = DesNoUpdateEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
904 if (ret != 0) {
905 LOGE("DesNoUpdateEncrypt failed! %d", ret);
906 goto clearup;
907 }
908
909 ret = DesNoUpdateDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
910 if (ret != 0) {
911 LOGE("DesNoUpdateDecrypt failed! %d", ret);
912 goto clearup;
913 }
914
915
916 clearup:
917 HcfObjDestroy((HcfObjectBase *)key);
918 HcfObjDestroy((HcfObjectBase *)cipher);
919 HcfObjDestroy((HcfObjectBase *)generator);
920 EXPECT_EQ(ret, 0);
921 }
922
923 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest015, TestSize.Level0)
924 {
925 int ret = 0;
926 uint8_t cipherText[128] = {0};
927 int cipherTextLen = 128;
928
929 HcfSymKeyGenerator *generator = NULL;
930 HcfCipher *cipher = NULL;
931 HcfSymKey *key = NULL;
932
933 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
934 if (ret != 0) {
935 LOGE("HcfSymKeyGeneratorCreate failed!");
936 goto clearup;
937 }
938
939 ret = generator->generateSymKey(generator, &key);
940 if (ret != 0) {
941 LOGE("generateSymKey failed!");
942 goto clearup;
943 }
944
945 ret = HcfCipherCreate("3DES192|ECB|PKCS7", &cipher);
946 if (ret != 0) {
947 LOGE("HcfCipherCreate failed!");
948 goto clearup;
949 }
950
951 ret = DesNoUpdateEncrypt(cipher, key, NULL, cipherText, &cipherTextLen);
952 if (ret != 0) {
953 LOGE("DesNoUpdateEncrypt failed! %d", ret);
954 goto clearup;
955 }
956
957 ret = DesNoUpdateDecrypt(cipher, key, NULL, cipherText, cipherTextLen);
958 if (ret != 0) {
959 LOGE("DesNoUpdateDecrypt failed! %d", ret);
960 goto clearup;
961 }
962
963 clearup:
964 HcfObjDestroy((HcfObjectBase *)key);
965 HcfObjDestroy((HcfObjectBase *)cipher);
966 HcfObjDestroy((HcfObjectBase *)generator);
967 EXPECT_EQ(ret, 0);
968 }
969
970
971 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest016, TestSize.Level0)
972 {
973 int ret = 0;
974 uint8_t cipherText[128] = {0};
975 int cipherTextLen = 128;
976
977 HcfSymKeyGenerator *generator = NULL;
978 HcfCipher *cipher = NULL;
979 HcfSymKey *key = NULL;
980 uint8_t iv[8] = {0};
981 HcfIvParamsSpec ivSpec = {};
982 ivSpec.iv.data = iv;
983 ivSpec.iv.len = 8;
984
985 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
986 if (ret != 0) {
987 LOGE("HcfSymKeyGeneratorCreate failed!");
988 goto clearup;
989 }
990
991 ret = generator->generateSymKey(generator, &key);
992 if (ret != 0) {
993 LOGE("generateSymKey failed!");
994 goto clearup;
995 }
996
997 ret = HcfCipherCreate("3DES192|CBC|NoPadding", &cipher);
998 if (ret != 0) {
999 LOGE("HcfCipherCreate failed!");
1000 goto clearup;
1001 }
1002
1003 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1004 if (ret != 0) {
1005 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1006 goto clearup;
1007 }
1008
1009 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1010 if (ret != 0) {
1011 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1012 goto clearup;
1013 }
1014
1015 clearup:
1016 HcfObjDestroy((HcfObjectBase *)key);
1017 HcfObjDestroy((HcfObjectBase *)cipher);
1018 HcfObjDestroy((HcfObjectBase *)generator);
1019 EXPECT_NE(ret, 0);
1020 }
1021
1022 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest017, TestSize.Level0)
1023 {
1024 int ret = 0;
1025 uint8_t cipherText[128] = {0};
1026 int cipherTextLen = 128;
1027
1028 HcfSymKeyGenerator *generator = NULL;
1029 HcfCipher *cipher = NULL;
1030 HcfSymKey *key = NULL;
1031 uint8_t iv[8] = {0};
1032 HcfIvParamsSpec ivSpec = {};
1033 ivSpec.iv.data = iv;
1034 ivSpec.iv.len = 8;
1035
1036 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1037 if (ret != 0) {
1038 LOGE("HcfSymKeyGeneratorCreate failed!");
1039 goto clearup;
1040 }
1041
1042 ret = generator->generateSymKey(generator, &key);
1043 if (ret != 0) {
1044 LOGE("generateSymKey failed!");
1045 goto clearup;
1046 }
1047
1048 ret = HcfCipherCreate("3DES192|CBC|PKCS5", &cipher);
1049 if (ret != 0) {
1050 LOGE("HcfCipherCreate failed!");
1051 goto clearup;
1052 }
1053
1054 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1055 if (ret != 0) {
1056 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1057 goto clearup;
1058 }
1059
1060 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1061 if (ret != 0) {
1062 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1063 goto clearup;
1064 }
1065
1066 clearup:
1067 HcfObjDestroy((HcfObjectBase *)key);
1068 HcfObjDestroy((HcfObjectBase *)cipher);
1069 HcfObjDestroy((HcfObjectBase *)generator);
1070 EXPECT_EQ(ret, 0);
1071 }
1072
1073 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest018, TestSize.Level0)
1074 {
1075 int ret = 0;
1076 uint8_t cipherText[128] = {0};
1077 int cipherTextLen = 128;
1078
1079 HcfSymKeyGenerator *generator = NULL;
1080 HcfCipher *cipher = NULL;
1081 HcfSymKey *key = NULL;
1082 uint8_t iv[8] = {0};
1083 HcfIvParamsSpec ivSpec = {};
1084 ivSpec.iv.data = iv;
1085 ivSpec.iv.len = 8;
1086
1087 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1088 if (ret != 0) {
1089 LOGE("HcfSymKeyGeneratorCreate failed!");
1090 goto clearup;
1091 }
1092
1093 ret = generator->generateSymKey(generator, &key);
1094 if (ret != 0) {
1095 LOGE("generateSymKey failed!");
1096 goto clearup;
1097 }
1098
1099 ret = HcfCipherCreate("3DES192|CBC|PKCS7", &cipher);
1100 if (ret != 0) {
1101 LOGE("HcfCipherCreate failed!");
1102 goto clearup;
1103 }
1104
1105 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1106 if (ret != 0) {
1107 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1108 goto clearup;
1109 }
1110
1111 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1112 if (ret != 0) {
1113 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1114 goto clearup;
1115 }
1116
1117 clearup:
1118 HcfObjDestroy((HcfObjectBase *)key);
1119 HcfObjDestroy((HcfObjectBase *)cipher);
1120 HcfObjDestroy((HcfObjectBase *)generator);
1121 EXPECT_EQ(ret, 0);
1122 }
1123
1124 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest019, TestSize.Level0)
1125 {
1126 int ret = 0;
1127 uint8_t cipherText[128] = {0};
1128 int cipherTextLen = 128;
1129
1130 HcfSymKeyGenerator *generator = NULL;
1131 HcfCipher *cipher = NULL;
1132 HcfSymKey *key = NULL;
1133 uint8_t iv[8] = {0};
1134 HcfIvParamsSpec ivSpec = {};
1135 ivSpec.iv.data = iv;
1136 ivSpec.iv.len = 8;
1137
1138 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1139 if (ret != 0) {
1140 LOGE("HcfSymKeyGeneratorCreate failed!");
1141 goto clearup;
1142 }
1143
1144 ret = generator->generateSymKey(generator, &key);
1145 if (ret != 0) {
1146 LOGE("generateSymKey failed!");
1147 goto clearup;
1148 }
1149
1150 ret = HcfCipherCreate("3DES192|OFB|NoPadding", &cipher);
1151 if (ret != 0) {
1152 LOGE("HcfCipherCreate failed!");
1153 goto clearup;
1154 }
1155
1156 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1157 if (ret != 0) {
1158 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1159 goto clearup;
1160 }
1161
1162 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1163 if (ret != 0) {
1164 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1165 goto clearup;
1166 }
1167
1168
1169 clearup:
1170 HcfObjDestroy((HcfObjectBase *)key);
1171 HcfObjDestroy((HcfObjectBase *)cipher);
1172 HcfObjDestroy((HcfObjectBase *)generator);
1173 EXPECT_EQ(ret, 0);
1174 }
1175
1176 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest020, TestSize.Level0)
1177 {
1178 int ret = 0;
1179 uint8_t cipherText[128] = {0};
1180 int cipherTextLen = 128;
1181
1182 HcfSymKeyGenerator *generator = NULL;
1183 HcfCipher *cipher = NULL;
1184 HcfSymKey *key = NULL;
1185 uint8_t iv[8] = {0};
1186 HcfIvParamsSpec ivSpec = {};
1187 ivSpec.iv.data = iv;
1188 ivSpec.iv.len = 8;
1189
1190 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1191 if (ret != 0) {
1192 LOGE("HcfSymKeyGeneratorCreate failed!");
1193 goto clearup;
1194 }
1195
1196 ret = generator->generateSymKey(generator, &key);
1197 if (ret != 0) {
1198 LOGE("generateSymKey failed!");
1199 goto clearup;
1200 }
1201
1202 ret = HcfCipherCreate("3DES192|OFB|PKCS5", &cipher);
1203 if (ret != 0) {
1204 LOGE("HcfCipherCreate failed!");
1205 goto clearup;
1206 }
1207
1208 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1209 if (ret != 0) {
1210 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1211 goto clearup;
1212 }
1213
1214 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1215 if (ret != 0) {
1216 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1217 goto clearup;
1218 }
1219
1220 clearup:
1221 HcfObjDestroy((HcfObjectBase *)key);
1222 HcfObjDestroy((HcfObjectBase *)cipher);
1223 HcfObjDestroy((HcfObjectBase *)generator);
1224 EXPECT_EQ(ret, 0);
1225 }
1226
1227 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest021, TestSize.Level0)
1228 {
1229 int ret = 0;
1230 uint8_t cipherText[128] = {0};
1231 int cipherTextLen = 128;
1232
1233 HcfSymKeyGenerator *generator = NULL;
1234 HcfCipher *cipher = NULL;
1235 HcfSymKey *key = NULL;
1236 uint8_t iv[8] = {0};
1237 HcfIvParamsSpec ivSpec = {};
1238 ivSpec.iv.data = iv;
1239 ivSpec.iv.len = 8;
1240
1241 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1242 if (ret != 0) {
1243 LOGE("HcfSymKeyGeneratorCreate failed!");
1244 goto clearup;
1245 }
1246
1247 ret = generator->generateSymKey(generator, &key);
1248 if (ret != 0) {
1249 LOGE("generateSymKey failed!");
1250 goto clearup;
1251 }
1252
1253 ret = HcfCipherCreate("3DES192|OFB|PKCS7", &cipher);
1254 if (ret != 0) {
1255 LOGE("HcfCipherCreate failed!");
1256 goto clearup;
1257 }
1258
1259 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1260 if (ret != 0) {
1261 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1262 goto clearup;
1263 }
1264
1265 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1266 if (ret != 0) {
1267 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1268 goto clearup;
1269 }
1270
1271
1272 clearup:
1273 HcfObjDestroy((HcfObjectBase *)key);
1274 HcfObjDestroy((HcfObjectBase *)cipher);
1275 HcfObjDestroy((HcfObjectBase *)generator);
1276 EXPECT_EQ(ret, 0);
1277 }
1278
1279 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest022, TestSize.Level0)
1280 {
1281 int ret = 0;
1282 uint8_t cipherText[128] = {0};
1283 int cipherTextLen = 128;
1284
1285 HcfSymKeyGenerator *generator = NULL;
1286 HcfCipher *cipher = NULL;
1287 HcfSymKey *key = NULL;
1288 uint8_t iv[8] = {0};
1289 HcfIvParamsSpec ivSpec = {};
1290 ivSpec.iv.data = iv;
1291 ivSpec.iv.len = 8;
1292
1293 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1294 if (ret != 0) {
1295 LOGE("HcfSymKeyGeneratorCreate failed!");
1296 goto clearup;
1297 }
1298
1299 ret = generator->generateSymKey(generator, &key);
1300 if (ret != 0) {
1301 LOGE("generateSymKey failed!");
1302 goto clearup;
1303 }
1304
1305 ret = HcfCipherCreate("3DES192|CFB|NoPadding", &cipher);
1306 if (ret != 0) {
1307 LOGE("HcfCipherCreate failed!");
1308 goto clearup;
1309 }
1310
1311 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1312 if (ret != 0) {
1313 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1314 goto clearup;
1315 }
1316
1317 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1318 if (ret != 0) {
1319 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1320 goto clearup;
1321 }
1322
1323 clearup:
1324 HcfObjDestroy((HcfObjectBase *)key);
1325 HcfObjDestroy((HcfObjectBase *)cipher);
1326 HcfObjDestroy((HcfObjectBase *)generator);
1327 EXPECT_EQ(ret, 0);
1328 }
1329
1330 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest023, TestSize.Level0)
1331 {
1332 int ret = 0;
1333 uint8_t cipherText[128] = {0};
1334 int cipherTextLen = 128;
1335
1336 HcfSymKeyGenerator *generator = NULL;
1337 HcfCipher *cipher = NULL;
1338 HcfSymKey *key = NULL;
1339 uint8_t iv[8] = {0};
1340 HcfIvParamsSpec ivSpec = {};
1341 ivSpec.iv.data = iv;
1342 ivSpec.iv.len = 8;
1343
1344 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1345 if (ret != 0) {
1346 LOGE("HcfSymKeyGeneratorCreate failed!");
1347 goto clearup;
1348 }
1349
1350 ret = generator->generateSymKey(generator, &key);
1351 if (ret != 0) {
1352 LOGE("generateSymKey failed!");
1353 goto clearup;
1354 }
1355
1356 ret = HcfCipherCreate("3DES192|CFB|PKCS5", &cipher);
1357 if (ret != 0) {
1358 LOGE("HcfCipherCreate failed!");
1359 goto clearup;
1360 }
1361
1362 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1363 if (ret != 0) {
1364 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1365 goto clearup;
1366 }
1367
1368 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1369 if (ret != 0) {
1370 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1371 goto clearup;
1372 }
1373
1374 clearup:
1375 HcfObjDestroy((HcfObjectBase *)key);
1376 HcfObjDestroy((HcfObjectBase *)cipher);
1377 HcfObjDestroy((HcfObjectBase *)generator);
1378 EXPECT_EQ(ret, 0);
1379 }
1380
1381 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest024, TestSize.Level0)
1382 {
1383 int ret = 0;
1384 uint8_t cipherText[128] = {0};
1385 int cipherTextLen = 128;
1386
1387 HcfSymKeyGenerator *generator = NULL;
1388 HcfCipher *cipher = NULL;
1389 HcfSymKey *key = NULL;
1390 uint8_t iv[8] = {0};
1391 HcfIvParamsSpec ivSpec = {};
1392 ivSpec.iv.data = iv;
1393 ivSpec.iv.len = 8;
1394
1395 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1396 if (ret != 0) {
1397 LOGE("HcfSymKeyGeneratorCreate failed!");
1398 goto clearup;
1399 }
1400
1401 ret = generator->generateSymKey(generator, &key);
1402 if (ret != 0) {
1403 LOGE("generateSymKey failed!");
1404 goto clearup;
1405 }
1406
1407 ret = HcfCipherCreate("3DES192|CFB|PKCS7", &cipher);
1408 if (ret != 0) {
1409 LOGE("HcfCipherCreate failed!");
1410 goto clearup;
1411 }
1412
1413 ret = DesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, &cipherTextLen);
1414 if (ret != 0) {
1415 LOGE("DesNoUpdateEncrypt failed! %d", ret);
1416 goto clearup;
1417 }
1418
1419 ret = DesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&ivSpec, cipherText, cipherTextLen);
1420 if (ret != 0) {
1421 LOGE("DesNoUpdateDecrypt failed! %d", ret);
1422 goto clearup;
1423 }
1424
1425 clearup:
1426 HcfObjDestroy((HcfObjectBase *)key);
1427 HcfObjDestroy((HcfObjectBase *)cipher);
1428 HcfObjDestroy((HcfObjectBase *)generator);
1429 EXPECT_EQ(ret, 0);
1430 }
1431
1432 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest025, TestSize.Level0)
1433 {
1434 int ret = 0;
1435 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
1436 int cipherTextLen = CIPHER_TEXT_LEN;
1437 HcfCipher *cipher = nullptr;
1438 HcfSymKey *key = nullptr;
1439 uint8_t iv[DES_IV_LEN] = { 0 };
1440 HcfIvParamsSpec ivSpec = {};
1441 ivSpec.iv.data = iv;
1442 ivSpec.iv.len = DES_IV_LEN;
1443
1444 ret = GenerateDesSymKey(&key);
1445 if (ret != 0) {
1446 LOGE("generateSymKey failed!");
1447 goto clearup;
1448 }
1449
1450 ret = HcfCipherCreate("3DES192|CFB1|NoPadding", &cipher);
1451 if (ret != 0) {
1452 LOGE("HcfCipherCreate failed!");
1453 goto clearup;
1454 }
1455
1456 ret = DesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
1457 if (ret != 0) {
1458 LOGE("DesEncrypt failed! %d", ret);
1459 goto clearup;
1460 }
1461
1462 ret = DesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
1463 if (ret != 0) {
1464 LOGE("DesDecrypt failed! %d", ret);
1465 }
1466
1467 clearup:
1468 HcfObjDestroy(key);
1469 HcfObjDestroy(cipher);
1470 EXPECT_EQ(ret, 0);
1471 }
1472
1473 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest026, TestSize.Level0)
1474 {
1475 int ret = 0;
1476 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
1477 int cipherTextLen = CIPHER_TEXT_LEN;
1478 HcfCipher *cipher = nullptr;
1479 HcfSymKey *key = nullptr;
1480 uint8_t iv[DES_IV_LEN] = { 0 };
1481 HcfIvParamsSpec ivSpec = {};
1482 ivSpec.iv.data = iv;
1483 ivSpec.iv.len = DES_IV_LEN;
1484
1485 ret = GenerateDesSymKey(&key);
1486 if (ret != 0) {
1487 LOGE("generateSymKey failed!");
1488 goto clearup;
1489 }
1490
1491 ret = HcfCipherCreate("3DES192|CFB8|NoPadding", &cipher);
1492 if (ret != 0) {
1493 LOGE("HcfCipherCreate failed!");
1494 goto clearup;
1495 }
1496
1497 ret = DesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
1498 if (ret != 0) {
1499 LOGE("DesEncrypt failed! %d", ret);
1500 goto clearup;
1501 }
1502
1503 ret = DesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
1504 if (ret != 0) {
1505 LOGE("DesDecrypt failed! %d", ret);
1506 }
1507
1508 clearup:
1509 HcfObjDestroy(key);
1510 HcfObjDestroy(cipher);
1511 EXPECT_EQ(ret, 0);
1512 }
1513
1514 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest027, TestSize.Level0)
1515 {
1516 int ret = 0;
1517 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 };
1518 int cipherTextLen = CIPHER_TEXT_LEN;
1519 HcfCipher *cipher = nullptr;
1520 HcfSymKey *key = nullptr;
1521 uint8_t iv[DES_IV_LEN] = { 0 };
1522 HcfIvParamsSpec ivSpec = {};
1523 ivSpec.iv.data = iv;
1524 ivSpec.iv.len = DES_IV_LEN;
1525
1526 ret = GenerateDesSymKey(&key);
1527 if (ret != 0) {
1528 LOGE("generateSymKey failed!");
1529 goto clearup;
1530 }
1531
1532 ret = HcfCipherCreate("3DES192|PKCS5", &cipher);
1533 if (ret != 0) {
1534 LOGE("HcfCipherCreate failed!");
1535 goto clearup;
1536 }
1537
1538 ret = DesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen);
1539 if (ret != 0) {
1540 LOGE("DesEncrypt failed! %d", ret);
1541 goto clearup;
1542 }
1543
1544 ret = DesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen);
1545 if (ret != 0) {
1546 LOGE("DesDecrypt failed! %d", ret);
1547 }
1548
1549 clearup:
1550 HcfObjDestroy(key);
1551 HcfObjDestroy(cipher);
1552 EXPECT_EQ(ret, 0);
1553 }
1554
1555 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest028, TestSize.Level0)
1556 {
1557 int ret = 0;
1558 HcfCipher *cipher = nullptr;
1559 HcfSymKey *key = nullptr;
1560
1561 ret = GenerateDesSymKey(&key);
1562 if (ret != 0) {
1563 LOGE("generateSymKey failed!");
1564 goto clearup;
1565 }
1566
1567 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
1568 if (ret != 0) {
1569 LOGE("HcfCipherCreate failed!");
1570 goto clearup;
1571 }
1572
1573 ret = cipher->init(nullptr, ENCRYPT_MODE, &(key->key), nullptr);
1574 if (ret != 0) {
1575 LOGE("init failed! %d", ret);
1576 }
1577
1578 clearup:
1579 HcfObjDestroy(key);
1580 HcfObjDestroy(cipher);
1581 EXPECT_NE(ret, 0);
1582 }
1583
1584 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest029, TestSize.Level0)
1585 {
1586 int ret = 0;
1587 HcfSymKeyGenerator *generator = nullptr;
1588 HcfCipher *cipher = nullptr;
1589 HcfSymKey *key = nullptr;
1590
1591 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
1592 if (ret != 0) {
1593 LOGE("HcfSymKeyGeneratorCreate failed!");
1594 goto clearup;
1595 }
1596
1597 ret = generator->generateSymKey(generator, &key);
1598 if (ret != 0) {
1599 LOGE("generateSymKey failed!");
1600 goto clearup;
1601 }
1602
1603 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
1604 if (ret != 0) {
1605 LOGE("HcfCipherCreate failed!");
1606 goto clearup;
1607 }
1608
1609 ret = cipher->init(reinterpret_cast<HcfCipher *>(generator), ENCRYPT_MODE, &(key->key), nullptr);
1610 if (ret != 0) {
1611 LOGE("init failed! %d", ret);
1612 }
1613
1614 clearup:
1615 HcfObjDestroy(key);
1616 HcfObjDestroy(cipher);
1617 HcfObjDestroy(generator);
1618 EXPECT_NE(ret, 0);
1619 }
1620
1621 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest030, TestSize.Level0)
1622 {
1623 int ret = 0;
1624 HcfCipher *cipher = nullptr;
1625 HcfSymKey *key = nullptr;
1626
1627 ret = GenerateDesSymKey(&key);
1628 if (ret != 0) {
1629 LOGE("generateSymKey failed!");
1630 goto clearup;
1631 }
1632
1633 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
1634 if (ret != 0) {
1635 LOGE("HcfCipherCreate failed!");
1636 goto clearup;
1637 }
1638
1639 ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast<HcfKey *>(cipher), nullptr);
1640 if (ret != 0) {
1641 LOGE("init failed! %d", ret);
1642 }
1643
1644 clearup:
1645 HcfObjDestroy(key);
1646 HcfObjDestroy(cipher);
1647 EXPECT_NE(ret, 0);
1648 }
1649
1650 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest031, TestSize.Level0)
1651 {
1652 int ret = 0;
1653 HcfCipher *cipher = nullptr;
1654 HcfSymKey *key = nullptr;
1655 uint8_t plainText[] = "this is test!";
1656 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN };
1657 HcfBlob output = { .data = nullptr, .len = 0 };
1658
1659 ret = GenerateDesSymKey(&key);
1660 if (ret != 0) {
1661 LOGE("generateSymKey failed!");
1662 goto clearup;
1663 }
1664
1665 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
1666 if (ret != 0) {
1667 LOGE("HcfCipherCreate failed!");
1668 goto clearup;
1669 }
1670
1671 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
1672 if (ret != 0) {
1673 LOGE("init failed! %d", ret);
1674 goto clearup;
1675 }
1676 ret = cipher->update(nullptr, &input, &output);
1677 if (ret != 0) {
1678 LOGE("update failed!");
1679 }
1680 clearup:
1681 HcfObjDestroy(key);
1682 HcfObjDestroy(cipher);
1683 if (output.data != nullptr) {
1684 HcfFree(output.data);
1685 output.data = nullptr;
1686 }
1687 EXPECT_NE(ret, 0);
1688 }
1689
1690 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest032, TestSize.Level0)
1691 {
1692 int ret = 0;
1693 HcfCipher *cipher = nullptr;
1694 HcfSymKey *key = nullptr;
1695 uint8_t plainText[] = "this is test!";
1696 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN };
1697 HcfBlob output = { .data = nullptr, .len = 0 };
1698
1699 ret = GenerateDesSymKey(&key);
1700 if (ret != 0) {
1701 LOGE("generateSymKey failed!");
1702 goto clearup;
1703 }
1704
1705 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
1706 if (ret != 0) {
1707 LOGE("HcfCipherCreate failed!");
1708 goto clearup;
1709 }
1710
1711 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
1712 if (ret != 0) {
1713 LOGE("init failed! %d", ret);
1714 goto clearup;
1715 }
1716 ret = cipher->update(reinterpret_cast<HcfCipher *>(key), &input, &output);
1717 if (ret != 0) {
1718 LOGE("update failed!");
1719 }
1720 clearup:
1721 HcfObjDestroy(key);
1722 HcfObjDestroy(cipher);
1723 if (output.data != nullptr) {
1724 HcfFree(output.data);
1725 output.data = nullptr;
1726 }
1727 EXPECT_NE(ret, 0);
1728 }
1729
1730 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest033, TestSize.Level0)
1731 {
1732 int ret = 0;
1733 HcfCipher *cipher = nullptr;
1734 HcfSymKey *key = nullptr;
1735 uint8_t plainText[] = "this is test!";
1736 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN };
1737 HcfBlob output = { .data = nullptr, .len = 0 };
1738
1739 ret = GenerateDesSymKey(&key);
1740 if (ret != 0) {
1741 LOGE("generateSymKey failed!");
1742 goto clearup;
1743 }
1744
1745 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
1746 if (ret != 0) {
1747 LOGE("HcfCipherCreate failed!");
1748 goto clearup;
1749 }
1750
1751 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
1752 if (ret != 0) {
1753 LOGE("init failed! %d", ret);
1754 goto clearup;
1755 }
1756 ret = cipher->doFinal(nullptr, &input, &output);
1757 if (ret != 0) {
1758 LOGE("doFinal failed!");
1759 }
1760 clearup:
1761 HcfObjDestroy(key);
1762 HcfObjDestroy(cipher);
1763 if (output.data != nullptr) {
1764 HcfFree(output.data);
1765 output.data = nullptr;
1766 }
1767 EXPECT_NE(ret, 0);
1768 }
1769
1770 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest034, TestSize.Level0)
1771 {
1772 int ret = 0;
1773 HcfCipher *cipher = nullptr;
1774 HcfSymKey *key = nullptr;
1775 uint8_t plainText[] = "this is test!";
1776 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN };
1777 HcfBlob output = { .data = nullptr, .len = 0 };
1778
1779 ret = GenerateDesSymKey(&key);
1780 if (ret != 0) {
1781 LOGE("generateSymKey failed!");
1782 goto clearup;
1783 }
1784
1785 ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher);
1786 if (ret != 0) {
1787 LOGE("HcfCipherCreate failed!");
1788 goto clearup;
1789 }
1790
1791 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr);
1792 if (ret != 0) {
1793 LOGE("init failed! %d", ret);
1794 goto clearup;
1795 }
1796 ret = cipher->doFinal(reinterpret_cast<HcfCipher *>(key), &input, &output);
1797 if (ret != 0) {
1798 LOGE("doFinal failed!");
1799 }
1800 clearup:
1801 HcfObjDestroy(key);
1802 HcfObjDestroy(cipher);
1803 if (output.data != nullptr) {
1804 HcfFree(output.data);
1805 output.data = nullptr;
1806 }
1807 EXPECT_NE(ret, 0);
1808 }
1809
1810 HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest035, TestSize.Level0)
1811 {
1812 int ret = HcfCipherDesGeneratorSpiCreate(nullptr, nullptr);
1813 if (ret != 0) {
1814 LOGE("HcfCipherDesGeneratorSpiCreate failed!");
1815 }
1816 EXPECT_NE(ret, 0);
1817 }
1818 }
1819