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