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 "memory.h"
22 #include "memory_mock.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace {
28 class NativeSignatureTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase()36 void NativeSignatureTest::SetUpTestCase() {}
TearDownTestCase()37 void NativeSignatureTest::TearDownTestCase() {}
38 
SetUp()39 void NativeSignatureTest::SetUp() // add init here, this will be called before test.
40 {
41 }
42 
TearDown()43 void NativeSignatureTest::TearDown() // add destroy here, this will be called when test case done.
44 {
45 }
46 
47 HWTEST_F(NativeSignatureTest, NativeSignatureTest001, TestSize.Level0)
48 {
49     OH_CryptoAsymKeyGenerator *generator = nullptr;
50     OH_Crypto_ErrCode res = OH_CryptoAsymKeyGenerator_Create("RSA2048|PRIMES_2", &generator);
51 
52     OH_CryptoKeyPair *keyPair = nullptr;
53     res = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPair);
54     EXPECT_EQ(res, CRYPTO_SUCCESS);
55 
56     OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(keyPair);
57     OH_CryptoVerify *verify = nullptr;
58     res = OH_CryptoVerify_Create("RSA1024|PSS|SHA256|MGF1_SHA512", &verify);
59     EXPECT_EQ(res, CRYPTO_SUCCESS);
60 
61     const char *algoName = OH_CryptoVerify_GetAlgoName(verify);
62     ASSERT_NE(algoName, nullptr);
63 
64     int32_t buf[] = {32};
65     Crypto_DataBlob value = { .data =  reinterpret_cast<uint8_t *>(buf), .len = sizeof(buf) };
66     res = OH_CryptoVerify_SetParam(verify, CRYPTO_PSS_SALT_LEN_INT, &value);
67     EXPECT_EQ(res, CRYPTO_SUCCESS);
68     res = OH_CryptoVerify_Init(verify, pubkey);
69     EXPECT_EQ(res, CRYPTO_SUCCESS);
70     res = OH_CryptoVerify_Update(verify, nullptr);
71     EXPECT_NE(res, 1);
72 
73     OH_CryptoVerify_Destroy(verify);
74     OH_CryptoKeyPair_Destroy(keyPair);
75     OH_CryptoAsymKeyGenerator_Destroy(generator);
76 }
77 
78 HWTEST_F(NativeSignatureTest, NativeSignatureTest002, TestSize.Level0)
79 {
80     OH_Crypto_ErrCode res = CRYPTO_SUCCESS;
81     OH_CryptoVerify *verify = nullptr;
82     res = OH_CryptoVerify_Create("RSA512|NoPadding|NoHash|Recover", &verify);
83     EXPECT_EQ(res, CRYPTO_SUCCESS);
84     EXPECT_NE(verify, nullptr);
85     EXPECT_NE(OH_CryptoVerify_Destroy, nullptr);
86     EXPECT_NE(OH_CryptoVerify_Init, nullptr);
87     EXPECT_NE(OH_CryptoVerify_Update, nullptr);
88     EXPECT_NE(OH_CryptoVerify_Final, nullptr);
89     EXPECT_NE(OH_CryptoVerify_Recover, nullptr);
90     OH_CryptoVerify_Destroy(verify);
91 }
92 
93 HWTEST_F(NativeSignatureTest, NativeSignatureTest003, TestSize.Level0)
94 {
95     OH_CryptoAsymKeyGenerator *keyCtx = nullptr;
96     OH_CryptoKeyPair *keyPair = nullptr;
97     OH_CryptoVerify *verify = nullptr;
98 
99     uint8_t plainText[] = {
100         0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c
101     };
102     Crypto_DataBlob msgBlob = {
103         .data = reinterpret_cast<uint8_t *>(plainText),
104         .len = sizeof(plainText)
105     };
106 
107     uint8_t pubKeyText[] = {
108         0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
109         0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00, 0x03, 0x4d, 0xe4, 0xbb, 0x11, 0x10,
110         0x1a, 0xd2, 0x05, 0x74, 0xf1, 0x0b, 0xb4, 0x75, 0x57, 0xf4, 0x3e, 0x55, 0x14, 0x17, 0x05, 0x4a,
111         0xb2, 0xfb, 0x8c, 0x84, 0x64, 0x38, 0x02, 0xa0, 0x2a, 0xa6, 0xf0
112     };
113 
114     Crypto_DataBlob keyBlob = {
115         .data = reinterpret_cast<uint8_t *>(pubKeyText),
116         .len = sizeof(pubKeyText)
117     };
118 
119     uint8_t signText[] = {
120         0x30, 0x44, 0x02, 0x20, 0x21, 0x89, 0x99, 0xb1, 0x56, 0x4e, 0x3a, 0x2c, 0x16, 0x08, 0xb5, 0x8a,
121         0x06, 0x6f, 0x67, 0x47, 0x1b, 0x04, 0x18, 0x7d, 0x53, 0x2d, 0xba, 0x00, 0x38, 0xd9, 0xe3, 0xe7,
122         0x8c, 0xcf, 0x76, 0x83, 0x02, 0x20, 0x13, 0x54, 0x84, 0x9d, 0x73, 0x40, 0xc3, 0x92, 0x66, 0xdc,
123         0x3e, 0xc9, 0xf1, 0x4c, 0x33, 0x84, 0x2a, 0x76, 0xaf, 0xc6, 0x61, 0x84, 0x5c, 0xae, 0x4b, 0x0d,
124         0x3c, 0xb0, 0xc8, 0x04, 0x89, 0x71
125     };
126 
127     Crypto_DataBlob signBlob = {
128         .data = reinterpret_cast<uint8_t *>(signText),
129         .len = sizeof(signText)
130     };
131 
132     // keypair
133     ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"ECC256", &keyCtx), CRYPTO_SUCCESS);
134     ASSERT_EQ(OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_DER, &keyBlob, nullptr, &keyPair), CRYPTO_SUCCESS);
135     OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair);
136     // verify
137     ASSERT_EQ(OH_CryptoVerify_Create((const char *)"ECC|SHA256", &verify), CRYPTO_SUCCESS);
138     ASSERT_EQ(OH_CryptoVerify_Init(verify, pubKey), CRYPTO_SUCCESS);
139     ASSERT_TRUE(OH_CryptoVerify_Final(verify, &msgBlob, &signBlob));
140 
141     OH_CryptoVerify_Destroy(verify);
142     OH_CryptoAsymKeyGenerator_Destroy(keyCtx);
143     OH_CryptoKeyPair_Destroy(keyPair);
144 }
145 }