1 /*
2 * Copyright (c) 2024 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 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21
22 #include "hks_core_service_key_generate.h"
23
24 #include <stdbool.h>
25 #include <stddef.h>
26
27 #include "hks_ability.h"
28 #include "dcm_attest.h"
29 #include "hks_auth.h"
30 #include "hks_base_check.h"
31 #include "hks_check_paramset.h"
32 #include "hks_chipset_platform_decrypt.h"
33 #include "hks_client_service_adapter_common.h"
34 #include "hks_cmd_id.h"
35 #include "hks_common_check.h"
36 #include "hks_core_service_three_stage.h"
37 #include "hks_crypto_adapter.h"
38 #include "hks_crypto_hal.h"
39 #include "hks_log.h"
40 #include "hks_mem.h"
41 #include "hks_param.h"
42 #include "hks_secure_access.h"
43 #include "hks_sm_import_wrap_key.h"
44 #include "hks_template.h"
45 #include "hks_type_inner.h"
46 #include "hks_util.h"
47 #include "hks_core_service_key_operate_one_stage.h"
48
49 #include "securec.h"
50
51 #ifndef _HARDWARE_ROOT_KEY_
52 #include "hks_rkc.h"
53 #endif
54
55 #ifndef _CUT_AUTHENTICATE_
56
57 static const uint8_t g_defaultRsaPubExponent[] = { 0x01, 0x00, 0x01 }; /* default 65537 */
58
GetGenType(const struct HksParamSet * paramSet,uint32_t * genType)59 static int32_t GetGenType(const struct HksParamSet *paramSet, uint32_t *genType)
60 {
61 struct HksParam *keyGenTypeParam = NULL;
62 int32_t ret = HksGetParam(paramSet, HKS_TAG_KEY_GENERATE_TYPE, &keyGenTypeParam);
63 /* if not set KEY_GENERATE_TYPE, gen key by default type */
64 HKS_IF_NOT_SUCC_RETURN(ret, HKS_SUCCESS)
65
66 struct HksParam *keyAlgParam = NULL;
67 ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &keyAlgParam);
68 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL, "get alg tag fail")
69
70 ret = HKS_ERROR_INVALID_ARGUMENT;
71 switch (keyGenTypeParam->uint32Param) {
72 case HKS_KEY_GENERATE_TYPE_DEFAULT:
73 *genType = HKS_KEY_GENERATE_TYPE_DEFAULT;
74 ret = HKS_SUCCESS;
75 break;
76 case HKS_KEY_GENERATE_TYPE_AGREE:
77 if (keyAlgParam->uint32Param == HKS_ALG_AES) { /* only aes key can be generated by agree */
78 *genType = HKS_KEY_GENERATE_TYPE_AGREE;
79 ret = HKS_SUCCESS;
80 } else {
81 *genType = HKS_KEY_GENERATE_TYPE_DEFAULT;
82 ret = HKS_SUCCESS;
83 }
84 break;
85 default:
86 HKS_LOG_E("invalid generated key type");
87 }
88
89 return ret;
90 }
91
92 #ifdef HKS_SUPPORT_ED25519_TO_X25519
CheckAgreeKeyIn(const struct HksBlob * key)93 static int32_t CheckAgreeKeyIn(const struct HksBlob *key)
94 {
95 HKS_IF_NOT_SUCC_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT)
96
97 if (key->size < sizeof(struct Hks25519KeyPair)) {
98 HKS_LOG_E("invlaid agree key size");
99 return HKS_ERROR_INVALID_ARGUMENT;
100 }
101
102 struct Hks25519KeyPair *keyPair = (struct Hks25519KeyPair *)(key->data);
103 if ((keyPair->privateBufferSize > (key->size - sizeof(*keyPair))) ||
104 (keyPair->publicBufferSize > (key->size - sizeof(*keyPair) - keyPair->privateBufferSize))) {
105 HKS_LOG_E("invlaid agree key size, small than keyPair");
106 return HKS_ERROR_INVALID_ARGUMENT;
107 }
108 return HKS_SUCCESS;
109 }
110
GetAgreeBaseKey(const bool isPubKey,const bool isPlainPubKey,const struct HksBlob * keyIn,struct HksBlob * keyOut)111 static int32_t GetAgreeBaseKey(const bool isPubKey, const bool isPlainPubKey, const struct HksBlob *keyIn,
112 struct HksBlob *keyOut)
113 {
114 struct Hks25519KeyPair *keyPair = (struct Hks25519KeyPair *)(keyIn->data);
115 uint32_t size = isPubKey ? keyPair->publicBufferSize : keyPair->privateBufferSize;
116 uint8_t *buffer = (uint8_t *)HksMalloc(size);
117 HKS_IF_NULL_LOGE_RETURN(buffer, HKS_ERROR_MALLOC_FAIL, "malloc failed")
118
119 uint8_t *tmp = isPubKey ? (keyIn->data + sizeof(*keyPair)) :
120 (keyIn->data + sizeof(*keyPair) + keyPair->publicBufferSize);
121 (void)memcpy_s(buffer, size, tmp, size);
122
123 if (isPlainPubKey) { /* public key is plain key, only copy */
124 keyOut->data = buffer;
125 keyOut->size = size;
126 return HKS_SUCCESS;
127 }
128
129 struct HksBlob tempKey = { size, buffer };
130 struct HksKeyNode *keyNode = HksGenerateKeyNode(&tempKey);
131 (void)memset_s(buffer, size, 0, size);
132 HKS_FREE(buffer);
133 HKS_IF_NULL_LOGE_RETURN(keyNode, HKS_ERROR_CORRUPT_FILE, "generating keynode with agree key failed")
134
135 bool isSupportUserAuth = false;
136 int32_t ret = HksCheckKeybBlobIsSupportUserAuth(keyNode->paramSet, &isSupportUserAuth);
137 if (ret != HKS_SUCCESS) {
138 HKS_LOG_E("HksCheckKeybBlobIsSupportUserAuth failed");
139 HksFreeKeyNode(&keyNode);
140 return ret;
141 }
142
143 if (isSupportUserAuth) {
144 HKS_LOG_E("key should do user auth, but one stage api do not support user auth operation");
145 HksFreeKeyNode(&keyNode);
146 return HKS_ERROR_NOT_SUPPORTED;
147 }
148
149 ret = HksGetRawKey(keyNode->paramSet, keyOut);
150 HKS_IF_NOT_SUCC_LOGE(ret, "get raw key during key agreement failed!")
151
152 HksFreeKeyNode(&keyNode);
153 return ret;
154 }
155
GetAgreePriKey(const struct HksBlob * keyIn,struct HksBlob * keyOut)156 static int32_t GetAgreePriKey(const struct HksBlob *keyIn, struct HksBlob *keyOut)
157 {
158 return GetAgreeBaseKey(false, false, keyIn, keyOut);
159 }
160
GetAgreePubKey(const struct HksBlob * keyIn,const struct HksParamSet * paramSet,struct HksBlob * keyOut)161 static int32_t GetAgreePubKey(const struct HksBlob *keyIn, const struct HksParamSet *paramSet, struct HksBlob *keyOut)
162 {
163 struct HksParam *isKeyAliasParam = NULL;
164 int32_t ret = HksGetParam(paramSet, HKS_TAG_AGREE_PUBLIC_KEY_IS_KEY_ALIAS, &isKeyAliasParam);
165 if ((ret == HKS_SUCCESS) && (!(isKeyAliasParam->boolParam))) {
166 return GetAgreeBaseKey(true, true, keyIn, keyOut);
167 }
168
169 return GetAgreeBaseKey(true, false, keyIn, keyOut);
170 }
171
GenAgreeKey(const struct HksParamSet * paramSet,const struct HksBlob * privateKey,const struct HksBlob * peerPublicKey,struct HksBlob * agreedKey)172 static int32_t GenAgreeKey(const struct HksParamSet *paramSet, const struct HksBlob *privateKey,
173 const struct HksBlob *peerPublicKey, struct HksBlob *agreedKey)
174 {
175 struct HksParam *agreeAlgParam = NULL;
176 int32_t ret = HksGetParam(paramSet, HKS_TAG_AGREE_ALG, &agreeAlgParam);
177 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "generate key not set agree alg")
178
179 agreedKey->size = HKS_KEY_BYTES(HKS_AES_KEY_SIZE_256);
180 agreedKey->data = (uint8_t *)HksMalloc(agreedKey->size);
181 HKS_IF_NULL_LOGE_RETURN(agreedKey->data, HKS_ERROR_MALLOC_FAIL, "malloc failed")
182
183 struct HksKeySpec agreeSpec = { 0, HKS_CURVE25519_KEY_SIZE_256, NULL };
184 if (agreeAlgParam->uint32Param == HKS_ALG_X25519) {
185 agreeSpec.algType = HKS_ALG_X25519;
186 } else if (agreeAlgParam->uint32Param == HKS_ALG_ED25519) {
187 agreeSpec.algType = HKS_ALG_ED25519;
188 } else {
189 HKS_FREE(agreedKey->data);
190 return HKS_ERROR_INVALID_ARGUMENT;
191 }
192
193 ret = HksCryptoHalAgreeKey(privateKey, peerPublicKey, &agreeSpec, agreedKey);
194 if (ret != HKS_SUCCESS) {
195 HKS_LOG_E("agree key failed, ret = %" LOG_PUBLIC "d", ret);
196 HKS_FREE(agreedKey->data); /* X25519AgreeKey will memset sharedKey if fail */
197 }
198 return ret;
199 }
200
GenKeyByAgree(const struct HksBlob * keyIn,const struct HksParamSet * paramSet,struct HksBlob * sharedKey)201 static int32_t GenKeyByAgree(const struct HksBlob *keyIn, const struct HksParamSet *paramSet,
202 struct HksBlob *sharedKey)
203 {
204 int32_t ret = CheckAgreeKeyIn(keyIn);
205 HKS_IF_NOT_SUCC_RETURN(ret, ret)
206
207 struct HksBlob priKey = { 0, NULL };
208 struct HksBlob pubKey = { 0, NULL };
209 do {
210 ret = GetAgreePriKey(keyIn, &priKey);
211 HKS_IF_NOT_SUCC_BREAK(ret)
212
213 ret = GetAgreePubKey(keyIn, paramSet, &pubKey);
214 HKS_IF_NOT_SUCC_BREAK(ret)
215
216 ret = GenAgreeKey(paramSet, &priKey, &pubKey, sharedKey);
217 } while (0);
218
219 if (priKey.data != NULL) {
220 (void)memset_s(priKey.data, priKey.size, 0, priKey.size);
221 HKS_FREE_BLOB(priKey);
222 }
223 if (pubKey.data != NULL) {
224 (void)memset_s(pubKey.data, pubKey.size, 0, pubKey.size);
225 HKS_FREE_BLOB(pubKey);
226 }
227 return ret;
228 }
229 #endif
230
HksCoreGenerateKey(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,const struct HksBlob * keyIn,struct HksBlob * keyOut)231 int32_t HksCoreGenerateKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
232 const struct HksBlob *keyIn, struct HksBlob *keyOut)
233 {
234 int32_t ret = HksCoreCheckGenKeyParams(keyAlias, paramSet, keyIn, keyOut, HKS_KEY_FLAG_GENERATE_KEY);
235 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "hks core check generate key params failed, ret:%" LOG_PUBLIC "x!", ret)
236
237 uint32_t genType = HKS_KEY_GENERATE_TYPE_DEFAULT;
238 ret = GetGenType(paramSet, &genType);
239 HKS_IF_NOT_SUCC_RETURN(ret, ret)
240
241 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckIfNeedIsDevicePasswordSet(paramSet), HKS_ERROR_DEVICE_PASSWORD_UNSET,
242 "a device password is required but not set yet!")
243
244 struct HksBlob key = { 0, NULL };
245 switch (genType) {
246 case HKS_KEY_GENERATE_TYPE_DEFAULT: {
247 struct HksKeySpec spec = {0};
248 HksFillKeySpec(paramSet, &spec);
249 ret = HksCryptoHalGenerateKey(&spec, &key);
250 break;
251 }
252 case HKS_KEY_GENERATE_TYPE_AGREE:
253 #ifdef HKS_SUPPORT_ED25519_TO_X25519
254 ret = GenKeyByAgree(keyIn, paramSet, &key);
255 #else
256 ret = HKS_ERROR_INVALID_ARGUMENT;
257 #endif
258 break;
259 default:
260 ret = HKS_ERROR_INVALID_ARGUMENT;
261 }
262 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "GenerateKey failed, ret:%" LOG_PUBLIC "x!", ret)
263
264 ret = HksBuildKeyBlob(keyAlias, HKS_KEY_FLAG_GENERATE_KEY, &key, paramSet, keyOut);
265 (void)memset_s(key.data, key.size, 0, key.size);
266 HKS_FREE_BLOB(key);
267 return ret;
268 }
269
AddProcessIdentityInfoToParamSet(const struct HksParamSet * inParamSet,struct HksParamSet * paramSet)270 static int32_t AddProcessIdentityInfoToParamSet(const struct HksParamSet *inParamSet, struct HksParamSet *paramSet)
271 {
272 uint32_t transferTagList[] = { HKS_TAG_ACCESS_TOKEN_ID, HKS_TAG_USER_ID, HKS_TAG_PROCESS_NAME };
273 int32_t ret;
274 for (uint32_t i = 0; i < HKS_ARRAY_SIZE(transferTagList); ++i) {
275 struct HksParam *tmpParam = NULL;
276 ret = HksGetParam(inParamSet, transferTagList[i], &tmpParam);
277 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get param %" LOG_PUBLIC "u failed.", i)
278
279 ret = HksAddParams(paramSet, tmpParam, 1);
280 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "add param %" LOG_PUBLIC "u failed.", i)
281 }
282 return ret;
283 }
284
AddAgreeKeyParamSetFromUnwrapSuite(uint32_t suite,const struct HksParamSet * inParamSet,struct HksParamSet * paramSet)285 static int32_t AddAgreeKeyParamSetFromUnwrapSuite(uint32_t suite, const struct HksParamSet *inParamSet,
286 struct HksParamSet *paramSet)
287 {
288 uint32_t alg = HKS_ALG_X25519;
289 uint32_t keySize = HKS_CURVE25519_KEY_SIZE_256;
290 switch (suite) {
291 case HKS_UNWRAP_SUITE_X25519_AES_256_GCM_NOPADDING:
292 alg = HKS_ALG_X25519;
293 keySize = HKS_CURVE25519_KEY_SIZE_256;
294 break;
295 case HKS_UNWRAP_SUITE_ECDH_AES_256_GCM_NOPADDING:
296 alg = HKS_ALG_ECDH;
297 keySize = HKS_ECC_KEY_SIZE_256;
298 break;
299 case HKS_UNWRAP_SUITE_SM2_SM4_128_CBC_PKCS7:
300 case HKS_UNWRAP_SUITE_SM2_SM4_128_CBC_PKCS7_WITH_VERIFY_DIG_SM3:
301 alg = HKS_ALG_SM2;
302 keySize = HKS_SM2_KEY_SIZE_256;
303 break;
304 default:
305 HKS_LOG_E("invalid suite type use x25519 default");
306 break;
307 }
308
309 struct HksParam agreeParams[] = {
310 { .tag = HKS_TAG_ALGORITHM, .uint32Param = alg },
311 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_AGREE },
312 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = keySize }
313 };
314
315 int32_t ret = HksAddParams(paramSet, agreeParams, sizeof(agreeParams) / sizeof(struct HksParam));
316 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "unwrap suite add params failed.")
317
318 return AddProcessIdentityInfoToParamSet(inParamSet, paramSet);
319 }
320
GenAgreeKeyParamSetFromUnwrapSuite(uint32_t suite,const struct HksParamSet * inParamSet,struct HksParamSet ** outParamSet)321 static int32_t GenAgreeKeyParamSetFromUnwrapSuite(uint32_t suite, const struct HksParamSet *inParamSet,
322 struct HksParamSet **outParamSet)
323 {
324 struct HksParamSet *paramSet = NULL;
325 int32_t ret = HksInitParamSet(¶mSet);
326 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "init agree_key param set fail")
327
328 ret = AddAgreeKeyParamSetFromUnwrapSuite(suite, inParamSet, paramSet);
329 if (ret != HKS_SUCCESS) {
330 HKS_LOG_E("unwrap suite add params failed.");
331 HksFreeParamSet(¶mSet);
332 return ret;
333 }
334
335 ret = HksBuildParamSet(¶mSet);
336 if (ret != HKS_SUCCESS) {
337 HKS_LOG_E("unwrap suite build params failed.");
338 HksFreeParamSet(¶mSet);
339 return ret;
340 }
341
342 *outParamSet = paramSet;
343 return HKS_SUCCESS;
344 }
345
BuildDecryptUsageSpecOfUnwrap(const struct HksBlob * aad,const struct HksBlob * nonce,const struct HksBlob * aeadTag,uint32_t payloadLen,struct HksUsageSpec * usageSpec)346 static int32_t BuildDecryptUsageSpecOfUnwrap(const struct HksBlob *aad, const struct HksBlob *nonce,
347 const struct HksBlob *aeadTag, uint32_t payloadLen, struct HksUsageSpec *usageSpec)
348 {
349 usageSpec->mode = HKS_MODE_GCM;
350 usageSpec->padding = HKS_PADDING_NONE;
351 usageSpec->digest = HKS_DIGEST_NONE;
352 usageSpec->algType = HKS_ALG_AES;
353
354 struct HksAeadParam *aeadParam = (struct HksAeadParam *)HksMalloc(sizeof(struct HksAeadParam));
355 HKS_IF_NULL_LOGE_RETURN(aeadParam, HKS_ERROR_MALLOC_FAIL, "build dec wrapped usage: aeadParam malloc failed!")
356
357 aeadParam->aad = *aad;
358 aeadParam->nonce = *nonce;
359 aeadParam->payloadLen = payloadLen;
360 aeadParam->tagDec = *aeadTag;
361
362 usageSpec->algParam = aeadParam;
363 return HKS_SUCCESS;
364 }
365
CheckWrappingKeyIsUsedForUnwrap(const struct HksBlob * wrappingKey)366 static int32_t CheckWrappingKeyIsUsedForUnwrap(const struct HksBlob *wrappingKey)
367 {
368 struct HksKeyNode *wrappingKeyNode = HksGenerateKeyNode(wrappingKey);
369 HKS_IF_NULL_LOGE_RETURN(wrappingKeyNode, HKS_ERROR_CORRUPT_FILE,
370 "check agree params: generate wrapping keynode failed!")
371
372 struct HksParam *purposeParamWrappingKey = NULL;
373 int32_t ret = HksGetParam(wrappingKeyNode->paramSet, HKS_TAG_PURPOSE, &purposeParamWrappingKey);
374 if (ret != HKS_SUCCESS) {
375 HKS_LOG_E("get wrapping key param 0x%" LOG_PUBLIC "x failed!", HKS_TAG_PURPOSE);
376 HksFreeKeyNode(&wrappingKeyNode);
377 return HKS_ERROR_CHECK_GET_PURPOSE_FAIL;
378 }
379
380 if (purposeParamWrappingKey->uint32Param != HKS_KEY_PURPOSE_UNWRAP) {
381 HKS_LOG_E("wrapping key is not used for unwrap!");
382 HksFreeKeyNode(&wrappingKeyNode);
383 return HKS_ERROR_INVALID_USAGE_OF_KEY;
384 }
385 HksFreeKeyNode(&wrappingKeyNode);
386 return HKS_SUCCESS;
387 }
388
GetPublicKeyInnerFormat(const struct HksBlob * wrappingKey,const struct HksBlob * wrappedKeyData,struct HksBlob * outPublicKey,uint32_t * partOffset)389 static int32_t GetPublicKeyInnerFormat(const struct HksBlob *wrappingKey, const struct HksBlob *wrappedKeyData,
390 struct HksBlob *outPublicKey, uint32_t *partOffset)
391 {
392 struct HksBlob peerPubKeyPart = { 0, NULL };
393 uint32_t offset = *partOffset;
394 int32_t ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, HKS_IMPORT_WRAPPED_KEY_TOTAL_BLOBS,
395 &peerPubKeyPart);
396 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get peer pub key failed!")
397
398 /* peer public key format should be same as wrapping key */
399 struct HksKeyNode *wrappingKeyNode = HksGenerateKeyNode(wrappingKey);
400 HKS_IF_NULL_LOGE_RETURN(wrappingKeyNode, HKS_ERROR_CORRUPT_FILE, "generate wrapping key keynode failed")
401
402 ret = GetHksPubKeyInnerFormat(wrappingKeyNode->paramSet, &peerPubKeyPart, outPublicKey);
403 HksFreeKeyNode(&wrappingKeyNode);
404 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get peer pub key inner format failed!")
405
406 *partOffset = offset;
407 return HKS_SUCCESS;
408 }
409
AgreeSharedSecretWithPeerPublicKey(const struct HksBlob * wrappingKey,const struct HksBlob * publicKey,uint32_t unwrapSuite,struct HksBlob * agreeSharedSecret,const struct HksParamSet * inParamSet)410 static int32_t AgreeSharedSecretWithPeerPublicKey(const struct HksBlob *wrappingKey, const struct HksBlob *publicKey,
411 uint32_t unwrapSuite, struct HksBlob *agreeSharedSecret, const struct HksParamSet *inParamSet)
412 {
413 int32_t ret = CheckWrappingKeyIsUsedForUnwrap(wrappingKey);
414 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check agree params failed!")
415
416 struct HksParamSet *agreeKeyParamSet = NULL;
417 ret = GenAgreeKeyParamSetFromUnwrapSuite(unwrapSuite, inParamSet, &agreeKeyParamSet);
418 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "gen agree key paramSet failed!")
419
420 struct HksBlob sharedSecret = { 0, NULL };
421 sharedSecret.size = HKS_KEY_BYTES(HKS_AES_KEY_SIZE_256);
422 uint8_t *shareSecretBuffer = (uint8_t *) HksMalloc(sharedSecret.size);
423 if (shareSecretBuffer == NULL) {
424 HksFreeParamSet(&agreeKeyParamSet);
425 HKS_LOG_E("malloc shared key failed!");
426 return HKS_ERROR_MALLOC_FAIL;
427 }
428 sharedSecret.data = shareSecretBuffer;
429
430 ret = HksCoreAgreeKey(agreeKeyParamSet, wrappingKey, publicKey, &sharedSecret);
431 HksFreeParamSet(&agreeKeyParamSet);
432 if (ret != HKS_SUCCESS) {
433 HKS_LOG_E("agree with peer public key failed! ret = %" LOG_PUBLIC "d", ret);
434 HKS_FREE(sharedSecret.data);
435 return ret;
436 }
437
438 agreeSharedSecret->size = sharedSecret.size;
439 agreeSharedSecret->data = sharedSecret.data;
440 return HKS_SUCCESS;
441 }
442
ParseKekDecryptParams(const struct HksBlob * wrappedKeyData,uint32_t * partOffset,uint32_t totalBlobs,struct HksBlob ** blobArray)443 static int32_t ParseKekDecryptParams(const struct HksBlob *wrappedKeyData, uint32_t *partOffset,
444 uint32_t totalBlobs, struct HksBlob **blobArray)
445 {
446 uint32_t offset = *partOffset;
447 uint32_t blobIndex = 0;
448 int32_t ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
449 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get agree-key aad data failed!")
450
451 ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
452 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get agree-key nonce data failed!")
453
454 ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
455 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get agree-key aead tag data failed!")
456
457 ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
458 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get kek enc data failed!")
459
460 *partOffset = offset;
461 return HKS_SUCCESS;
462 }
463
DecryptKekWithAgreeSharedSecret(const struct HksBlob * wrappedKeyData,const struct HksBlob * agreeSharedSecret,uint32_t * partOffset,struct HksBlob * outKekBlob)464 static int32_t DecryptKekWithAgreeSharedSecret(const struct HksBlob *wrappedKeyData,
465 const struct HksBlob *agreeSharedSecret, uint32_t *partOffset, struct HksBlob *outKekBlob)
466 {
467 struct HksBlob agreeKeyAadPart = { 0, NULL };
468 struct HksBlob agreeKeyNoncePart = { 0, NULL };
469 struct HksBlob agreeKeyTagPart = { 0, NULL };
470 struct HksBlob kekEncDataPart = { 0, NULL };
471 struct HksBlob *blobArray[] = { &agreeKeyAadPart, &agreeKeyNoncePart, &agreeKeyTagPart, &kekEncDataPart };
472
473 uint32_t offset = *partOffset;
474 int32_t ret = ParseKekDecryptParams(wrappedKeyData, &offset, HKS_IMPORT_WRAPPED_KEY_TOTAL_BLOBS, blobArray);
475 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "parse agree-key decrypt kek params failed!")
476
477 /* build decrypt kek usagespec */
478 struct HksUsageSpec *decKekUsageSpec = (struct HksUsageSpec *)HksMalloc(sizeof(struct HksUsageSpec));
479 HKS_IF_NULL_LOGE_RETURN(decKekUsageSpec, HKS_ERROR_MALLOC_FAIL, "malloc decrypt kek usage spec failed!")
480
481 (void)memset_s(decKekUsageSpec, sizeof(struct HksUsageSpec), 0, sizeof(struct HksUsageSpec));
482 ret = BuildDecryptUsageSpecOfUnwrap(&agreeKeyAadPart, &agreeKeyNoncePart, &agreeKeyTagPart, kekEncDataPart.size,
483 decKekUsageSpec);
484 if (ret != HKS_SUCCESS) {
485 HKS_LOG_E("build decrypt wrapped data kek usageSpec failed!");
486 HksFreeUsageSpec(&decKekUsageSpec);
487 return ret;
488 }
489 struct HksBlob kek = { 0, NULL };
490 kek.size = HKS_KEY_BYTES(HKS_AES_KEY_SIZE_256);
491 uint8_t *kekBuffer = (uint8_t *) HksMalloc(kek.size);
492 if (kekBuffer == NULL) {
493 HKS_LOG_E("malloc kek memory failed!");
494 HksFreeUsageSpec(&decKekUsageSpec);
495 return HKS_ERROR_MALLOC_FAIL;
496 }
497 kek.data = kekBuffer;
498 ret = HksCryptoHalDecrypt(agreeSharedSecret, decKekUsageSpec, &kekEncDataPart, &kek);
499 HksFreeUsageSpec(&decKekUsageSpec);
500 if (ret != HKS_SUCCESS) {
501 HKS_LOG_E("decrypt kek data failed!");
502 HKS_FREE(kek.data);
503 return ret;
504 }
505
506 outKekBlob->size = kek.size;
507 outKekBlob->data = kek.data;
508 *partOffset = offset;
509 return HKS_SUCCESS;
510 }
511
ParseImportedKeyDecryptParams(const struct HksBlob * wrappedKeyData,uint32_t * partOffset,uint32_t totalBlobs,uint32_t * outKeyMaterialSize,struct HksBlob ** blobArray)512 static int32_t ParseImportedKeyDecryptParams(const struct HksBlob *wrappedKeyData, uint32_t *partOffset,
513 uint32_t totalBlobs, uint32_t *outKeyMaterialSize, struct HksBlob **blobArray)
514 {
515 uint32_t offset = *partOffset;
516 uint32_t blobIndex = 0;
517 int32_t ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
518 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get kek aad data failed!")
519
520 ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
521 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get kek nonce data failed!")
522
523 ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
524 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get kek aead tag data failed!")
525
526 struct HksBlob keyMatLenBlobPart = { 0, NULL };
527 ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, &keyMatLenBlobPart);
528 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get key material len failed!")
529
530 if (keyMatLenBlobPart.size != sizeof(uint32_t)) {
531 HKS_LOG_E("key material len part is invalid!");
532 return HKS_ERROR_INVALID_WRAPPED_FORMAT;
533 }
534
535 uint32_t keyMaterialSize = 0;
536 (void)memcpy_s((uint8_t *)&keyMaterialSize, sizeof(uint32_t), keyMatLenBlobPart.data, keyMatLenBlobPart.size);
537 if ((keyMaterialSize == 0) || (keyMaterialSize > MAX_KEY_SIZE)) {
538 HKS_LOG_E("key material size is invalid!");
539 return HKS_ERROR_INVALID_WRAPPED_FORMAT;
540 }
541
542 ret = HksGetBlobFromWrappedData(wrappedKeyData, offset++, totalBlobs, blobArray[blobIndex++]);
543 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get imported key encryption data failed!")
544
545 *partOffset = offset;
546 *outKeyMaterialSize = keyMaterialSize;
547 return HKS_SUCCESS;
548 }
549
DecryptImportedKeyWithKek(const struct HksBlob * wrappedKeyData,const struct HksBlob * kek,uint32_t * partOffset,struct HksBlob * outImportedKey)550 static int32_t DecryptImportedKeyWithKek(const struct HksBlob *wrappedKeyData, const struct HksBlob *kek,
551 uint32_t *partOffset, struct HksBlob *outImportedKey)
552 {
553 struct HksBlob kekAadPart = { 0, NULL };
554 struct HksBlob kekNoncePart = { 0, NULL };
555 struct HksBlob kekTagPart = { 0, NULL };
556 struct HksBlob originKeyEncDataPart = { 0, NULL };
557 struct HksBlob *blobArray[] = { &kekAadPart, &kekNoncePart, &kekTagPart, &originKeyEncDataPart };
558
559 uint32_t offset = *partOffset;
560 uint32_t keyMaterialSize = 0;
561 int32_t ret = ParseImportedKeyDecryptParams(wrappedKeyData, &offset, HKS_IMPORT_WRAPPED_KEY_TOTAL_BLOBS,
562 &keyMaterialSize, blobArray);
563 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "parse kek decrypt imported-key params failed!")
564
565 struct HksBlob originKey = { 0, NULL };
566 originKey.size = keyMaterialSize;
567 uint8_t *originKeyBuffer = (uint8_t *) HksMalloc(originKey.size);
568 HKS_IF_NULL_LOGE_RETURN(originKeyBuffer, HKS_ERROR_MALLOC_FAIL, "malloc originKeyBuffer memory failed!")
569
570 originKey.data = originKeyBuffer;
571
572 struct HksUsageSpec *decOriginKeyUsageSpec = (struct HksUsageSpec *)HksMalloc(sizeof(struct HksUsageSpec));
573 if (decOriginKeyUsageSpec == NULL) {
574 HKS_LOG_E("malloc originKeyBuffer memory failed!");
575 HKS_FREE(originKey.data);
576 return HKS_ERROR_MALLOC_FAIL;
577 }
578 (void)memset_s(decOriginKeyUsageSpec, sizeof(struct HksUsageSpec), 0, sizeof(struct HksUsageSpec));
579 uint32_t payloadSize = originKeyEncDataPart.size;
580 ret = BuildDecryptUsageSpecOfUnwrap(&kekAadPart, &kekNoncePart, &kekTagPart, payloadSize, decOriginKeyUsageSpec);
581 if (ret != HKS_SUCCESS) {
582 HKS_LOG_E("build decrypt wrapped data origin key usageSpec failed!");
583 HKS_FREE(originKey.data);
584 HksFreeUsageSpec(&decOriginKeyUsageSpec);
585 return ret;
586 }
587
588 ret = HksCryptoHalDecrypt(kek, decOriginKeyUsageSpec, &originKeyEncDataPart, &originKey);
589 HksFreeUsageSpec(&decOriginKeyUsageSpec);
590 if (ret != HKS_SUCCESS) {
591 HKS_LOG_E("decrypt importKey failed!");
592 HKS_FREE(originKey.data);
593 return ret;
594 }
595
596 outImportedKey->size = originKey.size;
597 outImportedKey->data = originKey.data;
598 *partOffset = offset;
599 return HKS_SUCCESS;
600 }
601
ClearAndFreeKeyBlobsIfNeed(struct HksBlob * peerPublicKey,struct HksBlob * agreeSharedSecret,struct HksBlob * originKey,struct HksBlob * kek)602 static void ClearAndFreeKeyBlobsIfNeed(struct HksBlob *peerPublicKey, struct HksBlob *agreeSharedSecret,
603 struct HksBlob *originKey, struct HksBlob *kek)
604 {
605 if (originKey->data != NULL) {
606 (void)memset_s(originKey->data, originKey->size, 0, originKey->size);
607 HKS_FREE(originKey->data);
608 }
609
610 if (kek->data != NULL) {
611 (void)memset_s(kek->data, kek->size, 0, kek->size);
612 HKS_FREE(kek->data);
613 }
614
615 if (agreeSharedSecret->data != NULL) {
616 (void)memset_s(agreeSharedSecret->data, agreeSharedSecret->size, 0, agreeSharedSecret->size);
617 HKS_FREE(agreeSharedSecret->data);
618 }
619
620 if (peerPublicKey->data != NULL) {
621 (void)memset_s(peerPublicKey->data, peerPublicKey->size, 0, peerPublicKey->size);
622 HKS_FREE(peerPublicKey->data);
623 }
624 }
625
CheckRsaKeyMaterialLen(uint32_t keyType,const struct HksBlob * key)626 static int32_t CheckRsaKeyMaterialLen(uint32_t keyType, const struct HksBlob *key)
627 {
628 if (key->size < sizeof(struct HksKeyMaterialRsa)) {
629 HKS_LOG_E("invalid import key size: %" LOG_PUBLIC "u", key->size);
630 return HKS_ERROR_INVALID_KEY_INFO;
631 }
632
633 struct HksKeyMaterialRsa *keyMaterial = (struct HksKeyMaterialRsa *)(key->data);
634
635 if ((keyMaterial->nSize > HKS_RSA_KEY_SIZE_4096) || (keyMaterial->nSize == 0) ||
636 (keyMaterial->dSize > HKS_RSA_KEY_SIZE_4096) || (keyMaterial->dSize == 0) ||
637 (keyMaterial->eSize > HKS_RSA_KEY_SIZE_4096)) {
638 HKS_LOG_E("invalid import key material n/d/e size");
639 return HKS_ERROR_INVALID_KEY_INFO;
640 }
641
642 if ((keyType == HKS_KEY_TYPE_KEY_PAIR) && (keyMaterial->eSize == 0)) {
643 HKS_LOG_E("invalid import key material e size while import key pair");
644 return HKS_ERROR_INVALID_KEY_INFO;
645 }
646
647 uint32_t keySize = sizeof(struct HksKeyMaterialRsa) + keyMaterial->nSize + keyMaterial->dSize + keyMaterial->eSize;
648 if (key->size < keySize) {
649 HKS_LOG_E("import key size[%" LOG_PUBLIC "u] smaller than keySize[%" LOG_PUBLIC "u]", key->size, keySize);
650 return HKS_ERROR_INVALID_KEY_INFO;
651 }
652
653 return HKS_SUCCESS;
654 }
655
AppendRsaPublicExponent(const struct HksBlob * key,struct HksBlob * outKey)656 static int32_t AppendRsaPublicExponent(const struct HksBlob *key, struct HksBlob *outKey)
657 {
658 /* key len has been checked by caller */
659 struct HksKeyMaterialRsa *keyMaterial = (struct HksKeyMaterialRsa *)(key->data);
660 uint32_t size = sizeof(struct HksKeyMaterialRsa) + keyMaterial->nSize + keyMaterial->dSize +
661 sizeof(g_defaultRsaPubExponent);
662
663 uint8_t *out = (uint8_t *)HksMalloc(size);
664 HKS_IF_NULL_LOGE_RETURN(out, HKS_ERROR_MALLOC_FAIL, "malloc failed")
665
666 uint32_t offset = 0;
667 do {
668 (void)memcpy_s(out + offset, size - offset, key->data, sizeof(struct HksKeyMaterialRsa));
669 offset += sizeof(struct HksKeyMaterialRsa);
670
671 struct HksKeyMaterialRsa *newkeyMaterial = (struct HksKeyMaterialRsa *)out;
672 newkeyMaterial->eSize = sizeof(g_defaultRsaPubExponent);
673
674 (void)memcpy_s(out + offset, size - offset, key->data + offset, keyMaterial->nSize);
675 offset += keyMaterial->nSize;
676
677 (void)memcpy_s(out + offset, size - offset, g_defaultRsaPubExponent, sizeof(g_defaultRsaPubExponent));
678
679 (void)memcpy_s(out + offset + sizeof(g_defaultRsaPubExponent), size - offset - sizeof(g_defaultRsaPubExponent),
680 key->data + offset, keyMaterial->dSize);
681 } while (0);
682
683 outKey->data = out;
684 outKey->size = size;
685 return HKS_SUCCESS;
686 }
687
GetRsaPrivateOrPairInnerFormat(uint32_t keyType,const struct HksBlob * key,struct HksBlob * outKey)688 static int32_t GetRsaPrivateOrPairInnerFormat(uint32_t keyType, const struct HksBlob *key, struct HksBlob *outKey)
689 {
690 int32_t ret = CheckRsaKeyMaterialLen(keyType, key);
691 HKS_IF_NOT_SUCC_RETURN(ret, ret)
692
693 struct HksKeyMaterialRsa *keyMaterial = (struct HksKeyMaterialRsa *)(key->data);
694 if ((keyType == HKS_KEY_TYPE_PRIVATE_KEY) && (keyMaterial->eSize == 0)) {
695 return AppendRsaPublicExponent(key, outKey);
696 }
697
698 return CopyToInnerKey(key, outKey);
699 }
700
GetCurve25519PrivateOrPairInnerFormat(uint8_t alg,uint32_t keyType,const struct HksBlob * key,struct HksBlob * outKey)701 static int32_t GetCurve25519PrivateOrPairInnerFormat(uint8_t alg, uint32_t keyType,
702 const struct HksBlob *key, struct HksBlob *outKey)
703 {
704 if (keyType == HKS_KEY_TYPE_KEY_PAIR) {
705 return CopyToInnerKey(key, outKey);
706 }
707
708 if (key->size != HKS_KEY_BYTES(HKS_CURVE25519_KEY_SIZE_256)) {
709 HKS_LOG_E("Invalid curve25519 private key size! key size = 0x%" LOG_PUBLIC "X", key->size);
710 return HKS_ERROR_INVALID_KEY_INFO;
711 }
712
713 uint32_t totalSize = sizeof(struct HksKeyMaterial25519) + key->size;
714 uint8_t *buffer = (uint8_t *)HksMalloc(totalSize);
715 HKS_IF_NULL_LOGE_RETURN(buffer, HKS_ERROR_MALLOC_FAIL, "malloc failed! %" LOG_PUBLIC "u", totalSize)
716
717 (void)memset_s(buffer, totalSize, 0, totalSize);
718
719 struct HksKeyMaterial25519 *curve25519Key = (struct HksKeyMaterial25519 *)buffer;
720 curve25519Key->keyAlg = (enum HksKeyAlg)alg;
721 curve25519Key->keySize = HKS_CURVE25519_KEY_SIZE_256;
722 curve25519Key->priKeySize = key->size; /* curve25519 private key */
723
724 uint32_t offset = sizeof(struct HksKeyMaterial25519);
725 (void)memcpy_s(buffer + offset, totalSize - offset, key->data, key->size);
726 outKey->data = buffer;
727 outKey->size = totalSize;
728 return HKS_SUCCESS;
729 }
730
GetPrivateOrPairInnerFormat(uint32_t keyType,const struct HksBlob * key,const struct HksParamSet * paramSet,struct HksBlob * outKey)731 static int32_t GetPrivateOrPairInnerFormat(uint32_t keyType, const struct HksBlob *key,
732 const struct HksParamSet *paramSet, struct HksBlob *outKey)
733 {
734 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT, "invalid key or outKey")
735
736 struct HksParam *algParam = NULL;
737 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
738 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL, "get alg param failed")
739
740 switch (algParam->uint32Param) {
741 case HKS_ALG_RSA:
742 return GetRsaPrivateOrPairInnerFormat(keyType, key, outKey);
743 case HKS_ALG_ECC:
744 case HKS_ALG_DSA:
745 case HKS_ALG_DH:
746 case HKS_ALG_SM2:
747 case HKS_ALG_HMAC:
748 case HKS_ALG_SM3:
749 case HKS_ALG_SM4:
750 case HKS_ALG_AES:
751 return CopyToInnerKey(key, outKey);
752 case HKS_ALG_ED25519:
753 case HKS_ALG_X25519:
754 return GetCurve25519PrivateOrPairInnerFormat(algParam->uint32Param, keyType, key, outKey);
755 default:
756 return HKS_ERROR_INVALID_ALGORITHM;
757 }
758 }
759
HksCoreImportKey(const struct HksBlob * keyAlias,const struct HksBlob * key,const struct HksParamSet * paramSet,struct HksBlob * keyOut)760 int32_t HksCoreImportKey(const struct HksBlob *keyAlias, const struct HksBlob *key,
761 const struct HksParamSet *paramSet, struct HksBlob *keyOut)
762 {
763 struct HksBlob innerKey = { 0, NULL };
764 struct HksParam *importKeyTypeParam = NULL;
765 int32_t ret = HksGetParam(paramSet, HKS_TAG_IMPORT_KEY_TYPE, &importKeyTypeParam);
766 if ((ret == HKS_SUCCESS) &&
767 ((importKeyTypeParam->uint32Param == HKS_KEY_TYPE_PRIVATE_KEY) ||
768 (importKeyTypeParam->uint32Param == HKS_KEY_TYPE_KEY_PAIR))) {
769 ret = GetPrivateOrPairInnerFormat(importKeyTypeParam->uint32Param, key, paramSet, &innerKey);
770 } else {
771 ret = CopyToInnerKey(key, &innerKey);
772 }
773 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "translate key to inner format failed, ret = %" LOG_PUBLIC "d", ret)
774
775 do {
776 ret = HksCoreCheckImportKeyParams(keyAlias, &innerKey, paramSet, keyOut);
777 HKS_IF_NOT_SUCC_BREAK(ret)
778
779 ret = HksBuildKeyBlob(keyAlias, HKS_KEY_FLAG_IMPORT_KEY, &innerKey, paramSet, keyOut);
780 } while (0);
781
782 (void)memset_s(innerKey.data, innerKey.size, 0, innerKey.size);
783 HKS_FREE_BLOB(innerKey);
784 return ret;
785 }
786
HksCoreImportWrappedKey(const struct HksBlob * keyAlias,const struct HksBlob * wrappingKey,const struct HksBlob * wrappedKeyData,const struct HksParamSet * paramSet,struct HksBlob * keyOut)787 int32_t HksCoreImportWrappedKey(const struct HksBlob *keyAlias, const struct HksBlob *wrappingKey,
788 const struct HksBlob *wrappedKeyData, const struct HksParamSet *paramSet, struct HksBlob *keyOut)
789 {
790 uint32_t unwrapSuite = 0;
791 int32_t ret = HksCoreCheckImportWrappedKeyParams(wrappingKey, wrappedKeyData, paramSet, keyOut, &unwrapSuite);
792 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check import wrapped key params failed!")
793
794 if ((unwrapSuite == HKS_UNWRAP_SUITE_SM2_SM4_128_CBC_PKCS7_WITH_VERIFY_DIG_SM3) ||
795 (unwrapSuite == HKS_UNWRAP_SUITE_SM2_SM4_128_CBC_PKCS7)) {
796 return HksSmImportWrappedKey(keyAlias, paramSet, wrappingKey, wrappedKeyData, keyOut);
797 }
798
799 struct HksBlob peerPublicKey = { 0, NULL };
800 struct HksBlob agreeSharedSecret = { 0, NULL };
801 struct HksBlob originKey = { 0, NULL };
802 struct HksBlob kek = { 0, NULL };
803 uint32_t partOffset = 0;
804
805 do {
806 /* 1. get peer public key and translate to inner format */
807 ret = GetPublicKeyInnerFormat(wrappingKey, wrappedKeyData, &peerPublicKey, &partOffset);
808 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get peer public key of inner format failed!")
809
810 /* 2. agree shared key with wrappingAlias's private key and peer public key */
811 ret = AgreeSharedSecretWithPeerPublicKey(wrappingKey, &peerPublicKey, unwrapSuite, &agreeSharedSecret,
812 paramSet);
813 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "agree share secret failed!")
814
815 /* 4. decrypt kek data with agreed secret */
816 ret = DecryptKekWithAgreeSharedSecret(wrappedKeyData, &agreeSharedSecret, &partOffset, &kek);
817 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "decrypt kek with agreed secret failed!")
818
819 /* 5. decrypt imported key data with kek */
820 ret = DecryptImportedKeyWithKek(wrappedKeyData, &kek, &partOffset, &originKey);
821 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "decrypt origin key failed!")
822
823 /* 6. call HksCoreImportKey to build key blob */
824 ret = HksCoreImportKey(keyAlias, &originKey, paramSet, keyOut);
825 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "import origin key failed!")
826 } while (0);
827
828 ClearAndFreeKeyBlobsIfNeed(&peerPublicKey, &agreeSharedSecret, &originKey, &kek);
829 return ret;
830 }
831
832 #endif /* _CUT_AUTHENTICATE_ */
833
HksCoreGenerateRandom(const struct HksParamSet * paramSet,struct HksBlob * random)834 int32_t HksCoreGenerateRandom(const struct HksParamSet *paramSet, struct HksBlob *random)
835 {
836 (void)paramSet;
837 return HksCryptoHalFillRandom(random);
838 }