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