SecurityDataHuksEncTest.cpp 41.0 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
 * 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 SecurityDataHuksEncTestSuite : 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() {}
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Encrypt key
M
mamingshuai 已提交
69
// begin++++++++++++++++++++++++++++++++++++++++++++++++++++0520-0750
W
wenjun 已提交
70 71

/*
M
mamingshuai 已提交
72 73
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0520
 * @tc.name      : Aead Encrypt, normal input parameters key, keyParam.ken_len is 128, cryptParam, plaintext, ciphertext
W
wenjun 已提交
74 75
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
76
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0520, Function | MediumTest | Level1)
W
wenjun 已提交
77 78 79 80 81 82 83
{
    char alias[] = "test_hks_aead_encrypt";
    int32_t statusEncrypt;
    int32_t statusDecrypt;

    struct hks_blob key;
    HksStBlobInit1(&key, 1, NUM16, HKS_BLOB_TYPE_KEY);
M
mamingshuai 已提交
84
    hks_generate_random(&key);
W
wenjun 已提交
85 86 87 88 89 90 91 92 93 94 95 96

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;
M
mamingshuai 已提交
97
    struct hks_blob nonce = {0};
W
wenjun 已提交
98
    HksStBlobInit1(&nonce, 1, NUM16, HKS_BLOB_TYPE_IV);
M
mamingshuai 已提交
99
    hks_generate_random(&nonce);
W
wenjun 已提交
100

M
mamingshuai 已提交
101
    struct hks_blob aad = {0};
W
wenjun 已提交
102
    HksStBlobInit1(&aad, 1, NUM16, HKS_BLOB_TYPE_AAD);
M
mamingshuai 已提交
103
    hks_generate_random(&aad);
W
wenjun 已提交
104 105 106 107 108 109

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    HksStBlobInit1(&plaintext, 1, NUM64, HKS_BLOB_TYPE_PLAIN_TEXT);
M
mamingshuai 已提交
110
    hks_generate_random(&plaintext);
W
wenjun 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    struct hks_blob ciphertext;
    HksStBlobInit1(&ciphertext, 1, NUM64 + HKS_SALT_MAX_SIZE, HKS_BLOB_TYPE_CIPHER_TEXT);
    statusEncrypt = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(0, statusEncrypt);

    struct hks_blob decrypted;
    HksStBlobInit1(&decrypted, 1, NUM64, HKS_BLOB_TYPE_PLAIN_TEXT);
    statusDecrypt = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(0, statusDecrypt);

    size_t k;
    for (k = 0; k < decrypted.size; k++) {
        EXPECT_EQ(plaintext.data[k], decrypted.data[k]);
    }
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&nonce);
    HksBlobDestroyT1(&aad);
    HksBlobDestroyT1(&plaintext);
    HksBlobDestroyT1(&ciphertext);
    HksBlobDestroyT1(&decrypted);
};

/*
M
mamingshuai 已提交
135 136
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0530
 * @tc.name      : Aead Encrypt, normal input parameters key, keyParam.ken_len is 192, cryptParam, plaintext, ciphertext
W
wenjun 已提交
137 138
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
139
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0530, Function | MediumTest | Level1)
W
wenjun 已提交
140 141 142 143 144 145 146
{
    char alias[] = "test_hks_aead_encrypt";
    int32_t statusEncrypt;
    int32_t statusDecrypt;

    struct hks_blob key;
    HksStBlobInit1(&key, 1, NUM24, HKS_BLOB_TYPE_KEY);
M
mamingshuai 已提交
147
    hks_generate_random(&key);
W
wenjun 已提交
148 149 150 151 152 153 154 155 156 157 158 159

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM192;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;
M
mamingshuai 已提交
160
    struct hks_blob nonce = {0};
W
wenjun 已提交
161
    HksStBlobInit1(&nonce, 1, NUM16, HKS_BLOB_TYPE_IV);
M
mamingshuai 已提交
162
    hks_generate_random(&nonce);
W
wenjun 已提交
163

M
mamingshuai 已提交
164
    struct hks_blob aad = {0};
W
wenjun 已提交
165
    HksStBlobInit1(&aad, 1, NUM16, HKS_BLOB_TYPE_AAD);
M
mamingshuai 已提交
166
    hks_generate_random(&aad);
W
wenjun 已提交
167 168 169 170 171 172

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    HksStBlobInit1(&plaintext, 1, NUM64, HKS_BLOB_TYPE_PLAIN_TEXT);
M
mamingshuai 已提交
173
    hks_generate_random(&plaintext);
W
wenjun 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    struct hks_blob ciphertext;
    HksStBlobInit1(&ciphertext, 1, NUM64 + HKS_SALT_MAX_SIZE, HKS_BLOB_TYPE_CIPHER_TEXT);
    statusEncrypt = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(0, statusEncrypt);

    struct hks_blob decrypted;
    HksStBlobInit1(&decrypted, 1, NUM64, HKS_BLOB_TYPE_PLAIN_TEXT);
    statusDecrypt = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(0, statusDecrypt);

    size_t k;
    for (k = 0; k < decrypted.size; k++) {
        EXPECT_EQ(plaintext.data[k], decrypted.data[k]);
    }
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&nonce);
    HksBlobDestroyT1(&aad);
    HksBlobDestroyT1(&plaintext);
    HksBlobDestroyT1(&ciphertext);
    HksBlobDestroyT1(&decrypted);
};

/*
M
mamingshuai 已提交
198 199
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0540
 * @tc.name      : Aead Encrypt, normal input parameters key, keyParam.ken_len is 256, cryptParam, plaintext, ciphertext
W
wenjun 已提交
200 201
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
202
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0540, Function | MediumTest | Level1)
W
wenjun 已提交
203 204 205 206 207 208 209
{
    char alias[] = "test_hks_aead_encrypt";
    int32_t statusEncrypt;
    int32_t statusDecrypt;

    struct hks_blob key;
    HksStBlobInit1(&key, 1, NUM32, HKS_BLOB_TYPE_KEY);
M
mamingshuai 已提交
210
    hks_generate_random(&key);
W
wenjun 已提交
211 212 213 214 215 216 217 218 219 220 221 222

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM256;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;
M
mamingshuai 已提交
223
    struct hks_blob nonce = {0};
W
wenjun 已提交
224
    HksStBlobInit1(&nonce, 1, NUM16, HKS_BLOB_TYPE_IV);
M
mamingshuai 已提交
225
    hks_generate_random(&nonce);
W
wenjun 已提交
226

M
mamingshuai 已提交
227
    struct hks_blob aad = {0};
W
wenjun 已提交
228
    HksStBlobInit1(&aad, 1, NUM16, HKS_BLOB_TYPE_AAD);
M
mamingshuai 已提交
229
    hks_generate_random(&aad);
W
wenjun 已提交
230 231 232 233 234 235

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    HksStBlobInit1(&plaintext, 1, NUM64, HKS_BLOB_TYPE_PLAIN_TEXT);
M
mamingshuai 已提交
236
    hks_generate_random(&plaintext);
W
wenjun 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

    struct hks_blob ciphertext;
    HksStBlobInit1(&ciphertext, 1, NUM64 + HKS_SALT_MAX_SIZE, HKS_BLOB_TYPE_CIPHER_TEXT);
    statusEncrypt = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(0, statusEncrypt);

    struct hks_blob decrypted;
    HksStBlobInit1(&decrypted, 1, NUM64, HKS_BLOB_TYPE_PLAIN_TEXT);
    statusDecrypt = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(0, statusDecrypt);

    size_t k;
    for (k = 0; k < decrypted.size; k++) {
        EXPECT_EQ(plaintext.data[k], decrypted.data[k]);
    }
    HksBlobDestroyT1(&key);
    HksBlobDestroyT1(&nonce);
    HksBlobDestroyT1(&aad);
    HksBlobDestroyT1(&plaintext);
    HksBlobDestroyT1(&ciphertext);
    HksBlobDestroyT1(&decrypted);
};

/*
M
mamingshuai 已提交
261 262
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0550
 * @tc.name      : Aead Encrypt, abnormal input parameters key is null
W
wenjun 已提交
263 264
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
265
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0550, Function | MediumTest | Level2)
W
wenjun 已提交
266 267 268
{
    char alias[] = "test_hks_aead_encrypt";

M
mamingshuai 已提交
269
    uint8_t nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];
W
wenjun 已提交
270 271 272

    int32_t status;

M
mamingshuai 已提交
273
    struct hks_blob *key = nullptr;
W
wenjun 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
287
    struct hks_blob nonce = {0};
W
wenjun 已提交
288 289 290
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
291
    struct hks_blob aad = {0};
W
wenjun 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
311 312
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0560
 * @tc.name      : Aead Encrypt, abnormal input parameters key.size is not equal to keyParam.key_len divided by 8
W
wenjun 已提交
313 314
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
315
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0560, Function | MediumTest | Level2)
W
wenjun 已提交
316 317 318 319 320 321 322 323 324 325
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
M
mamingshuai 已提交
326
    key.size = NUM32;
W
wenjun 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
340
    struct hks_blob nonce = {0};
W
wenjun 已提交
341 342 343
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
344
    struct hks_blob aad = {0};
W
wenjun 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM1006, status);
}

/*
M
mamingshuai 已提交
364 365
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0570
 * @tc.name      : Aead Encrypt, abnormal input parameters key.data is null
W
wenjun 已提交
366 367
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
368
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0570, Function | MediumTest | Level2)
W
wenjun 已提交
369 370 371 372 373 374 375 376 377
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
M
mamingshuai 已提交
378
    key.data = NULL;
W
wenjun 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
393
    struct hks_blob nonce = {0};
W
wenjun 已提交
394 395 396
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
397
    struct hks_blob aad = {0};
W
wenjun 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM1006, status);
}

/*
M
mamingshuai 已提交
417 418
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0580
 * @tc.name      : Aead Encrypt, abnormal input parameters key.type is not equal to HKS_BLOB_TYPE_KEY
W
wenjun 已提交
419 420
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
421
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0580, Function | MediumTest | Level2)
W
wenjun 已提交
422 423 424 425 426 427 428 429
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
M
mamingshuai 已提交
430
    key.type = 0;
W
wenjun 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
446
    struct hks_blob nonce = {0};
W
wenjun 已提交
447 448 449
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
450
    struct hks_blob aad = {0};
W
wenjun 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM1006, status);
}

/*
M
mamingshuai 已提交
470 471
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0590
 * @tc.name      : Aead Encrypt, abnormal input parameters keyParam is null
W
wenjun 已提交
472 473
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
474
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0590, Function | MediumTest | Level2)
W
wenjun 已提交
475 476 477 478 479 480 481 482 483 484
{
    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

M
mamingshuai 已提交
485
    struct hks_key_param *keyParam = nullptr;
W
wenjun 已提交
486 487 488

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
489
    struct hks_blob nonce = {0};
W
wenjun 已提交
490 491 492
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
493
    struct hks_blob aad = {0};
W
wenjun 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
513 514
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0600
 * @tc.name      : Aead Encrypt, abnormal input parameters keyParam.key_mode is not equal to HKS_ALG_GCM
W
wenjun 已提交
515 516
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
517
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0600, Function | MediumTest | Level2)
W
wenjun 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
M
mamingshuai 已提交
534
    keyParam.key_mode = 0;
W
wenjun 已提交
535 536 537 538 539 540 541
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
542
    struct hks_blob nonce = {0};
W
wenjun 已提交
543 544 545
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
546
    struct hks_blob aad = {0};
W
wenjun 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
566 567
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0610
 * @tc.name      : Aead Encrypt, abnormal input parameters keyParam.key_len is not equal to 128, 192, 256
W
wenjun 已提交
568 569
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
570
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0610, Function | MediumTest | Level2)
W
wenjun 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
M
mamingshuai 已提交
585
    keyParam.key_len = NUM11;
W
wenjun 已提交
586 587 588 589 590 591 592 593 594
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
595
    struct hks_blob nonce = {0};
W
wenjun 已提交
596 597 598
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
599
    struct hks_blob aad = {0};
W
wenjun 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
619 620
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0620
 * @tc.name      : Aead Encrypt, abnormal input parameters keyParam.key_type is not equal to HKS_KEY_TYPE_AES
W
wenjun 已提交
621 622
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
623
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0620, Function | MediumTest | Level2)
W
wenjun 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
M
mamingshuai 已提交
637
    keyParam.key_type = 0;
W
wenjun 已提交
638 639 640 641 642 643 644 645 646 647
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
648
    struct hks_blob nonce = {0};
W
wenjun 已提交
649 650 651
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
652
    struct hks_blob aad = {0};
W
wenjun 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
672 673
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0630
 * @tc.name      : Aead Encrypt, abnormal input parameters keyParam.key_pad is not equal to HKS_PADDING_NONE
W
wenjun 已提交
674 675
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
676
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0630, Function | MediumTest | Level2)
W
wenjun 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;
    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
M
mamingshuai 已提交
693
    keyParam.key_pad = NUM88;
W
wenjun 已提交
694 695 696 697 698 699
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
700
    struct hks_blob nonce = {0};
W
wenjun 已提交
701 702 703
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
704
    struct hks_blob aad = {0};
W
wenjun 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
724 725 726
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0640
 * @tc.name      : Aead Encrypt, abnormal input parameters keyParam.key_usage
                   is not equal to HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT
W
wenjun 已提交
727 728
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
729
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0640, Function | MediumTest | Level2)
W
wenjun 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
M
mamingshuai 已提交
745
    keyParam.key_usage = 0;
W
wenjun 已提交
746 747 748 749 750 751 752 753
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
754
    struct hks_blob nonce = {0};
W
wenjun 已提交
755 756 757
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
758
    struct hks_blob aad = {0};
W
wenjun 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
778 779
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0650
 * @tc.name      : Aead Encrypt, abnormal input parameters cryptParam is null
W
wenjun 已提交
780 781
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
782
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0650, Function | MediumTest | Level2)
W
wenjun 已提交
783 784 785
{
    char alias[] = "test_hks_aead_encrypt";

M
mamingshuai 已提交
786
    uint8_t key1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];
W
wenjun 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

M
mamingshuai 已提交
805
    struct hks_crypt_param *cryptParam = nullptr;
W
wenjun 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820


    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
821 822
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0660
 * @tc.name      : Aead Encrypt, abnormal input parameters nonce.size is less than 12
W
wenjun 已提交
823 824
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
825
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0660, Function | MediumTest | Level2)
W
wenjun 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
850
    struct hks_blob nonce = {0};
W
wenjun 已提交
851
    nonce.data = (uint8_t *)nonce1;
M
mamingshuai 已提交
852
    nonce.size = NUM10;
W
wenjun 已提交
853

M
mamingshuai 已提交
854
    struct hks_blob aad = {0};
W
wenjun 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
874 875
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0670
 * @tc.name      : Aead Encrypt, abnormal input parameters nonce.data is null
W
wenjun 已提交
876 877
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
878
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0670, Function | MediumTest | Level2)
W
wenjun 已提交
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
903 904
    struct hks_blob nonce = {0};
    nonce.data = NULL;
W
wenjun 已提交
905 906
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
907
    struct hks_blob aad = {0};
W
wenjun 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
927 928
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0680
 * @tc.name      : Aead Encrypt, abnormal input parameters aad.size is 0
W
wenjun 已提交
929 930
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
931
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0680, Function | MediumTest | Level2)
W
wenjun 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
956
    struct hks_blob nonce = {0};
W
wenjun 已提交
957 958 959
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
960
    struct hks_blob aad = {0};
W
wenjun 已提交
961
    aad.data = (uint8_t *)aad1;
M
mamingshuai 已提交
962
    aad.size = 0;
W
wenjun 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
980 981
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0690
 * @tc.name      : Aead Encrypt, abnormal input parameters aad.data is null
W
wenjun 已提交
982 983
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
984
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0690, Function | MediumTest | Level2)
W
wenjun 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
1009
    struct hks_blob nonce = {0};
W
wenjun 已提交
1010 1011 1012
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1013 1014
    struct hks_blob aad = {0};
    aad.data = NULL;
W
wenjun 已提交
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1033 1034
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0700
 * @tc.name      : Aead Encrypt, abnormal input parameters plaintext is null
W
wenjun 已提交
1035 1036
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1037
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0700, Function | MediumTest | Level2)
W
wenjun 已提交
1038 1039 1040
{
    char alias[] = "test_hks_aead_encrypt";

M
mamingshuai 已提交
1041
    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], ciphertext1[NUM100];
W
wenjun 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
1062
    struct hks_blob nonce = {0};
W
wenjun 已提交
1063 1064 1065
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1066
    struct hks_blob aad = {0};
W
wenjun 已提交
1067 1068 1069 1070 1071 1072
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

M
mamingshuai 已提交
1073
    struct hks_blob *plaintext = nullptr;
W
wenjun 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, plaintext, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
1084 1085
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0710
 * @tc.name      : Aead Encrypt, abnormal input parameters plaintext.data is null
W
wenjun 已提交
1086 1087
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1088
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0710, Function | MediumTest | Level2)
W
wenjun 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
1113
    struct hks_blob nonce = {0};
W
wenjun 已提交
1114 1115 1116
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1117
    struct hks_blob aad = {0};
W
wenjun 已提交
1118 1119 1120 1121 1122 1123 1124
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
M
mamingshuai 已提交
1125
    plaintext.data = NULL;
W
wenjun 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1137 1138
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0720
 * @tc.name      : Aead Encrypt, abnormal input parameters plaintext.size is 0
W
wenjun 已提交
1139 1140
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1141
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0720, Function | MediumTest | Level2)
W
wenjun 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
1166
    struct hks_blob nonce = {0};
W
wenjun 已提交
1167 1168 1169
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1170
    struct hks_blob aad = {0};
W
wenjun 已提交
1171 1172 1173 1174 1175 1176 1177 1178
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
M
mamingshuai 已提交
1179
    plaintext.size = 0;
W
wenjun 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1190 1191
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0730
 * @tc.name      : Aead Encrypt, abnormal input parameters ciphertext is null
W
wenjun 已提交
1192 1193
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1194
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0730, Function | MediumTest | Level2)
W
wenjun 已提交
1195 1196 1197
{
    char alias[] = "test_hks_aead_encrypt";

M
mamingshuai 已提交
1198
    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64];
W
wenjun 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
1219
    struct hks_blob nonce = {0};
W
wenjun 已提交
1220 1221 1222
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1223
    struct hks_blob aad = {0};
W
wenjun 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

M
mamingshuai 已提交
1234
    struct hks_blob *ciphertext = nullptr;
W
wenjun 已提交
1235 1236 1237 1238 1239
    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
1240 1241
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0740
 * @tc.name      : Aead Encrypt, abnormal input parameters ciphertext.data is null
W
wenjun 已提交
1242 1243
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1244
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0740, Function | MediumTest | Level2)
W
wenjun 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;

    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
1269
    struct hks_blob nonce = {0};
W
wenjun 已提交
1270 1271 1272
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1273
    struct hks_blob aad = {0};
W
wenjun 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
M
mamingshuai 已提交
1285
    ciphertext.data = NULL;
W
wenjun 已提交
1286 1287 1288 1289 1290 1291 1292
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1293 1294
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0750
 * @tc.name      : Aead Encrypt, abnormal input parameters ciphertext.size is less than plaintext.size minus 16
W
wenjun 已提交
1295 1296
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1297
HWTEST_F(SecurityDataHuksEncTestSuite, securityDataAeadEncrypt0750, Function | MediumTest | Level2)
W
wenjun 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
{
    char alias[] = "test_hks_aead_encrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], plaintext1[NUM64], ciphertext1[NUM100];

    int32_t status;
    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
    key.size = sizeof(key1);

    struct hks_key_param keyParam;
    keyParam.key_type = HKS_KEY_TYPE_AES;
    keyParam.key_len = NUM128;
    keyParam.key_usage = HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT;
    keyParam.key_mode = HKS_ALG_GCM;
    keyParam.key_pad = HKS_PADDING_NONE;
    keyParam.key_auth_id.data = (uint8_t *)alias;
    keyParam.key_auth_id.size = sizeof(alias);
    keyParam.key_auth_id.type = HKS_BLOB_TYPE_AUTH_ID;

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
1321
    struct hks_blob nonce = {0};
W
wenjun 已提交
1322 1323 1324
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1325
    struct hks_blob aad = {0};
W
wenjun 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

    cryptParam.nonce = nonce;
    cryptParam.aad = aad;

    struct hks_blob plaintext;
    plaintext.data = (uint8_t *)plaintext1;
    plaintext.size = sizeof(plaintext1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
M
mamingshuai 已提交
1338
    ciphertext.size = NUM10;
W
wenjun 已提交
1339 1340 1341 1342 1343 1344

    status = hks_aead_encrypt(&key, &keyParam, &cryptParam, &plaintext, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Encrypt key
M
mamingshuai 已提交
1345
// end==++++++++++++++++++++++++++++++++++++++++++++++++++++0520-0750
W
wenjun 已提交
1346