• 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 <gtest/gtest.h>
17 
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 using namespace testing::ext;
28 namespace {
29 #ifndef _CUT_AUTHENTICATE_
30 
31 class HksAgreementTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34 
35     static void TearDownTestCase(void);
36 
37     void SetUp();
38 
39     void TearDown();
40 };
41 
SetUpTestCase(void)42 void HksAgreementTest::SetUpTestCase(void)
43 {
44 }
45 
TearDownTestCase(void)46 void HksAgreementTest::TearDownTestCase(void)
47 {
48 }
49 
SetUp()50 void HksAgreementTest::SetUp()
51 {
52     EXPECT_EQ(HksInitialize(), 0);
53 }
54 
TearDown()55 void HksAgreementTest::TearDown()
56 {
57 }
58 
59 const int TMP_SIZE = 512;
60 const int X25519_KEY_SIZE = 32;
61 const struct HksTestAgreeParams g_testAgreeParams[] = {
62     /* ree x25519 success */
63     { 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
64         { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
65         { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true,
66             HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY, true, HKS_DIGEST_SHA256,
67             false, 0, false, 0, true, HKS_STORAGE_TEMP },
68         { 0 },
69         { true, true, HKS_ALG_X25519, true, HKS_CURVE25519_KEY_SIZE_256, true, false },
70         { true, TMP_SIZE, true, TMP_SIZE },
71         { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE },
72         { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE },
73         { true, X25519_KEY_SIZE, true, X25519_KEY_SIZE }
74     },
75 };
76 
AgreeKey(const struct HksTestAgreeParamSet * agreeParamSetParams,struct HksBlob * privateKey,struct HksBlob * peerPublicKey,struct HksBlob * agreedKey)77 static int32_t AgreeKey(const struct HksTestAgreeParamSet *agreeParamSetParams, struct HksBlob *privateKey,
78     struct HksBlob *peerPublicKey, struct HksBlob *agreedKey)
79 {
80     struct HksParamSet *agreeParamSet = NULL;
81     struct TestAgreeParamSetStructure paramStruct = {
82         &agreeParamSet,
83         agreeParamSetParams->paramSetExist,
84         agreeParamSetParams->setAlg, agreeParamSetParams->alg,
85         agreeParamSetParams->setKeySize, agreeParamSetParams->keySize,
86         agreeParamSetParams->setIsKeyAlias, agreeParamSetParams->isKeyAlias
87     };
88     int32_t ret = TestConstructAgreeParamSet(&paramStruct);
89     HKS_TEST_ASSERT(ret == 0);
90 
91     ret = HksAgreeKeyRun(agreeParamSet, privateKey, peerPublicKey, agreedKey, 1);
92     HksFreeParamSet(&agreeParamSet);
93     return ret;
94 }
95 
96 /**
97  * @tc.name: HksDeleteTest.HksDeleteTest001
98  * @tc.desc: The static function will return true;
99  * @tc.type: FUNC
100  */
101 HWTEST_F(HksAgreementTest, HksAgreementTest001, TestSize.Level1)
102 {
103      /* 1. generate key */
104     struct HksBlob *privateKey = NULL;
105     struct HksBlob *peerPubKeyAlias = NULL;
106     struct HksBlob *peerPublicKey = NULL;
107     int32_t ret;
108 
109     if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
110         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
111         ret = GenerateLocalX25519Key(&privateKey, NULL, &g_testAgreeParams[0].localPrivateKeyParams, NULL);
112         ASSERT_TRUE(ret == 0);
113         ret = GenerateLocalX25519Key(NULL, &peerPublicKey, NULL, &g_testAgreeParams[0].localPublicKeyParams);
114         ASSERT_TRUE(ret == 0);
115     }
116     /* 2. agreeKey */
117     struct HksBlob *agreeKey = NULL;
118     ret = TestConstuctBlob(&agreeKey,
119         g_testAgreeParams[0].agreedKeyParams.blobExist,
120         g_testAgreeParams[0].agreedKeyParams.blobSize,
121         g_testAgreeParams[0].agreedKeyParams.blobDataExist,
122         g_testAgreeParams[0].agreedKeyParams.blobDataSize);
123     ASSERT_TRUE(ret == 0);
124 
125     ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey, agreeKey);
126     ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult);
127 
128     /* 3. delete key */
129     if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
130         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) &&
131         ((g_testAgreeParams[0].keyAlias1Params.blobExist) &&
132         (g_testAgreeParams[0].keyAlias2Params.blobExist))) {
133         ret = HksDeleteKey(privateKey, NULL);
134         ASSERT_TRUE(ret == 0);
135         ret = HksDeleteKey(peerPubKeyAlias, NULL);
136         ASSERT_TRUE(ret == 0);
137     }
138     TestFreeBlob(&privateKey);
139     TestFreeBlob(&peerPubKeyAlias);
140     TestFreeBlob(&peerPublicKey);
141     TestFreeBlob(&agreeKey);
142     ASSERT_TRUE(ret == 0);
143 }
144 
145 /**
146  * @tc.name: HksDeleteTest.HksDeleteTest001
147  * @tc.desc: The static function will return true;
148  * @tc.type: FUNC
149  */
150 HWTEST_F(HksAgreementTest, HksAgreementTest002, TestSize.Level1)
151 {
152      /* 1. generate key */
153     struct HksBlob *privateKey = NULL;
154     struct HksBlob *peerPubKeyAlias = NULL;
155     struct HksBlob *peerPublicKey = NULL;
156     struct HksBlob *privateKey2 = NULL;
157     struct HksBlob *peerPublicKey2 = NULL;
158     int32_t ret;
159 
160     if (g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag &&
161         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
162         ret = GenerateLocalX25519Key(&privateKey, &peerPublicKey, &g_testAgreeParams[0].localPrivateKeyParams, \
163             &g_testAgreeParams[0].localPublicKeyParams);
164         ASSERT_TRUE(ret == 0);
165         ret = GenerateLocalX25519Key(&privateKey2, &peerPublicKey2, &g_testAgreeParams[0].localPrivateKeyParams, \
166             &g_testAgreeParams[0].localPublicKeyParams);
167         ASSERT_TRUE(ret == 0);
168     }
169     /* 2. agreeKey */
170     struct HksBlob *agreeKey = NULL;
171     ret = TestConstuctBlob(&agreeKey, g_testAgreeParams[0].agreedKeyParams.blobExist, \
172         g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \
173         g_testAgreeParams[0].agreedKeyParams.blobDataSize);
174     ASSERT_TRUE(ret == 0);
175 
176     struct HksBlob *agreeKey2 = NULL;
177     ret = TestConstuctBlob(&agreeKey2, g_testAgreeParams[0].agreedKeyParams.blobExist, \
178         g_testAgreeParams[0].agreedKeyParams.blobSize, g_testAgreeParams[0].agreedKeyParams.blobDataExist, \
179         g_testAgreeParams[0].agreedKeyParams.blobDataSize);
180     ASSERT_TRUE(ret == 0);
181 
182     ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey, peerPublicKey2, agreeKey);
183     ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult);
184 
185     ret = AgreeKey(&g_testAgreeParams[0].agreeParamSetParams, privateKey2, peerPublicKey, agreeKey2);
186     ASSERT_TRUE(ret == g_testAgreeParams[0].expectResult);
187     EXPECT_EQ(agreeKey->size, agreeKey2->size);
188     EXPECT_EQ(memcmp(agreeKey->data, agreeKey2->data, agreeKey->size), 0);
189 
190     /* 3. delete key */
191     if (!(g_testAgreeParams[0].genKeyParamSetParams.setKeyStorageFlag && \
192         (g_testAgreeParams[0].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) && \
193         ((g_testAgreeParams[0].keyAlias1Params.blobExist) && (g_testAgreeParams[0].keyAlias2Params.blobExist))) {
194         ASSERT_TRUE(HksDeleteKey(privateKey, NULL) == 0);
195         ASSERT_TRUE(HksDeleteKey(peerPubKeyAlias, NULL) == 0);
196     }
197     TestFreeBlob(&privateKey);
198     TestFreeBlob(&peerPubKeyAlias);
199     TestFreeBlob(&peerPublicKey);
200     TestFreeBlob(&agreeKey);
201 }
202 
203 
204 #endif /* _CUT_AUTHENTICATE_ */
205 }