• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "securec.h"
18 
19 #include "md.h"
20 #include "md_openssl.h"
21 
22 #include "log.h"
23 #include "memory.h"
24 
25 using namespace std;
26 using namespace testing::ext;
27 
28 namespace {
29 class CryptoMdTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 
37 constexpr uint32_t MAX_MD_BLOB_LEN = 5000;
38 constexpr uint32_t INVALID_LEN = 0;
39 constexpr uint32_t SHA1_LEN = 20;
40 constexpr uint32_t SHA224_LEN = 28;
41 constexpr uint32_t SHA256_LEN = 32;
42 constexpr uint32_t SHA384_LEN = 48;
43 constexpr uint32_t SHA512_LEN = 64;
44 constexpr uint32_t MD5_LEN = 16;
45 
46 static char g_testBigData[] = "VqRH5dzdeeturr5zN5vE77DtqjV7kNKbDJqk4mNqyYRTXymhjR\r\n"
47 "Yz8c2pNvJiCxyFwvLvWfPh2Y2eDAuxbdm2Dt4UzKJtNqEpNYKVZLiyH4a4MhR4BpFhvhJVHy2ALbYq2rW\r\n"
48 "LqJfNzA6v8kHNaFypNDMgX35kifyCiYkq85XUKDJCdewUzCZ2N8twC8Z9kL37K67bkL35VYFZSXyrNTdV\r\n"
49 "pB6kqPjwZYrjx5tXzMMgJW8ePqmAhZUVjtPGXTLVt8BUnaVRuWjD97xjS3VH9EwFeyrqJ46B92rkuGexY\r\n"
50 "cjXuvhHTnQNPbYfake7KMEWG2wgGLmZmjnakULhFgjt6TQhvCWMJAHYn8Zgczd3C3HkPrQgUXJgAiwf3r\r\n"
51 "jJbgbBpQkkbcfMBZZ3SSLe2J9jw6MkdEf3eBQX9rFVQSgBQgZ9KEW8XLb5kCTcyhRZPBbiHD4qQRyrwKT\r\n"
52 "mnGZqP5Aru6GDkhFk78jfjtk35HyB7AY7UZXkczRfVYAxa5Mk256MhAHkE3uAvPZTyY7N3qk9U7cLTrce\r\n"
53 "wJLH6wrymrMvQWgpvrBevMghnURZUcZAWUznDn56WnwGAzYAWmJqdXqAfcvgZwCFTjxdiaEZGpEyUrcS8\r\n"
54 "nr48ZeXS5aytz5Y7RnU5SxcHbgF8PerWVdftxmghPAvGkQ6f3dcXr9w9bbGqg5KJHyQCxabp8bjZpyFdb\r\n"
55 "VTq8DpQ6AJjxdjn8cuLTf9giGFxDjtQncicUdqP7YvVDr5AFgWc83cddyryVLZEBGAFfqbbKWF9KnPjRZ\r\n"
56 "AbuZ7SqrkxhQHu87Hxh3xHUHB8Lb3DGZ4vhnqaLnJBxFK8Ve4F2FfbgfHfQtALFDUWp6dSz8Hvdpj4CGw\r\n"
57 "FaSb8b5hTemaQRguYAqaUwJVvZ7G2AwkFnV9PHUngmybAFxg8HMAT3K7yAiQJWWqPxdGq8jXPAqZFNkGu\r\n"
58 "2mnJ5xfnY3z63PFk6TXU9Ga2YmHvtycXxwqMBEctQRa3zVWGVSrh3NF6jXa\r\n";
59 
SetUpTestCase()60 void CryptoMdTest::SetUpTestCase() {}
TearDownTestCase()61 void CryptoMdTest::TearDownTestCase() {}
62 
SetUp()63 void CryptoMdTest::SetUp() // add init here, this will be called before test.
64 {
65 }
66 
TearDown()67 void CryptoMdTest::TearDown() // add destroy here, this will be called when test case done.
68 {
69 }
70 
PrintfBlobInHex(uint8_t * data,size_t dataLen)71 static void PrintfBlobInHex(uint8_t *data, size_t dataLen)
72 {
73     for (size_t i = 0; i < dataLen; i++) {
74         printf("%02hhX", data[i]);
75     }
76     printf("\n");
77 }
78 
79 /**
80  * @tc.name: CryptoFrameworkMdTest.CryptoFrameworkMdCreateTest001
81  * @tc.desc: Verify that the creation of the SHA1 Md obj is normal.
82  * @tc.type: FUNC
83  * @tc.require: I5QWEM
84  */
85 HWTEST_F(CryptoMdTest, CryptoFrameworkMdCreateTest001, TestSize.Level0)
86 {
87     // create a SHA1 obj
88     HcfResult ret = HcfMdCreate("SHA1", nullptr);
89     EXPECT_NE(ret, HCF_SUCCESS);
90 }
91 
92 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoSuppTest001, TestSize.Level0)
93 {
94     // create a SHA1 obj
95     HcfMd *mdObj = nullptr;
96     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
97     ASSERT_EQ(ret, HCF_SUCCESS);
98     ASSERT_NE(mdObj, nullptr);
99     HcfObjDestroy(mdObj);
100 }
101 
102 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoSuppTest002, TestSize.Level0)
103 {
104     // create a SHA3 obj (not supported)
105     HcfMd *mdObj = nullptr;
106     HcfResult ret = HcfMdCreate("SHA3", &mdObj);
107     EXPECT_NE(ret, HCF_SUCCESS);
108     EXPECT_EQ(mdObj, nullptr);
109 }
110 
111 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoSuppTest003, TestSize.Level0)
112 {
113     // create an obj with null algoInput (not supported)
114     HcfMd *mdObj = nullptr;
115     HcfResult ret = HcfMdCreate(nullptr, &mdObj);
116     EXPECT_NE(ret, HCF_SUCCESS);
117     EXPECT_EQ(mdObj, nullptr);
118 }
119 
120 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoNameTest001, TestSize.Level0)
121 {
122     // create a SHA1 obj
123     HcfMd *mdObj = nullptr;
124     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
125     ASSERT_EQ(ret, HCF_SUCCESS);
126     ASSERT_NE(mdObj, nullptr);
127     // test api functions
128     const char *algoName =  mdObj->getAlgoName(mdObj);
129     int32_t cmpRes = strcmp(algoName, "SHA1");
130     EXPECT_EQ(cmpRes, HCF_SUCCESS);
131     HcfObjDestroy(mdObj);
132 }
133 
134 HWTEST_F(CryptoMdTest, CryptoFrameworkMdUpdateTest001, TestSize.Level0)
135 {
136     // create a SHA1 obj
137     HcfMd *mdObj = nullptr;
138     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
139     ASSERT_EQ(ret, HCF_SUCCESS);
140     // define input and output data in blob form
141     HcfBlob *inBlob = nullptr;
142     // test api functions
143     ret = mdObj->update(mdObj, inBlob);
144     EXPECT_NE(ret, HCF_SUCCESS);
145     HcfObjDestroy(mdObj);
146 }
147 
148 HWTEST_F(CryptoMdTest, CryptoFrameworkMdUpdateTest002, TestSize.Level0)
149 {
150     // create a SHA1 obj
151     HcfMd *mdObj = nullptr;
152     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
153     ASSERT_EQ(ret, HCF_SUCCESS);
154     // set input and output buf
155     uint8_t testData[] = "My test data";
156     // define input and output data in blob form
157     HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
158     // test api functions
159     ret = mdObj->update(mdObj, &inBlob);
160     EXPECT_EQ(ret, HCF_SUCCESS);
161     HcfObjDestroy(mdObj);
162 }
163 
164 HWTEST_F(CryptoMdTest, CryptoFrameworkMdDoFinalTest001, TestSize.Level0)
165 {
166     // create a SHA1 obj
167     HcfMd *mdObj = nullptr;
168     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
169     ASSERT_EQ(ret, HCF_SUCCESS);
170     // set input and output buf
171     HcfBlob outBlob = { .data = nullptr, .len = 0 };
172     // test api functions
173     ret = mdObj->doFinal(mdObj, &outBlob);
174     EXPECT_EQ(ret, HCF_SUCCESS);
175     // destroy the API obj and blob data
176     HcfBlobDataClearAndFree(&outBlob);
177     HcfObjDestroy(mdObj);
178 }
179 
180 HWTEST_F(CryptoMdTest, CryptoFrameworkMdDoFinalTest002, TestSize.Level0)
181 {
182     // create a SHA1 obj
183     HcfMd *mdObj = nullptr;
184     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
185     ASSERT_EQ(ret, HCF_SUCCESS);
186     // set input and output buf
187     uint8_t testData[] = "My test data";
188     // define input and output data in blob form
189     HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
190     HcfBlob outBlob = { .data = nullptr, .len = 0 };
191     // test api functions
192     ret = mdObj->update(mdObj, &inBlob);
193     EXPECT_EQ(ret, HCF_SUCCESS);
194     ret = mdObj->doFinal(mdObj, &outBlob);
195     EXPECT_EQ(ret, HCF_SUCCESS);
196     // destroy the API obj and blob data
197     HcfBlobDataClearAndFree(&outBlob);
198     HcfObjDestroy(mdObj);
199 }
200 
201 HWTEST_F(CryptoMdTest, CryptoFrameworkMdDoFinalTest003, TestSize.Level0)
202 {
203     // create a SHA1 obj
204     HcfMd *mdObj = nullptr;
205     HcfResult ret = HcfMdCreate("SHA256", &mdObj);
206     ASSERT_EQ(ret, HCF_SUCCESS);
207     // define input and output data in blob form
208     HcfBlob inBlob = { .data = nullptr, .len = 0 };
209     inBlob.data = reinterpret_cast<uint8_t *>(g_testBigData);
210     inBlob.len = strnlen(g_testBigData, MAX_MD_BLOB_LEN);
211     HcfBlob outBlob = { .data = nullptr, .len = 0 };
212     // test api functions
213     ret = mdObj->update(mdObj, &inBlob);
214     EXPECT_EQ(ret, HCF_SUCCESS);
215     ret = mdObj->doFinal(mdObj, &outBlob);
216     EXPECT_EQ(ret, HCF_SUCCESS);
217     PrintfBlobInHex(outBlob.data, outBlob.len);
218     // destroy the API obj and blob data
219     HcfBlobDataClearAndFree(&outBlob);
220     HcfObjDestroy(mdObj);
221 }
222 
223 HWTEST_F(CryptoMdTest, CryptoFrameworkMdLenTest001, TestSize.Level0)
224 {
225     // create a API obj with SHA1
226     HcfMd *mdObj = nullptr;
227     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
228     ASSERT_EQ(ret, HCF_SUCCESS);
229     // test api functions
230     uint32_t len = mdObj->getMdLength(mdObj);
231     EXPECT_EQ(len, SHA1_LEN);
232     HcfObjDestroy(mdObj);
233 }
234 
235 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoTest001, TestSize.Level0)
236 {
237     // create a API obj with SHA1
238     HcfMd *mdObj = nullptr;
239     HcfResult ret = HcfMdCreate("SHA1", &mdObj);
240     ASSERT_EQ(ret, HCF_SUCCESS);
241     // set input and output buf
242     uint8_t testData[] = "My test data";
243     // define input and output data in blob form
244     struct HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
245     struct HcfBlob outBlob = { .data = nullptr, .len = 0 };
246     // test api functions
247     ret = mdObj->update(mdObj, &inBlob);
248     EXPECT_EQ(ret, HCF_SUCCESS);
249     ret = mdObj->doFinal(mdObj, &outBlob);
250     EXPECT_EQ(ret, HCF_SUCCESS);
251     uint32_t len = mdObj->getMdLength(mdObj);
252     EXPECT_EQ(len, SHA1_LEN);
253     // destroy the API obj and blob data
254     HcfBlobDataClearAndFree(&outBlob);
255     HcfObjDestroy(mdObj);
256 }
257 
258 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoTest002, TestSize.Level0)
259 {
260     // create a API obj with SHA1
261     HcfMd *mdObj = nullptr;
262     HcfResult ret = HcfMdCreate("SHA224", &mdObj);
263     ASSERT_EQ(ret, HCF_SUCCESS);
264     // set input and output buf
265     uint8_t testData[] = "My test data";
266     // define input and output data in blob form
267     struct HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
268     struct HcfBlob outBlob = { .data = nullptr, .len = 0 };
269     // test api functions
270     ret = mdObj->update(mdObj, &inBlob);
271     EXPECT_EQ(ret, HCF_SUCCESS);
272     ret = mdObj->doFinal(mdObj, &outBlob);
273     EXPECT_EQ(ret, HCF_SUCCESS);
274     uint32_t len = mdObj->getMdLength(mdObj);
275     EXPECT_EQ(len, SHA224_LEN);
276     // destroy the API obj and blob data
277     HcfBlobDataClearAndFree(&outBlob);
278     HcfObjDestroy(mdObj);
279 }
280 
281 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoTest003, TestSize.Level0)
282 {
283     // create a API obj with SHA1
284     HcfMd *mdObj = nullptr;
285     HcfResult ret = HcfMdCreate("SHA256", &mdObj);
286     ASSERT_EQ(ret, HCF_SUCCESS);
287     // set input and output buf
288     uint8_t testData[] = "My test data";
289     // define input and output data in blob form
290     struct HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
291     struct HcfBlob outBlob = { .data = nullptr, .len = 0 };
292     // test api functions
293     ret = mdObj->update(mdObj, &inBlob);
294     EXPECT_EQ(ret, HCF_SUCCESS);
295     ret = mdObj->doFinal(mdObj, &outBlob);
296     EXPECT_EQ(ret, HCF_SUCCESS);
297     uint32_t len = mdObj->getMdLength(mdObj);
298     EXPECT_EQ(len, SHA256_LEN);
299     // destroy the API obj and blob data
300     HcfBlobDataClearAndFree(&outBlob);
301     HcfObjDestroy(mdObj);
302 }
303 
304 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoTest004, TestSize.Level0)
305 {
306     // create a API obj with SHA1
307     HcfMd *mdObj = nullptr;
308     HcfResult ret = HcfMdCreate("SHA384", &mdObj);
309     ASSERT_EQ(ret, HCF_SUCCESS);
310     // set input and output buf
311     uint8_t testData[] = "My test data";
312     // define input and output data in blob form
313     struct HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
314     struct HcfBlob outBlob = { .data = nullptr, .len = 0 };
315     // test api functions
316     ret = mdObj->update(mdObj, &inBlob);
317     EXPECT_EQ(ret, HCF_SUCCESS);
318     ret = mdObj->doFinal(mdObj, &outBlob);
319     EXPECT_EQ(ret, HCF_SUCCESS);
320     uint32_t len = mdObj->getMdLength(mdObj);
321     EXPECT_EQ(len, SHA384_LEN);
322     // destroy the API obj and blob data
323     HcfBlobDataClearAndFree(&outBlob);
324     HcfObjDestroy(mdObj);
325 }
326 
327 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoTest005, TestSize.Level0)
328 {
329     // create a API obj with SHA1
330     HcfMd *mdObj = nullptr;
331     HcfResult ret = HcfMdCreate("SHA512", &mdObj);
332     ASSERT_EQ(ret, HCF_SUCCESS);
333     // set input and output buf
334     uint8_t testData[] = "My test data";
335     // define input and output data in blob form
336     struct HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
337     struct HcfBlob outBlob = { .data = nullptr, .len = 0 };
338     // test api functions
339     ret = mdObj->update(mdObj, &inBlob);
340     EXPECT_EQ(ret, HCF_SUCCESS);
341     ret = mdObj->doFinal(mdObj, &outBlob);
342     EXPECT_EQ(ret, HCF_SUCCESS);
343     uint32_t len = mdObj->getMdLength(mdObj);
344     EXPECT_EQ(len, SHA512_LEN);
345     // destroy the API obj and blob data
346     HcfBlobDataClearAndFree(&outBlob);
347     HcfObjDestroy(mdObj);
348 }
349 
350 HWTEST_F(CryptoMdTest, CryptoFrameworkMdAlgoTest006, TestSize.Level0)
351 {
352     // create a API obj with SHA1
353     HcfMd *mdObj = nullptr;
354     HcfResult ret = HcfMdCreate("MD5", &mdObj);
355     ASSERT_EQ(ret, HCF_SUCCESS);
356     // set input and output buf
357     uint8_t testData[] = "My test data";
358     // define input and output data in blob form
359     struct HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
360     struct HcfBlob outBlob = { .data = nullptr, .len = 0 };
361     // test api functions
362     ret = mdObj->update(mdObj, &inBlob);
363     EXPECT_EQ(ret, HCF_SUCCESS);
364     ret = mdObj->doFinal(mdObj, &outBlob);
365     EXPECT_EQ(ret, HCF_SUCCESS);
366     uint32_t len = mdObj->getMdLength(mdObj);
367     EXPECT_EQ(len, MD5_LEN);
368     // destroy the API obj and blob data
369     HcfBlobDataClearAndFree(&outBlob);
370     HcfObjDestroy(mdObj);
371 }
372 
GetInvalidMdClass(void)373 static const char *GetInvalidMdClass(void)
374 {
375     return "INVALID_MD_CLASS";
376 }
377 
378 HWTEST_F(CryptoMdTest, InvalidInputMdTest001, TestSize.Level0)
379 {
380     HcfResult ret = OpensslMdSpiCreate("SHA256", nullptr);
381     EXPECT_NE(ret, HCF_SUCCESS);
382 }
383 
384 HWTEST_F(CryptoMdTest, NullParamMdTest001, TestSize.Level0)
385 {
386     HcfMd *mdObj = nullptr;
387     HcfResult ret = HcfMdCreate("MD5", &mdObj);
388     ret = mdObj->update(nullptr, nullptr);
389     EXPECT_NE(ret, HCF_SUCCESS);
390     ret = mdObj->doFinal(nullptr, nullptr);
391     EXPECT_NE(ret, HCF_SUCCESS);
392     uint32_t len = mdObj->getMdLength(nullptr);
393     EXPECT_EQ(len, HCF_OPENSSL_INVALID_MD_LEN);
394     const char *algoName = mdObj->getAlgoName(nullptr);
395     EXPECT_EQ(algoName, nullptr);
396     mdObj->base.destroy(nullptr);
397     HcfObjDestroy(mdObj);
398 }
399 
400 HWTEST_F(CryptoMdTest, InvalidFrameworkClassMdTest001, TestSize.Level0)
401 {
402     HcfMd *mdObj = nullptr;
403     HcfResult ret = HcfMdCreate("SHA256", &mdObj);
404     ASSERT_EQ(ret, HCF_SUCCESS);
405     HcfMd invalidMdObj = {{0}};
406     invalidMdObj.base.getClass = GetInvalidMdClass;
407     uint8_t testData[] = "My test data";
408     HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
409     HcfBlob outBlob = { .data = nullptr, .len = 0 };
410     ret = mdObj->update(&invalidMdObj, &inBlob);
411     EXPECT_NE(ret, HCF_SUCCESS);
412     ret = mdObj->doFinal(&invalidMdObj, &outBlob);
413     EXPECT_NE(ret, HCF_SUCCESS);
414     uint32_t len = mdObj->getMdLength(&invalidMdObj);
415     EXPECT_EQ(len, HCF_OPENSSL_INVALID_MD_LEN);
416     const char *algoName = mdObj->getAlgoName(&invalidMdObj);
417     EXPECT_EQ(algoName, nullptr);
418     HcfBlobDataClearAndFree(&outBlob);
419     mdObj->base.destroy(&(invalidMdObj.base));
420     HcfObjDestroy(mdObj);
421 }
422 
423 HWTEST_F(CryptoMdTest, InvalidSpiClassMdTest001, TestSize.Level0)
424 {
425     HcfMdSpi *spiObj = nullptr;
426     HcfMdSpi invalidSpi = {{0}};
427     invalidSpi.base.getClass = GetInvalidMdClass;
428     // set input and output blob
429     uint8_t testData[] = "My test data";
430     HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = sizeof(testData)};
431     HcfBlob outBlob = { .data = nullptr, .len = 0 };
432     HcfResult ret = OpensslMdSpiCreate("SHA256", &spiObj);
433     ASSERT_EQ(ret, HCF_SUCCESS);
434     ASSERT_NE(spiObj, nullptr);
435     (void)spiObj->base.destroy(nullptr);
436     (void)spiObj->base.destroy(&(invalidSpi.base));
437     ret = spiObj->engineUpdateMd(&invalidSpi, &inBlob);
438     EXPECT_NE(ret, HCF_SUCCESS);
439     ret = spiObj->engineDoFinalMd(&invalidSpi, &outBlob);
440     EXPECT_NE(ret, HCF_SUCCESS);
441     uint32_t len = spiObj->engineGetMdLength(&invalidSpi);
442     EXPECT_EQ(len, INVALID_LEN);
443     HcfObjDestroy(spiObj);
444 }
445 }