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_192 192
33 #define HMAC_KEY_SIZE_SHA1 160
34 #define HMAC_KEY_SIZE_SHA224 224
35 #define HMAC_KEY_SIZE_SHA256 256
36 #define HMAC_KEY_SIZE_SHA384 384
37 #define HMAC_KEY_SIZE_SHA512 512
38 #define HMAC_KEY_SIZE_SM3 256
39 #define HMAC_KEY_SIZE_MD5 128
40 
41 typedef HcfResult (*SymKeyGeneratorSpiCreateFunc)(SymKeyAttr *, HcfSymKeyGeneratorSpi **);
42 
43 typedef struct {
44     SymKeyGeneratorSpiCreateFunc createFunc;
45 } SymKeyGenFuncSet;
46 
47 typedef struct {
48     HcfAlgValue algo;
49     SymKeyGenFuncSet funcSet;
50 } SymKeyGenAbility;
51 
52 typedef struct {
53     HcfSymKeyGenerator base;
54     HcfSymKeyGeneratorSpi *spiObj;
55     char algoName[HCF_MAX_ALGO_NAME_LEN];
56 } HcfSymmKeyGeneratorImpl;
57 
58 static const SymKeyGenAbility SYMKEY_ABILITY_SET[] = {
59     { HCF_ALG_AES, { HcfSymKeyGeneratorSpiCreate }},
60     { HCF_ALG_SM4, { HcfSymKeyGeneratorSpiCreate }},
61     { HCF_ALG_DES, { HcfSymKeyGeneratorSpiCreate }},
62     { HCF_ALG_HMAC, { HcfSymKeyGeneratorSpiCreate }}
63 };
64 
FindAbility(SymKeyAttr * attr)65 static const SymKeyGenFuncSet *FindAbility(SymKeyAttr *attr)
66 {
67     if (attr == NULL) {
68         return NULL;
69     }
70     for (uint32_t i = 0; i < sizeof(SYMKEY_ABILITY_SET) / sizeof(SymKeyGenAbility); i++) {
71         if (SYMKEY_ABILITY_SET[i].algo == attr->algo) {
72             return &(SYMKEY_ABILITY_SET[i].funcSet);
73         }
74     }
75     LOGE("Algo not support! [Algo]: %d", attr->algo);
76     return NULL;
77 }
78 
SetKeyLength(HcfAlgParaValue value,void * attr)79 static void SetKeyLength(HcfAlgParaValue value, void *attr)
80 {
81     SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
82 
83     switch (value) {
84         case HCF_ALG_AES_128:
85             keyAttr->algo = HCF_ALG_AES;
86             keyAttr->keySize = AES_KEY_SIZE_128;
87             break;
88         case HCF_ALG_AES_192:
89             keyAttr->algo = HCF_ALG_AES;
90             keyAttr->keySize = AES_KEY_SIZE_192;
91             break;
92         case HCF_ALG_AES_256:
93             keyAttr->algo = HCF_ALG_AES;
94             keyAttr->keySize = AES_KEY_SIZE_256;
95             break;
96         case HCF_ALG_SM4_128:
97             keyAttr->algo = HCF_ALG_SM4;
98             keyAttr->keySize = SM4_KEY_SIZE_128;
99             break;
100         case HCF_ALG_3DES_192:
101             keyAttr->algo = HCF_ALG_DES;
102             keyAttr->keySize = DES_KEY_SIZE_192;
103             break;
104         default:
105             break;
106     }
107 }
108 
SetKeyType(HcfAlgParaValue value,void * attr)109 static void SetKeyType(HcfAlgParaValue value, void *attr)
110 {
111     SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
112 
113     if (value == HCF_ALG_HMAC_DEFAULT) {
114         keyAttr->algo = HCF_ALG_HMAC;
115     }
116 }
117 
SetKeyLenByDigest(HcfAlgParaValue value,void * attr)118 static void SetKeyLenByDigest(HcfAlgParaValue value, void *attr)
119 {
120     SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
121 
122     switch (value) {
123         case HCF_OPENSSL_DIGEST_SHA1:
124             keyAttr->keySize = HMAC_KEY_SIZE_SHA1;
125             break;
126         case HCF_OPENSSL_DIGEST_SHA224:
127             keyAttr->keySize = HMAC_KEY_SIZE_SHA224;
128             break;
129         case HCF_OPENSSL_DIGEST_SHA256:
130             keyAttr->keySize = HMAC_KEY_SIZE_SHA256;
131             break;
132         case HCF_OPENSSL_DIGEST_SHA384:
133             keyAttr->keySize = HMAC_KEY_SIZE_SHA384;
134             break;
135         case HCF_OPENSSL_DIGEST_SHA512:
136             keyAttr->keySize = HMAC_KEY_SIZE_SHA512;
137             break;
138         case HCF_OPENSSL_DIGEST_SM3:
139             keyAttr->keySize = HMAC_KEY_SIZE_SM3;
140             break;
141         case HCF_OPENSSL_DIGEST_MD5:
142             keyAttr->keySize = HMAC_KEY_SIZE_MD5;
143             break;
144         default:
145             // We will ignore the and 'NoHash' inputs
146             LOGE("Invalid digest input: NoHash");
147             break;
148     }
149 }
150 
OnSetSymKeyParameter(const HcfParaConfig * config,void * attr)151 static HcfResult OnSetSymKeyParameter(const HcfParaConfig* config, void *attr)
152 {
153     if ((config == NULL) || (attr == NULL)) {
154         return HCF_INVALID_PARAMS;
155     }
156     HcfResult ret = HCF_SUCCESS;
157     LOGD("Set Parameter:%s\n", config->tag);
158     switch (config->paraType) {
159         case HCF_ALG_KEY_TYPE:
160             SetKeyLength(config->paraValue, attr);
161             break;
162         case HCF_ALG_TYPE:
163             SetKeyType(config->paraValue, attr);
164             break;
165         case HCF_ALG_DIGEST:
166             SetKeyLenByDigest(config->paraValue, attr);
167             break;
168         default:
169             ret = HCF_INVALID_PARAMS;
170             break;
171     }
172     return ret;
173 }
174 
GetSymKeyGeneratorClass(void)175 static const char *GetSymKeyGeneratorClass(void)
176 {
177     return "HcfSymKeyGenerator";
178 }
179 
GetAlgoName(HcfSymKeyGenerator * self)180 static const char *GetAlgoName(HcfSymKeyGenerator *self)
181 {
182     if (self == NULL) {
183         LOGE("The input self ptr is NULL!");
184         return NULL;
185     }
186     if (!HcfIsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
187         LOGE("Class is not match!");
188         return NULL;
189     }
190     return ((HcfSymmKeyGeneratorImpl *)self)->algoName;
191 }
192 
DestroySymmKeyGenerator(HcfObjectBase * base)193 static void DestroySymmKeyGenerator(HcfObjectBase *base)
194 {
195     if (base == NULL) {
196         return;
197     }
198     if (!HcfIsClassMatch((HcfObjectBase *)base, GetSymKeyGeneratorClass())) {
199         LOGE("Class is not match.");
200         return;
201     }
202     HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)base;
203     HcfObjDestroy(impl->spiObj);
204     HcfFree(impl);
205 }
206 
GenerateSymmKey(HcfSymKeyGenerator * self,HcfSymKey ** symmKey)207 static HcfResult GenerateSymmKey(HcfSymKeyGenerator *self, HcfSymKey **symmKey)
208 {
209     if ((self == NULL) || (symmKey == NULL)) {
210         LOGE("Invalid input parameter.");
211         return HCF_INVALID_PARAMS;
212     }
213 
214     if (!HcfIsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
215         LOGE("Class is not match.");
216         return HCF_INVALID_PARAMS;
217     }
218     HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
219     if (impl->spiObj == NULL || impl->spiObj->engineGenerateSymmKey == NULL) {
220         LOGE("Invalid input parameter.");
221         return HCF_INVALID_PARAMS;
222     }
223 
224     return impl->spiObj->engineGenerateSymmKey(impl->spiObj, symmKey);
225 }
226 
ConvertSymmKey(HcfSymKeyGenerator * self,const HcfBlob * key,HcfSymKey ** symmKey)227 static HcfResult ConvertSymmKey(HcfSymKeyGenerator *self, const HcfBlob *key, HcfSymKey **symmKey)
228 {
229     if ((self == NULL) || (symmKey == NULL) || !HcfIsBlobValid(key)) {
230         LOGE("Invalid input parameter.");
231         return HCF_INVALID_PARAMS;
232     }
233     if (!HcfIsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
234         LOGE("Class is not match.");
235         return HCF_INVALID_PARAMS;
236     }
237     HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
238     if (impl->spiObj == NULL || impl->spiObj->engineConvertSymmKey == NULL) {
239         LOGE("Invalid input parameter.");
240         return HCF_INVALID_PARAMS;
241     }
242     return impl->spiObj->engineConvertSymmKey(impl->spiObj, key, symmKey);
243 }
244 
HcfSymKeyGeneratorCreate(const char * algoName,HcfSymKeyGenerator ** returnObj)245 HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **returnObj)
246 {
247     if (!HcfIsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (returnObj == NULL)) {
248         LOGE("Invalid input params while creating symkey!");
249         return HCF_INVALID_PARAMS;
250     }
251 
252     SymKeyAttr attr = {0};
253     if (ParseAndSetParameter(algoName, (void *)&attr, OnSetSymKeyParameter) != HCF_SUCCESS) {
254         LOGE("ParseAndSetParameter Failed!");
255         return HCF_NOT_SUPPORT;
256     }
257 
258     const SymKeyGenFuncSet *funcSet = FindAbility(&attr);
259     if (funcSet == NULL) {
260         LOGE("FindAbility Failed!");
261         return HCF_NOT_SUPPORT;
262     }
263     HcfSymmKeyGeneratorImpl *returnGenerator = (HcfSymmKeyGeneratorImpl *)HcfMalloc(sizeof(HcfSymmKeyGeneratorImpl), 0);
264     if (returnGenerator == NULL) {
265         LOGE("Failed to allocate returnGenerator memory!");
266         return HCF_ERR_MALLOC;
267     }
268     if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName)) {
269         LOGE("Failed to copy algoName!");
270         HcfFree(returnGenerator);
271         return HCF_INVALID_PARAMS;
272     }
273     HcfSymKeyGeneratorSpi *spiObj = NULL;
274     HcfResult res = funcSet->createFunc(&attr, &spiObj);
275     if (res != HCF_SUCCESS) {
276         LOGE("Failed to create spi object!");
277         HcfFree(returnGenerator);
278         return res;
279     }
280     returnGenerator->base.generateSymKey = GenerateSymmKey;
281     returnGenerator->base.convertSymKey = ConvertSymmKey;
282     returnGenerator->base.base.destroy = DestroySymmKeyGenerator;
283     returnGenerator->base.base.getClass = GetSymKeyGeneratorClass;
284     returnGenerator->base.getAlgoName = GetAlgoName;
285     returnGenerator->spiObj = spiObj;
286 
287     *returnObj = (HcfSymKeyGenerator *)returnGenerator;
288     return HCF_SUCCESS;
289 }
290