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_signverify_sm2_test.h"
17
18 #include <gtest/gtest.h>
19
20 using namespace testing::ext;
21 namespace Unittest::Sm2SignVerify {
22 class HuksSignVerifySM2Test : 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 HuksSignVerifySM2Test::SetUpTestCase(void)
34 {
35 }
36
TearDownTestCase(void)37 void HuksSignVerifySM2Test::TearDownTestCase(void)
38 {
39 }
40
SetUp()41 void HuksSignVerifySM2Test::SetUp()
42 {
43
44 }
45
TearDown()46 void HuksSignVerifySM2Test::TearDown()
47 {
48 }
49 const uint32_t SUCCESS_RETURN_INDEX = 0;
50
51 static const struct GenerateKeyCaseParam g_genParamsTest[] = {
52 { 0,
53 OH_HUKS_SUCCESS,
54 {
55 {
56 .tag = OH_HUKS_TAG_ALGORITHM,
57 .uint32Param = OH_HUKS_ALG_SM2
58 }, {
59 .tag = OH_HUKS_TAG_KEY_SIZE,
60 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
61 }, {
62 .tag = OH_HUKS_TAG_PURPOSE,
63 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY
64 }, {
65 .tag = OH_HUKS_TAG_DIGEST,
66 .uint32Param = OH_HUKS_DIGEST_SM3
67 },
68 },
69 },
70
71 { 1,
72 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
73 {
74 {
75 .tag = OH_HUKS_TAG_ALGORITHM,
76 .uint32Param = OH_HUKS_ALG_SM2
77 }, {
78 .tag = OH_HUKS_TAG_KEY_SIZE,
79 .uint32Param = OH_HUKS_AES_KEY_SIZE_512
80 }, {
81 .tag = OH_HUKS_TAG_PURPOSE,
82 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY
83 }, {
84 .tag = OH_HUKS_TAG_DIGEST,
85 .uint32Param = OH_HUKS_DIGEST_SM3
86 },
87 },
88 },
89
90 { 2,
91 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
92 {
93 {
94 .tag = OH_HUKS_TAG_ALGORITHM,
95 .uint32Param = OH_HUKS_ALG_SM2
96 }, {
97 .tag = OH_HUKS_TAG_KEY_SIZE,
98 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
99 }, {
100 .tag = OH_HUKS_TAG_PURPOSE,
101 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY
102 }, {
103 .tag = OH_HUKS_TAG_DIGEST,
104 .uint32Param = OH_HUKS_DIGEST_SHA1
105 },
106 },
107 },
108
109 { 3,
110 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
111 {
112 {
113 .tag = OH_HUKS_TAG_ALGORITHM,
114 .uint32Param = OH_HUKS_ALG_SM2
115 }, {
116 .tag = OH_HUKS_TAG_KEY_SIZE,
117 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
118 }, {
119 .tag = OH_HUKS_TAG_PURPOSE,
120 .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT
121 }, {
122 .tag = OH_HUKS_TAG_DIGEST,
123 .uint32Param = OH_HUKS_DIGEST_SM3
124 },
125 },
126 },
127 };
128 static const struct GenerateKeyCaseParam g_signParamsTest[] = {
129 { 0,
130 OH_HUKS_SUCCESS,
131 {
132 {
133 .tag = OH_HUKS_TAG_ALGORITHM,
134 .uint32Param = OH_HUKS_ALG_SM2
135 }, {
136 .tag = OH_HUKS_TAG_KEY_SIZE,
137 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
138 }, {
139 .tag = OH_HUKS_TAG_PURPOSE,
140 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN
141 }, {
142 .tag = OH_HUKS_TAG_DIGEST,
143 .uint32Param = OH_HUKS_DIGEST_SM3
144 },
145 },
146 },
147
148 { 1,
149 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
150 {
151 {
152 .tag = OH_HUKS_TAG_ALGORITHM,
153 .uint32Param = OH_HUKS_ALG_SM2
154 }, {
155 .tag = OH_HUKS_TAG_KEY_SIZE,
156 .uint32Param = OH_HUKS_AES_KEY_SIZE_512
157 }, {
158 .tag = OH_HUKS_TAG_PURPOSE,
159 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN
160 }, {
161 .tag = OH_HUKS_TAG_DIGEST,
162 .uint32Param = OH_HUKS_DIGEST_SM3
163 },
164 },
165 },
166
167 { 2,
168 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
169 {
170 {
171 .tag = OH_HUKS_TAG_ALGORITHM,
172 .uint32Param = OH_HUKS_ALG_SM2
173 }, {
174 .tag = OH_HUKS_TAG_KEY_SIZE,
175 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
176 }, {
177 .tag = OH_HUKS_TAG_PURPOSE,
178 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN
179 }, {
180 .tag = OH_HUKS_TAG_DIGEST,
181 .uint32Param = OH_HUKS_DIGEST_SHA1
182 },
183 },
184 },
185
186 { 3,
187 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
188 {
189 {
190 .tag = OH_HUKS_TAG_ALGORITHM,
191 .uint32Param = OH_HUKS_ALG_SM2
192 }, {
193 .tag = OH_HUKS_TAG_KEY_SIZE,
194 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
195 }, {
196 .tag = OH_HUKS_TAG_PURPOSE,
197 .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT
198 }, {
199 .tag = OH_HUKS_TAG_DIGEST,
200 .uint32Param = OH_HUKS_DIGEST_SM3
201 },
202 },
203 },
204 };
205 static const struct GenerateKeyCaseParam g_verifyParamsTest[] = {
206 { 0,
207 OH_HUKS_SUCCESS,
208 {
209 {
210 .tag = OH_HUKS_TAG_ALGORITHM,
211 .uint32Param = OH_HUKS_ALG_SM2
212 }, {
213 .tag = OH_HUKS_TAG_KEY_SIZE,
214 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
215 }, {
216 .tag = OH_HUKS_TAG_PURPOSE,
217 .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY
218 }, {
219 .tag = OH_HUKS_TAG_DIGEST,
220 .uint32Param = OH_HUKS_DIGEST_SM3
221 },
222 },
223 },
224
225 { 1,
226 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
227 {
228 {
229 .tag = OH_HUKS_TAG_ALGORITHM,
230 .uint32Param = OH_HUKS_ALG_SM2
231 }, {
232 .tag = OH_HUKS_TAG_KEY_SIZE,
233 .uint32Param = OH_HUKS_AES_KEY_SIZE_512
234 }, {
235 .tag = OH_HUKS_TAG_PURPOSE,
236 .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY
237 }, {
238 .tag = OH_HUKS_TAG_DIGEST,
239 .uint32Param = OH_HUKS_DIGEST_SM3
240 },
241 },
242 },
243
244 { 2,
245 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
246 {
247 {
248 .tag = OH_HUKS_TAG_ALGORITHM,
249 .uint32Param = OH_HUKS_ALG_SM2
250 }, {
251 .tag = OH_HUKS_TAG_KEY_SIZE,
252 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
253 }, {
254 .tag = OH_HUKS_TAG_PURPOSE,
255 .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY
256 }, {
257 .tag = OH_HUKS_TAG_DIGEST,
258 .uint32Param = OH_HUKS_DIGEST_SHA1
259 },
260 },
261 },
262
263 { 3,
264 OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT,
265 {
266 {
267 .tag = OH_HUKS_TAG_ALGORITHM,
268 .uint32Param = OH_HUKS_ALG_SM2
269 }, {
270 .tag = OH_HUKS_TAG_KEY_SIZE,
271 .uint32Param = OH_HUKS_SM2_KEY_SIZE_256
272 }, {
273 .tag = OH_HUKS_TAG_PURPOSE,
274 .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT
275 }, {
276 .tag = OH_HUKS_TAG_DIGEST,
277 .uint32Param = OH_HUKS_DIGEST_SM3
278 },
279 },
280 },
281 };
282
HksTestSignVerify(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)283 static OH_Huks_Result HksTestSignVerify(const struct OH_Huks_Blob *keyAlias, const struct OH_Huks_ParamSet *paramSet,
284 const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
285 {
286 uint8_t tmpHandle[sizeof(uint64_t)] = {0};
287 struct OH_Huks_Blob handle = { sizeof(uint64_t), tmpHandle };
288 OH_Huks_Result ret;
289
290 do {
291 ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
292 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
293 break;
294 }
295
296 struct OH_Huks_Param *tmpParam = NULL;
297 ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_PURPOSE, &tmpParam);
298 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
299 break;
300 }
301
302 ret = TestUpdateFinish(&handle, paramSet, tmpParam->uint32Param, inData, outData);
303 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
304 break;
305 }
306 ret.errorCode = (int32_t)OH_HUKS_SUCCESS;
307 } while (0);
308
309 (void)OH_Huks_AbortSession(&handle, paramSet);
310 return ret;
311 }
312
CreateImportKeyAlias(struct OH_Huks_Blob * importKeyAlias,const struct OH_Huks_Blob * keyAlias)313 static OH_Huks_Result CreateImportKeyAlias(struct OH_Huks_Blob *importKeyAlias, const struct OH_Huks_Blob *keyAlias)
314 {
315 OH_Huks_Result ret;
316 ret.errorCode=OH_HUKS_SUCCESS;
317 const char *tmp = "new";
318 importKeyAlias->size = keyAlias->size + strlen(tmp);
319 importKeyAlias->data = (uint8_t *)HksMalloc(importKeyAlias->size);
320 if (importKeyAlias->data == nullptr) {
321 ret.errorCode=OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
322 return ret;
323 }
324
325 if (memcpy_s(importKeyAlias->data, importKeyAlias->size, keyAlias->data, keyAlias->size) != EOK) {
326 HksFree(importKeyAlias->data);
327 ret.errorCode=OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
328 return ret;
329 }
330
331 if (memcpy_s(importKeyAlias->data + keyAlias->size,
332 importKeyAlias->size - keyAlias->size, tmp, strlen(tmp)) != EOK) {
333 HksFree(importKeyAlias->data);
334 ret.errorCode=OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
335 return ret;
336 }
337 return ret;
338 }
339
FreeBuffAndDeleteKey(struct OH_Huks_ParamSet ** paramSet1,struct OH_Huks_ParamSet ** paramSet2,struct OH_Huks_ParamSet ** paramSet3,const struct OH_Huks_Blob * keyAlias1,const struct OH_Huks_Blob * keyAlias2)340 void static FreeBuffAndDeleteKey(struct OH_Huks_ParamSet **paramSet1, struct OH_Huks_ParamSet **paramSet2,
341 struct OH_Huks_ParamSet **paramSet3, const struct OH_Huks_Blob *keyAlias1, const struct OH_Huks_Blob *keyAlias2)
342 {
343 (void)OH_Huks_DeleteKeyItem(keyAlias1, *paramSet1);
344 (void)OH_Huks_DeleteKeyItem(keyAlias2, *paramSet1);
345 OH_Huks_FreeParamSet(paramSet1);
346 OH_Huks_FreeParamSet(paramSet2);
347 OH_Huks_FreeParamSet(paramSet3);
348 }
349
HksSm2SignVerifyTestRun(const struct OH_Huks_Blob * keyAlias,const uint32_t genIndex,const uint32_t signIndex,const uint32_t verifyIndex,const bool isTestSignData)350 static OH_Huks_Result HksSm2SignVerifyTestRun(const struct OH_Huks_Blob *keyAlias, const uint32_t genIndex,
351 const uint32_t signIndex, const uint32_t verifyIndex, const bool isTestSignData)
352 {
353 struct OH_Huks_ParamSet *genParamSet = nullptr;
354 struct OH_Huks_ParamSet *signParamSet = nullptr;
355 struct OH_Huks_ParamSet *verifyParamSet = nullptr;
356
357 struct OH_Huks_Blob inData = { g_inData.length(), (uint8_t *)g_inData.c_str() };
358 uint8_t outDataS[SM2_COMMON_SIZE] = {0};
359 struct OH_Huks_Blob outDataSign = { SM2_COMMON_SIZE, outDataS };
360
361 uint8_t pubKey[OH_HUKS_MAX_KEY_SIZE] = {0};
362 struct OH_Huks_Blob publicKey = { OH_HUKS_MAX_KEY_SIZE, pubKey };
363
364 struct OH_Huks_Blob importKeyAlias;
365 OH_Huks_Result ret = CreateImportKeyAlias(&importKeyAlias, keyAlias);
366 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "createImportKeyAlias failed.";
367
368 do {
369 /* 1. Generate Key */
370 ret = InitParamSet(&genParamSet, g_genParamsTest[genIndex].params,
371 sizeof(g_genParamsTest[genIndex].params) / sizeof(OH_Huks_Param));
372 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitGenParamSet failed.";
373 ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
374 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
375 ret.errorCode = ((ret.errorCode == g_genParamsTest[genIndex].result) ? (int32_t)OH_HUKS_SUCCESS : ret.errorCode);
376 break;
377 }
378
379 ret = OH_Huks_IsKeyItemExist(keyAlias, genParamSet);
380 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "isKeyExist failed.";
381
382 /* 2. Sign Three Stage */
383 ret = InitParamSet(&signParamSet, g_signParamsTest[signIndex].params,
384 sizeof(g_signParamsTest[signIndex].params) / sizeof(OH_Huks_Param));
385 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitSignParamSet failed.";
386 ret = HksTestSignVerify(keyAlias, signParamSet, &inData, &outDataSign);
387 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
388 ret.errorCode = ((ret.errorCode == g_signParamsTest[signIndex].result) ? (int32_t)OH_HUKS_SUCCESS : ret.errorCode);
389 break;
390 }
391
392 /* 3. Export Public Key */
393 ret = OH_Huks_ExportPublicKeyItem(keyAlias, genParamSet, &publicKey);
394 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey failed.";
395
396 /* 4. Import Key */
397 ret = InitParamSet(&verifyParamSet, g_verifyParamsTest[verifyIndex].params,
398 sizeof(g_verifyParamsTest[verifyIndex].params) / sizeof(OH_Huks_Param));
399 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitVerifyParamSet failed.";
400 ret = OH_Huks_ImportKeyItem(&importKeyAlias, verifyParamSet, &publicKey);
401 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
402 ret.errorCode = ((ret.errorCode == g_verifyParamsTest[verifyIndex].result) ? (int32_t)OH_HUKS_SUCCESS : ret.errorCode);
403 break;
404 }
405
406 /* 5. Verify Three Stage */
407 if (isTestSignData) {
408 (void)memset_s(outDataSign.data, outDataSign.size, 0, outDataSign.size);
409 }
410 ret = HksTestSignVerify(&importKeyAlias, verifyParamSet, &inData, &outDataSign);
411 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
412 ret.errorCode = ((ret.errorCode == g_verifyParamsTest[verifyIndex].result) ? (int32_t)OH_HUKS_SUCCESS : ret.errorCode);
413 }
414 } while (0);
415
416 FreeBuffAndDeleteKey(&genParamSet, &signParamSet, &verifyParamSet, keyAlias, &importKeyAlias);
417 HksFree(importKeyAlias.data);
418 return ret;
419 }
420
421 /**
422 * @tc.name: HuksSignVerifySM2Test.Security_HUKS_NAPI_SignVerify_SM2_0100
423 * @tc.desc: normal parameter test case : alg-SM2, pur-Sign/Verify, keySize-256 and dig-SM3.
424 * @tc.type: FUNC
425 */
426 HWTEST_F(HuksSignVerifySM2Test, Security_HUKS_NAPI_SignVerify_SM2_0100, TestSize.Level0)
427 {
428 const char *keyAliasString = "HksSM2SignVerifyKeyAliasTest001";
429 struct OH_Huks_Blob keyAlias = { strlen(keyAliasString), (uint8_t *)keyAliasString };
430 OH_Huks_Result ret = HksSm2SignVerifyTestRun(&keyAlias, SUCCESS_RETURN_INDEX,
431 SUCCESS_RETURN_INDEX, SUCCESS_RETURN_INDEX, false);
432 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "sm2SignVerifyTest001 failed.";
433 }
434 }
435