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