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
HksSymmetricKeySizeCheck( struct HksParam *algParam, const struct HksBlob *key, struct HksBlob *outKey)77 static int32_t HksSymmetricKeySizeCheck(
78 struct HksParam *algParam, const struct HksBlob *key, struct HksBlob *outKey)
79 {
80 switch (algParam->uint32Param) {
81 case HKS_ALG_AES:
82 if ((key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_128)) &&
83 (key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_192)) &&
84 (key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_256))) {
85 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
86 return HKS_ERROR_INVALID_KEY_INFO;
87 }
88 break;
89 case HKS_ALG_DES:
90 if (key->size != HKS_KEY_BYTES(HKS_DES_KEY_SIZE_64)) {
91 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
92 return HKS_ERROR_INVALID_KEY_INFO;
93 }
94 break;
95 case HKS_ALG_3DES:
96 if ((key->size != HKS_KEY_BYTES(HKS_3DES_KEY_SIZE_128)) &&
97 (key->size != HKS_KEY_BYTES(HKS_3DES_KEY_SIZE_192))) {
98 HKS_LOG_E("invalid input key size: %" LOG_PUBLIC "u", key->size);
99 return HKS_ERROR_INVALID_KEY_INFO;
100 }
101 break;
102 default:
103 HKS_LOG_E("invalid input key algParam: %" LOG_PUBLIC "u", algParam->uint32Param);
104 return HKS_ERROR_INVALID_ALGORITHM;
105 }
106
107 return CopyToInnerKey(key, outKey);
108 }
109
110
GetHksPubKeyInnerFormat(const struct HksParamSet *paramSet, const struct HksBlob *key, struct HksBlob *outKey)111 int32_t GetHksPubKeyInnerFormat(const struct HksParamSet *paramSet,
112 const struct HksBlob *key, struct HksBlob *outKey)
113 {
114 if ((CheckBlob(key) != HKS_SUCCESS) || (outKey == NULL)) {
115 HKS_LOG_E("invalid key or outKey");
116 return HKS_ERROR_INVALID_ARGUMENT;
117 }
118
119 struct HksParam *algParam = NULL;
120 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
121 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL, "get alg param failed")
122
123 switch (algParam->uint32Param) {
124 #if defined(HKS_SUPPORT_HMAC_C) || defined(HKS_SUPPORT_SM3_C) || defined(HKS_SUPPORT_SM4_C) || \
125 defined(HKS_SUPPORT_AES_C) || defined(HKS_SUPPORT_DES_C) || defined(HKS_SUPPORT_3DES_C)
126 case HKS_ALG_AES:
127 case HKS_ALG_DES:
128 case HKS_ALG_3DES:
129 return HksSymmetricKeySizeCheck(algParam, key, outKey);
130 case HKS_ALG_HMAC:
131 case HKS_ALG_SM3:
132 case HKS_ALG_SM4:
133 return CopyToInnerKey(key, outKey);
134 #endif
135 #if defined(HKS_SUPPORT_X25519_C) || defined(HKS_SUPPORT_ED25519_C)
136 case HKS_ALG_ED25519:
137 case HKS_ALG_X25519:
138 return TranslateToInnerCurve25519Format(algParam->uint32Param, key, outKey);
139 #endif
140 #if defined(HKS_SUPPORT_RSA_C) || defined(HKS_SUPPORT_ECC_C) || defined(HKS_SUPPORT_DSA_C) || \
141 defined(HKS_SUPPORT_DH_C) || defined(HKS_SUPPORT_SM2_C)
142 case HKS_ALG_RSA:
143 case HKS_ALG_ECC:
144 case HKS_ALG_ECDH:
145 case HKS_ALG_DSA:
146 case HKS_ALG_DH:
147 case HKS_ALG_SM2:
148 return TranslateFromX509PublicKey(algParam->uint32Param, key, outKey);
149 #endif
150 default:
151 return HKS_ERROR_INVALID_ALGORITHM;
152 }
153 }
154
155