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 "asy_key_generator.h"
17
18 #include <securec.h>
19
20 #include "asy_key_generator_spi.h"
21 #include "config.h"
22 #include "detailed_dsa_key_params.h"
23 #include "detailed_rsa_key_params.h"
24 #include "detailed_ecc_key_params.h"
25 #include "dsa_asy_key_generator_openssl.h"
26 #include "ecc_asy_key_generator_openssl.h"
27 #include "params_parser.h"
28 #include "rsa_asy_key_generator_openssl.h"
29 #include "sm2_asy_key_generator_openssl.h"
30 #include "log.h"
31 #include "memory.h"
32 #include "utils.h"
33
34 #define ALG_NAME_DSA "DSA"
35 #define ALG_NAME_ECC "ECC"
36 #define ALG_NAME_RSA "RSA"
37 #define ASY_KEY_GENERATOR_CLASS "HcfAsyKeyGenerator"
38 #define ASY_KEY_GENERATOR_BY_SPEC_CLASS "HcfAsyKeyGeneratorBySpec"
39
40 typedef HcfResult (*HcfAsyKeyGeneratorSpiCreateFunc)(HcfAsyKeyGenParams *, HcfAsyKeyGeneratorSpi **);
41
42 typedef struct {
43 HcfAsyKeyGenerator base;
44
45 HcfAsyKeyGeneratorSpi *spiObj;
46
47 char algoName[HCF_MAX_ALGO_NAME_LEN];
48 } HcfAsyKeyGeneratorImpl;
49
50 typedef struct {
51 HcfAsyKeyGeneratorBySpec base;
52
53 HcfAsyKeyGeneratorSpi *spiObj;
54
55 HcfAsyKeyParamsSpec *paramsSpec;
56 } HcfAsyKeyGeneratorBySpecImpl;
57
58 typedef struct {
59 HcfAlgValue algo;
60
61 HcfAsyKeyGeneratorSpiCreateFunc createSpiFunc;
62 } HcfAsyKeyGenAbility;
63
64 static const HcfAsyKeyGenAbility ASY_KEY_GEN_ABILITY_SET[] = {
65 { HCF_ALG_RSA, HcfAsyKeyGeneratorSpiRsaCreate },
66 { HCF_ALG_ECC, HcfAsyKeyGeneratorSpiEccCreate },
67 { HCF_ALG_DSA, HcfAsyKeyGeneratorSpiDsaCreate },
68 { HCF_ALG_SM2, HcfAsyKeyGeneratorSpiSm2Create }
69 };
70
71 typedef struct {
72 HcfAlgParaValue value;
73 int32_t bits; // keyLen
74 HcfAlgValue algo; // algType
75 } KeyTypeAlg;
76
77 static const KeyTypeAlg KEY_TYPE_MAP[] = {
78 { HCF_ALG_ECC_224, HCF_ALG_ECC_224, HCF_ALG_ECC },
79 { HCF_ALG_ECC_256, HCF_ALG_ECC_256, HCF_ALG_ECC },
80 { HCF_ALG_ECC_384, HCF_ALG_ECC_384, HCF_ALG_ECC },
81 { HCF_ALG_ECC_521, HCF_ALG_ECC_521, HCF_ALG_ECC },
82 { HCF_OPENSSL_RSA_512, HCF_RSA_KEY_SIZE_512, HCF_ALG_RSA },
83 { HCF_OPENSSL_RSA_768, HCF_RSA_KEY_SIZE_768, HCF_ALG_RSA },
84 { HCF_OPENSSL_RSA_1024, HCF_RSA_KEY_SIZE_1024, HCF_ALG_RSA },
85 { HCF_OPENSSL_RSA_2048, HCF_RSA_KEY_SIZE_2048, HCF_ALG_RSA },
86 { HCF_OPENSSL_RSA_3072, HCF_RSA_KEY_SIZE_3072, HCF_ALG_RSA },
87 { HCF_OPENSSL_RSA_4096, HCF_RSA_KEY_SIZE_4096, HCF_ALG_RSA },
88 { HCF_OPENSSL_RSA_4096, HCF_RSA_KEY_SIZE_4096, HCF_ALG_RSA },
89 { HCF_OPENSSL_RSA_8192, HCF_RSA_KEY_SIZE_8192, HCF_ALG_RSA },
90 { HCF_ALG_DSA_1024, HCF_DSA_KEY_SIZE_1024, HCF_ALG_DSA },
91 { HCF_ALG_DSA_2048, HCF_DSA_KEY_SIZE_2048, HCF_ALG_DSA },
92 { HCF_ALG_DSA_3072, HCF_DSA_KEY_SIZE_3072, HCF_ALG_DSA },
93 { HCF_ALG_SM2_256, HCF_ALG_SM2_256, HCF_ALG_SM2 }
94 };
IsDsaCommParamsSpecValid(HcfDsaCommParamsSpec * paramsSpec)95 static bool IsDsaCommParamsSpecValid(HcfDsaCommParamsSpec *paramsSpec)
96 {
97 if ((paramsSpec->p.data == NULL) || (paramsSpec->p.len == 0)) {
98 LOGE("BigInteger p is invalid");
99 return false;
100 }
101 if ((paramsSpec->q.data == NULL) || (paramsSpec->q.len == 0)) {
102 LOGE("BigInteger q is invalid");
103 return false;
104 }
105 if ((paramsSpec->g.data == NULL) || (paramsSpec->g.len == 0)) {
106 LOGE("BigInteger g is invalid");
107 return false;
108 }
109 return true;
110 }
111
IsDsaPubKeySpecValid(HcfDsaPubKeyParamsSpec * paramsSpec)112 static bool IsDsaPubKeySpecValid(HcfDsaPubKeyParamsSpec *paramsSpec)
113 {
114 if (!IsDsaCommParamsSpecValid(&(paramsSpec->base))) {
115 return false;
116 }
117 if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) {
118 LOGE("BigInteger pk is invalid");
119 return false;
120 }
121 return true;
122 }
123
IsDsaKeyPairSpecValid(HcfDsaKeyPairParamsSpec * paramsSpec)124 static bool IsDsaKeyPairSpecValid(HcfDsaKeyPairParamsSpec *paramsSpec)
125 {
126 if (!IsDsaCommParamsSpecValid(&(paramsSpec->base))) {
127 return false;
128 }
129 if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) {
130 LOGE("BigInteger pk is invalid");
131 return false;
132 }
133 if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) {
134 LOGE("BigInteger sk is invalid");
135 return false;
136 }
137 return true;
138 }
139
IsDsaParamsSpecValid(const HcfAsyKeyParamsSpec * paramsSpec)140 static bool IsDsaParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec)
141 {
142 bool ret = false;
143 switch (paramsSpec->specType) {
144 case HCF_COMMON_PARAMS_SPEC:
145 ret = IsDsaCommParamsSpecValid((HcfDsaCommParamsSpec *)paramsSpec);
146 break;
147 case HCF_PUBLIC_KEY_SPEC:
148 ret = IsDsaPubKeySpecValid((HcfDsaPubKeyParamsSpec *)paramsSpec);
149 break;
150 case HCF_KEY_PAIR_SPEC:
151 ret = IsDsaKeyPairSpecValid((HcfDsaKeyPairParamsSpec *)paramsSpec);
152 break;
153 default:
154 LOGE("SpecType not support! [SpecType]: %d", paramsSpec->specType);
155 break;
156 }
157 return ret;
158 }
159
IsEccCommParamsSpecValid(HcfEccCommParamsSpec * paramsSpec)160 static bool IsEccCommParamsSpecValid(HcfEccCommParamsSpec *paramsSpec)
161 {
162 if ((paramsSpec->a.data == NULL) || (paramsSpec->a.len == 0)) {
163 LOGE("BigInteger a is invalid");
164 return false;
165 }
166 if ((paramsSpec->b.data == NULL) || (paramsSpec->b.len == 0)) {
167 LOGE("BigInteger b is invalid");
168 return false;
169 }
170 if ((paramsSpec->n.data == NULL) || (paramsSpec->n.len == 0)) {
171 LOGE("BigInteger n is invalid");
172 return false;
173 }
174 if ((paramsSpec->g.x.data == NULL) || (paramsSpec->g.x.len == 0) ||
175 (paramsSpec->g.y.data == NULL) || (paramsSpec->g.y.len == 0)) {
176 LOGE("Point g is invalid");
177 return false;
178 }
179 if (paramsSpec->field == NULL) {
180 LOGE("Field is null.");
181 return false;
182 }
183 if (strcmp(paramsSpec->field->fieldType, "Fp") != 0) {
184 LOGE("Unknown field type.");
185 return false;
186 }
187 HcfECFieldFp *tmp = (HcfECFieldFp *)(paramsSpec->field);
188 if ((tmp->p.data == NULL) || (tmp->p.len == 0)) {
189 LOGE("EcFieldFp p is invalid");
190 return false;
191 }
192 return true;
193 }
194
IsEccPriKeySpecValid(HcfEccPriKeyParamsSpec * paramsSpec)195 static bool IsEccPriKeySpecValid(HcfEccPriKeyParamsSpec *paramsSpec)
196 {
197 if (!IsEccCommParamsSpecValid(&(paramsSpec->base))) {
198 return false;
199 }
200 if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) {
201 LOGE("BigInteger sk is invalid");
202 return false;
203 }
204 return true;
205 }
206
IsEccPubKeySpecValid(HcfEccPubKeyParamsSpec * paramsSpec)207 static bool IsEccPubKeySpecValid(HcfEccPubKeyParamsSpec *paramsSpec)
208 {
209 if (!IsEccCommParamsSpecValid(&(paramsSpec->base))) {
210 return false;
211 }
212 if ((paramsSpec->pk.x.data == NULL) || (paramsSpec->pk.x.len == 0) ||
213 (paramsSpec->pk.y.data == NULL) || (paramsSpec->pk.y.len == 0)) {
214 LOGE("Point pk is invalid");
215 return false;
216 }
217 return true;
218 }
219
IsEccKeyPairSpecValid(HcfEccKeyPairParamsSpec * paramsSpec)220 static bool IsEccKeyPairSpecValid(HcfEccKeyPairParamsSpec *paramsSpec)
221 {
222 if (!IsEccCommParamsSpecValid(&(paramsSpec->base))) {
223 return false;
224 }
225 if ((paramsSpec->pk.x.data == NULL) || (paramsSpec->pk.x.len == 0) ||
226 (paramsSpec->pk.y.data == NULL) || (paramsSpec->pk.y.len == 0)) {
227 LOGE("Point pk is invalid");
228 return false;
229 }
230 if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) {
231 LOGE("BigInteger sk is invalid");
232 return false;
233 }
234 return true;
235 }
236
IsEccParamsSpecValid(const HcfAsyKeyParamsSpec * paramsSpec)237 static bool IsEccParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec)
238 {
239 bool ret = false;
240 switch (paramsSpec->specType) {
241 case HCF_COMMON_PARAMS_SPEC:
242 ret = IsEccCommParamsSpecValid((HcfEccCommParamsSpec *)paramsSpec);
243 break;
244 case HCF_PRIVATE_KEY_SPEC:
245 ret = IsEccPriKeySpecValid((HcfEccPriKeyParamsSpec *)paramsSpec);
246 break;
247 case HCF_PUBLIC_KEY_SPEC:
248 ret = IsEccPubKeySpecValid((HcfEccPubKeyParamsSpec *)paramsSpec);
249 break;
250 case HCF_KEY_PAIR_SPEC:
251 ret = IsEccKeyPairSpecValid((HcfEccKeyPairParamsSpec *)paramsSpec);
252 break;
253 default:
254 LOGE("SpecType not support! [SpecType]: %d", paramsSpec->specType);
255 break;
256 }
257 return ret;
258 }
259
IsRsaCommParamsSpecValid(HcfRsaCommParamsSpec * paramsSpec)260 static bool IsRsaCommParamsSpecValid(HcfRsaCommParamsSpec *paramsSpec)
261 {
262 if ((paramsSpec->n.data == NULL) || (paramsSpec->n.len == 0)) {
263 LOGE("BigInteger n is invalid");
264 return false;
265 }
266 return true;
267 }
268
IsRsaPubKeySpecValid(HcfRsaPubKeyParamsSpec * paramsSpec)269 static bool IsRsaPubKeySpecValid(HcfRsaPubKeyParamsSpec *paramsSpec)
270 {
271 if (!IsRsaCommParamsSpecValid(&(paramsSpec->base))) {
272 return false;
273 }
274 if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) {
275 LOGE("BigInteger pk is invalid");
276 return false;
277 }
278 return true;
279 }
280
IsRsaKeyPairSpecValid(HcfRsaKeyPairParamsSpec * paramsSpec)281 static bool IsRsaKeyPairSpecValid(HcfRsaKeyPairParamsSpec *paramsSpec)
282 {
283 if (!IsRsaCommParamsSpecValid(&(paramsSpec->base))) {
284 return false;
285 }
286 if ((paramsSpec->pk.data == NULL) || (paramsSpec->pk.len == 0)) {
287 LOGE("BigInteger pk is invalid");
288 return false;
289 }
290 if ((paramsSpec->sk.data == NULL) || (paramsSpec->sk.len == 0)) {
291 LOGE("BigInteger sk is invalid");
292 return false;
293 }
294 return true;
295 }
296
IsRsaParamsSpecValid(const HcfAsyKeyParamsSpec * paramsSpec)297 static bool IsRsaParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec)
298 {
299 bool ret = false;
300 switch (paramsSpec->specType) {
301 case HCF_COMMON_PARAMS_SPEC:
302 ret = IsRsaCommParamsSpecValid((HcfRsaCommParamsSpec *)paramsSpec);
303 break;
304 case HCF_PUBLIC_KEY_SPEC:
305 ret = IsRsaPubKeySpecValid((HcfRsaPubKeyParamsSpec *)paramsSpec);
306 break;
307 case HCF_KEY_PAIR_SPEC:
308 ret = IsRsaKeyPairSpecValid((HcfRsaKeyPairParamsSpec *)paramsSpec);
309 break;
310 default:
311 LOGE("SpecType not support! [SpecType]: %d", paramsSpec->specType);
312 break;
313 }
314 return ret;
315 }
316
IsParamsSpecValid(const HcfAsyKeyParamsSpec * paramsSpec)317 static bool IsParamsSpecValid(const HcfAsyKeyParamsSpec *paramsSpec)
318 {
319 if ((paramsSpec == NULL) || (paramsSpec->algName == NULL)) {
320 LOGE("Params spec is null");
321 return false;
322 }
323 if (strcmp(paramsSpec->algName, ALG_NAME_DSA) == 0) {
324 return IsDsaParamsSpecValid(paramsSpec);
325 } else if (strcmp(paramsSpec->algName, ALG_NAME_ECC) == 0) {
326 return IsEccParamsSpecValid(paramsSpec);
327 } else if (strcmp(paramsSpec->algName, ALG_NAME_RSA) == 0) {
328 return IsRsaParamsSpecValid(paramsSpec);
329 } else {
330 LOGE("AlgName not support! [AlgName]: %s", paramsSpec->algName);
331 return false;
332 }
333 }
334
FindAbility(HcfAsyKeyGenParams * params)335 static HcfAsyKeyGeneratorSpiCreateFunc FindAbility(HcfAsyKeyGenParams *params)
336 {
337 for (uint32_t i = 0; i < sizeof(ASY_KEY_GEN_ABILITY_SET) / sizeof(ASY_KEY_GEN_ABILITY_SET[0]); i++) {
338 if (ASY_KEY_GEN_ABILITY_SET[i].algo == params->algo) {
339 return ASY_KEY_GEN_ABILITY_SET[i].createSpiFunc;
340 }
341 }
342 LOGE("Algo not support! [Algo]: %d", params->algo);
343 return NULL;
344 }
345
SetPrimes(HcfAlgParaValue value,HcfAsyKeyGenParams * params)346 static void SetPrimes(HcfAlgParaValue value, HcfAsyKeyGenParams *params)
347 {
348 if (params == NULL) {
349 LOGE("params is null.");
350 return;
351 }
352 switch (value) {
353 case HCF_OPENSSL_PRIMES_2:
354 params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_2;
355 break;
356 case HCF_OPENSSL_PRIMES_3:
357 params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_3;
358 break;
359 case HCF_OPENSSL_PRIMES_4:
360 params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_4;
361 break;
362 case HCF_OPENSSL_PRIMES_5:
363 params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_5;
364 break;
365 default:
366 params->primes = (int32_t)HCF_RSA_PRIMES_SIZE_2; // default primes is 2
367 LOGD("user default primes 2");
368 break;
369 }
370 LOGD("Set primes:%d!", params->primes);
371 }
372
SetKeyType(HcfAlgParaValue value,HcfAsyKeyGenParams * params)373 static void SetKeyType(HcfAlgParaValue value, HcfAsyKeyGenParams *params)
374 {
375 for (uint32_t i = 0; i < sizeof(KEY_TYPE_MAP) / sizeof(KEY_TYPE_MAP[0]); i++) {
376 if (KEY_TYPE_MAP[i].value == value) {
377 params->bits = KEY_TYPE_MAP[i].bits;
378 params->algo = KEY_TYPE_MAP[i].algo;
379 return;
380 }
381 }
382 LOGE("There is not matched algorithm.");
383 }
384
ParseAsyKeyGenParams(const HcfParaConfig * config,void * params)385 static HcfResult ParseAsyKeyGenParams(const HcfParaConfig* config, void *params)
386 {
387 if (config == NULL || params == NULL) {
388 return HCF_INVALID_PARAMS;
389 }
390 HcfResult ret = HCF_SUCCESS;
391 HcfAsyKeyGenParams *paramsObj = (HcfAsyKeyGenParams *)params;
392 LOGD("Set Parameter: %s", config->tag);
393 switch (config->paraType) {
394 case HCF_ALG_KEY_TYPE:
395 SetKeyType(config->paraValue, paramsObj);
396 break;
397 case HCF_ALG_PRIMES:
398 SetPrimes(config->paraValue, paramsObj);
399 break;
400 default:
401 ret = HCF_INVALID_PARAMS;
402 break;
403 }
404 return ret;
405 }
406
CopyAsyKeyParamsSpec(const HcfAsyKeyParamsSpec * srcSpec,HcfAsyKeyParamsSpec * destSpec)407 static HcfResult CopyAsyKeyParamsSpec(const HcfAsyKeyParamsSpec *srcSpec, HcfAsyKeyParamsSpec *destSpec)
408 {
409 int srcAlgNameLen = strlen(srcSpec->algName);
410 destSpec->algName = (char *)HcfMalloc(srcAlgNameLen + 1, 0);
411 if (destSpec->algName == NULL) {
412 LOGE("Failed to allocate alg name memory");
413 return HCF_ERR_MALLOC;
414 }
415 (void)memcpy_s(destSpec->algName, srcAlgNameLen, srcSpec->algName, srcAlgNameLen);
416 destSpec->specType = srcSpec->specType;
417 return HCF_SUCCESS;
418 }
419
CopyDsaCommonSpec(const HcfDsaCommParamsSpec * srcSpec,HcfDsaCommParamsSpec * destSpec)420 static HcfResult CopyDsaCommonSpec(const HcfDsaCommParamsSpec *srcSpec, HcfDsaCommParamsSpec *destSpec)
421 {
422 if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) {
423 return HCF_INVALID_PARAMS;
424 }
425 destSpec->p.data = (unsigned char *)HcfMalloc(srcSpec->p.len, 0);
426 if (destSpec->p.data == NULL) {
427 LOGE("Failed to allocate p data memory");
428 FreeDsaCommParamsSpec(destSpec);
429 return HCF_ERR_MALLOC;
430 }
431 destSpec->q.data = (unsigned char *)HcfMalloc(srcSpec->q.len, 0);
432 if (destSpec->q.data == NULL) {
433 LOGE("Failed to allocate q data memory");
434 FreeDsaCommParamsSpec(destSpec);
435 return HCF_ERR_MALLOC;
436 }
437 destSpec->g.data = (unsigned char *)HcfMalloc(srcSpec->g.len, 0);
438 if (destSpec->g.data == NULL) {
439 LOGE("Failed to allocate g data memory");
440 FreeDsaCommParamsSpec(destSpec);
441 return HCF_ERR_MALLOC;
442 }
443 (void)memcpy_s(destSpec->p.data, srcSpec->p.len, srcSpec->p.data, srcSpec->p.len);
444 (void)memcpy_s(destSpec->q.data, srcSpec->q.len, srcSpec->q.data, srcSpec->q.len);
445 (void)memcpy_s(destSpec->g.data, srcSpec->g.len, srcSpec->g.data, srcSpec->g.len);
446 destSpec->p.len = srcSpec->p.len;
447 destSpec->q.len = srcSpec->q.len;
448 destSpec->g.len = srcSpec->g.len;
449 return HCF_SUCCESS;
450 }
451
CreateDsaCommonSpecImpl(const HcfDsaCommParamsSpec * srcSpec,HcfDsaCommParamsSpec ** destSpec)452 static HcfResult CreateDsaCommonSpecImpl(const HcfDsaCommParamsSpec *srcSpec, HcfDsaCommParamsSpec **destSpec)
453 {
454 HcfDsaCommParamsSpec *spec = (HcfDsaCommParamsSpec *)HcfMalloc(sizeof(HcfDsaCommParamsSpec), 0);
455 if (spec == NULL) {
456 LOGE("Failed to allocate dest spec memory");
457 return HCF_ERR_MALLOC;
458 }
459
460 if (CopyDsaCommonSpec(srcSpec, spec) != HCF_SUCCESS) {
461 HcfFree(spec);
462 return HCF_INVALID_PARAMS;
463 }
464
465 *destSpec = spec;
466 return HCF_SUCCESS;
467 }
468
CreateDsaPubKeySpecImpl(const HcfDsaPubKeyParamsSpec * srcSpec,HcfDsaPubKeyParamsSpec ** destSpec)469 static HcfResult CreateDsaPubKeySpecImpl(const HcfDsaPubKeyParamsSpec *srcSpec, HcfDsaPubKeyParamsSpec **destSpec)
470 {
471 HcfDsaPubKeyParamsSpec *spec = (HcfDsaPubKeyParamsSpec *)HcfMalloc(sizeof(HcfDsaPubKeyParamsSpec), 0);
472 if (spec == NULL) {
473 LOGE("Failed to allocate dest spec memory");
474 return HCF_ERR_MALLOC;
475 }
476 if (CopyDsaCommonSpec(&(srcSpec->base), &(spec->base)) != HCF_SUCCESS) {
477 HcfFree(spec);
478 return HCF_INVALID_PARAMS;
479 }
480 spec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0);
481 if (spec->pk.data == NULL) {
482 LOGE("Failed to allocate public key memory");
483 FreeDsaCommParamsSpec(&(spec->base));
484 HcfFree(spec);
485 return HCF_ERR_MALLOC;
486 }
487 (void)memcpy_s(spec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len);
488 spec->pk.len = srcSpec->pk.len;
489
490 *destSpec = spec;
491 return HCF_SUCCESS;
492 }
493
CreateDsaKeyPairSpecImpl(const HcfDsaKeyPairParamsSpec * srcSpec,HcfDsaKeyPairParamsSpec ** destSpec)494 static HcfResult CreateDsaKeyPairSpecImpl(const HcfDsaKeyPairParamsSpec *srcSpec, HcfDsaKeyPairParamsSpec **destSpec)
495 {
496 HcfDsaKeyPairParamsSpec *spec = (HcfDsaKeyPairParamsSpec *)HcfMalloc(sizeof(HcfDsaKeyPairParamsSpec), 0);
497 if (spec == NULL) {
498 LOGE("Failed to allocate dest spec memory");
499 return HCF_ERR_MALLOC;
500 }
501 if (CopyDsaCommonSpec(&(srcSpec->base), &(spec->base)) != HCF_SUCCESS) {
502 HcfFree(spec);
503 return HCF_INVALID_PARAMS;
504 }
505 spec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0);
506 if (spec->pk.data == NULL) {
507 LOGE("Failed to allocate public key memory");
508 FreeDsaCommParamsSpec(&(spec->base));
509 HcfFree(spec);
510 return HCF_ERR_MALLOC;
511 }
512 spec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0);
513 if (spec->sk.data == NULL) {
514 LOGE("Failed to allocate private key memory");
515 FreeDsaCommParamsSpec(&(spec->base));
516 HcfFree(spec->pk.data);
517 HcfFree(spec);
518 return HCF_ERR_MALLOC;
519 }
520 (void)memcpy_s(spec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len);
521 (void)memcpy_s(spec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len);
522 spec->pk.len = srcSpec->pk.len;
523 spec->sk.len = srcSpec->sk.len;
524
525 *destSpec = spec;
526 return HCF_SUCCESS;
527 }
528
CreateDsaParamsSpecImpl(const HcfAsyKeyParamsSpec * paramsSpec,HcfAsyKeyParamsSpec ** impl)529 static HcfResult CreateDsaParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, HcfAsyKeyParamsSpec **impl)
530 {
531 HcfResult ret = HCF_SUCCESS;
532 HcfDsaCommParamsSpec *spec = NULL;
533 switch (paramsSpec->specType) {
534 case HCF_COMMON_PARAMS_SPEC:
535 ret = CreateDsaCommonSpecImpl((HcfDsaCommParamsSpec *)paramsSpec, &spec);
536 break;
537 case HCF_PUBLIC_KEY_SPEC:
538 ret = CreateDsaPubKeySpecImpl((HcfDsaPubKeyParamsSpec *)paramsSpec, (HcfDsaPubKeyParamsSpec **)&spec);
539 break;
540 case HCF_KEY_PAIR_SPEC:
541 ret = CreateDsaKeyPairSpecImpl((HcfDsaKeyPairParamsSpec *)paramsSpec, (HcfDsaKeyPairParamsSpec **)&spec);
542 break;
543 default:
544 ret = HCF_INVALID_PARAMS;
545 break;
546 }
547 if (ret == HCF_SUCCESS) {
548 *impl = (HcfAsyKeyParamsSpec *)spec;
549 }
550 return ret;
551 }
552
CopyEcField(const HcfECField * src,HcfECField ** dest)553 static HcfResult CopyEcField(const HcfECField *src, HcfECField **dest)
554 {
555 HcfECField *tmpField = (HcfECField *)HcfMalloc(sizeof(HcfECFieldFp), 0);
556 if (tmpField == NULL) {
557 LOGE("Alloc memory failed.");
558 return HCF_ERR_MALLOC;
559 }
560
561 int32_t srcFieldTypeLen = strlen(src->fieldType);
562 tmpField->fieldType = (char *)HcfMalloc(srcFieldTypeLen + 1, 0);
563 if (tmpField->fieldType == NULL) {
564 LOGE("Failed to allocate field memory.");
565 HcfFree(tmpField);
566 return HCF_ERR_MALLOC;
567 }
568 HcfECFieldFp *tmpDest = (HcfECFieldFp *)(tmpField);
569 HcfECFieldFp *tmpSrc = (HcfECFieldFp *)(src);
570 tmpDest->p.data = (unsigned char *)HcfMalloc(tmpSrc->p.len, 0);
571 if (tmpDest->p.data == NULL) {
572 LOGE("Failed to allocate b data memory");
573 HcfFree(tmpField->fieldType);
574 HcfFree(tmpField);
575 return HCF_ERR_MALLOC;
576 }
577 (void)memcpy_s(tmpField->fieldType, srcFieldTypeLen, src->fieldType, srcFieldTypeLen);
578 (void)memcpy_s(tmpDest->p.data, tmpSrc->p.len, tmpSrc->p.data, tmpSrc->p.len);
579 tmpDest->p.len = tmpSrc->p.len;
580
581 *dest = tmpField;
582 return HCF_SUCCESS;
583 }
584
CopyPoint(const HcfPoint * src,HcfPoint * dest)585 static HcfResult CopyPoint(const HcfPoint *src, HcfPoint *dest)
586 {
587 dest->x.data = (unsigned char *)HcfMalloc(src->x.len, 0);
588 if (dest->x.data == NULL) {
589 LOGE("Failed to allocate x data memory");
590 return HCF_ERR_MALLOC;
591 }
592 dest->y.data = (unsigned char *)HcfMalloc(src->y.len, 0);
593 if (dest->y.data == NULL) {
594 LOGE("Failed to allocate y data memory");
595 HcfFree(dest->x.data);
596 dest->x.data = NULL;
597 return HCF_ERR_MALLOC;
598 }
599 (void)memcpy_s(dest->x.data, src->x.len, src->x.data, src->x.len);
600 (void)memcpy_s(dest->y.data, src->y.len, src->y.data, src->y.len);
601 dest->x.len = src->x.len;
602 dest->y.len = src->y.len;
603 return HCF_SUCCESS;
604 }
605
CopyEccCommonSpec(const HcfEccCommParamsSpec * srcSpec,HcfEccCommParamsSpec * destSpec)606 static HcfResult CopyEccCommonSpec(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParamsSpec *destSpec)
607 {
608 if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) {
609 return HCF_INVALID_PARAMS;
610 }
611 destSpec->a.data = (unsigned char *)HcfMalloc(srcSpec->a.len, 0);
612 if (destSpec->a.data == NULL) {
613 LOGE("Failed to allocate a data memory");
614 FreeEccCommParamsSpec(destSpec);
615 return HCF_ERR_MALLOC;
616 }
617 destSpec->b.data = (unsigned char *)HcfMalloc(srcSpec->b.len, 0);
618 if (destSpec->b.data == NULL) {
619 LOGE("Failed to allocate b data memory");
620 FreeEccCommParamsSpec(destSpec);
621 return HCF_ERR_MALLOC;
622 }
623 destSpec->n.data = (unsigned char *)HcfMalloc(srcSpec->n.len, 0);
624 if (destSpec->n.data == NULL) {
625 LOGE("Failed to allocate n data memory");
626 FreeEccCommParamsSpec(destSpec);
627 return HCF_ERR_MALLOC;
628 }
629 HcfResult res = CopyEcField(srcSpec->field, &(destSpec->field));
630 if (res != HCF_SUCCESS) {
631 LOGE("Failed to allocate field data memory");
632 FreeEccCommParamsSpec(destSpec);
633 return HCF_ERR_MALLOC;
634 }
635 res = CopyPoint(&(srcSpec->g), &(destSpec->g));
636 if (res != HCF_SUCCESS) {
637 LOGE("Failed to allocate field data memory");
638 FreeEccCommParamsSpec(destSpec);
639 return HCF_ERR_MALLOC;
640 }
641 destSpec->h = srcSpec->h;
642 (void)memcpy_s(destSpec->a.data, srcSpec->a.len, srcSpec->a.data, srcSpec->a.len);
643 (void)memcpy_s(destSpec->b.data, srcSpec->b.len, srcSpec->b.data, srcSpec->b.len);
644 (void)memcpy_s(destSpec->n.data, srcSpec->n.len, srcSpec->n.data, srcSpec->n.len);
645 destSpec->a.len = srcSpec->a.len;
646 destSpec->b.len = srcSpec->b.len;
647 destSpec->n.len = srcSpec->n.len;
648 return HCF_SUCCESS;
649 }
650
CreateEccCommonSpecImpl(const HcfEccCommParamsSpec * srcSpec,HcfEccCommParamsSpec ** destSpec)651 static HcfResult CreateEccCommonSpecImpl(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParamsSpec **destSpec)
652 {
653 HcfEccCommParamsSpec *tmpSpec = (HcfEccCommParamsSpec *)HcfMalloc(sizeof(HcfEccCommParamsSpec), 0);
654 if (tmpSpec == NULL) {
655 LOGE("Failed to allocate dest spec memory");
656 return HCF_ERR_MALLOC;
657 }
658
659 if (CopyEccCommonSpec(srcSpec, tmpSpec) != HCF_SUCCESS) {
660 HcfFree(tmpSpec);
661 return HCF_INVALID_PARAMS;
662 }
663
664 *destSpec = tmpSpec;
665 return HCF_SUCCESS;
666 }
667
CreateEccPubKeySpecImpl(const HcfEccPubKeyParamsSpec * srcSpec,HcfEccPubKeyParamsSpec ** destSpec)668 static HcfResult CreateEccPubKeySpecImpl(const HcfEccPubKeyParamsSpec *srcSpec, HcfEccPubKeyParamsSpec **destSpec)
669 {
670 HcfEccPubKeyParamsSpec *tmpSpec = (HcfEccPubKeyParamsSpec *)HcfMalloc(sizeof(HcfEccPubKeyParamsSpec), 0);
671 if (tmpSpec == NULL) {
672 LOGE("Failed to allocate dest spec memory");
673 return HCF_ERR_MALLOC;
674 }
675 if (CopyEccCommonSpec(&(srcSpec->base), &(tmpSpec->base)) != HCF_SUCCESS) {
676 HcfFree(tmpSpec);
677 return HCF_INVALID_PARAMS;
678 }
679 HcfResult res = CopyPoint(&(srcSpec->pk), &(tmpSpec->pk));
680 if (res != HCF_SUCCESS) {
681 LOGE("Failed to allocate public key memory");
682 FreeEccCommParamsSpec(&(tmpSpec->base));
683 HcfFree(tmpSpec);
684 return res;
685 }
686
687 *destSpec = tmpSpec;
688 return HCF_SUCCESS;
689 }
690
CreateEccPriKeySpecImpl(const HcfEccPriKeyParamsSpec * srcSpec,HcfEccPriKeyParamsSpec ** destSpec)691 static HcfResult CreateEccPriKeySpecImpl(const HcfEccPriKeyParamsSpec *srcSpec, HcfEccPriKeyParamsSpec **destSpec)
692 {
693 HcfEccPriKeyParamsSpec *tmpSpec = (HcfEccPriKeyParamsSpec *)HcfMalloc(sizeof(HcfEccPriKeyParamsSpec), 0);
694 if (tmpSpec == NULL) {
695 LOGE("Failed to allocate dest spec memory");
696 return HCF_ERR_MALLOC;
697 }
698 if (CopyEccCommonSpec(&(srcSpec->base), &(tmpSpec->base)) != HCF_SUCCESS) {
699 HcfFree(tmpSpec);
700 return HCF_INVALID_PARAMS;
701 }
702 tmpSpec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0);
703 if (tmpSpec->sk.data == NULL) {
704 LOGE("Failed to allocate private key memory");
705 FreeEccCommParamsSpec(&(tmpSpec->base));
706 HcfFree(tmpSpec);
707 return HCF_ERR_MALLOC;
708 }
709 (void)memcpy_s(tmpSpec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len);
710 tmpSpec->sk.len = srcSpec->sk.len;
711
712 *destSpec = tmpSpec;
713 return HCF_SUCCESS;
714 }
715
CreateEccKeyPairSpecImpl(const HcfEccKeyPairParamsSpec * srcSpec,HcfEccKeyPairParamsSpec ** destSpec)716 static HcfResult CreateEccKeyPairSpecImpl(const HcfEccKeyPairParamsSpec *srcSpec, HcfEccKeyPairParamsSpec **destSpec)
717 {
718 HcfEccKeyPairParamsSpec *tmpSpec = (HcfEccKeyPairParamsSpec *)HcfMalloc(sizeof(HcfEccKeyPairParamsSpec), 0);
719 if (tmpSpec == NULL) {
720 LOGE("Failed to allocate dest spec memory");
721 return HCF_ERR_MALLOC;
722 }
723 if (CopyEccCommonSpec(&(srcSpec->base), &(tmpSpec->base)) != HCF_SUCCESS) {
724 HcfFree(tmpSpec);
725 return HCF_INVALID_PARAMS;
726 }
727 HcfResult res = CopyPoint(&(srcSpec->pk), &(tmpSpec->pk));
728 if (res != HCF_SUCCESS) {
729 LOGE("Failed to allocate public key memory");
730 FreeEccCommParamsSpec(&(tmpSpec->base));
731 HcfFree(tmpSpec);
732 return res;
733 }
734 tmpSpec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0);
735 if (tmpSpec->sk.data == NULL) {
736 LOGE("Failed to allocate private key memory");
737 FreeEccCommParamsSpec(&(tmpSpec->base));
738 HcfFree(tmpSpec->pk.x.data);
739 HcfFree(tmpSpec->pk.y.data);
740 HcfFree(tmpSpec);
741 return HCF_ERR_MALLOC;
742 }
743 (void)memcpy_s(tmpSpec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len);
744 tmpSpec->sk.len = srcSpec->sk.len;
745
746 *destSpec = tmpSpec;
747 return HCF_SUCCESS;
748 }
749
CreateEccParamsSpecImpl(const HcfAsyKeyParamsSpec * paramsSpec,HcfAsyKeyParamsSpec ** impl)750 static HcfResult CreateEccParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, HcfAsyKeyParamsSpec **impl)
751 {
752 HcfResult ret = HCF_SUCCESS;
753 HcfEccCommParamsSpec *spec = NULL;
754 switch (paramsSpec->specType) {
755 case HCF_COMMON_PARAMS_SPEC:
756 ret = CreateEccCommonSpecImpl((HcfEccCommParamsSpec *)paramsSpec, &spec);
757 break;
758 case HCF_PUBLIC_KEY_SPEC:
759 ret = CreateEccPubKeySpecImpl((HcfEccPubKeyParamsSpec *)paramsSpec, (HcfEccPubKeyParamsSpec **)(&spec));
760 break;
761 case HCF_PRIVATE_KEY_SPEC:
762 ret = CreateEccPriKeySpecImpl((HcfEccPriKeyParamsSpec *)paramsSpec, (HcfEccPriKeyParamsSpec **)(&spec));
763 break;
764 case HCF_KEY_PAIR_SPEC:
765 ret = CreateEccKeyPairSpecImpl((HcfEccKeyPairParamsSpec *)paramsSpec, (HcfEccKeyPairParamsSpec **)(&spec));
766 break;
767 default:
768 ret = HCF_INVALID_PARAMS;
769 break;
770 }
771 if (ret == HCF_SUCCESS) {
772 *impl = (HcfAsyKeyParamsSpec *)spec;
773 }
774 return ret;
775 }
776
CopyRsaCommonSpec(const HcfRsaCommParamsSpec * srcSpec,HcfRsaCommParamsSpec * destSpec)777 static HcfResult CopyRsaCommonSpec(const HcfRsaCommParamsSpec *srcSpec, HcfRsaCommParamsSpec *destSpec)
778 {
779 if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) {
780 return HCF_INVALID_PARAMS;
781 }
782 destSpec->n.data = (unsigned char *)HcfMalloc(srcSpec->n.len, 0);
783 if (destSpec->n.data == NULL) {
784 LOGE("Failed to allocate n data memory");
785 HcfFree(destSpec->base.algName);
786 return HCF_ERR_MALLOC;
787 }
788 (void)memcpy_s(destSpec->n.data, srcSpec->n.len, srcSpec->n.data, srcSpec->n.len);
789 destSpec->n.len = srcSpec->n.len;
790 return HCF_SUCCESS;
791 }
792
CreateRsaPubKeySpecImpl(const HcfRsaPubKeyParamsSpec * srcSpec,HcfRsaPubKeyParamsSpec ** destSpec)793 static HcfResult CreateRsaPubKeySpecImpl(const HcfRsaPubKeyParamsSpec *srcSpec, HcfRsaPubKeyParamsSpec **destSpec)
794 {
795 HcfRsaPubKeyParamsSpec *spec = (HcfRsaPubKeyParamsSpec *)HcfMalloc(sizeof(HcfRsaPubKeyParamsSpec), 0);
796 if (spec == NULL) {
797 LOGE("Failed to allocate dest spec memory");
798 return HCF_ERR_MALLOC;
799 }
800 if (CopyRsaCommonSpec(&(srcSpec->base), &(spec->base)) != HCF_SUCCESS) {
801 HcfFree(spec);
802 return HCF_INVALID_PARAMS;
803 }
804 spec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0);
805 if (spec->pk.data == NULL) {
806 LOGE("Failed to allocate public key memory");
807 DestroyRsaPubKeySpec(spec);
808 return HCF_ERR_MALLOC;
809 }
810 (void)memcpy_s(spec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len);
811 spec->pk.len = srcSpec->pk.len;
812
813 *destSpec = spec;
814 return HCF_SUCCESS;
815 }
816
CreateRsaKeyPairSpecImpl(const HcfRsaKeyPairParamsSpec * srcSpec,HcfRsaKeyPairParamsSpec ** destSpec)817 static HcfResult CreateRsaKeyPairSpecImpl(const HcfRsaKeyPairParamsSpec *srcSpec, HcfRsaKeyPairParamsSpec **destSpec)
818 {
819 HcfRsaKeyPairParamsSpec *spec = (HcfRsaKeyPairParamsSpec *)HcfMalloc(sizeof(HcfRsaKeyPairParamsSpec), 0);
820 if (spec == NULL) {
821 LOGE("Failed to allocate dest spec memory");
822 return HCF_ERR_MALLOC;
823 }
824 if (CopyRsaCommonSpec(&(srcSpec->base), &(spec->base)) != HCF_SUCCESS) {
825 HcfFree(spec);
826 return HCF_INVALID_PARAMS;
827 }
828 spec->pk.data = (unsigned char *)HcfMalloc(srcSpec->pk.len, 0);
829 if (spec->pk.data == NULL) {
830 LOGE("Failed to allocate public key memory");
831 FreeRsaCommParamsSpec(&(spec->base));
832 HcfFree(spec);
833 return HCF_ERR_MALLOC;
834 }
835 spec->sk.data = (unsigned char *)HcfMalloc(srcSpec->sk.len, 0);
836 if (spec->sk.data == NULL) {
837 LOGE("Failed to allocate private key memory");
838 FreeRsaCommParamsSpec(&(spec->base));
839 HcfFree(spec->pk.data);
840 HcfFree(spec);
841 return HCF_ERR_MALLOC;
842 }
843 (void)memcpy_s(spec->pk.data, srcSpec->pk.len, srcSpec->pk.data, srcSpec->pk.len);
844 (void)memcpy_s(spec->sk.data, srcSpec->sk.len, srcSpec->sk.data, srcSpec->sk.len);
845 spec->pk.len = srcSpec->pk.len;
846 spec->sk.len = srcSpec->sk.len;
847
848 *destSpec = spec;
849 return HCF_SUCCESS;
850 }
851
CreateRsaParamsSpecImpl(const HcfAsyKeyParamsSpec * paramsSpec,HcfAsyKeyParamsSpec ** impl)852 static HcfResult CreateRsaParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, HcfAsyKeyParamsSpec **impl)
853 {
854 HcfResult ret = HCF_SUCCESS;
855 HcfRsaCommParamsSpec *spec = NULL;
856 switch (paramsSpec->specType) {
857 // commonspe should not be used in RSA
858 case HCF_COMMON_PARAMS_SPEC:
859 LOGE("RSA not support comm spec");
860 ret = HCF_INVALID_PARAMS;
861 break;
862 case HCF_PUBLIC_KEY_SPEC:
863 ret = CreateRsaPubKeySpecImpl((HcfRsaPubKeyParamsSpec *)paramsSpec, (HcfRsaPubKeyParamsSpec **)&spec);
864 break;
865 case HCF_KEY_PAIR_SPEC:
866 ret = CreateRsaKeyPairSpecImpl((HcfRsaKeyPairParamsSpec *)paramsSpec, (HcfRsaKeyPairParamsSpec **)&spec);
867 break;
868 default:
869 ret = HCF_INVALID_PARAMS;
870 break;
871 }
872 if (ret == HCF_SUCCESS) {
873 *impl = (HcfAsyKeyParamsSpec *)spec;
874 }
875 return ret;
876 }
877
CreateAsyKeyParamsSpecImpl(const HcfAsyKeyParamsSpec * paramsSpec,HcfAlgValue alg,HcfAsyKeyParamsSpec ** impl)878 static HcfResult CreateAsyKeyParamsSpecImpl(const HcfAsyKeyParamsSpec *paramsSpec, HcfAlgValue alg,
879 HcfAsyKeyParamsSpec **impl)
880 {
881 HcfResult ret = HCF_SUCCESS;
882 switch (alg) {
883 case HCF_ALG_DSA:
884 ret = CreateDsaParamsSpecImpl(paramsSpec, impl);
885 break;
886 case HCF_ALG_ECC:
887 ret = CreateEccParamsSpecImpl(paramsSpec, impl);
888 break;
889 case HCF_ALG_RSA:
890 ret = CreateRsaParamsSpecImpl(paramsSpec, impl);
891 break;
892 default:
893 ret = HCF_INVALID_PARAMS;
894 break;
895 }
896 return ret;
897 }
898
899 // export interfaces
GetAsyKeyGeneratorClass(void)900 static const char *GetAsyKeyGeneratorClass(void)
901 {
902 return ASY_KEY_GENERATOR_CLASS;
903 }
904
GetAsyKeyGeneratorBySpecClass(void)905 static const char *GetAsyKeyGeneratorBySpecClass(void)
906 {
907 return ASY_KEY_GENERATOR_BY_SPEC_CLASS;
908 }
909
GetAlgoName(HcfAsyKeyGenerator * self)910 static const char *GetAlgoName(HcfAsyKeyGenerator *self)
911 {
912 if (self == NULL) {
913 LOGE("The input self ptr is NULL!");
914 return NULL;
915 }
916 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
917 return NULL;
918 }
919 HcfAsyKeyGeneratorImpl *impl = (HcfAsyKeyGeneratorImpl *)self;
920 return impl->algoName;
921 }
922
GetAlgNameBySpec(const HcfAsyKeyGeneratorBySpec * self)923 static const char *GetAlgNameBySpec(const HcfAsyKeyGeneratorBySpec *self)
924 {
925 if (self == NULL) {
926 LOGE("The input self ptr is NULL!");
927 return NULL;
928 }
929 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorBySpecClass())) {
930 return NULL;
931 }
932 HcfAsyKeyGeneratorBySpecImpl *impl = (HcfAsyKeyGeneratorBySpecImpl *)self;
933 return impl->paramsSpec->algName;
934 }
935
ConvertKey(HcfAsyKeyGenerator * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)936 static HcfResult ConvertKey(HcfAsyKeyGenerator *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
937 HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
938 {
939 if (self == NULL) {
940 LOGE("Invalid input parameter.");
941 return HCF_INVALID_PARAMS;
942 }
943 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
944 return HCF_INVALID_PARAMS;
945 }
946 HcfAsyKeyGeneratorImpl *impl = (HcfAsyKeyGeneratorImpl *)self;
947 return impl->spiObj->engineConvertKey(impl->spiObj, params, pubKeyBlob, priKeyBlob, returnKeyPair);
948 }
949
GenerateKeyPair(HcfAsyKeyGenerator * self,HcfParamsSpec * params,HcfKeyPair ** returnKeyPair)950 static HcfResult GenerateKeyPair(HcfAsyKeyGenerator *self, HcfParamsSpec *params,
951 HcfKeyPair **returnKeyPair)
952 {
953 (void)params;
954 if (self == NULL) {
955 LOGE("Invalid input parameter.");
956 return HCF_INVALID_PARAMS;
957 }
958 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
959 return HCF_INVALID_PARAMS;
960 }
961 HcfAsyKeyGeneratorImpl *impl = (HcfAsyKeyGeneratorImpl *)self;
962 return impl->spiObj->engineGenerateKeyPair(impl->spiObj, returnKeyPair);
963 }
964
GenerateKeyPairBySpec(const HcfAsyKeyGeneratorBySpec * self,HcfKeyPair ** returnKeyPair)965 static HcfResult GenerateKeyPairBySpec(const HcfAsyKeyGeneratorBySpec *self, HcfKeyPair **returnKeyPair)
966 {
967 if (self == NULL) {
968 LOGE("Invalid input parameter.");
969 return HCF_INVALID_PARAMS;
970 }
971 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorBySpecClass())) {
972 return HCF_INVALID_PARAMS;
973 }
974 HcfAsyKeyGeneratorBySpecImpl *impl = (HcfAsyKeyGeneratorBySpecImpl *)self;
975 return impl->spiObj->engineGenerateKeyPairBySpec(impl->spiObj, impl->paramsSpec, returnKeyPair);
976 }
977
GeneratePubKeyBySpec(const HcfAsyKeyGeneratorBySpec * self,HcfPubKey ** returnPubKey)978 static HcfResult GeneratePubKeyBySpec(const HcfAsyKeyGeneratorBySpec *self, HcfPubKey **returnPubKey)
979 {
980 if (self == NULL) {
981 LOGE("Invalid input parameter.");
982 return HCF_INVALID_PARAMS;
983 }
984 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorBySpecClass())) {
985 return HCF_INVALID_PARAMS;
986 }
987 HcfAsyKeyGeneratorBySpecImpl *impl = (HcfAsyKeyGeneratorBySpecImpl *)self;
988 return impl->spiObj->engineGeneratePubKeyBySpec(impl->spiObj, impl->paramsSpec, returnPubKey);
989 }
990
GeneratePriKeyBySpec(const HcfAsyKeyGeneratorBySpec * self,HcfPriKey ** returnPriKey)991 static HcfResult GeneratePriKeyBySpec(const HcfAsyKeyGeneratorBySpec *self, HcfPriKey **returnPriKey)
992 {
993 if (self == NULL) {
994 LOGE("Invalid input parameter.");
995 return HCF_INVALID_PARAMS;
996 }
997 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorBySpecClass())) {
998 return HCF_INVALID_PARAMS;
999 }
1000 HcfAsyKeyGeneratorBySpecImpl *impl = (HcfAsyKeyGeneratorBySpecImpl *)self;
1001 return impl->spiObj->engineGeneratePriKeyBySpec(impl->spiObj, impl->paramsSpec, returnPriKey);
1002 }
1003
DestroyAsyKeyGenerator(HcfObjectBase * self)1004 static void DestroyAsyKeyGenerator(HcfObjectBase *self)
1005 {
1006 if (self == NULL) {
1007 return;
1008 }
1009 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorClass())) {
1010 return;
1011 }
1012 HcfAsyKeyGeneratorImpl *impl = (HcfAsyKeyGeneratorImpl *)self;
1013 HcfObjDestroy(impl->spiObj);
1014 impl->spiObj = NULL;
1015 HcfFree(impl);
1016 }
1017
DestroyAsyKeyGeneratorBySpec(HcfObjectBase * self)1018 static void DestroyAsyKeyGeneratorBySpec(HcfObjectBase *self)
1019 {
1020 if (self == NULL) {
1021 return;
1022 }
1023 if (!IsClassMatch((HcfObjectBase *)self, GetAsyKeyGeneratorBySpecClass())) {
1024 return;
1025 }
1026 HcfAsyKeyGeneratorBySpecImpl *impl = (HcfAsyKeyGeneratorBySpecImpl *)self;
1027 HcfObjDestroy(impl->spiObj);
1028 impl->spiObj = NULL;
1029 FreeAsyKeySpec(impl->paramsSpec);
1030 impl->paramsSpec = NULL;
1031 HcfFree(impl);
1032 }
1033
HcfAsyKeyGeneratorCreate(const char * algoName,HcfAsyKeyGenerator ** returnObj)1034 HcfResult HcfAsyKeyGeneratorCreate(const char *algoName, HcfAsyKeyGenerator **returnObj)
1035 {
1036 if ((!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN)) || (returnObj == NULL)) {
1037 return HCF_INVALID_PARAMS;
1038 }
1039
1040 HcfAsyKeyGenParams params = { 0 };
1041 if (ParseAndSetParameter(algoName, ¶ms, ParseAsyKeyGenParams) != HCF_SUCCESS) {
1042 LOGE("Failed to parser parmas!");
1043 return HCF_INVALID_PARAMS;
1044 }
1045
1046 HcfAsyKeyGeneratorSpiCreateFunc createSpiFunc = FindAbility(¶ms);
1047 if (createSpiFunc == NULL) {
1048 return HCF_NOT_SUPPORT;
1049 }
1050
1051 HcfAsyKeyGeneratorImpl *returnGenerator = (HcfAsyKeyGeneratorImpl *)HcfMalloc(sizeof(HcfAsyKeyGeneratorImpl), 0);
1052 if (returnGenerator == NULL) {
1053 LOGE("Failed to allocate returnGenerator memory!");
1054 return HCF_ERR_MALLOC;
1055 }
1056 if (strcpy_s(returnGenerator->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
1057 LOGE("Failed to copy algoName!");
1058 HcfFree(returnGenerator);
1059 return HCF_INVALID_PARAMS;
1060 }
1061 HcfAsyKeyGeneratorSpi *spiObj = NULL;
1062 HcfResult res = HCF_SUCCESS;
1063 res = createSpiFunc(¶ms, &spiObj);
1064 if (res != HCF_SUCCESS) {
1065 LOGE("Failed to create spi object!");
1066 HcfFree(returnGenerator);
1067 return res;
1068 }
1069 returnGenerator->base.base.destroy = DestroyAsyKeyGenerator;
1070 returnGenerator->base.base.getClass = GetAsyKeyGeneratorClass;
1071 returnGenerator->base.convertKey = ConvertKey;
1072 returnGenerator->base.generateKeyPair = GenerateKeyPair;
1073 returnGenerator->base.getAlgoName = GetAlgoName;
1074 returnGenerator->spiObj = spiObj;
1075 *returnObj = (HcfAsyKeyGenerator *)returnGenerator;
1076 return HCF_SUCCESS;
1077 }
1078
HcfAsyKeyGeneratorBySpecCreate(const HcfAsyKeyParamsSpec * paramsSpec,HcfAsyKeyGeneratorBySpec ** returnObj)1079 HcfResult HcfAsyKeyGeneratorBySpecCreate(const HcfAsyKeyParamsSpec *paramsSpec, HcfAsyKeyGeneratorBySpec **returnObj)
1080 {
1081 if ((!IsParamsSpecValid(paramsSpec)) || (returnObj == NULL)) {
1082 return HCF_INVALID_PARAMS;
1083 }
1084 HcfAsyKeyGenParams params = { 0 };
1085 if (ParseAlgNameToParams(paramsSpec->algName, ¶ms) != HCF_SUCCESS) {
1086 LOGE("Failed to parser parmas!");
1087 return HCF_INVALID_PARAMS;
1088 }
1089 HcfAsyKeyGeneratorSpiCreateFunc createSpiFunc = FindAbility(¶ms);
1090 if (createSpiFunc == NULL) {
1091 return HCF_NOT_SUPPORT;
1092 }
1093 HcfAsyKeyGeneratorBySpecImpl *returnGenerator =
1094 (HcfAsyKeyGeneratorBySpecImpl *)HcfMalloc(sizeof(HcfAsyKeyGeneratorBySpecImpl), 0);
1095 if (returnGenerator == NULL) {
1096 LOGE("Failed to allocate returnGenerator memory!");
1097 return HCF_ERR_MALLOC;
1098 }
1099 HcfAsyKeyParamsSpec *paramsSpecImpl = NULL;
1100 HcfResult ret = CreateAsyKeyParamsSpecImpl(paramsSpec, params.algo, ¶msSpecImpl);
1101 if (ret != HCF_SUCCESS) {
1102 LOGE("Failed to create asy key params spec impl!");
1103 HcfFree(returnGenerator);
1104 return ret;
1105 }
1106 HcfAsyKeyGeneratorSpi *spiObj = NULL;
1107 ret = createSpiFunc(¶ms, &spiObj);
1108 if (ret != HCF_SUCCESS) {
1109 LOGE("Failed to create spi object!");
1110 HcfFree(returnGenerator);
1111 FreeAsyKeySpec(paramsSpecImpl);
1112 return ret;
1113 }
1114 returnGenerator->base.base.destroy = DestroyAsyKeyGeneratorBySpec;
1115 returnGenerator->base.base.getClass = GetAsyKeyGeneratorBySpecClass;
1116 returnGenerator->base.generateKeyPair = GenerateKeyPairBySpec;
1117 returnGenerator->base.generatePubKey = GeneratePubKeyBySpec;
1118 returnGenerator->base.generatePriKey = GeneratePriKeyBySpec;
1119 returnGenerator->base.getAlgName = GetAlgNameBySpec;
1120 returnGenerator->paramsSpec = paramsSpecImpl;
1121 returnGenerator->spiObj = spiObj;
1122 *returnObj = (HcfAsyKeyGeneratorBySpec *)returnGenerator;
1123 return HCF_SUCCESS;
1124 }