• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HcfSignCreateFuzzTest(const uint8_t * data,size_t size)62     bool HcfSignCreateFuzzTest(const uint8_t* data, size_t size)
63     {
64         TestSign();
65         HcfSign *sign = nullptr;
66         std::string algoName(reinterpret_cast<const char *>(data), size);
67         HcfResult res = HcfSignCreate(algoName.c_str(), &sign);
68         if (res != HCF_SUCCESS) {
69             return false;
70         }
71         HcfObjDestroy(sign);
72         return true;
73     }
74 }
75 
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     /* Run your code on data */
80     OHOS::HcfSignCreateFuzzTest(data, size);
81     return 0;
82 }
83