1 /*
2  * Copyright (C) 2021 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 "hks_mac_test.h"
17 
18 #include <hctest.h>
19 #include "hi_watchdog.h"
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_adapt_for_de.h"
23 #include "hks_test_api_performance.h"
24 #include "hks_test_common.h"
25 #include "hks_test_log.h"
26 
27 #define HKS_TEST_MAC_REE_KEY_SIZE_32 32
28 #define HKS_DEFAULT_MAC_SRCDATA_SIZE 253
29 #define HKS_DEFAULT_MAC_SHA256_SIZE 32
30 
31 
32 static const struct HksTestMacParams g_testMacParams[] = {
33     /* success: ree-sha256 */
34     { 0, HKS_SUCCESS, HKS_TEST_MAC_TYPE_REE, { 0 }, { 0 },
35         { true, HKS_TEST_MAC_REE_KEY_SIZE_32, true, HKS_TEST_MAC_REE_KEY_SIZE_32 },
36         { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
37         { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
38         { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
39     },
40 
41     /* success: tee-sha256 */
42     { 1, HKS_SUCCESS, HKS_TEST_MAC_TYPE_TEE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
43         {true, true, HKS_ALG_AES, true, HKS_AES_KEY_SIZE_256, true, HKS_KEY_PURPOSE_MAC,
44             true, HKS_DIGEST_SHA256, false, 0, false, 0 },
45         { 0 },
46         { true,  true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
47         { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
48         { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
49     },
50 };
51 
52 /*
53  * @tc.register: register a test suit named "CalcMultiTest"
54  * @param: test subsystem name
55  * @param: c_example module name
56  * @param: CalcMultiTest test suit name
57  */
58 LITE_TEST_SUIT(husk, huks_lite, HksMacTest);
59 
60 /**
61  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
62  * @return: true——setup success
63  */
HksMacTestSetUp()64 static BOOL HksMacTestSetUp()
65 {
66     LiteTestPrint("setup\n");
67     hi_watchdog_disable();
68     TEST_ASSERT_TRUE(HksInitialize() == 0);
69     return TRUE;
70 }
71 
72 /**
73  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
74  * @return: true——teardown success
75  */
HksMacTestTearDown()76 static BOOL HksMacTestTearDown()
77 {
78     LiteTestPrint("tearDown\n");
79     hi_watchdog_enable();
80     return TRUE;
81 }
82 
83 
ConstructDataToBlob(struct HksBlob ** srcData,struct HksBlob ** macData,const struct HksTestBlobParams * srcDataParams,const struct HksTestBlobParams * macDataParams)84 static int32_t ConstructDataToBlob(struct HksBlob **srcData, struct HksBlob **macData,
85     const struct HksTestBlobParams *srcDataParams, const struct HksTestBlobParams *macDataParams)
86 {
87     int32_t ret = TestConstuctBlob(srcData,
88         srcDataParams->blobExist,
89         srcDataParams->blobSize,
90         srcDataParams->blobDataExist,
91         srcDataParams->blobDataSize);
92     TEST_ASSERT_TRUE(ret == 0);
93 
94     ret = TestConstuctBlob(macData,
95         macDataParams->blobExist,
96         macDataParams->blobSize,
97         macDataParams->blobDataExist,
98         macDataParams->blobDataSize);
99     TEST_ASSERT_TRUE(ret == 0);
100     return ret;
101 }
102 
Mac(const struct HksBlob * key,const struct HksBlob * srcData,struct HksBlob * macData,const struct HksTestMacParamSet * macParamSetParams,enum HksTestMacType macType)103 static int32_t Mac(const struct HksBlob *key, const struct HksBlob *srcData, struct HksBlob *macData,
104     const struct HksTestMacParamSet *macParamSetParams, enum HksTestMacType macType)
105 {
106     struct HksParamSet *macParamSet = NULL;
107     int32_t ret;
108     if (macType == HKS_TEST_MAC_TYPE_REE) {
109         struct TestMacParamSetStructure paramStructTrue = {
110             &macParamSet,
111             macParamSetParams->paramSetExist,
112             macParamSetParams->setPurpose, macParamSetParams->purpose,
113             macParamSetParams->setDigest, macParamSetParams->digest, true, false
114         };
115         ret = TestConstructMacParamSet(&paramStructTrue);
116     } else {
117         struct TestMacParamSetStructure paramStructFalse = {
118             &macParamSet,
119             macParamSetParams->paramSetExist,
120             macParamSetParams->setPurpose, macParamSetParams->purpose,
121             macParamSetParams->setDigest, macParamSetParams->digest, false, false
122         };
123         ret = TestConstructMacParamSet(&paramStructFalse);
124     }
125     TEST_ASSERT_TRUE(ret == 0);
126 
127     ret = HksMacRun(key, macParamSet, srcData, macData, 1);
128     HksFreeParamSet(&macParamSet);
129     return ret;
130 }
131 
BaseTestMac(uint32_t index)132 static int32_t BaseTestMac(uint32_t index)
133 {
134     /* 1. generate key */
135     struct HksBlob *key = NULL;
136     int32_t ret;
137 
138     if (g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_REE) {
139         ret = TestConstuctBlob(&key,
140             g_testMacParams[index].keyParams.blobExist,
141             g_testMacParams[index].keyParams.blobSize,
142             g_testMacParams[index].keyParams.blobDataExist,
143             g_testMacParams[index].keyParams.blobDataSize);
144     } else {
145         if (g_testMacParams[index].keyAliasParams.blobExist) {
146             ret = GenerateKey(&key, &(g_testMacParams[index].keyAliasParams),
147                 &g_testMacParams[index].genKeyParamSetParams, NULL);
148         } else {
149             ret = TestConstuctBlob(&key,
150                 g_testMacParams[index].keyParams.blobExist,
151                 g_testMacParams[index].keyParams.blobSize,
152                 g_testMacParams[index].keyParams.blobDataExist,
153                 g_testMacParams[index].keyParams.blobDataSize);
154         }
155     }
156     TEST_ASSERT_TRUE(ret == 0);
157 
158     /* 2. mac */
159     struct HksBlob *srcData = NULL;
160     struct HksBlob *macData = NULL;
161     ret = ConstructDataToBlob(&srcData, &macData,
162         &g_testMacParams[index].srcDataParams, &g_testMacParams[index].macParams);
163     TEST_ASSERT_TRUE(ret == 0);
164 
165     ret = Mac(key, srcData, macData, &g_testMacParams[index].macParamSetParams, g_testMacParams[index].macType);
166     if (ret != g_testMacParams[index].expectResult) {
167         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testMacParams[index].testId, ret);
168     }
169     TEST_ASSERT_TRUE(ret == g_testMacParams[index].expectResult);
170 
171     /* 3. deletekey */
172     if ((g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_TEE) &&
173         (g_testMacParams[index].keyAliasParams.blobExist)) {
174         ret = HksDeleteKeyForDe(key, NULL);
175         TEST_ASSERT_TRUE(ret == 0);
176     }
177     TestFreeBlob(&key);
178     TestFreeBlob(&srcData);
179     TestFreeBlob(&macData);
180     return ret;
181 }
182 
183 /**
184  * @tc.name: HksMacTest.HksMacTest001
185  * @tc.desc: The static function will return true;
186  * @tc.type: FUNC
187  */
LITE_TEST_CASE(HksMacTest,HksMacTest001,Level1)188 LITE_TEST_CASE(HksMacTest, HksMacTest001, Level1)
189 {
190     int32_t ret = BaseTestMac(0);
191     TEST_ASSERT_TRUE(ret == 0);
192 }
193 
194 #ifndef _CUT_AUTHENTICATE_
195 /**
196  * @tc.name: HksMacTest.HksMacTest002
197  * @tc.desc: The static function will return true;
198  * @tc.type: FUNC
199  */
LITE_TEST_CASE(HksMacTest,HksMacTest002,Level1)200 LITE_TEST_CASE(HksMacTest, HksMacTest002, Level1)
201 {
202     int32_t ret = BaseTestMac(1);
203     TEST_ASSERT_TRUE(ret == 0);
204 }
205 #endif /* _CUT_AUTHENTICATE_ */
206 
207 RUN_TEST_SUITE(HksMacTest);
208