• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "huks_hdi_fuzzer.h"
17 #include "huks_hdi_passthrough_adapter.h"
18 #include "huks_hdi_fuzz_common.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <securec.h>
23 
24 
25 struct HuksHdi *g_instance = nullptr;
26 
27 #define SIZE_KEY 276
28 #define SIZE_PARAMSET_INIT 172
29 #define SIZE_PARAMSET_UPDATE 120
30 #define SIZE_PARAMSET_FINISH 188
31 #define ALLSIZE (SIZE_KEY + SIZE_PARAMSET_INIT + SIZE_PARAMSET_UPDATE + SIZE_PARAMSET_FINISH)
32 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)33 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
34 {
35     if (data == nullptr || size <= ALLSIZE) {
36         return false;
37     }
38 
39     uint8_t *myData = static_cast<uint8_t *>(malloc(sizeof(uint8_t) * size));
40     if (myData == nullptr) {
41         return false;
42     }
43 
44     (void)memcpy_s(myData, size, data, size);
45 
46     struct HksBlob key = { SIZE_KEY, myData };
47     struct HksParamSet *paramSet = reinterpret_cast<struct HksParamSet *>(myData + SIZE_KEY);
48     paramSet->paramSetSize = SIZE_PARAMSET_INIT;
49     uint8_t buffer[32];
50     struct HksBlob handle = {
51         .data = buffer,
52         .size = sizeof(buffer)
53     };
54 
55     struct HksBlob inData = { size, myData };
56     struct HksParamSet *paramSetUpdate = reinterpret_cast<struct HksParamSet *>(myData + SIZE_KEY + SIZE_PARAMSET_INIT);
57     paramSetUpdate->paramSetSize = SIZE_PARAMSET_UPDATE;
58 
59     struct HksParamSet *paramSetFinish = reinterpret_cast<struct HksParamSet *>(myData + SIZE_KEY + SIZE_PARAMSET_INIT +
60         SIZE_PARAMSET_UPDATE);
61     paramSetFinish->paramSetSize = SIZE_PARAMSET_FINISH;
62 
63 #ifdef HUKS_HDI_SOFTWARE
64     if (HuksFreshParamSet(paramSetUpdate, false) != 0 || HuksFreshParamSet(paramSetFinish, false) != 0) {
65         free(myData);
66         return false;
67     }
68 #endif
69     uint8_t buffer2[1024];
70     struct HksBlob out = {
71         .data = buffer2,
72         .size = sizeof(buffer2)
73     };
74 
75     (void)g_instance->HuksHdiInit(&key, paramSet, &handle, &out);
76     (void)g_instance->HuksHdiUpdate(&handle, paramSetUpdate, &inData, &out);
77     (void)g_instance->HuksHdiFinish(&handle, paramSetFinish, &inData, &out);
78 
79     free(myData);
80     return true;
81 }
82 
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
85 {
86     if (InitHuksCoreEngine(&g_instance) != 0) {
87         return -1;
88     }
89     DoSomethingInterestingWithMyAPI(data, size);
90     return 0;
91 }
92