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