SecurityDataHuksDecTest.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
    * 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 SecurityDataHuksDecTestSuite : 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
M
mamingshuai 已提交
33
    virtual void SetUp()
W
wenjun 已提交
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
    {
    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() {}
};

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Decrypt key
M
mamingshuai 已提交
69
// begin++++++++++++++++++++++++++++++++++++++++++++++++++++0760-0990
W
wenjun 已提交
70 71

/*
M
mamingshuai 已提交
72 73
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0760
 * @tc.name      : Aead Decrypt, 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(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0760, 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_0770
 * @tc.name      : Aead Decrypt, 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(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0770, 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_0780
 * @tc.name      : Aead Decrypt, 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(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0780, Function | MediumTest | Level1)
W
wenjun 已提交
203 204 205 206
{
    char alias[] = "test_hks_aead_encrypt";
    int32_t statusEncrypt;
    int32_t statusDecrypt;
M
mamingshuai 已提交
207

W
wenjun 已提交
208 209
    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_0790
 * @tc.name      : Aead Decrypt, abnormal input parameters key is null
W
wenjun 已提交
263 264
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
265
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0790, Function | MediumTest | Level2)
W
wenjun 已提交
266 267 268
{
    char alias[] = "test_hks_aead_decrypt";

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

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

    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 已提交
286
    struct hks_blob nonce = {0};
W
wenjun 已提交
287 288 289
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

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

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

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

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], ciphertext1[NUM100];

    int32_t status;
    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
    key.data = (uint8_t *)key1;
M
mamingshuai 已提交
324
    key.size = NUM32;
W
wenjun 已提交
325 326 327 328 329 330 331 332 333 334 335 336
    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 已提交
337
    struct hks_blob nonce = {0};
W
wenjun 已提交
338 339 340
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

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

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM1006, status);
}

/*
M
mamingshuai 已提交
361 362
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0810
 * @tc.name      : Aead Decrypt, abnormal input parameters key.data is null
W
wenjun 已提交
363 364
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
365
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0810, Function | MediumTest | Level2)
W
wenjun 已提交
366 367 368 369 370 371 372 373
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], ciphertext1[NUM100];

    int32_t status;
    struct hks_blob key;
    key.type = HKS_BLOB_TYPE_KEY;
M
mamingshuai 已提交
374
    key.data = NULL;
W
wenjun 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388
    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 已提交
389
    struct hks_blob nonce = {0};
W
wenjun 已提交
390 391 392
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

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

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM1006, status);
}

/*
M
mamingshuai 已提交
413 414
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0820
 * @tc.name      : Aead Decrypt, abnormal input parameters key.type is not equal to HKS_KEY_TYPE_AES
W
wenjun 已提交
415 416
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
417
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0820, Function | MediumTest | Level2)
W
wenjun 已提交
418 419 420 421 422 423 424
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], ciphertext1[NUM100];

    int32_t status;
    struct hks_blob key;
M
mamingshuai 已提交
425
    key.type = 0;
W
wenjun 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    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 已提交
441
    struct hks_blob nonce = {0};
W
wenjun 已提交
442 443 444
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
445
    struct hks_blob aad = {0};
W
wenjun 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM1006, status);
}

/*
M
mamingshuai 已提交
465 466
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0830
 * @tc.name      : Aead Decrypt, abnormal input parameters keyParam is null
W
wenjun 已提交
467 468
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
469
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0830, Function | MediumTest | Level2)
W
wenjun 已提交
470 471 472 473 474 475 476 477 478
{
    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
479
    struct hks_key_param *keyParam = nullptr;
W
wenjun 已提交
480 481 482

    struct hks_crypt_param cryptParam;

M
mamingshuai 已提交
483
    struct hks_blob nonce = {0};
W
wenjun 已提交
484 485 486
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
487
    struct hks_blob aad = {0};
W
wenjun 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
507 508
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0840
 * @tc.name      : Aead Decrypt, abnormal input parameters keyParam.key_mode is not equal to HKS_ALG_GCM
W
wenjun 已提交
509 510
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
511
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0840, Function | MediumTest | Level2)
W
wenjun 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
527
    keyParam.key_mode = 0;
W
wenjun 已提交
528 529 530 531 532 533 534
    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 已提交
535
    struct hks_blob nonce = {0};
W
wenjun 已提交
536 537 538
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
539
    struct hks_blob aad = {0};
W
wenjun 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
559 560
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0850
 * @tc.name      : Aead Decrypt, abnormal input parameters keyParam.key_len is not equal to 128, 192, 256
W
wenjun 已提交
561 562
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
563
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0850, Function | MediumTest | Level2)
W
wenjun 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
577
    keyParam.key_len = NUM12;
W
wenjun 已提交
578 579 580 581 582 583 584 585 586
    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 已提交
587
    struct hks_blob nonce = {0};
W
wenjun 已提交
588 589 590
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
591
    struct hks_blob aad = {0};
W
wenjun 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
611 612
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0860
 * @tc.name      : Aead Decrypt, abnormal input parameters keyParam.key_type is not equal to HKS_KEY_TYPE_AES
W
wenjun 已提交
613 614
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
615
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0860, Function | MediumTest | Level2)
W
wenjun 已提交
616 617 618 619 620 621 622 623 624 625 626 627
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
628
    keyParam.key_type = 0;
W
wenjun 已提交
629 630 631 632 633 634 635 636 637 638
    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 已提交
639
    struct hks_blob nonce = {0};
W
wenjun 已提交
640 641 642
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
643
    struct hks_blob aad = {0};
W
wenjun 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
663 664
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0870
 * @tc.name      : Aead Decrypt, abnormal input parameters keyParam.key_pad is not equal to HKS_PADDING_NONE
W
wenjun 已提交
665 666
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
667
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0870, Function | MediumTest | Level2)
W
wenjun 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
684
    keyParam.key_pad = NUM88;
W
wenjun 已提交
685 686 687 688 689 690
    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 已提交
691
    struct hks_blob nonce = {0};
W
wenjun 已提交
692 693 694
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
695
    struct hks_blob aad = {0};
W
wenjun 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
715 716 717
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0880
 * @tc.name      : Aead Decrypt, abnormal input parameters keyParam.key_usage
                   is not equal to HKS_KEY_USAGE_ENCRYPT | HKS_KEY_USAGE_DECRYPT
W
wenjun 已提交
718 719
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
720
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0880, Function | MediumTest | Level2)
W
wenjun 已提交
721 722 723 724 725 726 727 728 729 730 731 732 733 734
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
735
    keyParam.key_usage = 0;
W
wenjun 已提交
736 737 738 739 740 741 742 743
    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 已提交
744
    struct hks_blob nonce = {0};
W
wenjun 已提交
745 746 747
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
748
    struct hks_blob aad = {0};
W
wenjun 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM134, status);
}

/*
M
mamingshuai 已提交
768 769
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0890
 * @tc.name      : Aead Decrypt, abnormal input parameters cryptParam is null
W
wenjun 已提交
770 771
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
772
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0890, Function | MediumTest | Level2)
W
wenjun 已提交
773 774
{
    char alias[] = "test_hks_aead_decrypt";
M
mamingshuai 已提交
775
    uint8_t key1[NUM16], decrypted1[NUM100], ciphertext1[NUM100];
W
wenjun 已提交
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

    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 已提交
793
    struct hks_crypt_param *cryptParam = nullptr;
W
wenjun 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
808 809
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0900
 * @tc.name      : Aead Decrypt, abnormal input parameters nonce.size is 0
W
wenjun 已提交
810 811
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
812
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0900, Function | MediumTest | Level2)
W
wenjun 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
836
    struct hks_blob nonce = {0};
W
wenjun 已提交
837
    nonce.data = (uint8_t *)nonce1;
M
mamingshuai 已提交
838
    nonce.size = 0;
W
wenjun 已提交
839

M
mamingshuai 已提交
840
    struct hks_blob aad = {0};
W
wenjun 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
860 861
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0910
 * @tc.name      : Aead Decrypt, abnormal input parameters nonce.data is null
W
wenjun 已提交
862 863
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
864
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0910, Function | MediumTest | Level2)
W
wenjun 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
888 889
    struct hks_blob nonce = {0};
    nonce.data = NULL;
W
wenjun 已提交
890 891
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
892
    struct hks_blob aad = {0};
W
wenjun 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
912 913
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0920
 * @tc.name      : Aead Decrypt, abnormal input parameters aad.size is 0
W
wenjun 已提交
914 915
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
916
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0920, Function | MediumTest | Level2)
W
wenjun 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
940
    struct hks_blob nonce = {0};
W
wenjun 已提交
941 942 943
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
944
    struct hks_blob aad = {0};
W
wenjun 已提交
945
    aad.data = (uint8_t *)aad1;
M
mamingshuai 已提交
946
    aad.size = 0;
W
wenjun 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
964 965
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0930
 * @tc.name      : Aead Decrypt, abnormal input parameters aad.data is null
W
wenjun 已提交
966 967
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
968
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0930, Function | MediumTest | Level2)
W
wenjun 已提交
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
992
    struct hks_blob nonce = {0};
W
wenjun 已提交
993 994 995
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
996 997
    struct hks_blob aad = {0};
    aad.data = NULL;
W
wenjun 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1016 1017
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0940
 * @tc.name      : Aead Decrypt, abnormal input decrypted plaintext is null
W
wenjun 已提交
1018 1019
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1020
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0940, Function | MediumTest | Level2)
W
wenjun 已提交
1021 1022 1023
{
    char alias[] = "test_hks_aead_decrypt";

M
mamingshuai 已提交
1024
    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], ciphertext1[NUM100];
W
wenjun 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043

    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 已提交
1044
    struct hks_blob nonce = {0};
W
wenjun 已提交
1045 1046 1047
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1048
    struct hks_blob aad = {0};
W
wenjun 已提交
1049 1050 1051 1052 1053 1054
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

M
mamingshuai 已提交
1055
    struct hks_blob *decrypted = nullptr;
W
wenjun 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, decrypted, &ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
1066 1067
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0950
 * @tc.name      : Aead Decrypt, abnormal input decrypted plaintext.data is null
W
wenjun 已提交
1068 1069
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1070
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0950, Function | MediumTest | Level2)
W
wenjun 已提交
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
1094
    struct hks_blob nonce = {0};
W
wenjun 已提交
1095 1096 1097
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1098
    struct hks_blob aad = {0};
W
wenjun 已提交
1099 1100 1101 1102 1103 1104 1105
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
M
mamingshuai 已提交
1106
    decrypted.data = NULL;
W
wenjun 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    decrypted.size = sizeof(decrypted1);

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1118 1119
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0960
 * @tc.name      : Aead Decrypt, abnormal input parameters decrypted.size is less than ciphertext.size minus 16
W
wenjun 已提交
1120 1121
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1122
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0960, Function | MediumTest | Level2)
W
wenjun 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
1146
    struct hks_blob nonce = {0};
W
wenjun 已提交
1147 1148 1149
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1150
    struct hks_blob aad = {0};
W
wenjun 已提交
1151 1152 1153 1154 1155 1156 1157 1158
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
M
mamingshuai 已提交
1159
    decrypted.size = NUM15;
W
wenjun 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

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

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1170 1171
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0970
 * @tc.name      : Aead Decrypt, abnormal input parameters ciphertext.size is null
W
wenjun 已提交
1172 1173
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1174
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0970, Function | MediumTest | Level2)
W
wenjun 已提交
1175 1176 1177
{
    char alias[] = "test_hks_aead_decrypt";

M
mamingshuai 已提交
1178
    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100];
W
wenjun 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197

    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 已提交
1198
    struct hks_blob nonce = {0};
W
wenjun 已提交
1199 1200 1201
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1202
    struct hks_blob aad = {0};
W
wenjun 已提交
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

M
mamingshuai 已提交
1213
    struct hks_blob *ciphertext = nullptr;
W
wenjun 已提交
1214 1215 1216 1217 1218 1219

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, ciphertext);
    EXPECT_EQ(NUM1000, status);
}

/*
M
mamingshuai 已提交
1220 1221
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0980
 * @tc.name      : Aead Decrypt, abnormal input parameters ciphertext.data is null
W
wenjun 已提交
1222 1223
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1224
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0980, Function | MediumTest | Level2)
W
wenjun 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
1248
    struct hks_blob nonce = {0};
W
wenjun 已提交
1249 1250 1251
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1252
    struct hks_blob aad = {0};
W
wenjun 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

    struct hks_blob ciphertext;
M
mamingshuai 已提交
1264
    ciphertext.data = NULL;
W
wenjun 已提交
1265 1266 1267 1268 1269 1270 1271
    ciphertext.size = sizeof(ciphertext1);

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

/*
M
mamingshuai 已提交
1272 1273
 * @tc.number    : SUB_SEC_DataPro_HuksL1_0990
 * @tc.name      : Aead Decrypt, abnormal input parameters ciphertext.size is less than 16
W
wenjun 已提交
1274 1275
 * @tc.desc      : [C- SECURITY -1500]
 */
