1 /*
2 * Copyright (c) 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 "hks_access_control_test_common.h"
17
18 #include <gtest/gtest.h>
19
20 using namespace testing::ext;
21 namespace Unittest::HksAccessControlPartTest {
22 static struct HksParam AuthToken_Import_Params[] = {
23 {
24 .tag = HKS_TAG_ALGORITHM,
25 .uint32Param = HKS_ALG_HMAC
26 }, {
27 .tag = HKS_TAG_PURPOSE,
28 .uint32Param = HKS_KEY_PURPOSE_MAC
29 }, {
30 .tag = HKS_TAG_KEY_SIZE,
31 .uint32Param = HKS_AES_KEY_SIZE_256
32 }, {
33 .tag = HKS_TAG_DIGEST,
34 .uint32Param = HKS_DIGEST_SHA256
35 }
36 };
37
38 static struct HksParam AuthToken_HMAC_Params[] = {
39 {
40 .tag = HKS_TAG_ALGORITHM,
41 .uint32Param = HKS_ALG_HMAC
42 }, {
43 .tag = HKS_TAG_PURPOSE,
44 .uint32Param = HKS_KEY_PURPOSE_MAC
45 }, {
46 .tag = HKS_TAG_DIGEST,
47 .uint32Param = HKS_DIGEST_SHA256
48 }
49 };
50
AuthTokenImportKey()51 int32_t AuthTokenImportKey()
52 {
53 struct HksParamSet *importParamSet = nullptr;
54 int32_t ret = InitParamSet(&importParamSet, AuthToken_Import_Params,
55 sizeof(AuthToken_Import_Params) / sizeof(HksParam));
56 if (ret != HKS_SUCCESS) {
57 return ret;
58 }
59 struct HksBlob key = {
60 SHA256_KEY_LEN,
61 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(HKS_DEFAULT_USER_AT_KEY))
62 };
63
64 uint8_t alias[] = "AuthToken_Sign_Verify_KeyAlias";
65 struct HksBlob keyAlias = { sizeof(alias), alias };
66 ret = HksImportKey(&keyAlias, importParamSet, &key);
67 HksFreeParamSet(&importParamSet);
68 return ret;
69 }
70
AssignAuthToken(HksUserAuthToken * authTokenHal,struct HksBlob * challenge,const IDMParams & testIDMParams)71 static int32_t AssignAuthToken(HksUserAuthToken *authTokenHal, struct HksBlob *challenge,
72 const IDMParams &testIDMParams)
73 {
74 uint64_t curTime = 0;
75 int32_t ret = HksCoreHalElapsedRealTime(&curTime);
76 if (ret != HKS_SUCCESS) {
77 HKS_LOG_E("get elapsed real time failed!");
78 return ret;
79 }
80 authTokenHal->time = curTime + testIDMParams.time;
81 authTokenHal->secureUid = testIDMParams.secureUid;
82 authTokenHal->enrolledId = testIDMParams.enrolledId;
83 authTokenHal->authType = testIDMParams.authType;
84
85 if (memcpy_s(authTokenHal->challenge, TOKEN_CHALLENGE_LEN, challenge->data, challenge->size) != EOK) {
86 HKS_LOG_E("memcpy_s failed");
87 return HKS_FAILURE;
88 }
89 return HKS_SUCCESS;
90 }
91
AuthTokenSign(struct HksBlob * challenge,const IDMParams & testIDMParams,std::vector<uint8_t> & token)92 int32_t AuthTokenSign(struct HksBlob *challenge, const IDMParams &testIDMParams, std::vector<uint8_t>& token)
93 {
94 int32_t ret = HKS_FAILURE;
95 uint8_t alias[] = "AuthToken_Sign_Verify_KeyAlias";
96 const struct HksBlob keyAlias = { sizeof(alias), alias };
97 AuthTokenImportKey();
98 HksUserAuthToken *authTokenHal = nullptr;
99 struct HksParamSet *hmacParamSet = nullptr;
100 do {
101 authTokenHal = static_cast<struct HksUserAuthToken *>(HksMalloc(AUTH_TOKEN_LEN));
102 if (authTokenHal == nullptr) {
103 break;
104 }
105
106 ret = AssignAuthToken(authTokenHal, challenge, testIDMParams);
107 if (ret != HKS_SUCCESS) {
108 break;
109 }
110
111 uint8_t authTokenData[AUTH_TOKEN_DATA_LEN] = {0};
112 if (memcpy_s(authTokenData, AUTH_TOKEN_DATA_LEN, authTokenHal, AUTH_TOKEN_DATA_LEN) != EOK) {
113 break;
114 }
115 struct HksBlob inData = { AUTH_TOKEN_DATA_LEN, authTokenData };
116
117 ret = InitParamSet(&hmacParamSet, AuthToken_HMAC_Params, sizeof(AuthToken_HMAC_Params) / sizeof(HksParam));
118 if (ret != HKS_SUCCESS) {
119 break;
120 }
121 /// Init
122 uint8_t handle[32] = {0};
123 struct HksBlob handleHMAC = { 32, handle };
124
125 ret = HksInit(&keyAlias, hmacParamSet, &handleHMAC, nullptr);
126 if (ret != HKS_SUCCESS) {
127 break;
128 }
129 // Update & Finish
130 struct HksBlob outData = { SHA256_SIGN_LEN, authTokenHal->sign };
131
132 ret = TestUpdateFinish(&handleHMAC, hmacParamSet, HKS_KEY_PURPOSE_MAC, &inData, &outData);
133 if (ret != HKS_SUCCESS) {
134 break;
135 }
136
137 for (uint32_t i = 0; i < AUTH_TOKEN_DATA_LEN; i++) {
138 token.push_back(authTokenData[i]);
139 }
140 for (uint32_t i = 0; i < SHA256_SIGN_LEN; i++) {
141 token.push_back(authTokenHal->sign[i]);
142 }
143 HksFreeParamSet(&hmacParamSet);
144 return ret;
145 } while (0);
146 (void)HksDeleteKey(&keyAlias, nullptr);
147 HKS_FREE_PTR(authTokenHal);
148 HksFreeParamSet(&hmacParamSet);
149 return ret;
150 }
151
AppendToNewParamSet(const struct HksParamSet * paramSet,struct HksParamSet ** outParamSet)152 static int32_t AppendToNewParamSet(const struct HksParamSet *paramSet, struct HksParamSet **outParamSet)
153 {
154 int32_t ret;
155 struct HksParamSet *newParamSet = nullptr;
156 HKS_LOG_E("AppendToNewParamSet start ");
157 do {
158 ret = HksCheckParamSet(paramSet, paramSet->paramSetSize);
159 if (ret != HKS_SUCCESS) {
160 HKS_LOG_E("check paramSet failed");
161 break;
162 }
163
164 ret = HksFreshParamSet(const_cast<struct HksParamSet *>(paramSet), false);
165 if (ret != HKS_SUCCESS) {
166 HKS_LOG_E("append fresh paramset failed");
167 break;
168 }
169
170 ret = HksInitParamSet(&newParamSet);
171 if (ret != HKS_SUCCESS) {
172 HKS_LOG_E("append init operation param set failed");
173 break;
174 }
175
176 ret = HksAddParams(newParamSet, paramSet->params, paramSet->paramsCnt);
177 if (ret != HKS_SUCCESS) {
178 HKS_LOG_E("append params failed");
179 break;
180 }
181 HKS_LOG_E("AppendToNewParamSet end ");
182
183 *outParamSet = newParamSet;
184 return ret;
185 } while (0);
186
187 HksFreeParamSet(&newParamSet);
188 return ret;
189 }
190
HksBuildAuthtoken(struct HksParamSet ** initParamSet,struct HksBlob * authChallenge,const IDMParams & testIDMParams)191 int32_t HksBuildAuthtoken(struct HksParamSet **initParamSet, struct HksBlob *authChallenge,
192 const IDMParams &testIDMParams)
193 {
194 struct HksParam tmpParams;
195 std::vector<uint8_t> token;
196 int ret = AuthTokenSign(authChallenge, testIDMParams, token);
197 if (ret != HKS_SUCCESS) {
198 HKS_LOG_E("AuthToeknSign Failed.");
199 return ret;
200 }
201
202 uint8_t authToken[AUTH_TOKEN_LEN] = {0};
203 for (uint32_t i = 0; i < AUTH_TOKEN_LEN; i++) {
204 authToken[i] = token[i];
205 }
206 tmpParams.tag = HKS_TAG_AUTH_TOKEN;
207 tmpParams.blob.data = authToken;
208 tmpParams.blob.size = AUTH_TOKEN_LEN;
209
210 struct HksParamSet *newParamSet = nullptr;
211
212 ret = AppendToNewParamSet(*initParamSet, &newParamSet);
213 if (ret != HKS_SUCCESS) {
214 HKS_LOG_I("AppendToNewParamSet failed!\n");
215 return ret;
216 }
217
218 ret = HksAddParams(newParamSet, &tmpParams, 1);
219 if (ret != 0) {
220 HKS_LOG_I("HksAddParam failed!\n");
221 return ret;
222 }
223 ret = HksBuildParamSet(&newParamSet);
224 if (ret != 0) {
225 HKS_LOG_I("HksBuildParamSet failed!\n");
226 return ret;
227 }
228
229 *initParamSet = newParamSet;
230 return ret;
231 }
232
HksBuildAuthTokenSecure(struct HksParamSet * paramSet,struct HksTestGenAuthTokenParams * genAuthTokenParams,struct HksParamSet ** outParamSet)233 int32_t HksBuildAuthTokenSecure(struct HksParamSet *paramSet,
234 struct HksTestGenAuthTokenParams *genAuthTokenParams, struct HksParamSet **outParamSet)
235 {
236 if (paramSet == nullptr || genAuthTokenParams == nullptr || outParamSet == nullptr) {
237 return HKS_ERROR_NULL_POINTER;
238 }
239 struct HksParam tmpParams;
240 std::vector<uint8_t> token;
241 struct IDMParams testIDMParams = {genAuthTokenParams->secureUid,
242 genAuthTokenParams->enrolledId, genAuthTokenParams->time, genAuthTokenParams->authType};
243 int32_t ret = AuthTokenSign(genAuthTokenParams->authChallenge, testIDMParams, token);
244 if (ret != HKS_SUCCESS) {
245 HKS_LOG_E("AuthToeknSign Failed.");
246 return ret;
247 }
248 uint8_t authToken[AUTH_TOKEN_LEN + 1] = {0};
249 for (uint32_t i = 0; i < AUTH_TOKEN_LEN; i++) {
250 authToken[i] = token[i];
251 }
252 tmpParams.tag = HKS_TAG_AUTH_TOKEN;
253 tmpParams.blob.data = authToken;
254 tmpParams.blob.size = AUTH_TOKEN_LEN;
255 HKS_LOG_I("AuthToekn Data: %" LOG_PUBLIC "s", authToken);
256
257 struct HksParamSet *newParamSet = nullptr;
258
259 ret = AppendToNewParamSet(paramSet, &newParamSet);
260 if (ret != HKS_SUCCESS) {
261 HKS_LOG_I("AppendToNewParamSet failed!\n");
262 return ret;
263 }
264
265 ret = HksAddParams(newParamSet, &tmpParams, 1);
266 if (ret != 0) {
267 HKS_LOG_I("HksAddParam failed!\n");
268 return ret;
269 }
270 ret = HksBuildParamSet(&newParamSet);
271 if (ret != 0) {
272 HKS_LOG_I("HksBuildParamSet failed!\n");
273 return ret;
274 }
275
276 *outParamSet = newParamSet;
277 return ret;
278 }
279
AddAuthtokenUpdateFinish(struct HksBlob * handle,struct HksParamSet * initParamSet,uint32_t posNum)280 int32_t AddAuthtokenUpdateFinish(struct HksBlob *handle,
281 struct HksParamSet *initParamSet, uint32_t posNum)
282 {
283 struct HksBlob inData = { g_inData.length(),
284 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str())) };
285 uint8_t outDataS[DATA_COMMON_SIZE] = {0};
286 struct HksBlob outDataSign = { DATA_COMMON_SIZE, outDataS };
287 (void)posNum;
288
289 int ret = TestUpdateLoopFinish(handle, initParamSet, &inData, &outDataSign);
290 if (ret != HKS_SUCCESS) {
291 HKS_LOG_I("TestUpdateLoopFinish failed, ret : %" LOG_PUBLIC "d", ret);
292 }
293 return ret;
294 }
295
CheckAccessCipherTest(const TestAccessCaseParams & testCaseParams,const IDMParams & testIDMParams)296 int32_t CheckAccessCipherTest(const TestAccessCaseParams &testCaseParams,
297 const IDMParams &testIDMParams)
298 {
299 struct HksParamSet *genParamSet = nullptr;
300
301 int32_t ret = InitParamSet(&genParamSet, testCaseParams.genParams.data(), testCaseParams.genParams.size());
302 if (ret != HKS_SUCCESS) {
303 HKS_LOG_I("InitParamSet(gen) failed, ret : %" LOG_PUBLIC "d", ret);
304 return ret;
305 }
306 uint8_t alias[] = "testCheckAuth";
307 struct HksBlob keyAlias = { sizeof(alias), alias };
308 ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
309 if (ret != HKS_SUCCESS) {
310 HksFreeParamSet(&genParamSet);
311 HKS_LOG_I("HksGenerateKey failed, ret : %" LOG_PUBLIC "d", ret);
312 return ret;
313 }
314
315 struct HksParamSet *initParamSet = nullptr;
316 ret = InitParamSet(&initParamSet, testCaseParams.initParams.data(), testCaseParams.initParams.size());
317 if (ret != HKS_SUCCESS) {
318 HKS_LOG_I("InitParamSet(init) failed, ret : %" LOG_PUBLIC "d", ret);
319 return ret;
320 }
321 uint8_t challenge[32] = {0};
322 struct HksBlob challengeBlob = { 32, challenge };
323
324 uint8_t tmpHandle[32] = {0};
325 struct HksBlob handle = { 32, tmpHandle };
326 ret = HksInit(&keyAlias, initParamSet, &handle, &challengeBlob);
327 if (ret != HKS_SUCCESS) {
328 return ret;
329 }
330
331 ret = HksBuildAuthtoken(&initParamSet, &challengeBlob, testIDMParams);
332 if (ret != HKS_SUCCESS) {
333 HKS_LOG_I("HksBuildAuthtoken failed, ret : %" LOG_PUBLIC "d", ret);
334 HksDeleteKey(&keyAlias, genParamSet);
335 return ret;
336 }
337 ret = AddAuthtokenUpdateFinish(&handle, initParamSet, 0);
338
339 HksFreeParamSet(&genParamSet);
340 HksFreeParamSet(&initParamSet);
341 (void)HksDeleteKey(&keyAlias, nullptr);
342
343 return (ret == testCaseParams.initResult) ? HKS_SUCCESS : HKS_FAILURE;
344 }
345
CheckAccessHmacTest(const TestAccessCaseParams & testCaseParams,const IDMParams & testIDMParams)346 int32_t CheckAccessHmacTest(const TestAccessCaseParams &testCaseParams,
347 const IDMParams &testIDMParams)
348 {
349 struct HksParamSet *genParamSet = nullptr;
350 int32_t ret = InitParamSet(&genParamSet, testCaseParams.genParams.data(), testCaseParams.genParams.size());
351 if (ret != HKS_SUCCESS) {
352 return ret;
353 }
354 uint8_t alias[] = "testCheckAuth";
355 struct HksBlob keyAlias = { sizeof(alias), alias };
356 ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
357 if (ret != HKS_SUCCESS) {
358 HksFreeParamSet(&genParamSet);
359 HKS_LOG_I("HksGenerateKey failed, ret : %" LOG_PUBLIC "d", ret);
360 return ret;
361 }
362
363 struct HksParamSet *initParamSet = nullptr;
364 ret = InitParamSet(&initParamSet, testCaseParams.initParams.data(), testCaseParams.initParams.size());
365 if (ret != HKS_SUCCESS) {
366 return ret;
367 }
368 uint8_t challenge[32] = {0};
369 struct HksBlob challengeBlob = { 32, challenge };
370
371 uint8_t handle[32] = {0};
372 struct HksBlob handleHMAC = { 32, handle };
373 ret = HksInit(&keyAlias, initParamSet, &handleHMAC, &challengeBlob);
374 if (ret != HKS_SUCCESS) {
375 HksDeleteKey(&keyAlias, genParamSet);
376 return ret;
377 }
378
379 ret = HksBuildAuthtoken(&initParamSet, &challengeBlob, testIDMParams);
380 if (ret != HKS_SUCCESS) {
381 HksDeleteKey(&keyAlias, genParamSet);
382 return ret;
383 }
384
385 struct HksBlob inData = { g_inData.length(),
386 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str())) };
387 uint8_t out[HMAC_COMMON_SIZE] = {0};
388 struct HksBlob outData = { HMAC_COMMON_SIZE, out };
389 ret = TestUpdateFinish(&handleHMAC, initParamSet, HKS_KEY_PURPOSE_MAC, &inData, &outData);
390 if (ret != HKS_SUCCESS) {
391 HKS_LOG_I("TestUpdateFinish failed, ret : %" LOG_PUBLIC "d", ret);
392 HksDeleteKey(&keyAlias, genParamSet);
393 return ret;
394 }
395
396 /* 3. Delete Key */
397 ret = HksDeleteKey(&keyAlias, genParamSet);
398 if (ret != HKS_SUCCESS) {
399 HksDeleteKey(&keyAlias, genParamSet);
400 return ret;
401 }
402
403 return (ret == testCaseParams.initResult) ? HKS_SUCCESS : HKS_FAILURE;
404 }
405
UpdateAndFinishForAgreeTest(const struct HksBlob * handle,struct HksParamSet * initParamSet,struct HksParamSet * finishParamSet,struct HksBlob * publicKey)406 static int32_t UpdateAndFinishForAgreeTest(const struct HksBlob *handle, struct HksParamSet *initParamSet,
407 struct HksParamSet *finishParamSet, struct HksBlob *publicKey)
408 {
409 struct HksBlob inData = { g_inData.length(),
410 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str())) };
411
412 uint8_t outDataU[ECDH_COMMON_SIZE] = {0};
413 struct HksBlob outDataUpdate = { ECDH_COMMON_SIZE, outDataU };
414 int32_t ret = HksUpdate(handle, initParamSet, publicKey, &outDataUpdate);
415 if (ret != HKS_SUCCESS) {
416 return ret;
417 }
418
419 uint8_t outDataF[ECDH_COMMON_SIZE] = {0};
420 struct HksBlob outDataFinish = { ECDH_COMMON_SIZE, outDataF };
421 ret = HksFinish(handle, finishParamSet, (const struct HksBlob *)&inData, &outDataFinish);
422 return ret;
423 }
424
CheckAccessAgreeTest(const TestAccessCaseParams & testCaseParams,struct HksParamSet * finishParamSet,const IDMParams & testIDMParams)425 int32_t CheckAccessAgreeTest(const TestAccessCaseParams &testCaseParams, struct HksParamSet *finishParamSet,
426 const IDMParams &testIDMParams)
427 {
428 struct HksParamSet *genParamSet = nullptr;
429 int32_t ret = InitParamSet(&genParamSet, testCaseParams.genParams.data(), testCaseParams.genParams.size());
430 if (ret != HKS_SUCCESS) {
431 return ret;
432 }
433 uint8_t alias[] = "testCheckAuth";
434 struct HksBlob keyAlias = { sizeof(alias), alias };
435 ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
436 if (ret != HKS_SUCCESS) {
437 HksFreeParamSet(&genParamSet);
438 return ret;
439 }
440
441 uint8_t pubKey[4096] = {0};
442 struct HksBlob publicKey = { 4096, pubKey };
443 ret = HksExportPublicKey(&keyAlias, genParamSet, &publicKey);
444 if (ret != HKS_SUCCESS) {
445 return HKS_FAILURE;
446 }
447 struct HksParamSet *initParamSet = nullptr;
448 ret = InitParamSet(&initParamSet, testCaseParams.initParams.data(), testCaseParams.initParams.size());
449 if (ret != HKS_SUCCESS) {
450 return ret;
451 }
452 uint8_t challenge[32] = {0};
453 struct HksBlob challengeBlob = { 32, challenge };
454
455 uint8_t handleU[32] = {0};
456 struct HksBlob handle = { 32, handleU };
457 ret = HksInit(&keyAlias, initParamSet, &handle, &challengeBlob);
458 if (ret != HKS_SUCCESS) {
459 return HKS_FAILURE;
460 }
461
462 ret = HksBuildAuthtoken(&initParamSet, &challengeBlob, testIDMParams);
463 if (ret != HKS_SUCCESS) {
464 HksDeleteKey(&keyAlias, genParamSet);
465 return ret;
466 }
467
468 ret = UpdateAndFinishForAgreeTest((const struct HksBlob *)&handle, initParamSet, finishParamSet, &publicKey);
469 if (ret != HKS_SUCCESS) {
470 return HKS_FAILURE;
471 }
472
473 return (ret == testCaseParams.initResult) ? HKS_SUCCESS : HKS_FAILURE;
474 }
475
UpdateAndFinishForDeriveTest(const struct HksBlob * handleDerive,struct HksParamSet * initParamSet,struct HksParamSet * finishParamSet)476 static int32_t UpdateAndFinishForDeriveTest(const struct HksBlob *handleDerive, struct HksParamSet *initParamSet,
477 struct HksParamSet *finishParamSet)
478 {
479 struct HksBlob inData = { g_inData.length(),
480 const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str())) };
481
482 uint8_t tmpOut[DERIVE_COMMON_SIZE] = {0};
483 struct HksBlob outData = { DERIVE_COMMON_SIZE, tmpOut };
484 int32_t ret = HksUpdate(handleDerive, initParamSet, &inData, &outData);
485 if (ret != HKS_SUCCESS) {
486 return ret;
487 }
488
489 uint8_t outDataD[DERIVE_COMMON_SIZE] = {0};
490 struct HksBlob outDataDerive = { DERIVE_COMMON_SIZE, outDataD };
491 ret = HksFinish(handleDerive, finishParamSet, &inData, &outDataDerive);
492 return ret;
493 }
494
495
CheckAccessDeriveTest(const TestAccessCaseParams & testCaseParams,struct HksParamSet * finishParamSet,const IDMParams & testIDMParams)496 int32_t CheckAccessDeriveTest(const TestAccessCaseParams &testCaseParams, struct HksParamSet *finishParamSet,
497 const IDMParams &testIDMParams)
498 {
499 struct HksParamSet *genParamSet = nullptr;
500 struct HksParamSet *initParamSet = nullptr;
501 int32_t ret = InitParamSet(&genParamSet, testCaseParams.genParams.data(), testCaseParams.genParams.size());
502 if (ret != HKS_SUCCESS) {
503 return ret;
504 }
505 uint8_t alias[] = "testCheckAuth";
506 struct HksBlob keyAlias = { sizeof(alias), alias };
507 ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
508 if (ret != HKS_SUCCESS) {
509 HksFreeParamSet(&genParamSet);
510 return ret;
511 }
512
513 ret = InitParamSet(&initParamSet, testCaseParams.initParams.data(), testCaseParams.initParams.size());
514 if (ret != HKS_SUCCESS) {
515 return ret;
516 }
517 uint8_t challenge[32] = {0};
518 struct HksBlob challengeBlob = { 32, challenge };
519 // Init
520 uint8_t handleD[32] = {0};
521 struct HksBlob handleDerive = { 32, handleD };
522 ret = HksInit(&keyAlias, initParamSet, &handleDerive, &challengeBlob);
523 if (ret != HKS_SUCCESS) {
524 HksDeleteKey(&keyAlias, genParamSet);
525 return ret;
526 }
527
528 ret = HksBuildAuthtoken(&initParamSet, &challengeBlob, testIDMParams);
529 if (ret != HKS_SUCCESS) {
530 HksDeleteKey(&keyAlias, genParamSet);
531 return ret;
532 }
533
534 ret = UpdateAndFinishForDeriveTest((const struct HksBlob *)&handleDerive, initParamSet, finishParamSet);
535 if (ret != HKS_SUCCESS) {
536 HksDeleteKey(&keyAlias, genParamSet);
537 return ret;
538 }
539
540 HksDeleteKey(&keyAlias, genParamSet);
541 return (ret == testCaseParams.initResult) ? HKS_SUCCESS : HKS_FAILURE;
542 }
543
ConstructRsaKeyPair(const struct HksBlob * nDataBlob,const struct HksBlob * dDataBlob,const struct HksBlob * eDataBlob,uint32_t keySize,struct HksBlob * outKey)544 int32_t ConstructRsaKeyPair(const struct HksBlob *nDataBlob, const struct HksBlob *dDataBlob,
545 const struct HksBlob *eDataBlob, uint32_t keySize, struct HksBlob *outKey)
546 {
547 struct HksKeyMaterialRsa material;
548 material.keyAlg = HKS_ALG_RSA;
549 material.keySize = keySize;
550 material.nSize = nDataBlob->size;
551 material.eSize = eDataBlob->size;
552 material.dSize = dDataBlob->size;
553
554 uint32_t size = sizeof(material) + material.nSize + material.eSize + material.dSize;
555 uint8_t *data = static_cast<uint8_t *>(HksMalloc(size));
556 if (data == nullptr) {
557 return HKS_ERROR_MALLOC_FAIL;
558 }
559
560 // copy struct material
561 if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
562 HksFree(data);
563 return HKS_ERROR_BAD_STATE;
564 }
565
566 uint32_t offset = sizeof(material);
567 // copy nData
568 if (memcpy_s(data + offset, size - offset, nDataBlob->data, nDataBlob->size) != EOK) {
569 HksFree(data);
570 return HKS_ERROR_BAD_STATE;
571 }
572 offset += material.nSize;
573
574 // copy eData
575 if (memcpy_s(data + offset, size - offset, eDataBlob->data, eDataBlob->size) != EOK) {
576 HksFree(data);
577 return HKS_ERROR_BAD_STATE;
578 }
579 offset += material.eSize;
580
581 // copy dData
582 if (memcpy_s(data + offset, size - offset, dDataBlob->data, dDataBlob->size) != EOK) {
583 HksFree(data);
584 return HKS_ERROR_BAD_STATE;
585 }
586
587 outKey->data = data;
588 outKey->size = size;
589 return HKS_SUCCESS;
590 }
591
ConstructEd25519KeyPair(uint32_t keySize,uint32_t alg,struct HksBlob * ed25519PubData,struct HksBlob * ed25519PrivData,struct HksBlob * outKey)592 int32_t ConstructEd25519KeyPair(uint32_t keySize, uint32_t alg, struct HksBlob *ed25519PubData,
593 struct HksBlob *ed25519PrivData, struct HksBlob *outKey)
594 {
595 struct HksKeyMaterial25519 material;
596 material.keyAlg = (enum HksKeyAlg)alg;
597 material.keySize = keySize;
598 material.pubKeySize = ed25519PubData->size;
599 material.priKeySize = ed25519PrivData->size;
600 material.reserved = 0;
601
602 uint32_t size = sizeof(material) + material.pubKeySize + material.priKeySize;
603 uint8_t *data = static_cast<uint8_t *>(HksMalloc(size));
604 if (data == nullptr) {
605 return HKS_ERROR_MALLOC_FAIL;
606 }
607
608 // copy struct material
609 if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
610 HksFree(data);
611 return HKS_ERROR_BAD_STATE;
612 }
613
614 uint32_t offset = sizeof(material);
615 // copy publicData
616 if (memcpy_s(data + offset, size - offset, ed25519PubData, ed25519PubData->size) != EOK) {
617 HksFree(data);
618 return HKS_ERROR_BAD_STATE;
619 }
620 offset += material.pubKeySize;
621
622 // copy privateData
623 if (memcpy_s(data + offset, size - offset, ed25519PrivData, ed25519PrivData->size) != EOK) {
624 HksFree(data);
625 return HKS_ERROR_BAD_STATE;
626 }
627
628 outKey->data = data;
629 outKey->size = size;
630 return HKS_SUCCESS;
631 }
632
633
ConstructDsaKeyPair(uint32_t keySize,const struct TestDsaKeyParams * params,struct HksBlob * outKey)634 int32_t ConstructDsaKeyPair(uint32_t keySize, const struct TestDsaKeyParams *params, struct HksBlob *outKey)
635 {
636 struct HksKeyMaterialDsa material;
637 material.keyAlg = HKS_ALG_DSA;
638 material.keySize = keySize;
639 material.xSize = params->xData->size;
640 material.ySize = params->yData->size;
641 material.pSize = params->pData->size;
642 material.qSize = params->qData->size;
643 material.gSize = params->gData->size;
644
645 uint32_t size = sizeof(material) + material.xSize + material.ySize +
646 material.pSize + material.qSize + material.gSize;
647 uint8_t *data = static_cast<uint8_t *>(HksMalloc(size));
648 if (data == nullptr) {
649 return HKS_ERROR_MALLOC_FAIL;
650 }
651
652 // copy struct material
653 if (memcpy_s(data, size, &material, sizeof(material)) != EOK) {
654 HksFree(data);
655 return HKS_ERROR_BAD_STATE;
656 }
657
658 uint32_t offset = sizeof(material);
659 // copy xData
660 if (memcpy_s(data + offset, size - offset, params->xData->data, params->xData->size) != EOK) {
661 HksFree(data);
662 return HKS_ERROR_BAD_STATE;
663 }
664
665 offset += material.xSize;
666 // copy yData
667 if (memcpy_s(data + offset, size - offset, params->yData->data, params->yData->size) != EOK) {
668 HksFree(data);
669 return HKS_ERROR_BAD_STATE;
670 }
671 offset += material.ySize;
672
673 // copy pData
674 if (memcpy_s(data + offset, size - offset, params->pData->data, params->pData->size) != EOK) {
675 HksFree(data);
676 return HKS_ERROR_BAD_STATE;
677 }
678 offset += material.pSize;
679
680 // copy qData
681 if (memcpy_s(data + offset, size - offset, params->qData->data, params->qData->size) != EOK) {
682 HksFree(data);
683 return HKS_ERROR_BAD_STATE;
684 }
685 offset += material.qSize;
686
687 // copy gData
688 if (memcpy_s(data + offset, size - offset, params->gData->data, params->gData->size) != EOK) {
689 HksFree(data);
690 return HKS_ERROR_BAD_STATE;
691 }
692
693 outKey->data = data;
694 outKey->size = size;
695 return HKS_SUCCESS;
696 }
697
GenParamSetAuthTest(struct HksParamSet ** paramOutSet,const struct HksParamSet * genParamSet)698 int32_t GenParamSetAuthTest(struct HksParamSet **paramOutSet, const struct HksParamSet *genParamSet)
699 {
700 struct HksParam localSecureKey = {
701 .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
702 .blob = { .size = KEY_PARAMSET_SIZE, .data = static_cast<uint8_t *>(HksMalloc(KEY_PARAMSET_SIZE)) }
703 };
704 if (localSecureKey.blob.data == nullptr) {
705 return HKS_ERROR_MALLOC_FAIL;
706 }
707
708 int32_t ret = HksInitParamSet(paramOutSet);
709 EXPECT_EQ(ret, HKS_SUCCESS) << "HksInitParamSet failed.";
710
711 struct HksParam *algParam = nullptr;
712 ret = HksGetParam(genParamSet, HKS_TAG_ALGORITHM, &algParam);
713 EXPECT_EQ(ret, HKS_SUCCESS) << "HksGetParam alg failed.";
714
715 if (algParam->uint32Param == HKS_ALG_AES || algParam->uint32Param == HKS_ALG_SM4) {
716 localSecureKey.tag = HKS_TAG_SYMMETRIC_KEY_DATA;
717 } else if (algParam->uint32Param == HKS_ALG_RSA || algParam->uint32Param == HKS_ALG_SM2 ||
718 algParam->uint32Param == HKS_ALG_ECC || algParam->uint32Param == HKS_ALG_DSA ||
719 algParam->uint32Param == HKS_ALG_X25519 || algParam->uint32Param == HKS_ALG_ED25519 ||
720 algParam->uint32Param == HKS_ALG_DH) {
721 localSecureKey.tag = HKS_TAG_ASYMMETRIC_PUBLIC_KEY_DATA;
722 } else if (algParam->uint32Param == HKS_ALG_HMAC || algParam->uint32Param == HKS_ALG_SM3 ||
723 algParam->uint32Param == HKS_ALG_HKDF || algParam->uint32Param == HKS_ALG_PBKDF2) {
724 localSecureKey.tag = HKS_TAG_ASYMMETRIC_PRIVATE_KEY_DATA;
725 } else {
726 localSecureKey.tag = HKS_TAG_SYMMETRIC_KEY_DATA;
727 }
728
729 ret = HksAddParams(*paramOutSet, &localSecureKey, 1);
730 EXPECT_EQ(ret, HKS_SUCCESS) << "HksAddParams failed.";
731 ret = HksBuildParamSet(paramOutSet);
732 EXPECT_EQ(ret, HKS_SUCCESS) << "HksAddParams failed.";
733
734 return ret;
735 }
736 }
737