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