1 /*
2 * Copyright (c) 2024-2024 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 "hksreportwrapper_fuzzer.h"
17
18 #include <string>
19
20 #include "hks_log.h"
21 #include "hks_param.h"
22
23 #include "hks_report_wrapper.h"
24 #include "hks_template.h"
25 #include "hks_type.h"
26 #include "hks_type_inner.h"
27
28 const std::string TEST_PROCESS_NAME = "test_process";
29 const std::string TEST_USER_ID = "123465";
30 static const struct HksParam g_genParams[] = {
31 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
32 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
33 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
34 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE },
35 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
36 { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE },
37 };
38
39 namespace OHOS {
40 namespace Security {
41 namespace Hks {
42
BuildParamSet(const struct HksParam * param,uint32_t paramCnt,struct HksParamSet ** paramSetOut)43 static int32_t BuildParamSet(const struct HksParam *param, uint32_t paramCnt, struct HksParamSet **paramSetOut)
44 {
45 int32_t ret;
46 struct HksParamSet *paramSet = nullptr;
47 do {
48 ret = HksInitParamSet(¶mSet);
49 HKS_IF_NOT_SUCC_BREAK(ret)
50
51 if (param != nullptr && paramCnt > 0) {
52 ret = HksAddParams(paramSet, param, paramCnt);
53 HKS_IF_NOT_SUCC_BREAK(ret)
54 }
55
56 ret = HksBuildParamSet(¶mSet);
57 HKS_IF_NOT_SUCC_BREAK(ret)
58 } while (0);
59 if (ret != HKS_SUCCESS) {
60 HksFreeParamSet(¶mSet);
61 }
62 *paramSetOut = paramSet;
63 return HKS_SUCCESS;
64 }
65
HksReportWrapperTest001()66 static void HksReportWrapperTest001()
67 {
68 ReportFaultEvent(nullptr, nullptr, nullptr, HKS_SUCCESS);
69 }
70
HksReportWrapperTest002()71 static void HksReportWrapperTest002()
72 {
73 HksBlob processName = {
74 .size = TEST_PROCESS_NAME.size() + 1,
75 .data = (uint8_t *)&TEST_PROCESS_NAME[0],
76 };
77 HksBlob userId = {
78 .size = TEST_USER_ID.size() + 1,
79 .data = (uint8_t *)&TEST_USER_ID[0]
80 };
81 HksProcessInfo hksProcessInfo = {
82 .userId = userId,
83 .processName = processName
84 };
85
86 struct HksParamSet *paramSet = nullptr;
87 BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), ¶mSet);
88
89 ReportFaultEvent(__func__, &hksProcessInfo, paramSet, HKS_FAILURE);
90 HksFreeParamSet(¶mSet);
91 }
92 }
93 }
94 }
95
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)96 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
97 {
98 (void)data;
99 (void)size;
100 OHOS::Security::Hks::HksReportWrapperTest001();
101 OHOS::Security::Hks::HksReportWrapperTest002();
102 return 0;
103 }
104