• 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 {
23 bool useSoftware = true;
24 class HuksSignVerifyDSATest : public testing::Test
25 {
26    public:
27     static void SetUpTestCase(void);
28 
29     static void TearDownTestCase(void);
30 
31     void SetUp();
32 
33     void TearDown();
34 };
35 
SetUpTestCase(void)36 void HuksSignVerifyDSATest::SetUpTestCase(void) { useSoftware = checkUseSoftware(); }
37 
TearDownTestCase(void)38 void HuksSignVerifyDSATest::TearDownTestCase(void) {}
39 
SetUp()40 void HuksSignVerifyDSATest::SetUp() {}
41 
TearDown()42 void HuksSignVerifyDSATest::TearDown() {}
43 
44 static struct OH_Huks_Param g_genParamsTest001[] = {
45     {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_DSA},
46     {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY},
47     {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = DSA_COMMON_SIZE},
48     {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA1},
49     {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
50 static struct OH_Huks_Param g_signParamsTest001[] = {
51     {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_DSA},
52     {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN},
53     {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA1},
54     {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
55 static struct OH_Huks_Param g_verifyParamsTest001[] = {
56     {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_DSA},
57     {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY},
58     {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = DSA_COMMON_SIZE},
59     {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA1},
60     {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
61 
HksTestSignVerify(struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData,bool isSign)62 OH_Huks_Result HksTestSignVerify(struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *paramSet,
63                                  const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData, bool isSign)
64 {
65     uint8_t tmpHandle[sizeof(uint64_t)] = {0};
66     struct OH_Huks_Blob handle = {sizeof(uint64_t), tmpHandle};
67     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
68     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
69     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
70         return ret;
71     }
72 
73     struct OH_Huks_Param *tmpParam = NULL;
74     ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_PURPOSE, &tmpParam);
75     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
76         return ret;
77     }
78 
79     ret = TestUpdateFinish(&handle, paramSet, tmpParam->uint32Param, inData, outData);
80     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed.";
81     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
82         return ret;
83     }
84 
85     return ret;
86 }
87 
HksDsaSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * signParamSet,struct OH_Huks_ParamSet * verifyParamSet)88 OH_Huks_Result HksDsaSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias, struct OH_Huks_ParamSet *genParamSet,
89                                               struct OH_Huks_ParamSet *signParamSet,
90                                               struct OH_Huks_ParamSet *verifyParamSet)
91 {
92     struct OH_Huks_Blob inData = {g_inData.length(), (uint8_t *)g_inData.c_str()};
93     /* 1. Generate Key */
94     // Generate Key
95     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
96     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
97 
98     /* 2. Sign Three Stage */
99     uint8_t outDataS[DSA_COMMON_SIZE] = {0};
100     struct OH_Huks_Blob outDataSign = {DSA_COMMON_SIZE, outDataS};
101     ret = HksTestSignVerify(&keyAlias, signParamSet, &inData, &outDataSign, true);
102     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Sign failed.";
103 
104     /* 3. Export Public Key */
105     uint8_t pubKey[DSA_COMMON_SIZE] = {0};
106     struct OH_Huks_Blob publicKey = {DSA_COMMON_SIZE, pubKey};
107     ret = OH_Huks_ExportPublicKeyItem(&keyAlias, genParamSet, &publicKey);
108     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey failed.";
109 
110     /* 4. Import Key */
111     char newKey[] = "DSA_Sign_Verify_Import_KeyAlias";
112     struct OH_Huks_Blob newKeyAlias = {.size = strlen(newKey), .data = (uint8_t *)newKey};
113     ret = OH_Huks_ImportKeyItem(&newKeyAlias, verifyParamSet, &publicKey);
114     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ImportKey failed";
115 
116     /* 5. Verify Three Stage */
117     ret = HksTestSignVerify(&newKeyAlias, verifyParamSet, &inData, &outDataSign, false);
118     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Verify failed.";
119 
120     /* 6. Delete New Key */
121     ret = OH_Huks_DeleteKeyItem(&newKeyAlias, verifyParamSet);
122     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed.";
123 
124     return ret;
125 }
126 
127 /**
128  * @tc.name: HuksSignVerifyDSATest.Security_HUKS_NAPI_SignVerify_DSA_0100
129  * @tc.desc: alg-DSA pur-Sign-verify dig-SHA1
130  * @tc.type: FUNC
131  */
132 HWTEST_F(HuksSignVerifyDSATest, Security_HUKS_NAPI_SignVerify_DSA_0100, TestSize.Level0)
133 {
134     if (useSoftware) {
135         const char *keyAliasString = "HksDSASignVerifyKeyAliasTest001";
136         struct OH_Huks_ParamSet *genParamSet = nullptr;
137         struct OH_Huks_ParamSet *signParamSet = nullptr;
138         struct OH_Huks_ParamSet *verifyParamSet = nullptr;
139         struct OH_Huks_Blob keyAlias = {strlen(keyAliasString), (uint8_t *)keyAliasString};
140 
141         OH_Huks_Result ret =
142             InitParamSet(&genParamSet, g_genParamsTest001, sizeof(g_genParamsTest001) / sizeof(OH_Huks_Param));
143         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
144         ret = InitParamSet(&signParamSet, g_signParamsTest001, sizeof(g_signParamsTest001) / sizeof(OH_Huks_Param));
145         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
146         ret =
147             InitParamSet(&verifyParamSet, g_verifyParamsTest001, sizeof(g_verifyParamsTest001) / sizeof(OH_Huks_Param));
148         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
149         if ((genParamSet != nullptr) || (signParamSet != nullptr) || (verifyParamSet != nullptr)) {
150             ret = HksDsaSignVerifyTestNormalCase(keyAlias, genParamSet, signParamSet, verifyParamSet);
151         }
152 
153         /* 5. Delete Key */
154         ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
155         EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
156 
157         OH_Huks_FreeParamSet(&genParamSet);
158         OH_Huks_FreeParamSet(&signParamSet);
159         OH_Huks_FreeParamSet(&verifyParamSet);
160     }
161     ASSERT_TRUE(0 == 0);
162 }
163 }  // namespace Unittest::DsaSignVerify