• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "sm2_asy_key_generator_openssl.h"
17 #include "securec.h"
18 #include "ecc_openssl_common.h"
19 #include "ecc_openssl_common_param_spec.h"
20 #include "log.h"
21 #include "memory.h"
22 #include "openssl_adapter.h"
23 #include "utils.h"
24 
25 #define OPENSSL_SM2_256_BITS 256
26 #define OPENSSL_SM2_KEY_GENERATOR_CLASS "OPENSSL.SM2.KEY_GENERATOR_CLASS"
27 #define OPENSSL_SM2_ALGORITHM "SM2"
28 #define OPENSSL_SM2_PUB_KEY_FORMAT "X.509"
29 #define OPENSSL_SM2_PRI_KEY_FORMAT "PKCS#8"
30 static const char *const g_sm2GenerateFieldType = "Fp";
31 
32 typedef struct {
33     HcfAsyKeyGeneratorSpi base;
34 
35     int32_t curveId;
36 } HcfAsyKeyGeneratorSpiOpensslSm2Impl;
37 
CheckSm256CurveId(BIGNUM * p,BIGNUM * b,BIGNUM * x,BIGNUM * y)38 static HcfResult CheckSm256CurveId(BIGNUM *p, BIGNUM *b, BIGNUM *x, BIGNUM *y)
39 {
40     BIGNUM *pStd = NULL;
41     BIGNUM *bStd = NULL;
42     BIGNUM *xStd = NULL;
43     BIGNUM *yStd = NULL;
44     pStd = Openssl_BN_bin2bn(g_sm256CorrectBigP, NID_X9_62_prime256v1_len, NULL);
45     bStd = Openssl_BN_bin2bn(g_sm256CorrectBigB, NID_X9_62_prime256v1_len, NULL);
46     xStd = Openssl_BN_bin2bn(g_sm256CorrectBigGX, NID_X9_62_prime256v1_len, NULL);
47     yStd = Openssl_BN_bin2bn(g_sm256CorrectBigGY, NID_X9_62_prime256v1_len, NULL);
48     if ((pStd == NULL) || (bStd == NULL) || (xStd == NULL) || (yStd == NULL)) {
49         LOGD("[error] EC 256 Curve convert to BN fail");
50         FreeCurveBigNum(pStd, bStd, xStd, yStd);
51         return HCF_ERR_CRYPTO_OPERATION;
52     }
53     if (Openssl_BN_cmp(p, pStd) == 0 && Openssl_BN_cmp(b, bStd) == 0 &&
54         Openssl_BN_cmp(x, xStd) == 0 && Openssl_BN_cmp(y, yStd) == 0) {
55         FreeCurveBigNum(pStd, bStd, xStd, yStd);
56         return HCF_SUCCESS;
57     }
58     LOGD("[error] EC 256 compare fail");
59     FreeCurveBigNum(pStd, bStd, xStd, yStd);
60     return HCF_INVALID_PARAMS;
61 }
62 
CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec * ecParams,int32_t * curveId)63 static HcfResult CheckParamsSpecToGetCurveId(const HcfEccCommParamsSpec *ecParams, int32_t *curveId)
64 {
65     BIGNUM *p = NULL;
66     BIGNUM *b = NULL;
67     BIGNUM *x = NULL;
68     BIGNUM *y = NULL;
69     HcfECFieldFp *field = (HcfECFieldFp *)(ecParams->field);
70     if (BigIntegerToBigNum(&(field->p), &p) != HCF_SUCCESS ||
71         BigIntegerToBigNum(&(ecParams->b), &b) != HCF_SUCCESS ||
72         BigIntegerToBigNum(&(ecParams->g.x), &x) != HCF_SUCCESS ||
73         BigIntegerToBigNum(&(ecParams->g.y), &y) != HCF_SUCCESS) {
74         LOGD("[error] BigIntegerToBigNum failed.");
75         FreeCurveBigNum(p, b, x, y);
76         return HCF_ERR_CRYPTO_OPERATION;
77     }
78 
79     int32_t bitLenP = (int32_t)Openssl_BN_num_bits(p);
80     HcfResult ret = HCF_INVALID_PARAMS;
81     if (bitLenP != OPENSSL_SM2_256_BITS) {
82         LOGE("Find no bit len");
83         FreeCurveBigNum(p, b, x, y);
84         return ret;
85     }
86     ret = CheckSm256CurveId(p, b, x, y);
87     if (ret == HCF_SUCCESS) {
88         *curveId = NID_sm2;
89     }
90     FreeCurveBigNum(p, b, x, y);
91     return ret;
92 }
93 
GenerateSm2KeyWithParamsSpec(const HcfEccCommParamsSpec * ecParams,EC_KEY ** returnKey)94 static HcfResult GenerateSm2KeyWithParamsSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnKey)
95 {
96     if (ecParams == NULL || returnKey == NULL) {
97         LOGE("Invalid input parameters.");
98         return HCF_INVALID_PARAMS;
99     }
100     EC_KEY *ecKey = NULL;
101     int32_t curveId = 0;
102     HcfResult ret = CheckParamsSpecToGetCurveId(ecParams, &curveId);
103     if (ret == HCF_SUCCESS && curveId != 0) {
104         ecKey = Openssl_EC_KEY_new_by_curve_name(curveId);
105         LOGD("Generate EC_KEY by curve name");
106         if (ecKey == NULL) {
107             LOGD("[error] New ec key failed.");
108             return HCF_ERR_CRYPTO_OPERATION;
109         }
110     } else {
111         EC_GROUP *group = NULL;
112         ret = GenerateEcGroupWithParamsSpec(ecParams, &group);
113         if (ret != HCF_SUCCESS) {
114             LOGE("GenerateEcGroupWithParamsSpec failed.");
115             return ret;
116         }
117         ecKey = Openssl_EC_KEY_new();
118         if (ecKey == NULL) {
119             LOGD("[error] Openssl_EC_KEY_new failed.");
120             Openssl_EC_GROUP_free(group);
121             return HCF_ERR_CRYPTO_OPERATION;
122         }
123         if (Openssl_EC_KEY_set_group(ecKey, group) != HCF_OPENSSL_SUCCESS) {
124             LOGD("[error] Openssl_EC_KEY_set_group failed.");
125             Openssl_EC_GROUP_free(group);
126             Openssl_EC_KEY_free(ecKey);
127             return HCF_ERR_CRYPTO_OPERATION;
128         }
129         Openssl_EC_GROUP_free(group);
130         LOGD("Generate EC_KEY by group spec parmas");
131     }
132     // all exceptions have been returned above.
133     *returnKey = ecKey;
134     return HCF_SUCCESS;
135 }
136 
NewSm2KeyPairWithCommSpec(const HcfEccCommParamsSpec * ecParams,EC_KEY ** returnEckey)137 static HcfResult NewSm2KeyPairWithCommSpec(const HcfEccCommParamsSpec *ecParams, EC_KEY **returnEckey)
138 {
139     if (ecParams == NULL || returnEckey == NULL) {
140         LOGE("Invalid input parameters.");
141         return HCF_INVALID_PARAMS;
142     }
143     EC_KEY *ecKey = NULL;
144     HcfResult ret = GenerateSm2KeyWithParamsSpec(ecParams, &ecKey);
145     if (ret != HCF_SUCCESS) {
146         LOGE("Generate EC key failed");
147         return ret;
148     }
149     if (Openssl_EC_KEY_generate_key(ecKey) != HCF_OPENSSL_SUCCESS) {
150         LOGD("[error] Openssl_EC_KEY_generate_key failed.");
151         Openssl_EC_KEY_free(ecKey);
152         return HCF_ERR_CRYPTO_OPERATION;
153     }
154 
155     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
156         LOGD("[error] Check ecKey fail.");
157         Openssl_EC_KEY_free(ecKey);
158         return HCF_ERR_CRYPTO_OPERATION;
159     }
160     *returnEckey = ecKey;
161     return ret;
162 }
163 
NewSm2PubKeyWithPubSpec(const HcfEccPubKeyParamsSpec * ecParams,EC_KEY ** returnEcKey)164 static HcfResult NewSm2PubKeyWithPubSpec(const HcfEccPubKeyParamsSpec *ecParams, EC_KEY **returnEcKey)
165 {
166     if (ecParams == NULL || returnEcKey == NULL) {
167         LOGE("Invalid input parameters.");
168         return HCF_INVALID_PARAMS;
169     }
170     EC_KEY *ecKey = NULL;
171     HcfResult ret = GenerateSm2KeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
172     if (ret != HCF_SUCCESS) {
173         LOGE("Generate EC key failed");
174         return ret;
175     }
176     ret = SetEcKey(&(ecParams->pk), NULL, ecKey);
177     if (ret != HCF_SUCCESS) {
178         LOGD("[error] Set public ecKey failed.");
179         Openssl_EC_KEY_free(ecKey);
180         return HCF_ERR_CRYPTO_OPERATION;
181     }
182 
183     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
184         LOGD("[error] Check ecKey fail.");
185         Openssl_EC_KEY_free(ecKey);
186         return HCF_ERR_CRYPTO_OPERATION;
187     }
188     *returnEcKey = ecKey;
189     return ret;
190 }
191 
NewSm2PriKeyWithPriSpec(const HcfEccPriKeyParamsSpec * ecParams,EC_KEY ** returnEcKey)192 static HcfResult NewSm2PriKeyWithPriSpec(const HcfEccPriKeyParamsSpec *ecParams, EC_KEY **returnEcKey)
193 {
194     if (ecParams == NULL || returnEcKey == NULL) {
195         LOGE("Invalid input parameters.");
196         return HCF_INVALID_PARAMS;
197     }
198     EC_KEY *ecKey = NULL;
199     HcfResult ret = GenerateSm2KeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
200     if (ret != HCF_SUCCESS) {
201         LOGE("Generate EC key failed");
202         return ret;
203     }
204     ret = SetEcKey(NULL, &(ecParams->sk), ecKey);
205     if (ret != HCF_SUCCESS) {
206         LOGD("[error] Set private ecKey failed.");
207         Openssl_EC_KEY_free(ecKey);
208         return HCF_ERR_CRYPTO_OPERATION;
209     }
210 
211     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
212         LOGD("[error] Check ecKey failed.");
213         Openssl_EC_KEY_free(ecKey);
214         return HCF_ERR_CRYPTO_OPERATION;
215     }
216     *returnEcKey = ecKey;
217     return ret;
218 }
219 
NewSm2KeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec * ecParams,EC_KEY ** returnEcKey,bool needPrivate)220 static HcfResult NewSm2KeyWithKeyPairSpec(const HcfEccKeyPairParamsSpec *ecParams, EC_KEY **returnEcKey,
221     bool needPrivate)
222 {
223     if (ecParams == NULL || returnEcKey == NULL) {
224         LOGE("Invalid input parameters.");
225         return HCF_INVALID_PARAMS;
226     }
227     EC_KEY *ecKey = NULL;
228     HcfResult ret = GenerateSm2KeyWithParamsSpec((HcfEccCommParamsSpec *)ecParams, &ecKey);
229     if (ret != HCF_SUCCESS) {
230         LOGD("[error] Generate EC key failed");
231         return ret;
232     }
233     if (needPrivate) {
234         ret = SetEcKey(&(ecParams->pk), &(ecParams->sk), ecKey);
235     } else {
236         ret = SetEcKey(&(ecParams->pk), NULL, ecKey);
237     }
238     if (ret != HCF_SUCCESS) {
239         LOGD("[error] SetEcKey failed.");
240         Openssl_EC_KEY_free(ecKey);
241         return HCF_ERR_CRYPTO_OPERATION;
242     }
243 
244     if (Openssl_EC_KEY_check_key(ecKey) <= 0) {
245         LOGE("Check ecKey failed.");
246         Openssl_EC_KEY_free(ecKey);
247         return HCF_ERR_CRYPTO_OPERATION;
248     }
249     *returnEcKey = ecKey;
250     return ret;
251 }
252 
GenKeyPairSm2KeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)253 static HcfResult GenKeyPairSm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
254 {
255     HcfResult ret = HCF_INVALID_PARAMS;
256     switch (params->specType) {
257         case HCF_COMMON_PARAMS_SPEC:
258             ret = NewSm2KeyPairWithCommSpec((HcfEccCommParamsSpec *)params, ecKey);
259             break;
260         case HCF_KEY_PAIR_SPEC:
261             ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true);
262             break;
263         default:
264             LOGE("Invaild input spec to gen key pair.");
265             break;
266     }
267     return ret;
268 }
269 
GenPubKeySm2KeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)270 static HcfResult GenPubKeySm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
271 {
272     HcfResult ret = HCF_INVALID_PARAMS;
273     switch (params->specType) {
274         case HCF_PUBLIC_KEY_SPEC:
275             ret = NewSm2PubKeyWithPubSpec((HcfEccPubKeyParamsSpec *)params, ecKey);
276             break;
277         case HCF_KEY_PAIR_SPEC:
278             ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, false);
279             break;
280         default:
281             LOGE("Invaild input spec to gen pub key");
282             break;
283     }
284     return ret;
285 }
286 
GenPriKeySm2KeyBySpec(const HcfAsyKeyParamsSpec * params,EC_KEY ** ecKey)287 static HcfResult GenPriKeySm2KeyBySpec(const HcfAsyKeyParamsSpec *params, EC_KEY **ecKey)
288 {
289     HcfResult ret = HCF_INVALID_PARAMS;
290     switch (params->specType) {
291         case HCF_PRIVATE_KEY_SPEC:
292             ret = NewSm2PriKeyWithPriSpec((HcfEccPriKeyParamsSpec *)params, ecKey);
293             break;
294         case HCF_KEY_PAIR_SPEC:
295             ret = NewSm2KeyWithKeyPairSpec((HcfEccKeyPairParamsSpec *)params, ecKey, true);
296             break;
297         default:
298             LOGE("Invaild input spec to gen pri key");
299             break;
300     }
301     return ret;
302 }
303 
GetSm2KeyPairGeneratorClass(void)304 static const char *GetSm2KeyPairGeneratorClass(void)
305 {
306     return OPENSSL_SM2_KEY_GENERATOR_CLASS;
307 }
308 
GetSm2KeyPairClass(void)309 static const char *GetSm2KeyPairClass(void)
310 {
311     return HCF_OPENSSL_SM2_KEY_PAIR_CLASS;
312 }
313 
GetSm2PubKeyClass(void)314 static const char *GetSm2PubKeyClass(void)
315 {
316     return HCF_OPENSSL_SM2_PUB_KEY_CLASS;
317 }
318 
GetSm2PriKeyClass(void)319 static const char *GetSm2PriKeyClass(void)
320 {
321     return HCF_OPENSSL_SM2_PRI_KEY_CLASS;
322 }
323 
DestroySm2KeyPairGenerator(HcfObjectBase * self)324 static void DestroySm2KeyPairGenerator(HcfObjectBase *self)
325 {
326     if (self == NULL) {
327         LOGE("Class is null.");
328         return;
329     }
330     if (!IsClassMatch(self, GetSm2KeyPairGeneratorClass())) {
331         LOGE("Class not match.");
332         return;
333     }
334     HcfFree(self);
335 }
336 
DestroySm2PubKey(HcfObjectBase * self)337 static void DestroySm2PubKey(HcfObjectBase *self)
338 {
339     if (self == NULL) {
340         LOGE("Class is null.");
341         return;
342     }
343     if (!IsClassMatch(self, GetSm2PubKeyClass())) {
344         LOGE("Class not match.");
345         return;
346     }
347     HcfOpensslSm2PubKey *impl = (HcfOpensslSm2PubKey *)self;
348     Openssl_EC_KEY_free(impl->ecKey);
349     impl->ecKey = NULL;
350     HcfFree(impl->fieldType);
351     impl->fieldType = NULL;
352     HcfFree(impl);
353 }
354 
DestroySm2PriKey(HcfObjectBase * self)355 static void DestroySm2PriKey(HcfObjectBase *self)
356 {
357     if (self == NULL) {
358         LOGE("Class is null.");
359         return;
360     }
361     if (!IsClassMatch(self, GetSm2PriKeyClass())) {
362         LOGE("Class not match.");
363         return;
364     }
365     HcfOpensslSm2PriKey *impl = (HcfOpensslSm2PriKey *)self;
366     Openssl_EC_KEY_free(impl->ecKey);
367     impl->ecKey = NULL;
368     HcfFree(impl->fieldType);
369     impl->fieldType = NULL;
370     HcfFree(impl);
371 }
372 
DestroySm2KeyPair(HcfObjectBase * self)373 static void DestroySm2KeyPair(HcfObjectBase *self)
374 {
375     if (self == NULL) {
376         LOGE("Class is null.");
377         return;
378     }
379     if (!IsClassMatch(self, GetSm2KeyPairClass())) {
380         LOGE("Class not match.");
381         return;
382     }
383     HcfOpensslSm2KeyPair *impl = (HcfOpensslSm2KeyPair *)self;
384     if (impl->base.pubKey != NULL) {
385         DestroySm2PubKey((HcfObjectBase *)impl->base.pubKey);
386         impl->base.pubKey = NULL;
387     }
388     if (impl->base.priKey != NULL) {
389         DestroySm2PriKey((HcfObjectBase *)impl->base.priKey);
390         impl->base.priKey = NULL;
391     }
392     HcfFree(impl);
393 }
394 
GetSm2PubKeyAlgorithm(HcfKey * self)395 static const char *GetSm2PubKeyAlgorithm(HcfKey *self)
396 {
397     if (self == NULL) {
398         LOGE("Invalid input parameter.");
399         return NULL;
400     }
401     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_SM2_PUB_KEY_CLASS)) {
402         LOGE("Invalid SM2 public key class for algorithm");
403         return NULL;
404     }
405     return OPENSSL_SM2_ALGORITHM;
406 }
407 
GetSm2PriKeyAlgorithm(HcfKey * self)408 static const char *GetSm2PriKeyAlgorithm(HcfKey *self)
409 {
410     if (self == NULL) {
411         LOGE("Invalid input parameter.");
412         return NULL;
413     }
414     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_SM2_PRI_KEY_CLASS)) {
415         LOGE("Invalid SM2 private key class for algorithm");
416         return NULL;
417     }
418     return OPENSSL_SM2_ALGORITHM;
419 }
420 
GetSm2PubKeyFormat(HcfKey * self)421 static const char *GetSm2PubKeyFormat(HcfKey *self)
422 {
423     if (self == NULL) {
424         LOGE("Invalid input parameter.");
425         return NULL;
426     }
427     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_SM2_PUB_KEY_CLASS)) {
428         LOGE("Invalid SM2 public key class for format");
429         return NULL;
430     }
431     return OPENSSL_SM2_PUB_KEY_FORMAT;
432 }
433 
GetSm2PriKeyFormat(HcfKey * self)434 static const char *GetSm2PriKeyFormat(HcfKey *self)
435 {
436     if (self == NULL) {
437         LOGE("Invalid input parameter.");
438         return NULL;
439     }
440     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_SM2_PRI_KEY_CLASS)) {
441         LOGE("Invalid SM2 private key class for format");
442         return NULL;
443     }
444     return OPENSSL_SM2_PRI_KEY_FORMAT;
445 }
446 
GetSm2PubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)447 static HcfResult GetSm2PubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
448 {
449     if ((self == NULL) || (returnBlob == NULL)) {
450         LOGE("Invalid input parameter.");
451         return HCF_INVALID_PARAMS;
452     }
453     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_SM2_PUB_KEY_CLASS)) {
454         LOGE("Invalid SM2 public key class for encode");
455         return HCF_INVALID_PARAMS;
456     }
457 
458     HcfOpensslSm2PubKey *impl = (HcfOpensslSm2PubKey *)self;
459     if (impl->curveId != 0) {
460         LOGD("Have a curveId");
461         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE);
462     } else {
463         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE);
464     }
465 
466     unsigned char *returnData = NULL;
467     int returnDataLen = Openssl_i2d_EC_PUBKEY(impl->ecKey, &returnData);
468     if (returnDataLen <= 0) {
469         LOGD("[error] Call i2d_EC_PUBKEY fail");
470         HcfPrintOpensslError();
471         return HCF_ERR_CRYPTO_OPERATION;
472     }
473     returnBlob->data = returnData;
474     returnBlob->len = returnDataLen;
475     return HCF_SUCCESS;
476 }
477 
GetSm2PriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)478 static HcfResult GetSm2PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
479 {
480     if ((self == NULL) || (returnBlob == NULL)) {
481         LOGE("Invalid input parameter.");
482         return HCF_INVALID_PARAMS;
483     }
484     if (!IsClassMatch((HcfObjectBase *)self, HCF_OPENSSL_SM2_PRI_KEY_CLASS)) {
485         LOGE("Invalid SM2 private key class for encode");
486         return HCF_INVALID_PARAMS;
487     }
488 
489     HcfOpensslSm2PriKey *impl = (HcfOpensslSm2PriKey *)self;
490     if (impl->curveId != 0) {
491         LOGD("Have a curveId");
492         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_NAMED_CURVE);
493     } else {
494         Openssl_EC_KEY_set_asn1_flag(impl->ecKey, OPENSSL_EC_EXPLICIT_CURVE);
495     }
496     // keep consistence of 3.2
497     Openssl_EC_KEY_set_enc_flags(impl->ecKey, EC_PKEY_NO_PUBKEY);
498     // if the convert key has no pubKey, it will generate pub key automatically,
499     // and set the no pubKey flag to ensure the consistency of blob.
500     unsigned char *returnData = NULL;
501     int returnDataLen = Openssl_i2d_ECPrivateKey(impl->ecKey, &returnData);
502     if (returnDataLen <= 0) {
503         LOGD("[error] Call i2d_ECPrivateKey fail.");
504         HcfPrintOpensslError();
505         return HCF_ERR_CRYPTO_OPERATION;
506     }
507     returnBlob->data = returnData;
508     returnBlob->len = returnDataLen;
509     return HCF_SUCCESS;
510 }
511 
Sm2PriKeyClearMem(HcfPriKey * self)512 static void Sm2PriKeyClearMem(HcfPriKey *self)
513 {
514     if (self == NULL) {
515         LOGE("Class is null.");
516         return;
517     }
518     if (!IsClassMatch((HcfObjectBase *)self, GetSm2PriKeyClass())) {
519         LOGE("Class not match.");
520         return;
521     }
522     HcfOpensslSm2PriKey *impl = (HcfOpensslSm2PriKey *)self;
523     Openssl_EC_KEY_free(impl->ecKey);
524     impl->ecKey = NULL;
525 }
526 
GetCurveName(const HcfKey * self,bool isPriavte,char ** returnString)527 static HcfResult GetCurveName(const HcfKey *self, bool isPriavte, char **returnString)
528 {
529     int32_t curveId = 0;
530     if (isPriavte) {
531         curveId = ((HcfOpensslSm2PriKey *)self)->curveId;
532     } else {
533         curveId = ((HcfOpensslSm2PubKey *)self)->curveId;
534     }
535 
536     if (curveId != NID_sm2) {
537         LOGD("[error] Invalid curve name.");
538         return HCF_ERR_CRYPTO_OPERATION;
539     }
540 
541     char *curveIdStr = "NID_sm2";
542     size_t len = HcfStrlen(curveIdStr);
543     if (len == 0) {
544         LOGE("CurveIdStr is empty!");
545         return HCF_INVALID_PARAMS;
546     }
547     *returnString = (char *)HcfMalloc(len + 1, 0);
548     if (*returnString == NULL) {
549         LOGE("Allocate returnString memory failed.");
550         return HCF_ERR_MALLOC;
551     }
552     if (memcpy_s(*returnString, len, curveIdStr, len) != EOK) {
553         LOGE("Memcpy returnString failed.");
554         HcfFree(*returnString);
555         return HCF_ERR_MALLOC;
556     }
557     return HCF_SUCCESS;
558 }
559 
CheckSm2KeySelf(const HcfKey * self,bool * isPrivate)560 static HcfResult CheckSm2KeySelf(const HcfKey *self, bool *isPrivate)
561 {
562     if (IsClassMatch((HcfObjectBase *)self, GetSm2PubKeyClass())) {
563         *isPrivate = false;
564         return HCF_SUCCESS;
565     } else if (IsClassMatch((HcfObjectBase *)self, GetSm2PriKeyClass())) {
566         if (((HcfOpensslSm2PriKey *)self)->ecKey == NULL) {
567             LOGE("Cannot use priKey after free");
568             return HCF_INVALID_PARAMS;
569         }
570         *isPrivate = true;
571         return HCF_SUCCESS;
572     } else {
573         return HCF_INVALID_PARAMS;
574     }
575 }
576 
GetSm2KeySpecBigInteger(const HcfKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)577 static HcfResult GetSm2KeySpecBigInteger(const HcfKey *self, const AsyKeySpecItem item,
578     HcfBigInteger *returnBigInteger)
579 {
580     if (self == NULL || returnBigInteger == NULL) {
581         LOGE("Invalid input parameter.");
582         return HCF_INVALID_PARAMS;
583     }
584     bool isPrivate;
585     HcfResult ret = CheckSm2KeySelf(self, &isPrivate);
586     if (ret != HCF_SUCCESS) {
587         LOGE("Invalid input key");
588         return HCF_INVALID_PARAMS;
589     }
590     const EC_GROUP *group = NULL;
591     if (isPrivate) {
592         group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PriKey *)self)->ecKey);
593     } else {
594         group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PubKey *)self)->ecKey);
595     }
596     if (group == NULL) {
597         LOGE("Get group failed");
598         return HCF_INVALID_PARAMS;
599     }
600     switch (item) {
601         case ECC_FP_P_BN:
602         case ECC_A_BN:
603         case ECC_B_BN:
604             ret = GetCurveGFp(group, item, returnBigInteger);
605             break;
606         case ECC_G_X_BN:
607         case ECC_G_Y_BN:
608             ret = GetGenerator(group, item, returnBigInteger);
609             break;
610         case ECC_N_BN:
611             ret = GetOrder(group, returnBigInteger);
612             break;
613         case ECC_SK_BN:
614         case ECC_PK_X_BN:
615         case ECC_PK_Y_BN:
616             ret = GetPkSkBigInteger(self, isPrivate, item, returnBigInteger);
617             break;
618         default:
619             LOGE("Invalid ecc key big number spec!");
620             ret = HCF_INVALID_PARAMS;
621             break;
622     }
623     return ret;
624 }
625 
GetSm2KeySpecString(const HcfKey * self,const AsyKeySpecItem item,char ** returnString)626 static HcfResult GetSm2KeySpecString(const HcfKey *self, const AsyKeySpecItem item, char **returnString)
627 {
628     if (self == NULL || returnString == NULL) {
629         LOGE("Invalid input parameter.");
630         return HCF_INVALID_PARAMS;
631     }
632     bool isPrivate;
633     HcfResult ret = CheckSm2KeySelf(self, &isPrivate);
634     if (ret != HCF_SUCCESS) {
635         LOGE("Invalid input key");
636         return HCF_INVALID_PARAMS;
637     }
638 
639     switch (item) {
640         case ECC_FIELD_TYPE_STR:
641             ret = GetFieldType(self, isPrivate, returnString);
642             break;
643         case ECC_CURVE_NAME_STR:
644             ret = GetCurveName(self, isPrivate, returnString);
645             break;
646         default:
647             ret = HCF_INVALID_PARAMS;
648             LOGE("Invalid spec of ec string");
649             break;
650     }
651     return ret;
652 }
653 
GetSm2KeySpecInt(const HcfKey * self,const AsyKeySpecItem item,int * returnInt)654 static HcfResult GetSm2KeySpecInt(const HcfKey *self, const AsyKeySpecItem item, int *returnInt)
655 {
656     if (self == NULL || returnInt == NULL) {
657         LOGE("Invalid input parameter.");
658         return HCF_INVALID_PARAMS;
659     }
660     bool isPrivate;
661     HcfResult ret = CheckSm2KeySelf(self, &isPrivate);
662     if (ret != HCF_SUCCESS) {
663         LOGE("Invalid input key");
664         return HCF_INVALID_PARAMS;
665     }
666     const EC_GROUP *group = NULL;
667     if (isPrivate) {
668         group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PriKey *)self)->ecKey);
669     } else {
670         group = Openssl_EC_KEY_get0_group(((HcfOpensslSm2PubKey *)self)->ecKey);
671     }
672     if (group == NULL) {
673         LOGE("Get group failed");
674         return HCF_INVALID_PARAMS;
675     }
676     switch (item) {
677         case ECC_H_INT:
678             ret = GetCofactor(group, returnInt);
679             break;
680         case ECC_FIELD_SIZE_INT:
681             ret = GetFieldSize(group, returnInt);
682             break;
683         default:
684             ret = HCF_INVALID_PARAMS;
685             LOGE("Invalid ec key int spec");
686             break;
687     }
688     return ret;
689 }
690 
GetSm2PubKeySpecBigInteger(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)691 static HcfResult GetSm2PubKeySpecBigInteger(const HcfPubKey *self, const AsyKeySpecItem item,
692     HcfBigInteger *returnBigInteger)
693 {
694     return GetSm2KeySpecBigInteger((HcfKey *)self, item, returnBigInteger);
695 }
696 
GetSm2PriKeySpecBigInteger(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)697 static HcfResult GetSm2PriKeySpecBigInteger(const HcfPriKey *self, const AsyKeySpecItem item,
698     HcfBigInteger *returnBigInteger)
699 {
700     return GetSm2KeySpecBigInteger((HcfKey *)self, item, returnBigInteger);
701 }
702 
GetSm2PubKeySpecString(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)703 static HcfResult GetSm2PubKeySpecString(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
704 {
705     return GetSm2KeySpecString((HcfKey *)self, item, returnString);
706 }
707 
GetSm2PriKeySpecString(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)708 static HcfResult GetSm2PriKeySpecString(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
709 {
710     return GetSm2KeySpecString((HcfKey *)self, item, returnString);
711 }
712 
GetSm2PubKeySpecInt(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)713 static HcfResult GetSm2PubKeySpecInt(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
714 {
715     return GetSm2KeySpecInt((HcfKey *)self, item, returnInt);
716 }
717 
GetSm2PriKeySpecInt(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)718 static HcfResult GetSm2PriKeySpecInt(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
719 {
720     return GetSm2KeySpecInt((HcfKey *)self, item, returnInt);
721 }
722 
PackSm2PubKey(int32_t curveId,EC_KEY * ecKey,const char * fieldType,HcfOpensslSm2PubKey ** returnObj)723 static HcfResult PackSm2PubKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType,
724     HcfOpensslSm2PubKey **returnObj)
725 {
726     HcfOpensslSm2PubKey *returnPubKey = (HcfOpensslSm2PubKey *)HcfMalloc(sizeof(HcfOpensslSm2PubKey), 0);
727     if (returnPubKey == NULL) {
728         LOGE("Failed to allocate returnPubKey memory!");
729         return HCF_ERR_MALLOC;
730     }
731 
732     char *tmpFieldType = NULL;
733     if (fieldType != NULL) {
734         size_t len = HcfStrlen(fieldType);
735         if (len == 0) {
736             LOGE("FieldType is empty!");
737             HcfFree(returnPubKey);
738             return HCF_INVALID_PARAMS;
739         }
740         tmpFieldType = (char *)HcfMalloc(len + 1, 0);
741         if (tmpFieldType == NULL) {
742             LOGE("Allocate tmpFieldType memory failed.");
743             HcfFree(returnPubKey);
744             return HCF_ERR_MALLOC;
745         }
746         (void)memcpy_s(tmpFieldType, len, fieldType, len);
747     }
748     returnPubKey->base.base.base.destroy = DestroySm2PubKey;
749     returnPubKey->base.base.base.getClass = GetSm2PubKeyClass;
750     returnPubKey->base.base.getAlgorithm = GetSm2PubKeyAlgorithm;
751     returnPubKey->base.base.getEncoded = GetSm2PubKeyEncoded;
752     returnPubKey->base.base.getFormat = GetSm2PubKeyFormat;
753     returnPubKey->base.getAsyKeySpecBigInteger = GetSm2PubKeySpecBigInteger;
754     returnPubKey->base.getAsyKeySpecString = GetSm2PubKeySpecString;
755     returnPubKey->base.getAsyKeySpecInt = GetSm2PubKeySpecInt;
756     returnPubKey->curveId = curveId;
757     returnPubKey->ecKey = ecKey;
758     returnPubKey->fieldType = tmpFieldType;
759 
760     *returnObj = returnPubKey;
761     return HCF_SUCCESS;
762 }
763 
PackSm2PriKey(int32_t curveId,EC_KEY * ecKey,const char * fieldType,HcfOpensslSm2PriKey ** returnObj)764 static HcfResult PackSm2PriKey(int32_t curveId, EC_KEY *ecKey, const char *fieldType,
765     HcfOpensslSm2PriKey **returnObj)
766 {
767     HcfOpensslSm2PriKey *returnPriKey = (HcfOpensslSm2PriKey *)HcfMalloc(sizeof(HcfOpensslSm2PriKey), 0);
768     if (returnPriKey == NULL) {
769         LOGE("Failed to allocate returnPriKey memory!");
770         return HCF_ERR_MALLOC;
771     }
772 
773     char *tmpFieldType = NULL;
774     if (fieldType != NULL) {
775         size_t len = HcfStrlen(fieldType);
776         if (len == 0) {
777             LOGE("FieldType is empty!");
778             HcfFree(returnPriKey);
779             return HCF_INVALID_PARAMS;
780         }
781         tmpFieldType = (char *)HcfMalloc(len + 1, 0);
782         if (tmpFieldType == NULL) {
783             LOGE("Allocate tmpFieldType memory failed.");
784             HcfFree(returnPriKey);
785             return HCF_ERR_MALLOC;
786         }
787         (void)memcpy_s(tmpFieldType, len, fieldType, len);
788     }
789     returnPriKey->base.base.base.destroy = DestroySm2PriKey;
790     returnPriKey->base.base.base.getClass = GetSm2PriKeyClass;
791     returnPriKey->base.base.getAlgorithm = GetSm2PriKeyAlgorithm;
792     returnPriKey->base.base.getEncoded = GetSm2PriKeyEncoded;
793     returnPriKey->base.base.getFormat = GetSm2PriKeyFormat;
794     returnPriKey->base.getAsyKeySpecBigInteger = GetSm2PriKeySpecBigInteger;
795     returnPriKey->base.getAsyKeySpecString = GetSm2PriKeySpecString;
796     returnPriKey->base.getAsyKeySpecInt = GetSm2PriKeySpecInt;
797     returnPriKey->base.clearMem = Sm2PriKeyClearMem;
798     returnPriKey->curveId = curveId;
799     returnPriKey->ecKey = ecKey;
800     returnPriKey->fieldType = tmpFieldType;
801 
802     *returnObj = returnPriKey;
803     return HCF_SUCCESS;
804 }
805 
PackSm2KeyPair(HcfOpensslSm2PubKey * pubKey,HcfOpensslSm2PriKey * priKey,HcfOpensslSm2KeyPair ** returnObj)806 static HcfResult PackSm2KeyPair(HcfOpensslSm2PubKey *pubKey, HcfOpensslSm2PriKey *priKey,
807     HcfOpensslSm2KeyPair **returnObj)
808 {
809     HcfOpensslSm2KeyPair *returnKeyPair = (HcfOpensslSm2KeyPair *)HcfMalloc(sizeof(HcfOpensslSm2KeyPair), 0);
810     if (returnKeyPair == NULL) {
811         LOGE("Failed to allocate returnKeyPair memory!");
812         return HCF_ERR_MALLOC;
813     }
814     returnKeyPair->base.base.getClass = GetSm2KeyPairClass;
815     returnKeyPair->base.base.destroy = DestroySm2KeyPair;
816     returnKeyPair->base.pubKey = (HcfPubKey *)pubKey;
817     returnKeyPair->base.priKey = (HcfPriKey *)priKey;
818 
819     *returnObj = returnKeyPair;
820     return HCF_SUCCESS;
821 }
822 
ConvertEcPubKey(int32_t curveId,HcfBlob * pubKeyBlob,HcfOpensslSm2PubKey ** returnPubKey)823 static HcfResult ConvertEcPubKey(int32_t curveId, HcfBlob *pubKeyBlob, HcfOpensslSm2PubKey **returnPubKey)
824 {
825     const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data);
826     EC_KEY *ecKey = Openssl_d2i_EC_PUBKEY(NULL, &tmpData, pubKeyBlob->len);
827     if (ecKey == NULL) {
828         LOGD("[error] Call d2i_EC_PUBKEY fail.");
829         HcfPrintOpensslError();
830         return HCF_ERR_CRYPTO_OPERATION;
831     }
832     HcfResult ret = PackSm2PubKey(curveId, ecKey, g_sm2GenerateFieldType, returnPubKey);
833     if (ret != HCF_SUCCESS) {
834         LOGE("CreateSm2PubKey failed.");
835         Openssl_EC_KEY_free(ecKey);
836         return ret;
837     }
838     return HCF_SUCCESS;
839 }
840 
ConvertEcPriKey(int32_t curveId,HcfBlob * priKeyBlob,HcfOpensslSm2PriKey ** returnPriKey)841 static HcfResult ConvertEcPriKey(int32_t curveId, HcfBlob *priKeyBlob, HcfOpensslSm2PriKey **returnPriKey)
842 {
843     const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data);
844     EC_KEY *ecKey = Openssl_d2i_ECPrivateKey(NULL, &tmpData, priKeyBlob->len);
845     if (ecKey == NULL) {
846         LOGD("[error] Call d2i_ECPrivateKey fail");
847         HcfPrintOpensslError();
848         return HCF_ERR_CRYPTO_OPERATION;
849     }
850     HcfResult ret = PackSm2PriKey(curveId, ecKey, g_sm2GenerateFieldType, returnPriKey);
851     if (ret != HCF_SUCCESS) {
852         LOGE("CreateSm2PriKey failed.");
853         Openssl_EC_KEY_free(ecKey);
854         return ret;
855     }
856     return HCF_SUCCESS;
857 }
858 
EngineConvertSm2Key(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)859 static HcfResult EngineConvertSm2Key(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
860     HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
861 {
862     (void)params;
863     if ((self == NULL) || (returnKeyPair == NULL)) {
864         LOGE("Invalid input parameter.");
865         return HCF_INVALID_PARAMS;
866     }
867     if (!IsClassMatch((HcfObjectBase *)self, self->base.getClass())) {
868         LOGE("Class not match.");
869         return HCF_INVALID_PARAMS;
870     }
871     bool pubKeyValid = IsBlobValid(pubKeyBlob);
872     bool priKeyValid = IsBlobValid(priKeyBlob);
873     if ((!pubKeyValid) && (!priKeyValid)) {
874         LOGE("The private key and public key cannot both be NULL.");
875         return HCF_INVALID_PARAMS;
876     }
877 
878     HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl = (HcfAsyKeyGeneratorSpiOpensslSm2Impl *)self;
879     HcfResult ret = HCF_SUCCESS;
880     HcfOpensslSm2PubKey *pubKey = NULL;
881     HcfOpensslSm2PriKey *priKey = NULL;
882     HcfOpensslSm2KeyPair *keyPair = NULL;
883     do {
884         if (pubKeyValid) {
885             ret = ConvertEcPubKey(impl->curveId, pubKeyBlob, &pubKey);
886             if (ret != HCF_SUCCESS) {
887                 LOGD("[error] Convert ec pubKey failed.");
888                 break;
889             }
890         }
891         if (priKeyValid) {
892             ret = ConvertEcPriKey(impl->curveId, priKeyBlob, &priKey);
893             if (ret != HCF_SUCCESS) {
894                 LOGD("[error] Convert ec priKey failed.");
895                 break;
896             }
897         }
898         ret = PackSm2KeyPair(pubKey, priKey, &keyPair);
899     } while (0);
900     if (ret != HCF_SUCCESS) {
901         LOGD("[error] Convert sm2 keyPair failed.");
902         HcfObjDestroy(pubKey);
903         HcfObjDestroy(priKey);
904         return ret;
905     }
906 
907     *returnKeyPair = (HcfKeyPair *)keyPair;
908     return HCF_SUCCESS;
909 }
910 
PackAndAssignPubKey(const HcfAsyKeyGeneratorSpiOpensslSm2Impl * impl,const char * fieldType,EC_KEY * ecKey,HcfPubKey ** returnObj)911 static HcfResult PackAndAssignPubKey(const HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl, const char *fieldType,
912     EC_KEY *ecKey, HcfPubKey **returnObj)
913 {
914     HcfOpensslSm2PubKey *pubKey = NULL;
915     HcfResult ret = PackSm2PubKey(impl->curveId, ecKey, fieldType, &pubKey);
916     if (ret != HCF_SUCCESS) {
917         LOGD("[error] Create sm2 pubKey failed.");
918         return ret;
919     }
920     *returnObj = (HcfPubKey *)pubKey;
921     return HCF_SUCCESS;
922 }
923 
PackAndAssignPriKey(const HcfAsyKeyGeneratorSpiOpensslSm2Impl * impl,const char * fieldType,EC_KEY * ecKey,HcfPriKey ** returnObj)924 static HcfResult PackAndAssignPriKey(const HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl, const char *fieldType,
925     EC_KEY *ecKey, HcfPriKey **returnObj)
926 {
927     HcfOpensslSm2PriKey *priKey = NULL;
928     HcfResult ret = PackSm2PriKey(impl->curveId, ecKey, fieldType, &priKey);
929     if (ret != HCF_SUCCESS) {
930         LOGD("[error] Create sm2 priKey failed.");
931         return ret;
932     }
933     *returnObj = (HcfPriKey *)priKey;
934     return HCF_SUCCESS;
935 }
936 
CreateAndAssignKeyPair(const HcfAsyKeyGeneratorSpiOpensslSm2Impl * impl,const char * fieldType,EC_KEY * ecKey,HcfKeyPair ** returnObj)937 static HcfResult CreateAndAssignKeyPair(const HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl, const char *fieldType,
938     EC_KEY *ecKey, HcfKeyPair **returnObj)
939 {
940     EC_KEY *ecPriKey = EC_KEY_dup(ecKey);
941     if (ecPriKey == NULL) {
942         LOGD("[error] Dup ecKey fail.");
943         return HCF_ERR_CRYPTO_OPERATION;
944     }
945     HcfOpensslSm2PriKey *priKey = NULL;
946     HcfResult ret = PackSm2PriKey(impl->curveId, ecPriKey, fieldType, &priKey);
947     if (ret != HCF_SUCCESS) {
948         LOGD("[error] Create sm2 priKey failed.");
949         Openssl_EC_KEY_free(ecPriKey);
950         return ret;
951     }
952     HcfOpensslSm2PubKey *pubKey = NULL;
953     EC_KEY *ecPubKey = EC_KEY_dup(ecKey);
954     if (ecPubKey == NULL) {
955         LOGD("[error] Dup ecKey fail.");
956         HcfObjDestroy(priKey);
957         return HCF_ERR_CRYPTO_OPERATION;
958     }
959     ret = PackSm2PubKey(impl->curveId, ecPubKey, fieldType, &pubKey);
960     if (ret != HCF_SUCCESS) {
961         LOGD("[error] Create sm2 pubKey failed.");
962         HcfObjDestroy(priKey);
963         Openssl_EC_KEY_free(ecPubKey);
964         return ret;
965     }
966 
967     HcfOpensslSm2KeyPair *returnKeyPair = NULL;
968     ret = PackSm2KeyPair(pubKey, priKey, &returnKeyPair);
969     if (ret != HCF_SUCCESS) {
970         LOGE("Create sm2 keyPair failed.");
971         HcfObjDestroy(pubKey);
972         HcfObjDestroy(priKey);
973     }
974     *returnObj = (HcfKeyPair *)returnKeyPair;
975     return ret;
976 }
977 
EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnObj)978 static HcfResult EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnObj)
979 {
980     if ((self == NULL) || (returnObj == NULL)) {
981         LOGE("Invalid input parameter.");
982         return HCF_INVALID_PARAMS;
983     }
984     if (!IsClassMatch((HcfObjectBase *)self, self->base.getClass())) {
985         LOGE("Class not match.");
986         return HCF_INVALID_PARAMS;
987     }
988 
989     HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl = (HcfAsyKeyGeneratorSpiOpensslSm2Impl *)self;
990     EC_KEY *ecKey = NULL;
991     HcfResult ret = NewEcKeyPair(impl->curveId, &ecKey);
992     if (ret == HCF_SUCCESS) {
993         ret = CreateAndAssignKeyPair(impl, g_sm2GenerateFieldType, ecKey, returnObj);
994         if (ret != HCF_SUCCESS) {
995             LOGD("[error] CreateAndAssignKeyPair failed.");
996         }
997         Openssl_EC_KEY_free(ecKey);
998     }
999     return ret;
1000 }
1001 
EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfKeyPair ** returnKeyPair)1002 static HcfResult EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1003     HcfKeyPair **returnKeyPair)
1004 {
1005     if ((self == NULL) || (returnKeyPair == NULL)) {
1006         LOGE("Invalid input parameter.");
1007         return HCF_INVALID_PARAMS;
1008     }
1009     if (!IsClassMatch((HcfObjectBase *)self, GetSm2KeyPairGeneratorClass())) {
1010         LOGE("Class not match.");
1011         return HCF_INVALID_PARAMS;
1012     }
1013 
1014     HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl = (HcfAsyKeyGeneratorSpiOpensslSm2Impl *)self;
1015     EC_KEY *ecKey = NULL;
1016     HcfResult ret = GenKeyPairSm2KeyBySpec(params, &ecKey);
1017     if (ret != HCF_SUCCESS) {
1018         LOGD("[error] Gen ec key pair with spec failed.");
1019         return ret;
1020     }
1021 
1022     // curveId == 0 means no curve to match.
1023     int32_t curveId = (int32_t)Openssl_EC_GROUP_get_curve_name(Openssl_EC_KEY_get0_group(ecKey));
1024     if (curveId != 0) {
1025         impl->curveId = curveId;
1026     }
1027     // deep copy of ecKey, free ecKey whether it succeed or failed.
1028     ret = CreateAndAssignKeyPair(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnKeyPair);
1029     Openssl_EC_KEY_free(ecKey);
1030     if (ret != HCF_SUCCESS) {
1031         LOGD("[error] CreateAndAssignKeyPair failed.");
1032         return ret;
1033     }
1034     return HCF_SUCCESS;
1035 }
1036 
EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfPubKey ** returnPubKey)1037 static HcfResult EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1038     HcfPubKey **returnPubKey)
1039 {
1040     if ((self == NULL) || (returnPubKey == NULL)) {
1041         LOGE("Invalid input parameter.");
1042         return HCF_INVALID_PARAMS;
1043     }
1044     if (!IsClassMatch((HcfObjectBase *)self, GetSm2KeyPairGeneratorClass())) {
1045         LOGE("Class not match.");
1046         return HCF_INVALID_PARAMS;
1047     }
1048 
1049     HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl = (HcfAsyKeyGeneratorSpiOpensslSm2Impl *)self;
1050     EC_KEY *ecKey = NULL;
1051     HcfResult ret = GenPubKeySm2KeyBySpec(params, &ecKey);
1052     if (ret != HCF_SUCCESS) {
1053         LOGD("[error] Gen ec pubKey with spec failed.");
1054         return ret;
1055     }
1056     int32_t curveId = (int32_t)Openssl_EC_GROUP_get_curve_name(Openssl_EC_KEY_get0_group(ecKey));
1057     if (curveId != 0) {
1058         impl->curveId = curveId;
1059     }
1060     ret = PackAndAssignPubKey(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnPubKey);
1061     if (ret != HCF_SUCCESS) {
1062         LOGD("[error] PackAndAssignPubKey failed.");
1063         Openssl_EC_KEY_free(ecKey);
1064         return ret;
1065     }
1066     return HCF_SUCCESS;
1067 }
1068 
EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * params,HcfPriKey ** returnPriKey)1069 static HcfResult EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi *self, const HcfAsyKeyParamsSpec *params,
1070     HcfPriKey **returnPriKey)
1071 {
1072     if ((self == NULL) || (returnPriKey == NULL)) {
1073         LOGE("Invalid input parameter.");
1074         return HCF_INVALID_PARAMS;
1075     }
1076     if (!IsClassMatch((HcfObjectBase *)self, GetSm2KeyPairGeneratorClass())) {
1077         LOGE("Class not match.");
1078         return HCF_INVALID_PARAMS;
1079     }
1080 
1081     HcfAsyKeyGeneratorSpiOpensslSm2Impl *impl = (HcfAsyKeyGeneratorSpiOpensslSm2Impl *)self;
1082     EC_KEY *ecKey = NULL;
1083     HcfResult ret = GenPriKeySm2KeyBySpec(params, &ecKey);
1084     if (ret != HCF_SUCCESS) {
1085         LOGD("[error] Gen ec priKey with spec failed.");
1086         return ret;
1087     }
1088 
1089     int32_t curveId = (int32_t)Openssl_EC_GROUP_get_curve_name(Openssl_EC_KEY_get0_group(ecKey));
1090     if (curveId != 0) {
1091         impl->curveId = curveId;
1092     }
1093 
1094     ret = PackAndAssignPriKey(impl, ((HcfEccCommParamsSpec *)params)->field->fieldType, ecKey, returnPriKey);
1095     if (ret != HCF_SUCCESS) {
1096         LOGD("[error] PackAndAssignPriKey failed.");
1097         Openssl_EC_KEY_free(ecKey);
1098         return ret;
1099     }
1100     return HCF_SUCCESS;
1101 }
1102 
HcfAsyKeyGeneratorSpiSm2Create(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnObj)1103 HcfResult HcfAsyKeyGeneratorSpiSm2Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj)
1104 {
1105     if (params == NULL || returnObj == NULL) {
1106         LOGE("Invalid input parameter.");
1107         return HCF_INVALID_PARAMS;
1108     }
1109     int32_t curveId = 0;
1110     if (params->bits != 0) {
1111         if (GetOpensslCurveId(params->bits, &curveId) != HCF_SUCCESS) {
1112             LOGE("Get curve id failed.");
1113             return HCF_INVALID_PARAMS;
1114         }
1115     }
1116 
1117     HcfAsyKeyGeneratorSpiOpensslSm2Impl *returnImpl = (HcfAsyKeyGeneratorSpiOpensslSm2Impl *)HcfMalloc(
1118         sizeof(HcfAsyKeyGeneratorSpiOpensslSm2Impl), 0);
1119     if (returnImpl == NULL) {
1120         LOGE("Failed to allocate returnImpl memroy!");
1121         return HCF_ERR_MALLOC;
1122     }
1123     returnImpl->base.base.getClass = GetSm2KeyPairGeneratorClass;
1124     returnImpl->base.base.destroy = DestroySm2KeyPairGenerator;
1125     returnImpl->base.engineConvertKey = EngineConvertSm2Key;
1126     returnImpl->base.engineGenerateKeyPair = EngineGenerateKeyPair;
1127     returnImpl->base.engineGenerateKeyPairBySpec = EngineGenerateKeyPairBySpec;
1128     returnImpl->base.engineGeneratePubKeyBySpec = EngineGeneratePubKeyBySpec;
1129     returnImpl->base.engineGeneratePriKeyBySpec = EngineGeneratePriKeyBySpec;
1130     returnImpl->curveId = curveId;
1131 
1132     *returnObj = (HcfAsyKeyGeneratorSpi *)returnImpl;
1133     return HCF_SUCCESS;
1134 }
1135