• 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         ASSERT_NE(sdkVersion.data, nullptr);
502 
503         EXPECT_EQ(HksGetSdkVersion(&sdkVersion), HKS_SUCCESS);
504 
505         HksFree(sdkVersion.data);
506     } while (1);
507 }
508 
InitializeTest()509 void ApiPressureTest::InitializeTest()
510 {
511     do {
512         if (!GetThreadState()) {
513             break;
514         }
515         EXPECT_EQ(HksInitialize(), HKS_SUCCESS);
516     } while (1);
517 }
518 
RefreshKeyInfoTest()519 void ApiPressureTest::RefreshKeyInfoTest()
520 {
521     do {
522         if (!GetThreadState()) {
523             break;
524         }
525         EXPECT_EQ(HksRefreshKeyInfo(), HKS_SUCCESS);
526     } while (1);
527 }
528 
GenerateKeyTest()529 void ApiPressureTest::GenerateKeyTest()
530 {
531     do {
532         if (!GetThreadState()) {
533             break;
534         }
535         struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
536         struct HksParamSet *paramInSet = nullptr;
537         HksInitParamSet(&paramInSet);
538         struct HksParam tmpParams[] = {
539             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
540             { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
541             { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
542             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
543             { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
544             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
545             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
546         };
547         HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
548         HksBuildParamSet(&paramInSet);
549         EXPECT_EQ(HksGenerateKey(&authId, paramInSet, NULL), HKS_SUCCESS);
550         HksFreeParamSet(&paramInSet);
551     } while (1);
552 }
553 
ImportKeyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)554 void ApiPressureTest::ImportKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
555 {
556     struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
557     ASSERT_NE(pubKey.data, nullptr);
558     HksExportPublicKey(authId, paramInSet, &pubKey);
559     do {
560         if (!GetThreadState()) {
561             break;
562         }
563         struct HksBlob importId = { (uint32_t)strlen(IMPORT_KEY), (uint8_t *)IMPORT_KEY };
564         EXPECT_EQ(HksImportKey(&importId, paramInSet, &pubKey), HKS_SUCCESS);
565     } while (1);
566     HksFree(pubKey.data);
567 }
568 
ExportPublicKeyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)569 void ApiPressureTest::ExportPublicKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
570 {
571     do {
572         if (!GetThreadState()) {
573             break;
574         }
575         struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
576         ASSERT_NE(pubKey.data, nullptr);
577 
578         EXPECT_EQ(HksExportPublicKey(authId, paramInSet, &pubKey), HKS_SUCCESS);
579 
580         HksFree(pubKey.data);
581     } while (1);
582 }
583 
DeleteKeyTest(uint32_t ii)584 void ApiPressureTest::DeleteKeyTest(uint32_t ii)
585 {
586     do {
587         if (!GetThreadState()) {
588             break;
589         }
590         std::string key = "KeyForThread" + std::to_string(ii);
591         struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
592         struct HksParamSet *paramInSet = nullptr;
593         HksInitParamSet(&paramInSet);
594         struct HksParam tmpParams[] = {
595             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
596             { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
597             { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
598             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
599             { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
600             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
601             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
602         };
603         HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
604         HksBuildParamSet(&paramInSet);
605         HksGenerateKey(&authId, paramInSet, NULL);
606         EXPECT_EQ(HksDeleteKey(&authId, paramInSet), HKS_SUCCESS);
607         HksFreeParamSet(&paramInSet);
608     } while (1);
609 }
610 
GetKeyParamSetTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)611 void ApiPressureTest::GetKeyParamSetTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
612 {
613     do {
614         struct HksParamSet *paramOutSet = nullptr;
615         HksInitParamSet(&paramOutSet);
616         struct HksParam localKey = {
617             .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
618             .blob = { .size = KEY_PARAMSET_SIZE, .data = (uint8_t *)HksMalloc(KEY_PARAMSET_SIZE) }
619         };
620         ASSERT_NE(localKey.blob.data, nullptr);
621         HksAddParams(paramOutSet, &localKey, 1);
622         HksBuildParamSet(&paramOutSet);
623         if (!GetThreadState()) {
624             break;
625         }
626         EXPECT_EQ(HksGetKeyParamSet(authId, paramInSet, paramOutSet), HKS_SUCCESS);
627         HksFree(localKey.blob.data);
628         HksFreeParamSet(&paramOutSet);
629     } while (1);
630 }
631 
KeyExistTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)632 void ApiPressureTest::KeyExistTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
633 {
634     do {
635         if (!GetThreadState()) {
636             break;
637         }
638         EXPECT_EQ(HksKeyExist(authId, paramInSet), HKS_SUCCESS);
639     } while (1);
640 }
641 
GenerateRandomTest()642 void ApiPressureTest::GenerateRandomTest()
643 {
644     struct HksBlob authId = { .size = RANDOM_KEY_SIZE, .data = (uint8_t *)HksMalloc(RANDOM_KEY_SIZE) };
645     ASSERT_NE(authId.data, nullptr);
646     struct HksParamSet *paramInSet = nullptr;
647     HksInitParamSet(&paramInSet);
648     struct HksParam tmpParams[] = {
649         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
650         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
651         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
652         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
653     };
654     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
655     HksBuildParamSet(&paramInSet);
656     do {
657         if (!GetThreadState()) {
658             break;
659         }
660         EXPECT_EQ(HksGenerateRandom(paramInSet, &authId), HKS_SUCCESS);
661     } while (1);
662     HksFree(authId.data);
663     HksFreeParamSet(&paramInSet);
664 }
665 
SignTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet,const struct HksBlob * message)666 void ApiPressureTest::SignTest(
667     const struct HksBlob *authId, const struct HksParamSet *paramInSet, const struct HksBlob *message)
668 {
669     do {
670         if (!GetThreadState()) {
671             break;
672         }
673         HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
674         ASSERT_NE(signature.data, nullptr);
675         EXPECT_EQ(HksSign(authId, paramInSet, message, &signature), HKS_SUCCESS);
676         HksFree(signature.data);
677     } while (1);
678 }
679 
VerifyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet,const struct HksBlob * message,const struct HksBlob * signature)680 void ApiPressureTest::VerifyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
681     const struct HksBlob *message, const struct HksBlob *signature)
682 {
683     do {
684         if (!GetThreadState()) {
685             break;
686         }
687         EXPECT_EQ(HksVerify(authId, paramInSet, message, signature), HKS_SUCCESS);
688     } while (1);
689 }
690 
EncryptTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)691 void ApiPressureTest::EncryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
692 {
693     const char *hexData = "0123456789abcdef";
694     uint32_t dataLen = strlen(hexData);
695     HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
696     uint32_t inLen = dataLen + COMPLEMENT_LEN;
697     do {
698         if (!GetThreadState()) {
699             break;
700         }
701         HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
702         ASSERT_NE(cipherText.data, nullptr);
703         EXPECT_EQ(HksEncrypt(authId, paramInSet, &plainText, &cipherText), HKS_SUCCESS);
704         HksFree(cipherText.data);
705     } while (1);
706 }
707 
DecryptTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet,const struct HksBlob * cipherText,uint32_t * inLen)708 void ApiPressureTest::DecryptTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet,
709     const struct HksBlob *cipherText, uint32_t *inLen)
710 {
711     do {
712         if (!GetThreadState()) {
713             break;
714         }
715         HksBlob plainTextDecrypt = { .size = *inLen, .data = (uint8_t *)HksMalloc(*inLen) };
716         ASSERT_NE(plainTextDecrypt.data, nullptr);
717         EXPECT_EQ(HksDecrypt(authId, paramInSet, cipherText, &plainTextDecrypt), HKS_SUCCESS);
718         HksFree(plainTextDecrypt.data);
719     } while (1);
720 }
721 
AgreeKeyTest(const struct HksBlob * authId)722 void ApiPressureTest::AgreeKeyTest(const struct HksBlob *authId)
723 {
724     struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
725     ASSERT_NE(pubKey.data, nullptr);
726     HksExportPublicKey(authId, NULL, &pubKey);
727     struct HksParamSet *paramInSet = nullptr;
728     HksInitParamSet(&paramInSet);
729     struct HksParam tmpParams[] = {
730         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
731         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECDH },
732         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
733         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
734         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
735         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
736     };
737     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
738     HksBuildParamSet(&paramInSet);
739     do {
740         if (!GetThreadState()) {
741             break;
742         }
743         HksBlob agreeKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
744         ASSERT_NE(agreeKey.data, nullptr);
745         EXPECT_EQ(HksAgreeKey(paramInSet, authId, &pubKey, &agreeKey), HKS_SUCCESS);
746         HksFree(agreeKey.data);
747     } while (1);
748     HksFree(pubKey.data);
749     HksFreeParamSet(&paramInSet);
750 }
751 
DeriveKeyTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)752 void ApiPressureTest::DeriveKeyTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
753 {
754     do {
755         if (!GetThreadState()) {
756             break;
757         }
758         HksBlob derivedKey = { .size = DERIVE_KEY_SIZE, .data = (uint8_t *)HksMalloc(DERIVE_KEY_SIZE) };
759         ASSERT_NE(derivedKey.data, nullptr);
760         EXPECT_EQ(HksDeriveKey(paramInSet, authId, &derivedKey), HKS_SUCCESS);
761         HksFree(derivedKey.data);
762     } while (1);
763 }
764 
MacTest(const struct HksBlob * authId,const struct HksParamSet * paramInSet)765 void ApiPressureTest::MacTest(const struct HksBlob *authId, const struct HksParamSet *paramInSet)
766 {
767     const char *hexData = "0123456789abcdef";
768     uint32_t dataLen = strlen(hexData);
769     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
770     do {
771         if (!GetThreadState()) {
772             break;
773         }
774         HksBlob macMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
775         ASSERT_NE(macMessage.data, nullptr);
776         EXPECT_EQ(HksMac(authId, paramInSet, &message, &macMessage), HKS_SUCCESS);
777         HksFree(macMessage.data);
778     } while (1);
779 }
780 
HashTest()781 void ApiPressureTest::HashTest()
782 {
783     struct HksParamSet *paramInSet = nullptr;
784     HksInitParamSet(&paramInSet);
785     struct HksParam digest = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 };
786     HksAddParams(paramInSet, &digest, 1);
787     HksBuildParamSet(&paramInSet);
788     const char *hexData = "0123456789abcdef";
789     uint32_t dataLen = strlen(hexData);
790     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
791     do {
792         if (!GetThreadState()) {
793             break;
794         }
795         HksBlob shaMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
796         ASSERT_NE(shaMessage.data, nullptr);
797         EXPECT_EQ(HksHash(paramInSet, &message, &shaMessage), HKS_SUCCESS);
798         HksFree(shaMessage.data);
799     } while (1);
800     HksFreeParamSet(&paramInSet);
801 }
802 
CipherScene(uint32_t ii)803 void ApiPressureTest::CipherScene(uint32_t ii)
804 {
805     std::string key = "KeyForThread" + std::to_string(ii);
806     struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
807     do {
808         if (!GetThreadState()) {
809             break;
810         }
811         struct HksParamSet *paramInSet = nullptr;
812         HksInitParamSet(&paramInSet);
813         HksAddParams(paramInSet, CIPHER_PARAMS, sizeof(CIPHER_PARAMS) / sizeof(CIPHER_PARAMS[0]));
814         ParamForCipher(paramInSet);
815         HksBuildParamSet(&paramInSet);
816         HksGenerateKey(&authId, paramInSet, NULL);
817         struct HksParam *padding = nullptr;
818         HksGetParam(paramInSet, HKS_TAG_PADDING, &padding);
819         char hexData[MAX_STRING_LENTH] = {0};
820         uint32_t dataLen;
821         struct HksParam *algorithm = nullptr;
822         HksGetParam(paramInSet, HKS_TAG_ALGORITHM, &algorithm);
823         if (algorithm->uint32Param == HKS_ALG_RSA) {
824             if (padding->uint32Param == HKS_PADDING_NONE) {
825                 struct HksParam *keySize = nullptr;
826                 HksGetParam(paramInSet, HKS_TAG_KEY_SIZE, &keySize);
827                 dataLen = HKS_KEY_BYTES(keySize->uint32Param);
828             } else {
829                 dataLen = RSA_MESSAGE_SIZE;
830             }
831         } else {
832             dataLen = AES_MESSAGE_SIZE;
833         }
834         HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
835         HksBlob cipherText = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
836         ASSERT_NE(cipherText.data, nullptr);
837         HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
838         HksBlob plainTextDecrypt = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
839         ASSERT_NE(plainTextDecrypt.data, nullptr);
840         EXPECT_EQ(HksDecrypt(&authId, paramInSet, &cipherText, &plainTextDecrypt), HKS_SUCCESS);
841         (void)HksDeleteKey(&authId, nullptr);
842         HksFree(plainTextDecrypt.data);
843         HksFree(cipherText.data);
844         HksFreeParamSet(&paramInSet);
845     } while (1);
846 }
847 
SignScene(uint32_t ii)848 void ApiPressureTest::SignScene(uint32_t ii)
849 {
850     std::string key = "KeyForThread" + std::to_string(ii);
851     struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
852     do {
853         if (!GetThreadState()) {
854             break;
855         }
856         struct HksParamSet *paramInSet = nullptr;
857         HksInitParamSet(&paramInSet);
858         struct HksParam tmpParams[] = {
859             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
860             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
861             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
862             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
863         };
864         HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
865         ParamForSign(paramInSet);
866         HksBuildParamSet(&paramInSet);
867         HksGenerateKey(&authId, paramInSet, NULL);
868         const char *hexData = "0123456789abcdef";
869         uint32_t dataLen = strlen(hexData);
870         HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
871         HksBlob signature = { .size = CIPHER_SIZE, .data = (uint8_t *)HksMalloc(CIPHER_SIZE) };
872         ASSERT_NE(signature.data, nullptr);
873         HksSign(&authId, paramInSet, &message, &signature);
874         EXPECT_EQ(HksVerify(&authId, paramInSet, &message, &signature), HKS_SUCCESS);
875         (void)HksDeleteKey(&authId, nullptr);
876         HksFree(signature.data);
877         HksFreeParamSet(&paramInSet);
878     } while (1);
879 }
880 
AgreeScene(uint32_t ii)881 void ApiPressureTest::AgreeScene(uint32_t ii)
882 {
883     std::string key = "KeyForThread" + std::to_string(ii);
884     struct HksBlob authId = { .size = (uint32_t)key.length(), .data = (uint8_t *)&key[0] };
885     do {
886         if (!GetThreadState()) {
887             break;
888         }
889         struct HksParamSet *generateKeyParam = nullptr;
890         HksInitParamSet(&generateKeyParam);
891         struct HksParamSet *agreeKeyParam = nullptr;
892         HksInitParamSet(&agreeKeyParam);
893         struct HksParam tmpParams[] = {
894             { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
895             { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
896             { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
897             { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
898         };
899         HksAddParams(generateKeyParam, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
900         HksAddParams(agreeKeyParam, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
901         ParamForAgree(generateKeyParam, agreeKeyParam);
902         HksBuildParamSet(&generateKeyParam);
903         HksBuildParamSet(&agreeKeyParam);
904         HksGenerateKey(&authId, generateKeyParam, NULL);
905         struct HksBlob pubKey = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
906         ASSERT_NE(pubKey.data, nullptr);
907         HksExportPublicKey(&authId, generateKeyParam, &pubKey);
908         HksBlob agreeKey = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
909         ASSERT_NE(agreeKey.data, nullptr);
910         EXPECT_EQ(HksAgreeKey(agreeKeyParam, &authId, &pubKey, &agreeKey), HKS_SUCCESS);
911         (void)HksDeleteKey(&authId, nullptr);
912         HksFree(agreeKey.data);
913         HksFree(pubKey.data);
914         HksFreeParamSet(&generateKeyParam);
915         HksFreeParamSet(&agreeKeyParam);
916     } while (1);
917 }
918 
919 /**
920  * @tc.number    : ApiPressureTest.ApiPressureTest00100
921  * @tc.name      : ApiPressureTest00100
922  * @tc.desc      : HksGetSdkVersion
923  */
924 HWTEST_F(ApiPressureTest, ApiPressureTest00100, TestSize.Level1)
925 {
926     std::vector<std::thread> threads;
927     SetThreadState(true);
928     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
929         threads.emplace_back(std::thread(&ApiPressureTest::GetSdkVersionTest, this));
930     }
931     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
932     SetThreadState(false);
933     for (auto &t : threads) {
934         t.join();
935     }
936     int32_t ret = HksInitialize();
937     ASSERT_TRUE(ret == HKS_SUCCESS);
938 }
939 
940 /**
941  * @tc.number    : ApiPressureTest.ApiPressureTest00200
942  * @tc.name      : ApiPressureTest00200
943  * @tc.desc      : HksInitialize
944  */
945 HWTEST_F(ApiPressureTest, ApiPressureTest00200, TestSize.Level1)
946 {
947     std::vector<std::thread> threads;
948     SetThreadState(true);
949     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
950         threads.emplace_back(std::thread(&ApiPressureTest::InitializeTest, this));
951     }
952     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
953     SetThreadState(false);
954     for (auto &t : threads) {
955         t.join();
956     }
957     int32_t ret = HksInitialize();
958     ASSERT_TRUE(ret == HKS_SUCCESS);
959 }
960 
961 /**
962  * @tc.number    : ApiPressureTest.ApiPressureTest00300
963  * @tc.name      : ApiPressureTest00300
964  * @tc.desc      : HksRefreshKeyInfo
965  */
966 HWTEST_F(ApiPressureTest, ApiPressureTest00300, TestSize.Level1)
967 {
968     std::vector<std::thread> threads;
969     SetThreadState(true);
970     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
971         threads.emplace_back(std::thread(&ApiPressureTest::RefreshKeyInfoTest, this));
972     }
973     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
974     SetThreadState(false);
975     for (auto &t : threads) {
976         t.join();
977     }
978     int32_t ret = HksInitialize();
979     ASSERT_TRUE(ret == HKS_SUCCESS);
980 }
981 
982 /**
983  * @tc.number    : ApiPressureTest.ApiPressureTest00400
984  * @tc.name      : ApiPressureTest00400
985  * @tc.desc      : HksGenerateKey
986  */
987 HWTEST_F(ApiPressureTest, ApiPressureTest00400, TestSize.Level1)
988 {
989     std::vector<std::thread> threads;
990     SetThreadState(true);
991     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
992         threads.emplace_back(std::thread(&ApiPressureTest::GenerateKeyTest, this));
993     }
994     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
995     SetThreadState(false);
996     for (auto &t : threads) {
997         t.join();
998     }
999 
1000     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1001     (void)HksDeleteKey(&authId, nullptr);
1002     int32_t ret = HksInitialize();
1003     ASSERT_TRUE(ret == HKS_SUCCESS);
1004 }
1005 
1006 /**
1007  * @tc.number    : ApiPressureTest.ApiPressureTest00500
1008  * @tc.name      : ApiPressureTest00500
1009  * @tc.desc      : HksImportKey
1010  */
1011 HWTEST_F(ApiPressureTest, ApiPressureTest00500, TestSize.Level1)
1012 {
1013     std::vector<std::thread> threads;
1014     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1015     struct HksParamSet *paramInSet = nullptr;
1016     HksInitParamSet(&paramInSet);
1017     struct HksParam tmpParams[] = {
1018         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1019         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1020         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1021         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
1022         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1023         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1024         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1025     };
1026     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1027     HksBuildParamSet(&paramInSet);
1028     HksGenerateKey(&authId, paramInSet, NULL);
1029     SetThreadState(true);
1030     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1031         threads.emplace_back(std::thread(&ApiPressureTest::ImportKeyTest, this, &authId, paramInSet));
1032     }
1033     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1034     SetThreadState(false);
1035     for (auto &t : threads) {
1036         t.join();
1037     }
1038     (void)HksDeleteKey(&authId, nullptr);
1039     HksFreeParamSet(&paramInSet);
1040 }
1041 
1042 /**
1043  * @tc.number    : ApiPressureTest.ApiPressureTest00600
1044  * @tc.name      : ApiPressureTest00600
1045  * @tc.desc      : HksExportPublicKey
1046  */
1047 HWTEST_F(ApiPressureTest, ApiPressureTest00600, TestSize.Level1)
1048 {
1049     std::vector<std::thread> threads;
1050     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1051     struct HksParamSet *paramInSet = nullptr;
1052     HksInitParamSet(&paramInSet);
1053     struct HksParam tmpParams[] = {
1054         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1055         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1056         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1057         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
1058         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1059         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1060         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1061     };
1062     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1063     HksBuildParamSet(&paramInSet);
1064     HksGenerateKey(&authId, paramInSet, NULL);
1065     SetThreadState(true);
1066     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1067         threads.emplace_back(std::thread(&ApiPressureTest::ExportPublicKeyTest, this, &authId, paramInSet));
1068     }
1069     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1070     SetThreadState(false);
1071     for (auto &t : threads) {
1072         t.join();
1073     }
1074     (void)HksDeleteKey(&authId, nullptr);
1075     HksFreeParamSet(&paramInSet);
1076 }
1077 
1078 /**
1079  * @tc.number    : ApiPressureTest.ApiPressureTest00700
1080  * @tc.name      : ApiPressureTest00700
1081  * @tc.desc      : HksDeleteKey
1082  */
1083 HWTEST_F(ApiPressureTest, ApiPressureTest00700, TestSize.Level1)
1084 {
1085     std::vector<std::thread> threads;
1086     SetThreadState(true);
1087     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1088         threads.emplace_back(std::thread(&ApiPressureTest::DeleteKeyTest, this, ii));
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     int32_t ret = HksInitialize();
1096     ASSERT_TRUE(ret == HKS_SUCCESS);
1097 }
1098 
1099 /**
1100  * @tc.number    : ApiPressureTest.ApiPressureTest00800
1101  * @tc.name      : ApiPressureTest00800
1102  * @tc.desc      : HksGetKeyParamSet
1103  */
1104 HWTEST_F(ApiPressureTest, ApiPressureTest00800, TestSize.Level1)
1105 {
1106     std::vector<std::thread> threads;
1107     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1108     struct HksParamSet *paramInSet = nullptr;
1109     HksInitParamSet(&paramInSet);
1110     struct HksParam tmpParams[] = {
1111         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1112         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1113         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1114         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
1115         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1116         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1117         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1118     };
1119     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1120     HksBuildParamSet(&paramInSet);
1121     HksGenerateKey(&authId, paramInSet, NULL);
1122     SetThreadState(true);
1123     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1124         threads.emplace_back(std::thread(&ApiPressureTest::GetKeyParamSetTest, this, &authId, paramInSet));
1125     }
1126     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1127     SetThreadState(false);
1128     for (auto &t : threads) {
1129         t.join();
1130     }
1131     (void)HksDeleteKey(&authId, nullptr);
1132     HksFreeParamSet(&paramInSet);
1133 }
1134 
1135 /**
1136  * @tc.number    : ApiPressureTest.ApiPressureTest00900
1137  * @tc.name      : ApiPressureTest00900
1138  * @tc.desc      : HksKeyExist
1139  */
1140 HWTEST_F(ApiPressureTest, ApiPressureTest00900, TestSize.Level1)
1141 {
1142     std::vector<std::thread> threads;
1143     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1144     struct HksParamSet *paramInSet = nullptr;
1145     HksInitParamSet(&paramInSet);
1146     struct HksParam tmpParams[] = {
1147         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1148         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1149         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1150         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
1151         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1152         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1153         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1154     };
1155     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1156     HksBuildParamSet(&paramInSet);
1157     HksGenerateKey(&authId, paramInSet, NULL);
1158     SetThreadState(true);
1159     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1160         threads.emplace_back(std::thread(&ApiPressureTest::KeyExistTest, this, &authId, paramInSet));
1161     }
1162     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1163     SetThreadState(false);
1164     for (auto &t : threads) {
1165         t.join();
1166     }
1167     (void)HksDeleteKey(&authId, nullptr);
1168     HksFreeParamSet(&paramInSet);
1169 }
1170 
1171 /**
1172  * @tc.number    : ApiPressureTest.ApiPressureTest01000
1173  * @tc.name      : ApiPressureTest01000
1174  * @tc.desc      : HksGenerateRandom
1175  */
1176 HWTEST_F(ApiPressureTest, ApiPressureTest01000, TestSize.Level1)
1177 {
1178     std::vector<std::thread> threads;
1179     SetThreadState(true);
1180     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1181         threads.emplace_back(std::thread(&ApiPressureTest::GenerateRandomTest, this));
1182     }
1183     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1184     SetThreadState(false);
1185     for (auto &t : threads) {
1186         t.join();
1187     }
1188     int32_t ret = HksInitialize();
1189     ASSERT_TRUE(ret == HKS_SUCCESS);
1190 }
1191 
1192 /**
1193  * @tc.number    : ApiPressureTest.ApiPressureTest01100
1194  * @tc.name      : ApiPressureTest01100
1195  * @tc.desc      : HksSign
1196  */
1197 HWTEST_F(ApiPressureTest, ApiPressureTest01100, TestSize.Level1)
1198 {
1199     std::vector<std::thread> threads;
1200     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1201     struct HksParamSet *paramInSet = nullptr;
1202     HksInitParamSet(&paramInSet);
1203     struct HksParam tmpParams[] = {
1204         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1205         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1206         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1207         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
1208         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1209         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1210         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1211     };
1212     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1213     HksBuildParamSet(&paramInSet);
1214     HksGenerateKey(&authId, paramInSet, NULL);
1215     const char *hexData = "0123456789abcdef";
1216     uint32_t dataLen = strlen(hexData);
1217     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1218     SetThreadState(true);
1219     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1220         threads.emplace_back(std::thread(&ApiPressureTest::SignTest, this, &authId, paramInSet, &message));
1221     }
1222     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1223     SetThreadState(false);
1224     for (auto &t : threads) {
1225         t.join();
1226     }
1227     (void)HksDeleteKey(&authId, nullptr);
1228     HksFreeParamSet(&paramInSet);
1229 }
1230 
1231 /**
1232  * @tc.number    : ApiPressureTest.ApiPressureTest01200
1233  * @tc.name      : ApiPressureTest01200
1234  * @tc.desc      : HksVerify
1235  */
1236 HWTEST_F(ApiPressureTest, ApiPressureTest01200, TestSize.Level1)
1237 {
1238     std::vector<std::thread> threads;
1239     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1240     struct HksParamSet *paramInSet = nullptr;
1241     HksInitParamSet(&paramInSet);
1242     struct HksParam tmpParams[] = {
1243         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1244         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1245         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1246         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
1247         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1248         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1249         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1250     };
1251     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1252     HksBuildParamSet(&paramInSet);
1253     HksGenerateKey(&authId, paramInSet, NULL);
1254     const char *hexData = "0123456789abcdef";
1255     uint32_t dataLen = strlen(hexData);
1256     HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1257     HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1258     ASSERT_NE(signature.data, nullptr);
1259     HksSign(&authId, paramInSet, &message, &signature);
1260     SetThreadState(true);
1261     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1262         threads.emplace_back(
1263             std::thread(&ApiPressureTest::VerifyTest, this, &authId, paramInSet, &message, &signature));
1264     }
1265     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1266     SetThreadState(false);
1267     for (auto &t : threads) {
1268         t.join();
1269     }
1270     (void)HksDeleteKey(&authId, nullptr);
1271     HksFree(signature.data);
1272     HksFreeParamSet(&paramInSet);
1273 }
1274 
1275 /**
1276  * @tc.number    : ApiPressureTest.ApiPressureTest01300
1277  * @tc.name      : ApiPressureTest01300
1278  * @tc.desc      : HksEncrypt
1279  */
1280 HWTEST_F(ApiPressureTest, ApiPressureTest01300, TestSize.Level1)
1281 {
1282     std::vector<std::thread> threads;
1283     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1284     struct HksParamSet *paramInSet = nullptr;
1285     HksInitParamSet(&paramInSet);
1286     struct HksParam tmpParams[] = {
1287         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1288         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1289         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1290         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
1291         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1292         { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
1293         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1294         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1295         { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
1296     };
1297     uint8_t iv[IV_SIZE] = {0};
1298     struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1299     HksAddParams(paramInSet, &tagIv, 1);
1300     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1301     HksBuildParamSet(&paramInSet);
1302     HksGenerateKey(&authId, paramInSet, NULL);
1303     SetThreadState(true);
1304     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1305         threads.emplace_back(std::thread(&ApiPressureTest::EncryptTest, this, &authId, paramInSet));
1306     }
1307     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1308     SetThreadState(false);
1309     for (auto &t : threads) {
1310         t.join();
1311     }
1312     (void)HksDeleteKey(&authId, nullptr);
1313     HksFreeParamSet(&paramInSet);
1314 }
1315 
1316 /**
1317  * @tc.number    : ApiPressureTest.ApiPressureTest01400
1318  * @tc.name      : ApiPressureTest01400
1319  * @tc.desc      : HksDecrypt
1320  */
1321 HWTEST_F(ApiPressureTest, ApiPressureTest01400, TestSize.Level1)
1322 {
1323     std::vector<std::thread> threads;
1324     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1325     struct HksParamSet *paramInSet = nullptr;
1326     HksInitParamSet(&paramInSet);
1327     struct HksParam tmpParams[] = {
1328         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1329         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1330         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1331         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
1332         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1333         { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
1334         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1335         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1336         { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
1337     };
1338     uint8_t iv[IV_SIZE] = {0};
1339     struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1340     HksAddParams(paramInSet, &tagIv, 1);
1341     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1342     HksBuildParamSet(&paramInSet);
1343     HksGenerateKey(&authId, paramInSet, NULL);
1344     const char *hexData = "0123456789abcdef";
1345     uint32_t dataLen = strlen(hexData);
1346     HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
1347     uint32_t inLen = dataLen + COMPLEMENT_LEN;
1348     HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
1349     ASSERT_NE(cipherText.data, nullptr);
1350     HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
1351     SetThreadState(true);
1352     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1353         threads.emplace_back(
1354             std::thread(&ApiPressureTest::DecryptTest, this, &authId, paramInSet, &cipherText, &inLen));
1355     }
1356     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1357     SetThreadState(false);
1358     for (auto &t : threads) {
1359         t.join();
1360     }
1361     (void)HksDeleteKey(&authId, nullptr);
1362     HksFree(cipherText.data);
1363     HksFreeParamSet(&paramInSet);
1364 }
1365 
1366 /**
1367  * @tc.number    : ApiPressureTest.ApiPressureTest01500
1368  * @tc.name      : ApiPressureTest01500
1369  * @tc.desc      : HksAgreeKey
1370  */
1371 HWTEST_F(ApiPressureTest, ApiPressureTest01500, TestSize.Level1)
1372 {
1373     std::vector<std::thread> threads;
1374     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1375     struct HksParamSet *paramInSet = nullptr;
1376     HksInitParamSet(&paramInSet);
1377     struct HksParam tmpParams[] = {
1378         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1379         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1380         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1381         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
1382         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1383         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1384         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1385     };
1386     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1387     HksBuildParamSet(&paramInSet);
1388     HksGenerateKey(&authId, paramInSet, NULL);
1389     SetThreadState(true);
1390     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1391         threads.emplace_back(std::thread(&ApiPressureTest::AgreeKeyTest, this, &authId));
1392     }
1393     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1394     SetThreadState(false);
1395     for (auto &t : threads) {
1396         t.join();
1397     }
1398     (void)HksDeleteKey(&authId, nullptr);
1399     HksFreeParamSet(&paramInSet);
1400 }
1401 
1402 /**
1403  * @tc.number    : ApiPressureTest.ApiPressureTest01600
1404  * @tc.name      : ApiPressureTest01600
1405  * @tc.desc      : HksDeriveKey
1406  */
1407 HWTEST_F(ApiPressureTest, ApiPressureTest01600, TestSize.Level1)
1408 {
1409     std::vector<std::thread> threads;
1410     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1411     struct HksParamSet *paramInSet = nullptr;
1412     HksInitParamSet(&paramInSet);
1413     struct HksParam tmpParams[] = {
1414         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1415         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
1416         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
1417         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
1418         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
1419         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1420         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1421     };
1422     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1423     HksBuildParamSet(&paramInSet);
1424     HksGenerateKey(&authId, paramInSet, NULL);
1425     struct HksParamSet *paramInSetHkdf = nullptr;
1426     HksInitParamSet(&paramInSetHkdf);
1427     struct HksParam tmpParamsHkdf[] = {
1428         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1429         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HKDF },
1430         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
1431         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
1432         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1433         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1434     };
1435     HksAddParams(paramInSetHkdf, tmpParamsHkdf, sizeof(tmpParamsHkdf) / sizeof(tmpParamsHkdf[0]));
1436     HksBuildParamSet(&paramInSetHkdf);
1437     SetThreadState(true);
1438     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1439         threads.emplace_back(std::thread(&ApiPressureTest::DeriveKeyTest, this, &authId, paramInSetHkdf));
1440     }
1441     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1442     SetThreadState(false);
1443     for (auto &t : threads) {
1444         t.join();
1445     }
1446     (void)HksDeleteKey(&authId, nullptr);
1447     HksFreeParamSet(&paramInSet);
1448     HksFreeParamSet(&paramInSetHkdf);
1449 }
1450 
1451 /**
1452  * @tc.number    : ApiPressureTest.ApiPressureTest01700
1453  * @tc.name      : ApiPressureTest01700
1454  * @tc.desc      : HksMac
1455  */
1456 HWTEST_F(ApiPressureTest, ApiPressureTest01700, TestSize.Level1)
1457 {
1458     std::vector<std::thread> threads;
1459     struct HksBlob authId = { (uint32_t)strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1460     struct HksParamSet *paramInSet = nullptr;
1461     HksInitParamSet(&paramInSet);
1462     struct HksParam tmpParams[] = {
1463         { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
1464         { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HMAC },
1465         { .tag = HKS_TAG_KEY_SIZE, .uint32Param = TEST_KEY_SIZE },
1466         { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_MAC },
1467         { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 },
1468         { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
1469         { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1470     };
1471     HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1472     HksBuildParamSet(&paramInSet);
1473     HksGenerateKey(&authId, paramInSet, NULL);
1474     SetThreadState(true);
1475     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1476         threads.emplace_back(std::thread(&ApiPressureTest::MacTest, this, &authId, paramInSet));
1477     }
1478     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1479     SetThreadState(false);
1480     for (auto &t : threads) {
1481         t.join();
1482     }
1483     (void)HksDeleteKey(&authId, nullptr);
1484     HksFreeParamSet(&paramInSet);
1485 }
1486 
1487 /**
1488  * @tc.number    : ApiPressureTest.ApiPressureTest01800
1489  * @tc.name      : ApiPressureTest01800
1490  * @tc.desc      : HksHash
1491  */
1492 HWTEST_F(ApiPressureTest, ApiPressureTest01800, TestSize.Level1)
1493 {
1494     std::vector<std::thread> threads;
1495     SetThreadState(true);
1496     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1497         threads.emplace_back(std::thread(&ApiPressureTest::HashTest, this));
1498     }
1499     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1500     SetThreadState(false);
1501     for (auto &t : threads) {
1502         t.join();
1503     }
1504     int32_t ret = HksInitialize();
1505     ASSERT_TRUE(ret == HKS_SUCCESS);
1506 }
1507 
1508 /**
1509  * @tc.number    : ApiPressureTest.ApiPressureTest01900
1510  * @tc.name      : ApiPressureTest01900
1511  * @tc.desc      : Scenario 1: generate key and encrypt / decrypt
1512  */
1513 HWTEST_F(ApiPressureTest, ApiPressureTest01900, TestSize.Level1)
1514 {
1515     std::vector<std::thread> threads;
1516     SetThreadState(true);
1517     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1518         threads.emplace_back(std::thread(&ApiPressureTest::CipherScene, this, ii));
1519     }
1520     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1521     SetThreadState(false);
1522     for (auto &t : threads) {
1523         t.join();
1524     }
1525     int32_t ret = HksInitialize();
1526     ASSERT_TRUE(ret == HKS_SUCCESS);
1527 }
1528 
1529 /**
1530  * @tc.number    : ApiPressureTest.ApiPressureTest02000
1531  * @tc.name      : ApiPressureTest02000
1532  * @tc.desc      : Scenario 2: generate key and sign / verify
1533  */
1534 HWTEST_F(ApiPressureTest, ApiPressureTest02000, TestSize.Level1)
1535 {
1536     std::vector<std::thread> threads;
1537     SetThreadState(true);
1538     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1539         threads.emplace_back(std::thread(&ApiPressureTest::SignScene, this, ii));
1540     }
1541     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1542     SetThreadState(false);
1543     for (auto &t : threads) {
1544         t.join();
1545     }
1546     int32_t ret = HksInitialize();
1547     ASSERT_TRUE(ret == HKS_SUCCESS);
1548 }
1549 
1550 /**
1551  * @tc.number    : ApiPressureTest.ApiPressureTest02100
1552  * @tc.name      : ApiPressureTest02100
1553  * @tc.desc      : Scenario 3: generate key and agree
1554  */
1555 HWTEST_F(ApiPressureTest, ApiPressureTest02100, TestSize.Level1)
1556 {
1557     std::vector<std::thread> threads;
1558     SetThreadState(true);
1559     for (uint32_t ii = 0; ii < THREADS_NUM; ii++) {
1560         threads.emplace_back(std::thread(&ApiPressureTest::AgreeScene, this, ii));
1561     }
1562     std::this_thread::sleep_for(std::chrono::hours(TEST_HOURS));
1563     SetThreadState(false);
1564     for (auto &t : threads) {
1565         t.join();
1566     }
1567     int32_t ret = HksInitialize();
1568     ASSERT_TRUE(ret == HKS_SUCCESS);
1569 }
1570 }  // namespace
1571