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 <unistd.h>
20
21 #include "hks_api.h"
22 #include "hks_param.h"
23 #include "hks_test_api_performance.h"
24 #include "hks_test_common.h"
25 #include "hks_test_log.h"
26
27 #include "cmsis_os2.h"
28 #include "ohos_types.h"
29
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 #define CAL_ARRAY_SIZE(arr) ((sizeof(arr)) / (sizeof((arr)[0])))
37 //*********************************************
38 #define SINGLE_PRINT_LENGTH 50
39 //*********************************************
40
41 //******************************************************
IntToAscii(uint8_t in_num)42 static char IntToAscii(uint8_t in_num)
43 {
44 if (in_num <= 9) {
45 return (char)('0' + in_num);
46 }
47 return (char)('A' + in_num - 10);
48 }
49
BufferToAscii(uint8_t * src,uint32_t src_size,char * dst,uint32_t * dst_size)50 static int32_t BufferToAscii(uint8_t* src, uint32_t src_size, char* dst, uint32_t* dst_size)
51 {
52 const uint32_t ascii_len = src_size * 2 + 1;
53 if (*dst_size < ascii_len) {
54 printf("buffer is too samll.");
55 return -1;
56 }
57 for (uint32_t i = 0; i < src_size; i++) {
58 dst[2 * i] = IntToAscii(src[i] >> 4);/* take 4 high-order digits*/
59 dst[2 * i + 1] = IntToAscii(src[i] & 0b00001111); /*take 4 low-order digits*/
60 }
61
62 dst[ascii_len - 1] = '\0';
63 *dst_size = ascii_len;
64 return 0;
65 }
66
printBuffer(uint8_t * buffer,uint32_t buffer_size)67 static void printBuffer(uint8_t* buffer, uint32_t buffer_size)
68 {
69 uint32_t index = 0;
70 uint32_t print_count = buffer_size / SINGLE_PRINT_LENGTH;
71 for (uint32_t i = 0; i < (print_count + 1); i++) {
72 char chars[SINGLE_PRINT_LENGTH * 2 + 1] = { 0 };
73 uint32_t char_size = SINGLE_PRINT_LENGTH * 2 + 1;
74 BufferToAscii(buffer + index, (i == print_count) ? buffer_size % SINGLE_PRINT_LENGTH : SINGLE_PRINT_LENGTH,
75 chars, &char_size);
76 printf("buff[%2u] size[%2u]: \"%s\"", i, (char_size - 1) / 2, chars);
77 printf("\n");
78 index += SINGLE_PRINT_LENGTH;
79 }
80 }
81
CharToHex(char c)82 static uint8_t CharToHex(char c)
83 {
84 if ((c >= 'A') && (c <= 'F')) {
85 return (c - 'A' + 10);
86 } else if ((c >= 'a') && (c <= 'f')) {
87 return (c - 'a' + 10);
88 } else if ((c >= '0') && (c <= '9')) {
89 return (c - '0');
90 } else {
91 return 16;
92 }
93 }
94
HexStringToByteForTest(const char * hexStr,uint8_t * byte,uint32_t byteLen)95 int32_t HexStringToByteForTest(const char *hexStr, uint8_t *byte,
96 uint32_t byteLen)
97 {
98 if (byte == NULL || hexStr == NULL) {
99 return -1;
100 }
101 uint32_t realHexLen = strlen(hexStr);
102 /* even number or not */
103 if (realHexLen % 2 != 0 || byteLen < realHexLen / 2) {
104 return -1;
105 }
106
107 for (uint32_t i = 0; i < realHexLen / 2; i++) {
108 uint8_t high = CharToHex(hexStr[i * 2]);
109 uint8_t low = CharToHex(hexStr[i * 2 + 1]);
110 if (high == 16 || low == 16) {
111 return -1;
112 }
113 /* 4: Set the high nibble */
114 byte[i] = high << 4;
115 /* Set the low nibble */
116 byte[i] |= low;
117 }
118 return 0;
119 }
120
ConstructParamSet(struct HksParamSet ** out,const struct HksParam * inParam,const uint32_t inParamNum)121 static int32_t ConstructParamSet(struct HksParamSet **out, const struct HksParam *inParam,
122 const uint32_t inParamNum)
123 {
124 struct HksParamSet *paramSet = NULL;
125 int32_t ret = HksInitParamSet(¶mSet);
126 if (ret != HKS_SUCCESS) {
127 return HKS_ERROR_MALLOC_FAIL;
128 }
129
130 ret = HksAddParams(paramSet, inParam, inParamNum);
131 if (ret != HKS_SUCCESS) {
132 HksFreeParamSet(¶mSet);
133 return HKS_ERROR_MALLOC_FAIL;
134 }
135
136 ret = HksBuildParamSet(¶mSet);
137 if (ret != HKS_SUCCESS) {
138 HksFreeParamSet(¶mSet);
139 return HKS_ERROR_MALLOC_FAIL;
140 }
141
142 *out = paramSet;
143 return HKS_SUCCESS;
144 }
145
BaseTestMacAnswer()146 static int32_t BaseTestMacAnswer()
147 {
148 char mainKey[] = "8DD3C70014857C93F2B3B131892BB67662CD7B41D5D0D1E28CC60480975050F6";
149 char msgSource[] = "484A813EA48CD370DEF23EA6C488199BDE801FB1DE7E155C3B4ADBD3459DBBBE3030383532444145463338413436424130413833303336323332383637323334394437454530434135394435303646393139323932433133363633413631433435343230343539443933464537373346393934354644363432373746424132434142384642393936444443314430423937363736464242313234324233393330";
150 uint8_t mainKeyByte[32] = { 0 };
151 uint8_t sourceByte[160] = { 0 };
152 uint8_t macByte[32] = { 0 };
153 HexStringToByteForTest(mainKey, mainKeyByte, 32);
154 HexStringToByteForTest(msgSource, sourceByte, 160);
155 struct HksBlob keyBlob = { 32, mainKeyByte };
156 struct HksBlob srcBlob = { 160, sourceByte };
157 struct HksBlob hmacBlob = { 32, macByte };
158 struct HksParamSet *paramSet = NULL;
159 struct HksParam hmacParam[] = {
160 {
161 .tag = HKS_TAG_PURPOSE,
162 .uint32Param = HKS_KEY_PURPOSE_MAC
163 }, {
164 .tag = HKS_TAG_DIGEST,
165 .uint32Param = HKS_DIGEST_SHA256
166 }, {
167 .tag = HKS_TAG_IS_KEY_ALIAS,
168 .boolParam = false
169 }
170 };
171 int32_t ret = ConstructParamSet(¶mSet, hmacParam, CAL_ARRAY_SIZE(hmacParam));
172 if (ret != HKS_SUCCESS) {
173 return ret;
174 }
175 ret = HksMac(&keyBlob, paramSet, &srcBlob, &hmacBlob);
176 printf("print Hmac: \n");
177 printBuffer(macByte, 32);
178 HksFreeParamSet(¶mSet);
179 return ret;
180 }
181
182 //************************************
183 static osPriority_t g_setPriority;
184
185
186 static const struct HksTestMacParams g_testMacParams[] = {
187 /* success: ree-sha256 */
188 { 0, HKS_SUCCESS, HKS_TEST_MAC_TYPE_REE, { 0 }, { 0 },
189 { true, HKS_TEST_MAC_REE_KEY_SIZE_32, true, HKS_TEST_MAC_REE_KEY_SIZE_32 },
190 { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
191 { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
192 { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
193 },
194
195 /* success: tee-sha256 */
196 { 1, HKS_SUCCESS, HKS_TEST_MAC_TYPE_TEE, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
197 { true, true, HKS_ALG_AES, true, HKS_AES_KEY_SIZE_256, true, HKS_KEY_PURPOSE_MAC,
198 true, HKS_DIGEST_SHA256, false, 0, false, 0 },
199 { 0 },
200 { true, true, HKS_KEY_PURPOSE_MAC, true, HKS_DIGEST_SHA256 },
201 { true, HKS_DEFAULT_MAC_SRCDATA_SIZE, true, HKS_DEFAULT_MAC_SRCDATA_SIZE },
202 { true, HKS_DEFAULT_MAC_SHA256_SIZE, true, HKS_DEFAULT_MAC_SHA256_SIZE }
203 },
204 };
205
206 /*
207 * @tc.register: register a test suit named "CalcMultiTest"
208 * @param: test subsystem name
209 * @param: c_example module name
210 * @param: CalcMultiTest test suit name
211 */
212 LITE_TEST_SUIT(security, securityData, HksMacTest);
213
ExecHksInitialize(void const * argument)214 static void ExecHksInitialize(void const *argument)
215 {
216 LiteTestPrint("HksInitialize Begin!\n");
217 TEST_ASSERT_TRUE(HksInitialize() == 0);
218 LiteTestPrint("HksInitialize End!\n");
219 osThreadExit();
220 }
221
222 /**
223 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
224 * @return: true——setup success
225 */
HksMacTestSetUp()226 static BOOL HksMacTestSetUp()
227 {
228 LiteTestPrint("setup\n");
229 osThreadId_t id;
230 osThreadAttr_t attr;
231 g_setPriority = osPriorityAboveNormal6;
232 attr.name = "test";
233 attr.attr_bits = 0U;
234 attr.cb_mem = NULL;
235 attr.cb_size = 0U;
236 attr.stack_mem = NULL;
237 attr.stack_size = TEST_TASK_STACK_SIZE;
238 attr.priority = g_setPriority;
239 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
240 sleep(WAIT_TO_TEST_DONE);
241 LiteTestPrint("HksMacTestSetUp End2!\n");
242 return TRUE;
243 }
244
245 /**
246 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
247 * @return: true——teardown success
248 */
HksMacTestTearDown()249 static BOOL HksMacTestTearDown()
250 {
251 LiteTestPrint("tearDown\n");
252 return TRUE;
253 }
254
ConstructDataToBlob(struct HksBlob ** srcData,struct HksBlob ** macData,const struct HksTestBlobParams * srcDataParams,const struct HksTestBlobParams * macDataParams)255 static int32_t ConstructDataToBlob(struct HksBlob **srcData, struct HksBlob **macData,
256 const struct HksTestBlobParams *srcDataParams, const struct HksTestBlobParams *macDataParams)
257 {
258 int32_t ret = TestConstuctBlob(srcData,
259 srcDataParams->blobExist,
260 srcDataParams->blobSize,
261 srcDataParams->blobDataExist,
262 srcDataParams->blobDataSize);
263 TEST_ASSERT_TRUE(ret == 0);
264
265 ret = TestConstuctBlob(macData,
266 macDataParams->blobExist,
267 macDataParams->blobSize,
268 macDataParams->blobDataExist,
269 macDataParams->blobDataSize);
270 TEST_ASSERT_TRUE(ret == 0);
271 return ret;
272 }
273
Mac(const struct HksBlob * key,const struct HksBlob * srcData,struct HksBlob * macData,const struct HksTestMacParamSet * macParamSetParams,enum HksTestMacType macType)274 static int32_t Mac(const struct HksBlob *key, const struct HksBlob *srcData, struct HksBlob *macData,
275 const struct HksTestMacParamSet *macParamSetParams, enum HksTestMacType macType)
276 {
277 struct HksParamSet *macParamSet = NULL;
278 int32_t ret;
279 if (macType == HKS_TEST_MAC_TYPE_REE) {
280 struct TestMacParamSetStructure paramStructTrue = {
281 &macParamSet,
282 macParamSetParams->paramSetExist,
283 macParamSetParams->setPurpose, macParamSetParams->purpose,
284 macParamSetParams->setDigest, macParamSetParams->digest, true, false
285 };
286 ret = TestConstructMacParamSet(¶mStructTrue);
287 } else {
288 struct TestMacParamSetStructure paramStructFalse = {
289 &macParamSet,
290 macParamSetParams->paramSetExist,
291 macParamSetParams->setPurpose, macParamSetParams->purpose,
292 macParamSetParams->setDigest, macParamSetParams->digest, false, false
293 };
294 ret = TestConstructMacParamSet(¶mStructFalse);
295 }
296 TEST_ASSERT_TRUE(ret == 0);
297
298 ret = HksMacRun(key, macParamSet, srcData, macData, 1);
299 HksFreeParamSet(&macParamSet);
300 return ret;
301 }
302
BaseTestMac(uint32_t index)303 static int32_t BaseTestMac(uint32_t index)
304 {
305 /* 1. generate key */
306 struct HksBlob *key = NULL;
307 int32_t ret;
308
309 if (g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_REE) {
310 ret = TestConstuctBlob(&key,
311 g_testMacParams[index].keyParams.blobExist,
312 g_testMacParams[index].keyParams.blobSize,
313 g_testMacParams[index].keyParams.blobDataExist,
314 g_testMacParams[index].keyParams.blobDataSize);
315 } else {
316 if (g_testMacParams[index].keyAliasParams.blobExist) {
317 ret = HuksGenerateKey(&key, &(g_testMacParams[index].keyAliasParams),
318 &g_testMacParams[index].genKeyParamSetParams, NULL);
319 } else {
320 ret = TestConstuctBlob(&key,
321 g_testMacParams[index].keyParams.blobExist,
322 g_testMacParams[index].keyParams.blobSize,
323 g_testMacParams[index].keyParams.blobDataExist,
324 g_testMacParams[index].keyParams.blobDataSize);
325 }
326 }
327 TEST_ASSERT_TRUE(ret == 0);
328
329 /* 2. mac */
330 struct HksBlob *srcData = NULL;
331 struct HksBlob *macData = NULL;
332 ret = ConstructDataToBlob(&srcData, &macData,
333 &g_testMacParams[index].srcDataParams, &g_testMacParams[index].macParams);
334 TEST_ASSERT_TRUE(ret == 0);
335
336 ret = Mac(key, srcData, macData, &g_testMacParams[index].macParamSetParams, g_testMacParams[index].macType);
337 if (ret != g_testMacParams[index].expectResult) {
338 HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testMacParams[index].testId, ret);
339 }
340 TEST_ASSERT_TRUE(ret == g_testMacParams[index].expectResult);
341
342 /* 3. deletekey */
343 if ((g_testMacParams[index].macType == HKS_TEST_MAC_TYPE_TEE) &&
344 (g_testMacParams[index].keyAliasParams.blobExist)) {
345 ret = HksDeleteKey(key, NULL);
346 TEST_ASSERT_TRUE(ret == 0);
347 }
348 TestFreeBlob(&key);
349 TestFreeBlob(&srcData);
350 TestFreeBlob(&macData);
351 return ret;
352 }
353
ExecHksMacTest001(void const * argument)354 static void ExecHksMacTest001(void const *argument)
355 {
356 LiteTestPrint("HksMacTest001 Begin!\n");
357 int32_t ret = BaseTestMac(0);
358 TEST_ASSERT_TRUE(ret == 0);
359 LiteTestPrint("HksMacTest001 End!\n");
360 osThreadExit();
361 }
362
ExecHksMacTest002(void const * argument)363 static void ExecHksMacTest002(void const *argument)
364 {
365 LiteTestPrint("HksMacTest002 Begin!\n");
366 int32_t ret = BaseTestMac(1);
367 TEST_ASSERT_TRUE(ret == 0);
368 ret = BaseTestMacAnswer();
369 LiteTestPrint("HksMacTest002 End!\n");
370 osThreadExit();
371 }
372
373 /**
374 * @tc.name: HksMacTest.HksMacTest001
375 * @tc.desc: The static function will return true;
376 * @tc.type: FUNC
377 */
LITE_TEST_CASE(HksMacTest,HksMacTest001,Level1)378 LITE_TEST_CASE(HksMacTest, HksMacTest001, Level1)
379 {
380 osThreadId_t id;
381 osThreadAttr_t attr;
382 g_setPriority = osPriorityAboveNormal6;
383 attr.name = "test";
384 attr.attr_bits = 0U;
385 attr.cb_mem = NULL;
386 attr.cb_size = 0U;
387 attr.stack_mem = NULL;
388 attr.stack_size = TEST_TASK_STACK_SIZE;
389 attr.priority = g_setPriority;
390 id = osThreadNew((osThreadFunc_t)ExecHksMacTest001, NULL, &attr);
391 sleep(WAIT_TO_TEST_DONE);
392 LiteTestPrint("HksMacTest001 End2!\n");
393 }
394
395 #ifndef _CUT_AUTHENTICATE_
396 /**
397 * @tc.name: HksMacTest.HksMacTest002
398 * @tc.desc: The static function will return true;
399 * @tc.type: FUNC
400 */
LITE_TEST_CASE(HksMacTest,HksMacTest002,Level1)401 LITE_TEST_CASE(HksMacTest, HksMacTest002, Level1)
402 {
403 osThreadId_t id;
404 osThreadAttr_t attr;
405 g_setPriority = osPriorityAboveNormal6;
406 attr.name = "test";
407 attr.attr_bits = 0U;
408 attr.cb_mem = NULL;
409 attr.cb_size = 0U;
410 attr.stack_mem = NULL;
411 attr.stack_size = TEST_TASK_STACK_SIZE;
412 attr.priority = g_setPriority;
413 id = osThreadNew((osThreadFunc_t)ExecHksMacTest002, NULL, &attr);
414 sleep(WAIT_TO_TEST_DONE);
415 LiteTestPrint("HksMacTest002 End2!\n");
416 }
417 #endif /* _CUT_AUTHENTICATE_ */
418
419 RUN_TEST_SUITE(HksMacTest);
420