SecurityDataHuksHashRandomHmacTest.cpp 27.3 KB
Newer Older
M
mamingshuai 已提交
1
/* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
*/
#include "SecurityDataHuks.h"
#include "hks_client.h"
#include "hks_types.h"
#include <securec.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gtest/gtest.h"
using namespace std;
using namespace testing::ext;

class SecurityDataHuksHashRandomHmacTestSuite : public testing::Test {
protected:
    // SetUpTestCase: Testsuit setup, run before 1st testcase
    static void SetUpTestCase(void) {}
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void) {}
    // Testcase setup
    virtual void SetUp()
    {
        int32_t status;
        struct hks_file_callbacks fileCallbacks;

        fileCallbacks.read = FileRead;
        fileCallbacks.write = FileWrite;
        fileCallbacks.file_size = FileSize;

        status = hks_register_file_callbacks(&fileCallbacks);
        EXPECT_EQ(0, status);

        struct hks_log_f_group logFunc;
        logFunc.log_info = Logi;
        logFunc.log_warn = Logw;
        logFunc.log_error = Loge;
        logFunc.log_debug = Logd;

        status = hks_register_log_interface(&logFunc);
        EXPECT_EQ(0, status);

        status = hks_register_get_hardware_udid_callback(HksTestGetHardwareUdid);

        EXPECT_EQ(0, status);

        status = hks_init();
        if (status != 0) {
            status = hks_refresh_key_info();
        }
        EXPECT_EQ(0, status);
    }

    // Testcase teardown
    virtual void TearDown() {}
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hash
M
mamingshuai 已提交
70
// begin+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1100-1190
W
wenjun 已提交
71 72

