1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21
22 #include "hks_client_service_adapter_common.h"
23
24 #include <stddef.h>
25
26 #include "hks_client_service_adapter.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
CopyToInnerKey(const struct HksBlob * key,struct HksBlob * outKey)33 int32_t CopyToInnerKey(const struct HksBlob *key, struct HksBlob *outKey)
34 {
35 if ((key->size == 0) || (key->size > MAX_KEY_SIZE)) {
36 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
37 return HKS_ERROR_INVALID_ARGUMENT;
38 }
39
40 uint8_t *outData = (uint8_t *)HksMalloc(key->size);
41 HKS_IF_NULL_LOGE_RETURN(outData, HKS_ERROR_MALLOC_FAIL, "malloc failed")
42
43 (void)memcpy_s(outData, key->size, key->data, key->size);
44 outKey->data = outData;
45 outKey->size = key->size;
46
47 return HKS_SUCCESS;
48 }
49
50 #if defined(HKS_SUPPORT_ED25519_C) || defined(HKS_SUPPORT_X25519_C)
TranslateToInnerCurve25519Format(const uint32_t alg,const struct HksBlob * key,struct HksBlob * publicKey)51 static int32_t TranslateToInnerCurve25519Format(const uint32_t alg, const struct HksBlob *key,
52 struct HksBlob *publicKey)
53 {
54 if (key->size != HKS_KEY_BYTES(HKS_CURVE25519_KEY_SIZE_256)) {
55 HKS_LOG_E("Invalid curve25519 public key size! key size = 0x%" LOG_PUBLIC "X", key->size);
56 return HKS_ERROR_INVALID_KEY_INFO;
57 }
58
59 uint32_t totalSize = sizeof(struct HksPubKeyInfo) + key->size;
60 uint8_t *buffer = (uint8_t *)HksMalloc(totalSize);
61 HKS_IF_NULL_LOGE_RETURN(buffer, HKS_ERROR_MALLOC_FAIL, "malloc failed! %" LOG_PUBLIC "u", totalSize)
62 (void)memset_s(buffer, totalSize, 0, totalSize);
63
64 struct HksPubKeyInfo *curve25519Key = (struct HksPubKeyInfo *)buffer;
65 curve25519Key->keyAlg = (enum HksKeyAlg)alg;
66 curve25519Key->keySize = HKS_CURVE25519_KEY_SIZE_256;
67 curve25519Key->nOrXSize = key->size; /* curve25519 public key */
68
69 uint32_t offset = sizeof(struct HksPubKeyInfo);
70 (void)memcpy_s(buffer + offset, totalSize - offset, key->data, key->size);
71 publicKey->data = buffer;
72 publicKey->size = totalSize;
73 return HKS_SUCCESS;
74 }
75 #endif
76
GetHksPubKeyInnerFormat(const struct HksParamSet * paramSet,const struct HksBlob * key,struct HksBlob * outKey)77 int32_t GetHksPubKeyInnerFormat(const struct HksParamSet *paramSet,
78 const struct HksBlob *key, struct HksBlob *outKey)
79 {
80 if ((CheckBlob(key) != HKS_SUCCESS) || (outKey == NULL)) {
81 HKS_LOG_E("invalid key or outKey");
82 return HKS_ERROR_INVALID_ARGUMENT;
83 }
84
85 struct HksParam *algParam = NULL;
86 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
87 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL, "get alg param failed")
88
89 switch (algParam->uint32Param) {
90 #if defined(HKS_SUPPORT_HMAC_C) || defined(HKS_SUPPORT_SM3_C) || defined(HKS_SUPPORT_SM4_C) || \
91 defined(HKS_SUPPORT_AES_C)
92 case HKS_ALG_AES:
93 if ((key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_128)) &&
94 (key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_192)) &&
95 (key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_256))) {
96 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
97 return HKS_ERROR_INVALID_KEY_INFO;
98 } /* fall-through */
99 case HKS_ALG_HMAC:
100 case HKS_ALG_SM3:
101 case HKS_ALG_SM4:
102 return CopyToInnerKey(key, outKey);
103 #endif
104 #if defined(HKS_SUPPORT_X25519_C) || defined(HKS_SUPPORT_ED25519_C)
105 case HKS_ALG_ED25519:
106 case HKS_ALG_X25519:
107 return TranslateToInnerCurve25519Format(algParam->uint32Param, key, outKey);
108 #endif
109 #if defined(HKS_SUPPORT_RSA_C) || defined(HKS_SUPPORT_ECC_C) || defined(HKS_SUPPORT_DSA_C) || \
110 defined(HKS_SUPPORT_DH_C) || defined(HKS_SUPPORT_SM2_C)
111 case HKS_ALG_RSA:
112 case HKS_ALG_ECC:
113 case HKS_ALG_ECDH:
114 case HKS_ALG_DSA:
115 case HKS_ALG_DH:
116 case HKS_ALG_SM2:
117 return TranslateFromX509PublicKey(algParam->uint32Param, key, outKey);
118 #endif
119 default:
120 return HKS_ERROR_INVALID_ALGORITHM;
121 }
122 }
123
124