1 /*
2 * Copyright (C) 2024 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 #include "crypto_signature.h"
18 #include "crypto_common.h"
19 #include "crypto_asym_key.h"
20 #include "log.h"
21 #include "blob.h"
22 #include "memory.h"
23 #include "memory_mock.h"
24
25 using namespace std;
26 using namespace testing::ext;
27
28 namespace {
29 class NativeSignatureTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp();
34 void TearDown();
35 };
36
SetUpTestCase()37 void NativeSignatureTest::SetUpTestCase() {}
TearDownTestCase()38 void NativeSignatureTest::TearDownTestCase() {}
39
SetUp()40 void NativeSignatureTest::SetUp() // add init here, this will be called before test.
41 {
42 }
43
TearDown()44 void NativeSignatureTest::TearDown() // add destroy here, this will be called when test case done.
45 {
46 }
47
48 HWTEST_F(NativeSignatureTest, NativeSignatureTest001, TestSize.Level0)
49 {
50 OH_CryptoAsymKeyGenerator *generator = nullptr;
51 OH_Crypto_ErrCode res = OH_CryptoAsymKeyGenerator_Create("RSA2048|PRIMES_2", &generator);
52
53 OH_CryptoKeyPair *keyPair = nullptr;
54 res = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPair);
55 EXPECT_EQ(res, CRYPTO_SUCCESS);
56
57 OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(keyPair);
58 OH_CryptoVerify *verify = nullptr;
59 res = OH_CryptoVerify_Create("RSA1024|PSS|SHA256|MGF1_SHA512", &verify);
60 EXPECT_EQ(res, CRYPTO_SUCCESS);
61
62 const char *algoName = OH_CryptoVerify_GetAlgoName(verify);
63 ASSERT_NE(algoName, nullptr);
64
65 int32_t buf[] = {32};
66 Crypto_DataBlob value = {.data = reinterpret_cast<uint8_t *>(buf), .len = sizeof(buf)};
67 res = OH_CryptoVerify_SetParam(verify, CRYPTO_PSS_SALT_LEN_INT, &value);
68 EXPECT_EQ(res, CRYPTO_SUCCESS);
69 res = OH_CryptoVerify_Init(verify, pubkey);
70 EXPECT_EQ(res, CRYPTO_SUCCESS);
71 res = OH_CryptoVerify_Update(verify, nullptr);
72 EXPECT_NE(res, 1);
73
74 OH_CryptoVerify_Destroy(verify);
75 OH_CryptoKeyPair_Destroy(keyPair);
76 OH_CryptoAsymKeyGenerator_Destroy(generator);
77 }
78
79 HWTEST_F(NativeSignatureTest, NativeSignatureTest002, TestSize.Level0)
80 {
81 OH_Crypto_ErrCode res = CRYPTO_SUCCESS;
82 OH_CryptoVerify *verify = nullptr;
83 res = OH_CryptoVerify_Create("RSA512|NoPadding|NoHash|Recover", &verify);
84 EXPECT_EQ(res, CRYPTO_SUCCESS);
85 EXPECT_NE(verify, nullptr);
86 EXPECT_NE(OH_CryptoVerify_Destroy, nullptr);
87 EXPECT_NE(OH_CryptoVerify_Init, nullptr);
88 EXPECT_NE(OH_CryptoVerify_Update, nullptr);
89 EXPECT_NE(OH_CryptoVerify_Final, nullptr);
90 EXPECT_NE(OH_CryptoVerify_Recover, nullptr);
91 OH_CryptoVerify_Destroy(verify);
92 }
93
94 HWTEST_F(NativeSignatureTest, NativeSignatureTest003, TestSize.Level0)
95 {
96 OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
97 OH_CryptoKeyPair *keyPair = nullptr;
98 OH_CryptoVerify *verify = nullptr;
99
100 uint8_t plainText[] = {
101 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c};
102 Crypto_DataBlob msgBlob = {
103 .data = reinterpret_cast<uint8_t *>(plainText),
104 .len = sizeof(plainText)};
105
106 uint8_t pubKeyText[] = {
107 0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
108 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00, 0x03, 0x4d, 0xe4, 0xbb, 0x11, 0x10,
109 0x1a, 0xd2, 0x05, 0x74, 0xf1, 0x0b, 0xb4, 0x75, 0x57, 0xf4, 0x3e, 0x55, 0x14, 0x17, 0x05, 0x4a,
110 0xb2, 0xfb, 0x8c, 0x84, 0x64, 0x38, 0x02, 0xa0, 0x2a, 0xa6, 0xf0};
111
112 Crypto_DataBlob keyBlob = {
113 .data = reinterpret_cast<uint8_t *>(pubKeyText),
114 .len = sizeof(pubKeyText)};
115
116 uint8_t signText[] = {
117 0x30, 0x44, 0x02, 0x20, 0x21, 0x89, 0x99, 0xb1, 0x56, 0x4e, 0x3a, 0x2c, 0x16, 0x08, 0xb5, 0x8a,
118 0x06, 0x6f, 0x67, 0x47, 0x1b, 0x04, 0x18, 0x7d, 0x53, 0x2d, 0xba, 0x00, 0x38, 0xd9, 0xe3, 0xe7,
119 0x8c, 0xcf, 0x76, 0x83, 0x02, 0x20, 0x13, 0x54, 0x84, 0x9d, 0x73, 0x40, 0xc3, 0x92, 0x66, 0xdc,
120 0x3e, 0xc9, 0xf1, 0x4c, 0x33, 0x84, 0x2a, 0x76, 0xaf, 0xc6, 0x61, 0x84, 0x5c, 0xae, 0x4b, 0x0d,
121 0x3c, 0xb0, 0xc8, 0x04, 0x89, 0x71};
122
123 Crypto_DataBlob signBlob = {
124 .data = reinterpret_cast<uint8_t *>(signText),
125 .len = sizeof(signText)};
126
127 // keypair
128 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"ECC256", &keyCtx), CRYPTO_SUCCESS);
129 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_DER, &keyBlob, nullptr, &keyPair), CRYPTO_SUCCESS);
130 OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
131 // verify
132 ASSERT_EQ(OH_CryptoVerify_Create((const char *)"ECC|SHA256", &verify), CRYPTO_SUCCESS);
133 ASSERT_EQ(OH_CryptoVerify_Init(verify, pubKey), CRYPTO_SUCCESS);
134 ASSERT_TRUE(OH_CryptoVerify_Final(verify, &msgBlob, &signBlob));
135
136 OH_CryptoVerify_Destroy(verify);
137 OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
138 OH_CryptoKeyPair_Destroy(keyPair);
139 }
140
141 HWTEST_F(NativeSignatureTest, NativeSignatureTest_Sign001, TestSize.Level0)
142 {
143 OH_CryptoAsymKeyGenerator *generator = nullptr;
144 OH_Crypto_ErrCode res = OH_CryptoAsymKeyGenerator_Create("RSA2048|PRIMES_2", &generator);
145 EXPECT_EQ(res, CRYPTO_SUCCESS);
146
147 OH_CryptoKeyPair *keyPair = nullptr;
148 res = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPair);
149 EXPECT_EQ(res, CRYPTO_SUCCESS);
150
151 OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyPair);
152 ASSERT_NE(privKey, nullptr);
153
154 OH_CryptoSign *sign = nullptr;
155 res = OH_CryptoSign_Create("RSA2048|PKCS1|SHA256", &sign);
156 EXPECT_EQ(res, CRYPTO_SUCCESS);
157 ASSERT_NE(sign, nullptr);
158
159 const char *algoName = OH_CryptoSign_GetAlgoName(sign);
160 ASSERT_NE(algoName, nullptr);
161 EXPECT_STREQ(algoName, "RSA2048|PKCS1|SHA256");
162
163 res = OH_CryptoSign_Init(sign, privKey);
164 EXPECT_EQ(res, CRYPTO_SUCCESS);
165
166 uint8_t plainText[] = {
167 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c};
168 Crypto_DataBlob msgBlob = {
169 .data = reinterpret_cast<uint8_t *>(plainText),
170 .len = sizeof(plainText)};
171
172 res = OH_CryptoSign_Update(sign, &msgBlob);
173 EXPECT_EQ(res, CRYPTO_SUCCESS);
174
175 Crypto_DataBlob signBlob = {.data = nullptr, .len = 0};
176 res = OH_CryptoSign_Final(sign, nullptr, &signBlob);
177 EXPECT_EQ(res, CRYPTO_SUCCESS);
178 ASSERT_NE(signBlob.data, nullptr);
179 ASSERT_GT(signBlob.len, 0);
180
181 // Clean up
182 free(signBlob.data);
183 OH_CryptoSign_Destroy(sign);
184 OH_CryptoKeyPair_Destroy(keyPair);
185 OH_CryptoAsymKeyGenerator_Destroy(generator);
186 }
187
188 HWTEST_F(NativeSignatureTest, NativeSignatureTest_Sign002, TestSize.Level0)
189 {
190 OH_CryptoSign *sign = nullptr;
191 OH_Crypto_ErrCode res = OH_CryptoSign_Create("RSA2048|PSS|SHA256|MGF1_SHA256", &sign);
192 EXPECT_EQ(res, CRYPTO_SUCCESS);
193 ASSERT_NE(sign, nullptr);
194
195 const char *algoName = OH_CryptoSign_GetAlgoName(sign);
196 ASSERT_NE(algoName, nullptr);
197 EXPECT_STREQ(algoName, "RSA2048|PSS|SHA256|MGF1_SHA256");
198
199 uint8_t buf[] = {32};
200 Crypto_DataBlob value = {.data = reinterpret_cast<uint8_t *>(buf), .len = sizeof(buf)};
201 res = OH_CryptoSign_SetParam(sign, CRYPTO_PSS_SALT_LEN_INT, &value);
202 EXPECT_EQ(res, CRYPTO_PARAMETER_CHECK_FAILED);
203
204 Crypto_DataBlob outValue = {.data = nullptr, .len = 0};
205 res = OH_CryptoSign_GetParam(sign, CRYPTO_PSS_SALT_LEN_INT, &outValue);
206 EXPECT_EQ(res, CRYPTO_PARAMETER_CHECK_FAILED);
207
208 OH_CryptoSign_Destroy(sign);
209 }
210
211 HWTEST_F(NativeSignatureTest, NativeSignatureTest_Sign003, TestSize.Level0)
212 {
213 OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
214 OH_CryptoKeyPair *keyPair = nullptr;
215 OH_CryptoSign *sign = nullptr;
216
217 uint8_t plainText[] = {
218 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c};
219 Crypto_DataBlob msgBlob = {
220 .data = reinterpret_cast<uint8_t *>(plainText),
221 .len = sizeof(plainText)};
222
223 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"ECC256", &keyCtx), CRYPTO_SUCCESS);
224 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(keyCtx, &keyPair), CRYPTO_SUCCESS);
225
226 OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyPair);
227 ASSERT_NE(privKey, nullptr);
228
229 ASSERT_EQ(OH_CryptoSign_Create((const char *)"ECC|SHA256", &sign), CRYPTO_SUCCESS);
230 ASSERT_EQ(OH_CryptoSign_Init(sign, privKey), CRYPTO_SUCCESS);
231
232 ASSERT_EQ(OH_CryptoSign_Update(sign, &msgBlob), CRYPTO_SUCCESS);
233
234 Crypto_DataBlob signBlob = {.data = nullptr, .len = 0};
235 ASSERT_EQ(OH_CryptoSign_Final(sign, nullptr, &signBlob), CRYPTO_SUCCESS);
236 ASSERT_NE(signBlob.data, nullptr);
237 ASSERT_GT(signBlob.len, 0);
238
239 // Clean up
240 free(signBlob.data);
241 OH_CryptoSign_Destroy(sign);
242 OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
243 OH_CryptoKeyPair_Destroy(keyPair);
244 }
245
246 HWTEST_F(NativeSignatureTest, NativeSignatureTest_SignVerify001, TestSize.Level0)
247 {
248 OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
249 OH_CryptoKeyPair *keyPair = nullptr;
250 OH_CryptoSign *sign = nullptr;
251
252 uint8_t plainText[] = {
253 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c};
254 Crypto_DataBlob msgBlob = {
255 .data = reinterpret_cast<uint8_t *>(plainText),
256 .len = sizeof(plainText)};
257
258 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"RSA2048|PRIMES_2", &keyCtx), CRYPTO_SUCCESS);
259 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(keyCtx, &keyPair), CRYPTO_SUCCESS);
260
261 OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyPair);
262 ASSERT_NE(privKey, nullptr);
263
264 ASSERT_EQ(OH_CryptoSign_Create((const char *)"RSA1024|PSS|SHA256|MGF1_SHA512", &sign), CRYPTO_SUCCESS);
265 ASSERT_EQ(OH_CryptoSign_Init(sign, privKey), CRYPTO_SUCCESS);
266
267 ASSERT_EQ(OH_CryptoSign_Update(sign, &msgBlob), CRYPTO_SUCCESS);
268
269 Crypto_DataBlob signBlob = {.data = nullptr, .len = 0};
270 ASSERT_EQ(OH_CryptoSign_Final(sign, nullptr, &signBlob), CRYPTO_SUCCESS);
271 ASSERT_NE(signBlob.data, nullptr);
272 ASSERT_GT(signBlob.len, 0);
273
274 OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(keyPair);
275 OH_CryptoVerify *verify = nullptr;
276 OH_Crypto_ErrCode res = OH_CryptoVerify_Create("RSA1024|PSS|SHA256|MGF1_SHA512", &verify);
277 EXPECT_EQ(res, CRYPTO_SUCCESS);
278
279 const char *algoName = OH_CryptoVerify_GetAlgoName(verify);
280 ASSERT_NE(algoName, nullptr);
281
282 int32_t buf[] = {32};
283 Crypto_DataBlob value = {.data = reinterpret_cast<uint8_t *>(buf), .len = sizeof(buf)};
284 res = OH_CryptoVerify_SetParam(verify, CRYPTO_PSS_SALT_LEN_INT, &value);
285 EXPECT_EQ(res, CRYPTO_SUCCESS);
286 res = OH_CryptoVerify_Init(verify, pubkey);
287 EXPECT_EQ(res, CRYPTO_SUCCESS);
288 bool result = OH_CryptoVerify_Final(verify, &msgBlob, &signBlob);
289 EXPECT_NE(result, 1);
290
291 HcfBlobDataClearAndFree((HcfBlob *)&signBlob);
292 OH_CryptoVerify_Destroy(verify);
293 OH_CryptoSign_Destroy(sign);
294 OH_CryptoKeyPair_Destroy(keyPair);
295 OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
296 }
297
298 HWTEST_F(NativeSignatureTest, NativeSignatureTest_DerToRS001, TestSize.Level0)
299 {
300 OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
301 OH_CryptoKeyPair *keyPair = nullptr;
302 OH_CryptoSign *sign = nullptr;
303
304 uint8_t plainText[] = {
305 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c};
306 Crypto_DataBlob msgBlob = {
307 .data = reinterpret_cast<uint8_t *>(plainText),
308 .len = sizeof(plainText)};
309
310 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"SM2_256", &keyCtx), CRYPTO_SUCCESS);
311 ASSERT_EQ(OH_CryptoAsymKeyGenerator_Generate(keyCtx, &keyPair), CRYPTO_SUCCESS);
312
313 OH_CryptoPrivKey *privKey = OH_CryptoKeyPair_GetPrivKey(keyPair);
314 ASSERT_NE(privKey, nullptr);
315
316 ASSERT_EQ(OH_CryptoSign_Create((const char *)"SM2|SM3", &sign), CRYPTO_SUCCESS);
317 ASSERT_EQ(OH_CryptoSign_Init(sign, privKey), CRYPTO_SUCCESS);
318
319 ASSERT_EQ(OH_CryptoSign_Update(sign, &msgBlob), CRYPTO_SUCCESS);
320
321 Crypto_DataBlob signBlob = {.data = nullptr, .len = 0};
322 ASSERT_EQ(OH_CryptoSign_Final(sign, nullptr, &signBlob), CRYPTO_SUCCESS);
323 ASSERT_NE(signBlob.data, nullptr);
324 ASSERT_GT(signBlob.len, 0);
325
326 OH_CryptoEccSignatureSpec *eccSignSpec = nullptr;
327 ASSERT_EQ(OH_CryptoEccSignatureSpec_Create(&signBlob, &eccSignSpec), CRYPTO_SUCCESS);
328 ASSERT_NE(eccSignSpec, nullptr);
329 Crypto_DataBlob r = {.data = nullptr, .len = 0};
330 Crypto_DataBlob s = {.data = nullptr, .len = 0};
331 ASSERT_EQ(OH_CryptoEccSignatureSpec_GetRAndS(eccSignSpec, &r, &s), CRYPTO_SUCCESS);
332 ASSERT_NE(r.data, nullptr);
333 ASSERT_NE(s.data, nullptr);
334 OH_CryptoEccSignatureSpec_Destroy(eccSignSpec);
335 OH_CryptoEccSignatureSpec *eccSignSpec1 = nullptr;
336 Crypto_DataBlob signBlob1 = {.data = nullptr, .len = 0};
337 ASSERT_EQ(OH_CryptoEccSignatureSpec_Create(nullptr, &eccSignSpec1), CRYPTO_SUCCESS);
338 ASSERT_EQ(OH_CryptoEccSignatureSpec_SetRAndS(eccSignSpec1, &r, &s), CRYPTO_SUCCESS);
339 ASSERT_EQ(OH_CryptoEccSignatureSpec_Encode(eccSignSpec1, &signBlob1), CRYPTO_SUCCESS);
340 ASSERT_NE(signBlob1.data, nullptr);
341 ASSERT_GT(signBlob1.len, 0);
342
343
344 // Clean up
345 HcfBlobDataClearAndFree((HcfBlob *)&signBlob);
346 HcfBlobDataClearAndFree((HcfBlob *)&signBlob1);
347 HcfBlobDataClearAndFree((HcfBlob *)&r);
348 HcfBlobDataClearAndFree((HcfBlob *)&s);
349 OH_CryptoSign_Destroy(sign);
350 OH_CryptoEccSignatureSpec_Destroy(eccSignSpec1);
351 OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
352 OH_CryptoKeyPair_Destroy(keyPair);
353 }
354 }