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_ecc_test_common.h"
17
18 #include <gtest/gtest.h>
19
20 using namespace testing::ext;
21 namespace Unittest::EccSifnVerify {
HksTestSignVerify(struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData,bool isSign)22 OH_Huks_Result HksTestSignVerify(struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *paramSet,
23 const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData, bool isSign)
24 {
25 uint8_t tmpHandle[sizeof(uint64_t)] = {0};
26 struct OH_Huks_Blob handle = { sizeof(uint64_t), tmpHandle };
27 OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
28 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
29 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
30 return ret;
31 }
32
33 struct OH_Huks_Param *tmpParam = NULL;
34 ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_PURPOSE, &tmpParam);
35 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
36 return ret;
37 }
38
39 ret = TestUpdateFinish(&handle, paramSet, tmpParam->uint32Param, inData, outData);
40 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
41 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
42 return ret;
43 }
44 return ret;
45 }
46
HksEccSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * signParamSet,struct OH_Huks_ParamSet * verifyParamSet)47 OH_Huks_Result HksEccSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias,
48 struct OH_Huks_ParamSet *genParamSet, struct OH_Huks_ParamSet *signParamSet, struct OH_Huks_ParamSet *verifyParamSet)
49 {
50 struct OH_Huks_Blob inData = {
51 g_inData.length(),
52 (uint8_t *)g_inData.c_str()
53 };
54
55 /* 1. Generate Key */
56 // Generate Key
57 OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
58 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
59
60 /* 2. Sign Three Stage */
61 uint8_t outDataS[ECC_COMMON_SIZE] = {0};
62 struct OH_Huks_Blob outDataSign = { ECC_COMMON_SIZE, outDataS };
63 ret = HksTestSignVerify(&keyAlias, signParamSet, &inData, &outDataSign, true);
64 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Sign failed.";
65
66 /* 3. Export Public Key */
67 uint8_t pubKey[OH_HUKS_ECC_KEY_SIZE_521] = {0};
68 struct OH_Huks_Blob publicKey = { OH_HUKS_ECC_KEY_SIZE_521, pubKey };
69 ret = OH_Huks_ExportPublicKeyItem(&keyAlias, genParamSet, &publicKey);
70 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey failed.";
71
72 /* 4. Import Key */
73 char newKey[] = "ECC_Sign_Verify_Import_KeyAlias";
74 struct OH_Huks_Blob newKeyAlias = { .size = strlen(newKey), .data = (uint8_t *)newKey };
75 ret = OH_Huks_ImportKeyItem(&newKeyAlias, verifyParamSet, &publicKey);
76 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ImportKey failed";
77
78 /* 5. Verify Three Stage */
79 ret = HksTestSignVerify(&newKeyAlias, verifyParamSet, &inData, &outDataSign, false);
80 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Verify failed.";
81
82 /* 6. Delete New Key */
83 ret = OH_Huks_DeleteKeyItem(&newKeyAlias, verifyParamSet);
84 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed.";
85
86 return ret;
87 }
88 }