• 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_openssl.h"
17 
18 #include <openssl/evp.h>
19 
20 #include "log.h"
21 #include "memory.h"
22 #include "openssl_adapter.h"
23 #include "openssl_common.h"
24 #include "openssl_class.h"
25 #include "utils.h"
26 
27 #define OPENSSL_DSA_SIGN_CLASS "OPENSSL.DSA.SIGN"
28 #define OPENSSL_DSA_VERIFY_CLASS "OPENSSL.DSA.VERIFY"
29 
30 typedef struct {
31     HcfSignSpi base;
32 
33     const EVP_MD *digestAlg;
34 
35     EVP_MD_CTX *mdCtx;
36 
37     EVP_PKEY_CTX *pkeyCtx;
38 
39     CryptoStatus status;
40 } HcfSignSpiDsaOpensslImpl;
41 
42 typedef struct {
43     HcfVerifySpi base;
44 
45     const EVP_MD *digestAlg;
46 
47     EVP_MD_CTX *mdCtx;
48 
49     EVP_PKEY_CTX *pkeyCtx;
50 
51     CryptoStatus status;
52 } HcfVerifySpiDsaOpensslImpl;
53 
GetDsaSignClass(void)54 static const char *GetDsaSignClass(void)
55 {
56     return OPENSSL_DSA_SIGN_CLASS;
57 }
58 
GetDsaVerifyClass(void)59 static const char *GetDsaVerifyClass(void)
60 {
61     return OPENSSL_DSA_VERIFY_CLASS;
62 }
63 
IsSignInitInputValid(HcfSignSpi * self,HcfPriKey * privateKey)64 static bool IsSignInitInputValid(HcfSignSpi *self, HcfPriKey *privateKey)
65 {
66     if ((self == NULL) || (privateKey == NULL)) {
67         LOGE("Invalid input parameter.");
68         return false;
69     }
70     if ((!IsClassMatch((HcfObjectBase *)self, GetDsaSignClass())) ||
71         (!IsClassMatch((HcfObjectBase *)privateKey, OPENSSL_DSA_PRIKEY_CLASS))) {
72         return false;
73     }
74     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)self;
75     if (impl->status != UNINITIALIZED) {
76         LOGE("Repeated initialization is not allowed.");
77         return false;
78     }
79     return true;
80 }
81 
IsVerifyInitInputValid(HcfVerifySpi * self,HcfPubKey * publicKey)82 static bool IsVerifyInitInputValid(HcfVerifySpi *self, HcfPubKey *publicKey)
83 {
84     if ((self == NULL) || (publicKey == NULL)) {
85         LOGE("Invalid input parameter.");
86         return false;
87     }
88     if ((!IsClassMatch((HcfObjectBase *)self, GetDsaVerifyClass())) ||
89         (!IsClassMatch((HcfObjectBase *)publicKey, OPENSSL_DSA_PUBKEY_CLASS))) {
90         return false;
91     }
92     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)self;
93     if (impl->status != UNINITIALIZED) {
94         LOGE("Repeated initialization is not allowed.");
95         return false;
96     }
97     return true;
98 }
99 
IsSignDoFinalInputValid(HcfSignSpi * self,HcfBlob * returnSignatureData)100 static bool IsSignDoFinalInputValid(HcfSignSpi *self, HcfBlob *returnSignatureData)
101 {
102     if ((self == NULL) || (returnSignatureData == NULL)) {
103         LOGE("Invalid input parameter.");
104         return false;
105     }
106     if (!IsClassMatch((HcfObjectBase *)self, GetDsaSignClass())) {
107         return false;
108     }
109     return true;
110 }
111 
IsVerifyDoFinalInputValid(HcfVerifySpi * self,HcfBlob * signatureData)112 static bool IsVerifyDoFinalInputValid(HcfVerifySpi *self, HcfBlob *signatureData)
113 {
114     if ((self == NULL) || (!IsBlobValid(signatureData))) {
115         LOGE("Invalid input parameter.");
116         return false;
117     }
118     if (!IsClassMatch((HcfObjectBase *)self, GetDsaVerifyClass())) {
119         return false;
120     }
121     return true;
122 }
123 
DestroyDsaSign(HcfObjectBase * self)124 static void DestroyDsaSign(HcfObjectBase *self)
125 {
126     if (self == NULL) {
127         return;
128     }
129 
130     if (!IsClassMatch(self, GetDsaSignClass())) {
131         return;
132     }
133     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)self;
134     if (impl->mdCtx != NULL) {
135         Openssl_EVP_MD_CTX_free(impl->mdCtx);
136         impl->mdCtx = NULL;
137     }
138     if (impl->pkeyCtx != NULL) {
139         Openssl_EVP_PKEY_CTX_free(impl->pkeyCtx);
140         impl->pkeyCtx = NULL;
141     }
142     HcfFree(impl);
143 }
144 
DestroyDsaVerify(HcfObjectBase * self)145 static void DestroyDsaVerify(HcfObjectBase *self)
146 {
147     if (self == NULL) {
148         return;
149     }
150     if (!IsClassMatch(self, GetDsaVerifyClass())) {
151         return;
152     }
153     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)self;
154     if (impl->mdCtx != NULL) {
155         Openssl_EVP_MD_CTX_free(impl->mdCtx);
156         impl->mdCtx = NULL;
157     }
158     if (impl->pkeyCtx != NULL) {
159         Openssl_EVP_PKEY_CTX_free(impl->pkeyCtx);
160         impl->pkeyCtx = NULL;
161     }
162     HcfFree(impl);
163 }
164 
CreateDsaEvpKeyByDsa(HcfKey * key,bool isSign)165 static EVP_PKEY *CreateDsaEvpKeyByDsa(HcfKey *key, bool isSign)
166 {
167     EVP_PKEY *pKey = Openssl_EVP_PKEY_new();
168     if (pKey == NULL) {
169         LOGE("EVP_PKEY_new fail");
170         HcfPrintOpensslError();
171         return NULL;
172     }
173     DSA *dsa = isSign ? ((HcfOpensslDsaPriKey *)key)->sk : ((HcfOpensslDsaPubKey *)key)->pk;
174     if (Openssl_EVP_PKEY_set1_DSA(pKey, dsa) != HCF_OPENSSL_SUCCESS) {
175         LOGE("EVP_PKEY_set1_DSA fail");
176         HcfPrintOpensslError();
177         EVP_PKEY_free(pKey);
178         return NULL;
179     }
180     return pKey;
181 }
182 
EngineDsaSignInit(HcfSignSpi * self,HcfParamsSpec * params,HcfPriKey * privateKey)183 static HcfResult EngineDsaSignInit(HcfSignSpi *self, HcfParamsSpec *params, HcfPriKey *privateKey)
184 {
185     (void)params;
186     if (!IsSignInitInputValid(self, privateKey)) {
187         return HCF_INVALID_PARAMS;
188     }
189     EVP_PKEY *pKey = CreateDsaEvpKeyByDsa((HcfKey *)privateKey, true);
190     if (pKey == NULL) {
191         LOGE("Create DSA evp key failed!");
192         return HCF_ERR_CRYPTO_OPERATION;
193     }
194     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)self;
195     if (Openssl_EVP_DigestSignInit(impl->mdCtx, NULL, impl->digestAlg, NULL, pKey) != HCF_OPENSSL_SUCCESS) {
196         HcfPrintOpensslError();
197         Openssl_EVP_PKEY_free(pKey);
198         return HCF_ERR_CRYPTO_OPERATION;
199     }
200 
201     Openssl_EVP_PKEY_free(pKey);
202     impl->status = INITIALIZED;
203     return HCF_SUCCESS;
204 }
205 
EngineDsaSignWithoutDigestInit(HcfSignSpi * self,HcfParamsSpec * params,HcfPriKey * privateKey)206 static HcfResult EngineDsaSignWithoutDigestInit(HcfSignSpi *self, HcfParamsSpec *params, HcfPriKey *privateKey)
207 {
208     (void)params;
209     if (!IsSignInitInputValid(self, privateKey)) {
210         return HCF_INVALID_PARAMS;
211     }
212     EVP_PKEY *pKey = CreateDsaEvpKeyByDsa((HcfKey *)privateKey, true);
213     if (pKey == NULL) {
214         LOGE("Create DSA evp key failed!");
215         return HCF_ERR_CRYPTO_OPERATION;
216     }
217     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)self;
218 
219     impl->pkeyCtx = Openssl_EVP_PKEY_CTX_new(pKey, NULL);
220     if (impl->pkeyCtx == NULL) {
221         HcfPrintOpensslError();
222         Openssl_EVP_PKEY_free(pKey);
223         return HCF_ERR_CRYPTO_OPERATION;
224     }
225     if (Openssl_EVP_PKEY_sign_init(impl->pkeyCtx) != HCF_OPENSSL_SUCCESS) {
226         HcfPrintOpensslError();
227         Openssl_EVP_PKEY_free(pKey);
228         Openssl_EVP_PKEY_CTX_free(impl->pkeyCtx);
229         impl->pkeyCtx = NULL;
230         return HCF_ERR_CRYPTO_OPERATION;
231     }
232     Openssl_EVP_PKEY_free(pKey);
233     impl->status = READY;
234     return HCF_SUCCESS;
235 }
236 
EngineDsaVerifyInit(HcfVerifySpi * self,HcfParamsSpec * params,HcfPubKey * publicKey)237 static HcfResult EngineDsaVerifyInit(HcfVerifySpi *self, HcfParamsSpec *params, HcfPubKey *publicKey)
238 {
239     (void)params;
240     if (!IsVerifyInitInputValid(self, publicKey)) {
241         return HCF_INVALID_PARAMS;
242     }
243     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)self;
244     EVP_PKEY *pKey = CreateDsaEvpKeyByDsa((HcfKey *)publicKey, false);
245     if (pKey == NULL) {
246         LOGE("Create DSA evp key failed!");
247         return HCF_ERR_CRYPTO_OPERATION;
248     }
249     if (Openssl_EVP_DigestVerifyInit(impl->mdCtx, NULL, impl->digestAlg, NULL, pKey) != HCF_OPENSSL_SUCCESS) {
250         HcfPrintOpensslError();
251         Openssl_EVP_PKEY_free(pKey);
252         return HCF_ERR_CRYPTO_OPERATION;
253     }
254 
255     Openssl_EVP_PKEY_free(pKey);
256     impl->status = INITIALIZED;
257     return HCF_SUCCESS;
258 }
259 
EngineDsaVerifyWithoutDigestInit(HcfVerifySpi * self,HcfParamsSpec * params,HcfPubKey * publicKey)260 static HcfResult EngineDsaVerifyWithoutDigestInit(HcfVerifySpi *self, HcfParamsSpec *params, HcfPubKey *publicKey)
261 {
262     (void)params;
263     if (!IsVerifyInitInputValid(self, publicKey)) {
264         return HCF_INVALID_PARAMS;
265     }
266     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)self;
267     EVP_PKEY *pKey = CreateDsaEvpKeyByDsa((HcfKey *)publicKey, false);
268     if (pKey == NULL) {
269         LOGE("Create dsa evp key failed!");
270         return HCF_ERR_CRYPTO_OPERATION;
271     }
272     impl->pkeyCtx = Openssl_EVP_PKEY_CTX_new(pKey, NULL);
273     if (impl->pkeyCtx == NULL) {
274         HcfPrintOpensslError();
275         Openssl_EVP_PKEY_free(pKey);
276         return HCF_ERR_CRYPTO_OPERATION;
277     }
278     if (Openssl_EVP_PKEY_verify_init(impl->pkeyCtx) != HCF_OPENSSL_SUCCESS) {
279         HcfPrintOpensslError();
280         Openssl_EVP_PKEY_free(pKey);
281         Openssl_EVP_PKEY_CTX_free(impl->pkeyCtx);
282         impl->pkeyCtx = NULL;
283         return HCF_ERR_CRYPTO_OPERATION;
284     }
285     Openssl_EVP_PKEY_free(pKey);
286     impl->status = READY;
287     return HCF_SUCCESS;
288 }
289 
EngineDsaSignUpdate(HcfSignSpi * self,HcfBlob * data)290 static HcfResult EngineDsaSignUpdate(HcfSignSpi *self, HcfBlob *data)
291 {
292     if ((self == NULL) || (!IsBlobValid(data))) {
293         LOGE("Invalid input parameter.");
294         return HCF_INVALID_PARAMS;
295     }
296     if (!IsClassMatch((HcfObjectBase *)self, GetDsaSignClass())) {
297         return HCF_INVALID_PARAMS;
298     }
299     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)self;
300     if (impl->status == UNINITIALIZED) {
301         LOGE("Sign object has not been initialized.");
302         return HCF_INVALID_PARAMS;
303     }
304     if (Openssl_EVP_DigestSignUpdate(impl->mdCtx, data->data, data->len) != HCF_OPENSSL_SUCCESS) {
305         HcfPrintOpensslError();
306         return HCF_ERR_CRYPTO_OPERATION;
307     }
308     impl->status = READY;
309     return HCF_SUCCESS;
310 }
311 
EngineDsaSignWithoutDigestUpdate(HcfSignSpi * self,HcfBlob * data)312 static HcfResult EngineDsaSignWithoutDigestUpdate(HcfSignSpi *self, HcfBlob *data)
313 {
314     (void)self;
315     (void)data;
316     return HCF_SUCCESS;
317 }
318 
EngineDsaVerifyUpdate(HcfVerifySpi * self,HcfBlob * data)319 static HcfResult EngineDsaVerifyUpdate(HcfVerifySpi *self, HcfBlob *data)
320 {
321     if ((self == NULL) || (!IsBlobValid(data))) {
322         LOGE("Invalid input parameter.");
323         return HCF_INVALID_PARAMS;
324     }
325     if (!IsClassMatch((HcfObjectBase *)self, GetDsaVerifyClass())) {
326         return HCF_INVALID_PARAMS;
327     }
328     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)self;
329     if (impl->status == UNINITIALIZED) {
330         LOGE("Verify object has not been initialized.");
331         return HCF_INVALID_PARAMS;
332     }
333 
334     if (Openssl_EVP_DigestVerifyUpdate(impl->mdCtx, data->data, data->len) != HCF_OPENSSL_SUCCESS) {
335         HcfPrintOpensslError();
336         return HCF_ERR_CRYPTO_OPERATION;
337     }
338     impl->status = READY;
339     return HCF_SUCCESS;
340 }
341 
EngineDsaVerifyWithoutDigestUpdate(HcfVerifySpi * self,HcfBlob * data)342 static HcfResult EngineDsaVerifyWithoutDigestUpdate(HcfVerifySpi *self, HcfBlob *data)
343 {
344     (void)self;
345     (void)data;
346     return HCF_SUCCESS;
347 }
348 
EngineDsaSignDoFinal(HcfSignSpi * self,HcfBlob * data,HcfBlob * returnSignatureData)349 static HcfResult EngineDsaSignDoFinal(HcfSignSpi *self, HcfBlob *data, HcfBlob *returnSignatureData)
350 {
351     if (!IsSignDoFinalInputValid(self, returnSignatureData)) {
352         return HCF_INVALID_PARAMS;
353     }
354     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)self;
355     if (IsBlobValid(data)) {
356         if (Openssl_EVP_DigestSignUpdate(impl->mdCtx, data->data, data->len) != HCF_OPENSSL_SUCCESS) {
357             HcfPrintOpensslError();
358             return HCF_ERR_CRYPTO_OPERATION;
359         }
360         impl->status = READY;
361     }
362     if (impl->status != READY) {
363         LOGE("The message has not been transferred.");
364         return HCF_INVALID_PARAMS;
365     }
366     size_t maxLen;
367     if (Openssl_EVP_DigestSignFinal(impl->mdCtx, NULL, &maxLen) != HCF_OPENSSL_SUCCESS) {
368         HcfPrintOpensslError();
369         return HCF_ERR_CRYPTO_OPERATION;
370     }
371     uint8_t *signatureData = (uint8_t *)HcfMalloc(maxLen, 0);
372     if (signatureData == NULL) {
373         LOGE("Failed to allocate signatureData memory!");
374         return HCF_ERR_MALLOC;
375     }
376     size_t actualLen = maxLen;
377     if (Openssl_EVP_DigestSignFinal(impl->mdCtx, signatureData, &actualLen) != HCF_OPENSSL_SUCCESS) {
378         HcfPrintOpensslError();
379         HcfFree(signatureData);
380         return HCF_ERR_CRYPTO_OPERATION;
381     }
382     if (actualLen > maxLen) {
383         LOGE("Signature data too long.");
384         HcfFree(signatureData);
385         return HCF_ERR_CRYPTO_OPERATION;
386     }
387 
388     returnSignatureData->data = signatureData;
389     returnSignatureData->len = (uint32_t)actualLen;
390     return HCF_SUCCESS;
391 }
392 
EngineDsaSignWithoutDigestDoFinal(HcfSignSpi * self,HcfBlob * data,HcfBlob * returnSignatureData)393 static HcfResult EngineDsaSignWithoutDigestDoFinal(HcfSignSpi *self, HcfBlob *data, HcfBlob *returnSignatureData)
394 {
395     if (!IsSignDoFinalInputValid(self, returnSignatureData)) {
396         return HCF_INVALID_PARAMS;
397     }
398     if (!IsBlobValid(data)) {
399         LOGE("Src data is invalid.");
400         return HCF_INVALID_PARAMS;
401     }
402     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)self;
403     if (impl->status != READY) {
404         LOGE("Not init yet.");
405         return HCF_INVALID_PARAMS;
406     }
407     size_t maxLen;
408     if (Openssl_EVP_PKEY_sign(impl->pkeyCtx, NULL, &maxLen,
409         (const unsigned char *)data->data, data->len) != HCF_OPENSSL_SUCCESS) {
410         HcfPrintOpensslError();
411         return HCF_ERR_CRYPTO_OPERATION;
412     }
413     uint8_t *signatureData = (uint8_t *)HcfMalloc(maxLen, 0);
414     if (signatureData == NULL) {
415         LOGE("Failed to allocate signatureData memory!");
416         return HCF_ERR_MALLOC;
417     }
418     size_t actualLen = maxLen;
419     if (Openssl_EVP_PKEY_sign(impl->pkeyCtx, signatureData, &actualLen,
420         (const unsigned char *)data->data, data->len) != HCF_OPENSSL_SUCCESS) {
421         HcfPrintOpensslError();
422         HcfFree(signatureData);
423         return HCF_ERR_CRYPTO_OPERATION;
424     }
425     if (actualLen > maxLen) {
426         LOGE("Signature data too long.");
427         HcfFree(signatureData);
428         return HCF_ERR_CRYPTO_OPERATION;
429     }
430 
431     returnSignatureData->data = signatureData;
432     returnSignatureData->len = (uint32_t)actualLen;
433     return HCF_SUCCESS;
434 }
435 
EngineDsaVerifyDoFinal(HcfVerifySpi * self,HcfBlob * data,HcfBlob * signatureData)436 static bool EngineDsaVerifyDoFinal(HcfVerifySpi *self, HcfBlob *data, HcfBlob *signatureData)
437 {
438     if (!IsVerifyDoFinalInputValid(self, signatureData)) {
439         return false;
440     }
441 
442     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)self;
443     if (IsBlobValid(data)) {
444         if (Openssl_EVP_DigestVerifyUpdate(impl->mdCtx, data->data, data->len) != HCF_OPENSSL_SUCCESS) {
445             LOGE("Openssl update failed.");
446             HcfPrintOpensslError();
447             return false;
448         }
449         impl->status = READY;
450     }
451     if (impl->status != READY) {
452         LOGE("The message has not been transferred.");
453         return false;
454     }
455 
456     if (Openssl_EVP_DigestVerifyFinal(impl->mdCtx, signatureData->data, signatureData->len) != HCF_OPENSSL_SUCCESS) {
457         HcfPrintOpensslError();
458         return false;
459     }
460     return true;
461 }
462 
EngineDsaVerifyWithoutDigestDoFinal(HcfVerifySpi * self,HcfBlob * data,HcfBlob * signatureData)463 static bool EngineDsaVerifyWithoutDigestDoFinal(HcfVerifySpi *self, HcfBlob *data, HcfBlob *signatureData)
464 {
465     if (!IsVerifyDoFinalInputValid(self, signatureData)) {
466         return false;
467     }
468     if (!IsBlobValid(data)) {
469         LOGE("Src data is invalid.");
470         return false;
471     }
472     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)self;
473     if (impl->status != READY) {
474         LOGE("Not init yet.");
475         return false;
476     }
477 
478     if (Openssl_EVP_PKEY_verify(impl->pkeyCtx, signatureData->data,
479         signatureData->len, data->data, data->len) != HCF_OPENSSL_SUCCESS) {
480         HcfPrintOpensslError();
481         return false;
482     }
483     return true;
484 }
485 
EngineSetSignDsaSpecInt(HcfSignSpi * self,SignSpecItem item,int32_t saltLen)486 HcfResult EngineSetSignDsaSpecInt(HcfSignSpi *self, SignSpecItem item, int32_t saltLen)
487 {
488     (void)self;
489     (void)item;
490     (void)saltLen;
491     return HCF_NOT_SUPPORT;
492 }
493 
EngineSetVerifyDsaSpecInt(HcfVerifySpi * self,SignSpecItem item,int32_t saltLen)494 HcfResult EngineSetVerifyDsaSpecInt(HcfVerifySpi *self, SignSpecItem item, int32_t saltLen)
495 {
496     (void)self;
497     (void)item;
498     (void)saltLen;
499     return HCF_NOT_SUPPORT;
500 }
501 
EngineGetSignDsaSpecInt(HcfSignSpi * self,SignSpecItem item,int32_t * returnInt)502 HcfResult EngineGetSignDsaSpecInt(HcfSignSpi *self, SignSpecItem item, int32_t *returnInt)
503 {
504     (void)self;
505     (void)item;
506     (void)returnInt;
507     return HCF_NOT_SUPPORT;
508 }
509 
EngineGetVerifyDsaSpecInt(HcfVerifySpi * self,SignSpecItem item,int32_t * returnInt)510 HcfResult EngineGetVerifyDsaSpecInt(HcfVerifySpi *self, SignSpecItem item, int32_t *returnInt)
511 {
512     (void)self;
513     (void)item;
514     (void)returnInt;
515     return HCF_NOT_SUPPORT;
516 }
517 
EngineGetSignDsaSpecString(HcfSignSpi * self,SignSpecItem item,char ** returnString)518 HcfResult EngineGetSignDsaSpecString(HcfSignSpi *self, SignSpecItem item, char **returnString)
519 {
520     (void)self;
521     (void)item;
522     (void)returnString;
523     return HCF_NOT_SUPPORT;
524 }
525 
EngineGetVerifyDsaSpecString(HcfVerifySpi * self,SignSpecItem item,char ** returnString)526 HcfResult EngineGetVerifyDsaSpecString(HcfVerifySpi *self, SignSpecItem item, char **returnString)
527 {
528     (void)self;
529     (void)item;
530     (void)returnString;
531     return HCF_NOT_SUPPORT;
532 }
533 
HcfSignSpiDsaCreate(HcfSignatureParams * params,HcfSignSpi ** returnObj)534 HcfResult HcfSignSpiDsaCreate(HcfSignatureParams *params, HcfSignSpi **returnObj)
535 {
536     if ((params == NULL) || (returnObj == NULL)) {
537         LOGE("Invalid input parameter.");
538         return HCF_INVALID_PARAMS;
539     }
540 
541     HcfSignSpiDsaOpensslImpl *impl = (HcfSignSpiDsaOpensslImpl *)HcfMalloc(sizeof(HcfSignSpiDsaOpensslImpl), 0);
542     if (impl == NULL) {
543         LOGE("Failed to allocate impl memroy!");
544         return HCF_ERR_MALLOC;
545     }
546 
547     EVP_MD *digestAlg = NULL;
548 
549     if (params->md == HCF_OPENSSL_DIGEST_NONE) {
550         impl->base.engineInit = EngineDsaSignWithoutDigestInit;
551         impl->base.engineUpdate = EngineDsaSignWithoutDigestUpdate;
552         impl->base.engineSign = EngineDsaSignWithoutDigestDoFinal;
553     } else {
554         HcfResult ret = GetOpensslDigestAlg(params->md, &digestAlg);
555         if (ret != HCF_SUCCESS) {
556             HcfFree(impl);
557             return HCF_INVALID_PARAMS;
558         }
559         impl->base.engineInit = EngineDsaSignInit;
560         impl->base.engineUpdate = EngineDsaSignUpdate;
561         impl->base.engineSign = EngineDsaSignDoFinal;
562         impl->mdCtx = Openssl_EVP_MD_CTX_new();
563         if (impl->mdCtx == NULL) {
564             LOGE("Failed to allocate ctx memory!");
565             HcfFree(impl);
566             return HCF_ERR_MALLOC;
567         }
568     }
569     impl->base.base.getClass = GetDsaSignClass;
570     impl->base.base.destroy = DestroyDsaSign;
571     impl->base.engineSetSignSpecInt = EngineSetSignDsaSpecInt;
572     impl->base.engineGetSignSpecInt = EngineGetSignDsaSpecInt;
573     impl->base.engineGetSignSpecString = EngineGetSignDsaSpecString;
574     impl->status = UNINITIALIZED;
575     impl->digestAlg = digestAlg;
576     *returnObj = (HcfSignSpi *)impl;
577     return HCF_SUCCESS;
578 }
579 
HcfVerifySpiDsaCreate(HcfSignatureParams * params,HcfVerifySpi ** returnObj)580 HcfResult HcfVerifySpiDsaCreate(HcfSignatureParams *params, HcfVerifySpi **returnObj)
581 {
582     if ((params == NULL) || (returnObj == NULL)) {
583         LOGE("Invalid input parameter.");
584         return HCF_INVALID_PARAMS;
585     }
586 
587     HcfVerifySpiDsaOpensslImpl *impl = (HcfVerifySpiDsaOpensslImpl *)HcfMalloc(sizeof(HcfVerifySpiDsaOpensslImpl), 0);
588     if (impl == NULL) {
589         LOGE("Failed to allocate impl memroy!");
590         return HCF_ERR_MALLOC;
591     }
592 
593     EVP_MD *digestAlg = NULL;
594     if (params->md == HCF_OPENSSL_DIGEST_NONE) {
595         impl->base.engineInit = EngineDsaVerifyWithoutDigestInit;
596         impl->base.engineUpdate = EngineDsaVerifyWithoutDigestUpdate;
597         impl->base.engineVerify = EngineDsaVerifyWithoutDigestDoFinal;
598     } else {
599         HcfResult ret = GetOpensslDigestAlg(params->md, &digestAlg);
600         if (ret != HCF_SUCCESS) {
601             return HCF_INVALID_PARAMS;
602         }
603         impl->base.engineInit = EngineDsaVerifyInit;
604         impl->base.engineUpdate = EngineDsaVerifyUpdate;
605         impl->base.engineVerify = EngineDsaVerifyDoFinal;
606         impl->mdCtx = Openssl_EVP_MD_CTX_new();
607         if (impl->mdCtx == NULL) {
608             LOGE("Failed to allocate ctx memory!");
609             HcfFree(impl);
610             return HCF_ERR_MALLOC;
611         }
612     }
613     impl->base.base.getClass = GetDsaVerifyClass;
614     impl->base.base.destroy = DestroyDsaVerify;
615     impl->base.engineSetVerifySpecInt = EngineSetVerifyDsaSpecInt;
616     impl->base.engineGetVerifySpecInt = EngineGetVerifyDsaSpecInt;
617     impl->base.engineGetVerifySpecString = EngineGetVerifyDsaSpecString;
618     impl->digestAlg = digestAlg;
619     impl->status = UNINITIALIZED;
620 
621     *returnObj = (HcfVerifySpi *)impl;
622     return HCF_SUCCESS;
623 }