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