1 /*
2 * Copyright (C) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sym_key_generator.h"
17 #include "sym_key_factory_spi.h"
18 #include "sym_common_defines.h"
19 #include "params_parser.h"
20 #include "utils.h"
21
22 #include <securec.h>
23 #include "log.h"
24 #include "memory.h"
25 #include "result.h"
26 #include "config.h"
27
28 #define AES_KEY_SIZE_128 128
29 #define AES_KEY_SIZE_192 192
30 #define AES_KEY_SIZE_256 256
31 #define SM4_KEY_SIZE_128 128
32 #define DES_KEY_SIZE_64 64
33 #define TRIPLE_DES_KEY_SIZE_192 192
34 #define HMAC_KEY_SIZE_SHA1 160
35 #define HMAC_KEY_SIZE_SHA224 224
36 #define HMAC_KEY_SIZE_SHA256 256
37 #define HMAC_KEY_SIZE_SHA384 384
38 #define HMAC_KEY_SIZE_SHA512 512
39 #define HMAC_KEY_SIZE_SM3 256
40 #define HMAC_KEY_SIZE_MD5 128
41
42 typedef HcfResult (*SymKeyGeneratorSpiCreateFunc)(SymKeyAttr *, HcfSymKeyGeneratorSpi **);
43
44 typedef struct {
45 SymKeyGeneratorSpiCreateFunc createFunc;
46 } SymKeyGenFuncSet;
47
48 typedef struct {
49 HcfAlgValue algo;
50 SymKeyGenFuncSet funcSet;
51 } SymKeyGenAbility;
52
53 typedef struct {
54 HcfSymKeyGenerator base;
55 HcfSymKeyGeneratorSpi *spiObj;
56 char algoName[HCF_MAX_ALGO_NAME_LEN];
57 } HcfSymmKeyGeneratorImpl;
58
59 static const SymKeyGenAbility SYMKEY_ABILITY_SET[] = {
60 { HCF_ALG_AES, { HcfSymKeyGeneratorSpiCreate }},
61 { HCF_ALG_SM4, { HcfSymKeyGeneratorSpiCreate }},
62 { HCF_ALG_DES, { HcfSymKeyGeneratorSpiCreate }},
63 { HCF_ALG_3DES, { HcfSymKeyGeneratorSpiCreate }},
64 { HCF_ALG_HMAC, { HcfSymKeyGeneratorSpiCreate }}
65 };
66
FindAbility(SymKeyAttr * attr)67 static const SymKeyGenFuncSet *FindAbility(SymKeyAttr *attr)
68 {
69 if (attr == NULL) {
70 return NULL;
71 }
72 for (uint32_t i = 0; i < sizeof(SYMKEY_ABILITY_SET) / sizeof(SymKeyGenAbility); i++) {
73 if (SYMKEY_ABILITY_SET[i].algo == attr->algo) {
74 return &(SYMKEY_ABILITY_SET[i].funcSet);
75 }
76 }
77 LOGE("Algo not support! [Algo]: %{public}d", attr->algo);
78 return NULL;
79 }
80
SetKeyLength(HcfAlgParaValue value,void * attr)81 static void SetKeyLength(HcfAlgParaValue value, void *attr)
82 {
83 SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
84
85 switch (value) {
86 case HCF_ALG_AES_128:
87 keyAttr->algo = HCF_ALG_AES;
88 keyAttr->keySize = AES_KEY_SIZE_128;
89 break;
90 case HCF_ALG_AES_192:
91 keyAttr->algo = HCF_ALG_AES;
92 keyAttr->keySize = AES_KEY_SIZE_192;
93 break;
94 case HCF_ALG_AES_256:
95 keyAttr->algo = HCF_ALG_AES;
96 keyAttr->keySize = AES_KEY_SIZE_256;
97 break;
98 case HCF_ALG_SM4_128:
99 keyAttr->algo = HCF_ALG_SM4;
100 keyAttr->keySize = SM4_KEY_SIZE_128;
101 break;
102 case HCF_ALG_3DES_192:
103 keyAttr->algo = HCF_ALG_3DES;
104 keyAttr->keySize = TRIPLE_DES_KEY_SIZE_192;
105 break;
106 case HCF_ALG_DES_64:
107 keyAttr->algo = HCF_ALG_DES;
108 keyAttr->keySize = DES_KEY_SIZE_64;
109 break;
110 default:
111 break;
112 }
113 }
114
SetKeyType(HcfAlgParaValue value,void * attr)115 static void SetKeyType(HcfAlgParaValue value, void *attr)
116 {
117 SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
118
119 if (value == HCF_ALG_HMAC_DEFAULT) {
120 keyAttr->algo = HCF_ALG_HMAC;
121 }
122 }
123
SetKeyLenByDigest(HcfAlgParaValue value,void * attr)124 static void SetKeyLenByDigest(HcfAlgParaValue value, void *attr)
125 {
126 SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
127
128 switch (value) {
129 case HCF_OPENSSL_DIGEST_SHA1:
130 keyAttr->keySize = HMAC_KEY_SIZE_SHA1;
131 break;
132 case HCF_OPENSSL_DIGEST_SHA224:
133 keyAttr->keySize = HMAC_KEY_SIZE_SHA224;
134 break;
135 case HCF_OPENSSL_DIGEST_SHA256:
136 keyAttr->keySize = HMAC_KEY_SIZE_SHA256;
137 break;
138 case HCF_OPENSSL_DIGEST_SHA384:
139 keyAttr->keySize = HMAC_KEY_SIZE_SHA384;
140 break;
141 case HCF_OPENSSL_DIGEST_SHA512:
142 keyAttr->keySize = HMAC_KEY_SIZE_SHA512;
143 break;
144 case HCF_OPENSSL_DIGEST_SM3:
145 keyAttr->keySize = HMAC_KEY_SIZE_SM3;
146 break;
147 case HCF_OPENSSL_DIGEST_MD5:
148 keyAttr->keySize = HMAC_KEY_SIZE_MD5;
149 break;
150 default:
151 // We will ignore the and 'NoHash' inputs
152 LOGE("Invalid digest input: NoHash");
153 break;
154 }
155 }
156
OnSetSymKeyParameter(const HcfParaConfig * config,void * attr)157 static HcfResult OnSetSymKeyParameter(const HcfParaConfig* config, void *attr)
158 {
159 if ((config == NULL) || (attr == NULL)) {
160 return HCF_INVALID_PARAMS;
161 }
162 HcfResult ret = HCF_SUCCESS;
163 LOGD("Set Parameter:%s\n", config->tag);
164 switch (config->paraType) {
165 case HCF_ALG_KEY_TYPE:
166 SetKeyLength(config->paraValue, attr);
167 break;
168 case HCF_ALG_TYPE:
169 SetKeyType(config->paraValue, attr);
170 break;
171 case HCF_ALG_DIGEST:
172 SetKeyLenByDigest(config->paraValue, attr);
173 break;
174 default:
175 ret = HCF_INVALID_PARAMS;
176 break;
177 }
178 return ret;
179 }
180
GetSymKeyGeneratorClass(void)181 static const char *GetSymKeyGeneratorClass(void)
182 {
183 return "HcfSymKeyGenerator";
184 }
185
GetAlgoName(HcfSymKeyGenerator * self)186 static const char *GetAlgoName(HcfSymKeyGenerator *self)
187 {
188 if (self == NULL) {
189 LOGE("The input self ptr is NULL!");
190 return NULL;
191 }
192 if (!HcfIsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
193 LOGE("Class is not match!");
194 return NULL;
195 }
196 return ((HcfSymmKeyGeneratorImpl *)self)->algoName;
197 }
198
DestroySymmKeyGenerator(HcfObjectBase * base)199 static void DestroySymmKeyGenerator(HcfObjectBase *base)
200 {
201 if (base == NULL) {
202 return;
203 }
204 if (!HcfIsClassMatch((HcfObjectBase *)base, GetSymKeyGeneratorClass())) {
205 LOGE("Class is not match.");
206 return;
207 }
208 HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)base;
209 HcfObjDestroy(impl->spiObj);
210 impl->spiObj = NULL;
211 HcfFree(impl);
212 }
213
GenerateSymmKey(HcfSymKeyGenerator * self,HcfSymKey ** symmKey)214 static HcfResult GenerateSymmKey(HcfSymKeyGenerator *self, HcfSymKey **symmKey)
215 {
216 if ((self == NULL) || (symmKey == NULL)) {
217 LOGE("Invalid input parameter.");
218 return HCF_INVALID_PARAMS;
219 }
220
221 if (!HcfIsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
222 LOGE("Class is not match.");
223 return HCF_INVALID_PARAMS;
224 }
225 HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
226 if (impl->spiObj == NULL || impl->spiObj->engineGenerateSymmKey == NULL) {
227 LOGE("Invalid input parameter.");
228 return HCF_INVALID_PARAMS;
229 }
230
231 return impl->spiObj->engineGenerateSymmKey(impl->spiObj, symmKey);
232 }
233
ConvertSymmKey(HcfSymKeyGenerator * self,const HcfBlob * key,HcfSymKey ** symmKey)234 static HcfResult ConvertSymmKey(HcfSymKeyGenerator *self, const HcfBlob *key, HcfSymKey **symmKey)
235 {
236 if ((self == NULL) || (symmKey == NULL) || !HcfIsBlobValid(key)) {
237 LOGE("Invalid input parameter.");
238 return HCF_INVALID_PARAMS;
239 }
240 if (!HcfIsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
241 LOGE("Class is not match.");
242 return HCF_INVALID_PARAMS;
243 }
244 HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
245 if (impl->spiObj == NULL || impl->spiObj->engineConvertSymmKey == NULL) {
246 LOGE("Invalid input parameter.");
247 return HCF_INVALID_PARAMS;
248 }
249 return impl->spiObj->engineConvertSymmKey(impl->spiObj, key, symmKey);
250 }
251
HcfSymKeyGeneratorCreate(const char * algoName,HcfSymKeyGenerator ** returnObj)252 HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **returnObj)
253 {
254 if (!HcfIsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (returnObj == NULL)) {
255 LOGE("Invalid input params while creating symkey!");
256 return HCF_INVALID_PARAMS;
257 }
258
259 SymKeyAttr attr = {0};
260 if (ParseAndSetParameter(algoName, (void *)&attr, OnSetSymKeyParameter) != HCF_SUCCESS) {
261 LOGE("ParseAndSetParameter Failed!");
262 return HCF_NOT_SUPPORT;
263 }
264
265 const SymKeyGenFuncSet *funcSet = FindAbility(&attr);
266 if (funcSet == NULL) {
267 LOGE("FindAbility Failed!");
268 return HCF_NOT_SUPPORT;
269 }
270 HcfSymmKeyGeneratorImpl *returnGenerator = (HcfSymmKeyGeneratorImpl *)HcfMalloc(sizeof(HcfSymmKeyGeneratorImpl), 0);
271 if (returnGenerator == NULL) {
272 LOGE("Failed to allocate returnGenerator memory!");
273 return HCF_ERR_MALLOC;
274 }
275 if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
276 LOGE("Failed to copy algoName!");
277 HcfFree(returnGenerator);
278 returnGenerator = NULL;
279 return HCF_INVALID_PARAMS;
280 }
281 HcfSymKeyGeneratorSpi *spiObj = NULL;
282 HcfResult res = funcSet->createFunc(&attr, &spiObj);
283 if (res != HCF_SUCCESS) {
284 LOGE("Failed to create spi object!");
285 HcfFree(returnGenerator);
286 returnGenerator = NULL;
287 return res;
288 }
289 returnGenerator->base.generateSymKey = GenerateSymmKey;
290 returnGenerator->base.convertSymKey = ConvertSymmKey;
291 returnGenerator->base.base.destroy = DestroySymmKeyGenerator;
292 returnGenerator->base.base.getClass = GetSymKeyGeneratorClass;
293 returnGenerator->base.getAlgoName = GetAlgoName;
294 returnGenerator->spiObj = spiObj;
295
296 *returnObj = (HcfSymKeyGenerator *)returnGenerator;
297 return HCF_SUCCESS;
298 }
299