• 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 #ifndef _CUT_AUTHENTICATE_
17 
18 #include "hks_safe_compare_key_test.h"
19 #include "hks_mem.h"
20 #include <hctest.h>
21 #include <unistd.h>
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_file_operator.h"
27 #include "hks_test_log.h"
28 #include "hks_test_mem.h"
29 
30 #include "cmsis_os2.h"
31 #include "ohos_types.h"
32 
33 static const char *g_storePath = "/storage/";
34 static const char *g_testOne = "TestOne";
35 static const char *g_testTwo = "TestTwo";
36 
37 #define TEST_TASK_STACK_SIZE      0x2000
38 #define WAIT_TO_TEST_DONE         4
39 
40 static osPriority_t g_setPriority;
41 
42 /*
43  * @tc.register: register a test suit named "CalcMultiTest"
44  * @param: test subsystem name
45  * @param: c_example module name
46  * @param: CalcMultiTest test suit name
47  */
48 LITE_TEST_SUIT(security, securityData, HksSafeCompareKeyTest);
49 
50 /**
51  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
52  * @return: true——setup success
53  */
ExecHksInitialize(void const * argument)54 static void ExecHksInitialize(void const *argument)
55 {
56     LiteTestPrint("HksInitialize Begin!\n");
57     TEST_ASSERT_TRUE(HksInitialize() == 0);
58     LiteTestPrint("HksInitialize End!\n");
59     osThreadExit();
60 }
61 
HksSafeCompareKeyTestSetUp()62 static BOOL HksSafeCompareKeyTestSetUp()
63 {
64     LiteTestPrint("setup\n");
65     osThreadId_t id;
66     osThreadAttr_t attr;
67     g_setPriority = osPriorityAboveNormal6;
68     attr.name = "test";
69     attr.attr_bits = 0U;
70     attr.cb_mem = NULL;
71     attr.cb_size = 0U;
72     attr.stack_mem = NULL;
73     attr.stack_size = TEST_TASK_STACK_SIZE;
74     attr.priority = g_setPriority;
75     id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
76     sleep(WAIT_TO_TEST_DONE);
77     LiteTestPrint("HksSafeCompareKeyTestSetUp End2!\n");
78     return TRUE;
79 }
80 
81 /**
82  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
83  * @return: true——teardown success
84  */
HksSafeCompareKeyTestTearDown()85 static BOOL HksSafeCompareKeyTestTearDown()
86 {
87     LiteTestPrint("tearDown\n");
88     return TRUE;
89 }
90 
91 
92 
93 static const struct HksTestGenKeyParams g_testGenKeyParams[] = {
94     /* x25519: tee sign/verify */
95     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
96         {
97             true,
98             true, HKS_ALG_X25519,
99             true, HKS_CURVE25519_KEY_SIZE_256,
100             true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
101             false, 0,
102             false, 0,
103             false, 0,
104             false, 0
105         },
106         { false, 0 },
107     },
108 };
109 
SafeTestGenerateKey(struct HksBlob * keyAlias)110 static int32_t SafeTestGenerateKey(struct HksBlob *keyAlias)
111 {
112     uint32_t index = 0;
113     uint32_t performTimes = 1;
114 
115     struct HksParamSet *paramSet = NULL;
116     struct GenerateKeyParamSetStructure paramStruct = {
117         &paramSet,
118         g_testGenKeyParams[index].paramSetParams.paramSetExist,
119         g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
120         g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
121         g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
122         g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
123         g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
124         g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
125         g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
126         g_testGenKeyParams[index].paramSetParams.keyStorageFlag
127     };
128     int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
129     HKS_TEST_ASSERT(ret == 0);
130 
131     struct HksParamSet *paramSetOut = NULL;
132     ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
133         g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
134         g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
135     HKS_TEST_ASSERT(ret == 0);
136 
137     ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
138     if (ret != g_testGenKeyParams[index].expectResult) {
139         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
140     }
141     HKS_TEST_ASSERT(ret == g_testGenKeyParams[index].expectResult);
142 
143     if (ret == g_testGenKeyParams[index].expectResult) {
144         ret = 0;
145     }
146     HksFreeParamSet(&paramSet);
147     HksFreeParamSet(&paramSetOut);
148     return ret;
149 }
150 
151 
152 static struct HksBlob g_storageImageBuffer;
153 
GetKeyOffsetByKeyAlias(const struct HksBlob * keyAlias,uint32_t * keyOffset)154 static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset)
155 {
156     struct HksBlob storageBuf = g_storageImageBuffer;
157 
158     /* 1. get imageBuffer total Len */
159     struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data;
160     uint32_t keyCount = keyInfoHead->keyCount;
161     uint32_t totalLen = keyInfoHead->totalLen;
162 
163     /* 2. traverse imageBuffer to search for keyAlias */
164     uint32_t offset = sizeof(*keyInfoHead);
165     for (uint32_t i = 0; i < keyCount; ++i) {
166         uint8_t *tmpBuf = storageBuf.data + offset;
167         struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf;
168         if (keyInfo->aliasSize == keyAlias->size) {
169             if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) {
170                 *keyOffset = offset;
171                 return HKS_SUCCESS;
172             }
173         }
174 
175         offset += keyInfo->keyInfoLen;
176     }
177 
178     return HKS_ERROR_NOT_EXIST;
179 }
180 
181 
CompareKeyData(struct HksBlob * keyAliasOne,struct HksBlob * keyAliasTwo)182 static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAliasTwo)
183 {
184     uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore");
185     if (sizeOne == 0) {
186         return 0;
187     }
188     uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
189     if (bufOne == NULL) {
190         return HKS_ERROR_MALLOC_FAIL;
191     }
192 
193     uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne);
194     TEST_ASSERT_TRUE(sizeRead > 0);
195 
196     g_storageImageBuffer.data = bufOne;
197     g_storageImageBuffer.size = sizeOne;
198 
199     int32_t offset1;
200     int ret = GetKeyOffsetByKeyAlias(keyAliasOne, (uint32_t *)(&offset1));
201     TEST_ASSERT_TRUE(ret == 0);
202 
203     struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1);
204 
205     int32_t offset2;
206     ret = GetKeyOffsetByKeyAlias(keyAliasTwo, (uint32_t *)(&offset2));
207     TEST_ASSERT_TRUE(ret == 0);
208 
209     struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2);
210 
211     TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen);
212 
213     ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen);
214     HksTestFree(bufOne);
215     return ret;
216 }
217 
ExcHksSafeCompareKeyTest001(void const * argument)218 static void ExcHksSafeCompareKeyTest001(void const *argument)
219 {
220     LiteTestPrint("HksSafeCompareKeyTest001 Begin!\n");
221     struct HksBlob keyAliasOne = { strlen(g_testOne), (uint8_t *)g_testOne };
222     int32_t ret = SafeTestGenerateKey(&keyAliasOne);
223     HKS_TEST_ASSERT(ret == 0);
224     struct HksBlob keyAliasTwo = { strlen(g_testTwo), (uint8_t *)g_testTwo };
225     ret = SafeTestGenerateKey(&keyAliasTwo);
226     HKS_TEST_ASSERT(ret == 0);
227 
228     ret = CompareKeyData(&keyAliasOne, &keyAliasTwo);
229     HKS_TEST_ASSERT(ret != 0);
230     TEST_ASSERT_TRUE(ret != 0);
231     LiteTestPrint("HksSafeCompareKeyTest001 End!\n");
232     osThreadExit();
233 }
234 
235 /**
236  * @tc.name: HksSafeCompareKeyTest.HksSafeCompareKeyTest001
237  * @tc.desc: The static function will return true;
238  * @tc.type: FUNC
239  */
LITE_TEST_CASE(HksSafeCompareKeyTest,HksSafeCompareKeyTest001,Level1)240 LITE_TEST_CASE(HksSafeCompareKeyTest, HksSafeCompareKeyTest001, Level1)
241 {
242     osThreadId_t id;
243     osThreadAttr_t attr;
244     g_setPriority = osPriorityAboveNormal6;
245     attr.name = "test";
246     attr.attr_bits = 0U;
247     attr.cb_mem = NULL;
248     attr.cb_size = 0U;
249     attr.stack_mem = NULL;
250     attr.stack_size = TEST_TASK_STACK_SIZE;
251     attr.priority = g_setPriority;
252     id = osThreadNew((osThreadFunc_t)ExcHksSafeCompareKeyTest001, NULL, &attr);
253     sleep(WAIT_TO_TEST_DONE);
254     LiteTestPrint("HksSafeCompareKeyTest001 End2!\n");
255 }
256 RUN_TEST_SUITE(HksSafeCompareKeyTest);
257 
258 #endif /* _CUT_AUTHENTICATE_ */
259