• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "huks_agree_dh_test.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 namespace Unittest::DhAgree {
22 class HuksAgreeDHTest : public testing::Test {
23 public:
24     static void SetUpTestCase(void);
25 
26     static void TearDownTestCase(void);
27 
28     void SetUp();
29 
30     void TearDown();
31 };
32 
SetUpTestCase(void)33 void HuksAgreeDHTest::SetUpTestCase(void)
34 {
35 }
36 
TearDownTestCase(void)37 void HuksAgreeDHTest::TearDownTestCase(void)
38 {
39 }
40 
SetUp()41 void HuksAgreeDHTest::SetUp()
42 {
43 }
44 
TearDown()45 void HuksAgreeDHTest::TearDown()
46 {
47 }
48 
HksDhAgreeFinish(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_Blob * publicKey,const struct OH_Huks_ParamSet * initParamSet,const struct OH_Huks_ParamSet * finishParamSet,struct OH_Huks_Blob * outData)49 OH_Huks_Result HksDhAgreeFinish(const struct OH_Huks_Blob *keyAlias, const struct OH_Huks_Blob *publicKey,
50                          const struct OH_Huks_ParamSet *initParamSet, const struct OH_Huks_ParamSet *finishParamSet,
51                          struct OH_Huks_Blob *outData)
52 {
53     struct OH_Huks_Blob inData = { g_inData.length(), (uint8_t *)g_inData.c_str() };
54 
55     uint8_t handleU[sizeof(uint64_t)] = {0};
56     struct OH_Huks_Blob handle = { sizeof(uint64_t), handleU };
57     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, initParamSet, &handle, nullptr);
58     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
59     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
60         return ret;
61     }
62 
63     uint8_t outDataU[DH_COMMON_SIZE] = {0};
64     struct OH_Huks_Blob outDataUpdate = { DH_COMMON_SIZE, outDataU };
65     ret = OH_Huks_UpdateSession(&handle, initParamSet, publicKey, &outDataUpdate);
66     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Update failed.";
67     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
68         return ret;
69     }
70 
71     ret = OH_Huks_FinishSession(&handle, finishParamSet, &inData, outData);
72     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Finish failed.";
73     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
74         return ret;
75     }
76     return ret;
77 }
78 
HksDhAgreeAbort(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_Blob * publicKey,const struct OH_Huks_ParamSet * initParamSet)79 OH_Huks_Result HksDhAgreeAbort(const struct OH_Huks_Blob *keyAlias, const struct OH_Huks_Blob *publicKey,
80                         const struct OH_Huks_ParamSet *initParamSet)
81 {
82     uint8_t handleU[sizeof(uint64_t)] = {0};
83     struct OH_Huks_Blob handle = { sizeof(uint64_t), handleU };
84     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, initParamSet, &handle, nullptr);
85     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
86     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
87         return ret;
88     }
89 
90     uint8_t outDataU[DH_COMMON_SIZE] = {0};
91     struct OH_Huks_Blob outDataUpdate = { DH_COMMON_SIZE, outDataU };
92     ret = OH_Huks_UpdateSession(&handle, initParamSet, publicKey, &outDataUpdate);
93     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Update failed.";
94     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
95         return ret;
96     }
97 
98     ret = OH_Huks_AbortSession(&handle, initParamSet);
99     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Finish failed.";
100     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
101         return ret;
102     }
103     return ret;
104 }
105 
HksDhAgreeExport(const struct OH_Huks_Blob * keyAlias1,const struct OH_Huks_Blob * keyAlias2,struct OH_Huks_Blob * publicKey1,struct OH_Huks_Blob * publicKey2,const struct OH_Huks_ParamSet * genParamSet)106 OH_Huks_Result HksDhAgreeExport(const struct OH_Huks_Blob *keyAlias1, const struct OH_Huks_Blob *keyAlias2,
107                          struct OH_Huks_Blob *publicKey1, struct OH_Huks_Blob *publicKey2,
108                          const struct OH_Huks_ParamSet *genParamSet)
109 {
110     OH_Huks_Result ret = OH_Huks_ExportPublicKeyItem(keyAlias1, genParamSet, publicKey1);
111     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey01 failed.";
112     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
113         return ret;
114     }
115     ret = OH_Huks_ExportPublicKeyItem(keyAlias2, genParamSet, publicKey2);
116     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey02 failed.";
117     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
118         return ret;
119     }
120     return ret;
121 }
122 
HksDhAgreeFreeParamSet(struct OH_Huks_ParamSet * paramSet1,struct OH_Huks_ParamSet * paramSet2,struct OH_Huks_ParamSet * paramSet3)123 void HksDhAgreeFreeParamSet(struct OH_Huks_ParamSet *paramSet1, struct OH_Huks_ParamSet *paramSet2,
124                             struct OH_Huks_ParamSet *paramSet3)
125 {
126     OH_Huks_FreeParamSet(&paramSet1);
127     OH_Huks_FreeParamSet(&paramSet2);
128     OH_Huks_FreeParamSet(&paramSet3);
129 }
130 
HksDhAgreeFreeParamSet(struct OH_Huks_ParamSet * paramSet1,struct OH_Huks_ParamSet * paramSet2,struct OH_Huks_ParamSet * paramSet3,struct OH_Huks_ParamSet * paramSet4,struct OH_Huks_ParamSet * paramSet5)131 void HksDhAgreeFreeParamSet(struct OH_Huks_ParamSet *paramSet1, struct OH_Huks_ParamSet *paramSet2,
132                             struct OH_Huks_ParamSet *paramSet3, struct OH_Huks_ParamSet *paramSet4,
133                             struct OH_Huks_ParamSet *paramSet5)
134 {
135     OH_Huks_FreeParamSet(&paramSet1);
136     OH_Huks_FreeParamSet(&paramSet2);
137     OH_Huks_FreeParamSet(&paramSet3);
138     OH_Huks_FreeParamSet(&paramSet4);
139     OH_Huks_FreeParamSet(&paramSet5);
140 }
141 
HksDhAgreeFreeBlob(struct OH_Huks_Blob * blob1,struct OH_Huks_Blob * blob2)142 void HksDhAgreeFreeBlob(struct OH_Huks_Blob *blob1, struct OH_Huks_Blob *blob2)
143 {
144     HksFree(blob1->data);
145     HksFree(blob2->data);
146 }
147 
HksDhAgreeFreeBlob(struct OH_Huks_Blob * blob1,struct OH_Huks_Blob * blob2,struct OH_Huks_Blob * blob3,struct OH_Huks_Blob * blob4)148 void HksDhAgreeFreeBlob(struct OH_Huks_Blob *blob1, struct OH_Huks_Blob *blob2, struct OH_Huks_Blob *blob3, struct OH_Huks_Blob *blob4)
149 {
150     HksFree(blob1->data);
151     HksFree(blob2->data);
152     HksFree(blob3->data);
153     HksFree(blob4->data);
154 }
155 
156 /**
157  * @tc.name: HuksAgreeDHTest.Security_HUKS_NAPI_Agree_DH_0100
158  * @tc.desc: alg-DH, pur-AGREE
159  * @tc.type: FUNC
160  */
161 HWTEST_F(HuksAgreeDHTest, Security_HUKS_NAPI_Agree_DH_0100, TestSize.Level0)
162 {
163     struct OH_Huks_ParamSet *genParamSet = nullptr;
164     struct OH_Huks_ParamSet *initParamSet01 = nullptr;
165     struct OH_Huks_ParamSet *finishParamSet01 = nullptr;
166     struct OH_Huks_ParamSet *initParamSet02 = nullptr;
167     struct OH_Huks_ParamSet *finishParamSet02 = nullptr;
168     OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
169     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen) failed.";
170     ret = InitParamSet(&initParamSet01, g_agreeParams01Init001, sizeof(g_agreeParams01Init001) / sizeof(OH_Huks_Param));
171     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(init & update)01 failed.";
172     ret = InitParamSet(&finishParamSet01, g_agreeParams01Finish001,
173         sizeof(g_agreeParams01Finish001) / sizeof(OH_Huks_Param));
174     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(finish)01 failed.";
175     ret = InitParamSet(&initParamSet02, g_agreeParams02Init001, sizeof(g_agreeParams02Init001) / sizeof(OH_Huks_Param));
176     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(init & update)02 failed.";
177     ret = InitParamSet(&finishParamSet02, g_agreeParams02Finish001,
178         sizeof(g_agreeParams02Finish001) / sizeof(OH_Huks_Param));
179     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(finish)02 failed.";
180 
181     OH_Huks_Result ret1 = OH_Huks_GenerateKeyItem(&g_keyAlias01001, genParamSet, nullptr);
182     EXPECT_EQ(ret1.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey01 failed.";
183     ret1 = OH_Huks_GenerateKeyItem(&g_keyAlias02001, genParamSet, nullptr);
184     EXPECT_EQ(ret1.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey02 failed.";
185 
186     struct OH_Huks_Blob publicKey01 = { .size = OH_HUKS_DH_KEY_SIZE_4096, .data = nullptr };
187     struct OH_Huks_Blob publicKey02 = { .size = OH_HUKS_DH_KEY_SIZE_4096, .data = nullptr };
188     ret = MallocAndCheckBlobData(&publicKey01, publicKey01.size);
189     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc publicKey01 failed.";
190     ret = MallocAndCheckBlobData(&publicKey02, publicKey02.size);
191     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc publicKey02 failed.";
192     ret = HksDhAgreeExport(&g_keyAlias01001, &g_keyAlias02001, &publicKey01, &publicKey02, genParamSet);
193     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportKey failed.";
194 
195     struct OH_Huks_Blob outData01 = { .size = DH_COMMON_SIZE, .data = nullptr };
196     struct OH_Huks_Blob outData02 = { .size = DH_COMMON_SIZE, .data = nullptr };
197     ret = MallocAndCheckBlobData(&outData01, outData01.size);
198     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc outData01 failed.";
199     ret = MallocAndCheckBlobData(&outData02, outData02.size);
200     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc outData02 failed.";
201     ret = HksDhAgreeFinish(&g_keyAlias01001, &publicKey02, initParamSet01, finishParamSet01, &outData01);
202     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksDhAgreeFinish01 failed.";
203     ret = HksDhAgreeFinish(&g_keyAlias02001, &publicKey01, initParamSet02, finishParamSet02, &outData02);
204     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksDhAgreeFinish02 failed.";
205 
206     OH_Huks_DeleteKeyItem(&g_keyAlias01001, genParamSet);
207     OH_Huks_DeleteKeyItem(&g_keyAlias02001, genParamSet);
208     OH_Huks_DeleteKeyItem(&g_keyAliasFinal1001, NULL);
209     OH_Huks_DeleteKeyItem(&g_keyAliasFinal2001, NULL);
210     HksDhAgreeFreeParamSet(genParamSet, initParamSet01, finishParamSet01, initParamSet02, finishParamSet02);
211     HksDhAgreeFreeBlob(&publicKey01, &publicKey02, &outData01, &outData02);
212 }
213 } // namespace Unittest::DhAgree