1 /* 2 * Copyright (C) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 #include <fstream> 18 #include <iostream> 19 #include "securec.h" 20 #include "aes_openssl.h" 21 #include "aes_common.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 #include "sm4_common.h" 32 #include "sm4_openssl.h" 33 34 using namespace std; 35 using namespace testing::ext; 36 37 namespace { 38 class CryptoSM4CipherTest : public testing::Test { 39 public: SetUpTestCase()40 static void SetUpTestCase() {}; TearDownTestCase()41 static void TearDownTestCase() {}; SetUp()42 void SetUp() {}; TearDown()43 void TearDown() {}; 44 }; 45 46 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest004, TestSize.Level0) 47 { 48 uint8_t cipherText[128] = {0}; 49 int cipherTextLen = 128; 50 51 HcfSymKeyGenerator *generator = nullptr; 52 HcfCipher *cipher = nullptr; 53 HcfSymKey *key = nullptr; 54 55 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 56 ASSERT_EQ(ret, 0); 57 58 ret = generator->generateSymKey(generator, &key); 59 ASSERT_EQ(ret, 0); 60 61 ret = HcfCipherCreate("SM4_128|CBC|NoPadding", &cipher); 62 ASSERT_EQ(ret, 0); 63 64 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 65 ASSERT_NE(ret, 0); 66 67 HcfObjDestroy((HcfObjectBase *)key); 68 HcfObjDestroy((HcfObjectBase *)cipher); 69 HcfObjDestroy((HcfObjectBase *)generator); 70 } 71 72 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest005, TestSize.Level0) 73 { 74 uint8_t cipherText[128] = {0}; 75 int cipherTextLen = 128; 76 77 HcfSymKeyGenerator *generator = nullptr; 78 HcfCipher *cipher = nullptr; 79 HcfSymKey *key = nullptr; 80 81 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 82 ASSERT_EQ(ret, 0); 83 84 ret = generator->generateSymKey(generator, &key); 85 ASSERT_EQ(ret, 0); 86 87 ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher); 88 ASSERT_EQ(ret, 0); 89 90 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 91 ASSERT_EQ(ret, 0); 92 93 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 94 ASSERT_EQ(ret, 0); 95 96 HcfObjDestroy((HcfObjectBase *)key); 97 HcfObjDestroy((HcfObjectBase *)cipher); 98 HcfObjDestroy((HcfObjectBase *)generator); 99 } 100 101 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest006, TestSize.Level0) 102 { 103 uint8_t cipherText[128] = {0}; 104 int cipherTextLen = 128; 105 106 HcfSymKeyGenerator *generator = nullptr; 107 HcfCipher *cipher = nullptr; 108 HcfSymKey *key = nullptr; 109 110 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 111 ASSERT_EQ(ret, 0); 112 113 ret = generator->generateSymKey(generator, &key); 114 ASSERT_EQ(ret, 0); 115 116 ret = HcfCipherCreate("SM4_128|CBC|PKCS7", &cipher); 117 ASSERT_EQ(ret, 0); 118 119 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 120 ASSERT_EQ(ret, 0); 121 122 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 123 ASSERT_EQ(ret, 0); 124 125 HcfObjDestroy((HcfObjectBase *)key); 126 HcfObjDestroy((HcfObjectBase *)cipher); 127 HcfObjDestroy((HcfObjectBase *)generator); 128 } 129 130 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest007, TestSize.Level0) 131 { 132 uint8_t cipherText[128] = {0}; 133 int cipherTextLen = 128; 134 135 HcfSymKeyGenerator *generator = nullptr; 136 HcfCipher *cipher = nullptr; 137 HcfSymKey *key = nullptr; 138 139 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 140 ASSERT_EQ(ret, 0); 141 142 ret = generator->generateSymKey(generator, &key); 143 ASSERT_EQ(ret, 0); 144 145 ret = HcfCipherCreate("SM4_128|OFB|NoPadding", &cipher); 146 ASSERT_EQ(ret, 0); 147 148 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 149 ASSERT_EQ(ret, 0); 150 151 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 152 ASSERT_EQ(ret, 0); 153 154 HcfObjDestroy((HcfObjectBase *)key); 155 HcfObjDestroy((HcfObjectBase *)cipher); 156 HcfObjDestroy((HcfObjectBase *)generator); 157 } 158 159 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest008, TestSize.Level0) 160 { 161 uint8_t cipherText[128] = {0}; 162 int cipherTextLen = 128; 163 164 HcfSymKeyGenerator *generator = nullptr; 165 HcfCipher *cipher = nullptr; 166 HcfSymKey *key = nullptr; 167 168 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 169 ASSERT_EQ(ret, 0); 170 171 ret = generator->generateSymKey(generator, &key); 172 ASSERT_EQ(ret, 0); 173 174 ret = HcfCipherCreate("SM4_128|OFB|PKCS5", &cipher); 175 ASSERT_EQ(ret, 0); 176 177 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 178 ASSERT_EQ(ret, 0); 179 180 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 181 ASSERT_EQ(ret, 0); 182 183 HcfObjDestroy((HcfObjectBase *)key); 184 HcfObjDestroy((HcfObjectBase *)cipher); 185 HcfObjDestroy((HcfObjectBase *)generator); 186 } 187 188 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest009, TestSize.Level0) 189 { 190 uint8_t cipherText[128] = {0}; 191 int cipherTextLen = 128; 192 193 HcfSymKeyGenerator *generator = nullptr; 194 HcfCipher *cipher = nullptr; 195 HcfSymKey *key = nullptr; 196 197 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 198 ASSERT_EQ(ret, 0); 199 200 ret = generator->generateSymKey(generator, &key); 201 ASSERT_EQ(ret, 0); 202 203 ret = HcfCipherCreate("SM4_128|OFB|PKCS7", &cipher); 204 ASSERT_EQ(ret, 0); 205 206 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 207 ASSERT_EQ(ret, 0); 208 209 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 210 ASSERT_EQ(ret, 0); 211 212 HcfObjDestroy((HcfObjectBase *)key); 213 HcfObjDestroy((HcfObjectBase *)cipher); 214 HcfObjDestroy((HcfObjectBase *)generator); 215 } 216 217 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest016, TestSize.Level0) 218 { 219 uint8_t cipherText[128] = {0}; 220 int cipherTextLen = 128; 221 222 HcfSymKeyGenerator *generator = nullptr; 223 HcfCipher *cipher = nullptr; 224 HcfSymKey *key = nullptr; 225 226 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 227 ASSERT_EQ(ret, 0); 228 229 ret = generator->generateSymKey(generator, &key); 230 ASSERT_EQ(ret, 0); 231 232 ret = HcfCipherCreate("SM4_128|CTR|NoPadding", &cipher); 233 ASSERT_EQ(ret, 0); 234 235 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 236 ASSERT_EQ(ret, 0); 237 238 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 239 ASSERT_EQ(ret, 0); 240 241 HcfObjDestroy((HcfObjectBase *)key); 242 HcfObjDestroy((HcfObjectBase *)cipher); 243 HcfObjDestroy((HcfObjectBase *)generator); 244 } 245 246 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest017, TestSize.Level0) 247 { 248 uint8_t cipherText[128] = {0}; 249 int cipherTextLen = 128; 250 251 HcfSymKeyGenerator *generator = nullptr; 252 HcfCipher *cipher = nullptr; 253 HcfSymKey *key = nullptr; 254 255 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 256 ASSERT_EQ(ret, 0); 257 258 ret = generator->generateSymKey(generator, &key); 259 ASSERT_EQ(ret, 0); 260 261 ret = HcfCipherCreate("SM4_128|CTR|PKCS5", &cipher); 262 ASSERT_EQ(ret, 0); 263 264 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 265 ASSERT_EQ(ret, 0); 266 267 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 268 ASSERT_EQ(ret, 0); 269 270 HcfObjDestroy((HcfObjectBase *)key); 271 HcfObjDestroy((HcfObjectBase *)cipher); 272 HcfObjDestroy((HcfObjectBase *)generator); 273 } 274 275 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest018, TestSize.Level0) 276 { 277 uint8_t cipherText[128] = {0}; 278 int cipherTextLen = 128; 279 280 HcfSymKeyGenerator *generator = nullptr; 281 HcfCipher *cipher = nullptr; 282 HcfSymKey *key = nullptr; 283 284 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 285 ASSERT_EQ(ret, 0); 286 287 ret = generator->generateSymKey(generator, &key); 288 ASSERT_EQ(ret, 0); 289 290 ret = HcfCipherCreate("SM4_128|CTR|PKCS7", &cipher); 291 ASSERT_EQ(ret, 0); 292 293 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 294 ASSERT_EQ(ret, 0); 295 296 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 297 ASSERT_EQ(ret, 0); 298 299 HcfObjDestroy((HcfObjectBase *)key); 300 HcfObjDestroy((HcfObjectBase *)cipher); 301 HcfObjDestroy((HcfObjectBase *)generator); 302 } 303 304 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest022, TestSize.Level0) 305 { 306 uint8_t cipherText[128] = {0}; 307 int cipherTextLen = 128; 308 309 HcfSymKeyGenerator *generator = nullptr; 310 HcfCipher *cipher = nullptr; 311 HcfSymKey *key = nullptr; 312 313 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 314 ASSERT_EQ(ret, 0); 315 316 ret = generator->generateSymKey(generator, &key); 317 ASSERT_EQ(ret, 0); 318 319 ret = HcfCipherCreate("SM4_128|CBC|NoPadding", &cipher); 320 ASSERT_EQ(ret, 0); 321 322 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 323 EXPECT_NE(ret, 0); 324 325 HcfObjDestroy((HcfObjectBase *)key); 326 HcfObjDestroy((HcfObjectBase *)cipher); 327 HcfObjDestroy((HcfObjectBase *)generator); 328 } 329 330 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest023, TestSize.Level0) 331 { 332 uint8_t cipherText[128] = {0}; 333 int cipherTextLen = 128; 334 335 HcfSymKeyGenerator *generator = nullptr; 336 HcfCipher *cipher = nullptr; 337 HcfSymKey *key = nullptr; 338 339 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 340 ASSERT_EQ(ret, 0); 341 342 ret = generator->generateSymKey(generator, &key); 343 ASSERT_EQ(ret, 0); 344 345 ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher); 346 ASSERT_EQ(ret, 0); 347 348 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 349 ASSERT_EQ(ret, 0); 350 351 ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); 352 ASSERT_EQ(ret, 0); 353 354 HcfObjDestroy((HcfObjectBase *)key); 355 HcfObjDestroy((HcfObjectBase *)cipher); 356 HcfObjDestroy((HcfObjectBase *)generator); 357 } 358 359 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest024, TestSize.Level0) 360 { 361 uint8_t cipherText[128] = {0}; 362 int cipherTextLen = 128; 363 364 HcfSymKeyGenerator *generator = nullptr; 365 HcfCipher *cipher = nullptr; 366 HcfSymKey *key = nullptr; 367 368 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 369 ASSERT_EQ(ret, 0); 370 371 ret = generator->generateSymKey(generator, &key); 372 ASSERT_EQ(ret, 0); 373 374 ret = HcfCipherCreate("SM4_128|CBC|PKCS7", &cipher); 375 ASSERT_EQ(ret, 0); 376 377 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 378 ASSERT_EQ(ret, 0); 379 380 ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); 381 ASSERT_EQ(ret, 0); 382 383 HcfObjDestroy((HcfObjectBase *)key); 384 HcfObjDestroy((HcfObjectBase *)cipher); 385 HcfObjDestroy((HcfObjectBase *)generator); 386 } 387 388 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest025, TestSize.Level0) 389 { 390 uint8_t cipherText[128] = {0}; 391 int cipherTextLen = 128; 392 393 HcfSymKeyGenerator *generator = nullptr; 394 HcfCipher *cipher = nullptr; 395 HcfSymKey *key = nullptr; 396 397 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 398 ASSERT_EQ(ret, 0); 399 400 ret = generator->generateSymKey(generator, &key); 401 ASSERT_EQ(ret, 0); 402 403 ret = HcfCipherCreate("SM4_128|OFB|NoPadding", &cipher); 404 ASSERT_EQ(ret, 0); 405 406 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 407 ASSERT_EQ(ret, 0); 408 409 ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); 410 ASSERT_EQ(ret, 0); 411 412 HcfObjDestroy((HcfObjectBase *)key); 413 HcfObjDestroy((HcfObjectBase *)cipher); 414 HcfObjDestroy((HcfObjectBase *)generator); 415 } 416 417 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest026, TestSize.Level0) 418 { 419 uint8_t cipherText[128] = {0}; 420 int cipherTextLen = 128; 421 422 HcfSymKeyGenerator *generator = nullptr; 423 HcfCipher *cipher = nullptr; 424 HcfSymKey *key = nullptr; 425 426 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 427 ASSERT_EQ(ret, 0); 428 429 ret = generator->generateSymKey(generator, &key); 430 ASSERT_EQ(ret, 0); 431 432 ret = HcfCipherCreate("SM4_128|OFB|PKCS5", &cipher); 433 ASSERT_EQ(ret, 0); 434 435 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 436 ASSERT_EQ(ret, 0); 437 438 ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); 439 ASSERT_EQ(ret, 0); 440 441 HcfObjDestroy((HcfObjectBase *)key); 442 HcfObjDestroy((HcfObjectBase *)cipher); 443 HcfObjDestroy((HcfObjectBase *)generator); 444 } 445 446 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest027, TestSize.Level0) 447 { 448 uint8_t cipherText[128] = {0}; 449 int cipherTextLen = 128; 450 451 HcfSymKeyGenerator *generator = nullptr; 452 HcfCipher *cipher = nullptr; 453 HcfSymKey *key = nullptr; 454 455 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 456 ASSERT_EQ(ret, 0); 457 458 ret = generator->generateSymKey(generator, &key); 459 ASSERT_EQ(ret, 0); 460 461 ret = HcfCipherCreate("SM4_128|OFB|PKCS7", &cipher); 462 ASSERT_EQ(ret, 0); 463 464 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 465 ASSERT_EQ(ret, 0); 466 467 ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); 468 ASSERT_EQ(ret, 0); 469 470 HcfObjDestroy((HcfObjectBase *)key); 471 HcfObjDestroy((HcfObjectBase *)cipher); 472 HcfObjDestroy((HcfObjectBase *)generator); 473 } 474 475 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest044, TestSize.Level0) 476 { 477 int ret = 0; 478 HcfCipher *cipher = nullptr; 479 480 ret = HcfCipherCreate("", &cipher); 481 if (ret != 0) { 482 LOGE("HcfCipherCreate failed!"); 483 } 484 485 HcfObjDestroy(cipher); 486 EXPECT_NE(ret, 0); 487 } 488 489 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest045, TestSize.Level0) 490 { 491 int ret = 0; 492 HcfCipher *cipher = nullptr; 493 494 ret = HcfCipherCreate(nullptr, &cipher); 495 if (ret != 0) { 496 LOGE("HcfCipherCreate failed!"); 497 } 498 499 HcfObjDestroy(cipher); 500 EXPECT_NE(ret, 0); 501 } 502 503 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest052, TestSize.Level0) 504 { 505 int ret = 0; 506 HcfCipher *cipher = nullptr; 507 508 ret = HcfCipherCreate("SM4_128|CCC|NoPadding", &cipher); 509 if (ret != 0) { 510 LOGE("HcfCipherCreate failed! Should not select CCC for SM4 generator."); 511 } 512 513 HcfObjDestroy(cipher); 514 EXPECT_NE(ret, 0); 515 } 516 517 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest055, TestSize.Level0) 518 { 519 uint8_t iv[AES_IV_LEN] = { 0 }; 520 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 521 int cipherTextLen = CIPHER_TEXT_LEN; 522 523 HcfIvParamsSpec ivSpec = {}; 524 HcfCipher *cipher = nullptr; 525 HcfSymKey *key = nullptr; 526 ivSpec.iv.data = iv; 527 ivSpec.iv.len = AES_IV_LEN; 528 529 int ret = GenerateSymKeyForSm4("SM4_128", &key); 530 ASSERT_EQ(ret, 0); 531 532 ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher); 533 ASSERT_EQ(ret, 0); 534 535 ret = Sm4Encrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); 536 ASSERT_EQ(ret, 0); 537 538 ret = Sm4Decrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); 539 ASSERT_EQ(ret, 0); 540 541 HcfObjDestroy(key); 542 HcfObjDestroy(cipher); 543 } 544 545 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest057, TestSize.Level0) 546 { 547 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 548 int cipherTextLen = CIPHER_TEXT_LEN; 549 HcfCipher *cipher = nullptr; 550 HcfSymKey *key = nullptr; 551 552 int ret = GenerateSymKeyForSm4("SM4_128", &key); 553 ASSERT_EQ(ret, 0); 554 555 ret = HcfCipherCreate("SM4_128|PKCS5", &cipher); 556 ASSERT_EQ(ret, 0); 557 558 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 559 ASSERT_EQ(ret, 0); 560 561 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 562 ASSERT_EQ(ret, 0); 563 564 HcfObjDestroy(key); 565 HcfObjDestroy(cipher); 566 } 567 568 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest060, TestSize.Level0) 569 { 570 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 571 int cipherTextLen = CIPHER_TEXT_LEN; 572 HcfCipher *cipher = nullptr; 573 HcfSymKey *key = nullptr; 574 575 int ret = GenerateSymKeyForSm4("SM4_128", &key); 576 ASSERT_EQ(ret, 0); 577 578 // allow input without encryption mode. It will pick the last PKCS5, and use default aes128ecb. 579 ret = HcfCipherCreate("SM4_128|NoPadding|PKCS5", &cipher); 580 ASSERT_EQ(ret, 0); 581 582 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 583 ASSERT_EQ(ret, 0); 584 585 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 586 ASSERT_EQ(ret, 0); 587 588 HcfObjDestroy(key); 589 HcfObjDestroy(cipher); 590 } 591 592 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest062, TestSize.Level0) 593 { 594 HcfResult ret = HCF_SUCCESS; 595 596 ret = HcfCipherSm4GeneratorSpiCreate(nullptr, nullptr); 597 EXPECT_NE(ret, HCF_SUCCESS); 598 599 HcfCipherGeneratorSpi *cipher = nullptr; 600 CipherAttr params = { 601 .algo = HCF_ALG_SM4, 602 .mode = HCF_ALG_MODE_ECB, 603 .paddingMode = HCF_ALG_PADDING_PKCS5, 604 }; 605 ret = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 606 EXPECT_EQ(ret, HCF_SUCCESS); 607 608 ret = cipher->init(nullptr, ENCRYPT_MODE, nullptr, nullptr); 609 EXPECT_EQ(ret, HCF_INVALID_PARAMS); 610 611 ret = cipher->update(nullptr, nullptr, nullptr); 612 EXPECT_EQ(ret, HCF_INVALID_PARAMS); 613 614 ret = cipher->doFinal(nullptr, nullptr, nullptr); 615 EXPECT_EQ(ret, HCF_INVALID_PARAMS); 616 617 HcfBlob dataArray = { .data = nullptr, .len = 0 }; 618 ret = cipher->getCipherSpecString(nullptr, OAEP_MGF1_MD_STR, nullptr); 619 EXPECT_EQ(ret, HCF_NOT_SUPPORT); 620 621 ret = cipher->getCipherSpecUint8Array(nullptr, OAEP_MGF1_MD_STR, &dataArray); 622 EXPECT_EQ(ret, HCF_NOT_SUPPORT); 623 624 HcfBlob dataUint8 = { .data = nullptr, .len = 0 }; 625 ret = cipher->setCipherSpecUint8Array(nullptr, OAEP_MGF1_MD_STR, dataUint8); 626 EXPECT_EQ(ret, HCF_NOT_SUPPORT); 627 628 (void)cipher->base.destroy(nullptr); 629 630 HcfObjDestroy(cipher); 631 HcfBlobDataFree(&dataArray); 632 } 633 634 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest063, TestSize.Level0) 635 { 636 HcfResult res = HCF_SUCCESS; 637 HcfCipherGeneratorSpi *cipher = nullptr; 638 CipherAttr params = { 639 .algo = HCF_ALG_SM4, 640 .md = HCF_OPENSSL_DIGEST_SM3, 641 }; 642 res = HcfCipherSm4GeneratorSpiCreate(¶ms, nullptr); 643 EXPECT_NE(res, HCF_SUCCESS); 644 HcfObjDestroy(cipher); 645 } 646 647 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest065, TestSize.Level0) 648 { 649 int retkey = 0; 650 HcfResult res = HCF_SUCCESS; 651 HcfCipherGeneratorSpi *cipher = nullptr; 652 HcfSymKey *key = nullptr; 653 CipherAttr params = { 654 .algo = HCF_ALG_SM4, 655 .md = HCF_OPENSSL_DIGEST_SM3, 656 }; 657 retkey = GenerateSymKeyForSm4("SM4_128", &key); 658 EXPECT_EQ(retkey, 0); 659 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 660 EXPECT_EQ(res, HCF_SUCCESS); 661 res = cipher->init(nullptr, ENCRYPT_MODE, (HcfKey *)key, nullptr); 662 ASSERT_EQ(res, HCF_INVALID_PARAMS); 663 664 HcfObjDestroy(key); 665 HcfObjDestroy(cipher); 666 } 667 668 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest066, TestSize.Level0) 669 { 670 HcfResult res = HCF_SUCCESS; 671 HcfCipherGeneratorSpi *cipher = nullptr; 672 CipherAttr params = { 673 .algo = HCF_ALG_SM4, 674 .md = HCF_OPENSSL_DIGEST_SM3, 675 }; 676 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 677 EXPECT_EQ(res, HCF_SUCCESS); 678 res = cipher->init(cipher, ENCRYPT_MODE, nullptr, nullptr); 679 ASSERT_EQ(res, HCF_INVALID_PARAMS); 680 681 HcfObjDestroy(cipher); 682 } 683 684 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest067, TestSize.Level0) 685 { 686 int retkey = 0; 687 HcfResult res = HCF_SUCCESS; 688 HcfCipherGeneratorSpi *cipher = nullptr; 689 HcfSymKey *key = nullptr; 690 CipherAttr params = { 691 .algo = HCF_ALG_SM4, 692 .md = HCF_OPENSSL_DIGEST_SM3, 693 }; 694 retkey = GenerateSymKeyForSm4("SM4_128", &key); 695 EXPECT_EQ(retkey, 0); 696 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 697 EXPECT_EQ(res, HCF_SUCCESS); 698 cipher->base.destroy(nullptr); 699 700 HcfObjDestroy(key); 701 HcfObjDestroy(cipher); 702 } 703 704 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest069, TestSize.Level0) 705 { 706 int retkey = 0; 707 HcfResult res = HCF_SUCCESS; 708 HcfCipherGeneratorSpi *cipher = nullptr; 709 HcfSymKey *key = nullptr; 710 CipherAttr params = { 711 .algo = HCF_ALG_SM4, 712 .md = HCF_OPENSSL_DIGEST_SM3, 713 }; 714 715 uint8_t plan[] = "12312123123"; 716 HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)}; 717 718 retkey = GenerateSymKeyForSm4("SM4_128", &key); 719 EXPECT_EQ(retkey, 0); 720 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 721 EXPECT_EQ(res, HCF_SUCCESS); 722 HcfBlob blob; 723 res = cipher->update(nullptr, &input, &blob); 724 EXPECT_NE(res, 0); 725 726 HcfObjDestroy(key); 727 HcfObjDestroy(cipher); 728 } 729 730 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest070, TestSize.Level0) 731 { 732 int retkey = 0; 733 HcfResult res = HCF_SUCCESS; 734 HcfCipherGeneratorSpi *cipher = nullptr; 735 HcfSymKey *key = nullptr; 736 CipherAttr params = { 737 .algo = HCF_ALG_SM4, 738 .md = HCF_OPENSSL_DIGEST_SM3, 739 }; 740 741 retkey = GenerateSymKeyForSm4("SM4_128", &key); 742 EXPECT_EQ(retkey, 0); 743 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 744 EXPECT_EQ(res, HCF_SUCCESS); 745 HcfBlob blob; 746 res = cipher->update(cipher, nullptr, &blob); 747 EXPECT_NE(res, 0); 748 749 HcfObjDestroy(key); 750 HcfObjDestroy(cipher); 751 } 752 753 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest071, TestSize.Level0) 754 { 755 int retkey = 0; 756 HcfResult res = HCF_SUCCESS; 757 HcfCipherGeneratorSpi *cipher = nullptr; 758 HcfSymKey *key = nullptr; 759 CipherAttr params = { 760 .algo = HCF_ALG_SM4, 761 .md = HCF_OPENSSL_DIGEST_SM3, 762 }; 763 764 uint8_t plan[] = "12312123123"; 765 HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)}; 766 767 retkey = GenerateSymKeyForSm4("SM4_128", &key); 768 EXPECT_EQ(retkey, 0); 769 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 770 EXPECT_EQ(res, HCF_SUCCESS); 771 res = cipher->update(cipher, &input, nullptr); 772 EXPECT_NE(res, 0); 773 774 HcfObjDestroy(key); 775 HcfObjDestroy(cipher); 776 } 777 778 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest073, TestSize.Level0) 779 { 780 HcfResult res = HCF_SUCCESS; 781 HcfCipherGeneratorSpi *cipher = nullptr; 782 CipherAttr params = { 783 .algo = HCF_ALG_SM2, 784 .md = HCF_OPENSSL_DIGEST_SM3, 785 }; 786 uint8_t plan[] = "12312123123"; 787 HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)}; 788 HcfBlob out = { .data = nullptr, .len = 0 }; 789 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 790 EXPECT_EQ(res, HCF_SUCCESS); 791 res = cipher->doFinal(nullptr, &input, &out); 792 ASSERT_EQ(res, HCF_INVALID_PARAMS); 793 794 HcfObjDestroy(cipher); 795 } 796 797 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest074, TestSize.Level0) 798 { 799 HcfResult res = HCF_SUCCESS; 800 HcfCipherGeneratorSpi *cipher = nullptr; 801 CipherAttr params = { 802 .algo = HCF_ALG_SM2, 803 .md = HCF_OPENSSL_DIGEST_SM3, 804 }; 805 uint8_t plan[] = "12312123123"; 806 HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)}; 807 808 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 809 EXPECT_EQ(res, HCF_SUCCESS); 810 res = cipher->doFinal(cipher, &input, nullptr); 811 ASSERT_EQ(res, HCF_INVALID_PARAMS); 812 813 HcfObjDestroy(cipher); 814 } 815 816 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest075, TestSize.Level0) 817 { 818 HcfResult res = HCF_SUCCESS; 819 HcfCipherGeneratorSpi *cipher = nullptr; 820 CipherAttr params = { 821 .algo = HCF_ALG_SM2, 822 .md = HCF_OPENSSL_DIGEST_SM3, 823 }; 824 HcfBlob input = { 825 .data = nullptr, 826 .len = 12 827 }; 828 HcfBlob out = { .data = nullptr, .len = 0 }; 829 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 830 EXPECT_EQ(res, HCF_SUCCESS); 831 res = cipher->doFinal(cipher, &input, &out); 832 ASSERT_EQ(res, HCF_INVALID_PARAMS); 833 834 HcfObjDestroy(cipher); 835 } 836 837 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest077, TestSize.Level0) 838 { 839 HcfResult res = HCF_SUCCESS; 840 HcfCipherGeneratorSpi *cipher = nullptr; 841 CipherAttr params = { 842 .algo = HCF_ALG_SM2, 843 .md = HCF_OPENSSL_DIGEST_SM3, 844 }; 845 uint8_t plan[] = "12312123123"; 846 HcfBlob input = { 847 .data = (uint8_t *)plan, 848 .len = -1 849 }; 850 HcfBlob out = { .data = nullptr, .len = 0 }; 851 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 852 EXPECT_EQ(res, HCF_SUCCESS); 853 res = cipher->doFinal(cipher, &input, &out); 854 ASSERT_EQ(res, HCF_INVALID_PARAMS); 855 856 HcfObjDestroy(cipher); 857 } 858 859 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest078, TestSize.Level0) 860 { 861 HcfResult res = HCF_SUCCESS; 862 HcfCipherGeneratorSpi *cipher = nullptr; 863 CipherAttr params = { 864 .algo = HCF_ALG_SM2, 865 .md = HCF_OPENSSL_DIGEST_SM3, 866 }; 867 HcfBlob input = { 868 .data = nullptr, 869 .len = 12 870 }; 871 HcfBlob out = { .data = nullptr, .len = 0 }; 872 res = HcfCipherSm4GeneratorSpiCreate(¶ms, &cipher); 873 EXPECT_EQ(res, HCF_SUCCESS); 874 res = cipher->update(cipher, &input, &out); 875 ASSERT_EQ(res, HCF_INVALID_PARAMS); 876 877 HcfObjDestroy(cipher); 878 } 879 880 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest079, TestSize.Level0) 881 { 882 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 883 int cipherTextLen = CIPHER_TEXT_LEN; 884 HcfCipher *cipher = nullptr; 885 HcfSymKey *key = nullptr; 886 887 int ret = GenerateSymKeyForSm4("AES256", &key); 888 ASSERT_EQ(ret, 0); 889 890 ret = HcfCipherCreate("SM4_128|CBC|PKCS5", &cipher); 891 ASSERT_EQ(ret, 0); 892 893 // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key. 894 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 895 ASSERT_NE(ret, 0); 896 897 HcfObjDestroy(key); 898 HcfObjDestroy(cipher); 899 } 900 901 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest080, TestSize.Level0) 902 { 903 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 904 int cipherTextLen = CIPHER_TEXT_LEN; 905 HcfCipher *cipher = nullptr; 906 HcfSymKey *key = nullptr; 907 908 int ret = GenerateSymKeyForSm4("AES256", &key); 909 ASSERT_EQ(ret, 0); 910 911 ret = HcfCipherCreate("SM4_128|CTR|PKCS5", &cipher); 912 ASSERT_EQ(ret, 0); 913 914 // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key. 915 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 916 ASSERT_NE(ret, 0); 917 918 HcfObjDestroy(key); 919 HcfObjDestroy(cipher); 920 } 921 922 HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest081, TestSize.Level0) 923 { 924 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 925 int cipherTextLen = CIPHER_TEXT_LEN; 926 HcfCipher *cipher = nullptr; 927 HcfSymKey *key = nullptr; 928 929 int ret = GenerateSymKeyForSm4("AES256", &key); 930 ASSERT_EQ(ret, 0); 931 932 ret = HcfCipherCreate("SM4_128|OFB|PKCS5", &cipher); 933 ASSERT_EQ(ret, 0); 934 935 // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key. 936 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 937 ASSERT_NE(ret, 0); 938 939 HcfObjDestroy(key); 940 HcfObjDestroy(cipher); 941 } 942 }