• 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 "dsa_asy_key_generator_openssl.h"
17 
18 #include <openssl/dsa.h>
19 #include <openssl/evp.h>
20 #include <string.h>
21 
22 #include "detailed_dsa_key_params.h"
23 #include "log.h"
24 #include "memory.h"
25 #include "openssl_adapter.h"
26 #include "openssl_class.h"
27 #include "openssl_common.h"
28 #include "utils.h"
29 
30 #define OPENSSL_DSA_GENERATOR_CLASS "OPENSSL.DSA.KEYGENERATOR"
31 #define OPENSSL_DSA_PUBKEY_FORMAT "X.509"
32 #define OPENSSL_DSA_PRIKEY_FORMAT "PKCS#8"
33 #define ALGORITHM_NAME_DSA "DSA"
34 
35 typedef struct {
36     HcfAsyKeyGeneratorSpi base;
37 
38     int32_t bits;
39 } HcfAsyKeyGeneratorSpiDsaOpensslImpl;
40 
FreeCtx(EVP_PKEY_CTX * paramsCtx,EVP_PKEY * paramsPkey,EVP_PKEY_CTX * pkeyCtx)41 static void FreeCtx(EVP_PKEY_CTX *paramsCtx, EVP_PKEY *paramsPkey, EVP_PKEY_CTX *pkeyCtx)
42 {
43     if (paramsCtx != NULL) {
44         Openssl_EVP_PKEY_CTX_free(paramsCtx);
45     }
46     if (paramsPkey != NULL) {
47         Openssl_EVP_PKEY_free(paramsPkey);
48     }
49     if (pkeyCtx != NULL) {
50         Openssl_EVP_PKEY_CTX_free(pkeyCtx);
51     }
52 }
53 
FreeCommSpecBn(BIGNUM * p,BIGNUM * q,BIGNUM * g)54 static void FreeCommSpecBn(BIGNUM *p, BIGNUM *q, BIGNUM *g)
55 {
56     if (p != NULL) {
57         Openssl_BN_free(p);
58     }
59     if (q != NULL) {
60         Openssl_BN_free(q);
61     }
62     if (g != NULL) {
63         Openssl_BN_free(g);
64     }
65 }
66 
GetDsaKeyGeneratorSpiClass(void)67 static const char *GetDsaKeyGeneratorSpiClass(void)
68 {
69     return OPENSSL_DSA_GENERATOR_CLASS;
70 }
71 
GetDsaKeyPairClass(void)72 static const char *GetDsaKeyPairClass(void)
73 {
74     return OPENSSL_DSA_KEYPAIR_CLASS;
75 }
76 
GetDsaPubKeyClass(void)77 static const char *GetDsaPubKeyClass(void)
78 {
79     return OPENSSL_DSA_PUBKEY_CLASS;
80 }
81 
GetDsaPriKeyClass(void)82 static const char *GetDsaPriKeyClass(void)
83 {
84     return OPENSSL_DSA_PRIKEY_CLASS;
85 }
86 
DestroyDsaKeyGeneratorSpiImpl(HcfObjectBase * self)87 static void DestroyDsaKeyGeneratorSpiImpl(HcfObjectBase *self)
88 {
89     if (self == NULL) {
90         return;
91     }
92     if (!IsClassMatch(self, GetDsaKeyGeneratorSpiClass())) {
93         return;
94     }
95     HcfFree(self);
96 }
97 
DestroyDsaPubKey(HcfObjectBase * self)98 static void DestroyDsaPubKey(HcfObjectBase *self)
99 {
100     if (self == NULL) {
101         return;
102     }
103     if (!IsClassMatch(self, GetDsaPubKeyClass())) {
104         return;
105     }
106     HcfOpensslDsaPubKey *impl = (HcfOpensslDsaPubKey *)self;
107     Openssl_DSA_free(impl->pk);
108     impl->pk = NULL;
109     HcfFree(impl);
110 }
111 
DestroyDsaPriKey(HcfObjectBase * self)112 static void DestroyDsaPriKey(HcfObjectBase *self)
113 {
114     if (self == NULL) {
115         return;
116     }
117     if (!IsClassMatch(self, GetDsaPriKeyClass())) {
118         return;
119     }
120     HcfOpensslDsaPriKey *impl = (HcfOpensslDsaPriKey *)self;
121     Openssl_DSA_free(impl->sk);
122     impl->sk = NULL;
123     HcfFree(impl);
124 }
125 
DestroyDsaKeyPair(HcfObjectBase * self)126 static void DestroyDsaKeyPair(HcfObjectBase *self)
127 {
128     if (self == NULL) {
129         return;
130     }
131     if (!IsClassMatch(self, GetDsaKeyPairClass())) {
132         return;
133     }
134     HcfOpensslDsaKeyPair *impl = (HcfOpensslDsaKeyPair *)self;
135     DestroyDsaPubKey((HcfObjectBase *)impl->base.pubKey);
136     impl->base.pubKey = NULL;
137     DestroyDsaPriKey((HcfObjectBase *)impl->base.priKey);
138     impl->base.priKey = NULL;
139     HcfFree(self);
140 }
141 
GetDsaPubKeyAlgorithm(HcfKey * self)142 static const char *GetDsaPubKeyAlgorithm(HcfKey *self)
143 {
144     if (self == NULL) {
145         LOGE("Invalid input parameter.");
146         return NULL;
147     }
148     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPubKeyClass())) {
149         return NULL;
150     }
151     return ALGORITHM_NAME_DSA;
152 }
153 
GetDsaPriKeyAlgorithm(HcfKey * self)154 static const char *GetDsaPriKeyAlgorithm(HcfKey *self)
155 {
156     if (self == NULL) {
157         LOGE("Invalid input parameter.");
158         return NULL;
159     }
160     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPriKeyClass())) {
161         return NULL;
162     }
163     return ALGORITHM_NAME_DSA;
164 }
165 
GetDsaPubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)166 static HcfResult GetDsaPubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
167 {
168     if ((self == NULL) || (returnBlob == NULL)) {
169         LOGE("Invalid input parameter.");
170         return HCF_INVALID_PARAMS;
171     }
172     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPubKeyClass())) {
173         return HCF_INVALID_PARAMS;
174     }
175     HcfOpensslDsaPubKey *impl = (HcfOpensslDsaPubKey *)self;
176     unsigned char *returnData = NULL;
177     int len = Openssl_i2d_DSA_PUBKEY(impl->pk, &returnData);
178     if (len <= 0) {
179         LOGE("Call i2d_DSA_PUBKEY failed");
180         HcfPrintOpensslError();
181         return HCF_ERR_CRYPTO_OPERATION;
182     }
183     returnBlob->data = returnData;
184     returnBlob->len = len;
185     return HCF_SUCCESS;
186 }
187 
GetDsaPriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)188 static HcfResult GetDsaPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
189 {
190     if ((self == NULL) || (returnBlob == NULL)) {
191         LOGE("Invalid input parameter.");
192         return HCF_INVALID_PARAMS;
193     }
194     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPriKeyClass())) {
195         return HCF_INVALID_PARAMS;
196     }
197     HcfOpensslDsaPriKey *impl = (HcfOpensslDsaPriKey *)self;
198     unsigned char *returnData = NULL;
199     int len = Openssl_i2d_DSAPrivateKey(impl->sk, &returnData);
200     if (len <= 0) {
201         LOGE("Call i2d_DSAPrivateKey failed.");
202         HcfPrintOpensslError();
203         return HCF_ERR_CRYPTO_OPERATION;
204     }
205     returnBlob->data = returnData;
206     returnBlob->len = len;
207     return HCF_SUCCESS;
208 }
209 
GetDsaPubKeyFormat(HcfKey * self)210 static const char *GetDsaPubKeyFormat(HcfKey *self)
211 {
212     if (self == NULL) {
213         LOGE("Invalid input parameter.");
214         return NULL;
215     }
216     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPubKeyClass())) {
217         return NULL;
218     }
219     return OPENSSL_DSA_PUBKEY_FORMAT;
220 }
221 
GetDsaPriKeyFormat(HcfKey * self)222 static const char *GetDsaPriKeyFormat(HcfKey *self)
223 {
224     if (self == NULL) {
225         LOGE("Invalid input parameter.");
226         return NULL;
227     }
228     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPriKeyClass())) {
229         return NULL;
230     }
231     return OPENSSL_DSA_PRIKEY_FORMAT;
232 }
233 
GetBigIntegerSpecFromDsaPubKey(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)234 static HcfResult GetBigIntegerSpecFromDsaPubKey(const HcfPubKey *self, const AsyKeySpecItem item,
235     HcfBigInteger *returnBigInteger)
236 {
237     if (self ==  NULL || returnBigInteger == NULL) {
238         LOGE("Invalid input parameter.");
239         return HCF_INVALID_PARAMS;
240     }
241     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPubKeyClass())) {
242         LOGE("Invalid class of self.");
243         return HCF_INVALID_PARAMS;
244     }
245     HcfResult ret = HCF_SUCCESS;
246     HcfOpensslDsaPubKey *impl = (HcfOpensslDsaPubKey *)self;
247     DSA *dsaPk = impl->pk;
248     if (dsaPk == NULL) {
249         return HCF_INVALID_PARAMS;
250     }
251     switch (item) {
252         case DSA_P_BN:
253             ret = BigNumToBigInteger(Openssl_DSA_get0_p(dsaPk), returnBigInteger);
254             break;
255         case DSA_Q_BN:
256             ret = BigNumToBigInteger(Openssl_DSA_get0_q(dsaPk), returnBigInteger);
257             break;
258         case DSA_G_BN:
259             ret = BigNumToBigInteger(Openssl_DSA_get0_g(dsaPk), returnBigInteger);
260             break;
261         case DSA_PK_BN:
262             ret = BigNumToBigInteger(Openssl_DSA_get0_pub_key(dsaPk), returnBigInteger);
263             break;
264         default:
265             LOGE("Input item is invalid");
266             ret = HCF_INVALID_PARAMS;
267             break;
268     }
269     return ret;
270 }
271 
GetBigIntegerSpecFromDsaPriKey(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)272 static HcfResult GetBigIntegerSpecFromDsaPriKey(const HcfPriKey *self, const AsyKeySpecItem item,
273     HcfBigInteger *returnBigInteger)
274 {
275     if (self ==  NULL || returnBigInteger == NULL) {
276         LOGE("Invalid input parameter.");
277         return HCF_INVALID_PARAMS;
278     }
279     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPriKeyClass())) {
280         LOGE("Invalid class of self.");
281         return HCF_INVALID_PARAMS;
282     }
283     HcfResult ret = HCF_SUCCESS;
284     HcfOpensslDsaPriKey *impl = (HcfOpensslDsaPriKey *)self;
285     DSA *dsaSk = impl->sk;
286     if (dsaSk == NULL) {
287         return HCF_INVALID_PARAMS;
288     }
289     switch (item) {
290         case DSA_P_BN:
291             ret = BigNumToBigInteger(Openssl_DSA_get0_p(dsaSk), returnBigInteger);
292             break;
293         case DSA_Q_BN:
294             ret = BigNumToBigInteger(Openssl_DSA_get0_q(dsaSk), returnBigInteger);
295             break;
296         case DSA_G_BN:
297             ret = BigNumToBigInteger(Openssl_DSA_get0_g(dsaSk), returnBigInteger);
298             break;
299         case DSA_SK_BN:
300             ret = BigNumToBigInteger(Openssl_DSA_get0_priv_key(dsaSk), returnBigInteger);
301             break;
302         default:
303             LOGE("Input item is invalid");
304             ret = HCF_INVALID_PARAMS;
305             break;
306     }
307     return ret;
308 }
309 
GetIntSpecFromDsaPubKey(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)310 static HcfResult GetIntSpecFromDsaPubKey(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
311 {
312     (void)self;
313     (void)returnInt;
314     return HCF_NOT_SUPPORT;
315 }
316 
GetIntSpecFromDsaPriKey(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)317 static HcfResult GetIntSpecFromDsaPriKey(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
318 {
319     (void)self;
320     (void)returnInt;
321     return HCF_NOT_SUPPORT;
322 }
323 
GetStrSpecFromDsaPubKey(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)324 static HcfResult GetStrSpecFromDsaPubKey(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
325 {
326     (void)self;
327     (void)returnString;
328     return HCF_NOT_SUPPORT;
329 }
330 
GetStrSpecFromDsaPriKey(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)331 static HcfResult GetStrSpecFromDsaPriKey(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
332 {
333     (void)self;
334     (void)returnString;
335     return HCF_NOT_SUPPORT;
336 }
337 
ClearDsaPriKeyMem(HcfPriKey * self)338 static void ClearDsaPriKeyMem(HcfPriKey *self)
339 {
340     if (self == NULL) {
341         return;
342     }
343     if (!IsClassMatch((HcfObjectBase *)self, GetDsaPriKeyClass())) {
344         return;
345     }
346     HcfOpensslDsaPriKey *impl = (HcfOpensslDsaPriKey *)self;
347     Openssl_DSA_free(impl->sk);
348     impl->sk = NULL;
349 }
350 
GenerateDsaEvpKey(int32_t keyLen,EVP_PKEY ** ppkey)351 static HcfResult GenerateDsaEvpKey(int32_t keyLen, EVP_PKEY **ppkey)
352 {
353     EVP_PKEY_CTX *paramsCtx = NULL;
354     EVP_PKEY *paramsPkey = NULL;
355     EVP_PKEY_CTX *pkeyCtx = NULL;
356     HcfResult ret = HCF_SUCCESS;
357     do {
358         paramsCtx = Openssl_EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, NULL);
359         if (paramsCtx == NULL) {
360             LOGE("Create params ctx failed.");
361             ret = HCF_ERR_MALLOC;
362             break;
363         }
364         if (Openssl_EVP_PKEY_paramgen_init(paramsCtx) != HCF_OPENSSL_SUCCESS) {
365             LOGE("Params ctx generate init failed.");
366             ret = HCF_ERR_CRYPTO_OPERATION;
367             break;
368         }
369         if (Openssl_EVP_PKEY_CTX_set_dsa_paramgen_bits(paramsCtx, keyLen) <= 0) {
370             LOGE("Set length of bits to params ctx failed.");
371             ret = HCF_ERR_CRYPTO_OPERATION;
372             break;
373         }
374         if (Openssl_EVP_PKEY_paramgen(paramsCtx, &paramsPkey) != HCF_OPENSSL_SUCCESS) {
375             LOGE("Generate params pkey failed.");
376             ret = HCF_ERR_CRYPTO_OPERATION;
377             break;
378         }
379         pkeyCtx = Openssl_EVP_PKEY_CTX_new(paramsPkey, NULL);
380         if (pkeyCtx == NULL) {
381             LOGE("Create pkey ctx failed.");
382             ret = HCF_ERR_CRYPTO_OPERATION;
383             break;
384         }
385         if (Openssl_EVP_PKEY_keygen_init(pkeyCtx) != HCF_OPENSSL_SUCCESS) {
386             LOGE("Key ctx generate init failed.");
387             ret = HCF_ERR_CRYPTO_OPERATION;
388             break;
389         }
390         if (Openssl_EVP_PKEY_keygen(pkeyCtx, ppkey) != HCF_OPENSSL_SUCCESS) {
391             LOGE("Generate pkey failed.");
392             ret = HCF_ERR_CRYPTO_OPERATION;
393             break;
394         }
395     } while (0);
396     FreeCtx(paramsCtx, paramsPkey, pkeyCtx);
397     return ret;
398 }
399 
FillOpensslDsaPubKeyFunc(HcfOpensslDsaPubKey * pk)400 static void FillOpensslDsaPubKeyFunc(HcfOpensslDsaPubKey *pk)
401 {
402     pk->base.base.base.destroy = DestroyDsaPubKey;
403     pk->base.base.base.getClass = GetDsaPubKeyClass;
404     pk->base.base.getAlgorithm = GetDsaPubKeyAlgorithm;
405     pk->base.base.getEncoded = GetDsaPubKeyEncoded;
406     pk->base.base.getFormat = GetDsaPubKeyFormat;
407     pk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDsaPubKey;
408     pk->base.getAsyKeySpecInt = GetIntSpecFromDsaPubKey;
409     pk->base.getAsyKeySpecString = GetStrSpecFromDsaPubKey;
410 }
411 
FillOpensslDsaPriKeyFunc(HcfOpensslDsaPriKey * sk)412 static void FillOpensslDsaPriKeyFunc(HcfOpensslDsaPriKey *sk)
413 {
414     sk->base.base.base.destroy = DestroyDsaPriKey;
415     sk->base.base.base.getClass = GetDsaPriKeyClass;
416     sk->base.base.getAlgorithm = GetDsaPriKeyAlgorithm;
417     sk->base.base.getEncoded = GetDsaPriKeyEncoded;
418     sk->base.base.getFormat = GetDsaPriKeyFormat;
419     sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDsaPriKey;
420     sk->base.getAsyKeySpecInt = GetIntSpecFromDsaPriKey;
421     sk->base.getAsyKeySpecString = GetStrSpecFromDsaPriKey;
422     sk->base.clearMem = ClearDsaPriKeyMem;
423 }
424 
CreateDsaPubKey(DSA * pk,HcfOpensslDsaPubKey ** returnPubKey)425 static HcfResult CreateDsaPubKey(DSA *pk, HcfOpensslDsaPubKey **returnPubKey)
426 {
427     HcfOpensslDsaPubKey *dsaPubKey = (HcfOpensslDsaPubKey *)HcfMalloc(sizeof(HcfOpensslDsaPubKey), 0);
428     if (dsaPubKey == NULL) {
429         LOGE("Failed to allocate DSA public key memory.");
430         return HCF_ERR_MALLOC;
431     }
432     FillOpensslDsaPubKeyFunc(dsaPubKey);
433     dsaPubKey->pk = pk;
434 
435     *returnPubKey = dsaPubKey;
436     return HCF_SUCCESS;
437 }
438 
CreateDsaPriKey(DSA * sk,HcfOpensslDsaPriKey ** returnPriKey)439 static HcfResult CreateDsaPriKey(DSA *sk, HcfOpensslDsaPriKey **returnPriKey)
440 {
441     HcfOpensslDsaPriKey *dsaPriKey = (HcfOpensslDsaPriKey *)HcfMalloc(sizeof(HcfOpensslDsaPriKey), 0);
442     if (dsaPriKey == NULL) {
443         LOGE("Failed to allocate DSA private key memory.");
444         return HCF_ERR_MALLOC;
445     }
446     FillOpensslDsaPriKeyFunc(dsaPriKey);
447     dsaPriKey->sk = sk;
448 
449     *returnPriKey = dsaPriKey;
450     return HCF_SUCCESS;
451 }
452 
CreateDsaKeyPair(const HcfOpensslDsaPubKey * pubKey,const HcfOpensslDsaPriKey * priKey,HcfKeyPair ** returnKeyPair)453 static HcfResult CreateDsaKeyPair(const HcfOpensslDsaPubKey *pubKey, const HcfOpensslDsaPriKey *priKey,
454     HcfKeyPair **returnKeyPair)
455 {
456     HcfOpensslDsaKeyPair *keyPair = (HcfOpensslDsaKeyPair *)HcfMalloc(sizeof(HcfOpensslDsaKeyPair), 0);
457     if (keyPair == NULL) {
458         LOGE("Failed to allocate keyPair memory.");
459         return HCF_ERR_MALLOC;
460     }
461     keyPair->base.base.getClass = GetDsaKeyPairClass;
462     keyPair->base.base.destroy = DestroyDsaKeyPair;
463     keyPair->base.pubKey = (HcfPubKey *)pubKey;
464     keyPair->base.priKey = (HcfPriKey *)priKey;
465 
466     *returnKeyPair = (HcfKeyPair *)keyPair;
467     return HCF_SUCCESS;
468 }
469 
GeneratePubKeyByPkey(EVP_PKEY * pkey,HcfOpensslDsaPubKey ** returnPubKey)470 static HcfResult GeneratePubKeyByPkey(EVP_PKEY *pkey, HcfOpensslDsaPubKey **returnPubKey)
471 {
472     DSA *pk = Openssl_EVP_PKEY_get1_DSA(pkey);
473     if (pk == NULL) {
474         LOGE("Get das public key from pkey failed");
475         HcfPrintOpensslError();
476         return HCF_ERR_CRYPTO_OPERATION;
477     }
478     HcfResult ret = CreateDsaPubKey(pk, returnPubKey);
479     if (ret != HCF_SUCCESS) {
480         LOGE("Create DSA public key failed");
481         Openssl_DSA_free(pk);
482     }
483     return ret;
484 }
485 
GeneratePriKeyByPkey(EVP_PKEY * pkey,HcfOpensslDsaPriKey ** returnPriKey)486 static HcfResult GeneratePriKeyByPkey(EVP_PKEY *pkey, HcfOpensslDsaPriKey **returnPriKey)
487 {
488     DSA *sk = Openssl_EVP_PKEY_get1_DSA(pkey);
489     if (sk == NULL) {
490         LOGE("Get DSA private key from pkey failed");
491         HcfPrintOpensslError();
492         return HCF_ERR_CRYPTO_OPERATION;
493     }
494     HcfResult ret = CreateDsaPriKey(sk, returnPriKey);
495     if (ret != HCF_SUCCESS) {
496         LOGE("Create DSA private key failed");
497         Openssl_DSA_free(sk);
498     }
499     return ret;
500 }
501 
GenerateDsaPubAndPriKey(int32_t keyLen,HcfOpensslDsaPubKey ** returnPubKey,HcfOpensslDsaPriKey ** returnPriKey)502 static HcfResult GenerateDsaPubAndPriKey(int32_t keyLen, HcfOpensslDsaPubKey **returnPubKey,
503     HcfOpensslDsaPriKey **returnPriKey)
504 {
505     EVP_PKEY *pkey = NULL;
506     HcfResult ret = GenerateDsaEvpKey(keyLen, &pkey);
507     if (ret != HCF_SUCCESS) {
508         LOGE("Generate DSA EVP_PKEY failed.");
509         return ret;
510     }
511 
512     ret = GeneratePubKeyByPkey(pkey, returnPubKey);
513     if (ret != HCF_SUCCESS) {
514         Openssl_EVP_PKEY_free(pkey);
515         return ret;
516     }
517 
518     ret = GeneratePriKeyByPkey(pkey, returnPriKey);
519     if (ret != HCF_SUCCESS) {
520         HcfObjDestroy(*returnPubKey);
521         *returnPubKey = NULL;
522         Openssl_EVP_PKEY_free(pkey);
523         return HCF_ERR_CRYPTO_OPERATION;
524     }
525 
526     Openssl_EVP_PKEY_free(pkey);
527     return ret;
528 }
529 
ConvertCommSpec2Bn(const HcfDsaCommParamsSpec * paramsSpec,BIGNUM ** p,BIGNUM ** q,BIGNUM ** g)530 static HcfResult ConvertCommSpec2Bn(const HcfDsaCommParamsSpec *paramsSpec, BIGNUM **p, BIGNUM **q, BIGNUM **g)
531 {
532     if (BigIntegerToBigNum(&(paramsSpec->p), p) != HCF_SUCCESS) {
533         LOGE("Get openssl BN p failed");
534         return HCF_ERR_CRYPTO_OPERATION;
535     }
536     if (BigIntegerToBigNum(&(paramsSpec->q), q) != HCF_SUCCESS)  {
537         LOGE("Get openssl BN q failed");
538         Openssl_BN_free(*p);
539         *p = NULL;
540         return HCF_ERR_CRYPTO_OPERATION;
541     }
542     if (BigIntegerToBigNum(&(paramsSpec->g), g) != HCF_SUCCESS) {
543         LOGE("Get openssl BN g failed");
544         Openssl_BN_free(*p);
545         *p = NULL;
546         Openssl_BN_free(*q);
547         *q = NULL;
548         return HCF_ERR_CRYPTO_OPERATION;
549     }
550     return HCF_SUCCESS;
551 }
552 
CreateOpensslDsaKey(const HcfDsaCommParamsSpec * paramsSpec,BIGNUM * pk,BIGNUM * sk,DSA ** returnDsa)553 static HcfResult CreateOpensslDsaKey(const HcfDsaCommParamsSpec *paramsSpec, BIGNUM *pk, BIGNUM *sk, DSA **returnDsa)
554 {
555     BIGNUM *p = NULL;
556     BIGNUM *q = NULL;
557     BIGNUM *g = NULL;
558     if (ConvertCommSpec2Bn(paramsSpec, &p, &q, &g)!= HCF_SUCCESS) {
559         return HCF_ERR_CRYPTO_OPERATION;
560     }
561     DSA *dsa = Openssl_DSA_new();
562     if (dsa == NULL) {
563         FreeCommSpecBn(p, q, g);
564         LOGE("Openssl DSA new failed");
565         HcfPrintOpensslError();
566         return HCF_ERR_CRYPTO_OPERATION;
567     }
568     if (Openssl_DSA_set0_pqg(dsa, p, q, g) != HCF_OPENSSL_SUCCESS) {
569         LOGE("Openssl DSA set pqg failed");
570         FreeCommSpecBn(p, q, g);
571         HcfPrintOpensslError();
572         Openssl_DSA_free(dsa);
573         return HCF_ERR_CRYPTO_OPERATION;
574     }
575     if ((pk == NULL) && (sk == NULL)) {
576         *returnDsa = dsa;
577         return HCF_SUCCESS;
578     }
579     if (Openssl_DSA_set0_key(dsa, pk, sk) != HCF_OPENSSL_SUCCESS) {
580         LOGE("Openssl DSA set pqg failed");
581         HcfPrintOpensslError();
582         Openssl_DSA_free(dsa);
583         return HCF_ERR_CRYPTO_OPERATION;
584     }
585     *returnDsa = dsa;
586     return HCF_SUCCESS;
587 }
588 
GenerateOpensslDsaKeyByCommSpec(const HcfDsaCommParamsSpec * paramsSpec,DSA ** returnDsa)589 static HcfResult GenerateOpensslDsaKeyByCommSpec(const HcfDsaCommParamsSpec *paramsSpec, DSA **returnDsa)
590 {
591     if (CreateOpensslDsaKey(paramsSpec, NULL, NULL, returnDsa) != HCF_SUCCESS) {
592         return HCF_ERR_CRYPTO_OPERATION;
593     }
594 
595     if (Openssl_DSA_generate_key(*returnDsa) != HCF_OPENSSL_SUCCESS) {
596         LOGE("Openssl DSA generate key failed");
597         HcfPrintOpensslError();
598         Openssl_DSA_free(*returnDsa);
599         *returnDsa = NULL;
600         return HCF_ERR_CRYPTO_OPERATION;
601     }
602     return HCF_SUCCESS;
603 }
604 
GenerateOpensslDsaKeyByPubKeySpec(const HcfDsaPubKeyParamsSpec * paramsSpec,DSA ** returnDsa)605 static HcfResult GenerateOpensslDsaKeyByPubKeySpec(const HcfDsaPubKeyParamsSpec *paramsSpec, DSA **returnDsa)
606 {
607     BIGNUM *pubKey = NULL;
608     if (BigIntegerToBigNum(&(paramsSpec->pk), &pubKey) != HCF_SUCCESS) {
609         LOGE("Get openssl BN pk failed");
610         return HCF_ERR_CRYPTO_OPERATION;
611     }
612 
613     if (CreateOpensslDsaKey(&(paramsSpec->base), pubKey, NULL, returnDsa) != HCF_SUCCESS) {
614         Openssl_BN_free(pubKey);
615         return HCF_ERR_CRYPTO_OPERATION;
616     }
617     return HCF_SUCCESS;
618 }
619 
GenerateOpensslDsaKeyByKeyPairSpec(const HcfDsaKeyPairParamsSpec * paramsSpec,DSA ** returnDsa)620 static HcfResult GenerateOpensslDsaKeyByKeyPairSpec(const HcfDsaKeyPairParamsSpec *paramsSpec, DSA **returnDsa)
621 {
622     BIGNUM *pubKey = NULL;
623     BIGNUM *priKey = NULL;
624     if (BigIntegerToBigNum(&(paramsSpec->pk), &pubKey) != HCF_SUCCESS) {
625         LOGE("Get openssl BN pk failed");
626         return HCF_ERR_CRYPTO_OPERATION;
627     }
628     if (BigIntegerToBigNum(&(paramsSpec->sk), &priKey) != HCF_SUCCESS) {
629         LOGE("Get openssl BN sk failed");
630         Openssl_BN_free(pubKey);
631         return HCF_ERR_CRYPTO_OPERATION;
632     }
633     if (CreateOpensslDsaKey(&(paramsSpec->base), pubKey, priKey, returnDsa) != HCF_SUCCESS) {
634         Openssl_BN_free(pubKey);
635         Openssl_BN_free(priKey);
636         return HCF_ERR_CRYPTO_OPERATION;
637     }
638     return HCF_SUCCESS;
639 }
640 
CreateDsaKeyPairByCommSpec(const HcfDsaCommParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)641 static HcfResult CreateDsaKeyPairByCommSpec(const HcfDsaCommParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
642 {
643     DSA *dsa = NULL;
644     if (GenerateOpensslDsaKeyByCommSpec(paramsSpec, &dsa) != HCF_SUCCESS) {
645         return HCF_ERR_CRYPTO_OPERATION;
646     }
647     HcfOpensslDsaPubKey *pubKey = NULL;
648     if (CreateDsaPubKey(dsa, &pubKey) != HCF_SUCCESS) {
649         Openssl_DSA_free(dsa);
650         return HCF_ERR_MALLOC;
651     }
652 
653     if (Openssl_DSA_up_ref(dsa) != HCF_OPENSSL_SUCCESS) {
654         LOGE("Dup DSA failed.");
655         HcfPrintOpensslError();
656         HcfObjDestroy(pubKey);
657         return HCF_ERR_CRYPTO_OPERATION;
658     }
659 
660     HcfOpensslDsaPriKey *priKey = NULL;
661     if (CreateDsaPriKey(dsa, &priKey) != HCF_SUCCESS) {
662         Openssl_DSA_free(dsa);
663         HcfObjDestroy(pubKey);
664         return HCF_ERR_MALLOC;
665     }
666 
667     if (CreateDsaKeyPair(pubKey, priKey, returnKeyPair) != HCF_SUCCESS) {
668         HcfObjDestroy(pubKey);
669         HcfObjDestroy(priKey);
670         return HCF_ERR_MALLOC;
671     }
672     return HCF_SUCCESS;
673 }
674 
CreateDsaPubKeyByKeyPairSpec(const HcfDsaKeyPairParamsSpec * paramsSpec,HcfOpensslDsaPubKey ** returnPubKey)675 static HcfResult CreateDsaPubKeyByKeyPairSpec(const HcfDsaKeyPairParamsSpec *paramsSpec,
676     HcfOpensslDsaPubKey **returnPubKey)
677 {
678     DSA *dsa = NULL;
679     if (GenerateOpensslDsaKeyByKeyPairSpec(paramsSpec, &dsa) != HCF_SUCCESS) {
680         return HCF_ERR_CRYPTO_OPERATION;
681     }
682     if (CreateDsaPubKey(dsa, returnPubKey) != HCF_SUCCESS) {
683         Openssl_DSA_free(dsa);
684         return HCF_ERR_MALLOC;
685     }
686     return HCF_SUCCESS;
687 }
688 
CreateDsaPriKeyByKeyPairSpec(const HcfDsaKeyPairParamsSpec * paramsSpec,HcfOpensslDsaPriKey ** returnPriKey)689 static HcfResult CreateDsaPriKeyByKeyPairSpec(const HcfDsaKeyPairParamsSpec *paramsSpec,
690     HcfOpensslDsaPriKey **returnPriKey)
691 {
692     DSA *dsa = NULL;
693     if (GenerateOpensslDsaKeyByKeyPairSpec(paramsSpec, &dsa) != HCF_SUCCESS) {
694         return HCF_ERR_CRYPTO_OPERATION;
695     }
696     if (CreateDsaPriKey(dsa, returnPriKey) != HCF_SUCCESS) {
697         Openssl_DSA_free(dsa);
698         return HCF_ERR_MALLOC;
699     }
700     return HCF_SUCCESS;
701 }
702 
CreateDsaKeyPairByKeyPairSpec(const HcfDsaKeyPairParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)703 static HcfResult CreateDsaKeyPairByKeyPairSpec(const HcfDsaKeyPairParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
704 {
705     HcfOpensslDsaPubKey *pubKey = NULL;
706     HcfResult ret = CreateDsaPubKeyByKeyPairSpec(paramsSpec, &pubKey);
707     if (ret != HCF_SUCCESS) {
708         return ret;
709     }
710 
711     HcfOpensslDsaPriKey *priKey = NULL;
712     ret = CreateDsaPriKeyByKeyPairSpec(paramsSpec, &priKey);
713     if (ret != HCF_SUCCESS) {
714         HcfObjDestroy(pubKey);
715         return ret;
716     }
717     ret = CreateDsaKeyPair(pubKey, priKey, returnKeyPair);
718     if (ret != HCF_SUCCESS) {
719         HcfObjDestroy(pubKey);
720         HcfObjDestroy(priKey);
721         return ret;
722     }
723     return HCF_SUCCESS;
724 }
725 
CreateDsaKeyPairBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)726 static HcfResult CreateDsaKeyPairBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
727 {
728     if (paramsSpec->specType == HCF_COMMON_PARAMS_SPEC) {
729         return CreateDsaKeyPairByCommSpec((const HcfDsaCommParamsSpec *)paramsSpec, returnKeyPair);
730     } else {
731         return CreateDsaKeyPairByKeyPairSpec((const HcfDsaKeyPairParamsSpec *)paramsSpec, returnKeyPair);
732     }
733 }
734 
CreateDsaPubKeyByPubKeySpec(const HcfDsaPubKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)735 static HcfResult CreateDsaPubKeyByPubKeySpec(const HcfDsaPubKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
736 {
737     DSA *dsa = NULL;
738     if (GenerateOpensslDsaKeyByPubKeySpec(paramsSpec, &dsa) != HCF_SUCCESS) {
739         return HCF_ERR_CRYPTO_OPERATION;
740     }
741 
742     HcfOpensslDsaPubKey *pubKey = NULL;
743     if (CreateDsaPubKey(dsa, &pubKey) != HCF_SUCCESS) {
744         Openssl_DSA_free(dsa);
745         return HCF_ERR_MALLOC;
746     }
747     *returnPubKey = (HcfPubKey *)pubKey;
748     return HCF_SUCCESS;
749 }
750 
ConvertDsaPubKey(const HcfBlob * pubKeyBlob,HcfOpensslDsaPubKey ** returnPubKey)751 static HcfResult ConvertDsaPubKey(const HcfBlob *pubKeyBlob, HcfOpensslDsaPubKey **returnPubKey)
752 {
753     const unsigned char *tmpData = (const unsigned char *)(pubKeyBlob->data);
754     DSA *dsa = Openssl_d2i_DSA_PUBKEY(NULL, &tmpData, pubKeyBlob->len);
755     if (dsa == NULL) {
756         LOGE("D2i_DSA_PUBKEY fail.");
757         HcfPrintOpensslError();
758         return HCF_ERR_CRYPTO_OPERATION;
759     }
760     HcfResult ret = CreateDsaPubKey(dsa, returnPubKey);
761     if (ret != HCF_SUCCESS) {
762         LOGE("Create DSA public key failed");
763         Openssl_DSA_free(dsa);
764     }
765     return ret;
766 }
767 
ConvertDsaPriKey(const HcfBlob * priKeyBlob,HcfOpensslDsaPriKey ** returnPriKey)768 static HcfResult ConvertDsaPriKey(const HcfBlob *priKeyBlob, HcfOpensslDsaPriKey **returnPriKey)
769 {
770     const unsigned char *tmpData = (const unsigned char *)(priKeyBlob->data);
771     DSA *dsa = Openssl_d2i_DSAPrivateKey(NULL, &tmpData, priKeyBlob->len);
772     if (dsa == NULL) {
773         LOGE("D2i_DSADSAPrivateKey fail.");
774         HcfPrintOpensslError();
775         return HCF_ERR_CRYPTO_OPERATION;
776     }
777     HcfResult ret = CreateDsaPriKey(dsa, returnPriKey);
778     if (ret != HCF_SUCCESS) {
779         LOGE("Create DSA private key failed");
780         Openssl_DSA_free(dsa);
781     }
782     return ret;
783 }
784 
ConvertDsaPubAndPriKey(const HcfBlob * pubKeyBlob,const HcfBlob * priKeyBlob,HcfOpensslDsaPubKey ** returnPubKey,HcfOpensslDsaPriKey ** returnPriKey)785 static HcfResult ConvertDsaPubAndPriKey(const HcfBlob *pubKeyBlob, const HcfBlob *priKeyBlob,
786     HcfOpensslDsaPubKey **returnPubKey, HcfOpensslDsaPriKey **returnPriKey)
787 {
788     if (pubKeyBlob != NULL) {
789         if (ConvertDsaPubKey(pubKeyBlob, returnPubKey) != HCF_SUCCESS) {
790             LOGE("Convert DSA public key failed.");
791             return HCF_ERR_CRYPTO_OPERATION;
792         }
793     }
794     if (priKeyBlob != NULL) {
795         if (ConvertDsaPriKey(priKeyBlob, returnPriKey) != HCF_SUCCESS) {
796             LOGE("Convert DSA private key failed.");
797             HcfObjDestroy(*returnPubKey);
798             *returnPubKey = NULL;
799             return HCF_ERR_CRYPTO_OPERATION;
800         }
801     }
802     return HCF_SUCCESS;
803 }
804 
EngineGenerateDsaKeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnKeyPair)805 static HcfResult EngineGenerateDsaKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnKeyPair)
806 {
807     if (self == NULL || returnKeyPair == NULL) {
808         LOGE("Invalid params.");
809         return HCF_INVALID_PARAMS;
810     }
811     if (!IsClassMatch((HcfObjectBase *)self, GetDsaKeyGeneratorSpiClass())) {
812         LOGE("Class not match.");
813         return HCF_INVALID_PARAMS;
814     }
815     HcfAsyKeyGeneratorSpiDsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiDsaOpensslImpl *)self;
816 
817     HcfOpensslDsaPubKey *pubKey = NULL;
818     HcfOpensslDsaPriKey *priKey = NULL;
819     HcfResult ret = GenerateDsaPubAndPriKey(impl->bits, &pubKey, &priKey);
820     if (ret != HCF_SUCCESS) {
821         LOGE("Generate DSA pk and sk by openssl failed.");
822         return ret;
823     }
824 
825     ret = CreateDsaKeyPair(pubKey, priKey, returnKeyPair);
826     if (ret != HCF_SUCCESS) {
827         HcfObjDestroy(pubKey);
828         HcfObjDestroy(priKey);
829         return ret;
830     }
831     return HCF_SUCCESS;
832 }
833 
EngineConvertDsaKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)834 static HcfResult EngineConvertDsaKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
835     HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
836 {
837     (void)params;
838     if ((self == NULL) || (returnKeyPair == NULL)) {
839         LOGE("Invalid input parameter.");
840         return HCF_INVALID_PARAMS;
841     }
842     if (!IsClassMatch((HcfObjectBase *)self, GetDsaKeyGeneratorSpiClass())) {
843         LOGE("Class not match.");
844         return HCF_INVALID_PARAMS;
845     }
846     bool pubKeyValid = IsBlobValid(pubKeyBlob);
847     bool priKeyValid = IsBlobValid(priKeyBlob);
848     if ((!pubKeyValid) && (!priKeyValid)) {
849         LOGE("The private key and public key cannot both be NULL.");
850         return HCF_INVALID_PARAMS;
851     }
852 
853     HcfOpensslDsaPubKey *pubKey = NULL;
854     HcfOpensslDsaPriKey *priKey = NULL;
855     HcfBlob *inputPk = pubKeyValid ? pubKeyBlob : NULL;
856     HcfBlob *inputSk = priKeyValid ? priKeyBlob : NULL;
857     HcfResult ret = ConvertDsaPubAndPriKey(inputPk, inputSk, &pubKey, &priKey);
858     if (ret != HCF_SUCCESS) {
859         return ret;
860     }
861     ret = CreateDsaKeyPair(pubKey, priKey, returnKeyPair);
862     if (ret != HCF_SUCCESS) {
863         HcfObjDestroy(pubKey);
864         HcfObjDestroy(priKey);
865     }
866     return ret;
867 }
868 
EngineGenerateDsaKeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)869 static HcfResult EngineGenerateDsaKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self,
870     const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
871 {
872     if ((self == NULL) || (paramsSpec == NULL) || (returnKeyPair == NULL)) {
873         LOGE("Invalid input parameter.");
874         return HCF_INVALID_PARAMS;
875     }
876 
877     if (!IsClassMatch((HcfObjectBase *)self, GetDsaKeyGeneratorSpiClass())) {
878         return HCF_INVALID_PARAMS;
879     }
880 
881     if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DSA) != 0) ||
882         ((paramsSpec->specType != HCF_COMMON_PARAMS_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) {
883         LOGE("Invalid params spec.");
884         return HCF_INVALID_PARAMS;
885     }
886     HcfResult ret = CreateDsaKeyPairBySpec(paramsSpec, returnKeyPair);
887     if (ret != HCF_SUCCESS) {
888         LOGE("Create DSA key pair by spec falied.");
889     }
890     return ret;
891 }
892 
EngineGenerateDsaPubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)893 static HcfResult EngineGenerateDsaPubKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
894     const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
895 {
896     if ((self == NULL) || (paramsSpec == NULL) || (returnPubKey == NULL)) {
897         LOGE("Invalid input parameter.");
898         return HCF_INVALID_PARAMS;
899     }
900 
901     if (!IsClassMatch((HcfObjectBase *)self, GetDsaKeyGeneratorSpiClass())) {
902         return HCF_INVALID_PARAMS;
903     }
904 
905     if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DSA) != 0) ||
906         ((paramsSpec->specType != HCF_PUBLIC_KEY_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) {
907         LOGE("Invalid params spec.");
908         return HCF_INVALID_PARAMS;
909     }
910 
911     HcfResult ret = CreateDsaPubKeyByPubKeySpec((const HcfDsaPubKeyParamsSpec *)paramsSpec, returnPubKey);
912     if (ret != HCF_SUCCESS) {
913         LOGE("Create DSA public key by spec falied.");
914     }
915     return ret;
916 }
917 
EngineGenerateDsaPriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)918 static HcfResult EngineGenerateDsaPriKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
919     const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
920 {
921     if ((self == NULL) || (paramsSpec == NULL) || (returnPriKey == NULL)) {
922         LOGE("Invalid input parameter.");
923         return HCF_INVALID_PARAMS;
924     }
925     if (!IsClassMatch((HcfObjectBase *)self, GetDsaKeyGeneratorSpiClass())) {
926         return HCF_INVALID_PARAMS;
927     }
928     if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DSA) != 0) || (paramsSpec->specType != HCF_KEY_PAIR_SPEC)) {
929         LOGE("Invalid params spec.");
930         return HCF_INVALID_PARAMS;
931     }
932 
933     HcfOpensslDsaPriKey *dsaSk = NULL;
934     HcfResult ret = CreateDsaPriKeyByKeyPairSpec((const HcfDsaKeyPairParamsSpec *)paramsSpec, &dsaSk);
935     if (ret != HCF_SUCCESS) {
936         LOGE("Create DSA private key by spec falied.");
937     } else {
938         *returnPriKey = (HcfPriKey *)dsaSk;
939     }
940     return ret;
941 }
942 
HcfAsyKeyGeneratorSpiDsaCreate(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** returnSpi)943 HcfResult HcfAsyKeyGeneratorSpiDsaCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **returnSpi)
944 {
945     if (params == NULL || returnSpi == NULL) {
946         LOGE("Invalid input parameter.");
947         return HCF_INVALID_PARAMS;
948     }
949     HcfAsyKeyGeneratorSpiDsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiDsaOpensslImpl *)HcfMalloc(
950         sizeof(HcfAsyKeyGeneratorSpiDsaOpensslImpl), 0);
951     if (impl == NULL) {
952         LOGE("Failed to allocate generator impl memroy.");
953         return HCF_ERR_MALLOC;
954     }
955     impl->bits = params->bits;
956     impl->base.base.getClass = GetDsaKeyGeneratorSpiClass;
957     impl->base.base.destroy = DestroyDsaKeyGeneratorSpiImpl;
958     impl->base.engineGenerateKeyPair = EngineGenerateDsaKeyPair;
959     impl->base.engineConvertKey = EngineConvertDsaKey;
960     impl->base.engineGenerateKeyPairBySpec = EngineGenerateDsaKeyPairBySpec;
961     impl->base.engineGeneratePubKeyBySpec = EngineGenerateDsaPubKeyBySpec;
962     impl->base.engineGeneratePriKeyBySpec = EngineGenerateDsaPriKeyBySpec;
963 
964     *returnSpi = (HcfAsyKeyGeneratorSpi *)impl;
965     return HCF_SUCCESS;
966 }