• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #include "hkswrapkey_fuzzer.h"
16 
17 #include "hks_api.h"
18 #include "hks_log.h"
19 #include "hks_mem.h"
20 #include "hks_param.h"
21 #include "hks_type.h"
22 
23 #include <securec.h>
24 
25 #include "hks_fuzz_util.h"
26 
27 constexpr int WRAPPED_KEY_SIZE = 2048;
28 constexpr int BLOB_NUM = 3;
29 
30 namespace OHOS {
31 namespace Security {
32 namespace Hks {
33 
DoSomethingInterestingWithMyAPI(uint8_t * data,size_t size)34 int DoSomethingInterestingWithMyAPI(uint8_t *data, size_t size)
35 {
36     if (data == nullptr || size < (BLOB_NUM * sizeof(uint32_t))) {
37         return -1;
38     }
39 
40     struct HksBlob key = { sizeof(uint32_t), ReadData<uint8_t *>(data, size, sizeof(uint32_t)) };
41     struct HksBlob srcData = { sizeof(uint32_t), ReadData<uint8_t *>(data, size, sizeof(uint32_t)) };
42     struct HksBlob mac = { sizeof(uint32_t), ReadData<uint8_t *>(data, size, sizeof(uint32_t)) };
43 
44     WrapParamSet ps = ConstructHksParamSetFromFuzz(data, size);
45 
46     uint8_t WrappedData[WRAPPED_KEY_SIZE] = {0};
47     struct HksBlob wrappedKey = {WRAPPED_KEY_SIZE, WrappedData};
48     (void)HksWrapKey(&key, nullptr, ps.s, &wrappedKey);
49 
50     (void)HksUnwrapKey(&key, nullptr, &srcData, ps.s);
51 
52     (void)HcmIsDeviceKeyExist(ps.s);
53 
54     [[maybe_unused]] int ret = HksMac(&key, ps.s, &srcData, &mac);
55 
56     return 0;
57 }
58 
59 }
60 }
61 }
62 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64 {
65     std::vector<uint8_t> v(data, data + size);
66     OHOS::Security::Hks::DoSomethingInterestingWithMyAPI(v.data(), v.size());
67 
68     return 0;
69 }
70