1 /*
2  * Copyright (c) 2024-2024 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 "hks_fuzz_util.h"
17 
18 namespace OHOS {
19 namespace Security {
20 namespace Hks {
ConstructHksParams(uint8_t * & data,size_t & size)21 std::vector<HksParam> ConstructHksParams(uint8_t *&data, size_t &size)
22 {
23     std::vector<HksParam> params {};
24     while (size >= sizeof(HksParam)) {
25         HksParam *p = ReadData<HksParam *>(data, size, sizeof(HksParam));
26         if (GetTagType(static_cast<HksTag>(p->tag)) != HKS_TAG_TYPE_BYTES) {
27             params.emplace_back(*p);
28             continue;
29         }
30         if (size < p->blob.size) {
31             continue;
32         }
33         p->blob.data = ReadData<uint8_t *>(data, size, p->blob.size);
34         params.emplace_back(*p);
35     }
36     return params;
37 }
38 
ConstructHksParamSetFromFuzz(uint8_t * & data,size_t & size)39 WrapParamSet ConstructHksParamSetFromFuzz(uint8_t *&data, size_t &size)
40 {
41     auto params = ConstructHksParams(data, size);
42     WrapParamSet ps {};
43     int32_t ret = HksInitParamSet(&ps.s);
44     if (ret != HKS_SUCCESS) {
45         return {};
46     }
47     if (!params.empty()) {
48         ret = HksAddParams(ps.s, params.data(), params.size());
49         if (ret != HKS_SUCCESS) {
50             return {};
51         }
52     }
53     ret = HksBuildParamSet(&ps.s);
54     if (ret != HKS_SUCCESS) {
55         return {};
56     }
57     return ps;
58 }
59 }}}