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 PARAMSET_SIZE 108
27 #define PRIVATE_KEY_SIZE 320
28 #define PUBLIC_KEY_SIZE 52
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 <= (PARAMSET_SIZE + PRIVATE_KEY_SIZE + PUBLIC_KEY_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 privatekey = { PRIVATE_KEY_SIZE, myData + PARAMSET_SIZE };
41 struct HksParamSet *paramSetIn = reinterpret_cast<struct HksParamSet *>(myData);
42 paramSetIn->paramSetSize = PARAMSET_SIZE;
43 #ifdef HUKS_HDI_SOFTWARE
44 if (HuksFreshParamSet(paramSetIn, false) != 0) {
45 free(myData);
46 return false;
47 }
48 #endif
49 struct HksBlob publickey = {
50 .data = myData + PARAMSET_SIZE + PRIVATE_KEY_SIZE,
51 .size = PUBLIC_KEY_SIZE
52 };
53 uint8_t buffer[1024];
54 struct HksBlob out = {
55 .data = buffer,
56 .size = sizeof(buffer)
57 };
58 (void)g_instance->HuksHdiAgreeKey(paramSetIn, &privatekey, &publickey, &out);
59 free(myData);
60 return true;
61 }
62
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
65 {
66 if (InitHuksCoreEngine(&g_instance) != 0) {
67 return -1;
68 }
69 DoSomethingInterestingWithMyAPI(data, size);
70 return 0;
71 }
72