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 "mac.h"
21 #include "sym_key_generator.h"
22 #include "mac_openssl.h"
23
24 #include "log.h"
25 #include "memory.h"
26
27 using namespace std;
28 using namespace testing::ext;
29
30 namespace {
31 class CryptoSM3MacTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp();
36 void TearDown();
37 };
38
39 constexpr uint32_t MAX_MAC_BLOB_LEN = 5000;
40 constexpr uint32_t INVALID_LEN = 0;
41 constexpr uint32_t SM3_LEN = 32;
42
43 static char g_testBigData[] = "VqRH5dzdeeturr5zN5vE77DtqjV7kNKbDJqk4mNqyYRTXymhjR\r\n"
44 "Yz8c2pNvJiCxyFwvLvWfPh2Y2eDAuxbdm2Dt4UzKJtNqEpNYKVZLiyH4a4MhR4BpFhvhJVHy2ALbYq2rW\r\n"
45 "LqJfNzA6v8kHNaFypNDMgX35kifyCiYkq85XUKDJCdewUzCZ2N8twC8Z9kL37K67bkL35VYFZSXyrNTdV\r\n"
46 "pB6kqPjwZYrjx5tXzMMgJW8ePqmAhZUVjtPGXTLVt8BUnaVRuWjD97xjS3VH9EwFeyrqJ46B92rkuGexY\r\n"
47 "cjXuvhHTnQNPbYfake7KMEWG2wgGLmZmjnakULhFgjt6TQhvCWMJAHYn8Zgczd3C3HkPrQgUXJgAiwf3r\r\n"
48 "jJbgbBpQkkbcfMBZZ3SSLe2J9jw6MkdEf3eBQX9rFVQSgBQgZ9KEW8XLb5kCTcyhRZPBbiHD4qQRyrwKT\r\n"
49 "mnGZqP5Aru6GDkhFk78jfjtk35HyB7AY7UZXkczRfVYAxa5Mk256MhAHkE3uAvPZTyY7N3qk9U7cLTrce\r\n"
50 "wJLH6wrymrMvQWgpvrBevMghnURZUcZAWUznDn56WnwGAzYAWmJqdXqAfcvgZwCFTjxdiaEZGpEyUrcS8\r\n"
51 "nr48ZeXS5aytz5Y7RnU5SxcHbgF8PerWVdftxmghPAvGkQ6f3dcXr9w9bbGqg5KJHyQCxabp8bjZpyFdb\r\n"
52 "VTq8DpQ6AJjxdjn8cuLTf9giGFxDjtQncicUdqP7YvVDr5AFgWc83cddyryVLZEBGAFfqbbKWF9KnPjRZ\r\n"
53 "AbuZ7SqrkxhQHu87Hxh3xHUHB8Lb3DGZ4vhnqaLnJBxFK8Ve4F2FfbgfHfQtALFDUWp6dSz8Hvdpj4CGw\r\n"
54 "FaSb8b5hTemaQRguYAqaUwJVvZ7G2AwkFnV9PHUngmybAFxg8HMAT3K7yAiQJWWqPxdGq8jXPAqZFNkGu\r\n"
55 "2mnJ5xfnY3z63PFk6TXU9Ga2YmHvtycXxwqMBEctQRa3zVWGVSrh3NF6jXa\r\n";
56
SetUpTestCase()57 void CryptoSM3MacTest::SetUpTestCase() {}
TearDownTestCase()58 void CryptoSM3MacTest::TearDownTestCase() {}
59
SetUp()60 void CryptoSM3MacTest::SetUp() // add init here, this will be called before test.
61 {
62 }
63
TearDown()64 void CryptoSM3MacTest::TearDown() // add destroy here, this will be called when test case done.
65 {
66 }
67
PrintfBlobInHex(uint8_t * data,size_t dataLen)68 static void PrintfBlobInHex(uint8_t *data, size_t dataLen)
69 {
70 for (size_t i = 0; i < dataLen; i++) {
71 printf("%02hhX", data[i]);
72 }
73 printf("\n");
74 }
75
76 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3CreateTest001, TestSize.Level0)
77 {
78 HcfResult ret = HcfMacCreate("SM3", nullptr);
79 EXPECT_NE(ret, HCF_SUCCESS);
80 }
81
82 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoSuppTest001, TestSize.Level0)
83 {
84 // create a SM3 obj
85 HcfMac *macObj = nullptr;
86 HcfResult ret = HcfMacCreate("SM3", &macObj);
87 ASSERT_EQ(ret, HCF_SUCCESS);
88 EXPECT_NE(macObj, nullptr);
89 HcfObjDestroy(macObj);
90 }
91
92 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoSuppTest002, TestSize.Level0)
93 {
94 // create a SM3 obj
95 HcfMac *macObj = nullptr;
96 HcfResult ret = HcfMacCreate("SHA3", &macObj);
97 EXPECT_NE(ret, HCF_SUCCESS);
98 EXPECT_EQ(macObj, nullptr);
99 }
100
101 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoSuppTest003, TestSize.Level0)
102 {
103 // create a SM3 obj
104 HcfMac *macObj = nullptr;
105 HcfResult ret = HcfMacCreate(nullptr, &macObj);
106 EXPECT_NE(ret, HCF_SUCCESS);
107 EXPECT_EQ(macObj, nullptr);
108 }
109
110 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoNameTest001, TestSize.Level0)
111 {
112 // create a SM3 obj
113 HcfMac *macObj = nullptr;
114 HcfResult ret = HcfMacCreate("SM3", &macObj);
115 ASSERT_EQ(ret, HCF_SUCCESS);
116 EXPECT_NE(macObj, nullptr);
117 // test api functions
118 const char *algoName = macObj->getAlgoName(macObj);
119 int32_t cmpRes = strcmp(algoName, "SM3");
120 EXPECT_EQ(cmpRes, HCF_SUCCESS);
121 HcfObjDestroy(macObj);
122 }
123
124 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3InitTest001, TestSize.Level0)
125 {
126 // create a SM3 obj
127 HcfMac *macObj = nullptr;
128 HcfResult ret = HcfMacCreate("SM3", &macObj);
129 ASSERT_EQ(ret, HCF_SUCCESS);
130 // set a nullptr key
131 HcfSymKey *key = nullptr;
132 // test api functions
133 ret = macObj->init(macObj, key);
134 EXPECT_NE(ret, HCF_SUCCESS);
135 HcfObjDestroy(macObj);
136 }
137
138 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3InitTest002, TestSize.Level0)
139 {
140 // create a SM3 obj
141 HcfMac *macObj = nullptr;
142 HcfResult ret = HcfMacCreate("SM3", &macObj);
143 ASSERT_EQ(ret, HCF_SUCCESS);
144 // create a symKey generator
145 HcfSymKeyGenerator *generator = nullptr;
146 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
147 ASSERT_EQ(ret, HCF_SUCCESS);
148 // get sym key from preset keyBlob
149 uint8_t testKey[] = "abcdefghijklmnop";
150 uint32_t testKeyLen = 16;
151 HcfSymKey *key = nullptr;
152 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
153 generator->convertSymKey(generator, &keyMaterialBlob, &key);
154 // test api functions
155 ret = macObj->init(macObj, key);
156 EXPECT_EQ(ret, HCF_SUCCESS);
157 HcfObjDestroy(macObj);
158 HcfObjDestroy(key);
159 HcfObjDestroy(generator);
160 }
161
162 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3UpdateTest001, TestSize.Level0)
163 {
164 // create a SM3 obj
165 HcfMac *macObj = nullptr;
166 HcfResult ret = HcfMacCreate("SM3", &macObj);
167 ASSERT_EQ(ret, HCF_SUCCESS);
168 // set input and output buf
169 uint8_t testData[] = "My test data";
170 // define input and output data in blob form
171 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
172 // test api functions
173 ret = macObj->update(macObj, &inBlob);
174 EXPECT_NE(ret, HCF_SUCCESS);
175 HcfObjDestroy(macObj);
176 }
177
178 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3UpdateTest002, TestSize.Level0)
179 {
180 // create a SM3 obj
181 HcfMac *macObj = nullptr;
182 HcfResult ret = HcfMacCreate("SM3", &macObj);
183 ASSERT_EQ(ret, HCF_SUCCESS);
184 // cteate key generator and set key text
185 HcfSymKeyGenerator *generator = nullptr;
186 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
187 ASSERT_EQ(ret, HCF_SUCCESS);
188 // get sym key from preset keyBlob
189 uint8_t testKey[] = "abcdefghijklmnop";
190 uint32_t testKeyLen = 16;
191 HcfSymKey *key = nullptr;
192 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
193 generator->convertSymKey(generator, &keyMaterialBlob, &key);
194 // define input and output data in blob form
195 HcfBlob *inBlob = nullptr;
196 // test api functions
197 ret = macObj->init(macObj, key);
198 EXPECT_EQ(ret, HCF_SUCCESS);
199 ret = macObj->update(macObj, inBlob);
200 EXPECT_NE(ret, HCF_SUCCESS);
201 HcfObjDestroy(macObj);
202 HcfObjDestroy(key);
203 HcfObjDestroy(generator);
204 }
205
206 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3UpdateTest003, TestSize.Level0)
207 {
208 // create a API obj with SM3
209 HcfMac *macObj = nullptr;
210 HcfResult ret = HcfMacCreate("SM3", &macObj);
211 ASSERT_EQ(ret, HCF_SUCCESS);
212 // cteate key generator and set key text
213 HcfSymKeyGenerator *generator = nullptr;
214 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
215 ASSERT_EQ(ret, HCF_SUCCESS);
216 // get sym key from preset keyBlob
217 uint8_t testKey[] = "abcdefghijklmnop";
218 uint32_t testKeyLen = 16;
219 HcfSymKey *key = nullptr;
220 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
221 generator->convertSymKey(generator, &keyMaterialBlob, &key);
222 // set input and output buf
223 uint8_t testData[] = "My test data";
224 // define input and output data in blob form
225 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
226 // test api functions
227 ret = macObj->init(macObj, key);
228 EXPECT_EQ(ret, HCF_SUCCESS);
229 ret = macObj->update(macObj, &inBlob);
230 EXPECT_EQ(ret, HCF_SUCCESS);
231 HcfObjDestroy(macObj);
232 HcfObjDestroy(key);
233 HcfObjDestroy(generator);
234 }
235
236 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest001, TestSize.Level0)
237 {
238 // create a SM3 obj
239 HcfMac *macObj = nullptr;
240 HcfResult ret = HcfMacCreate("SM3", &macObj);
241 ASSERT_EQ(ret, HCF_SUCCESS);
242 // set input and output buf
243 HcfBlob outBlob = { .data = nullptr, .len = 0 };
244 // test api functions
245 ret = macObj->doFinal(macObj, &outBlob);
246 EXPECT_NE(ret, HCF_SUCCESS);
247 // destroy the API obj and blob data
248 HcfObjDestroy(macObj);
249 }
250
251 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest002, TestSize.Level0)
252 {
253 // create a SM3 obj
254 HcfMac *macObj = nullptr;
255 HcfResult ret = HcfMacCreate("SM3", &macObj);
256 ASSERT_EQ(ret, HCF_SUCCESS);
257 // cteate key generator and set key text
258 HcfSymKeyGenerator *generator = nullptr;
259 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
260 ASSERT_EQ(ret, HCF_SUCCESS);
261 // get sym key from preset keyBlob
262 uint8_t testKey[] = "abcdefghijklmnop";
263 uint32_t testKeyLen = 16;
264 HcfSymKey *key = nullptr;
265 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
266 generator->convertSymKey(generator, &keyMaterialBlob, &key);
267 printf("get symkey finish");
268 // set input and output buf
269 HcfBlob outBlob = { .data = nullptr, .len = 0 };
270 // test api functions
271 ret = macObj->init(macObj, key);
272 EXPECT_EQ(ret, HCF_SUCCESS);
273 printf("test init finish");
274 ret = macObj->doFinal(macObj, &outBlob);
275 EXPECT_EQ(ret, HCF_SUCCESS);
276 printf("test dofinal finish");
277 PrintfBlobInHex(outBlob.data, outBlob.len);
278 // destroy the API obj and blob data
279 HcfBlobDataClearAndFree(&outBlob);
280 printf("HcfBlobDataClearAndFree finish");
281 HcfObjDestroy(macObj);
282 printf("HcfObjDestroy macObj finish");
283 HcfObjDestroy(key);
284 printf("HcfObjDestroy key finish");
285 HcfObjDestroy(generator);
286 printf("test finish");
287 }
288
289 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest003, TestSize.Level0)
290 {
291 // create a SM3 obj
292 HcfMac *macObj = nullptr;
293 HcfResult ret = HcfMacCreate("SM3", &macObj);
294 ASSERT_EQ(ret, HCF_SUCCESS);
295 // cteate key generator
296 HcfSymKeyGenerator *generator = nullptr;
297 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
298 ASSERT_EQ(ret, HCF_SUCCESS);
299 // get sym key from preset keyBlob
300 uint8_t testKey[] = "abcdefghijklmnop";
301 uint32_t testKeyLen = 16;
302 HcfSymKey *key = nullptr;
303 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
304 generator->convertSymKey(generator, &keyMaterialBlob, &key);
305 // set input and output buf
306 uint8_t testData[] = "My test data";
307 // define input and output data in blob form
308 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
309 HcfBlob outBlob = { .data = nullptr, .len = 0 };
310 // test api functions
311 ret = macObj->init(macObj, key);
312 EXPECT_EQ(ret, HCF_SUCCESS);
313 ret = macObj->update(macObj, &inBlob);
314 EXPECT_EQ(ret, HCF_SUCCESS);
315 ret = macObj->doFinal(macObj, &outBlob);
316 EXPECT_EQ(ret, HCF_SUCCESS);
317 // destroy the API obj and blob data
318 HcfBlobDataClearAndFree(&outBlob);
319 HcfObjDestroy(macObj);
320 HcfObjDestroy(key);
321 HcfObjDestroy(generator);
322 }
323
324 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3DoFinalTest004, TestSize.Level0)
325 {
326 // create a SM3 obj
327 HcfMac *macObj = nullptr;
328 HcfResult ret = HcfMacCreate("SM3", &macObj);
329 ASSERT_EQ(ret, HCF_SUCCESS);
330 // cteate key generator
331 HcfSymKeyGenerator *generator = nullptr;
332 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
333 ASSERT_EQ(ret, HCF_SUCCESS);
334 // get sym key from preset keyBlob
335 uint8_t testKey[] = "abcdefghijklmnop";
336 uint32_t testKeyLen = 16;
337 HcfSymKey *key = nullptr;
338 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
339 generator->convertSymKey(generator, &keyMaterialBlob, &key);
340 // define input and output data in blob form
341 HcfBlob inBlob = {0};
342 inBlob.data = reinterpret_cast<uint8_t *>(g_testBigData);
343 inBlob.len = strnlen(g_testBigData, MAX_MAC_BLOB_LEN);
344 HcfBlob outBlob = { .data = nullptr, .len = 0 };
345 // test api functions
346 ret = macObj->init(macObj, key);
347 EXPECT_EQ(ret, HCF_SUCCESS);
348 ret = macObj->update(macObj, &inBlob);
349 EXPECT_EQ(ret, HCF_SUCCESS);
350 ret = macObj->doFinal(macObj, &outBlob);
351 EXPECT_EQ(ret, HCF_SUCCESS);
352 PrintfBlobInHex(outBlob.data, outBlob.len);
353 // destroy the API obj and blob data
354 HcfBlobDataClearAndFree(&outBlob);
355 HcfObjDestroy(macObj);
356 HcfObjDestroy(key);
357 HcfObjDestroy(generator);
358 }
359
360 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3LenTest001, TestSize.Level0)
361 {
362 // create a SM3 obj
363 HcfMac *macObj = nullptr;
364 HcfResult ret = HcfMacCreate("SM3", &macObj);
365 ASSERT_EQ(ret, HCF_SUCCESS);
366 // test api functions
367 uint32_t len = macObj->getMacLength(macObj);
368 EXPECT_EQ(len, INVALID_LEN);
369 HcfObjDestroy(macObj);
370 }
371
372 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3LenTest002, TestSize.Level0)
373 {
374 // create a SM3 obj
375 HcfMac *macObj = nullptr;
376 HcfResult ret = HcfMacCreate("SM3", &macObj);
377 ASSERT_EQ(ret, HCF_SUCCESS);
378 // cteate key generator
379 HcfSymKeyGenerator *generator = nullptr;
380 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
381 ASSERT_EQ(ret, HCF_SUCCESS);
382 // get sym key from preset keyBlob
383 uint8_t testKey[] = "abcdefghijklmnop";
384 uint32_t testKeyLen = 16;
385 HcfSymKey *key = nullptr;
386 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
387 generator->convertSymKey(generator, &keyMaterialBlob, &key);
388 // test api functions
389 ret = macObj->init(macObj, key);
390 EXPECT_EQ(ret, HCF_SUCCESS);
391 uint32_t len = macObj->getMacLength(macObj);
392 EXPECT_EQ(len, SM3_LEN);
393 HcfObjDestroy(macObj);
394 HcfObjDestroy(key);
395 HcfObjDestroy(generator);
396 }
397
398 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest001, TestSize.Level0)
399 {
400 // create a SM3 obj
401 HcfMac *macObj = nullptr;
402 HcfResult ret = HcfMacCreate("SM3", &macObj);
403 ASSERT_EQ(ret, HCF_SUCCESS);
404 // create a symKey generator
405 HcfSymKeyGenerator *generator = nullptr;
406 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
407 ASSERT_EQ(ret, HCF_SUCCESS);
408 // set key data and convert it to key obj
409 uint8_t testKey[] = "abcdefghijklmnop";
410 uint32_t testKeyLen = 16;
411 HcfSymKey *key = nullptr;
412 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
413 generator->convertSymKey(generator, &keyMaterialBlob, &key);
414 // set input and output blob
415 uint8_t testData[] = "My test data";
416 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
417 HcfBlob outBlob = { .data = nullptr, .len = 0 };
418 // test api funcitons
419 ret = macObj->init(macObj, key);
420 EXPECT_EQ(ret, HCF_SUCCESS);
421 ret = macObj->update(macObj, &inBlob);
422 EXPECT_EQ(ret, HCF_SUCCESS);
423 ret = macObj->doFinal(macObj, &outBlob);
424 EXPECT_EQ(ret, HCF_SUCCESS);
425 uint32_t len = macObj->getMacLength(macObj);
426 EXPECT_EQ(len, SM3_LEN);
427 // destroy the API obj and blob data
428 HcfBlobDataClearAndFree(&outBlob);
429 HcfObjDestroy(macObj);
430 HcfObjDestroy(key);
431 HcfObjDestroy(generator);
432 }
433
434 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest002, TestSize.Level0)
435 {
436 // create a SM3 obj
437 HcfMac *macObj = nullptr;
438 HcfResult ret = HcfMacCreate("SM3", &macObj);
439 ASSERT_EQ(ret, HCF_SUCCESS);
440 // create a symKey generator
441 HcfSymKeyGenerator *generator = nullptr;
442 ret = HcfSymKeyGeneratorCreate("AES192", &generator);
443 ASSERT_EQ(ret, HCF_SUCCESS);
444 // set key data and convert it to key obj
445 HcfSymKey *key = nullptr;
446 generator->generateSymKey(generator, &key);
447 // set input and output blob
448 uint8_t testData[] = "My test data";
449 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
450 HcfBlob outBlob = { .data = nullptr, .len = 0 };
451 // test api funcitons
452 ret = macObj->init(macObj, key);
453 EXPECT_EQ(ret, HCF_SUCCESS);
454 ret = macObj->update(macObj, &inBlob);
455 EXPECT_EQ(ret, HCF_SUCCESS);
456 ret = macObj->doFinal(macObj, &outBlob);
457 EXPECT_EQ(ret, HCF_SUCCESS);
458 uint32_t len = macObj->getMacLength(macObj);
459 EXPECT_EQ(len, SM3_LEN);
460 // destroy the API obj and blob data
461 HcfBlobDataClearAndFree(&outBlob);
462 HcfObjDestroy(macObj);
463 HcfObjDestroy(key);
464 HcfObjDestroy(generator);
465 }
466
467 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest003, TestSize.Level0)
468 {
469 // create a SM3 obj
470 HcfMac *macObj = nullptr;
471 HcfResult ret = HcfMacCreate("SM3", &macObj);
472 ASSERT_EQ(ret, HCF_SUCCESS);
473 // create a symKey generator
474 HcfSymKeyGenerator *generator = nullptr;
475 ret = HcfSymKeyGeneratorCreate("AES256", &generator);
476 ASSERT_EQ(ret, HCF_SUCCESS);
477 // set key data and convert it to key obj
478 HcfSymKey *key = nullptr;
479 generator->generateSymKey(generator, &key);
480 // set input and output blob
481 uint8_t testData[] = "My test data";
482 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
483 HcfBlob outBlob = { .data = nullptr, .len = 0 };
484 // test api funcitons
485 ret = macObj->init(macObj, key);
486 EXPECT_EQ(ret, HCF_SUCCESS);
487 ret = macObj->update(macObj, &inBlob);
488 EXPECT_EQ(ret, HCF_SUCCESS);
489 ret = macObj->doFinal(macObj, &outBlob);
490 EXPECT_EQ(ret, HCF_SUCCESS);
491 uint32_t len = macObj->getMacLength(macObj);
492 EXPECT_EQ(len, SM3_LEN);
493 // destroy the API obj and blob data
494 HcfBlobDataClearAndFree(&outBlob);
495 HcfObjDestroy(macObj);
496 HcfObjDestroy(key);
497 HcfObjDestroy(generator);
498 }
499
500 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest004, TestSize.Level0)
501 {
502 // create a SM3 obj
503 HcfMac *macObj = nullptr;
504 HcfResult ret = HcfMacCreate("SM3", &macObj);
505 ASSERT_EQ(ret, HCF_SUCCESS);
506 // create a symKey generator
507 HcfSymKeyGenerator *generator = nullptr;
508 ret = HcfSymKeyGeneratorCreate("3DES192", &generator);
509 ASSERT_EQ(ret, HCF_SUCCESS);
510 // set key data and convert it to key obj
511 HcfSymKey *key = nullptr;
512 generator->generateSymKey(generator, &key);
513 // set input and output blob
514 uint8_t testData[] = "My test data";
515 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
516 HcfBlob outBlob = { .data = nullptr, .len = 0 };
517 // test api funcitons
518 ret = macObj->init(macObj, key);
519 EXPECT_EQ(ret, HCF_SUCCESS);
520 ret = macObj->update(macObj, &inBlob);
521 EXPECT_EQ(ret, HCF_SUCCESS);
522 ret = macObj->doFinal(macObj, &outBlob);
523 EXPECT_EQ(ret, HCF_SUCCESS);
524 uint32_t len = macObj->getMacLength(macObj);
525 EXPECT_EQ(len, SM3_LEN);
526 // destroy the API obj and blob data
527 HcfBlobDataClearAndFree(&outBlob);
528 HcfObjDestroy(macObj);
529 HcfObjDestroy(key);
530 HcfObjDestroy(generator);
531 }
532
533 HWTEST_F(CryptoSM3MacTest, CryptoFrameworkHmacSM3AlgoTest005, TestSize.Level0)
534 {
535 // create a SM3 obj
536 HcfMac *macObj = nullptr;
537 HcfResult ret = HcfMacCreate("SM3", &macObj);
538 ASSERT_EQ(ret, HCF_SUCCESS);
539 // create a symKey generator
540 HcfSymKeyGenerator *generator = nullptr;
541 ret = HcfSymKeyGeneratorCreate("SM4_128", &generator);
542 ASSERT_EQ(ret, HCF_SUCCESS);
543 // set key data and convert it to key obj
544 HcfSymKey *key = nullptr;
545 generator->generateSymKey(generator, &key);
546 // set input and output blob
547 uint8_t testData[] = "My test data";
548 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
549 HcfBlob outBlob = {.data = nullptr, .len = 0};
550 // test api funcitons
551 ret = macObj->init(macObj, key);
552 EXPECT_EQ(ret, HCF_SUCCESS);
553 ret = macObj->update(macObj, &inBlob);
554 EXPECT_EQ(ret, HCF_SUCCESS);
555 ret = macObj->doFinal(macObj, &outBlob);
556 EXPECT_EQ(ret, HCF_SUCCESS);
557 uint32_t len = macObj->getMacLength(macObj);
558 EXPECT_EQ(len, SM3_LEN);
559 // destroy the API obj and blob data
560 HcfBlobDataClearAndFree(&outBlob);
561 HcfObjDestroy(macObj);
562 HcfObjDestroy(key);
563 HcfObjDestroy(generator);
564 }
565
GetInvalidMacSm3Class(void)566 static const char *GetInvalidMacSm3Class(void)
567 {
568 return "INVALID_MAC_CLASS";
569 }
570
571 HWTEST_F(CryptoSM3MacTest, InvalidInputMacSM3Test001, TestSize.Level0)
572 {
573 HcfResult ret = OpensslMacSpiCreate("SM3", nullptr);
574 EXPECT_NE(ret, HCF_SUCCESS);
575 HcfMacSpi *spiObj = nullptr;
576 ret = OpensslMacSpiCreate(nullptr, &spiObj);
577 EXPECT_NE(ret, HCF_SUCCESS);
578 }
579
580 HWTEST_F(CryptoSM3MacTest, NullParamMacSM3Test001, TestSize.Level0)
581 {
582 HcfMac *macObj = nullptr;
583 HcfResult ret = HcfMacCreate("SM3", &macObj);
584 ASSERT_EQ(ret, HCF_SUCCESS);
585 ret = macObj->init(nullptr, nullptr);
586 EXPECT_NE(ret, HCF_SUCCESS);
587 ret = macObj->update(nullptr, nullptr);
588 EXPECT_NE(ret, HCF_SUCCESS);
589 ret = macObj->doFinal(nullptr, nullptr);
590 EXPECT_NE(ret, HCF_SUCCESS);
591 uint32_t len = macObj->getMacLength(nullptr);
592 EXPECT_EQ(len, HCF_OPENSSL_INVALID_MAC_LEN);
593 const char *algoName = macObj->getAlgoName(nullptr);
594 EXPECT_EQ(algoName, nullptr);
595 macObj->base.destroy(nullptr);
596 HcfObjDestroy(macObj);
597 }
598
599 HWTEST_F(CryptoSM3MacTest, InvalidFrameworkClassMacSM3Test001, TestSize.Level0)
600 {
601 HcfMac *macObj = nullptr;
602 HcfResult ret = HcfMacCreate("SM3", &macObj);
603 ASSERT_EQ(ret, HCF_SUCCESS);
604 HcfMac invalidMacObj = {{0}};
605 invalidMacObj.base.getClass = GetInvalidMacSm3Class;
606 HcfSymKeyGenerator *generator = nullptr;
607 ret = HcfSymKeyGeneratorCreate("AES128", &generator);
608 ASSERT_EQ(ret, HCF_SUCCESS);
609 uint8_t testKey[] = "abcdefghijklmnop";
610 uint32_t testKeyLen = sizeof(testKey) / sizeof(testKey[0]);
611 HcfSymKey *key = nullptr;
612 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
613 generator->convertSymKey(generator, &keyMaterialBlob, &key);
614 uint8_t testData[] = "My test data";
615 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
616 HcfBlob outBlob = { .data = nullptr, .len = 0 };
617 ret = macObj->init(&invalidMacObj, key);
618 EXPECT_NE(ret, HCF_SUCCESS);
619 ret = macObj->update(&invalidMacObj, &inBlob);
620 EXPECT_NE(ret, HCF_SUCCESS);
621 ret = macObj->doFinal(&invalidMacObj, &outBlob);
622 EXPECT_NE(ret, HCF_SUCCESS);
623 uint32_t len = macObj->getMacLength(&invalidMacObj);
624 EXPECT_EQ(len, HCF_OPENSSL_INVALID_MAC_LEN);
625 const char *algoName = macObj->getAlgoName(&invalidMacObj);
626 EXPECT_EQ(algoName, nullptr);
627 HcfBlobDataClearAndFree(&outBlob);
628 macObj->base.destroy(&(invalidMacObj.base));
629 HcfObjDestroy(macObj);
630 HcfObjDestroy(key);
631 HcfObjDestroy(generator);
632 }
633
634 HWTEST_F(CryptoSM3MacTest, InvalidSpiClassMacSM3Test001, TestSize.Level0)
635 {
636 HcfMacSpi *spiObj = nullptr;
637 HcfMacSpi invalidSpi = {{0}};
638 invalidSpi.base.getClass = GetInvalidMacSm3Class;
639 // create a symKey generator
640 HcfSymKeyGenerator *generator = nullptr;
641 HcfResult ret = HcfSymKeyGeneratorCreate("AES128", &generator);
642 ASSERT_EQ(ret, HCF_SUCCESS);
643 // set key data and convert it to key obj
644 uint8_t testKey[] = "abcdefghijklmnop";
645 uint32_t testKeyLen = 16;
646 HcfSymKey *key = nullptr;
647 HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
648 generator->convertSymKey(generator, &keyMaterialBlob, &key);
649 // set input and output blob
650 uint8_t testData[] = "My test data";
651 HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
652 HcfBlob outBlob = { .data = nullptr, .len = 0 };
653 ret = OpensslMacSpiCreate("SM3", &spiObj);
654 EXPECT_EQ(ret, HCF_SUCCESS);
655 ASSERT_NE(spiObj, nullptr);
656 (void)spiObj->base.destroy(nullptr);
657 (void)spiObj->base.destroy(&(invalidSpi.base));
658 ret = spiObj->engineInitMac(&invalidSpi, key);
659 EXPECT_NE(ret, HCF_SUCCESS);
660 ret = spiObj->engineUpdateMac(&invalidSpi, &inBlob);
661 EXPECT_NE(ret, HCF_SUCCESS);
662 ret = spiObj->engineDoFinalMac(&invalidSpi, &outBlob);
663 EXPECT_NE(ret, HCF_SUCCESS);
664 uint32_t len = spiObj->engineGetMacLength(&invalidSpi);
665 EXPECT_EQ(len, INVALID_LEN);
666 HcfObjDestroy(spiObj);
667 HcfObjDestroy(key);
668 HcfObjDestroy(generator);
669 }
670 }