• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_mac_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::CryptoFrameworkMacNapiTest {
28 class OHCryptoFrameworkMacNapiTest : public testing::Test {
29 public:
SetUpTestCase(void)30     static void SetUpTestCase(void) {};
TearDownTestCase(void)31     static void TearDownTestCase(void) {};
SetUp()32     void SetUp() {};
TearDown()33     void TearDown() {};
34 };
35 
36 class MAC_TEST : public testing::TestWithParam<MacSpec> {
37     public:
SetUpTestCase()38         static void SetUpTestCase() {};
TearDownTestCase()39         static void TearDownTestCase() {};
40 };
41 
42 class MAC_VECTOR_TEST : public testing::TestWithParam<VectorMacSpec> {
43     public:
SetUpTestCase()44         static void SetUpTestCase() {};
TearDownTestCase()45         static void TearDownTestCase() {};
46 };
47 
OHTEST_GenSymKey(const char * algName,OH_CryptoSymKey ** keyCtx)48 static OH_Crypto_ErrCode OHTEST_GenSymKey(const char *algName, OH_CryptoSymKey **keyCtx)
49 {
50     OH_CryptoSymKeyGenerator *ctx = nullptr;
51     OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
52 
53     ret = OH_CryptoSymKeyGenerator_Create(algName, &ctx);
54     if (ret != CRYPTO_SUCCESS) {
55         goto EXIT;
56     }
57     ret = OH_CryptoSymKeyGenerator_Generate(ctx, keyCtx);
58     if (ret != CRYPTO_SUCCESS) {
59         goto EXIT;
60     }
61 
62 EXIT:
63     OH_CryptoSymKeyGenerator_Destroy(ctx);
64     return ret;
65 }
66 
OHTEST_CryptoCmac_SetParam(OH_CryptoMac * ctx,const char * cipherName)67 static OH_Crypto_ErrCode OHTEST_CryptoCmac_SetParam(OH_CryptoMac *ctx, const char *cipherName)
68 {
69     OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
70     const Crypto_DataBlob value = {
71         .data = (uint8_t *)cipherName,
72         .len = strlen(cipherName)
73     };
74 
75     ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &value);
76 
77     return ret;
78 }
79 
OHTEST_CryptoHmac_SetParam(OH_CryptoMac * ctx,const char * degistName)80 static OH_Crypto_ErrCode OHTEST_CryptoHmac_SetParam(OH_CryptoMac *ctx, const char *degistName)
81 {
82     OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
83     const Crypto_DataBlob value = {
84         .data = (uint8_t *)degistName,
85         .len = strlen(degistName)
86     };
87 
88     ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &value);
89 
90     return ret;
91 }
92 
OHTEST_GenRandomMsg(size_t msgLen,Crypto_DataBlob * msgBlob)93 static OH_Crypto_ErrCode OHTEST_GenRandomMsg(size_t msgLen, Crypto_DataBlob *msgBlob)
94 {
95     OH_CryptoRand *randCtx = nullptr;
96     OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
97 
98     ret = OH_CryptoRand_Create(&randCtx);
99     if (randCtx != nullptr && ret != CRYPTO_SUCCESS) {
100         goto EXIT;
101     }
102     ret = OH_CryptoRand_GenerateRandom(randCtx, msgLen, msgBlob);
103 
104 EXIT:
105     OH_CryptoRand_Destroy(randCtx);
106     return ret;
107 }
108 
OHTEST_DoMac(OH_CryptoMac ** ctx,MacSpec macInfo,OH_CryptoSymKey * key,Crypto_DataBlob * msgBlob,Crypto_DataBlob * out)109 static OH_Crypto_ErrCode OHTEST_DoMac(OH_CryptoMac **ctx, MacSpec macInfo,
110     OH_CryptoSymKey *key, Crypto_DataBlob *msgBlob, Crypto_DataBlob *out)
111 {
112     OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
113 
114     ret = OH_CryptoMac_Create(macInfo.type, ctx);
115     if (ret != CRYPTO_SUCCESS) {
116         goto EXIT;
117     }
118     if (strcmp(macInfo.type, "CMAC") == 0) {
119         ret = OHTEST_CryptoCmac_SetParam(*ctx, macInfo.paramType.cipherName);
120     } else {
121         ret = OHTEST_CryptoHmac_SetParam(*ctx, macInfo.paramType.degistName);
122     }
123     if (ret != CRYPTO_SUCCESS) {
124         goto EXIT;
125     }
126     if ((OH_CryptoMac_Init(*ctx, key) != CRYPTO_SUCCESS)
127         || (OH_CryptoMac_Update(*ctx, msgBlob) != CRYPTO_SUCCESS)
128         || (OH_CryptoMac_Final(*ctx, out) != CRYPTO_SUCCESS)) {
129         ret = CRYPTO_OPERTION_ERROR;
130         goto EXIT;
131     }
132 
133 EXIT:
134     return ret;
135 }
136 
OHTEST_DoSegmentMac(OH_CryptoMac ** ctx,MacSpec macInfo,OH_CryptoSymKey * key,int blockSize,Crypto_DataBlob * msgBlob,Crypto_DataBlob * out)137 static OH_Crypto_ErrCode OHTEST_DoSegmentMac(OH_CryptoMac **ctx,
138     MacSpec macInfo, OH_CryptoSymKey *key, int blockSize, Crypto_DataBlob *msgBlob, Crypto_DataBlob *out)
139 {
140     OH_Crypto_ErrCode ret = CRYPTO_SUCCESS;
141     int quotient = msgBlob->len / blockSize;
142     int remainder = msgBlob->len % blockSize;
143     uint8_t *data = msgBlob->data;
144     size_t msgLen = msgBlob->len;
145     Crypto_DataBlob *dataBlob = msgBlob;
146 
147     ret = OH_CryptoMac_Create(macInfo.type, ctx);
148     if (ret != CRYPTO_SUCCESS) {
149         goto EXIT;
150     }
151     if (strcmp(macInfo.type, "CMAC") == 0) {
152         ret = OHTEST_CryptoCmac_SetParam(*ctx, macInfo.paramType.cipherName);
153     } else {
154         ret = OHTEST_CryptoHmac_SetParam(*ctx, macInfo.paramType.degistName);
155     }
156     if (ret != CRYPTO_SUCCESS) {
157         goto EXIT;
158     }
159     ret = OH_CryptoMac_Init(*ctx, key);
160     if (ret != CRYPTO_SUCCESS) {
161         goto EXIT;
162     }
163     for (int i = 0; i < quotient; i++) {
164         dataBlob->len = blockSize;
165         ret = OH_CryptoMac_Update(*ctx, dataBlob);
166         if (ret != CRYPTO_SUCCESS) {
167             goto EXIT;
168         }
169         dataBlob->data += blockSize;
170     }
171     if (remainder) {
172         dataBlob->len = remainder;
173         ret = OH_CryptoMac_Update(*ctx, dataBlob);
174         if (ret != CRYPTO_SUCCESS) {
175             goto EXIT;
176         }
177     }
178     ret = OH_CryptoMac_Final(*ctx, out);
179 
180 EXIT:
181     dataBlob->data = data;
182     dataBlob->len = msgLen;
183     return ret;
184 }
185 
186 MacSpec g_macSpec[] = {
187     /* HMAC */
188     {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SHA1", .paramType.degistName = "SHA1"},
189     {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SHA224", .paramType.degistName = "SHA224"},
190     {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SHA256", .paramType.degistName = "SHA256"},
191     {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SHA384", .paramType.degistName = "SHA384"},
192     {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SHA512", .paramType.degistName = "SHA512"},
193     {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SM3", .paramType.degistName = "SM3"},
194     {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|MD5", .paramType.degistName = "MD5"},
195     {.type = OHTEST_MAC_HMAC, .algoKeyName = "AES256", .paramType.degistName = "SHA256"},
196     {.type = OHTEST_MAC_HMAC, .algoKeyName = "AES128", .paramType.degistName = "SHA256"},
197     /* CMAC */
198     {.type = OHTEST_MAC_CMAC, .algoKeyName = "AES128", .paramType.cipherName = "AES128"},
199     {.type = OHTEST_MAC_CMAC, .algoKeyName = "AES256", .paramType.cipherName = "AES256"},
200     {.type = OHTEST_MAC_CMAC, .algoKeyName = "HMAC|SHA256", .paramType.cipherName = "AES256"},
201     {.type = OHTEST_MAC_CMAC, .algoKeyName = "HMAC|MD5", .paramType.cipherName = "AES128"},
202 };
203 INSTANTIATE_TEST_CASE_P(OHCryptoFrameworkMacNapiTest, MAC_TEST, ::testing::ValuesIn(g_macSpec));
204 
205 /**
206 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0100
207 * @tc.name OHCryptoFrameworkMacNapiTest/MAC_TEST.SUB_Security_CryptoFramework_NAPI_Mac_Test_0100/x
208 * @tc.desc algorithm is Mac
209 * @tc.size Medium
210 * @tc.type Func
211 * @tc.level Level0
212 */
213 HWTEST_P(MAC_TEST, SUB_Security_CryptoFramework_NAPI_Mac_Test_0100, TestSize.Level0)
214 {
215     MacSpec macInfo = GetParam();
216     OH_CryptoSymKey *key = nullptr;
217     OH_CryptoMac *ctx = nullptr;
218     size_t msgLen = 16;
219     Crypto_DataBlob msgBlob = {.data = nullptr, .len = 0};
220     Crypto_DataBlob out = {.data = nullptr, .len = 0};
221     uint32_t macLength = 0;
222 
223     EXPECT_EQ(OHTEST_GenRandomMsg(msgLen, &msgBlob), CRYPTO_SUCCESS);
224     EXPECT_TRUE(msgBlob.len == msgLen);
225     EXPECT_EQ(OHTEST_GenSymKey(macInfo.algoKeyName, &key), CRYPTO_SUCCESS);
226     EXPECT_EQ(OH_CryptoMac_Create(macInfo.type, &ctx), CRYPTO_SUCCESS);
227     if (strcmp(macInfo.type, "CMAC") == 0) {
228         EXPECT_EQ(OHTEST_CryptoCmac_SetParam(ctx, macInfo.paramType.cipherName), CRYPTO_SUCCESS);
229     } else {
230         EXPECT_EQ(OHTEST_CryptoHmac_SetParam(ctx, macInfo.paramType.degistName), CRYPTO_SUCCESS);
231     }
232     EXPECT_EQ(OH_CryptoMac_Init(ctx, key), CRYPTO_SUCCESS);
233     EXPECT_EQ(OH_CryptoMac_Update(ctx, &msgBlob), CRYPTO_SUCCESS);
234     EXPECT_EQ(OH_CryptoMac_Final(ctx, &out), CRYPTO_SUCCESS);
235     EXPECT_EQ(OH_CryptoMac_GetLength(ctx, &macLength), CRYPTO_SUCCESS);
236     EXPECT_TRUE(out.len == macLength);
237 
238     OH_Crypto_FreeDataBlob(&out);
239     OH_Crypto_FreeDataBlob(&msgBlob);
240     OH_CryptoSymKey_Destroy(key);
241     OH_CryptoMac_Destroy(ctx);
242 }
243 
244 /**
245 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0200
246 * @tc.name OHCryptoFrameworkMacNapiTest/MAC_TEST.SUB_Security_CryptoFramework_NAPI_Mac_Test_0200/x
247 * @tc.desc algorithm is Mac
248 * @tc.size Medium
249 * @tc.type Func
250 * @tc.level Level0
251 */
252 HWTEST_P(MAC_TEST, SUB_Security_CryptoFramework_NAPI_Mac_Test_0200, TestSize.Level0)
253 {
254     MacSpec macInfo = GetParam();
255     OH_CryptoSymKey *key = nullptr;
256     OH_CryptoMac *ctx = nullptr;
257     size_t msgLen = 64;
258     Crypto_DataBlob msgBlob = {.data = nullptr, .len = 0};
259     Crypto_DataBlob out1 = {.data = nullptr, .len = 0};
260     Crypto_DataBlob out2 = {.data = nullptr, .len = 0};
261     uint32_t macLength = 0;
262     int blockSize = 30;
263 
264     EXPECT_EQ(OHTEST_GenRandomMsg(msgLen, &msgBlob), CRYPTO_SUCCESS);
265     EXPECT_TRUE(msgBlob.len == msgLen);
266     EXPECT_EQ(OHTEST_GenSymKey(macInfo.algoKeyName, &key), CRYPTO_SUCCESS);
267     EXPECT_EQ(OHTEST_DoMac(&ctx, macInfo, key, &msgBlob, &out1), CRYPTO_SUCCESS);
268     OH_CryptoMac_Destroy(ctx);
269     ctx = nullptr;
270     EXPECT_EQ(OHTEST_DoSegmentMac(&ctx, macInfo, key, blockSize, &msgBlob, &out2), CRYPTO_SUCCESS);
271     EXPECT_EQ(OH_CryptoMac_GetLength(ctx, &macLength), CRYPTO_SUCCESS);
272     EXPECT_TRUE(out1.len == macLength);
273     EXPECT_TRUE(out1.len == out2.len);
274     EXPECT_TRUE(memcmp(out2.data, out1.data, out2.len) == 0);
275 
276     OH_Crypto_FreeDataBlob(&out1);
277     OH_Crypto_FreeDataBlob(&out2);
278     OH_Crypto_FreeDataBlob(&msgBlob);
279     OH_CryptoSymKey_Destroy(key);
280     OH_CryptoMac_Destroy(ctx);
281 }
282 
283 /**
284 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0300
285 * @tc.name OHCryptoFrameworkMacNapiTest.SUB_Security_CryptoFramework_NAPI_Mac_Test_0300
286 * @tc.desc algorithm is Mac
287 * @tc.size Medium
288 * @tc.type Func
289 * @tc.level Level0
290 */
291 HWTEST_F(OHCryptoFrameworkMacNapiTest, SUB_Security_CryptoFramework_NAPI_Mac_Test_0300, TestSize.Level0)
292 {
293     OH_CryptoMac *ctx = nullptr;
294     const Crypto_DataBlob value = {
295         .data = reinterpret_cast<uint8_t *>(const_cast<char *>("SHA256")),
296         .len = strlen("SHA256")
297     };
298 
299     EXPECT_EQ(OH_CryptoMac_Create("CMAC", &ctx), CRYPTO_SUCCESS);
300     EXPECT_EQ(OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &value), CRYPTO_PARAMETER_CHECK_FAILED);
301 
302     OH_CryptoMac_Destroy(ctx);
303 }
304 
305 /**
306 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0400
307 * @tc.name OHCryptoFrameworkMacNapiTest/MAC_TEST.SUB_Security_CryptoFramework_NAPI_Mac_Test_0400/x
308 * @tc.desc algorithm is Mac
309 * @tc.size Medium
310 * @tc.type Func
311 * @tc.level Level0
312 */
313 HWTEST_P(MAC_TEST, SUB_Security_CryptoFramework_NAPI_Mac_Test_0400, TestSize.Level0)
314 {
315     MacSpec macInfo = GetParam();
316     OH_CryptoSymKey *key = nullptr;
317     OH_CryptoMac *ctx = nullptr;
318     Crypto_DataBlob out = {.data = nullptr, .len = 0};
319     uint32_t macLength = 0;
320 
321     EXPECT_EQ(OHTEST_GenSymKey(macInfo.algoKeyName, &key), CRYPTO_SUCCESS);
322     EXPECT_EQ(OH_CryptoMac_Create(macInfo.type, &ctx), CRYPTO_SUCCESS);
323     if (strcmp(macInfo.type, "CMAC") == 0) {
324         EXPECT_EQ(OHTEST_CryptoCmac_SetParam(ctx, macInfo.paramType.cipherName), CRYPTO_SUCCESS);
325     } else {
326         EXPECT_EQ(OHTEST_CryptoHmac_SetParam(ctx, macInfo.paramType.degistName), CRYPTO_SUCCESS);
327     }
328     EXPECT_EQ(OH_CryptoMac_Init(ctx, key), CRYPTO_SUCCESS);
329     EXPECT_EQ(OH_CryptoMac_Final(ctx, &out), CRYPTO_SUCCESS);
330     EXPECT_EQ(OH_CryptoMac_GetLength(ctx, &macLength), CRYPTO_SUCCESS);
331     EXPECT_TRUE(out.len == macLength);
332 
333     OH_Crypto_FreeDataBlob(&out);
334     OH_CryptoSymKey_Destroy(key);
335     OH_CryptoMac_Destroy(ctx);
336 }
337 
338 /**
339 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0500
340 * @tc.name OHCryptoFrameworkMacNapiTest.SUB_Security_CryptoFramework_NAPI_Mac_Test_0500
341 * @tc.desc algorithm is Mac
342 * @tc.size Medium
343 * @tc.type Func
344 * @tc.level Level0
345 */
346 HWTEST_F(OHCryptoFrameworkMacNapiTest, SUB_Security_CryptoFramework_NAPI_Mac_Test_0500, TestSize.Level0)
347 {
348     OH_CryptoMac *ctx = nullptr;
349     const Crypto_DataBlob value = {
350         .data = reinterpret_cast<uint8_t *>(const_cast<char *>("AES128")),
351         .len = strlen("AES128")
352     };
353 
354     EXPECT_EQ(OH_CryptoMac_Create("HMAC", &ctx), CRYPTO_SUCCESS);
355     EXPECT_EQ(OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &value), CRYPTO_PARAMETER_CHECK_FAILED);
356 
357     OH_CryptoMac_Destroy(ctx);
358 }
359 
360 /**
361 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0600
362 * @tc.name OHCryptoFrameworkMacNapiTest.SUB_Security_CryptoFramework_NAPI_Mac_Test_0600
363 * @tc.desc algorithm is Mac
364 * @tc.size Medium
365 * @tc.type Func
366 * @tc.level Level0
367 */
368 HWTEST_F(OHCryptoFrameworkMacNapiTest, SUB_Security_CryptoFramework_NAPI_Mac_Test_0600, TestSize.Level0)
369 {
370     MacSpec macInfo = {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SHA1", .paramType.degistName = "SHA1"};
371     OH_CryptoMac *ctx = nullptr;
372     size_t msgLen = 16;
373     Crypto_DataBlob msgBlob = {.data = nullptr, .len = 0};
374     Crypto_DataBlob out = {.data = nullptr, .len = 0};
375     uint32_t macLength = 0;
376 
377     EXPECT_EQ(OHTEST_GenRandomMsg(msgLen, &msgBlob), CRYPTO_SUCCESS);
378     EXPECT_TRUE(msgBlob.len == msgLen);
379     EXPECT_EQ(OH_CryptoMac_Create(macInfo.type, &ctx), CRYPTO_SUCCESS);
380     EXPECT_EQ(OHTEST_CryptoHmac_SetParam(ctx, macInfo.paramType.degistName), CRYPTO_SUCCESS);
381     EXPECT_EQ(OH_CryptoMac_Update(ctx, &msgBlob), CRYPTO_OPERTION_ERROR);
382     EXPECT_EQ(OH_CryptoMac_Final(ctx, &out), CRYPTO_OPERTION_ERROR);
383     EXPECT_EQ(OH_CryptoMac_GetLength(ctx, &macLength), CRYPTO_SUCCESS);
384 
385     OH_Crypto_FreeDataBlob(&out);
386     OH_Crypto_FreeDataBlob(&msgBlob);
387     OH_CryptoMac_Destroy(ctx);
388 }
389 
390 /**
391 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0700
392 * @tc.name OHCryptoFrameworkMacNapiTest.SUB_Security_CryptoFramework_NAPI_Mac_Test_0700
393 * @tc.desc algorithm is Mac
394 * @tc.size Medium
395 * @tc.type Func
396 * @tc.level Level0
397 */
398 HWTEST_F(OHCryptoFrameworkMacNapiTest, SUB_Security_CryptoFramework_NAPI_Mac_Test_0700, TestSize.Level0)
399 {
400     MacSpec macInfo = {.type = OHTEST_MAC_CMAC, .algoKeyName = "AES128", .paramType.cipherName = "AES128"};
401     OH_CryptoMac *ctx = nullptr;
402     size_t msgLen = 16;
403     Crypto_DataBlob msgBlob = {.data = nullptr, .len = 0};
404     Crypto_DataBlob out = {.data = nullptr, .len = 0};
405 
406     EXPECT_EQ(OHTEST_GenRandomMsg(msgLen, &msgBlob), CRYPTO_SUCCESS);
407     EXPECT_TRUE(msgBlob.len == msgLen);
408     EXPECT_EQ(OH_CryptoMac_Create(macInfo.type, &ctx), CRYPTO_SUCCESS);
409     EXPECT_EQ(OHTEST_CryptoCmac_SetParam(ctx, macInfo.paramType.cipherName), CRYPTO_SUCCESS);
410     EXPECT_EQ(OH_CryptoMac_Update(ctx, &msgBlob), CRYPTO_OPERTION_ERROR);
411     // Final、GetLength: openssl 原生接口也会出现crash
412 
413     OH_Crypto_FreeDataBlob(&out);
414     OH_Crypto_FreeDataBlob(&msgBlob);
415     OH_CryptoMac_Destroy(ctx);
416 }
417 
418 /**
419 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0800
420 * @tc.name OHCryptoFrameworkMacNapiTest.SUB_Security_CryptoFramework_NAPI_Mac_Test_0800
421 * @tc.desc algorithm is Mac
422 * @tc.size Medium
423 * @tc.type Func
424 * @tc.level Level0
425 */
426 HWTEST_F(OHCryptoFrameworkMacNapiTest, SUB_Security_CryptoFramework_NAPI_Mac_Test_0800, TestSize.Level0)
427 {
428     MacSpec macInfo = {.type = OHTEST_MAC_CMAC, .algoKeyName = "AES128", .paramType.cipherName = "AES128"};
429     OH_CryptoSymKey *key = nullptr;
430     OH_CryptoMac *ctx = nullptr;
431 
432     EXPECT_EQ(OHTEST_GenSymKey(macInfo.algoKeyName, &key), CRYPTO_SUCCESS);
433     EXPECT_EQ(OH_CryptoMac_Create(macInfo.type, &ctx), CRYPTO_SUCCESS);
434     EXPECT_EQ(OH_CryptoMac_Init(ctx, key), CRYPTO_PARAMETER_CHECK_FAILED);
435 
436     OH_CryptoSymKey_Destroy(key);
437     OH_CryptoMac_Destroy(ctx);
438 }
439 
440 /**
441 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Test_0900
442 * @tc.name OHCryptoFrameworkMacNapiTest.SUB_Security_CryptoFramework_NAPI_Mac_Test_0900
443 * @tc.desc algorithm is Mac
444 * @tc.size Medium
445 * @tc.type Func
446 * @tc.level Level0
447 */
448 HWTEST_F(OHCryptoFrameworkMacNapiTest, SUB_Security_CryptoFramework_NAPI_Mac_Test_0900, TestSize.Level0)
449 {
450     MacSpec macInfo = {.type = OHTEST_MAC_HMAC, .algoKeyName = "HMAC|SHA1", .paramType.degistName = "SHA1"};
451     OH_CryptoSymKey *key = nullptr;
452     OH_CryptoMac *ctx = nullptr;
453 
454     EXPECT_EQ(OHTEST_GenSymKey(macInfo.algoKeyName, &key), CRYPTO_SUCCESS);
455     EXPECT_EQ(OH_CryptoMac_Create(macInfo.type, &ctx), CRYPTO_SUCCESS);
456     EXPECT_EQ(OH_CryptoMac_Init(ctx, key), CRYPTO_PARAMETER_CHECK_FAILED);
457 
458     OH_CryptoSymKey_Destroy(key);
459     OH_CryptoMac_Destroy(ctx);
460 }
461 
462 static VectorMacSpec g_vectors[] = {
463     {
464         .macType = "CMAC",
465         .vectorSource = "RFC4493",
466         .algoName = "AES128",
467         .key = {
468             0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
469         },
470         .keyLen = 16,
471         .msg = {0},
472         .msgLen = 0,
473         .expectMac = {
474             0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28, 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
475         },
476         .macLen = 16
477     },
478     {
479         .macType = "CMAC",
480         .vectorSource = "RFC4493",
481         .algoName = "AES128",
482         .key = {
483             0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
484         },
485         .keyLen = 16,
486         .msg = {
487             0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
488         },
489         .msgLen = 16,
490         .expectMac = {
491             0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44, 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
492         },
493         .macLen = 16
494     },
495     {
496         .macType = "CMAC",
497         .vectorSource = "nistspecialpublication800-38b.pdf",
498         .algoName = "AES256",
499         .key = {
500             0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
501             0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
502         },
503         .keyLen = 32,
504         .msg = {
505             0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
506         },
507         .msgLen = 16,
508         .expectMac = {
509             0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82, 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
510         },
511         .macLen = 16
512     },
513     {
514         .macType = "HMAC",
515         .vectorSource = "RFC2104",
516         .algoName = "MD5",
517         .key = {
518             0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
519         },
520         .keyLen = 16,
521         .msg = {
522             0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
523         },
524         .msgLen = strlen("Hi There"),
525         .expectMac = {
526             0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d
527         },
528         .macLen = 16
529     },
530     {
531         .macType = "HMAC",
532         .vectorSource = "RFC2202",
533         .algoName = "SHA1",
534         .key = {
535             0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
536             0x0b, 0x0b, 0x0b, 0x0b
537         },
538         .keyLen = 20,
539         .msg = {
540             0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
541         },
542         .msgLen = strlen("Hi There"),
543         .expectMac = {
544             0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
545             0xf1, 0x46, 0xbe, 0x00
546         },
547         .macLen = 20
548     },
549     {
550         .macType = "HMAC",
551         .vectorSource = "RFC6234",
552         .algoName = "SHA224",
553         .key = {
554             0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
555             0x0b, 0x0b, 0x0b, 0x0b
556         },
557         .keyLen = 20,
558         .msg = {
559             0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
560         },
561         .msgLen = strlen("Hi There"),
562         .expectMac = {
563             0x89, 0x6F, 0xB1, 0x12, 0x8A, 0xBB, 0xDF, 0x19, 0x68, 0x32, 0x10, 0x7C, 0xD4, 0x9D, 0xF3, 0x3F,
564             0x47, 0xB4, 0xB1, 0x16, 0x99, 0x12, 0xBA, 0x4F, 0x53, 0x68, 0x4B, 0x22
565         },
566         .macLen = 28
567     },
568     {
569         .macType = "HMAC",
570         .vectorSource = "RFC6234",
571         .algoName = "SHA256",
572         .key = {
573             0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
574             0x0b, 0x0b, 0x0b, 0x0b
575         },
576         .keyLen = 20,
577         .msg = {
578             0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
579         },
580         .msgLen = strlen("Hi There"),
581         .expectMac = {
582             0xB0, 0x34, 0x4C, 0x61, 0xD8, 0xDB, 0x38, 0x53, 0x5C, 0xA8, 0xAF, 0xCE, 0xAF, 0x0B, 0xF1, 0x2B,
583             0x88, 0x1D, 0xC2, 0x00, 0xC9, 0x83, 0x3D, 0xA7, 0x26, 0xE9, 0x37, 0x6C, 0x2E, 0x32, 0xCF, 0xF7
584         },
585         .macLen = 32
586     },
587     {
588         .macType = "HMAC",
589         .vectorSource = "RFC6234",
590         .algoName = "SHA384",
591         .key = {
592             0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
593             0x0b, 0x0b, 0x0b, 0x0b
594         },
595         .keyLen = 20,
596         .msg = {
597             0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
598         },
599         .msgLen = strlen("Hi There"),
600         .expectMac = {
601             0xAF, 0xD0, 0x39, 0x44, 0xD8, 0x48, 0x95, 0x62, 0x6B, 0x08, 0x25, 0xF4, 0xAB, 0x46, 0x90, 0x7F,
602             0x15, 0xF9, 0xDA, 0xDB, 0xE4, 0x10, 0x1E, 0xC6, 0x82, 0xAA, 0x03, 0x4C, 0x7C, 0xEB, 0xC5, 0x9C,
603             0xFA, 0xEA, 0x9E, 0xA9, 0x07, 0x6E, 0xDE, 0x7F, 0x4A, 0xF1, 0x52, 0xE8, 0xB2, 0xFA, 0x9C, 0xB6
604         },
605         .macLen = 48
606     },
607     {
608         .macType = "HMAC",
609         .vectorSource = "RFC6234",
610         .algoName = "SHA512",
611         .key = {
612             0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
613             0x0b, 0x0b, 0x0b, 0x0b
614         },
615         .keyLen = 20,
616         .msg = {
617             0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
618         },
619         .msgLen = strlen("Hi There"),
620         .expectMac = {
621             0x87, 0xAA, 0x7C, 0xDE, 0xA5, 0xEF, 0x61, 0x9D, 0x4F, 0xF0, 0xB4, 0x24, 0x1A, 0x1D, 0x6C, 0xB0,
622             0x23, 0x79, 0xF4, 0xE2, 0xCE, 0x4E, 0xC2, 0x78, 0x7A, 0xD0, 0xB3, 0x05, 0x45, 0xE1, 0x7C, 0xDE,
623             0xDA, 0xA8, 0x33, 0xB7, 0xD6, 0xB8, 0xA7, 0x02, 0x03, 0x8B, 0x27, 0x4E, 0xAE, 0xA3, 0xF4, 0xE4,
624             0xBE, 0x9D, 0x91, 0x4E, 0xEB, 0x61, 0xF1, 0x70, 0x2E, 0x69, 0x6C, 0x20, 0x3A, 0x12, 0x68, 0x54
625         },
626         .macLen = 64
627     },
628     {
629         .macType = "HMAC",
630         .vectorSource = "HiTLS",
631         .algoName = "SM3",
632         .key = {
633             0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
634             0x0b, 0x0b, 0x0b, 0x0b
635         },
636         .keyLen = 20,
637         .msg = {
638             0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65
639         },
640         .msgLen = strlen("Hi There"),
641         .expectMac = {
642             0x51, 0xb0, 0x0d, 0x1f, 0xb4, 0x98, 0x32, 0xbf, 0xb0, 0x1c, 0x3c, 0xe2, 0x78, 0x48, 0xe5, 0x9f,
643             0x87, 0x1d, 0x9b, 0xa9, 0x38, 0xdc, 0x56, 0x3b, 0x33, 0x8c, 0xa9, 0x64, 0x75, 0x5c, 0xce, 0x70
644         },
645         .macLen = 32
646     },
647 };
648 INSTANTIATE_TEST_CASE_P(OHCryptoFrameworkMacNapiTest, MAC_VECTOR_TEST, ::testing::ValuesIn(g_vectors));
649 
650 /**
651 * @tc.number SUB_Security_CryptoFramework_NAPI_Mac_Vector_Test_0100
652 * @tc.name OHCryptoFrameworkMacNapiTest/MAC_VECTOR_TEST.SUB_Security_CryptoFramework_NAPI_Mac_Vector_Test_0100/x
653 * @tc.desc algorithm is Mac
654 * @tc.size Medium
655 * @tc.type Func
656 * @tc.level Level0
657 */
658 HWTEST_P(MAC_VECTOR_TEST, SUB_Security_CryptoFramework_NAPI_Mac_Vector_Test_0100, TestSize.Level0)
659 {
660     VectorMacSpec macInfo = GetParam();
661     OH_CryptoSymKey *key = nullptr;
662     OH_CryptoMac *macCtx = nullptr;
663     Crypto_DataBlob out = {.data = nullptr, .len = 0};
664     uint32_t macLength = 0;
665     OH_CryptoSymKeyGenerator *ctx = nullptr;
666     Crypto_DataBlob symKey = {
667         .data = macInfo.key,
668         .len = macInfo.keyLen
669     };
670     if (memcmp((const char *)macInfo.macType, "HMAC", strlen("HMAC")) == 0) {
671         EXPECT_EQ(OH_CryptoSymKeyGenerator_Create(macInfo.macType, &ctx), CRYPTO_SUCCESS);
672     } else {
673         EXPECT_EQ(OH_CryptoSymKeyGenerator_Create(macInfo.algoName, &ctx), CRYPTO_SUCCESS);
674     }
675     EXPECT_EQ(OH_CryptoSymKeyGenerator_Convert(ctx, &symKey, &key), CRYPTO_SUCCESS);
676     EXPECT_EQ(OH_CryptoMac_Create(macInfo.macType, &macCtx), CRYPTO_SUCCESS);
677     if (memcmp((const char *)macInfo.macType, "HMAC", strlen("HMAC")) == 0) {
678         EXPECT_EQ(OHTEST_CryptoHmac_SetParam(macCtx, macInfo.algoName), CRYPTO_SUCCESS);
679     } else {
680         EXPECT_EQ(OHTEST_CryptoCmac_SetParam(macCtx, macInfo.algoName), CRYPTO_SUCCESS);
681     }
682     EXPECT_EQ(OH_CryptoMac_Init(macCtx, key), CRYPTO_SUCCESS);
683     if (macInfo.msgLen != 0) {
684         Crypto_DataBlob msgBlob = {.data = macInfo.msg, .len = macInfo.msgLen};
685         EXPECT_EQ(OH_CryptoMac_Update(macCtx, &msgBlob), CRYPTO_SUCCESS);
686     }
687     EXPECT_EQ(OH_CryptoMac_Final(macCtx, &out), CRYPTO_SUCCESS);
688     EXPECT_EQ(OH_CryptoMac_GetLength(macCtx, &macLength), CRYPTO_SUCCESS);
689     if (out.len == macInfo.macLen) {
690         EXPECT_TRUE(memcmp(out.data, macInfo.expectMac, out.len) == 0);
691     }
692     EXPECT_TRUE(out.len == macLength);
693     EXPECT_TRUE(out.len == macInfo.macLen);
694 
695     OH_Crypto_FreeDataBlob(&out);
696     OH_CryptoSymKey_Destroy(key);
697     OH_CryptoMac_Destroy(macCtx);
698     OH_CryptoSymKeyGenerator_Destroy(ctx);
699 }
700 
701 } // namespace Unittest::CryptoFrameworkMacNapiTest