• 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 #include "hi_watchdog.h"
18 #include "hks_agreement_test.h"
19 
20 #include "hks_api.h"
21 #include "hks_param.h"
22 #include "hks_test_api_performance.h"
23 #include "hks_test_common.h"
24 #include "hks_test_log.h"
25 #include "hks_type.h"
26 
27 #ifndef _CUT_AUTHENTICATE_
28 
29 /*
30  * @tc.register: register a test suit named "CalcMultiTest"
31  * @param: test subsystem name
32  * @param: c_example module name
33  * @param: CalcMultiTest test suit name
34  */
35 LITE_TEST_SUIT(husk, huks_lite, HksAgreementTest);
36 
37 /**
38  * @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
39  * @return: true——setup success
40  */
HksAgreementTestSetUp()41 static BOOL HksAgreementTestSetUp()
42 {
43     LiteTestPrint("setup\n");
44 
45     hi_watchdog_disable();
46     TEST_ASSERT_TRUE(HksInitialize() == 0);
47     return TRUE;
48 }
49 
50 /**
51  * @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
52  * @return: true——teardown success
53  */
HksAgreementTestTearDown()54 static BOOL HksAgreementTestTearDown()
55 {
56     LiteTestPrint("tearDown\n");
57 
58     hi_watchdog_enable();
59     return TRUE;
60 }
61 
62 #define TMP_SIZE 512
63 #define X25519_KEY_SIZE 32
64 static const struct HksTestAgreeParams g_testAgreeParams[] = {
65     /* ree x25519 success */
66     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
67         { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
68         { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true,
69             HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, true, HKS_DIGEST_SHA256,
70             false, 0, false, 0, true, HKS_STORAGE_TEMP },
71         { 0 },
72         { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true, false },
73         { true, TMP_SIZE, true, TMP_SIZE },
74         { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE },
75         { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE },
76         { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE }
77     },
78 };
79 
AgreeKey(const struct HksTestAgreeParamSet * agreeParamSetParams,struct HksBlob * privateKey,struct HksBlob * peerPublicKey,struct HksBlob * agreedKey)80 static int32_t AgreeKey(const struct HksTestAgreeParamSet *agreeParamSetParams, struct HksBlob *privateKey,
81     struct HksBlob *peerPublicKey, struct HksBlob *agreedKey)
82 {
83     struct HksParamSet *agreeParamSet = NULL;
84     struct TestAgreeParamSetStructure paramStruct = {
85         &agreeParamSet,
86         agreeParamSetParams->paramSetExist,
87         agreeParamSetParams->setAlg, agreeParamSetParams->alg,
88         agreeParamSetParams->setKeySize, agreeParamSetParams->keySize,
89         agreeParamSetParams->setIsKeyAlias, agreeParamSetParams->isKeyAlias
90     };
91     int32_t ret = TestConstructAgreeParamSet(&paramStruct);
92     HKS_TEST_ASSERT(ret == 0);
93 
94     ret = HksAgreeKeyRun(agreeParamSet, privateKey, peerPublicKey, agreedKey, 1);
95     HksFreeParamSet(&agreeParamSet);
96     return ret;
97 }
98 
99 /**
100  * @tc.name: HksDeleteTest.HksDeleteTest001
101  * @tc.desc: The static function will return true;
102  * @tc.type: FUNC
103  */
LITE_TEST_CASE(HksAgreementTest,HksAgreementTest001,Level1)104 LITE_TEST_CASE(HksAgreementTest, HksAgreementTest001, Level1)
105 {
106      /* 1. generate key */
107     struct HksBlob *privateKey = NULL;
108     struct HksBlob *peerPubKeyAlias = NULL;
109     struct HksBlob *peerPublicKey = NULL;
110     int32_t ret;
111 
112     if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
113         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
114         ret = GenerateLocalX25519Key(&privateKey, NULL, &g_testAgreeParams[0].localPrivateKeyParams, NULL);
115         HKS_TEST_ASSERT(ret == 0);
116         ret = GenerateLocalX25519Key(NULL, &peerPublicKey, NULL, &g_testAgreeParams[0].localPublicKeyParams);
117         HKS_TEST_ASSERT(ret == 0);
118     }
119     /* 2. agreeKey */
120     struct HksBlob *agreeKey = NULL;
121     ret = TestConstuctBlob(&agreeKey,
122         g_testAgreeParams[0].agreedKeyParams.blobExist,
123         g_testAgreeParams[0].agreedKeyParams.blobSize,
124         g_testAgreeParams[0].agreedKeyParams.blobDataExist,
125         g_testAgreeParams[0].agreedKeyParams.blobDataSize);
126     HKS_TEST_ASSERT(ret == 0);
127 
128     ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey, agreeKey);
129     HKS_TEST_ASSERT(ret == g_testAgreeParams[0].expectResult);
130 
131     /* 3. delete key */
132     if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
133         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
134         ((g_testAgreeParams[0].keyAlias1Params.blobExist) &&
135         (g_testAgreeParams[0].keyAlias2Params.blobExist))) {
136         ret = HksDeleteKey(privateKey, NULL);
137         HKS_TEST_ASSERT(ret == 0);
138         ret = HksDeleteKey(peerPubKeyAlias, NULL);
139         HKS_TEST_ASSERT(ret == 0);
140     }
141     TestFreeBlob(&privateKey);
142     TestFreeBlob(&peerPubKeyAlias);
143     TestFreeBlob(&peerPublicKey);
144     TestFreeBlob(&agreeKey);
145     TEST_ASSERT_TRUE(ret == 0);
146 }
147 
148 
149 /**
150  * @tc.name: HksDeleteTest.HksDeleteTest002
151  * @tc.desc: The static function will return true;
152  * @tc.type: FUNC
153  */
LITE_TEST_CASE(HksAgreementTest,HksAgreementTest002,Level1)154 LITE_TEST_CASE(HksAgreementTest, HksAgreementTest002, Level1)
155 {
156      /* 1. generate key */
157     struct HksBlob *privateKey = NULL;
158     struct HksBlob *peerPubKeyAlias = NULL;
159     struct HksBlob *peerPublicKey = NULL;
160     struct HksBlob *privateKey1 = NULL;
161     struct HksBlob *peerPubKeyAlias1 = NULL;
162     struct HksBlob *peerPublicKey1 = NULL;
163     int32_t ret;
164 
165     if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
166         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
167         ret = GenerateLocalX25519Key(&privateKey, &peerPublicKey, &g_testAgreeParams[0].localPrivateKeyParams, \
168             &g_testAgreeParams[0].localPublicKeyParams);
169         TEST_ASSERT_TRUE(ret == 0);
170         ret = GenerateLocalX25519Key(&privateKey1, &peerPublicKey1, &g_testAgreeParams[0].localPrivateKeyParams, \
171             &g_testAgreeParams[0].localPublicKeyParams);
172         TEST_ASSERT_TRUE(ret == 0);
173     }
174     /* 2. agreeKey */
175     struct HksBlob *agreeKey = NULL;
176     ret = TestConstuctBlob(&agreeKey, g_testAgreeParams[0].agreedKeyParams.blobExist, \
177         g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \
178         g_testAgreeParams[0].agreedKeyParams.blobDataSize);
179     TEST_ASSERT_TRUE(ret == 0);
180 
181     ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey1, agreeKey);
182     TEST_ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult);
183 
184     struct HksBlob *agreeKey1 = NULL;
185     ret = TestConstuctBlob(&agreeKey1, g_testAgreeParams[0].agreedKeyParams.blobExist, \
186         g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \
187         g_testAgreeParams[0].agreedKeyParams.blobDataSize);
188     TEST_ASSERT_TRUE(ret == 0);
189 
190     ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey1, peerPublicKey, agreeKey1);
191     TEST_ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult);
192     TEST_ASSERT_EQUAL(agreeKey->size, agreeKey1->size);
193     TEST_ASSERT_TRUE(memcmp(agreeKey->data, agreeKey1->data, agreeKey->size) == 0);
194 
195     /* 3. delete key */
196     if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
197         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
198         ((g_testAgreeParams[0].keyAlias1Params.blobExist) && (g_testAgreeParams[0].keyAlias2Params.blobExist))) {
199         TEST_ASSERT_TRUE(HksDeleteKey(privateKey, NULL) == 0);
200         TEST_ASSERT_TRUE(HksDeleteKey(peerPubKeyAlias, NULL) == 0);
201         TEST_ASSERT_TRUE(HksDeleteKey(privateKey1, NULL) == 0);
202         TEST_ASSERT_TRUE(HksDeleteKey(peerPubKeyAlias1, NULL) == 0);
203     }
204     TestFreeBlob(&privateKey);
205     TestFreeBlob(&peerPubKeyAlias);
206     TestFreeBlob(&peerPublicKey);
207     TestFreeBlob(&agreeKey);
208     TestFreeBlob(&privateKey1);
209     TestFreeBlob(&peerPubKeyAlias1);
210     TestFreeBlob(&peerPublicKey1);
211     TestFreeBlob(&agreeKey1);
212 }
213 
214 RUN_TEST_SUITE(HksAgreementTest);
215 #endif /* _CUT_AUTHENTICATE_ */
216 
217