• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_crypto_adapter.h"
23 
24 #include <stddef.h>
25 
26 #include "hks_common_check.h"
27 #include "hks_log.h"
28 #include "hks_mem.h"
29 #include "hks_param.h"
30 #include "hks_template.h"
31 #include "securec.h"
32 
HksFillKeySpec(const struct HksParamSet * paramSet,struct HksKeySpec * spec)33 void HksFillKeySpec(const struct HksParamSet *paramSet, struct HksKeySpec *spec)
34 {
35     for (uint32_t i = 0; i < paramSet->paramsCnt; i++) {
36         switch (paramSet->params[i].tag) {
37             case HKS_TAG_ALGORITHM:
38                 spec->algType = paramSet->params[i].uint32Param;
39                 break;
40             case HKS_TAG_KEY_SIZE:
41                 spec->keyLen = paramSet->params[i].uint32Param;
42                 break;
43             default:
44                 break;
45         }
46     }
47 }
48 
HksFillUsageSpec(const struct HksParamSet * paramSet,struct HksUsageSpec * usageSpec)49 void HksFillUsageSpec(const struct HksParamSet *paramSet, struct HksUsageSpec *usageSpec)
50 {
51     for (uint32_t i = 0; i < paramSet->paramsCnt; i++) {
52         switch (paramSet->params[i].tag) {
53             case HKS_TAG_ALGORITHM:
54                 usageSpec->algType = paramSet->params[i].uint32Param;
55                 break;
56             case HKS_TAG_PADDING:
57                 usageSpec->padding = paramSet->params[i].uint32Param;
58                 break;
59             case HKS_TAG_DIGEST:
60                 usageSpec->digest = paramSet->params[i].uint32Param;
61                 break;
62             case HKS_TAG_BLOCK_MODE:
63                 usageSpec->mode = paramSet->params[i].uint32Param;
64                 break;
65             case HKS_TAG_PURPOSE:
66                 usageSpec->purpose = paramSet->params[i].uint32Param;
67                 break;
68             case HKS_TAG_MGF_DIGEST:
69                 usageSpec->mgfDigest = paramSet->params[i].uint32Param;
70                 break;
71             default:
72                 break;
73         }
74     }
75     usageSpec->algParam = NULL;
76 }
77 
HksFreeUsageSpec(struct HksUsageSpec ** usageSpec)78 void HksFreeUsageSpec(struct HksUsageSpec **usageSpec)
79 {
80     if ((usageSpec == NULL) || (*usageSpec == NULL)) {
81         return;
82     }
83 
84     if ((*usageSpec)->algParam != NULL) {
85         HKS_FREE((*usageSpec)->algParam);
86     }
87     HKS_FREE(*usageSpec);
88 }
89 
HksFillKeyDerivationParam(const struct HksParamSet * paramSet,struct HksKeyDerivationParam * param)90 void HksFillKeyDerivationParam(const struct HksParamSet *paramSet, struct HksKeyDerivationParam *param)
91 {
92     for (uint32_t i = 0; i < paramSet->paramsCnt; i++) {
93         switch (paramSet->params[i].tag) {
94             case HKS_TAG_DIGEST:
95                 param->digestAlg = paramSet->params[i].uint32Param;
96                 break;
97             case HKS_TAG_SALT:
98                 param->salt = paramSet->params[i].blob;
99                 break;
100             case HKS_TAG_INFO:
101                 param->info = paramSet->params[i].blob;
102                 break;
103             case HKS_TAG_ITERATION:
104                 param->iterations = paramSet->params[i].uint32Param;
105                 break;
106             default:
107                 break;
108         }
109     }
110 }
111 
HksFillAeadParam(const struct HksParamSet * paramSet,struct HksBlob * inputText,struct HksUsageSpec * usageSpec,bool isEncrypt)112 int32_t HksFillAeadParam(
113     const struct HksParamSet *paramSet, struct HksBlob *inputText, struct HksUsageSpec *usageSpec, bool isEncrypt)
114 {
115     struct HksParam *nonceParam = NULL;
116     int32_t ret = HksGetParam(paramSet, HKS_TAG_NONCE, &nonceParam);
117     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "HksFillAeadParam get nonce param failed!")
118 
119     struct HksParam emptyAadParam = {
120         .tag = HKS_TAG_ASSOCIATED_DATA,
121         .blob = {
122             .size = 0,
123             .data = NULL
124         }
125     };
126     struct HksParam *aadParam = NULL;
127     ret = HksGetParam(paramSet, HKS_TAG_ASSOCIATED_DATA, &aadParam);
128     if (ret == HKS_ERROR_PARAM_NOT_EXIST) {
129         HKS_LOG_W("HksFillAeadParam no input aad, do not use aad");
130         aadParam = &emptyAadParam;
131     } else if (ret != HKS_SUCCESS) {
132         HKS_LOG_E("HksFillAeadParam get aad param failed!");
133         return ret;
134     }
135 
136     struct HksParam tagParam;
137     if (!isEncrypt) {
138         if (inputText->size <= HKS_AE_TAG_LEN) {
139             HKS_LOG_E("too small inputText size");
140             return HKS_ERROR_INVALID_ARGUMENT;
141         }
142         inputText->size -= HKS_AE_TAG_LEN;
143 
144         tagParam.blob.size = HKS_AE_TAG_LEN;
145         tagParam.blob.data = inputText->data + inputText->size;
146     }
147 
148     struct HksAeadParam *aeadParam = (struct HksAeadParam *)HksMalloc(sizeof(struct HksAeadParam));
149     HKS_IF_NULL_LOGE_RETURN(aeadParam, HKS_ERROR_MALLOC_FAIL, "aeadParam malloc failed!")
150 
151     if (!isEncrypt) {
152         aeadParam->tagDec = tagParam.blob;
153     } else {
154         aeadParam->tagLenEnc = HKS_AE_TAG_LEN;
155     }
156 
157     aeadParam->nonce = nonceParam->blob;
158     aeadParam->aad = aadParam->blob;
159     aeadParam->payloadLen = 0;
160     usageSpec->algParam = aeadParam;
161     return HKS_SUCCESS;
162 }
163 
HksFillIvParam(const struct HksParamSet * paramSet,struct HksUsageSpec * usageSpec)164 int32_t HksFillIvParam(const struct HksParamSet *paramSet, struct HksUsageSpec *usageSpec)
165 {
166     struct HksParam *ivParam = NULL;
167     int32_t ret = HksGetParam(paramSet, HKS_TAG_IV, &ivParam);
168     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "cipher get iv param failed!")
169 
170     struct HksCipherParam *param = (struct HksCipherParam *)HksMalloc(sizeof(struct HksCipherParam));
171     HKS_IF_NULL_LOGE_RETURN(param, HKS_ERROR_MALLOC_FAIL, "param malloc failed!")
172 
173     param->iv = ivParam->blob;
174     usageSpec->algParam = param;
175     return HKS_SUCCESS;
176 }
177 
HksIsAlgorithmSm4(const struct HksParamSet * paramSet)178 static bool HksIsAlgorithmSm4(const struct HksParamSet *paramSet)
179 {
180     struct HksParam *algParam = NULL;
181     int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
182     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, false, "check sm4 get alg param failed!")
183     return (algParam->uint32Param == HKS_ALG_SM4);
184 }
185 
HksBuildCipherUsageSpec(const struct HksParamSet * paramSet,bool isEncrypt,struct HksBlob * inputText,struct HksUsageSpec ** outUsageSpec)186 int32_t HksBuildCipherUsageSpec(
187     const struct HksParamSet *paramSet, bool isEncrypt, struct HksBlob *inputText, struct HksUsageSpec **outUsageSpec)
188 {
189     bool isAes = false;
190     bool isAeMode = false;
191     int32_t ret = HksCheckAesAeMode(paramSet, &isAes, &isAeMode);
192     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get aeMode failed!")
193 
194     struct HksUsageSpec *usageSpec = (struct HksUsageSpec *)HksMalloc(sizeof(struct HksUsageSpec));
195     HKS_IF_NULL_LOGE_RETURN(usageSpec, HKS_ERROR_MALLOC_FAIL, "cipher usageSpec malloc failed!")
196 
197     HksFillUsageSpec(paramSet, usageSpec);
198 
199     if (usageSpec->algType == HKS_ALG_RSA && usageSpec->digest == HKS_DIGEST_NONE) {
200         usageSpec->digest = HKS_DIGEST_SHA1;
201     }
202 
203     if (HksIsAlgorithmSm4(paramSet)) { // is sm4
204         ret = HksFillIvParam(paramSet, usageSpec);
205     } else if (!isAes) { // not sm4, not aes
206         *outUsageSpec = usageSpec;
207         return HKS_SUCCESS;
208     } else if (isAeMode) { // is aes, is ae mode
209         ret = HksFillAeadParam(paramSet, inputText, usageSpec, isEncrypt);
210     } else { // is aes, not ae mode
211         ret = HksFillIvParam(paramSet, usageSpec);
212     }
213 
214     if (ret != HKS_SUCCESS) {
215         HksFreeUsageSpec(&usageSpec);
216         HKS_LOG_E("fill[%" LOG_PUBLIC "x] param failed!", isAeMode);
217         return ret;
218     }
219 
220     *outUsageSpec = usageSpec;
221     return HKS_SUCCESS;
222 }
223 
HksGetEncryptAeTag(const struct HksParamSet * paramSet,const struct HksBlob * inData,struct HksBlob * outData,struct HksBlob * tagAead)224 int32_t HksGetEncryptAeTag(
225     const struct HksParamSet *paramSet, const struct HksBlob *inData, struct HksBlob *outData, struct HksBlob *tagAead)
226 {
227     bool isAes = false;
228     bool isAeMode = false;
229     int32_t ret = HksCheckAesAeMode(paramSet, &isAes, &isAeMode);
230     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get aeMode failed!")
231 
232     if ((!isAes) || (!isAeMode)) {
233         tagAead->data = NULL;
234         tagAead->size = 0;
235         return HKS_SUCCESS;
236     }
237 
238     if (outData->size < (inData->size + HKS_AE_TAG_LEN)) {
239         HKS_LOG_E("too small out buf!");
240         return HKS_ERROR_INVALID_ARGUMENT;
241     }
242 
243     tagAead->data = outData->data + inData->size;
244     tagAead->size = HKS_AE_TAG_LEN;
245     return HKS_SUCCESS;
246 }
247 
BuildParamSetOut(const struct HksParam * params,uint32_t paramCnt,struct HksParamSet * paramSetOut)248 static int32_t BuildParamSetOut(const struct HksParam *params, uint32_t paramCnt, struct HksParamSet *paramSetOut)
249 {
250     int32_t ret;
251     struct HksParamSet *tmpParamSetOut = NULL;
252 
253     ret = HksInitParamSet(&tmpParamSetOut);
254     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "init paramSet failed!")
255 
256     ret = HksAddParams(tmpParamSetOut, params, paramCnt);
257     if (ret != HKS_SUCCESS) {
258         HKS_LOG_E("add params failed");
259         HksFreeParamSet(&tmpParamSetOut);
260         return ret;
261     }
262 
263     ret = HksBuildParamSet(&tmpParamSetOut);
264     if (ret != HKS_SUCCESS) {
265         HKS_LOG_E("build paramSet failed");
266         HksFreeParamSet(&tmpParamSetOut);
267         return ret;
268     }
269 
270     if (memcpy_s(paramSetOut, paramSetOut->paramSetSize, tmpParamSetOut, tmpParamSetOut->paramSetSize) != EOK) {
271         HksFreeParamSet(&tmpParamSetOut);
272         HKS_LOG_E("memcpy paramSet out failed, paramSetOut size = %" LOG_PUBLIC "u", paramSetOut->paramSetSize);
273         return HKS_ERROR_INSUFFICIENT_MEMORY;
274     }
275 
276     HksFreeParamSet(&tmpParamSetOut);
277     return HksFreshParamSet(paramSetOut, false);
278 }
279 
280 #if defined(HKS_SUPPORT_ECC_C) && defined(HKS_SUPPORT_ECC_GENERATE_KEY)
FormatKeyInner(uint32_t publicKeySize,uint8_t * publicKey,const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)281 static int32_t FormatKeyInner(uint32_t publicKeySize, uint8_t *publicKey, const struct HksBlob *keyIn,
282     struct HksParamSet *paramSetOut)
283 {
284     struct HksParam params[] = {
285         {
286             .tag = HKS_TAG_ASYMMETRIC_PUBLIC_KEY_DATA,
287             .blob = { publicKeySize, publicKey },
288         },
289         {
290             .tag = HKS_TAG_ASYMMETRIC_PRIVATE_KEY_DATA,
291             .blob = { keyIn->size, keyIn->data },
292         },
293     };
294     return BuildParamSetOut(params, HKS_ARRAY_SIZE(params), paramSetOut);
295 }
296 #endif
297 
298 #ifndef _CUT_AUTHENTICATE_
SetCurve25519KeyMaterial(bool isPubKey,const struct HksBlob * keyIn,struct HksBlob * keyOut)299 static int32_t SetCurve25519KeyMaterial(bool isPubKey, const struct HksBlob *keyIn, struct HksBlob *keyOut)
300 {
301     struct KeyMaterial25519 curve25519Km = {HKS_ALG_ED25519, 0, 0, 0, 0};
302     curve25519Km.keySize = HKS_CURVE25519_KEY_SIZE_256;
303     curve25519Km.reserved = 0;
304 
305     uint32_t offset = sizeof(struct KeyMaterial25519);
306     if (isPubKey) {
307         curve25519Km.pubKeySize = keyIn->size;
308         curve25519Km.priKeySize = 0;
309     } else {
310         curve25519Km.pubKeySize = 0;
311         curve25519Km.priKeySize = keyIn->size;
312     }
313 
314     keyOut->size = sizeof(struct KeyMaterial25519) + curve25519Km.pubKeySize + curve25519Km.priKeySize;
315     keyOut->data = (uint8_t *)HksMalloc(keyOut->size);
316     HKS_IF_NULL_RETURN(keyOut->data, HKS_ERROR_MALLOC_FAIL)
317 
318     (void)memcpy_s(keyOut->data, keyOut->size, &curve25519Km, sizeof(struct KeyMaterial25519));
319 
320     (void)memcpy_s(keyOut->data + offset, keyOut->size - offset, keyIn->data, keyIn->size);
321 
322     return HKS_SUCCESS;
323 }
324 
CheckCurve25519KeySize(const struct HksBlob * keyIn)325 static int32_t CheckCurve25519KeySize(const struct HksBlob *keyIn)
326 {
327     if (keyIn->size < sizeof(struct KeyMaterial25519)) {
328         HKS_LOG_E("keyIn buffer too small");
329         return HKS_ERROR_INVALID_ARGUMENT;
330     }
331 
332     struct KeyMaterial25519 *keyMaterial = (struct KeyMaterial25519 *)keyIn->data;
333 
334     /* input pubKeySize and priKeySize of keyMaterial have been guaranteed that the addition will not overflow */
335     if (keyIn->size < (sizeof(struct KeyMaterial25519) + keyMaterial->pubKeySize + keyMaterial->priKeySize)) {
336         HKS_LOG_E("keyIn is not a valid key material");
337         return HKS_ERROR_INVALID_ARGUMENT;
338     }
339 
340     return HKS_SUCCESS;
341 }
342 
CheckFormatCurve25519Key(const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)343 static int32_t CheckFormatCurve25519Key(const struct HksBlob *keyIn, struct HksParamSet *paramSetOut)
344 {
345     int32_t ret = CheckCurve25519KeySize(keyIn);
346     HKS_IF_NOT_SUCC_RETURN(ret, ret)
347 
348     struct KeyMaterial25519 *keyMaterial = (struct KeyMaterial25519 *)keyIn->data;
349     uint32_t offset = sizeof(struct HksParamSet) + (sizeof(struct HksParam) << 1);
350     if (keyMaterial->pubKeySize > MAX_KEY_SIZE || keyMaterial->priKeySize > MAX_KEY_SIZE) {
351         HKS_LOG_E("pubKey or priKey buffer too big");
352         return HKS_ERROR_INVALID_ARGUMENT;
353     }
354     if (paramSetOut->paramSetSize < (offset + keyMaterial->pubKeySize + keyMaterial->priKeySize)) {
355         HKS_LOG_E("pubKey or priKey buffer too small");
356         return HKS_ERROR_BUFFER_TOO_SMALL;
357     }
358 
359     return HKS_SUCCESS;
360 }
361 
FormatCurve25519Key(const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)362 static int32_t FormatCurve25519Key(const struct HksBlob *keyIn, struct HksParamSet *paramSetOut)
363 {
364     int32_t ret = CheckFormatCurve25519Key(keyIn, paramSetOut);
365     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check curve 25519 key failed")
366 
367     struct KeyMaterial25519 *keyMaterial = (struct KeyMaterial25519 *)keyIn->data;
368     struct HksParam params[] = {
369         {
370             .tag = HKS_TAG_ASYMMETRIC_PUBLIC_KEY_DATA,
371             .blob = { keyMaterial->pubKeySize, keyIn->data + sizeof(struct KeyMaterial25519) },
372         },
373         {
374             .tag = HKS_TAG_ASYMMETRIC_PRIVATE_KEY_DATA,
375             .blob = { keyMaterial->priKeySize,
376                 keyIn->data + sizeof(struct KeyMaterial25519) + keyMaterial->pubKeySize },
377         },
378     };
379 
380     return BuildParamSetOut(params, HKS_ARRAY_SIZE(params), paramSetOut);
381 }
382 
GetCurve25519FromKeyMaterial(const bool isPubKey,const struct HksBlob * keyMaterial,struct HksBlob * keyOut)383 int32_t GetCurve25519FromKeyMaterial(const bool isPubKey, const struct HksBlob *keyMaterial,
384     struct HksBlob *keyOut)
385 {
386     int32_t ret = CheckCurve25519KeySize(keyMaterial);
387     HKS_IF_NOT_SUCC_RETURN(ret, ret)
388 
389     const struct KeyMaterial25519 *km = (struct KeyMaterial25519 *)(keyMaterial->data);
390 
391     uint32_t size = (isPubKey ? km->pubKeySize : km->priKeySize);
392     if (size == 0) {
393         HKS_LOG_E("get key material size invalid, pubSize = %" LOG_PUBLIC "u, priSize = %" LOG_PUBLIC "u",
394             km->pubKeySize, km->priKeySize);
395         return HKS_ERROR_INVALID_KEY_INFO;
396     }
397     uint8_t *buffer = (uint8_t *)HksMalloc(size);
398     HKS_IF_NULL_RETURN(buffer, HKS_ERROR_MALLOC_FAIL)
399 
400     uint32_t offset = sizeof(struct KeyMaterial25519);
401     uint8_t *tmp = (isPubKey ? (keyMaterial->data + offset) : (keyMaterial->data + offset + km->pubKeySize));
402     (void)memcpy_s(buffer, size, tmp, size);
403 
404     keyOut->data = buffer;
405     keyOut->size = size;
406     return HKS_SUCCESS;
407 }
408 
409 #if defined(HKS_SUPPORT_AES_C) || (defined(HKS_SUPPORT_HMAC_C) && defined(HKS_SUPPORT_HMAC_GENERATE_KEY))
FormatAesOrHmacKey(const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)410 static int32_t FormatAesOrHmacKey(const struct HksBlob *keyIn, struct HksParamSet *paramSetOut)
411 {
412     struct HksParam params[] = {
413         {
414             .tag = HKS_TAG_SYMMETRIC_KEY_DATA,
415             .blob = { keyIn->size, keyIn->data },
416         },
417     };
418     return BuildParamSetOut(params, HKS_ARRAY_SIZE(params), paramSetOut);
419 }
420 #endif
421 
422 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
FormatRsaKey(const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)423 static int32_t FormatRsaKey(const struct HksBlob *keyIn, struct HksParamSet *paramSetOut)
424 {
425     if (keyIn->size < sizeof(struct KeyMaterialRsa)) {
426         return HKS_ERROR_INVALID_ARGUMENT;
427     }
428 
429     struct KeyMaterialRsa *keyMaterial = (struct KeyMaterialRsa *)keyIn->data;
430     uint32_t publicKeySize = sizeof(struct KeyMaterialRsa) + keyMaterial->nSize + keyMaterial->eSize;
431     if (keyIn->size < publicKeySize) {
432         HKS_LOG_E("invalid key info.");
433         return HKS_ERROR_INVALID_KEY_INFO;
434     }
435 
436     uint8_t *publicKey = (uint8_t *)HksMalloc(publicKeySize);
437     HKS_IF_NULL_LOGE_RETURN(publicKey, HKS_ERROR_MALLOC_FAIL, "malloc public key failed.")
438 
439     (void)memcpy_s(publicKey, publicKeySize, keyIn->data, publicKeySize);
440     ((struct KeyMaterialRsa *)publicKey)->dSize = 0;
441 
442     int32_t ret = FormatKeyInner(publicKeySize, publicKey, keyIn, paramSetOut);
443     (void)memset_s(publicKey, publicKeySize, 0, publicKeySize);
444     HKS_FREE(publicKey);
445     return ret;
446 }
447 #endif
448 
449 #if defined(HKS_SUPPORT_DSA_C) && defined(HKS_SUPPORT_DSA_GENERATE_KEY)
FormatDsaKey(const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)450 static int32_t FormatDsaKey(const struct HksBlob *keyIn, struct HksParamSet *paramSetOut)
451 {
452     if (keyIn->size < sizeof(struct KeyMaterialDsa)) {
453         return HKS_ERROR_INVALID_ARGUMENT;
454     }
455 
456     struct KeyMaterialDsa *keyMaterial = (struct KeyMaterialDsa *)keyIn->data;
457     uint32_t publicKeySize = sizeof(struct KeyMaterialDsa) + keyMaterial->ySize + keyMaterial->pSize +
458                              keyMaterial->qSize + keyMaterial->gSize;
459     if (keyIn->size < publicKeySize) {
460         HKS_LOG_E("invalid key info.");
461         return HKS_ERROR_INVALID_KEY_INFO;
462     }
463 
464     uint8_t *publicKey = (uint8_t *)HksMalloc(publicKeySize);
465     HKS_IF_NULL_LOGE_RETURN(publicKey, HKS_ERROR_MALLOC_FAIL, "malloc key failed.")
466 
467     (void)memcpy_s(publicKey, publicKeySize, keyIn->data, sizeof(struct KeyMaterialDsa));
468     uint32_t inOffset = sizeof(struct KeyMaterialDsa);
469     uint32_t outOffset = sizeof(struct KeyMaterialDsa) + keyMaterial->xSize;
470     (void)memcpy_s(publicKey + inOffset, publicKeySize - inOffset, keyIn->data + outOffset, publicKeySize - inOffset);
471     ((struct KeyMaterialDsa *)publicKey)->xSize = 0;
472 
473     int32_t ret = FormatKeyInner(publicKeySize, publicKey, keyIn, paramSetOut);
474     (void)memset_s(publicKey, publicKeySize, 0, publicKeySize);
475     HKS_FREE(publicKey);
476     return ret;
477 }
478 #endif
479 
480 #if defined(HKS_SUPPORT_ECC_C) && defined(HKS_SUPPORT_ECC_GENERATE_KEY)
FormatEccKey(const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)481 static int32_t FormatEccKey(const struct HksBlob *keyIn, struct HksParamSet *paramSetOut)
482 {
483     if (keyIn->size < sizeof(struct KeyMaterialEcc)) {
484         return HKS_ERROR_INVALID_ARGUMENT;
485     }
486 
487     struct KeyMaterialEcc *keyMaterial = (struct KeyMaterialEcc *)keyIn->data;
488     uint32_t publicKeySize = sizeof(struct KeyMaterialEcc) + keyMaterial->xSize + keyMaterial->ySize;
489     if (keyIn->size < publicKeySize) {
490         HKS_LOG_E("invalid key info.");
491         return HKS_ERROR_INVALID_KEY_INFO;
492     }
493 
494     uint8_t *publicKey = (uint8_t *)HksMalloc(publicKeySize);
495     HKS_IF_NULL_LOGE_RETURN(publicKey, HKS_ERROR_MALLOC_FAIL, "malloc public key failed.")
496 
497     (void)memcpy_s(publicKey, publicKeySize, keyIn->data, publicKeySize);
498     ((struct KeyMaterialEcc *)publicKey)->zSize = 0;
499     int32_t ret = FormatKeyInner(publicKeySize, publicKey, keyIn, paramSetOut);
500     (void)memset_s(publicKey, publicKeySize, 0, publicKeySize);
501     HKS_FREE(publicKey);
502     return ret;
503 }
504 #endif
505 
506 #if defined(HKS_SUPPORT_DH_C) && defined(HKS_SUPPORT_DH_GENERATE_KEY)
FormatDhKey(const struct HksBlob * keyIn,struct HksParamSet * paramSetOut)507 static int32_t FormatDhKey(const struct HksBlob *keyIn, struct HksParamSet *paramSetOut)
508 {
509     if (keyIn->size < sizeof(struct KeyMaterialDh)) {
510         return HKS_ERROR_INVALID_ARGUMENT;
511     }
512 
513     struct KeyMaterialDh *keyMaterial = (struct KeyMaterialDh *)keyIn->data;
514     uint32_t publicKeySize = sizeof(struct KeyMaterialDh) + keyMaterial->pubKeySize;
515     if (keyIn->size < publicKeySize) {
516         HKS_LOG_E("invalid key info.");
517         return HKS_ERROR_INVALID_KEY_INFO;
518     }
519 
520     uint8_t *publicKey = (uint8_t *)HksMalloc(publicKeySize);
521     HKS_IF_NULL_LOGE_RETURN(publicKey, HKS_ERROR_MALLOC_FAIL, "malloc public key failed.")
522 
523     (void)memcpy_s(publicKey, publicKeySize, keyIn->data, publicKeySize);
524     ((struct KeyMaterialDh *)publicKey)->priKeySize = 0;
525     int32_t ret = FormatKeyInner(publicKeySize, publicKey, keyIn, paramSetOut);
526     (void)memset_s(publicKey, publicKeySize, 0, publicKeySize);
527     HKS_FREE(publicKey);
528     return ret;
529 }
530 #endif
531 
HksSetKeyToMaterial(uint32_t alg,bool isPubKey,const struct HksBlob * key,struct HksBlob * keyMaterial)532 int32_t HksSetKeyToMaterial(uint32_t alg, bool isPubKey, const struct HksBlob *key, struct HksBlob *keyMaterial)
533 {
534     switch (alg) {
535         case HKS_ALG_X25519:
536         case HKS_ALG_ED25519:
537             return SetCurve25519KeyMaterial(isPubKey, key, keyMaterial);
538         case HKS_ALG_RSA:
539         case HKS_ALG_DSA:
540         case HKS_ALG_ECC:
541         case HKS_ALG_ECDH:
542         case HKS_ALG_DH:
543             keyMaterial->size = key->size;
544             keyMaterial->data = (uint8_t *)HksMalloc(keyMaterial->size);
545             if (keyMaterial->data != NULL) {
546                 (void)memcpy_s(keyMaterial->data, keyMaterial->size, key->data, key->size);
547                 return HKS_SUCCESS;
548             } else {
549                 return HKS_ERROR_MALLOC_FAIL;
550             }
551             break;
552         default:
553             HKS_LOG_E("alg not support");
554             return HKS_ERROR_INVALID_ALGORITHM;
555     }
556 }
557 
HksGetKeyFromMaterial(uint32_t alg,bool isPubKey,const struct HksBlob * keyMaterial,struct HksBlob * key)558 int32_t HksGetKeyFromMaterial(uint32_t alg, bool isPubKey, const struct HksBlob *keyMaterial, struct HksBlob *key)
559 {
560     switch (alg) {
561         case HKS_ALG_X25519:
562         case HKS_ALG_ED25519:
563             return GetCurve25519FromKeyMaterial(isPubKey, keyMaterial, key);
564         default:
565             HKS_LOG_E("alg not support");
566             return HKS_ERROR_INVALID_ALGORITHM;
567     }
568 }
569 
HksFormatKeyFromMaterial(uint32_t alg,const struct HksBlob * keyMaterial,struct HksParamSet * paramSetOut)570 int32_t HksFormatKeyFromMaterial(uint32_t alg, const struct HksBlob *keyMaterial,
571     struct HksParamSet *paramSetOut)
572 {
573     switch (alg) {
574         case HKS_ALG_X25519:
575         case HKS_ALG_ED25519:
576             return FormatCurve25519Key(keyMaterial, paramSetOut);
577 #if defined(HKS_SUPPORT_AES_C) && defined(HKS_SUPPORT_AES_GENERATE_KEY)
578         case HKS_ALG_AES:
579             return FormatAesOrHmacKey(keyMaterial, paramSetOut);
580 #endif
581 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_GENERATE_KEY)
582         case HKS_ALG_RSA:
583             return FormatRsaKey(keyMaterial, paramSetOut);
584 #endif
585 #if defined(HKS_SUPPORT_HMAC_C) && defined(HKS_SUPPORT_HMAC_GENERATE_KEY)
586         case HKS_ALG_HMAC:
587             return FormatAesOrHmacKey(keyMaterial, paramSetOut);
588 #endif
589 #if defined(HKS_SUPPORT_DSA_C) && defined(HKS_SUPPORT_DSA_GENERATE_KEY)
590         case HKS_ALG_DSA:
591             return FormatDsaKey(keyMaterial, paramSetOut);
592 #endif
593 #if defined(HKS_SUPPORT_ECC_C) && defined(HKS_SUPPORT_ECC_GENERATE_KEY)
594         case HKS_ALG_ECC:
595         case HKS_ALG_ECDH:
596             return FormatEccKey(keyMaterial, paramSetOut);
597 #endif
598 #if defined(HKS_SUPPORT_DH_C) && defined(HKS_SUPPORT_DH_GENERATE_KEY)
599         case HKS_ALG_DH:
600             return FormatDhKey(keyMaterial, paramSetOut);
601 #endif
602         default:
603             HKS_LOG_E("alg not support");
604             return HKS_ERROR_INVALID_ALGORITHM;
605     }
606 }
607 #endif
608