• 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 "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     static const int TEST_DATA_LEN = 12;
30 
TestMac(void)31     static void TestMac(void)
32     {
33         HcfMac *macObj = nullptr;
34         HcfResult res = HcfMacCreate("SHA1", &macObj);
35         if (res != HCF_SUCCESS) {
36             return;
37         }
38         HcfSymKeyGenerator *generator = nullptr;
39         (void)HcfSymKeyGeneratorCreate("AES128", &generator);
40         char testKey[] = "abcdefghijklmnop";
41         uint32_t testKeyLen = KEY_LEN;
42         HcfSymKey *key = nullptr;
43         HcfBlob keyMaterialBlob = {.data = reinterpret_cast<uint8_t *>(testKey), .len = testKeyLen};
44         generator->convertSymKey(generator, &keyMaterialBlob, &key);
45 
46         char testData[] = "My test data";
47         uint32_t testDataLen = TEST_DATA_LEN;
48         HcfBlob inBlob = {.data = reinterpret_cast<uint8_t *>(testData), .len = testDataLen};
49         (void)macObj->init(macObj, key);
50         (void)macObj->update(macObj, &inBlob);
51         HcfBlob outBlob = { 0 };
52         (void)macObj->doFinal(macObj, &outBlob);
53         (void)macObj->getAlgoName(macObj);
54         (void)macObj->getMacLength(macObj);
55         HcfBlobDataClearAndFree(&outBlob);
56         HcfObjDestroy(macObj);
57         HcfObjDestroy(key);
58         HcfObjDestroy(generator);
59     }
60 
HcfMacCreateFuzzTest(const uint8_t * data,size_t size)61     bool HcfMacCreateFuzzTest(const uint8_t* data, size_t size)
62     {
63         TestMac();
64         HcfMac *macObj = nullptr;
65         std::string alg(reinterpret_cast<const char *>(data), size);
66         HcfResult res = HcfMacCreate(alg.c_str(), &macObj);
67         if (res != HCF_SUCCESS) {
68             return false;
69         }
70         HcfObjDestroy(macObj);
71         return true;
72     }
73 }
74 
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     OHOS::HcfMacCreateFuzzTest(data, size);
80     return 0;
81 }
82