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 "hcfmaccreate_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 
22 #include "blob.h"
23 #include "mac.h"
24 #include "result.h"
25 #include "sym_key_generator.h"
26 
27 namespace OHOS {
28     static const int KEY_LEN = 16;
29 
TestMacConvertSymKey(const uint8_t * data,size_t size)30     static void TestMacConvertSymKey(const uint8_t* data, size_t size)
31     {
32         HcfMac *macObj = nullptr;
33         HcfResult res = HcfMacCreate("SHA1", &macObj);
34         if (res != HCF_SUCCESS) {
35             return;
36         }
37         HcfSymKeyGenerator *generator = nullptr;
38         (void)HcfSymKeyGeneratorCreate("AES128", &generator);
39         HcfSymKey *key = nullptr;
40         HcfBlob keyMaterialBlob = {.data = const_cast<uint8_t *>(data), .len = size};
41         generator->convertSymKey(generator, &keyMaterialBlob, &key);
42 
43         HcfObjDestroy(macObj);
44         HcfObjDestroy(key);
45         HcfObjDestroy(generator);
46     }
47 
TestMac(const uint8_t * data,size_t size)48     static void TestMac(const uint8_t* data, size_t size)
49     {
50         HcfMac *macObj = nullptr;
51         HcfResult res = HcfMacCreate("SHA1", &macObj);
52         if (res != HCF_SUCCESS) {
53             return;
54         }
55         HcfSymKeyGenerator *generator = nullptr;
56         (void)HcfSymKeyGeneratorCreate("AES128", &generator);
57         char testKey[] = "abcdefghijklmnop";
58         uint32_t testKeyLen = KEY_LEN;
59         HcfSymKey *key = nullptr;
60         HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
61         generator->convertSymKey(generator, &keyMaterialBlob, &key);
62 
63         HcfBlob inBlob = {.data = const_cast<uint8_t *>(data), .len = size};
64         (void)macObj->init(macObj, key);
65         (void)macObj->update(macObj, &inBlob);
66         HcfBlob outBlob = { 0 };
67         (void)macObj->doFinal(macObj, &outBlob);
68         (void)macObj->getAlgoName(macObj);
69         (void)macObj->getMacLength(macObj);
70         HcfBlobDataClearAndFree(&outBlob);
71         HcfObjDestroy(macObj);
72         HcfObjDestroy(key);
73         HcfObjDestroy(generator);
74     }
75 
HcfMacCreateFuzzTest(const uint8_t * data,size_t size)76     bool HcfMacCreateFuzzTest(const uint8_t* data, size_t size)
77     {
78         std::string alg(reinterpret_cast<const char *>(data), size);
79         TestMacConvertSymKey(data, size);
80         TestMac(data, size);
81         HcfMac *macObj = nullptr;
82         HcfResult res = HcfMacCreate(alg.c_str(), &macObj);
83         if (res != HCF_SUCCESS) {
84             return false;
85         }
86         HcfObjDestroy(macObj);
87         return true;
88     }
89 }
90 
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
93 {
94     /* Run your code on data */
95     OHOS::HcfMacCreateFuzzTest(data, size);
96     return 0;
97 }
98