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 "iot_watchdog.h"
17 #include <hctest.h>
18
19 #include "hks_derive_test.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 #include "cmsis_os2.h"
27 #include "ohos_types.h"
28
29 #define DEFAULT_DERIVE_SIZE 32
30 #define DEFAULT_INFO_SIZE 55
31 #define DEFAULT_SALT_SIZE 16
32 #define TEST_TASK_STACK_SIZE 0x2000
33 #define WAIT_TO_TEST_DONE 4
34
35 static osPriority_t g_setPriority;
36
37 /*
38 * @tc.register: register a test suit named "CalcMultiTest"
39 * @param: test subsystem name
40 * @param: c_example module name
41 * @param: CalcMultiTest test suit name
42 */
43 LITE_TEST_SUIT(security, securityData, HksDeriveTest);
44
ExecHksInitialize(void const * argument)45 static void ExecHksInitialize(void const *argument)
46 {
47 LiteTestPrint("HksInitialize Begin!\n");
48 TEST_ASSERT_TRUE(HksInitialize() == 0);
49 LiteTestPrint("HksInitialize End!\n");
50 osThreadExit();
51 }
52 /**
53 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
54 * @return: true——setup success
55 */
HksDeriveTestSetUp()56 static BOOL HksDeriveTestSetUp()
57 {
58 LiteTestPrint("setup\n");
59 IoTWatchDogDisable();
60 osThreadId_t id;
61 osThreadAttr_t attr;
62 g_setPriority = osPriorityAboveNormal6;
63 attr.name = "test";
64 attr.attr_bits = 0U;
65 attr.cb_mem = NULL;
66 attr.cb_size = 0U;
67 attr.stack_mem = NULL;
68 attr.stack_size = TEST_TASK_STACK_SIZE;
69 attr.priority = g_setPriority;
70 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
71 sleep(WAIT_TO_TEST_DONE);
72 LiteTestPrint("HksDeriveTestSetUp End2!\n");
73 return TRUE;
74 }
75
76 /**
77 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
78 * @return: true——teardown success
79 */
HksDeriveTestTearDown()80 static BOOL HksDeriveTestTearDown()
81 {
82 LiteTestPrint("tearDown\n");
83 IoTWatchDogEnable();
84 return TRUE;
85 }
86
87
88 static const struct HksTestDeriveParams g_testDeriveParams[] = {
89 /* hkdf-sha256-salt-info */
90 { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
91 {
92 true, /* genKey params */
93 true, HKS_ALG_AES,
94 true, HKS_AES_KEY_SIZE_256,
95 true, HKS_KEY_PURPOSE_DERIVE,
96 true, HKS_DIGEST_SHA256,
97 false, 0,
98 false, 0,
99 false, 0 },
100 { 0 },
101 {
102 true, /* derive params */
103 true, HKS_ALG_HKDF,
104 true, HKS_KEY_PURPOSE_DERIVE,
105 true, HKS_DIGEST_SHA256,
106 false, 0,
107 true, DEFAULT_SALT_SIZE,
108 true, DEFAULT_INFO_SIZE,
109 false, true },
110 {
111 true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE },
112 {
113 false, 0, false, 0 }
114 },
115
116 /* local: hkdf-sha256-salt-info */
117 { 1, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
118 {
119 true, /* genKey params */
120 true, HKS_ALG_AES,
121 true, HKS_AES_KEY_SIZE_256,
122 true, HKS_KEY_PURPOSE_DERIVE,
123 true, HKS_DIGEST_SHA256,
124 false, 0,
125 false, 0,
126 true, HKS_STORAGE_TEMP },
127 { 0 },
128 {
129 true, /* derive params */
130 true, HKS_ALG_HKDF,
131 true, HKS_KEY_PURPOSE_DERIVE,
132 true, HKS_DIGEST_SHA256,
133 false, 0,
134 true, DEFAULT_SALT_SIZE,
135 true, DEFAULT_INFO_SIZE,
136 true, false },
137 {
138 true, DEFAULT_DERIVE_SIZE, true, DEFAULT_DERIVE_SIZE },
139 {
140 true, DEFAULT_LOCAL_KEY_SIZE, true, DEFAULT_LOCAL_KEY_SIZE }
141 },
142 };
143
DeriveKey(const struct HksTestDeriveParamSet * deriveParamSetParams,const struct HksBlob * masterKey,struct HksBlob * derivedKey,struct HksBlob ** saltData,struct HksBlob ** infoData)144 static int32_t DeriveKey(const struct HksTestDeriveParamSet *deriveParamSetParams, const struct HksBlob *masterKey,
145 struct HksBlob *derivedKey, struct HksBlob **saltData, struct HksBlob **infoData)
146 {
147 struct HksParamSet *deriveParamSet = NULL;
148 uint32_t saltSize = deriveParamSetParams->saltSize;
149 uint32_t infoSize = deriveParamSetParams->infoSize;
150 if (saltSize != 0) {
151 HKS_TEST_ASSERT(TestConstuctBlob(saltData, true, saltSize, true, saltSize) == 0);
152 }
153 if (infoSize != 0) {
154 HKS_TEST_ASSERT(TestConstuctBlob(infoData, true, infoSize, true, infoSize) == 0);
155 }
156 struct TestDeriveParamSetStructure paramStruct = {
157 &deriveParamSet,
158 deriveParamSetParams->paramSetExist,
159 deriveParamSetParams->setAlg, deriveParamSetParams->alg,
160 deriveParamSetParams->setPurpose, deriveParamSetParams->purpose,
161 deriveParamSetParams->setDigest, deriveParamSetParams->digest,
162 deriveParamSetParams->setIteration, deriveParamSetParams->iteration,
163 deriveParamSetParams->setSalt, *saltData,
164 deriveParamSetParams->setInfo, *infoData,
165 deriveParamSetParams->setIsKeyAlias, deriveParamSetParams->isKeyAlias
166 };
167 int32_t ret = TestConstructDeriveParamSet(¶mStruct);
168 HKS_TEST_ASSERT(ret == 0);
169
170 ret = HksDeriveKeyRun(deriveParamSet, masterKey, derivedKey, 1);
171 HksFreeParamSet(&deriveParamSet);
172 return ret;
173 }
174
BaseTestDerive(uint32_t index)175 static int32_t BaseTestDerive(uint32_t index)
176 {
177 /* 1. generate key */
178 struct HksBlob *keyAlias = NULL;
179 int32_t ret;
180 if (g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
181 (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
182 ret = GenerateLocalRandomKey(&keyAlias, &g_testDeriveParams[index].localKeyParams);
183 } else {
184 if (g_testDeriveParams[index].keyAliasParams.blobExist) {
185 ret = GenerateKey(&keyAlias, &g_testDeriveParams[index].keyAliasParams,
186 &g_testDeriveParams[index].genKeyParamSetParams, NULL);
187 } else {
188 ret = TestConstuctBlob(&keyAlias,
189 g_testDeriveParams[index].masterKeyParams.blobExist,
190 g_testDeriveParams[index].masterKeyParams.blobSize,
191 g_testDeriveParams[index].masterKeyParams.blobDataExist,
192 g_testDeriveParams[index].masterKeyParams.blobDataSize);
193 }
194 }
195 TEST_ASSERT_TRUE(ret == 0);
196
197 /* 2. derive */
198 struct HksBlob *derivedKey = NULL;
199 ret = TestConstuctBlob(&derivedKey,
200 g_testDeriveParams[index].derivedKeyParams.blobExist,
201 g_testDeriveParams[index].derivedKeyParams.blobSize,
202 g_testDeriveParams[index].derivedKeyParams.blobDataExist,
203 g_testDeriveParams[index].derivedKeyParams.blobDataSize);
204 TEST_ASSERT_TRUE(ret == 0);
205
206 struct HksBlob *saltData = NULL;
207 struct HksBlob *infoData = NULL;
208 ret = DeriveKey(&g_testDeriveParams[index].deriveParamSetParams, keyAlias, derivedKey, &saltData, &infoData);
209 if (ret != g_testDeriveParams[index].expectResult) {
210 HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testDeriveParams[index].testId, ret);
211 }
212 TEST_ASSERT_TRUE(ret == g_testDeriveParams[index].expectResult);
213
214 /* 3. delete key */
215 if (!(g_testDeriveParams[index].genKeyParamSetParams.setKeyStorageFlag &&
216 (g_testDeriveParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
217 (g_testDeriveParams[index].keyAliasParams.blobExist)) {
218 TEST_ASSERT_TRUE(HksDeleteKey(keyAlias, NULL) == 0);
219 }
220 TestFreeBlob(&keyAlias);
221 TestFreeBlob(&derivedKey);
222 TestFreeBlob(&saltData);
223 TestFreeBlob(&infoData);
224
225 return ret;
226 }
227
ExecHksDeriveTest001(void const * argument)228 static void ExecHksDeriveTest001(void const *argument)
229 {
230 LiteTestPrint("HksDeriveTest001 Begin!\n");
231 int32_t ret = BaseTestDerive(0);
232 TEST_ASSERT_TRUE(ret == 0);
233 LiteTestPrint("HksDeriveTest001 End!\n");
234 osThreadExit();
235 }
236
ExecHksDeriveTest002(void const * argument)237 static void ExecHksDeriveTest002(void const *argument)
238 {
239 LiteTestPrint("HksDeriveTest002 Begin!\n");
240 int32_t ret = BaseTestDerive(1);
241 TEST_ASSERT_TRUE(ret == 0);
242 LiteTestPrint("HksDeriveTest002 End!\n");
243 osThreadExit();
244 }
245
246 #ifndef _CUT_AUTHENTICATE_
247 /**
248 * @tc.name: HksDeriveTest.HksDeriveTest001
249 * @tc.desc: The static function will return true;
250 * @tc.type: FUNC
251 */
LITE_TEST_CASE(HksDeriveTest,HksDeriveTest001,Level1)252 LITE_TEST_CASE(HksDeriveTest, HksDeriveTest001, Level1)
253 {
254 osThreadId_t id;
255 osThreadAttr_t attr;
256 g_setPriority = osPriorityAboveNormal6;
257 attr.name = "test";
258 attr.attr_bits = 0U;
259 attr.cb_mem = NULL;
260 attr.cb_size = 0U;
261 attr.stack_mem = NULL;
262 attr.stack_size = TEST_TASK_STACK_SIZE;
263 attr.priority = g_setPriority;
264 id = osThreadNew((osThreadFunc_t)ExecHksDeriveTest001, NULL, &attr);
265 sleep(WAIT_TO_TEST_DONE);
266 LiteTestPrint("HksDeriveTest001 End2!\n");
267 }
268 #endif /* _CUT_AUTHENTICATE_ */
269
270 /**
271 * @tc.name: HksDeriveTest.HksDeriveTest002
272 * @tc.desc: The static function will return true;
273 * @tc.type: FUNC
274 */
LITE_TEST_CASE(HksDeriveTest,HksDeriveTest002,Level1)275 LITE_TEST_CASE(HksDeriveTest, HksDeriveTest002, Level1)
276 {
277 osThreadId_t id;
278 osThreadAttr_t attr;
279 g_setPriority = osPriorityAboveNormal6;
280 attr.name = "test";
281 attr.attr_bits = 0U;
282 attr.cb_mem = NULL;
283 attr.cb_size = 0U;
284 attr.stack_mem = NULL;
285 attr.stack_size = TEST_TASK_STACK_SIZE;
286 attr.priority = g_setPriority;
287 id = osThreadNew((osThreadFunc_t)ExecHksDeriveTest002, NULL, &attr);
288 sleep(WAIT_TO_TEST_DONE);
289 LiteTestPrint("HksDeriveTest002 End2!\n");
290 }
291 RUN_TEST_SUITE(HksDeriveTest);
292