M
mamingshuai 已提交
1276
HWTEST_F(SecurityDataHuksDecTestSuite, securityDataAeadDecrypt0990, Function | MediumTest | Level2)
W
wenjun 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
{
    char alias[] = "test_hks_aead_decrypt";

    uint8_t key1[NUM16], nonce1[NUM16], aad1[NUM16], decrypted1[NUM100], 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 已提交
1300
    struct hks_blob nonce = {0};
W
wenjun 已提交
1301 1302 1303
    nonce.data = (uint8_t *)nonce1;
    nonce.size = sizeof(nonce1);

M
mamingshuai 已提交
1304
    struct hks_blob aad = {0};
W
wenjun 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
    aad.data = (uint8_t *)aad1;
    aad.size = sizeof(aad1);

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

    struct hks_blob decrypted;
    decrypted.data = (uint8_t *)decrypted1;
    decrypted.size = sizeof(decrypted1);

    struct hks_blob ciphertext;
    ciphertext.data = (uint8_t *)ciphertext1;
M
mamingshuai 已提交
1317
    ciphertext.size = NUM10;
W
wenjun 已提交
1318 1319 1320 1321 1322 1323

    status = hks_aead_decrypt(&key, &keyParam, &cryptParam, &decrypted, &ciphertext);
    EXPECT_EQ(NUM135, status);
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Decrypt key
M
mamingshuai 已提交
1324 1325
// begin++++++++++++++++++++++++++++++++++++++++++++++++++++0760-0990