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