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_ed25519_test.h"
17 #include <gtest/gtest.h>
18
19 using namespace testing::ext;
20 namespace Unittest::Ed25519 {
21 class HuksSignVerifyED25519Test : public testing::Test {
22 public:
23 static void SetUpTestCase(void);
24
25 static void TearDownTestCase(void);
26
27 void SetUp();
28
29 void TearDown();
30 };
31
SetUpTestCase(void)32 void HuksSignVerifyED25519Test::SetUpTestCase(void)
33 {
34 }
35
TearDownTestCase(void)36 void HuksSignVerifyED25519Test::TearDownTestCase(void)
37 {
38 }
39
SetUp()40 void HuksSignVerifyED25519Test::SetUp()
41 {
42
43 }
44
TearDown()45 void HuksSignVerifyED25519Test::TearDown()
46 {
47 }
48
HksTestFreeParamSet(struct OH_Huks_ParamSet * paramSet1,struct OH_Huks_ParamSet * paramSet2,struct OH_Huks_ParamSet * paramSet3)49 void HksTestFreeParamSet(struct OH_Huks_ParamSet *paramSet1, struct OH_Huks_ParamSet *paramSet2, struct OH_Huks_ParamSet *paramSet3)
50 {
51 OH_Huks_FreeParamSet(¶mSet1);
52 OH_Huks_FreeParamSet(¶mSet2);
53 OH_Huks_FreeParamSet(¶mSet3);
54 }
55
HksTestSignVerify(struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData,bool isSign)56 OH_Huks_Result HksTestSignVerify(struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData,
57 struct OH_Huks_Blob *outData, bool isSign)
58 {
59 uint8_t tmpHandle[sizeof(uint64_t)] = {0};
60 struct OH_Huks_Blob handle = { sizeof(uint64_t), tmpHandle };
61 OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
62 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
63 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
64 return ret;
65 }
66
67 struct OH_Huks_Param *tmpParam = NULL;
68 ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_PURPOSE, &tmpParam);
69 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
70 return ret;
71 }
72
73 ret = TestUpdateFinish(&handle, paramSet, tmpParam->uint32Param, inData, outData);
74 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
75 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
76 return ret;
77 }
78
79 return ret;
80 }
81
82 /**
83 * @tc.name: HuksSignVerifyED25519Test.Security_HUKS_NAPI_SignVerify_ED25519_0100
84 * @tc.desc: alg-ED25519 pur-Sign.
85 * @tc.type: FUNC
86 */
87 HWTEST_F(HuksSignVerifyED25519Test, Security_HUKS_NAPI_SignVerify_ED25519_0100, TestSize.Level0)
88 {
89 const char *keyAliasString = "HksED25519SignVerifyKeyAliasTest001";
90 struct OH_Huks_ParamSet *genParamSet = nullptr;
91 struct OH_Huks_ParamSet *signParamSet = nullptr;
92 struct OH_Huks_ParamSet *verifyParamSet = nullptr;
93
94 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParamsTest001,
95 sizeof(g_genParamsTest001) / sizeof(OH_Huks_Param));
96 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
97 ret = InitParamSet(&signParamSet, g_signParamsTest001,
98 sizeof(g_signParamsTest001) / sizeof(OH_Huks_Param));
99 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
100 ret = InitParamSet(&verifyParamSet, g_verifyParamsTest001,
101 sizeof(g_verifyParamsTest001) / sizeof(OH_Huks_Param));
102 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
103
104 struct OH_Huks_Blob keyAlias = { strlen(keyAliasString), (uint8_t *)keyAliasString };
105
106 /* 1. Generate Key */
107 ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
108 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
109
110 /* 2. Sign Three Stage */
111 uint8_t outDataS[ED25519_COMMON_SIZE] = {0};
112 struct OH_Huks_Blob outDataSign = { ED25519_COMMON_SIZE, outDataS };
113 ret = HksTestSignVerify(&keyAlias, signParamSet, &g_inData, &outDataSign, true);
114 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Sign failed.";
115
116 /* 3. Export Public Key */
117 uint8_t pubKey[OH_HUKS_CURVE25519_KEY_SIZE_256] = {0};
118 struct OH_Huks_Blob publicKey = { OH_HUKS_CURVE25519_KEY_SIZE_256, pubKey };
119 ret = OH_Huks_ExportPublicKeyItem(&keyAlias, genParamSet, &publicKey);
120 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey failed.";
121
122 /* 4. Import Key */
123 char newKey[] = "ECC_Sign_Verify_Import_KeyAlias";
124 struct OH_Huks_Blob newKeyAlias = { .size = strlen(newKey), .data = (uint8_t *)newKey };
125 ret = OH_Huks_ImportKeyItem(&newKeyAlias, verifyParamSet, &publicKey);
126 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ImportKey failed";
127
128 /* 5. Verify Three Stage */
129 ret = HksTestSignVerify(&newKeyAlias, verifyParamSet, &g_inData, &outDataSign, false);
130 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Verify failed.";
131
132 /* 5. Delete Key */
133 EXPECT_EQ(OH_Huks_DeleteKeyItem(&keyAlias, genParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
134 EXPECT_EQ(OH_Huks_DeleteKeyItem(&newKeyAlias, verifyParamSet).errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed.";
135
136 HksTestFreeParamSet(genParamSet, signParamSet, verifyParamSet);
137 }
138 } // namespace Unittest::Ed25519