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