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