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 struct HuksHdi *g_instance = nullptr;
25
26 #define WRAPPED_KEY_DATA 287
27 #define PARAMSET_SIZE 124
28 #define KEY_SIZE 400
29
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)30 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
31 {
32 if (data == nullptr || size <= (KEY_SIZE + WRAPPED_KEY_DATA + PARAMSET_SIZE)) {
33 return false;
34 }
35 uint8_t *myData = static_cast<uint8_t *>(malloc(sizeof(uint8_t) * size));
36 if (myData == nullptr) {
37 return false;
38 }
39 (void)memcpy_s(myData, size, data, size);
40 struct HksBlob wrappedKeyData = { WRAPPED_KEY_DATA, myData };
41 struct HksParamSet *paramSetIn = reinterpret_cast<struct HksParamSet *>(myData + WRAPPED_KEY_DATA);
42 paramSetIn->paramSetSize = PARAMSET_SIZE;
43
44 #ifdef HUKS_HDI_SOFTWARE
45 if (HuksFreshParamSet(paramSetIn, false) != 0) {
46 free(myData);
47 return false;
48 }
49 #endif
50 struct HksBlob key = {
51 .data = myData + WRAPPED_KEY_DATA + PARAMSET_SIZE,
52 .size = size - (WRAPPED_KEY_DATA + PARAMSET_SIZE)
53 };
54 uint8_t buffer[1024];
55 struct HksBlob out = {
56 .data = buffer,
57 .size = sizeof(buffer)
58 };
59 (void)g_instance->HuksHdiImportWrappedKey(nullptr, &key, &wrappedKeyData, paramSetIn, &out);
60 free(myData);
61 return true;
62 }
63
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
66 {
67 if (InitHuksCoreEngine(&g_instance) != 0) {
68 return -1;
69 }
70 DoSomethingInterestingWithMyAPI(data, size);
71 return 0;
72 }
73