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