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