• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_dsa_test.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 namespace Unittest::DsaSignVerify {
22 bool useSoftware = true;
23 class HuksSignVerifyDSATest : 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 HuksSignVerifyDSATest::SetUpTestCase(void)
35 {
36     useSoftware = checkUseSoftware();
37 }
38 
TearDownTestCase(void)39 void HuksSignVerifyDSATest::TearDownTestCase(void)
40 {
41 }
42 
SetUp()43 void HuksSignVerifyDSATest::SetUp()
44 {
45 }
46 
TearDown()47 void HuksSignVerifyDSATest::TearDown()
48 {
49 }
50 
51 static struct OH_Huks_Param g_genParamsTest001[] = {
52     {
53         .tag = OH_HUKS_TAG_ALGORITHM,
54         .uint32Param = OH_HUKS_ALG_DSA
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 = DSA_COMMON_SIZE
61     }, {
62         .tag = OH_HUKS_TAG_DIGEST,
63         .uint32Param = OH_HUKS_DIGEST_SHA1
64     }
65 };
66 static struct OH_Huks_Param g_signParamsTest001[] = {
67     {
68         .tag = OH_HUKS_TAG_ALGORITHM,
69         .uint32Param = OH_HUKS_ALG_DSA
70     }, {
71         .tag = OH_HUKS_TAG_PURPOSE,
72         .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN
73     }, {
74         .tag = OH_HUKS_TAG_DIGEST,
75         .uint32Param = OH_HUKS_DIGEST_SHA1
76     }
77 };
78 static struct OH_Huks_Param g_verifyParamsTest001[] = {
79     {
80         .tag = OH_HUKS_TAG_ALGORITHM,
81         .uint32Param = OH_HUKS_ALG_DSA
82     }, {
83         .tag = OH_HUKS_TAG_PURPOSE,
84         .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY
85     }, {
86         .tag = OH_HUKS_TAG_KEY_SIZE,
87         .uint32Param = DSA_COMMON_SIZE
88     }, {
89         .tag = OH_HUKS_TAG_DIGEST,
90         .uint32Param = OH_HUKS_DIGEST_SHA1
91     }
92 };
93 
HksTestSignVerify(struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData,bool isSign)94 OH_Huks_Result HksTestSignVerify(struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData,
95     struct OH_Huks_Blob *outData, bool isSign)
96 {
97     uint8_t tmpHandle[sizeof(uint64_t)] = {0};
98     struct OH_Huks_Blob handle = { sizeof(uint64_t), tmpHandle };
99     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
100     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
101     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
102         return ret;
103     }
104 
105     struct OH_Huks_Param *tmpParam = NULL;
106     ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_PURPOSE, &tmpParam);
107     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
108         return ret;
109     }
110 
111     ret = TestUpdateFinish(&handle, paramSet, tmpParam->uint32Param, inData, outData);
112     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
113     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
114         return ret;
115     }
116 
117     return ret;
118 }
119 
HksDsaSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * signParamSet,struct OH_Huks_ParamSet * verifyParamSet)120 OH_Huks_Result HksDsaSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias, struct OH_Huks_ParamSet *genParamSet,
121     struct OH_Huks_ParamSet *signParamSet, struct OH_Huks_ParamSet *verifyParamSet)
122 {
123     struct OH_Huks_Blob inData = {
124         g_inData.length(),
125         (uint8_t *)g_inData.c_str()
126     };
127     /* 1. Generate Key */
128     // Generate Key
129     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
130     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
131 
132     /* 2. Sign Three Stage */
133     uint8_t outDataS[DSA_COMMON_SIZE] = {0};
134     struct OH_Huks_Blob outDataSign = { DSA_COMMON_SIZE, outDataS };
135     ret = HksTestSignVerify(&keyAlias, signParamSet, &inData, &outDataSign, true);
136     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Sign failed.";
137 
138     /* 3. Export Public Key */
139     uint8_t pubKey[DSA_COMMON_SIZE] = {0};
140     struct OH_Huks_Blob publicKey = { DSA_COMMON_SIZE, pubKey };
141     ret = OH_Huks_ExportPublicKeyItem(&keyAlias, genParamSet, &publicKey);
142     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey failed.";
143 
144     /* 4. Import Key */
145     char newKey[] = "DSA_Sign_Verify_Import_KeyAlias";
146     struct OH_Huks_Blob newKeyAlias = { .size = strlen(newKey), .data = (uint8_t *)newKey };
147     ret = OH_Huks_ImportKeyItem(&newKeyAlias, verifyParamSet, &publicKey);
148     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ImportKey failed";
149 
150     /* 5. Verify Three Stage */
151     ret = HksTestSignVerify(&newKeyAlias, verifyParamSet, &inData, &outDataSign, false);
152     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Verify failed.";
153 
154     /* 6. Delete New Key */
155     ret = OH_Huks_DeleteKeyItem(&newKeyAlias, verifyParamSet);
156     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed.";
157 
158     return ret;
159 }
160 
161 /**
162  * @tc.name: HuksSignVerifyDSATest.Security_HUKS_NAPI_SignVerify_DSA_0100
163  * @tc.desc: alg-DSA pur-Sign-verify dig-SHA1
164  * @tc.type: FUNC
165  */
166 HWTEST_F(HuksSignVerifyDSATest, Security_HUKS_NAPI_SignVerify_DSA_0100, TestSize.Level0)
167 {
168     if (useSoftware)
169     {
170         const char *keyAliasString = "HksDSASignVerifyKeyAliasTest001";
171         struct OH_Huks_ParamSet *genParamSet = nullptr;
172         struct OH_Huks_ParamSet *signParamSet = nullptr;
173         struct OH_Huks_ParamSet *verifyParamSet = nullptr;
174         struct OH_Huks_Blob keyAlias = { strlen(keyAliasString), (uint8_t *)keyAliasString };
175 
176         OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParamsTest001, sizeof(g_genParamsTest001) / sizeof(OH_Huks_Param));
177         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
178         ret = InitParamSet(&signParamSet, g_signParamsTest001, sizeof(g_signParamsTest001) / sizeof(OH_Huks_Param));
179         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
180         ret = InitParamSet(&verifyParamSet, g_verifyParamsTest001, sizeof(g_verifyParamsTest001) / sizeof(OH_Huks_Param));
181         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
182         if ((genParamSet != nullptr) || (signParamSet != nullptr) || (verifyParamSet != nullptr)) {
183             ret = HksDsaSignVerifyTestNormalCase(keyAlias, genParamSet, signParamSet, verifyParamSet);
184         }
185 
186         /* 5. Delete Key */
187         ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
188         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
189 
190         OH_Huks_FreeParamSet(&genParamSet);
191         OH_Huks_FreeParamSet(&signParamSet);
192         OH_Huks_FreeParamSet(&verifyParamSet);
193     }
194     ASSERT_TRUE(0 == 0);
195 }
196 } // namespace Unittest::DsaSignVerify