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_ecdh_test.h"
17 #include "huks_agree_ecdh_test_common.h"
18
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace Unittest::EcdhAgree {
23 class HuksAgreeECDHTest : public testing::Test {
24 public:
25 static void SetUpTestCase(void);
26
27 static void TearDownTestCase(void);
28
29 void SetUp();
30
31 void TearDown();
32 };
33
SetUpTestCase(void)34 void HuksAgreeECDHTest::SetUpTestCase(void)
35 {
36 }
37
TearDownTestCase(void)38 void HuksAgreeECDHTest::TearDownTestCase(void)
39 {
40 }
41
SetUp()42 void HuksAgreeECDHTest::SetUp()
43 {
44 }
45
TearDown()46 void HuksAgreeECDHTest::TearDown()
47 {
48 }
49
50 static struct OH_Huks_Blob g_keyAlias01001 = {
51 strlen("HksECDHAgreeKeyAliasTest001_1"),
52 (uint8_t *)"HksECDHAgreeKeyAliasTest001_1"
53 };
54 static struct OH_Huks_Blob g_keyAlias02001 = {
55 strlen("HksECDHAgreeKeyAliasTest001_2"),
56 (uint8_t *)"HksECDHAgreeKeyAliasTest001_2"
57 };
58 static struct OH_Huks_Blob g_keyAliasFinal1001 = {
59 strlen("HksECDHAgreeKeyAliasTest001_1_final"),
60 (uint8_t *)"HksECDHAgreeKeyAliasTest001_1_final"
61 };
62 static struct OH_Huks_Blob g_keyAliasFinal2001 = {
63 strlen("HksECDHAgreeKeyAliasTest001_2_final"),
64 (uint8_t *)"HksECDHAgreeKeyAliasTest001_2_final"
65 };
66 static struct OH_Huks_Param g_genParams001[] = {
67 {
68 .tag = OH_HUKS_TAG_ALGORITHM,
69 .uint32Param = OH_HUKS_ALG_ECC
70 }, {
71 .tag = OH_HUKS_TAG_PURPOSE,
72 .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE
73 }, {
74 .tag = OH_HUKS_TAG_KEY_SIZE,
75 .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
76 }, {
77 .tag = OH_HUKS_TAG_DIGEST,
78 .uint32Param = OH_HUKS_DIGEST_NONE
79 }, {
80 .tag = OH_HUKS_TAG_PADDING,
81 .uint32Param = OH_HUKS_PADDING_NONE
82 }, {
83 .tag = OH_HUKS_TAG_BLOCK_MODE,
84 .uint32Param = OH_HUKS_MODE_CBC
85 }
86 };
87 static struct OH_Huks_Param g_agreeParams01Init001[] = {
88 {
89 .tag = OH_HUKS_TAG_ALGORITHM,
90 .uint32Param = OH_HUKS_ALG_ECDH
91 }, {
92 .tag = OH_HUKS_TAG_PURPOSE,
93 .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE
94 }, {
95 .tag = OH_HUKS_TAG_KEY_SIZE,
96 .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
97 }
98 };
99 static struct OH_Huks_Param g_agreeParams01Finish001[] = {
100 {
101 .tag = OH_HUKS_TAG_KEY_STORAGE_FLAG,
102 .uint32Param = OH_HUKS_STORAGE_PERSISTENT
103 }, {
104 .tag = OH_HUKS_TAG_IS_KEY_ALIAS,
105 .boolParam = true
106 }, {
107 .tag = OH_HUKS_TAG_ALGORITHM,
108 .uint32Param = OH_HUKS_ALG_AES
109 }, {
110 .tag = OH_HUKS_TAG_KEY_SIZE,
111 .uint32Param = OH_HUKS_AES_KEY_SIZE_256
112 }, {
113 .tag = OH_HUKS_TAG_PURPOSE,
114 .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE
115 }, {
116 .tag = OH_HUKS_TAG_DIGEST,
117 .uint32Param = OH_HUKS_DIGEST_SHA256
118 }, {
119 .tag = OH_HUKS_TAG_KEY_ALIAS,
120 .blob = g_keyAliasFinal1001
121 }, {
122 .tag = OH_HUKS_TAG_PADDING,
123 .uint32Param = OH_HUKS_PADDING_NONE
124 }, {
125 .tag = OH_HUKS_TAG_BLOCK_MODE,
126 .uint32Param = OH_HUKS_MODE_CBC
127 }
128 };
129 static struct OH_Huks_Param g_agreeParams02Init001[] = {
130 {
131 .tag = OH_HUKS_TAG_ALGORITHM,
132 .uint32Param = OH_HUKS_ALG_ECDH
133 }, {
134 .tag = OH_HUKS_TAG_PURPOSE,
135 .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE
136 }, {
137 .tag = OH_HUKS_TAG_KEY_SIZE,
138 .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
139 }
140 };
141 static struct OH_Huks_Param g_agreeParams02Finish001[] = {
142 {
143 .tag = OH_HUKS_TAG_KEY_STORAGE_FLAG,
144 .uint32Param = OH_HUKS_STORAGE_PERSISTENT
145 }, {
146 .tag = OH_HUKS_TAG_IS_KEY_ALIAS,
147 .boolParam = true
148 }, {
149 .tag = OH_HUKS_TAG_ALGORITHM,
150 .uint32Param = OH_HUKS_ALG_AES
151 }, {
152 .tag = OH_HUKS_TAG_KEY_SIZE,
153 .uint32Param = OH_HUKS_AES_KEY_SIZE_256
154 }, {
155 .tag = OH_HUKS_TAG_PURPOSE,
156 .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE
157 }, {
158 .tag = OH_HUKS_TAG_DIGEST,
159 .uint32Param = OH_HUKS_DIGEST_SHA256
160 }, {
161 .tag = OH_HUKS_TAG_KEY_ALIAS,
162 .blob = g_keyAliasFinal2001
163 }, {
164 .tag = OH_HUKS_TAG_PADDING,
165 .uint32Param = OH_HUKS_PADDING_NONE
166 }, {
167 .tag = OH_HUKS_TAG_BLOCK_MODE,
168 .uint32Param = OH_HUKS_MODE_CBC
169 }
170 };
171
172 /**
173 * @tc.name: HuksAgreeECDHTest.Security_HUKS_NAPI_Agree_ECDH_0100
174 * @tc.desc: alg-ECDH, pur-AGREE
175 * @tc.type: FUNC
176 */
177 HWTEST_F(HuksAgreeECDHTest, Security_HUKS_NAPI_Agree_ECDH_0100, TestSize.Level0)
178 {
179 struct OH_Huks_ParamSet *genParamSet = nullptr;
180 struct OH_Huks_ParamSet *initParamSet01 = nullptr;
181 struct OH_Huks_ParamSet *finishParamSet01 = nullptr;
182 struct OH_Huks_ParamSet *initParamSet02 = nullptr;
183 struct OH_Huks_ParamSet *finishParamSet02 = nullptr;
184 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
185 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen) failed.";
186 ret = InitParamSet(&initParamSet01, g_agreeParams01Init001, sizeof(g_agreeParams01Init001) / sizeof(OH_Huks_Param));
187 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(init & update)01 failed.";
188 ret = InitParamSet(&finishParamSet01, g_agreeParams01Finish001,
189 sizeof(g_agreeParams01Finish001) / sizeof(OH_Huks_Param));
190 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(finish)01 failed.";
191 ret = InitParamSet(&initParamSet02, g_agreeParams02Init001, sizeof(g_agreeParams02Init001) / sizeof(OH_Huks_Param));
192 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(init & update)02 failed.";
193 ret = InitParamSet(&finishParamSet02, g_agreeParams02Finish001,
194 sizeof(g_agreeParams02Finish001) / sizeof(OH_Huks_Param));
195 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(finish)02 failed.";
196
197 OH_Huks_Result ret1 = OH_Huks_GenerateKeyItem(&g_keyAlias01001, genParamSet, nullptr);
198 EXPECT_EQ(ret1.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey01 failed.";
199 ret1 = OH_Huks_GenerateKeyItem(&g_keyAlias02001, genParamSet, nullptr);
200 EXPECT_EQ(ret1.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey02 failed.";
201
202 struct OH_Huks_Blob publicKey01 = { .size = OH_HUKS_ECC_KEY_SIZE_256, .data = nullptr };
203 struct OH_Huks_Blob publicKey02 = { .size = OH_HUKS_ECC_KEY_SIZE_256, .data = nullptr };
204 ret = MallocAndCheckBlobData(&publicKey01, publicKey01.size);
205 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc publicKey01 failed.";
206 ret = MallocAndCheckBlobData(&publicKey02, publicKey02.size);
207 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc publicKey02 failed.";
208 ret = HksEcdhAgreeExport(&g_keyAlias01001, &g_keyAlias02001, &publicKey01, &publicKey02, genParamSet);
209 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportKey failed.";
210
211 struct OH_Huks_Blob outData01 = { .size = ECDH_COMMON_SIZE, .data = nullptr };
212 struct OH_Huks_Blob outData02 = { .size = ECDH_COMMON_SIZE, .data = nullptr };
213 ret = MallocAndCheckBlobData(&outData01, outData01.size);
214 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc outData01 failed.";
215 ret = MallocAndCheckBlobData(&outData02, outData02.size);
216 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc outData02 failed.";
217 ret = HksEcdhAgreeFinish(&g_keyAlias01001, &publicKey02, initParamSet01, finishParamSet01, &outData01);
218 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksEcdhAgreeFinish01 failed.";
219 ret = HksEcdhAgreeFinish(&g_keyAlias02001, &publicKey01, initParamSet02, finishParamSet02, &outData02);
220 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksEcdhAgreeFinish02 failed.";
221
222 OH_Huks_DeleteKeyItem(&g_keyAlias01001, genParamSet);
223 OH_Huks_DeleteKeyItem(&g_keyAlias02001, genParamSet);
224 OH_Huks_DeleteKeyItem(&g_keyAliasFinal1001, NULL);
225 OH_Huks_DeleteKeyItem(&g_keyAliasFinal2001, NULL);
226 HksEcdhAgreeFreeParamSet(genParamSet, initParamSet01, finishParamSet01, initParamSet02, finishParamSet02);
227 HksEcdhAgreeFreeBlob(&publicKey01, &publicKey02, &outData01, &outData02);
228 }
229 } // namespace Unittest::EcdhAgree