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 "hctest.h"
17 #include "iot_watchdog.h"
18 #include "hks_agreement_test.h"
19 #include "hks_api.h"
20 #include "hks_param.h"
21 #include "hks_test_api_performance.h"
22 #include "hks_test_common.h"
23 #include "hks_test_log.h"
24 #include "hks_type.h"
25
26 #include "cmsis_os2.h"
27 #include "ohos_types.h"
28
29 #define TMP_SIZE 512
30 #define X25519_KEY_SIZE 32
31 #define TEST_TASK_STACK_SIZE 0x2000
32 #define WAIT_TO_TEST_DONE 4
33
34 static osPriority_t g_setPriority;
35
36 #ifndef _CUT_AUTHENTICATE_
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, HksAgreementTest);
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 /**
54 * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
55 * @return: true——setup success
56 */
HksAgreementTestSetUp()57 static BOOL HksAgreementTestSetUp()
58 {
59 LiteTestPrint("setup\n");
60 IoTWatchDogDisable();
61 osThreadId_t id;
62 osThreadAttr_t attr;
63 g_setPriority = osPriorityAboveNormal6;
64 attr.name = "test";
65 attr.attr_bits = 0U;
66 attr.cb_mem = NULL;
67 attr.cb_size = 0U;
68 attr.stack_mem = NULL;
69 attr.stack_size = TEST_TASK_STACK_SIZE;
70 attr.priority = g_setPriority;
71 id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
72 sleep(WAIT_TO_TEST_DONE);
73 LiteTestPrint("HksAgreementTestSetUp End2!\n");
74 return TRUE;
75 }
76
77 /**
78 * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
79 * @return: true——teardown success
80 */
HksAgreementTestTearDown()81 static BOOL HksAgreementTestTearDown()
82 {
83 LiteTestPrint("tearDown\n");
84
85 IoTWatchDogEnable();
86 return TRUE;
87 }
88
89 static const struct HksTestAgreeParams g_testAgreeParams[] = {
90 /* ree x25519 success */
91 { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
92 { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
93 { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true,
94 HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, true, HKS_DIGEST_SHA256,
95 false, 0, false, 0, true, HKS_STORAGE_TEMP },
96 { 0 },
97 { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true, false },
98 { true, TMP_SIZE, true, TMP_SIZE },
99 { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE },
100 { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE },
101 { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE }
102 },
103 };
104
AgreeKey(const struct HksTestAgreeParamSet * agreeParamSetParams,struct HksBlob * privateKey,struct HksBlob * peerPublicKey,struct HksBlob * agreedKey)105 static int32_t AgreeKey(const struct HksTestAgreeParamSet *agreeParamSetParams, struct HksBlob *privateKey,
106 struct HksBlob *peerPublicKey, struct HksBlob *agreedKey)
107 {
108 struct HksParamSet *agreeParamSet = NULL;
109 struct TestAgreeParamSetStructure paramStruct = {
110 &agreeParamSet,
111 agreeParamSetParams->paramSetExist,
112 agreeParamSetParams->setAlg, agreeParamSetParams->alg,
113 agreeParamSetParams->setKeySize, agreeParamSetParams->keySize,
114 agreeParamSetParams->setIsKeyAlias, agreeParamSetParams->isKeyAlias
115 };
116 int32_t ret = TestConstructAgreeParamSet(¶mStruct);
117 HKS_TEST_ASSERT(ret == 0);
118
119 ret = HksAgreeKeyRun(agreeParamSet, privateKey, peerPublicKey, agreedKey, 1);
120 HksFreeParamSet(&agreeParamSet);
121 return ret;
122 }
123
ExecHksAgreementTest001(void const * argument)124 static void ExecHksAgreementTest001(void const *argument)
125 {
126 LiteTestPrint("HksAgreementTest001 Begin!\n");
127
128 /* 1. generate key */
129 struct HksBlob *privateKey = NULL;
130 struct HksBlob *peerPubKeyAlias = NULL;
131 struct HksBlob *peerPublicKey = NULL;
132 int32_t ret;
133
134 if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
135 (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
136 ret = GenerateLocalX25519Key(&privateKey, NULL, &g_testAgreeParams[0].localPrivateKeyParams, NULL);
137 HKS_TEST_ASSERT(ret == 0);
138 ret = GenerateLocalX25519Key(NULL, &peerPublicKey, NULL, &g_testAgreeParams[0].localPublicKeyParams);
139 HKS_TEST_ASSERT(ret == 0);
140 }
141 /* 2. agreeKey */
142 struct HksBlob *agreeKey = NULL;
143 ret = TestConstuctBlob(&agreeKey,
144 g_testAgreeParams[0].agreedKeyParams.blobExist,
145 g_testAgreeParams[0].agreedKeyParams.blobSize,
146 g_testAgreeParams[0].agreedKeyParams.blobDataExist,
147 g_testAgreeParams[0].agreedKeyParams.blobDataSize);
148 HKS_TEST_ASSERT(ret == 0);
149
150 ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey, agreeKey);
151 HKS_TEST_ASSERT(ret == g_testAgreeParams[0].expectResult);
152
153 /* 3. delete key */
154 if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
155 (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
156 ((g_testAgreeParams[0].keyAlias1Params.blobExist) &&
157 (g_testAgreeParams[0].keyAlias2Params.blobExist))) {
158 ret = HksDeleteKey(privateKey, NULL);
159 HKS_TEST_ASSERT(ret == 0);
160 ret = HksDeleteKey(peerPubKeyAlias, NULL);
161 HKS_TEST_ASSERT(ret == 0);
162 }
163 TestFreeBlob(&privateKey);
164 TestFreeBlob(&peerPubKeyAlias);
165 TestFreeBlob(&peerPublicKey);
166 TestFreeBlob(&agreeKey);
167 TEST_ASSERT_TRUE(ret == 0);
168
169 LiteTestPrint("HksAgreementTest001 End!\n");
170 osThreadExit();
171 }
172
ExecHksAgreementTest002(void const * argument)173 static void ExecHksAgreementTest002(void const *argument)
174 {
175 LiteTestPrint("HksAgreementTest002 Begin!\n");
176
177 /* 1. generate key */
178 struct HksBlob *privateKey = NULL;
179 struct HksBlob *peerPubKeyAlias = NULL;
180 struct HksBlob *peerPublicKey = NULL;
181 int32_t ret;
182
183 if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
184 (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
185 ret = GenerateLocalX25519Key(&privateKey, NULL, &g_testAgreeParams[0].localPrivateKeyParams, NULL);
186 HKS_TEST_ASSERT(ret == 0);
187 ret = GenerateLocalX25519Key(NULL, &peerPublicKey, NULL, &g_testAgreeParams[0].localPublicKeyParams);
188 HKS_TEST_ASSERT(ret == 0);
189 }
190 /* 2. agreeKey */
191 struct HksBlob *agreeKey = NULL;
192 ret = TestConstuctBlob(&agreeKey,
193 g_testAgreeParams[0].agreedKeyParams.blobExist,
194 g_testAgreeParams[0].agreedKeyParams.blobSize,
195 g_testAgreeParams[0].agreedKeyParams.blobDataExist,
196 g_testAgreeParams[0].agreedKeyParams.blobDataSize);
197 HKS_TEST_ASSERT(ret == 0);
198
199 ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey, agreeKey);
200 HKS_TEST_ASSERT(ret == g_testAgreeParams[0].expectResult);
201
202 /* 3. delete key */
203 if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
204 (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
205 ((g_testAgreeParams[0].keyAlias1Params.blobExist) &&
206 (g_testAgreeParams[0].keyAlias2Params.blobExist))) {
207 ret = HksDeleteKey(privateKey, NULL);
208 HKS_TEST_ASSERT(ret == 0);
209 ret = HksDeleteKey(peerPubKeyAlias, NULL);
210 HKS_TEST_ASSERT(ret == 0);
211 }
212 TestFreeBlob(&privateKey);
213 TestFreeBlob(&peerPubKeyAlias);
214 TestFreeBlob(&peerPublicKey);
215 TestFreeBlob(&agreeKey);
216 TEST_ASSERT_TRUE(ret == 0);
217
218 LiteTestPrint("HksAgreementTest002 End!\n");
219 osThreadExit();
220 }
221
222 /**
223 * @tc.name: HksDeleteTest.HksDeleteTest001
224 * @tc.desc: The static function will return true;
225 * @tc.type: FUNC
226 */
LITE_TEST_CASE(HksAgreementTest,HksAgreementTest001,Level1)227 LITE_TEST_CASE(HksAgreementTest, HksAgreementTest001, Level1)
228 {
229 osThreadId_t id;
230 osThreadAttr_t attr;
231 g_setPriority = osPriorityAboveNormal6;
232 attr.name = "test";
233 attr.attr_bits = 0U;
234 attr.cb_mem = NULL;
235 attr.cb_size = 0U;
236 attr.stack_mem = NULL;
237 attr.stack_size = TEST_TASK_STACK_SIZE;
238 attr.priority = g_setPriority;
239 id = osThreadNew((osThreadFunc_t)ExecHksAgreementTest001, NULL, &attr);
240 sleep(WAIT_TO_TEST_DONE);
241 LiteTestPrint("HksAgreementTest001 End2!\n");
242 }
243
244
245 /**
246 * @tc.name: HksDeleteTest.HksDeleteTest002
247 * @tc.desc: The static function will return true;
248 * @tc.type: FUNC
249 */
LITE_TEST_CASE(HksAgreementTest,HksAgreementTest002,Level1)250 LITE_TEST_CASE(HksAgreementTest, HksAgreementTest002, Level1)
251 {
252 osThreadId_t id;
253 osThreadAttr_t attr;
254 g_setPriority = osPriorityAboveNormal6;
255 attr.name = "test";
256 attr.attr_bits = 0U;
257 attr.cb_mem = NULL;
258 attr.cb_size = 0U;
259 attr.stack_mem = NULL;
260 attr.stack_size = TEST_TASK_STACK_SIZE;
261 attr.priority = g_setPriority;
262 id = osThreadNew((osThreadFunc_t)ExecHksAgreementTest002, NULL, &attr);
263 sleep(WAIT_TO_TEST_DONE);
264 LiteTestPrint("HksAgreementTest002 End2!\n");
265 }
266
267 RUN_TEST_SUITE(HksAgreementTest);
268 #endif /* _CUT_AUTHENTICATE_ */
269
270