1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <gtest/gtest.h> 17 #include <fstream> 18 #include <iostream> 19 #include "securec.h" 20 21 #include "aes_common.h" 22 #include "aes_openssl.h" 23 #include "blob.h" 24 #include "cipher.h" 25 #include "detailed_iv_params.h" 26 #include "detailed_gcm_params.h" 27 #include "detailed_ccm_params.h" 28 #include "log.h" 29 #include "memory.h" 30 #include "sym_common_defines.h" 31 #include "sym_key_generator.h" 32 33 using namespace std; 34 using namespace testing::ext; 35 36 namespace { 37 class CryptoAesCcmCipherTest : public testing::Test { 38 public: SetUpTestCase()39 static void SetUpTestCase() {}; TearDownTestCase()40 static void TearDownTestCase() {}; SetUp()41 void SetUp() {}; TearDown()42 void TearDown() {}; 43 }; 44 45 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest001, TestSize.Level0) 46 { 47 int ret = 0; 48 uint8_t aad[8] = {0}; 49 uint8_t tag[12] = {0}; 50 uint8_t iv[7] = {0}; 51 uint8_t cipherText[128] = {0}; 52 int cipherTextLen = 128; 53 54 HcfCipher *cipher = nullptr; 55 HcfSymKey *key = nullptr; 56 HcfCcmParamsSpec spec = {}; 57 spec.aad.data = aad; 58 spec.aad.len = sizeof(aad); 59 spec.tag.data = tag; 60 spec.tag.len = sizeof(tag); 61 spec.iv.data = iv; 62 spec.iv.len = sizeof(iv); 63 64 ret = GenerateSymKey("AES128", &key); 65 ASSERT_EQ(ret, 0); 66 67 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); 68 ASSERT_EQ(ret, 0); 69 70 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen); 71 ASSERT_EQ(ret, 0); 72 73 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12); 74 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 75 cipherTextLen -= 12; 76 77 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen); 78 ASSERT_EQ(ret, 0); 79 80 HcfObjDestroy((HcfObjectBase *)key); 81 HcfObjDestroy((HcfObjectBase *)cipher); 82 } 83 84 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest002, TestSize.Level0) 85 { 86 int ret = 0; 87 uint8_t aad[8] = {0}; 88 uint8_t tag[12] = {0}; 89 uint8_t iv[7] = {0}; 90 uint8_t cipherText[128] = {0}; 91 int cipherTextLen = 128; 92 93 HcfCipher *cipher = nullptr; 94 HcfSymKey *key = nullptr; 95 HcfCcmParamsSpec spec = {}; 96 spec.aad.data = aad; 97 spec.aad.len = sizeof(aad); 98 spec.tag.data = tag; 99 spec.tag.len = sizeof(tag); 100 spec.iv.data = iv; 101 spec.iv.len = sizeof(iv); 102 103 ret = GenerateSymKey("AES128", &key); 104 ASSERT_EQ(ret, 0); 105 106 ret = HcfCipherCreate("AES128|CCM|PKCS5", &cipher); 107 ASSERT_EQ(ret, 0); 108 109 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen); 110 ASSERT_EQ(ret, 0); 111 112 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12); 113 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 114 cipherTextLen -= 12; 115 116 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen); 117 ASSERT_EQ(ret, 0); 118 119 HcfObjDestroy((HcfObjectBase *)key); 120 HcfObjDestroy((HcfObjectBase *)cipher); 121 } 122 123 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest003, TestSize.Level0) 124 { 125 int ret = 0; 126 uint8_t aad[8] = {0}; 127 uint8_t tag[12] = {0}; 128 uint8_t iv[7] = {0}; 129 uint8_t cipherText[128] = {0}; 130 int cipherTextLen = 128; 131 132 HcfCipher *cipher = nullptr; 133 HcfSymKey *key = nullptr; 134 HcfCcmParamsSpec spec = {}; 135 spec.aad.data = aad; 136 spec.aad.len = sizeof(aad); 137 spec.tag.data = tag; 138 spec.tag.len = sizeof(tag); 139 spec.iv.data = iv; 140 spec.iv.len = sizeof(iv); 141 142 ret = GenerateSymKey("AES128", &key); 143 ASSERT_EQ(ret, 0); 144 145 ret = HcfCipherCreate("AES128|CCM|PKCS7", &cipher); 146 ASSERT_EQ(ret, 0); 147 148 ret = AesEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen); 149 ASSERT_EQ(ret, 0); 150 151 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12); 152 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 153 cipherTextLen -= 12; 154 155 ret = AesDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen); 156 ASSERT_EQ(ret, 0); 157 158 HcfObjDestroy((HcfObjectBase *)key); 159 HcfObjDestroy((HcfObjectBase *)cipher); 160 } 161 162 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest004, TestSize.Level0) 163 { 164 int ret = 0; 165 uint8_t aad[8] = {0}; 166 uint8_t tag[12] = {0}; 167 uint8_t iv[7] = {0}; 168 uint8_t cipherText[128] = {0}; 169 int cipherTextLen = 128; 170 171 HcfCipher *cipher = nullptr; 172 HcfSymKey *key = nullptr; 173 HcfCcmParamsSpec spec = {}; 174 spec.aad.data = aad; 175 spec.aad.len = sizeof(aad); 176 spec.tag.data = tag; 177 spec.tag.len = sizeof(tag); 178 spec.iv.data = iv; 179 spec.iv.len = sizeof(iv); 180 181 ret = GenerateSymKey("AES128", &key); 182 ASSERT_EQ(ret, 0); 183 184 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); 185 ASSERT_EQ(ret, 0); 186 187 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen); 188 ASSERT_EQ(ret, 0); 189 190 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12); 191 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 192 cipherTextLen -= 12; 193 194 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen); 195 ASSERT_EQ(ret, 0); 196 197 HcfObjDestroy((HcfObjectBase *)key); 198 HcfObjDestroy((HcfObjectBase *)cipher); 199 } 200 201 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest005, TestSize.Level0) 202 { 203 int ret = 0; 204 uint8_t aad[8] = {0}; 205 uint8_t tag[12] = {0}; 206 uint8_t iv[7] = {0}; 207 uint8_t cipherText[128] = {0}; 208 int cipherTextLen = 128; 209 210 HcfCipher *cipher = nullptr; 211 HcfSymKey *key = nullptr; 212 HcfCcmParamsSpec spec = {}; 213 spec.aad.data = aad; 214 spec.aad.len = sizeof(aad); 215 spec.tag.data = tag; 216 spec.tag.len = sizeof(tag); 217 spec.iv.data = iv; 218 spec.iv.len = sizeof(iv); 219 220 ret = GenerateSymKey("AES128", &key); 221 ASSERT_EQ(ret, 0); 222 223 ret = HcfCipherCreate("AES128|CCM|PKCS5", &cipher); 224 ASSERT_EQ(ret, 0); 225 226 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen); 227 ASSERT_EQ(ret, 0); 228 229 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12); 230 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 231 cipherTextLen -= 12; 232 233 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen); 234 ASSERT_EQ(ret, 0); 235 236 HcfObjDestroy((HcfObjectBase *)key); 237 HcfObjDestroy((HcfObjectBase *)cipher); 238 } 239 240 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest006, TestSize.Level0) 241 { 242 int ret = 0; 243 uint8_t aad[8] = {0}; 244 uint8_t tag[12] = {0}; 245 uint8_t iv[7] = {0}; 246 uint8_t cipherText[128] = {0}; 247 int cipherTextLen = 128; 248 249 HcfCipher *cipher = nullptr; 250 HcfSymKey *key = nullptr; 251 HcfCcmParamsSpec spec = {}; 252 spec.aad.data = aad; 253 spec.aad.len = sizeof(aad); 254 spec.tag.data = tag; 255 spec.tag.len = sizeof(tag); 256 spec.iv.data = iv; 257 spec.iv.len = sizeof(iv); 258 259 ret = GenerateSymKey("AES128", &key); 260 ASSERT_EQ(ret, 0); 261 262 ret = HcfCipherCreate("AES128|CCM|PKCS7", &cipher); 263 ASSERT_EQ(ret, 0); 264 265 ret = AesNoUpdateEncrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, &cipherTextLen); 266 ASSERT_EQ(ret, 0); 267 268 (void)memcpy_s(spec.tag.data, 12, cipherText + cipherTextLen - 12, 12); 269 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 270 cipherTextLen -= 12; 271 272 ret = AesNoUpdateDecrypt(cipher, key, (HcfParamsSpec *)&spec, cipherText, cipherTextLen); 273 ASSERT_EQ(ret, 0); 274 275 HcfObjDestroy((HcfObjectBase *)key); 276 HcfObjDestroy((HcfObjectBase *)cipher); 277 } 278 279 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest007, TestSize.Level0) 280 { 281 int ret = 0; 282 uint8_t aad[CCM_AAD_LEN] = { 0 }; 283 uint8_t tag[CCM_TAG_LEN] = { 0 }; 284 uint8_t iv[CCM_IV_LEN] = { 0 }; 285 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 286 int cipherTextLen = CIPHER_TEXT_LEN; 287 288 HcfCipher *cipher = nullptr; 289 HcfSymKey *key = nullptr; 290 HcfCcmParamsSpec spec = {}; 291 spec.aad.data = aad; 292 spec.aad.len = sizeof(aad); 293 spec.tag.data = tag; 294 spec.tag.len = sizeof(tag); 295 spec.iv.data = iv; 296 spec.iv.len = sizeof(iv); 297 298 ret = GenerateSymKey("AES192", &key); 299 ASSERT_EQ(ret, 0); 300 301 ret = HcfCipherCreate("AES192|CCM|PKCS5", &cipher); 302 ASSERT_EQ(ret, 0); 303 304 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); 305 ASSERT_EQ(ret, 0); 306 307 (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN); 308 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 309 cipherTextLen -= CCM_TAG_LEN; 310 311 ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen); 312 ASSERT_EQ(ret, 0); 313 314 HcfObjDestroy(key); 315 HcfObjDestroy(cipher); 316 } 317 318 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest008, TestSize.Level0) 319 { 320 int ret = 0; 321 uint8_t aad[CCM_AAD_LEN] = { 0 }; 322 uint8_t tag[CCM_TAG_LEN] = { 0 }; 323 uint8_t iv[CCM_IV_LEN] = { 0 }; 324 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 325 int cipherTextLen = CIPHER_TEXT_LEN; 326 327 HcfCipher *cipher = nullptr; 328 HcfSymKey *key = nullptr; 329 HcfCcmParamsSpec spec = {}; 330 spec.aad.data = aad; 331 spec.aad.len = sizeof(aad); 332 spec.tag.data = tag; 333 spec.tag.len = sizeof(tag); 334 spec.iv.data = iv; 335 spec.iv.len = sizeof(iv); 336 337 ret = GenerateSymKey("AES256", &key); 338 ASSERT_EQ(ret, 0); 339 340 ret = HcfCipherCreate("AES256|CCM|PKCS5", &cipher); 341 ASSERT_EQ(ret, 0); 342 343 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); 344 ASSERT_EQ(ret, 0); 345 346 (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN); 347 PrintfHex("ccm tag", spec.tag.data, spec.tag.len); 348 cipherTextLen -= CCM_TAG_LEN; 349 350 ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen); 351 ASSERT_EQ(ret, 0); 352 353 HcfObjDestroy(key); 354 HcfObjDestroy(cipher); 355 } 356 357 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest009, TestSize.Level0) 358 { 359 int ret = 0; 360 uint8_t cipherText[CIPHER_TEXT_LEN] = {0}; 361 int cipherTextLen = CIPHER_TEXT_LEN; 362 363 HcfCipher *cipher = nullptr; 364 HcfSymKey *key = nullptr; 365 366 ret = GenerateSymKey("AES128", &key); 367 ASSERT_EQ(ret, 0); 368 369 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); 370 ASSERT_EQ(ret, 0); 371 372 ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); 373 ASSERT_NE(ret, 0); 374 375 HcfObjDestroy(key); 376 HcfObjDestroy(cipher); 377 } 378 379 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest010, TestSize.Level0) 380 { 381 int ret = 0; 382 uint8_t aad[CCM_AAD_LEN] = { 0 }; 383 uint8_t tag[CCM_TAG_LEN] = { 0 }; 384 uint8_t iv[CCM_IV_LEN] = { 0 }; 385 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 386 int cipherTextLen = CIPHER_TEXT_LEN; 387 388 HcfCipher *cipher = nullptr; 389 HcfSymKey *key = nullptr; 390 HcfCcmParamsSpec spec = {}; 391 spec.aad.data = nullptr; 392 spec.aad.len = sizeof(aad); 393 spec.tag.data = tag; 394 spec.tag.len = sizeof(tag); 395 spec.iv.data = iv; 396 spec.iv.len = sizeof(iv); 397 398 ret = GenerateSymKey("AES128", &key); 399 ASSERT_EQ(ret, 0); 400 401 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); 402 ASSERT_EQ(ret, 0); 403 404 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); 405 ASSERT_NE(ret, 0); 406 407 HcfObjDestroy(key); 408 HcfObjDestroy(cipher); 409 } 410 411 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest011, TestSize.Level0) 412 { 413 int ret = 0; 414 uint8_t aad[CCM_AAD_LEN] = { 0 }; 415 uint8_t tag[CCM_TAG_LEN] = { 0 }; 416 uint8_t iv[CCM_IV_LEN] = { 0 }; 417 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 418 int cipherTextLen = CIPHER_TEXT_LEN; 419 420 HcfCipher *cipher = nullptr; 421 HcfSymKey *key = nullptr; 422 HcfCcmParamsSpec spec = {}; 423 spec.aad.data = aad; 424 spec.aad.len = sizeof(aad); 425 spec.tag.data = tag; 426 spec.tag.len = sizeof(tag); 427 spec.iv.data = nullptr; 428 spec.iv.len = sizeof(iv); 429 430 ret = GenerateSymKey("AES128", &key); 431 ASSERT_EQ(ret, 0); 432 433 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); 434 ASSERT_EQ(ret, 0); 435 436 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); 437 ASSERT_NE(ret, 0); 438 439 HcfObjDestroy(key); 440 HcfObjDestroy(cipher); 441 } 442 443 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest012, TestSize.Level0) 444 { 445 int ret = 0; 446 uint8_t aad[CCM_AAD_LEN] = { 0 }; 447 uint8_t tag[CCM_TAG_LEN] = { 0 }; 448 uint8_t iv[CCM_IV_LEN] = { 0 }; 449 uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; 450 int cipherTextLen = CIPHER_TEXT_LEN; 451 452 HcfCipher *cipher = nullptr; 453 HcfSymKey *key = nullptr; 454 HcfCcmParamsSpec spec = {}; 455 spec.aad.data = aad; 456 spec.aad.len = sizeof(aad); 457 spec.tag.data = nullptr; 458 spec.tag.len = sizeof(tag); 459 spec.iv.data = iv; 460 spec.iv.len = sizeof(iv); 461 462 ret = GenerateSymKey("AES128", &key); 463 ASSERT_EQ(ret, 0); 464 465 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); 466 ASSERT_EQ(ret, 0); 467 468 ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); 469 ASSERT_NE(ret, 0); 470 471 HcfObjDestroy(key); 472 HcfObjDestroy(cipher); 473 } 474 475 HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest013, TestSize.Level0) 476 { 477 int ret = 0; 478 HcfCipher *cipher = nullptr; 479 HcfSymKey *key = nullptr; 480 HcfSymKeyGenerator *generator = nullptr; 481 482 ret = HcfSymKeyGeneratorCreate("AES128", &generator); 483 ASSERT_EQ(ret, 0); 484 485 ret = generator->generateSymKey(generator, &key); 486 ASSERT_EQ(ret, 0); 487 488 generator->base.destroy(nullptr); 489 490 ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); 491 ASSERT_EQ(ret, 0); 492 493 ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast<HcfKey *>(key), nullptr); 494 ASSERT_NE(ret, 0); 495 496 HcfObjDestroy(key); 497 HcfObjDestroy(cipher); 498 HcfObjDestroy(generator); 499 } 500 }