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
34 typedef HcfResult (*SymKeyGeneratorSpiCreateFunc)(SymKeyAttr *, HcfSymKeyGeneratorSpi **);
35
36 typedef struct {
37 SymKeyGeneratorSpiCreateFunc createFunc;
38 } SymKeyGenFuncSet;
39
40 typedef struct {
41 HcfAlgValue algo;
42 SymKeyGenFuncSet funcSet;
43 } SymKeyGenAbility;
44
45 typedef struct {
46 HcfSymKeyGenerator base;
47 HcfSymKeyGeneratorSpi *spiObj;
48 char algoName[HCF_MAX_ALGO_NAME_LEN];
49 } HcfSymmKeyGeneratorImpl;
50
51 static const SymKeyGenAbility SYMKEY_ABILITY_SET[] = {
52 { HCF_ALG_AES, { HcfSymKeyGeneratorSpiCreate }},
53 { HCF_ALG_SM4, { HcfSymKeyGeneratorSpiCreate }},
54 { HCF_ALG_DES, { HcfSymKeyGeneratorSpiCreate }}
55 };
56
FindAbility(SymKeyAttr * attr)57 static const SymKeyGenFuncSet *FindAbility(SymKeyAttr *attr)
58 {
59 if (attr == NULL) {
60 return NULL;
61 }
62 for (uint32_t i = 0; i < sizeof(SYMKEY_ABILITY_SET) / sizeof(SymKeyGenAbility); i++) {
63 if (SYMKEY_ABILITY_SET[i].algo == attr->algo) {
64 return &(SYMKEY_ABILITY_SET[i].funcSet);
65 }
66 }
67 LOGE("Algo not support! [Algo]: %d", attr->algo);
68 return NULL;
69 }
70
SetKeyLength(HcfAlgParaValue value,void * attr)71 static void SetKeyLength(HcfAlgParaValue value, void *attr)
72 {
73 SymKeyAttr *keyAttr = (SymKeyAttr *)attr;
74
75 switch (value) {
76 case HCF_ALG_AES_128:
77 keyAttr->algo = HCF_ALG_AES;
78 keyAttr->keySize = AES_KEY_SIZE_128;
79 break;
80 case HCF_ALG_AES_192:
81 keyAttr->algo = HCF_ALG_AES;
82 keyAttr->keySize = AES_KEY_SIZE_192;
83 break;
84 case HCF_ALG_AES_256:
85 keyAttr->algo = HCF_ALG_AES;
86 keyAttr->keySize = AES_KEY_SIZE_256;
87 break;
88 case HCF_ALG_SM4_128:
89 keyAttr->algo = HCF_ALG_SM4;
90 keyAttr->keySize = SM4_KEY_SIZE_128;
91 break;
92 case HCF_ALG_3DES_192:
93 keyAttr->algo = HCF_ALG_DES;
94 keyAttr->keySize = DES_KEY_SIZE_192;
95 break;
96 default:
97 break;
98 }
99 }
100
OnSetSymKeyParameter(const HcfParaConfig * config,void * attr)101 static HcfResult OnSetSymKeyParameter(const HcfParaConfig* config, void *attr)
102 {
103 if ((config == NULL) || (attr == NULL)) {
104 return HCF_INVALID_PARAMS;
105 }
106 HcfResult ret = HCF_SUCCESS;
107 LOGD("Set Parameter:%s\n", config->tag);
108 switch (config->paraType) {
109 case HCF_ALG_KEY_TYPE:
110 SetKeyLength(config->paraValue, attr);
111 break;
112 default:
113 ret = HCF_INVALID_PARAMS;
114 break;
115 }
116 return ret;
117 }
118
GetSymKeyGeneratorClass(void)119 static const char *GetSymKeyGeneratorClass(void)
120 {
121 return "HcfSymKeyGenerator";
122 }
123
GetAlgoName(HcfSymKeyGenerator * self)124 static const char *GetAlgoName(HcfSymKeyGenerator *self)
125 {
126 if (self == NULL) {
127 LOGE("The input self ptr is NULL!");
128 return NULL;
129 }
130 if (!IsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
131 LOGE("Class is not match!");
132 return NULL;
133 }
134 return ((HcfSymmKeyGeneratorImpl *)self)->algoName;
135 }
136
DestroySymmKeyGenerator(HcfObjectBase * base)137 static void DestroySymmKeyGenerator(HcfObjectBase *base)
138 {
139 if (base == NULL) {
140 return;
141 }
142 if (!IsClassMatch((HcfObjectBase *)base, GetSymKeyGeneratorClass())) {
143 LOGE("Class is not match.");
144 return;
145 }
146 HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)base;
147 HcfObjDestroy(impl->spiObj);
148 HcfFree(impl);
149 }
150
GenerateSymmKey(HcfSymKeyGenerator * self,HcfSymKey ** symmKey)151 static HcfResult GenerateSymmKey(HcfSymKeyGenerator *self, HcfSymKey **symmKey)
152 {
153 if ((self == NULL) || (symmKey == NULL)) {
154 LOGE("Invalid input parameter.");
155 return HCF_INVALID_PARAMS;
156 }
157
158 if (!IsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
159 LOGE("Class is not match.");
160 return HCF_INVALID_PARAMS;
161 }
162 HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
163
164 return impl->spiObj->engineGenerateSymmKey(impl->spiObj, symmKey);
165 }
166
ConvertSymmKey(HcfSymKeyGenerator * self,const HcfBlob * key,HcfSymKey ** symmKey)167 static HcfResult ConvertSymmKey(HcfSymKeyGenerator *self, const HcfBlob *key, HcfSymKey **symmKey)
168 {
169 if ((self == NULL) || (symmKey == NULL) || !IsBlobValid(key)) {
170 LOGE("Invalid input parameter.");
171 return HCF_INVALID_PARAMS;
172 }
173 if (!IsClassMatch((HcfObjectBase *)self, GetSymKeyGeneratorClass())) {
174 LOGE("Class is not match.");
175 return HCF_INVALID_PARAMS;
176 }
177 HcfSymmKeyGeneratorImpl *impl = (HcfSymmKeyGeneratorImpl *)self;
178
179 return impl->spiObj->engineConvertSymmKey(impl->spiObj, key, symmKey);
180 }
181
HcfSymKeyGeneratorCreate(const char * algoName,HcfSymKeyGenerator ** returnObj)182 HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **returnObj)
183 {
184 if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (returnObj == NULL)) {
185 LOGE("Invalid input params while creating symkey!");
186 return HCF_INVALID_PARAMS;
187 }
188
189 SymKeyAttr attr = {0};
190 if (ParseAndSetParameter(algoName, (void *)&attr, OnSetSymKeyParameter) != HCF_SUCCESS) {
191 LOGE("ParseAndSetParameter Failed!");
192 return HCF_NOT_SUPPORT;
193 }
194
195 const SymKeyGenFuncSet *funcSet = FindAbility(&attr);
196 if (funcSet == NULL) {
197 LOGE("FindAbility Failed!");
198 return HCF_NOT_SUPPORT;
199 }
200 HcfSymmKeyGeneratorImpl *returnGenerator = (HcfSymmKeyGeneratorImpl *)HcfMalloc(sizeof(HcfSymmKeyGeneratorImpl), 0);
201 if (returnGenerator == NULL) {
202 LOGE("Failed to allocate returnGenerator memory!");
203 return HCF_ERR_MALLOC;
204 }
205 if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName)) {
206 LOGE("Failed to copy algoName!");
207 HcfFree(returnGenerator);
208 return HCF_INVALID_PARAMS;
209 }
210 HcfSymKeyGeneratorSpi *spiObj = NULL;
211 HcfResult res = funcSet->createFunc(&attr, &spiObj);
212 if (res != HCF_SUCCESS) {
213 LOGE("Failed to create spi object!");
214 HcfFree(returnGenerator);
215 return res;
216 }
217 returnGenerator->base.generateSymKey = GenerateSymmKey;
218 returnGenerator->base.convertSymKey = ConvertSymmKey;
219 returnGenerator->base.base.destroy = DestroySymmKeyGenerator;
220 returnGenerator->base.base.getClass = GetSymKeyGeneratorClass;
221 returnGenerator->base.getAlgoName = GetAlgoName;
222 returnGenerator->spiObj = spiObj;
223
224 *returnObj = (HcfSymKeyGenerator *)returnGenerator;
225 return HCF_SUCCESS;
226 }
227