/*
M
mamingshuai 已提交
73 74
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1100
 * @tc.name      : Hash, normal input parameters SHA256, src, dst
W
wenjun 已提交
75 76
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
77
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1100, Function | MediumTest | Level1)
W
wenjun 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
{
    struct hks_blob src, dst;
    src.data = (uint8_t *)"123456";
    src.size = NUM6;
    dst.data = (uint8_t *)malloc(NUM65);
    if (dst.data == NULL) {
        EXPECT_EQ(0, 1);
    }
    dst.size = NUM65;

    int32_t res = hks_hash(HKS_ALG_HASH_SHA_256, &src, &dst);
    EXPECT_EQ(0, res);
    free(dst.data);
};

/*
M
mamingshuai 已提交
94 95
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1110
 * @tc.name      : Hash, normal input parameters SHA512, src, dst
W
wenjun 已提交
96 97
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
98
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1110, Function | MediumTest | Level1)
W
wenjun 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
{
    struct hks_blob src, dst;
    src.data = (uint8_t *)"123456";
    src.size = NUM6;
    dst.data = (uint8_t *)malloc(NUM65);
    if (dst.data == NULL) {
        EXPECT_EQ(0, 1);
    }
    dst.size = NUM65;

    int32_t res = hks_hash(HKS_ALG_HASH_SHA_512, &src, &dst);
    EXPECT_EQ(0, res);
    free(dst.data);
};

/*
M
mamingshuai 已提交
115 116
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1120
 * @tc.name      : Hash, abnormal input parameters alg is not equal to HKS_ALG_HASH_SHA_256 or HKS_ALG_HASH_SHA_512
W
wenjun 已提交
117 118
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
119
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1120, Function | MediumTest | Level2)
W
wenjun 已提交
120 121 122 123 124 125 126 127 128
{
    struct hks_blob srcData = { 0 };
    char tmpData6[] = "30313233343536373839616263646566";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0);

    struct hks_blob hash = { 0 };
    char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566";
    BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

M
mamingshuai 已提交
129
    uint32_t alg = HKS_ALG_HASH_SHA_1;
W
wenjun 已提交
130 131 132 133 134 135 136
    int32_t status = hks_hash(alg, &srcData, &hash);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&hash);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
137 138
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1130
 * @tc.name      : Hash, abnormal input parameters srcData is null
W
wenjun 已提交
139 140
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
141
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1130, Function | MediumTest | Level2)
W
wenjun 已提交
142
{
M
mamingshuai 已提交
143
    struct hks_blob *srcData = nullptr;
W
wenjun 已提交
144 145 146 147 148 149 150 151 152 153 154 155

    struct hks_blob hash = { 0 };
    const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566";
    BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = HKS_ALG_HASH_SHA_256;
    int32_t status = hks_hash(alg, srcData, &hash);
    HksBlobDestroyT1(&hash);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
156 157
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1140
 * @tc.name      : Hash, abnormal input parameters srcData.data is null
W
wenjun 已提交
158 159
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
160
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1140, Function | MediumTest | Level2)
W
wenjun 已提交
161 162 163
{
    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "30313233343536373839616263646566";
M
mamingshuai 已提交
164
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 1);
W
wenjun 已提交
165 166 167 168 169 170 171 172 173 174 175 176
    struct hks_blob hash = { 0 };
    const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566";
    BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = HKS_ALG_HASH_SHA_256;
    int32_t status = hks_hash(alg, &srcData, &hash);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&hash);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
177 178
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1150
 * @tc.name      : Hash, abnormal input parameters srcData.size is 0
W
wenjun 已提交
179 180
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
181
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1150, Function | MediumTest | Level2)
W
wenjun 已提交
182 183 184
{
    struct hks_blob srcData = { 0 };
    srcData.data = (uint8_t *)"1234567890abcdefghigkl0123456789";
M
mamingshuai 已提交
185
    srcData.size = 0;
W
wenjun 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198
    srcData.type = HKS_BLOB_TYPE_RAW;

    struct hks_blob hash = { 0 };
    const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566";
    BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = HKS_ALG_HASH_SHA_256;
    int32_t status = hks_hash(alg, &srcData, &hash);
    HksBlobDestroyT1(&hash);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
199 200
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1160
 * @tc.name      : Hash, abnormal input parameters hash is null
W
wenjun 已提交
201 202
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
203
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1160, Function | MediumTest | Level2)
W
wenjun 已提交
204 205 206 207 208
{
    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "30313233343536373839616263646566";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0);

M
mamingshuai 已提交
209
    struct hks_blob *hash = nullptr;
W
wenjun 已提交
210 211 212 213 214 215 216 217

    uint32_t alg = HKS_ALG_HASH_SHA_256;
    int32_t status = hks_hash(alg, &srcData, hash);
    HksBlobDestroyT1(&srcData);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
218 219
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1170
 * @tc.name      : Hash, abnormal input parameters hash.data is null
W
wenjun 已提交
220 221
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
222
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1170, Function | MediumTest | Level2)
W
wenjun 已提交
223 224 225 226 227 228 229
{
    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "30313233343536373839616263646566";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0);

    struct hks_blob hash = { 0 };
    const char tmpData7[] = "3031323334353637383961626364656630313233343536373839616263646566";
M
mamingshuai 已提交
230
    BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 1);
W
wenjun 已提交
231 232 233 234 235 236 237 238 239

    uint32_t alg = HKS_ALG_HASH_SHA_256;
    int32_t status = hks_hash(alg, &srcData, &hash);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&hash);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
240 241
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1180
 * @tc.name      : Hash, abnormal input parameters hash.size is less than 32
W
wenjun 已提交
242 243
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
244
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1180, Function | MediumTest | Level2)
W
wenjun 已提交
245 246 247 248 249 250 251
{
    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "30313233343536373839616263646566";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0);

    struct hks_blob hash = { 0 };
    const char tmpData7[] = "303132333435363738396162636465663031323334353637383961611266";
M
mamingshuai 已提交
252
    BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM30, 0);
W
wenjun 已提交
253 254 255 256 257 258 259 260 261

    uint32_t alg = HKS_ALG_HASH_SHA_256;
    int32_t status = hks_hash(alg, &srcData, &hash);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&hash);
    EXPECT_EQ(NUM1007, status);
}

/*
M
mamingshuai 已提交
262 263
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1190
 * @tc.name      : Hash, abnormal input parameters hash.size is less than 64
W
wenjun 已提交
264 265
 * @tc.desc      : [C- SECURITY -1600]
 */
