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 "hcfmdcreate_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21
22 #include "blob.h"
23 #include "md.h"
24 #include "result.h"
25
26 namespace OHOS {
TestMd(const uint8_t * data,size_t size)27 static void TestMd(const uint8_t* data, size_t size)
28 {
29 HcfMd *mdObj = nullptr;
30 HcfResult res = HcfMdCreate("SHA1", &mdObj);
31 if (res != HCF_SUCCESS) {
32 return;
33 }
34 HcfBlob inBlob = {.data = const_cast<uint8_t *>(data), .len = size};
35 (void)mdObj->update(mdObj, &inBlob);
36 HcfBlob outBlob = { 0 };
37 (void)mdObj->doFinal(mdObj, &outBlob);
38 (void)mdObj->getAlgoName(mdObj);
39 (void)mdObj->getMdLength(mdObj);
40 HcfBlobDataClearAndFree(&outBlob);
41 HcfObjDestroy(mdObj);
42 }
43
HcMdCreateFuzzTest(const uint8_t * data,size_t size)44 bool HcMdCreateFuzzTest(const uint8_t* data, size_t size)
45 {
46 TestMd(data, size);
47 HcfMd *mdObj = nullptr;
48 std::string alg(reinterpret_cast<const char *>(data), size);
49 HcfResult res = HcfMdCreate(alg.c_str(), &mdObj);
50 if (res != HCF_SUCCESS) {
51 return false;
52 }
53 HcfObjDestroy(mdObj);
54 return true;
55 }
56 }
57
58 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
60 {
61 /* Run your code on data */
62 OHOS::HcMdCreateFuzzTest(data, size);
63 return 0;
64 }
65