1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 #include <fstream> 18 #include <iostream> 19 #include "securec.h" 20 #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 CryptoSM4EcbCipherTest : 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(CryptoSM4EcbCipherTest, CryptoSm4CipherTest001, 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|ECB|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(CryptoSM4EcbCipherTest, CryptoSm4CipherTest002, 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|ECB|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(CryptoSM4EcbCipherTest, CryptoSm4CipherTest003, 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|ECB|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(CryptoSM4EcbCipherTest, CryptoSm4CipherTest019, 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|ECB|NoPadding", &cipher); 146 ASSERT_EQ(ret, 0); 147 148 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 149 ASSERT_NE(ret, 0); 150 151 HcfObjDestroy((HcfObjectBase *)key); 152 HcfObjDestroy((HcfObjectBase *)cipher); 153 HcfObjDestroy((HcfObjectBase *)generator); 154 } 155 156 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest020, TestSize.Level0) 157 { 158 uint8_t cipherText[128] = {0}; 159 int cipherTextLen = 128; 160 161 HcfSymKeyGenerator *generator = nullptr; 162 HcfCipher *cipher = nullptr; 163 HcfSymKey *key = nullptr; 164 165 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 166 ASSERT_EQ(ret, 0); 167 168 ret = generator->generateSymKey(generator, &key); 169 ASSERT_EQ(ret, 0); 170 171 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 172 ASSERT_EQ(ret, 0); 173 174 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 175 ASSERT_EQ(ret, 0); 176 177 ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); 178 ASSERT_EQ(ret, 0); 179 180 HcfObjDestroy((HcfObjectBase *)key); 181 HcfObjDestroy((HcfObjectBase *)cipher); 182 HcfObjDestroy((HcfObjectBase *)generator); 183 } 184 185 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest021, TestSize.Level0) 186 { 187 uint8_t cipherText[128] = {0}; 188 int cipherTextLen = 128; 189 190 HcfSymKeyGenerator *generator = nullptr; 191 HcfCipher *cipher = nullptr; 192 HcfSymKey *key = nullptr; 193 194 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 195 ASSERT_EQ(ret, 0); 196 197 ret = generator->generateSymKey(generator, &key); 198 ASSERT_EQ(ret, 0); 199 200 ret = HcfCipherCreate("SM4_128|ECB|PKCS7", &cipher); 201 ASSERT_EQ(ret, 0); 202 203 ret = Sm4NoUpdateEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 204 ASSERT_EQ(ret, 0); 205 206 ret = Sm4NoUpdateDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); 207 ASSERT_EQ(ret, 0); 208 209 HcfObjDestroy((HcfObjectBase *)key); 210 HcfObjDestroy((HcfObjectBase *)cipher); 211 HcfObjDestroy((HcfObjectBase *)generator); 212 } 213 214 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest034, TestSize.Level0) 215 { 216 HcfCipher *cipher = nullptr; 217 HcfSymKey *key = nullptr; 218 219 int ret = GenerateSm4SymKey(&key); 220 ASSERT_EQ(ret, 0); 221 222 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 223 ASSERT_EQ(ret, 0); 224 225 ret = cipher->init(nullptr, ENCRYPT_MODE, &(key->key), nullptr); 226 ASSERT_NE(ret, 0); 227 228 HcfObjDestroy(key); 229 HcfObjDestroy(cipher); 230 } 231 232 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest035, TestSize.Level0) 233 { 234 HcfCipher *cipher = nullptr; 235 HcfSymKey *key = nullptr; 236 237 int ret = GenerateSm4SymKey(&key); 238 ASSERT_EQ(ret, 0); 239 240 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 241 ASSERT_EQ(ret, 0); 242 243 ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast<HcfKey *>(cipher), nullptr); 244 ASSERT_NE(ret, 0); 245 246 HcfObjDestroy(key); 247 HcfObjDestroy(cipher); 248 } 249 250 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest036, TestSize.Level0) 251 { 252 HcfCipher *cipher = nullptr; 253 HcfSymKey *key = nullptr; 254 uint8_t plainText[] = "this is test!"; 255 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; 256 HcfBlob output = { .data = nullptr, .len = 0 }; 257 258 int ret = GenerateSm4SymKey(&key); 259 ASSERT_EQ(ret, 0); 260 261 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 262 ASSERT_EQ(ret, 0); 263 264 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); 265 ASSERT_EQ(ret, 0); 266 267 ret = cipher->update(nullptr, &input, &output); 268 ASSERT_NE(ret, 0); 269 270 HcfObjDestroy(key); 271 HcfObjDestroy(cipher); 272 if (output.data != nullptr) { 273 HcfFree(output.data); 274 output.data = nullptr; 275 } 276 } 277 278 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest037, TestSize.Level0) 279 { 280 HcfCipher *cipher = nullptr; 281 HcfSymKey *key = nullptr; 282 uint8_t plainText[] = "this is test!"; 283 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; 284 HcfBlob output = { .data = nullptr, .len = 0 }; 285 286 int ret = GenerateSm4SymKey(&key); 287 ASSERT_EQ(ret, 0); 288 289 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 290 ASSERT_EQ(ret, 0); 291 292 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); 293 ASSERT_EQ(ret, 0); 294 295 ret = cipher->update(reinterpret_cast<HcfCipher *>(key), &input, &output); 296 ASSERT_NE(ret, 0); 297 298 HcfObjDestroy(key); 299 HcfObjDestroy(cipher); 300 if (output.data != nullptr) { 301 HcfFree(output.data); 302 output.data = nullptr; 303 } 304 } 305 306 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest038, TestSize.Level0) 307 { 308 HcfCipher *cipher = nullptr; 309 HcfSymKey *key = nullptr; 310 uint8_t plainText[] = "this is test!"; 311 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; 312 HcfBlob output = { .data = nullptr, .len = 0 }; 313 314 int ret = GenerateSm4SymKey(&key); 315 ASSERT_EQ(ret, 0); 316 317 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 318 ASSERT_EQ(ret, 0); 319 320 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); 321 ASSERT_EQ(ret, 0); 322 323 ret = cipher->doFinal(nullptr, &input, &output); 324 ASSERT_NE(ret, 0); 325 326 HcfObjDestroy(key); 327 HcfObjDestroy(cipher); 328 if (output.data != nullptr) { 329 HcfFree(output.data); 330 output.data = nullptr; 331 } 332 } 333 334 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest039, TestSize.Level0) 335 { 336 HcfCipher *cipher = nullptr; 337 HcfSymKey *key = nullptr; 338 uint8_t plainText[] = "this is test!"; 339 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; 340 HcfBlob output = { .data = nullptr, .len = 0 }; 341 342 int ret = GenerateSm4SymKey(&key); 343 ASSERT_EQ(ret, 0); 344 345 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 346 ASSERT_EQ(ret, 0); 347 348 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); 349 ASSERT_EQ(ret, 0); 350 351 ret = cipher->doFinal(reinterpret_cast<HcfCipher *>(key), &input, &output); 352 ASSERT_NE(ret, 0); 353 354 HcfObjDestroy(key); 355 HcfObjDestroy(cipher); 356 if (output.data != nullptr) { 357 HcfFree(output.data); 358 output.data = nullptr; 359 } 360 } 361 362 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest046, TestSize.Level0) 363 { 364 HcfSymKeyGenerator *generator = nullptr; 365 HcfCipher *cipher = nullptr; 366 367 int ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 368 ASSERT_EQ(ret, 0); 369 370 ret = cipher->init(cipher, ENCRYPT_MODE, nullptr, nullptr); 371 ASSERT_NE(ret, 0); 372 373 HcfObjDestroy(cipher); 374 HcfObjDestroy(generator); 375 } 376 377 HWTEST_F(CryptoSM4EcbCipherTest, CryptoAesCipherTest048, TestSize.Level0) 378 { 379 HcfCipher *cipher = nullptr; 380 HcfSymKey *key = nullptr; 381 uint8_t plainText[] = "this is test!"; 382 HcfBlob input = { .data = plainText, .len = 0 }; 383 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 384 int cipherTextLen = CIPHER_TEXT_LEN; 385 386 int ret = GenerateSymKeyForSm4("SM4_128", &key); 387 ASSERT_EQ(ret, 0); 388 389 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 390 ASSERT_EQ(ret, 0); 391 392 ret = Sm4EncryptWithInput(cipher, key, &input, cipherText, &cipherTextLen); 393 ASSERT_EQ(ret, 0); 394 395 ret = Sm4DecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen); 396 ASSERT_EQ(ret, 0); 397 398 HcfObjDestroy(key); 399 HcfObjDestroy(cipher); 400 } 401 402 HWTEST_F(CryptoSM4EcbCipherTest, CryptoAesCipherTest049, TestSize.Level0) 403 { 404 HcfCipher *cipher = nullptr; 405 HcfSymKey *key = nullptr; 406 HcfBlob input = { .data = nullptr, .len = 0 }; 407 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 408 int cipherTextLen = CIPHER_TEXT_LEN; 409 410 int ret = GenerateSymKeyForSm4("SM4_128", &key); 411 ASSERT_EQ(ret, 0); 412 413 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 414 ASSERT_EQ(ret, 0); 415 416 ret = Sm4EncryptWithInput(cipher, key, &input, cipherText, &cipherTextLen); 417 ASSERT_EQ(ret, 0); 418 419 ret = Sm4DecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen); 420 ASSERT_EQ(ret, 0); 421 422 HcfObjDestroy(key); 423 HcfObjDestroy(cipher); 424 } 425 426 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest053, TestSize.Level0) 427 { 428 HcfSymKeyGenerator *generator = nullptr; 429 HcfSymKey *key = nullptr; 430 HcfCipher *cipher = nullptr; 431 432 int ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 433 ASSERT_EQ(ret, 0); 434 435 ret = generator->generateSymKey(generator, &key); 436 ASSERT_EQ(ret, 0); 437 438 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 439 ASSERT_EQ(ret, 0); 440 441 ret = cipher->init(reinterpret_cast<HcfCipher *>(generator), ENCRYPT_MODE, &(key->key), nullptr); 442 ASSERT_NE(ret, 0); 443 444 HcfObjDestroy(key); 445 HcfObjDestroy(cipher); 446 HcfObjDestroy(generator); 447 } 448 449 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest054, TestSize.Level0) 450 { 451 HcfCipher *cipher = nullptr; 452 HcfSymKey *key = nullptr; 453 uint8_t plainText[] = "this is test!"; 454 HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; 455 456 int ret = GenerateSymKeyForSm4("SM4_128", &key); 457 ASSERT_EQ(ret, 0); 458 459 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 460 ASSERT_EQ(ret, 0); 461 462 ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); 463 ASSERT_EQ(ret, 0); 464 465 ret = cipher->doFinal(cipher, &input, nullptr); 466 ASSERT_NE(ret, 0); 467 468 HcfObjDestroy(key); 469 HcfObjDestroy(cipher); 470 } 471 472 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest058, TestSize.Level0) 473 { 474 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 475 int cipherTextLen = CIPHER_TEXT_LEN; 476 HcfCipher *cipher = nullptr; 477 HcfSymKey *key = nullptr; 478 479 int ret = GenerateSymKeyForSm4("SM4_128", &key); 480 ASSERT_EQ(ret, 0); 481 482 // allow input with more than one padding mode. It will pick the last PKCS5. 483 ret = HcfCipherCreate("SM4_128|ECB|NoPadding|PKCS5", &cipher); 484 ASSERT_EQ(ret, 0); 485 486 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 487 ASSERT_EQ(ret, 0); 488 489 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 490 ASSERT_EQ(ret, 0); 491 492 HcfObjDestroy(key); 493 HcfObjDestroy(cipher); 494 } 495 496 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest059, TestSize.Level0) 497 { 498 HcfSymKeyGenerator *generator = nullptr; 499 HcfSymKey *key = nullptr; 500 HcfCipher *cipher = nullptr; 501 502 int ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 503 ASSERT_EQ(ret, 0); 504 505 ret = HcfSymKeyGeneratorCreate("SM4_128", &generator); 506 ASSERT_EQ(ret, 0); 507 508 ret = generator->generateSymKey(reinterpret_cast<HcfSymKeyGenerator *>(cipher), &key); 509 ASSERT_NE(ret, 0); 510 511 HcfObjDestroy(key); 512 HcfObjDestroy(generator); 513 HcfObjDestroy(cipher); 514 } 515 516 HWTEST_F(CryptoSM4EcbCipherTest, CryptoSm4CipherTest061, TestSize.Level0) 517 { 518 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 519 int cipherTextLen = CIPHER_TEXT_LEN; 520 HcfCipher *cipher = nullptr; 521 HcfSymKey *key = nullptr; 522 523 int ret = GenerateSymKeyForSm4("AES256", &key); 524 ASSERT_EQ(ret, 0); 525 526 ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); 527 ASSERT_EQ(ret, 0); 528 529 // It is not allowed that AES128 in key is smaller AES256 in cipher. -> now only use the size of input key. 530 ret = Sm4Encrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 531 ASSERT_NE(ret, 0); 532 533 ret = Sm4Decrypt(cipher, key, nullptr, cipherText, cipherTextLen); 534 ASSERT_NE(ret, 0); 535 536 HcfObjDestroy(key); 537 HcfObjDestroy(cipher); 538 } 539 }