• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "hksstorage_fuzzer.h"
16 
17 #include <string>
18 
19 #include "hks_api.h"
20 #include "hks_config.h"
21 #include "hks_log.h"
22 #include "hks_mem.h"
23 #include "hks_param.h"
24 #include "hks_storage_manager.h"
25 #include "hks_template.h"
26 #include "hks_type.h"
27 #include "hks_type_inner.h"
28 
29 const std::string TEST_PROCESS_NAME = "test_process";
30 const std::string TEST_USER_ID = "123465";
31 const std::string TEST_KEY_ALIAS = "key_alias";
32 constexpr uint32_t TEST_BLOB_SIZE = 16;
33 constexpr uint8_t TEST_BLOB[TEST_BLOB_SIZE] = {0};
34 
35 namespace OHOS {
36 namespace Security {
37 namespace Hks {
38 
BuildParamSet(const struct HksParam * param,uint32_t paramCnt,struct HksParamSet ** paramSetOut)39 static int32_t BuildParamSet(const struct HksParam *param, uint32_t paramCnt, struct HksParamSet **paramSetOut)
40 {
41     int32_t ret;
42     struct HksParamSet *paramSet = nullptr;
43     do {
44         ret = HksInitParamSet(&paramSet);
45         HKS_IF_NOT_SUCC_BREAK(ret)
46 
47         if (param != nullptr && paramCnt > 0) {
48             ret = HksAddParams(paramSet, param, paramCnt);
49             HKS_IF_NOT_SUCC_BREAK(ret)
50         }
51 
52         ret = HksBuildParamSet(&paramSet);
53         HKS_IF_NOT_SUCC_BREAK(ret)
54     } while (0);
55     if (ret != HKS_SUCCESS) {
56         HksFreeParamSet(&paramSet);
57     }
58     *paramSetOut = paramSet;
59     return HKS_SUCCESS;
60 }
61 
62 static const struct HksParam g_genParams[] = {
63     { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
64     { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
65     { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_128 },
66     { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE },
67     { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_CBC },
68     { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE },
69 };
70 
PrepareBlob()71 static void PrepareBlob()
72 {
73     HksBlob processName = {
74         .size = TEST_PROCESS_NAME.size() + 1,
75         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
76     };
77     HksBlob userId = {
78         .size = TEST_USER_ID.size() + 1,
79         .data = (uint8_t *)&TEST_USER_ID[0]
80     };
81     HksBlob keyAlias = {
82         .size = TEST_KEY_ALIAS.size() + 1,
83         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
84     };
85     HksBlob keyBlob = {
86         .size = TEST_BLOB_SIZE,
87         .data = (uint8_t *)TEST_BLOB,
88     };
89     HksProcessInfo hksProcessInfo = {
90         .userId = userId,
91         .processName = processName
92     };
93 
94     struct HksParamSet *paramSet = nullptr;
95     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
96     HksManageStoreKeyBlob(&hksProcessInfo, paramSet, &keyAlias,
97         &keyBlob, HksStorageType::HKS_STORAGE_TYPE_KEY);
98     HksFreeParamSet(&paramSet);
99 }
100 
HksStorageTest001()101 static void HksStorageTest001()
102 {
103     HksBlob processName = {
104         .size = TEST_PROCESS_NAME.size() + 1,
105         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
106     };
107     HksBlob userId = {
108         .size = TEST_USER_ID.size() + 1,
109         .data = (uint8_t *)&TEST_USER_ID[0]
110     };
111     HksBlob keyBlob = {
112         .size = TEST_BLOB_SIZE,
113         .data = (uint8_t *)TEST_BLOB,
114     };
115     HksBlob keyAlias = {
116         .size = TEST_KEY_ALIAS.size() + 1,
117         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
118     };
119     HksProcessInfo hksProcessInfo = {
120         .userId = userId,
121         .processName = processName
122     };
123 
124     struct HksParamSet *paramSet = nullptr;
125     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
126     HksManageStoreKeyBlob(&hksProcessInfo, paramSet, &keyAlias,
127         &keyBlob, HksStorageType::HKS_STORAGE_TYPE_KEY);
128     HksFreeParamSet(&paramSet);
129 }
130 
HksStorageTest002()131 static void HksStorageTest002()
132 {
133     PrepareBlob();
134 
135     HksBlob processName = {
136         .size = TEST_PROCESS_NAME.size() + 1,
137         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
138     };
139     HksBlob userId = {
140         .size = TEST_USER_ID.size() + 1,
141         .data = (uint8_t *)&TEST_USER_ID[0]
142     };
143     HksBlob keyAlias = {
144         .size = TEST_KEY_ALIAS.size() + 1,
145         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
146     };
147     uint8_t buff[TEST_BLOB_SIZE] = {0};
148     HksBlob keyBlob = {
149         .size = TEST_BLOB_SIZE,
150         .data = buff,
151     };
152     HksProcessInfo hksProcessInfo = {
153         .userId = userId,
154         .processName = processName
155     };
156 
157     struct HksParamSet *paramSet = nullptr;
158     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
159     HksManageStoreGetKeyBlob(&hksProcessInfo, paramSet,
160         &keyAlias, &keyBlob, HksStorageType::HKS_STORAGE_TYPE_KEY);
161     HksFreeParamSet(&paramSet);
162 }
163 
HksStorageTest003()164 static void HksStorageTest003()
165 {
166     PrepareBlob();
167 
168     HksBlob processName = {
169         .size = TEST_PROCESS_NAME.size() + 1,
170         .data = (uint8_t *)&TEST_PROCESS_NAME[0],
171     };
172     HksBlob userId = {
173         .size = TEST_USER_ID.size() + 1,
174         .data = (uint8_t *)&TEST_USER_ID[0]
175     };
176     HksBlob keyAlias = {
177         .size = TEST_KEY_ALIAS.size() + 1,
178         .data = (uint8_t *)&TEST_KEY_ALIAS[0],
179     };
180     HksProcessInfo hksProcessInfo = {
181         .userId = userId,
182         .processName = processName
183     };
184     struct HksParamSet *paramSet = nullptr;
185     BuildParamSet(g_genParams, HKS_ARRAY_SIZE(g_genParams), &paramSet);
186     HksManageStoreDeleteKeyBlob(&hksProcessInfo, paramSet, &keyAlias,
187         HksStorageType::HKS_STORAGE_TYPE_KEY);
188     HksFreeParamSet(&paramSet);
189 }
190 }
191 }
192 }
193 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)194 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
195 {
196     (void)data;
197     (void)size;
198     OHOS::Security::Hks::HksStorageTest001();
199     OHOS::Security::Hks::HksStorageTest002();
200     OHOS::Security::Hks::HksStorageTest003();
201     return 0;
202 }
203