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 "hcfsigncreate_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
TestSign(void)31 static void TestSign(void)
32 {
33 HcfAsyKeyGenerator *generator = nullptr;
34 HcfResult res = HcfAsyKeyGeneratorCreate("ECC384", &generator);
35 if (res != HCF_SUCCESS) {
36 return;
37 }
38
39 HcfKeyPair *ecc384KeyPair = nullptr;
40 res = generator->generateKeyPair(generator, nullptr, &ecc384KeyPair);
41 HcfObjDestroy(generator);
42 if (res != HCF_SUCCESS) {
43 return;
44 }
45
46 HcfSign *sign = nullptr;
47 res = HcfSignCreate("ECC384|SHA384", &sign);
48 if (res != HCF_SUCCESS) {
49 HcfObjDestroy(ecc384KeyPair);
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, ecc384KeyPair->priKey);
57 (void)sign->update(sign, &mockInput);
58 HcfObjDestroy(ecc384KeyPair);
59 HcfObjDestroy(sign);
60 }
61
TestSignSm2(void)62 static void TestSignSm2(void)
63 {
64 HcfAsyKeyGenerator *generator = nullptr;
65 HcfResult res = HcfAsyKeyGeneratorCreate("SM2_256", &generator);
66 if (res != HCF_SUCCESS) {
67 return;
68 }
69
70 HcfKeyPair *sm2256KeyPair = nullptr;
71 res = generator->generateKeyPair(generator, nullptr, &sm2256KeyPair);
72 HcfObjDestroy(generator);
73 if (res != HCF_SUCCESS) {
74 return;
75 }
76
77 HcfSign *sign = nullptr;
78 res = HcfSignCreate("SM2_256|SM3", &sign);
79 if (res != HCF_SUCCESS) {
80 HcfObjDestroy(sm2256KeyPair);
81 return;
82 }
83 static HcfBlob mockInput = {
84 .data = reinterpret_cast<uint8_t *>(g_mockMessage),
85 .len = INPUT_MSG_LEN
86 };
87 (void)sign->init(sign, nullptr, sm2256KeyPair->priKey);
88 (void)sign->update(sign, &mockInput);
89 HcfObjDestroy(sm2256KeyPair);
90 HcfObjDestroy(sign);
91 }
92
TestSignBrainpool(void)93 static void TestSignBrainpool(void)
94 {
95 HcfAsyKeyGenerator *generator = nullptr;
96 HcfResult res = HcfAsyKeyGeneratorCreate("ECC_BrainPoolP160r1", &generator);
97 if (res != HCF_SUCCESS) {
98 return;
99 }
100
101 HcfKeyPair *brainPoolP160r1KeyPair = nullptr;
102 res = generator->generateKeyPair(generator, nullptr, &brainPoolP160r1KeyPair);
103 HcfObjDestroy(generator);
104 if (res != HCF_SUCCESS) {
105 return;
106 }
107
108 HcfSign *sign = nullptr;
109 res = HcfSignCreate("ECC_BrainPoolP160r1|SHA1", &sign);
110 if (res != HCF_SUCCESS) {
111 HcfObjDestroy(brainPoolP160r1KeyPair);
112 return;
113 }
114 static HcfBlob mockInput = {
115 .data = reinterpret_cast<uint8_t *>(g_mockMessage),
116 .len = INPUT_MSG_LEN
117 };
118 (void)sign->init(sign, nullptr, brainPoolP160r1KeyPair->priKey);
119 (void)sign->update(sign, &mockInput);
120 HcfObjDestroy(brainPoolP160r1KeyPair);
121 HcfObjDestroy(sign);
122 }
123
TestSignEd25519(void)124 static void TestSignEd25519(void)
125 {
126 HcfAsyKeyGenerator *generator = nullptr;
127 HcfResult res = HcfAsyKeyGeneratorCreate("Ed25519", &generator);
128 if (res != HCF_SUCCESS) {
129 return;
130 }
131
132 HcfKeyPair *ed25519KeyPair = nullptr;
133 res = generator->generateKeyPair(generator, nullptr, &ed25519KeyPair);
134 HcfObjDestroy(generator);
135 if (res != HCF_SUCCESS) {
136 return;
137 }
138
139 HcfSign *sign = nullptr;
140 res = HcfSignCreate("Ed25519", &sign);
141 if (res != HCF_SUCCESS) {
142 HcfObjDestroy(ed25519KeyPair);
143 return;
144 }
145 static HcfBlob mockInput = {
146 .data = reinterpret_cast<uint8_t *>(g_mockMessage),
147 .len = INPUT_MSG_LEN
148 };
149 (void)sign->init(sign, nullptr, ed25519KeyPair->priKey);
150 (void)sign->update(sign, &mockInput);
151 HcfObjDestroy(ed25519KeyPair);
152 HcfObjDestroy(sign);
153 }
154
HcfSignCreateFuzzTest(const uint8_t * data,size_t size)155 bool HcfSignCreateFuzzTest(const uint8_t* data, size_t size)
156 {
157 TestSign();
158 TestSignSm2();
159 TestSignBrainpool();
160 TestSignEd25519();
161 HcfSign *sign = nullptr;
162 std::string algoName(reinterpret_cast<const char *>(data), size);
163 HcfResult res = HcfSignCreate(algoName.c_str(), &sign);
164 if (res != HCF_SUCCESS) {
165 return false;
166 }
167 HcfObjDestroy(sign);
168 return true;
169 }
170 }
171
172 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)173 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
174 {
175 /* Run your code on data */
176 OHOS::HcfSignCreateFuzzTest(data, size);
177 return 0;
178 }
179