M
mamingshuai 已提交
266
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHash1190, Function | MediumTest | Level2)
W
wenjun 已提交
267 268 269 270 271 272
{
    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "30313233343536373839616263646566";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM16, 0);

    struct hks_blob hash = { 0 };
M
mamingshuai 已提交
273 274 275
    const char tmpData7[] = "30313233343536373839616263646566303132333435363738396162636465663031323334"
    "3536373839616263646566303132333435363738396162";
    BuildBlobData(&hash, tmpData7, HKS_BLOB_TYPE_RAW, NUM60, 0);
W
wenjun 已提交
276 277 278 279 280 281 282 283 284

    uint32_t alg = HKS_ALG_HASH_SHA_512;
    int32_t status = hks_hash(alg, &srcData, &hash);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&hash);
    EXPECT_EQ(NUM1007, status);
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hash
M
mamingshuai 已提交
285
// end+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1100-1190
W
wenjun 已提交
286 287 288


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Generate random
M
mamingshuai 已提交
289
// beign++++++++++++++++++++++++++++++++++++++++++++++++1200-1230
W
wenjun 已提交
290 291

/*
M
mamingshuai 已提交
292 293
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1200
 * @tc.name      : Generate Random, normal input parameters random
W
wenjun 已提交
294 295
 * @tc.desc      : [C- SECURITY -1700]
 */
M
mamingshuai 已提交
296
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1200, Function | MediumTest | Level1)
W
wenjun 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
    int32_t status;
    struct hks_blob random = { 0 };

    random.data = (uint8_t *)calloc(1, NUM64);
    if (random.data == NULL) {
        EXPECT_EQ(0, 1);
        return;
    }
    if (memset_s(random.data, NUM64, 0, NUM64) != EOK) {
        EXPECT_EQ(0, 1);
    }
    random.size = NUM64;
    random.type = 0;
    status = hks_generate_random(&random);
    EXPECT_EQ(0, status);

    HksBlobDestroyT1(&random);
};

/*
M
mamingshuai 已提交
318 319
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1210
 * @tc.name      : Generate Random, abnormal input parameters random is null
W
wenjun 已提交
320 321
 * @tc.desc      : [C- SECURITY -1700]
 */
M
mamingshuai 已提交
322
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1210, Function | MediumTest | Level2)
W
wenjun 已提交
323 324
{
    int32_t status;
M
mamingshuai 已提交
325
    struct hks_blob *random = nullptr;
W
wenjun 已提交
326 327 328 329 330 331

    status = hks_generate_random(random);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
332 333
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1220
 * @tc.name      : Generate Random, abnormal input parameters random.data is null
W
wenjun 已提交
334 335
 * @tc.desc      : [C- SECURITY -1700]
 */
M
mamingshuai 已提交
336
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1220, Function | MediumTest | Level2)
W
wenjun 已提交
337 338 339 340
{
    int32_t status;
    struct hks_blob random = { 0 };

M
mamingshuai 已提交
341
    random.data = NULL;
W
wenjun 已提交
342 343 344 345 346 347 348 349 350
    random.size = NUM32;
    random.type = HKS_BLOB_TYPE_KEY;

    status = hks_generate_random(&random);
    EXPECT_EQ(NUM1000, status);
    HksBlobDestroyT1(&random);
}

/*
M
mamingshuai 已提交
351 352
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1230
 * @tc.name      : Generate Random, abnormal input parameters random.size is more than 1024
W
wenjun 已提交
353 354
 * @tc.desc      : [C- SECURITY -1700]
 */
