1 /*
2 * Copyright (c) 2022-2023 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_three_stage.h"
23
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <stdint.h>
27
28 #include "hks_auth.h"
29 #include "hks_base_check.h"
30 #include "hks_client_service_adapter_common.h"
31 #include "hks_cmd_id.h"
32 #include "hks_common_check.h"
33 #include "hks_core_service.h"
34 #include "hks_crypto_adapter.h"
35 #include "hks_crypto_hal.h"
36 #include "hks_keyblob.h"
37 #include "hks_log.h"
38 #include "hks_mem.h"
39 #include "hks_param.h"
40 #include "hks_template.h"
41 #include "securec.h"
42
43 #ifndef _HARDWARE_ROOT_KEY_
44 #include "hks_rkc.h"
45 #endif
46
47 #ifndef _CUT_AUTHENTICATE_
48 #define CURVE25519_KEY_BYTE_SIZE HKS_KEY_BYTES(HKS_CURVE25519_KEY_SIZE_256)
49
50 #ifdef HKS_SUPPORT_ED25519_TO_X25519
51
52 #endif
53 #define HKS_RSA_OAEP_DIGEST_NUM 2
54 #define HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE 16
55 #define HKS_TEMP_SIZE 32
56 #define MAX_BUF_SIZE (5 * 1024 * 1024)
57
CheckRsaCipherData(bool isEncrypt,uint32_t keyLen,struct HksUsageSpec * usageSpec,const struct HksBlob * outData)58 static int32_t CheckRsaCipherData(bool isEncrypt, uint32_t keyLen, struct HksUsageSpec *usageSpec,
59 const struct HksBlob *outData)
60 {
61 uint32_t keySize = keyLen / HKS_BITS_PER_BYTE;
62 uint32_t padding = usageSpec->padding;
63 uint32_t digest = usageSpec->digest;
64
65 if (padding == HKS_PADDING_NONE) {
66 if (outData->size < keySize) {
67 HKS_LOG_E("outData buffer too small size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u",
68 outData->size, keySize);
69 return HKS_ERROR_BUFFER_TOO_SMALL;
70 }
71 } else if (padding == HKS_PADDING_OAEP) {
72 uint32_t digestLen;
73 int32_t ret = HksGetDigestLen(digest, &digestLen);
74 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "GetDigestLen failed, ret = %" LOG_PUBLIC "x", ret)
75
76 if (keySize <= (HKS_RSA_OAEP_DIGEST_NUM * digestLen + HKS_RSA_OAEP_DIGEST_NUM)) {
77 return HKS_ERROR_INVALID_KEY_FILE;
78 }
79
80 uint32_t size = keySize - HKS_RSA_OAEP_DIGEST_NUM * digestLen - HKS_RSA_OAEP_DIGEST_NUM;
81 if (isEncrypt) {
82 if (outData->size < keySize) {
83 HKS_LOG_E("encrypt, outData buffer too small size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u",
84 outData->size, keySize);
85 return HKS_ERROR_BUFFER_TOO_SMALL;
86 }
87 } else {
88 if (outData->size < size) {
89 HKS_LOG_E("decrypt, outData buffer too small size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u",
90 outData->size, keySize);
91 return HKS_ERROR_BUFFER_TOO_SMALL;
92 }
93 }
94 }
95
96 return HKS_SUCCESS;
97 }
98
CheckAesCipherAead(bool isEncrypt,const struct HksBlob * inData,const struct HksBlob * outData)99 static int32_t CheckAesCipherAead(bool isEncrypt, const struct HksBlob *inData,
100 const struct HksBlob *outData)
101 {
102 if (isEncrypt) {
103 if (outData->size < (inData->size + HKS_AE_TAG_LEN)) {
104 HKS_LOG_E("encrypt, out buffer too small size: %" LOG_PUBLIC "u, inSize: %" LOG_PUBLIC "u",
105 outData->size, inData->size);
106 return HKS_ERROR_BUFFER_TOO_SMALL;
107 }
108 } else {
109 if (outData->size < inData->size) {
110 HKS_LOG_E("decryptfinal, out buffer too small size: %" LOG_PUBLIC "u, inSize: %" LOG_PUBLIC "u",
111 outData->size, inData->size);
112 return HKS_ERROR_BUFFER_TOO_SMALL;
113 }
114 }
115
116 return HKS_SUCCESS;
117 }
118
CheckBlockCipherOther(bool isEncrypt,uint32_t padding,const struct HksBlob * inData,const struct HksBlob * outData)119 static int32_t CheckBlockCipherOther(bool isEncrypt, uint32_t padding, const struct HksBlob *inData,
120 const struct HksBlob *outData)
121 {
122 uint32_t paddingSize = 0;
123
124 if (isEncrypt) {
125 if (padding == HKS_PADDING_NONE) {
126 if (inData->size % HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE != 0) {
127 HKS_LOG_E("encrypt cbc no-padding, invalid inSize: %" LOG_PUBLIC "u", inData->size);
128 return HKS_ERROR_INVALID_ARGUMENT;
129 }
130 } else {
131 paddingSize = HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE - inData->size % HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE;
132 if (inData->size > (UINT32_MAX - paddingSize)) {
133 HKS_LOG_E("encrypt, invalid inData size: %" LOG_PUBLIC "u", inData->size);
134 return HKS_ERROR_INVALID_ARGUMENT;
135 }
136 }
137 if (outData->size < (inData->size + paddingSize)) {
138 HKS_LOG_E("encrypt, outData buffer too small size: %" LOG_PUBLIC "u, need: %" LOG_PUBLIC "u",
139 outData->size, inData->size + paddingSize);
140 return HKS_ERROR_BUFFER_TOO_SMALL;
141 }
142 } else {
143 if (outData->size < inData->size) {
144 HKS_LOG_E("decrypt, outData buffer too small size: %" LOG_PUBLIC "u, inDataSize: %" LOG_PUBLIC "u",
145 outData->size, inData->size);
146 return HKS_ERROR_BUFFER_TOO_SMALL;
147 }
148 }
149
150 return HKS_SUCCESS;
151 }
152
CheckBlockCipherData(bool isEncrypt,const struct HksUsageSpec * usageSpec,const struct HksBlob * inData,const struct HksBlob * outData)153 static int32_t CheckBlockCipherData(bool isEncrypt, const struct HksUsageSpec *usageSpec,
154 const struct HksBlob *inData, const struct HksBlob *outData)
155 {
156 const uint32_t padding = usageSpec->padding;
157 const uint32_t mode = usageSpec->mode;
158 const uint32_t alg = usageSpec->algType;
159 int32_t ret = HKS_ERROR_NOT_SUPPORTED;
160 if (((alg == HKS_ALG_AES) || (alg == HKS_ALG_SM4)) &&
161 ((mode == HKS_MODE_CBC) || (mode == HKS_MODE_CTR) || (mode == HKS_MODE_ECB))) {
162 ret = CheckBlockCipherOther(isEncrypt, padding, inData, outData);
163 } else if ((alg == HKS_ALG_AES) && ((mode == HKS_MODE_GCM) || (mode == HKS_MODE_CCM))) {
164 ret = CheckAesCipherAead(isEncrypt, inData, outData);
165 }
166
167 return ret;
168 }
169
HksCheckFinishOutSize(bool isEncrypt,struct HksParamSet * paramSet,const struct HksBlob * inData,const struct HksBlob * outData)170 static int32_t HksCheckFinishOutSize(bool isEncrypt, struct HksParamSet *paramSet,
171 const struct HksBlob *inData, const struct HksBlob *outData)
172 {
173 struct HksUsageSpec usageSpec = {0};
174 HksFillUsageSpec(paramSet, &usageSpec);
175 struct HksKeySpec cihperSpec = {0};
176 HksFillKeySpec(paramSet, &cihperSpec);
177 uint32_t alg = usageSpec.algType;
178
179 switch (alg) {
180 case HKS_ALG_RSA:
181 return CheckRsaCipherData(isEncrypt, cihperSpec.keyLen, &usageSpec, outData);
182 case HKS_ALG_AES:
183 return CheckBlockCipherData(isEncrypt, &usageSpec, inData, outData);
184 case HKS_ALG_SM4:
185 return CheckBlockCipherData(isEncrypt, &usageSpec, inData, outData);
186 default:
187 return HKS_ERROR_INVALID_ALGORITHM;
188 }
189 }
190
SignVerifyAuth(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet)191 static int32_t SignVerifyAuth(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet)
192 {
193 struct HksParam *algParam = NULL;
194 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
195 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "append sign/verify get alg param failed!")
196
197 if (algParam->uint32Param == HKS_ALG_ECC || algParam->uint32Param == HKS_ALG_SM2 ||
198 algParam->uint32Param == HKS_ALG_DSA) {
199 return HksThreeStageAuth(HKS_AUTH_ID_SIGN_VERIFY_ECC, keyNode);
200 } else if (algParam->uint32Param == HKS_ALG_RSA) {
201 struct HksParam *padding = NULL;
202 ret = HksGetParam(paramSet, HKS_TAG_PADDING, &padding);
203 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "append sign/verify get padding param failed!")
204 if (padding->uint32Param == HKS_PADDING_PSS) {
205 ret = HksCheckParamsetOneAndPatamsetTwoExist(keyNode->keyBlobParamSet, keyNode->runtimeParamSet,
206 HKS_TAG_RSA_PSS_SALT_LEN_TYPE);
207 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "HksCheckParamsetOneAndPatamsetTwoExist failed!")
208 }
209 return HksThreeStageAuth(HKS_AUTH_ID_SIGN_VERIFY_RSA, keyNode);
210 } else if (algParam->uint32Param == HKS_ALG_ED25519) {
211 return HksThreeStageAuth(HKS_AUTH_ID_SIGN_VERIFY_ED25519, keyNode);
212 }
213 return HKS_ERROR_INVALID_ALGORITHM;
214 }
215
AgreeAuth(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet)216 static int32_t AgreeAuth(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet)
217 {
218 struct HksParam *algParam = NULL;
219 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
220 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "append agree get alg param failed!")
221
222 if (algParam->uint32Param == HKS_ALG_ECDH || algParam->uint32Param == HKS_ALG_X25519 ||
223 algParam->uint32Param == HKS_ALG_DH || algParam->uint32Param == HKS_ALG_SM2) {
224 return HksThreeStageAuth(HKS_AUTH_ID_AGREE, keyNode);
225 }
226 return HKS_ERROR_INVALID_ALGORITHM;
227 }
228
HmacAuth(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet)229 static int32_t HmacAuth(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet)
230 {
231 struct HksParam *algParam = NULL;
232 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
233 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "append hmac get alg param failed!")
234
235 if (algParam->uint32Param == HKS_ALG_HMAC) {
236 return HksThreeStageAuth(HKS_AUTH_ID_MAC_HMAC, keyNode);
237 } else if (algParam->uint32Param == HKS_ALG_SM3) {
238 return HksThreeStageAuth(HKS_AUTH_ID_MAC_SM3, keyNode);
239 }
240 return HKS_ERROR_INVALID_ALGORITHM;
241 }
242
CipherAuth(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet)243 static int32_t CipherAuth(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet)
244 {
245 struct HksParam *algParam = NULL;
246 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
247 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "append cipher get alg param failed!")
248
249 if (algParam->uint32Param == HKS_ALG_AES) {
250 return HksThreeStageAuth(HKS_AUTH_ID_SYM_CIPHER, keyNode);
251 } else if ((algParam->uint32Param == HKS_ALG_RSA) || (algParam->uint32Param == HKS_ALG_SM2)) {
252 return HksThreeStageAuth(HKS_AUTH_ID_ASYM_CIPHER, keyNode);
253 } else if (algParam->uint32Param == HKS_ALG_SM4) {
254 return HksThreeStageAuth(HKS_AUTH_ID_SYM_CIPHER, keyNode);
255 }
256 return HKS_ERROR_INVALID_ALGORITHM;
257 }
258
GetCryptoCtx(const struct HuksKeyNode * keyNode)259 static void *GetCryptoCtx(const struct HuksKeyNode *keyNode)
260 {
261 struct HksParam *ctxParam = NULL;
262 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
263
264 return (ret != HKS_SUCCESS || ctxParam == NULL) ? NULL : (void *)(uintptr_t)ctxParam->uint64Param;
265 }
266
ClearCryptoCtx(const struct HuksKeyNode * keyNode)267 static void ClearCryptoCtx(const struct HuksKeyNode *keyNode)
268 {
269 struct HksParam *ctxParam = NULL;
270 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
271 if (ret != HKS_SUCCESS || ctxParam == NULL) {
272 return;
273 }
274
275 ctxParam->uint64Param = (uint64_t)(uintptr_t)NULL;
276 return;
277 }
278
SetCacheModeCtx(const struct HuksKeyNode * keyNode)279 static int32_t SetCacheModeCtx(const struct HuksKeyNode *keyNode)
280 {
281 struct HksParam *ctxParam = NULL;
282 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
283 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
284
285 struct HksBlob *tempData = (struct HksBlob *)HksMalloc(sizeof(struct HksBlob));
286 HKS_IF_NULL_LOGE_RETURN(tempData, HKS_ERROR_MALLOC_FAIL, "get cache mode ctx malloc fail.")
287
288 tempData->size = 0;
289 tempData->data = NULL;
290 ctxParam->uint64Param = (uint64_t)(uintptr_t)tempData;
291 return HKS_SUCCESS;
292 }
293
CopyNewCachedData(const struct HksBlob * cachedBlob,const struct HksBlob * inData,uint8_t * newData,uint32_t newSize)294 static int32_t CopyNewCachedData(const struct HksBlob *cachedBlob, const struct HksBlob *inData,
295 uint8_t *newData, uint32_t newSize)
296 {
297 if (cachedBlob->size != 0) {
298 if (memcpy_s(newData, newSize, cachedBlob->data, cachedBlob->size) != EOK) {
299 HKS_LOG_E("memcpy cached data failed");
300 return HKS_ERROR_INSUFFICIENT_MEMORY;
301 }
302 }
303 if (inData->size != 0) {
304 if (memcpy_s(newData + cachedBlob->size, newSize - cachedBlob->size, inData->data, inData->size) != EOK) {
305 HKS_LOG_E("memcpy in data failed");
306 return HKS_ERROR_INSUFFICIENT_MEMORY;
307 }
308 }
309 return HKS_SUCCESS;
310 }
311
GetNewCachedData(const struct HksBlob * cachedBlob,const struct HksBlob * inData,struct HksBlob * newBlob)312 static int32_t GetNewCachedData(const struct HksBlob *cachedBlob, const struct HksBlob *inData,
313 struct HksBlob *newBlob)
314 {
315 if ((cachedBlob->size > MAX_BUF_SIZE) || (inData->size > (MAX_BUF_SIZE - cachedBlob->size))) {
316 HKS_LOG_E("input data size too large, size = %" LOG_PUBLIC "u", inData->size);
317 return HKS_ERROR_INVALID_ARGUMENT;
318 }
319
320 uint32_t newSize = cachedBlob->size + inData->size;
321 uint8_t *newData = (uint8_t *)HksMalloc(newSize);
322 HKS_IF_NULL_LOGE_RETURN(newData, HKS_ERROR_MALLOC_FAIL, "update cache data malloc fail.")
323
324 int32_t ret = CopyNewCachedData(cachedBlob, inData, newData, newSize);
325 if (ret != HKS_SUCCESS) {
326 HKS_FREE_PTR(newData);
327 return ret;
328 }
329
330 newBlob->data = newData;
331 newBlob->size = newSize;
332 return HKS_SUCCESS;
333 }
334
UpdateCachedData(const struct HuksKeyNode * keyNode,const struct HksBlob * srcData)335 static int32_t UpdateCachedData(const struct HuksKeyNode *keyNode, const struct HksBlob *srcData)
336 {
337 struct HksParam *ctxParam = NULL;
338 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
339 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
340
341 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
342 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_BAD_STATE, "ctx is invalid: null!")
343
344 struct HksBlob *cachedData = (struct HksBlob *)ctx;
345 struct HksBlob *newCachedBlob = (struct HksBlob *)HksMalloc(sizeof(struct HksBlob));
346 HKS_IF_NULL_LOGE_RETURN(newCachedBlob, HKS_ERROR_MALLOC_FAIL, "malloc new blob failed")
347
348 ret = GetNewCachedData(cachedData, srcData, newCachedBlob);
349 if (ret != HKS_SUCCESS) {
350 HKS_LOG_E("get mew cached data failed, ret = %" LOG_PUBLIC "d", ret);
351 HKS_FREE_PTR(newCachedBlob);
352 return ret;
353 }
354
355 HKS_FREE_PTR(cachedData->data);
356 HKS_FREE_PTR(cachedData);
357 ctxParam->uint64Param = (uint64_t)(uintptr_t)newCachedBlob;
358 return HKS_SUCCESS;
359 }
360
FreeCachedData(struct HksBlob ** cachedData)361 static void FreeCachedData(struct HksBlob **cachedData)
362 {
363 if ((cachedData == NULL) || (*cachedData == NULL)) {
364 return;
365 }
366 if ((*cachedData)->data != NULL) {
367 (void)memset_s((*cachedData)->data, (*cachedData)->size, 0, (*cachedData)->size);
368 HKS_FREE_PTR((*cachedData)->data);
369 }
370 HKS_FREE_PTR(*cachedData);
371 }
372
FinishCachedData(const struct HuksKeyNode * keyNode,const struct HksBlob * srcData,struct HksBlob * outData)373 static int32_t FinishCachedData(const struct HuksKeyNode *keyNode, const struct HksBlob *srcData,
374 struct HksBlob *outData)
375 {
376 struct HksParam *ctxParam = NULL;
377 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
378 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
379
380 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
381 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_BAD_STATE, "ctx is invalid: null!")
382
383 struct HksBlob *cachedData = (struct HksBlob *)ctx;
384 ret = GetNewCachedData(cachedData, srcData, outData);
385 HKS_IF_NOT_SUCC_LOGE(ret, "get new cached data failed, ret = %" LOG_PUBLIC "d", ret)
386
387 FreeCachedData(&cachedData);
388 ctxParam->uint64Param = 0; /* clear ctx to NULL */
389 return ret;
390 }
391
CoreHashInit(const struct HuksKeyNode * keyNode,uint32_t alg)392 static int32_t CoreHashInit(const struct HuksKeyNode *keyNode, uint32_t alg)
393 {
394 struct HksParam *ctxParam = NULL;
395 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
396 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
397
398 void *ctx = NULL;
399
400 ret = HksCryptoHalHashInit(alg, &ctx);
401 if (ret != HKS_SUCCESS) {
402 HKS_LOG_E("hal hash init failed ret : %" LOG_PUBLIC "d", ret);
403 return ret;
404 }
405 ctxParam->uint64Param = (uint64_t)(uintptr_t)ctx;
406 return HKS_SUCCESS;
407 }
408
CoreHashUpdate(const struct HuksKeyNode * keyNode,const struct HksBlob * srcData)409 static int32_t CoreHashUpdate(const struct HuksKeyNode *keyNode, const struct HksBlob *srcData)
410 {
411 void *ctx = GetCryptoCtx(keyNode);
412 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_BAD_STATE, "ctx is invalid: null!")
413
414 int32_t ret = HksCryptoHalHashUpdate(srcData, ctx);
415 HKS_IF_NOT_SUCC_LOGE(ret, "hal hash update failed ret : %" LOG_PUBLIC "d", ret)
416
417 return ret;
418 }
419
CoreHashFinish(const struct HuksKeyNode * keyNode,const struct HksBlob * srcData,struct HksBlob * outData)420 static int32_t CoreHashFinish(const struct HuksKeyNode *keyNode, const struct HksBlob *srcData,
421 struct HksBlob *outData)
422 {
423 struct HksParam *ctxParam = NULL;
424 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
425 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
426
427 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
428 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_BAD_STATE, "ctx is invalid: null!")
429
430 outData->size = MAX_HASH_SIZE;
431 outData->data = (uint8_t *)HksMalloc(MAX_HASH_SIZE);
432 HKS_IF_NULL_LOGE_RETURN(outData->data, HKS_ERROR_MALLOC_FAIL, "malloc fail.")
433
434 ret = HksCryptoHalHashFinal(srcData, &ctx, outData);
435 if (ret != HKS_SUCCESS) {
436 HKS_LOG_E("hal hash final failed ret : %" LOG_PUBLIC "d", ret);
437 HKS_FREE_BLOB(*outData);
438 }
439
440 ctxParam->uint64Param = 0; /* clear ctx to NULL */
441 return ret;
442 }
443
CheckSignVerifyParams(const struct HuksKeyNode * keyNode,const struct HksBlob * outData)444 static int32_t CheckSignVerifyParams(const struct HuksKeyNode *keyNode, const struct HksBlob *outData)
445 {
446 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(outData), HKS_ERROR_INVALID_ARGUMENT, "invalid outData")
447
448 struct HksParam *tmpParam = NULL;
449 int32_t ret = HksGetParam(keyNode->keyBlobParamSet, HKS_TAG_ALGORITHM, &tmpParam);
450 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get alg from keyNode failed!")
451
452 uint32_t alg = tmpParam->uint32Param;
453
454 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_PURPOSE, &tmpParam);
455 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get purpoes from keyNode failed!")
456
457 uint32_t purpose = tmpParam->uint32Param;
458
459 ret = HksGetParam(keyNode->keyBlobParamSet, HKS_TAG_KEY_SIZE, &tmpParam);
460 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get key size from keyNode failed!")
461
462 uint32_t keySize = tmpParam->uint32Param;
463
464 ret = HksCheckSignature((purpose == HKS_KEY_PURPOSE_SIGN) ? HKS_CMD_ID_SIGN : HKS_CMD_ID_VERIFY,
465 alg, keySize, outData);
466 HKS_IF_NOT_SUCC_LOGE(ret, "check signature failed!")
467
468 return ret;
469 }
470
CoreSignVerify(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData)471 static int32_t CoreSignVerify(const struct HuksKeyNode *keyNode, const struct HksBlob *inData,
472 struct HksBlob *outData)
473 {
474 int32_t ret = CheckSignVerifyParams(keyNode, outData);
475 HKS_IF_NOT_SUCC_RETURN(ret, ret)
476
477 struct HksBlob rawKey = { 0, NULL };
478 ret = HksGetRawKey(keyNode->keyBlobParamSet, &rawKey);
479 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "SignVerify get raw key failed!")
480
481 struct HksUsageSpec usageSpec;
482 (void)memset_s(&usageSpec, sizeof(struct HksUsageSpec), 0, sizeof(struct HksUsageSpec));
483 HksFillUsageSpec(keyNode->runtimeParamSet, &usageSpec);
484 SetRsaPssSaltLenType(keyNode->runtimeParamSet, &usageSpec);
485
486 if (usageSpec.purpose == HKS_KEY_PURPOSE_SIGN) {
487 ret = HksCryptoHalSign(&rawKey, &usageSpec, inData, outData);
488 } else {
489 ret = HksCryptoHalVerify(&rawKey, &usageSpec, inData, outData);
490 }
491 HKS_IF_NOT_SUCC_LOGE(ret, "SignVerify Finish failed, purpose = 0x%" LOG_PUBLIC "x, ret = %" LOG_PUBLIC "d",
492 usageSpec.purpose, ret)
493
494 (void)memset_s(rawKey.data, rawKey.size, 0, rawKey.size);
495 HKS_FREE_PTR(rawKey.data);
496 return ret;
497 }
498
FreeSignVerifyCtx(const struct HuksKeyNode * keyNode)499 static void FreeSignVerifyCtx(const struct HuksKeyNode *keyNode)
500 {
501 struct HksParam *ctxParam = NULL;
502 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
503 if (ret != HKS_SUCCESS) {
504 HKS_LOG_E("get ctx from keyNode failed!");
505 return;
506 }
507 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
508 if (ctx == NULL) {
509 return;
510 }
511
512 struct HksParam *algParam = NULL;
513 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
514 if (ret != HKS_SUCCESS) {
515 HKS_LOG_E("append cipher get alg param failed!");
516 return;
517 }
518 struct HksParam *digestParam = NULL;
519 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_DIGEST, &digestParam);
520 if (ret != HKS_SUCCESS) {
521 HKS_LOG_E("append cipher get digest param failed!");
522 return;
523 }
524 if (HksCheckNeedCache(algParam->uint32Param, digestParam->uint32Param) == HKS_SUCCESS) {
525 struct HksBlob *cachedData = (struct HksBlob *)ctx;
526 FreeCachedData(&cachedData);
527 } else {
528 HksCryptoHalHashFreeCtx(&ctx);
529 }
530
531 ctxParam->uint64Param = 0; /* clear ctx to NULL */
532 }
533
CoreCipherInit(const struct HuksKeyNode * keyNode)534 static int32_t CoreCipherInit(const struct HuksKeyNode *keyNode)
535 {
536 struct HksParam *ctxParam = NULL;
537 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
538 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
539
540 struct HksParam *purposeParam = NULL;
541 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_PURPOSE, &purposeParam);
542 HKS_IF_NOT_SUCC_RETURN(ret, HKS_ERROR_CHECK_GET_PURPOSE_FAIL)
543
544 struct HksBlob rawKey = { 0, NULL };
545 struct HksUsageSpec *usageSpec = NULL;
546 do {
547 uint8_t tmpData[HKS_TEMP_SIZE] = {0};
548 struct HksBlob tmpInData = { HKS_TEMP_SIZE, tmpData };
549 bool isEncrypt = (purposeParam->uint32Param == HKS_KEY_PURPOSE_ENCRYPT) ? true : false;
550 ret = HksBuildCipherUsageSpec(keyNode->runtimeParamSet, isEncrypt, &tmpInData, &usageSpec);
551 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "build cipher usage failed")
552
553 ret = HksGetRawKey(keyNode->keyBlobParamSet, &rawKey);
554 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "cipher get raw key failed")
555
556 void *ctx = NULL;
557 if (purposeParam->uint32Param == HKS_KEY_PURPOSE_ENCRYPT) {
558 ret = HksCryptoHalEncryptInit(&rawKey, usageSpec, &ctx);
559 } else {
560 ret = HksCryptoHalDecryptInit(&rawKey, usageSpec, &ctx);
561 }
562 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "cipher ctx init failed, ret = %" LOG_PUBLIC "d", ret)
563 ctxParam->uint64Param = (uint64_t)(uintptr_t)ctx;
564 } while (0);
565
566 if (rawKey.data != NULL) {
567 (void)memset_s(rawKey.data, rawKey.size, 0, rawKey.size);
568 }
569 HKS_FREE_PTR(rawKey.data);
570 HksFreeUsageSpec(&usageSpec);
571 return ret;
572 }
573
CoreCipherUpdate(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)574 static int32_t CoreCipherUpdate(const struct HuksKeyNode *keyNode, const struct HksBlob *inData,
575 struct HksBlob *outData, uint32_t alg)
576 {
577 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(outData), HKS_ERROR_INVALID_ARGUMENT, "invalid outData")
578
579 if (outData->size < inData->size) {
580 HKS_LOG_E("cipher update, out buffer too small size: %" LOG_PUBLIC "u, inSize: %" LOG_PUBLIC "u",
581 outData->size, inData->size);
582 return HKS_ERROR_BUFFER_TOO_SMALL;
583 }
584
585 void *ctx = GetCryptoCtx(keyNode);
586 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is invalid: null!")
587
588 struct HksParam *purposeParam = NULL;
589 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_PURPOSE, &purposeParam);
590 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "append cipher get purpose param failed!")
591
592 if (purposeParam->uint32Param == HKS_KEY_PURPOSE_ENCRYPT) {
593 ret = HksCryptoHalEncryptUpdate(inData, ctx, outData, alg);
594 } else {
595 ret = HksCryptoHalDecryptUpdate(inData, ctx, outData, alg);
596 }
597 HKS_IF_NOT_SUCC_LOGE(ret, "cipher update failed! ret : %" LOG_PUBLIC "d", ret)
598 return ret;
599 }
600
CoreAesEncryptFinish(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)601 static int32_t CoreAesEncryptFinish(const struct HuksKeyNode *keyNode,
602 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
603 {
604 struct HksBlob tag = { 0, NULL };
605 int32_t ret = HksGetEncryptAeTag(keyNode->runtimeParamSet, inData, outData, &tag);
606 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "cipher encrypt get ae tag failed!")
607
608 ret = HksCheckFinishOutSize(true, keyNode->runtimeParamSet, inData, outData);
609 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "aes encrypt finish check data size failed")
610
611 struct HksParam *ctxParam = NULL;
612 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
613 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get ctx from keyNode failed!")
614
615 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
616 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is invalid: null!")
617
618 ret = HksCryptoHalEncryptFinal(inData, &ctx, outData, &tag, alg);
619 if (ret != HKS_SUCCESS) {
620 HKS_LOG_E("aes encrypt Finish failed! ret : %" LOG_PUBLIC "d", ret);
621 ctxParam->uint64Param = 0; /* clear ctx to NULL */
622 return ret;
623 }
624
625 outData->size += tag.size;
626 ctxParam->uint64Param = 0; /* clear ctx to NULL */
627 return HKS_SUCCESS;
628 }
629
CoreAesDecryptFinish(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)630 static int32_t CoreAesDecryptFinish(const struct HuksKeyNode *keyNode,
631 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
632 {
633 bool isAes = false;
634 bool isAeMode = false;
635 int32_t ret = HksCheckAesAeMode(keyNode->runtimeParamSet, &isAes, &isAeMode);
636 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get aeMode failed!")
637
638 struct HksBlob tag = { 0, NULL };
639 if (isAes && isAeMode) {
640 struct HksParam *tagParam = NULL;
641 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_AE_TAG, &tagParam);
642 if (ret != HKS_SUCCESS || tagParam == NULL) {
643 HKS_LOG_E("get tag failed!");
644 return ret;
645 }
646 tag = tagParam->blob;
647 }
648
649 ret = HksCheckFinishOutSize(false, keyNode->runtimeParamSet, inData, outData);
650 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "aes decrypt finish check data size failed")
651
652 struct HksParam *ctxParam = NULL;
653 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
654 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get ctx from keyNode failed!")
655
656 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
657 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is invalid: null!")
658
659 ret = HksCryptoHalDecryptFinal(inData, &ctx, outData, &tag, alg);
660 HKS_IF_NOT_SUCC_LOGE(ret, "cipher DecryptFinish failed! ret : %" LOG_PUBLIC "d", ret)
661
662 ctxParam->uint64Param = 0; /* clear ctx to NULL */
663 return ret;
664 }
665
666
CoreSm4EncryptFinish(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)667 static int32_t CoreSm4EncryptFinish(const struct HuksKeyNode *keyNode,
668 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
669 {
670 int32_t ret = HksCheckFinishOutSize(true, keyNode->runtimeParamSet, inData, outData);
671 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "aes encrypt finish check data size failed")
672
673 struct HksParam *ctxParam = NULL;
674 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
675 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get ctx from keyNode failed!")
676
677 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
678 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is invalid: null!")
679
680 ret = HksCryptoHalEncryptFinal(inData, &ctx, outData, NULL, alg);
681 if (ret != HKS_SUCCESS) {
682 HKS_LOG_E("aes encrypt Finish failed! ret : %" LOG_PUBLIC "d", ret);
683 ctxParam->uint64Param = 0; /* clear ctx to NULL */
684 return ret;
685 }
686
687 ctxParam->uint64Param = 0; /* clear ctx to NULL */
688 return HKS_SUCCESS;
689 }
690
CoreSm4DecryptFinish(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)691 static int32_t CoreSm4DecryptFinish(const struct HuksKeyNode *keyNode,
692 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
693 {
694 int32_t ret = HksCheckFinishOutSize(false, keyNode->runtimeParamSet, inData, outData);
695 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "sm4 decrypt finish check data size failed")
696
697 struct HksParam *ctxParam = NULL;
698 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
699 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get ctx from keyNode failed!")
700
701 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
702 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is invalid: null!")
703
704 ret = HksCryptoHalDecryptFinal(inData, &ctx, outData, NULL, alg);
705 HKS_IF_NOT_SUCC_LOGE(ret, "cipher DecryptFinish failed! ret : %" LOG_PUBLIC "d", ret)
706
707 ctxParam->uint64Param = 0; /* clear ctx to NULL */
708 return ret;
709 }
710
RsaCipherFinish(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData)711 static int32_t RsaCipherFinish(const struct HuksKeyNode *keyNode, const struct HksBlob *inData,
712 struct HksBlob *outData)
713 {
714 HKS_LOG_E("rsa inData.size = %" LOG_PUBLIC "u", inData->size);
715 struct HksBlob rawKey = { 0, NULL };
716 int32_t ret = HksGetRawKey(keyNode->keyBlobParamSet, &rawKey);
717 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "SignVerify get raw key failed!")
718
719 struct HksUsageSpec usageSpec;
720 (void)memset_s(&usageSpec, sizeof(struct HksUsageSpec), 0, sizeof(struct HksUsageSpec));
721 HksFillUsageSpec(keyNode->runtimeParamSet, &usageSpec);
722
723 bool isEncrypt = (usageSpec.purpose == HKS_KEY_PURPOSE_ENCRYPT) ? true : false;
724 ret = HksCheckFinishOutSize(isEncrypt, keyNode->runtimeParamSet, inData, outData);
725 if (ret != HKS_SUCCESS) {
726 HKS_LOG_E("rsa cipher finish check data size failed");
727 (void)memset_s(rawKey.data, rawKey.size, 0, rawKey.size);
728 HKS_FREE_PTR(rawKey.data);
729 return ret;
730 }
731
732 if (usageSpec.purpose == HKS_KEY_PURPOSE_ENCRYPT) {
733 struct HksBlob tag = { 0, NULL };
734 ret = HksCryptoHalEncrypt(&rawKey, &usageSpec, inData, outData, &tag);
735 } else {
736 ret = HksCryptoHalDecrypt(&rawKey, &usageSpec, inData, outData);
737 }
738 HKS_IF_NOT_SUCC_LOGE(ret, "rsa cipher Finish failed, purpose = 0x%" LOG_PUBLIC "x, ret = %" LOG_PUBLIC "d",
739 usageSpec.purpose, ret)
740
741 (void)memset_s(rawKey.data, rawKey.size, 0, rawKey.size);
742 HKS_FREE_PTR(rawKey.data);
743 return ret;
744 }
745
CoreRsaCipherFinish(const struct HuksKeyNode * keyNode,const struct HksBlob * inData,struct HksBlob * outData)746 static int32_t CoreRsaCipherFinish(const struct HuksKeyNode *keyNode, const struct HksBlob *inData,
747 struct HksBlob *outData)
748 {
749 struct HksBlob tempInData = { 0, NULL };
750 int32_t ret = FinishCachedData(keyNode, inData, &tempInData);
751 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get rsa cipher cached data faile")
752
753 ret = RsaCipherFinish(keyNode, &tempInData, outData);
754 HKS_FREE_BLOB(tempInData);
755 return ret;
756 }
757
FreeCryptoCtx(const struct HuksKeyNode * keyNode,uint32_t alg)758 static void FreeCryptoCtx(const struct HuksKeyNode *keyNode, uint32_t alg)
759 {
760 struct HksParam *ctxParam = NULL;
761 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
762 if (ret != HKS_SUCCESS) {
763 HKS_LOG_E("get ctx from keyNode failed!");
764 return;
765 }
766 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
767 if (ctx == NULL) {
768 return;
769 }
770
771 if (alg == HKS_ALG_AES || alg == HKS_ALG_SM4) {
772 HksCryptoHalEncryptFreeCtx(&ctx, alg);
773 } else {
774 struct HksBlob *cachedData = (struct HksBlob *)ctx;
775 FreeCachedData(&cachedData);
776 }
777 ctxParam->uint64Param = 0; /* clear ctx to NULL */
778 }
779
GetRawkey(const struct HuksKeyNode * keyNode,struct HksBlob * rawKey)780 static int32_t GetRawkey(const struct HuksKeyNode *keyNode, struct HksBlob *rawKey)
781 {
782 if (GetCryptoCtx(keyNode) != NULL) {
783 HKS_LOG_E("avoid running into this function multiple times!");
784 return HKS_FAILURE;
785 }
786
787 int32_t ret = HksThreeStageAuth(HKS_AUTH_ID_DERIVE, keyNode);
788 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "derive auth failed!")
789
790 ret = HksGetRawKey(keyNode->keyBlobParamSet, rawKey);
791 HKS_IF_NOT_SUCC_LOGE(ret, "Derive get raw key failed!")
792
793 return ret;
794 }
795
FreeOutBlob(struct HksBlob ** out)796 static void FreeOutBlob(struct HksBlob **out)
797 {
798 if ((out == NULL) || (*out == NULL)) {
799 return;
800 }
801 HKS_FREE_PTR((*out)->data);
802 HKS_FREE_PTR(*out);
803 }
804
ConstructDervieBlob(const struct HksParamSet * paramSet,struct HksBlob ** out)805 static int32_t ConstructDervieBlob(const struct HksParamSet *paramSet, struct HksBlob **out)
806 {
807 struct HksParam *deriveSizeParam = NULL;
808 int32_t ret = HksGetParam(paramSet, HKS_TAG_DERIVE_KEY_SIZE, &deriveSizeParam);
809 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ARGUMENT, "get derive size failed, ret = %" LOG_PUBLIC "d", ret)
810
811 uint32_t deriveSize = deriveSizeParam->uint32Param;
812 if ((deriveSize == 0) || (deriveSize > MAX_OUT_BLOB_SIZE)) {
813 HKS_LOG_E("derive size invalid, size = %" LOG_PUBLIC "u", deriveSize);
814 return HKS_ERROR_INVALID_ARGUMENT;
815 }
816
817 struct HksBlob *tempOut = (struct HksBlob *)HksMalloc(sizeof(struct HksBlob));
818 HKS_IF_NULL_LOGE_RETURN(tempOut, HKS_ERROR_MALLOC_FAIL, "construct derive blob malloc failed")
819
820 tempOut->data = (uint8_t *)HksMalloc(deriveSize);
821 if (tempOut->data == NULL) {
822 HKS_FREE_PTR(tempOut);
823 HKS_LOG_E("malloc out derive blob data failed.");
824 return HKS_ERROR_MALLOC_FAIL;
825 }
826 tempOut->size = deriveSize;
827 *out = tempOut;
828 return HKS_SUCCESS;
829 }
830
ConstructAgreeBlob(struct HksBlob ** agreeOut)831 static int32_t ConstructAgreeBlob(struct HksBlob **agreeOut)
832 {
833 struct HksBlob *agreeTemp = (struct HksBlob *)HksMalloc(sizeof(struct HksBlob));
834 HKS_IF_NULL_LOGE_RETURN(agreeTemp, HKS_ERROR_MALLOC_FAIL, "malloc agreeTemp failed.")
835
836 agreeTemp->size = MAX_KEY_SIZE;
837 agreeTemp->data = (uint8_t *)HksMalloc(MAX_KEY_SIZE);
838 if (agreeTemp->data == NULL) {
839 HKS_LOG_E("malloc agreeTemp data failed.");
840 HKS_FREE_PTR(agreeTemp);
841 return HKS_ERROR_MALLOC_FAIL;
842 }
843 *agreeOut = agreeTemp;
844 return HKS_SUCCESS;
845 }
846
847
HksCoreSignVerifyThreeStageInit(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)848 int32_t HksCoreSignVerifyThreeStageInit(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
849 uint32_t alg)
850 {
851 (void)paramSet;
852 int32_t ret = SignVerifyAuth(keyNode, keyNode->runtimeParamSet);
853 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "HksCoreSignVerifyThreeStageInit SignAuth fail ret : %" LOG_PUBLIC "d", ret)
854
855 struct HksParam *algParam = NULL;
856 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
857 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
858 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_ALGORITHM)
859
860 uint32_t digest = alg; // In signature or verify scenario, alg represents digest. See code {GetPurposeAndAlgorithm}
861 HKS_LOG_I("Init cache or hash init.");
862 if (HksCheckNeedCache(algParam->uint32Param, digest) == HKS_SUCCESS) {
863 return SetCacheModeCtx(keyNode);
864 } else {
865 return CoreHashInit(keyNode, alg);
866 }
867 }
868
HksCoreSignVerifyThreeStageUpdate(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * signature,uint32_t alg)869 int32_t HksCoreSignVerifyThreeStageUpdate(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
870 const struct HksBlob *srcData, struct HksBlob *signature, uint32_t alg)
871 {
872 (void)signature;
873 (void)alg;
874 (void)paramSet;
875
876 struct HksParam *algParam = NULL;
877 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
878 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
879 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_ALGORITHM)
880
881 uint32_t digest = alg; // In signature or verify scenario, alg represents digest. See code {GetPurposeAndAlgorithm}
882 HKS_LOG_I("Update cache or hash update.");
883 if (HksCheckNeedCache(algParam->uint32Param, digest) == HKS_SUCCESS) {
884 return UpdateCachedData(keyNode, srcData);
885 } else {
886 return CoreHashUpdate(keyNode, srcData);
887 }
888 }
889
HksCoreSignVerifyThreeStageFinish(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)890 int32_t HksCoreSignVerifyThreeStageFinish(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
891 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
892 {
893 (void)paramSet;
894 (void)alg;
895
896 struct HksBlob message = { 0, NULL };
897 struct HksParam *purposeParam = NULL;
898 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_PURPOSE, &purposeParam);
899 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_PURPOSE_FAIL,
900 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_PURPOSE)
901
902 if (purposeParam->uint32Param == HKS_KEY_PURPOSE_SIGN) { /* inData indicates signature when processing verify */
903 message.data = inData->data;
904 message.size = inData->size;
905 }
906
907 struct HksParam *algParam = NULL;
908 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
909 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
910 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_ALGORITHM)
911
912 uint32_t digest = alg; // In signature or verify scenario, alg represents digest. See code {GetPurposeAndAlgorithm}
913
914 struct HksBlob signVerifyData = { 0, NULL };
915 if (HksCheckNeedCache(algParam->uint32Param, digest) == HKS_SUCCESS) {
916 ret = FinishCachedData(keyNode, &message, &signVerifyData);
917 } else {
918 ret = CoreHashFinish(keyNode, &message, &signVerifyData);
919 }
920 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "signVerify Finish get Data failed, ret = %" LOG_PUBLIC "d", ret)
921
922 /* inData indicates signature when processing verify */
923 ret = CoreSignVerify(keyNode, &signVerifyData,
924 (purposeParam->uint32Param == HKS_KEY_PURPOSE_SIGN) ? outData : (struct HksBlob *)inData);
925 HKS_FREE_BLOB(signVerifyData);
926 return ret;
927 }
928
HksCoreSignVerifyThreeStageAbort(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)929 int32_t HksCoreSignVerifyThreeStageAbort(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
930 uint32_t alg)
931 {
932 (void)paramSet;
933 (void)alg;
934
935 FreeSignVerifyCtx(keyNode);
936 return HKS_SUCCESS;
937 }
938
HksCoreCryptoThreeStageInit(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)939 int32_t HksCoreCryptoThreeStageInit(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
940 uint32_t alg)
941 {
942 (void)alg;
943
944 int32_t ret = CipherAuth(keyNode, paramSet);
945 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "cipher init failed, ret = %" LOG_PUBLIC "d", ret)
946
947 struct HksParam *algParam = NULL;
948 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
949 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
950 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_ALGORITHM)
951
952 HKS_LOG_I("Init cache or cipher init.");
953
954 if ((algParam->uint32Param == HKS_ALG_RSA) || (algParam->uint32Param == HKS_ALG_SM2)) {
955 return SetCacheModeCtx(keyNode);
956 } else if (algParam->uint32Param == HKS_ALG_AES) {
957 return CoreCipherInit(keyNode);
958 } else if (algParam->uint32Param == HKS_ALG_SM4) {
959 return CoreCipherInit(keyNode);
960 } else {
961 return HKS_ERROR_INVALID_ALGORITHM;
962 }
963 }
964
HksCoreCryptoThreeStageUpdate(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)965 int32_t HksCoreCryptoThreeStageUpdate(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
966 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
967 {
968 (void)paramSet;
969 struct HksParam *algParam = NULL;
970 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
971 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
972 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_ALGORITHM)
973
974 if (algParam->uint32Param == HKS_ALG_RSA) {
975 return UpdateCachedData(keyNode, inData);
976 } else if (algParam->uint32Param == HKS_ALG_AES) {
977 return CoreCipherUpdate(keyNode, inData, outData, alg);
978 } else if (algParam->uint32Param == HKS_ALG_SM4) {
979 return CoreCipherUpdate(keyNode, inData, outData, alg);
980 } else {
981 return HKS_ERROR_INVALID_ALGORITHM;
982 }
983 }
984
HksCoreEncryptThreeStageFinish(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)985 int32_t HksCoreEncryptThreeStageFinish(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
986 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
987 {
988 (void)paramSet;
989 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(outData), HKS_ERROR_INVALID_ARGUMENT, "invalid outData")
990
991 struct HksParam *algParam = NULL;
992 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
993 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
994 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_ALGORITHM)
995
996 if (algParam->uint32Param == HKS_ALG_RSA) {
997 return CoreRsaCipherFinish(keyNode, inData, outData);
998 } else if (algParam->uint32Param == HKS_ALG_AES) {
999 return CoreAesEncryptFinish(keyNode, inData, outData, alg);
1000 } else if (algParam->uint32Param == HKS_ALG_SM4) {
1001 return CoreSm4EncryptFinish(keyNode, inData, outData, alg);
1002 } else {
1003 return HKS_ERROR_INVALID_ALGORITHM;
1004 }
1005 }
1006
HksCoreDecryptThreeStageFinish(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)1007 int32_t HksCoreDecryptThreeStageFinish(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1008 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
1009 {
1010 (void)paramSet;
1011 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(outData), HKS_ERROR_INVALID_ARGUMENT, "invalid outData")
1012
1013 struct HksParam *algParam = NULL;
1014 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_ALGORITHM, &algParam);
1015 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
1016 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_ALGORITHM)
1017
1018 if (algParam->uint32Param == HKS_ALG_RSA) {
1019 return CoreRsaCipherFinish(keyNode, inData, outData);
1020 } else if (algParam->uint32Param == HKS_ALG_AES) {
1021 return CoreAesDecryptFinish(keyNode, inData, outData, alg);
1022 } else if (algParam->uint32Param == HKS_ALG_SM4) {
1023 return CoreSm4DecryptFinish(keyNode, inData, outData, alg);
1024 } else {
1025 return HKS_ERROR_INVALID_ALGORITHM;
1026 }
1027 }
1028
HksCoreCryptoThreeStageAbort(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)1029 int32_t HksCoreCryptoThreeStageAbort(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1030 uint32_t alg)
1031 {
1032 (void)paramSet;
1033 FreeCryptoCtx(keyNode, alg);
1034 return HKS_SUCCESS;
1035 }
1036
HksCoreDeriveThreeStageInit(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)1037 int32_t HksCoreDeriveThreeStageInit(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1038 uint32_t alg)
1039 {
1040 (void)keyNode;
1041 (void)paramSet;
1042 (void)alg;
1043
1044 HKS_LOG_D("HksCoreDeriveThreeStageInit start");
1045 return HKS_SUCCESS;
1046 }
1047
HksCoreDeriveThreeStageUpdate(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * derive,uint32_t alg)1048 int32_t HksCoreDeriveThreeStageUpdate(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1049 const struct HksBlob *srcData, struct HksBlob *derive, uint32_t alg)
1050 {
1051 (void)srcData;
1052 (void)alg;
1053 (void)derive;
1054 (void)paramSet;
1055 struct HksParam *ctxParam = NULL;
1056 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
1057 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
1058
1059 struct HksBlob rawKey = { 0, NULL };
1060 do {
1061 ret = GetRawkey(keyNode, &rawKey);
1062 HKS_IF_NOT_SUCC_BREAK(ret)
1063
1064 struct HksBlob *deriveBlob = NULL;
1065 ret = ConstructDervieBlob(keyNode->runtimeParamSet, &deriveBlob);
1066 HKS_IF_NOT_SUCC_BREAK(ret)
1067
1068 struct HksKeyDerivationParam derParam = { { 0, NULL }, { 0, NULL }, 0, 0 };
1069 struct HksKeySpec derivationSpec = { 0, 0, &derParam };
1070 HksFillKeySpec(keyNode->runtimeParamSet, &derivationSpec);
1071 HksFillKeyDerivationParam(keyNode->runtimeParamSet, &derParam);
1072
1073 ret = HksCryptoHalDeriveKey(&rawKey, &derivationSpec, deriveBlob);
1074 if (ret != HKS_SUCCESS) {
1075 HKS_LOG_E("HksCryptoHalDeriveKey fail");
1076 FreeOutBlob(&deriveBlob);
1077 break;
1078 }
1079
1080 ctxParam->uint64Param = (uint64_t)(uintptr_t)deriveBlob;
1081 } while (0);
1082
1083 if (rawKey.data != NULL) {
1084 (void)memset_s(rawKey.data, rawKey.size, 0, rawKey.size);
1085 }
1086 HKS_FREE_PTR(rawKey.data);
1087
1088 return ret;
1089 }
1090
DoBuildKeyBlobOrGetOutDataAction(const struct HksParamSet * paramSet,const struct HksBlob * restoreData,struct HksBlob * outData,uint8_t keyFlag,bool isNeedStorage)1091 static int32_t DoBuildKeyBlobOrGetOutDataAction(const struct HksParamSet *paramSet, const struct HksBlob *restoreData,
1092 struct HksBlob *outData, uint8_t keyFlag, bool isNeedStorage)
1093 {
1094 if (isNeedStorage) {
1095 return HksBuildKeyBlob(NULL, keyFlag, restoreData, paramSet, outData);
1096 }
1097 if (outData->size < restoreData->size) {
1098 HKS_LOG_E("outData size is too small, size : %" LOG_PUBLIC "u", outData->size);
1099 return HKS_ERROR_BUFFER_TOO_SMALL;
1100 }
1101 outData->size = restoreData->size;
1102 (void)memcpy_s(outData->data, outData->size, restoreData->data, outData->size);
1103 return HKS_SUCCESS;
1104 }
1105
BuildKeyBlobOrGetOutData(const struct HksParamSet * paramSet,const struct HksBlob * restoreData,struct HksBlob * outData,uint8_t keyFlag,const struct HuksKeyNode * keyNode)1106 static int32_t BuildKeyBlobOrGetOutData(const struct HksParamSet *paramSet, const struct HksBlob *restoreData,
1107 struct HksBlob *outData, uint8_t keyFlag, const struct HuksKeyNode *keyNode)
1108 {
1109 bool isNeedStorageOrExported = false;
1110 struct HksParam *keyStorageFlagParam = NULL;
1111 uint32_t storageFlag = 0;
1112 int32_t ret = HksGetParam(keyNode->keyBlobParamSet, HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG, &keyStorageFlagParam);
1113 if (ret == HKS_ERROR_INVALID_ARGUMENT) {
1114 return ret;
1115 }
1116 if (ret == HKS_SUCCESS) {
1117 storageFlag = keyStorageFlagParam->uint32Param;
1118 } else {
1119 isNeedStorageOrExported = true;
1120 }
1121
1122 bool isNeedStorage = false;
1123 HksCheckKeyNeedStored(paramSet, &isNeedStorage);
1124 if (isNeedStorageOrExported) {
1125 // Whether the derived/agreed key needs to be stored was not specified when generating the key
1126 HKS_LOG_D("default mode of storage");
1127 return DoBuildKeyBlobOrGetOutDataAction(paramSet, restoreData, outData, keyFlag, isNeedStorage);
1128 }
1129 if ((storageFlag == HKS_STORAGE_ONLY_USED_IN_HUKS) == isNeedStorage) {
1130 HKS_LOG_D("need store flag: %" LOG_PUBLIC "d", storageFlag);
1131 ret = DoBuildKeyBlobOrGetOutDataAction(paramSet, restoreData, outData, keyFlag, isNeedStorage);
1132 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "DoBuildKeyBlobOrGetOutDataAction failed!")
1133 } else {
1134 HKS_LOG_E("store flags do not match: genKey flag is %" LOG_PUBLIC "d, use flag is %" LOG_PUBLIC "d",
1135 (storageFlag == HKS_STORAGE_ONLY_USED_IN_HUKS), isNeedStorage);
1136 ret = HKS_ERROR_BAD_STATE;
1137 }
1138 return ret;
1139 }
1140
HksCoreDeriveThreeStageFinish(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)1141 int32_t HksCoreDeriveThreeStageFinish(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1142 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
1143 {
1144 HKS_LOG_D("HksCoreDeriveThreeStageFinish start");
1145 (void)inData;
1146 (void)alg;
1147 int32_t ret = CheckBlob(outData);
1148 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ARGUMENT, "invalid outData")
1149
1150 void *ctx = GetCryptoCtx(keyNode);
1151 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is NULL!")
1152
1153 struct HksBlob *restoreData = (struct HksBlob *)ctx;
1154
1155 ret = BuildKeyBlobOrGetOutData(paramSet, restoreData, outData, HKS_KEY_FLAG_DERIVE_KEY, keyNode);
1156
1157 FreeCachedData(&restoreData);
1158 ClearCryptoCtx(keyNode);
1159 return ret;
1160 }
1161
HksCoreDeriveThreeStageAbort(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)1162 int32_t HksCoreDeriveThreeStageAbort(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1163 uint32_t alg)
1164 {
1165 (void)paramSet;
1166 (void)alg;
1167
1168 void *ctx = GetCryptoCtx(keyNode);
1169 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is NULL!")
1170
1171 struct HksBlob *restoreData = (struct HksBlob *)ctx;
1172
1173 FreeCachedData(&restoreData);
1174 ClearCryptoCtx(keyNode);
1175 return HKS_SUCCESS;
1176 }
1177
HksCoreAgreeThreeStageInit(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)1178 int32_t HksCoreAgreeThreeStageInit(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1179 uint32_t alg)
1180 {
1181 (void)keyNode;
1182 (void)paramSet;
1183 (void)alg;
1184
1185 int32_t ret = AgreeAuth(keyNode, keyNode->runtimeParamSet);
1186 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "HksCoreAgreeThreeStageInit AgreeAuth fail ret : %" LOG_PUBLIC "d", ret)
1187
1188 return HKS_SUCCESS;
1189 }
1190
HksCoreAgreeThreeStageUpdate(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * signature,uint32_t alg)1191 int32_t HksCoreAgreeThreeStageUpdate(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1192 const struct HksBlob *srcData, struct HksBlob *signature, uint32_t alg)
1193 {
1194 (void)signature;
1195 (void)paramSet;
1196 (void)alg;
1197
1198 struct HksParam *ctxParam = NULL;
1199 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
1200 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
1201
1202 if (ctxParam->uint64Param != 0) {
1203 HKS_LOG_E("avoid running into this function multiple times!");
1204 return HKS_FAILURE;
1205 }
1206
1207 struct HksBlob rawKey = { 0, NULL };
1208 struct HksBlob publicKey = { 0, NULL };
1209
1210 do {
1211 ret = GetHksPubKeyInnerFormat(keyNode->runtimeParamSet, srcData, &publicKey);
1212 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "get public key from x509 format failed, ret = %" LOG_PUBLIC "d.", ret)
1213
1214 struct HksBlob *agreeTemp = NULL;
1215 ret = ConstructAgreeBlob(&agreeTemp);
1216 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "HksCoreAgreeBuildData failed, ret = %" LOG_PUBLIC "d.", ret)
1217
1218 ret = HksGetRawKey(keyNode->keyBlobParamSet, &rawKey);
1219 if (ret != HKS_SUCCESS) {
1220 HKS_LOG_E("agree get raw key failed!");
1221 FreeOutBlob(&agreeTemp);
1222 break;
1223 }
1224
1225 struct HksKeySpec agreeSpec = { 0 };
1226 HksFillKeySpec(keyNode->runtimeParamSet, &agreeSpec);
1227
1228 ret = HksCryptoHalAgreeKey(&rawKey, &publicKey, &agreeSpec, agreeTemp);
1229 if (ret != HKS_SUCCESS) {
1230 HKS_LOG_E("HksCryptoHalAgreeKey failed, ret = %" LOG_PUBLIC "d.", ret);
1231 FreeOutBlob(&agreeTemp);
1232 break;
1233 }
1234
1235 ctxParam->uint64Param = (uint64_t)(uintptr_t)agreeTemp;
1236 } while (0);
1237
1238 if (rawKey.data != NULL) {
1239 (void)memset_s(rawKey.data, rawKey.size, 0, rawKey.size);
1240 }
1241 HKS_FREE_PTR(rawKey.data);
1242 HKS_FREE_PTR(publicKey.data);
1243 return ret;
1244 }
1245
HksCoreAgreeThreeStageFinish(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)1246 int32_t HksCoreAgreeThreeStageFinish(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1247 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
1248 {
1249 (void)inData;
1250 (void)alg;
1251 int32_t ret = CheckBlob(outData);
1252 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ARGUMENT, "invalid outData")
1253
1254 void *ctx = GetCryptoCtx(keyNode);
1255 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is NULL!")
1256
1257 struct HksBlob *restoreData = (struct HksBlob *)ctx;
1258
1259 ret = BuildKeyBlobOrGetOutData(paramSet, restoreData, outData, HKS_KEY_FLAG_AGREE_KEY, keyNode);
1260
1261 FreeCachedData(&restoreData);
1262 ClearCryptoCtx(keyNode);
1263 return ret;
1264 }
1265
HksCoreAgreeThreeStageAbort(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)1266 int32_t HksCoreAgreeThreeStageAbort(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet, uint32_t alg)
1267 {
1268 (void)paramSet;
1269 (void)alg;
1270
1271 void *ctx = GetCryptoCtx(keyNode);
1272 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is NULL!")
1273
1274 struct HksBlob *restoreData = (struct HksBlob *)ctx;
1275
1276 FreeCachedData(&restoreData);
1277 ClearCryptoCtx(keyNode);
1278 return HKS_SUCCESS;
1279 }
1280
HksCoreMacThreeStageInit(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)1281 int32_t HksCoreMacThreeStageInit(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1282 uint32_t alg)
1283 {
1284 (void)paramSet;
1285 int32_t ret = HmacAuth(keyNode, keyNode->runtimeParamSet);
1286 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "HksCoreMacThreeStageInit MacAuth fail ret : %" LOG_PUBLIC "d", ret)
1287
1288 struct HksParam *ctxParam = NULL;
1289 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
1290 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
1291
1292 struct HksBlob rawKey = { 0, NULL };
1293 do {
1294 ret = HksGetRawKey(keyNode->keyBlobParamSet, &rawKey);
1295 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "Derive get raw key failed!")
1296
1297 void *ctx = NULL;
1298 ret = HksCryptoHalHmacInit(&rawKey, alg, &ctx);
1299 HKS_IF_NOT_SUCC_LOGE_BREAK(ret, "hmac init failed! ret : %" LOG_PUBLIC "d", ret)
1300
1301 ctxParam->uint64Param = (uint64_t)(uintptr_t)ctx;
1302 } while (0);
1303
1304 if (rawKey.data != NULL) {
1305 (void)memset_s(rawKey.data, rawKey.size, 0, rawKey.size);
1306 }
1307 HKS_FREE_PTR(rawKey.data);
1308
1309 return ret;
1310 }
1311
HksCoreMacThreeStageUpdate(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * srcData,struct HksBlob * mac,uint32_t alg)1312 int32_t HksCoreMacThreeStageUpdate(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1313 const struct HksBlob *srcData, struct HksBlob *mac, uint32_t alg)
1314 {
1315 (void)paramSet;
1316 (void)mac;
1317 (void)alg;
1318 int32_t ret;
1319
1320 void *ctx = GetCryptoCtx(keyNode);
1321 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx is NULL!")
1322
1323 ret = HksCryptoHalHmacUpdate(srcData, ctx);
1324 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "hmac update failed! ret : %" LOG_PUBLIC "d", ret)
1325
1326 return HKS_SUCCESS;
1327 }
1328
HksCoreMacThreeStageFinish(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,uint32_t alg)1329 int32_t HksCoreMacThreeStageFinish(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet,
1330 const struct HksBlob *inData, struct HksBlob *outData, uint32_t alg)
1331 {
1332 (void)paramSet;
1333 (void)alg;
1334
1335 struct HksParam *digestParam = NULL;
1336 int32_t ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_DIGEST, &digestParam);
1337 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_DIGEST_FAIL, "get ctx from keyNode failed!")
1338
1339 uint32_t macLen;
1340 ret = HksGetDigestLen(digestParam->uint32Param, &macLen);
1341 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get digest len failed")
1342
1343 if ((CheckBlob(outData) != HKS_SUCCESS) || (outData->size < macLen)) {
1344 HKS_LOG_E("out buffer too small");
1345 return HKS_ERROR_BUFFER_TOO_SMALL;
1346 }
1347
1348 struct HksParam *ctxParam = NULL;
1349 ret = HksGetParam(keyNode->runtimeParamSet, HKS_TAG_CRYPTO_CTX, &ctxParam);
1350 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_BAD_STATE, "get ctx from keyNode failed!")
1351
1352 void *ctx = (void *)(uintptr_t)ctxParam->uint64Param;
1353 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx invalid")
1354
1355 ret = HksCryptoHalHmacFinal(inData, &ctx, outData);
1356 HKS_IF_NOT_SUCC_LOGE(ret, "hmac final failed! ret : %" LOG_PUBLIC "d", ret)
1357
1358 ctxParam->uint64Param = 0; /* clear ctx to NULL */
1359 return ret;
1360 }
1361
HksCoreMacThreeStageAbort(const struct HuksKeyNode * keyNode,const struct HksParamSet * paramSet,uint32_t alg)1362 int32_t HksCoreMacThreeStageAbort(const struct HuksKeyNode *keyNode, const struct HksParamSet *paramSet, uint32_t alg)
1363 {
1364 (void)alg;
1365 (void)paramSet;
1366
1367 void *ctx = GetCryptoCtx(keyNode);
1368 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_NULL_POINTER, "ctx invalid")
1369
1370 HksCryptoHalHmacFreeCtx(&ctx);
1371 ClearCryptoCtx(keyNode);
1372
1373 return HKS_SUCCESS;
1374 }
1375
1376 #endif /* _CUT_AUTHENTICATE_ */
1377