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(HcfKey * self,const char * format,char ** returnString)237 static HcfResult GetAlg25519PriKeyEncodedPem(HcfKey *self, const char *format, char **returnString)
238 {
239     (void)self;
240     (void)format;
241     (void)returnString;
242     return HCF_INVALID_PARAMS;
243 }
244 
GetAlg25519PubKeyFormat(HcfKey * self)245 static const char *GetAlg25519PubKeyFormat(HcfKey *self)
246 {
247     if (self == NULL) {
248         LOGE("Invalid input parameter.");
249         return NULL;
250     }
251     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
252         LOGE("Invalid class of self.");
253         return NULL;
254     }
255     return OPENSSL_ALG_25519_PUBKEY_FORMAT;
256 }
257 
GetAlg25519PriKeyFormat(HcfKey * self)258 static const char *GetAlg25519PriKeyFormat(HcfKey *self)
259 {
260     if (self == NULL) {
261         LOGE("Invalid input parameter.");
262         return NULL;
263     }
264     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
265         LOGE("Invalid class of self.");
266         return NULL;
267     }
268     return OPENSSL_ALG_25519_PRIKEY_FORMAT;
269 }
270 
GetAlg25519PubKey(EVP_PKEY * pubKey,HcfBigInteger * returnBigInteger)271 static HcfResult GetAlg25519PubKey(EVP_PKEY *pubKey, HcfBigInteger *returnBigInteger)
272 {
273     size_t len = 0;
274     if (!OpensslEvpPkeyGetRawPublicKey(pubKey, NULL, &len)) {
275         LOGD("[error] Get len failed.");
276         return HCF_ERR_CRYPTO_OPERATION;
277     }
278     returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0);
279     if (returnBigInteger->data == NULL) {
280         LOGE("Failed to allocate returnBigInteger memory.");
281         return HCF_ERR_MALLOC;
282     }
283     if (!OpensslEvpPkeyGetRawPublicKey(pubKey, returnBigInteger->data, &len)) {
284         LOGD("[error] Get data failed.");
285         HcfFree(returnBigInteger->data);
286         returnBigInteger->data = NULL;
287         return HCF_ERR_CRYPTO_OPERATION;
288     }
289     returnBigInteger->len = len;
290     return HCF_SUCCESS;
291 }
292 
CheckEvpKeyTypeFromAlg25519PubKey(EVP_PKEY * alg25519Pk,const AsyKeySpecItem item)293 static HcfResult CheckEvpKeyTypeFromAlg25519PubKey(EVP_PKEY *alg25519Pk, const AsyKeySpecItem item)
294 {
295     int type = OpensslEvpPkeyBaseId(alg25519Pk);
296     if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) {
297         LOGE("Invalid pkey type.");
298         return HCF_INVALID_PARAMS;
299     }
300     if ((type == EVP_PKEY_ED25519 && item != ED25519_PK_BN) ||
301         (type == EVP_PKEY_X25519 && item != X25519_PK_BN)) {
302         LOGE("Invalid AsyKeySpecItem.");
303         return HCF_INVALID_PARAMS;
304     }
305     return HCF_SUCCESS;
306 }
307 
CheckEvpKeyTypeFromAlg25519PriKey(EVP_PKEY * alg25519Sk,const AsyKeySpecItem item)308 static HcfResult CheckEvpKeyTypeFromAlg25519PriKey(EVP_PKEY *alg25519Sk, const AsyKeySpecItem item)
309 {
310     int type = OpensslEvpPkeyBaseId(alg25519Sk);
311     if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) {
312         LOGE("Invalid pkey type.");
313         return HCF_INVALID_PARAMS;
314     }
315     if ((type == EVP_PKEY_ED25519 && item != ED25519_SK_BN) ||
316         (type == EVP_PKEY_X25519 && item != X25519_SK_BN)) {
317         LOGE("Invalid AsyKeySpecItem.");
318         return HCF_INVALID_PARAMS;
319     }
320     return HCF_SUCCESS;
321 }
322 
GetBigIntegerSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)323 static HcfResult GetBigIntegerSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item,
324     HcfBigInteger *returnBigInteger)
325 {
326     if (self == NULL || returnBigInteger == NULL) {
327         LOGE("Invalid input parameter.");
328         return HCF_INVALID_PARAMS;
329     }
330     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
331         LOGE("Invalid class of self.");
332         return HCF_INVALID_PARAMS;
333     }
334     HcfResult ret = HCF_INVALID_PARAMS;
335     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
336     EVP_PKEY *alg25519Pk = impl->pkey;
337     if (alg25519Pk == NULL) {
338         LOGE("pKey is null.");
339         return HCF_INVALID_PARAMS;
340     }
341     if (CheckEvpKeyTypeFromAlg25519PubKey(alg25519Pk, item) != HCF_SUCCESS) {
342         LOGE("Check pKey type failed.");
343         return HCF_INVALID_PARAMS;
344     }
345     if (item == ED25519_PK_BN || item == X25519_PK_BN) {
346         ret = GetAlg25519PubKey(alg25519Pk, returnBigInteger);
347     } else {
348         LOGE("Input item is invalid");
349     }
350     return ret;
351 }
352 
GetAlg25519PriKey(EVP_PKEY * priKey,HcfBigInteger * returnBigInteger)353 static HcfResult GetAlg25519PriKey(EVP_PKEY *priKey, HcfBigInteger *returnBigInteger)
354 {
355     size_t len = 0;
356     if (!OpensslEvpPkeyGetRawPrivateKey(priKey, NULL, &len)) {
357         LOGD("[error] Get private key length failed.");
358         return HCF_ERR_CRYPTO_OPERATION;
359     }
360     returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0);
361     if (returnBigInteger->data == NULL) {
362         LOGE("Failed to allocate returnBigInteger memory.");
363         return HCF_ERR_MALLOC;
364     }
365     if (!OpensslEvpPkeyGetRawPrivateKey(priKey, returnBigInteger->data, &len)) {
366         LOGD("[error] Get data failed.");
367         HcfFree(returnBigInteger->data);
368         returnBigInteger->data = NULL;
369         return HCF_ERR_CRYPTO_OPERATION;
370     }
371     returnBigInteger->len = len;
372     return HCF_SUCCESS;
373 }
374 
GetBigIntegerSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)375 static HcfResult GetBigIntegerSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item,
376     HcfBigInteger *returnBigInteger)
377 {
378     if (self == NULL || returnBigInteger == NULL) {
379         LOGE("Invalid input parameter.");
380         return HCF_INVALID_PARAMS;
381     }
382     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
383         LOGE("Invalid class of self.");
384         return HCF_INVALID_PARAMS;
385     }
386     HcfResult ret = HCF_INVALID_PARAMS;
387     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
388     EVP_PKEY *alg25519Sk = impl->pkey;
389     if (alg25519Sk == NULL) {
390         LOGE("pKey is null.");
391         return HCF_INVALID_PARAMS;
392     }
393     if (CheckEvpKeyTypeFromAlg25519PriKey(alg25519Sk, item) != HCF_SUCCESS) {
394         LOGE("Check pKey type failed.");
395         return HCF_INVALID_PARAMS;
396     }
397     if (item == ED25519_SK_BN || item == X25519_SK_BN) {
398         ret = GetAlg25519PriKey(alg25519Sk, returnBigInteger);
399     } else {
400         LOGE("Input item is invalid");
401     }
402     return ret;
403 }
404 
GetIntSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)405 static HcfResult GetIntSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
406 {
407     (void)self;
408     (void)returnInt;
409     return HCF_NOT_SUPPORT;
410 }
411 
GetIntSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)412 static HcfResult GetIntSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
413 {
414     (void)self;
415     (void)returnInt;
416     return HCF_NOT_SUPPORT;
417 }
418 
GetStrSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)419 static HcfResult GetStrSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
420 {
421     (void)self;
422     (void)returnString;
423     return HCF_NOT_SUPPORT;
424 }
425 
GetStrSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)426 static HcfResult GetStrSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
427 {
428     (void)self;
429     (void)returnString;
430     return HCF_NOT_SUPPORT;
431 }
432 
GetAlg25519PriKeyEncodedDer(const HcfPriKey * self,const char * format,HcfBlob * returnBlob)433 static HcfResult GetAlg25519PriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob)
434 {
435     (void)self;
436     (void)format;
437     (void)returnBlob;
438     return HCF_INVALID_PARAMS;
439 }
440 
ClearAlg25519PriKeyMem(HcfPriKey * self)441 static void ClearAlg25519PriKeyMem(HcfPriKey *self)
442 {
443     if (self == NULL) {
444         LOGE("Invalid params.");
445         return;
446     }
447     if (!HcfIsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
448         LOGE("Invalid class of self.");
449         return;
450     }
451     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
452     OpensslEvpPkeyFree(impl->pkey);
453     impl->pkey = NULL;
454 }
455 
GenerateAlg25519EvpKey(int type,EVP_PKEY ** ppkey)456 static HcfResult GenerateAlg25519EvpKey(int type, EVP_PKEY **ppkey)
457 {
458     EVP_PKEY_CTX *paramsCtx = NULL;
459     HcfResult ret = HCF_SUCCESS;
460     do {
461         paramsCtx = OpensslEvpPkeyCtxNewId(type, NULL);
462         if (paramsCtx == NULL) {
463             LOGE("Create params ctx failed.");
464             ret = HCF_ERR_MALLOC;
465             break;
466         }
467         if (OpensslEvpPkeyKeyGenInit(paramsCtx) != HCF_OPENSSL_SUCCESS) {
468             LOGD("[error] Key ctx generate init failed.");
469             ret = HCF_ERR_CRYPTO_OPERATION;
470             break;
471         }
472         if (OpensslEvpPkeyKeyGen(paramsCtx, ppkey) != HCF_OPENSSL_SUCCESS) {
473             LOGD("[error] Generate pkey failed.");
474             ret = HCF_ERR_CRYPTO_OPERATION;
475             break;
476         }
477     } while (0);
478     if (paramsCtx != NULL) {
479         OpensslEvpPkeyCtxFree(paramsCtx);
480     }
481     return ret;
482 }
483 
GetAlg25519PubKeyEncodedDer(const HcfPubKey * self,const char * format,HcfBlob * returnBlob)484 static HcfResult GetAlg25519PubKeyEncodedDer(const HcfPubKey *self, const char *format, HcfBlob *returnBlob)
485 {
486     (void)self;
487     (void)format;
488     (void)returnBlob;
489     return HCF_INVALID_PARAMS;
490 }
491 
FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey * pk)492 static void FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey *pk)
493 {
494     pk->base.base.base.destroy = DestroyAlg25519PubKey;
495     pk->base.base.base.getClass = GetAlg25519PubKeyClass;
496     pk->base.base.getAlgorithm = GetAlg25519PubKeyAlgorithm;
497     pk->base.base.getEncoded = GetAlg25519PubKeyEncoded;
498     pk->base.base.getEncodedPem = GetAlg25519PubKeyEncodedPem;
499     pk->base.base.getFormat = GetAlg25519PubKeyFormat;
500     pk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PubKey;
501     pk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PubKey;
502     pk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PubKey;
503     pk->base.getEncodedDer = GetAlg25519PubKeyEncodedDer;
504 }
505 
FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey * sk)506 static void FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey *sk)
507 {
508     sk->base.base.base.destroy = DestroyAlg25519PriKey;
509     sk->base.base.base.getClass = GetAlg25519PriKeyClass;
510     sk->base.base.getAlgorithm = GetAlg25519PriKeyAlgorithm;
511     sk->base.base.getEncoded = GetAlg25519PriKeyEncoded;
512     sk->base.base.getEncodedPem = GetAlg25519PriKeyEncodedPem;
513     sk->base.base.getFormat = GetAlg25519PriKeyFormat;
514     sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PriKey;
515     sk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PriKey;
516     sk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PriKey;
517     sk->base.getEncodedDer = GetAlg25519PriKeyEncodedDer;
518     sk->base.clearMem = ClearAlg25519PriKeyMem;
519 }
520 
CreateAlg25519PubKey(EVP_PKEY * pkey,HcfOpensslAlg25519PubKey ** returnPubKey)521 static HcfResult CreateAlg25519PubKey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey)
522 {
523     HcfOpensslAlg25519PubKey *alg25519PubKey =
524         (HcfOpensslAlg25519PubKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PubKey), 0);
525     if (alg25519PubKey == NULL) {
526         LOGE("Failed to allocate alg25519 public key memory.");
527         return HCF_ERR_MALLOC;
528     }
529     FillOpensslAlg25519PubKeyFunc(alg25519PubKey);
530     alg25519PubKey->pkey = pkey;
531     *returnPubKey = alg25519PubKey;
532     return HCF_SUCCESS;
533 }
534 
CreateAlg25519PriKey(EVP_PKEY * pkey,HcfOpensslAlg25519PriKey ** returnPriKey)535 static HcfResult CreateAlg25519PriKey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey)
536 {
537     HcfOpensslAlg25519PriKey *alg25519PriKey =
538         (HcfOpensslAlg25519PriKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PriKey), 0);
539     if (alg25519PriKey == NULL) {
540         LOGE("Failed to allocate alg25519 private key memory.");
541         return HCF_ERR_MALLOC;
542     }
543     FillOpensslAlg25519PriKeyFunc(alg25519PriKey);
544     alg25519PriKey->pkey = pkey;
545     *returnPriKey = alg25519PriKey;
546     return HCF_SUCCESS;
547 }
548 
CreateAlg25519KeyPair(const HcfOpensslAlg25519PubKey * pubKey,const HcfOpensslAlg25519PriKey * priKey,HcfKeyPair ** returnKeyPair)549 static HcfResult CreateAlg25519KeyPair(const HcfOpensslAlg25519PubKey *pubKey,
550     const HcfOpensslAlg25519PriKey *priKey, HcfKeyPair **returnKeyPair)
551 {
552     HcfOpensslAlg25519KeyPair *keyPair =
553         (HcfOpensslAlg25519KeyPair *)HcfMalloc(sizeof(HcfOpensslAlg25519KeyPair), 0);
554     if (keyPair == NULL) {
555         LOGE("Failed to allocate keyPair memory.");
556         return HCF_ERR_MALLOC;
557     }
558     keyPair->base.base.getClass = GetAlg25519KeyPairClass;
559     keyPair->base.base.destroy = DestroyAlg25519KeyPair;
560     keyPair->base.pubKey = (HcfPubKey *)pubKey;
561     keyPair->base.priKey = (HcfPriKey *)priKey;
562 
563     *returnKeyPair = (HcfKeyPair *)keyPair;
564     return HCF_SUCCESS;
565 }
566 
GeneratePubKeyByPkey(EVP_PKEY * pkey,HcfOpensslAlg25519PubKey ** returnPubKey)567 static HcfResult GeneratePubKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey)
568 {
569     EVP_PKEY *evpPkey = OpensslEvpPkeyDup(pkey);
570     if (evpPkey == NULL) {
571         LOGD("[error] pkey dup failed");
572         HcfPrintOpensslError();
573         return HCF_ERR_CRYPTO_OPERATION;
574     }
575     HcfResult ret = CreateAlg25519PubKey(evpPkey, returnPubKey);
576     if (ret != HCF_SUCCESS) {
577         LOGD("[error] Create alg25519 public key failed");
578         OpensslEvpPkeyFree(evpPkey);
579     }
580     return ret;
581 }
582 
GeneratePriKeyByPkey(EVP_PKEY * pkey,HcfOpensslAlg25519PriKey ** returnPriKey)583 static HcfResult GeneratePriKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey)
584 {
585     EVP_PKEY *evpPkey = OpensslEvpPkeyDup(pkey);
586     if (evpPkey == NULL) {
587         LOGD("[error] pkey dup failed");
588         HcfPrintOpensslError();
589         return HCF_ERR_CRYPTO_OPERATION;
590     }
591     HcfResult ret = CreateAlg25519PriKey(evpPkey, returnPriKey);
592     if (ret != HCF_SUCCESS) {
593         LOGD("[error] Create alg25519 private key failed");
594         OpensslEvpPkeyFree(evpPkey);
595     }
596     return ret;
597 }
598 
GenerateAlg25519PubAndPriKey(int type,HcfOpensslAlg25519PubKey ** returnPubKey,HcfOpensslAlg25519PriKey ** returnPriKey)599 static HcfResult GenerateAlg25519PubAndPriKey(int type, HcfOpensslAlg25519PubKey **returnPubKey,
600     HcfOpensslAlg25519PriKey **returnPriKey)
601 {
602     EVP_PKEY *pkey = NULL;
603     HcfResult ret = GenerateAlg25519EvpKey(type, &pkey);
604     if (ret != HCF_SUCCESS) {
605         LOGD("[error] Generate alg25519 EVP_PKEY failed.");
606         return ret;
607     }
608 
609     ret = GeneratePubKeyByPkey(pkey, returnPubKey);
610     if (ret != HCF_SUCCESS) {
611         LOGD("[error] Generate pubkey fail.");
612         OpensslEvpPkeyFree(pkey);
613         return ret;
614     }
615 
616     ret = GeneratePriKeyByPkey(pkey, returnPriKey);
617     if (ret != HCF_SUCCESS) {
618         LOGD("[error] Generate prikey fail.");
619         HcfObjDestroy(*returnPubKey);
620         *returnPubKey = NULL;
621         OpensslEvpPkeyFree(pkey);
622         return HCF_ERR_CRYPTO_OPERATION;
623     }
624 
625     OpensslEvpPkeyFree(pkey);
626     return ret;
627 }
628 
ConvertAlg25519PubKey(const HcfBlob * pubKeyBlob,HcfOpensslAlg25519PubKey ** returnPubKey)629 static HcfResult ConvertAlg25519PubKey(const HcfBlob *pubKeyBlob, HcfOpensslAlg25519PubKey **returnPubKey)
630 {
631     const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data);
632     EVP_PKEY *pkey = OpensslD2iPubKey(NULL, &tmpData, pubKeyBlob->len);
633     if (pkey == NULL) {
634         LOGD("[error] Call d2i_PUBKEY fail.");
635         HcfPrintOpensslError();
636         return HCF_ERR_CRYPTO_OPERATION;
637     }
638     HcfResult ret = CreateAlg25519PubKey(pkey, returnPubKey);
639     if (ret != HCF_SUCCESS) {
640         LOGD("[error] Create alg25519 public key failed");
641         OpensslEvpPkeyFree(pkey);
642     }
643     return ret;
644 }
645 
ConvertAlg25519PriKey(int type,const HcfBlob * priKeyBlob,HcfOpensslAlg25519PriKey ** returnPriKey)646 static HcfResult ConvertAlg25519PriKey(int type, const HcfBlob *priKeyBlob,
647     HcfOpensslAlg25519PriKey **returnPriKey)
648 {
649     const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data);
650     EVP_PKEY *pkey = OpensslD2iPrivateKey(type, NULL, &tmpData, priKeyBlob->len);
651     if (pkey == NULL) {
652         LOGD("[error] Call d2i_PrivateKey fail.");
653         HcfPrintOpensslError();
654         return HCF_ERR_CRYPTO_OPERATION;
655     }
656     HcfResult ret = CreateAlg25519PriKey(pkey, returnPriKey);
657     if (ret != HCF_SUCCESS) {
658         LOGD("[error] Create alg25519 private key failed");
659         OpensslEvpPkeyFree(pkey);
660     }
661     return ret;
662 }
663 
ConvertAlg25519PubAndPriKey(int type,const HcfBlob * pubKeyBlob,const HcfBlob * priKeyBlob,HcfOpensslAlg25519PubKey ** returnPubKey,HcfOpensslAlg25519PriKey ** returnPriKey)664 static HcfResult ConvertAlg25519PubAndPriKey(int type, const HcfBlob *pubKeyBlob, const HcfBlob *priKeyBlob,
665     HcfOpensslAlg25519PubKey **returnPubKey, HcfOpensslAlg25519PriKey **returnPriKey)
666 {
667     if (pubKeyBlob != NULL) {
668         if (ConvertAlg25519PubKey(pubKeyBlob, returnPubKey) != HCF_SUCCESS) {
669             LOGD("[error] Convert alg25519 public key failed.");
670             return HCF_ERR_CRYPTO_OPERATION;
671         }
672     }
673     if (priKeyBlob != NULL) {
674         if (ConvertAlg25519PriKey(type, priKeyBlob, returnPriKey) != HCF_SUCCESS) {
675             LOGD("[error] Convert alg25519 private key failed.");
676             HcfObjDestroy(*returnPubKey);
677             *returnPubKey = NULL;
678             return HCF_ERR_CRYPTO_OPERATION;
679         }
680     }
681     return HCF_SUCCESS;
682 }
683 
CheckClassMatch(HcfAsyKeyGeneratorSpi * self,int * type)684 static HcfResult CheckClassMatch(HcfAsyKeyGeneratorSpi *self, int *type)
685 {
686     if (HcfIsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass())) {
687         *type = EVP_PKEY_ED25519;
688     } else if (HcfIsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) {
689         *type = EVP_PKEY_X25519;
690     } else {
691         LOGE("Invalid class of self.");
692         return HCF_INVALID_PARAMS;
693     }
694     return HCF_SUCCESS;
695 }
696 
EngineGenerateAlg25519KeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnKeyPair)697 static HcfResult EngineGenerateAlg25519KeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnKeyPair)
698 {
699     if (self == NULL || returnKeyPair == NULL) {
700         LOGE("Invalid params.");
701         return HCF_INVALID_PARAMS;
702     }
703     int type = 0;
704     if (CheckClassMatch(self, &type) != HCF_SUCCESS) {
705         LOGE("Invalid class of self.");
706         return HCF_INVALID_PARAMS;
707     }
708 
709     HcfOpensslAlg25519PubKey *pubKey = NULL;
710     HcfOpensslAlg25519PriKey *priKey = NULL;
711     HcfResult ret = GenerateAlg25519PubAndPriKey(type, &pubKey, &priKey);
712     if (ret != HCF_SUCCESS) {
713         LOGE("Generate alg25519 pk and sk by openssl failed.");
714         return ret;
715     }
716 
717     if (pubKey != NULL) {
718         pubKey->type = type;
719     }
720 
721     if (priKey != NULL) {
722         priKey->type = type;
723     }
724 
725     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
726     if (ret != HCF_SUCCESS) {
727         LOGE("Create alg25519 keyPair failed.");
728         HcfObjDestroy(pubKey);
729         HcfObjDestroy(priKey);
730         return ret;
731     }
732     return HCF_SUCCESS;
733 }
734 
EngineConvertAlg25519Key(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)735 static HcfResult EngineConvertAlg25519Key(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
736     HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
737 {
738     (void)params;
739     if ((self == NULL) || (returnKeyPair == NULL)) {
740         LOGE("Invalid input parameter.");
741         return HCF_INVALID_PARAMS;
742     }
743     int type = 0;
744     if (CheckClassMatch(self, &type) != HCF_SUCCESS) {
745         LOGE("Invalid class of self.");
746         return HCF_INVALID_PARAMS;
747     }
748     bool pubKeyValid = HcfIsBlobValid(pubKeyBlob);
749     bool priKeyValid = HcfIsBlobValid(priKeyBlob);
750     if ((!pubKeyValid) && (!priKeyValid)) {
751         LOGE("The private key and public key cannot both be NULL.");
752         return HCF_INVALID_PARAMS;
753     }
754 
755     HcfOpensslAlg25519PubKey *pubKey = NULL;
756     HcfOpensslAlg25519PriKey *priKey = NULL;
757     HcfBlob *inputPk = pubKeyValid ? pubKeyBlob : NULL;
758     HcfBlob *inputSk = priKeyValid ? priKeyBlob : NULL;
759     HcfResult ret = ConvertAlg25519PubAndPriKey(type, inputPk, inputSk, &pubKey, &priKey);
760     if (ret != HCF_SUCCESS) {
761         LOGE("Convert alg25519 keyPair failed.");
762         return ret;
763     }
764 
765     if (pubKey != NULL) {
766         pubKey->type = type;
767     }
768 
769     if (priKey != NULL) {
770         priKey->type = type;
771     }
772 
773     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
774     if (ret != HCF_SUCCESS) {
775         LOGE("Create alg25519 keyPair failed.");
776         HcfObjDestroy(pubKey);
777         HcfObjDestroy(priKey);
778     }
779     return ret;
780 }
781 
CreateOpensslAlg25519PubKey(const HcfBigInteger * pk,const char * algName,EVP_PKEY ** returnAlg25519)782 static HcfResult CreateOpensslAlg25519PubKey(const HcfBigInteger *pk, const char *algName,
783     EVP_PKEY **returnAlg25519)
784 {
785     EVP_PKEY *pubkey = NULL;
786     if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) {
787         pubkey = OpensslEvpPkeyNewRawPublicKey(EVP_PKEY_ED25519, NULL, pk->data, pk->len);
788     } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) {
789         pubkey = OpensslEvpPkeyNewRawPublicKey(EVP_PKEY_X25519, NULL, pk->data, pk->len);
790     } else {
791         LOGE("Invalid algName! [Algo]: %s", algName);
792         return HCF_INVALID_PARAMS;
793     }
794     if (pubkey == NULL) {
795         LOGD("[error] Set alg25519 pubKey failed.");
796         HcfPrintOpensslError();
797         return HCF_ERR_CRYPTO_OPERATION;
798     }
799     *returnAlg25519 = pubkey;
800     return HCF_SUCCESS;
801 }
802 
CreateOpensslAlg25519PriKey(const HcfBigInteger * sk,const char * algName,EVP_PKEY ** returnAlg25519)803 static HcfResult CreateOpensslAlg25519PriKey(const HcfBigInteger *sk, const char *algName,
804     EVP_PKEY **returnAlg25519)
805 {
806     EVP_PKEY *privkey = NULL;
807     if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) {
808         privkey = OpensslEvpPkeyNewRawPrivateKey(EVP_PKEY_ED25519, NULL, sk->data, sk->len);
809     } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) {
810         privkey = OpensslEvpPkeyNewRawPrivateKey(EVP_PKEY_X25519, NULL, sk->data, sk->len);
811     } else {
812         LOGE("Invalid algName! [Algo]: %s", algName);
813         return HCF_INVALID_PARAMS;
814     }
815     if (privkey == NULL) {
816         LOGD("[error] Get alg25519 priKey failed.");
817         HcfPrintOpensslError();
818         return HCF_ERR_CRYPTO_OPERATION;
819     }
820     *returnAlg25519 = privkey;
821     return HCF_SUCCESS;
822 }
823 
CreateAlg25519PubKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PubKey ** returnPubKey)824 static HcfResult CreateAlg25519PubKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
825     const char *algName, HcfOpensslAlg25519PubKey **returnPubKey)
826 {
827     EVP_PKEY *alg25519 = NULL;
828     if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) {
829         LOGD("[error] Create openssl alg25519 pubKey failed.");
830         return HCF_ERR_CRYPTO_OPERATION;
831     }
832     if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) {
833         LOGE("Create alg25519 pubKey failed.");
834         OpensslEvpPkeyFree(alg25519);
835         return HCF_ERR_MALLOC;
836     }
837     return HCF_SUCCESS;
838 }
839 
CreateAlg25519PriKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PriKey ** returnPriKey)840 static HcfResult CreateAlg25519PriKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
841     const char *algName, HcfOpensslAlg25519PriKey **returnPriKey)
842 {
843     EVP_PKEY *alg25519 = NULL;
844     if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) {
845         LOGD("[error] Create openssl alg25519 priKey failed.");
846         return HCF_ERR_CRYPTO_OPERATION;
847     }
848     if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) {
849         LOGE("Create alg25519 priKey failed.");
850         OpensslEvpPkeyFree(alg25519);
851         return HCF_ERR_MALLOC;
852     }
853     return HCF_SUCCESS;
854 }
855 
CreateAlg25519KeyPairByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfKeyPair ** returnKeyPair)856 static HcfResult CreateAlg25519KeyPairByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
857     const char *algName, HcfKeyPair **returnKeyPair)
858 {
859     HcfOpensslAlg25519PubKey *pubKey = NULL;
860     HcfResult ret = CreateAlg25519PubKeyByKeyPairSpec(paramsSpec, algName, &pubKey);
861     if (ret != HCF_SUCCESS) {
862         LOGE("Create alg25519 pubKey failed.");
863         return ret;
864     }
865 
866     HcfOpensslAlg25519PriKey *priKey = NULL;
867     ret = CreateAlg25519PriKeyByKeyPairSpec(paramsSpec, algName, &priKey);
868     if (ret != HCF_SUCCESS) {
869         LOGE("Create alg25519 priKey failed.");
870         HcfObjDestroy(pubKey);
871         return ret;
872     }
873     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
874     if (ret != HCF_SUCCESS) {
875         LOGE("Create alg25519 keyPair failed.");
876         HcfObjDestroy(pubKey);
877         HcfObjDestroy(priKey);
878         return ret;
879     }
880     return HCF_SUCCESS;
881 }
882 
CreateAlg25519PubKeyByPubKeySpec(const HcfAlg25519PubKeyParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PubKey ** returnPubKey)883 static HcfResult CreateAlg25519PubKeyByPubKeySpec(const HcfAlg25519PubKeyParamsSpec *paramsSpec,
884     const char *algName, HcfOpensslAlg25519PubKey **returnPubKey)
885 {
886     EVP_PKEY *alg25519 = NULL;
887     if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) {
888         LOGD("[error] Create openssl alg25519 pubKey failed.");
889         return HCF_ERR_CRYPTO_OPERATION;
890     }
891     if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) {
892         LOGE("Create alg25519 pubKey failed.");
893         OpensslEvpPkeyFree(alg25519);
894         return HCF_ERR_MALLOC;
895     }
896     return HCF_SUCCESS;
897 }
898 
CreateAlg25519PriKeyByPriKeySpec(const HcfAlg25519PriKeyParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PriKey ** returnPriKey)899 static HcfResult CreateAlg25519PriKeyByPriKeySpec(const HcfAlg25519PriKeyParamsSpec *paramsSpec,
900     const char *algName, HcfOpensslAlg25519PriKey **returnPriKey)
901 {
902     EVP_PKEY *alg25519 = NULL;
903     if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) {
904         LOGD("[error] Create openssl alg25519 priKey failed.");
905         return HCF_ERR_CRYPTO_OPERATION;
906     }
907     if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) {
908         LOGE("Create alg25519 priKey failed.");
909         OpensslEvpPkeyFree(alg25519);
910         return HCF_ERR_MALLOC;
911     }
912     return HCF_SUCCESS;
913 }
914 
EngineGenerateAlg25519PubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)915 static HcfResult EngineGenerateAlg25519PubKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
916     const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
917 {
918     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnPubKey == NULL)) {
919         LOGE("Invalid input parameter.");
920         return HCF_INVALID_PARAMS;
921     }
922 
923     int type = 0;
924     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
925         LOGE("Invalid class of self.");
926         return HCF_INVALID_PARAMS;
927     }
928 
929     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
930         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
931         (paramsSpec->specType != HCF_PUBLIC_KEY_SPEC)) {
932         LOGE("Invalid params spec.");
933         return HCF_INVALID_PARAMS;
934     }
935     HcfOpensslAlg25519PubKey *alg25519Pk = NULL;
936     HcfResult ret = CreateAlg25519PubKeyByPubKeySpec((const HcfAlg25519PubKeyParamsSpec *)paramsSpec,
937         paramsSpec->algName, &alg25519Pk);
938     if (ret != HCF_SUCCESS) {
939         LOGD("[error] Create alg25519 public key by spec failed.");
940         return ret;
941     }
942 
943     alg25519Pk->type = type;
944     *returnPubKey = (HcfPubKey *)alg25519Pk;
945 
946     return ret;
947 }
948 
EngineGenerateAlg25519PriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)949 static HcfResult EngineGenerateAlg25519PriKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
950     const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
951 {
952     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnPriKey == NULL)) {
953         LOGE("Invalid input parameter.");
954         return HCF_INVALID_PARAMS;
955     }
956 
957     int type = 0;
958     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
959         LOGE("Invalid class of self.");
960         return HCF_INVALID_PARAMS;
961     }
962 
963     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
964         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
965         (paramsSpec->specType != HCF_PRIVATE_KEY_SPEC)) {
966         LOGE("Invalid params spec.");
967         return HCF_INVALID_PARAMS;
968     }
969     HcfOpensslAlg25519PriKey *alg25519Sk = NULL;
970     HcfResult ret = CreateAlg25519PriKeyByPriKeySpec((const HcfAlg25519PriKeyParamsSpec *)paramsSpec,
971         paramsSpec->algName, &alg25519Sk);
972     if (ret != HCF_SUCCESS) {
973         LOGD("[error] Create alg25519 private key by spec failed.");
974         return ret;
975     }
976 
977     alg25519Sk->type = type;
978     *returnPriKey = (HcfPriKey *)alg25519Sk;
979 
980     return ret;
981 }
982 
EngineGenerateAlg25519KeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)983 static HcfResult EngineGenerateAlg25519KeyPairBySpec(const HcfAsyKeyGeneratorSpi *self,
984     const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
985 {
986     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnKeyPair == NULL)) {
987         LOGE("Invalid input parameter.");
988         return HCF_INVALID_PARAMS;
989     }
990 
991     int type = 0;
992     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
993         LOGE("Invalid class of self.");
994         return HCF_INVALID_PARAMS;
995     }
996 
997     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
998         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
999         (paramsSpec->specType != HCF_KEY_PAIR_SPEC)) {
1000         LOGE("Invalid params spec.");
1001         return HCF_INVALID_PARAMS;
1002     }
1003     HcfResult ret = CreateAlg25519KeyPairByKeyPairSpec((const HcfAlg25519KeyPairParamsSpec *)paramsSpec,
1004         paramsSpec->algName, returnKeyPair);
1005     if (ret != HCF_SUCCESS) {
1006         LOGD("[error] Create alg25519 key pair by spec failed.");
1007         return ret;
1008     }
1009 
1010     HcfOpensslAlg25519KeyPair *keyPair = (HcfOpensslAlg25519KeyPair *)(*returnKeyPair);
1011     HcfOpensslAlg25519PubKey *pubKey = (HcfOpensslAlg25519PubKey *)(keyPair->base.pubKey);
1012     HcfOpensslAlg25519PriKey *priKey = (HcfOpensslAlg25519PriKey *)(keyPair->base.priKey);
1013     pubKey->type = type;
1014     priKey->type = type;
1015     return ret;
1016 }
1017 
HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnObj)1018 HcfResult HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj)
1019 {
1020     (void)params;
1021     if (params == NULL || returnObj == NULL) {
1022         LOGE("Invalid input parameter.");
1023         return HCF_INVALID_PARAMS;
1024     }
1025     HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc(
1026         sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0);
1027     if (impl == NULL) {
1028         LOGE("Failed to allocate generator impl memroy.");
1029         return HCF_ERR_MALLOC;
1030     }
1031     impl->base.base.getClass = GetEd25519KeyGeneratorSpiClass;
1032     impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl;
1033     impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair;
1034     impl->base.engineConvertKey = EngineConvertAlg25519Key;
1035     impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec;
1036     impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec;
1037     impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec;
1038 
1039     *returnObj = (HcfAsyKeyGeneratorSpi *)impl;
1040     return HCF_SUCCESS;
1041 }
1042 
HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnObj)1043 HcfResult HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnObj)
1044 {
1045     (void)params;
1046     if (params == NULL || returnObj == NULL) {
1047         LOGE("Invalid input parameter.");
1048         return HCF_INVALID_PARAMS;
1049     }
1050     HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc(
1051         sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0);
1052     if (impl == NULL) {
1053         LOGE("Failed to allocate generator impl memroy.");
1054         return HCF_ERR_MALLOC;
1055     }
1056     impl->base.base.getClass = GetX25519KeyGeneratorSpiClass;
1057     impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl;
1058     impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair;
1059     impl->base.engineConvertKey = EngineConvertAlg25519Key;
1060     impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec;
1061     impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec;
1062     impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec;
1063 
1064     *returnObj = (HcfAsyKeyGeneratorSpi *)impl;
1065     return HCF_SUCCESS;
1066 }
1067 
1068