1 /*
2 * Copyright (c) 2024 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 #include <stdlib.h>
16 #include <stdio.h>
17 #include <pthread.h>
18 #include <string.h>
19
20 #include "crypto_framework_asym_key_test.h"
21 #include "blob.h"
22 #include "log.h"
23
24 #include <gtest/gtest.h>
25
26 using namespace testing::ext;
27 namespace Unittest::CryptoFrameworkAsymKeyNapiTest {
28 class OHCryptoFrameworkAsymKeyNapiTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31
32 static void TearDownTestCase(void);
33
34 void SetUp();
35
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void OHCryptoFrameworkAsymKeyNapiTest::SetUpTestCase(void)
40 {
41 }
42
TearDownTestCase(void)43 void OHCryptoFrameworkAsymKeyNapiTest::TearDownTestCase(void)
44 {
45 }
46
SetUp()47 void OHCryptoFrameworkAsymKeyNapiTest::SetUp()
48 {
49 }
50
TearDown()51 void OHCryptoFrameworkAsymKeyNapiTest::TearDown()
52 {
53 }
54
55 class ASYMKEY_TEST : public testing::TestWithParam<AsymKeySpec> {
56 public:
SetUpTestCase()57 static void SetUpTestCase() {}
TearDownTestCase()58 static void TearDownTestCase() {}
59 };
60
61 AsymKeySpec g_asymKeyData[] = {
62 {(uint8_t *)"DSA1024", "DSA"},
63 {(uint8_t *)"DSA2048", "DSA"},
64 {(uint8_t *)"DSA3072", "DSA"},
65 {(uint8_t *)"Ed25519", "Ed25519"},
66 {(uint8_t *)"X25519", "X25519"},
67 {(uint8_t *)"DH_modp1536", "DH"},
68 {(uint8_t *)"DH_modp2048", "DH"},
69 {(uint8_t *)"DH_modp3072", "DH"},
70 {(uint8_t *)"DH_modp4096", "DH"},
71 {(uint8_t *)"DH_modp6144", "DH"},
72 {(uint8_t *)"DH_modp8192", "DH"},
73 {(uint8_t *)"DH_ffdhe2048", "DH"},
74 {(uint8_t *)"DH_ffdhe3072", "DH"},
75 {(uint8_t *)"DH_ffdhe4096", "DH"},
76 {(uint8_t *)"DH_ffdhe6144", "DH"},
77 {(uint8_t *)"DH_ffdhe8192", "DH"},
78 {(uint8_t *)"RSA512|PRIMES_2", "RSA"},
79 {(uint8_t *)"RSA768|PRIMES_2", "RSA"},
80 {(uint8_t *)"RSA1024|PRIMES_2", "RSA"},
81 {(uint8_t *)"RSA1024|PRIMES_3", "RSA"},
82 {(uint8_t *)"RSA2048|PRIMES_2", "RSA"},
83 {(uint8_t *)"RSA2048|PRIMES_3", "RSA"},
84 {(uint8_t *)"RSA3072|PRIMES_2", "RSA"},
85 {(uint8_t *)"RSA3072|PRIMES_3", "RSA"},
86 {(uint8_t *)"RSA4096|PRIMES_2", "RSA"},
87 {(uint8_t *)"RSA4096|PRIMES_3", "RSA"},
88 {(uint8_t *)"RSA4096|PRIMES_4", "RSA"},
89 {(uint8_t *)"RSA8192|PRIMES_2", "RSA"},
90 {(uint8_t *)"RSA8192|PRIMES_3", "RSA"},
91 {(uint8_t *)"RSA8192|PRIMES_4", "RSA"},
92 {(uint8_t *)"RSA8192|PRIMES_5", "RSA"},
93 {(uint8_t *)"SM2_256", "SM2"},
94 {(uint8_t *)"ECC224", "ECC"},
95 {(uint8_t *)"ECC256", "ECC"},
96 {(uint8_t *)"ECC384", "ECC"},
97 {(uint8_t *)"ECC521", "ECC"},
98 {(uint8_t *)"ECC_BrainPoolP160r1", "ECC"},
99 {(uint8_t *)"ECC_BrainPoolP160t1", "ECC"},
100 {(uint8_t *)"ECC_BrainPoolP192r1", "ECC"},
101 {(uint8_t *)"ECC_BrainPoolP192t1", "ECC"},
102 {(uint8_t *)"ECC_BrainPoolP224r1", "ECC"},
103 {(uint8_t *)"ECC_BrainPoolP224t1", "ECC"},
104 {(uint8_t *)"ECC_BrainPoolP256r1", "ECC"},
105 {(uint8_t *)"ECC_BrainPoolP256t1", "ECC"},
106 {(uint8_t *)"ECC_BrainPoolP320r1", "ECC"},
107 {(uint8_t *)"ECC_BrainPoolP320t1", "ECC"},
108 {(uint8_t *)"ECC_BrainPoolP384r1", "ECC"},
109 {(uint8_t *)"ECC_BrainPoolP384t1", "ECC"},
110 {(uint8_t *)"ECC_BrainPoolP512r1", "ECC"},
111 {(uint8_t *)"ECC_BrainPoolP512t1", "ECC"},
112 };
113
Rsa_PubkeyEncode(OH_CryptoAsymKeyGenerator * ctx,OH_CryptoPubKey * pubKey,OH_CryptoKeyPair ** dupKeyPair)114 static OH_Crypto_ErrCode Rsa_PubkeyEncode(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoPubKey *pubKey, OH_CryptoKeyPair **dupKeyPair)
115 {
116 Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
117 OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
118 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob);
119 if (ret != CRYPTO_SUCCESS) {
120 return ret;
121 }
122
123 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_PEM, &retBlob, nullptr, dupKeyPair);
124 if (ret != CRYPTO_SUCCESS) {
125 return ret;
126 }
127 return ret;
128 }
129
Ecc_PubkeyEncode(OH_CryptoAsymKeyGenerator * ctx,OH_CryptoPubKey * pubKey,OH_CryptoKeyPair ** dupKeyPair)130 static OH_Crypto_ErrCode Ecc_PubkeyEncode(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoPubKey *pubKey, OH_CryptoKeyPair **dupKeyPair)
131 {
132 Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
133 OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
134 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, "X509|COMPRESSED", &retBlob);
135 if (ret != CRYPTO_SUCCESS) {
136 return ret;
137 }
138
139 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &retBlob, nullptr, dupKeyPair);
140 if (ret != CRYPTO_SUCCESS) {
141 return ret;
142 }
143 return ret;
144 }
145
PubkeyEncode(OH_CryptoAsymKeyGenerator * ctx,OH_CryptoPubKey * pubKey,OH_CryptoKeyPair ** dupKeyPair)146 static OH_Crypto_ErrCode PubkeyEncode(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoPubKey *pubKey, OH_CryptoKeyPair **dupKeyPair)
147 {
148 Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 };
149 OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
150 ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob);
151 if (ret != CRYPTO_SUCCESS) {
152 return ret;
153 }
154
155 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &retBlob, nullptr, dupKeyPair);
156 if (ret != CRYPTO_SUCCESS) {
157 return ret;
158 }
159 return ret;
160 }
161
Ecc_GetEncode(OH_CryptoPubKey * pubKey,Crypto_DataBlob * value)162 static OH_Crypto_ErrCode Ecc_GetEncode(OH_CryptoPubKey *pubKey, Crypto_DataBlob *value)
163 {
164 OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_FP_P_DATABLOB, value);
165 return ret;
166 }
167
Rsa_GetEncode(OH_CryptoPubKey * pubKey,Crypto_DataBlob * value)168 static OH_Crypto_ErrCode Rsa_GetEncode(OH_CryptoPubKey *pubKey, Crypto_DataBlob *value)
169 {
170 OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_RSA_N_DATABLOB, value);
171 return ret;
172 }
173
GetEncode(const char * name,OH_CryptoPubKey * pubKey,Crypto_DataBlob * value)174 static OH_Crypto_ErrCode GetEncode(const char *name, OH_CryptoPubKey *pubKey, Crypto_DataBlob *value)
175 {
176 CryptoAsymKey_ParamType item;
177 if (strcmp(name, "DH") == 0) {
178 item = CRYPTO_DH_P_DATABLOB;
179 } else if (strcmp(name, "Ed25519") == 0) {
180 item = CRYPTO_ED25519_PK_DATABLOB;
181 } else if (strcmp(name, "X25519") == 0) {
182 item = CRYPTO_X25519_PK_DATABLOB;
183 } else if (strcmp(name, "DSA") == 0) {
184 item = CRYPTO_DSA_P_DATABLOB;
185 } else {
186 item = CRYPTO_ECC_H_INT;
187 }
188
189 OH_Crypto_ErrCode ret = OH_CryptoPubKey_GetParam(pubKey, item, value);
190 return ret;
191 }
192
193
194 INSTANTIATE_TEST_CASE_P(OHCryptoFrameworkAsymKeyNapiTest, ASYMKEY_TEST, ::testing::ValuesIn(g_asymKeyData));
195
196 /**
197 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0100
198 * @tc.name OHCryptoFrameworkAsymKeyNapiTest/ASYMKEY_TEST.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0100/x
199 * @tc.desc algorithm is Asym Key
200 * @tc.size Medium
201 * @tc.type Func
202 * @tc.level Level0
203 */
204 HWTEST_P(ASYMKEY_TEST, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0100, TestSize.Level0)
205 {
206 AsymKeySpec AsymKeyData = GetParam();
207 OH_CryptoAsymKeyGenerator *ctx = nullptr;
208 OH_CryptoKeyPair *keyPair = nullptr;
209 OH_Crypto_ErrCode ret;
210
211 ret = OH_CryptoAsymKeyGenerator_Create((const char *)AsymKeyData.algName, &ctx);
212 ASSERT_EQ(ret, CRYPTO_SUCCESS);
213
214 ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair);
215 ASSERT_EQ(ret, CRYPTO_SUCCESS);
216
217 const char *algoName = OH_CryptoAsymKeyGenerator_GetAlgoName(ctx);
218 EXPECT_NE(algoName, nullptr);
219
220 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
221 ASSERT_EQ(ret, CRYPTO_SUCCESS);
222
223 OH_CryptoKeyPair *dupKeyPair = nullptr;
224 Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 };
225 if (strcmp(AsymKeyData.name, "RSA") == 0) {
226 ret = Rsa_PubkeyEncode(ctx, pubKey, &dupKeyPair);
227 ASSERT_EQ(ret, CRYPTO_SUCCESS);
228 OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
229 ASSERT_EQ(ret, CRYPTO_SUCCESS);
230 ret = Rsa_GetEncode(pubKey1, &dataBlob);
231 ASSERT_EQ(ret, CRYPTO_SUCCESS);
232 } else if (strcmp(AsymKeyData.name, "ECC") == 0) {
233 ret = Ecc_PubkeyEncode(ctx, pubKey, &dupKeyPair);
234 ASSERT_EQ(ret, CRYPTO_SUCCESS);
235 OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
236 ASSERT_EQ(ret, CRYPTO_SUCCESS);
237 ret = Ecc_GetEncode(pubKey1, &dataBlob);
238 ASSERT_EQ(ret, CRYPTO_SUCCESS);
239 } else {
240 ret = PubkeyEncode(ctx, pubKey, &dupKeyPair);
241 ASSERT_EQ(ret, CRYPTO_SUCCESS);
242 OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
243 ASSERT_EQ(ret, CRYPTO_SUCCESS);
244 ret = GetEncode(AsymKeyData.name, pubKey1, &dataBlob);
245 ASSERT_EQ(ret, CRYPTO_SUCCESS);
246 }
247
248 ASSERT_NE(dataBlob.data, nullptr);
249 ASSERT_NE(dataBlob.len, 0);
250 OH_Crypto_FreeDataBlob(&dataBlob);
251
252 OH_CryptoAsymKeyGenerator_Destroy(ctx);
253 OH_CryptoKeyPair_Destroy(keyPair);
254 OH_CryptoKeyPair_Destroy(dupKeyPair);
255 }
256
257 /**
258 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0200
259 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0200
260 * @tc.desc algorithm is Asym Key
261 * @tc.size Medium
262 * @tc.type Func
263 * @tc.level Level0
264 */
265 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0200, TestSize.Level0)
266 {
267 OH_CryptoAsymKeyGenerator *ctx = nullptr;
268 OH_Crypto_ErrCode ret;
269 ret = OH_CryptoAsymKeyGenerator_Create("SM9", &ctx);
270 EXPECT_NE(ret, CRYPTO_SUCCESS);
271 OH_CryptoAsymKeyGenerator_Destroy(ctx);
272 }
273
274 /**
275 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0300
276 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0300
277 * @tc.desc algorithm is Asym Key
278 * @tc.size Medium
279 * @tc.type Func
280 * @tc.level Level0
281 */
282 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0300, TestSize.Level0)
283 {
284 OH_CryptoAsymKeyGenerator *ctx = nullptr;
285 OH_Crypto_ErrCode ret;
286 ret = OH_CryptoAsymKeyGenerator_Create("SM", &ctx);
287 EXPECT_NE(ret, CRYPTO_SUCCESS);
288 OH_CryptoAsymKeyGenerator_Destroy(ctx);
289 }
290
291 /**
292 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0400
293 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0400
294 * @tc.desc algorithm is Asym Key
295 * @tc.size Medium
296 * @tc.type Func
297 * @tc.level Level0
298 */
299 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0400, TestSize.Level0)
300 {
301 OH_CryptoAsymKeyGenerator *ctx = nullptr;
302 OH_CryptoKeyPair *dupKeyPair = nullptr;
303 OH_Crypto_ErrCode ret;
304 ret = OH_CryptoAsymKeyGenerator_Create("ECC224", &ctx);
305 EXPECT_EQ(ret, CRYPTO_SUCCESS);
306 uint8_t array[] = {48, 50, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0,
307 33, 3, 30, 0, 2, 62, 18, 146, 41, 146, 160, 136, 78, 124, 38, 148, 1, 204, 24, 45, 172,
308 101, 234, 253, 74, 133, 28, 184, 168, 153, 170, 203, 134};
309 Crypto_DataBlob dataBlob = { .data = array, .len = sizeof(array) };
310 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &dataBlob, nullptr, &dupKeyPair);
311 ASSERT_EQ(ret, CRYPTO_SUCCESS);
312 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
313 ASSERT_NE(pubKey, nullptr);
314
315 OH_CryptoAsymKeyGenerator_Destroy(ctx);
316 OH_CryptoKeyPair_Destroy(dupKeyPair);
317 }
318
319 /**
320 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0500
321 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0500
322 * @tc.desc algorithm is Asym Key
323 * @tc.size Medium
324 * @tc.type Func
325 * @tc.level Level0
326 */
327 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0500, TestSize.Level0)
328 {
329 OH_CryptoAsymKeyGenerator *ctx = nullptr;
330 OH_CryptoKeyPair *dupKeyPair = nullptr;
331 OH_Crypto_ErrCode ret;
332 ret = OH_CryptoAsymKeyGenerator_Create("ECC521", &ctx);
333 EXPECT_EQ(ret, CRYPTO_SUCCESS);
334 uint8_t array[] = { 48, 90, 48, 20, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 9, 43, 36, 3,
335 3, 2, 8, 1, 1, 7, 3, 66, 0, 4, 143, 39, 57, 249, 145, 50, 63, 222, 35, 70, 178, 121,
336 202, 154, 21, 146, 129, 75, 76, 63, 8, 195, 157, 111, 40, 217, 215, 148, 120, 224,
337 205, 82, 83, 92, 185, 21, 211, 184, 5, 19, 114, 33, 86, 85, 228, 123, 242, 206, 200,
338 98, 178, 184, 130, 35, 232, 45, 5, 202, 189, 11, 46, 163, 156, 152};
339 Crypto_DataBlob dataBlob = { .data = array, .len = sizeof(array) };
340 ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &dataBlob, nullptr, &dupKeyPair);
341 ASSERT_EQ(ret, CRYPTO_SUCCESS);
342 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(dupKeyPair);
343 ASSERT_NE(pubKey, nullptr);
344
345 OH_CryptoAsymKeyGenerator_Destroy(ctx);
346 OH_CryptoKeyPair_Destroy(dupKeyPair);
347 }
348
349 /**
350 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0600
351 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0600
352 * @tc.desc algorithm is Asym Key
353 * @tc.size Medium
354 * @tc.type Func
355 * @tc.level Level0
356 */
357 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0600, TestSize.Level0)
358 {
359 OH_CryptoAsymKeyGenerator *ctx = nullptr;
360 OH_CryptoKeyPair *keyPair = nullptr;
361 Crypto_DataBlob dsaPBlob = { .data = nullptr, .len = 0 };
362 Crypto_DataBlob dsaQBlob = { .data = nullptr, .len = 0 };
363 Crypto_DataBlob dsaGBlob = { .data = nullptr, .len = 0 };
364 Crypto_DataBlob dsaPKBlob = { .data = nullptr, .len = 0 };
365 Crypto_DataBlob dsaSKBlob = { .data = nullptr, .len = 0 };
366
367 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"DSA1024", &ctx), CRYPTO_SUCCESS);
368 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair), CRYPTO_SUCCESS);
369 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
370
371 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_P_DATABLOB, &dsaPBlob), CRYPTO_SUCCESS);
372 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_Q_DATABLOB, &dsaQBlob), CRYPTO_SUCCESS);
373 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_G_DATABLOB, &dsaGBlob), CRYPTO_SUCCESS);
374 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_SK_DATABLOB, &dsaSKBlob), CRYPTO_INVALID_PARAMS);
375 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_PK_DATABLOB, &dsaPKBlob), CRYPTO_SUCCESS);
376
377 OH_Crypto_FreeDataBlob(&dsaPBlob);
378 OH_Crypto_FreeDataBlob(&dsaQBlob);
379 OH_Crypto_FreeDataBlob(&dsaGBlob);
380 OH_Crypto_FreeDataBlob(&dsaPKBlob);
381 OH_Crypto_FreeDataBlob(&dsaSKBlob);
382 OH_CryptoAsymKeyGenerator_Destroy(ctx);
383 OH_CryptoKeyPair_Destroy(keyPair);
384 }
385
386 /**
387 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0700
388 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0700
389 * @tc.desc algorithm is Asym Key
390 * @tc.size Medium
391 * @tc.type Func
392 * @tc.level Level0
393 */
394 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0700, TestSize.Level0)
395 {
396 OH_CryptoAsymKeyGenerator *ctx = nullptr;
397 OH_CryptoKeyPair *keyPair = nullptr;
398 Crypto_DataBlob ecc_FP_PBlob = { .data = nullptr, .len = 0 };
399 Crypto_DataBlob ecc_ABlob = { .data = nullptr, .len = 0 };
400 Crypto_DataBlob ecc_BBlob = { .data = nullptr, .len = 0 };
401 Crypto_DataBlob ecc_G_XBlob = { .data = nullptr, .len = 0 };
402 Crypto_DataBlob ecc_G_YBlob = { .data = nullptr, .len = 0 };
403 Crypto_DataBlob ecc_NBlob = { .data = nullptr, .len = 0 };
404 Crypto_DataBlob ecc_SKBlob = { .data = nullptr, .len = 0 };
405 Crypto_DataBlob ecc_PK_XBlob = { .data = nullptr, .len = 0 };
406 Crypto_DataBlob ecc_PK_YBlob = { .data = nullptr, .len = 0 };
407 Crypto_DataBlob ecc_HBlob = { .data = nullptr, .len = 0 };
408 Crypto_DataBlob ecc_FIELD_TYPEBlob = { .data = nullptr, .len = 0 };
409 Crypto_DataBlob ecc_FELD_SIZEBlob = { .data = nullptr, .len = 0 };
410 Crypto_DataBlob ecc_NAMEBlob = { .data = nullptr, .len = 0 };
411
412 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"ECC224", &ctx), CRYPTO_SUCCESS);
413 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair), CRYPTO_SUCCESS);
414 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
415 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_FP_P_DATABLOB, &ecc_FP_PBlob), CRYPTO_SUCCESS);
416 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_A_DATABLOB, &ecc_ABlob), CRYPTO_SUCCESS);
417 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_B_DATABLOB, &ecc_BBlob), CRYPTO_SUCCESS);
418 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_G_X_DATABLOB, &ecc_G_XBlob), CRYPTO_SUCCESS);
419 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_G_Y_DATABLOB, &ecc_G_YBlob), CRYPTO_SUCCESS);
420 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_N_DATABLOB, &ecc_NBlob), CRYPTO_SUCCESS);
421 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_H_INT, &ecc_HBlob), CRYPTO_SUCCESS);
422 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_SK_DATABLOB, &ecc_SKBlob), CRYPTO_INVALID_PARAMS);
423 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_PK_X_DATABLOB, &ecc_PK_XBlob), CRYPTO_SUCCESS);
424 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_PK_Y_DATABLOB, &ecc_PK_YBlob), CRYPTO_SUCCESS);
425 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_FIELD_TYPE_STR, &ecc_FIELD_TYPEBlob), CRYPTO_SUCCESS);
426 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_FIELD_SIZE_INT, &ecc_FELD_SIZEBlob), CRYPTO_SUCCESS);
427 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ECC_CURVE_NAME_STR, &ecc_NAMEBlob), CRYPTO_SUCCESS);
428
429 OH_Crypto_FreeDataBlob(&ecc_FP_PBlob);
430 OH_Crypto_FreeDataBlob(&ecc_ABlob);
431 OH_Crypto_FreeDataBlob(&ecc_BBlob);
432 OH_Crypto_FreeDataBlob(&ecc_G_XBlob);
433 OH_Crypto_FreeDataBlob(&ecc_G_YBlob);
434 OH_Crypto_FreeDataBlob(&ecc_NBlob);
435 OH_Crypto_FreeDataBlob(&ecc_SKBlob);
436 OH_Crypto_FreeDataBlob(&ecc_PK_XBlob);
437 OH_Crypto_FreeDataBlob(&ecc_PK_YBlob);
438 OH_Crypto_FreeDataBlob(&ecc_HBlob);
439 OH_Crypto_FreeDataBlob(&ecc_NAMEBlob);
440 OH_Crypto_FreeDataBlob(&ecc_FIELD_TYPEBlob);
441 OH_Crypto_FreeDataBlob(&ecc_FELD_SIZEBlob);
442 OH_CryptoAsymKeyGenerator_Destroy(ctx);
443 OH_CryptoKeyPair_Destroy(keyPair);
444 }
445
446 /**
447 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0800
448 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0800
449 * @tc.desc algorithm is Asym Key
450 * @tc.size Medium
451 * @tc.type Func
452 * @tc.level Level0
453 */
454 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0800, TestSize.Level0)
455 {
456 OH_CryptoAsymKeyGenerator *ctx = nullptr;
457 OH_CryptoKeyPair *keyPair = nullptr;
458 Crypto_DataBlob rsaNBlob = { .data = nullptr, .len = 0 };
459 Crypto_DataBlob rsaDBlob = { .data = nullptr, .len = 0 };
460 Crypto_DataBlob rsaEBlob = { .data = nullptr, .len = 0 };
461
462 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"RSA512", &ctx), CRYPTO_SUCCESS);
463 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair), CRYPTO_SUCCESS);
464 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
465 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_RSA_N_DATABLOB, &rsaNBlob), CRYPTO_SUCCESS);
466 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_RSA_D_DATABLOB, &rsaDBlob), CRYPTO_INVALID_PARAMS);
467 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_RSA_E_DATABLOB, &rsaEBlob), CRYPTO_SUCCESS);
468
469 OH_Crypto_FreeDataBlob(&rsaNBlob);
470 OH_Crypto_FreeDataBlob(&rsaDBlob);
471 OH_Crypto_FreeDataBlob(&rsaEBlob);
472 OH_CryptoAsymKeyGenerator_Destroy(ctx);
473 OH_CryptoKeyPair_Destroy(keyPair);
474 }
475
476 /**
477 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0900
478 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0900
479 * @tc.desc algorithm is Asym Key
480 * @tc.size Medium
481 * @tc.type Func
482 * @tc.level Level0
483 */
484 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_0900, TestSize.Level0)
485 {
486 OH_CryptoAsymKeyGenerator *ctx = nullptr;
487 OH_CryptoKeyPair *keyPair = nullptr;
488 Crypto_DataBlob dhPBlob = { .data = nullptr, .len = 0 };
489 Crypto_DataBlob dhGBlob = { .data = nullptr, .len = 0 };
490 Crypto_DataBlob dhLBlob = { .data = nullptr, .len = 0 };
491 Crypto_DataBlob dhSKBlob = { .data = nullptr, .len = 0 };
492 Crypto_DataBlob dhPKBlob = { .data = nullptr, .len = 0 };
493
494 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"DH_modp1536", &ctx), CRYPTO_SUCCESS);
495 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair), CRYPTO_SUCCESS);
496 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
497 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DH_P_DATABLOB, &dhPBlob), CRYPTO_SUCCESS);
498 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DH_G_DATABLOB, &dhGBlob), CRYPTO_SUCCESS);
499 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DH_L_INT, &dhLBlob), CRYPTO_NOT_SUPPORTED);
500 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DH_SK_DATABLOB, &dhSKBlob), CRYPTO_INVALID_PARAMS);
501 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DH_PK_DATABLOB, &dhPKBlob), CRYPTO_SUCCESS);
502
503 OH_Crypto_FreeDataBlob(&dhPBlob);
504 OH_Crypto_FreeDataBlob(&dhGBlob);
505 OH_Crypto_FreeDataBlob(&dhLBlob);
506 OH_Crypto_FreeDataBlob(&dhSKBlob);
507 OH_Crypto_FreeDataBlob(&dhPKBlob);
508 OH_CryptoAsymKeyGenerator_Destroy(ctx);
509 OH_CryptoKeyPair_Destroy(keyPair);
510 }
511
512 /**
513 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_1000
514 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_1000
515 * @tc.desc algorithm is Asym Key
516 * @tc.size Medium
517 * @tc.type Func
518 * @tc.level Level0
519 */
520 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_1000, TestSize.Level0)
521 {
522 OH_CryptoAsymKeyGenerator *ctx = nullptr;
523 OH_CryptoKeyPair *keyPair = nullptr;
524 Crypto_DataBlob ed25519SKBlob = { .data = nullptr, .len = 0 };
525 Crypto_DataBlob ed25519PKBlob = { .data = nullptr, .len = 0 };
526
527 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"Ed25519", &ctx), CRYPTO_SUCCESS);
528 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair), CRYPTO_SUCCESS);
529 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
530 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ED25519_SK_DATABLOB, &ed25519SKBlob), CRYPTO_INVALID_PARAMS);
531 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_ED25519_PK_DATABLOB, &ed25519PKBlob), CRYPTO_SUCCESS);
532
533 OH_Crypto_FreeDataBlob(&ed25519SKBlob);
534 OH_Crypto_FreeDataBlob(&ed25519PKBlob);
535 OH_CryptoAsymKeyGenerator_Destroy(ctx);
536 OH_CryptoKeyPair_Destroy(keyPair);
537 }
538
539 /**
540 * @tc.number SUB_Security_CryptoFramework_NAPI_AsymKey_Test_1100
541 * @tc.name OHCryptoFrameworkAsymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_AsymKey_Test_1100
542 * @tc.desc algorithm is Asym Key
543 * @tc.size Medium
544 * @tc.type Func
545 * @tc.level Level0
546 */
547 HWTEST_F(OHCryptoFrameworkAsymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_AsymKey_Test_1100, TestSize.Level0)
548 {
549 OH_CryptoAsymKeyGenerator *ctx = nullptr;
550 OH_CryptoKeyPair *keyPair = nullptr;
551 Crypto_DataBlob x25519SKBlob = { .data = nullptr, .len = 0 };
552 Crypto_DataBlob x25519PKBlob = { .data = nullptr, .len = 0 };
553
554 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"X25519", &ctx), CRYPTO_SUCCESS);
555 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair), CRYPTO_SUCCESS);
556 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
557 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_X25519_SK_DATABLOB, &x25519SKBlob), CRYPTO_INVALID_PARAMS);
558 ASSERT_EQ(OH_CryptoPubKey_GetParam(pubKey, CRYPTO_X25519_PK_DATABLOB, &x25519PKBlob), CRYPTO_SUCCESS);
559
560 OH_Crypto_FreeDataBlob(&x25519SKBlob);
561 OH_Crypto_FreeDataBlob(&x25519PKBlob);
562 OH_CryptoAsymKeyGenerator_Destroy(ctx);
563 OH_CryptoKeyPair_Destroy(keyPair);
564 }
565
566 } // namespace Unittest::CryptoFrameworkAsymKeyNapiTest
567