• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2024 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 "alg_25519_asy_key_generator_openssl.h"
17 
18 #include "securec.h"
19 
20 #include <openssl/evp.h>
21 #include <string.h>
22 
23 #include "detailed_alg_25519_key_params.h"
24 #include "log.h"
25 #include "memory.h"
26 #include "openssl_adapter.h"
27 #include "openssl_class.h"
28 #include "openssl_common.h"
29 #include "utils.h"
30 
31 #define OPENSSL_ED25519_GENERATOR_CLASS "OPENSSL.ED25519.KEYGENERATOR"
32 #define OPENSSL_X25519_GENERATOR_CLASS "OPENSSL.X25519.KEYGENERATOR"
33 #define OPENSSL_ALG_25519_PUBKEY_FORMAT "X.509"
34 #define OPENSSL_ALG_25519_PRIKEY_FORMAT "PKCS#8"
35 #define ALGORITHM_NAME_ALG25519 "Alg25519"
36 #define ALGORITHM_NAME_ED25519 "Ed25519"
37 #define ALGORITHM_NAME_X25519 "X25519"
38 
39 typedef struct {
40     HcfAsyKeyGeneratorSpi base;
41 } HcfAsyKeyGeneratorSpiAlg25519OpensslImpl;
42 
GetEd25519KeyGeneratorSpiClass(void)43 static const char *GetEd25519KeyGeneratorSpiClass(void)
44 {
45     return OPENSSL_ED25519_GENERATOR_CLASS;
46 }
47 
GetX25519KeyGeneratorSpiClass(void)48 static const char *GetX25519KeyGeneratorSpiClass(void)
49 {
50     return OPENSSL_X25519_GENERATOR_CLASS;
51 }
52 
GetAlg25519KeyPairClass(void)53 static const char *GetAlg25519KeyPairClass(void)
54 {
55     return OPENSSL_ALG25519_KEYPAIR_CLASS;
56 }
57 
GetAlg25519PubKeyClass(void)58 static const char *GetAlg25519PubKeyClass(void)
59 {
60     return OPENSSL_ALG25519_PUBKEY_CLASS;
61 }
62 
GetAlg25519PriKeyClass(void)63 static const char *GetAlg25519PriKeyClass(void)
64 {
65     return OPENSSL_ALG25519_PRIKEY_CLASS;
66 }
67 
DestroyAlg25519KeyGeneratorSpiImpl(HcfObjectBase * self)68 static void DestroyAlg25519KeyGeneratorSpiImpl(HcfObjectBase *self)
69 {
70     if ((self == NULL) || (self->getClass() == NULL)) {
71         LOGE("Invalid input parameter.");
72         return;
73     }
74 
75     if (strcmp(self->getClass(), GetX25519KeyGeneratorSpiClass()) == 0) {
76         HcfFree(self);
77         return;
78     }
79 
80     if (strcmp(self->getClass(), GetEd25519KeyGeneratorSpiClass()) == 0) {
81         HcfFree(self);
82         return;
83     }
84     LOGE("Invalid input parameter.");
85 }
86 
DestroyAlg25519PubKey(HcfObjectBase * self)87 static void DestroyAlg25519PubKey(HcfObjectBase *self)
88 {
89     if (self == NULL) {
90         LOGE("Invalid input parameter.");
91         return;
92     }
93     if (!HcfIsClassMatch(self, GetAlg25519PubKeyClass())) {
94         LOGE("Invalid class of self.");
95         return;
96     }
97     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
98     OpensslEvpPkeyFree(impl->pkey);
99     impl->pkey = NULL;
100     HcfFree(impl);
101 }
102 
DestroyAlg25519PriKey(HcfObjectBase * self)103 static void DestroyAlg25519PriKey(HcfObjectBase *self)
104 {
105     if (self == NULL) {
106         LOGE("Invalid input parameter.");
107         return;
108     }
109     if (!HcfIsClassMatch(self, GetAlg25519PriKeyClass())) {
110         LOGE("Invalid class of self.");
111         return;
112     }
113     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
114     OpensslEvpPkeyFree(impl->pkey);
115     impl->pkey = NULL;
116     HcfFree(impl);
117 }
118 
DestroyAlg25519KeyPair(HcfObjectBase * self)119 static void DestroyAlg25519KeyPair(HcfObjectBase *self)
120 {
121     if (self == NULL) {
122         LOGE("Invalid input parameter.");
123         return;
124     }
125     if (!HcfIsClassMatch(self, GetAlg25519KeyPairClass())) {
126         LOGE("Invalid class of self.");
127         return;
128     }
129     HcfOpensslAlg25519KeyPair *impl = (HcfOpensslAlg25519KeyPair *)self;
130     DestroyAlg25519PubKey((HcfObjectBase *)impl->base.pubKey);
131     impl->base.pubKey = NULL;
132     DestroyAlg25519PriKey((HcfObjectBase *)impl->base.priKey);
133     impl->base.priKey = NULL;
134     HcfFree(self);
135 }
136 
GetAlg25519PubKeyAlgorithm(HcfKey * self)137 static const char *GetAlg25519PubKeyAlgorithm(HcfKey *self)
138 {
139     if (self == NULL) {
140         LOGE("Invalid input parameter.");
141         return NULL;
142     }
143     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
144         LOGE("Invalid class of self.");
145         return NULL;
146     }
147 
148     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
149     if (impl->type == EVP_PKEY_ED25519) {
150         return ALGORITHM_NAME_ED25519;
151     }
152 
153     return ALGORITHM_NAME_X25519;
154 }
155 
GetAlg25519PriKeyAlgorithm(HcfKey * self)156 static const char *GetAlg25519PriKeyAlgorithm(HcfKey *self)
157 {
158     if (self == NULL) {
159         LOGE("Invalid input parameter.");
160         return NULL;
161     }
162     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
163         LOGE("Invalid class of self.");
164         return NULL;
165     }
166 
167     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
168     if (impl->type == EVP_PKEY_ED25519) {
169         return ALGORITHM_NAME_ED25519;
170     }
171 
172     return ALGORITHM_NAME_X25519;
173 }
174 
GetAlg25519PubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)175 static HcfResult GetAlg25519PubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
176 {
177     if ((self == NULL) || (returnBlob == NULL)) {
178         LOGE("Invalid input parameter.");
179         return HCF_INVALID_PARAMS;
180     }
181     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
182         LOGE("Invalid class of self.");
183         return HCF_INVALID_PARAMS;
184     }
185     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
186     if (impl->pkey == NULL) {
187         LOGE("pkey is NULL.");
188         return HCF_INVALID_PARAMS;
189     }
190     unsigned char *returnData = NULL;
191     int len = OpensslI2dPubKey(impl->pkey, &returnData);
192     if (len <= 0) {
193         LOGD("[error] Call i2d_PUBKEY failed");
194         HcfPrintOpensslError();
195         return HCF_ERR_CRYPTO_OPERATION;
196     }
197     returnBlob->data = returnData;
198     returnBlob->len = len;
199     return HCF_SUCCESS;
200 }
201 
GetAlg25519PubKeyEncodedPem(HcfKey * self,const char * format,char ** returnString)202 static HcfResult GetAlg25519PubKeyEncodedPem(HcfKey *self, const char *format, char **returnString)
203 {
204     (void)self;
205     (void)format;
206     (void)returnString;
207     return HCF_INVALID_PARAMS;
208 }
209 
GetAlg25519PriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)210 static HcfResult GetAlg25519PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
211 {
212     if ((self == NULL) || (returnBlob == NULL)) {
213         LOGE("Invalid input parameter.");
214         return HCF_INVALID_PARAMS;
215     }
216     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
217         LOGE("Invalid class of self.");
218         return HCF_INVALID_PARAMS;
219     }
220     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
221     if (impl->pkey == NULL) {
222         LOGE("pkey is NULL.");
223         return HCF_INVALID_PARAMS;
224     }
225     unsigned char *returnData = NULL;
226     int len = OpensslI2dPrivateKey(impl->pkey, &returnData);
227     if (len <= 0) {
228         LOGD("[error] Call i2d_PrivateKey failed");
229         HcfPrintOpensslError();
230         return HCF_ERR_CRYPTO_OPERATION;
231     }
232     returnBlob->data = returnData;
233     returnBlob->len = len;
234     return HCF_SUCCESS;
235 }
236 
GetAlg25519PriKeyEncodedPem(const HcfPriKey * self,HcfParamsSpec * paramsSpec,const char * format,char ** returnString)237 static HcfResult GetAlg25519PriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *paramsSpec, const char *format,
238     char **returnString)
239 {
240     (void)self;
241     (void)paramsSpec;
242     (void)format;
243     (void)returnString;
244     return HCF_INVALID_PARAMS;
245 }
246 
GetAlg25519PubKeyFormat(HcfKey * self)247 static const char *GetAlg25519PubKeyFormat(HcfKey *self)
248 {
249     if (self == NULL) {
250         LOGE("Invalid input parameter.");
251         return NULL;
252     }
253     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
254         LOGE("Invalid class of self.");
255         return NULL;
256     }
257     return OPENSSL_ALG_25519_PUBKEY_FORMAT;
258 }
259 
GetAlg25519PriKeyFormat(HcfKey * self)260 static const char *GetAlg25519PriKeyFormat(HcfKey *self)
261 {
262     if (self == NULL) {
263         LOGE("Invalid input parameter.");
264         return NULL;
265     }
266     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
267         LOGE("Invalid class of self.");
268         return NULL;
269     }
270     return OPENSSL_ALG_25519_PRIKEY_FORMAT;
271 }
272 
GetAlg25519PubKey(EVP_PKEY * pubKey,HcfBigInteger * returnBigInteger)273 static HcfResult GetAlg25519PubKey(EVP_PKEY *pubKey, HcfBigInteger *returnBigInteger)
274 {
275     size_t len = 0;
276     if (!OpensslEvpPkeyGetRawPublicKey(pubKey, NULL, &len)) {
277         LOGD("[error] Get len failed.");
278         return HCF_ERR_CRYPTO_OPERATION;
279     }
280     returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0);
281     if (returnBigInteger->data == NULL) {
282         LOGE("Failed to allocate returnBigInteger memory.");
283         return HCF_ERR_MALLOC;
284     }
285     if (!OpensslEvpPkeyGetRawPublicKey(pubKey, returnBigInteger->data, &len)) {
286         LOGD("[error] Get data failed.");
287         HcfFree(returnBigInteger->data);
288         returnBigInteger->data = NULL;
289         return HCF_ERR_CRYPTO_OPERATION;
290     }
291     returnBigInteger->len = len;
292     return HCF_SUCCESS;
293 }
294 
CheckEvpKeyTypeFromAlg25519PubKey(EVP_PKEY * alg25519Pk,const AsyKeySpecItem item)295 static HcfResult CheckEvpKeyTypeFromAlg25519PubKey(EVP_PKEY *alg25519Pk, const AsyKeySpecItem item)
296 {
297     int type = OpensslEvpPkeyBaseId(alg25519Pk);
298     if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) {
299         LOGE("Invalid pkey type.");
300         return HCF_INVALID_PARAMS;
301     }
302     if ((type == EVP_PKEY_ED25519 && item != ED25519_PK_BN) ||
303         (type == EVP_PKEY_X25519 && item != X25519_PK_BN)) {
304         LOGE("Invalid AsyKeySpecItem.");
305         return HCF_INVALID_PARAMS;
306     }
307     return HCF_SUCCESS;
308 }
309 
CheckEvpKeyTypeFromAlg25519PriKey(EVP_PKEY * alg25519Sk,const AsyKeySpecItem item)310 static HcfResult CheckEvpKeyTypeFromAlg25519PriKey(EVP_PKEY *alg25519Sk, const AsyKeySpecItem item)
311 {
312     int type = OpensslEvpPkeyBaseId(alg25519Sk);
313     if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) {
314         LOGE("Invalid pkey type.");
315         return HCF_INVALID_PARAMS;
316     }
317     if ((type == EVP_PKEY_ED25519 && item != ED25519_SK_BN) ||
318         (type == EVP_PKEY_X25519 && item != X25519_SK_BN)) {
319         LOGE("Invalid AsyKeySpecItem.");
320         return HCF_INVALID_PARAMS;
321     }
322     return HCF_SUCCESS;
323 }
324 
GetBigIntegerSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)325 static HcfResult GetBigIntegerSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item,
326     HcfBigInteger *returnBigInteger)
327 {
328     if (self == NULL || returnBigInteger == NULL) {
329         LOGE("Invalid input parameter.");
330         return HCF_INVALID_PARAMS;
331     }
332     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
333         LOGE("Invalid class of self.");
334         return HCF_INVALID_PARAMS;
335     }
336     HcfResult ret = HCF_INVALID_PARAMS;
337     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
338     EVP_PKEY *alg25519Pk = impl->pkey;
339     if (alg25519Pk == NULL) {
340         LOGE("pKey is null.");
341         return HCF_INVALID_PARAMS;
342     }
343     if (CheckEvpKeyTypeFromAlg25519PubKey(alg25519Pk, item) != HCF_SUCCESS) {
344         LOGE("Check pKey type failed.");
345         return HCF_INVALID_PARAMS;
346     }
347     if (item == ED25519_PK_BN || item == X25519_PK_BN) {
348         ret = GetAlg25519PubKey(alg25519Pk, returnBigInteger);
349     } else {
350         LOGE("Input item is invalid");
351     }
352     return ret;
353 }
354 
GetAlg25519PriKey(EVP_PKEY * priKey,HcfBigInteger * returnBigInteger)355 static HcfResult GetAlg25519PriKey(EVP_PKEY *priKey, HcfBigInteger *returnBigInteger)
356 {
357     size_t len = 0;
358     if (!OpensslEvpPkeyGetRawPrivateKey(priKey, NULL, &len)) {
359         LOGD("[error] Get private key length failed.");
360         return HCF_ERR_CRYPTO_OPERATION;
361     }
362     returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0);
363     if (returnBigInteger->data == NULL) {
364         LOGE("Failed to allocate returnBigInteger memory.");
365         return HCF_ERR_MALLOC;
366     }
367     if (!OpensslEvpPkeyGetRawPrivateKey(priKey, returnBigInteger->data, &len)) {
368         LOGD("[error] Get data failed.");
369         HcfFree(returnBigInteger->data);
370         returnBigInteger->data = NULL;
371         return HCF_ERR_CRYPTO_OPERATION;
372     }
373     returnBigInteger->len = len;
374     return HCF_SUCCESS;
375 }
376 
GetBigIntegerSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)377 static HcfResult GetBigIntegerSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item,
378     HcfBigInteger *returnBigInteger)
379 {
380     if (self == NULL || returnBigInteger == NULL) {
381         LOGE("Invalid input parameter.");
382         return HCF_INVALID_PARAMS;
383     }
384     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
385         LOGE("Invalid class of self.");
386         return HCF_INVALID_PARAMS;
387     }
388     HcfResult ret = HCF_INVALID_PARAMS;
389     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
390     EVP_PKEY *alg25519Sk = impl->pkey;
391     if (alg25519Sk == NULL) {
392         LOGE("pKey is null.");
393         return HCF_INVALID_PARAMS;
394     }
395     if (CheckEvpKeyTypeFromAlg25519PriKey(alg25519Sk, item) != HCF_SUCCESS) {
396         LOGE("Check pKey type failed.");
397         return HCF_INVALID_PARAMS;
398     }
399     if (item == ED25519_SK_BN || item == X25519_SK_BN) {
400         ret = GetAlg25519PriKey(alg25519Sk, returnBigInteger);
401     } else {
402         LOGE("Input item is invalid");
403     }
404     return ret;
405 }
406 
GetIntSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)407 static HcfResult GetIntSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
408 {
409     (void)self;
410     (void)returnInt;
411     return HCF_NOT_SUPPORT;
412 }
413 
GetIntSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)414 static HcfResult GetIntSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
415 {
416     (void)self;
417     (void)returnInt;
418     return HCF_NOT_SUPPORT;
419 }
420 
GetStrSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)421 static HcfResult GetStrSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
422 {
423     (void)self;
424     (void)returnString;
425     return HCF_NOT_SUPPORT;
426 }
427 
GetStrSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)428 static HcfResult GetStrSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
429 {
430     (void)self;
431     (void)returnString;
432     return HCF_NOT_SUPPORT;
433 }
434 
GetAlg25519PriKeyEncodedDer(const HcfPriKey * self,const char * format,HcfBlob * returnBlob)435 static HcfResult GetAlg25519PriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob)
436 {
437     (void)self;
438     (void)format;
439     (void)returnBlob;
440     return HCF_INVALID_PARAMS;
441 }
442 
ClearAlg25519PriKeyMem(HcfPriKey * self)443 static void ClearAlg25519PriKeyMem(HcfPriKey *self)
444 {
445     if (self == NULL) {
446         LOGE("Invalid params.");
447         return;
448     }
449     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
450         LOGE("Invalid class of self.");
451         return;
452     }
453     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
454     OpensslEvpPkeyFree(impl->pkey);
455     impl->pkey = NULL;
456 }
457 
GenerateAlg25519EvpKey(int type,EVP_PKEY ** ppkey)458 static HcfResult GenerateAlg25519EvpKey(int type, EVP_PKEY **ppkey)
459 {
460     EVP_PKEY_CTX *paramsCtx = NULL;
461     HcfResult ret = HCF_SUCCESS;
462     do {
463         paramsCtx = OpensslEvpPkeyCtxNewId(type, NULL);
464         if (paramsCtx == NULL) {
465             LOGE("Create params ctx failed.");
466             ret = HCF_ERR_MALLOC;
467             break;
468         }
469         if (OpensslEvpPkeyKeyGenInit(paramsCtx) != HCF_OPENSSL_SUCCESS) {
470             LOGD("[error] Key ctx generate init failed.");
471             ret = HCF_ERR_CRYPTO_OPERATION;
472             break;
473         }
474         if (OpensslEvpPkeyKeyGen(paramsCtx, ppkey) != HCF_OPENSSL_SUCCESS) {
475             LOGD("[error] Generate pkey failed.");
476             ret = HCF_ERR_CRYPTO_OPERATION;
477             break;
478         }
479     } while (0);
480     if (paramsCtx != NULL) {
481         OpensslEvpPkeyCtxFree(paramsCtx);
482     }
483     return ret;
484 }
485 
GetAlg25519PubKeyEncodedDer(const HcfPubKey * self,const char * format,HcfBlob * returnBlob)486 static HcfResult GetAlg25519PubKeyEncodedDer(const HcfPubKey *self, const char *format, HcfBlob *returnBlob)
487 {
488     (void)self;
489     (void)format;
490     (void)returnBlob;
491     return HCF_INVALID_PARAMS;
492 }
493 
FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey * pk)494 static void FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey *pk)
495 {
496     pk->base.base.base.destroy = DestroyAlg25519PubKey;
497     pk->base.base.base.getClass = GetAlg25519PubKeyClass;
498     pk->base.base.getAlgorithm = GetAlg25519PubKeyAlgorithm;
499     pk->base.base.getEncoded = GetAlg25519PubKeyEncoded;
500     pk->base.base.getEncodedPem = GetAlg25519PubKeyEncodedPem;
501     pk->base.base.getFormat = GetAlg25519PubKeyFormat;
502     pk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PubKey;
503     pk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PubKey;
504     pk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PubKey;
505     pk->base.getEncodedDer = GetAlg25519PubKeyEncodedDer;
506 }
507 
FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey * sk)508 static void FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey *sk)
509 {
510     sk->base.base.base.destroy = DestroyAlg25519PriKey;
511     sk->base.base.base.getClass = GetAlg25519PriKeyClass;
512     sk->base.base.getAlgorithm = GetAlg25519PriKeyAlgorithm;
513     sk->base.base.getEncoded = GetAlg25519PriKeyEncoded;
514     sk->base.getEncodedPem = GetAlg25519PriKeyEncodedPem;
515     sk->base.base.getFormat = GetAlg25519PriKeyFormat;
516     sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PriKey;
517     sk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PriKey;
518     sk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PriKey;
519     sk->base.getEncodedDer = GetAlg25519PriKeyEncodedDer;
520     sk->base.clearMem = ClearAlg25519PriKeyMem;
521 }
522 
CreateAlg25519PubKey(EVP_PKEY * pkey,HcfOpensslAlg25519PubKey ** returnPubKey)523 static HcfResult CreateAlg25519PubKey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey)
524 {
525     HcfOpensslAlg25519PubKey *alg25519PubKey =
526         (HcfOpensslAlg25519PubKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PubKey), 0);
527     if (alg25519PubKey == NULL) {
528         LOGE("Failed to allocate alg25519 public key memory.");
529         return HCF_ERR_MALLOC;
530     }
531     FillOpensslAlg25519PubKeyFunc(alg25519PubKey);
532     alg25519PubKey->pkey = pkey;
533     *returnPubKey = alg25519PubKey;
534     return HCF_SUCCESS;
535 }
536 
CreateAlg25519PriKey(EVP_PKEY * pkey,HcfOpensslAlg25519PriKey ** returnPriKey)537 static HcfResult CreateAlg25519PriKey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey)
538 {
539     HcfOpensslAlg25519PriKey *alg25519PriKey =
540         (HcfOpensslAlg25519PriKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PriKey), 0);
541     if (alg25519PriKey == NULL) {
542         LOGE("Failed to allocate alg25519 private key memory.");
543         return HCF_ERR_MALLOC;
544     }
545     FillOpensslAlg25519PriKeyFunc(alg25519PriKey);
546     alg25519PriKey->pkey = pkey;
547     *returnPriKey = alg25519PriKey;
548     return HCF_SUCCESS;
549 }
550 
CreateAlg25519KeyPair(const HcfOpensslAlg25519PubKey * pubKey,const HcfOpensslAlg25519PriKey * priKey,HcfKeyPair ** returnKeyPair)551 static HcfResult CreateAlg25519KeyPair(const HcfOpensslAlg25519PubKey *pubKey,
552     const HcfOpensslAlg25519PriKey *priKey, HcfKeyPair **returnKeyPair)
553 {
554     HcfOpensslAlg25519KeyPair *keyPair =
555         (HcfOpensslAlg25519KeyPair *)HcfMalloc(sizeof(HcfOpensslAlg25519KeyPair), 0);
556     if (keyPair == NULL) {
557         LOGE("Failed to allocate keyPair memory.");
558         return HCF_ERR_MALLOC;
559     }
560     keyPair->base.base.getClass = GetAlg25519KeyPairClass;
561     keyPair->base.base.destroy = DestroyAlg25519KeyPair;
562     keyPair->base.pubKey = (HcfPubKey *)pubKey;
563     keyPair->base.priKey = (HcfPriKey *)priKey;
564 
565     *returnKeyPair = (HcfKeyPair *)keyPair;
566     return HCF_SUCCESS;
567 }
568 
GeneratePubKeyByPkey(EVP_PKEY * pkey,HcfOpensslAlg25519PubKey ** returnPubKey)569 static HcfResult GeneratePubKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey)
570 {
571     EVP_PKEY *evpPkey = OpensslEvpPkeyDup(pkey);
572     if (evpPkey == NULL) {
573         LOGD("[error] pkey dup failed");
574         HcfPrintOpensslError();
575         return HCF_ERR_CRYPTO_OPERATION;
576     }
577     HcfResult ret = CreateAlg25519PubKey(evpPkey, returnPubKey);
578     if (ret != HCF_SUCCESS) {
579         LOGD("[error] Create alg25519 public key failed");
580         OpensslEvpPkeyFree(evpPkey);
581     }
582     return ret;
583 }
584 
GeneratePriKeyByPkey(EVP_PKEY * pkey,HcfOpensslAlg25519PriKey ** returnPriKey)585 static HcfResult GeneratePriKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey)
586 {
587     EVP_PKEY *evpPkey = OpensslEvpPkeyDup(pkey);
588     if (evpPkey == NULL) {
589         LOGD("[error] pkey dup failed");
590         HcfPrintOpensslError();
591         return HCF_ERR_CRYPTO_OPERATION;
592     }
593     HcfResult ret = CreateAlg25519PriKey(evpPkey, returnPriKey);
594     if (ret != HCF_SUCCESS) {
595         LOGD("[error] Create alg25519 private key failed");
596         OpensslEvpPkeyFree(evpPkey);
597     }
598     return ret;
599 }
600 
GenerateAlg25519PubAndPriKey(int type,HcfOpensslAlg25519PubKey ** returnPubKey,HcfOpensslAlg25519PriKey ** returnPriKey)601 static HcfResult GenerateAlg25519PubAndPriKey(int type, HcfOpensslAlg25519PubKey **returnPubKey,
602     HcfOpensslAlg25519PriKey **returnPriKey)
603 {
604     EVP_PKEY *pkey = NULL;
605     HcfResult ret = GenerateAlg25519EvpKey(type, &pkey);
606     if (ret != HCF_SUCCESS) {
607         LOGD("[error] Generate alg25519 EVP_PKEY failed.");
608         return ret;
609     }
610 
611     ret = GeneratePubKeyByPkey(pkey, returnPubKey);
612     if (ret != HCF_SUCCESS) {
613         LOGD("[error] Generate pubkey fail.");
614         OpensslEvpPkeyFree(pkey);
615         return ret;
616     }
617 
618     ret = GeneratePriKeyByPkey(pkey, returnPriKey);
619     if (ret != HCF_SUCCESS) {
620         LOGD("[error] Generate prikey fail.");
621         HcfObjDestroy(*returnPubKey);
622         *returnPubKey = NULL;
623         OpensslEvpPkeyFree(pkey);
624         return HCF_ERR_CRYPTO_OPERATION;
625     }
626 
627     OpensslEvpPkeyFree(pkey);
628     return ret;
629 }
630 
ConvertAlg25519PubKey(const HcfBlob * pubKeyBlob,HcfOpensslAlg25519PubKey ** returnPubKey)631 static HcfResult ConvertAlg25519PubKey(const HcfBlob *pubKeyBlob, HcfOpensslAlg25519PubKey **returnPubKey)
632 {
633     const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data);
634     EVP_PKEY *pkey = OpensslD2iPubKey(NULL, &tmpData, pubKeyBlob->len);
635     if (pkey == NULL) {
636         LOGD("[error] Call d2i_PUBKEY fail.");
637         HcfPrintOpensslError();
638         return HCF_ERR_CRYPTO_OPERATION;
639     }
640     HcfResult ret = CreateAlg25519PubKey(pkey, returnPubKey);
641     if (ret != HCF_SUCCESS) {
642         LOGD("[error] Create alg25519 public key failed");
643         OpensslEvpPkeyFree(pkey);
644     }
645     return ret;
646 }
647 
ConvertAlg25519PriKey(int type,const HcfBlob * priKeyBlob,HcfOpensslAlg25519PriKey ** returnPriKey)648 static HcfResult ConvertAlg25519PriKey(int type, const HcfBlob *priKeyBlob,
649     HcfOpensslAlg25519PriKey **returnPriKey)
650 {
651     const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data);
652     EVP_PKEY *pkey = OpensslD2iPrivateKey(type, NULL, &tmpData, priKeyBlob->len);
653     if (pkey == NULL) {
654         LOGD("[error] Call d2i_PrivateKey fail.");
655         HcfPrintOpensslError();
656         return HCF_ERR_CRYPTO_OPERATION;
657     }
658     HcfResult ret = CreateAlg25519PriKey(pkey, returnPriKey);
659     if (ret != HCF_SUCCESS) {
660         LOGD("[error] Create alg25519 private key failed");
661         OpensslEvpPkeyFree(pkey);
662     }
663     return ret;
664 }
665 
ConvertAlg25519PubAndPriKey(int type,const HcfBlob * pubKeyBlob,const HcfBlob * priKeyBlob,HcfOpensslAlg25519PubKey ** returnPubKey,HcfOpensslAlg25519PriKey ** returnPriKey)666 static HcfResult ConvertAlg25519PubAndPriKey(int type, const HcfBlob *pubKeyBlob, const HcfBlob *priKeyBlob,
667     HcfOpensslAlg25519PubKey **returnPubKey, HcfOpensslAlg25519PriKey **returnPriKey)
668 {
669     if (pubKeyBlob != NULL) {
670         if (ConvertAlg25519PubKey(pubKeyBlob, returnPubKey) != HCF_SUCCESS) {
671             LOGD("[error] Convert alg25519 public key failed.");
672             return HCF_ERR_CRYPTO_OPERATION;
673         }
674     }
675     if (priKeyBlob != NULL) {
676         if (ConvertAlg25519PriKey(type, priKeyBlob, returnPriKey) != HCF_SUCCESS) {
677             LOGD("[error] Convert alg25519 private key failed.");
678             HcfObjDestroy(*returnPubKey);
679             *returnPubKey = NULL;
680             return HCF_ERR_CRYPTO_OPERATION;
681         }
682     }
683     return HCF_SUCCESS;
684 }
685 
CheckClassMatch(HcfAsyKeyGeneratorSpi * self,int * type)686 static HcfResult CheckClassMatch(HcfAsyKeyGeneratorSpi *self, int *type)
687 {
688     if (HcfIsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass())) {
689         *type = EVP_PKEY_ED25519;
690     } else if (HcfIsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) {
691         *type = EVP_PKEY_X25519;
692     } else {
693         LOGE("Invalid class of self.");
694         return HCF_INVALID_PARAMS;
695     }
696     return HCF_SUCCESS;
697 }
698 
EngineGenerateAlg25519KeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnKeyPair)699 static HcfResult EngineGenerateAlg25519KeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnKeyPair)
700 {
701     if (self == NULL || returnKeyPair == NULL) {
702         LOGE("Invalid params.");
703         return HCF_INVALID_PARAMS;
704     }
705     int type = 0;
706     if (CheckClassMatch(self, &type) != HCF_SUCCESS) {
707         LOGE("Invalid class of self.");
708         return HCF_INVALID_PARAMS;
709     }
710 
711     HcfOpensslAlg25519PubKey *pubKey = NULL;
712     HcfOpensslAlg25519PriKey *priKey = NULL;
713     HcfResult ret = GenerateAlg25519PubAndPriKey(type, &pubKey, &priKey);
714     if (ret != HCF_SUCCESS) {
715         LOGE("Generate alg25519 pk and sk by openssl failed.");
716         return ret;
717     }
718 
719     if (pubKey != NULL) {
720         pubKey->type = type;
721     }
722 
723     if (priKey != NULL) {
724         priKey->type = type;
725     }
726 
727     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
728     if (ret != HCF_SUCCESS) {
729         LOGE("Create alg25519 keyPair failed.");
730         HcfObjDestroy(pubKey);
731         pubKey = NULL;
732         HcfObjDestroy(priKey);
733         priKey = NULL;
734         return ret;
735     }
736     return HCF_SUCCESS;
737 }
738 
EngineConvertAlg25519Key(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)739 static HcfResult EngineConvertAlg25519Key(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
740     HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
741 {
742     (void)params;
743     if ((self == NULL) || (returnKeyPair == NULL)) {
744         LOGE("Invalid input parameter.");
745         return HCF_INVALID_PARAMS;
746     }
747     int type = 0;
748     if (CheckClassMatch(self, &type) != HCF_SUCCESS) {
749         LOGE("Invalid class of self.");
750         return HCF_INVALID_PARAMS;
751     }
752     bool pubKeyValid = HcfIsBlobValid(pubKeyBlob);
753     bool priKeyValid = HcfIsBlobValid(priKeyBlob);
754     if ((!pubKeyValid) && (!priKeyValid)) {
755         LOGE("The private key and public key cannot both be NULL.");
756         return HCF_INVALID_PARAMS;
757     }
758 
759     HcfOpensslAlg25519PubKey *pubKey = NULL;
760     HcfOpensslAlg25519PriKey *priKey = NULL;
761     HcfBlob *inputPk = pubKeyValid ? pubKeyBlob : NULL;
762     HcfBlob *inputSk = priKeyValid ? priKeyBlob : NULL;
763     HcfResult ret = ConvertAlg25519PubAndPriKey(type, inputPk, inputSk, &pubKey, &priKey);
764     if (ret != HCF_SUCCESS) {
765         LOGE("Convert alg25519 keyPair failed.");
766         return ret;
767     }
768 
769     if (pubKey != NULL) {
770         pubKey->type = type;
771     }
772 
773     if (priKey != NULL) {
774         priKey->type = type;
775     }
776 
777     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
778     if (ret != HCF_SUCCESS) {
779         LOGE("Create alg25519 keyPair failed.");
780         HcfObjDestroy(pubKey);
781         pubKey = NULL;
782         HcfObjDestroy(priKey);
783         priKey = NULL;
784     }
785     return ret;
786 }
787 
ConvertAlg25519PemPubKey(int type,const char * pubKeyStr,HcfOpensslAlg25519PubKey ** returnPubKey)788 static HcfResult ConvertAlg25519PemPubKey(int type, const char *pubKeyStr, HcfOpensslAlg25519PubKey **returnPubKey)
789 {
790     EVP_PKEY *pkey = NULL;
791     const char *keyType = NULL;
792     if (type == EVP_PKEY_ED25519) {
793         keyType = "ED25519";
794     }
795     if (type == EVP_PKEY_X25519) {
796         keyType = "X25519";
797     }
798 
799     HcfResult ret = ConvertPubPemStrToKey(&pkey, keyType, EVP_PKEY_PUBLIC_KEY, pubKeyStr);
800     if (ret != HCF_SUCCESS) {
801         LOGE("Convert public key from pem to key failed.");
802         return ret;
803     }
804 
805     ret = CreateAlg25519PubKey(pkey, returnPubKey);
806     if (ret != HCF_SUCCESS) {
807         LOGE("Create alg25519 public key failed.");
808         OpensslEvpPkeyFree(pkey);
809     }
810 
811     return ret;
812 }
813 
ConvertAlg25519PemPriKey(int type,const char * priKeyStr,HcfOpensslAlg25519PriKey ** returnPriKey)814 static HcfResult ConvertAlg25519PemPriKey(int type, const char *priKeyStr, HcfOpensslAlg25519PriKey **returnPriKey)
815 {
816     EVP_PKEY *pkey = NULL;
817     const char *keyType = NULL;
818     if (type == EVP_PKEY_ED25519) {
819         keyType = "ED25519";
820     }
821     if (type == EVP_PKEY_X25519) {
822         keyType = "X25519";
823     }
824 
825     HcfResult ret = ConvertPriPemStrToKey(priKeyStr, &pkey, keyType);
826     if (ret != HCF_SUCCESS) {
827         LOGE("Convert private key from pem to key failed.");
828         return ret;
829     }
830 
831     ret = CreateAlg25519PriKey(pkey, returnPriKey);
832     if (ret != HCF_SUCCESS) {
833         LOGE("Create alg25519 private key failed.");
834         OpensslEvpPkeyFree(pkey);
835     }
836     return ret;
837 }
838 
ConvertAlg25519PemPubAndPriKey(int type,const char * pubKeyStr,const char * priKeyStr,HcfOpensslAlg25519PubKey ** returnPubKey,HcfOpensslAlg25519PriKey ** returnPriKey)839 static HcfResult ConvertAlg25519PemPubAndPriKey(int type, const char *pubKeyStr, const char *priKeyStr,
840     HcfOpensslAlg25519PubKey **returnPubKey, HcfOpensslAlg25519PriKey **returnPriKey)
841 {
842     if (pubKeyStr != NULL && strlen(pubKeyStr) != 0) {
843         if (ConvertAlg25519PemPubKey(type, pubKeyStr, returnPubKey) != HCF_SUCCESS) {
844             LOGE("Convert alg25519 pem public key failed.");
845             return HCF_ERR_CRYPTO_OPERATION;
846         }
847     }
848     if (priKeyStr != NULL && strlen(priKeyStr) != 0) {
849         if (ConvertAlg25519PemPriKey(type, priKeyStr, returnPriKey) != HCF_SUCCESS) {
850             LOGE("Convert alg25519 pem private key failed.");
851             HcfObjDestroy(*returnPubKey);
852             *returnPubKey = NULL;
853             return HCF_ERR_CRYPTO_OPERATION;
854         }
855     }
856     return HCF_SUCCESS;
857 }
858 
EngineConvertX25519PemKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,const char * pubKeyStr,const char * priKeyStr,HcfKeyPair ** returnKeyPair)859 static HcfResult EngineConvertX25519PemKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, const char *pubKeyStr,
860     const char *priKeyStr, HcfKeyPair **returnKeyPair)
861 {
862     (void)params;
863     if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyStr == NULL) && (priKeyStr == NULL))) {
864         LOGE("Invalid input parameter.");
865         return HCF_INVALID_PARAMS;
866     }
867 
868     if (!HcfIsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) {
869         LOGE("Class not match.");
870         return HCF_INVALID_PARAMS;
871     }
872 
873     HcfOpensslAlg25519PubKey *pubKey = NULL;
874     HcfOpensslAlg25519PriKey *priKey = NULL;
875     int type = EVP_PKEY_X25519;
876     HcfResult ret = ConvertAlg25519PemPubAndPriKey(type, pubKeyStr, priKeyStr, &pubKey, &priKey);
877     if (ret != HCF_SUCCESS) {
878         LOGE("Convert alg25519 pem keyPair failed.");
879         return ret;
880     }
881 
882     if (pubKey != NULL) {
883         pubKey->type = type;
884     }
885     if (priKey != NULL) {
886         priKey->type = type;
887     }
888     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
889     if (ret != HCF_SUCCESS) {
890         LOGE("Create alg25519 keyPair failed.");
891         HcfObjDestroy(pubKey);
892         pubKey = NULL;
893         HcfObjDestroy(priKey);
894         priKey = NULL;
895     }
896     return ret;
897 }
898 
EngineConvertEd25519PemKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,const char * pubKeyStr,const char * priKeyStr,HcfKeyPair ** returnKeyPair)899 static HcfResult EngineConvertEd25519PemKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, const char *pubKeyStr,
900     const char *priKeyStr, HcfKeyPair **returnKeyPair)
901 {
902     (void)params;
903     if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyStr == NULL) && (priKeyStr == NULL))) {
904         LOGE("Invalid input parameter.");
905         return HCF_INVALID_PARAMS;
906     }
907 
908     if (!HcfIsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass())) {
909         LOGE("Class not match.");
910         return HCF_INVALID_PARAMS;
911     }
912 
913     HcfOpensslAlg25519PubKey *pubKey = NULL;
914     HcfOpensslAlg25519PriKey *priKey = NULL;
915     int type = EVP_PKEY_ED25519;
916     HcfResult ret = ConvertAlg25519PemPubAndPriKey(type, pubKeyStr, priKeyStr, &pubKey, &priKey);
917     if (ret != HCF_SUCCESS) {
918         LOGE("Convert alg25519 pem keyPair failed.");
919         return ret;
920     }
921 
922     if (pubKey != NULL) {
923         pubKey->type = type;
924     }
925     if (priKey != NULL) {
926         priKey->type = type;
927     }
928     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
929     if (ret != HCF_SUCCESS) {
930         LOGE("Create alg25519 keyPair failed.");
931         HcfObjDestroy(pubKey);
932         pubKey = NULL;
933         HcfObjDestroy(priKey);
934         priKey = NULL;
935     }
936     return ret;
937 }
938 
CreateOpensslAlg25519PubKey(const HcfBigInteger * pk,const char * algName,EVP_PKEY ** returnAlg25519)939 static HcfResult CreateOpensslAlg25519PubKey(const HcfBigInteger *pk, const char *algName,
940     EVP_PKEY **returnAlg25519)
941 {
942     EVP_PKEY *pubkey = NULL;
943     if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) {
944         pubkey = OpensslEvpPkeyNewRawPublicKey(EVP_PKEY_ED25519, NULL, pk->data, pk->len);
945     } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) {
946         pubkey = OpensslEvpPkeyNewRawPublicKey(EVP_PKEY_X25519, NULL, pk->data, pk->len);
947     } else {
948         LOGE("Invalid algName! [Algo]: %{public}s", algName);
949         return HCF_INVALID_PARAMS;
950     }
951     if (pubkey == NULL) {
952         LOGD("[error] Set alg25519 pubKey failed.");
953         HcfPrintOpensslError();
954         return HCF_ERR_CRYPTO_OPERATION;
955     }
956     *returnAlg25519 = pubkey;
957     return HCF_SUCCESS;
958 }
959 
CreateOpensslAlg25519PriKey(const HcfBigInteger * sk,const char * algName,EVP_PKEY ** returnAlg25519)960 static HcfResult CreateOpensslAlg25519PriKey(const HcfBigInteger *sk, const char *algName,
961     EVP_PKEY **returnAlg25519)
962 {
963     EVP_PKEY *privkey = NULL;
964     if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) {
965         privkey = OpensslEvpPkeyNewRawPrivateKey(EVP_PKEY_ED25519, NULL, sk->data, sk->len);
966     } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) {
967         privkey = OpensslEvpPkeyNewRawPrivateKey(EVP_PKEY_X25519, NULL, sk->data, sk->len);
968     } else {
969         LOGE("Invalid algName! [Algo]: %{public}s", algName);
970         return HCF_INVALID_PARAMS;
971     }
972     if (privkey == NULL) {
973         LOGD("[error] Get alg25519 priKey failed.");
974         HcfPrintOpensslError();
975         return HCF_ERR_CRYPTO_OPERATION;
976     }
977     *returnAlg25519 = privkey;
978     return HCF_SUCCESS;
979 }
980 
CreateAlg25519PubKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PubKey ** returnPubKey)981 static HcfResult CreateAlg25519PubKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
982     const char *algName, HcfOpensslAlg25519PubKey **returnPubKey)
983 {
984     EVP_PKEY *alg25519 = NULL;
985     if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) {
986         LOGD("[error] Create openssl alg25519 pubKey failed.");
987         return HCF_ERR_CRYPTO_OPERATION;
988     }
989     if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) {
990         LOGE("Create alg25519 pubKey failed.");
991         OpensslEvpPkeyFree(alg25519);
992         return HCF_ERR_MALLOC;
993     }
994     return HCF_SUCCESS;
995 }
996 
CreateAlg25519PriKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PriKey ** returnPriKey)997 static HcfResult CreateAlg25519PriKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
998     const char *algName, HcfOpensslAlg25519PriKey **returnPriKey)
999 {
1000     EVP_PKEY *alg25519 = NULL;
1001     if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) {
1002         LOGD("[error] Create openssl alg25519 priKey failed.");
1003         return HCF_ERR_CRYPTO_OPERATION;
1004     }
1005     if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) {
1006         LOGE("Create alg25519 priKey failed.");
1007         OpensslEvpPkeyFree(alg25519);
1008         return HCF_ERR_MALLOC;
1009     }
1010     return HCF_SUCCESS;
1011 }
1012 
CreateAlg25519KeyPairByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfKeyPair ** returnKeyPair)1013 static HcfResult CreateAlg25519KeyPairByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
1014     const char *algName, HcfKeyPair **returnKeyPair)
1015 {
1016     HcfOpensslAlg25519PubKey *pubKey = NULL;
1017     HcfResult ret = CreateAlg25519PubKeyByKeyPairSpec(paramsSpec, algName, &pubKey);
1018     if (ret != HCF_SUCCESS) {
1019         LOGE("Create alg25519 pubKey failed.");
1020         return ret;
1021     }
1022 
1023     HcfOpensslAlg25519PriKey *priKey = NULL;
1024     ret = CreateAlg25519PriKeyByKeyPairSpec(paramsSpec, algName, &priKey);
1025     if (ret != HCF_SUCCESS) {
1026         LOGE("Create alg25519 priKey failed.");
1027         HcfObjDestroy(pubKey);
1028         pubKey = NULL;
1029         return ret;
1030     }
1031     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
1032     if (ret != HCF_SUCCESS) {
1033         LOGE("Create alg25519 keyPair failed.");
1034         HcfObjDestroy(pubKey);
1035         pubKey = NULL;
1036         HcfObjDestroy(priKey);
1037         priKey = NULL;
1038         return ret;
1039     }
1040     return HCF_SUCCESS;
1041 }
1042 
CreateAlg25519PubKeyByPubKeySpec(const HcfAlg25519PubKeyParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PubKey ** returnPubKey)1043 static HcfResult CreateAlg25519PubKeyByPubKeySpec(const HcfAlg25519PubKeyParamsSpec *paramsSpec,
1044     const char *algName, HcfOpensslAlg25519PubKey **returnPubKey)
1045 {
1046     EVP_PKEY *alg25519 = NULL;
1047     if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) {
1048         LOGD("[error] Create openssl alg25519 pubKey failed.");
1049         return HCF_ERR_CRYPTO_OPERATION;
1050     }
1051     if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) {
1052         LOGE("Create alg25519 pubKey failed.");
1053         OpensslEvpPkeyFree(alg25519);
1054         return HCF_ERR_MALLOC;
1055     }
1056     return HCF_SUCCESS;
1057 }
1058 
CreateAlg25519PriKeyByPriKeySpec(const HcfAlg25519PriKeyParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PriKey ** returnPriKey)1059 static HcfResult CreateAlg25519PriKeyByPriKeySpec(const HcfAlg25519PriKeyParamsSpec *paramsSpec,
1060     const char *algName, HcfOpensslAlg25519PriKey **returnPriKey)
1061 {
1062     EVP_PKEY *alg25519 = NULL;
1063     if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) {
1064         LOGD("[error] Create openssl alg25519 priKey failed.");
1065         return HCF_ERR_CRYPTO_OPERATION;
1066     }
1067     if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) {
1068         LOGE("Create alg25519 priKey failed.");
1069         OpensslEvpPkeyFree(alg25519);
1070         return HCF_ERR_MALLOC;
1071     }
1072     return HCF_SUCCESS;
1073 }
1074 
EngineGenerateAlg25519PubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)1075 static HcfResult EngineGenerateAlg25519PubKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1076     const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
1077 {
1078     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnPubKey == NULL)) {
1079         LOGE("Invalid input parameter.");
1080         return HCF_INVALID_PARAMS;
1081     }
1082 
1083     int type = 0;
1084     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
1085         LOGE("Invalid class of self.");
1086         return HCF_INVALID_PARAMS;
1087     }
1088 
1089     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
1090         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
1091         (paramsSpec->specType != HCF_PUBLIC_KEY_SPEC)) {
1092         LOGE("Invalid params spec.");
1093         return HCF_INVALID_PARAMS;
1094     }
1095     HcfOpensslAlg25519PubKey *alg25519Pk = NULL;
1096     HcfResult ret = CreateAlg25519PubKeyByPubKeySpec((const HcfAlg25519PubKeyParamsSpec *)paramsSpec,
1097         paramsSpec->algName, &alg25519Pk);
1098     if (ret != HCF_SUCCESS) {
1099         LOGD("[error] Create alg25519 public key by spec failed.");
1100         return ret;
1101     }
1102 
1103     alg25519Pk->type = type;
1104     *returnPubKey = (HcfPubKey *)alg25519Pk;
1105 
1106     return ret;
1107 }
1108 
EngineGenerateAlg25519PriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)1109 static HcfResult EngineGenerateAlg25519PriKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1110     const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
1111 {
1112     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnPriKey == NULL)) {
1113         LOGE("Invalid input parameter.");
1114         return HCF_INVALID_PARAMS;
1115     }
1116 
1117     int type = 0;
1118     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
1119         LOGE("Invalid class of self.");
1120         return HCF_INVALID_PARAMS;
1121     }
1122 
1123     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
1124         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
1125         (paramsSpec->specType != HCF_PRIVATE_KEY_SPEC)) {
1126         LOGE("Invalid params spec.");
1127         return HCF_INVALID_PARAMS;
1128     }
1129     HcfOpensslAlg25519PriKey *alg25519Sk = NULL;
1130     HcfResult ret = CreateAlg25519PriKeyByPriKeySpec((const HcfAlg25519PriKeyParamsSpec *)paramsSpec,
1131         paramsSpec->algName, &alg25519Sk);
1132     if (ret != HCF_SUCCESS) {
1133         LOGD("[error] Create alg25519 private key by spec failed.");
1134         return ret;
1135     }
1136 
1137     alg25519Sk->type = type;
1138     *returnPriKey = (HcfPriKey *)alg25519Sk;
1139 
1140     return ret;
1141 }
1142 
EngineGenerateAlg25519KeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)1143 static HcfResult EngineGenerateAlg25519KeyPairBySpec(const HcfAsyKeyGeneratorSpi *self,
1144     const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
1145 {
1146     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnKeyPair == NULL)) {
1147         LOGE("Invalid input parameter.");
1148         return HCF_INVALID_PARAMS;
1149     }
1150 
1151     int type = 0;
1152     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
1153         LOGE("Invalid class of self.");
1154         return HCF_INVALID_PARAMS;
1155     }
1156 
1157     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
1158         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
1159         (paramsSpec->specType != HCF_KEY_PAIR_SPEC)) {
1160         LOGE("Invalid params spec.");
1161         return HCF_INVALID_PARAMS;
1162     }
1163     HcfResult ret = CreateAlg25519KeyPairByKeyPairSpec((const HcfAlg25519KeyPairParamsSpec *)paramsSpec,
1164         paramsSpec->algName, returnKeyPair);
1165     if (ret != HCF_SUCCESS) {
1166         LOGD("[error] Create alg25519 key pair by spec failed.");
1167         return ret;
1168     }
1169 
1170     HcfOpensslAlg25519KeyPair *keyPair = (HcfOpensslAlg25519KeyPair *)(*returnKeyPair);
1171     HcfOpensslAlg25519PubKey *pubKey = (HcfOpensslAlg25519PubKey *)(keyPair->base.pubKey);
1172     HcfOpensslAlg25519PriKey *priKey = (HcfOpensslAlg25519PriKey *)(keyPair->base.priKey);
1173     pubKey->type = type;
1174     priKey->type = type;
1175     return ret;
1176 }
1177 
HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnObj)1178 HcfResult HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj)
1179 {
1180     (void)params;
1181     if (params == NULL || returnObj == NULL) {
1182         LOGE("Invalid input parameter.");
1183         return HCF_INVALID_PARAMS;
1184     }
1185     HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc(
1186         sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0);
1187     if (impl == NULL) {
1188         LOGE("Failed to allocate generator impl memroy.");
1189         return HCF_ERR_MALLOC;
1190     }
1191     impl->base.base.getClass = GetEd25519KeyGeneratorSpiClass;
1192     impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl;
1193     impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair;
1194     impl->base.engineConvertKey = EngineConvertAlg25519Key;
1195     impl->base.engineConvertPemKey = EngineConvertEd25519PemKey;
1196     impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec;
1197     impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec;
1198     impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec;
1199 
1200     *returnObj = (HcfAsyKeyGeneratorSpi *)impl;
1201     return HCF_SUCCESS;
1202 }
1203 
HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnObj)1204 HcfResult HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj)
1205 {
1206     (void)params;
1207     if (params == NULL || returnObj == NULL) {
1208         LOGE("Invalid input parameter.");
1209         return HCF_INVALID_PARAMS;
1210     }
1211     HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc(
1212         sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0);
1213     if (impl == NULL) {
1214         LOGE("Failed to allocate generator impl memroy.");
1215         return HCF_ERR_MALLOC;
1216     }
1217     impl->base.base.getClass = GetX25519KeyGeneratorSpiClass;
1218     impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl;
1219     impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair;
1220     impl->base.engineConvertKey = EngineConvertAlg25519Key;
1221     impl->base.engineConvertPemKey = EngineConvertX25519PemKey;
1222     impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec;
1223     impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec;
1224     impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec;
1225 
1226     *returnObj = (HcfAsyKeyGeneratorSpi *)impl;
1227     return HCF_SUCCESS;
1228 }
1229 
1230