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 <chrono>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include <sys/time.h>
20
21 #include "hks_api.h"
22 #include "hks_log.h"
23 #include "hks_mem.h"
24 #include "hks_param.h"
25
26 using namespace testing::ext;
27 namespace {
28 namespace {
29 const char GENERATE_KEY[] = "This is for generate key";
30 const char IMPORT_KEY[] = "This is for import key";
31 const uint32_t TEST_FREQUENCY = 1000;
32 const uint32_t MAX_SDK_VERSION_SIZE = 64;
33 const uint32_t IV_SIZE = 16;
34 const uint32_t COMPLEMENT_LEN = 16;
35 const uint32_t KEY_PARAMSET_SIZE = 1024;
36 const uint32_t MESSAGE_SIZE = 64;
37 const uint32_t TEST_KEY_SIZE = 512;
38 const uint32_t DERIVED_KEY_SIZE = 64;
39
40 static const struct HksParam PARAMS_FOR_ENCRYPT[] = {
41 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
42 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
43 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
44 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
45 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
46 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
47 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
48 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
49 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
50 };
51
52 static const struct HksParam PARAMS_FOR_DECRYPT[] = {
53 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
54 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
55 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
56 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
57 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
58 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
59 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
60 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
61 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
62 };
63 } // namespace
64 class PressureTest : public testing::Test {
65 public:
66 int32_t LocalHksGenerate(const uint32_t keyLen, const struct HksBlob *authId, const struct HksParamSet *paramSetIn,
67 struct HksBlob *priKey, struct HksBlob *pubKey) const;
68 };
69
LocalHksGenerate(const uint32_t keyLen,const struct HksBlob * authId,const struct HksParamSet * paramSetIn,struct HksBlob * priKey,struct HksBlob * pubKey) const70 int32_t PressureTest::LocalHksGenerate(const uint32_t keyLen, const struct HksBlob *authId,
71 const struct HksParamSet *paramSetIn, struct HksBlob *priKey, struct HksBlob *pubKey) const
72 {
73 struct HksParamSet *paramOutSet = nullptr;
74 HksInitParamSet(¶mOutSet);
75 struct HksParam localKey = {
76 .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
77 .blob = { .size = keyLen, .data = (uint8_t *)HksMalloc(keyLen) }
78 };
79 if (localKey.blob.data == nullptr) {
80 HksFreeParamSet(¶mOutSet);
81 return HKS_FAILURE;
82 }
83 HksAddParams(paramOutSet, &localKey, 1);
84 HksBuildParamSet(¶mOutSet);
85
86 if (HksGenerateKey(authId, paramSetIn, paramOutSet) != HKS_SUCCESS) {
87 HksFree(localKey.blob.data);
88 HksFreeParamSet(¶mOutSet);
89 return HKS_SUCCESS;
90 }
91
92 HksParam *priParam = nullptr;
93 HksGetParam(paramOutSet, HKS_TAG_ASYMMETRIC_PRIVATE_KEY_DATA, &priParam);
94 priKey->size = priParam->blob.size;
95 (void)memcpy_s(priKey->data, priParam->blob.size, priParam->blob.data, priParam->blob.size);
96
97 HksParam *pubParam = nullptr;
98 HksGetParam(paramOutSet, HKS_TAG_ASYMMETRIC_PUBLIC_KEY_DATA, &pubParam);
99 pubKey->size = pubParam->blob.size;
100 (void)memcpy_s(pubKey->data, pubParam->blob.size, pubParam->blob.data, pubParam->blob.size);
101
102 HksFree(localKey.blob.data);
103 HksFreeParamSet(¶mOutSet);
104 return HKS_SUCCESS;
105 }
106
107 /**
108 * @tc.number : PressureTest.PressureTest00100
109 * @tc.name : PressureTest00100
110 * @tc.desc : HksGetSdkVersion
111 */
112 HWTEST_F(PressureTest, PressureTest00100, TestSize.Level1)
113 {
114 double programTimes = 0;
115
116 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
117 struct HksBlob sdkVersion = { .size = MAX_SDK_VERSION_SIZE,
118 .data = (uint8_t *)HksMalloc(MAX_SDK_VERSION_SIZE) };
119 ASSERT_NE(sdkVersion.data, nullptr);
120 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
121 int32_t ret = HksGetSdkVersion(&sdkVersion);
122 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
123 EXPECT_EQ(ret, HKS_SUCCESS);
124 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
125 HksFree(sdkVersion.data);
126 }
127 HKS_LOG_I("HksGetSdkVersion Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
128 }
129
130 /**
131 * @tc.number : PressureTest.PressureTest00200
132 * @tc.name : PressureTest00200
133 * @tc.desc : HksInitialize
134 */
135 HWTEST_F(PressureTest, PressureTest00200, TestSize.Level1)
136 {
137 double programTimes = 0;
138
139 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
140 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
141 int32_t ret = HksInitialize();
142 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
143 EXPECT_EQ(ret, HKS_SUCCESS);
144 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
145 }
146 HKS_LOG_I("HksInitialize Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
147 }
148
149 /**
150 * @tc.number : PressureTest.PressureTest00300
151 * @tc.name : PressureTest00300
152 * @tc.desc : HksRefreshKeyInfo
153 */
154 HWTEST_F(PressureTest, PressureTest00300, TestSize.Level1)
155 {
156 double programTimes = 0;
157
158 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
159 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
160 int32_t ret = HksRefreshKeyInfo();
161 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
162 EXPECT_EQ(ret, HKS_SUCCESS);
163 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
164 }
165 HKS_LOG_I("HksRefreshKeyInfo Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
166 }
167
168 /**
169 * @tc.number : PressureTest.PressureTest00400
170 * @tc.name : PressureTest00400
171 * @tc.desc : HksGenerateKey
172 */
173 HWTEST_F(PressureTest, PressureTest00400, TestSize.Level1)
174 {
175 double programTimes = 0;
176 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
177
178 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
179 struct HksParamSet *paramInSet = nullptr;
180 HksInitParamSet(¶mInSet);
181
182 struct HksParam tmpParams[] = {
183 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
184 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
185 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
186 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
187 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
188 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
189 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
190 };
191
192 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
193 HksBuildParamSet(¶mInSet);
194
195 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
196 int32_t ret = HksGenerateKey(&authId, paramInSet, NULL);
197 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
198 EXPECT_EQ(ret, HKS_SUCCESS);
199 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
200
201 HksDeleteKey(&authId, paramInSet);
202 HksFreeParamSet(¶mInSet);
203 }
204 HKS_LOG_I("HksGenerateKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
205 }
206
207 /**
208 * @tc.number : PressureTest.PressureTest00500
209 * @tc.name : PressureTest00500
210 * @tc.desc : HksImportKey
211 */
212 HWTEST_F(PressureTest, PressureTest00500, TestSize.Level1)
213 {
214 double programTimes = 0;
215 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
216
217 struct HksParamSet *paramInSet = nullptr;
218 HksInitParamSet(¶mInSet);
219
220 struct HksParam tmpParams[] = {
221 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
222 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
223 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
224 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
225 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
226 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
227 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
228 };
229
230 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
231 HksBuildParamSet(¶mInSet);
232
233 HksGenerateKey(&authId, paramInSet, NULL);
234
235 struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
236 ASSERT_NE(pubKey.data, nullptr);
237 HksExportPublicKey(&authId, paramInSet, &pubKey);
238
239 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
240 struct HksBlob importId = { strlen(IMPORT_KEY), (uint8_t *)IMPORT_KEY };
241 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
242 int32_t ret = HksImportKey(&importId, paramInSet, &pubKey);
243 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
244 EXPECT_EQ(ret, HKS_SUCCESS);
245 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
246 HksDeleteKey(&importId, paramInSet);
247 }
248 HksDeleteKey(&authId, paramInSet);
249 HksFree(pubKey.data);
250 HksFreeParamSet(¶mInSet);
251 HKS_LOG_I("HksImportKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
252 }
253
254 /**
255 * @tc.number : PressureTest.PressureTest00600
256 * @tc.name : PressureTest00600
257 * @tc.desc : HksExportPublicKey
258 */
259 HWTEST_F(PressureTest, PressureTest00600, TestSize.Level1)
260 {
261 double programTimes = 0;
262 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
263
264 struct HksParamSet *paramInSet = nullptr;
265 HksInitParamSet(¶mInSet);
266
267 struct HksParam tmpParams[] = {
268 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
269 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
270 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
271 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
272 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
273 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
274 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
275 };
276
277 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
278 HksBuildParamSet(¶mInSet);
279
280 HksGenerateKey(&authId, paramInSet, NULL);
281
282 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
283 struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
284 ASSERT_NE(pubKey.data, nullptr);
285 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
286 int32_t ret = HksExportPublicKey(&authId, paramInSet, &pubKey);
287 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
288 EXPECT_EQ(ret, HKS_SUCCESS);
289 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
290
291 HksFree(pubKey.data);
292 }
293 HksDeleteKey(&authId, paramInSet);
294 HksFreeParamSet(¶mInSet);
295 HKS_LOG_I("HksExportPublicKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
296 }
297
298 /**
299 * @tc.number : PressureTest.PressureTest00700
300 * @tc.name : PressureTest00700
301 * @tc.desc : HksDeleteKey
302 */
303 HWTEST_F(PressureTest, PressureTest00700, TestSize.Level1)
304 {
305 double programTimes = 0;
306
307 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
308 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
309
310 struct HksParamSet *paramInSet = nullptr;
311 HksInitParamSet(¶mInSet);
312
313 struct HksParam tmpParams[] = {
314 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
315 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
316 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
317 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
318 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
319 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
320 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
321 };
322
323 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
324 HksBuildParamSet(¶mInSet);
325
326 HksGenerateKey(&authId, paramInSet, NULL);
327 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
328 int32_t ret = HksDeleteKey(&authId, paramInSet);
329 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
330 EXPECT_EQ(ret, HKS_SUCCESS);
331 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
332
333 HksFreeParamSet(¶mInSet);
334 }
335 HKS_LOG_I("HksDeleteKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
336 }
337
338 /**
339 * @tc.number : PressureTest.PressureTest00800
340 * @tc.name : PressureTest00800
341 * @tc.desc : HksGetKeyParamSet
342 */
343 HWTEST_F(PressureTest, PressureTest00800, TestSize.Level1)
344 {
345 double programTimes = 0;
346 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
347
348 struct HksParamSet *paramInSet = nullptr;
349 HksInitParamSet(¶mInSet);
350
351 struct HksParam tmpParams[] = {
352 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
353 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
354 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
355 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
356 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
357 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
358 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
359 };
360
361 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
362 HksBuildParamSet(¶mInSet);
363
364 HksGenerateKey(&authId, paramInSet, NULL);
365
366 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
367 struct HksParamSet *paramOutSet = nullptr;
368 HksInitParamSet(¶mOutSet);
369 struct HksParam localKey = { .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
370 .blob = { .size = KEY_PARAMSET_SIZE, .data = (uint8_t *)HksMalloc(KEY_PARAMSET_SIZE) } };
371 ASSERT_NE(localKey.blob.data, nullptr);
372 HksAddParams(paramOutSet, &localKey, 1);
373
374 HksBuildParamSet(¶mOutSet);
375 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
376 int32_t ret = HksGetKeyParamSet(&authId, paramInSet, paramOutSet);
377 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
378 EXPECT_EQ(ret, HKS_SUCCESS);
379 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
380
381 HksFree(localKey.blob.data);
382 HksFreeParamSet(¶mOutSet);
383 }
384 HksDeleteKey(&authId, paramInSet);
385 HksFreeParamSet(¶mInSet);
386 HKS_LOG_I("HksGetKeyParamSet Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
387 }
388
389 /**
390 * @tc.number : PressureTest.PressureTest00900
391 * @tc.name : PressureTest00900
392 * @tc.desc : HksKeyExist
393 */
394 HWTEST_F(PressureTest, PressureTest00900, TestSize.Level1)
395 {
396 double programTimes = 0;
397 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
398
399 struct HksParamSet *paramInSet = nullptr;
400 HksInitParamSet(¶mInSet);
401
402 struct HksParam tmpParams[] = {
403 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
404 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
405 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
406 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
407 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
408 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
409 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
410 };
411
412 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
413 HksBuildParamSet(¶mInSet);
414
415 HksGenerateKey(&authId, paramInSet, NULL);
416
417 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
418 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
419 int32_t ret = HksKeyExist(&authId, paramInSet);
420 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
421 EXPECT_EQ(ret, HKS_SUCCESS);
422 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
423 }
424 HksDeleteKey(&authId, paramInSet);
425 HksFreeParamSet(¶mInSet);
426 HKS_LOG_I("HksKeyExist Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
427 }
428
429 /**
430 * @tc.number : PressureTest.PressureTest01000
431 * @tc.name : PressureTest01000
432 * @tc.desc : HksGenerateRandom
433 */
434 HWTEST_F(PressureTest, PressureTest01000, TestSize.Level1)
435 {
436 double programTimes = 0;
437
438 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
439 struct HksBlob authId = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
440 ASSERT_NE(authId.data, nullptr);
441 struct HksParamSet *paramInSet = nullptr;
442 HksInitParamSet(¶mInSet);
443
444 struct HksParam tmpParams[] = {
445 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
446 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
447 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
448 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
449 };
450
451 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
452 HksBuildParamSet(¶mInSet);
453
454 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
455 int32_t ret = HksGenerateRandom(paramInSet, &authId);
456 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
457 EXPECT_EQ(ret, HKS_SUCCESS);
458 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
459
460 HksFree(authId.data);
461 HksFreeParamSet(¶mInSet);
462 }
463 HKS_LOG_I("HksGenerateRandom Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
464 }
465
466 /**
467 * @tc.number : PressureTest.PressureTest01100
468 * @tc.name : PressureTest01100
469 * @tc.desc : HksSign
470 */
471 HWTEST_F(PressureTest, PressureTest01100, TestSize.Level1)
472 {
473 double programTimes = 0;
474 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
475
476 struct HksParamSet *paramInSet = nullptr;
477 HksInitParamSet(¶mInSet);
478
479 struct HksParam tmpParams[] = {
480 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
481 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
482 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
483 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
484 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
485 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
486 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
487 };
488
489 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
490 HksBuildParamSet(¶mInSet);
491
492 HksGenerateKey(&authId, paramInSet, NULL);
493
494 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
495 const char *hexData = "0123456789abcdef";
496 uint32_t dataLen = strlen(hexData);
497 HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
498 HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
499 ASSERT_NE(signature.data, nullptr);
500
501 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
502 int32_t ret = HksSign(&authId, paramInSet, &message, &signature);
503 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
504 EXPECT_EQ(ret, HKS_SUCCESS);
505 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
506
507 HksFree(signature.data);
508 }
509 HksDeleteKey(&authId, paramInSet);
510 HksFreeParamSet(¶mInSet);
511 HKS_LOG_I("HksSign Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
512 }
513
514 /**
515 * @tc.number : PressureTest.PressureTest01200
516 * @tc.name : PressureTest01200
517 * @tc.desc : HksVerify
518 */
519 HWTEST_F(PressureTest, PressureTest01200, TestSize.Level1)
520 {
521 double programTimes = 0;
522 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
523
524 struct HksParamSet *paramInSet = nullptr;
525 HksInitParamSet(¶mInSet);
526
527 struct HksParam tmpParams[] = {
528 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
529 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
530 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
531 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
532 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
533 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
534 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
535 };
536
537 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
538 HksBuildParamSet(¶mInSet);
539
540 HksGenerateKey(&authId, paramInSet, NULL);
541 const char *hexData = "0123456789abcdef";
542 uint32_t dataLen = strlen(hexData);
543 HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
544 HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
545 ASSERT_NE(signature.data, nullptr);
546
547 HksSign(&authId, paramInSet, &message, &signature);
548
549 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
550
551 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
552 int32_t ret = HksVerify(&authId, paramInSet, &message, &signature);
553 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
554 EXPECT_EQ(ret, HKS_SUCCESS);
555 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
556 }
557 HksDeleteKey(&authId, paramInSet);
558 HksFree(signature.data);
559 HksFreeParamSet(¶mInSet);
560 HKS_LOG_I("HksVerify Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
561 }
562
563 /**
564 * @tc.number : PressureTest.PressureTest01300
565 * @tc.name : PressureTest01300
566 * @tc.desc : HksEncrypt
567 */
568 HWTEST_F(PressureTest, PressureTest01300, TestSize.Level1)
569 {
570 double programTimes = 0;
571 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
572
573 struct HksParamSet *paramInSet = nullptr;
574 HksInitParamSet(¶mInSet);
575
576 struct HksParam tmpParams[] = {
577 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
578 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
579 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
580 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
581 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
582 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
583 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
584 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
585 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
586 };
587
588 uint8_t iv[IV_SIZE] = {0};
589 struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
590 HksAddParams(paramInSet, &tagIv, 1);
591
592 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
593 HksBuildParamSet(¶mInSet);
594
595 HksGenerateKey(&authId, paramInSet, NULL);
596
597 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
598 const char *hexData = "0123456789abcdef";
599 uint32_t dataLen = strlen(hexData);
600 HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
601
602 uint32_t inLen = dataLen + COMPLEMENT_LEN;
603 HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
604 ASSERT_NE(cipherText.data, nullptr);
605
606 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
607 int32_t ret = HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
608 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
609 EXPECT_EQ(ret, HKS_SUCCESS);
610 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
611
612 HksFree(cipherText.data);
613 }
614 HksDeleteKey(&authId, paramInSet);
615 HksFreeParamSet(¶mInSet);
616 HKS_LOG_I("HksEncrypt Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
617 }
618
619 /**
620 * @tc.number : PressureTest.PressureTest01400
621 * @tc.name : PressureTest01400
622 * @tc.desc : HksDecrypt
623 */
624 HWTEST_F(PressureTest, PressureTest01400, TestSize.Level1)
625 {
626 double programTimes = 0;
627 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
628
629 struct HksParamSet *paramInSet = nullptr;
630 HksInitParamSet(¶mInSet);
631
632 struct HksParam tmpParams[] = {
633 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
634 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
635 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
636 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
637 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
638 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_PKCS7 },
639 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
640 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
641 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
642 };
643
644 uint8_t iv[IV_SIZE] = {0};
645 struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
646 HksAddParams(paramInSet, &tagIv, 1);
647
648 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
649 HksBuildParamSet(¶mInSet);
650
651 HksGenerateKey(&authId, paramInSet, NULL);
652
653 const char *hexData = "0123456789abcdef";
654 uint32_t dataLen = strlen(hexData);
655 HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
656
657 uint32_t inLen = dataLen + COMPLEMENT_LEN;
658 HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
659 ASSERT_NE(cipherText.data, nullptr);
660
661 HksEncrypt(&authId, paramInSet, &plainText, &cipherText);
662
663 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
664 HksBlob plainTextDecrypt = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
665 ASSERT_NE(plainTextDecrypt.data, nullptr);
666 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
667 int32_t ret = HksDecrypt(&authId, paramInSet, &cipherText, &plainTextDecrypt);
668 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
669 EXPECT_EQ(ret, HKS_SUCCESS);
670 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
671
672 HksFree(plainTextDecrypt.data);
673 }
674 HksDeleteKey(&authId, paramInSet);
675 HksFree(cipherText.data);
676 HksFreeParamSet(¶mInSet);
677 HKS_LOG_I("HksDecrypt Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
678 }
679
680 /**
681 * @tc.number : PressureTest.PressureTest01500
682 * @tc.name : PressureTest01500
683 * @tc.desc : HksAgreeKey
684 */
685 HWTEST_F(PressureTest, PressureTest01500, TestSize.Level1)
686 {
687 double programTimes = 0;
688 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
689
690 struct HksParamSet *paramInSetForKey = nullptr;
691 HksInitParamSet(¶mInSetForKey);
692 struct HksParamSet *paramInSet = nullptr;
693 HksInitParamSet(¶mInSet);
694
695 struct HksParam tmpParams[] = {
696 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
697 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
698 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
699 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
700 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
701 };
702
703 HksAddParams(paramInSetForKey, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
704 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
705
706 HksParam algForKey = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC };
707 HksParam alg = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECDH };
708
709 HksAddParams(paramInSetForKey, &algForKey, 1);
710 HksAddParams(paramInSet, &alg, 1);
711 HksParam degistForKey = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE };
712 HksAddParams(paramInSetForKey, °istForKey, 1);
713 HksBuildParamSet(¶mInSetForKey);
714 HksBuildParamSet(¶mInSet);
715
716 HksGenerateKey(&authId, paramInSetForKey, NULL);
717
718 struct HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
719 ASSERT_NE(pubKey.data, nullptr);
720 HksExportPublicKey(&authId, paramInSetForKey, &pubKey);
721
722 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
723 HksBlob agreeKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
724 ASSERT_NE(agreeKey.data, nullptr);
725 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
726 int32_t ret = HksAgreeKey(paramInSet, &authId, &pubKey, &agreeKey);
727 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
728 EXPECT_EQ(ret, HKS_SUCCESS);
729 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
730
731 HksFree(agreeKey.data);
732 }
733 HksDeleteKey(&authId, paramInSet);
734 HksFree(pubKey.data);
735 HksFreeParamSet(¶mInSetForKey);
736 HksFreeParamSet(¶mInSet);
737 HKS_LOG_I("HksAgreeKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
738 }
739
740 /**
741 * @tc.number : PressureTest.PressureTest01600
742 * @tc.name : PressureTest01600
743 * @tc.desc : HksDeriveKey
744 */
745 HWTEST_F(PressureTest, PressureTest01600, TestSize.Level1)
746 {
747 double programTimes = 0;
748
749 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
750
751 struct HksParamSet *paramInSet = nullptr;
752 HksInitParamSet(¶mInSet);
753
754 struct HksParam tmpParams[] = {
755 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
756 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
757 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
758 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
759 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
760 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
761 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
762 };
763
764 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
765 HksBuildParamSet(¶mInSet);
766
767 HksGenerateKey(&authId, paramInSet, NULL);
768
769 struct HksParamSet *paramInSetHkdf = nullptr;
770 HksInitParamSet(¶mInSetHkdf);
771 struct HksParam tmpParamsHkdf[] = {
772 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
773 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HKDF },
774 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = DERIVED_KEY_SIZE },
775 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
776 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
777 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
778 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
779 };
780
781 HksAddParams(paramInSetHkdf, tmpParamsHkdf, sizeof(tmpParamsHkdf) / sizeof(tmpParamsHkdf[0]));
782 HksBuildParamSet(¶mInSetHkdf);
783
784 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
785 HksBlob derivedKey = { .size = DERIVED_KEY_SIZE, .data = (uint8_t *)HksMalloc(DERIVED_KEY_SIZE) };
786 ASSERT_NE(derivedKey.data, nullptr);
787 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
788 int32_t ret = HksDeriveKey(paramInSetHkdf, &authId, &derivedKey);
789 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
790 EXPECT_EQ(ret, HKS_SUCCESS);
791 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
792
793 HksFree(derivedKey.data);
794 }
795 HksDeleteKey(&authId, paramInSet);
796 HksFreeParamSet(¶mInSet);
797 HksFreeParamSet(¶mInSetHkdf);
798 HKS_LOG_I("HksDeriveKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
799 }
800
801 /**
802 * @tc.number : PressureTest.PressureTest01700
803 * @tc.name : PressureTest01700
804 * @tc.desc : HksMac
805 */
806 HWTEST_F(PressureTest, PressureTest01700, TestSize.Level1)
807 {
808 double programTimes = 0;
809 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
810
811 struct HksParamSet *paramInSet = nullptr;
812 HksInitParamSet(¶mInSet);
813
814 struct HksParam tmpParams[] = {
815 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_PERSISTENT },
816 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HMAC },
817 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = TEST_KEY_SIZE },
818 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_MAC },
819 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 },
820 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = true },
821 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
822 };
823
824 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
825 HksBuildParamSet(¶mInSet);
826
827 HksGenerateKey(&authId, paramInSet, NULL);
828
829 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
830 const char *hexData = "0123456789abcdef";
831 uint32_t dataLen = strlen(hexData);
832 HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
833 HksBlob macMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
834 ASSERT_NE(macMessage.data, nullptr);
835 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
836 int32_t ret = HksMac(&authId, paramInSet, &message, &macMessage);
837 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
838 EXPECT_EQ(ret, HKS_SUCCESS);
839 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
840
841 HksFree(macMessage.data);
842 }
843 HksDeleteKey(&authId, paramInSet);
844 HksFreeParamSet(¶mInSet);
845 HKS_LOG_I("HksMac Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
846 }
847
848 /**
849 * @tc.number : PressureTest.PressureTest01800
850 * @tc.name : PressureTest01800
851 * @tc.desc : HksHash
852 */
853 HWTEST_F(PressureTest, PressureTest01800, TestSize.Level1)
854 {
855 double programTimes = 0;
856
857 struct HksParamSet *paramInSet = nullptr;
858 HksInitParamSet(¶mInSet);
859
860 struct HksParam digest = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 };
861 HksAddParams(paramInSet, &digest, 1);
862 HksBuildParamSet(¶mInSet);
863
864 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
865 const char *hexData = "0123456789abcdef";
866 uint32_t dataLen = strlen(hexData);
867 HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
868 HksBlob shaMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
869 ASSERT_NE(shaMessage.data, nullptr);
870 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
871 int32_t ret = HksHash(paramInSet, &message, &shaMessage);
872 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
873 EXPECT_EQ(ret, HKS_SUCCESS);
874 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
875
876 HksFree(shaMessage.data);
877 }
878 HksFreeParamSet(¶mInSet);
879 HKS_LOG_I("HksHash Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
880 }
881
882 /**
883 * @tc.number : PressureTest.PressureTest01900
884 * @tc.name : PressureTest01900
885 * @tc.desc : HksGenerateKey
886 */
887 HWTEST_F(PressureTest, PressureTest01900, TestSize.Level1)
888 {
889 double programTimes = 0;
890 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
891
892 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
893 struct HksParamSet *paramInSet = nullptr;
894 HksInitParamSet(¶mInSet);
895
896 struct HksParam tmpParams[] = {
897 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
898 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
899 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
900 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_VERIFY },
901 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
902 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
903 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
904 };
905
906 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
907 HksBuildParamSet(¶mInSet);
908
909 struct HksParamSet *paramOutSet = nullptr;
910 HksInitParamSet(¶mOutSet);
911 struct HksParam localKey = { .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
912 .blob = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) } };
913 ASSERT_NE(localKey.blob.data, nullptr);
914 HksAddParams(paramOutSet, &localKey, 1);
915
916 HksBuildParamSet(¶mOutSet);
917 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
918 int32_t ret = HksGenerateKey(&authId, paramInSet, paramOutSet);
919 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
920 EXPECT_EQ(ret, HKS_SUCCESS);
921 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
922
923 HksFree(localKey.blob.data);
924 HksFreeParamSet(¶mInSet);
925 HksFreeParamSet(¶mOutSet);
926 }
927 HKS_LOG_I("Local HksGenerateKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
928 }
929
930 /**
931 * @tc.number : PressureTest.PressureTest02000
932 * @tc.name : PressureTest02000
933 * @tc.desc : HksSign
934 */
935 HWTEST_F(PressureTest, PressureTest02000, TestSize.Level1)
936 {
937 double programTimes = 0;
938 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
939
940 struct HksParamSet *paramInSet = nullptr;
941 HksInitParamSet(¶mInSet);
942
943 struct HksParam tmpParams[] = {
944 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
945 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
946 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
947 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
948 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
949 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
950 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
951 };
952
953 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
954 HksBuildParamSet(¶mInSet);
955
956 HksBlob priKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
957 ASSERT_NE(priKey.data, nullptr);
958 HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
959 ASSERT_NE(pubKey.data, nullptr);
960
961 PressureTest::LocalHksGenerate(HKS_ECC_KEY_SIZE_224, &authId, paramInSet, &priKey, &pubKey);
962
963 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
964 const char *hexData = "0123456789abcdef";
965 uint32_t dataLen = strlen(hexData);
966 HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
967 HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
968 ASSERT_NE(signature.data, nullptr);
969
970 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
971 int32_t ret = HksSign(&priKey, paramInSet, &message, &signature);
972 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
973 EXPECT_EQ(ret, HKS_SUCCESS);
974 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
975
976 HksFree(signature.data);
977 }
978 HksFree(priKey.data);
979 HksFree(pubKey.data);
980 HksFreeParamSet(¶mInSet);
981 HKS_LOG_I("Local HksSign Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
982 }
983
984 /**
985 * @tc.number : PressureTest.PressureTest02100
986 * @tc.name : PressureTest02100
987 * @tc.desc : HksVerify
988 */
989 HWTEST_F(PressureTest, PressureTest02100, TestSize.Level1)
990 {
991 double programTimes = 0;
992 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
993
994 struct HksParamSet *paramInSet = nullptr;
995 HksInitParamSet(¶mInSet);
996
997 struct HksParam tmpParams[] = {
998 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
999 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC },
1000 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1001 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY },
1002 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE },
1003 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
1004 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1005 };
1006
1007 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1008 HksBuildParamSet(¶mInSet);
1009
1010 HksBlob priKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1011 ASSERT_NE(priKey.data, nullptr);
1012 HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1013 ASSERT_NE(pubKey.data, nullptr);
1014
1015 PressureTest::LocalHksGenerate(HKS_ECC_KEY_SIZE_224, &authId, paramInSet, &priKey, &pubKey);
1016
1017 const char *hexData = "0123456789abcdef";
1018 uint32_t dataLen = strlen(hexData);
1019 HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1020 HksBlob signature = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1021 ASSERT_NE(signature.data, nullptr);
1022
1023 HksSign(&priKey, paramInSet, &message, &signature);
1024
1025 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
1026
1027 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1028 int32_t ret = HksVerify(&pubKey, paramInSet, &message, &signature);
1029 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1030 EXPECT_EQ(ret, HKS_SUCCESS);
1031 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
1032 }
1033 HksFree(priKey.data);
1034 HksFree(pubKey.data);
1035 HksFree(signature.data);
1036 HksFreeParamSet(¶mInSet);
1037 HKS_LOG_I("Local HksVerify Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
1038 }
1039
1040 /**
1041 * @tc.number : PressureTest.PressureTest02200
1042 * @tc.name : PressureTest02200
1043 * @tc.desc : HksEncrypt
1044 */
1045 HWTEST_F(PressureTest, PressureTest02200, TestSize.Level1)
1046 {
1047 double programTimes = 0;
1048 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1049
1050 struct HksParamSet *paramInSet = nullptr;
1051 HksInitParamSet(¶mInSet);
1052
1053 uint8_t iv[IV_SIZE] = {0};
1054 struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1055 HksAddParams(paramInSet, &tagIv, 1);
1056
1057 HksAddParams(paramInSet, PARAMS_FOR_ENCRYPT, sizeof(PARAMS_FOR_ENCRYPT) / sizeof(PARAMS_FOR_ENCRYPT[0]));
1058 HksBuildParamSet(¶mInSet);
1059
1060 struct HksParamSet *paramOutSet = nullptr;
1061 HksInitParamSet(¶mOutSet);
1062 struct HksParam localKey = { .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
1063 .blob = { .size = HKS_AES_KEY_SIZE_128, .data = (uint8_t *)HksMalloc(HKS_AES_KEY_SIZE_128) } };
1064 ASSERT_NE(localKey.blob.data, nullptr);
1065 HksAddParams(paramOutSet, &localKey, 1);
1066 HksBuildParamSet(¶mOutSet);
1067
1068 HksGenerateKey(&authId, paramInSet, paramOutSet);
1069
1070 HksParam *paramOut = nullptr;
1071 HksGetParam(paramOutSet, HKS_TAG_SYMMETRIC_KEY_DATA, ¶mOut);
1072 HksBlob authKey = { .size = paramOut->blob.size, .data = (uint8_t *)HksMalloc(paramOut->blob.size) };
1073 ASSERT_NE(authKey.data, nullptr);
1074 (void)memcpy_s(authKey.data, paramOut->blob.size, paramOut->blob.data, paramOut->blob.size);
1075
1076 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
1077 const char *hexData = "0123456789abcdef";
1078 uint32_t dataLen = strlen(hexData);
1079 HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
1080
1081 uint32_t inLen = dataLen + COMPLEMENT_LEN;
1082 HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
1083 ASSERT_NE(cipherText.data, nullptr);
1084
1085 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1086 int32_t ret = HksEncrypt(&authKey, paramInSet, &plainText, &cipherText);
1087 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1088 EXPECT_EQ(ret, HKS_SUCCESS);
1089 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
1090
1091 HksFree(cipherText.data);
1092 }
1093 HksFree(localKey.blob.data);
1094 HksFree(authKey.data);
1095 HksFreeParamSet(¶mInSet);
1096 HksFreeParamSet(¶mOutSet);
1097 HKS_LOG_I("Local HksEncrypt Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
1098 }
1099
1100 /**
1101 * @tc.number : PressureTest.PressureTest02300
1102 * @tc.name : PressureTest02300
1103 * @tc.desc : HksDecrypt
1104 */
1105 HWTEST_F(PressureTest, PressureTest02300, TestSize.Level1)
1106 {
1107 double programTimes = 0;
1108 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1109
1110 struct HksParamSet *paramInSet = nullptr;
1111 HksInitParamSet(¶mInSet);
1112
1113 uint8_t iv[IV_SIZE] = {0};
1114 struct HksParam tagIv = { .tag = HKS_TAG_IV, .blob = { .size = IV_SIZE, .data = iv } };
1115 HksAddParams(paramInSet, &tagIv, 1);
1116
1117 HksAddParams(paramInSet, PARAMS_FOR_DECRYPT, sizeof(PARAMS_FOR_DECRYPT) / sizeof(PARAMS_FOR_DECRYPT[0]));
1118 HksBuildParamSet(¶mInSet);
1119
1120 struct HksParamSet *paramOutSet = nullptr;
1121 HksInitParamSet(¶mOutSet);
1122 struct HksParam localKey = { .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
1123 .blob = { .size = HKS_AES_KEY_SIZE_128, .data = (uint8_t *)HksMalloc(HKS_AES_KEY_SIZE_128) } };
1124 ASSERT_NE(localKey.blob.data, nullptr);
1125 HksAddParams(paramOutSet, &localKey, 1);
1126 HksBuildParamSet(¶mOutSet);
1127
1128 HksGenerateKey(&authId, paramInSet, paramOutSet);
1129
1130 HksParam *paramOut = nullptr;
1131 HksGetParam(paramOutSet, HKS_TAG_SYMMETRIC_KEY_DATA, ¶mOut);
1132 HksBlob authKey = { .size = paramOut->blob.size, .data = (uint8_t *)HksMalloc(paramOut->blob.size) };
1133 ASSERT_NE(authKey.data, nullptr);
1134 (void)memcpy_s(authKey.data, paramOut->blob.size, paramOut->blob.data, paramOut->blob.size);
1135
1136 const char *hexData = "0123456789abcdef";
1137 uint32_t dataLen = strlen(hexData);
1138 HksBlob plainText = { .size = dataLen, .data = (uint8_t *)hexData };
1139
1140 uint32_t inLen = dataLen + COMPLEMENT_LEN;
1141 HksBlob cipherText = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
1142 ASSERT_NE(cipherText.data, nullptr);
1143
1144 HksEncrypt(&authKey, paramInSet, &plainText, &cipherText);
1145
1146 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
1147 HksBlob plainTextDecrypt = { .size = inLen, .data = (uint8_t *)HksMalloc(inLen) };
1148 ASSERT_NE(plainTextDecrypt.data, nullptr);
1149 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1150 int32_t ret = HksDecrypt(&authKey, paramInSet, &cipherText, &plainTextDecrypt);
1151 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1152 EXPECT_EQ(ret, HKS_SUCCESS);
1153 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
1154
1155 HksFree(plainTextDecrypt.data);
1156 }
1157 HksFree(localKey.blob.data);
1158 HksFree(authKey.data);
1159 HksFree(cipherText.data);
1160 HksFreeParamSet(¶mInSet);
1161 HksFreeParamSet(¶mOutSet);
1162 HKS_LOG_I("Local HksDecrypt Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
1163 }
1164
1165 /**
1166 * @tc.number : PressureTest.PressureTest02400
1167 * @tc.name : PressureTest02400
1168 * @tc.desc : HksAgreeKey
1169 */
1170 HWTEST_F(PressureTest, PressureTest02400, TestSize.Level1)
1171 {
1172 double programTimes = 0;
1173 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1174
1175 struct HksParamSet *paramInSetForKey = nullptr;
1176 HksInitParamSet(¶mInSetForKey);
1177 struct HksParamSet *paramInSet = nullptr;
1178 HksInitParamSet(¶mInSet);
1179
1180 struct HksParam tmpParams[] = {
1181 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
1182 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_ECC_KEY_SIZE_224 },
1183 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
1184 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
1185 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1186 };
1187
1188 HksAddParams(paramInSetForKey, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1189 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1190
1191 HksParam algForKey = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECC };
1192 HksParam alg = { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_ECDH };
1193
1194 HksAddParams(paramInSetForKey, &algForKey, 1);
1195 HksAddParams(paramInSet, &alg, 1);
1196 HksParam degistForKey = { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_NONE };
1197 HksAddParams(paramInSetForKey, °istForKey, 1);
1198 HksBuildParamSet(¶mInSetForKey);
1199 HksBuildParamSet(¶mInSet);
1200
1201 HksBlob priKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1202 ASSERT_NE(priKey.data, nullptr);
1203 HksBlob pubKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1204 ASSERT_NE(pubKey.data, nullptr);
1205
1206 PressureTest::LocalHksGenerate(HKS_ECC_KEY_SIZE_224, &authId, paramInSetForKey, &priKey, &pubKey);
1207
1208 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
1209 HksBlob agreeKey = { .size = HKS_ECC_KEY_SIZE_224, .data = (uint8_t *)HksMalloc(HKS_ECC_KEY_SIZE_224) };
1210 ASSERT_NE(agreeKey.data, nullptr);
1211 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1212 int32_t ret = HksAgreeKey(paramInSet, &priKey, &pubKey, &agreeKey);
1213 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1214 EXPECT_EQ(ret, HKS_SUCCESS);
1215 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
1216
1217 HksFree(agreeKey.data);
1218 }
1219 HksFree(priKey.data);
1220 HksFree(pubKey.data);
1221 HksFreeParamSet(¶mInSetForKey);
1222 HksFreeParamSet(¶mInSet);
1223 HKS_LOG_I("Local HksAgreeKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
1224 }
1225
1226 /**
1227 * @tc.number : PressureTest.PressureTest02500
1228 * @tc.name : PressureTest02500
1229 * @tc.desc : HksDeriveKey
1230 */
1231 HWTEST_F(PressureTest, PressureTest02500, TestSize.Level1)
1232 {
1233 double programTimes = 0;
1234 struct HksBlob authId = { .size = TEST_KEY_SIZE, .data = (uint8_t *)HksMalloc(TEST_KEY_SIZE) };
1235 ASSERT_NE(authId.data, nullptr);
1236
1237 struct HksParamSet *paramInSet = nullptr;
1238 HksInitParamSet(¶mInSet);
1239
1240 struct HksParam tmpParams[] = {
1241 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
1242 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HKDF },
1243 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = DERIVED_KEY_SIZE },
1244 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_DERIVE },
1245 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA256 },
1246 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
1247 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1248 };
1249
1250 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1251 HksBuildParamSet(¶mInSet);
1252
1253 HksGenerateRandom(paramInSet, &authId);
1254 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
1255 HksBlob derivedKey = { .size = DERIVED_KEY_SIZE, .data = (uint8_t *)HksMalloc(DERIVED_KEY_SIZE) };
1256 ASSERT_NE(derivedKey.data, nullptr);
1257 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1258 int32_t ret = HksDeriveKey(paramInSet, &authId, &derivedKey);
1259 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1260 EXPECT_EQ(ret, HKS_SUCCESS);
1261 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
1262
1263 HksFree(derivedKey.data);
1264 }
1265 HksFree(authId.data);
1266 HksFreeParamSet(¶mInSet);
1267 HKS_LOG_I("Local HksDeriveKey Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
1268 }
1269
1270 /**
1271 * @tc.number : PressureTest.PressureTest02600
1272 * @tc.name : PressureTest02600
1273 * @tc.desc : HksMac
1274 */
1275 HWTEST_F(PressureTest, PressureTest02600, TestSize.Level1)
1276 {
1277 double programTimes = 0;
1278 struct HksBlob authId = { strlen(GENERATE_KEY), (uint8_t *)GENERATE_KEY };
1279
1280 struct HksParamSet *paramInSet = nullptr;
1281 HksInitParamSet(¶mInSet);
1282
1283 struct HksParam tmpParams[] = {
1284 { .tag = HKS_TAG_KEY_STORAGE_FLAG, .uint32Param = HKS_STORAGE_TEMP },
1285 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_HMAC },
1286 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = TEST_KEY_SIZE },
1287 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_MAC },
1288 { .tag = HKS_TAG_DIGEST, .uint32Param = HKS_DIGEST_SHA1 },
1289 { .tag = HKS_TAG_IS_KEY_ALIAS, .boolParam = false },
1290 { .tag = HKS_TAG_KEY_GENERATE_TYPE, .uint32Param = HKS_KEY_GENERATE_TYPE_DEFAULT },
1291 };
1292
1293 HksAddParams(paramInSet, tmpParams, sizeof(tmpParams) / sizeof(tmpParams[0]));
1294 HksBuildParamSet(¶mInSet);
1295
1296 struct HksParamSet *paramOutSet = nullptr;
1297 HksInitParamSet(¶mOutSet);
1298 struct HksParam localKey = { .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
1299 .blob = { .size = HKS_AES_KEY_SIZE_128, .data = (uint8_t *)HksMalloc(HKS_AES_KEY_SIZE_128) } };
1300 ASSERT_NE(localKey.blob.data, nullptr);
1301 HksAddParams(paramOutSet, &localKey, 1);
1302 HksBuildParamSet(¶mOutSet);
1303
1304 HksGenerateKey(&authId, paramInSet, paramOutSet);
1305
1306 HksParam *paramOut = nullptr;
1307 HksGetParam(paramOutSet, HKS_TAG_SYMMETRIC_KEY_DATA, ¶mOut);
1308 HksBlob authKey = { .size = paramOut->blob.size, .data = (uint8_t *)HksMalloc(paramOut->blob.size) };
1309 ASSERT_NE(authKey.data, nullptr);
1310 (void)memcpy_s(authKey.data, paramOut->blob.size, paramOut->blob.data, paramOut->blob.size);
1311
1312 for (uint32_t ii = 0; ii < TEST_FREQUENCY; ii++) {
1313 const char *hexData = "0123456789abcdef";
1314 uint32_t dataLen = strlen(hexData);
1315 HksBlob message = { .size = dataLen, .data = (uint8_t *)hexData };
1316 HksBlob macMessage = { .size = MESSAGE_SIZE, .data = (uint8_t *)HksMalloc(MESSAGE_SIZE) };
1317 ASSERT_NE(macMessage.data, nullptr);
1318 auto start = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1319 int32_t ret = HksMac(&authKey, paramInSet, &message, &macMessage);
1320 auto end = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
1321 EXPECT_EQ(ret, HKS_SUCCESS);
1322 programTimes += (end.time_since_epoch().count() - start.time_since_epoch().count());
1323
1324 HksFree(macMessage.data);
1325 }
1326 HksFree(localKey.blob.data);
1327 HksFree(authKey.data);
1328 HksFreeParamSet(¶mInSet);
1329 HksFreeParamSet(¶mOutSet);
1330 HKS_LOG_I("Local HksMac Interface Call Duration: %" LOG_PUBLIC "f", (programTimes / TEST_FREQUENCY));
1331 }
1332 } // namespace