1526fd984Sopenharmony_ci/*
2526fd984Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3526fd984Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4526fd984Sopenharmony_ci * you may not use this file except in compliance with the License.
5526fd984Sopenharmony_ci * You may obtain a copy of the License at
6526fd984Sopenharmony_ci *
7526fd984Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8526fd984Sopenharmony_ci *
9526fd984Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10526fd984Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11526fd984Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12526fd984Sopenharmony_ci * See the License for the specific language governing permissions and
13526fd984Sopenharmony_ci * limitations under the License.
14526fd984Sopenharmony_ci */
15526fd984Sopenharmony_ci
16526fd984Sopenharmony_ci#include <mutex>
17526fd984Sopenharmony_ci#include <random>
18526fd984Sopenharmony_ci#include <string>
19526fd984Sopenharmony_ci#include <thread>
20526fd984Sopenharmony_ci#include "securec.h"
21526fd984Sopenharmony_ci
22526fd984Sopenharmony_ci#include <gtest/gtest.h>
23526fd984Sopenharmony_ci
24526fd984Sopenharmony_ci#include "hks_api.h"
25526fd984Sopenharmony_ci#include "hks_config.h"
26526fd984Sopenharmony_ci#include "hks_mem.h"
27526fd984Sopenharmony_ci#include "hks_param.h"
28526fd984Sopenharmony_ci
29526fd984Sopenharmony_ci#define HKS_VERIFY_FINISH_ECC_224_COMMON \
30526fd984Sopenharmony_ci{ .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT }, \
31526fd984Sopenharmony_ci{ .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC }, \
32526fd984Sopenharmony_ci{ .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 }, \
33526fd984Sopenharmony_ci{ .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY }, \
34526fd984Sopenharmony_ci{ .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE }, \
35526fd984Sopenharmony_ci{ .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true }, \
36526fd984Sopenharmony_ci{ .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
37526fd984Sopenharmony_ci
38526fd984Sopenharmony_ciusing namespace testing::ext;
39526fd984Sopenharmony_cinamespace {
40526fd984Sopenharmony_cinamespace {
41526fd984Sopenharmony_ciconst char GENERATE_KEY[] = "This is for generate key";
42526fd984Sopenharmony_ciconst char IMPORT_KEY[] = "This is for import key";
43526fd984Sopenharmony_ciconst uint32_t TEST_HOURS = 6;
44526fd984Sopenharmony_ciconst uint32_t DSA_KEY_SIZE = 1024;
45526fd984Sopenharmony_ciconst uint32_t THREADS_NUM = 15;
46526fd984Sopenharmony_ciconst uint32_t RANDOM_KEY_SIZE = 512;
47526fd984Sopenharmony_ciconst uint32_t MAX_SDK_VERSION_SIZE = 64;
48526fd984Sopenharmony_ciconst uint32_t DERIVE_KEY_SIZE = 64;
49526fd984Sopenharmony_ciconst uint32_t IV_SIZE = 16;
50526fd984Sopenharmony_ciconst uint32_t AAD_SIZE = 16;
51526fd984Sopenharmony_ciconst uint32_t RSA_MESSAGE_SIZE = 6;
52526fd984Sopenharmony_ciconst uint32_t AES_MESSAGE_SIZE = 16;
53526fd984Sopenharmony_ciconst uint32_t COMPLEMENT_LEN = 16;
54526fd984Sopenharmony_ciconst uint32_t KEY_PARAMSET_SIZE = 1024;
55526fd984Sopenharmony_ciconst uint32_t MESSAGE_SIZE = 64;
56526fd984Sopenharmony_ciconst uint32_t TEST_KEY_SIZE = 4096;
57526fd984Sopenharmony_ciconst uint32_t CIPHER_SIZE = 4096;
58526fd984Sopenharmony_ciconst uint32_t MAX_STRING_LENTH = 513;
59526fd984Sopenharmony_ci
60526fd984Sopenharmony_cistatic const struct HksParam CIPHER_PARAMS[] = {
61526fd984Sopenharmony_ci    { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
62526fd984Sopenharmony_ci    { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
63526fd984Sopenharmony_ci    { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
64526fd984Sopenharmony_ci    { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
65526fd984Sopenharmony_ci};
66526fd984Sopenharmony_ci
67526fd984Sopenharmony_ciconst struct HksParam g_PARAM_ALGORITHM_AES = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES };
68526fd984Sopenharmony_ciconst struct HksParam g_PARAM_ALGORITHM_RSA = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_RSA };
69526fd984Sopenharmony_ciconst struct HksParam g_PARAM_ALGORITHM_ECC = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC };
70526fd984Sopenharmony_ci#ifndef _USE_MBEDTLS_
71526fd984Sopenharmony_ciconst struct HksParam g_PARAM_ALGORITHM_DSA = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DSA };
72526fd984Sopenharmony_ci#endif
73526fd984Sopenharmony_ci#ifdef HKS_SUPPORT_DH_C
74526fd984Sopenharmony_ciconst struct HksParam g_PARAM_ALGORITHM_DH = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DH };
75526fd984Sopenharmony_ci#endif
76526fd984Sopenharmony_ciconst struct HksParam g_PARAM_ALGORITHM_ECDH = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECDH };
77526fd984Sopenharmony_ci
78526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_AES_128 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 };
79526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_AES_192 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_192 };
80526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_AES_256 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_256 };
81526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_RSA_512 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_512 };
82526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_RSA_768 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_768 };
83526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_RSA_1024 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_1024 };
84526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_RSA_2048 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_2048 };
85526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_RSA_3072 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_3072 };
86526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_RSA_4096 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_4096 };
87526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_ECC_224 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 };
88526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_ECC_256 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_256 };
89526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_ECC_384 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_384 };
90526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_ECC_521 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_521 };
91526fd984Sopenharmony_ci#ifdef HKS_SUPPORT_DH_C
92526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_DH_2048 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_DH_KEY_SIZE_2048 };
93526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_DH_3072 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_DH_KEY_SIZE_3072 };
94526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_DH_4096 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_DH_KEY_SIZE_4096 };
95526fd984Sopenharmony_ci#endif
96526fd984Sopenharmony_ciconst struct HksParam g_PARAM_KEYSIZE_DSA_1024 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = DSA_KEY_SIZE };
97526fd984Sopenharmony_ci
98526fd984Sopenharmony_ciconst struct HksParam g_PARAM_PADDING_NONE = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE };
99526fd984Sopenharmony_ciconst struct HksParam g_PARAM_PADDING_OAEP = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_OAEP };
100526fd984Sopenharmony_ciconst struct HksParam g_PARAM_PADDING_PKCS1_V1_5 = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS1_V1_5 };
101526fd984Sopenharmony_ciconst struct HksParam g_PARAM_PADDING_PKCS7 = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 };
102526fd984Sopenharmony_ciconst struct HksParam g_PARAM_PADDING_PSS = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PSS };
103526fd984Sopenharmony_ci
104526fd984Sopenharmony_ciconst struct HksParam g_PARAM_MODE_CBC = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC };
105526fd984Sopenharmony_ciconst struct HksParam g_PARAM_MODE_CTR = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CTR };
106526fd984Sopenharmony_ciconst struct HksParam g_PARAM_MODE_ECB = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_ECB };
107526fd984Sopenharmony_ciconst struct HksParam g_PARAM_MODE_GCM = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_GCM };
108526fd984Sopenharmony_ci
109526fd984Sopenharmony_ciconst struct HksParam g_PARAM_DIGEST_NONE = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE };
110526fd984Sopenharmony_ciconst struct HksParam g_PARAM_DIGEST_MD5 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_MD5 };
111526fd984Sopenharmony_ciconst struct HksParam g_PARAM_DIGEST_SHA1 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 };
112526fd984Sopenharmony_ciconst struct HksParam g_PARAM_DIGEST_SHA224 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA224 };
113526fd984Sopenharmony_ciconst struct HksParam g_PARAM_DIGEST_SHA256 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 };
114526fd984Sopenharmony_ciconst struct HksParam g_PARAM_DIGEST_SHA384 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA384 };
115526fd984Sopenharmony_ciconst struct HksParam g_PARAM_DIGEST_SHA512 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA512 };
116526fd984Sopenharmony_ci
117526fd984Sopenharmony_ciinline uint32_t GetRandom()
118526fd984Sopenharmony_ci{
119526fd984Sopenharmony_ci    std::random_device rd;
120526fd984Sopenharmony_ci    return rd();
121526fd984Sopenharmony_ci}
122526fd984Sopenharmony_ci
123526fd984Sopenharmony_civoid AESModeRandForCiper(struct HksParamSet *paramInSet)
124526fd984Sopenharmony_ci{
125526fd984Sopenharmony_ci    struct HksParam tagIv;
126526fd984Sopenharmony_ci    uint8_t iv[IV_SIZE] = {0};
127526fd984Sopenharmony_ci    const struct HksParam *paramMode[] = {
128526fd984Sopenharmony_ci        &g_PARAM_MODE_CBC,
129526fd984Sopenharmony_ci        &g_PARAM_MODE_CTR,
130526fd984Sopenharmony_ci        &g_PARAM_MODE_ECB,
131526fd984Sopenharmony_ci        &g_PARAM_MODE_GCM,
132526fd984Sopenharmony_ci    };
133526fd984Sopenharmony_ci    const struct HksParam *paramPadding[] = {
134526fd984Sopenharmony_ci        &g_PARAM_PADDING_NONE,
135526fd984Sopenharmony_ci        &g_PARAM_PADDING_PKCS7,
136526fd984Sopenharmony_ci    };
137526fd984Sopenharmony_ci    const uint32_t countOfMode = sizeof(paramMode) / sizeof(struct HksParam *);
138526fd984Sopenharmony_ci    const uint32_t countOfPadding = sizeof(paramPadding) / sizeof(struct HksParam *);
139526fd984Sopenharmony_ci    const uint32_t indexOfMode = GetRandom() % countOfMode;
140526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramMode[indexOfMode], 1);
141526fd984Sopenharmony_ci    if (paramMode[indexOfMode]->uint32Param == HKS_MODE_CBC) {
142526fd984Sopenharmony_ci        tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
143526fd984Sopenharmony_ci        HksAddParams(paramInSet, &tagIv, 1);
144526fd984Sopenharmony_ci        HksAddParams(paramInSet, paramPadding[GetRandom() % countOfPadding], 1);
145526fd984Sopenharmony_ci    } else if (paramMode[indexOfMode]->uint32Param == HKS_MODE_CTR) {
146526fd984Sopenharmony_ci        HksAddParams(paramInSet, &g_PARAM_PADDING_NONE, 1);
147526fd984Sopenharmony_ci        tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
148526fd984Sopenharmony_ci        HksAddParams(paramInSet, &tagIv, 1);
149526fd984Sopenharmony_ci    } else if (paramMode[indexOfMode]->uint32Param == HKS_MODE_ECB) {
150526fd984Sopenharmony_ci        tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
151526fd984Sopenharmony_ci        HksAddParams(paramInSet, &tagIv, 1);
152526fd984Sopenharmony_ci#ifndef _USE_MBEDTLS_
153526fd984Sopenharmony_ci        HksAddParams(paramInSet, paramPadding[GetRandom() % countOfPadding], 1);
154526fd984Sopenharmony_ci#else
155526fd984Sopenharmony_ci        HksAddParams(paramInSet, &g_PARAM_PADDING_NONE, 1);
156526fd984Sopenharmony_ci#endif
157526fd984Sopenharmony_ci    } else if (paramMode[indexOfMode]->uint32Param == HKS_MODE_GCM) {
158526fd984Sopenharmony_ci        HksAddParams(paramInSet, &g_PARAM_PADDING_NONE, 1);
159526fd984Sopenharmony_ci        uint8_t aadData[AAD_SIZE] = { 'a', 'a', 'd' };
160526fd984Sopenharmony_ci        struct HksParam aad = { .tag = HKS_TAG_ASSOCIATED_DATA, .blob = { .size = sizeof(aadData), .data = aadData } };
161526fd984Sopenharmony_ci        HksAddParams(paramInSet, &aad, 1);
162526fd984Sopenharmony_ci        tagIv = { .tag = HKS_TAG_NONCE, .blob = { .size = IV_SIZE, .data = iv } };
163526fd984Sopenharmony_ci        HksAddParams(paramInSet, &tagIv, 1);
164526fd984Sopenharmony_ci    }
165526fd984Sopenharmony_ci}
166526fd984Sopenharmony_ci
167526fd984Sopenharmony_civoid ParamForAESCipher(struct HksParamSet *paramInSet)
168526fd984Sopenharmony_ci{
169526fd984Sopenharmony_ci    const struct HksParam *paramKeySize[] = {
170526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_AES_128,
171526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_AES_192,
172526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_AES_256,
173526fd984Sopenharmony_ci    };
174526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
175526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
176526fd984Sopenharmony_ci    AESModeRandForCiper(paramInSet);
177526fd984Sopenharmony_ci}
178526fd984Sopenharmony_ci
179526fd984Sopenharmony_civoid RSAKeySizeRand(struct HksParamSet *paramInSet)
180526fd984Sopenharmony_ci{
181526fd984Sopenharmony_ci    const struct HksParam *paramKeySize[] = {
182526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_512,
183526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_768,
184526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_1024,
185526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_2048,
186526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_3072,
187526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_4096,
188526fd984Sopenharmony_ci    };
189526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
190526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
191526fd984Sopenharmony_ci}
192526fd984Sopenharmony_ci
193526fd984Sopenharmony_civoid RSAKeySizeWithout512(struct HksParamSet *paramInSet)
194526fd984Sopenharmony_ci{
195526fd984Sopenharmony_ci    const struct HksParam *paramKeySize[] = {
196526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_768,
197526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_1024,
198526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_2048,
199526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_3072,
200526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_4096,
201526fd984Sopenharmony_ci    };
202526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
203526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
204526fd984Sopenharmony_ci}
205526fd984Sopenharmony_ci
206526fd984Sopenharmony_civoid RSAKeySizeWithout768(struct HksParamSet *paramInSet)
207526fd984Sopenharmony_ci{
208526fd984Sopenharmony_ci    const struct HksParam *paramKeySize[] = {
209526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_1024,
210526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_2048,
211526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_3072,
212526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_4096,
213526fd984Sopenharmony_ci    };
214526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
215526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
216526fd984Sopenharmony_ci}
217526fd984Sopenharmony_ci
218526fd984Sopenharmony_civoid OaepSha512KeySize(struct HksParamSet *paramInSet)
219526fd984Sopenharmony_ci{
220526fd984Sopenharmony_ci    const struct HksParam *paramKeySize[] = {
221526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_2048,
222526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_3072,
223526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_RSA_4096,
224526fd984Sopenharmony_ci    };
225526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
226526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
227526fd984Sopenharmony_ci}
228526fd984Sopenharmony_ci
229526fd984Sopenharmony_civoid ParamForRSACipher(struct HksParamSet *paramInSet)
230526fd984Sopenharmony_ci{
231526fd984Sopenharmony_ci    const struct HksParam *paramPadding[] = {
232526fd984Sopenharmony_ci#ifndef _USE_MBEDTLS_
233526fd984Sopenharmony_ci        &g_PARAM_PADDING_NONE,
234526fd984Sopenharmony_ci#endif
235526fd984Sopenharmony_ci        &g_PARAM_PADDING_PKCS1_V1_5,
236526fd984Sopenharmony_ci        &g_PARAM_PADDING_OAEP,
237526fd984Sopenharmony_ci    };
238526fd984Sopenharmony_ci    const struct HksParam *paramDigest[] = {
239526fd984Sopenharmony_ci        &g_PARAM_DIGEST_NONE,
240526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA1,
241526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA224,
242526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA256,
243526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA384,
244526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA512,
245526fd984Sopenharmony_ci    };
246526fd984Sopenharmony_ci    const uint32_t countOfPadding = sizeof(paramPadding) / sizeof(struct HksParam *);
247526fd984Sopenharmony_ci    const uint32_t countOfDigest = sizeof(paramDigest) / sizeof(struct HksParam *);
248526fd984Sopenharmony_ci    const uint32_t indexOfPadding = GetRandom() % countOfPadding;
249526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramPadding[indexOfPadding], 1);
250526fd984Sopenharmony_ci    if ((paramPadding[indexOfPadding]->uint32Param == HKS_PADDING_NONE) ||
251526fd984Sopenharmony_ci        (paramPadding[indexOfPadding]->uint32Param == HKS_PADDING_PKCS1_V1_5)) {
252526fd984Sopenharmony_ci        RSAKeySizeRand(paramInSet);
253526fd984Sopenharmony_ci        HksAddParams(paramInSet, &g_PARAM_DIGEST_NONE, 1);
254526fd984Sopenharmony_ci    } else if (paramPadding[indexOfPadding]->uint32Param == HKS_PADDING_OAEP) {
255526fd984Sopenharmony_ci        const uint32_t indexOfDigest = GetRandom() % countOfDigest;
256526fd984Sopenharmony_ci        HksAddParams(paramInSet, paramDigest[indexOfDigest], 1);
257526fd984Sopenharmony_ci        if (paramDigest[indexOfDigest]->uint32Param == HKS_DIGEST_SHA256) {
258526fd984Sopenharmony_ci            RSAKeySizeWithout512(paramInSet);
259526fd984Sopenharmony_ci        } else if (paramDigest[indexOfDigest]->uint32Param == HKS_DIGEST_SHA384) {
260526fd984Sopenharmony_ci            RSAKeySizeWithout768(paramInSet);
261526fd984Sopenharmony_ci        } else if (paramDigest[indexOfDigest]->uint32Param == HKS_DIGEST_SHA512) {
262526fd984Sopenharmony_ci            OaepSha512KeySize(paramInSet);
263526fd984Sopenharmony_ci        } else {
264526fd984Sopenharmony_ci            RSAKeySizeRand(paramInSet);
265526fd984Sopenharmony_ci        }
266526fd984Sopenharmony_ci    }
267526fd984Sopenharmony_ci}
268526fd984Sopenharmony_ci
269526fd984Sopenharmony_civoid ParamForCipher(struct HksParamSet *paramInSet)
270526fd984Sopenharmony_ci{
271526fd984Sopenharmony_ci    const struct HksParam *paramAlgorithm[] = {
272526fd984Sopenharmony_ci        &g_PARAM_ALGORITHM_AES,
273526fd984Sopenharmony_ci        &g_PARAM_ALGORITHM_RSA,
274526fd984Sopenharmony_ci    };
275526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramAlgorithm) / sizeof(struct HksParam *);
276526fd984Sopenharmony_ci    const uint32_t index = GetRandom() % count;
277526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramAlgorithm[index], 1);
278526fd984Sopenharmony_ci    if (paramAlgorithm[index]->uint32Param == HKS_ALG_AES) {
279526fd984Sopenharmony_ci        ParamForAESCipher(paramInSet);
280526fd984Sopenharmony_ci    } else if (paramAlgorithm[index]->uint32Param == HKS_ALG_RSA) {
281526fd984Sopenharmony_ci        HksAddParams(paramInSet, &g_PARAM_MODE_ECB, 1);
282526fd984Sopenharmony_ci        ParamForRSACipher(paramInSet);
283526fd984Sopenharmony_ci    }
284526fd984Sopenharmony_ci}
285526fd984Sopenharmony_ci
286526fd984Sopenharmony_civoid ShaForPkcs1(struct HksParamSet *paramInSet)
287526fd984Sopenharmony_ci{
288526fd984Sopenharmony_ci    const struct HksParam *paramDigest[] = {
289526fd984Sopenharmony_ci        &g_PARAM_DIGEST_NONE,
290526fd984Sopenharmony_ci        &g_PARAM_DIGEST_MD5,
291526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA1,
292526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA224,
293526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA256,
294526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA384,
295526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA512,
296526fd984Sopenharmony_ci    };
297526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramDigest) / sizeof(struct HksParam *);
298526fd984Sopenharmony_ci    const uint32_t index = GetRandom() % count;
299526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramDigest[index], 1);
300526fd984Sopenharmony_ci    if ((paramDigest[index]->uint32Param == HKS_DIGEST_SHA384) ||
301526fd984Sopenharmony_ci        (paramDigest[index]->uint32Param == HKS_DIGEST_SHA512)) {
302526fd984Sopenharmony_ci        RSAKeySizeWithout512(paramInSet);
303526fd984Sopenharmony_ci    } else {
304526fd984Sopenharmony_ci        RSAKeySizeRand(paramInSet);
305526fd984Sopenharmony_ci    }
306526fd984Sopenharmony_ci}
307526fd984Sopenharmony_ci
308526fd984Sopenharmony_civoid ShaForPss(struct HksParamSet *paramInSet)
309526fd984Sopenharmony_ci{
310526fd984Sopenharmony_ci    const struct HksParam *paramDigest[] = {
311526fd984Sopenharmony_ci        &g_PARAM_DIGEST_NONE,
312526fd984Sopenharmony_ci        &g_PARAM_DIGEST_MD5,
313526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA1,
314526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA224,
315526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA256,
316526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA384,
317526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA512,
318526fd984Sopenharmony_ci    };
319526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramDigest) / sizeof(struct HksParam *);
320526fd984Sopenharmony_ci    const uint32_t index = GetRandom() % count;
321526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramDigest[index], 1);
322526fd984Sopenharmony_ci    if (paramDigest[index]->uint32Param == HKS_DIGEST_SHA384) {
323526fd984Sopenharmony_ci#ifndef _USE_MBEDTLS_
324526fd984Sopenharmony_ci        RSAKeySizeRand(paramInSet);
325526fd984Sopenharmony_ci#else
326526fd984Sopenharmony_ci        RSAKeySizeWithout512(paramInSet);
327526fd984Sopenharmony_ci#endif
328526fd984Sopenharmony_ci    } else if (paramDigest[index]->uint32Param == HKS_DIGEST_SHA512) {
329526fd984Sopenharmony_ci#ifndef _USE_MBEDTLS_
330526fd984Sopenharmony_ci        RSAKeySizeWithout512(paramInSet);
331526fd984Sopenharmony_ci#else
332526fd984Sopenharmony_ci        RSAKeySizeWithout768(paramInSet);
333526fd984Sopenharmony_ci#endif
334526fd984Sopenharmony_ci    } else {
335526fd984Sopenharmony_ci        RSAKeySizeRand(paramInSet);
336526fd984Sopenharmony_ci    }
337526fd984Sopenharmony_ci}
338526fd984Sopenharmony_ci
339526fd984Sopenharmony_civoid ParamForRSASign(struct HksParamSet *paramInSet)
340526fd984Sopenharmony_ci{
341526fd984Sopenharmony_ci    const struct HksParam *paramPadding[] = {
342526fd984Sopenharmony_ci        &g_PARAM_PADDING_PKCS1_V1_5,
343526fd984Sopenharmony_ci        &g_PARAM_PADDING_PSS,
344526fd984Sopenharmony_ci    };
345526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramPadding) / sizeof(struct HksParam *);
346526fd984Sopenharmony_ci    const uint32_t index = GetRandom() % count;
347526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramPadding[index], 1);
348526fd984Sopenharmony_ci    if (paramPadding[index]->uint32Param == HKS_PADDING_PKCS1_V1_5) {
349526fd984Sopenharmony_ci        ShaForPkcs1(paramInSet);
350526fd984Sopenharmony_ci    } else if (paramPadding[index]->uint32Param == HKS_PADDING_PSS) {
351526fd984Sopenharmony_ci        ShaForPss(paramInSet);
352526fd984Sopenharmony_ci    }
353526fd984Sopenharmony_ci}
354526fd984Sopenharmony_ci
355526fd984Sopenharmony_civoid ParamForECCSign(struct HksParamSet *paramInSet)
356526fd984Sopenharmony_ci{
357526fd984Sopenharmony_ci    const struct HksParam *paramKeySize[] = {
358526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_ECC_224,
359526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_ECC_256,
360526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_ECC_384,
361526fd984Sopenharmony_ci        &g_PARAM_KEYSIZE_ECC_521,
362526fd984Sopenharmony_ci    };
363526fd984Sopenharmony_ci    const uint32_t countOfKeySize = sizeof(paramKeySize) / sizeof(struct HksParam *);
364526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramKeySize[GetRandom() % countOfKeySize], 1);
365526fd984Sopenharmony_ci
366526fd984Sopenharmony_ci    const struct HksParam *paramDigest[] = {
367526fd984Sopenharmony_ci        &g_PARAM_DIGEST_NONE,
368526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA1,
369526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA224,
370526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA256,
371526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA384,
372526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA512,
373526fd984Sopenharmony_ci    };
374526fd984Sopenharmony_ci    const uint32_t countOfDigest = sizeof(paramDigest) / sizeof(struct HksParam *);
375526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramDigest[GetRandom() % countOfDigest], 1);
376526fd984Sopenharmony_ci}
377526fd984Sopenharmony_ci
378526fd984Sopenharmony_civoid ParamForDSASign(struct HksParamSet *paramInSet)
379526fd984Sopenharmony_ci{
380526fd984Sopenharmony_ci    HksAddParams(paramInSet, &g_PARAM_PADDING_PKCS1_V1_5, 1);
381526fd984Sopenharmony_ci    HksAddParams(paramInSet, &g_PARAM_KEYSIZE_DSA_1024, 1);
382526fd984Sopenharmony_ci    const struct HksParam *paramDigest[] = {
383526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA1,
384526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA224,
385526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA256,
386526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA384,
387526fd984Sopenharmony_ci        &g_PARAM_DIGEST_SHA512,
388526fd984Sopenharmony_ci    };
389526fd984Sopenharmony_ci    const uint32_t countOfDigest = sizeof(paramDigest) / sizeof(struct HksParam *);
390526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramDigest[GetRandom() % countOfDigest], 1);
391526fd984Sopenharmony_ci}
392526fd984Sopenharmony_ci
393526fd984Sopenharmony_civoid ParamForSign(struct HksParamSet *paramInSet)
394526fd984Sopenharmony_ci{
395526fd984Sopenharmony_ci    const struct HksParam *paramAlgorithm[] = {
396526fd984Sopenharmony_ci        &g_PARAM_ALGORITHM_RSA,
397526fd984Sopenharmony_ci        &g_PARAM_ALGORITHM_ECC,
398526fd984Sopenharmony_ci#ifndef _USE_MBEDTLS_
399526fd984Sopenharmony_ci        &g_PARAM_ALGORITHM_DSA,
400526fd984Sopenharmony_ci#endif
401526fd984Sopenharmony_ci    };
402526fd984Sopenharmony_ci    const uint32_t count = sizeof(paramAlgorithm) / sizeof(struct HksParam *);
403526fd984Sopenharmony_ci    const uint32_t index = GetRandom() % count;
404526fd984Sopenharmony_ci    HksAddParams(paramInSet, paramAlgorithm[index], 1);
405526fd984Sopenharmony_ci    if (paramAlgorithm[index]->uint32Param == HKS_ALG_RSA) {
406526fd984Sopenharmony_ci        ParamForRSASign(paramInSet);
407526fd984Sopenharmony_ci    } else if (paramAlgorithm[index]->uint32Param == HKS_ALG_ECC) {
408526fd984Sopenharmony_ci        ParamForECCSign(paramInSet);
409526fd984Sopenharmony_ci    } else if (paramAlgorithm[index]->uint32Param == HKS_ALG_DSA) {
410526fd984Sopenharmony_ci        ParamForDSASign(paramInSet);
411526fd984Sopenharmony_ci    }
412526fd984Sopenharmony_ci}
413526fd984Sopenharmony_ci
414526fd984Sopenharmony_civoid ParamForAgree(struct HksParamSet *generateKeyParam, struct HksParamSet *agreeKeyParam)
415526fd984Sopenharmony_ci{
416526fd984Sopenharmony_ci    const struct HksParam *paramAlg[] = {
417526fd984Sopenharmony_ci        &g_PARAM_ALGORITHM_ECDH,
418526fd984Sopenharmony_ci#ifdef HKS_SUPPORT_DH_C
419526fd984Sopenharmony_ci        &g_PARAM_ALGORITHM_DH,
420526fd984Sopenharmony_ci#endif
421526fd984Sopenharmony_ci    };
422526fd984Sopenharmony_ci    const uint32_t countOfAlg = sizeof(paramAlg) / sizeof(struct HksParam *);
423526fd984Sopenharmony_ci    const uint32_t indexOfAlg = GetRandom() % countOfAlg;
424526fd984Sopenharmony_ci
425526fd984Sopenharmony_ci    HksAddParams(agreeKeyParam, paramAlg[indexOfAlg], 1);
426526fd984Sopenharmony_ci    if (paramAlg[indexOfAlg]->uint32Param == HKS_ALG_ECDH) {
427526fd984Sopenharmony_ci        HksAddParams(generateKeyParam, &g_PARAM_ALGORITHM_ECC, 1);
428526fd984Sopenharmony_ci        const struct HksParam *paramKeySize[] = {
429526fd984Sopenharmony_ci            &g_PARAM_KEYSIZE_ECC_224,
430526fd984Sopenharmony_ci            &g_PARAM_KEYSIZE_ECC_256,
431526fd984Sopenharmony_ci            &g_PARAM_KEYSIZE_ECC_384,
432526fd984Sopenharmony_ci            &g_PARAM_KEYSIZE_ECC_521,
433526fd984Sopenharmony_ci        };
434526fd984Sopenharmony_ci        const uint32_t countOfKeySize = sizeof(paramKeySize) / sizeof(struct HksParam *);
435526fd984Sopenharmony_ci        const uint32_t indexOfKeySize = GetRandom() % countOfKeySize;
436526fd984Sopenharmony_ci        HksAddParams(generateKeyParam, paramKeySize[indexOfKeySize], 1);
437526fd984Sopenharmony_ci        HksAddParams(agreeKeyParam, paramKeySize[indexOfKeySize], 1);
438526fd984Sopenharmony_ci        HksAddParams(generateKeyParam, &g_PARAM_DIGEST_NONE, 1);
439526fd984Sopenharmony_ci    }
440526fd984Sopenharmony_ci#ifdef HKS_SUPPORT_DH_C
441526fd984Sopenharmony_ci    if (paramAlg[indexOfAlg]->uint32Param == HKS_ALG_DH) {
442526fd984Sopenharmony_ci        HksAddParams(generateKeyParam, &g_PARAM_ALGORITHM_DH, 1);
443526fd984Sopenharmony_ci        const struct HksParam *paramKeySize[] = {
444526fd984Sopenharmony_ci            &g_PARAM_KEYSIZE_DH_2048,
445526fd984Sopenharmony_ci            &g_PARAM_KEYSIZE_DH_3072,
446526fd984Sopenharmony_ci            &g_PARAM_KEYSIZE_DH_4096,
447526fd984Sopenharmony_ci        };
448526fd984Sopenharmony_ci        const uint32_t countOfKeySize = sizeof(paramKeySize) / sizeof(struct HksParam *);
449526fd984Sopenharmony_ci        const uint32_t indexOfKeySize = GetRandom() % countOfKeySize;
450526fd984Sopenharmony_ci        HksAddParams(generateKeyParam, paramKeySize[indexOfKeySize], 1);
451526fd984Sopenharmony_ci        HksAddParams(agreeKeyParam, paramKeySize[indexOfKeySize], 1);
452526fd984Sopenharmony_ci    }
453526fd984Sopenharmony_ci#endif
454526fd984Sopenharmony_ci}
455526fd984Sopenharmony_ci}  // namespace
456526fd984Sopenharmony_ci
457526fd984Sopenharmony_ciclass ApiPressureTest : public testing::Test {
458526fd984Sopenharmony_cipublic:
459526fd984Sopenharmony_ci    void SetThreadState(bool threadState)
460526fd984Sopenharmony_ci    {
461526fd984Sopenharmony_ci        std::lock_guard<std::mutex> lock(mutex_);
462526fd984Sopenharmony_ci
463526fd984Sopenharmony_ci        threadState_ = threadState;
464526fd984Sopenharmony_ci    }
465526fd984Sopenharmony_ci    bool GetThreadState()
466526fd984Sopenharmony_ci    {
467526fd984Sopenharmony_ci        std::lock_guard<std::mutex> lock(mutex_);
468526fd984Sopenharmony_ci
469526fd984Sopenharmony_ci        return threadState_;
470526fd984Sopenharmony_ci    }
471526fd984Sopenharmony_ci    void GetSdkVersionTest();
472526fd984Sopenharmony_ci    void InitializeTest();
473526fd984Sopenharmony_ci    void RefreshKeyInfoTest();
474526fd984Sopenharmony_ci    void GenerateKeyTest();
475526fd984Sopenharmony_ci    void ImportKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
476526fd984Sopenharmony_ci    void ExportPublicKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
477526fd984Sopenharmony_ci    void DeleteKeyTest(uint32_t ii);
478526fd984Sopenharmony_ci    void GetKeyParamSetTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
479526fd984Sopenharmony_ci    void KeyExistTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
480526fd984Sopenharmony_ci    void GenerateRandomTest();
481526fd984Sopenharmony_ci    void SignTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet, const struct HksBlob *message);
482526fd984Sopenharmony_ci    void VerifyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet, const struct HksBlob *message,
483526fd984Sopenharmony_ci        const struct HksBlob *signature);
484526fd984Sopenharmony_ci    void EncryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
485526fd984Sopenharmony_ci    void DecryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
486526fd984Sopenharmony_ci        const struct HksBlob *cipherText, uint32_t *inLen);
487526fd984Sopenharmony_ci    void AgreeKeyTest(const struct HksBlob *authId);
488526fd984Sopenharmony_ci    void DeriveKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
489526fd984Sopenharmony_ci    void MacTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
490526fd984Sopenharmony_ci    void HashTest();
491526fd984Sopenharmony_ci    void CipherScene(uint32_t ii);
492526fd984Sopenharmony_ci    void SignScene(uint32_t ii);
493526fd984Sopenharmony_ci    void AgreeScene(uint32_t ii);
494526fd984Sopenharmony_ci
495526fd984Sopenharmony_ciprivate:
496526fd984Sopenharmony_ci    bool threadState_ = false;
497526fd984Sopenharmony_ci    std::mutex mutex_;
498526fd984Sopenharmony_ci};
499526fd984Sopenharmony_ci
500526fd984Sopenharmony_civoid ApiPressureTest::GetSdkVersionTest()
501526fd984Sopenharmony_ci{
502526fd984Sopenharmony_ci    do {
503526fd984Sopenharmony_ci        if (!GetThreadState()) {
504526fd984Sopenharmony_ci            break;
505526fd984Sopenharmony_ci        }
506526fd984Sopenharmony_ci        struct HksBlob sdkVersion = {
507526fd984Sopenharmony_ci            .size = MAX_SDK_VERSION_SIZE,
508526fd984Sopenharmony_ci            .data = (uint8_t *)HksMalloc(MAX_SDK_VERSION_SIZE)
509526fd984Sopenharmony_ci        };
510526fd984Sopenharmony_ci        ASSERT_NE(sdkVersion.data, nullptr);
511526fd984Sopenharmony_ci
512526fd984Sopenharmony_ci        EXPECT_EQ(HksGetSdkVersion(&sdkVersion), HKS_SUCCESS);
513526fd984Sopenharmony_ci
514526fd984Sopenharmony_ci        HKS_FREE(sdkVersion.data);
515526fd984Sopenharmony_ci    } while (1);
516526fd984Sopenharmony_ci}
517526fd984Sopenharmony_ci
518526fd984Sopenharmony_civoid ApiPressureTest::InitializeTest()
519526fd984Sopenharmony_ci{
520526fd984Sopenharmony_ci    do {
521526fd984Sopenharmony_ci        if (!GetThreadState()) {
522526fd984Sopenharmony_ci            break;
523526fd984Sopenharmony_ci        }
524526fd984Sopenharmony_ci        EXPECT_EQ(HksInitialize(), HKS_SUCCESS);
525526fd984Sopenharmony_ci    } while (1);
526526fd984Sopenharmony_ci}
527526fd984Sopenharmony_ci
528526fd984Sopenharmony_civoid ApiPressureTest::RefreshKeyInfoTest()
529526fd984Sopenharmony_ci{
530526fd984Sopenharmony_ci    do {
531526fd984Sopenharmony_ci        if (!GetThreadState()) {
532526fd984Sopenharmony_ci            break;
533526fd984Sopenharmony_ci        }
534526fd984Sopenharmony_ci        EXPECT_EQ(HksRefreshKeyInfo(), HKS_SUCCESS);
535526fd984Sopenharmony_ci    } while (1);
536526fd984Sopenharmony_ci}
537526fd984Sopenharmony_ci
538526fd984Sopenharmony_civoid ApiPressureTest::GenerateKeyTest()
539526fd984Sopenharmony_ci{
540526fd984Sopenharmony_ci    do {
541526fd984Sopenharmony_ci        if (!GetThreadState()) {
542526fd984Sopenharmony_ci            break;
543526fd984Sopenharmony_ci        }
544526fd984Sopenharmony_ci        struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
545526fd984Sopenharmony_ci        struct HksParamSet *paramInSet = nullptr;
546526fd984Sopenharmony_ci        HksInitParamSet(&paramInSet);
547526fd984Sopenharmony_ci        struct HksParam tmpParams[] = {
548526fd984Sopenharmony_ci            HKS_VERIFY_FINISH_ECC_224_COMMON
549526fd984Sopenharmony_ci        };
550526fd984Sopenharmony_ci        HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
551526fd984Sopenharmony_ci        HksBuildParamSet(&paramInSet);
552526fd984Sopenharmony_ci        EXPECT_EQ(HksGenerateKey(&authId, paramInSet, NULL), HKS_SUCCESS);
553526fd984Sopenharmony_ci        HksFreeParamSet(&paramInSet);
554526fd984Sopenharmony_ci    } while (1);
555526fd984Sopenharmony_ci}
556526fd984Sopenharmony_ci
557526fd984Sopenharmony_civoid ApiPressureTest::ImportKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
558526fd984Sopenharmony_ci{
559526fd984Sopenharmony_ci    struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
560526fd984Sopenharmony_ci    ASSERT_NE(pubKey.data, nullptr);
561526fd984Sopenharmony_ci    HksExportPublicKey(authId, paramInSet, &pubKey);
562526fd984Sopenharmony_ci    do {
563526fd984Sopenharmony_ci        if (!GetThreadState()) {
564526fd984Sopenharmony_ci            break;
565526fd984Sopenharmony_ci        }
566526fd984Sopenharmony_ci        struct HksBlob importId = { (uint32_t)strlen(IMPORT_KEY), (uint8_t *)IMPORT_KEY };
567526fd984Sopenharmony_ci        EXPECT_EQ(HksImportKey(&importId, paramInSet, &pubKey), HKS_SUCCESS);
568526fd984Sopenharmony_ci    } while (1);
569526fd984Sopenharmony_ci    HKS_FREE(pubKey.data);
570526fd984Sopenharmony_ci}
571526fd984Sopenharmony_ci
572526fd984Sopenharmony_civoid ApiPressureTest::ExportPublicKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
573526fd984Sopenharmony_ci{
574526fd984Sopenharmony_ci    do {
575526fd984Sopenharmony_ci        if (!GetThreadState()) {
576526fd984Sopenharmony_ci            break;
577526fd984Sopenharmony_ci        }
578526fd984Sopenharmony_ci        struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
579526fd984Sopenharmony_ci        ASSERT_NE(pubKey.data, nullptr);
580526fd984Sopenharmony_ci
581526fd984Sopenharmony_ci        EXPECT_EQ(HksExportPublicKey(authId, paramInSet, &pubKey), HKS_SUCCESS);
582526fd984Sopenharmony_ci
583526fd984Sopenharmony_ci        HKS_FREE(pubKey.data);
584526fd984Sopenharmony_ci    } while (1);
585526fd984Sopenharmony_ci}
586526fd984Sopenharmony_ci
587526fd984Sopenharmony_civoid ApiPressureTest::DeleteKeyTest(uint32_t ii)
588526fd984Sopenharmony_ci{
589526fd984Sopenharmony_ci    do {
590526fd984Sopenharmony_ci        if (!GetThreadState()) {
591526fd984Sopenharmony_ci            break;
592526fd984Sopenharmony_ci        }
593526fd984Sopenharmony_ci        std::string key = "KeyForThread" + std::to_string(ii);
594526fd984Sopenharmony_ci        struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
595526fd984Sopenharmony_ci        struct HksParamSet *paramInSet = nullptr;
596526fd984Sopenharmony_ci        HksInitParamSet(&paramInSet);
597526fd984Sopenharmony_ci        struct HksParam tmpParams[] = {
598526fd984Sopenharmony_ci            HKS_VERIFY_FINISH_ECC_224_COMMON
599526fd984Sopenharmony_ci        };
600526fd984Sopenharmony_ci        HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
601526fd984Sopenharmony_ci        HksBuildParamSet(&paramInSet);
602526fd984Sopenharmony_ci        HksGenerateKey(&authId, paramInSet, NULL);
603526fd984Sopenharmony_ci        EXPECT_EQ(HksDeleteKey(&authId, paramInSet), HKS_SUCCESS);
604526fd984Sopenharmony_ci        HksFreeParamSet(&paramInSet);
605526fd984Sopenharmony_ci    } while (1);
606526fd984Sopenharmony_ci}
607526fd984Sopenharmony_ci
608526fd984Sopenharmony_civoid ApiPressureTest::GetKeyParamSetTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
609526fd984Sopenharmony_ci{
610526fd984Sopenharmony_ci    do {
611526fd984Sopenharmony_ci        struct HksParamSet *paramOutSet = nullptr;
612526fd984Sopenharmony_ci        HksInitParamSet(&paramOutSet);
613526fd984Sopenharmony_ci        struct HksParam localKey = {
614526fd984Sopenharmony_ci            .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
615526fd984Sopenharmony_ci            .blob = { .size = KEY_PARAMSET_SIZE, .data = (uint8_t *)HksMalloc(KEY_PARAMSET_SIZE) }
616526fd984Sopenharmony_ci        };
617526fd984Sopenharmony_ci        ASSERT_NE(localKey.blob.data, nullptr);
618526fd984Sopenharmony_ci        HksAddParams(paramOutSet, &localKey, 1);
619526fd984Sopenharmony_ci        HksBuildParamSet(&paramOutSet);
620526fd984Sopenharmony_ci        if (!GetThreadState()) {
621526fd984Sopenharmony_ci            break;
622526fd984Sopenharmony_ci        }
623526fd984Sopenharmony_ci        EXPECT_EQ(HksGetKeyParamSet(authId, paramInSet, paramOutSet), HKS_SUCCESS);
624526fd984Sopenharmony_ci        HKS_FREE(localKey.blob.data);
625526fd984Sopenharmony_ci        HksFreeParamSet(&paramOutSet);
626526fd984Sopenharmony_ci    } while (1);
627526fd984Sopenharmony_ci}
628526fd984Sopenharmony_ci
629526fd984Sopenharmony_civoid ApiPressureTest::KeyExistTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
630526fd984Sopenharmony_ci{
631526fd984Sopenharmony_ci    do {
632526fd984Sopenharmony_ci        if (!GetThreadState()) {
633526fd984Sopenharmony_ci            break;
634526fd984Sopenharmony_ci        }
635526fd984Sopenharmony_ci        EXPECT_EQ(HksKeyExist(authId, paramInSet), HKS_SUCCESS);
636526fd984Sopenharmony_ci    } while (1);
637526fd984Sopenharmony_ci}
638526fd984Sopenharmony_ci
639526fd984Sopenharmony_civoid ApiPressureTest::GenerateRandomTest()
640526fd984Sopenharmony_ci{
641526fd984Sopenharmony_ci    struct HksBlob authId = { .size = RANDOM_KEY_SIZE, .data = (uint8_t *)HksMalloc(RANDOM_KEY_SIZE) };
642526fd984Sopenharmony_ci    ASSERT_NE(authId.data, nullptr);
643526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
644526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
645526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
646526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
647526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
648526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
649526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
650526fd984Sopenharmony_ci    };
651526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
652526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
653526fd984Sopenharmony_ci    do {
654526fd984Sopenharmony_ci        if (!GetThreadState()) {
655526fd984Sopenharmony_ci            break;
656526fd984Sopenharmony_ci        }
657526fd984Sopenharmony_ci        EXPECT_EQ(HksGenerateRandom(paramInSet, &authId), HKS_SUCCESS);
658526fd984Sopenharmony_ci    } while (1);
659526fd984Sopenharmony_ci    HKS_FREE(authId.data);
660526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
661526fd984Sopenharmony_ci}
662526fd984Sopenharmony_ci
663526fd984Sopenharmony_civoid ApiPressureTest::SignTest(
664526fd984Sopenharmony_ci    const struct HksBlob *authId, const struct HksParamSet *paramInSet, const struct HksBlob *message)
665526fd984Sopenharmony_ci{
666526fd984Sopenharmony_ci    do {
667526fd984Sopenharmony_ci        if (!GetThreadState()) {
668526fd984Sopenharmony_ci            break;
669526fd984Sopenharmony_ci        }
670526fd984Sopenharmony_ci        HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
671526fd984Sopenharmony_ci        ASSERT_NE(signature.data, nullptr);
672526fd984Sopenharmony_ci        EXPECT_EQ(HksSign(authId, paramInSet, message, &signature), HKS_SUCCESS);
673526fd984Sopenharmony_ci        HKS_FREE(signature.data);
674526fd984Sopenharmony_ci    } while (1);
675526fd984Sopenharmony_ci}
676526fd984Sopenharmony_ci
677526fd984Sopenharmony_civoid ApiPressureTest::VerifyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
678526fd984Sopenharmony_ci    const struct HksBlob *message, const struct HksBlob *signature)
679526fd984Sopenharmony_ci{
680526fd984Sopenharmony_ci    do {
681526fd984Sopenharmony_ci        if (!GetThreadState()) {
682526fd984Sopenharmony_ci            break;
683526fd984Sopenharmony_ci        }
684526fd984Sopenharmony_ci        EXPECT_EQ(HksVerify(authId, paramInSet, message, signature), HKS_SUCCESS);
685526fd984Sopenharmony_ci    } while (1);
686526fd984Sopenharmony_ci}
687526fd984Sopenharmony_ci
688526fd984Sopenharmony_civoid ApiPressureTest::EncryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
689526fd984Sopenharmony_ci{
690526fd984Sopenharmony_ci    const char *hexData = "0123456789abcdef";
691526fd984Sopenharmony_ci    uint32_t dataLen = strlen(hexData);
692526fd984Sopenharmony_ci    HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
693526fd984Sopenharmony_ci    uint32_t inLen = dataLen + COMPLEMENT_LEN;
694526fd984Sopenharmony_ci    do {
695526fd984Sopenharmony_ci        if (!GetThreadState()) {
696526fd984Sopenharmony_ci            break;
697526fd984Sopenharmony_ci        }
698526fd984Sopenharmony_ci        HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
699526fd984Sopenharmony_ci        ASSERT_NE(cipherText.data, nullptr);
700526fd984Sopenharmony_ci        EXPECT_EQ(HksEncrypt(authId, paramInSet, &plainText, &cipherText), HKS_SUCCESS);
701526fd984Sopenharmony_ci        HKS_FREE(cipherText.data);
702526fd984Sopenharmony_ci    } while (1);
703526fd984Sopenharmony_ci}
704526fd984Sopenharmony_ci
705526fd984Sopenharmony_civoid ApiPressureTest::DecryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
706526fd984Sopenharmony_ci    const struct HksBlob *cipherText, uint32_t *inLen)
707526fd984Sopenharmony_ci{
708526fd984Sopenharmony_ci    do {
709526fd984Sopenharmony_ci        if (!GetThreadState()) {
710526fd984Sopenharmony_ci            break;
711526fd984Sopenharmony_ci        }
712526fd984Sopenharmony_ci        HksBlob plainTextDecrypt = { .size = *inLen, .data = (uint8_t *)HksMalloc(*inLen) };
713526fd984Sopenharmony_ci        ASSERT_NE(plainTextDecrypt.data, nullptr);
714526fd984Sopenharmony_ci        EXPECT_EQ(HksDecrypt(authId, paramInSet, cipherText, &plainTextDecrypt), HKS_SUCCESS);
715526fd984Sopenharmony_ci        HKS_FREE(plainTextDecrypt.data);
716526fd984Sopenharmony_ci    } while (1);
717526fd984Sopenharmony_ci}
718526fd984Sopenharmony_ci
719526fd984Sopenharmony_civoid ApiPressureTest::AgreeKeyTest(const struct HksBlob *authId)
720526fd984Sopenharmony_ci{
721526fd984Sopenharmony_ci    struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
722526fd984Sopenharmony_ci    ASSERT_NE(pubKey.data, nullptr);
723526fd984Sopenharmony_ci    HksExportPublicKey(authId, NULL, &pubKey);
724526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
725526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
726526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
727526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
728526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECDH },
729526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
730526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
731526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
732526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
733526fd984Sopenharmony_ci    };
734526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
735526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
736526fd984Sopenharmony_ci    do {
737526fd984Sopenharmony_ci        if (!GetThreadState()) {
738526fd984Sopenharmony_ci            break;
739526fd984Sopenharmony_ci        }
740526fd984Sopenharmony_ci        HksBlob agreeKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
741526fd984Sopenharmony_ci        ASSERT_NE(agreeKey.data, nullptr);
742526fd984Sopenharmony_ci        EXPECT_EQ(HksAgreeKey(paramInSet, authId, &pubKey, &agreeKey), HKS_SUCCESS);
743526fd984Sopenharmony_ci        HKS_FREE(agreeKey.data);
744526fd984Sopenharmony_ci    } while (1);
745526fd984Sopenharmony_ci    HKS_FREE(pubKey.data);
746526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
747526fd984Sopenharmony_ci}
748526fd984Sopenharmony_ci
749526fd984Sopenharmony_civoid ApiPressureTest::DeriveKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
750526fd984Sopenharmony_ci{
751526fd984Sopenharmony_ci    do {
752526fd984Sopenharmony_ci        if (!GetThreadState()) {
753526fd984Sopenharmony_ci            break;
754526fd984Sopenharmony_ci        }
755526fd984Sopenharmony_ci        HksBlob derivedKey = { .size = DERIVE_KEY_SIZE, .data = (uint8_t *)HksMalloc(DERIVE_KEY_SIZE) };
756526fd984Sopenharmony_ci        ASSERT_NE(derivedKey.data, nullptr);
757526fd984Sopenharmony_ci        EXPECT_EQ(HksDeriveKey(paramInSet, authId, &derivedKey), HKS_SUCCESS);
758526fd984Sopenharmony_ci        HKS_FREE(derivedKey.data);
759526fd984Sopenharmony_ci    } while (1);
760526fd984Sopenharmony_ci}
761526fd984Sopenharmony_ci
762526fd984Sopenharmony_civoid ApiPressureTest::MacTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
763526fd984Sopenharmony_ci{
764526fd984Sopenharmony_ci    const char *hexData = "0123456789abcdef";
765526fd984Sopenharmony_ci    uint32_t dataLen = strlen(hexData);
766526fd984Sopenharmony_ci    HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
767526fd984Sopenharmony_ci    do {
768526fd984Sopenharmony_ci        if (!GetThreadState()) {
769526fd984Sopenharmony_ci            break;
770526fd984Sopenharmony_ci        }
771526fd984Sopenharmony_ci        HksBlob macMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
772526fd984Sopenharmony_ci        ASSERT_NE(macMessage.data, nullptr);
773526fd984Sopenharmony_ci        EXPECT_EQ(HksMac(authId, paramInSet, &message, &macMessage), HKS_SUCCESS);
774526fd984Sopenharmony_ci        HKS_FREE(macMessage.data);
775526fd984Sopenharmony_ci    } while (1);
776526fd984Sopenharmony_ci}
777526fd984Sopenharmony_ci
778526fd984Sopenharmony_civoid ApiPressureTest::HashTest()
779526fd984Sopenharmony_ci{
780526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
781526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
782526fd984Sopenharmony_ci    struct HksParam digest = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 };
783526fd984Sopenharmony_ci    HksAddParams(paramInSet, &digest, 1);
784526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
785526fd984Sopenharmony_ci    const char *hexData = "0123456789abcdef";
786526fd984Sopenharmony_ci    uint32_t dataLen = strlen(hexData);
787526fd984Sopenharmony_ci    HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
788526fd984Sopenharmony_ci    do {
789526fd984Sopenharmony_ci        if (!GetThreadState()) {
790526fd984Sopenharmony_ci            break;
791526fd984Sopenharmony_ci        }
792526fd984Sopenharmony_ci        HksBlob shaMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
793526fd984Sopenharmony_ci        ASSERT_NE(shaMessage.data, nullptr);
794526fd984Sopenharmony_ci        EXPECT_EQ(HksHash(paramInSet, &message, &shaMessage), HKS_SUCCESS);
795526fd984Sopenharmony_ci        HKS_FREE(shaMessage.data);
796526fd984Sopenharmony_ci    } while (1);
797526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
798526fd984Sopenharmony_ci}
799526fd984Sopenharmony_ci
800526fd984Sopenharmony_civoid ApiPressureTest::CipherScene(uint32_t ii)
801526fd984Sopenharmony_ci{
802526fd984Sopenharmony_ci    std::string key = "KeyForThread" + std::to_string(ii);
803526fd984Sopenharmony_ci    struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
804526fd984Sopenharmony_ci    do {
805526fd984Sopenharmony_ci        if (!GetThreadState()) {
806526fd984Sopenharmony_ci            break;
807526fd984Sopenharmony_ci        }
808526fd984Sopenharmony_ci        struct HksParamSet *paramInSet = nullptr;
809526fd984Sopenharmony_ci        HksInitParamSet(&paramInSet);
810526fd984Sopenharmony_ci        HksAddParams(paramInSet, CIPHER_PARAMS, sizeof(CIPHER_PARAMS) / sizeof(CIPHER_PARAMS[0]));
811526fd984Sopenharmony_ci        ParamForCipher(paramInSet);
812526fd984Sopenharmony_ci        HksBuildParamSet(&paramInSet);
813526fd984Sopenharmony_ci        HksGenerateKey(&authId, paramInSet, NULL);
814526fd984Sopenharmony_ci        struct HksParam *padding = nullptr;
815526fd984Sopenharmony_ci        HksGetParam(paramInSet, HKS_TAG_PADDING, &padding);
816526fd984Sopenharmony_ci        char hexData[MAX_STRING_LENTH] = {0};
817526fd984Sopenharmony_ci        uint32_t dataLen;
818526fd984Sopenharmony_ci        struct HksParam *algorithm = nullptr;
819526fd984Sopenharmony_ci        HksGetParam(paramInSet, HKS_TAG_ALGORITHM, &algorithm);
820526fd984Sopenharmony_ci        if (algorithm->uint32Param == HKS_ALG_RSA) {
821526fd984Sopenharmony_ci            if (padding->uint32Param == HKS_PADDING_NONE) {
822526fd984Sopenharmony_ci                struct HksParam *keySize = nullptr;
823526fd984Sopenharmony_ci                HksGetParam(paramInSet, HKS_TAG_KEY_SIZE, &keySize);
824526fd984Sopenharmony_ci                dataLen = HKS_KEY_BYTES(keySize->uint32Param);
825526fd984Sopenharmony_ci            } else {
826526fd984Sopenharmony_ci                dataLen = RSA_MESSAGE_SIZE;
827526fd984Sopenharmony_ci            }
828526fd984Sopenharmony_ci        } else {
829526fd984Sopenharmony_ci            dataLen = AES_MESSAGE_SIZE;
830526fd984Sopenharmony_ci        }
831526fd984Sopenharmony_ci        HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
832526fd984Sopenharmony_ci        HksBlob cipherText = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
833526fd984Sopenharmony_ci        ASSERT_NE(cipherText.data, nullptr);
834526fd984Sopenharmony_ci        HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
835526fd984Sopenharmony_ci        HksBlob plainTextDecrypt = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
836526fd984Sopenharmony_ci        ASSERT_NE(plainTextDecrypt.data, nullptr);
837526fd984Sopenharmony_ci        EXPECT_EQ(HksDecrypt(&authId, paramInSet, &cipherText, &plainTextDecrypt), HKS_SUCCESS);
838526fd984Sopenharmony_ci        (void)HksDeleteKey(&authId, nullptr);
839526fd984Sopenharmony_ci        HKS_FREE(plainTextDecrypt.data);
840526fd984Sopenharmony_ci        HKS_FREE(cipherText.data);
841526fd984Sopenharmony_ci        HksFreeParamSet(&paramInSet);
842526fd984Sopenharmony_ci    } while (1);
843526fd984Sopenharmony_ci}
844526fd984Sopenharmony_ci
845526fd984Sopenharmony_civoid ApiPressureTest::SignScene(uint32_t ii)
846526fd984Sopenharmony_ci{
847526fd984Sopenharmony_ci    std::string key = "KeyForThread" + std::to_string(ii);
848526fd984Sopenharmony_ci    struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
849526fd984Sopenharmony_ci    do {
850526fd984Sopenharmony_ci        if (!GetThreadState()) {
851526fd984Sopenharmony_ci            break;
852526fd984Sopenharmony_ci        }
853526fd984Sopenharmony_ci        struct HksParamSet *paramInSet = nullptr;
854526fd984Sopenharmony_ci        HksInitParamSet(&paramInSet);
855526fd984Sopenharmony_ci        struct HksParam tmpParams[] = {
856526fd984Sopenharmony_ci            { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
857526fd984Sopenharmony_ci            { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
858526fd984Sopenharmony_ci            { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
859526fd984Sopenharmony_ci            { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
860526fd984Sopenharmony_ci        };
861526fd984Sopenharmony_ci        HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
862526fd984Sopenharmony_ci        ParamForSign(paramInSet);
863526fd984Sopenharmony_ci        HksBuildParamSet(&paramInSet);
864526fd984Sopenharmony_ci        HksGenerateKey(&authId, paramInSet, NULL);
865526fd984Sopenharmony_ci        const char *hexData = "0123456789abcdef";
866526fd984Sopenharmony_ci        uint32_t dataLen = strlen(hexData);
867526fd984Sopenharmony_ci        HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
868526fd984Sopenharmony_ci        HksBlob signature = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
869526fd984Sopenharmony_ci        ASSERT_NE(signature.data, nullptr);
870526fd984Sopenharmony_ci        HksSign(&authId, paramInSet, &message, &signature);
871526fd984Sopenharmony_ci        EXPECT_EQ(HksVerify(&authId, paramInSet, &message, &signature), HKS_SUCCESS);
872526fd984Sopenharmony_ci        (void)HksDeleteKey(&authId, nullptr);
873526fd984Sopenharmony_ci        HKS_FREE(signature.data);
874526fd984Sopenharmony_ci        HksFreeParamSet(&paramInSet);
875526fd984Sopenharmony_ci    } while (1);
876526fd984Sopenharmony_ci}
877526fd984Sopenharmony_ci
878526fd984Sopenharmony_civoid ApiPressureTest::AgreeScene(uint32_t ii)
879526fd984Sopenharmony_ci{
880526fd984Sopenharmony_ci    std::string key = "KeyForThread" + std::to_string(ii);
881526fd984Sopenharmony_ci    struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
882526fd984Sopenharmony_ci    do {
883526fd984Sopenharmony_ci        if (!GetThreadState()) {
884526fd984Sopenharmony_ci            break;
885526fd984Sopenharmony_ci        }
886526fd984Sopenharmony_ci        struct HksParamSet *generateKeyParam = nullptr;
887526fd984Sopenharmony_ci        HksInitParamSet(&generateKeyParam);
888526fd984Sopenharmony_ci        struct HksParamSet *agreeKeyParam = nullptr;
889526fd984Sopenharmony_ci        HksInitParamSet(&agreeKeyParam);
890526fd984Sopenharmony_ci        struct HksParam tmpParams[] = {
891526fd984Sopenharmony_ci            { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
892526fd984Sopenharmony_ci            { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
893526fd984Sopenharmony_ci            { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
894526fd984Sopenharmony_ci            { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
895526fd984Sopenharmony_ci        };
896526fd984Sopenharmony_ci        HksAddParams(generateKeyParam, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
897526fd984Sopenharmony_ci        HksAddParams(agreeKeyParam, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
898526fd984Sopenharmony_ci        ParamForAgree(generateKeyParam, agreeKeyParam);
899526fd984Sopenharmony_ci        HksBuildParamSet(&generateKeyParam);
900526fd984Sopenharmony_ci        HksBuildParamSet(&agreeKeyParam);
901526fd984Sopenharmony_ci        HksGenerateKey(&authId, generateKeyParam, NULL);
902526fd984Sopenharmony_ci        struct HksBlob pubKey = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
903526fd984Sopenharmony_ci        ASSERT_NE(pubKey.data, nullptr);
904526fd984Sopenharmony_ci        HksExportPublicKey(&authId, generateKeyParam, &pubKey);
905526fd984Sopenharmony_ci        HksBlob agreeKey = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
906526fd984Sopenharmony_ci        ASSERT_NE(agreeKey.data, nullptr);
907526fd984Sopenharmony_ci        EXPECT_EQ(HksAgreeKey(agreeKeyParam, &authId, &pubKey, &agreeKey), HKS_SUCCESS);
908526fd984Sopenharmony_ci        (void)HksDeleteKey(&authId, nullptr);
909526fd984Sopenharmony_ci        HKS_FREE(agreeKey.data);
910526fd984Sopenharmony_ci        HKS_FREE(pubKey.data);
911526fd984Sopenharmony_ci        HksFreeParamSet(&generateKeyParam);
912526fd984Sopenharmony_ci        HksFreeParamSet(&agreeKeyParam);
913526fd984Sopenharmony_ci    } while (1);
914526fd984Sopenharmony_ci}
915526fd984Sopenharmony_ci
916526fd984Sopenharmony_ci/**
917526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00100
918526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00100
919526fd984Sopenharmony_ci * @tc.desc      : HksGetSdkVersion
920526fd984Sopenharmony_ci */
921526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00100, TestSize.Level1)
922526fd984Sopenharmony_ci{
923526fd984Sopenharmony_ci    std::vector<std::thread> threads;
924526fd984Sopenharmony_ci    SetThreadState(true);
925526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
926526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::GetSdkVersionTest, this));
927526fd984Sopenharmony_ci    }
928526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
929526fd984Sopenharmony_ci    SetThreadState(false);
930526fd984Sopenharmony_ci    for (auto &t : threads) {
931526fd984Sopenharmony_ci        t.join();
932526fd984Sopenharmony_ci    }
933526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
934526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
935526fd984Sopenharmony_ci}
936526fd984Sopenharmony_ci
937526fd984Sopenharmony_ci/**
938526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00200
939526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00200
940526fd984Sopenharmony_ci * @tc.desc      : HksInitialize
941526fd984Sopenharmony_ci */
942526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00200, TestSize.Level1)
943526fd984Sopenharmony_ci{
944526fd984Sopenharmony_ci    std::vector<std::thread> threads;
945526fd984Sopenharmony_ci    SetThreadState(true);
946526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
947526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::InitializeTest, this));
948526fd984Sopenharmony_ci    }
949526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
950526fd984Sopenharmony_ci    SetThreadState(false);
951526fd984Sopenharmony_ci    for (auto &t : threads) {
952526fd984Sopenharmony_ci        t.join();
953526fd984Sopenharmony_ci    }
954526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
955526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
956526fd984Sopenharmony_ci}
957526fd984Sopenharmony_ci
958526fd984Sopenharmony_ci/**
959526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00300
960526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00300
961526fd984Sopenharmony_ci * @tc.desc      : HksRefreshKeyInfo
962526fd984Sopenharmony_ci */
963526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00300, TestSize.Level1)
964526fd984Sopenharmony_ci{
965526fd984Sopenharmony_ci    std::vector<std::thread> threads;
966526fd984Sopenharmony_ci    SetThreadState(true);
967526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
968526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::RefreshKeyInfoTest, this));
969526fd984Sopenharmony_ci    }
970526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
971526fd984Sopenharmony_ci    SetThreadState(false);
972526fd984Sopenharmony_ci    for (auto &t : threads) {
973526fd984Sopenharmony_ci        t.join();
974526fd984Sopenharmony_ci    }
975526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
976526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
977526fd984Sopenharmony_ci}
978526fd984Sopenharmony_ci
979526fd984Sopenharmony_ci/**
980526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00400
981526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00400
982526fd984Sopenharmony_ci * @tc.desc      : HksGenerateKey
983526fd984Sopenharmony_ci */
984526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00400, TestSize.Level1)
985526fd984Sopenharmony_ci{
986526fd984Sopenharmony_ci    std::vector<std::thread> threads;
987526fd984Sopenharmony_ci    SetThreadState(true);
988526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
989526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::GenerateKeyTest, this));
990526fd984Sopenharmony_ci    }
991526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
992526fd984Sopenharmony_ci    SetThreadState(false);
993526fd984Sopenharmony_ci    for (auto &t : threads) {
994526fd984Sopenharmony_ci        t.join();
995526fd984Sopenharmony_ci    }
996526fd984Sopenharmony_ci
997526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
998526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
999526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
1000526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
1001526fd984Sopenharmony_ci}
1002526fd984Sopenharmony_ci
1003526fd984Sopenharmony_ci/**
1004526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00500
1005526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00500
1006526fd984Sopenharmony_ci * @tc.desc      : HksImportKey
1007526fd984Sopenharmony_ci */
1008526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00500, TestSize.Level1)
1009526fd984Sopenharmony_ci{
1010526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1011526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1012526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1013526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1014526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1015526fd984Sopenharmony_ci        HKS_VERIFY_FINISH_ECC_224_COMMON
1016526fd984Sopenharmony_ci    };
1017526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1018526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1019526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1020526fd984Sopenharmony_ci    SetThreadState(true);
1021526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1022526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::ImportKeyTest, this, &authId, paramInSet));
1023526fd984Sopenharmony_ci    }
1024526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1025526fd984Sopenharmony_ci    SetThreadState(false);
1026526fd984Sopenharmony_ci    for (auto &t : threads) {
1027526fd984Sopenharmony_ci        t.join();
1028526fd984Sopenharmony_ci    }
1029526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1030526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1031526fd984Sopenharmony_ci}
1032526fd984Sopenharmony_ci
1033526fd984Sopenharmony_ci/**
1034526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00600
1035526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00600
1036526fd984Sopenharmony_ci * @tc.desc      : HksExportPublicKey
1037526fd984Sopenharmony_ci */
1038526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00600, TestSize.Level1)
1039526fd984Sopenharmony_ci{
1040526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1041526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1042526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1043526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1044526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1045526fd984Sopenharmony_ci        HKS_VERIFY_FINISH_ECC_224_COMMON
1046526fd984Sopenharmony_ci    };
1047526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1048526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1049526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1050526fd984Sopenharmony_ci    SetThreadState(true);
1051526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1052526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::ExportPublicKeyTest, this, &authId, paramInSet));
1053526fd984Sopenharmony_ci    }
1054526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1055526fd984Sopenharmony_ci    SetThreadState(false);
1056526fd984Sopenharmony_ci    for (auto &t : threads) {
1057526fd984Sopenharmony_ci        t.join();
1058526fd984Sopenharmony_ci    }
1059526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1060526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1061526fd984Sopenharmony_ci}
1062526fd984Sopenharmony_ci
1063526fd984Sopenharmony_ci/**
1064526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00700
1065526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00700
1066526fd984Sopenharmony_ci * @tc.desc      : HksDeleteKey
1067526fd984Sopenharmony_ci */
1068526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00700, TestSize.Level1)
1069526fd984Sopenharmony_ci{
1070526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1071526fd984Sopenharmony_ci    SetThreadState(true);
1072526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1073526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::DeleteKeyTest, this, ii));
1074526fd984Sopenharmony_ci    }
1075526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1076526fd984Sopenharmony_ci    SetThreadState(false);
1077526fd984Sopenharmony_ci    for (auto &t : threads) {
1078526fd984Sopenharmony_ci        t.join();
1079526fd984Sopenharmony_ci    }
1080526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
1081526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
1082526fd984Sopenharmony_ci}
1083526fd984Sopenharmony_ci
1084526fd984Sopenharmony_ci/**
1085526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00800
1086526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00800
1087526fd984Sopenharmony_ci * @tc.desc      : HksGetKeyParamSet
1088526fd984Sopenharmony_ci */
1089526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00800, TestSize.Level1)
1090526fd984Sopenharmony_ci{
1091526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1092526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1093526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1094526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1095526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1096526fd984Sopenharmony_ci        HKS_VERIFY_FINISH_ECC_224_COMMON
1097526fd984Sopenharmony_ci    };
1098526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1099526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1100526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1101526fd984Sopenharmony_ci    SetThreadState(true);
1102526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1103526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::GetKeyParamSetTest, this, &authId, paramInSet));
1104526fd984Sopenharmony_ci    }
1105526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1106526fd984Sopenharmony_ci    SetThreadState(false);
1107526fd984Sopenharmony_ci    for (auto &t : threads) {
1108526fd984Sopenharmony_ci        t.join();
1109526fd984Sopenharmony_ci    }
1110526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1111526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1112526fd984Sopenharmony_ci}
1113526fd984Sopenharmony_ci
1114526fd984Sopenharmony_ci/**
1115526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest00900
1116526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest00900
1117526fd984Sopenharmony_ci * @tc.desc      : HksKeyExist
1118526fd984Sopenharmony_ci */
1119526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest00900, TestSize.Level1)
1120526fd984Sopenharmony_ci{
1121526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1122526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1123526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1124526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1125526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1126526fd984Sopenharmony_ci        HKS_VERIFY_FINISH_ECC_224_COMMON
1127526fd984Sopenharmony_ci    };
1128526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1129526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1130526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1131526fd984Sopenharmony_ci    SetThreadState(true);
1132526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1133526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::KeyExistTest, this, &authId, paramInSet));
1134526fd984Sopenharmony_ci    }
1135526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1136526fd984Sopenharmony_ci    SetThreadState(false);
1137526fd984Sopenharmony_ci    for (auto &t : threads) {
1138526fd984Sopenharmony_ci        t.join();
1139526fd984Sopenharmony_ci    }
1140526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1141526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1142526fd984Sopenharmony_ci}
1143526fd984Sopenharmony_ci
1144526fd984Sopenharmony_ci/**
1145526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01000
1146526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01000
1147526fd984Sopenharmony_ci * @tc.desc      : HksGenerateRandom
1148526fd984Sopenharmony_ci */
1149526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01000, TestSize.Level1)
1150526fd984Sopenharmony_ci{
1151526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1152526fd984Sopenharmony_ci    SetThreadState(true);
1153526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1154526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::GenerateRandomTest, this));
1155526fd984Sopenharmony_ci    }
1156526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1157526fd984Sopenharmony_ci    SetThreadState(false);
1158526fd984Sopenharmony_ci    for (auto &t : threads) {
1159526fd984Sopenharmony_ci        t.join();
1160526fd984Sopenharmony_ci    }
1161526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
1162526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
1163526fd984Sopenharmony_ci}
1164526fd984Sopenharmony_ci
1165526fd984Sopenharmony_ci/**
1166526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01100
1167526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01100
1168526fd984Sopenharmony_ci * @tc.desc      : HksSign
1169526fd984Sopenharmony_ci */
1170526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01100, TestSize.Level1)
1171526fd984Sopenharmony_ci{
1172526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1173526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1174526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1175526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1176526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1177526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1178526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1179526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1180526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
1181526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1182526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1183526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1184526fd984Sopenharmony_ci    };
1185526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1186526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1187526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1188526fd984Sopenharmony_ci    const char *hexData = "0123456789abcdef";
1189526fd984Sopenharmony_ci    uint32_t dataLen = strlen(hexData);
1190526fd984Sopenharmony_ci    HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1191526fd984Sopenharmony_ci    SetThreadState(true);
1192526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1193526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::SignTest, this, &authId, paramInSet, &message));
1194526fd984Sopenharmony_ci    }
1195526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1196526fd984Sopenharmony_ci    SetThreadState(false);
1197526fd984Sopenharmony_ci    for (auto &t : threads) {
1198526fd984Sopenharmony_ci        t.join();
1199526fd984Sopenharmony_ci    }
1200526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1201526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1202526fd984Sopenharmony_ci}
1203526fd984Sopenharmony_ci
1204526fd984Sopenharmony_ci/**
1205526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01200
1206526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01200
1207526fd984Sopenharmony_ci * @tc.desc      : HksVerify
1208526fd984Sopenharmony_ci */
1209526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01200, TestSize.Level1)
1210526fd984Sopenharmony_ci{
1211526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1212526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1213526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1214526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1215526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1216526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1217526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1218526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1219526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
1220526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1221526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1222526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1223526fd984Sopenharmony_ci    };
1224526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1225526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1226526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1227526fd984Sopenharmony_ci    const char *hexData = "0123456789abcdef";
1228526fd984Sopenharmony_ci    uint32_t dataLen = strlen(hexData);
1229526fd984Sopenharmony_ci    HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1230526fd984Sopenharmony_ci    HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1231526fd984Sopenharmony_ci    ASSERT_NE(signature.data, nullptr);
1232526fd984Sopenharmony_ci    HksSign(&authId, paramInSet, &message, &signature);
1233526fd984Sopenharmony_ci    SetThreadState(true);
1234526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1235526fd984Sopenharmony_ci        threads.emplace_back(
1236526fd984Sopenharmony_ci            std::thread(&ApiPressureTest::VerifyTest, this, &authId, paramInSet, &message, &signature));
1237526fd984Sopenharmony_ci    }
1238526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1239526fd984Sopenharmony_ci    SetThreadState(false);
1240526fd984Sopenharmony_ci    for (auto &t : threads) {
1241526fd984Sopenharmony_ci        t.join();
1242526fd984Sopenharmony_ci    }
1243526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1244526fd984Sopenharmony_ci    HKS_FREE(signature.data);
1245526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1246526fd984Sopenharmony_ci}
1247526fd984Sopenharmony_ci
1248526fd984Sopenharmony_ci/**
1249526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01300
1250526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01300
1251526fd984Sopenharmony_ci * @tc.desc      : HksEncrypt
1252526fd984Sopenharmony_ci */
1253526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01300, TestSize.Level1)
1254526fd984Sopenharmony_ci{
1255526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1256526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1257526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1258526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1259526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1260526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1261526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1262526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1263526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE,
1264526fd984Sopenharmony_ci        .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
1265526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1266526fd984Sopenharmony_ci        { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
1267526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1268526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE,
1269526fd984Sopenharmony_ci        .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1270526fd984Sopenharmony_ci        { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
1271526fd984Sopenharmony_ci    };
1272526fd984Sopenharmony_ci    uint8_t iv[IV_SIZE] = {0};
1273526fd984Sopenharmony_ci    struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1274526fd984Sopenharmony_ci    HksAddParams(paramInSet, &tagIv, 1);
1275526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1276526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1277526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1278526fd984Sopenharmony_ci    SetThreadState(true);
1279526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1280526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::EncryptTest, this, &authId, paramInSet));
1281526fd984Sopenharmony_ci    }
1282526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1283526fd984Sopenharmony_ci    SetThreadState(false);
1284526fd984Sopenharmony_ci    for (auto &t : threads) {
1285526fd984Sopenharmony_ci        t.join();
1286526fd984Sopenharmony_ci    }
1287526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1288526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1289526fd984Sopenharmony_ci}
1290526fd984Sopenharmony_ci
1291526fd984Sopenharmony_ci/**
1292526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01400
1293526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01400
1294526fd984Sopenharmony_ci * @tc.desc      : HksDecrypt
1295526fd984Sopenharmony_ci */
1296526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01400, TestSize.Level1)
1297526fd984Sopenharmony_ci{
1298526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1299526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1300526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1301526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1302526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1303526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1304526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1305526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1306526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
1307526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST,
1308526fd984Sopenharmony_ci        .uint32Param = HKS_DIGEST_NONE },
1309526fd984Sopenharmony_ci        { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
1310526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1311526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1312526fd984Sopenharmony_ci        { .tag = HKS_TAG_BLOCK_MODE,
1313526fd984Sopenharmony_ci        .uint32Param = HKS_MODE_CBC },
1314526fd984Sopenharmony_ci    };
1315526fd984Sopenharmony_ci    uint8_t iv[IV_SIZE] = {0};
1316526fd984Sopenharmony_ci    struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1317526fd984Sopenharmony_ci    HksAddParams(paramInSet, &tagIv, 1);
1318526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1319526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1320526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1321526fd984Sopenharmony_ci    const char *hexData = "0123456789abcdef";
1322526fd984Sopenharmony_ci    uint32_t dataLen = strlen(hexData);
1323526fd984Sopenharmony_ci    HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
1324526fd984Sopenharmony_ci    uint32_t inLen = dataLen + COMPLEMENT_LEN;
1325526fd984Sopenharmony_ci    HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
1326526fd984Sopenharmony_ci    ASSERT_NE(cipherText.data, nullptr);
1327526fd984Sopenharmony_ci    HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
1328526fd984Sopenharmony_ci    SetThreadState(true);
1329526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1330526fd984Sopenharmony_ci        threads.emplace_back(
1331526fd984Sopenharmony_ci            std::thread(&ApiPressureTest::DecryptTest, this, &authId, paramInSet, &cipherText, &inLen));
1332526fd984Sopenharmony_ci    }
1333526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1334526fd984Sopenharmony_ci    SetThreadState(false);
1335526fd984Sopenharmony_ci    for (auto &t : threads) {
1336526fd984Sopenharmony_ci        t.join();
1337526fd984Sopenharmony_ci    }
1338526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1339526fd984Sopenharmony_ci    HKS_FREE(cipherText.data);
1340526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1341526fd984Sopenharmony_ci}
1342526fd984Sopenharmony_ci
1343526fd984Sopenharmony_ci/**
1344526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01500
1345526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01500
1346526fd984Sopenharmony_ci * @tc.desc      : HksAgreeKey
1347526fd984Sopenharmony_ci */
1348526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01500, TestSize.Level1)
1349526fd984Sopenharmony_ci{
1350526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1351526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1352526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1353526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1354526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1355526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1356526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1357526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1358526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
1359526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1360526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1361526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1362526fd984Sopenharmony_ci    };
1363526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1364526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1365526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1366526fd984Sopenharmony_ci    SetThreadState(true);
1367526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1368526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::AgreeKeyTest, this, &authId));
1369526fd984Sopenharmony_ci    }
1370526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1371526fd984Sopenharmony_ci    SetThreadState(false);
1372526fd984Sopenharmony_ci    for (auto &t : threads) {
1373526fd984Sopenharmony_ci        t.join();
1374526fd984Sopenharmony_ci    }
1375526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1376526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1377526fd984Sopenharmony_ci}
1378526fd984Sopenharmony_ci
1379526fd984Sopenharmony_ci/**
1380526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01600
1381526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01600
1382526fd984Sopenharmony_ci * @tc.desc      : HksDeriveKey
1383526fd984Sopenharmony_ci */
1384526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01600, TestSize.Level1)
1385526fd984Sopenharmony_ci{
1386526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1387526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1388526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1389526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1390526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1391526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1392526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1393526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1394526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
1395526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
1396526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1397526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1398526fd984Sopenharmony_ci    };
1399526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1400526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1401526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1402526fd984Sopenharmony_ci    struct HksParamSet *paramInSetHkdf = nullptr;
1403526fd984Sopenharmony_ci    HksInitParamSet(&paramInSetHkdf);
1404526fd984Sopenharmony_ci    struct HksParam tmpParamsHkdf[] = {
1405526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1406526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HKDF },
1407526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
1408526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
1409526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1410526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1411526fd984Sopenharmony_ci    };
1412526fd984Sopenharmony_ci    HksAddParams(paramInSetHkdf, tmpParamsHkdf, sizeof(tmpParamsHkdf) / sizeof(tmpParamsHkdf[0]));
1413526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSetHkdf);
1414526fd984Sopenharmony_ci    SetThreadState(true);
1415526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1416526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::DeriveKeyTest, this, &authId, paramInSetHkdf));
1417526fd984Sopenharmony_ci    }
1418526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1419526fd984Sopenharmony_ci    SetThreadState(false);
1420526fd984Sopenharmony_ci    for (auto &t : threads) {
1421526fd984Sopenharmony_ci        t.join();
1422526fd984Sopenharmony_ci    }
1423526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1424526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1425526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSetHkdf);
1426526fd984Sopenharmony_ci}
1427526fd984Sopenharmony_ci
1428526fd984Sopenharmony_ci/**
1429526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01700
1430526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01700
1431526fd984Sopenharmony_ci * @tc.desc      : HksMac
1432526fd984Sopenharmony_ci */
1433526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01700, TestSize.Level1)
1434526fd984Sopenharmony_ci{
1435526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1436526fd984Sopenharmony_ci    struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1437526fd984Sopenharmony_ci    struct HksParamSet *paramInSet = nullptr;
1438526fd984Sopenharmony_ci    HksInitParamSet(&paramInSet);
1439526fd984Sopenharmony_ci    struct HksParam tmpParams[] = {
1440526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1441526fd984Sopenharmony_ci        { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HMAC },
1442526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_SIZE, .uint32Param = TEST_KEY_SIZE },
1443526fd984Sopenharmony_ci        { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_MAC },
1444526fd984Sopenharmony_ci        { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 },
1445526fd984Sopenharmony_ci        { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1446526fd984Sopenharmony_ci        { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1447526fd984Sopenharmony_ci    };
1448526fd984Sopenharmony_ci    HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1449526fd984Sopenharmony_ci    HksBuildParamSet(&paramInSet);
1450526fd984Sopenharmony_ci    HksGenerateKey(&authId, paramInSet, NULL);
1451526fd984Sopenharmony_ci    SetThreadState(true);
1452526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1453526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::MacTest, this, &authId, paramInSet));
1454526fd984Sopenharmony_ci    }
1455526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1456526fd984Sopenharmony_ci    SetThreadState(false);
1457526fd984Sopenharmony_ci    for (auto &t : threads) {
1458526fd984Sopenharmony_ci        t.join();
1459526fd984Sopenharmony_ci    }
1460526fd984Sopenharmony_ci    (void)HksDeleteKey(&authId, nullptr);
1461526fd984Sopenharmony_ci    HksFreeParamSet(&paramInSet);
1462526fd984Sopenharmony_ci}
1463526fd984Sopenharmony_ci
1464526fd984Sopenharmony_ci/**
1465526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01800
1466526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01800
1467526fd984Sopenharmony_ci * @tc.desc      : HksHash
1468526fd984Sopenharmony_ci */
1469526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01800, TestSize.Level1)
1470526fd984Sopenharmony_ci{
1471526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1472526fd984Sopenharmony_ci    SetThreadState(true);
1473526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1474526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::HashTest, this));
1475526fd984Sopenharmony_ci    }
1476526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1477526fd984Sopenharmony_ci    SetThreadState(false);
1478526fd984Sopenharmony_ci    for (auto &t : threads) {
1479526fd984Sopenharmony_ci        t.join();
1480526fd984Sopenharmony_ci    }
1481526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
1482526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
1483526fd984Sopenharmony_ci}
1484526fd984Sopenharmony_ci
1485526fd984Sopenharmony_ci/**
1486526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest01900
1487526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest01900
1488526fd984Sopenharmony_ci * @tc.desc      : Scenario 1: generate key and encrypt / decrypt
1489526fd984Sopenharmony_ci */
1490526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest01900, TestSize.Level1)
1491526fd984Sopenharmony_ci{
1492526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1493526fd984Sopenharmony_ci    SetThreadState(true);
1494526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1495526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::CipherScene, this, ii));
1496526fd984Sopenharmony_ci    }
1497526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1498526fd984Sopenharmony_ci    SetThreadState(false);
1499526fd984Sopenharmony_ci    for (auto &t : threads) {
1500526fd984Sopenharmony_ci        t.join();
1501526fd984Sopenharmony_ci    }
1502526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
1503526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
1504526fd984Sopenharmony_ci}
1505526fd984Sopenharmony_ci
1506526fd984Sopenharmony_ci/**
1507526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest02000
1508526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest02000
1509526fd984Sopenharmony_ci * @tc.desc      : Scenario 2: generate key and sign / verify
1510526fd984Sopenharmony_ci */
1511526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest02000, TestSize.Level1)
1512526fd984Sopenharmony_ci{
1513526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1514526fd984Sopenharmony_ci    SetThreadState(true);
1515526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1516526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::SignScene, this, ii));
1517526fd984Sopenharmony_ci    }
1518526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1519526fd984Sopenharmony_ci    SetThreadState(false);
1520526fd984Sopenharmony_ci    for (auto &t : threads) {
1521526fd984Sopenharmony_ci        t.join();
1522526fd984Sopenharmony_ci    }
1523526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
1524526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
1525526fd984Sopenharmony_ci}
1526526fd984Sopenharmony_ci
1527526fd984Sopenharmony_ci/**
1528526fd984Sopenharmony_ci * @tc.number    : ApiPressureTest.ApiPressureTest02100
1529526fd984Sopenharmony_ci * @tc.name      : ApiPressureTest02100
1530526fd984Sopenharmony_ci * @tc.desc      : Scenario 3: generate key and agree
1531526fd984Sopenharmony_ci */
1532526fd984Sopenharmony_ciHWTEST_F(ApiPressureTest, ApiPressureTest02100, TestSize.Level1)
1533526fd984Sopenharmony_ci{
1534526fd984Sopenharmony_ci    std::vector<std::thread> threads;
1535526fd984Sopenharmony_ci    SetThreadState(true);
1536526fd984Sopenharmony_ci    for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1537526fd984Sopenharmony_ci        threads.emplace_back(std::thread(&ApiPressureTest::AgreeScene, this, ii));
1538526fd984Sopenharmony_ci    }
1539526fd984Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1540526fd984Sopenharmony_ci    SetThreadState(false);
1541526fd984Sopenharmony_ci    for (auto &t : threads) {
1542526fd984Sopenharmony_ci        t.join();
1543526fd984Sopenharmony_ci    }
1544526fd984Sopenharmony_ci    int32_t ret = HksInitialize();
1545526fd984Sopenharmony_ci    ASSERT_TRUE(ret == HKS_SUCCESS);
1546526fd984Sopenharmony_ci}
1547526fd984Sopenharmony_ci}  // namespace
1548