M
mamingshuai 已提交
355
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataGenerateRandom1230, Function | MediumTest | Level2)
W
wenjun 已提交
356 357 358 359 360 361 362 363
{
    int32_t status;
    struct hks_blob random = { 0 };

    random.data = (uint8_t *)malloc(NUM1025);
    if (random.data == NULL) {
        EXPECT_EQ(0, 1);
    }
M
mamingshuai 已提交
364
    random.size = NUM1025;
W
wenjun 已提交
365 366 367 368 369 370 371 372
    random.type = HKS_BLOB_TYPE_KEY;

    status = hks_generate_random(&random);
    EXPECT_EQ(NUM135, status);
    HksBlobDestroyT1(&random);
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Generate random
M
mamingshuai 已提交
373
// end++++++++++++++++++++++++++++++++++++++++++++++++++1200-1230
W
wenjun 已提交
374 375

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hmac
M
mamingshuai 已提交
376
// begin+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1240-1380
W
wenjun 已提交
377 378

/*
M
mamingshuai 已提交
379 380
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1240
 * @tc.name      : Hmac, normal input parameters SHA256, keyAlias, alg, srcData, output
W
wenjun 已提交
381 382
 * @tc.desc      : [C- SECURITY -2000]
 */
M
mamingshuai 已提交
383
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1240, Function | MediumTest | Level1)
W
wenjun 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
{
    struct hks_blob keyAlias;
    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    struct hks_blob srcData;
    srcData.data = (uint8_t *)"123456789";
    srcData.size = NUM9;

    struct hks_blob output;
    output.data = (uint8_t *)malloc(NUM65);
    output.size = NUM65;
    keyAlias.data = (uint8_t *)"1234567890abcdefghigkl0123456789";
    keyAlias.size = NUM32;
    keyAlias.type = HKS_KEY_USAGE_EXPORT;

    int32_t status = hks_hmac(&keyAlias, alg, &srcData, &output);
    EXPECT_EQ(0, status);

    HKS_FREE_PTR1(output.data);
};

/*
M
mamingshuai 已提交
405 406
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1250
 * @tc.name      : Hmac, normal input parameters SHA512, keyAlias, alg, srcData, output
W
wenjun 已提交
407 408
 * @tc.desc      : [C- SECURITY -2000]
 */
M
mamingshuai 已提交
409
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1250, Function | MediumTest | Level1)
W
wenjun 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
{
    struct hks_blob keyAlias;
    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_512);
    struct hks_blob srcData;
    srcData.data = (uint8_t *)"123456789";
    srcData.size = NUM9;

    struct hks_blob output;
    output.data = (uint8_t *)malloc(NUM65);
    output.size = NUM65;
    keyAlias.data = (uint8_t *)"1234567890abcdefghigkl0123456789";
    keyAlias.size = NUM65;
    keyAlias.type = HKS_KEY_USAGE_EXPORT;

    int32_t status = hks_hmac(&keyAlias, alg, &srcData, &output);
    EXPECT_EQ(0, status);

    HKS_FREE_PTR1(output.data);
};

/*
M
mamingshuai 已提交
431 432
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1260
 * @tc.name      : Hmac, abnormal input parameters key is null
W
wenjun 已提交
433 434
 * @tc.desc      : [C- SECURITY -2000]
 */
M
mamingshuai 已提交
435
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1260, Function | MediumTest | Level2)
W
wenjun 已提交
436
{
M
mamingshuai 已提交
437
    struct hks_blob *key = nullptr;
W
wenjun 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(key, alg, &srcData, &output);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
454 455
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1270
 * @tc.name      : Hmac, abnormal input parameters key.data is null
W
wenjun 已提交
456 457
 * @tc.desc      : [C- SECURITY -2000]
 */
M
mamingshuai 已提交
458
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1270, Function | MediumTest | Level2)
W
wenjun 已提交
459 460 461
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
M
mamingshuai 已提交
462
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, NUM1);
W
wenjun 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
481 482
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1280
 * @tc.name      : Hmac, abnormal input parameters key.size is 0
