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_rsa_test.h"
17 #include "huks_signverify_rsa_test_common.h"
18
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace Unittest::RsaSignVerify {
23 class HuksSignVerifyRSATest : 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 HuksSignVerifyRSATest::SetUpTestCase(void)
35 {
36 }
37
TearDownTestCase(void)38 void HuksSignVerifyRSATest::TearDownTestCase(void)
39 {
40 }
41
SetUp()42 void HuksSignVerifyRSATest::SetUp()
43 {
44
45 }
46
TearDown()47 void HuksSignVerifyRSATest::TearDown()
48 {
49 }
50
51 static struct OH_Huks_Param g_genParamsTest071[] = {
52 {
53 .tag = OH_HUKS_TAG_ALGORITHM,
54 .uint32Param = OH_HUKS_ALG_RSA
55 }, {
56 .tag = OH_HUKS_TAG_PURPOSE,
57 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY
58 }, {
59 .tag = OH_HUKS_TAG_KEY_SIZE,
60 .uint32Param = OH_HUKS_RSA_KEY_SIZE_4096
61 }, {
62 .tag = OH_HUKS_TAG_PADDING,
63 .uint32Param = OH_HUKS_PADDING_PSS
64 }, {
65 .tag = OH_HUKS_TAG_DIGEST,
66 .uint32Param = OH_HUKS_DIGEST_SHA384
67 },
68 };
69 static struct OH_Huks_Param g_signParamsTest071[] = {
70 {
71 .tag = OH_HUKS_TAG_ALGORITHM,
72 .uint32Param = OH_HUKS_ALG_RSA
73 }, {
74 .tag = OH_HUKS_TAG_PURPOSE,
75 .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN
76 }, {
77 .tag = OH_HUKS_TAG_KEY_SIZE,
78 .uint32Param = OH_HUKS_RSA_KEY_SIZE_4096
79 }, {
80 .tag = OH_HUKS_TAG_PADDING,
81 .uint32Param = OH_HUKS_PADDING_PSS
82 }, {
83 .tag = OH_HUKS_TAG_DIGEST,
84 .uint32Param = OH_HUKS_DIGEST_SHA384
85 }
86 };
87 static struct OH_Huks_Param g_verifyParamsTest071[] = {
88 {
89 .tag = OH_HUKS_TAG_ALGORITHM,
90 .uint32Param = OH_HUKS_ALG_RSA
91 }, {
92 .tag = OH_HUKS_TAG_PURPOSE,
93 .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY
94 }, {
95 .tag = OH_HUKS_TAG_KEY_SIZE,
96 .uint32Param = OH_HUKS_RSA_KEY_SIZE_4096
97 }, {
98 .tag = OH_HUKS_TAG_PADDING,
99 .uint32Param = OH_HUKS_PADDING_PSS
100 }, {
101 .tag = OH_HUKS_TAG_DIGEST,
102 .uint32Param = OH_HUKS_DIGEST_SHA384
103 }
104 };
105
106 /**
107 * @tc.name: HuksSignVerifyRSATest.Security_HUKS_NAPI_SignVerify_RSA_0100
108 * @tc.desc: alg-RSA pur-Sign pad-PKCS1_V1_5 digest-MD5.
109 * @tc.type: FUNC
110 */
111 HWTEST_F(HuksSignVerifyRSATest, Security_HUKS_NAPI_SignVerify_RSA_0100, TestSize.Level1)
112 {
113 const char *keyAliasString = "HksRSASignVerifyKeyAliasTest071";
114 struct OH_Huks_ParamSet *genParamSet = nullptr;
115 struct OH_Huks_ParamSet *signParamSet = nullptr;
116 struct OH_Huks_ParamSet *verifyParamSet = nullptr;
117
118 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParamsTest071, sizeof(g_genParamsTest071) / sizeof(OH_Huks_Param));
119 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
120 ret = InitParamSet(&signParamSet, g_signParamsTest071, sizeof(g_signParamsTest071) / sizeof(OH_Huks_Param));
121 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
122 ret = InitParamSet(&verifyParamSet, g_verifyParamsTest071, sizeof(g_verifyParamsTest071) / sizeof(OH_Huks_Param));
123 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
124
125 /* Generate Key */
126 struct OH_Huks_Blob keyAlias = {strlen(keyAliasString), (uint8_t *)keyAliasString};
127
128 if ((genParamSet != nullptr) || (signParamSet != nullptr) || (verifyParamSet != nullptr)) {
129 ret = HksRsaSignVerifyTestNormalCase(keyAlias, genParamSet, signParamSet, verifyParamSet);
130 }
131
132 /* Delete Key */
133 ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
134 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
135
136 OH_Huks_FreeParamSet(&genParamSet);
137 OH_Huks_FreeParamSet(&signParamSet);
138 OH_Huks_FreeParamSet(&verifyParamSet);
139 }
140 } // namespace Unittest::RsaSignVerify