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