1 /*
2 * Copyright (C) 2021-2025 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 "huks_adapter.h"
17 #include "huks_adapter_utils.h"
18
19 #include "huks_adapter_diff_impl.h"
20 #include "hc_log.h"
21 #include "mbedtls_ec_adapter.h"
22 #include "string_util.h"
23
24 #define ECDH_COMMON_SIZE_P256 512
25
26 static enum HksKeyAlg g_algToHksAlgorithm[] = {
27 HKS_ALG_ED25519,
28 HKS_ALG_X25519,
29 HKS_ALG_ECC,
30 HKS_ALG_AES,
31 };
32
Sha256(const Uint8Buff * message,Uint8Buff * hash)33 static int32_t Sha256(const Uint8Buff *message, Uint8Buff *hash)
34 {
35 CHECK_PTR_RETURN_HAL_ERROR_CODE(message, "message");
36 CHECK_PTR_RETURN_HAL_ERROR_CODE(message->val, "message->val");
37 CHECK_LEN_ZERO_RETURN_ERROR_CODE(message->length, "message->length");
38
39 CHECK_PTR_RETURN_HAL_ERROR_CODE(hash, "hash");
40 CHECK_PTR_RETURN_HAL_ERROR_CODE(hash->val, "hash->val");
41 CHECK_LEN_EQUAL_RETURN(hash->length, SHA256_LEN, "hash->length");
42
43 struct HksBlob srcBlob = { message->length, message->val };
44 struct HksBlob hashBlob = { hash->length, hash->val };
45 struct HksParamSet *paramSet = NULL;
46 struct HksParam digestParam[] = {
47 {
48 .tag = HKS_TAG_DIGEST,
49 .uint32Param = HKS_DIGEST_SHA256
50 }
51 };
52 int32_t res = ConstructParamSet(¶mSet, digestParam, CAL_ARRAY_SIZE(digestParam));
53 if (res != HAL_SUCCESS) {
54 LOGE("construct param set failed, res = %" LOG_PUB "d", res);
55 return res;
56 }
57
58 res = HksHash(paramSet, &srcBlob, &hashBlob);
59 if (res != HKS_SUCCESS || hashBlob.size != SHA256_LEN) {
60 LOGE("[HUKS]: HksHash fail. [Res]: %" LOG_PUB "d", res);
61 FreeParamSet(paramSet);
62 return HAL_ERR_HUKS;
63 }
64
65 FreeParamSet(paramSet);
66 return HAL_SUCCESS;
67 }
68
GenerateRandom(Uint8Buff * rand)69 static int32_t GenerateRandom(Uint8Buff *rand)
70 {
71 CHECK_PTR_RETURN_HAL_ERROR_CODE(rand, "rand");
72 CHECK_PTR_RETURN_HAL_ERROR_CODE(rand->val, "rand->val");
73 CHECK_LEN_ZERO_RETURN_ERROR_CODE(rand->length, "rand->length");
74
75 struct HksBlob randBlob = { rand->length, rand->val };
76 int32_t res = HksGenerateRandom(NULL, &randBlob);
77 if (res != HKS_SUCCESS) {
78 LOGE("[HUKS]: HksGenerateRandom fail. [Res]: %" LOG_PUB "d", res);
79 return HAL_FAILED;
80 }
81
82 return HAL_SUCCESS;
83 }
84
CheckKeyExist(const Uint8Buff * keyAlias,bool isDeStorage,int32_t osAccountId)85 static int32_t CheckKeyExist(const Uint8Buff *keyAlias, bool isDeStorage, int32_t osAccountId)
86 {
87 CHECK_PTR_RETURN_HAL_ERROR_CODE(keyAlias, "keyAlias");
88 CHECK_PTR_RETURN_HAL_ERROR_CODE(keyAlias->val, "keyAlias->val");
89 CHECK_LEN_ZERO_RETURN_ERROR_CODE(keyAlias->length, "keyAlias->length");
90
91 struct HksParamSet *deParamSet = NULL;
92 int32_t res = ConstructCheckParamSet(true, osAccountId, &deParamSet);
93 if (res != HAL_SUCCESS) {
94 return res;
95 }
96 struct HksParamSet *ceParamSet = NULL;
97 res = ConstructCheckParamSet(false, osAccountId, &ceParamSet);
98 if (res != HAL_SUCCESS) {
99 FreeParamSet(deParamSet);
100 return res;
101 }
102 struct HksBlob keyAliasBlob = { keyAlias->length, keyAlias->val };
103 if (isDeStorage) {
104 res = HksKeyExist(&keyAliasBlob, deParamSet);
105 if (res == HKS_SUCCESS) {
106 MoveDeKeyToCe(true, osAccountId, &keyAliasBlob);
107 } else {
108 res = HksKeyExist(&keyAliasBlob, ceParamSet);
109 }
110 } else {
111 res = HksKeyExist(&keyAliasBlob, ceParamSet);
112 if (res != HKS_SUCCESS) {
113 res = HksKeyExist(&keyAliasBlob, deParamSet);
114 }
115 }
116 FreeParamSet(deParamSet);
117 FreeParamSet(ceParamSet);
118
119 if (res == HKS_ERROR_NOT_EXIST) {
120 LOGE("[HUKS]: Key not exist. [Res]: %" LOG_PUB "d", res);
121 return HAL_ERR_KEY_NOT_EXIST;
122 }
123 if (res != HKS_SUCCESS) {
124 LOGE("[HUKS]: HksKeyExist fail. [Res]: %" LOG_PUB "d", res);
125 return HAL_ERR_HUKS;
126 }
127
128 return HAL_SUCCESS;
129 }
130
DeleteKey(const Uint8Buff * keyAlias,bool isDeStorage,int32_t osAccountId)131 static int32_t DeleteKey(const Uint8Buff *keyAlias, bool isDeStorage, int32_t osAccountId)
132 {
133 CHECK_PTR_RETURN_HAL_ERROR_CODE(keyAlias, "keyAlias");
134 CHECK_PTR_RETURN_HAL_ERROR_CODE(keyAlias->val, "keyAlias->val");
135 CHECK_LEN_ZERO_RETURN_ERROR_CODE(keyAlias->length, "keyAlias->length");
136
137 struct HksParamSet *deParamSet = NULL;
138 int32_t res = ConstructDeleteParamSet(true, osAccountId, &deParamSet);
139 if (res != HAL_SUCCESS) {
140 return res;
141 }
142 struct HksParamSet *ceParamSet = NULL;
143 res = ConstructDeleteParamSet(false, osAccountId, &ceParamSet);
144 if (res != HAL_SUCCESS) {
145 FreeParamSet(deParamSet);
146 return res;
147 }
148 struct HksBlob keyAliasBlob = { keyAlias->length, keyAlias->val };
149
150 LOGI("[HUKS]: HksDeleteKey enter.");
151 if (isDeStorage) {
152 res = HksDeleteKey(&keyAliasBlob, deParamSet);
153 if (res != HKS_SUCCESS) {
154 res = HksDeleteKey(&keyAliasBlob, ceParamSet);
155 }
156 } else {
157 res = HksDeleteKey(&keyAliasBlob, ceParamSet);
158 if (res != HKS_SUCCESS) {
159 res = HksDeleteKey(&keyAliasBlob, deParamSet);
160 }
161 }
162 LOGI("[HUKS]: HksDeleteKey quit. [Res]: %" LOG_PUB "d", res);
163
164 FreeParamSet(deParamSet);
165 FreeParamSet(ceParamSet);
166 if (res == HKS_ERROR_NOT_EXIST) {
167 LOGI("Key not exists.");
168 return HAL_SUCCESS;
169 }
170 if (res != HKS_SUCCESS) {
171 LOGE("[HUKS]: HksDeleteKey fail. [Res]: %" LOG_PUB "d", res);
172 return HAL_ERR_HUKS;
173 }
174 return HAL_SUCCESS;
175 }
176
ComputeHmac(const KeyParams * keyParams,const Uint8Buff * message,Uint8Buff * outHmac)177 static int32_t ComputeHmac(const KeyParams *keyParams, const Uint8Buff *message, Uint8Buff *outHmac)
178 {
179 int32_t res = CheckHmacParams(keyParams, message, outHmac);
180 if (res != HAL_SUCCESS) {
181 return res;
182 }
183
184 struct HksParamSet *deParamSet = NULL;
185 res = ConstructHmacParamSet(true, keyParams->osAccountId, keyParams->keyBuff.isAlias, &deParamSet);
186 if (res != HAL_SUCCESS) {
187 return res;
188 }
189 struct HksParamSet *ceParamSet = NULL;
190 res = ConstructHmacParamSet(false, keyParams->osAccountId, keyParams->keyBuff.isAlias, &ceParamSet);
191 if (res != HAL_SUCCESS) {
192 FreeParamSet(deParamSet);
193 return res;
194 }
195 struct HksBlob keyBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
196 struct HksBlob srcBlob = { message->length, message->val };
197 struct HksBlob hmacBlob = { outHmac->length, outHmac->val };
198
199 LOGI("[HUKS]: HksMac enter.");
200 if (keyParams->isDeStorage) {
201 res = HksMac(&keyBlob, deParamSet, &srcBlob, &hmacBlob);
202 } else {
203 res = HksMac(&keyBlob, ceParamSet, &srcBlob, &hmacBlob);
204 if (res != HKS_SUCCESS || hmacBlob.size != HMAC_LEN) {
205 res = HksMac(&keyBlob, deParamSet, &srcBlob, &hmacBlob);
206 }
207 }
208 LOGI("[HUKS]: HksMac quit. [Res]: %" LOG_PUB "d", res);
209 FreeParamSet(deParamSet);
210 FreeParamSet(ceParamSet);
211 if (res != HKS_SUCCESS || hmacBlob.size != HMAC_LEN) {
212 LOGE("[HUKS]: HksMac fail. [Res]: %" LOG_PUB "d", res);
213 return HAL_FAILED;
214 }
215 return HAL_SUCCESS;
216 }
217
DoAbortHks(struct HksBlob * handleDerive,struct HksParamSet * deriveParamSet)218 static void DoAbortHks(struct HksBlob *handleDerive, struct HksParamSet *deriveParamSet)
219 {
220 int32_t res = HksAbort(handleDerive, deriveParamSet);
221 if (res != HKS_SUCCESS) {
222 LOGE("Failed to abort huks, res:%" LOG_PUB "d", res);
223 }
224 }
225
ComputeHmacWithThreeStageInner(const KeyParams * keyParams,const Uint8Buff * message,Uint8Buff * outHmac)226 static int32_t ComputeHmacWithThreeStageInner(const KeyParams *keyParams, const Uint8Buff *message, Uint8Buff *outHmac)
227 {
228 struct HksParamSet *deriveParamSet = NULL;
229 int32_t res = ConstructDeriveParamSet(keyParams, message, &deriveParamSet);
230 if (res != HAL_SUCCESS) {
231 return res;
232 }
233 struct HksParamSet *finishParamSet = NULL;
234 res = ConstructFinishParamSet(keyParams, &finishParamSet);
235 if (res != HAL_SUCCESS) {
236 FreeParamSet(deriveParamSet);
237 return res;
238 }
239 uint8_t handle[sizeof(uint64_t)] = { 0 };
240 struct HksBlob handleDerive = { sizeof(uint64_t), handle };
241 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
242 uint8_t tmpOut[2048] = { 0 };
243 struct HksBlob outData = { 2048, tmpOut };
244 struct HksBlob inData = { 0, NULL };
245 struct HksBlob hmacBlob = { outHmac->length, outHmac->val };
246 do {
247 res = HksInit(&keyAliasBlob, deriveParamSet, &handleDerive, NULL);
248 if (res != HKS_SUCCESS) {
249 LOGE("Failed to init derive params!");
250 res = HAL_FAILED;
251 break;
252 }
253 res = HksUpdate(&handleDerive, deriveParamSet, &inData, &outData);
254 if (res != HKS_SUCCESS) {
255 LOGE("Failed to update derive params!");
256 DoAbortHks(&handleDerive, deriveParamSet);
257 res = HAL_FAILED;
258 break;
259 }
260 res = HksFinish(&handleDerive, finishParamSet, &inData, &hmacBlob);
261 if (res != HKS_SUCCESS || hmacBlob.size != HMAC_LEN) {
262 LOGE("Compute hmac with three stage failed! [Res]: %" LOG_PUB "d, [size]: %" LOG_PUB "d",
263 res, hmacBlob.size);
264 DoAbortHks(&handleDerive, finishParamSet);
265 res = HAL_FAILED;
266 break;
267 }
268 } while (0);
269 FreeParamSet(deriveParamSet);
270 FreeParamSet(finishParamSet);
271 return res;
272 }
273
ComputeHmacWithThreeStage(const KeyParams * keyParams,const Uint8Buff * message,Uint8Buff * outHmac)274 static int32_t ComputeHmacWithThreeStage(const KeyParams *keyParams, const Uint8Buff *message, Uint8Buff *outHmac)
275 {
276 int32_t res = CheckHmacWithThreeStageParams(keyParams, message, outHmac);
277 if (res != HAL_SUCCESS) {
278 return res;
279 }
280
281 res = ComputeHmacWithThreeStageInner(keyParams, message, outHmac);
282 if (!keyParams->isDeStorage) {
283 return res;
284 }
285 if (res == HAL_SUCCESS) {
286 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
287 MoveDeKeyToCe(keyParams->keyBuff.isAlias, keyParams->osAccountId, &keyAliasBlob);
288 } else {
289 KeyParams ceParams = {
290 .keyBuff = { keyParams->keyBuff.key, keyParams->keyBuff.keyLen, keyParams->keyBuff.isAlias },
291 .isDeStorage = false,
292 .osAccountId = keyParams->osAccountId
293 };
294 res = ComputeHmacWithThreeStageInner(&ceParams, message, outHmac);
295 }
296 return res;
297 }
298
ComputeHkdf(const KeyParams * keyParams,const Uint8Buff * salt,const Uint8Buff * keyInfo,Uint8Buff * outHkdf)299 static int32_t ComputeHkdf(const KeyParams *keyParams, const Uint8Buff *salt, const Uint8Buff *keyInfo,
300 Uint8Buff *outHkdf)
301 {
302 int32_t res = CheckHkdfParams(keyParams, salt, outHkdf);
303 if (res != HAL_SUCCESS) {
304 return res;
305 }
306
307 struct HksBlob srcKeyBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
308 struct HksBlob derivedKeyBlob = { outHkdf->length, outHkdf->val };
309
310 struct HksParamSet *deParamSet = NULL;
311 res = ConstructHkdfParamSet(true, keyParams, salt, keyInfo, &deParamSet);
312 if (res != HAL_SUCCESS) {
313 return res;
314 }
315 struct HksParamSet *ceParamSet = NULL;
316 res = ConstructHkdfParamSet(false, keyParams, salt, keyInfo, &ceParamSet);
317 if (res != HAL_SUCCESS) {
318 FreeParamSet(deParamSet);
319 return res;
320 }
321
322 LOGI("[HUKS]: HksDeriveKey enter.");
323 if (keyParams->isDeStorage) {
324 res = HksDeriveKey(deParamSet, &srcKeyBlob, &derivedKeyBlob);
325 if (res == HKS_SUCCESS) {
326 MoveDeKeyToCe(keyParams->keyBuff.isAlias, keyParams->osAccountId, &srcKeyBlob);
327 } else {
328 res = HksDeriveKey(ceParamSet, &srcKeyBlob, &derivedKeyBlob);
329 }
330 } else {
331 res = HksDeriveKey(ceParamSet, &srcKeyBlob, &derivedKeyBlob);
332 if (res != HKS_SUCCESS) {
333 res = HksDeriveKey(deParamSet, &srcKeyBlob, &derivedKeyBlob);
334 }
335 }
336 LOGI("[HUKS]: HksDeriveKey quit. [Res]: %" LOG_PUB "d", res);
337 FreeParamSet(deParamSet);
338 FreeParamSet(ceParamSet);
339 if (res != HKS_SUCCESS) {
340 LOGE("[HUKS]: HksDeriveKey fail. [Res]: %" LOG_PUB "d", res);
341 return HAL_FAILED;
342 }
343 return HAL_SUCCESS;
344 }
345
ComputePseudonymPskInner(const KeyParams * keyParams,const Uint8Buff * pskKeyAlias,const Uint8Buff * extInfo,Uint8Buff * outPsk)346 static int32_t ComputePseudonymPskInner(const KeyParams *keyParams, const Uint8Buff *pskKeyAlias,
347 const Uint8Buff *extInfo, Uint8Buff *outPsk)
348 {
349 struct HksBlob srcKeyBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
350 struct HksBlob derivedKeyBlob = { outPsk->length, outPsk->val };
351 struct HksBlob extInfoBlob = { 0, NULL };
352 if (extInfo != NULL) {
353 extInfoBlob.data = extInfo->val;
354 extInfoBlob.size = extInfo->length;
355 }
356 struct HksParamSet *paramSet = NULL;
357 int32_t res = ConstructPseudonymParamSet(keyParams, pskKeyAlias, &extInfoBlob, outPsk->length, ¶mSet);
358 if (res != HAL_SUCCESS) {
359 LOGE("Construct param set failed!");
360 return res;
361 }
362
363 LOGI("[HUKS]: HksDeriveKey enter.");
364 res = HksDeriveKey(paramSet, &srcKeyBlob, &derivedKeyBlob);
365 FreeParamSet(paramSet);
366 LOGI("[HUKS]: HksDeriveKey quit. [Res]: %" LOG_PUB "d", res);
367 if (res != HKS_SUCCESS) {
368 LOGE("[HUKS]: HksDeriveKey fail. [Res]: %" LOG_PUB "d", res);
369 return HAL_FAILED;
370 }
371 return HAL_SUCCESS;
372 }
373
374 // pseudonym psk alias:sha256(serviceType bytes+peerAuthId bytes+{0x00, 0x07})
ComputePseudonymPsk(const KeyParams * keyParams,const Uint8Buff * pskKeyAlias,const Uint8Buff * extInfo,Uint8Buff * outPsk)375 static int32_t ComputePseudonymPsk(const KeyParams *keyParams, const Uint8Buff *pskKeyAlias,
376 const Uint8Buff *extInfo, Uint8Buff *outPsk)
377 {
378 int32_t res = CheckPskParams(keyParams, pskKeyAlias, outPsk);
379 if (res != HAL_SUCCESS) {
380 return res;
381 }
382
383 res = ComputePseudonymPskInner(keyParams, pskKeyAlias, extInfo, outPsk);
384 if (!keyParams->isDeStorage) {
385 return res;
386 }
387 if (res == HAL_SUCCESS) {
388 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
389 MoveDeKeyToCe(keyParams->keyBuff.isAlias, keyParams->osAccountId, &keyAliasBlob);
390 } else {
391 KeyParams ceParams = {
392 .keyBuff = { keyParams->keyBuff.key, keyParams->keyBuff.keyLen, keyParams->keyBuff.isAlias },
393 .isDeStorage = false,
394 .osAccountId = keyParams->osAccountId
395 };
396 res = ComputePseudonymPskInner(&ceParams, pskKeyAlias, extInfo, outPsk);
397 }
398 return res;
399 }
400
ConstructOutParamSet(struct HksParamSet ** outParamSet)401 static int32_t ConstructOutParamSet(struct HksParamSet **outParamSet)
402 {
403 int32_t res = HksInitParamSet(outParamSet);
404 if (res != HKS_SUCCESS) {
405 LOGE("init out param set failed, res = %" LOG_PUB "d", res);
406 return HAL_ERR_INIT_PARAM_SET_FAILED;
407 }
408
409 uint32_t outParamSetSize = 2048;
410 uint8_t *blobVal = (uint8_t *)HcMalloc(outParamSetSize, 0);
411 if (blobVal == NULL) {
412 LOGE("Failed to alloc memory for out blob value!");
413 HksFreeParamSet(outParamSet);
414 return HAL_ERR_BAD_ALLOC;
415 }
416 struct HksParam getParam = {
417 .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
418 .blob = { .size = outParamSetSize, .data = blobVal }
419 };
420
421 res = HksAddParams(*outParamSet, &getParam, 1);
422 if (res != HKS_SUCCESS) {
423 LOGE("Failed to add param!");
424 HksFreeParamSet(outParamSet);
425 HcFree(blobVal);
426 return HAL_ERR_ADD_PARAM_FAILED;
427 }
428
429 res = HksBuildParamSet(outParamSet);
430 HcFree(blobVal);
431 if (res != HKS_SUCCESS) {
432 LOGE("build param set failed, res = %" LOG_PUB "d", res);
433 HksFreeParamSet(outParamSet);
434 return HAL_ERR_BUILD_PARAM_SET_FAILED;
435 }
436 return HAL_SUCCESS;
437 }
438
GetKeyExtInfoInner(const KeyParams * keyParams,Uint8Buff * outExtInfo)439 static int32_t GetKeyExtInfoInner(const KeyParams *keyParams, Uint8Buff *outExtInfo)
440 {
441 struct HksParamSet *paramSet = NULL;
442 int32_t res = ConstructGetKeyExtInfoParamSet(keyParams, ¶mSet);
443 if (res != HAL_SUCCESS) {
444 return res;
445 }
446 struct HksParamSet *outParamSet = NULL;
447 res = ConstructOutParamSet(&outParamSet);
448 if (res != HAL_SUCCESS) {
449 FreeParamSet(paramSet);
450 return res;
451 }
452 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
453 res = HksGetKeyParamSet(&keyAliasBlob, paramSet, outParamSet);
454 FreeParamSet(paramSet);
455 if (res != HKS_SUCCESS) {
456 LOGE("Failed to get key param set!");
457 FreeParamSet(outParamSet);
458 return HAL_FAILED;
459 }
460 res = GetExtInfoByParamSet(outParamSet, outExtInfo);
461 FreeParamSet(outParamSet);
462 return res;
463 }
464
GetKeyExtInfo(const KeyParams * keyParams,Uint8Buff * outExtInfo)465 static int32_t GetKeyExtInfo(const KeyParams *keyParams, Uint8Buff *outExtInfo)
466 {
467 int32_t res = CheckKeyParams(keyParams);
468 if (res != HAL_SUCCESS) {
469 return res;
470 }
471 if (outExtInfo == NULL) {
472 LOGE("outExtInfo is null!");
473 return HAL_ERR_NULL_PTR;
474 }
475
476 res = GetKeyExtInfoInner(keyParams, outExtInfo);
477 if (!keyParams->isDeStorage) {
478 return res;
479 }
480 if (res == HAL_SUCCESS) {
481 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
482 MoveDeKeyToCe(keyParams->keyBuff.isAlias, keyParams->osAccountId, &keyAliasBlob);
483 } else {
484 KeyParams ceParams = {
485 .keyBuff = { keyParams->keyBuff.key, keyParams->keyBuff.keyLen, keyParams->keyBuff.isAlias },
486 .isDeStorage = false,
487 .osAccountId = keyParams->osAccountId
488 };
489 res = GetKeyExtInfoInner(&ceParams, outExtInfo);
490 }
491 return res;
492 }
493
AesGcmEncrypt(const KeyParams * keyParams,const Uint8Buff * plain,const GcmParam * encryptInfo,Uint8Buff * outCipher)494 static int32_t AesGcmEncrypt(const KeyParams *keyParams, const Uint8Buff *plain, const GcmParam *encryptInfo,
495 Uint8Buff *outCipher)
496 {
497 int32_t res = CheckAesGcmEncryptParam(keyParams, plain, encryptInfo, outCipher);
498 if (res != HAL_SUCCESS) {
499 return res;
500 }
501
502 struct HksBlob keyBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
503 struct HksBlob plainBlob = { plain->length, plain->val };
504 struct HksBlob cipherBlob = { outCipher->length, outCipher->val };
505
506 struct HksParamSet *paramSet = NULL;
507 res = ConstructAesGcmEncryptParamSet(encryptInfo, keyParams, ¶mSet);
508 if (res != HAL_SUCCESS) {
509 return res;
510 }
511
512 LOGI("[HUKS]: HksEncrypt enter.");
513 res = HksEncrypt(&keyBlob, paramSet, &plainBlob, &cipherBlob);
514 FreeParamSet(paramSet);
515 LOGI("[HUKS]: HksEncrypt quit. [Res]: %" LOG_PUB "d", res);
516 if (res != HKS_SUCCESS) {
517 LOGE("[HUKS]: HksEncrypt fail. [Res]: %" LOG_PUB "d", res);
518 return HAL_FAILED;
519 }
520 return HAL_SUCCESS;
521 }
522
AesGcmDecrypt(const KeyParams * keyParams,const Uint8Buff * cipher,const GcmParam * decryptInfo,Uint8Buff * outPlain)523 static int32_t AesGcmDecrypt(const KeyParams *keyParams, const Uint8Buff *cipher, const GcmParam *decryptInfo,
524 Uint8Buff *outPlain)
525 {
526 int32_t res = CheckAesGcmDecryptParam(keyParams, cipher, decryptInfo, outPlain);
527 if (res != HAL_SUCCESS) {
528 return res;
529 }
530
531 struct HksBlob keyBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
532 struct HksBlob cipherBlob = { cipher->length, cipher->val };
533 struct HksBlob plainBlob = { outPlain->length, outPlain->val };
534
535 struct HksParamSet *paramSet = NULL;
536 res = ConstructAesGcmDecryptParamSet(decryptInfo, keyParams, ¶mSet);
537 if (res != HAL_SUCCESS) {
538 return res;
539 }
540
541 LOGI("[HUKS]: HksDecrypt enter.");
542 res = HksDecrypt(&keyBlob, paramSet, &cipherBlob, &plainBlob);
543 FreeParamSet(paramSet);
544 LOGI("[HUKS]: HksDecrypt quit. [Res]: %" LOG_PUB "d", res);
545 if (res != HKS_SUCCESS) {
546 LOGE("[HUKS]: HksDecrypt fail. [Res]: %" LOG_PUB "d", res);
547 return HAL_FAILED;
548 }
549 return HAL_SUCCESS;
550 }
551
HashToPoint(const Uint8Buff * hash,Algorithm algo,Uint8Buff * outEcPoint)552 static int32_t HashToPoint(const Uint8Buff *hash, Algorithm algo, Uint8Buff *outEcPoint)
553 {
554 CHECK_PTR_RETURN_HAL_ERROR_CODE(hash, "hash");
555 CHECK_PTR_RETURN_HAL_ERROR_CODE(hash->val, "hash->val");
556 CHECK_LEN_EQUAL_RETURN(hash->length, SHA256_LEN, "hash->length");
557 CHECK_PTR_RETURN_HAL_ERROR_CODE(outEcPoint, "outEcPoint");
558 CHECK_PTR_RETURN_HAL_ERROR_CODE(outEcPoint->val, "outEcPoint->val");
559
560 if (algo != X25519 && algo != P256) {
561 LOGE("Compute algo: %" LOG_PUB "d.", algo);
562 return HAL_ERR_INVALID_PARAM;
563 }
564 if (algo == P256) {
565 LOGI("Compute HashToPoint for P256");
566 return MbedtlsHashToPoint(hash, outEcPoint);
567 }
568
569 CHECK_LEN_EQUAL_RETURN(outEcPoint->length, SHA256_LEN, "outEcPoint->length");
570 return HashToPointX25519(hash, outEcPoint);
571 }
572
AgreeSharedSecretWithStorageP256(const KeyParams * priKeyParams,const KeyBuff * pubKeyBuff,const struct HksBlob * sharedKeyAliasBlob)573 static int32_t AgreeSharedSecretWithStorageP256(const KeyParams *priKeyParams, const KeyBuff *pubKeyBuff,
574 const struct HksBlob *sharedKeyAliasBlob)
575 {
576 struct HksParamSet *initParamSet = NULL;
577 struct HksParamSet *finishParamSet = NULL;
578 int32_t res = ConstructInitParamsP256(&initParamSet, priKeyParams);
579 if (res != HAL_SUCCESS) {
580 return res;
581 }
582 res = ConstructFinishParamsP256(&finishParamSet, priKeyParams, sharedKeyAliasBlob);
583 if (res != HAL_SUCCESS) {
584 FreeParamSet(initParamSet);
585 return res;
586 }
587 struct HksBlob priKeyAliasBlob = { priKeyParams->keyBuff.keyLen, priKeyParams->keyBuff.key };
588 struct HksBlob pubKeyBlob = { pubKeyBuff->keyLen, pubKeyBuff->key };
589 uint8_t handle[sizeof(uint64_t)] = { 0 };
590 struct HksBlob handleBlob = { sizeof(uint64_t), handle };
591 uint8_t outDataUpdate[ECDH_COMMON_SIZE_P256] = { 0 };
592 struct HksBlob outDataUpdateBlob = { ECDH_COMMON_SIZE_P256, outDataUpdate };
593 uint8_t outDataFinish[ECDH_COMMON_SIZE_P256] = { 0 };
594 struct HksBlob outDataFinishBlob = { ECDH_COMMON_SIZE_P256, outDataFinish };
595 do {
596 res = HksInit(&priKeyAliasBlob, initParamSet, &handleBlob, NULL);
597 if (res != HKS_SUCCESS) {
598 LOGE("Huks agree P256 key: HksInit failed, res = %" LOG_PUB "d", res);
599 res = HAL_ERR_HUKS;
600 break;
601 }
602 res = HksUpdate(&handleBlob, initParamSet, &pubKeyBlob, &outDataUpdateBlob);
603 if (res != HKS_SUCCESS) {
604 LOGE("Huks agree P256 key: HksUpdate failed, res = %" LOG_PUB "d", res);
605 DoAbortHks(&handleBlob, initParamSet);
606 res = HAL_ERR_HUKS;
607 break;
608 }
609 LOGI("[HUKS]: HksFinish enter.");
610 res = HksFinish(&handleBlob, finishParamSet, &pubKeyBlob, &outDataFinishBlob);
611 LOGI("[HUKS]: HksFinish quit. [Res]: %" LOG_PUB "d", res);
612 if (res != HKS_SUCCESS) {
613 LOGE("[HUKS]: HksFinish fail. [Res]: %" LOG_PUB "d", res);
614 DoAbortHks(&handleBlob, finishParamSet);
615 res = HAL_ERR_HUKS;
616 break;
617 }
618 } while (0);
619 FreeParamSet(initParamSet);
620 FreeParamSet(finishParamSet);
621 return res;
622 }
623
AgreeSharedSecretWithStorage(const KeyParams * priKeyParams,const KeyBuff * pubKeyBuff,Algorithm algo,uint32_t sharedKeyLen,const Uint8Buff * sharedKeyAlias)624 static int32_t AgreeSharedSecretWithStorage(const KeyParams *priKeyParams, const KeyBuff *pubKeyBuff,
625 Algorithm algo, uint32_t sharedKeyLen, const Uint8Buff *sharedKeyAlias)
626 {
627 int32_t res = CheckAgreeWithStorageParams(priKeyParams, pubKeyBuff, sharedKeyLen, sharedKeyAlias);
628 if (res != HAL_SUCCESS) {
629 return res;
630 }
631
632 struct HksBlob sharedKeyAliasBlob = { sharedKeyAlias->length, sharedKeyAlias->val };
633 if (g_algToHksAlgorithm[algo] == HKS_ALG_ECC) {
634 LOGI("Hks agree key with storage for P256.");
635 return AgreeSharedSecretWithStorageP256(priKeyParams, pubKeyBuff, &sharedKeyAliasBlob);
636 }
637 struct HksParamSet *deParamSet = NULL;
638 KeyParams keyParams = {
639 .keyBuff = { priKeyParams->keyBuff.key, priKeyParams->keyBuff.keyLen, priKeyParams->keyBuff.isAlias },
640 .isDeStorage = true,
641 .osAccountId = priKeyParams->osAccountId
642 };
643 res = ConstructAgreeWithStorageParams(&deParamSet, sharedKeyLen, algo, &keyParams, pubKeyBuff);
644 if (res != HAL_SUCCESS) {
645 return res;
646 }
647 struct HksParamSet *ceParamSet = NULL;
648 keyParams.isDeStorage = false;
649 res = ConstructAgreeWithStorageParams(&ceParamSet, sharedKeyLen, algo, &keyParams, pubKeyBuff);
650 if (res != HAL_SUCCESS) {
651 FreeParamSet(deParamSet);
652 return res;
653 }
654
655 LOGI("[HUKS]: HksGenerateKey enter.");
656 if (priKeyParams->isDeStorage) {
657 res = HksGenerateKey(&sharedKeyAliasBlob, deParamSet, NULL);
658 if (res == HKS_SUCCESS) {
659 MoveSharedKeyToCe(priKeyParams, &sharedKeyAliasBlob);
660 } else {
661 res = HksGenerateKey(&sharedKeyAliasBlob, ceParamSet, NULL);
662 }
663 } else {
664 res = HksGenerateKey(&sharedKeyAliasBlob, ceParamSet, NULL);
665 if (res != HKS_SUCCESS) {
666 res = HksGenerateKey(&sharedKeyAliasBlob, deParamSet, NULL);
667 }
668 }
669 LOGI("[HUKS]: HksGenerateKey quit. [Res]: %" LOG_PUB "d", res);
670 FreeParamSet(deParamSet);
671 FreeParamSet(ceParamSet);
672 if (res != HKS_SUCCESS) {
673 LOGE("[HUKS]: HksGenerateKey fail. [Res]: %" LOG_PUB "d", res);
674 return HAL_FAILED;
675 }
676 return HAL_SUCCESS;
677 }
678
AgreeSharedSecret(const KeyParams * priKeyParams,const KeyBuff * pubKey,Algorithm algo,Uint8Buff * sharedKey)679 static int32_t AgreeSharedSecret(const KeyParams *priKeyParams, const KeyBuff *pubKey, Algorithm algo,
680 Uint8Buff *sharedKey)
681 {
682 int32_t res = CheckAgreeParams(priKeyParams, pubKey, sharedKey);
683 if (res != HAL_SUCCESS) {
684 return res;
685 }
686
687 if (g_algToHksAlgorithm[algo] == HKS_ALG_ECC) {
688 LOGI("Hks agree key for P256.");
689 KeyBuff priKey = { priKeyParams->keyBuff.key, priKeyParams->keyBuff.keyLen, priKeyParams->keyBuff.isAlias };
690 return MbedtlsAgreeSharedSecret(&priKey, pubKey, sharedKey);
691 }
692
693 struct HksBlob priKeyBlob = { priKeyParams->keyBuff.keyLen, priKeyParams->keyBuff.key };
694 struct HksBlob pubKeyBlob = { pubKey->keyLen, pubKey->key };
695 struct HksBlob sharedKeyBlob = { sharedKey->length, sharedKey->val };
696
697 struct HksParamSet *paramSet = NULL;
698 res = ConstructAgreeParamSet(priKeyParams, algo, sharedKey, ¶mSet);
699 if (res != HAL_SUCCESS) {
700 return res;
701 }
702
703 LOGI("[HUKS]: HksAgreeKey enter.");
704 res = HksAgreeKey(paramSet, &priKeyBlob, &pubKeyBlob, &sharedKeyBlob);
705 FreeParamSet(paramSet);
706 LOGI("[HUKS]: HksAgreeKey quit. [Res]: %" LOG_PUB "d", res);
707 if (res != HKS_SUCCESS) {
708 LOGE("[HUKS]: HksAgreeKey fail. [Res]: %" LOG_PUB "d", res);
709 return HAL_FAILED;
710 }
711 return HAL_SUCCESS;
712 }
713
GenerateKeyPairWithStorage(const KeyParams * keyParams,uint32_t keyLen,Algorithm algo,KeyPurpose purpose,const ExtraInfo * exInfo)714 static int32_t GenerateKeyPairWithStorage(const KeyParams *keyParams, uint32_t keyLen, Algorithm algo,
715 KeyPurpose purpose, const ExtraInfo *exInfo)
716 {
717 int32_t res = CheckGenerateKeyPairParams(keyParams, exInfo, keyLen);
718 if (res != HAL_SUCCESS) {
719 return res;
720 }
721
722 KeyParams authIdParams = {
723 .keyBuff = { exInfo->authId.val, exInfo->authId.length, true },
724 .isDeStorage = keyParams->isDeStorage,
725 .osAccountId = keyParams->osAccountId
726 };
727 struct HksParamSet *paramSet = NULL;
728 res = ConstructGenerateKeyPairWithStorageParams(¶mSet, algo, keyLen, purpose, &authIdParams);
729 if (res != HAL_SUCCESS) {
730 return res;
731 }
732 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
733
734 LOGI("[HUKS]: HksGenerateKey enter.");
735 res = HksGenerateKey(&keyAliasBlob, paramSet, NULL);
736 FreeParamSet(paramSet);
737 LOGI("[HUKS]: HksGenerateKey quit. [Res]: %" LOG_PUB "d", res);
738 if (res != HKS_SUCCESS) {
739 LOGE("[HUKS]: HksGenerateKey fail. [Res]: %" LOG_PUB "d", res);
740 return HAL_ERR_HUKS;
741 }
742 return HAL_SUCCESS;
743 }
744
GetKeyPair(struct HksParamSet * outParamSet,Uint8Buff * outPriKey,Uint8Buff * outPubKey)745 static int32_t GetKeyPair(struct HksParamSet *outParamSet, Uint8Buff *outPriKey, Uint8Buff *outPubKey)
746 {
747 int32_t res = HksFreshParamSet(outParamSet, false); /* false means fresh by local, not through IPC */
748 if (res != HKS_SUCCESS) {
749 LOGE("fresh param set failed, res:%" LOG_PUB "d", res);
750 return HAL_ERR_FRESH_PARAM_SET_FAILED;
751 }
752
753 struct HksParam *pubKeyParam = NULL;
754 res = HksGetParam(outParamSet, HKS_TAG_ASYMMETRIC_PUBLIC_KEY_DATA, &pubKeyParam);
755 if (res != HKS_SUCCESS) {
756 LOGE("get pub key from param set failed, res:%" LOG_PUB "d", res);
757 return HAL_ERR_GET_PARAM_FAILED;
758 }
759
760 struct HksParam *priKeyParam = NULL;
761 res = HksGetParam(outParamSet, HKS_TAG_ASYMMETRIC_PRIVATE_KEY_DATA, &priKeyParam);
762 if (res != HKS_SUCCESS) {
763 LOGE("get priv key from param set failed, res:%" LOG_PUB "d", res);
764 return HAL_ERR_GET_PARAM_FAILED;
765 }
766
767 if (memcpy_s(outPubKey->val, outPubKey->length, pubKeyParam->blob.data, pubKeyParam->blob.size) != EOK) {
768 LOGE("parse x25519 output param set memcpy public key failed!");
769 return HAL_ERR_MEMORY_COPY;
770 }
771 outPubKey->length = pubKeyParam->blob.size;
772
773 if (memcpy_s(outPriKey->val, outPriKey->length, priKeyParam->blob.data, priKeyParam->blob.size) != EOK) {
774 LOGE("parse x25519 output param set memcpy private key failed!");
775 return HAL_ERR_MEMORY_COPY;
776 }
777 outPriKey->length = priKeyParam->blob.size;
778
779 return HAL_SUCCESS;
780 }
781
GenerateKeyPair(Algorithm algo,Uint8Buff * outPriKey,Uint8Buff * outPubKey)782 static int32_t GenerateKeyPair(Algorithm algo, Uint8Buff *outPriKey, Uint8Buff *outPubKey)
783 {
784 CHECK_PTR_RETURN_HAL_ERROR_CODE(outPriKey, "outPriKey");
785 CHECK_PTR_RETURN_HAL_ERROR_CODE(outPriKey->val, "outPriKey->key");
786 CHECK_LEN_ZERO_RETURN_ERROR_CODE(outPriKey->length, "outPriKey->keyLen");
787 CHECK_PTR_RETURN_HAL_ERROR_CODE(outPubKey, "outPubKey");
788 CHECK_PTR_RETURN_HAL_ERROR_CODE(outPubKey->val, "outPubKey->key");
789 CHECK_LEN_ZERO_RETURN_ERROR_CODE(outPubKey->length, "outPubKey->keyLen");
790
791 if (outPriKey->length != outPubKey->length) {
792 LOGE("key len not equal.");
793 return HAL_ERR_INVALID_LEN;
794 }
795 uint32_t keyLen = outPriKey->length;
796
797 struct HksParamSet *paramSet = NULL;
798 struct HksParamSet *outParamSet = NULL;
799 int32_t res = ConstructGenerateKeyPairParams(¶mSet, algo, keyLen);
800 if (res != HAL_SUCCESS) {
801 return res;
802 }
803
804 /* need 2 HksParam struct for outPriKey and outPubKey */
805 uint32_t outParamSetSize = sizeof(struct HksParamSet) +
806 2 * (sizeof(struct HksParam)) + outPriKey->length + outPubKey->length;
807 outParamSet = (struct HksParamSet *)HcMalloc(outParamSetSize, 0);
808 if (outParamSet == NULL) {
809 LOGE("allocate buffer for output param set failed");
810 res = HAL_ERR_BAD_ALLOC;
811 goto ERR;
812 }
813 outParamSet->paramSetSize = outParamSetSize;
814
815 LOGI("[HUKS]: HksGenerateKey enter.");
816 res = HksGenerateKey(NULL, paramSet, outParamSet);
817 LOGI("[HUKS]: HksGenerateKey quit. [Res]: %" LOG_PUB "d", res);
818 if (res != HKS_SUCCESS) {
819 LOGI("[HUKS]: HksGenerateKey quit. [Res]: %" LOG_PUB "d", res);
820 res = HAL_FAILED;
821 goto ERR;
822 }
823
824 res = GetKeyPair(outParamSet, outPriKey, outPubKey);
825 if (res != HAL_SUCCESS) {
826 LOGE("parse x25519 output param set failed, res:%" LOG_PUB "d", res);
827 goto ERR;
828 }
829 ERR:
830 FreeParamSet(paramSet);
831 HcFree(outParamSet);
832 return res;
833 }
834
ExportPublicKey(const KeyParams * keyParams,Uint8Buff * outPubKey)835 static int32_t ExportPublicKey(const KeyParams *keyParams, Uint8Buff *outPubKey)
836 {
837 int32_t res = CheckExportParams(keyParams, outPubKey);
838 if (res != HAL_SUCCESS) {
839 return res;
840 }
841
842 struct HksParamSet *deParamSet = NULL;
843 res = ConstructExportParams(true, keyParams->osAccountId, &deParamSet);
844 if (res != HAL_SUCCESS) {
845 return res;
846 }
847 struct HksParamSet *ceParamSet = NULL;
848 res = ConstructExportParams(false, keyParams->osAccountId, &ceParamSet);
849 if (res != HAL_SUCCESS) {
850 FreeParamSet(deParamSet);
851 return res;
852 }
853 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
854 struct HksBlob keyBlob = { outPubKey->length, outPubKey->val };
855
856 LOGI("[HUKS]: HksExportPublicKey enter.");
857 if (keyParams->isDeStorage) {
858 res = HksExportPublicKey(&keyAliasBlob, deParamSet, &keyBlob);
859 if (res == HKS_SUCCESS) {
860 MoveDeKeyToCe(true, keyParams->osAccountId, &keyAliasBlob);
861 } else {
862 res = HksExportPublicKey(&keyAliasBlob, ceParamSet, &keyBlob);
863 }
864 } else {
865 res = HksExportPublicKey(&keyAliasBlob, ceParamSet, &keyBlob);
866 if (res != HKS_SUCCESS) {
867 res = HksExportPublicKey(&keyAliasBlob, deParamSet, &keyBlob);
868 }
869 }
870 LOGI("[HUKS]: HksExportPublicKey quit. [Res]: %" LOG_PUB "d", res);
871 FreeParamSet(deParamSet);
872 FreeParamSet(ceParamSet);
873 if (res != HKS_SUCCESS) {
874 LOGE("[HUKS]: HksExportPublicKey failed. [Res]: %" LOG_PUB "d", res);
875 return HAL_ERR_HUKS;
876 }
877 outPubKey->length = keyBlob.size;
878 return HAL_SUCCESS;
879 }
880
Sign(const KeyParams * keyParams,const Uint8Buff * message,Algorithm algo,Uint8Buff * outSignature)881 static int32_t Sign(const KeyParams *keyParams, const Uint8Buff *message, Algorithm algo,
882 Uint8Buff *outSignature)
883 {
884 int32_t res = CheckSignParams(keyParams, message, outSignature);
885 if (res != HAL_SUCCESS) {
886 return res;
887 }
888
889 uint8_t messageHashVal[SHA256_LEN] = { 0 };
890 Uint8Buff messageHash = { messageHashVal, SHA256_LEN };
891 res = Sha256(message, &messageHash);
892 if (res != HAL_SUCCESS) {
893 LOGE("Sha256 failed.");
894 return res;
895 }
896 struct HksParamSet *deParamSet = NULL;
897 res = ConstructSignParams(true, keyParams->osAccountId, &deParamSet, algo);
898 if (res != HAL_SUCCESS) {
899 return res;
900 }
901 struct HksParamSet *ceParamSet = NULL;
902 res = ConstructSignParams(false, keyParams->osAccountId, &ceParamSet, algo);
903 if (res != HAL_SUCCESS) {
904 FreeParamSet(deParamSet);
905 return res;
906 }
907 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
908 struct HksBlob messageBlob = { messageHash.length, messageHash.val };
909 struct HksBlob signatureBlob = { outSignature->length, outSignature->val };
910
911 LOGI("[HUKS]: HksSign enter.");
912 if (keyParams->isDeStorage) {
913 res = HksSign(&keyAliasBlob, deParamSet, &messageBlob, &signatureBlob);
914 if (res == HKS_SUCCESS) {
915 MoveDeKeyToCe(keyParams->keyBuff.isAlias, keyParams->osAccountId, &keyAliasBlob);
916 } else {
917 res = HksSign(&keyAliasBlob, ceParamSet, &messageBlob, &signatureBlob);
918 }
919 } else {
920 res = HksSign(&keyAliasBlob, ceParamSet, &messageBlob, &signatureBlob);
921 if (res != HKS_SUCCESS) {
922 res = HksSign(&keyAliasBlob, deParamSet, &messageBlob, &signatureBlob);
923 }
924 }
925 LOGI("[HUKS]: HksSign quit. [Res]: %" LOG_PUB "d", res);
926 FreeParamSet(deParamSet);
927 FreeParamSet(ceParamSet);
928 if (res != HKS_SUCCESS) {
929 LOGE("[HUKS]: HksSign fail. [Res]: %" LOG_PUB "d", res);
930 return HAL_FAILED;
931 }
932 outSignature->length = signatureBlob.size;
933 return HAL_SUCCESS;
934 }
935
Verify(const KeyParams * keyParams,const Uint8Buff * message,Algorithm algo,const Uint8Buff * signature)936 static int32_t Verify(const KeyParams *keyParams, const Uint8Buff *message, Algorithm algo,
937 const Uint8Buff *signature)
938 {
939 int32_t res = CheckVerifyParams(keyParams, message, signature);
940 if (res != HAL_SUCCESS) {
941 return res;
942 }
943
944 uint8_t messageHashVal[SHA256_LEN] = { 0 };
945 Uint8Buff messageHash = { messageHashVal, SHA256_LEN };
946 res = Sha256(message, &messageHash);
947 if (res != HAL_SUCCESS) {
948 LOGE("Sha256 failed.");
949 return res;
950 }
951
952 struct HksParamSet *paramSet = NULL;
953 res = ConstructVerifyParams(¶mSet, keyParams, algo);
954 if (res != HAL_SUCCESS) {
955 return res;
956 }
957 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
958 struct HksBlob messageBlob = { messageHash.length, messageHash.val };
959 struct HksBlob signatureBlob = { signature->length, signature->val };
960
961 LOGI("[HUKS]: HksVerify enter.");
962 res = HksVerify(&keyAliasBlob, paramSet, &messageBlob, &signatureBlob);
963 FreeParamSet(paramSet);
964 LOGI("[HUKS]: HksVerify quit. [Res]: %" LOG_PUB "d", res);
965 if ((res != HKS_SUCCESS)) {
966 LOGE("[HUKS]: HksVerify fail. [Res]: %" LOG_PUB "d", res);
967 return HAL_FAILED;
968 }
969 return HAL_SUCCESS;
970 }
971
ImportPublicKey(const KeyParams * keyParams,const Uint8Buff * pubKey,Algorithm algo,const ExtraInfo * exInfo)972 static int32_t ImportPublicKey(const KeyParams *keyParams, const Uint8Buff *pubKey, Algorithm algo,
973 const ExtraInfo *exInfo)
974 {
975 int32_t res = CheckImportPubKeyParams(keyParams, pubKey, exInfo);
976 if (res != HAL_SUCCESS) {
977 return res;
978 }
979
980 union KeyRoleInfoUnion roleInfoUnion;
981 roleInfoUnion.roleInfoStruct.userType = (uint8_t)exInfo->userType;
982 roleInfoUnion.roleInfoStruct.pairType = (uint8_t)exInfo->pairType;
983 roleInfoUnion.roleInfoStruct.reserved1 = (uint8_t)0;
984 roleInfoUnion.roleInfoStruct.reserved2 = (uint8_t)0;
985
986 KeyParams authIdParams = {
987 .keyBuff = { exInfo->authId.val, exInfo->authId.length, true },
988 .isDeStorage = keyParams->isDeStorage,
989 .osAccountId = keyParams->osAccountId
990 };
991 struct HksParamSet *paramSet = NULL;
992 res = ConstructImportPublicKeyParams(¶mSet, algo, pubKey->length, &authIdParams, &roleInfoUnion);
993 if (res != HAL_SUCCESS) {
994 return res;
995 }
996 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
997 struct HksBlob pubKeyBlob = { pubKey->length, pubKey->val };
998
999 LOGI("[HUKS]: HksImportKey enter.");
1000 res = HksImportKey(&keyAliasBlob, paramSet, &pubKeyBlob);
1001 FreeParamSet(paramSet);
1002 LOGI("[HUKS]: HksImportKey quit. [Res]: %" LOG_PUB "d", res);
1003 if (res != HKS_SUCCESS) {
1004 LOGE("[HUKS]: HksImportKey fail. [Res]: %" LOG_PUB "d", res);
1005 return HAL_ERR_HUKS;
1006 }
1007 return HAL_SUCCESS;
1008 }
1009
BigNumCompare(const Uint8Buff * a,const Uint8Buff * b)1010 static int32_t BigNumCompare(const Uint8Buff *a, const Uint8Buff *b)
1011 {
1012 int res = 0;
1013 if (!CheckBigNumCompareParams(a, b, &res)) {
1014 return res;
1015 }
1016 const uint8_t *tmpA = a->val;
1017 const uint8_t *tmpB = b->val;
1018 uint32_t len = a->length;
1019 if (a->length < b->length) {
1020 for (uint32_t i = 0; i < b->length - a->length; i++) {
1021 if (b->val[i] > 0) {
1022 return 1; // a < b
1023 }
1024 }
1025 tmpA = a->val;
1026 tmpB = b->val + b->length - a->length;
1027 len = a->length;
1028 }
1029 if (a->length > b->length) {
1030 for (uint32_t i = 0; i < a->length - b->length; i++) {
1031 if (a->val[i] > 0) {
1032 return -1; // a > b
1033 }
1034 }
1035 tmpA = a->val + a->length - b->length;
1036 tmpB = b->val;
1037 len = b->length;
1038 }
1039 for (uint32_t i = 0; i < len; i++) {
1040 if (*(tmpA + i) > *(tmpB + i)) {
1041 return -1; // a > b
1042 }
1043 if (*(tmpA + i) < *(tmpB + i)) {
1044 return 1; // a < b
1045 }
1046 }
1047 return 0; // a == b
1048 }
1049
CheckDlPublicKey(const Uint8Buff * key,const char * primeHex)1050 static bool CheckDlPublicKey(const Uint8Buff *key, const char *primeHex)
1051 {
1052 if (key == NULL || key->val == NULL || primeHex == NULL) {
1053 LOGE("Params is null.");
1054 return false;
1055 }
1056 uint8_t min = 1;
1057
1058 uint32_t innerKeyLen = HcStrlen(primeHex) / BYTE_TO_HEX_OPER_LENGTH;
1059 if (key->length > innerKeyLen) {
1060 LOGE("Key length > prime number length.");
1061 return false;
1062 }
1063 uint8_t *primeByte = (uint8_t *)HcMalloc(innerKeyLen, 0);
1064 if (primeByte == NULL) {
1065 LOGE("Malloc for primeByte failed.");
1066 return false;
1067 }
1068 if (HexStringToByte(primeHex, primeByte, innerKeyLen) != HAL_SUCCESS) {
1069 LOGE("Convert prime number from hex string to byte failed.");
1070 HcFree(primeByte);
1071 return false;
1072 }
1073 /*
1074 * P - 1, since the last byte of large prime number must be greater than 1,
1075 * needn't to think about borrowing forward
1076 */
1077 primeByte[innerKeyLen - 1] -= 1;
1078
1079 Uint8Buff minBuff = { &min, sizeof(uint8_t) };
1080 if (BigNumCompare(key, &minBuff) >= 0) {
1081 LOGE("Pubkey is invalid, key <= 1.");
1082 HcFree(primeByte);
1083 return false;
1084 }
1085
1086 Uint8Buff primeBuff = { primeByte, innerKeyLen };
1087 if (BigNumCompare(key, &primeBuff) <= 0) {
1088 LOGE("Pubkey is invalid, key >= p - 1.");
1089 HcFree(primeByte);
1090 return false;
1091 }
1092
1093 HcFree(primeByte);
1094 return true;
1095 }
1096
CheckEcPublicKey(const Uint8Buff * pubKey,Algorithm algo)1097 static bool CheckEcPublicKey(const Uint8Buff *pubKey, Algorithm algo)
1098 {
1099 if (algo == P256) {
1100 LOGI("Start check P256 public key");
1101 return MbedtlsIsP256PublicKeyValid(pubKey);
1102 } else if (algo == X25519) {
1103 LOGI("Start check X25519 public key");
1104 return MbedtlsIsX25519PublicKeyValid(pubKey);
1105 }
1106 LOGE("Algorithm not support!");
1107 return false;
1108 }
1109
ImportSymmetricKey(const KeyParams * keyParams,const Uint8Buff * authToken,KeyPurpose purpose,const ExtraInfo * exInfo)1110 static int32_t ImportSymmetricKey(const KeyParams *keyParams, const Uint8Buff *authToken, KeyPurpose purpose,
1111 const ExtraInfo *exInfo)
1112 {
1113 int32_t res = CheckImportSymmetricKeyParams(keyParams, authToken);
1114 if (res != HAL_SUCCESS) {
1115 return res;
1116 }
1117
1118 struct HksParamSet *paramSet = NULL;
1119 res = ConstructImportSymmetricKeyParam(¶mSet, keyParams, authToken->length, purpose, exInfo);
1120 if (res != HAL_SUCCESS) {
1121 LOGE("construct param set failed, res = %" LOG_PUB "d", res);
1122 return res;
1123 }
1124 struct HksBlob keyAliasBlob = { keyParams->keyBuff.keyLen, keyParams->keyBuff.key };
1125 struct HksBlob symKeyBlob = { authToken->length, authToken->val };
1126
1127 LOGI("[HUKS]: HksImportKey enter.");
1128 res = HksImportKey(&keyAliasBlob, paramSet, &symKeyBlob);
1129 FreeParamSet(paramSet);
1130 LOGI("[HUKS]: HksImportKey quit. [Res]: %" LOG_PUB "d", res);
1131 if (res != HKS_SUCCESS) {
1132 LOGE("[HUKS]: HksImportKey fail. [Res]: %" LOG_PUB "d", res);
1133 return HAL_FAILED;
1134 }
1135 return HAL_SUCCESS;
1136 }
1137
1138 static const AlgLoader g_huksLoader = {
1139 .initAlg = InitHks,
1140 .sha256 = Sha256,
1141 .generateRandom = GenerateRandom,
1142 .computeHmac = ComputeHmac,
1143 .computeHmacWithThreeStage = ComputeHmacWithThreeStage,
1144 .computeHkdf = ComputeHkdf,
1145 .computePseudonymPsk = ComputePseudonymPsk,
1146 .getKeyExtInfo = GetKeyExtInfo,
1147 .importSymmetricKey = ImportSymmetricKey,
1148 .checkKeyExist = CheckKeyExist,
1149 .deleteKey = DeleteKey,
1150 .aesGcmEncrypt = AesGcmEncrypt,
1151 .aesGcmDecrypt = AesGcmDecrypt,
1152 .hashToPoint = HashToPoint,
1153 .agreeSharedSecretWithStorage = AgreeSharedSecretWithStorage,
1154 .agreeSharedSecret = AgreeSharedSecret,
1155 .bigNumExpMod = BigNumExpMod,
1156 .generateKeyPairWithStorage = GenerateKeyPairWithStorage,
1157 .generateKeyPair = GenerateKeyPair,
1158 .exportPublicKey = ExportPublicKey,
1159 .sign = Sign,
1160 .verify = Verify,
1161 .importPublicKey = ImportPublicKey,
1162 .checkDlPublicKey = CheckDlPublicKey,
1163 .checkEcPublicKey = CheckEcPublicKey,
1164 .bigNumCompare = BigNumCompare,
1165 .base64Encode = MbedtlsBase64Encode,
1166 .base64Decode = MbedtlsBase64Decode
1167 };
1168
GetRealLoaderInstance(void)1169 const AlgLoader *GetRealLoaderInstance(void)
1170 {
1171 return &g_huksLoader;
1172 }
1173