• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <mutex>
17 #include <random>
18 #include <string>
19 #include <thread>
20 #include "securec.h"
21 
22 #include <gtest/gtest.h>
23 
24 #include "hks_api.h"
25 #include "hks_config.h"
26 #include "hks_mem.h"
27 #include "hks_param.h"
28 
29 using namespace testing::ext;
30 namespace {
31 namespace {
32 const char GENERATE_KEY[] = "This is for generate key";
33 const char IMPORT_KEY[] = "This is for import key";
34 const uint32_t TEST_HOURS = 6;
35 const uint32_t DSA_KEY_SIZE = 1024;
36 const uint32_t THREADS_NUM = 15;
37 const uint32_t RANDOM_KEY_SIZE = 512;
38 const uint32_t MAX_SDK_VERSION_SIZE = 64;
39 const uint32_t DERIVE_KEY_SIZE = 64;
40 const uint32_t IV_SIZE = 16;
41 const uint32_t AAD_SIZE = 16;
42 const uint32_t RSA_MESSAGE_SIZE = 6;
43 const uint32_t AES_MESSAGE_SIZE = 16;
44 const uint32_t COMPLEMENT_LEN = 16;
45 const uint32_t KEY_PARAMSET_SIZE = 1024;
46 const uint32_t MESSAGE_SIZE = 64;
47 const uint32_t TEST_KEY_SIZE = 4096;
48 const uint32_t CIPHER_SIZE = 4096;
49 const uint32_t MAX_STRING_LENTH = 513;
50 
51 static const struct HksParam CIPHER_PARAMS[] = {
52     { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
53     { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
54     { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
55     { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
56 };
57 
58 const struct HksParam g_PARAM_ALGORITHM_AES = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES };
59 const struct HksParam g_PARAM_ALGORITHM_RSA = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_RSA };
60 const struct HksParam g_PARAM_ALGORITHM_ECC = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC };
61 #ifndef _USE_MBEDTLS_
62 const struct HksParam g_PARAM_ALGORITHM_DSA = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DSA };
63 #endif
64 #ifdef HKS_SUPPORT_DH_C
65 const struct HksParam g_PARAM_ALGORITHM_DH = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_DH };
66 #endif
67 const struct HksParam g_PARAM_ALGORITHM_ECDH = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECDH };
68 
69 const struct HksParam g_PARAM_KEYSIZE_AES_128 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 };
70 const struct HksParam g_PARAM_KEYSIZE_AES_192 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_192 };
71 const struct HksParam g_PARAM_KEYSIZE_AES_256 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_256 };
72 const struct HksParam g_PARAM_KEYSIZE_RSA_512 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_512 };
73 const struct HksParam g_PARAM_KEYSIZE_RSA_768 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_768 };
74 const struct HksParam g_PARAM_KEYSIZE_RSA_1024 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_1024 };
75 const struct HksParam g_PARAM_KEYSIZE_RSA_2048 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_2048 };
76 const struct HksParam g_PARAM_KEYSIZE_RSA_3072 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_3072 };
77 const struct HksParam g_PARAM_KEYSIZE_RSA_4096 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_RSA_KEY_SIZE_4096 };
78 const struct HksParam g_PARAM_KEYSIZE_ECC_224 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 };
79 const struct HksParam g_PARAM_KEYSIZE_ECC_256 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_256 };
80 const struct HksParam g_PARAM_KEYSIZE_ECC_384 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_384 };
81 const struct HksParam g_PARAM_KEYSIZE_ECC_521 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_521 };
82 #ifdef HKS_SUPPORT_DH_C
83 const struct HksParam g_PARAM_KEYSIZE_DH_2048 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_DH_KEY_SIZE_2048 };
84 const struct HksParam g_PARAM_KEYSIZE_DH_3072 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_DH_KEY_SIZE_3072 };
85 const struct HksParam g_PARAM_KEYSIZE_DH_4096 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_DH_KEY_SIZE_4096 };
86 #endif
87 const struct HksParam g_PARAM_KEYSIZE_DSA_1024 = { .tag = HKS_TAG_KEY_SIZE, .uint32Param = DSA_KEY_SIZE };
88 
89 const struct HksParam g_PARAM_PADDING_NONE = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE };
90 const struct HksParam g_PARAM_PADDING_OAEP = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_OAEP };
91 const struct HksParam g_PARAM_PADDING_PKCS1_V1_5 = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS1_V1_5 };
92 const struct HksParam g_PARAM_PADDING_PKCS7 = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 };
93 const struct HksParam g_PARAM_PADDING_PSS = { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PSS };
94 
95 const struct HksParam g_PARAM_MODE_CBC = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC };
96 const struct HksParam g_PARAM_MODE_CTR = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CTR };
97 const struct HksParam g_PARAM_MODE_ECB = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_ECB };
98 const struct HksParam g_PARAM_MODE_GCM = { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_GCM };
99 
100 const struct HksParam g_PARAM_DIGEST_NONE = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE };
101 const struct HksParam g_PARAM_DIGEST_MD5 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_MD5 };
102 const struct HksParam g_PARAM_DIGEST_SHA1 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 };
103 const struct HksParam g_PARAM_DIGEST_SHA224 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA224 };
104 const struct HksParam g_PARAM_DIGEST_SHA256 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 };
105 const struct HksParam g_PARAM_DIGEST_SHA384 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA384 };
106 const struct HksParam g_PARAM_DIGEST_SHA512 = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA512 };
107 
GetRandom()108 inline uint32_t GetRandom()
109 {
110     std::random_device rd;
111     return rd();
112 }
113 
AESModeRandForCiper(struct HksParamSet * paramInSet)114 void AESModeRandForCiper(struct HksParamSet *paramInSet)
115 {
116     struct HksParam tagIv;
117     uint8_t iv[IV_SIZE] = {0};
118     const struct HksParam *paramMode[] = {
119         &g_PARAM_MODE_CBC,
120         &g_PARAM_MODE_CTR,
121         &g_PARAM_MODE_ECB,
122         &g_PARAM_MODE_GCM,
123     };
124     const struct HksParam *paramPadding[] = {
125         &g_PARAM_PADDING_NONE,
126         &g_PARAM_PADDING_PKCS7,
127     };
128     const uint32_t countOfMode = sizeof(paramMode) / sizeof(struct HksParam *);
129     const uint32_t countOfPadding = sizeof(paramPadding) / sizeof(struct HksParam *);
130     const uint32_t indexOfMode = GetRandom() % countOfMode;
131     HksAddParams(paramInSet, paramMode[indexOfMode], 1);
132     if (paramMode[indexOfMode]->uint32Param == HKS_MODE_CBC) {
133         tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
134         HksAddParams(paramInSet, &tagIv, 1);
135         HksAddParams(paramInSet, paramPadding[GetRandom() % countOfPadding], 1);
136     } else if (paramMode[indexOfMode]->uint32Param == HKS_MODE_CTR) {
137         HksAddParams(paramInSet, &g_PARAM_PADDING_NONE, 1);
138         tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
139         HksAddParams(paramInSet, &tagIv, 1);
140     } else if (paramMode[indexOfMode]->uint32Param == HKS_MODE_ECB) {
141         tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
142         HksAddParams(paramInSet, &tagIv, 1);
143 #ifndef _USE_MBEDTLS_
144         HksAddParams(paramInSet, paramPadding[GetRandom() % countOfPadding], 1);
145 #else
146         HksAddParams(paramInSet, &g_PARAM_PADDING_NONE, 1);
147 #endif
148     } else if (paramMode[indexOfMode]->uint32Param == HKS_MODE_GCM) {
149         HksAddParams(paramInSet, &g_PARAM_PADDING_NONE, 1);
150         uint8_t aadData[AAD_SIZE] = { 'a', 'a', 'd' };
151         struct HksParam aad = { .tag = HKS_TAG_ASSOCIATED_DATA, .blob = { .size = sizeof(aadData), .data = aadData } };
152         HksAddParams(paramInSet, &aad, 1);
153         tagIv = { .tag = HKS_TAG_NONCE, .blob = { .size = IV_SIZE, .data = iv } };
154         HksAddParams(paramInSet, &tagIv, 1);
155     }
156 }
157 
ParamForAESCipher(struct HksParamSet * paramInSet)158 void ParamForAESCipher(struct HksParamSet *paramInSet)
159 {
160     const struct HksParam *paramKeySize[] = {
161         &g_PARAM_KEYSIZE_AES_128,
162         &g_PARAM_KEYSIZE_AES_192,
163         &g_PARAM_KEYSIZE_AES_256,
164     };
165     const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
166     HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
167     AESModeRandForCiper(paramInSet);
168 }
169 
RSAKeySizeRand(struct HksParamSet * paramInSet)170 void RSAKeySizeRand(struct HksParamSet *paramInSet)
171 {
172     const struct HksParam *paramKeySize[] = {
173         &g_PARAM_KEYSIZE_RSA_512,
174         &g_PARAM_KEYSIZE_RSA_768,
175         &g_PARAM_KEYSIZE_RSA_1024,
176         &g_PARAM_KEYSIZE_RSA_2048,
177         &g_PARAM_KEYSIZE_RSA_3072,
178         &g_PARAM_KEYSIZE_RSA_4096,
179     };
180     const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
181     HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
182 }
183 
RSAKeySizeWithout512(struct HksParamSet * paramInSet)184 void RSAKeySizeWithout512(struct HksParamSet *paramInSet)
185 {
186     const struct HksParam *paramKeySize[] = {
187         &g_PARAM_KEYSIZE_RSA_768,
188         &g_PARAM_KEYSIZE_RSA_1024,
189         &g_PARAM_KEYSIZE_RSA_2048,
190         &g_PARAM_KEYSIZE_RSA_3072,
191         &g_PARAM_KEYSIZE_RSA_4096,
192     };
193     const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
194     HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
195 }
196 
RSAKeySizeWithout768(struct HksParamSet * paramInSet)197 void RSAKeySizeWithout768(struct HksParamSet *paramInSet)
198 {
199     const struct HksParam *paramKeySize[] = {
200         &g_PARAM_KEYSIZE_RSA_1024,
201         &g_PARAM_KEYSIZE_RSA_2048,
202         &g_PARAM_KEYSIZE_RSA_3072,
203         &g_PARAM_KEYSIZE_RSA_4096,
204     };
205     const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
206     HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
207 }
208 
OaepSha512KeySize(struct HksParamSet * paramInSet)209 void OaepSha512KeySize(struct HksParamSet *paramInSet)
210 {
211     const struct HksParam *paramKeySize[] = {
212         &g_PARAM_KEYSIZE_RSA_2048,
213         &g_PARAM_KEYSIZE_RSA_3072,
214         &g_PARAM_KEYSIZE_RSA_4096,
215     };
216     const uint32_t count = sizeof(paramKeySize) / sizeof(struct HksParam *);
217     HksAddParams(paramInSet, paramKeySize[GetRandom() % count], 1);
218 }
219 
ParamForRSACipher(struct HksParamSet * paramInSet)220 void ParamForRSACipher(struct HksParamSet *paramInSet)
221 {
222     const struct HksParam *paramPadding[] = {
223 #ifndef _USE_MBEDTLS_
224         &g_PARAM_PADDING_NONE,
225 #endif
226         &g_PARAM_PADDING_PKCS1_V1_5,
227         &g_PARAM_PADDING_OAEP,
228     };
229     const struct HksParam *paramDigest[] = {
230         &g_PARAM_DIGEST_NONE,
231         &g_PARAM_DIGEST_SHA1,
232         &g_PARAM_DIGEST_SHA224,
233         &g_PARAM_DIGEST_SHA256,
234         &g_PARAM_DIGEST_SHA384,
235         &g_PARAM_DIGEST_SHA512,
236     };
237     const uint32_t countOfPadding = sizeof(paramPadding) / sizeof(struct HksParam *);
238     const uint32_t countOfDigest = sizeof(paramDigest) / sizeof(struct HksParam *);
239     const uint32_t indexOfPadding = GetRandom() % countOfPadding;
240     HksAddParams(paramInSet, paramPadding[indexOfPadding], 1);
241     if ((paramPadding[indexOfPadding]->uint32Param == HKS_PADDING_NONE) ||
242         (paramPadding[indexOfPadding]->uint32Param == HKS_PADDING_PKCS1_V1_5)) {
243         RSAKeySizeRand(paramInSet);
244         HksAddParams(paramInSet, &g_PARAM_DIGEST_NONE, 1);
245     } else if (paramPadding[indexOfPadding]->uint32Param == HKS_PADDING_OAEP) {
246         const uint32_t indexOfDigest = GetRandom() % countOfDigest;
247         HksAddParams(paramInSet, paramDigest[indexOfDigest], 1);
248         if (paramDigest[indexOfDigest]->uint32Param == HKS_DIGEST_SHA256) {
249             RSAKeySizeWithout512(paramInSet);
250         } else if (paramDigest[indexOfDigest]->uint32Param == HKS_DIGEST_SHA384) {
251             RSAKeySizeWithout768(paramInSet);
252         } else if (paramDigest[indexOfDigest]->uint32Param == HKS_DIGEST_SHA512) {
253             OaepSha512KeySize(paramInSet);
254         } else {
255             RSAKeySizeRand(paramInSet);
256         }
257     }
258 }
259 
ParamForCipher(struct HksParamSet * paramInSet)260 void ParamForCipher(struct HksParamSet *paramInSet)
261 {
262     const struct HksParam *paramAlgorithm[] = {
263         &g_PARAM_ALGORITHM_AES,
264         &g_PARAM_ALGORITHM_RSA,
265     };
266     const uint32_t count = sizeof(paramAlgorithm) / sizeof(struct HksParam *);
267     const uint32_t index = GetRandom() % count;
268     HksAddParams(paramInSet, paramAlgorithm[index], 1);
269     if (paramAlgorithm[index]->uint32Param == HKS_ALG_AES) {
270         ParamForAESCipher(paramInSet);
271     } else if (paramAlgorithm[index]->uint32Param == HKS_ALG_RSA) {
272         HksAddParams(paramInSet, &g_PARAM_MODE_ECB, 1);
273         ParamForRSACipher(paramInSet);
274     }
275 }
276 
ShaForPkcs1(struct HksParamSet * paramInSet)277 void ShaForPkcs1(struct HksParamSet *paramInSet)
278 {
279     const struct HksParam *paramDigest[] = {
280         &g_PARAM_DIGEST_NONE,
281         &g_PARAM_DIGEST_MD5,
282         &g_PARAM_DIGEST_SHA1,
283         &g_PARAM_DIGEST_SHA224,
284         &g_PARAM_DIGEST_SHA256,
285         &g_PARAM_DIGEST_SHA384,
286         &g_PARAM_DIGEST_SHA512,
287     };
288     const uint32_t count = sizeof(paramDigest) / sizeof(struct HksParam *);
289     const uint32_t index = GetRandom() % count;
290     HksAddParams(paramInSet, paramDigest[index], 1);
291     if ((paramDigest[index]->uint32Param == HKS_DIGEST_SHA384) ||
292         (paramDigest[index]->uint32Param == HKS_DIGEST_SHA512)) {
293         RSAKeySizeWithout512(paramInSet);
294     } else {
295         RSAKeySizeRand(paramInSet);
296     }
297 }
298 
ShaForPss(struct HksParamSet * paramInSet)299 void ShaForPss(struct HksParamSet *paramInSet)
300 {
301     const struct HksParam *paramDigest[] = {
302         &g_PARAM_DIGEST_NONE,
303         &g_PARAM_DIGEST_MD5,
304         &g_PARAM_DIGEST_SHA1,
305         &g_PARAM_DIGEST_SHA224,
306         &g_PARAM_DIGEST_SHA256,
307         &g_PARAM_DIGEST_SHA384,
308         &g_PARAM_DIGEST_SHA512,
309     };
310     const uint32_t count = sizeof(paramDigest) / sizeof(struct HksParam *);
311     const uint32_t index = GetRandom() % count;
312     HksAddParams(paramInSet, paramDigest[index], 1);
313     if (paramDigest[index]->uint32Param == HKS_DIGEST_SHA384) {
314 #ifndef _USE_MBEDTLS_
315         RSAKeySizeRand(paramInSet);
316 #else
317         RSAKeySizeWithout512(paramInSet);
318 #endif
319     } else if (paramDigest[index]->uint32Param == HKS_DIGEST_SHA512) {
320 #ifndef _USE_MBEDTLS_
321         RSAKeySizeWithout512(paramInSet);
322 #else
323         RSAKeySizeWithout768(paramInSet);
324 #endif
325     } else {
326         RSAKeySizeRand(paramInSet);
327     }
328 }
329 
ParamForRSASign(struct HksParamSet * paramInSet)330 void ParamForRSASign(struct HksParamSet *paramInSet)
331 {
332     const struct HksParam *paramPadding[] = {
333         &g_PARAM_PADDING_PKCS1_V1_5,
334         &g_PARAM_PADDING_PSS,
335     };
336     const uint32_t count = sizeof(paramPadding) / sizeof(struct HksParam *);
337     const uint32_t index = GetRandom() % count;
338     HksAddParams(paramInSet, paramPadding[index], 1);
339     if (paramPadding[index]->uint32Param == HKS_PADDING_PKCS1_V1_5) {
340         ShaForPkcs1(paramInSet);
341     } else if (paramPadding[index]->uint32Param == HKS_PADDING_PSS) {
342         ShaForPss(paramInSet);
343     }
344 }
345 
ParamForECCSign(struct HksParamSet * paramInSet)346 void ParamForECCSign(struct HksParamSet *paramInSet)
347 {
348     const struct HksParam *paramKeySize[] = {
349         &g_PARAM_KEYSIZE_ECC_224,
350         &g_PARAM_KEYSIZE_ECC_256,
351         &g_PARAM_KEYSIZE_ECC_384,
352         &g_PARAM_KEYSIZE_ECC_521,
353     };
354     const uint32_t countOfKeySize = sizeof(paramKeySize) / sizeof(struct HksParam *);
355     HksAddParams(paramInSet, paramKeySize[GetRandom() % countOfKeySize], 1);
356 
357     const struct HksParam *paramDigest[] = {
358         &g_PARAM_DIGEST_NONE,
359         &g_PARAM_DIGEST_SHA1,
360         &g_PARAM_DIGEST_SHA224,
361         &g_PARAM_DIGEST_SHA256,
362         &g_PARAM_DIGEST_SHA384,
363         &g_PARAM_DIGEST_SHA512,
364     };
365     const uint32_t countOfDigest = sizeof(paramDigest) / sizeof(struct HksParam *);
366     HksAddParams(paramInSet, paramDigest[GetRandom() % countOfDigest], 1);
367 }
368 
ParamForDSASign(struct HksParamSet * paramInSet)369 void ParamForDSASign(struct HksParamSet *paramInSet)
370 {
371     HksAddParams(paramInSet, &g_PARAM_PADDING_PKCS1_V1_5, 1);
372     HksAddParams(paramInSet, &g_PARAM_KEYSIZE_DSA_1024, 1);
373     const struct HksParam *paramDigest[] = {
374         &g_PARAM_DIGEST_SHA1,
375         &g_PARAM_DIGEST_SHA224,
376         &g_PARAM_DIGEST_SHA256,
377         &g_PARAM_DIGEST_SHA384,
378         &g_PARAM_DIGEST_SHA512,
379     };
380     const uint32_t countOfDigest = sizeof(paramDigest) / sizeof(struct HksParam *);
381     HksAddParams(paramInSet, paramDigest[GetRandom() % countOfDigest], 1);
382 }
383 
ParamForSign(struct HksParamSet * paramInSet)384 void ParamForSign(struct HksParamSet *paramInSet)
385 {
386     const struct HksParam *paramAlgorithm[] = {
387         &g_PARAM_ALGORITHM_RSA,
388         &g_PARAM_ALGORITHM_ECC,
389 #ifndef _USE_MBEDTLS_
390         &g_PARAM_ALGORITHM_DSA,
391 #endif
392     };
393     const uint32_t count = sizeof(paramAlgorithm) / sizeof(struct HksParam *);
394     const uint32_t index = GetRandom() % count;
395     HksAddParams(paramInSet, paramAlgorithm[index], 1);
396     if (paramAlgorithm[index]->uint32Param == HKS_ALG_RSA) {
397         ParamForRSASign(paramInSet);
398     } else if (paramAlgorithm[index]->uint32Param == HKS_ALG_ECC) {
399         ParamForECCSign(paramInSet);
400     } else if (paramAlgorithm[index]->uint32Param == HKS_ALG_DSA) {
401         ParamForDSASign(paramInSet);
402     }
403 }
404 
ParamForAgree(struct HksParamSet * generateKeyParam,struct HksParamSet * agreeKeyParam)405 void ParamForAgree(struct HksParamSet *generateKeyParam, struct HksParamSet *agreeKeyParam)
406 {
407     const struct HksParam *paramAlg[] = {
408         &g_PARAM_ALGORITHM_ECDH,
409 #ifdef HKS_SUPPORT_DH_C
410         &g_PARAM_ALGORITHM_DH,
411 #endif
412     };
413     const uint32_t countOfAlg = sizeof(paramAlg) / sizeof(struct HksParam *);
414     const uint32_t indexOfAlg = GetRandom() % countOfAlg;
415 
416     HksAddParams(agreeKeyParam, paramAlg[indexOfAlg], 1);
417     if (paramAlg[indexOfAlg]->uint32Param == HKS_ALG_ECDH) {
418         HksAddParams(generateKeyParam, &g_PARAM_ALGORITHM_ECC, 1);
419         const struct HksParam *paramKeySize[] = {
420             &g_PARAM_KEYSIZE_ECC_224,
421             &g_PARAM_KEYSIZE_ECC_256,
422             &g_PARAM_KEYSIZE_ECC_384,
423             &g_PARAM_KEYSIZE_ECC_521,
424         };
425         const uint32_t countOfKeySize = sizeof(paramKeySize) / sizeof(struct HksParam *);
426         const uint32_t indexOfKeySize = GetRandom() % countOfKeySize;
427         HksAddParams(generateKeyParam, paramKeySize[indexOfKeySize], 1);
428         HksAddParams(agreeKeyParam, paramKeySize[indexOfKeySize], 1);
429         HksAddParams(generateKeyParam, &g_PARAM_DIGEST_NONE, 1);
430     }
431 #ifdef HKS_SUPPORT_DH_C
432     if (paramAlg[indexOfAlg]->uint32Param == HKS_ALG_DH) {
433         HksAddParams(generateKeyParam, &g_PARAM_ALGORITHM_DH, 1);
434         const struct HksParam *paramKeySize[] = {
435             &g_PARAM_KEYSIZE_DH_2048,
436             &g_PARAM_KEYSIZE_DH_3072,
437             &g_PARAM_KEYSIZE_DH_4096,
438         };
439         const uint32_t countOfKeySize = sizeof(paramKeySize) / sizeof(struct HksParam *);
440         const uint32_t indexOfKeySize = GetRandom() % countOfKeySize;
441         HksAddParams(generateKeyParam, paramKeySize[indexOfKeySize], 1);
442         HksAddParams(agreeKeyParam, paramKeySize[indexOfKeySize], 1);
443     }
444 #endif
445 }
446 }  // namespace
447 
448 class ApiPressureTest : public testing::Test {
449 public:
SetThreadState(bool threadState)450     void SetThreadState(bool threadState)
451     {
452         std::lock_guard<std::mutex> lock(mutex_);
453 
454         threadState_ = threadState;
455     }
GetThreadState()456     bool GetThreadState()
457     {
458         std::lock_guard<std::mutex> lock(mutex_);
459 
460         return threadState_;
461     }
462     void GetSdkVersionTest();
463     void InitializeTest();
464     void RefreshKeyInfoTest();
465     void GenerateKeyTest();
466     void ImportKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
467     void ExportPublicKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
468     void DeleteKeyTest(uint32_t ii);
469     void GetKeyParamSetTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
470     void KeyExistTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
471     void GenerateRandomTest();
472     void SignTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet, const struct HksBlob *message);
473     void VerifyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet, const struct HksBlob *message,
474         const struct HksBlob *signature);
475     void EncryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
476     void DecryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
477         const struct HksBlob *cipherText, uint32_t *inLen);
478     void AgreeKeyTest(const struct HksBlob *authId);
479     void DeriveKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
480     void MacTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet);
481     void HashTest();
482     void CipherScene(uint32_t ii);
483     void SignScene(uint32_t ii);
484     void AgreeScene(uint32_t ii);
485 
486 private:
487     bool threadState_ = false;
488     std::mutex mutex_;
489 };
490 
GetSdkVersionTest()491 void ApiPressureTest::GetSdkVersionTest()
492 {
493     do {
494         if (!GetThreadState()) {
495             break;
496         }
497         struct HksBlob sdkVersion = {
498             .size = MAX_SDK_VERSION_SIZE,
499             .data = (uint8_t *)HksMalloc(MAX_SDK_VERSION_SIZE)
500         };
501 
502         EXPECT_EQ(HksGetSdkVersion(&sdkVersion), HKS_SUCCESS);
503 
504         HksFree(sdkVersion.data);
505     } while (1);
506 }
507 
InitializeTest()508 void ApiPressureTest::InitializeTest()
509 {
510     do {
511         if (!GetThreadState()) {
512             break;
513         }
514         EXPECT_EQ(HksInitialize(), HKS_SUCCESS);
515     } while (1);
516 }
517 
RefreshKeyInfoTest()518 void ApiPressureTest::RefreshKeyInfoTest()
519 {
520     do {
521         if (!GetThreadState()) {
522             break;
523         }
524         EXPECT_EQ(HksRefreshKeyInfo(), HKS_SUCCESS);
525     } while (1);
526 }
527 
GenerateKeyTest()528 void ApiPressureTest::GenerateKeyTest()
529 {
530     do {
531         if (!GetThreadState()) {
532             break;
533         }
534         struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
535         struct HksParamSet *paramInSet = nullptr;
536         HksInitParamSet(&paramInSet);
537         struct HksParam tmpParams[] = {
538             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
539             { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
540             { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
541             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
542             { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
543             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
544             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
545         };
546         HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
547         HksBuildParamSet(&paramInSet);
548         EXPECT_EQ(HksGenerateKey(&authId, paramInSet, NULL), HKS_SUCCESS);
549         HksFreeParamSet(&paramInSet);
550     } while (1);
551 }
552 
ImportKeyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)553 void ApiPressureTest::ImportKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
554 {
555     struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
556     HksExportPublicKey(authId, paramInSet, &pubKey);
557     do {
558         if (!GetThreadState()) {
559             break;
560         }
561         struct HksBlob importId = { (uint32_t)strlen(IMPORT_KEY), (uint8_t *)IMPORT_KEY };
562         EXPECT_EQ(HksImportKey(&importId, paramInSet, &pubKey), HKS_SUCCESS);
563     } while (1);
564     HksFree(pubKey.data);
565 }
566 
ExportPublicKeyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)567 void ApiPressureTest::ExportPublicKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
568 {
569     do {
570         if (!GetThreadState()) {
571             break;
572         }
573         struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
574 
575         EXPECT_EQ(HksExportPublicKey(authId, paramInSet, &pubKey), HKS_SUCCESS);
576 
577         HksFree(pubKey.data);
578     } while (1);
579 }
580 
DeleteKeyTest(uint32_t ii)581 void ApiPressureTest::DeleteKeyTest(uint32_t ii)
582 {
583     do {
584         if (!GetThreadState()) {
585             break;
586         }
587         std::string key = "KeyForThread" + std::to_string(ii);
588         struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
589         struct HksParamSet *paramInSet = nullptr;
590         HksInitParamSet(&paramInSet);
591         struct HksParam tmpParams[] = {
592             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
593             { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
594             { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
595             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
596             { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
597             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
598             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
599         };
600         HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
601         HksBuildParamSet(&paramInSet);
602         HksGenerateKey(&authId, paramInSet, NULL);
603         EXPECT_EQ(HksDeleteKey(&authId, paramInSet), HKS_SUCCESS);
604         HksFreeParamSet(&paramInSet);
605     } while (1);
606 }
607 
GetKeyParamSetTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)608 void ApiPressureTest::GetKeyParamSetTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
609 {
610     do {
611         struct HksParamSet *paramOutSet = nullptr;
612         HksInitParamSet(&paramOutSet);
613         struct HksParam localKey = {
614             .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
615             .blob = { .size = KEY_PARAMSET_SIZE, .data = (uint8_t *)HksMalloc(KEY_PARAMSET_SIZE) }
616         };
617         HksAddParams(paramOutSet, &localKey, 1);
618         HksBuildParamSet(&paramOutSet);
619         if (!GetThreadState()) {
620             break;
621         }
622         EXPECT_EQ(HksGetKeyParamSet(authId, paramInSet, paramOutSet), HKS_SUCCESS);
623         HksFree(localKey.blob.data);
624         HksFreeParamSet(&paramOutSet);
625     } while (1);
626 }
627 
KeyExistTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)628 void ApiPressureTest::KeyExistTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
629 {
630     do {
631         if (!GetThreadState()) {
632             break;
633         }
634         EXPECT_EQ(HksKeyExist(authId, paramInSet), HKS_SUCCESS);
635     } while (1);
636 }
637 
GenerateRandomTest()638 void ApiPressureTest::GenerateRandomTest()
639 {
640     struct HksBlob authId = { .size = RANDOM_KEY_SIZE, .data = (uint8_t *)HksMalloc(RANDOM_KEY_SIZE) };
641     struct HksParamSet *paramInSet = nullptr;
642     HksInitParamSet(&paramInSet);
643     struct HksParam tmpParams[] = {
644         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
645         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
646         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
647         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
648     };
649     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
650     HksBuildParamSet(&paramInSet);
651     do {
652         if (!GetThreadState()) {
653             break;
654         }
655         EXPECT_EQ(HksGenerateRandom(paramInSet, &authId), HKS_SUCCESS);
656     } while (1);
657     HksFree(authId.data);
658     HksFreeParamSet(&paramInSet);
659 }
660 
SignTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet,const struct HksBlob * message)661 void ApiPressureTest::SignTest(
662     const struct HksBlob *authId, const struct HksParamSet *paramInSet, const struct HksBlob *message)
663 {
664     do {
665         if (!GetThreadState()) {
666             break;
667         }
668         HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
669         EXPECT_EQ(HksSign(authId, paramInSet, message, &signature), HKS_SUCCESS);
670         HksFree(signature.data);
671     } while (1);
672 }
673 
VerifyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet,const struct HksBlob * message,const struct HksBlob * signature)674 void ApiPressureTest::VerifyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
675     const struct HksBlob *message, const struct HksBlob *signature)
676 {
677     do {
678         if (!GetThreadState()) {
679             break;
680         }
681         EXPECT_EQ(HksVerify(authId, paramInSet, message, signature), HKS_SUCCESS);
682     } while (1);
683 }
684 
EncryptTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)685 void ApiPressureTest::EncryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
686 {
687     const char *hexData = "0123456789abcdef";
688     uint32_t dataLen = strlen(hexData);
689     HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
690     uint32_t inLen = dataLen + COMPLEMENT_LEN;
691     do {
692         if (!GetThreadState()) {
693             break;
694         }
695         HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
696         EXPECT_EQ(HksEncrypt(authId, paramInSet, &plainText, &cipherText), HKS_SUCCESS);
697         HksFree(cipherText.data);
698     } while (1);
699 }
700 
DecryptTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet,const struct HksBlob * cipherText,uint32_t * inLen)701 void ApiPressureTest::DecryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
702     const struct HksBlob *cipherText, uint32_t *inLen)
703 {
704     do {
705         if (!GetThreadState()) {
706             break;
707         }
708         HksBlob plainTextDecrypt = { .size = *inLen, .data = (uint8_t *)HksMalloc(*inLen) };
709         EXPECT_EQ(HksDecrypt(authId, paramInSet, cipherText, &plainTextDecrypt), HKS_SUCCESS);
710         HksFree(plainTextDecrypt.data);
711     } while (1);
712 }
713 
AgreeKeyTest(const struct HksBlob * authId)714 void ApiPressureTest::AgreeKeyTest(const struct HksBlob *authId)
715 {
716     struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
717     HksExportPublicKey(authId, NULL, &pubKey);
718     struct HksParamSet *paramInSet = nullptr;
719     HksInitParamSet(&paramInSet);
720     struct HksParam tmpParams[] = {
721         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
722         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECDH },
723         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
724         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
725         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
726         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
727     };
728     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
729     HksBuildParamSet(&paramInSet);
730     do {
731         if (!GetThreadState()) {
732             break;
733         }
734         HksBlob agreeKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
735         EXPECT_EQ(HksAgreeKey(paramInSet, authId, &pubKey, &agreeKey), HKS_SUCCESS);
736         HksFree(agreeKey.data);
737     } while (1);
738     HksFree(pubKey.data);
739     HksFreeParamSet(&paramInSet);
740 }
741 
DeriveKeyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)742 void ApiPressureTest::DeriveKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
743 {
744     do {
745         if (!GetThreadState()) {
746             break;
747         }
748         HksBlob derivedKey = { .size = DERIVE_KEY_SIZE, .data = (uint8_t *)HksMalloc(DERIVE_KEY_SIZE) };
749         EXPECT_EQ(HksDeriveKey(paramInSet, authId, &derivedKey), HKS_SUCCESS);
750         HksFree(derivedKey.data);
751     } while (1);
752 }
753 
MacTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)754 void ApiPressureTest::MacTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
755 {
756     const char *hexData = "0123456789abcdef";
757     uint32_t dataLen = strlen(hexData);
758     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
759     do {
760         if (!GetThreadState()) {
761             break;
762         }
763         HksBlob macMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
764         EXPECT_EQ(HksMac(authId, paramInSet, &message, &macMessage), HKS_SUCCESS);
765         HksFree(macMessage.data);
766     } while (1);
767 }
768 
HashTest()769 void ApiPressureTest::HashTest()
770 {
771     struct HksParamSet *paramInSet = nullptr;
772     HksInitParamSet(&paramInSet);
773     struct HksParam digest = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 };
774     HksAddParams(paramInSet, &digest, 1);
775     HksBuildParamSet(&paramInSet);
776     const char *hexData = "0123456789abcdef";
777     uint32_t dataLen = strlen(hexData);
778     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
779     do {
780         if (!GetThreadState()) {
781             break;
782         }
783         HksBlob shaMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
784         EXPECT_EQ(HksHash(paramInSet, &message, &shaMessage), HKS_SUCCESS);
785         HksFree(shaMessage.data);
786     } while (1);
787     HksFreeParamSet(&paramInSet);
788 }
789 
CipherScene(uint32_t ii)790 void ApiPressureTest::CipherScene(uint32_t ii)
791 {
792     std::string key = "KeyForThread" + std::to_string(ii);
793     struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
794     do {
795         if (!GetThreadState()) {
796             break;
797         }
798         struct HksParamSet *paramInSet = nullptr;
799         HksInitParamSet(&paramInSet);
800         HksAddParams(paramInSet, CIPHER_PARAMS, sizeof(CIPHER_PARAMS) / sizeof(CIPHER_PARAMS[0]));
801         ParamForCipher(paramInSet);
802         HksBuildParamSet(&paramInSet);
803         HksGenerateKey(&authId, paramInSet, NULL);
804         struct HksParam *padding = nullptr;
805         HksGetParam(paramInSet, HKS_TAG_PADDING, &padding);
806         char hexData[MAX_STRING_LENTH] = {0};
807         uint32_t dataLen;
808         struct HksParam *algorithm = nullptr;
809         HksGetParam(paramInSet, HKS_TAG_ALGORITHM, &algorithm);
810         if (algorithm->uint32Param == HKS_ALG_RSA) {
811             if (padding->uint32Param == HKS_PADDING_NONE) {
812                 struct HksParam *keySize = nullptr;
813                 HksGetParam(paramInSet, HKS_TAG_KEY_SIZE, &keySize);
814                 dataLen = HKS_KEY_BYTES(keySize->uint32Param);
815             } else {
816                 dataLen = RSA_MESSAGE_SIZE;
817             }
818         } else {
819             dataLen = AES_MESSAGE_SIZE;
820         }
821         HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
822         HksBlob cipherText = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
823         HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
824         HksBlob plainTextDecrypt = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
825         EXPECT_EQ(HksDecrypt(&authId, paramInSet, &cipherText, &plainTextDecrypt), HKS_SUCCESS);
826         HksFree(plainTextDecrypt.data);
827         HksFree(cipherText.data);
828         HksFreeParamSet(&paramInSet);
829     } while (1);
830 }
831 
SignScene(uint32_t ii)832 void ApiPressureTest::SignScene(uint32_t ii)
833 {
834     std::string key = "KeyForThread" + std::to_string(ii);
835     struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
836     do {
837         if (!GetThreadState()) {
838             break;
839         }
840         struct HksParamSet *paramInSet = nullptr;
841         HksInitParamSet(&paramInSet);
842         struct HksParam tmpParams[] = {
843             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
844             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
845             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
846             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
847         };
848         HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
849         ParamForSign(paramInSet);
850         HksBuildParamSet(&paramInSet);
851         HksGenerateKey(&authId, paramInSet, NULL);
852         const char *hexData = "0123456789abcdef";
853         uint32_t dataLen = strlen(hexData);
854         HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
855         HksBlob signature = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
856         HksSign(&authId, paramInSet, &message, &signature);
857         EXPECT_EQ(HksVerify(&authId, paramInSet, &message, &signature), HKS_SUCCESS);
858         HksFree(signature.data);
859         HksFreeParamSet(&paramInSet);
860     } while (1);
861 }
862 
AgreeScene(uint32_t ii)863 void ApiPressureTest::AgreeScene(uint32_t ii)
864 {
865     std::string key = "KeyForThread" + std::to_string(ii);
866     struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
867     do {
868         if (!GetThreadState()) {
869             break;
870         }
871         struct HksParamSet *generateKeyParam = nullptr;
872         HksInitParamSet(&generateKeyParam);
873         struct HksParamSet *agreeKeyParam = nullptr;
874         HksInitParamSet(&agreeKeyParam);
875         struct HksParam tmpParams[] = {
876             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
877             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
878             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
879             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
880         };
881         HksAddParams(generateKeyParam, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
882         HksAddParams(agreeKeyParam, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
883         ParamForAgree(generateKeyParam, agreeKeyParam);
884         HksBuildParamSet(&generateKeyParam);
885         HksBuildParamSet(&agreeKeyParam);
886         HksGenerateKey(&authId, generateKeyParam, NULL);
887         struct HksBlob pubKey = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
888         HksExportPublicKey(&authId, generateKeyParam, &pubKey);
889         HksBlob agreeKey = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
890         EXPECT_EQ(HksAgreeKey(agreeKeyParam, &authId, &pubKey, &agreeKey), HKS_SUCCESS);
891         HksFree(agreeKey.data);
892         HksFree(pubKey.data);
893         HksFreeParamSet(&generateKeyParam);
894         HksFreeParamSet(&agreeKeyParam);
895     } while (1);
896 }
897 
898 /**
899  * @tc.number    : ApiPressureTest.ApiPressureTest00100
900  * @tc.name      : ApiPressureTest00100
901  * @tc.desc      : HksGetSdkVersion
902  */
903 HWTEST_F(ApiPressureTest, ApiPressureTest00100, TestSize.Level1)
904 {
905     std::vector<std::thread> threads;
906     SetThreadState(true);
907     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
908         threads.emplace_back(std::thread(&ApiPressureTest::GetSdkVersionTest, this));
909     }
910     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
911     SetThreadState(false);
912     for (auto &t : threads) {
913         t.join();
914     }
915 }
916 
917 /**
918  * @tc.number    : ApiPressureTest.ApiPressureTest00200
919  * @tc.name      : ApiPressureTest00200
920  * @tc.desc      : HksInitialize
921  */
922 HWTEST_F(ApiPressureTest, ApiPressureTest00200, TestSize.Level1)
923 {
924     std::vector<std::thread> threads;
925     SetThreadState(true);
926     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
927         threads.emplace_back(std::thread(&ApiPressureTest::InitializeTest, this));
928     }
929     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
930     SetThreadState(false);
931     for (auto &t : threads) {
932         t.join();
933     }
934 }
935 
936 /**
937  * @tc.number    : ApiPressureTest.ApiPressureTest00300
938  * @tc.name      : ApiPressureTest00300
939  * @tc.desc      : HksRefreshKeyInfo
940  */
941 HWTEST_F(ApiPressureTest, ApiPressureTest00300, TestSize.Level1)
942 {
943     std::vector<std::thread> threads;
944     SetThreadState(true);
945     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
946         threads.emplace_back(std::thread(&ApiPressureTest::RefreshKeyInfoTest, this));
947     }
948     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
949     SetThreadState(false);
950     for (auto &t : threads) {
951         t.join();
952     }
953 }
954 
955 /**
956  * @tc.number    : ApiPressureTest.ApiPressureTest00400
957  * @tc.name      : ApiPressureTest00400
958  * @tc.desc      : HksGenerateKey
959  */
960 HWTEST_F(ApiPressureTest, ApiPressureTest00400, TestSize.Level1)
961 {
962     std::vector<std::thread> threads;
963     SetThreadState(true);
964     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
965         threads.emplace_back(std::thread(&ApiPressureTest::GenerateKeyTest, this));
966     }
967     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
968     SetThreadState(false);
969     for (auto &t : threads) {
970         t.join();
971     }
972 }
973 
974 /**
975  * @tc.number    : ApiPressureTest.ApiPressureTest00500
976  * @tc.name      : ApiPressureTest00500
977  * @tc.desc      : HksImportKey
978  */
979 HWTEST_F(ApiPressureTest, ApiPressureTest00500, TestSize.Level1)
980 {
981     std::vector<std::thread> threads;
982     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
983     struct HksParamSet *paramInSet = nullptr;
984     HksInitParamSet(&paramInSet);
985     struct HksParam tmpParams[] = {
986         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
987         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
988         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
989         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
990         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
991         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
992         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
993     };
994     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
995     HksBuildParamSet(&paramInSet);
996     HksGenerateKey(&authId, paramInSet, NULL);
997     SetThreadState(true);
998     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
999         threads.emplace_back(std::thread(&ApiPressureTest::ImportKeyTest, this, &authId, paramInSet));
1000     }
1001     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1002     SetThreadState(false);
1003     for (auto &t : threads) {
1004         t.join();
1005     }
1006     HksFreeParamSet(&paramInSet);
1007 }
1008 
1009 /**
1010  * @tc.number    : ApiPressureTest.ApiPressureTest00600
1011  * @tc.name      : ApiPressureTest00600
1012  * @tc.desc      : HksExportPublicKey
1013  */
1014 HWTEST_F(ApiPressureTest, ApiPressureTest00600, TestSize.Level1)
1015 {
1016     std::vector<std::thread> threads;
1017     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1018     struct HksParamSet *paramInSet = nullptr;
1019     HksInitParamSet(&paramInSet);
1020     struct HksParam tmpParams[] = {
1021         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1022         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1023         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1024         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
1025         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1026         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1027         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1028     };
1029     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1030     HksBuildParamSet(&paramInSet);
1031     HksGenerateKey(&authId, paramInSet, NULL);
1032     SetThreadState(true);
1033     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1034         threads.emplace_back(std::thread(&ApiPressureTest::ExportPublicKeyTest, this, &authId, paramInSet));
1035     }
1036     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1037     SetThreadState(false);
1038     for (auto &t : threads) {
1039         t.join();
1040     }
1041     HksFreeParamSet(&paramInSet);
1042 }
1043 
1044 /**
1045  * @tc.number    : ApiPressureTest.ApiPressureTest00700
1046  * @tc.name      : ApiPressureTest00700
1047  * @tc.desc      : HksDeleteKey
1048  */
1049 HWTEST_F(ApiPressureTest, ApiPressureTest00700, TestSize.Level1)
1050 {
1051     std::vector<std::thread> threads;
1052     SetThreadState(true);
1053     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1054         threads.emplace_back(std::thread(&ApiPressureTest::DeleteKeyTest, this, ii));
1055     }
1056     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1057     SetThreadState(false);
1058     for (auto &t : threads) {
1059         t.join();
1060     }
1061 }
1062 
1063 /**
1064  * @tc.number    : ApiPressureTest.ApiPressureTest00800
1065  * @tc.name      : ApiPressureTest00800
1066  * @tc.desc      : HksGetKeyParamSet
1067  */
1068 HWTEST_F(ApiPressureTest, ApiPressureTest00800, TestSize.Level1)
1069 {
1070     std::vector<std::thread> threads;
1071     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1072     struct HksParamSet *paramInSet = nullptr;
1073     HksInitParamSet(&paramInSet);
1074     struct HksParam tmpParams[] = {
1075         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1076         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1077         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1078         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
1079         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1080         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1081         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1082     };
1083     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1084     HksBuildParamSet(&paramInSet);
1085     HksGenerateKey(&authId, paramInSet, NULL);
1086     SetThreadState(true);
1087     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1088         threads.emplace_back(std::thread(&ApiPressureTest::GetKeyParamSetTest, this, &authId, paramInSet));
1089     }
1090     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1091     SetThreadState(false);
1092     for (auto &t : threads) {
1093         t.join();
1094     }
1095     HksFreeParamSet(&paramInSet);
1096 }
1097 
1098 /**
1099  * @tc.number    : ApiPressureTest.ApiPressureTest00900
1100  * @tc.name      : ApiPressureTest00900
1101  * @tc.desc      : HksKeyExist
1102  */
1103 HWTEST_F(ApiPressureTest, ApiPressureTest00900, TestSize.Level1)
1104 {
1105     std::vector<std::thread> threads;
1106     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1107     struct HksParamSet *paramInSet = nullptr;
1108     HksInitParamSet(&paramInSet);
1109     struct HksParam tmpParams[] = {
1110         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1111         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1112         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1113         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
1114         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1115         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1116         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1117     };
1118     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1119     HksBuildParamSet(&paramInSet);
1120     HksGenerateKey(&authId, paramInSet, NULL);
1121     SetThreadState(true);
1122     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1123         threads.emplace_back(std::thread(&ApiPressureTest::KeyExistTest, this, &authId, paramInSet));
1124     }
1125     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1126     SetThreadState(false);
1127     for (auto &t : threads) {
1128         t.join();
1129     }
1130     HksFreeParamSet(&paramInSet);
1131 }
1132 
1133 /**
1134  * @tc.number    : ApiPressureTest.ApiPressureTest01000
1135  * @tc.name      : ApiPressureTest01000
1136  * @tc.desc      : HksGenerateRandom
1137  */
1138 HWTEST_F(ApiPressureTest, ApiPressureTest01000, TestSize.Level1)
1139 {
1140     std::vector<std::thread> threads;
1141     SetThreadState(true);
1142     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1143         threads.emplace_back(std::thread(&ApiPressureTest::GenerateRandomTest, this));
1144     }
1145     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1146     SetThreadState(false);
1147     for (auto &t : threads) {
1148         t.join();
1149     }
1150 }
1151 
1152 /**
1153  * @tc.number    : ApiPressureTest.ApiPressureTest01100
1154  * @tc.name      : ApiPressureTest01100
1155  * @tc.desc      : HksSign
1156  */
1157 HWTEST_F(ApiPressureTest, ApiPressureTest01100, TestSize.Level1)
1158 {
1159     std::vector<std::thread> threads;
1160     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1161     struct HksParamSet *paramInSet = nullptr;
1162     HksInitParamSet(&paramInSet);
1163     struct HksParam tmpParams[] = {
1164         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1165         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1166         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1167         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
1168         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1169         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1170         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1171     };
1172     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1173     HksBuildParamSet(&paramInSet);
1174     HksGenerateKey(&authId, paramInSet, NULL);
1175     const char *hexData = "0123456789abcdef";
1176     uint32_t dataLen = strlen(hexData);
1177     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1178     SetThreadState(true);
1179     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1180         threads.emplace_back(std::thread(&ApiPressureTest::SignTest, this, &authId, paramInSet, &message));
1181     }
1182     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1183     SetThreadState(false);
1184     for (auto &t : threads) {
1185         t.join();
1186     }
1187     HksFreeParamSet(&paramInSet);
1188 }
1189 
1190 /**
1191  * @tc.number    : ApiPressureTest.ApiPressureTest01200
1192  * @tc.name      : ApiPressureTest01200
1193  * @tc.desc      : HksVerify
1194  */
1195 HWTEST_F(ApiPressureTest, ApiPressureTest01200, TestSize.Level1)
1196 {
1197     std::vector<std::thread> threads;
1198     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1199     struct HksParamSet *paramInSet = nullptr;
1200     HksInitParamSet(&paramInSet);
1201     struct HksParam tmpParams[] = {
1202         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1203         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1204         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1205         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
1206         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1207         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1208         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1209     };
1210     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1211     HksBuildParamSet(&paramInSet);
1212     HksGenerateKey(&authId, paramInSet, NULL);
1213     const char *hexData = "0123456789abcdef";
1214     uint32_t dataLen = strlen(hexData);
1215     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1216     HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1217     HksSign(&authId, paramInSet, &message, &signature);
1218     SetThreadState(true);
1219     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1220         threads.emplace_back(
1221             std::thread(&ApiPressureTest::VerifyTest, this, &authId, paramInSet, &message, &signature));
1222     }
1223     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1224     SetThreadState(false);
1225     for (auto &t : threads) {
1226         t.join();
1227     }
1228     HksFree(signature.data);
1229     HksFreeParamSet(&paramInSet);
1230 }
1231 
1232 /**
1233  * @tc.number    : ApiPressureTest.ApiPressureTest01300
1234  * @tc.name      : ApiPressureTest01300
1235  * @tc.desc      : HksEncrypt
1236  */
1237 HWTEST_F(ApiPressureTest, ApiPressureTest01300, TestSize.Level1)
1238 {
1239     std::vector<std::thread> threads;
1240     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1241     struct HksParamSet *paramInSet = nullptr;
1242     HksInitParamSet(&paramInSet);
1243     struct HksParam tmpParams[] = {
1244         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1245         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1246         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1247         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
1248         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1249         { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
1250         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1251         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1252         { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
1253     };
1254     uint8_t iv[IV_SIZE] = {0};
1255     struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1256     HksAddParams(paramInSet, &tagIv, 1);
1257     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1258     HksBuildParamSet(&paramInSet);
1259     HksGenerateKey(&authId, paramInSet, NULL);
1260     SetThreadState(true);
1261     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1262         threads.emplace_back(std::thread(&ApiPressureTest::EncryptTest, this, &authId, paramInSet));
1263     }
1264     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1265     SetThreadState(false);
1266     for (auto &t : threads) {
1267         t.join();
1268     }
1269     HksFreeParamSet(&paramInSet);
1270 }
1271 
1272 /**
1273  * @tc.number    : ApiPressureTest.ApiPressureTest01400
1274  * @tc.name      : ApiPressureTest01400
1275  * @tc.desc      : HksDecrypt
1276  */
1277 HWTEST_F(ApiPressureTest, ApiPressureTest01400, TestSize.Level1)
1278 {
1279     std::vector<std::thread> threads;
1280     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1281     struct HksParamSet *paramInSet = nullptr;
1282     HksInitParamSet(&paramInSet);
1283     struct HksParam tmpParams[] = {
1284         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1285         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1286         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1287         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
1288         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1289         { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
1290         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1291         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1292         { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
1293     };
1294     uint8_t iv[IV_SIZE] = {0};
1295     struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1296     HksAddParams(paramInSet, &tagIv, 1);
1297     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1298     HksBuildParamSet(&paramInSet);
1299     HksGenerateKey(&authId, paramInSet, NULL);
1300     const char *hexData = "0123456789abcdef";
1301     uint32_t dataLen = strlen(hexData);
1302     HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
1303     uint32_t inLen = dataLen + COMPLEMENT_LEN;
1304     HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
1305     HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
1306     SetThreadState(true);
1307     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1308         threads.emplace_back(
1309             std::thread(&ApiPressureTest::DecryptTest, this, &authId, paramInSet, &cipherText, &inLen));
1310     }
1311     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1312     SetThreadState(false);
1313     for (auto &t : threads) {
1314         t.join();
1315     }
1316     HksFree(cipherText.data);
1317     HksFreeParamSet(&paramInSet);
1318 }
1319 
1320 /**
1321  * @tc.number    : ApiPressureTest.ApiPressureTest01500
1322  * @tc.name      : ApiPressureTest01500
1323  * @tc.desc      : HksAgreeKey
1324  */
1325 HWTEST_F(ApiPressureTest, ApiPressureTest01500, TestSize.Level1)
1326 {
1327     std::vector<std::thread> threads;
1328     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1329     struct HksParamSet *paramInSet = nullptr;
1330     HksInitParamSet(&paramInSet);
1331     struct HksParam tmpParams[] = {
1332         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1333         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1334         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1335         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
1336         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1337         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1338         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1339     };
1340     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1341     HksBuildParamSet(&paramInSet);
1342     HksGenerateKey(&authId, paramInSet, NULL);
1343     SetThreadState(true);
1344     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1345         threads.emplace_back(std::thread(&ApiPressureTest::AgreeKeyTest, this, &authId));
1346     }
1347     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1348     SetThreadState(false);
1349     for (auto &t : threads) {
1350         t.join();
1351     }
1352     HksFreeParamSet(&paramInSet);
1353 }
1354 
1355 /**
1356  * @tc.number    : ApiPressureTest.ApiPressureTest01600
1357  * @tc.name      : ApiPressureTest01600
1358  * @tc.desc      : HksDeriveKey
1359  */
1360 HWTEST_F(ApiPressureTest, ApiPressureTest01600, TestSize.Level1)
1361 {
1362     std::vector<std::thread> threads;
1363     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1364     struct HksParamSet *paramInSet = nullptr;
1365     HksInitParamSet(&paramInSet);
1366     struct HksParam tmpParams[] = {
1367         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1368         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1369         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1370         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
1371         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
1372         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1373         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1374     };
1375     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1376     HksBuildParamSet(&paramInSet);
1377     HksGenerateKey(&authId, paramInSet, NULL);
1378     struct HksParamSet *paramInSetHkdf = nullptr;
1379     HksInitParamSet(&paramInSetHkdf);
1380     struct HksParam tmpParamsHkdf[] = {
1381         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1382         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HKDF },
1383         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
1384         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
1385         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1386         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1387     };
1388     HksAddParams(paramInSetHkdf, tmpParamsHkdf, sizeof(tmpParamsHkdf) / sizeof(tmpParamsHkdf[0]));
1389     HksBuildParamSet(&paramInSetHkdf);
1390     SetThreadState(true);
1391     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1392         threads.emplace_back(std::thread(&ApiPressureTest::DeriveKeyTest, this, &authId, paramInSetHkdf));
1393     }
1394     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1395     SetThreadState(false);
1396     for (auto &t : threads) {
1397         t.join();
1398     }
1399     HksFreeParamSet(&paramInSet);
1400     HksFreeParamSet(&paramInSetHkdf);
1401 }
1402 
1403 /**
1404  * @tc.number    : ApiPressureTest.ApiPressureTest01700
1405  * @tc.name      : ApiPressureTest01700
1406  * @tc.desc      : HksMac
1407  */
1408 HWTEST_F(ApiPressureTest, ApiPressureTest01700, TestSize.Level1)
1409 {
1410     std::vector<std::thread> threads;
1411     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1412     struct HksParamSet *paramInSet = nullptr;
1413     HksInitParamSet(&paramInSet);
1414     struct HksParam tmpParams[] = {
1415         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1416         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HMAC },
1417         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = TEST_KEY_SIZE },
1418         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_MAC },
1419         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 },
1420         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1421         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1422     };
1423     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1424     HksBuildParamSet(&paramInSet);
1425     HksGenerateKey(&authId, paramInSet, NULL);
1426     SetThreadState(true);
1427     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1428         threads.emplace_back(std::thread(&ApiPressureTest::MacTest, this, &authId, paramInSet));
1429     }
1430     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1431     SetThreadState(false);
1432     for (auto &t : threads) {
1433         t.join();
1434     }
1435     HksFreeParamSet(&paramInSet);
1436 }
1437 
1438 /**
1439  * @tc.number    : ApiPressureTest.ApiPressureTest01800
1440  * @tc.name      : ApiPressureTest01800
1441  * @tc.desc      : HksHash
1442  */
1443 HWTEST_F(ApiPressureTest, ApiPressureTest01800, TestSize.Level1)
1444 {
1445     std::vector<std::thread> threads;
1446     SetThreadState(true);
1447     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1448         threads.emplace_back(std::thread(&ApiPressureTest::HashTest, this));
1449     }
1450     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1451     SetThreadState(false);
1452     for (auto &t : threads) {
1453         t.join();
1454     }
1455 }
1456 
1457 /**
1458  * @tc.number    : ApiPressureTest.ApiPressureTest01900
1459  * @tc.name      : ApiPressureTest01900
1460  * @tc.desc      : Scenario 1: generate key and encrypt / decrypt
1461  */
1462 HWTEST_F(ApiPressureTest, ApiPressureTest01900, TestSize.Level1)
1463 {
1464     std::vector<std::thread> threads;
1465     SetThreadState(true);
1466     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1467         threads.emplace_back(std::thread(&ApiPressureTest::CipherScene, this, ii));
1468     }
1469     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1470     SetThreadState(false);
1471     for (auto &t : threads) {
1472         t.join();
1473     }
1474 }
1475 
1476 /**
1477  * @tc.number    : ApiPressureTest.ApiPressureTest02000
1478  * @tc.name      : ApiPressureTest02000
1479  * @tc.desc      : Scenario 2: generate key and sign / verify
1480  */
1481 HWTEST_F(ApiPressureTest, ApiPressureTest02000, TestSize.Level1)
1482 {
1483     std::vector<std::thread> threads;
1484     SetThreadState(true);
1485     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1486         threads.emplace_back(std::thread(&ApiPressureTest::SignScene, this, ii));
1487     }
1488     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1489     SetThreadState(false);
1490     for (auto &t : threads) {
1491         t.join();
1492     }
1493 }
1494 
1495 /**
1496  * @tc.number    : ApiPressureTest.ApiPressureTest02100
1497  * @tc.name      : ApiPressureTest02100
1498  * @tc.desc      : Scenario 3: generate key and agree
1499  */
1500 HWTEST_F(ApiPressureTest, ApiPressureTest02100, TestSize.Level1)
1501 {
1502     std::vector<std::thread> threads;
1503     SetThreadState(true);
1504     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1505         threads.emplace_back(std::thread(&ApiPressureTest::AgreeScene, this, ii));
1506     }
1507     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1508     SetThreadState(false);
1509     for (auto &t : threads) {
1510         t.join();
1511     }
1512 }
1513 }  // namespace