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