1 /*
2 * Copyright (C) 2023 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 "ecckeyutil_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21
22 #include "ecc_key_util.h"
23 #include "blob.h"
24 #include "detailed_ecc_key_params.h"
25 #include "result.h"
26
27 using namespace std;
28
29 namespace OHOS {
30 static bool g_testFlag = true;
TestEccKey(void)31 static void TestEccKey(void)
32 {
33 HcfEccCommParamsSpec *returnCommonParamSpec = nullptr;
34 int32_t res = HcfEccKeyUtilCreate("NID_secp224r1", &returnCommonParamSpec);
35 if (res != HCF_SUCCESS) {
36 return;
37 }
38 FreeEccCommParamsSpec(returnCommonParamSpec);
39 }
40
TestSm2Key(void)41 static void TestSm2Key(void)
42 {
43 HcfEccCommParamsSpec *returnCommonParamSpec = nullptr;
44 int32_t res = HcfEccKeyUtilCreate("NID_sm2", &returnCommonParamSpec);
45 if (res != HCF_SUCCESS) {
46 return;
47 }
48 FreeEccCommParamsSpec(returnCommonParamSpec);
49 }
50
TestBrainpoolKey(void)51 static void TestBrainpoolKey(void)
52 {
53 HcfEccCommParamsSpec *returnCommonParamSpec = nullptr;
54 int32_t res = HcfEccKeyUtilCreate("NID_brainpoolP160r1", &returnCommonParamSpec);
55 if (res != HCF_SUCCESS) {
56 return;
57 }
58 FreeEccCommParamsSpec(returnCommonParamSpec);
59 }
60
EccKeyUtilFuzzTest(const uint8_t * data,size_t size)61 bool EccKeyUtilFuzzTest(const uint8_t* data, size_t size)
62 {
63 if (g_testFlag) {
64 TestEccKey();
65 TestSm2Key();
66 TestBrainpoolKey();
67 g_testFlag = false;
68 }
69 HcfEccCommParamsSpec *returnCommonParamSpec = nullptr;
70 std::string algoName(reinterpret_cast<const char *>(data), size);
71 HcfResult res = HcfEccKeyUtilCreate(algoName.c_str(), &returnCommonParamSpec);
72 if (res != HCF_SUCCESS) {
73 return false;
74 }
75 FreeEccCommParamsSpec(returnCommonParamSpec);
76 return true;
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::EccKeyUtilFuzzTest(data, size);
85 return 0;
86 }
87