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.h"
17 #include "huks_signverify_ecc_test_common.h"
18
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace Unittest::EccSifnVerify {
23 class HuksSignVerifyECCTest : 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 HuksSignVerifyECCTest::SetUpTestCase(void)
35 {
36 }
37
TearDownTestCase(void)38 void HuksSignVerifyECCTest::TearDownTestCase(void)
39 {
40 }
41
SetUp()42 void HuksSignVerifyECCTest::SetUp()
43 {
44 }
45
TearDown()46 void HuksSignVerifyECCTest::TearDown()
47 {
48 }
49
50 static struct OH_Huks_Param g_genParamsTest001[] = {
51 {
52 .tag = OH_HUKS_TAG_ALGORITHM,
53 .uint32Param = OH_HUKS_ALG_ECC
54 }, {
55 .tag = OH_HUKS_TAG_PURPOSE,
56 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY
57 }, {
58 .tag = OH_HUKS_TAG_KEY_SIZE,
59 .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
60 }, {
61 .tag = OH_HUKS_TAG_DIGEST,
62 .uint32Param = OH_HUKS_DIGEST_SHA256
63 }
64 };
65 static struct OH_Huks_Param g_signParamsTest001[] = {
66 {
67 .tag = OH_HUKS_TAG_ALGORITHM,
68 .uint32Param = OH_HUKS_ALG_ECC
69 }, {
70 .tag = OH_HUKS_TAG_PURPOSE,
71 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN
72 }, {
73 .tag = OH_HUKS_TAG_KEY_SIZE,
74 .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
75 }, {
76 .tag = OH_HUKS_TAG_DIGEST,
77 .uint32Param = OH_HUKS_DIGEST_SHA256
78 }
79 };
80 static struct OH_Huks_Param g_verifyParamsTest001[] = {
81 {
82 .tag = OH_HUKS_TAG_ALGORITHM,
83 .uint32Param = OH_HUKS_ALG_ECC
84 }, {
85 .tag = OH_HUKS_TAG_PURPOSE,
86 .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY
87 }, {
88 .tag = OH_HUKS_TAG_KEY_SIZE,
89 .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
90 }, {
91 .tag = OH_HUKS_TAG_DIGEST,
92 .uint32Param = OH_HUKS_DIGEST_SHA256
93 }
94 };
95
96 /**
97 * @tc.name: HuksSignVerifyECCTest.Security_HUKS_NAPI_SignVerify_ECC_0100
98 * @tc.desc: alg-ECC pur-Sign.
99 * @tc.type: FUNC
100 */
101 HWTEST_F(HuksSignVerifyECCTest, Security_HUKS_NAPI_SignVerify_ECC_0100, TestSize.Level0)
102 {
103 const char *keyAliasString = "HksECCSignVerifyKeyAliasTest001";
104 struct OH_Huks_ParamSet *genParamSet = nullptr;
105 struct OH_Huks_ParamSet *signParamSet = nullptr;
106 struct OH_Huks_ParamSet *verifyParamSet = nullptr;
107 struct OH_Huks_Blob keyAlias = { strlen(keyAliasString), (uint8_t *)keyAliasString };
108
109 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParamsTest001, sizeof(g_genParamsTest001) / sizeof(OH_Huks_Param));
110 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
111 ret = InitParamSet(&signParamSet, g_signParamsTest001, sizeof(g_signParamsTest001) / sizeof(OH_Huks_Param));
112 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
113 ret = InitParamSet(&verifyParamSet, g_verifyParamsTest001, sizeof(g_verifyParamsTest001) / sizeof(OH_Huks_Param));
114 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
115 if ((genParamSet != nullptr) || (signParamSet != nullptr) || (verifyParamSet != nullptr)) {
116 ret = HksEccSignVerifyTestNormalCase(keyAlias, genParamSet, signParamSet, verifyParamSet);
117 }
118
119 /* Delete Key */
120 ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
121 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
122
123 OH_Huks_FreeParamSet(&genParamSet);
124 OH_Huks_FreeParamSet(&signParamSet);
125 OH_Huks_FreeParamSet(&verifyParamSet);
126 }
127 } // namespace Unittest::EccSifnVerify