• 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 "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 = NULL;
34         HcfResult res = HcfAsyKeyGeneratorCreate("ECC224", &generator);
35         if (res != HCF_SUCCESS) {
36             return;
37         }
38 
39         HcfKeyPair *ecc224KeyPair = NULL;
40         res = generator->generateKeyPair(generator, NULL, &ecc224KeyPair);
41         HcfObjDestroy(generator);
42         if (res != HCF_SUCCESS) {
43             return;
44         }
45 
46         HcfSign *sign = NULL;
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, NULL, ecc224KeyPair->priKey);
57         (void)sign->update(sign, &mockInput);
58 
59         HcfVerify *verify = NULL;
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 = NULL,
68             .len = 0
69         };
70         (void)sign->sign(sign, NULL, &out);
71         (void)verify->init(verify, NULL, ecc224KeyPair->pubKey);
72         (void)verify->update(verify, &mockInput);
73         (void)verify->verify(verify, NULL, &out);
74         HcfObjDestroy(ecc224KeyPair);
75         HcfObjDestroy(sign);
76         HcfBlobDataFree(&out);
77         HcfObjDestroy(verify);
78     }
79 
HcfVerifyCreateFuzzTest(const uint8_t * data,size_t size)80     bool HcfVerifyCreateFuzzTest(const uint8_t* data, size_t size)
81     {
82         TestVerify();
83         HcfVerify *verify = nullptr;
84         std::string algoName(reinterpret_cast<const char *>(data), size);
85         HcfResult res = HcfVerifyCreate(algoName.c_str(), &verify);
86         if (res != HCF_SUCCESS) {
87             return false;
88         }
89         HcfObjDestroy(verify);
90         return true;
91     }
92 }
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     /* Run your code on data */
98     OHOS::HcfVerifyCreateFuzzTest(data, size);
99     return 0;
100 }
101