• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_sym_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::CryptoFrameworkSymKeyNapiTest {
28 class OHCryptoFrameworkSymKeyNapiTest : 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 OHCryptoFrameworkSymKeyNapiTest::SetUpTestCase(void)
40 {
41 }
42 
TearDownTestCase(void)43 void OHCryptoFrameworkSymKeyNapiTest::TearDownTestCase(void)
44 {
45 }
46 
SetUp()47 void OHCryptoFrameworkSymKeyNapiTest::SetUp()
48 {
49 }
50 
TearDown()51 void OHCryptoFrameworkSymKeyNapiTest::TearDown()
52 {
53 }
54 
55 class OHCryptoFrameworkSymKeyConvertNapiTest : public testing::Test {
56 public:
SetUpTestCase()57     static void SetUpTestCase() {};
58 
TearDownTestCase()59     static void TearDownTestCase() {};
60 };
61 
62 class SYMKEY_TEST : public testing::TestWithParam<SymKeySpec> {
63     public:
SetUpTestCase()64         static void SetUpTestCase() {}
TearDownTestCase()65         static void TearDownTestCase() {}
66 };
67 
68 class SYMKEY_CONVERT_TEST : public testing::TestWithParam<SymKeySpec> {
69     public:
SetUpTestCase()70         static void SetUpTestCase() {}
TearDownTestCase()71         static void TearDownTestCase() {}
72 };
73 
74 SymKeySpec g_symKeyData[] = {
75     {(uint8_t *)"AES128", OH_CRYPTO_AES_128_KEY_SIZE, (uint8_t *)"AES128"},
76     {(uint8_t *)"AES192", OH_CRYPTO_AES_192_KEY_SIZE, (uint8_t *)"AES192"},
77     {(uint8_t *)"AES256", OH_CRYPTO_AES_256_KEY_SIZE, (uint8_t *)"AES256"},
78     {(uint8_t *)"3DES192", OH_CRYPTO_3DES_192_KEY_SIZE, (uint8_t *)"3DES192"},
79     {(uint8_t *)"SM4_128", OH_CRYPTO_SM4_128_KEY_SIZE, (uint8_t *)"SM4128"},
80     {(uint8_t *)"HMAC|SHA1", OH_CRYPTO_HMAC_SHA1_KEY_SIZE, (uint8_t *)"HMAC160"},
81     {(uint8_t *)"HMAC|SHA224", OH_CRYPTO_HMAC_SHA224_KEY_SIZE, (uint8_t *)"HMAC224"},
82     {(uint8_t *)"HMAC|SHA256", OH_CRYPTO_HMAC_SHA256_KEY_SIZE, (uint8_t *)"HMAC256"},
83     {(uint8_t *)"HMAC|SHA384", OH_CRYPTO_HMAC_SHA384_KEY_SIZE, (uint8_t *)"HMAC384"},
84     {(uint8_t *)"HMAC|SHA512", OH_CRYPTO_HMAC_SHA512_KEY_SIZE, (uint8_t *)"HMAC512"},
85     {(uint8_t *)"HMAC|SM3", OH_CRYPTO_HMAC_SM3_KEY_SIZE, (uint8_t *)"HMAC256"},
86 };
87 
88 SymKeySpec g_symKeyConvertData[] = {
89     {(uint8_t *)"HMAC", 1, (uint8_t *)"HMAC8"},
90     {(uint8_t *)"HMAC", 16, (uint8_t *)"HMAC128"},
91     {(uint8_t *)"HMAC", 64, (uint8_t *)"HMAC512"},
92     {(uint8_t *)"HMAC", 1024, (uint8_t *)"HMAC8192"},
93     {(uint8_t *)"HMAC", 4096, (uint8_t *)"HMAC32768"},
94 };
95 
96 INSTANTIATE_TEST_CASE_P(OHCryptoFrameworkSymKeyNapiTest, SYMKEY_TEST, ::testing::ValuesIn(g_symKeyData));
97 
98 /**
99 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0100
100 * @tc.name OHCryptoFrameworkSymKeyNapiTest/SYMKEY_TEST.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0100/x
101 * @tc.desc algorithm is Sym Key
102 * @tc.size Medium
103 * @tc.type Func
104 * @tc.level Level0
105 */
106 HWTEST_P(SYMKEY_TEST, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0100, TestSize.Level0)
107 {
108     SymKeySpec symKeyData = GetParam();
109     OH_CryptoSymKeyGenerator *ctx = nullptr;
110     OH_CryptoSymKey *keyCtx = nullptr;
111     OH_CryptoSymKey *convertKeyCtx = nullptr;
112     OH_Crypto_ErrCode ret;
113     Crypto_DataBlob out = {.data = nullptr, .len = 0};
114     Crypto_DataBlob convertOut = {.data = nullptr, .len = 0};
115 
116     ret = OH_CryptoSymKeyGenerator_Create((const char *)symKeyData.algName, &ctx);
117     EXPECT_EQ(ret, CRYPTO_SUCCESS);
118     ret = OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx);
119     EXPECT_EQ(ret, CRYPTO_SUCCESS);
120     const char *algoName1 = OH_CryptoSymKeyGenerator_GetAlgoName(ctx);
121     EXPECT_TRUE(memcmp((const char *)symKeyData.algName, algoName1, strlen(algoName1)) == 0);
122     const char *algoName2 = OH_CryptoSymKey_GetAlgoName(keyCtx);
123     EXPECT_TRUE(memcmp((const char *)symKeyData.getAlgName, algoName2, strlen(algoName2)) == 0);
124     ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out);
125     EXPECT_EQ(ret, CRYPTO_SUCCESS);
126     ret = OH_CryptoSymKeyGenerator_Convert(ctx, &out, &convertKeyCtx);
127     EXPECT_EQ(ret, CRYPTO_SUCCESS);
128     ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &convertOut);
129     EXPECT_EQ(ret, CRYPTO_SUCCESS);
130     EXPECT_TRUE(memcmp(out.data, convertOut.data, out.len) == 0);
131 
132     OH_CryptoSymKeyGenerator_Destroy(ctx);
133     OH_CryptoSymKey_Destroy(keyCtx);
134     OH_CryptoSymKey_Destroy(convertKeyCtx);
135     HcfBlobDataClearAndFree((HcfBlob *)&out);
136     HcfBlobDataClearAndFree((HcfBlob *)&convertOut);
137 }
138 
139 INSTANTIATE_TEST_CASE_P(OHCryptoFrameworkSymKeyConvertNapiTest, SYMKEY_CONVERT_TEST, ::testing::ValuesIn(g_symKeyConvertData));
140 
141 /**
142 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0200
143 * @tc.name OHCryptoFrameworkSymKeyConvertNapiTest/SYMKEY_CONVERT_TEST.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0200/x
144 * @tc.desc algorithm is Sym Key
145 * @tc.size Medium
146 * @tc.type Func
147 * @tc.level Level0
148 */
149 HWTEST_P(SYMKEY_CONVERT_TEST, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0200, TestSize.Level0)
150 {
151     SymKeySpec symKeyData = GetParam();
152     OH_CryptoSymKeyGenerator *ctx = nullptr;
153     OH_CryptoSymKey *convertKeyCtx = nullptr;
154     OH_Crypto_ErrCode ret;
155 
156     uint8_t *testData = (uint8_t *)malloc(symKeyData.bits);
157     Crypto_DataBlob in = {
158         .data = reinterpret_cast<uint8_t *>(testData),
159         .len = symKeyData.bits
160     };
161     Crypto_DataBlob convertOut = {.data = nullptr, .len = 0};
162 
163     ret = OH_CryptoSymKeyGenerator_Create((const char *)symKeyData.algName, &ctx);
164     EXPECT_EQ(ret, CRYPTO_SUCCESS);
165     ret = OH_CryptoSymKeyGenerator_Convert(ctx, &in, &convertKeyCtx);
166     EXPECT_EQ(ret, CRYPTO_SUCCESS);
167     const char *algoName = OH_CryptoSymKey_GetAlgoName(convertKeyCtx);
168     EXPECT_TRUE(memcmp((const char *)symKeyData.getAlgName, algoName, strlen(algoName)) == 0);
169     ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &convertOut);
170     EXPECT_EQ(ret, CRYPTO_SUCCESS);
171     EXPECT_TRUE(memcmp(in.data, convertOut.data, in.len) == 0);
172 
173     OH_CryptoSymKeyGenerator_Destroy(ctx);
174     OH_CryptoSymKey_Destroy(convertKeyCtx);
175     HcfBlobDataClearAndFree((HcfBlob *)&convertOut);
176 }
177 
178 /**
179 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0300
180 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0300
181 * @tc.desc algorithm is Sym Key
182 * @tc.size Medium
183 * @tc.type Func
184 * @tc.level Level0
185 */
186 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0300, TestSize.Level0)
187 {
188     OH_CryptoSymKeyGenerator *ctx = nullptr;
189 
190     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create((const char *)"RC4", &ctx), CRYPTO_NOT_SUPPORTED);
191     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create((const char *)"AES128", nullptr), CRYPTO_INVALID_PARAMS);
192     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create((const char *)"aes128", &ctx), CRYPTO_NOT_SUPPORTED); // 返回码有问题
193     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create(nullptr, &ctx), CRYPTO_INVALID_PARAMS);
194     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create((const char *)"AES225", &ctx), CRYPTO_NOT_SUPPORTED);
195 }
196 
197 /**
198 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0400
199 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0400
200 * @tc.desc algorithm is Sym Key
201 * @tc.size Medium
202 * @tc.type Func
203 * @tc.level Level0
204 */
205 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0400, TestSize.Level0)
206 {
207     OH_CryptoSymKeyGenerator *ctx = nullptr;
208     OH_CryptoSymKey *keyCtx = nullptr;
209 
210     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create((const char *)"AES256", &ctx), CRYPTO_SUCCESS);
211     EXPECT_EQ(OH_CryptoSymKeyGenerator_Generate(nullptr, &keyCtx), CRYPTO_INVALID_PARAMS);
212     EXPECT_EQ(OH_CryptoSymKeyGenerator_Generate(ctx, nullptr), CRYPTO_INVALID_PARAMS);
213     EXPECT_EQ(OH_CryptoSymKeyGenerator_Generate(nullptr, nullptr), CRYPTO_INVALID_PARAMS);
214 
215     OH_CryptoSymKeyGenerator_Destroy(ctx);
216 }
217 
218 /**
219 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0500
220 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0500
221 * @tc.desc algorithm is Sym Key
222 * @tc.size Medium
223 * @tc.type Func
224 * @tc.level Level0
225 */
226 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0500, TestSize.Level0)
227 {
228     OH_CryptoSymKeyGenerator *ctx = nullptr;
229 
230     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create((const char *)"HMAC|SHA256", &ctx), CRYPTO_SUCCESS);
231     EXPECT_EQ(OH_CryptoSymKeyGenerator_GetAlgoName(nullptr), nullptr);
232 
233     OH_CryptoSymKeyGenerator_Destroy(ctx);
234 }
235 
236 /**
237 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0600
238 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0600
239 * @tc.desc algorithm is Sym Key
240 * @tc.size Medium
241 * @tc.type Func
242 * @tc.level Level0
243 */
244 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0600, TestSize.Level0)
245 {
246     OH_CryptoSymKeyGenerator *ctx = nullptr;
247     OH_CryptoSymKey *convertKeyCtx = nullptr;
248     const char *algName = "HMAC|SHA256";
249 
250     uint8_t *testData1 = (uint8_t *)malloc(OH_CRYPTO_HMAC_SHA256_KEY_SIZE / 8 - 2);
251     Crypto_DataBlob in1 = {
252         .data = reinterpret_cast<uint8_t *>(testData1),
253         .len = OH_CRYPTO_HMAC_SHA256_KEY_SIZE / 8 - 2
254     };
255     uint8_t *testData2 = (uint8_t *)malloc(OH_CRYPTO_HMAC_SHA256_KEY_SIZE / 8 + 2);
256     Crypto_DataBlob in2 = {
257         .data = reinterpret_cast<uint8_t *>(testData2),
258         .len = OH_CRYPTO_HMAC_SHA256_KEY_SIZE / 8 + 2
259     };
260     uint8_t *testData3 = (uint8_t *)malloc(OH_CRYPTO_HMAC_SHA256_KEY_SIZE / 8);
261     Crypto_DataBlob in3 = {
262         .data = reinterpret_cast<uint8_t *>(testData3),
263         .len = OH_CRYPTO_HMAC_SHA256_KEY_SIZE / 8
264     };
265 
266     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create(algName, &ctx), CRYPTO_SUCCESS);
267     EXPECT_EQ(OH_CryptoSymKeyGenerator_Convert(nullptr, &in3, &convertKeyCtx), CRYPTO_INVALID_PARAMS);
268     EXPECT_EQ(OH_CryptoSymKeyGenerator_Convert(ctx, nullptr, &convertKeyCtx), CRYPTO_INVALID_PARAMS);
269     EXPECT_EQ(OH_CryptoSymKeyGenerator_Convert(ctx, &in3, nullptr), CRYPTO_INVALID_PARAMS);
270     EXPECT_EQ(OH_CryptoSymKeyGenerator_Convert(ctx, &in1, &convertKeyCtx), CRYPTO_INVALID_PARAMS);
271     EXPECT_EQ(OH_CryptoSymKeyGenerator_Convert(ctx, &in2, &convertKeyCtx), CRYPTO_INVALID_PARAMS);
272 
273     OH_CryptoSymKeyGenerator_Destroy(ctx);
274     OH_CryptoSymKey_Destroy(convertKeyCtx);
275 }
276 
277 /**
278 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0700
279 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0700
280 * @tc.desc algorithm is Sym Key
281 * @tc.size Medium
282 * @tc.type Func
283 * @tc.level Level0
284 */
285 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0700, TestSize.Level0)
286 {
287     OH_CryptoSymKeyGenerator *ctx = nullptr;
288     OH_CryptoSymKey *keyCtx = nullptr;
289     Crypto_DataBlob out = {.data = nullptr, .len = 0};
290 
291     EXPECT_EQ(OH_CryptoSymKeyGenerator_Create((const char *)"AES192", &ctx), CRYPTO_SUCCESS);
292     EXPECT_EQ(OH_CryptoSymKeyGenerator_Generate(ctx, &keyCtx), CRYPTO_SUCCESS);
293     EXPECT_EQ(OH_CryptoSymKey_GetKeyData(nullptr, &out), CRYPTO_INVALID_PARAMS);
294     EXPECT_EQ(OH_CryptoSymKey_GetKeyData(keyCtx, nullptr), CRYPTO_INVALID_PARAMS);
295 
296     OH_CryptoSymKeyGenerator_Destroy(ctx);
297     OH_CryptoSymKey_Destroy(keyCtx);
298 }
299 
300 /**
301 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0800
302 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0800
303 * @tc.desc algorithm is Sym Key
304 * @tc.size Medium
305 * @tc.type Func
306 * @tc.level Level0
307 */
308 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0800, TestSize.Level0)
309 {
310     const char *algName = "3DES192";
311     OH_CryptoSymKeyGenerator *ctx = nullptr;
312     OH_CryptoSymKey *convertKeyCtx = nullptr;
313     OH_Crypto_ErrCode ret;
314 
315     uint8_t arr[] = {
316         0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
317         0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
318         0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72
319     };
320     Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
321     Crypto_DataBlob convertOut = {.data = nullptr, .len = 0};
322 
323     ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
324     EXPECT_EQ(ret, CRYPTO_SUCCESS);
325     ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
326     EXPECT_EQ(ret, CRYPTO_SUCCESS);
327     ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &convertOut);
328     EXPECT_EQ(ret, CRYPTO_SUCCESS);
329     EXPECT_TRUE(memcmp(convertBlob.data, convertOut.data, convertBlob.len) == 0);
330 
331     OH_CryptoSymKeyGenerator_Destroy(ctx);
332     OH_CryptoSymKey_Destroy(convertKeyCtx);
333     HcfBlobDataClearAndFree((HcfBlob *)&convertOut);
334 }
335 
336 /**
337 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_0900
338 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_0900
339 * @tc.desc algorithm is Sym Key
340 * @tc.size Medium
341 * @tc.type Func
342 * @tc.level Level0
343 */
344 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_0900, TestSize.Level0)
345 {
346     const char *algName = "3DES192";
347     OH_CryptoSymKeyGenerator *ctx = nullptr;
348     OH_CryptoSymKey *convertKeyCtx = nullptr;
349     OH_Crypto_ErrCode ret;
350 
351     uint8_t arr[] = {
352         0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
353         0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
354         0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30,
355     };
356     Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
357 
358     ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
359     EXPECT_EQ(ret, CRYPTO_SUCCESS);
360     ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
361     EXPECT_EQ(ret, CRYPTO_INVALID_PARAMS);
362 
363     OH_CryptoSymKeyGenerator_Destroy(ctx);
364 }
365 
366 /**
367 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_1000
368 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_1000
369 * @tc.desc algorithm is Sym Key
370 * @tc.size Medium
371 * @tc.type Func
372 * @tc.level Level0
373 */
374 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_1000, TestSize.Level0)
375 {
376     const char *algName = "SM4_128";
377     OH_CryptoSymKeyGenerator *ctx = nullptr;
378     OH_CryptoSymKey *convertKeyCtx = nullptr;
379     OH_Crypto_ErrCode ret;
380 
381     uint8_t arr[] = {
382         0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
383         0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
384         0xba,
385     };
386     Crypto_DataBlob convertBlob = {.data = arr, .len = sizeof(arr)};
387 
388     ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
389     EXPECT_EQ(ret, CRYPTO_SUCCESS);
390     ret = OH_CryptoSymKeyGenerator_Convert(ctx, &convertBlob, &convertKeyCtx);
391     EXPECT_EQ(ret, CRYPTO_INVALID_PARAMS);
392 
393     OH_CryptoSymKeyGenerator_Destroy(ctx);
394 }
395 
396 /**
397 * @tc.number SUB_Security_CryptoFramework_NAPI_SymKey_Test_1100
398 * @tc.name OHCryptoFrameworkSymKeyNapiTest.SUB_Security_CryptoFramework_NAPI_SymKey_Test_1100
399 * @tc.desc algorithm is Sym Key
400 * @tc.size Medium
401 * @tc.type Func
402 * @tc.level Level0
403 */
404 HWTEST_F(OHCryptoFrameworkSymKeyNapiTest, SUB_Security_CryptoFramework_NAPI_SymKey_Test_1100, TestSize.Level0)
405 {
406     const char *genAlgName = "HMAC|SHA256";
407     const char *conertAlgName = "AES256";
408     OH_CryptoSymKeyGenerator *genCtx = nullptr;
409     OH_CryptoSymKeyGenerator *convertCtx = nullptr;
410     OH_CryptoSymKey *keyCtx = nullptr;
411     OH_CryptoSymKey *convertKeyCtx = nullptr;
412     OH_Crypto_ErrCode ret;
413     Crypto_DataBlob out = {.data = nullptr, .len = 0};
414     Crypto_DataBlob convertOut = {.data = nullptr, .len = 0};
415 
416     ret = OH_CryptoSymKeyGenerator_Create(genAlgName, &genCtx);
417     EXPECT_EQ(ret, CRYPTO_SUCCESS);
418     ret = OH_CryptoSymKeyGenerator_Generate(genCtx, &keyCtx);
419     EXPECT_EQ(ret, CRYPTO_SUCCESS);
420     ret = OH_CryptoSymKey_GetKeyData(keyCtx, &out);
421     EXPECT_EQ(ret, CRYPTO_SUCCESS);
422     ret = OH_CryptoSymKeyGenerator_Create(conertAlgName, &convertCtx);
423     EXPECT_EQ(ret, CRYPTO_SUCCESS);
424     ret = OH_CryptoSymKeyGenerator_Convert(convertCtx, &out, &convertKeyCtx);
425     EXPECT_EQ(ret, CRYPTO_SUCCESS);
426     ret = OH_CryptoSymKey_GetKeyData(convertKeyCtx, &convertOut);
427     EXPECT_EQ(ret, CRYPTO_SUCCESS);
428     EXPECT_TRUE(memcmp(out.data, convertOut.data, out.len) == 0);
429 
430     OH_CryptoSymKeyGenerator_Destroy(genCtx);
431     OH_CryptoSymKeyGenerator_Destroy(convertCtx);
432     OH_CryptoSymKey_Destroy(keyCtx);
433     OH_CryptoSymKey_Destroy(convertKeyCtx);
434     HcfBlobDataClearAndFree((HcfBlob *)&out);
435     HcfBlobDataClearAndFree((HcfBlob *)&convertOut);
436 }
437 
438 } // namespace Unittest::CryptoFrameworkSymKeyNapiTest
439