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