• 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 <gtest/gtest.h>
17 #include <cstring>
18 
19 #include "alg_25519_common_param_spec.h"
20 #include "asy_key_generator.h"
21 #include "blob.h"
22 #include "dh_key_util.h"
23 #include "ecdsa_openssl.h"
24 #include "memory.h"
25 #include "securec.h"
26 #include "openssl_common.h"
27 #include "asy_key_params.h"
28 #include "params_parser.h"
29 #include "dh_asy_key_generator_openssl.h"
30 #include "detailed_dh_key_params.h"
31 #include "alg_25519_asy_key_generator_openssl.h"
32 #include "dh_common_param_spec_generator_openssl.h"
33 #include "memory_mock.h"
34 #include "openssl_adapter_mock.h"
35 #include "detailed_ecc_key_params.h"
36 #include "sm2_crypto_params.h"
37 
38 using namespace std;
39 using namespace testing::ext;
40 
41 namespace {
42 constexpr int SKLEN_DH128 = 128;
43 constexpr int SKLEN_DH223 = 223;
44 constexpr int SKLEN_DH255 = 255;
45 constexpr int SKLEN_DH303 = 303;
46 constexpr int SKLEN_DH351 = 351;
47 constexpr int SKLEN_DH399 = 399;
48 constexpr int SKLEN_DH1024 = 1024;
49 constexpr int SKLEN_EQZERO = 0;
50 constexpr int PLEN_DH511 = 511;
51 constexpr int PLEN_DH512 = 512;
52 constexpr int PLEN_DH1536 = 1536;
53 constexpr int PLEN_DH2048 = 2048;
54 constexpr int PLEN_DH3072 = 3072;
55 constexpr int PLEN_DH4096 = 4096;
56 constexpr int PLEN_DH6144 = 6144;
57 constexpr int PLEN_DH8192 = 8192;
58 constexpr int PLEN_DH10001 = 10001;
59 constexpr int PLEN_LTSK = 20;
60 
61 class CryptoDHAsyKeyGeneratorBySpecTest : public testing::Test {
62 public:
63     static void SetUpTestCase();
64     static void TearDownTestCase();
65     void SetUp();
66     void TearDown();
67 };
68 
69 static string g_dh1536AlgoName = "DH_modp1536";
70 static string g_dh2048AlgoName = "DH_modp2048";
71 static string g_dh3072AlgoName = "DH_modp3072";
72 static string g_dh4096AlgoName = "DH_modp4096";
73 static string g_dh6144AlgoName = "DH_modp6144";
74 static string g_dh8192AlgoName = "DH_modp8192";
75 
76 static string g_dhAlgoName = "DH";
77 static string g_dhpubkeyformatName = "X.509";
78 static string g_dhprikeyformatName = "PKCS#8";
79 
80 HcfDhCommParamsSpec *g_dh1536CommSpec = nullptr;
81 HcfDhCommParamsSpec *g_dh2048CommSpec = nullptr;
82 HcfDhCommParamsSpec *g_dh3072CommSpec = nullptr;
83 HcfDhCommParamsSpec *g_dh4096CommSpec = nullptr;
84 HcfDhCommParamsSpec *g_dh6144CommSpec = nullptr;
85 HcfDhCommParamsSpec *g_dh8192CommSpec = nullptr;
86 HcfKeyPair *g_dhKeyPair = nullptr;
87 
SetUp()88 void CryptoDHAsyKeyGeneratorBySpecTest::SetUp() {}
TearDown()89 void CryptoDHAsyKeyGeneratorBySpecTest::TearDown() {}
90 
GetMockClass(void)91 static const char *GetMockClass(void)
92 {
93     return "HcfEcc";
94 }
95 HcfObjectBase g_obj = {
96     .getClass = GetMockClass,
97     .destroy = nullptr
98 };
99 
DestroyTestDhPriKeySpec(HcfDhPriKeyParamsSpec * spec)100 static void DestroyTestDhPriKeySpec(HcfDhPriKeyParamsSpec *spec)
101 {
102     if (spec == nullptr) {
103         return;
104     }
105     if (spec->sk.data != nullptr) {
106         (void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len);
107         HcfFree(spec->sk.data);
108         spec->sk.data = nullptr;
109     }
110     HcfFree(spec);
111 }
112 
DestroyTestDhPubKeySpec(HcfDhPubKeyParamsSpec * spec)113 static void DestroyTestDhPubKeySpec(HcfDhPubKeyParamsSpec *spec)
114 {
115     if (spec == nullptr) {
116         return;
117     }
118     HcfFree(spec->pk.data);
119     spec->pk.data = nullptr;
120     HcfFree(spec);
121 }
122 
DestroyTestDhKeyPairSpec(HcfDhKeyPairParamsSpec * spec)123 static void DestroyTestDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec)
124 {
125     if (spec == nullptr) {
126         return;
127     }
128     HcfFree(spec->pk.data);
129     spec->pk.data = nullptr;
130     if (spec->sk.data != nullptr) {
131         (void)memset_s(spec->sk.data, spec->sk.len, 0, spec->sk.len);
132         HcfFree(spec->sk.data);
133         spec->sk.data = nullptr;
134     }
135     HcfFree(spec);
136 }
137 
ConstructDHKeyCommParamsSpec(int32_t pLen,int32_t skLen,HcfDhCommParamsSpec ** spec)138 static HcfResult ConstructDHKeyCommParamsSpec(int32_t pLen, int32_t skLen, HcfDhCommParamsSpec **spec)
139 {
140     HcfDhCommParamsSpec *dhCommSpec = nullptr;
141     HcfResult res = HcfDhKeyUtilCreate(pLen, skLen, &dhCommSpec);
142     if (res != HCF_SUCCESS) {
143         return HCF_INVALID_PARAMS;
144     }
145     *spec = dhCommSpec;
146     return HCF_SUCCESS;
147 }
148 
ConstructDhKeyPairBigInt(HcfKeyPair * keyPair,HcfDhKeyPairParamsSpec * dhKeyPairSpec)149 static HcfResult ConstructDhKeyPairBigInt(HcfKeyPair *keyPair, HcfDhKeyPairParamsSpec *dhKeyPairSpec)
150 {
151     HcfBigInteger retBigInt = { .data = nullptr, .len = 0 };
152     HcfResult res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_PK_BN, &retBigInt);
153     if (res != HCF_SUCCESS) {
154         return res;
155     }
156     dhKeyPairSpec->pk.data = retBigInt.data;
157     dhKeyPairSpec->pk.len = retBigInt.len;
158 
159     res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DH_SK_BN, &retBigInt);
160     if (res != HCF_SUCCESS) {
161         HcfFree(dhKeyPairSpec->pk.data);
162         dhKeyPairSpec->pk.data = nullptr;
163         dhKeyPairSpec->pk.len = 0;
164         return res;
165     }
166     dhKeyPairSpec->sk.data = retBigInt.data;
167     dhKeyPairSpec->sk.len = retBigInt.len;
168     return HCF_SUCCESS;
169 }
170 
ConstructDHKeyPairParamsCommonSpec(const std::string & algoName,HcfDhCommParamsSpec * dhCommParamsSpec,HcfAsyKeyParamsSpec ** spec)171 static HcfResult ConstructDHKeyPairParamsCommonSpec(const std::string &algoName,
172     HcfDhCommParamsSpec *dhCommParamsSpec, HcfAsyKeyParamsSpec **spec)
173 {
174     HcfAsyKeyGenerator *generator = nullptr;
175     HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator);
176     if (res != HCF_SUCCESS) {
177         return res;
178     }
179 
180     HcfKeyPair *keyPair = nullptr;
181     res = generator->generateKeyPair(generator, nullptr, &keyPair);
182     if (res != HCF_SUCCESS) {
183         HcfObjDestroy(generator);
184         return res;
185     }
186 
187     HcfDhKeyPairParamsSpec *dhKeyPairSpec =
188         reinterpret_cast<HcfDhKeyPairParamsSpec*>(HcfMalloc(sizeof(HcfDhKeyPairParamsSpec), 0));
189     if (dhKeyPairSpec != nullptr) {
190         dhKeyPairSpec->base.base.algName = dhCommParamsSpec->base.algName;
191         dhKeyPairSpec->base.base.specType = HCF_COMMON_PARAMS_SPEC;
192         dhKeyPairSpec->base.p.data = dhCommParamsSpec->p.data;
193         dhKeyPairSpec->base.p.len = dhCommParamsSpec->p.len;
194         dhKeyPairSpec->base.g.data = dhCommParamsSpec->g.data;
195         dhKeyPairSpec->base.g.len = dhCommParamsSpec->g.len;
196         dhKeyPairSpec->base.length = dhCommParamsSpec->length;
197         res = ConstructDhKeyPairBigInt(keyPair, dhKeyPairSpec);
198         if (res != HCF_SUCCESS) {
199             HcfObjDestroy(generator);
200             HcfObjDestroy(keyPair);
201             HcfFree(dhKeyPairSpec);
202             return res;
203         }
204         *spec = reinterpret_cast<HcfAsyKeyParamsSpec *>(dhKeyPairSpec);
205     }
206 
207     HcfObjDestroy(generator);
208     HcfObjDestroy(keyPair);
209     return HCF_SUCCESS;
210 }
211 
ConstructDHKeyPairParamsSpec(const std::string & algoName,HcfDhCommParamsSpec * dhCommParamsSpec,HcfAsyKeyParamsSpec ** spec)212 static HcfResult ConstructDHKeyPairParamsSpec(const std::string &algoName,
213     HcfDhCommParamsSpec *dhCommParamsSpec, HcfAsyKeyParamsSpec **spec)
214 {
215     HcfAsyKeyGenerator *generator = nullptr;
216     HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator);
217     if (res != HCF_SUCCESS) {
218         return res;
219     }
220 
221     HcfKeyPair *keyPair = nullptr;
222     res = generator->generateKeyPair(generator, nullptr, &keyPair);
223     if (res != HCF_SUCCESS) {
224         HcfObjDestroy(generator);
225         return res;
226     }
227 
228     HcfDhKeyPairParamsSpec *dhKeyPairSpec =
229         reinterpret_cast<HcfDhKeyPairParamsSpec*>(HcfMalloc(sizeof(HcfDhKeyPairParamsSpec), 0));
230     if (dhKeyPairSpec != nullptr) {
231         dhKeyPairSpec->base.base.algName = dhCommParamsSpec->base.algName;
232         dhKeyPairSpec->base.base.specType = HCF_KEY_PAIR_SPEC;
233         dhKeyPairSpec->base.p.data = dhCommParamsSpec->p.data;
234         dhKeyPairSpec->base.p.len = dhCommParamsSpec->p.len;
235         dhKeyPairSpec->base.g.data = dhCommParamsSpec->g.data;
236         dhKeyPairSpec->base.g.len = dhCommParamsSpec->g.len;
237         dhKeyPairSpec->base.length = dhCommParamsSpec->length;
238         res = ConstructDhKeyPairBigInt(keyPair, dhKeyPairSpec);
239         if (res != HCF_SUCCESS) {
240             HcfObjDestroy(generator);
241             HcfObjDestroy(keyPair);
242             HcfFree(dhKeyPairSpec);
243             return res;
244         }
245         *spec = reinterpret_cast<HcfAsyKeyParamsSpec *>(dhKeyPairSpec);
246     }
247 
248     HcfObjDestroy(generator);
249     HcfObjDestroy(keyPair);
250     return HCF_SUCCESS;
251 }
252 
ConstructDHPubKeyParamsSpec(const string & algoName,HcfDhCommParamsSpec * comSpec,HcfAsyKeyParamsSpec ** spec)253 static HcfResult ConstructDHPubKeyParamsSpec(const string &algoName, HcfDhCommParamsSpec *comSpec,
254     HcfAsyKeyParamsSpec **spec)
255 {
256     if (comSpec == nullptr) {
257         return HCF_INVALID_PARAMS;
258     }
259     HcfDhPubKeyParamsSpec *dhPubKeySpec = (HcfDhPubKeyParamsSpec*)HcfMalloc(sizeof(HcfDhPubKeyParamsSpec), 0);
260     if (dhPubKeySpec == nullptr) {
261         return HCF_ERR_MALLOC;
262     }
263     HcfAsyKeyGenerator *generator = nullptr;
264     HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator);
265     if (res != HCF_SUCCESS) {
266         HcfFree(dhPubKeySpec);
267         return res;
268     }
269     HcfKeyPair *keyPair = nullptr;
270     res = generator->generateKeyPair(generator, nullptr, &keyPair);
271     if (res != HCF_SUCCESS) {
272         HcfObjDestroy(generator);
273         HcfFree(dhPubKeySpec);
274         return res;
275     }
276     HcfBigInteger retBigInt = { .data = nullptr, .len = 0 };
277     dhPubKeySpec->base.base.algName = comSpec->base.algName;
278     dhPubKeySpec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
279     dhPubKeySpec->base.g = comSpec->g;
280     dhPubKeySpec->base.length = comSpec->length;
281     dhPubKeySpec->base.p = comSpec->p;
282     res = keyPair->pubKey->getAsyKeySpecBigInteger(keyPair->pubKey, DH_PK_BN, &retBigInt);
283     if (res != HCF_SUCCESS) {
284         HcfObjDestroy(generator);
285         HcfObjDestroy(keyPair);
286         HcfFree(dhPubKeySpec);
287         return res;
288     }
289     dhPubKeySpec->pk.data = retBigInt.data;
290     dhPubKeySpec->pk.len = retBigInt.len;
291 
292     *spec = reinterpret_cast<HcfAsyKeyParamsSpec *>(dhPubKeySpec);
293     HcfObjDestroy(generator);
294     HcfObjDestroy(keyPair);
295     return HCF_SUCCESS;
296 }
297 
ConstructDHPriKeyParamsSpec(const string & algoName,HcfDhCommParamsSpec * comSpec,HcfAsyKeyParamsSpec ** spec)298 static HcfResult ConstructDHPriKeyParamsSpec(const string &algoName, HcfDhCommParamsSpec *comSpec,
299     HcfAsyKeyParamsSpec **spec)
300 {
301     if (comSpec == nullptr) {
302         return HCF_INVALID_PARAMS;
303     }
304     HcfDhPriKeyParamsSpec *dhPriKeySpec = (HcfDhPriKeyParamsSpec*)HcfMalloc(sizeof(HcfDhPriKeyParamsSpec), 0);
305     if (dhPriKeySpec == nullptr) {
306         return HCF_ERR_MALLOC;
307     }
308     HcfAsyKeyGenerator *generator = nullptr;
309     HcfResult res = HcfAsyKeyGeneratorCreate(algoName.c_str(), &generator);
310     if (res != HCF_SUCCESS) {
311         HcfFree(dhPriKeySpec);
312         return res;
313     }
314 
315     HcfKeyPair *keyPair = nullptr;
316     res = generator->generateKeyPair(generator, nullptr, &keyPair);
317     if (res != HCF_SUCCESS) {
318         HcfObjDestroy(generator);
319         HcfFree(dhPriKeySpec);
320         return res;
321     }
322     HcfBigInteger retBigInt = { .data = nullptr, .len = 0 };
323     dhPriKeySpec->base.base.algName = comSpec->base.algName;
324     dhPriKeySpec->base.base.specType = HCF_PRIVATE_KEY_SPEC;
325     dhPriKeySpec->base.g = comSpec->g;
326     dhPriKeySpec->base.length = comSpec->length;
327     dhPriKeySpec->base.p = comSpec->p;
328     res = keyPair->priKey->getAsyKeySpecBigInteger(keyPair->priKey, DH_SK_BN, &retBigInt);
329     if (res != HCF_SUCCESS) {
330         HcfObjDestroy(generator);
331         HcfObjDestroy(keyPair);
332         HcfFree(dhPriKeySpec);
333         return res;
334     }
335     dhPriKeySpec->sk.data = retBigInt.data;
336     dhPriKeySpec->sk.len = retBigInt.len;
337 
338     *spec = reinterpret_cast<HcfAsyKeyParamsSpec *>(dhPriKeySpec);
339     HcfObjDestroy(generator);
340     HcfObjDestroy(keyPair);
341     return HCF_SUCCESS;
342 }
343 
HcfDhKeyUtilCreateTest(const int pLen,const int skLen)344 static HcfResult HcfDhKeyUtilCreateTest(const int pLen, const int skLen)
345 {
346     HcfDhCommParamsSpec *returnCommonParamSpec = nullptr;
347     HcfResult res = HcfDhKeyUtilCreate(pLen, skLen, &returnCommonParamSpec);
348     if (res == HCF_SUCCESS) {
349         FreeDhCommParamsSpec(returnCommonParamSpec);
350         HcfFree(returnCommonParamSpec);
351     }
352     return res;
353 }
354 
HcfDhKeyUtilErrBranch()355 static void HcfDhKeyUtilErrBranch()
356 {
357     FreeDhCommParamsSpec(nullptr);
358     DestroyDhPubKeySpec(nullptr);
359     DestroyDhPriKeySpec(nullptr);
360     DestroyDhKeyPairSpec(nullptr);
361     FreeEcPointMem(nullptr);
362     DestroyAlg25519PubKeySpec(nullptr);
363     DestroyAlg25519PriKeySpec(nullptr);
364     DestroyAlg25519KeyPairSpec(nullptr);
365     DestroySm2CipherTextSpec(nullptr);
366 }
367 
generateKeyPairTest(HcfKeyPair ** keyPair)368 static HcfResult generateKeyPairTest(HcfKeyPair **keyPair)
369 {
370     HcfAsyKeyParamsSpec *paramSpec = nullptr;
371     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
372     if (res != HCF_SUCCESS) {
373         return res;
374     }
375     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
376     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
377     if (res != HCF_SUCCESS) {
378         return res;
379     }
380     res = returnObj->generateKeyPair(returnObj, keyPair);
381     if (res != HCF_SUCCESS) {
382         return res;
383     }
384     HcfObjDestroy(returnObj);
385     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
386     return HCF_SUCCESS;
387 }
388 
SetUpTestCase()389 void CryptoDHAsyKeyGeneratorBySpecTest::SetUpTestCase()
390 {
391     HcfResult res = ConstructDHKeyCommParamsSpec(PLEN_DH1536, SKLEN_DH1024, &g_dh1536CommSpec);
392     ASSERT_EQ(res, HCF_SUCCESS);
393     res = ConstructDHKeyCommParamsSpec(PLEN_DH2048, SKLEN_DH1024, &g_dh2048CommSpec);
394     ASSERT_EQ(res, HCF_SUCCESS);
395     res = ConstructDHKeyCommParamsSpec(PLEN_DH3072, SKLEN_DH1024, &g_dh3072CommSpec);
396     ASSERT_EQ(res, HCF_SUCCESS);
397     res = ConstructDHKeyCommParamsSpec(PLEN_DH4096, SKLEN_DH1024, &g_dh4096CommSpec);
398     ASSERT_EQ(res, HCF_SUCCESS);
399     res = ConstructDHKeyCommParamsSpec(PLEN_DH6144, SKLEN_DH1024, &g_dh6144CommSpec);
400     ASSERT_EQ(res, HCF_SUCCESS);
401     res = ConstructDHKeyCommParamsSpec(PLEN_DH8192, SKLEN_DH1024, &g_dh8192CommSpec);
402     ASSERT_EQ(res, HCF_SUCCESS);
403     res = generateKeyPairTest(&g_dhKeyPair);
404 }
405 
TearDownTestCase()406 void CryptoDHAsyKeyGeneratorBySpecTest::TearDownTestCase()
407 {
408     FreeDhCommParamsSpec(g_dh1536CommSpec);
409     FreeDhCommParamsSpec(g_dh2048CommSpec);
410     FreeDhCommParamsSpec(g_dh3072CommSpec);
411     FreeDhCommParamsSpec(g_dh4096CommSpec);
412     FreeDhCommParamsSpec(g_dh6144CommSpec);
413     FreeDhCommParamsSpec(g_dh8192CommSpec);
414     HcfObjDestroy(g_dhKeyPair);
415 }
416 
417 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_1, TestSize.Level0)
418 {
419     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH2048, SKLEN_DH1024);
420     ASSERT_EQ(res, HCF_SUCCESS);
421 }
422 
423 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_2, TestSize.Level0)
424 {
425     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH3072, SKLEN_DH1024);
426     ASSERT_EQ(res, HCF_SUCCESS);
427 }
428 
429 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_3, TestSize.Level0)
430 {
431     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH4096, SKLEN_DH1024);
432     ASSERT_EQ(res, HCF_SUCCESS);
433 }
434 
435 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_4, TestSize.Level0)
436 {
437     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH6144, SKLEN_DH1024);
438     ASSERT_EQ(res, HCF_SUCCESS);
439 }
440 
441 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_5, TestSize.Level0)
442 {
443     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH8192, SKLEN_DH1024);
444     ASSERT_EQ(res, HCF_SUCCESS);
445 }
446 
447 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest001_6, TestSize.Level0)
448 {
449     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH8192, SKLEN_EQZERO);
450     ASSERT_EQ(res, HCF_SUCCESS);
451 }
452 
453 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_1, TestSize.Level0)
454 {
455     HcfAsyKeyParamsSpec *paramSpec = nullptr;
456     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
457     ASSERT_EQ(res, HCF_SUCCESS);
458     ASSERT_NE(paramSpec, nullptr);
459 
460     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
461     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
462     ASSERT_EQ(res, HCF_SUCCESS);
463     ASSERT_NE(returnObj, nullptr);
464 
465     HcfObjDestroy(returnObj);
466     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
467 }
468 
469 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_2, TestSize.Level0)
470 {
471     HcfAsyKeyParamsSpec *paramSpec = nullptr;
472     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh2048AlgoName, g_dh2048CommSpec, &paramSpec);
473     ASSERT_EQ(res, HCF_SUCCESS);
474     ASSERT_NE(paramSpec, nullptr);
475 
476     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
477     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
478     ASSERT_EQ(res, HCF_SUCCESS);
479     ASSERT_NE(returnObj, nullptr);
480 
481     HcfObjDestroy(returnObj);
482     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
483 }
484 
485 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_3, TestSize.Level0)
486 {
487     HcfAsyKeyParamsSpec *paramSpec = nullptr;
488     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh3072AlgoName, g_dh3072CommSpec, &paramSpec);
489     ASSERT_EQ(res, HCF_SUCCESS);
490     ASSERT_NE(paramSpec, nullptr);
491 
492     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
493     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
494     ASSERT_EQ(res, HCF_SUCCESS);
495     ASSERT_NE(returnObj, nullptr);
496 
497     HcfObjDestroy(returnObj);
498     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
499 }
500 
501 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_4, TestSize.Level0)
502 {
503     HcfAsyKeyParamsSpec *paramSpec = nullptr;
504     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh4096AlgoName, g_dh4096CommSpec, &paramSpec);
505     ASSERT_EQ(res, HCF_SUCCESS);
506     ASSERT_NE(paramSpec, nullptr);
507 
508     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
509     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
510     ASSERT_EQ(res, HCF_SUCCESS);
511     ASSERT_NE(returnObj, nullptr);
512 
513     HcfObjDestroy(returnObj);
514     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
515 }
516 
517 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_5, TestSize.Level0)
518 {
519     HcfAsyKeyParamsSpec *paramSpec = nullptr;
520     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh6144AlgoName, g_dh6144CommSpec, &paramSpec);
521     ASSERT_EQ(res, HCF_SUCCESS);
522     ASSERT_NE(paramSpec, nullptr);
523 
524     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
525     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
526     ASSERT_EQ(res, HCF_SUCCESS);
527     ASSERT_NE(returnObj, nullptr);
528 
529     HcfObjDestroy(returnObj);
530     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
531 }
532 
533 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest002_6, TestSize.Level0)
534 {
535     HcfAsyKeyParamsSpec *paramSpec = nullptr;
536     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh8192AlgoName, g_dh8192CommSpec, &paramSpec);
537     ASSERT_EQ(res, HCF_SUCCESS);
538     ASSERT_NE(paramSpec, nullptr);
539 
540     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
541     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
542     ASSERT_EQ(res, HCF_SUCCESS);
543     ASSERT_NE(returnObj, nullptr);
544 
545     HcfObjDestroy(returnObj);
546     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
547 }
548 
549 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest003, TestSize.Level0)
550 {
551     HcfAsyKeyParamsSpec *paramSpec = nullptr;
552     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
553     ASSERT_EQ(res, HCF_SUCCESS);
554     ASSERT_NE(paramSpec, nullptr);
555 
556     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
557     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
558     ASSERT_EQ(res, HCF_SUCCESS);
559     ASSERT_NE(returnObj, nullptr);
560 
561     const char *className = returnObj->base.getClass();
562     ASSERT_NE(className, nullptr);
563 
564     HcfObjDestroy(returnObj);
565     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
566 }
567 
568 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest004, TestSize.Level0)
569 {
570     HcfAsyKeyParamsSpec *paramSpec = nullptr;
571     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
572     ASSERT_EQ(res, HCF_SUCCESS);
573     ASSERT_NE(paramSpec, nullptr);
574 
575     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
576     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
577     ASSERT_EQ(res, HCF_SUCCESS);
578     ASSERT_NE(returnObj, nullptr);
579 
580     returnObj->base.destroy(&(returnObj->base));
581     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
582 }
583 
584 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest005, TestSize.Level0)
585 {
586     HcfAsyKeyParamsSpec *paramSpec = nullptr;
587     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
588     ASSERT_EQ(res, HCF_SUCCESS);
589     ASSERT_NE(paramSpec, nullptr);
590 
591     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
592     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
593     ASSERT_EQ(res, HCF_SUCCESS);
594     ASSERT_NE(returnObj, nullptr);
595 
596     const char * algName = returnObj->getAlgName(returnObj);
597     ASSERT_EQ(algName, g_dhAlgoName);
598 
599     HcfObjDestroy(returnObj);
600     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
601 }
602 
603 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest006, TestSize.Level0)
604 {
605     HcfKeyPair *keyPair = nullptr;
606     HcfResult res = generateKeyPairTest(&keyPair);
607     ASSERT_EQ(res, HCF_SUCCESS);
608     ASSERT_NE(keyPair, nullptr);
609 
610     HcfObjDestroy(keyPair);
611 }
612 
613 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest007, TestSize.Level0)
614 {
615     const char *className = g_dhKeyPair->base.getClass();
616     ASSERT_NE(className, nullptr);
617 }
618 
619 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest008, TestSize.Level0)
620 {
621     HcfKeyPair *keyPair = nullptr;
622     HcfResult res = generateKeyPairTest(&keyPair);
623     ASSERT_EQ(res, HCF_SUCCESS);
624     ASSERT_NE(keyPair, nullptr);
625 
626     keyPair->base.destroy(&(keyPair->base));
627 }
628 
629 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest009, TestSize.Level0)
630 {
631     const char *className = g_dhKeyPair->pubKey->base.base.getClass();
632     ASSERT_NE(className, nullptr);
633 }
634 
635 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest010, TestSize.Level0)
636 {
637     HcfKeyPair *keyPair = nullptr;
638     HcfResult res = generateKeyPairTest(&keyPair);
639     ASSERT_EQ(res, HCF_SUCCESS);
640     ASSERT_NE(keyPair, nullptr);
641 
642     keyPair->pubKey->base.base.destroy(&(keyPair->pubKey->base.base));
643     keyPair->pubKey = nullptr;
644 
645     HcfObjDestroy(keyPair);
646 }
647 
648 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest011, TestSize.Level0)
649 {
650     const char *algorithmName = g_dhKeyPair->pubKey->base.getAlgorithm(&(g_dhKeyPair->pubKey->base));
651     ASSERT_EQ(algorithmName, g_dhAlgoName);
652 
653     HcfBlob blob = { .data = nullptr, .len = 0 };
654     HcfResult res = g_dhKeyPair->pubKey->base.getEncoded(&(g_dhKeyPair->pubKey->base), &blob);
655     ASSERT_EQ(res, HCF_SUCCESS);
656     ASSERT_NE(blob.data, nullptr);
657     ASSERT_NE(blob.len, 0);
658 
659     HcfFree(blob.data);
660     const char *formatName = g_dhKeyPair->pubKey->base.getFormat(&(g_dhKeyPair->pubKey->base));
661     ASSERT_EQ(formatName, g_dhpubkeyformatName);
662 }
663 
664 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest012, TestSize.Level0)
665 {
666     const char *className = g_dhKeyPair->priKey->base.base.getClass();
667     ASSERT_NE(className, nullptr);
668 }
669 
670 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest013, TestSize.Level0)
671 {
672     HcfKeyPair *keyPair = nullptr;
673     HcfResult res = generateKeyPairTest(&keyPair);
674     ASSERT_EQ(res, HCF_SUCCESS);
675     ASSERT_NE(keyPair, nullptr);
676 
677     keyPair->priKey->base.base.destroy(&(keyPair->priKey->base.base));
678     keyPair->priKey = nullptr;
679 
680     HcfObjDestroy(keyPair);
681 }
682 
683 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest014, TestSize.Level0)
684 {
685     const char *algorithmName = g_dhKeyPair->priKey->base.getAlgorithm(&(g_dhKeyPair->priKey->base));
686     ASSERT_EQ(algorithmName, g_dhAlgoName);
687 
688     HcfBlob blob = { .data = nullptr, .len = 0 };
689     HcfResult res = g_dhKeyPair->priKey->base.getEncoded(&(g_dhKeyPair->priKey->base), &blob);
690     ASSERT_EQ(res, HCF_SUCCESS);
691     ASSERT_NE(blob.data, nullptr);
692     ASSERT_NE(blob.len, 0);
693     HcfFree(blob.data);
694     const char *formatName = g_dhKeyPair->priKey->base.getFormat(&(g_dhKeyPair->priKey->base));
695     ASSERT_EQ(formatName, g_dhprikeyformatName);
696 }
697 
698 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest015, TestSize.Level0)
699 {
700     HcfKeyPair *keyPair = nullptr;
701     HcfResult res = generateKeyPairTest(&keyPair);
702     ASSERT_EQ(res, HCF_SUCCESS);
703     ASSERT_NE(keyPair, nullptr);
704 
705     keyPair->priKey->clearMem(keyPair->priKey);
706     HcfBlob blob = { .data = nullptr, .len = 0 };
707     res = keyPair->priKey->base.getEncoded(&(keyPair->priKey->base), &blob);
708     ASSERT_EQ(res, HCF_ERR_CRYPTO_OPERATION);
709     ASSERT_EQ(blob.data, nullptr);
710     ASSERT_EQ(blob.len, 0);
711     HcfFree(blob.data);
712     HcfObjDestroy(keyPair);
713 }
714 
715 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest016, TestSize.Level0)
716 {
717     HcfAsyKeyParamsSpec *paramSpec = nullptr;
718     HcfResult res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
719     ASSERT_EQ(res, HCF_SUCCESS);
720     ASSERT_NE(paramSpec, nullptr);
721 
722     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
723     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
724     ASSERT_EQ(res, HCF_SUCCESS);
725     ASSERT_NE(returnObj, nullptr);
726 
727     HcfPubKey *pubKey = nullptr;
728     res = returnObj->generatePubKey(returnObj, &pubKey);
729     ASSERT_EQ(res, HCF_SUCCESS);
730     ASSERT_NE(pubKey, nullptr);
731 
732     HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 };
733     res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_P_BN, &returnBigInteger);
734     ASSERT_EQ(res, HCF_SUCCESS);
735     ASSERT_NE(returnBigInteger.data, nullptr);
736     ASSERT_NE(returnBigInteger.len, 0);
737     HcfFree(returnBigInteger.data);
738 
739     res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_G_BN, &returnBigInteger);
740     ASSERT_EQ(res, HCF_SUCCESS);
741     ASSERT_NE(returnBigInteger.data, nullptr);
742     ASSERT_NE(returnBigInteger.len, 0);
743     HcfFree(returnBigInteger.data);
744 
745     res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_PK_BN, &returnBigInteger);
746     ASSERT_EQ(res, HCF_SUCCESS);
747     ASSERT_NE(returnBigInteger.data, nullptr);
748     ASSERT_NE(returnBigInteger.len, 0);
749     HcfFree(returnBigInteger.data);
750     int32_t returnInt = 0;
751     res = pubKey->getAsyKeySpecInt(pubKey, DH_L_NUM, &returnInt);
752     ASSERT_EQ(res, HCF_NOT_SUPPORT);
753     ASSERT_EQ(returnInt, 0);
754 
755     HcfObjDestroy(pubKey);
756     HcfObjDestroy(returnObj);
757     DestroyTestDhPubKeySpec(reinterpret_cast<HcfDhPubKeyParamsSpec *>(paramSpec));
758 }
759 
760 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest017, TestSize.Level0)
761 {
762     HcfAsyKeyParamsSpec *paramSpec = nullptr;
763     HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
764     ASSERT_EQ(res, HCF_SUCCESS);
765     ASSERT_NE(paramSpec, nullptr);
766 
767     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
768     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
769     ASSERT_EQ(res, HCF_SUCCESS);
770     ASSERT_NE(returnObj, nullptr);
771 
772     HcfPriKey *priKey = nullptr;
773     res = returnObj->generatePriKey(returnObj, &priKey);
774     ASSERT_EQ(res, HCF_SUCCESS);
775     ASSERT_NE(priKey, nullptr);
776 
777     HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 };
778     res = priKey->getAsyKeySpecBigInteger(priKey, DH_P_BN, &returnBigInteger);
779     ASSERT_EQ(res, HCF_SUCCESS);
780     ASSERT_NE(returnBigInteger.data, nullptr);
781     ASSERT_NE(returnBigInteger.len, 0);
782     HcfFree(returnBigInteger.data);
783 
784     res = priKey->getAsyKeySpecBigInteger(priKey, DH_G_BN, &returnBigInteger);
785     ASSERT_EQ(res, HCF_SUCCESS);
786     ASSERT_NE(returnBigInteger.data, nullptr);
787     ASSERT_NE(returnBigInteger.len, 0);
788     HcfFree(returnBigInteger.data);
789 
790     res = priKey->getAsyKeySpecBigInteger(priKey, DH_SK_BN, &returnBigInteger);
791     ASSERT_EQ(res, HCF_SUCCESS);
792     ASSERT_NE(returnBigInteger.data, nullptr);
793     ASSERT_NE(returnBigInteger.len, 0);
794     HcfFree(returnBigInteger.data);
795     int32_t returnInt = 0;
796     res = priKey->getAsyKeySpecInt(priKey, DH_L_NUM, &returnInt);
797     ASSERT_EQ(res, HCF_SUCCESS);
798     ASSERT_NE(returnInt, 0);
799 
800     HcfObjDestroy(priKey);
801     HcfObjDestroy(returnObj);
802     DestroyTestDhPriKeySpec(reinterpret_cast<HcfDhPriKeyParamsSpec *>(paramSpec));
803 }
804 
805 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest018, TestSize.Level0)
806 {
807     HcfAsyKeyGenParams params = {
808         .algo = HCF_ALG_DH,
809         .bits = HCF_OPENSSL_DH_MODP_1536,
810         .primes = HCF_OPENSSL_PRIMES_2,
811     };
812 
813     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
814     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
815 
816     ASSERT_EQ(res, HCF_SUCCESS);
817     ASSERT_NE(returnSpi, nullptr);
818 
819     HcfObjDestroy(returnSpi);
820 }
821 
822 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest019, TestSize.Level0)
823 {
824     HcfAsyKeyGenParams params = {
825         .algo = HCF_ALG_DH,
826         .bits = HCF_OPENSSL_DH_MODP_1536,
827         .primes = HCF_OPENSSL_PRIMES_2,
828     };
829 
830     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
831     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
832     ASSERT_EQ(res, HCF_SUCCESS);
833     ASSERT_NE(returnSpi, nullptr);
834 
835     HcfAsyKeyParamsSpec *paramSpec = nullptr;
836     res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
837     ASSERT_EQ(res, HCF_SUCCESS);
838     ASSERT_NE(paramSpec, nullptr);
839 
840     HcfKeyPair *keyPair = nullptr;
841     res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec, &keyPair);
842     ASSERT_EQ(res, HCF_SUCCESS);
843     ASSERT_NE(keyPair, nullptr);
844 
845     HcfObjDestroy(returnSpi);
846     HcfObjDestroy(keyPair);
847     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
848 }
849 
850 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest020, TestSize.Level0)
851 {
852     HcfAsyKeyGenParams params = {
853         .algo = HCF_ALG_DH,
854         .bits = HCF_OPENSSL_DH_MODP_1536,
855         .primes = HCF_OPENSSL_PRIMES_2,
856     };
857 
858     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
859     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
860     ASSERT_EQ(res, HCF_SUCCESS);
861     ASSERT_NE(returnSpi, nullptr);
862 
863     HcfAsyKeyParamsSpec *paramSpec = nullptr;
864     res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
865     ASSERT_EQ(res, HCF_SUCCESS);
866     ASSERT_NE(paramSpec, nullptr);
867 
868     HcfPubKey *pubKey = nullptr;
869     res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramSpec, &pubKey);
870     ASSERT_EQ(res, HCF_SUCCESS);
871     ASSERT_NE(pubKey, nullptr);
872 
873     HcfObjDestroy(returnSpi);
874     HcfObjDestroy(pubKey);
875     DestroyTestDhPubKeySpec(reinterpret_cast<HcfDhPubKeyParamsSpec *>(paramSpec));
876 }
877 
878 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest021, TestSize.Level0)
879 {
880     HcfAsyKeyGenParams params = {
881         .algo = HCF_ALG_DH,
882         .bits = HCF_OPENSSL_DH_MODP_1536,
883         .primes = HCF_OPENSSL_PRIMES_2,
884     };
885 
886     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
887     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
888     ASSERT_EQ(res, HCF_SUCCESS);
889     ASSERT_NE(returnSpi, nullptr);
890 
891     HcfAsyKeyParamsSpec *paramSpec = nullptr;
892     res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
893     ASSERT_EQ(res, HCF_SUCCESS);
894     ASSERT_NE(paramSpec, nullptr);
895 
896     HcfPriKey *priKey = nullptr;
897     res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramSpec, &priKey);
898     ASSERT_EQ(res, HCF_SUCCESS);
899     ASSERT_NE(priKey, nullptr);
900 
901     HcfObjDestroy(returnSpi);
902     HcfObjDestroy(priKey);
903     DestroyTestDhPriKeySpec(reinterpret_cast<HcfDhPriKeyParamsSpec *>(paramSpec));
904 }
905 
906 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest022, TestSize.Level0)
907 {
908     HcfAsyKeyParamsSpec *paramSpec = nullptr;
909     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
910     ASSERT_EQ(res, HCF_SUCCESS);
911     ASSERT_NE(paramSpec, nullptr);
912 
913     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
914     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
915     ASSERT_EQ(res, HCF_SUCCESS);
916     ASSERT_NE(returnObj, nullptr);
917 
918     const char *algName1 = returnObj->getAlgName(nullptr);
919     ASSERT_NE(algName1, g_dhAlgoName.data());
920 
921     const char *algName2 = returnObj->getAlgName((HcfAsyKeyGeneratorBySpec *)&g_obj);
922     ASSERT_NE(algName2, g_dhAlgoName.data());
923 
924     HcfObjDestroy(returnObj);
925     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
926 }
927 
928 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest023, TestSize.Level0)
929 {
930     HcfAsyKeyParamsSpec *paramSpec = nullptr;
931     HcfResult res = ConstructDHKeyPairParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
932     ASSERT_EQ(res, HCF_SUCCESS);
933     ASSERT_NE(paramSpec, nullptr);
934 
935     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
936     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
937     ASSERT_EQ(res, HCF_SUCCESS);
938     ASSERT_NE(returnObj, nullptr);
939 
940     HcfKeyPair *keyPair = nullptr;
941     res = returnObj->generateKeyPair(nullptr, &keyPair);
942     ASSERT_EQ(res, HCF_INVALID_PARAMS);
943     ASSERT_EQ(keyPair, nullptr);
944 
945     res = returnObj->generateKeyPair((HcfAsyKeyGeneratorBySpec *)&g_obj, &keyPair);
946     ASSERT_EQ(res, HCF_INVALID_PARAMS);
947     ASSERT_EQ(keyPair, nullptr);
948 
949     HcfObjDestroy(returnObj);
950     HcfObjDestroy(keyPair);
951     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
952 }
953 
954 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest024, TestSize.Level0)
955 {
956     const char *algorithmName = g_dhKeyPair->pubKey->base.getAlgorithm(nullptr);
957     ASSERT_EQ(algorithmName, nullptr);
958 
959     const char *algorithmName1 = g_dhKeyPair->pubKey->base.getAlgorithm((HcfKey *)&g_obj);
960     ASSERT_EQ(algorithmName1, nullptr);
961 }
962 
963 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest025, TestSize.Level0)
964 {
965     HcfBlob blob = { .data = nullptr, .len = 0 };
966     HcfResult res = g_dhKeyPair->pubKey->base.getEncoded(nullptr, &blob);
967     ASSERT_EQ(res, HCF_INVALID_PARAMS);
968     ASSERT_EQ(blob.data, nullptr);
969     ASSERT_EQ(blob.len, 0);
970 
971     res = g_dhKeyPair->pubKey->base.getEncoded(&(g_dhKeyPair->pubKey->base), nullptr);
972     ASSERT_EQ(res, HCF_INVALID_PARAMS);
973 
974     res = g_dhKeyPair->pubKey->base.getEncoded((HcfKey *)&g_obj, &blob);
975     ASSERT_EQ(res, HCF_INVALID_PARAMS);
976     ASSERT_EQ(blob.data, nullptr);
977     ASSERT_EQ(blob.len, 0);
978     HcfFree(blob.data);
979 }
980 
981 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest026, TestSize.Level0)
982 {
983     const char *formatName = g_dhKeyPair->pubKey->base.getFormat(nullptr);
984     ASSERT_EQ(formatName, nullptr);
985 
986     const char *formatName1 = g_dhKeyPair->pubKey->base.getFormat((HcfKey *)&g_obj);
987     ASSERT_EQ(formatName1, nullptr);
988 }
989 
990 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest027, TestSize.Level0)
991 {
992     const char *algorithmName = g_dhKeyPair->priKey->base.getAlgorithm(nullptr);
993     ASSERT_EQ(algorithmName, nullptr);
994 
995     const char *algorithmName1 = g_dhKeyPair->priKey->base.getAlgorithm((HcfKey *)&g_obj);
996     ASSERT_EQ(algorithmName1, nullptr);
997 }
998 
999 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest028, TestSize.Level0)
1000 {
1001     HcfBlob blob = { .data = nullptr, .len = 0 };
1002     HcfResult res = g_dhKeyPair->priKey->base.getEncoded(nullptr, &blob);
1003     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1004     ASSERT_EQ(blob.data, nullptr);
1005     ASSERT_EQ(blob.len, 0);
1006 
1007     res = g_dhKeyPair->priKey->base.getEncoded(&(g_dhKeyPair->priKey->base), nullptr);
1008     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1009 
1010     res = g_dhKeyPair->priKey->base.getEncoded((HcfKey *)&g_obj, &blob);
1011     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1012     ASSERT_EQ(blob.data, nullptr);
1013     ASSERT_EQ(blob.len, 0);
1014     HcfFree(blob.data);
1015 }
1016 
1017 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest029, TestSize.Level0)
1018 {
1019     const char *formatName = g_dhKeyPair->priKey->base.getFormat(nullptr);
1020     ASSERT_EQ(formatName, nullptr);
1021 
1022     const char *formatName1 = g_dhKeyPair->priKey->base.getFormat((HcfKey *)&g_obj);
1023     ASSERT_EQ(formatName1, nullptr);
1024 }
1025 
1026 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest030, TestSize.Level0)
1027 {
1028     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1029     HcfResult res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1030     ASSERT_EQ(res, HCF_SUCCESS);
1031     ASSERT_NE(paramSpec, nullptr);
1032 
1033     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
1034     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
1035     ASSERT_EQ(res, HCF_SUCCESS);
1036     ASSERT_NE(returnObj, nullptr);
1037 
1038     HcfPubKey *pubKey = nullptr;
1039     res = returnObj->generatePubKey(returnObj, &pubKey);
1040     ASSERT_EQ(res, HCF_SUCCESS);
1041     ASSERT_NE(pubKey, nullptr);
1042 
1043     res = pubKey->getAsyKeySpecBigInteger(pubKey, DH_PK_BN, nullptr);
1044     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1045 
1046     HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 };
1047     res = pubKey->getAsyKeySpecBigInteger(pubKey, X25519_PK_BN, &returnBigInteger);
1048     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1049     ASSERT_EQ(returnBigInteger.data, nullptr);
1050     ASSERT_EQ(returnBigInteger.len, 0);
1051 
1052     res = pubKey->getAsyKeySpecBigInteger((HcfPubKey *)&g_obj, DH_PK_BN, &returnBigInteger);
1053     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1054     ASSERT_EQ(returnBigInteger.data, nullptr);
1055     ASSERT_EQ(returnBigInteger.len, 0);
1056     HcfFree(returnBigInteger.data);
1057     res = pubKey->getAsyKeySpecInt(pubKey, DH_L_NUM, nullptr);
1058     ASSERT_EQ(res, HCF_NOT_SUPPORT);
1059 
1060     int32_t returnInt = 0;
1061     res = pubKey->getAsyKeySpecInt(pubKey, X25519_PK_BN, &returnInt);
1062     ASSERT_EQ(res, HCF_NOT_SUPPORT);
1063     ASSERT_EQ(returnInt, 0);
1064 
1065     res = pubKey->getAsyKeySpecInt((HcfPubKey *)&g_obj, DH_L_NUM, &returnInt);
1066     ASSERT_EQ(res, HCF_NOT_SUPPORT);
1067     ASSERT_EQ(returnInt, 0);
1068 
1069     char *returnString = nullptr;
1070     res = pubKey->getAsyKeySpecString(pubKey, ED25519_SK_BN, &returnString);
1071     ASSERT_EQ(res, HCF_NOT_SUPPORT);
1072     ASSERT_EQ(returnString, nullptr);
1073 
1074     res = pubKey->getAsyKeySpecString(pubKey, ECC_CURVE_NAME_STR, &returnString);
1075     ASSERT_EQ(res, HCF_NOT_SUPPORT);
1076     ASSERT_EQ(returnString, nullptr);
1077 
1078     HcfObjDestroy(pubKey);
1079     HcfObjDestroy(returnObj);
1080     DestroyTestDhPubKeySpec(reinterpret_cast<HcfDhPubKeyParamsSpec *>(paramSpec));
1081 }
1082 
1083 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest031, TestSize.Level0)
1084 {
1085     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1086     HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1087     ASSERT_EQ(res, HCF_SUCCESS);
1088     ASSERT_NE(paramSpec, nullptr);
1089 
1090     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
1091     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
1092     ASSERT_EQ(res, HCF_SUCCESS);
1093     ASSERT_NE(returnObj, nullptr);
1094 
1095     HcfPriKey *priKey = nullptr;
1096     res = returnObj->generatePriKey(returnObj, &priKey);
1097     ASSERT_EQ(res, HCF_SUCCESS);
1098     ASSERT_NE(priKey, nullptr);
1099 
1100     res = priKey->getAsyKeySpecBigInteger(priKey, DH_PK_BN, nullptr);
1101     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1102 
1103     HcfBigInteger returnBigInteger = { .data = nullptr, .len = 0 };
1104     res = priKey->getAsyKeySpecBigInteger(priKey, X25519_PK_BN, &returnBigInteger);
1105     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1106     ASSERT_EQ(returnBigInteger.data, nullptr);
1107     ASSERT_EQ(returnBigInteger.len, 0);
1108 
1109     res = priKey->getAsyKeySpecBigInteger((HcfPriKey *)&g_obj, DH_PK_BN, &returnBigInteger);
1110     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1111     ASSERT_EQ(returnBigInteger.data, nullptr);
1112     ASSERT_EQ(returnBigInteger.len, 0);
1113     HcfFree(returnBigInteger.data);
1114     res = priKey->getAsyKeySpecInt(priKey, DH_L_NUM, nullptr);
1115     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1116 
1117     int32_t returnInt = 0;
1118     res = priKey->getAsyKeySpecInt(priKey, X25519_PK_BN, &returnInt);
1119     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1120     ASSERT_EQ(returnInt, 0);
1121 
1122     res = priKey->getAsyKeySpecInt((HcfPriKey *)&g_obj, DH_L_NUM, &returnInt);
1123     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1124     ASSERT_EQ(returnInt, 0);
1125 
1126     char *returnString = nullptr;
1127     res = priKey->getAsyKeySpecString(priKey, ED25519_SK_BN, &returnString);
1128     ASSERT_EQ(res, HCF_NOT_SUPPORT);
1129     ASSERT_EQ(returnString, nullptr);
1130 
1131     res = priKey->getAsyKeySpecString(priKey, ECC_CURVE_NAME_STR, &returnString);
1132     ASSERT_EQ(res, HCF_NOT_SUPPORT);
1133     ASSERT_EQ(returnString, nullptr);
1134 
1135     HcfObjDestroy(priKey);
1136     HcfObjDestroy(returnObj);
1137     DestroyTestDhPriKeySpec(reinterpret_cast<HcfDhPriKeyParamsSpec *>(paramSpec));
1138 }
1139 
1140 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest032, TestSize.Level0)
1141 {
1142     HcfDhCommParamsSpec *returnCommonParamSpec = nullptr;
1143     HcfResult res = HcfDhKeyUtilCreate(INT_MAX, SKLEN_DH1024, &returnCommonParamSpec);
1144     ASSERT_EQ(res, HCF_ERR_MALLOC);
1145 
1146     res = HcfDhKeyUtilCreate(PLEN_LTSK, SKLEN_DH1024, &returnCommonParamSpec);
1147     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1148 
1149     res = HcfDhKeyUtilCreate(INT_MAX, SKLEN_DH1024, nullptr);
1150     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1151 }
1152 
1153 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest033, TestSize.Level0)
1154 {
1155     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1156     HcfResult res = ConstructDHPubKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1157     ASSERT_EQ(res, HCF_SUCCESS);
1158     ASSERT_NE(paramSpec, nullptr);
1159 
1160     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
1161     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
1162     ASSERT_EQ(res, HCF_SUCCESS);
1163     ASSERT_NE(returnObj, nullptr);
1164 
1165     HcfKeyPair *keyPair = nullptr;
1166     res = returnObj->generateKeyPair(returnObj, &keyPair);
1167     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1168     ASSERT_EQ(keyPair, nullptr);
1169 
1170     HcfPriKey *priKey = nullptr;
1171     res = returnObj->generatePriKey(returnObj, &priKey);
1172     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1173     ASSERT_EQ(priKey, nullptr);
1174 
1175     HcfObjDestroy(returnObj);
1176     HcfObjDestroy(keyPair);
1177     DestroyTestDhPubKeySpec(reinterpret_cast<HcfDhPubKeyParamsSpec *>(paramSpec));
1178 }
1179 
1180 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest034, TestSize.Level0)
1181 {
1182     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1183     HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1184     ASSERT_EQ(res, HCF_SUCCESS);
1185     ASSERT_NE(paramSpec, nullptr);
1186 
1187     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
1188     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
1189     ASSERT_EQ(res, HCF_SUCCESS);
1190     ASSERT_NE(returnObj, nullptr);
1191 
1192     HcfKeyPair *keyPair = nullptr;
1193     res = returnObj->generateKeyPair(returnObj, &keyPair);
1194     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1195     ASSERT_EQ(keyPair, nullptr);
1196 
1197     HcfPubKey *pubKey = nullptr;
1198     res = returnObj->generatePubKey(returnObj, &pubKey);
1199     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1200     ASSERT_EQ(pubKey, nullptr);
1201 
1202     HcfObjDestroy(returnObj);
1203     HcfObjDestroy(keyPair);
1204     DestroyTestDhPriKeySpec(reinterpret_cast<HcfDhPriKeyParamsSpec *>(paramSpec));
1205 }
1206 
1207 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest035, TestSize.Level0)
1208 {
1209     HcfAsyKeyGenParams params = {
1210         .algo = HCF_ALG_DH,
1211         .bits = HCF_OPENSSL_DH_MODP_1536,
1212         .primes = HCF_OPENSSL_PRIMES_2,
1213     };
1214 
1215     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
1216     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
1217     ASSERT_EQ(res, HCF_SUCCESS);
1218     ASSERT_NE(returnSpi, nullptr);
1219 
1220     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1221     res = ConstructDHKeyPairParamsCommonSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1222     ASSERT_EQ(res, HCF_SUCCESS);
1223     ASSERT_NE(paramSpec, nullptr);
1224 
1225     HcfKeyPair *keyPair = nullptr;
1226     res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec, &keyPair);
1227     ASSERT_EQ(res, HCF_SUCCESS);
1228     ASSERT_NE(keyPair, nullptr);
1229 
1230     HcfObjDestroy(returnSpi);
1231     HcfObjDestroy(keyPair);
1232     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
1233 }
1234 
1235 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest036, TestSize.Level0)
1236 {
1237     HcfAsyKeyGenParams params = {
1238         .algo = HCF_ALG_DH,
1239         .bits = HCF_OPENSSL_DH_MODP_1536,
1240         .primes = HCF_OPENSSL_PRIMES_2,
1241     };
1242 
1243     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
1244     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
1245 
1246     ASSERT_EQ(res, HCF_SUCCESS);
1247     ASSERT_NE(returnSpi, nullptr);
1248 
1249     returnSpi->base.destroy(nullptr);
1250     returnSpi->base.destroy(&g_obj);
1251     HcfObjDestroy(returnSpi);
1252 }
1253 
1254 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest037, TestSize.Level0)
1255 {
1256     HcfAsyKeyGenParams params = {
1257         .algo = HCF_ALG_DH,
1258         .bits = HCF_OPENSSL_DH_MODP_1536,
1259         .primes = HCF_OPENSSL_PRIMES_2,
1260     };
1261 
1262     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
1263     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
1264     ASSERT_EQ(res, HCF_SUCCESS);
1265     ASSERT_NE(returnSpi, nullptr);
1266 
1267     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1268     res = ConstructDHKeyPairParamsCommonSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1269     ASSERT_EQ(res, HCF_SUCCESS);
1270     ASSERT_NE(paramSpec, nullptr);
1271 
1272     res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec, nullptr);
1273     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1274 
1275     HcfKeyPair *keyPair = nullptr;
1276     res = returnSpi->engineGenerateKeyPairBySpec((HcfAsyKeyGeneratorSpi *)&g_obj, paramSpec, &keyPair);
1277     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1278     ASSERT_EQ(keyPair, nullptr);
1279 
1280     HcfAsyKeyParamsSpec *paramSpec1 = nullptr;
1281     res = ConstructAlg25519KeyPairParamsSpec("Ed25519", true, &paramSpec1);
1282     ASSERT_EQ(res, HCF_SUCCESS);
1283     ASSERT_NE(paramSpec1, nullptr);
1284 
1285     HcfKeyPair *keyPair1 = nullptr;
1286     res = returnSpi->engineGenerateKeyPairBySpec(returnSpi, paramSpec1, &keyPair1);
1287     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1288     ASSERT_EQ(keyPair1, nullptr);
1289 
1290     HcfObjDestroy(returnSpi);
1291     HcfObjDestroy(keyPair);
1292     HcfObjDestroy(keyPair1);
1293     DestroyAlg25519KeyPairSpec(reinterpret_cast<HcfAlg25519KeyPairParamsSpec *>(paramSpec1));
1294     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
1295 }
1296 
1297 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest038, TestSize.Level0)
1298 {
1299     HcfAsyKeyGenParams params = {
1300         .algo = HCF_ALG_DH,
1301         .bits = HCF_OPENSSL_DH_MODP_2048,
1302         .primes = HCF_OPENSSL_PRIMES_2,
1303     };
1304 
1305     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
1306     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
1307     ASSERT_EQ(res, HCF_SUCCESS);
1308     ASSERT_NE(returnSpi, nullptr);
1309 
1310     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1311     res = ConstructDHPubKeyParamsSpec(g_dh2048AlgoName, g_dh2048CommSpec, &paramSpec);
1312     ASSERT_EQ(res, HCF_SUCCESS);
1313     ASSERT_NE(paramSpec, nullptr);
1314 
1315     res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramSpec, nullptr);
1316     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1317 
1318     HcfPubKey *pubKey = nullptr;
1319     res = returnSpi->engineGeneratePubKeyBySpec((HcfAsyKeyGeneratorSpi *)&g_obj, paramSpec, &pubKey);
1320     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1321     ASSERT_EQ(pubKey, nullptr);
1322 
1323     HcfAsyKeyParamsSpec *paramSpec1 = nullptr;
1324     res = ConstructAlg25519PubKeyParamsSpec("Ed25519", true, &paramSpec1);
1325     ASSERT_EQ(res, HCF_SUCCESS);
1326     ASSERT_NE(paramSpec1, nullptr);
1327 
1328     HcfPubKey *pubKey1 = nullptr;
1329     res = returnSpi->engineGeneratePubKeyBySpec(returnSpi, paramSpec1, &pubKey1);
1330     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1331     ASSERT_EQ(pubKey1, nullptr);
1332 
1333     HcfObjDestroy(returnSpi);
1334     HcfObjDestroy(pubKey);
1335     HcfObjDestroy(pubKey1);
1336     DestroyTestDhPubKeySpec(reinterpret_cast<HcfDhPubKeyParamsSpec *>(paramSpec));
1337     DestroyAlg25519PubKeySpec(reinterpret_cast<HcfAlg25519PubKeyParamsSpec *>(paramSpec1));
1338 }
1339 
1340 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest039, TestSize.Level0)
1341 {
1342     HcfAsyKeyGenParams params = {
1343         .algo = HCF_ALG_DH,
1344         .bits = HCF_OPENSSL_DH_MODP_3072,
1345         .primes = HCF_OPENSSL_PRIMES_2,
1346     };
1347 
1348     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
1349     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(&params, &returnSpi);
1350     ASSERT_EQ(res, HCF_SUCCESS);
1351     ASSERT_NE(returnSpi, nullptr);
1352 
1353     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1354     res = ConstructDHPriKeyParamsSpec(g_dh3072AlgoName, g_dh3072CommSpec, &paramSpec);
1355     ASSERT_EQ(res, HCF_SUCCESS);
1356     ASSERT_NE(paramSpec, nullptr);
1357 
1358     res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramSpec, nullptr);
1359     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1360 
1361     HcfPriKey *priKey = nullptr;
1362     res = returnSpi->engineGeneratePriKeyBySpec((HcfAsyKeyGeneratorSpi *)&g_obj, paramSpec, &priKey);
1363     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1364     ASSERT_EQ(priKey, nullptr);
1365 
1366     HcfAsyKeyParamsSpec *paramSpec1 = nullptr;
1367     res = ConstructAlg25519PriKeyParamsSpec("Ed25519", true, &paramSpec1);
1368     ASSERT_EQ(res, HCF_SUCCESS);
1369     ASSERT_NE(paramSpec1, nullptr);
1370 
1371     HcfPriKey *priKey1 = nullptr;
1372     res = returnSpi->engineGeneratePriKeyBySpec(returnSpi, paramSpec1, &priKey1);
1373     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1374     ASSERT_EQ(priKey1, nullptr);
1375 
1376     HcfObjDestroy(returnSpi);
1377     HcfObjDestroy(priKey);
1378     HcfObjDestroy(priKey1);
1379     DestroyTestDhPriKeySpec(reinterpret_cast<HcfDhPriKeyParamsSpec *>(paramSpec));
1380     DestroyAlg25519PriKeySpec(reinterpret_cast<HcfAlg25519PriKeyParamsSpec *>(paramSpec1));
1381 }
1382 
1383 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest040, TestSize.Level0)
1384 {
1385     HcfAsyKeyGeneratorSpi *returnSpi = nullptr;
1386     HcfResult res = HcfAsyKeyGeneratorSpiDhCreate(nullptr, &returnSpi);
1387 
1388     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1389     ASSERT_EQ(returnSpi, nullptr);
1390 
1391     HcfObjDestroy(returnSpi);
1392 }
1393 
OpensslMockTestFunc(uint32_t mallocCount,HcfAsyKeyParamsSpec * paramSpec)1394 static void OpensslMockTestFunc(uint32_t mallocCount, HcfAsyKeyParamsSpec *paramSpec)
1395 {
1396     for (uint32_t i = 0; i < mallocCount; i++) {
1397         ResetOpensslCallNum();
1398         SetOpensslCallMockIndex(i);
1399         HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
1400         HcfResult res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
1401         if (res != HCF_SUCCESS) {
1402             continue;
1403         }
1404 
1405         HcfKeyPair *keyPair = nullptr;
1406         res = returnObj->generateKeyPair(returnObj, &keyPair);
1407         if (res != HCF_SUCCESS) {
1408             HcfObjDestroy(returnObj);
1409             continue;
1410         }
1411 
1412         HcfObjDestroy(returnObj);
1413         HcfObjDestroy(keyPair);
1414     }
1415 }
1416 
1417 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest041, TestSize.Level0)
1418 {
1419     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1420     HcfResult res = ConstructDHKeyPairParamsCommonSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1421     ASSERT_EQ(res, HCF_SUCCESS);
1422     ASSERT_NE(paramSpec, nullptr);
1423     StartRecordOpensslCallNum();
1424 
1425     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
1426     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
1427     ASSERT_EQ(res, HCF_SUCCESS);
1428     ASSERT_NE(returnObj, nullptr);
1429 
1430     HcfKeyPair *keyPair = nullptr;
1431     res = returnObj->generateKeyPair(returnObj, &keyPair);
1432     ASSERT_EQ(res, HCF_SUCCESS);
1433     ASSERT_NE(keyPair, nullptr);
1434 
1435     HcfObjDestroy(returnObj);
1436     HcfObjDestroy(keyPair);
1437 
1438     uint32_t mallocCount = GetOpensslCallNum();
1439     OpensslMockTestFunc(mallocCount, paramSpec);
1440 
1441     EndRecordOpensslCallNum();
1442     DestroyTestDhKeyPairSpec(reinterpret_cast<HcfDhKeyPairParamsSpec *>(paramSpec));
1443 }
1444 
OpensslMockTestFunc1(uint32_t mallocCount)1445 static void OpensslMockTestFunc1(uint32_t mallocCount)
1446 {
1447     for (uint32_t i = 0; i < mallocCount; i++) {
1448         ResetOpensslCallNum();
1449         SetOpensslCallMockIndex(i);
1450 
1451         HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH4096, SKLEN_DH1024);
1452         if (res != HCF_SUCCESS) {
1453             continue;
1454         }
1455     }
1456 }
1457 
1458 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest042, TestSize.Level0)
1459 {
1460     StartRecordOpensslCallNum();
1461     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH4096, SKLEN_DH1024);
1462     ASSERT_EQ(res, HCF_SUCCESS);
1463 
1464     uint32_t mallocCount = GetOpensslCallNum();
1465     OpensslMockTestFunc1(mallocCount);
1466 
1467     EndRecordOpensslCallNum();
1468 }
1469 
1470 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest043, TestSize.Level0)
1471 {
1472     HcfResult res = HcfDhCommonParamSpecCreate(PLEN_DH4096, SKLEN_DH1024, nullptr);
1473     ASSERT_EQ(res, HCF_INVALID_PARAMS);
1474 }
1475 
OpensslMockTestFunc2(uint32_t mallocCount)1476 static void OpensslMockTestFunc2(uint32_t mallocCount)
1477 {
1478     for (uint32_t i = 0; i < mallocCount; i++) {
1479         ResetOpensslCallNum();
1480         SetOpensslCallMockIndex(i);
1481 
1482         HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH512, SKLEN_DH128);
1483         if (res != HCF_SUCCESS) {
1484             continue;
1485         }
1486     }
1487 }
1488 
1489 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest044, TestSize.Level0)
1490 {
1491     StartRecordOpensslCallNum();
1492     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH512, SKLEN_DH128);
1493     ASSERT_EQ(res, HCF_SUCCESS);
1494 
1495     uint32_t mallocCount = GetOpensslCallNum();
1496     OpensslMockTestFunc2(mallocCount);
1497 
1498     EndRecordOpensslCallNum();
1499 }
1500 
1501 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest045, TestSize.Level0)
1502 {
1503     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH2048, SKLEN_DH223);
1504     ASSERT_EQ(res, HCF_ERR_MALLOC);
1505 
1506     res = HcfDhKeyUtilCreateTest(PLEN_DH3072, SKLEN_DH255);
1507     ASSERT_EQ(res, HCF_ERR_MALLOC);
1508 
1509     res = HcfDhKeyUtilCreateTest(PLEN_DH4096, SKLEN_DH303);
1510     ASSERT_EQ(res, HCF_ERR_MALLOC);
1511 
1512     res = HcfDhKeyUtilCreateTest(PLEN_DH6144, SKLEN_DH351);
1513     ASSERT_EQ(res, HCF_ERR_MALLOC);
1514 
1515     res = HcfDhKeyUtilCreateTest(PLEN_DH8192, SKLEN_DH399);
1516     ASSERT_EQ(res, HCF_ERR_MALLOC);
1517 }
1518 
1519 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest046, TestSize.Level0)
1520 {
1521     HcfAsyKeyParamsSpec *paramSpec = nullptr;
1522     HcfResult res = ConstructDHPriKeyParamsSpec(g_dh1536AlgoName, g_dh1536CommSpec, &paramSpec);
1523     ASSERT_EQ(res, HCF_SUCCESS);
1524     ASSERT_NE(paramSpec, nullptr);
1525 
1526     HcfAsyKeyGeneratorBySpec *returnObj = nullptr;
1527     res = HcfAsyKeyGeneratorBySpecCreate(paramSpec, &returnObj);
1528     ASSERT_EQ(res, HCF_SUCCESS);
1529     ASSERT_NE(returnObj, nullptr);
1530 
1531     HcfPriKey *priKey = nullptr;
1532     res = returnObj->generatePriKey(returnObj, &priKey);
1533     ASSERT_EQ(res, HCF_SUCCESS);
1534     ASSERT_NE(priKey, nullptr);
1535 
1536     HcfBlob blob = { .data = nullptr, .len = 0 };
1537     res = priKey->base.getEncoded(&(priKey->base), &blob);
1538     ASSERT_EQ(res, HCF_SUCCESS);
1539     ASSERT_NE(blob.data, nullptr);
1540     ASSERT_NE(blob.len, 0);
1541 
1542     HcfAsyKeyGenerator *generator = nullptr;
1543     res = HcfAsyKeyGeneratorCreate("DH_modp1536", &generator);
1544     ASSERT_EQ(res, HCF_SUCCESS);
1545 
1546     HcfKeyPair *keyPair = nullptr;
1547     res = generator->convertKey(generator, nullptr, nullptr, &blob, &keyPair);
1548     ASSERT_EQ(res, HCF_SUCCESS);
1549     ASSERT_NE(keyPair, nullptr);
1550 
1551     HcfFree(blob.data);
1552     HcfObjDestroy(returnObj);
1553     HcfObjDestroy(generator);
1554     HcfObjDestroy(priKey);
1555     HcfObjDestroy(keyPair);
1556     DestroyTestDhPriKeySpec(reinterpret_cast<HcfDhPriKeyParamsSpec *>(paramSpec));
1557 }
1558 
1559 HWTEST_F(CryptoDHAsyKeyGeneratorBySpecTest, CryptoDHAsyKeyGeneratorBySpecTest047, TestSize.Level0)
1560 {
1561     HcfDhKeyUtilErrBranch();
1562     HcfResult res = HcfDhKeyUtilCreateTest(PLEN_DH10001, SKLEN_EQZERO);
1563     ASSERT_EQ(res, HCF_ERR_MALLOC);
1564 
1565     res = HcfDhKeyUtilCreateTest(PLEN_DH511, SKLEN_EQZERO);
1566     ASSERT_EQ(res, HCF_ERR_MALLOC);
1567 }
1568 }