W
wenjun 已提交
483 484
 * @tc.desc      : [C- SECURITY -2000]
 */
M
mamingshuai 已提交
485
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1280, Function | MediumTest | Level2)
W
wenjun 已提交
486 487 488
{
    struct hks_blob key = { 0 };
    key.data = (uint8_t *)"1234567890abcdefghigkl0123456789";
M
mamingshuai 已提交
489
    key.size = 0;
W
wenjun 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    key.type = HKS_BLOB_TYPE_RAW;

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1000, status);
}
M
mamingshuai 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1290
 * @tc.name      : Hmac, abnormal input parameters alg is not equal to sha256 or sha512
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1290, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_1);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM135, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1300
 * @tc.name      : Hmac, abnormal input parameters srcData is null
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1300, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0);

    struct hks_blob *srcData = nullptr;

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1000, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1310
 * @tc.name      : Hmac, abnormal input parameters srcData.data is null
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1310, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, NUM1);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1000, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1320
 * @tc.name      : Hmac, abnormal input parameters srcData.size is 0
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1320, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0);

    struct hks_blob srcData = { 0 };
    srcData.data = (uint8_t *)"1234567890abcdefghigkl0123456789";
    srcData.size = 0;
    srcData.type = HKS_BLOB_TYPE_RAW;

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1000, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1330
 * @tc.name      : Hmac, abnormal input parameters output is null
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1330, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob *output = nullptr;

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    EXPECT_EQ(NUM1000, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1340
 * @tc.name      : Hmac, abnormal input parameters output.data is null
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1340, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, NUM1);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1000, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1350
 * @tc.name      : Hmac, abnormal input parameters alg is sha256 and key.size is less than 32
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1350, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "303132333435363730313233343536373031323334353637303132333411";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM30, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM32, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM135, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1360
 * @tc.name      : Hmac, abnormal input parameters alg is sha512 and key.size is less than 64
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1360, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "30313233343536373031323334353637303132333435363730313233343536373031323334"
    "35363730313233343536373031323334353637303132333435";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM62, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "30313233343536373031323334353637303132333435363730313233343536373031323334"
    "353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM64, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_512);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM135, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1370
 * @tc.name      : Hmac, abnormal input parameters alg is sha256 and output.size is less than 32
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1370, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "3031323334353637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM32, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);


    struct hks_blob output = { 0 };
    const char tmpData7[] = "303132333435363730313233343536373031323334353637303132333413";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM30, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_256);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1007, status);
}

/*
 * @tc.number    : SUB_SEC_DataPro_HuksL1_1380
 * @tc.name      : Hmac, abnormal input parameters alg is sha512 and output.size is less than 64
 * @tc.desc      : [C- SECURITY -2000]
 */
HWTEST_F(SecurityDataHuksHashRandomHmacTestSuite, securityDataHmac1380, Function | MediumTest | Level2)
{
    struct hks_blob key = { 0 };
    const char tmpData5[] = "303132333435363730313233343536373031323334353637303132333435363730313233343"
    "53637303132333435363730313233343536373031323334353637";
    BuildBlobData(&key, tmpData5, HKS_BLOB_TYPE_RAW, NUM64, 0);

    struct hks_blob srcData = { 0 };
    const char tmpData6[] = "3031323334353637";
    BuildBlobData(&srcData, tmpData6, HKS_BLOB_TYPE_RAW, NUM8, 0);

    struct hks_blob output = { 0 };
    const char tmpData7[] = "303132333435363730313233343536373031323334353637303132333435363730313233343"
    "5363730313233343536373031323334353637303132333435";
    BuildBlobData(&output, tmpData7, HKS_BLOB_TYPE_RAW, NUM62, 0);

    uint32_t alg = hks_alg_hmac(HKS_ALG_HASH_SHA_512);
    int32_t status = hks_hmac(&key, alg, &srcData, &output);
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&srcData);
    HksBlobDestroyT1(&output);
    EXPECT_EQ(NUM1007, status);
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Hmac
// end+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++1240-1380