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 "hcfverifycreate_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21
22 #include "asy_key_generator.h"
23 #include "blob.h"
24 #include "result.h"
25 #include "signature.h"
26
27 namespace OHOS {
28 static char g_mockMessage[] = "hello world";
29 const int INPUT_MSG_LEN = 12;
30
TestVerify(void)31 static void TestVerify(void)
32 {
33 HcfAsyKeyGenerator *generator = nullptr;
34 HcfResult res = HcfAsyKeyGeneratorCreate("ECC224", &generator);
35 if (res != HCF_SUCCESS) {
36 return;
37 }
38
39 HcfKeyPair *ecc224KeyPair = nullptr;
40 res = generator->generateKeyPair(generator, nullptr, &ecc224KeyPair);
41 HcfObjDestroy(generator);
42 if (res != HCF_SUCCESS) {
43 return;
44 }
45
46 HcfSign *sign = nullptr;
47 res = HcfSignCreate("ECC224|SHA384", &sign);
48 if (res != HCF_SUCCESS) {
49 HcfObjDestroy(ecc224KeyPair);
50 return;
51 }
52 static HcfBlob mockInput = {
53 .data = reinterpret_cast<uint8_t *>(g_mockMessage),
54 .len = INPUT_MSG_LEN
55 };
56 (void)sign->init(sign, nullptr, ecc224KeyPair->priKey);
57 (void)sign->update(sign, &mockInput);
58
59 HcfVerify *verify = nullptr;
60 res = HcfVerifyCreate("ECC224|SHA384", &verify);
61 if (res != HCF_SUCCESS) {
62 HcfObjDestroy(ecc224KeyPair);
63 HcfObjDestroy(sign);
64 return;
65 }
66 HcfBlob out = {
67 .data = nullptr,
68 .len = 0
69 };
70 (void)sign->sign(sign, nullptr, &out);
71 (void)verify->init(verify, nullptr, ecc224KeyPair->pubKey);
72 (void)verify->update(verify, &mockInput);
73 (void)verify->verify(verify, nullptr, &out);
74 HcfObjDestroy(ecc224KeyPair);
75 HcfObjDestroy(sign);
76 HcfBlobDataFree(&out);
77 HcfObjDestroy(verify);
78 }
79
TestVerifySm2(void)80 static void TestVerifySm2(void)
81 {
82 HcfAsyKeyGenerator *generator = nullptr;
83 HcfResult res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
84 if (res != HCF_SUCCESS) {
85 return;
86 }
87
88 HcfKeyPair *sm2256KeyPair = nullptr;
89 res = generator->generateKeyPair(generator, nullptr, &sm2256KeyPair);
90 HcfObjDestroy(generator);
91 if (res != HCF_SUCCESS) {
92 return;
93 }
94
95 HcfSign *sign = nullptr;
96 res = HcfSignCreate("SM2_256|SM3", &sign);
97 if (res != HCF_SUCCESS) {
98 HcfObjDestroy(sm2256KeyPair);
99 return;
100 }
101 static HcfBlob mockInput = {
102 .data = reinterpret_cast<uint8_t *>(g_mockMessage),
103 .len = INPUT_MSG_LEN
104 };
105 (void)sign->init(sign, nullptr, sm2256KeyPair->priKey);
106 (void)sign->update(sign, &mockInput);
107
108 HcfVerify *verify = nullptr;
109 res = HcfVerifyCreate("SM2_256|SM3", &verify);
110 if (res != HCF_SUCCESS) {
111 HcfObjDestroy(sm2256KeyPair);
112 HcfObjDestroy(sign);
113 return;
114 }
115 HcfBlob out = {
116 .data = nullptr,
117 .len = 0
118 };
119 (void)sign->sign(sign, nullptr, &out);
120 (void)verify->init(verify, nullptr, sm2256KeyPair->pubKey);
121 (void)verify->update(verify, &mockInput);
122 (void)verify->verify(verify, nullptr, &out);
123 HcfObjDestroy(sm2256KeyPair);
124 HcfObjDestroy(sign);
125 HcfBlobDataFree(&out);
126 HcfObjDestroy(verify);
127 }
128
TestVerifyBrainpool(void)129 static void TestVerifyBrainpool(void)
130 {
131 HcfAsyKeyGenerator *generator = nullptr;
132 HcfResult res = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP160r1", &generator);
133 if (res != HCF_SUCCESS) {
134 return;
135 }
136
137 HcfKeyPair *brainPoolP160r1KeyPair = nullptr;
138 res = generator->generateKeyPair(generator, nullptr, &brainPoolP160r1KeyPair);
139 HcfObjDestroy(generator);
140 if (res != HCF_SUCCESS) {
141 return;
142 }
143
144 HcfSign *sign = nullptr;
145 res = HcfSignCreate("ECC_BrainPoolP160r1|SHA1", &sign);
146 if (res != HCF_SUCCESS) {
147 HcfObjDestroy(brainPoolP160r1KeyPair);
148 return;
149 }
150 static HcfBlob mockInput = {
151 .data = reinterpret_cast<uint8_t *>(g_mockMessage),
152 .len = INPUT_MSG_LEN
153 };
154 (void)sign->init(sign, nullptr, brainPoolP160r1KeyPair->priKey);
155 (void)sign->update(sign, &mockInput);
156
157 HcfVerify *verify = nullptr;
158 res = HcfVerifyCreate("ECC_BrainPoolP160r1|SHA1", &verify);
159 if (res != HCF_SUCCESS) {
160 HcfObjDestroy(brainPoolP160r1KeyPair);
161 HcfObjDestroy(sign);
162 return;
163 }
164 HcfBlob out = {
165 .data = nullptr,
166 .len = 0
167 };
168 (void)sign->sign(sign, nullptr, &out);
169 (void)verify->init(verify, nullptr, brainPoolP160r1KeyPair->pubKey);
170 (void)verify->update(verify, &mockInput);
171 (void)verify->verify(verify, nullptr, &out);
172 HcfObjDestroy(brainPoolP160r1KeyPair);
173 HcfObjDestroy(sign);
174 HcfBlobDataFree(&out);
175 HcfObjDestroy(verify);
176 }
177
TestVerifyEd25519(void)178 static void TestVerifyEd25519(void)
179 {
180 HcfAsyKeyGenerator *generator = nullptr;
181 HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator);
182 if (res != HCF_SUCCESS) {
183 return;
184 }
185
186 HcfKeyPair *ed25519KeyPair = nullptr;
187 res = generator->generateKeyPair(generator, nullptr, &ed25519KeyPair);
188 HcfObjDestroy(generator);
189 if (res != HCF_SUCCESS) {
190 return;
191 }
192
193 HcfSign *sign = nullptr;
194 res = HcfSignCreate("Ed25519", &sign);
195 if (res != HCF_SUCCESS) {
196 HcfObjDestroy(ed25519KeyPair);
197 return;
198 }
199 static HcfBlob mockInput = {
200 .data = reinterpret_cast<uint8_t *>(g_mockMessage),
201 .len = INPUT_MSG_LEN
202 };
203 (void)sign->init(sign, nullptr, ed25519KeyPair->priKey);
204 (void)sign->update(sign, &mockInput);
205
206 HcfVerify *verify = nullptr;
207 res = HcfVerifyCreate("Ed25519", &verify);
208 if (res != HCF_SUCCESS) {
209 HcfObjDestroy(ed25519KeyPair);
210 HcfObjDestroy(sign);
211 return;
212 }
213 HcfBlob out = {
214 .data = nullptr,
215 .len = 0
216 };
217 (void)sign->sign(sign, nullptr, &out);
218 (void)verify->init(verify, nullptr, ed25519KeyPair->pubKey);
219 (void)verify->update(verify, &mockInput);
220 (void)verify->verify(verify, nullptr, &out);
221 HcfObjDestroy(ed25519KeyPair);
222 HcfObjDestroy(sign);
223 HcfBlobDataFree(&out);
224 HcfObjDestroy(verify);
225 }
226
HcfVerifyCreateFuzzTest(const uint8_t * data,size_t size)227 bool HcfVerifyCreateFuzzTest(const uint8_t* data, size_t size)
228 {
229 TestVerify();
230 TestVerifySm2();
231 TestVerifyBrainpool();
232 TestVerifyEd25519();
233 HcfVerify *verify = nullptr;
234 std::string algoName(reinterpret_cast<const char *>(data), size);
235 HcfResult res = HcfVerifyCreate(algoName.c_str(), &verify);
236 if (res != HCF_SUCCESS) {
237 return false;
238 }
239 HcfObjDestroy(verify);
240 return true;
241 }
242 }
243
244 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)245 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
246 {
247 /* Run your code on data */
248 OHOS::HcfVerifyCreateFuzzTest(data, size);
249 return 0;
250 }
251