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 Execept 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 "iot_watchdog.h"
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_api_performance.h"
23 #include "hks_test_common.h"
24 #include "hks_test_log.h"
25
26 #include "cmsis_os2.h"
27 #include "ohos_types.h"
28
29 #define HKS_TEST_MAC_REE_KEY_SIZE_32 32
30 #define HKS_DEFAULT_MAC_SRCDATA_SIZE 253
31 #define HKS_DEFAULT_MAC_SHA256_SIZE 32
32
33 #define TEST_TASK_STACK_SIZE 0x2000
34 #define WAIT_TO_TEST_DONE 4
35
36 static osPriority_t g_setPriority;
37
38
39 static const struct HksTestMacParams g_testMacParams[] = {
40 /* success: ree-sha256 */
41 { 0, HKS_SUCCESS, HKS_TEST_MAC_TYPE_REE, { 0 }, { 0 },
42 { true, HKS_TEST_MAC_REE_KEY_SIZE_32, true, HKS_TEST_MAC_REE_KEY_SIZE_32 },
43 { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
44 { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
45 { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
46 },
47
48 /* success: tee-sha256 */
49 { 1, HKS_SUCCESS, HKS_TEST_MAC_TYPE_TEE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
50 { true, true, HKS_ALG_AES, true, HKS_AES_KEY_SIZE_256, true, HKS_KEY_PURPOSE_MAC,
51 true, HKS_DIGEST_SHA256, false, 0, false, 0 },
52 { 0 },
53 { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
54 { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
55 { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
56 },
57 };
58
59 /*
60 * @tc.register: register a test suit named "CalcMultiTest"
61 * @param: test subsystem name
62 * @param: c_example module name
63 * @param: CalcMultiTest test suit name
64 */
65 LITE_TEST_SUIT(security, securityData, HksMacTest);
66
ExecHksInitialize(void const * argument)67 static void ExecHksInitialize(void const *argument)
68 {
69 LiteTestPrint("HksInitialize Begin!\n");
70 TEST_ASSERT_TRUE(HksInitialize() == 0);
71 LiteTestPrint("HksInitialize End!\n");
72 osThreadExit();
73 }
74
75 /**
76 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
77 * @return: true——setup success
78 */
HksMacTestSetUp()79 static BOOL HksMacTestSetUp()
80 {
81 LiteTestPrint("setup\n");
82 IoTWatchDogDisable();
83 osThreadId_t id;
84 osThreadAttr_t attr;
85 g_setPriority = osPriorityAboveNormal6;
86 attr.name = "test";
87 attr.attr_bits = 0U;
88 attr.cb_mem = NULL;
89 attr.cb_size = 0U;
90 attr.stack_mem = NULL;
91 attr.stack_size = TEST_TASK_STACK_SIZE;
92 attr.priority = g_setPriority;
93 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
94 sleep(WAIT_TO_TEST_DONE);
95 LiteTestPrint("HksMacTestSetUp End2!\n");
96 return TRUE;
97 }
98
99 /**
100 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
101 * @return: true——teardown success
102 */
HksMacTestTearDown()103 static BOOL HksMacTestTearDown()
104 {
105 LiteTestPrint("tearDown\n");
106 IoTWatchDogEnable();
107 return TRUE;
108 }
109
ConstructDataToBlob(struct HksBlob ** srcData,struct HksBlob ** macData,const struct HksTestBlobParams * srcDataParams,const struct HksTestBlobParams * macDataParams)110 static int32_t ConstructDataToBlob(struct HksBlob **srcData, struct HksBlob **macData,
111 const struct HksTestBlobParams *srcDataParams, const struct HksTestBlobParams *macDataParams)
112 {
113 int32_t ret = TestConstuctBlob(srcData,
114 srcDataParams->blobExist,
115 srcDataParams->blobSize,
116 srcDataParams->blobDataExist,
117 srcDataParams->blobDataSize);
118 TEST_ASSERT_TRUE(ret == 0);
119
120 ret = TestConstuctBlob(macData,
121 macDataParams->blobExist,
122 macDataParams->blobSize,
123 macDataParams->blobDataExist,
124 macDataParams->blobDataSize);
125 TEST_ASSERT_TRUE(ret == 0);
126 return ret;
127 }
128
Mac(const struct HksBlob * key,const struct HksBlob * srcData,struct HksBlob * macData,const struct HksTestMacParamSet * macParamSetParams,enum HksTestMacType macType)129 static int32_t Mac(const struct HksBlob *key, const struct HksBlob *srcData, struct HksBlob *macData,
130 const struct HksTestMacParamSet *macParamSetParams, enum HksTestMacType macType)
131 {
132 struct HksParamSet *macParamSet = NULL;
133 int32_t ret;
134 if (macType == HKS_TEST_MAC_TYPE_REE) {
135 struct TestMacParamSetStructure paramStructTrue = {
136 &macParamSet,
137 macParamSetParams->paramSetExist,
138 macParamSetParams->setPurpose, macParamSetParams->purpose,
139 macParamSetParams->setDigest, macParamSetParams->digest, true, false
140 };
141 ret = TestConstructMacParamSet(¶mStructTrue);
142 } else {
143 struct TestMacParamSetStructure paramStructFalse = {
144 &macParamSet,
145 macParamSetParams->paramSetExist,
146 macParamSetParams->setPurpose, macParamSetParams->purpose,
147 macParamSetParams->setDigest, macParamSetParams->digest, false, false
148 };
149 ret = TestConstructMacParamSet(¶mStructFalse);
150 }
151 TEST_ASSERT_TRUE(ret == 0);
152
153 ret = HksMacRun(key, macParamSet, srcData, macData, 1);
154 HksFreeParamSet(&macParamSet);
155 return ret;
156 }
157
BaseTestMac(uint32_t index)158 static int32_t BaseTestMac(uint32_t index)
159 {
160 /* 1. generate key */
161 struct HksBlob *key = NULL;
162 int32_t ret;
163
164 if (g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_REE) {
165 ret = TestConstuctBlob(&key,
166 g_testMacParams[index].keyParams.blobExist,
167 g_testMacParams[index].keyParams.blobSize,
168 g_testMacParams[index].keyParams.blobDataExist,
169 g_testMacParams[index].keyParams.blobDataSize);
170 } else {
171 if (g_testMacParams[index].keyAliasParams.blobExist) {
172 ret = GenerateKey(&key, &(g_testMacParams[index].keyAliasParams),
173 &g_testMacParams[index].genKeyParamSetParams, NULL);
174 } else {
175 ret = TestConstuctBlob(&key,
176 g_testMacParams[index].keyParams.blobExist,
177 g_testMacParams[index].keyParams.blobSize,
178 g_testMacParams[index].keyParams.blobDataExist,
179 g_testMacParams[index].keyParams.blobDataSize);
180 }
181 }
182 TEST_ASSERT_TRUE(ret == 0);
183
184 /* 2. mac */
185 struct HksBlob *srcData = NULL;
186 struct HksBlob *macData = NULL;
187 ret = ConstructDataToBlob(&srcData, &macData,
188 &g_testMacParams[index].srcDataParams, &g_testMacParams[index].macParams);
189 TEST_ASSERT_TRUE(ret == 0);
190
191 ret = Mac(key, srcData, macData, &g_testMacParams[index].macParamSetParams, g_testMacParams[index].macType);
192 if (ret != g_testMacParams[index].expectResult) {
193 HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testMacParams[index].testId, ret);
194 }
195 TEST_ASSERT_TRUE(ret == g_testMacParams[index].expectResult);
196
197 /* 3. deletekey */
198 if ((g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_TEE) &&
199 (g_testMacParams[index].keyAliasParams.blobExist)) {
200 ret = HksDeleteKey(key, NULL);
201 TEST_ASSERT_TRUE(ret == 0);
202 }
203 TestFreeBlob(&key);
204 TestFreeBlob(&srcData);
205 TestFreeBlob(&macData);
206 return ret;
207 }
208
ExecHksMacTest001(void const * argument)209 static void ExecHksMacTest001(void const *argument)
210 {
211 LiteTestPrint("HksMacTest001 Begin!\n");
212 int32_t ret = BaseTestMac(0);
213 TEST_ASSERT_TRUE(ret == 0);
214 LiteTestPrint("HksMacTest001 End!\n");
215 osThreadExit();
216 }
217
ExecHksMacTest002(void const * argument)218 static void ExecHksMacTest002(void const *argument)
219 {
220 LiteTestPrint("HksMacTest002 Begin!\n");
221 int32_t ret = BaseTestMac(1);
222 TEST_ASSERT_TRUE(ret == 0);
223 LiteTestPrint("HksMacTest002 End!\n");
224 osThreadExit();
225 }
226
227 /**
228 * @tc.name: HksMacTest.HksMacTest001
229 * @tc.desc: The static function will return true;
230 * @tc.type: FUNC
231 */
LITE_TEST_CASE(HksMacTest,HksMacTest001,Level1)232 LITE_TEST_CASE(HksMacTest, HksMacTest001, Level1)
233 {
234 osThreadId_t id;
235 osThreadAttr_t attr;
236 g_setPriority = osPriorityAboveNormal6;
237 attr.name = "test";
238 attr.attr_bits = 0U;
239 attr.cb_mem = NULL;
240 attr.cb_size = 0U;
241 attr.stack_mem = NULL;
242 attr.stack_size = TEST_TASK_STACK_SIZE;
243 attr.priority = g_setPriority;
244 id = osThreadNew((osThreadFunc_t)ExecHksMacTest001, NULL, &attr);
245 sleep(WAIT_TO_TEST_DONE);
246 LiteTestPrint("HksMacTest001 End2!\n");
247 }
248
249 #ifndef _CUT_AUTHENTICATE_
250 /**
251 * @tc.name: HksMacTest.HksMacTest002
252 * @tc.desc: The static function will return true;
253 * @tc.type: FUNC
254 */
LITE_TEST_CASE(HksMacTest,HksMacTest002,Level1)255 LITE_TEST_CASE(HksMacTest, HksMacTest002, Level1)
256 {
257 osThreadId_t id;
258 osThreadAttr_t attr;
259 g_setPriority = osPriorityAboveNormal6;
260 attr.name = "test";
261 attr.attr_bits = 0U;
262 attr.cb_mem = NULL;
263 attr.cb_size = 0U;
264 attr.stack_mem = NULL;
265 attr.stack_size = TEST_TASK_STACK_SIZE;
266 attr.priority = g_setPriority;
267 id = osThreadNew((osThreadFunc_t)ExecHksMacTest002, NULL, &attr);
268 sleep(WAIT_TO_TEST_DONE);
269 LiteTestPrint("HksMacTest002 End2!\n");
270 }
271 #endif /* _CUT_AUTHENTICATE_ */
272
273 RUN_TEST_SUITE(HksMacTest);
274