1 /*
2 * Copyright (C) 2023 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 <gtest/gtest.h>
17
18 #include "dsa_asy_key_generator_openssl.c"
19 #include "dsa_openssl.c"
20 #include "params_parser.h"
21
22 using namespace std;
23 using namespace testing::ext;
24
25 namespace {
26 class CryptoDsaExceptionTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp();
31 void TearDown();
32 };
33
SetUpTestCase()34 void CryptoDsaExceptionTest::SetUpTestCase() {}
TearDownTestCase()35 void CryptoDsaExceptionTest::TearDownTestCase() {}
SetUp()36 void CryptoDsaExceptionTest::SetUp() {}
TearDown()37 void CryptoDsaExceptionTest::TearDown() {}
38
GetMockClass(void)39 static const char *GetMockClass(void)
40 {
41 return "HcfAsyKeyGenerator";
42 }
43
44 HcfObjectBase g_obj = {
45 .getClass = GetMockClass,
46 .destroy = nullptr
47 };
48
49 static const char *g_algNameDSA = "DSA";
50
51 constexpr uint32_t DSA2048_PUB_SIZE = 256;
52 constexpr uint32_t DSA2048_P_SIZE = 256;
53 constexpr uint32_t DSA2048_Q_SIZE = 20;
54 constexpr uint32_t DSA2048_G_SIZE = 256;
55
56 static const bool IS_BIG_ENDIAN = IsBigEndian();
57
58 static unsigned char g_dsa2048PBigE[] = {
59 0x00, 0x25, 0xFA, 0xAD, 0xF4, 0x8E, 0xB9, 0xE5, 0x99, 0xF3, 0x5D, 0x6F,
60 0x4F, 0x83, 0x34, 0xE2, 0x7E, 0xCF, 0x6F, 0xBF, 0x30, 0xAF, 0x6F, 0x81,
61 0xEB, 0xF8, 0xC4, 0x13, 0xD9, 0xA0, 0x5D, 0x8B, 0x5C, 0x8E, 0xDC, 0xC2,
62 0x1D, 0x0B, 0x41, 0x32, 0xB0, 0x1F, 0xFE, 0xEF, 0x0C, 0xC2, 0xA2, 0x7E,
63 0x68, 0x5C, 0x28, 0x21, 0xE9, 0xF5, 0xB1, 0x58, 0x12, 0x63, 0x4C, 0x19,
64 0x4E, 0xFF, 0x02, 0x4B, 0x92, 0xED, 0xD2, 0x07, 0x11, 0x4D, 0x8C, 0x58,
65 0x16, 0x5C, 0x55, 0x8E, 0xAD, 0xA3, 0x67, 0x7D, 0xB9, 0x86, 0x6E, 0x0B,
66 0xE6, 0x54, 0x6F, 0x40, 0xAE, 0x0E, 0x67, 0x4C, 0xF9, 0x12, 0x5B, 0x3C,
67 0x08, 0x7A, 0xF7, 0xFC, 0x67, 0x86, 0x69, 0xE7, 0x0A, 0x94, 0x40, 0xBF,
68 0x8B, 0x76, 0xFE, 0x26, 0xD1, 0xF2, 0xA1, 0x1A, 0x84, 0xA1, 0x43, 0x56,
69 0x28, 0xBC, 0x9A, 0x5F, 0xD7, 0x3B, 0x69, 0x89, 0x8A, 0x36, 0x2C, 0x51,
70 0xDF, 0x12, 0x77, 0x2F, 0x57, 0x7B, 0xA0, 0xAA, 0xDD, 0x7F, 0xA1, 0x62,
71 0x3B, 0x40, 0x7B, 0x68, 0x1A, 0x8F, 0x0D, 0x38, 0xBB, 0x21, 0x5D, 0x18,
72 0xFC, 0x0F, 0x46, 0xF7, 0xA3, 0xB0, 0x1D, 0x23, 0xC3, 0xD2, 0xC7, 0x72,
73 0x51, 0x18, 0xDF, 0x46, 0x95, 0x79, 0xD9, 0xBD, 0xB5, 0x19, 0x02, 0x2C,
74 0x87, 0xDC, 0xE7, 0x57, 0x82, 0x7E, 0xF1, 0x8B, 0x06, 0x3D, 0x00, 0xA5,
75 0x7B, 0x6B, 0x26, 0x27, 0x91, 0x0F, 0x6A, 0x77, 0xE4, 0xD5, 0x04, 0xE4,
76 0x12, 0x2C, 0x42, 0xFF, 0xD2, 0x88, 0xBB, 0xD3, 0x92, 0xA0, 0xF9, 0xC8,
77 0x51, 0x64, 0x14, 0x5C, 0xD8, 0xF9, 0x6C, 0x47, 0x82, 0xB4, 0x1C, 0x7F,
78 0x09, 0xB8, 0xF0, 0x25, 0x83, 0x1D, 0x3F, 0x3F, 0x05, 0xB3, 0x21, 0x0A,
79 0x5D, 0xA7, 0xD8, 0x54, 0xC3, 0x65, 0x7D, 0xC3, 0xB0, 0x1D, 0xBF, 0xAE,
80 0xF8, 0x68, 0xCF, 0x00,
81 };
82
83 static unsigned char g_dsa2048PLittleE[] = {
84 0x00, 0xcf, 0x68, 0xf8, 0xae, 0xbf, 0x1d, 0xb0, 0xc3, 0x7d, 0x65, 0xc3,
85 0x54, 0xd8, 0xa7, 0x5d, 0x0a, 0x21, 0xb3, 0x05, 0x3f, 0x3f, 0x1d, 0x83,
86 0x25, 0xf0, 0xb8, 0x09, 0x7f, 0x1c, 0xb4, 0x82, 0x47, 0x6c, 0xf9, 0xd8,
87 0x5c, 0x14, 0x64, 0x51, 0xc8, 0xf9, 0xa0, 0x92, 0xd3, 0xbb, 0x88, 0xd2,
88 0xff, 0x42, 0x2c, 0x12, 0xe4, 0x04, 0xd5, 0xe4, 0x77, 0x6a, 0x0f, 0x91,
89 0x27, 0x26, 0x6b, 0x7b, 0xa5, 0x00, 0x3d, 0x06, 0x8b, 0xf1, 0x7e, 0x82,
90 0x57, 0xe7, 0xdc, 0x87, 0x2c, 0x02, 0x19, 0xb5, 0xbd, 0xd9, 0x79, 0x95,
91 0x46, 0xdf, 0x18, 0x51, 0x72, 0xc7, 0xd2, 0xc3, 0x23, 0x1d, 0xb0, 0xa3,
92 0xf7, 0x46, 0x0f, 0xfc, 0x18, 0x5d, 0x21, 0xbb, 0x38, 0x0d, 0x8f, 0x1a,
93 0x68, 0x7b, 0x40, 0x3b, 0x62, 0xa1, 0x7f, 0xdd, 0xaa, 0xa0, 0x7b, 0x57,
94 0x2f, 0x77, 0x12, 0xdf, 0x51, 0x2c, 0x36, 0x8a, 0x89, 0x69, 0x3b, 0xd7,
95 0x5f, 0x9a, 0xbc, 0x28, 0x56, 0x43, 0xa1, 0x84, 0x1a, 0xa1, 0xf2, 0xd1,
96 0x26, 0xfe, 0x76, 0x8b, 0xbf, 0x40, 0x94, 0x0a, 0xe7, 0x69, 0x86, 0x67,
97 0xfc, 0xf7, 0x7a, 0x08, 0x3c, 0x5b, 0x12, 0xf9, 0x4c, 0x67, 0x0e, 0xae,
98 0x40, 0x6f, 0x54, 0xe6, 0x0b, 0x6e, 0x86, 0xb9, 0x7d, 0x67, 0xa3, 0xad,
99 0x8e, 0x55, 0x5c, 0x16, 0x58, 0x8c, 0x4d, 0x11, 0x07, 0xd2, 0xed, 0x92,
100 0x4b, 0x02, 0xff, 0x4e, 0x19, 0x4c, 0x63, 0x12, 0x58, 0xb1, 0xf5, 0xe9,
101 0x21, 0x28, 0x5c, 0x68, 0x7e, 0xa2, 0xc2, 0x0c, 0xef, 0xfe, 0x1f, 0xb0,
102 0x32, 0x41, 0x0b, 0x1d, 0xc2, 0xdc, 0x8e, 0x5c, 0x8b, 0x5d, 0xa0, 0xd9,
103 0x13, 0xc4, 0xf8, 0xeb, 0x81, 0x6f, 0xaf, 0x30, 0xbf, 0x6f, 0xcf, 0x7e,
104 0xe2, 0x34, 0x83, 0x4f, 0x6f, 0x5d, 0xf3, 0x99, 0xe5, 0xb9, 0x8e, 0xf4,
105 0xad, 0xfa, 0x25, 0x00,
106 };
107
108 static unsigned char g_dsa2048QBigE[] = {
109 0x00, 0xE7, 0x33, 0x4D, 0xD3, 0x94, 0x3E, 0x0B, 0xDB, 0x62, 0x74, 0xC6,
110 0xA1, 0x08, 0xDD, 0x19, 0xA3, 0x75, 0x17, 0x00,
111 };
112
113 static unsigned char g_dsa2048QLittleE[] = {
114 0x00, 0x17, 0x75, 0xa3, 0x19, 0xdd, 0x08, 0xa1, 0xc6, 0x74, 0x62, 0xdb,
115 0x0b, 0x3e, 0x94, 0xd3, 0x4d, 0x33, 0xe7, 0x00,
116 };
117
118 static unsigned char g_dsa2048GBigE[] = {
119 0x00, 0x78, 0x16, 0x59, 0x34, 0x63, 0xF4, 0xF3, 0x92, 0xFC, 0xB5, 0xA5,
120 0x4F, 0x13, 0xDE, 0x2F, 0x1C, 0xA4, 0x3C, 0xAE, 0xAD, 0x38, 0x3F, 0x7E,
121 0x90, 0xBF, 0x96, 0xA6, 0xAE, 0x25, 0x90, 0x72, 0xF5, 0x8E, 0x80, 0x0C,
122 0x39, 0x1C, 0xD9, 0xEC, 0xBA, 0x90, 0x5B, 0x3A, 0xE8, 0x58, 0x6C, 0x9E,
123 0x30, 0x42, 0x37, 0x02, 0x31, 0x82, 0xBC, 0x6A, 0xDF, 0x6A, 0x09, 0x29,
124 0xE3, 0xC0, 0x46, 0xD1, 0xCB, 0x85, 0xEC, 0x0C, 0x30, 0x5E, 0xEA, 0xC8,
125 0x39, 0x8E, 0x22, 0x9F, 0x22, 0x10, 0xD2, 0x34, 0x61, 0x68, 0x37, 0x3D,
126 0x2E, 0x4A, 0x5B, 0x9A, 0xF5, 0xC1, 0x48, 0xC6, 0xF6, 0xDC, 0x63, 0x1A,
127 0xD3, 0x96, 0x64, 0xBA, 0x34, 0xC9, 0xD1, 0xA0, 0xD1, 0xAE, 0x6C, 0x2F,
128 0x48, 0x17, 0x93, 0x14, 0x43, 0xED, 0xF0, 0x21, 0x30, 0x19, 0xC3, 0x1B,
129 0x5F, 0xDE, 0xA3, 0xF0, 0x70, 0x78, 0x18, 0xE1, 0xA8, 0xE4, 0xEE, 0x2E,
130 0x00, 0xA5, 0xE4, 0xB3, 0x17, 0xC8, 0x0C, 0x7D, 0x6E, 0x42, 0xDC, 0xB7,
131 0x46, 0x00, 0x36, 0x4D, 0xD4, 0x46, 0xAA, 0x3D, 0x3C, 0x46, 0x89, 0x40,
132 0xBF, 0x1D, 0x84, 0x77, 0x0A, 0x75, 0xF3, 0x87, 0x1D, 0x08, 0x4C, 0xA6,
133 0xD1, 0xA9, 0x1C, 0x1E, 0x12, 0x1E, 0xE1, 0xC7, 0x30, 0x28, 0x76, 0xA5,
134 0x7F, 0x6C, 0x85, 0x96, 0x2B, 0x6F, 0xDB, 0x80, 0x66, 0x26, 0xAE, 0xF5,
135 0x93, 0xC7, 0x8E, 0xAE, 0x9A, 0xED, 0xE4, 0xCA, 0x04, 0xEA, 0x3B, 0x72,
136 0xEF, 0xDC, 0x87, 0xED, 0x0D, 0xA5, 0x4C, 0x4A, 0xDD, 0x71, 0x22, 0x64,
137 0x59, 0x69, 0x4E, 0x8E, 0xBF, 0x43, 0xDC, 0xAB, 0x8E, 0x66, 0xBB, 0x01,
138 0xB6, 0xF4, 0xE7, 0xFD, 0xD2, 0xAD, 0x9F, 0x36, 0xC1, 0xA0, 0x29, 0x99,
139 0xD1, 0x96, 0x70, 0x59, 0x06, 0x78, 0x35, 0xBD, 0x65, 0x55, 0x52, 0x9E,
140 0xF8, 0xB2, 0xE5, 0x00,
141 };
142
143 static unsigned char g_dsa2048GLittleE[] = {
144 0x00, 0xe5, 0xb2, 0xf8, 0x9e, 0x52, 0x55, 0x65, 0xbd, 0x35, 0x78, 0x06,
145 0x59, 0x70, 0x96, 0xd1, 0x99, 0x29, 0xa0, 0xc1, 0x36, 0x9f, 0xad, 0xd2,
146 0xfd, 0xe7, 0xf4, 0xb6, 0x01, 0xbb, 0x66, 0x8e, 0xab, 0xdc, 0x43, 0xbf,
147 0x8e, 0x4e, 0x69, 0x59, 0x64, 0x22, 0x71, 0xdd, 0x4a, 0x4c, 0xa5, 0x0d,
148 0xed, 0x87, 0xdc, 0xef, 0x72, 0x3b, 0xea, 0x04, 0xca, 0xe4, 0xed, 0x9a,
149 0xae, 0x8e, 0xc7, 0x93, 0xf5, 0xae, 0x26, 0x66, 0x80, 0xdb, 0x6f, 0x2b,
150 0x96, 0x85, 0x6c, 0x7f, 0xa5, 0x76, 0x28, 0x30, 0xc7, 0xe1, 0x1e, 0x12,
151 0x1e, 0x1c, 0xa9, 0xd1, 0xa6, 0x4c, 0x08, 0x1d, 0x87, 0xf3, 0x75, 0x0a,
152 0x77, 0x84, 0x1d, 0xbf, 0x40, 0x89, 0x46, 0x3c, 0x3d, 0xaa, 0x46, 0xd4,
153 0x4d, 0x36, 0x00, 0x46, 0xb7, 0xdc, 0x42, 0x6e, 0x7d, 0x0c, 0xc8, 0x17,
154 0xb3, 0xe4, 0xa5, 0x00, 0x2e, 0xee, 0xe4, 0xa8, 0xe1, 0x18, 0x78, 0x70,
155 0xf0, 0xa3, 0xde, 0x5f, 0x1b, 0xc3, 0x19, 0x30, 0x21, 0xf0, 0xed, 0x43,
156 0x14, 0x93, 0x17, 0x48, 0x2f, 0x6c, 0xae, 0xd1, 0xa0, 0xd1, 0xc9, 0x34,
157 0xba, 0x64, 0x96, 0xd3, 0x1a, 0x63, 0xdc, 0xf6, 0xc6, 0x48, 0xc1, 0xf5,
158 0x9a, 0x5b, 0x4a, 0x2e, 0x3d, 0x37, 0x68, 0x61, 0x34, 0xd2, 0x10, 0x22,
159 0x9f, 0x22, 0x8e, 0x39, 0xc8, 0xea, 0x5e, 0x30, 0x0c, 0xec, 0x85, 0xcb,
160 0xd1, 0x46, 0xc0, 0xe3, 0x29, 0x09, 0x6a, 0xdf, 0x6a, 0xbc, 0x82, 0x31,
161 0x02, 0x37, 0x42, 0x30, 0x9e, 0x6c, 0x58, 0xe8, 0x3a, 0x5b, 0x90, 0xba,
162 0xec, 0xd9, 0x1c, 0x39, 0x0c, 0x80, 0x8e, 0xf5, 0x72, 0x90, 0x25, 0xae,
163 0xa6, 0x96, 0xbf, 0x90, 0x7e, 0x3f, 0x38, 0xad, 0xae, 0x3c, 0xa4, 0x1c,
164 0x2f, 0xde, 0x13, 0x4f, 0xa5, 0xb5, 0xfc, 0x92, 0xf3, 0xf4, 0x63, 0x34,
165 0x59, 0x16, 0x78, 0x00,
166 };
167
168 static HcfBigInteger p_BN = { .data = IS_BIG_ENDIAN ? g_dsa2048PBigE : g_dsa2048PLittleE, .len = DSA2048_P_SIZE };
169 static HcfBigInteger q_BN = { .data = IS_BIG_ENDIAN ? g_dsa2048QBigE : g_dsa2048QLittleE, .len = DSA2048_Q_SIZE };
170 static HcfBigInteger g_BN = { .data = IS_BIG_ENDIAN ? g_dsa2048GBigE : g_dsa2048GLittleE, .len = DSA2048_G_SIZE };
171
172 static HcfBigInteger p_EXBN = { .data = IS_BIG_ENDIAN ? g_dsa2048PBigE : g_dsa2048PLittleE, .len = DSA2048_P_SIZE + 1 };
173
174 static HcfAsyKeyParamsSpec asySpecComm = {
175 .algName = const_cast<char *>(g_algNameDSA),
176 .specType = HCF_COMMON_PARAMS_SPEC
177 };
178 static HcfAsyKeyParamsSpec asySpecPk = {
179 .algName = const_cast<char *>(g_algNameDSA),
180 .specType = HCF_PUBLIC_KEY_SPEC
181 };
182 static HcfAsyKeyParamsSpec asySpecKeyPair = {
183 .algName = const_cast<char *>(g_algNameDSA),
184 .specType = HCF_KEY_PAIR_SPEC
185 };
186
187 static HcfDsaCommParamsSpec dsaCommonSpec = { .base = asySpecComm, .p = p_BN, .q = q_BN, .g = g_BN };
188 static HcfDsaCommParamsSpec dsaCommonSpecException = { .base = asySpecComm, .p = p_EXBN, .q = q_BN, .g = g_BN };
189 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest001, TestSize.Level0)
190 {
191 BIGNUM *p = NULL;
192 BIGNUM *q = NULL;
193 BIGNUM *g = NULL;
194
195 HcfResult ret = BigIntegerToBigNum(&p_BN, &p);
196 ASSERT_EQ(ret, HCF_SUCCESS);
197
198 ret = BigIntegerToBigNum(&q_BN, &q);
199 ASSERT_EQ(ret, HCF_SUCCESS);
200
201 ret = BigIntegerToBigNum(&g_BN, &g);
202 ASSERT_EQ(ret, HCF_SUCCESS);
203
204 FreeCommSpecBn(p, q, g);
205
206 FreeCtx(nullptr, nullptr, nullptr);
207 FreeCommSpecBn(nullptr, nullptr, nullptr);
208 }
209
210 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest002, TestSize.Level0)
211 {
212 DestroyDsaKeyGeneratorSpiImpl(&g_obj);
213 DestroyDsaKeyGeneratorSpiImpl(nullptr);
214
215 DestroyDsaPriKey(&g_obj);
216
217 GetDsaPriKeyAlgorithm(nullptr);
218 GetDsaPriKeyAlgorithm((HcfKey *)&g_obj);
219
220 HcfResult ret = GetDsaPriKeyEncoded(nullptr, nullptr);
221 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
222
223 ret = GetDsaPriKeyEncoded((HcfKey *)&g_obj, nullptr);
224 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
225
226 HcfBlob blob = { .data = nullptr, .len = 0 };
227 ret = GetDsaPriKeyEncoded((HcfKey *)&g_obj, &blob);
228 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
229
230 (void)GetDsaPubKeyFormat((HcfKey *)&g_obj);
231 (void)GetDsaPriKeyFormat(nullptr);
232 (void)GetDsaPriKeyFormat((HcfKey *)&g_obj);
233 }
234
235 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest003, TestSize.Level0)
236 {
237 HcfResult ret = GetBigIntegerSpecFromDsaPubKey(nullptr, DSA_P_BN, nullptr);
238 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
239
240 ret = GetBigIntegerSpecFromDsaPubKey((const HcfPubKey *)&g_obj, DSA_P_BN, nullptr);
241 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
242
243 HcfBigInteger p = { .data = nullptr, .len = 0 };
244 GetBigIntegerSpecFromDsaPubKey((const HcfPubKey *)&g_obj, DSA_P_BN, &p);
245 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
246 }
247
248 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest004, TestSize.Level0)
249 {
250 HcfResult ret = GetBigIntegerSpecFromDsaPriKey(nullptr, DSA_P_BN, nullptr);
251 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
252
253 ret = GetBigIntegerSpecFromDsaPriKey((const HcfPriKey *)&g_obj, DSA_P_BN, nullptr);
254 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
255
256 HcfBigInteger p = { .data = nullptr, .len = 0 };
257 GetBigIntegerSpecFromDsaPriKey((const HcfPriKey *)&g_obj, DSA_P_BN, &p);
258 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
259 }
260
261 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest005, TestSize.Level0)
262 {
263 ClearDsaPriKeyMem(nullptr);
264 ClearDsaPriKeyMem((HcfPriKey *)&g_obj);
265
266 HcfOpensslDsaPubKey *pubKey = nullptr;
267 HcfOpensslDsaPriKey *priKey = nullptr;
268 HcfResult ret = GenerateDsaPubAndPriKey(DSA2048_PUB_SIZE + 1, &pubKey, &priKey);
269 ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
270 }
271
272 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest006, TestSize.Level0)
273 {
274 BIGNUM *p = NULL;
275 BIGNUM *q = NULL;
276
277 HcfResult ret = ConvertCommSpec2Bn(&dsaCommonSpec, nullptr, nullptr, nullptr);
278 ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
279
280 ret = ConvertCommSpec2Bn(&dsaCommonSpec, &p, nullptr, nullptr);
281 ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
282
283 ret = ConvertCommSpec2Bn(&dsaCommonSpec, &p, &q, nullptr);
284 ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
285 }
286
287 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest007, TestSize.Level0)
288 {
289 HcfKeyPair *keyPair = nullptr;
290 HcfResult ret = CreateDsaKeyPairByCommSpec(&dsaCommonSpecException, &keyPair);
291 ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
292 }
293
294 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest008, TestSize.Level0)
295 {
296 HcfOpensslDsaPubKey *pubKey = nullptr;
297 HcfOpensslDsaPriKey *priKey = nullptr;
298 HcfResult ret = ConvertDsaPubAndPriKey(nullptr, nullptr, &pubKey, &priKey);
299 ASSERT_EQ(ret, HCF_SUCCESS);
300 }
301
302 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest009, TestSize.Level0)
303 {
304 HcfKeyPair *keyPair = nullptr;
305 HcfResult ret = EngineGenerateDsaKeyPair((HcfAsyKeyGeneratorSpi *)&g_obj, &keyPair);
306 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
307
308 ret = EngineGenerateDsaKeyPair((HcfAsyKeyGeneratorSpi *)&g_obj, nullptr);
309 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
310 }
311
312 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest010, TestSize.Level0)
313 {
314 HcfResult ret = EngineConvertDsaKey(nullptr, nullptr, nullptr, nullptr, nullptr);
315 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
316
317 ret = EngineConvertDsaKey((HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr, nullptr, nullptr);
318 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
319
320 HcfKeyPair *keyPair = nullptr;
321 ret = EngineConvertDsaKey((HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr, nullptr, &keyPair);
322 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
323 }
324
325 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest011, TestSize.Level0)
326 {
327 HcfResult ret = EngineGenerateDsaKeyPairBySpec(nullptr, nullptr, nullptr);
328 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
329
330 ret = EngineGenerateDsaKeyPairBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr);
331 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
332
333 ret = EngineGenerateDsaKeyPairBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, nullptr);
334 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
335
336 HcfKeyPair *keyPair = nullptr;
337 ret = EngineGenerateDsaKeyPairBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, &keyPair);
338 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
339 }
340
341 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest012, TestSize.Level0)
342 {
343 HcfResult ret = EngineGenerateDsaPubKeyBySpec(nullptr, nullptr, nullptr);
344 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
345
346 ret = EngineGenerateDsaPubKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr);
347 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
348
349 ret = EngineGenerateDsaPubKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecPk, nullptr);
350 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
351
352 HcfPubKey *pk = nullptr;
353 ret = EngineGenerateDsaPubKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecPk, &pk);
354 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
355 }
356
357 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest013, TestSize.Level0)
358 {
359 HcfResult ret = EngineGenerateDsaPriKeyBySpec(nullptr, nullptr, nullptr);
360 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
361
362 ret = EngineGenerateDsaPriKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr);
363 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
364
365 ret = EngineGenerateDsaPriKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, nullptr);
366 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
367
368 HcfPriKey *sk = nullptr;
369 ret = EngineGenerateDsaPriKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, &sk);
370 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
371 }
372
373 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest014, TestSize.Level0)
374 {
375 HcfAsyKeyGenParams params = {
376 .algo = HCF_ALG_DSA,
377 .bits = 0,
378 .primes = HCF_OPENSSL_PRIMES_2
379 };
380
381 HcfResult ret = HcfAsyKeyGeneratorSpiDsaCreate(nullptr, nullptr);
382 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
383
384 ret = HcfAsyKeyGeneratorSpiDsaCreate(¶ms, nullptr);
385 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
386 }
387
388 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest100, TestSize.Level0)
389 {
390 bool ret = IsSignInitInputValid(nullptr, nullptr);
391 ASSERT_EQ(ret, false);
392
393 ret = IsSignInitInputValid((HcfSignSpi *)&g_obj, nullptr);
394 ASSERT_EQ(ret, false);
395
396 HcfPriKey *sk = nullptr;
397 ret = IsSignInitInputValid((HcfSignSpi *)&g_obj, sk);
398 ASSERT_EQ(ret, false);
399 }
400
401 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest101, TestSize.Level0)
402 {
403 bool ret = IsVerifyInitInputValid(nullptr, nullptr);
404 ASSERT_EQ(ret, false);
405
406 ret = IsVerifyInitInputValid((HcfVerifySpi *)&g_obj, nullptr);
407 ASSERT_EQ(ret, false);
408
409 HcfPubKey *pk = nullptr;
410 ret = IsVerifyInitInputValid((HcfVerifySpi *)&g_obj, pk);
411 ASSERT_EQ(ret, false);
412 }
413
414 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest102, TestSize.Level0)
415 {
416 bool ret = IsSignDoFinalInputValid(nullptr, nullptr);
417 ASSERT_EQ(ret, false);
418
419 ret = IsSignDoFinalInputValid((HcfSignSpi *)&g_obj, nullptr);
420 ASSERT_EQ(ret, false);
421
422 HcfBlob signatureData = { .data = nullptr, .len = 0 };
423 ret = IsSignDoFinalInputValid((HcfSignSpi *)&g_obj, &signatureData);
424 ASSERT_EQ(ret, false);
425 }
426
427 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest103, TestSize.Level0)
428 {
429 bool ret = IsVerifyDoFinalInputValid(nullptr, nullptr);
430 ASSERT_EQ(ret, false);
431
432 ret = IsVerifyDoFinalInputValid((HcfVerifySpi *)&g_obj, nullptr);
433 ASSERT_EQ(ret, false);
434
435 HcfBlob signatureData = { .data = nullptr, .len = 0 };
436 ret = IsVerifyDoFinalInputValid((HcfVerifySpi *)&g_obj, &signatureData);
437 ASSERT_EQ(ret, false);
438
439 DestroyDsaSign(nullptr);
440 DestroyDsaSign((HcfObjectBase *)&g_obj);
441
442 DestroyDsaVerify(nullptr);
443 DestroyDsaVerify((HcfObjectBase *)&g_obj);
444 }
445
446 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest104, TestSize.Level0)
447 {
448 HcfResult ret = EngineDsaSignInit(nullptr, nullptr, nullptr);
449 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
450
451 ret = EngineDsaSignWithoutDigestInit(nullptr, nullptr, nullptr);
452 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
453
454 ret = EngineDsaVerifyInit(nullptr, nullptr, nullptr);
455 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
456
457 ret = EngineDsaVerifyWithoutDigestInit(nullptr, nullptr, nullptr);
458 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
459 }
460
461 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest105, TestSize.Level0)
462 {
463 HcfResult ret = EngineDsaSignUpdate(nullptr, nullptr);
464 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
465
466 ret = EngineDsaSignUpdate((HcfSignSpi *)&g_obj, nullptr);
467 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
468
469 HcfBlob data = { .data = nullptr, .len = 0 };
470 ret = EngineDsaSignUpdate((HcfSignSpi *)&g_obj, &data);
471 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
472 }
473
474 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest106, TestSize.Level0)
475 {
476 HcfResult ret = EngineDsaVerifyUpdate(nullptr, nullptr);
477 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
478
479 ret = EngineDsaVerifyUpdate((HcfVerifySpi *)&g_obj, nullptr);
480 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
481
482 HcfBlob data = { .data = nullptr, .len = 0 };
483 ret = EngineDsaVerifyUpdate((HcfVerifySpi *)&g_obj, &data);
484 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
485 }
486
487 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest107, TestSize.Level0)
488 {
489 HcfResult ret = EngineDsaSignDoFinal(nullptr, nullptr, nullptr);
490 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
491
492 ret = EngineDsaSignWithoutDigestDoFinal(nullptr, nullptr, nullptr);
493 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
494
495 (void)EngineDsaVerifyDoFinal(nullptr, nullptr, nullptr);
496
497 (void)EngineDsaVerifyWithoutDigestDoFinal(nullptr, nullptr, nullptr);
498 }
499
500 HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest108, TestSize.Level0)
501 {
502 HcfResult ret = HcfSignSpiDsaCreate(nullptr, nullptr);
503 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
504
505 HcfSignatureParams params = {
506 .algo = HCF_ALG_DSA,
507 .padding = HCF_ALG_NOPADDING,
508 .md = HCF_OPENSSL_DIGEST_SHA256,
509 .mgf1md = HCF_OPENSSL_DIGEST_SHA256
510 };
511 ret = HcfSignSpiDsaCreate(¶ms, nullptr);
512 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
513
514 ret = HcfVerifySpiDsaCreate(nullptr, nullptr);
515 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
516
517 ret = HcfVerifySpiDsaCreate(¶ms, nullptr);
518 ASSERT_EQ(ret, HCF_INVALID_PARAMS);
519 }
520 }
521