• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "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) {
71         LOGE("Invalid input parameter.");
72         return;
73     }
74     if (!IsClassMatch(self, GetEd25519KeyGeneratorSpiClass()) &&
75         !IsClassMatch(self, GetX25519KeyGeneratorSpiClass())) {
76         LOGE("Invalid class of self.");
77         return;
78     }
79     HcfFree(self);
80 }
81 
DestroyAlg25519PubKey(HcfObjectBase * self)82 static void DestroyAlg25519PubKey(HcfObjectBase *self)
83 {
84     if (self == NULL) {
85         LOGE("Invalid input parameter.");
86         return;
87     }
88     if (!IsClassMatch(self, GetAlg25519PubKeyClass())) {
89         LOGE("Invalid class of self.");
90         return;
91     }
92     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
93     Openssl_EVP_PKEY_free(impl->pkey);
94     impl->pkey = NULL;
95     HcfFree(impl);
96 }
97 
DestroyAlg25519PriKey(HcfObjectBase * self)98 static void DestroyAlg25519PriKey(HcfObjectBase *self)
99 {
100     if (self == NULL) {
101         LOGE("Invalid input parameter.");
102         return;
103     }
104     if (!IsClassMatch(self, GetAlg25519PriKeyClass())) {
105         LOGE("Invalid class of self.");
106         return;
107     }
108     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
109     Openssl_EVP_PKEY_free(impl->pkey);
110     impl->pkey = NULL;
111     HcfFree(impl);
112 }
113 
DestroyAlg25519KeyPair(HcfObjectBase * self)114 static void DestroyAlg25519KeyPair(HcfObjectBase *self)
115 {
116     if (self == NULL) {
117         LOGE("Invalid input parameter.");
118         return;
119     }
120     if (!IsClassMatch(self, GetAlg25519KeyPairClass())) {
121         LOGE("Invalid class of self.");
122         return;
123     }
124     HcfOpensslAlg25519KeyPair *impl = (HcfOpensslAlg25519KeyPair *)self;
125     DestroyAlg25519PubKey((HcfObjectBase *)impl->base.pubKey);
126     impl->base.pubKey = NULL;
127     DestroyAlg25519PriKey((HcfObjectBase *)impl->base.priKey);
128     impl->base.priKey = NULL;
129     HcfFree(self);
130 }
131 
GetAlg25519PubKeyAlgorithm(HcfKey * self)132 static const char *GetAlg25519PubKeyAlgorithm(HcfKey *self)
133 {
134     if (self == NULL) {
135         LOGE("Invalid input parameter.");
136         return NULL;
137     }
138     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
139         LOGE("Invalid class of self.");
140         return NULL;
141     }
142 
143     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
144     if (impl->type == EVP_PKEY_ED25519) {
145         return ALGORITHM_NAME_ED25519;
146     }
147 
148     return ALGORITHM_NAME_X25519;
149 }
150 
GetAlg25519PriKeyAlgorithm(HcfKey * self)151 static const char *GetAlg25519PriKeyAlgorithm(HcfKey *self)
152 {
153     if (self == NULL) {
154         LOGE("Invalid input parameter.");
155         return NULL;
156     }
157     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
158         LOGE("Invalid class of self.");
159         return NULL;
160     }
161 
162     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
163     if (impl->type == EVP_PKEY_ED25519) {
164         return ALGORITHM_NAME_ED25519;
165     }
166 
167     return ALGORITHM_NAME_X25519;
168 }
169 
GetAlg25519PubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)170 static HcfResult GetAlg25519PubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
171 {
172     if ((self == NULL) || (returnBlob == NULL)) {
173         LOGE("Invalid input parameter.");
174         return HCF_INVALID_PARAMS;
175     }
176     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
177         LOGE("Invalid class of self.");
178         return HCF_INVALID_PARAMS;
179     }
180     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
181     if (impl->pkey == NULL) {
182         LOGE("pkey is NULL.");
183         return HCF_INVALID_PARAMS;
184     }
185     unsigned char *returnData = NULL;
186     int len = Openssl_i2d_PUBKEY(impl->pkey, &returnData);
187     if (len <= 0) {
188         LOGD("[error] Call i2d_PUBKEY failed");
189         HcfPrintOpensslError();
190         return HCF_ERR_CRYPTO_OPERATION;
191     }
192     returnBlob->data = returnData;
193     returnBlob->len = len;
194     return HCF_SUCCESS;
195 }
196 
GetAlg25519PriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)197 static HcfResult GetAlg25519PriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
198 {
199     if ((self == NULL) || (returnBlob == NULL)) {
200         LOGE("Invalid input parameter.");
201         return HCF_INVALID_PARAMS;
202     }
203     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
204         LOGE("Invalid class of self.");
205         return HCF_INVALID_PARAMS;
206     }
207     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
208     if (impl->pkey == NULL) {
209         LOGE("pkey is NULL.");
210         return HCF_INVALID_PARAMS;
211     }
212     unsigned char *returnData = NULL;
213     int len = Openssl_i2d_PrivateKey(impl->pkey, &returnData);
214     if (len <= 0) {
215         LOGD("[error] Call i2d_PrivateKey failed");
216         HcfPrintOpensslError();
217         return HCF_ERR_CRYPTO_OPERATION;
218     }
219     returnBlob->data = returnData;
220     returnBlob->len = len;
221     return HCF_SUCCESS;
222 }
223 
GetAlg25519PubKeyFormat(HcfKey * self)224 static const char *GetAlg25519PubKeyFormat(HcfKey *self)
225 {
226     if (self == NULL) {
227         LOGE("Invalid input parameter.");
228         return NULL;
229     }
230     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
231         LOGE("Invalid class of self.");
232         return NULL;
233     }
234     return OPENSSL_ALG_25519_PUBKEY_FORMAT;
235 }
236 
GetAlg25519PriKeyFormat(HcfKey * self)237 static const char *GetAlg25519PriKeyFormat(HcfKey *self)
238 {
239     if (self == NULL) {
240         LOGE("Invalid input parameter.");
241         return NULL;
242     }
243     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
244         LOGE("Invalid class of self.");
245         return NULL;
246     }
247     return OPENSSL_ALG_25519_PRIKEY_FORMAT;
248 }
249 
GetAlg25519PubKey(EVP_PKEY * pubKey,HcfBigInteger * returnBigInteger)250 static HcfResult GetAlg25519PubKey(EVP_PKEY *pubKey, HcfBigInteger *returnBigInteger)
251 {
252     size_t len = 0;
253     if (!Openssl_EVP_PKEY_get_raw_public_key(pubKey, NULL, &len)) {
254         LOGD("[error] Get len failed.");
255         return HCF_ERR_CRYPTO_OPERATION;
256     }
257     returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0);
258     if (returnBigInteger->data == NULL) {
259         LOGE("Failed to allocate returnBigInteger memory.");
260         return HCF_ERR_MALLOC;
261     }
262     if (!Openssl_EVP_PKEY_get_raw_public_key(pubKey, returnBigInteger->data, &len)) {
263         LOGD("[error] Get data failed.");
264         HcfFree(returnBigInteger->data);
265         returnBigInteger->data = NULL;
266         return HCF_ERR_CRYPTO_OPERATION;
267     }
268     returnBigInteger->len = len;
269     return HCF_SUCCESS;
270 }
271 
CheckEvpKeyTypeFromAlg25519PubKey(EVP_PKEY * alg25519Pk,const AsyKeySpecItem item)272 static HcfResult CheckEvpKeyTypeFromAlg25519PubKey(EVP_PKEY *alg25519Pk, const AsyKeySpecItem item)
273 {
274     int type = Openssl_EVP_PKEY_base_id(alg25519Pk);
275     if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) {
276         LOGE("Invalid pkey type.");
277         return HCF_INVALID_PARAMS;
278     }
279     if ((type == EVP_PKEY_ED25519 && item != ED25519_PK_BN) ||
280         (type == EVP_PKEY_X25519 && item != X25519_PK_BN)) {
281         LOGE("Invalid AsyKeySpecItem.");
282         return HCF_INVALID_PARAMS;
283     }
284     return HCF_SUCCESS;
285 }
286 
CheckEvpKeyTypeFromAlg25519PriKey(EVP_PKEY * alg25519Sk,const AsyKeySpecItem item)287 static HcfResult CheckEvpKeyTypeFromAlg25519PriKey(EVP_PKEY *alg25519Sk, const AsyKeySpecItem item)
288 {
289     int type = Openssl_EVP_PKEY_base_id(alg25519Sk);
290     if (type != EVP_PKEY_ED25519 && type != EVP_PKEY_X25519) {
291         LOGE("Invalid pkey type.");
292         return HCF_INVALID_PARAMS;
293     }
294     if ((type == EVP_PKEY_ED25519 && item != ED25519_SK_BN) ||
295         (type == EVP_PKEY_X25519 && item != X25519_SK_BN)) {
296         LOGE("Invalid AsyKeySpecItem.");
297         return HCF_INVALID_PARAMS;
298     }
299     return HCF_SUCCESS;
300 }
301 
GetBigIntegerSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)302 static HcfResult GetBigIntegerSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item,
303     HcfBigInteger *returnBigInteger)
304 {
305     if (self == NULL || returnBigInteger == NULL) {
306         LOGE("Invalid input parameter.");
307         return HCF_INVALID_PARAMS;
308     }
309     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PubKeyClass())) {
310         LOGE("Invalid class of self.");
311         return HCF_INVALID_PARAMS;
312     }
313     HcfResult ret = HCF_INVALID_PARAMS;
314     HcfOpensslAlg25519PubKey *impl = (HcfOpensslAlg25519PubKey *)self;
315     EVP_PKEY *alg25519Pk = impl->pkey;
316     if (alg25519Pk == NULL) {
317         LOGE("pKey is null.");
318         return HCF_INVALID_PARAMS;
319     }
320     if (CheckEvpKeyTypeFromAlg25519PubKey(alg25519Pk, item) != HCF_SUCCESS) {
321         LOGE("Check pKey type failed.");
322         return HCF_INVALID_PARAMS;
323     }
324     if (item == ED25519_PK_BN || item == X25519_PK_BN) {
325         ret = GetAlg25519PubKey(alg25519Pk, returnBigInteger);
326     } else {
327         LOGE("Input item is invalid");
328     }
329     return ret;
330 }
331 
GetAlg25519PriKey(EVP_PKEY * priKey,HcfBigInteger * returnBigInteger)332 static HcfResult GetAlg25519PriKey(EVP_PKEY *priKey, HcfBigInteger *returnBigInteger)
333 {
334     size_t len = 0;
335     if (!Openssl_EVP_PKEY_get_raw_private_key(priKey, NULL, &len)) {
336         LOGD("[error] Get private key length failed.");
337         return HCF_ERR_CRYPTO_OPERATION;
338     }
339     returnBigInteger->data = (unsigned char *)HcfMalloc(len, 0);
340     if (returnBigInteger->data == NULL) {
341         LOGE("Failed to allocate returnBigInteger memory.");
342         return HCF_ERR_MALLOC;
343     }
344     if (!Openssl_EVP_PKEY_get_raw_private_key(priKey, returnBigInteger->data, &len)) {
345         LOGD("[error] Get data failed.");
346         HcfFree(returnBigInteger->data);
347         returnBigInteger->data = NULL;
348         return HCF_ERR_CRYPTO_OPERATION;
349     }
350     returnBigInteger->len = len;
351     return HCF_SUCCESS;
352 }
353 
GetBigIntegerSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)354 static HcfResult GetBigIntegerSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item,
355     HcfBigInteger *returnBigInteger)
356 {
357     if (self == NULL || returnBigInteger == NULL) {
358         LOGE("Invalid input parameter.");
359         return HCF_INVALID_PARAMS;
360     }
361     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
362         LOGE("Invalid class of self.");
363         return HCF_INVALID_PARAMS;
364     }
365     HcfResult ret = HCF_INVALID_PARAMS;
366     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
367     EVP_PKEY *alg25519Sk = impl->pkey;
368     if (alg25519Sk == NULL) {
369         LOGE("pKey is null.");
370         return HCF_INVALID_PARAMS;
371     }
372     if (CheckEvpKeyTypeFromAlg25519PriKey(alg25519Sk, item) != HCF_SUCCESS) {
373         LOGE("Check pKey type failed.");
374         return HCF_INVALID_PARAMS;
375     }
376     if (item == ED25519_SK_BN || item == X25519_SK_BN) {
377         ret = GetAlg25519PriKey(alg25519Sk, returnBigInteger);
378     } else {
379         LOGE("Input item is invalid");
380     }
381     return ret;
382 }
383 
GetIntSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)384 static HcfResult GetIntSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
385 {
386     (void)self;
387     (void)returnInt;
388     return HCF_NOT_SUPPORT;
389 }
390 
GetIntSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)391 static HcfResult GetIntSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
392 {
393     (void)self;
394     (void)returnInt;
395     return HCF_NOT_SUPPORT;
396 }
397 
GetStrSpecFromAlg25519PubKey(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)398 static HcfResult GetStrSpecFromAlg25519PubKey(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
399 {
400     (void)self;
401     (void)returnString;
402     return HCF_NOT_SUPPORT;
403 }
404 
GetStrSpecFromAlg25519PriKey(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)405 static HcfResult GetStrSpecFromAlg25519PriKey(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
406 {
407     (void)self;
408     (void)returnString;
409     return HCF_NOT_SUPPORT;
410 }
411 
ClearAlg25519PriKeyMem(HcfPriKey * self)412 static void ClearAlg25519PriKeyMem(HcfPriKey *self)
413 {
414     if (self == NULL) {
415         LOGE("Invalid params.");
416         return;
417     }
418     if (!IsClassMatch((HcfObjectBase *)self, GetAlg25519PriKeyClass())) {
419         LOGE("Invalid class of self.");
420         return;
421     }
422     HcfOpensslAlg25519PriKey *impl = (HcfOpensslAlg25519PriKey *)self;
423     Openssl_EVP_PKEY_free(impl->pkey);
424     impl->pkey = NULL;
425 }
426 
GenerateAlg25519EvpKey(int type,EVP_PKEY ** ppkey)427 static HcfResult GenerateAlg25519EvpKey(int type, EVP_PKEY **ppkey)
428 {
429     EVP_PKEY_CTX *paramsCtx = NULL;
430     HcfResult ret = HCF_SUCCESS;
431     do {
432         paramsCtx = Openssl_EVP_PKEY_CTX_new_id(type, NULL);
433         if (paramsCtx == NULL) {
434             LOGE("Create params ctx failed.");
435             ret = HCF_ERR_MALLOC;
436             break;
437         }
438         if (Openssl_EVP_PKEY_keygen_init(paramsCtx) != HCF_OPENSSL_SUCCESS) {
439             LOGD("[error] Key ctx generate init failed.");
440             ret = HCF_ERR_CRYPTO_OPERATION;
441             break;
442         }
443         if (Openssl_EVP_PKEY_keygen(paramsCtx, ppkey) != HCF_OPENSSL_SUCCESS) {
444             LOGD("[error] Generate pkey failed.");
445             ret = HCF_ERR_CRYPTO_OPERATION;
446             break;
447         }
448     } while (0);
449     if (paramsCtx != NULL) {
450         Openssl_EVP_PKEY_CTX_free(paramsCtx);
451     }
452     return ret;
453 }
454 
FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey * pk)455 static void FillOpensslAlg25519PubKeyFunc(HcfOpensslAlg25519PubKey *pk)
456 {
457     pk->base.base.base.destroy = DestroyAlg25519PubKey;
458     pk->base.base.base.getClass = GetAlg25519PubKeyClass;
459     pk->base.base.getAlgorithm = GetAlg25519PubKeyAlgorithm;
460     pk->base.base.getEncoded = GetAlg25519PubKeyEncoded;
461     pk->base.base.getFormat = GetAlg25519PubKeyFormat;
462     pk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PubKey;
463     pk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PubKey;
464     pk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PubKey;
465 }
466 
FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey * sk)467 static void FillOpensslAlg25519PriKeyFunc(HcfOpensslAlg25519PriKey *sk)
468 {
469     sk->base.base.base.destroy = DestroyAlg25519PriKey;
470     sk->base.base.base.getClass = GetAlg25519PriKeyClass;
471     sk->base.base.getAlgorithm = GetAlg25519PriKeyAlgorithm;
472     sk->base.base.getEncoded = GetAlg25519PriKeyEncoded;
473     sk->base.base.getFormat = GetAlg25519PriKeyFormat;
474     sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromAlg25519PriKey;
475     sk->base.getAsyKeySpecInt = GetIntSpecFromAlg25519PriKey;
476     sk->base.getAsyKeySpecString = GetStrSpecFromAlg25519PriKey;
477     sk->base.clearMem = ClearAlg25519PriKeyMem;
478 }
479 
CreateAlg25519PubKey(EVP_PKEY * pkey,HcfOpensslAlg25519PubKey ** returnPubKey)480 static HcfResult CreateAlg25519PubKey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey)
481 {
482     HcfOpensslAlg25519PubKey *alg25519PubKey =
483         (HcfOpensslAlg25519PubKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PubKey), 0);
484     if (alg25519PubKey == NULL) {
485         LOGE("Failed to allocate alg25519 public key memory.");
486         return HCF_ERR_MALLOC;
487     }
488     FillOpensslAlg25519PubKeyFunc(alg25519PubKey);
489     alg25519PubKey->pkey = pkey;
490     *returnPubKey = alg25519PubKey;
491     return HCF_SUCCESS;
492 }
493 
CreateAlg25519PriKey(EVP_PKEY * pkey,HcfOpensslAlg25519PriKey ** returnPriKey)494 static HcfResult CreateAlg25519PriKey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey)
495 {
496     HcfOpensslAlg25519PriKey *alg25519PriKey =
497         (HcfOpensslAlg25519PriKey *)HcfMalloc(sizeof(HcfOpensslAlg25519PriKey), 0);
498     if (alg25519PriKey == NULL) {
499         LOGE("Failed to allocate alg25519 private key memory.");
500         return HCF_ERR_MALLOC;
501     }
502     FillOpensslAlg25519PriKeyFunc(alg25519PriKey);
503     alg25519PriKey->pkey = pkey;
504     *returnPriKey = alg25519PriKey;
505     return HCF_SUCCESS;
506 }
507 
CreateAlg25519KeyPair(const HcfOpensslAlg25519PubKey * pubKey,const HcfOpensslAlg25519PriKey * priKey,HcfKeyPair ** returnKeyPair)508 static HcfResult CreateAlg25519KeyPair(const HcfOpensslAlg25519PubKey *pubKey,
509     const HcfOpensslAlg25519PriKey *priKey, HcfKeyPair **returnKeyPair)
510 {
511     HcfOpensslAlg25519KeyPair *keyPair =
512         (HcfOpensslAlg25519KeyPair *)HcfMalloc(sizeof(HcfOpensslAlg25519KeyPair), 0);
513     if (keyPair == NULL) {
514         LOGE("Failed to allocate keyPair memory.");
515         return HCF_ERR_MALLOC;
516     }
517     keyPair->base.base.getClass = GetAlg25519KeyPairClass;
518     keyPair->base.base.destroy = DestroyAlg25519KeyPair;
519     keyPair->base.pubKey = (HcfPubKey *)pubKey;
520     keyPair->base.priKey = (HcfPriKey *)priKey;
521 
522     *returnKeyPair = (HcfKeyPair *)keyPair;
523     return HCF_SUCCESS;
524 }
525 
GeneratePubKeyByPkey(EVP_PKEY * pkey,HcfOpensslAlg25519PubKey ** returnPubKey)526 static HcfResult GeneratePubKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PubKey **returnPubKey)
527 {
528     EVP_PKEY *evpPkey = Openssl_EVP_PKEY_dup(pkey);
529     if (evpPkey == NULL) {
530         LOGD("[error] pkey dup failed");
531         HcfPrintOpensslError();
532         return HCF_ERR_CRYPTO_OPERATION;
533     }
534     HcfResult ret = CreateAlg25519PubKey(evpPkey, returnPubKey);
535     if (ret != HCF_SUCCESS) {
536         LOGD("[error] Create alg25519 public key failed");
537         Openssl_EVP_PKEY_free(evpPkey);
538     }
539     return ret;
540 }
541 
GeneratePriKeyByPkey(EVP_PKEY * pkey,HcfOpensslAlg25519PriKey ** returnPriKey)542 static HcfResult GeneratePriKeyByPkey(EVP_PKEY *pkey, HcfOpensslAlg25519PriKey **returnPriKey)
543 {
544     EVP_PKEY *evpPkey = Openssl_EVP_PKEY_dup(pkey);
545     if (evpPkey == NULL) {
546         LOGD("[error] pkey dup failed");
547         HcfPrintOpensslError();
548         return HCF_ERR_CRYPTO_OPERATION;
549     }
550     HcfResult ret = CreateAlg25519PriKey(evpPkey, returnPriKey);
551     if (ret != HCF_SUCCESS) {
552         LOGD("[error] Create alg25519 private key failed");
553         Openssl_EVP_PKEY_free(evpPkey);
554     }
555     return ret;
556 }
557 
GenerateAlg25519PubAndPriKey(int type,HcfOpensslAlg25519PubKey ** returnPubKey,HcfOpensslAlg25519PriKey ** returnPriKey)558 static HcfResult GenerateAlg25519PubAndPriKey(int type, HcfOpensslAlg25519PubKey **returnPubKey,
559     HcfOpensslAlg25519PriKey **returnPriKey)
560 {
561     EVP_PKEY *pkey = NULL;
562     HcfResult ret = GenerateAlg25519EvpKey(type, &pkey);
563     if (ret != HCF_SUCCESS) {
564         LOGD("[error] Generate alg25519 EVP_PKEY failed.");
565         return ret;
566     }
567 
568     ret = GeneratePubKeyByPkey(pkey, returnPubKey);
569     if (ret != HCF_SUCCESS) {
570         LOGD("[error] Generate pubkey fail.");
571         Openssl_EVP_PKEY_free(pkey);
572         return ret;
573     }
574 
575     ret = GeneratePriKeyByPkey(pkey, returnPriKey);
576     if (ret != HCF_SUCCESS) {
577         LOGD("[error] Generate prikey fail.");
578         HcfObjDestroy(*returnPubKey);
579         *returnPubKey = NULL;
580         Openssl_EVP_PKEY_free(pkey);
581         return HCF_ERR_CRYPTO_OPERATION;
582     }
583 
584     Openssl_EVP_PKEY_free(pkey);
585     return ret;
586 }
587 
ConvertAlg25519PubKey(const HcfBlob * pubKeyBlob,HcfOpensslAlg25519PubKey ** returnPubKey)588 static HcfResult ConvertAlg25519PubKey(const HcfBlob *pubKeyBlob, HcfOpensslAlg25519PubKey **returnPubKey)
589 {
590     const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data);
591     EVP_PKEY *pkey = Openssl_d2i_PUBKEY(NULL, &tmpData, pubKeyBlob->len);
592     if (pkey == NULL) {
593         LOGD("[error] Call d2i_PUBKEY fail.");
594         HcfPrintOpensslError();
595         return HCF_ERR_CRYPTO_OPERATION;
596     }
597     HcfResult ret = CreateAlg25519PubKey(pkey, returnPubKey);
598     if (ret != HCF_SUCCESS) {
599         LOGD("[error] Create alg25519 public key failed");
600         Openssl_EVP_PKEY_free(pkey);
601     }
602     return ret;
603 }
604 
ConvertAlg25519PriKey(int type,const HcfBlob * priKeyBlob,HcfOpensslAlg25519PriKey ** returnPriKey)605 static HcfResult ConvertAlg25519PriKey(int type, const HcfBlob *priKeyBlob,
606     HcfOpensslAlg25519PriKey **returnPriKey)
607 {
608     const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data);
609     EVP_PKEY *pkey = Openssl_d2i_PrivateKey(type, NULL, &tmpData, priKeyBlob->len);
610     if (pkey == NULL) {
611         LOGD("[error] Call d2i_PrivateKey fail.");
612         HcfPrintOpensslError();
613         return HCF_ERR_CRYPTO_OPERATION;
614     }
615     HcfResult ret = CreateAlg25519PriKey(pkey, returnPriKey);
616     if (ret != HCF_SUCCESS) {
617         LOGD("[error] Create alg25519 private key failed");
618         Openssl_EVP_PKEY_free(pkey);
619     }
620     return ret;
621 }
622 
ConvertAlg25519PubAndPriKey(int type,const HcfBlob * pubKeyBlob,const HcfBlob * priKeyBlob,HcfOpensslAlg25519PubKey ** returnPubKey,HcfOpensslAlg25519PriKey ** returnPriKey)623 static HcfResult ConvertAlg25519PubAndPriKey(int type, const HcfBlob *pubKeyBlob, const HcfBlob *priKeyBlob,
624     HcfOpensslAlg25519PubKey **returnPubKey, HcfOpensslAlg25519PriKey **returnPriKey)
625 {
626     if (pubKeyBlob != NULL) {
627         if (ConvertAlg25519PubKey(pubKeyBlob, returnPubKey) != HCF_SUCCESS) {
628             LOGD("[error] Convert alg25519 public key failed.");
629             return HCF_ERR_CRYPTO_OPERATION;
630         }
631     }
632     if (priKeyBlob != NULL) {
633         if (ConvertAlg25519PriKey(type, priKeyBlob, returnPriKey) != HCF_SUCCESS) {
634             LOGD("[error] Convert alg25519 private key failed.");
635             HcfObjDestroy(*returnPubKey);
636             *returnPubKey = NULL;
637             return HCF_ERR_CRYPTO_OPERATION;
638         }
639     }
640     return HCF_SUCCESS;
641 }
642 
CheckClassMatch(HcfAsyKeyGeneratorSpi * self,int * type)643 static HcfResult CheckClassMatch(HcfAsyKeyGeneratorSpi *self, int *type)
644 {
645     if (IsClassMatch((HcfObjectBase *)self, GetEd25519KeyGeneratorSpiClass())) {
646         *type = EVP_PKEY_ED25519;
647     } else if (IsClassMatch((HcfObjectBase *)self, GetX25519KeyGeneratorSpiClass())) {
648         *type = EVP_PKEY_X25519;
649     } else {
650         LOGE("Invalid class of self.");
651         return HCF_INVALID_PARAMS;
652     }
653     return HCF_SUCCESS;
654 }
655 
EngineGenerateAlg25519KeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnKeyPair)656 static HcfResult EngineGenerateAlg25519KeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnKeyPair)
657 {
658     if (self == NULL || returnKeyPair == NULL) {
659         LOGE("Invalid params.");
660         return HCF_INVALID_PARAMS;
661     }
662     int type = 0;
663     if (CheckClassMatch(self, &type) != HCF_SUCCESS) {
664         LOGE("Invalid class of self.");
665         return HCF_INVALID_PARAMS;
666     }
667 
668     HcfOpensslAlg25519PubKey *pubKey = NULL;
669     HcfOpensslAlg25519PriKey *priKey = NULL;
670     HcfResult ret = GenerateAlg25519PubAndPriKey(type, &pubKey, &priKey);
671     if (ret != HCF_SUCCESS) {
672         LOGE("Generate alg25519 pk and sk by openssl failed.");
673         return ret;
674     }
675 
676     if (pubKey != NULL) {
677         pubKey->type = type;
678     }
679 
680     if (priKey != NULL) {
681         priKey->type = type;
682     }
683 
684     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
685     if (ret != HCF_SUCCESS) {
686         LOGE("Create alg25519 keyPair failed.");
687         HcfObjDestroy(pubKey);
688         HcfObjDestroy(priKey);
689         return ret;
690     }
691     return HCF_SUCCESS;
692 }
693 
EngineConvertAlg25519Key(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)694 static HcfResult EngineConvertAlg25519Key(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
695     HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
696 {
697     (void)params;
698     if ((self == NULL) || (returnKeyPair == NULL)) {
699         LOGE("Invalid input parameter.");
700         return HCF_INVALID_PARAMS;
701     }
702     int type = 0;
703     if (CheckClassMatch(self, &type) != HCF_SUCCESS) {
704         LOGE("Invalid class of self.");
705         return HCF_INVALID_PARAMS;
706     }
707     bool pubKeyValid = IsBlobValid(pubKeyBlob);
708     bool priKeyValid = IsBlobValid(priKeyBlob);
709     if ((!pubKeyValid) && (!priKeyValid)) {
710         LOGE("The private key and public key cannot both be NULL.");
711         return HCF_INVALID_PARAMS;
712     }
713 
714     HcfOpensslAlg25519PubKey *pubKey = NULL;
715     HcfOpensslAlg25519PriKey *priKey = NULL;
716     HcfBlob *inputPk = pubKeyValid ? pubKeyBlob : NULL;
717     HcfBlob *inputSk = priKeyValid ? priKeyBlob : NULL;
718     HcfResult ret = ConvertAlg25519PubAndPriKey(type, inputPk, inputSk, &pubKey, &priKey);
719     if (ret != HCF_SUCCESS) {
720         LOGE("Convert alg25519 keyPair failed.");
721         return ret;
722     }
723 
724     if (pubKey != NULL) {
725         pubKey->type = type;
726     }
727 
728     if (priKey != NULL) {
729         priKey->type = type;
730     }
731 
732     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
733     if (ret != HCF_SUCCESS) {
734         LOGE("Create alg25519 keyPair failed.");
735         HcfObjDestroy(pubKey);
736         HcfObjDestroy(priKey);
737     }
738     return ret;
739 }
740 
CreateOpensslAlg25519PubKey(const HcfBigInteger * pk,const char * algName,EVP_PKEY ** returnAlg25519)741 static HcfResult CreateOpensslAlg25519PubKey(const HcfBigInteger *pk, const char *algName,
742     EVP_PKEY **returnAlg25519)
743 {
744     EVP_PKEY *pubkey = NULL;
745     if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) {
746         pubkey = Openssl_EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, pk->data, pk->len);
747     } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) {
748         pubkey = Openssl_EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, pk->data, pk->len);
749     } else {
750         LOGE("Invalid algName! [Algo]: %s", algName);
751         return HCF_INVALID_PARAMS;
752     }
753     if (pubkey == NULL) {
754         LOGD("[error] Set alg25519 pubKey failed.");
755         HcfPrintOpensslError();
756         return HCF_ERR_CRYPTO_OPERATION;
757     }
758     *returnAlg25519 = pubkey;
759     return HCF_SUCCESS;
760 }
761 
CreateOpensslAlg25519PriKey(const HcfBigInteger * sk,const char * algName,EVP_PKEY ** returnAlg25519)762 static HcfResult CreateOpensslAlg25519PriKey(const HcfBigInteger *sk, const char *algName,
763     EVP_PKEY **returnAlg25519)
764 {
765     EVP_PKEY *privkey = NULL;
766     if (strcmp(algName, ALGORITHM_NAME_ED25519) == 0) {
767         privkey = Openssl_EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, sk->data, sk->len);
768     } else if (strcmp(algName, ALGORITHM_NAME_X25519) == 0) {
769         privkey = Openssl_EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, sk->data, sk->len);
770     } else {
771         LOGE("Invalid algName! [Algo]: %s", algName);
772         return HCF_INVALID_PARAMS;
773     }
774     if (privkey == NULL) {
775         LOGD("[error] Get alg25519 priKey failed.");
776         HcfPrintOpensslError();
777         return HCF_ERR_CRYPTO_OPERATION;
778     }
779     *returnAlg25519 = privkey;
780     return HCF_SUCCESS;
781 }
782 
CreateAlg25519PubKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PubKey ** returnPubKey)783 static HcfResult CreateAlg25519PubKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
784     const char *algName, HcfOpensslAlg25519PubKey **returnPubKey)
785 {
786     EVP_PKEY *alg25519 = NULL;
787     if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) {
788         LOGD("[error] Create openssl alg25519 pubKey failed.");
789         return HCF_ERR_CRYPTO_OPERATION;
790     }
791     if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) {
792         LOGE("Create alg25519 pubKey failed.");
793         Openssl_EVP_PKEY_free(alg25519);
794         return HCF_ERR_MALLOC;
795     }
796     return HCF_SUCCESS;
797 }
798 
CreateAlg25519PriKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PriKey ** returnPriKey)799 static HcfResult CreateAlg25519PriKeyByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
800     const char *algName, HcfOpensslAlg25519PriKey **returnPriKey)
801 {
802     EVP_PKEY *alg25519 = NULL;
803     if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) {
804         LOGD("[error] Create openssl alg25519 priKey failed.");
805         return HCF_ERR_CRYPTO_OPERATION;
806     }
807     if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) {
808         LOGE("Create alg25519 priKey failed.");
809         Openssl_EVP_PKEY_free(alg25519);
810         return HCF_ERR_MALLOC;
811     }
812     return HCF_SUCCESS;
813 }
814 
CreateAlg25519KeyPairByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec * paramsSpec,const char * algName,HcfKeyPair ** returnKeyPair)815 static HcfResult CreateAlg25519KeyPairByKeyPairSpec(const HcfAlg25519KeyPairParamsSpec *paramsSpec,
816     const char *algName, HcfKeyPair **returnKeyPair)
817 {
818     HcfOpensslAlg25519PubKey *pubKey = NULL;
819     HcfResult ret = CreateAlg25519PubKeyByKeyPairSpec(paramsSpec, algName, &pubKey);
820     if (ret != HCF_SUCCESS) {
821         LOGE("Create alg25519 pubKey failed.");
822         return ret;
823     }
824 
825     HcfOpensslAlg25519PriKey *priKey = NULL;
826     ret = CreateAlg25519PriKeyByKeyPairSpec(paramsSpec, algName, &priKey);
827     if (ret != HCF_SUCCESS) {
828         LOGE("Create alg25519 priKey failed.");
829         HcfObjDestroy(pubKey);
830         return ret;
831     }
832     ret = CreateAlg25519KeyPair(pubKey, priKey, returnKeyPair);
833     if (ret != HCF_SUCCESS) {
834         LOGE("Create alg25519 keyPair failed.");
835         HcfObjDestroy(pubKey);
836         HcfObjDestroy(priKey);
837         return ret;
838     }
839     return HCF_SUCCESS;
840 }
841 
CreateAlg25519PubKeyByPubKeySpec(const HcfAlg25519PubKeyParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PubKey ** returnPubKey)842 static HcfResult CreateAlg25519PubKeyByPubKeySpec(const HcfAlg25519PubKeyParamsSpec *paramsSpec,
843     const char *algName, HcfOpensslAlg25519PubKey **returnPubKey)
844 {
845     EVP_PKEY *alg25519 = NULL;
846     if (CreateOpensslAlg25519PubKey(&(paramsSpec->pk), algName, &alg25519) != HCF_SUCCESS) {
847         LOGD("[error] Create openssl alg25519 pubKey failed.");
848         return HCF_ERR_CRYPTO_OPERATION;
849     }
850     if (CreateAlg25519PubKey(alg25519, returnPubKey) != HCF_SUCCESS) {
851         LOGE("Create alg25519 pubKey failed.");
852         Openssl_EVP_PKEY_free(alg25519);
853         return HCF_ERR_MALLOC;
854     }
855     return HCF_SUCCESS;
856 }
857 
CreateAlg25519PriKeyByPriKeySpec(const HcfAlg25519PriKeyParamsSpec * paramsSpec,const char * algName,HcfOpensslAlg25519PriKey ** returnPriKey)858 static HcfResult CreateAlg25519PriKeyByPriKeySpec(const HcfAlg25519PriKeyParamsSpec *paramsSpec,
859     const char *algName, HcfOpensslAlg25519PriKey **returnPriKey)
860 {
861     EVP_PKEY *alg25519 = NULL;
862     if (CreateOpensslAlg25519PriKey(&(paramsSpec->sk), algName, &alg25519) != HCF_SUCCESS) {
863         LOGD("[error] Create openssl alg25519 priKey failed.");
864         return HCF_ERR_CRYPTO_OPERATION;
865     }
866     if (CreateAlg25519PriKey(alg25519, returnPriKey) != HCF_SUCCESS) {
867         LOGE("Create alg25519 priKey failed.");
868         Openssl_EVP_PKEY_free(alg25519);
869         return HCF_ERR_MALLOC;
870     }
871     return HCF_SUCCESS;
872 }
873 
EngineGenerateAlg25519PubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)874 static HcfResult EngineGenerateAlg25519PubKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
875     const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
876 {
877     if ((self == NULL) || (paramsSpec == NULL) || (returnPubKey == NULL)) {
878         LOGE("Invalid input parameter.");
879         return HCF_INVALID_PARAMS;
880     }
881 
882     int type = 0;
883     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
884         LOGE("Invalid class of self.");
885         return HCF_INVALID_PARAMS;
886     }
887 
888     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
889         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
890         (paramsSpec->specType != HCF_PUBLIC_KEY_SPEC)) {
891         LOGE("Invalid params spec.");
892         return HCF_INVALID_PARAMS;
893     }
894     HcfOpensslAlg25519PubKey *alg25519Pk = NULL;
895     HcfResult ret = CreateAlg25519PubKeyByPubKeySpec((const HcfAlg25519PubKeyParamsSpec *)paramsSpec,
896         paramsSpec->algName, &alg25519Pk);
897     if (ret != HCF_SUCCESS) {
898         LOGD("[error] Create alg25519 public key by spec failed.");
899         return ret;
900     }
901 
902     alg25519Pk->type = type;
903     *returnPubKey = (HcfPubKey *)alg25519Pk;
904 
905     return ret;
906 }
907 
EngineGenerateAlg25519PriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)908 static HcfResult EngineGenerateAlg25519PriKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
909     const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
910 {
911     if ((self == NULL) || (paramsSpec == NULL) || (returnPriKey == NULL)) {
912         LOGE("Invalid input parameter.");
913         return HCF_INVALID_PARAMS;
914     }
915 
916     int type = 0;
917     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
918         LOGE("Invalid class of self.");
919         return HCF_INVALID_PARAMS;
920     }
921 
922     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
923         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
924         (paramsSpec->specType != HCF_PRIVATE_KEY_SPEC)) {
925         LOGE("Invalid params spec.");
926         return HCF_INVALID_PARAMS;
927     }
928     HcfOpensslAlg25519PriKey *alg25519Sk = NULL;
929     HcfResult ret = CreateAlg25519PriKeyByPriKeySpec((const HcfAlg25519PriKeyParamsSpec *)paramsSpec,
930         paramsSpec->algName, &alg25519Sk);
931     if (ret != HCF_SUCCESS) {
932         LOGD("[error] Create alg25519 private key by spec failed.");
933         return ret;
934     }
935 
936     alg25519Sk->type = type;
937     *returnPriKey = (HcfPriKey *)alg25519Sk;
938 
939     return ret;
940 }
941 
EngineGenerateAlg25519KeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)942 static HcfResult EngineGenerateAlg25519KeyPairBySpec(const HcfAsyKeyGeneratorSpi *self,
943     const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
944 {
945     if ((self == NULL) || (paramsSpec == NULL) || (returnKeyPair == NULL)) {
946         LOGE("Invalid input parameter.");
947         return HCF_INVALID_PARAMS;
948     }
949 
950     int type = 0;
951     if (CheckClassMatch((HcfAsyKeyGeneratorSpi *)self, &type) != HCF_SUCCESS) {
952         LOGE("Invalid class of self.");
953         return HCF_INVALID_PARAMS;
954     }
955 
956     if (((strcmp(paramsSpec->algName, ALGORITHM_NAME_ED25519) != 0) &&
957         (strcmp(paramsSpec->algName, ALGORITHM_NAME_X25519) != 0)) ||
958         (paramsSpec->specType != HCF_KEY_PAIR_SPEC)) {
959         LOGE("Invalid params spec.");
960         return HCF_INVALID_PARAMS;
961     }
962     HcfResult ret = CreateAlg25519KeyPairByKeyPairSpec((const HcfAlg25519KeyPairParamsSpec *)paramsSpec,
963         paramsSpec->algName, returnKeyPair);
964     if (ret != HCF_SUCCESS) {
965         LOGD("[error] Create alg25519 key pair by spec failed.");
966         return ret;
967     }
968 
969     HcfOpensslAlg25519KeyPair *keyPair = (HcfOpensslAlg25519KeyPair *)(*returnKeyPair);
970     HcfOpensslAlg25519PubKey *pubKey = (HcfOpensslAlg25519PubKey *)(keyPair->base.pubKey);
971     HcfOpensslAlg25519PriKey *priKey = (HcfOpensslAlg25519PriKey *)(keyPair->base.priKey);
972     pubKey->type = type;
973     priKey->type = type;
974     return ret;
975 }
976 
HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnSpi)977 HcfResult HcfAsyKeyGeneratorSpiEd25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnSpi)
978 {
979     (void)params;
980     if (params == NULL || returnSpi == NULL) {
981         LOGE("Invalid input parameter.");
982         return HCF_INVALID_PARAMS;
983     }
984     HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc(
985         sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0);
986     if (impl == NULL) {
987         LOGE("Failed to allocate generator impl memroy.");
988         return HCF_ERR_MALLOC;
989     }
990     impl->base.base.getClass = GetEd25519KeyGeneratorSpiClass;
991     impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl;
992     impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair;
993     impl->base.engineConvertKey = EngineConvertAlg25519Key;
994     impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec;
995     impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec;
996     impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec;
997 
998     *returnSpi = (HcfAsyKeyGeneratorSpi *)impl;
999     return HCF_SUCCESS;
1000 }
1001 
HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnSpi)1002 HcfResult HcfAsyKeyGeneratorSpiX25519Create(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnSpi)
1003 {
1004     (void)params;
1005     if (params == NULL || returnSpi == NULL) {
1006         LOGE("Invalid input parameter.");
1007         return HCF_INVALID_PARAMS;
1008     }
1009     HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *impl = (HcfAsyKeyGeneratorSpiAlg25519OpensslImpl *)HcfMalloc(
1010         sizeof(HcfAsyKeyGeneratorSpiAlg25519OpensslImpl), 0);
1011     if (impl == NULL) {
1012         LOGE("Failed to allocate generator impl memroy.");
1013         return HCF_ERR_MALLOC;
1014     }
1015     impl->base.base.getClass = GetX25519KeyGeneratorSpiClass;
1016     impl->base.base.destroy = DestroyAlg25519KeyGeneratorSpiImpl;
1017     impl->base.engineGenerateKeyPair = EngineGenerateAlg25519KeyPair;
1018     impl->base.engineConvertKey = EngineConvertAlg25519Key;
1019     impl->base.engineGenerateKeyPairBySpec = EngineGenerateAlg25519KeyPairBySpec;
1020     impl->base.engineGeneratePubKeyBySpec = EngineGenerateAlg25519PubKeyBySpec;
1021     impl->base.engineGeneratePriKeyBySpec = EngineGenerateAlg25519PriKeyBySpec;
1022 
1023     *returnSpi = (HcfAsyKeyGeneratorSpi *)impl;
1024     return HCF_SUCCESS;
1025 }
1026 
1027