1 /* Copyright (c) 2020-2021 Huawei Device Co., Ltd. 2 * Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 #include "SecurityDataHuks.h" 15 #include "hks_client.h" 16 #include "hks_types.h" 17 #include <securec.h> 18 #include <stdbool.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #include "gtest/gtest.h" 23 using namespace std; 24 using namespace testing::ext; 25 26 class SecurityDataHuksHashRandomHmacTestSuite : public testing::Test { 27 protected: 28 // SetUpTestCase: Testsuit setup, run before 1st testcase SetUpTestCase(void)29 static void SetUpTestCase(void) {} 30 // TearDownTestCase: Testsuit teardown, run after last testcase TearDownTestCase(void)31 static void TearDownTestCase(void) {} 32 // Testcase setup SetUp()33 virtual void SetUp() 34 { 35 int32_t status; 36 struct hks_file_callbacks fileCallbacks; 37 38 fileCallbacks.read = FileRead; 39 fileCallbacks.write = FileWrite; 40 fileCallbacks.file_size = FileSize; 41 42 status = hks_register_file_callbacks(&fileCallbacks); 43 EXPECT_EQ(0, status); 44 45 struct hks_log_f_group logFunc; 46 logFunc.log_info = Logi; 47 logFunc.log_warn = Logw; 48 logFunc.log_error = Loge; 49 logFunc.log_debug = Logd; 50 51 status = hks_register_log_interface(&logFunc); 52 EXPECT_EQ(0, status); 53 54 status = hks_register_get_hardware_udid_callback(HksTestGetHardwareUdid); 55 56 EXPECT_EQ(0, status); 57 58 status = hks_init(); 59 if (status != 0) { 60 status = hks_refresh_key_info(); 61 } 62 EXPECT_EQ(0, status); 63 } 64 65 // Testcase teardown TearDown()66 virtual void TearDown() {} 67 }; 68 69 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hash 70 // begin+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1100-1190 71 72 /* 73 * @tc.number : SUB_SEC_DataPro_HuksL1_1100 74 * @tc.name : Hash, normal input parameters SHA256, src, dst 75 * @tc.desc : [C- SECURITY -1600] 76 */ 77 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1100, Function | MediumTest | Level1) 78 { 79 struct hks_blob src, dst; 80 src.data = (uint8_t *)"123456"; 81 src.size = NUM6; 82 dst.data = (uint8_t *)malloc(NUM65); 83 if (dst.data == NULL) { 84 EXPECT_EQ(0, 1); 85 } 86 dst.size = NUM65; 87 88 int32_t res = hks_hash(HKS_ALG_HASH_SHA_256, &src, &dst); 89 EXPECT_EQ(0, res); 90 free(dst.data); 91 }; 92 93 /* 94 * @tc.number : SUB_SEC_DataPro_HuksL1_1110 95 * @tc.name : Hash, normal input parameters SHA512, src, dst 96 * @tc.desc : [C- SECURITY -1600] 97 */ 98 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1110, Function | MediumTest | Level1) 99 { 100 struct hks_blob src, dst; 101 src.data = (uint8_t *)"123456"; 102 src.size = NUM6; 103 dst.data = (uint8_t *)malloc(NUM65); 104 if (dst.data == NULL) { 105 EXPECT_EQ(0, 1); 106 } 107 dst.size = NUM65; 108 109 int32_t res = hks_hash(HKS_ALG_HASH_SHA_512, &src, &dst); 110 EXPECT_EQ(0, res); 111 free(dst.data); 112 }; 113 114 /* 115 * @tc.number : SUB_SEC_DataPro_HuksL1_1120 116 * @tc.name : Hash, abnormal input parameters alg is not equal to HKS_ALG_HASH_SHA_256 or HKS_ALG_HASH_SHA_512 117 * @tc.desc : [C- SECURITY -1600] 118 */ 119 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1120, Function | MediumTest | Level2) 120 { 121 struct hks_blob srcData = { 0 }; 122 char tmpData6[] = "30313233343536373839616263646566"; 123 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0); 124 125 struct hks_blob hash = { 0 }; 126 char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566"; 127 BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 128 129 uint32_t alg = HKS_ALG_HASH_SHA_1; 130 int32_t status = hks_hash(alg, &srcData, &hash); 131 HksBlobDestroyT1(&srcData); 132 HksBlobDestroyT1(&hash); 133 EXPECT_EQ(NUM135, status); 134 } 135 136 /* 137 * @tc.number : SUB_SEC_DataPro_HuksL1_1130 138 * @tc.name : Hash, abnormal input parameters srcData is null 139 * @tc.desc : [C- SECURITY -1600] 140 */ 141 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1130, Function | MediumTest | Level2) 142 { 143 struct hks_blob *srcData = nullptr; 144 145 struct hks_blob hash = { 0 }; 146 const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566"; 147 BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 148 149 uint32_t alg = HKS_ALG_HASH_SHA_256; 150 int32_t status = hks_hash(alg, srcData, &hash); 151 HksBlobDestroyT1(&hash); 152 EXPECT_EQ(NUM1000, status); 153 } 154 155 /* 156 * @tc.number : SUB_SEC_DataPro_HuksL1_1140 157 * @tc.name : Hash, abnormal input parameters srcData.data is null 158 * @tc.desc : [C- SECURITY -1600] 159 */ 160 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1140, Function | MediumTest | Level2) 161 { 162 struct hks_blob srcData = { 0 }; 163 const char tmpData6[] = "30313233343536373839616263646566"; 164 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 1); 165 struct hks_blob hash = { 0 }; 166 const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566"; 167 BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 168 169 uint32_t alg = HKS_ALG_HASH_SHA_256; 170 int32_t status = hks_hash(alg, &srcData, &hash); 171 HksBlobDestroyT1(&srcData); 172 HksBlobDestroyT1(&hash); 173 EXPECT_EQ(NUM1000, status); 174 } 175 176 /* 177 * @tc.number : SUB_SEC_DataPro_HuksL1_1150 178 * @tc.name : Hash, abnormal input parameters srcData.size is 0 179 * @tc.desc : [C- SECURITY -1600] 180 */ 181 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1150, Function | MediumTest | Level2) 182 { 183 struct hks_blob srcData = { 0 }; 184 srcData.data = (uint8_t *)"1234567890abcdefghigkl0123456789"; 185 srcData.size = 0; 186 srcData.type = HKS_BLOB_TYPE_RAW; 187 188 struct hks_blob hash = { 0 }; 189 const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566"; 190 BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 191 192 uint32_t alg = HKS_ALG_HASH_SHA_256; 193 int32_t status = hks_hash(alg, &srcData, &hash); 194 HksBlobDestroyT1(&hash); 195 EXPECT_EQ(NUM1000, status); 196 } 197 198 /* 199 * @tc.number : SUB_SEC_DataPro_HuksL1_1160 200 * @tc.name : Hash, abnormal input parameters hash is null 201 * @tc.desc : [C- SECURITY -1600] 202 */ 203 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1160, Function | MediumTest | Level2) 204 { 205 struct hks_blob srcData = { 0 }; 206 const char tmpData6[] = "30313233343536373839616263646566"; 207 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0); 208 209 struct hks_blob *hash = nullptr; 210 211 uint32_t alg = HKS_ALG_HASH_SHA_256; 212 int32_t status = hks_hash(alg, &srcData, hash); 213 HksBlobDestroyT1(&srcData); 214 EXPECT_EQ(NUM1000, status); 215 } 216 217 /* 218 * @tc.number : SUB_SEC_DataPro_HuksL1_1170 219 * @tc.name : Hash, abnormal input parameters hash.data is null 220 * @tc.desc : [C- SECURITY -1600] 221 */ 222 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1170, Function | MediumTest | Level2) 223 { 224 struct hks_blob srcData = { 0 }; 225 const char tmpData6[] = "30313233343536373839616263646566"; 226 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0); 227 228 struct hks_blob hash = { 0 }; 229 const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566"; 230 BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 1); 231 232 uint32_t alg = HKS_ALG_HASH_SHA_256; 233 int32_t status = hks_hash(alg, &srcData, &hash); 234 HksBlobDestroyT1(&srcData); 235 HksBlobDestroyT1(&hash); 236 EXPECT_EQ(NUM1000, status); 237 } 238 239 /* 240 * @tc.number : SUB_SEC_DataPro_HuksL1_1180 241 * @tc.name : Hash, abnormal input parameters hash.size is less than 32 242 * @tc.desc : [C- SECURITY -1600] 243 */ 244 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1180, Function | MediumTest | Level2) 245 { 246 struct hks_blob srcData = { 0 }; 247 const char tmpData6[] = "30313233343536373839616263646566"; 248 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0); 249 250 struct hks_blob hash = { 0 }; 251 const char tmpData7[] = "303132333435363738396162636465663031323334353637383961611266"; 252 BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM30, 0); 253 254 uint32_t alg = HKS_ALG_HASH_SHA_256; 255 int32_t status = hks_hash(alg, &srcData, &hash); 256 HksBlobDestroyT1(&srcData); 257 HksBlobDestroyT1(&hash); 258 EXPECT_EQ(NUM1007, status); 259 } 260 261 /* 262 * @tc.number : SUB_SEC_DataPro_HuksL1_1190 263 * @tc.name : Hash, abnormal input parameters hash.size is less than 64 264 * @tc.desc : [C- SECURITY -1600] 265 */ 266 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1190, Function | MediumTest | Level2) 267 { 268 struct hks_blob srcData = { 0 }; 269 const char tmpData6[] = "30313233343536373839616263646566"; 270 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0); 271 272 struct hks_blob hash = { 0 }; 273 const char tmpData7[] = "30313233343536373839616263646566303132333435363738396162636465663031323334" 274 "3536373839616263646566303132333435363738396162"; 275 BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM60, 0); 276 277 uint32_t alg = HKS_ALG_HASH_SHA_512; 278 int32_t status = hks_hash(alg, &srcData, &hash); 279 HksBlobDestroyT1(&srcData); 280 HksBlobDestroyT1(&hash); 281 EXPECT_EQ(NUM1007, status); 282 } 283 284 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hash 285 // end+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1100-1190 286 287 288 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Generate random 289 // beign++++++++++++++++++++++++++++++++++++++++++++++++1200-1230 290 291 /* 292 * @tc.number : SUB_SEC_DataPro_HuksL1_1200 293 * @tc.name : Generate Random, normal input parameters random 294 * @tc.desc : [C- SECURITY -1700] 295 */ 296 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1200, Function | MediumTest | Level1) 297 { 298 int32_t status; 299 struct hks_blob random = { 0 }; 300 301 random.data = (uint8_t *)calloc(1, NUM64); 302 if (random.data == NULL) { 303 EXPECT_EQ(0, 1); 304 return; 305 } 306 if (memset_s(random.data, NUM64, 0, NUM64) != EOK) { 307 EXPECT_EQ(0, 1); 308 } 309 random.size = NUM64; 310 random.type = 0; 311 status = hks_generate_random(&random); 312 EXPECT_EQ(0, status); 313 314 HksBlobDestroyT1(&random); 315 }; 316 317 /* 318 * @tc.number : SUB_SEC_DataPro_HuksL1_1210 319 * @tc.name : Generate Random, abnormal input parameters random is null 320 * @tc.desc : [C- SECURITY -1700] 321 */ 322 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1210, Function | MediumTest | Level2) 323 { 324 int32_t status; 325 struct hks_blob *random = nullptr; 326 327 status = hks_generate_random(random); 328 EXPECT_EQ(NUM1000, status); 329 } 330 331 /* 332 * @tc.number : SUB_SEC_DataPro_HuksL1_1220 333 * @tc.name : Generate Random, abnormal input parameters random.data is null 334 * @tc.desc : [C- SECURITY -1700] 335 */ 336 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1220, Function | MediumTest | Level2) 337 { 338 int32_t status; 339 struct hks_blob random = { 0 }; 340 341 random.data = NULL; 342 random.size = NUM32; 343 random.type = HKS_BLOB_TYPE_KEY; 344 345 status = hks_generate_random(&random); 346 EXPECT_EQ(NUM1000, status); 347 HksBlobDestroyT1(&random); 348 } 349 350 /* 351 * @tc.number : SUB_SEC_DataPro_HuksL1_1230 352 * @tc.name : Generate Random, abnormal input parameters random.size is more than 1024 353 * @tc.desc : [C- SECURITY -1700] 354 */ 355 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1230, Function | MediumTest | Level2) 356 { 357 int32_t status; 358 struct hks_blob random = { 0 }; 359 360 random.data = (uint8_t *)malloc(NUM1025); 361 if (random.data == NULL) { 362 EXPECT_EQ(0, 1); 363 } 364 random.size = NUM1025; 365 random.type = HKS_BLOB_TYPE_KEY; 366 367 status = hks_generate_random(&random); 368 EXPECT_EQ(NUM135, status); 369 HksBlobDestroyT1(&random); 370 } 371 372 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Generate random 373 // end++++++++++++++++++++++++++++++++++++++++++++++++++1200-1230 374 375 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hmac 376 // begin+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1240-1380 377 378 /* 379 * @tc.number : SUB_SEC_DataPro_HuksL1_1240 380 * @tc.name : Hmac, normal input parameters SHA256, keyAlias, alg, srcData, output 381 * @tc.desc : [C- SECURITY -2000] 382 */ 383 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1240, Function | MediumTest | Level1) 384 { 385 struct hks_blob keyAlias; 386 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 387 struct hks_blob srcData; 388 srcData.data = (uint8_t *)"123456789"; 389 srcData.size = NUM9; 390 391 struct hks_blob output; 392 output.data = (uint8_t *)malloc(NUM65); 393 output.size = NUM65; 394 keyAlias.data = (uint8_t *)"1234567890abcdefghigkl0123456789"; 395 keyAlias.size = NUM32; 396 keyAlias.type = HKS_KEY_USAGE_EXPORT; 397 398 int32_t status = hks_hmac(&keyAlias, alg, &srcData, &output); 399 EXPECT_EQ(0, status); 400 401 HKS_FREE_PTR1(output.data); 402 }; 403 404 /* 405 * @tc.number : SUB_SEC_DataPro_HuksL1_1250 406 * @tc.name : Hmac, normal input parameters SHA512, keyAlias, alg, srcData, output 407 * @tc.desc : [C- SECURITY -2000] 408 */ 409 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1250, Function | MediumTest | Level1) 410 { 411 struct hks_blob keyAlias; 412 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_512); 413 struct hks_blob srcData; 414 srcData.data = (uint8_t *)"123456789"; 415 srcData.size = NUM9; 416 417 struct hks_blob output; 418 output.data = (uint8_t *)malloc(NUM65); 419 output.size = NUM65; 420 keyAlias.data = (uint8_t *)"1234567890abcdefghigkl0123456789"; 421 keyAlias.size = NUM65; 422 keyAlias.type = HKS_KEY_USAGE_EXPORT; 423 424 int32_t status = hks_hmac(&keyAlias, alg, &srcData, &output); 425 EXPECT_EQ(0, status); 426 427 HKS_FREE_PTR1(output.data); 428 }; 429 430 /* 431 * @tc.number : SUB_SEC_DataPro_HuksL1_1260 432 * @tc.name : Hmac, abnormal input parameters key is null 433 * @tc.desc : [C- SECURITY -2000] 434 */ 435 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1260, Function | MediumTest | Level2) 436 { 437 struct hks_blob *key = nullptr; 438 struct hks_blob srcData = { 0 }; 439 const char tmpData6[] = "3031323334353637"; 440 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 441 442 struct hks_blob output = { 0 }; 443 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 444 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 445 446 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 447 int32_t status = hks_hmac(key, alg, &srcData, &output); 448 HksBlobDestroyT1(&srcData); 449 HksBlobDestroyT1(&output); 450 EXPECT_EQ(NUM1000, status); 451 } 452 453 /* 454 * @tc.number : SUB_SEC_DataPro_HuksL1_1270 455 * @tc.name : Hmac, abnormal input parameters key.data is null 456 * @tc.desc : [C- SECURITY -2000] 457 */ 458 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1270, Function | MediumTest | Level2) 459 { 460 struct hks_blob key = { 0 }; 461 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 462 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, NUM1); 463 464 struct hks_blob srcData = { 0 }; 465 const char tmpData6[] = "3031323334353637"; 466 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 467 468 struct hks_blob output = { 0 }; 469 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 470 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 471 472 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 473 int32_t status = hks_hmac(&key, alg, &srcData, &output); 474 HksBlobDestroyT1(&key); 475 HksBlobDestroyT1(&srcData); 476 HksBlobDestroyT1(&output); 477 EXPECT_EQ(NUM1000, status); 478 } 479 480 /* 481 * @tc.number : SUB_SEC_DataPro_HuksL1_1280 482 * @tc.name : Hmac, abnormal input parameters key.size is 0 483 * @tc.desc : [C- SECURITY -2000] 484 */ 485 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1280, Function | MediumTest | Level2) 486 { 487 struct hks_blob key = { 0 }; 488 key.data = (uint8_t *)"1234567890abcdefghigkl0123456789"; 489 key.size = 0; 490 key.type = HKS_BLOB_TYPE_RAW; 491 492 struct hks_blob srcData = { 0 }; 493 const char tmpData6[] = "3031323334353637"; 494 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 495 496 struct hks_blob output = { 0 }; 497 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 498 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 499 500 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 501 int32_t status = hks_hmac(&key, alg, &srcData, &output); 502 HksBlobDestroyT1(&srcData); 503 HksBlobDestroyT1(&output); 504 EXPECT_EQ(NUM1000, status); 505 } 506 507 /* 508 * @tc.number : SUB_SEC_DataPro_HuksL1_1290 509 * @tc.name : Hmac, abnormal input parameters alg is not equal to sha256 or sha512 510 * @tc.desc : [C- SECURITY -2000] 511 */ 512 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1290, Function | MediumTest | Level2) 513 { 514 struct hks_blob key = { 0 }; 515 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 516 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0); 517 518 struct hks_blob srcData = { 0 }; 519 const char tmpData6[] = "3031323334353637"; 520 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 521 522 struct hks_blob output = { 0 }; 523 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 524 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 525 526 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_1); 527 int32_t status = hks_hmac(&key, alg, &srcData, &output); 528 HksBlobDestroyT1(&key); 529 HksBlobDestroyT1(&srcData); 530 HksBlobDestroyT1(&output); 531 EXPECT_EQ(NUM135, status); 532 } 533 534 /* 535 * @tc.number : SUB_SEC_DataPro_HuksL1_1300 536 * @tc.name : Hmac, abnormal input parameters srcData is null 537 * @tc.desc : [C- SECURITY -2000] 538 */ 539 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1300, Function | MediumTest | Level2) 540 { 541 struct hks_blob key = { 0 }; 542 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 543 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0); 544 545 struct hks_blob *srcData = nullptr; 546 547 struct hks_blob output = { 0 }; 548 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 549 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 550 551 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 552 int32_t status = hks_hmac(&key, alg, srcData, &output); 553 HksBlobDestroyT1(&key); 554 HksBlobDestroyT1(&output); 555 EXPECT_EQ(NUM1000, status); 556 } 557 558 /* 559 * @tc.number : SUB_SEC_DataPro_HuksL1_1310 560 * @tc.name : Hmac, abnormal input parameters srcData.data is null 561 * @tc.desc : [C- SECURITY -2000] 562 */ 563 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1310, Function | MediumTest | Level2) 564 { 565 struct hks_blob key = { 0 }; 566 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 567 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0); 568 569 struct hks_blob srcData = { 0 }; 570 const char tmpData6[] = "3031323334353637"; 571 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, NUM1); 572 573 struct hks_blob output = { 0 }; 574 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 575 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 576 577 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 578 int32_t status = hks_hmac(&key, alg, &srcData, &output); 579 HksBlobDestroyT1(&key); 580 HksBlobDestroyT1(&srcData); 581 HksBlobDestroyT1(&output); 582 EXPECT_EQ(NUM1000, status); 583 } 584 585 /* 586 * @tc.number : SUB_SEC_DataPro_HuksL1_1320 587 * @tc.name : Hmac, abnormal input parameters srcData.size is 0 588 * @tc.desc : [C- SECURITY -2000] 589 */ 590 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1320, Function | MediumTest | Level2) 591 { 592 struct hks_blob key = { 0 }; 593 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 594 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0); 595 596 struct hks_blob srcData = { 0 }; 597 srcData.data = (uint8_t *)"1234567890abcdefghigkl0123456789"; 598 srcData.size = 0; 599 srcData.type = HKS_BLOB_TYPE_RAW; 600 601 struct hks_blob output = { 0 }; 602 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 603 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 604 605 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 606 int32_t status = hks_hmac(&key, alg, &srcData, &output); 607 HksBlobDestroyT1(&key); 608 HksBlobDestroyT1(&output); 609 EXPECT_EQ(NUM1000, status); 610 } 611 612 /* 613 * @tc.number : SUB_SEC_DataPro_HuksL1_1330 614 * @tc.name : Hmac, abnormal input parameters output is null 615 * @tc.desc : [C- SECURITY -2000] 616 */ 617 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1330, Function | MediumTest | Level2) 618 { 619 struct hks_blob key = { 0 }; 620 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 621 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0); 622 623 struct hks_blob srcData = { 0 }; 624 const char tmpData6[] = "3031323334353637"; 625 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 626 627 struct hks_blob *output = nullptr; 628 629 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 630 int32_t status = hks_hmac(&key, alg, &srcData, output); 631 HksBlobDestroyT1(&key); 632 HksBlobDestroyT1(&srcData); 633 EXPECT_EQ(NUM1000, status); 634 } 635 636 /* 637 * @tc.number : SUB_SEC_DataPro_HuksL1_1340 638 * @tc.name : Hmac, abnormal input parameters output.data is null 639 * @tc.desc : [C- SECURITY -2000] 640 */ 641 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1340, Function | MediumTest | Level2) 642 { 643 struct hks_blob key = { 0 }; 644 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 645 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0); 646 647 struct hks_blob srcData = { 0 }; 648 const char tmpData6[] = "3031323334353637"; 649 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 650 651 struct hks_blob output = { 0 }; 652 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 653 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, NUM1); 654 655 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 656 int32_t status = hks_hmac(&key, alg, &srcData, &output); 657 HksBlobDestroyT1(&key); 658 HksBlobDestroyT1(&srcData); 659 HksBlobDestroyT1(&output); 660 EXPECT_EQ(NUM1000, status); 661 } 662 663 /* 664 * @tc.number : SUB_SEC_DataPro_HuksL1_1350 665 * @tc.name : Hmac, abnormal input parameters alg is sha256 and key.size is less than 32 666 * @tc.desc : [C- SECURITY -2000] 667 */ 668 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1350, Function | MediumTest | Level2) 669 { 670 struct hks_blob key = { 0 }; 671 const char tmpData5[] = "303132333435363730313233343536373031323334353637303132333411"; 672 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM30, 0); 673 674 struct hks_blob srcData = { 0 }; 675 const char tmpData6[] = "3031323334353637"; 676 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 677 678 struct hks_blob output = { 0 }; 679 const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637"; 680 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0); 681 682 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 683 int32_t status = hks_hmac(&key, alg, &srcData, &output); 684 HksBlobDestroyT1(&key); 685 HksBlobDestroyT1(&srcData); 686 HksBlobDestroyT1(&output); 687 EXPECT_EQ(NUM135, status); 688 } 689 690 /* 691 * @tc.number : SUB_SEC_DataPro_HuksL1_1360 692 * @tc.name : Hmac, abnormal input parameters alg is sha512 and key.size is less than 64 693 * @tc.desc : [C- SECURITY -2000] 694 */ 695 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1360, Function | MediumTest | Level2) 696 { 697 struct hks_blob key = { 0 }; 698 const char tmpData5[] = "30313233343536373031323334353637303132333435363730313233343536373031323334" 699 "35363730313233343536373031323334353637303132333435"; 700 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM62, 0); 701 702 struct hks_blob srcData = { 0 }; 703 const char tmpData6[] = "3031323334353637"; 704 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 705 706 struct hks_blob output = { 0 }; 707 const char tmpData7[] = "30313233343536373031323334353637303132333435363730313233343536373031323334" 708 "353637303132333435363730313233343536373031323334353637"; 709 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM64, 0); 710 711 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_512); 712 int32_t status = hks_hmac(&key, alg, &srcData, &output); 713 HksBlobDestroyT1(&key); 714 HksBlobDestroyT1(&srcData); 715 HksBlobDestroyT1(&output); 716 EXPECT_EQ(NUM135, status); 717 } 718 719 /* 720 * @tc.number : SUB_SEC_DataPro_HuksL1_1370 721 * @tc.name : Hmac, abnormal input parameters alg is sha256 and output.size is less than 32 722 * @tc.desc : [C- SECURITY -2000] 723 */ 724 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1370, Function | MediumTest | Level2) 725 { 726 struct hks_blob key = { 0 }; 727 const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637"; 728 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0); 729 730 struct hks_blob srcData = { 0 }; 731 const char tmpData6[] = "3031323334353637"; 732 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 733 734 735 struct hks_blob output = { 0 }; 736 const char tmpData7[] = "303132333435363730313233343536373031323334353637303132333413"; 737 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM30, 0); 738 739 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256); 740 int32_t status = hks_hmac(&key, alg, &srcData, &output); 741 HksBlobDestroyT1(&key); 742 HksBlobDestroyT1(&srcData); 743 HksBlobDestroyT1(&output); 744 EXPECT_EQ(NUM1007, status); 745 } 746 747 /* 748 * @tc.number : SUB_SEC_DataPro_HuksL1_1380 749 * @tc.name : Hmac, abnormal input parameters alg is sha512 and output.size is less than 64 750 * @tc.desc : [C- SECURITY -2000] 751 */ 752 HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1380, Function | MediumTest | Level2) 753 { 754 struct hks_blob key = { 0 }; 755 const char tmpData5[] = "303132333435363730313233343536373031323334353637303132333435363730313233343" 756 "53637303132333435363730313233343536373031323334353637"; 757 BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM64, 0); 758 759 struct hks_blob srcData = { 0 }; 760 const char tmpData6[] = "3031323334353637"; 761 BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0); 762 763 struct hks_blob output = { 0 }; 764 const char tmpData7[] = "303132333435363730313233343536373031323334353637303132333435363730313233343" 765 "5363730313233343536373031323334353637303132333435"; 766 BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM62, 0); 767 768 uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_512); 769 int32_t status = hks_hmac(&key, alg, &srcData, &output); 770 HksBlobDestroyT1(&key); 771 HksBlobDestroyT1(&srcData); 772 HksBlobDestroyT1(&output); 773 EXPECT_EQ(NUM1007, status); 774 } 775 776 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hmac 777 // end+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1240-1380 778 779