• 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 "hi_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 static const char *g_storePath = "/storage/";
31 static const char *g_testOne = "TestOne";
32 static const char *g_testTwo = "TestTwo";
33 
34 /*
35  * @tc.register: register a test suit named "CalcMultiTest"
36  * @param: test subsystem name
37  * @param: c_example module name
38  * @param: CalcMultiTest test suit name
39  */
40 LITE_TEST_SUIT(husk, huks_lite, HksSafeCompareKeyTest);
41 
42 /**
43  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
44  * @return: true——setup success
45  */
HksSafeCompareKeyTestSetUp()46 static BOOL HksSafeCompareKeyTestSetUp()
47 {
48     LiteTestPrint("setup\n");
49     hi_watchdog_disable();
50     TEST_ASSERT_TRUE(HksInitialize() == 0);
51     return TRUE;
52 }
53 
54 /**
55  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
56  * @return: true——teardown success
57  */
HksSafeCompareKeyTestTearDown()58 static BOOL HksSafeCompareKeyTestTearDown()
59 {
60     LiteTestPrint("tearDown\n");
61     hi_watchdog_enable();
62     return TRUE;
63 }
64 
65 
66 static const struct HksTestGenKeyParams g_testGenKeyParams[] = {
67     /* x25519: tee sign/verify */
68     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
69         {
70             true,
71             true, HKS_ALG_X25519,
72             true, HKS_CURVE25519_KEY_SIZE_256,
73             true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
74             false, 0,
75             false, 0,
76             false, 0,
77             false, 0
78         },
79         { false, 0 },
80     },
81 };
82 
SafeTestGenerateKey(struct HksBlob * keyAlias)83 static int32_t SafeTestGenerateKey(struct HksBlob *keyAlias)
84 {
85     uint32_t index = 0;
86     uint32_t performTimes = 1;
87 
88     struct HksParamSet *paramSet = NULL;
89     struct GenerateKeyParamSetStructure paramStruct = {
90         &paramSet,
91         g_testGenKeyParams[index].paramSetParams.paramSetExist,
92         g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
93         g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
94         g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
95         g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
96         g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
97         g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
98         g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
99         g_testGenKeyParams[index].paramSetParams.keyStorageFlag
100     };
101     int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
102     HKS_TEST_ASSERT(ret == 0);
103 
104     struct HksParamSet *paramSetOut = NULL;
105     ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
106         g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
107         g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
108     HKS_TEST_ASSERT(ret == 0);
109 
110     ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
111     if (ret != g_testGenKeyParams[index].expectResult) {
112         HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
113     }
114     HKS_TEST_ASSERT(ret == g_testGenKeyParams[index].expectResult);
115 
116     if (ret == g_testGenKeyParams[index].expectResult) {
117         ret = 0;
118     }
119     HksFreeParamSet(&paramSet);
120     HksFreeParamSet(&paramSetOut);
121     return ret;
122 }
123 
124 
125 static struct HksBlob g_storageImageBuffer;
126 
GetKeyOffsetByKeyAlias(const struct HksBlob * keyAlias,uint32_t * keyOffset)127 static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset)
128 {
129     struct HksBlob storageBuf = g_storageImageBuffer;
130 
131     /* 1. get imageBuffer total Len */
132     struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data;
133     uint32_t keyCount = keyInfoHead->keyCount;
134 
135     /* 2. traverse imageBuffer to search for keyAlias */
136     uint32_t offset = sizeof(*keyInfoHead);
137     for (uint16_t i = 0; i < keyCount; ++i) {
138         uint8_t *tmpBuf = storageBuf.data + offset;
139         struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf;
140         if (keyInfo->aliasSize == keyAlias->size) {
141             if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) {
142                 *keyOffset = offset;
143                 return HKS_SUCCESS;
144             }
145         }
146 
147         offset += keyInfo->keyInfoLen;
148     }
149 
150     return HKS_ERROR_NOT_EXIST;
151 }
152 
153 
CompareKeyData(struct HksBlob * keyAliasOne,struct HksBlob * keyAliasTwo)154 static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAliasTwo)
155 {
156     uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore");
157     if (sizeOne == 0) {
158         return 0;
159     }
160     uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
161     if (bufOne == NULL) {
162         return HKS_ERROR_MALLOC_FAIL;
163     }
164 
165     uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne);
166     TEST_ASSERT_TRUE(sizeRead > 0);
167 
168     g_storageImageBuffer.data = bufOne;
169     g_storageImageBuffer.size = sizeOne;
170 
171     int32_t offset1;
172     int ret = GetKeyOffsetByKeyAlias(keyAliasOne, &offset1);
173     TEST_ASSERT_TRUE(ret == 0);
174 
175     struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1);
176 
177     int32_t offset2;
178     ret = GetKeyOffsetByKeyAlias(keyAliasTwo, &offset2);
179     TEST_ASSERT_TRUE(ret == 0);
180 
181     struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2);
182 
183     TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen);
184 
185     ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen);
186     HksTestFree(bufOne);
187     return ret;
188 }
189 
190 /**
191  * @tc.name: HksSafeCompareKeyTest.HksSafeCompareKeyTest001
192  * @tc.desc: The static function will return true;
193  * @tc.type: FUNC
194  */
LITE_TEST_CASE(HksSafeCompareKeyTest,HksSafeCompareKeyTest001,Level1)195 LITE_TEST_CASE(HksSafeCompareKeyTest, HksSafeCompareKeyTest001, Level1)
196 {
197     struct HksBlob keyAliasOne = { strlen(g_testOne), (uint8_t *)g_testOne };
198     int32_t ret = SafeTestGenerateKey(&keyAliasOne);
199     HKS_TEST_ASSERT(ret == 0);
200     struct HksBlob keyAliasTwo = { strlen(g_testTwo), (uint8_t *)g_testTwo };
201     ret = SafeTestGenerateKey(&keyAliasTwo);
202     HKS_TEST_ASSERT(ret == 0);
203 
204     ret = CompareKeyData(&keyAliasOne, &keyAliasTwo);
205     HKS_TEST_ASSERT(ret != 0);
206     TEST_ASSERT_TRUE(ret != 0);
207 }
208 RUN_TEST_SUITE(HksSafeCompareKeyTest);
209 
210 #endif /* _CUT_AUTHENTICATE_ */
211