1 /*
2 * Copyright (c) 2021 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 "hks_test_common.h"
17
18 #include <limits.h>
19 #include <stdbool.h>
20
21 #include "hks_api.h"
22 #include "hks_param.h"
23 #include "hks_test_log.h"
24 #include "hks_test_mem.h"
25
26 #include "securec.h"
27
28 #define HKS_TEST_1024 1024
29 #define HKS_TEST_COMMON_8 8
30 #define HKS_TEST_COMMON_128 128
31
TestFreeBlob(struct HksBlob ** key)32 void TestFreeBlob(struct HksBlob **key)
33 {
34 if (key == NULL || *key == NULL) {
35 return;
36 }
37 HksTestFree((*key)->data);
38 HksTestFree(*key);
39 }
40
TestConstuctBlob(struct HksBlob ** blob,bool blobExist,uint32_t blobSize,bool blobDataExist,uint32_t realBlobDataSize)41 int32_t TestConstuctBlob(struct HksBlob **blob, bool blobExist, uint32_t blobSize, bool blobDataExist,
42 uint32_t realBlobDataSize)
43 {
44 if (!blobExist) { // blob not exist
45 return 0; // do nothing, return null
46 }
47
48 *blob = (struct HksBlob *)HksTestMalloc(sizeof(struct HksBlob));
49 if (*blob == NULL) {
50 return HKS_ERROR_MALLOC_FAIL;
51 }
52 (void)memset_s(*blob, sizeof(struct HksBlob), 0, sizeof(struct HksBlob));
53
54 if (blobDataExist) {
55 (*blob)->size = realBlobDataSize;
56 (*blob)->data = (uint8_t *)HksTestMalloc(realBlobDataSize);
57 if ((*blob)->data == NULL) {
58 return HKS_ERROR_MALLOC_FAIL;
59 }
60 (void)memset_s((*blob)->data, realBlobDataSize, 0, realBlobDataSize);
61 uint32_t offset = 0;
62 for (uint32_t i = 0; i < (realBlobDataSize - 1) / HKS_TEST_1024; i++) {
63 struct HksBlob tmp;
64 tmp.size = HKS_TEST_1024;
65 tmp.data = (uint8_t *)HksTestMalloc(tmp.size);
66 HKS_TEST_ASSERT(tmp.data != NULL);
67 (void)memset_s(tmp.data, tmp.size, 0, tmp.size);
68
69 HKS_TEST_ASSERT(HksGenerateRandom(NULL, &tmp) == 0);
70
71 HKS_TEST_ASSERT(memcpy_s((*blob)->data + offset, realBlobDataSize - offset, tmp.data, tmp.size) == EOK);
72 offset += tmp.size;
73 HksTestFree(tmp.data);
74 }
75
76 uint32_t remainSize = realBlobDataSize - offset;
77 struct HksBlob tmp;
78 tmp.size = remainSize;
79 tmp.data = (uint8_t *)HksTestMalloc(tmp.size);
80 HKS_TEST_ASSERT(tmp.data != NULL);
81 (void)memset_s(tmp.data, tmp.size, 0, tmp.size);
82
83 HKS_TEST_ASSERT(HksGenerateRandom(NULL, &tmp) == 0);
84 HKS_TEST_ASSERT(memcpy_s((*blob)->data + offset, remainSize, tmp.data, tmp.size) == EOK);
85 offset += tmp.size;
86 HksTestFree(tmp.data);
87 } else {
88 (*blob)->data = NULL;
89 }
90
91 (*blob)->size = blobSize;
92 return 0;
93 }
94
TestGenerateKeyParamSetPre(struct GenerateKeyParamSetStructure * paramStruct,struct HksParamSet * paramSet)95 static int32_t TestGenerateKeyParamSetPre(struct GenerateKeyParamSetStructure *paramStruct,
96 struct HksParamSet *paramSet)
97 {
98 if (!paramStruct->paramSetExist) {
99 return 0; // do nothing, return null
100 }
101 int32_t ret = 1;
102
103 if (paramStruct->setAlg) {
104 struct HksParam algParam = {0};
105 algParam.tag = HKS_TAG_ALGORITHM;
106 algParam.uint32Param = paramStruct->alg;
107 ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1);
108 HKS_TEST_ASSERT(ret == 0);
109 }
110
111 if (paramStruct->setKeySize) {
112 struct HksParam keySizeParam = {0};
113 keySizeParam.tag = HKS_TAG_KEY_SIZE;
114 keySizeParam.uint32Param = paramStruct->keySize;
115 ret = HksAddParams(paramSet, (const struct HksParam *)&keySizeParam, 1);
116 HKS_TEST_ASSERT(ret == 0);
117 }
118
119 if (paramStruct->setPurpose) {
120 struct HksParam purposeParam = {0};
121 purposeParam.tag = HKS_TAG_PURPOSE;
122 purposeParam.uint32Param = paramStruct->purpose;
123 ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1);
124 HKS_TEST_ASSERT(ret == 0);
125 }
126
127 if (paramStruct->setDigest) {
128 struct HksParam digestParam = {0};
129 digestParam.tag = HKS_TAG_DIGEST;
130 digestParam.uint32Param = paramStruct->digest;
131 ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1);
132 HKS_TEST_LOG_I("HksAddParams ret = 0x%X", ret);
133 HKS_TEST_ASSERT(ret == 0);
134 }
135
136 if (paramStruct->setPadding) {
137 struct HksParam paddingParam = {0};
138 paddingParam.tag = HKS_TAG_PADDING;
139 paddingParam.uint32Param = paramStruct->padding;
140 ret = HksAddParams(paramSet, (const struct HksParam *)&paddingParam, 1);
141 HKS_TEST_ASSERT(ret == 0);
142 }
143 return ret;
144 }
145
TestGenerateKeyParamSetPost(struct GenerateKeyParamSetStructure * paramStruct,struct HksParamSet * paramSet)146 static int32_t TestGenerateKeyParamSetPost(struct GenerateKeyParamSetStructure *paramStruct,
147 struct HksParamSet *paramSet)
148 {
149 if (!paramStruct->paramSetExist) {
150 return 0; // do nothing, return null
151 }
152 int32_t ret;
153 if (paramStruct->setBlockMode) {
154 struct HksParam keyMode = {0};
155 keyMode.tag = HKS_TAG_BLOCK_MODE;
156 keyMode.uint32Param = paramStruct->mode;
157 ret = HksAddParams(paramSet, (const struct HksParam *)&keyMode, 1);
158 HKS_TEST_ASSERT(ret == 0);
159 }
160
161 if (paramStruct->setKeyStorageFlag) {
162 struct HksParam keyStorageFlagParam = {0};
163 keyStorageFlagParam.tag = HKS_TAG_KEY_STORAGE_FLAG;
164 keyStorageFlagParam.uint32Param = paramStruct->keyStorageFlag;
165 ret = HksAddParams(paramSet, (const struct HksParam *)&keyStorageFlagParam, 1);
166 HKS_TEST_ASSERT(ret == 0);
167 }
168
169 struct HksParam tmpParam = {0};
170 tmpParam.tag = HKS_TAG_KEY_ROLE;
171 tmpParam.uint32Param = 0xFFFFFFFF;
172 ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1);
173 HKS_TEST_ASSERT(ret == 0);
174
175 ret = HksBuildParamSet(¶mSet);
176 HKS_TEST_ASSERT(ret == 0);
177
178 *(paramStruct->outParamSet) = paramSet;
179 return ret;
180 }
181
TestConstructGenerateKeyParamSet(struct GenerateKeyParamSetStructure * paramStruct)182 int32_t TestConstructGenerateKeyParamSet(struct GenerateKeyParamSetStructure *paramStruct)
183 {
184 struct HksParamSet *paramSet = NULL;
185 int32_t ret = HksInitParamSet(¶mSet);
186 HKS_TEST_ASSERT(ret == 0);
187
188 ret = TestGenerateKeyParamSetPre(paramStruct, paramSet);
189 HKS_TEST_ASSERT(ret == 0);
190 ret = TestGenerateKeyParamSetPost(paramStruct, paramSet);
191 HKS_TEST_ASSERT(ret == 0);
192 return ret;
193 }
194
TestConstructGenerateKeyParamSetOut(struct HksParamSet ** outParamSet,bool paramSetExist,uint32_t paramSetSize)195 int32_t TestConstructGenerateKeyParamSetOut(
196 struct HksParamSet **outParamSet,
197 bool paramSetExist, uint32_t paramSetSize)
198 {
199 if (!paramSetExist) {
200 return 0; // do nothing, return null
201 }
202 struct HksParamSet *tempParamSet = (struct HksParamSet *)HksTestMalloc(paramSetSize);
203 if (tempParamSet == NULL) {
204 return -1; // -1 is error code
205 }
206 (void)memset_s(tempParamSet, paramSetSize, 0, paramSetSize);
207 tempParamSet->paramSetSize = paramSetSize;
208
209 *outParamSet = tempParamSet;
210 return 0;
211 }
212
TestConstructRsaCipherParamSet(struct TestRsaCipherParamSet * paramStruct)213 int32_t TestConstructRsaCipherParamSet(struct TestRsaCipherParamSet *paramStruct)
214 {
215 struct GenerateKeyParamSetStructure genParamStruct = {
216 paramStruct->outParamSet, paramStruct->paramSetExist,
217 paramStruct->setAlg, paramStruct->alg,
218 false, 0,
219 paramStruct->setPurpose, paramStruct->purpose,
220 paramStruct->setDigest, paramStruct->digest,
221 paramStruct->setPadding, paramStruct->padding,
222 false, 0,
223 false, 0
224 };
225 return TestConstructGenerateKeyParamSet(&genParamStruct);
226 }
227
TestAesCipherParamSetPre(struct AesCipherParamSetStructure * paramStruct,struct HksParamSet * paramSet)228 int32_t TestAesCipherParamSetPre(struct AesCipherParamSetStructure *paramStruct, struct HksParamSet *paramSet)
229 {
230 if (!paramStruct->paramSetExist) {
231 return 0; // do nothing, return null
232 }
233 int32_t ret = 1;
234 if (paramStruct->setAlg) {
235 struct HksParam algParam = {0};
236 algParam.tag = HKS_TAG_ALGORITHM;
237 algParam.uint32Param = paramStruct->alg;
238 ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1);
239 HKS_TEST_ASSERT(ret == 0);
240 }
241
242 if (paramStruct->setPurpose) {
243 struct HksParam purposeParam = {0};
244 purposeParam.tag = HKS_TAG_PURPOSE;
245 purposeParam.uint32Param = paramStruct->purpose;
246 ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1);
247 HKS_TEST_ASSERT(ret == 0);
248 }
249
250 if (paramStruct->setPadding) {
251 struct HksParam paddingParam = {0};
252 paddingParam.tag = HKS_TAG_PADDING;
253 paddingParam.uint32Param = paramStruct->padding;
254 ret = HksAddParams(paramSet, (const struct HksParam *)&paddingParam, 1);
255 HKS_TEST_ASSERT(ret == 0);
256 }
257
258 if (paramStruct->setBlockMode) {
259 struct HksParam keyMode = {0};
260 keyMode.tag = HKS_TAG_BLOCK_MODE;
261 keyMode.uint32Param = paramStruct->mode;
262 ret = HksAddParams(paramSet, (const struct HksParam *)&keyMode, 1);
263 HKS_TEST_ASSERT(ret == 0);
264 }
265
266 if (paramStruct->setIv) {
267 struct HksParam ivParam = {0};
268 ivParam.tag = HKS_TAG_IV;
269 ivParam.blob = *(paramStruct->ivBlob);
270 ret = HksAddParams(paramSet, (const struct HksParam *)&ivParam, 1);
271 HKS_TEST_ASSERT(ret == 0);
272 }
273 return ret;
274 }
275
TestAesCipherParamSetPost(struct AesCipherParamSetStructure * paramStruct,struct HksParamSet * paramSet)276 int32_t TestAesCipherParamSetPost(struct AesCipherParamSetStructure *paramStruct, struct HksParamSet *paramSet)
277 {
278 if (!paramStruct->paramSetExist) {
279 return 0; // do nothing, return null
280 }
281 int32_t ret;
282 if (paramStruct->setNonce) {
283 struct HksParam nonceParam = {0};
284 nonceParam.tag = HKS_TAG_NONCE;
285 nonceParam.blob = *(paramStruct->nonceBlob);
286 ret = HksAddParams(paramSet, (const struct HksParam *)&nonceParam, 1);
287 HKS_TEST_ASSERT(ret == 0);
288 }
289
290 if (paramStruct->setAad) {
291 struct HksParam aadParam = {0};
292 aadParam.tag = HKS_TAG_ASSOCIATED_DATA;
293 aadParam.blob = *(paramStruct->aadBlob);
294 ret = HksAddParams(paramSet, (const struct HksParam *)&aadParam, 1);
295 HKS_TEST_ASSERT(ret == 0);
296 }
297
298 if (paramStruct->setIsKeyAlias) {
299 struct HksParam isKeyAilasParam = {0};
300 isKeyAilasParam.tag = HKS_TAG_IS_KEY_ALIAS;
301 isKeyAilasParam.boolParam = paramStruct->isKeyAlias;
302 ret = HksAddParams(paramSet, (const struct HksParam *)&isKeyAilasParam, 1);
303 HKS_TEST_ASSERT(ret == 0);
304 }
305
306 struct HksParam tmpParam = {0};
307 tmpParam.tag = HKS_TAG_KEY_ROLE;
308 tmpParam.uint32Param = 0xFFFFFFFF;
309 ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1);
310 HKS_TEST_ASSERT(ret == 0);
311
312 ret = HksBuildParamSet(¶mSet);
313 HKS_TEST_ASSERT(ret == 0);
314
315 *(paramStruct->outParamSet) = paramSet;
316 return ret;
317 }
318
TestConstructAesCipherParamSet(struct AesCipherParamSetStructure * paramStruct)319 int32_t TestConstructAesCipherParamSet(struct AesCipherParamSetStructure *paramStruct)
320 {
321 struct HksParamSet *paramSet = NULL;
322 int32_t ret = HksInitParamSet(¶mSet);
323 HKS_TEST_ASSERT(ret == 0);
324
325 ret = TestAesCipherParamSetPre(paramStruct, paramSet);
326 HKS_TEST_ASSERT(ret == 0);
327 ret = TestAesCipherParamSetPost(paramStruct, paramSet);
328 HKS_TEST_ASSERT(ret == 0);
329 return ret;
330 }
331
TestConstructMacParamSet(struct TestMacParamSetStructure * paramStruct)332 int32_t TestConstructMacParamSet(struct TestMacParamSetStructure *paramStruct)
333 {
334 if (!paramStruct->paramSetExist) {
335 return 0; // do nothing, return null
336 }
337 struct HksParamSet *paramSet = NULL;
338 int32_t ret = HksInitParamSet(¶mSet);
339 HKS_TEST_ASSERT(ret == 0);
340
341 if (paramStruct->setPurpose) {
342 struct HksParam purposeParam = {0};
343 purposeParam.tag = HKS_TAG_PURPOSE;
344 purposeParam.uint32Param = paramStruct->purpose;
345 ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1);
346 HKS_TEST_ASSERT(ret == 0);
347 }
348
349 if (paramStruct->setDigest) {
350 struct HksParam digestParam = {0};
351 digestParam.tag = HKS_TAG_DIGEST;
352 digestParam.uint32Param = paramStruct->digest;
353 ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1);
354 HKS_TEST_ASSERT(ret == 0);
355 }
356
357 if (paramStruct->setKeyAlias) {
358 struct HksParam keyIsKeyAlias = {0};
359 keyIsKeyAlias.tag = HKS_TAG_IS_KEY_ALIAS;
360 keyIsKeyAlias.boolParam = paramStruct->isKeyAlias;
361 ret = HksAddParams(paramSet, (const struct HksParam *)&keyIsKeyAlias, 1);
362 HKS_TEST_ASSERT(ret == 0);
363 }
364
365 struct HksParam tmpParam = {0};
366 tmpParam.tag = HKS_TAG_KEY_ROLE;
367 tmpParam.uint32Param = 0xFFFFFFFF;
368 ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1);
369 HKS_TEST_ASSERT(ret == 0);
370
371 ret = HksBuildParamSet(¶mSet);
372 HKS_TEST_ASSERT(ret == 0);
373
374 *(paramStruct->outParamSet) = paramSet;
375 return ret;
376 }
377
TestConstructAgreeParamSet(struct TestAgreeParamSetStructure * paramStruct)378 int32_t TestConstructAgreeParamSet(struct TestAgreeParamSetStructure *paramStruct)
379 {
380 if (!paramStruct->paramSetExist) {
381 return 0; // do nothing, return null
382 }
383 struct HksParamSet *paramSet = NULL;
384 int32_t ret = HksInitParamSet(¶mSet);
385 HKS_TEST_ASSERT(ret == 0);
386
387 if (paramStruct->setAlg) {
388 struct HksParam algParam = {0};
389 algParam.tag = HKS_TAG_ALGORITHM;
390 algParam.uint32Param = paramStruct->alg;
391 ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1);
392 HKS_TEST_ASSERT(ret == 0);
393 }
394
395 if (paramStruct->setKeySize) {
396 struct HksParam keySizeParam = {0};
397 keySizeParam.tag = HKS_TAG_KEY_SIZE;
398 keySizeParam.uint32Param = paramStruct->keySize;
399 ret = HksAddParams(paramSet, (const struct HksParam *)&keySizeParam, 1);
400 HKS_TEST_ASSERT(ret == 0);
401 }
402
403 if (paramStruct->setIsKeyAlias) {
404 struct HksParam isKeyAliasParam = {0};
405 isKeyAliasParam.tag = HKS_TAG_IS_KEY_ALIAS;
406 isKeyAliasParam.uint32Param = paramStruct->isKeyAlias;
407 ret = HksAddParams(paramSet, (const struct HksParam *)&isKeyAliasParam, 1);
408 HKS_TEST_ASSERT(ret == 0);
409 }
410
411 struct HksParam tmpParam = {0};
412 tmpParam.tag = HKS_TAG_KEY_ROLE;
413 tmpParam.uint32Param = 0xFFFFFFFF;
414 ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1);
415 HKS_TEST_ASSERT(ret == 0);
416
417 ret = HksBuildParamSet(¶mSet);
418 HKS_TEST_ASSERT(ret == 0);
419
420 *(paramStruct->outParamSet) = paramSet;
421 return ret;
422 }
423
424
TestDeriveParamSetPre(struct TestDeriveParamSetStructure * paramStruct,struct HksParamSet * paramSet)425 int32_t TestDeriveParamSetPre(struct TestDeriveParamSetStructure *paramStruct, struct HksParamSet *paramSet)
426 {
427 if (!paramStruct->paramSetExist) {
428 return 0; // do nothing, return null
429 }
430 int32_t ret = 1;
431 if (paramStruct->setAlg) {
432 struct HksParam algParam = {0};
433 algParam.tag = HKS_TAG_ALGORITHM;
434 algParam.uint32Param = paramStruct->alg;
435 ret = HksAddParams(paramSet, (const struct HksParam *)&algParam, 1);
436 HKS_TEST_ASSERT(ret == 0);
437 }
438
439 if (paramStruct->setPurpose) {
440 struct HksParam purposeParam = {0};
441 purposeParam.tag = HKS_TAG_PURPOSE;
442 purposeParam.uint32Param = paramStruct->purpose;
443 ret = HksAddParams(paramSet, (const struct HksParam *)&purposeParam, 1);
444 HKS_TEST_ASSERT(ret == 0);
445 }
446
447 if (paramStruct->setDigest) {
448 struct HksParam digestParam = {0};
449 digestParam.tag = HKS_TAG_DIGEST;
450 digestParam.uint32Param = paramStruct->digest;
451 ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1);
452 HKS_TEST_ASSERT(ret == 0);
453 }
454
455 if (paramStruct->setIteration) {
456 struct HksParam iterationParam = {0};
457 iterationParam.tag = HKS_TAG_ITERATION;
458 iterationParam.uint32Param = paramStruct->iteration;
459 ret = HksAddParams(paramSet, (const struct HksParam *)&iterationParam, 1);
460 HKS_TEST_ASSERT(ret == 0);
461 }
462 return ret;
463 }
464
TestDeriveParamSetPost(struct TestDeriveParamSetStructure * paramStruct,struct HksParamSet * paramSet)465 int32_t TestDeriveParamSetPost(struct TestDeriveParamSetStructure *paramStruct, struct HksParamSet *paramSet)
466 {
467 if (!paramStruct->paramSetExist) {
468 return 0; // do nothing, return null
469 }
470 int32_t ret;
471 if (paramStruct->setSalt) {
472 struct HksParam saltParam = {0};
473 saltParam.tag = HKS_TAG_SALT;
474 saltParam.blob = *(paramStruct->saltBlob);
475 ret = HksAddParams(paramSet, (const struct HksParam *)&saltParam, 1);
476 HKS_TEST_ASSERT(ret == 0);
477 }
478
479 if (paramStruct->setInfo) {
480 struct HksParam infoParam = {0};
481 infoParam.tag = HKS_TAG_INFO;
482 infoParam.blob = *(paramStruct->infoBlob);
483 ret = HksAddParams(paramSet, (const struct HksParam *)&infoParam, 1);
484 HKS_TEST_ASSERT(ret == 0);
485 }
486
487 if (paramStruct->setIsKeyAlias) {
488 struct HksParam isKeyAliasParam = {0};
489 isKeyAliasParam.tag = HKS_TAG_IS_KEY_ALIAS;
490 isKeyAliasParam.boolParam = paramStruct->isKeyAlias;
491 ret = HksAddParams(paramSet, (const struct HksParam *)&isKeyAliasParam, 1);
492 HKS_TEST_ASSERT(ret == 0);
493 }
494
495 struct HksParam tmpParam = {0};
496 tmpParam.tag = HKS_TAG_KEY_ROLE;
497 tmpParam.uint32Param = 0xFFFFFFFF;
498 ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1);
499 HKS_TEST_ASSERT(ret == 0);
500
501 ret = HksBuildParamSet(¶mSet);
502 HKS_TEST_ASSERT(ret == 0);
503
504 *(paramStruct->outParamSet) = paramSet;
505 return ret;
506 }
507
TestConstructDeriveParamSet(struct TestDeriveParamSetStructure * paramStruct)508 int32_t TestConstructDeriveParamSet(struct TestDeriveParamSetStructure *paramStruct)
509 {
510 struct HksParamSet *paramSet = NULL;
511 int32_t ret = HksInitParamSet(¶mSet);
512 HKS_TEST_ASSERT(ret == 0);
513
514 ret = TestDeriveParamSetPre(paramStruct, paramSet);
515 HKS_TEST_ASSERT(ret == 0);
516 ret = TestDeriveParamSetPost(paramStruct, paramSet);
517 HKS_TEST_ASSERT(ret == 0);
518 return ret;
519 }
520
TestConstructHashParamSet(struct HksParamSet ** outParamSet,bool paramSetExist,bool setDigest,uint32_t digest)521 int32_t TestConstructHashParamSet(
522 struct HksParamSet **outParamSet,
523 bool paramSetExist,
524 bool setDigest, uint32_t digest)
525 {
526 if (!paramSetExist) {
527 return 0; // do nothing, return null
528 }
529 struct HksParamSet *paramSet = NULL;
530 int32_t ret = HksInitParamSet(¶mSet);
531 HKS_TEST_ASSERT(ret == 0);
532
533 if (setDigest) {
534 struct HksParam digestParam = {0};
535 digestParam.tag = HKS_TAG_DIGEST;
536 digestParam.uint32Param = digest;
537 ret = HksAddParams(paramSet, (const struct HksParam *)&digestParam, 1);
538 HKS_TEST_ASSERT(ret == 0);
539 }
540
541 struct HksParam tmpParam = {0};
542 tmpParam.tag = HKS_TAG_KEY_ROLE;
543 tmpParam.uint32Param = 0xFFFFFFFF;
544 ret = HksAddParams(paramSet, (const struct HksParam *)&tmpParam, 1);
545 HKS_TEST_ASSERT(ret == 0);
546
547 ret = HksBuildParamSet(¶mSet);
548 HKS_TEST_ASSERT(ret == 0);
549
550 *outParamSet = paramSet;
551 return ret;
552 }
553
HuksGenerateKey(struct HksBlob ** keyAlias,const struct HksTestBlobParams * keyAliasParams,const struct HksTestGenKeyParamsParamSet * genKeyParamSetParams,const struct HksTestGenKeyParamsParamSetOut * genKeyParamSetParamsOut)554 int32_t HuksGenerateKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *keyAliasParams,
555 const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams,
556 const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut)
557 {
558 int32_t ret = TestConstuctBlob(keyAlias,
559 keyAliasParams->blobExist,
560 keyAliasParams->blobSize,
561 keyAliasParams->blobDataExist,
562 keyAliasParams->blobDataSize);
563 HKS_TEST_ASSERT(ret == 0);
564
565 struct HksParamSet *paramSet = NULL;
566 struct GenerateKeyParamSetStructure paramStruct = {
567 ¶mSet,
568 genKeyParamSetParams->paramSetExist,
569 genKeyParamSetParams->setAlg, genKeyParamSetParams->alg,
570 genKeyParamSetParams->setKeySize, genKeyParamSetParams->keySize,
571 genKeyParamSetParams->setPurpose, genKeyParamSetParams->purpose,
572 genKeyParamSetParams->setDigest, genKeyParamSetParams->digest,
573 genKeyParamSetParams->setPadding, genKeyParamSetParams->padding,
574 genKeyParamSetParams->setBlockMode, genKeyParamSetParams->mode,
575 genKeyParamSetParams->setKeyStorageFlag, genKeyParamSetParams->keyStorageFlag
576 };
577 ret = TestConstructGenerateKeyParamSet(¶mStruct);
578 HKS_TEST_ASSERT(ret == 0);
579
580 struct HksParamSet *paramSetOut = NULL;
581 if (genKeyParamSetParamsOut != NULL) {
582 ret = TestConstructGenerateKeyParamSetOut(¶mSet,
583 genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize);
584 HKS_TEST_ASSERT(ret == 0);
585 }
586
587 ret = HksGenerateKey(*keyAlias, paramSet, paramSetOut);
588 HKS_TEST_ASSERT(ret == 0);
589
590 HksFreeParamSet(¶mSet);
591 return ret;
592 }
593
GenerateLocalRandomKey(struct HksBlob ** keyAlias,const struct HksTestBlobParams * localKeyParams)594 int32_t GenerateLocalRandomKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *localKeyParams)
595 {
596 int32_t ret = TestConstuctBlob(keyAlias,
597 localKeyParams->blobExist,
598 localKeyParams->blobSize,
599 localKeyParams->blobDataExist,
600 localKeyParams->blobDataSize);
601 HKS_TEST_ASSERT(ret == 0);
602 return ret;
603 }
604
TestConstructBlobOut(struct HksBlob ** blob,bool blobExist,uint32_t blobSize,bool blobDataExist,uint32_t realBlobDataSize)605 int32_t TestConstructBlobOut(struct HksBlob **blob,
606 bool blobExist,
607 uint32_t blobSize,
608 bool blobDataExist,
609 uint32_t realBlobDataSize)
610 {
611 if (!blobExist) { // blob not exist
612 return 0; // do nothing, return null
613 }
614
615 *blob = (struct HksBlob *)HksTestMalloc(sizeof(struct HksBlob));
616 if (*blob == NULL) {
617 HKS_TEST_LOG_E("malloc fail");
618 return HKS_ERROR_MALLOC_FAIL;
619 }
620 (void)memset_s(*blob, sizeof(struct HksBlob), 0, sizeof(struct HksBlob));
621
622 if (blobDataExist) {
623 (*blob)->size = realBlobDataSize;
624 (*blob)->data = (uint8_t *)HksTestMalloc(realBlobDataSize);
625 if ((*blob)->data == NULL) {
626 HKS_TEST_LOG_E("malloc fail");
627 return HKS_ERROR_MALLOC_FAIL;
628 }
629 (void)memset_s((*blob)->data, realBlobDataSize, 0, realBlobDataSize);
630 } else {
631 (*blob)->data = NULL;
632 }
633
634 (*blob)->size = blobSize;
635 return 0;
636 }
637
GenerateLocalX25519Key(struct HksBlob ** privateKey,struct HksBlob ** publicKey,const struct HksTestBlobParams * localPrivateKeyParams,const struct HksTestBlobParams * localPublicKeyParams)638 int32_t GenerateLocalX25519Key(struct HksBlob **privateKey, struct HksBlob **publicKey,
639 const struct HksTestBlobParams *localPrivateKeyParams, const struct HksTestBlobParams *localPublicKeyParams)
640 {
641 int32_t ret;
642 if ((privateKey != NULL) && (localPrivateKeyParams != NULL)) {
643 ret = TestConstuctBlob(privateKey,
644 localPrivateKeyParams->blobExist,
645 localPrivateKeyParams->blobSize,
646 localPrivateKeyParams->blobDataExist,
647 localPrivateKeyParams->blobDataSize);
648 HKS_TEST_ASSERT(ret == 0);
649 }
650
651 if ((publicKey != NULL) && (localPublicKeyParams != NULL)) {
652 ret = TestConstuctBlob(publicKey,
653 localPublicKeyParams->blobExist,
654 localPublicKeyParams->blobSize,
655 localPublicKeyParams->blobDataExist,
656 localPublicKeyParams->blobDataSize);
657 HKS_TEST_ASSERT(ret == 0);
658 }
659
660 struct HksParamSet *paramSet = NULL;
661 struct GenerateKeyParamSetStructure paramStruct = {
662 ¶mSet, true, true, HKS_ALG_X25519,
663 true, HKS_CURVE25519_KEY_SIZE_256, true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, false, 0,
664 false, 0, false, 0, true, HKS_STORAGE_TEMP
665 };
666 ret = TestConstructGenerateKeyParamSet(¶mStruct);
667 HKS_TEST_ASSERT(ret == 0);
668
669 struct HksParamSet *paramSetOut = NULL;
670 ret = TestConstructGenerateKeyParamSetOut(¶mSetOut, true, HKS_TEST_COMMON_128);
671 HKS_TEST_ASSERT(ret == 0);
672
673 ret = HksGenerateKey(NULL, paramSet, paramSetOut);
674 HKS_TEST_ASSERT(ret == 0);
675
676 if ((publicKey != NULL) && ((*publicKey) != NULL) &&
677 (localPublicKeyParams->blobDataExist) &&
678 (localPublicKeyParams->blobDataSize == (HKS_CURVE25519_KEY_SIZE_256 / HKS_TEST_COMMON_8))) {
679 (void)memcpy_s((*publicKey)->data, (*publicKey)->size,
680 paramSetOut->params[0].blob.data, paramSetOut->params[0].blob.size);
681 }
682
683 if ((privateKey != NULL) && ((*privateKey) != NULL) &&
684 (localPrivateKeyParams->blobDataExist) &&
685 (localPrivateKeyParams->blobDataSize == (HKS_CURVE25519_KEY_SIZE_256 / HKS_TEST_COMMON_8))) {
686 (void)memcpy_s((*privateKey)->data, (*privateKey)->size,
687 paramSetOut->params[1].blob.data, paramSetOut->params[1].blob.size);
688 }
689
690 HksFreeParamSet(¶mSet);
691 HksFreeParamSet(¶mSetOut);
692 return ret;
693 }
694
TestGenDefaultKeyAndGetAlias(struct HksBlob ** keyAlias)695 int32_t TestGenDefaultKeyAndGetAlias(struct HksBlob **keyAlias)
696 {
697 struct HksTestGenKeyParams genKeyParam = {
698 0xffffffff, HKS_SUCCESS,
699 { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE }, {
700 true,
701 true, HKS_ALG_AES,
702 true, HKS_AES_KEY_SIZE_128,
703 true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT,
704 true, HKS_DIGEST_SHA256,
705 true, HKS_PADDING_NONE,
706 true, HKS_MODE_CBC },
707 { false, 0 }
708 };
709
710 int32_t ret = TestConstuctBlob(keyAlias,
711 genKeyParam.keyAliasParams.blobExist,
712 genKeyParam.keyAliasParams.blobSize,
713 genKeyParam.keyAliasParams.blobDataExist,
714 genKeyParam.keyAliasParams.blobDataSize);
715 HKS_TEST_ASSERT(ret == 0);
716
717 struct HksParamSet *paramSet = NULL;
718 struct GenerateKeyParamSetStructure paramStruct = {
719 ¶mSet, genKeyParam.paramSetParams.paramSetExist,
720 genKeyParam.paramSetParams.setAlg, genKeyParam.paramSetParams.alg,
721 genKeyParam.paramSetParams.setKeySize, genKeyParam.paramSetParams.keySize,
722 genKeyParam.paramSetParams.setPurpose, genKeyParam.paramSetParams.purpose,
723 genKeyParam.paramSetParams.setDigest, genKeyParam.paramSetParams.digest,
724 genKeyParam.paramSetParams.setPadding, genKeyParam.paramSetParams.padding,
725 genKeyParam.paramSetParams.setBlockMode, genKeyParam.paramSetParams.mode,
726 false, 0
727 };
728 ret = TestConstructGenerateKeyParamSet(¶mStruct);
729 HKS_TEST_ASSERT(ret == 0);
730
731 ret = HksGenerateKey(*keyAlias, paramSet, NULL);
732 HKS_TEST_ASSERT(ret == 0);
733
734 HksFreeParamSet(¶mSet);
735 return ret;
736 }
737