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 SIZE_ALIAS 16
27 #define SIZE_KEY 16
28
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)29 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
30 {
31 if (data == nullptr || size <= (sizeof(struct HksParamSet) + SIZE_ALIAS + SIZE_KEY)) {
32 return false;
33 }
34
35 uint8_t *myData = static_cast<uint8_t *>(malloc(sizeof(uint8_t) * size));
36 if (myData == nullptr) {
37 return false;
38 }
39
40 (void)memcpy_s(myData, size, data, size);
41
42 struct HksBlob keyAlias = { SIZE_ALIAS, myData };
43 struct HksBlob aesKey = { SIZE_KEY, static_cast<uint8_t *>(myData + SIZE_ALIAS) };
44 struct HksParamSet *paramSet = reinterpret_cast<struct HksParamSet *>(myData + SIZE_ALIAS + SIZE_KEY);
45 paramSet->paramSetSize = size - (SIZE_ALIAS + SIZE_KEY);
46
47 #ifdef HUKS_HDI_SOFTWARE
48 if (HuksFreshParamSet(paramSet, false) != 0) {
49 free(myData);
50 return false;
51 }
52 #endif
53 uint8_t buffer[1024];
54 struct HksBlob out = {
55 .data = buffer,
56 .size = sizeof(buffer)
57 };
58 (void)g_instance->HuksHdiImportKey(&keyAlias, &aesKey, paramSet, &out);
59
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