1/*
2 * Copyright (c) 2020-2021 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#ifndef __HICHAIN_H__
17#define __HICHAIN_H__
18
19#include <stdint.h>
20
21#if defined(_WINDOWS)
22#ifdef DLL_EXPORT
23#define DLL_API_PUBLIC __declspec(dllexport)
24#else
25#define DLL_API_PUBLIC __declspec(dllimport)
26#endif
27#else
28#define DLL_API_PUBLIC __attribute__ ((visibility("default")))
29#endif
30
31#ifdef HILINK_E2E_SECURITY_CONFIG
32#define _SCANTY_MEMORY_
33#endif
34
35enum hc_error {
36    HC_OK = 0,
37    HC_INPUT_ERROR,
38    HC_INNER_ERROR,
39    HC_STATE_ERROR,
40    HC_SERVICE_CONFIRM_ERROR,
41    HC_UNKNOW_MESSAGE,
42    HC_NO_MESSAGE_TO_SEND,
43    HC_REPEATED_REFERENCE,
44    HC_NEED_DEPEND,
45    HC_BUILD_OBJECT_FAILED,
46    HC_BUILD_SEND_DATA_FAILED,
47    HC_MALLOC_FAILED,
48    HC_VERSION_UNSUPPORT,
49    HC_LARGE_PRIME_NUMBER_LEN_UNSUPPORT,
50    HC_CAL_BIGNUM_EXP_FAILED,
51    HC_INTERNAL_ERROR,
52    HC_IMPORT_AUTH_DATA_FAILED,
53    HC_VERIFY_PROOF_FAILED,
54    HC_GENERATE_PROOF_FAILED,
55    HC_GENERATE_SESSION_KEY_FAILED,
56    HC_ST_KEY_PAIR_FAILED,
57    HC_COMPUTE_STS_SHARED_SECRET_FAILED,
58    HC_COMPUTE_HKDF_FAILED,
59    HC_PAKE_SESSION_KEY_ERROR,
60    HC_PAKE_OBJECT_ERROR,
61    HC_STS_OBJECT_ERROR,
62    HC_DECRYPT_FAILED,
63    HC_ENCRYPT_FAILED,
64    HC_SIGN_TOO_SHORT,
65    HC_VERIFY_EXCHANGE_FAILED,
66    HC_SIGN_EXCHANGE_FAILED,
67    HC_SAVE_LTPK_FAILED,
68    HC_DELETE_LTPK_FAILED,
69    HC_GET_LTPK_FAILED,
70    HC_GEN_ALIAS_FAILED,
71    HC_GEN_SERVICE_ID_FAILED,
72    HC_GEN_RANDOM_FAILED,
73    HC_UNSUPPORT,
74    HC_MESSAGE_INCONSISTENCY,
75    HC_OPERATION_CODE_ERROR,
76    HC_MEMCPY_ERROR,
77    PROTOCOL_STATE_ERROR,
78    PROTOCOL_SET_STATE_ERROR,
79    PROTOCOL_TIMEOUT_ERROR,
80    PROTOCOL_SAHRED_SECRET_FAIL,
81    PROTOCOL_HKDF_FAIL,
82    PROTOCOL_SHA_FAIL,
83    PROTOCOL_RAND_FAIL,
84    PROTOCOL_ENCRYPT_FAIL,
85    PROTOCOL_DECRYPT_FAIL,
86    PROTOCOL_VERIFY_DATA_FAIL,
87    PROTOCOL_KEY_ALG_FAIL,
88    PROTOCOL_RESULT_ERROR,
89    KEK_NOT_EXIST,
90    DEK_NOT_EXIST,
91    TEMP_KEY_GEN_FAILED,
92    HC_INPUT_PTR_NULL
93};
94
95enum hc_type {
96    HC_CENTRE = 1,
97    HC_ACCESSORY,
98};
99
100enum hc_user_type {
101    HC_USER_TYPE_ACCESSORY = 0,
102    HC_USER_TYPE_CONTROLLER = 1
103};
104
105enum hc_operation {
106    INVALID_OPERATION_CODE = -1,
107    NO_OPERATION_CODE = 0,
108    BIND = 1,
109    AUTHENTICATE,
110    ADD_AUTHINFO,
111    REMOVE_AUTHINFO,
112    UNBIND,
113    AUTH_KEY_AGREEMENT,
114    REGISTER,
115    SEC_CLONE_OP,
116    REMOVE_ALL_AUTHINFO,
117    GET_PEER_USER_TYPE,
118    GENERATE_KEY_PAIR = 99
119};
120
121enum hc_result {
122    END_SUCCESS = 0,
123    END_FAILED,
124    KEY_AGREEMENT_PROCESSING,
125    KEY_AGREEMENT_END,
126    OPERATION_PROCESSING,
127    OPERATION_END,
128};
129
130enum hc_export_type {
131    EXPORT_DATA_FULL_AUTH_INFO   = 0,
132    EXPORT_DATA_LITE_AUTH_INFO   = 1,
133    EXPORT_DATA_SIGNED_AUTH_INFO = 2
134};
135
136#define HC_AUTH_ID_BUFF_LEN 64
137#define HC_SALT_BUFF_LEN    16
138#define HC_PIN_BUFF_LEN     16
139#define HC_SESSION_KEY_LEN  128
140#define HC_KEY_TYPE_LEN     2
141#define HC_BASE_KEY_NUM     5
142
143#if !defined(_SCANTY_MEMORY_)
144#define HC_PACKAGE_NAME_BUFF_LEN    256
145#define HC_SERVICE_TYPE_BUFF_LEN    256
146#else /* _SCANTY_MEMORY_ */
147#define HC_PACKAGE_NAME_BUFF_LEN    16
148#define HC_SERVICE_TYPE_BUFF_LEN    16
149#endif /* _SCANTY_MEMORY_ */
150#define HC_KEY_ALIAS_MAX_LEN        64
151
152/* alia list max size */
153#if defined(_SCANTY_MEMORY_)
154#define HC_PUB_KEY_ALIAS_MAX_NUM    15
155#elif defined(_SUPPORT_SEC_CLONE_SERVER_)
156#define HC_PUB_KEY_ALIAS_MAX_NUM    120
157#else
158#define HC_PUB_KEY_ALIAS_MAX_NUM    20
159#endif
160
161enum hc_trust_peer_result {
162    HC_NOT_TRUST_PEER = 0,
163    HC_BINDED_TRUST_PEER,
164    HC_AUTHED_TRUST_PEER,
165    HC_ACCESSORY_TRUST_PEER
166};
167
168typedef void *hc_handle;
169
170struct uint8_buff {
171    uint8_t *val;
172    uint32_t size;
173    uint32_t length;
174};
175
176struct hc_pin {
177    uint32_t length;
178    uint8_t pin[HC_PIN_BUFF_LEN];
179};
180
181struct hc_salt {
182    uint32_t length;
183    uint8_t salt[HC_SALT_BUFF_LEN];
184};
185
186struct hc_auth_id {
187    uint32_t length;
188    uint8_t auth_id[HC_AUTH_ID_BUFF_LEN];
189};
190
191struct hc_session_key {
192    uint32_t length;
193    uint8_t session_key[HC_SESSION_KEY_LEN];
194};
195
196struct hc_package_name {
197    uint32_t length;
198    uint8_t name[HC_PACKAGE_NAME_BUFF_LEN];
199};
200
201struct hc_service_type {
202    uint32_t length;
203    uint8_t type[HC_SERVICE_TYPE_BUFF_LEN];
204};
205
206struct hc_user_info {
207    struct hc_auth_id auth_id;
208    int32_t user_type;
209};
210
211struct operation_parameter {
212    struct hc_auth_id self_auth_id;
213    struct hc_auth_id peer_auth_id;
214    uint32_t key_length;
215};
216
217struct hc_key_alias {
218    uint32_t length;
219    uint8_t key_alias[HC_KEY_ALIAS_MAX_LEN];
220};
221
222struct session_identity {
223    uint32_t session_id;
224    struct hc_package_name package_name;
225    struct hc_service_type service_type;
226    void *context;
227};
228
229typedef void (*transmit_cb)(const struct session_identity *identity, const void *data, uint32_t length);
230typedef void (*get_protocol_params_cb)(const struct session_identity *identity, int32_t operation_code,
231                                       struct hc_pin *pin, struct operation_parameter *para);
232typedef void (*set_session_key_func)(const struct session_identity *identity,
233                                     const struct hc_session_key *session_key);
234typedef void (*set_service_result_func)(const struct session_identity *identity, int32_t result, int32_t errorCodeRecv);
235typedef int32_t (*confirm_receive_request_func)(const struct session_identity *identity, int32_t operation_code);
236
237struct hc_call_back {
238    transmit_cb transmit;
239    get_protocol_params_cb get_protocol_params;
240    set_session_key_func set_session_key;
241    set_service_result_func set_service_result;
242    confirm_receive_request_func confirm_receive_request;
243};
244
245typedef void (*log_func)(const char *tag, const char *func_name, const char *format, ...);
246
247struct log_func_group {
248    log_func log_d;
249    log_func log_i;
250    log_func log_w;
251    log_func log_e;
252};
253
254#ifdef __cplusplus
255extern "C" {
256#endif
257
258/*
259 * Register log operation callback
260 *
261 * para log:  The log callback functions group
262 * return  void
263 */
264DLL_API_PUBLIC void registe_log(struct log_func_group *log);
265
266/*
267 * Get hichain instance
268 *
269 * para identity:  basic information of session
270 * pare type:  hichain device type
271 * hc_call_back:  hichain callback functions
272 * return  hichain instance
273 */
274DLL_API_PUBLIC hc_handle get_instance(const struct session_identity *identity, enum hc_type type,
275    const struct hc_call_back *call_back);
276
277/*
278 * Destroy hichain instance
279 *
280 * para handle:  hichain instance
281 * return  void
282 */
283DLL_API_PUBLIC void destroy(hc_handle *handle);
284
285/*
286 * set context in handle
287 *
288 * para handle:  hichain instance
289 * para context: put in an object in instance
290 * return  void
291 */
292DLL_API_PUBLIC void set_context(hc_handle handle, void *context);
293
294/*
295 * Hichain receives message data
296 *
297 * para handle:  hichain instance
298 * para data:  message data
299 * return  0 ok, others error
300 */
301DLL_API_PUBLIC int32_t receive_data(hc_handle handle, struct uint8_buff *data);
302
303/*
304 * Hichain receives message data , data is json object
305 *
306 * para handle:  hichain instance
307 * para json_object:  message data
308 * return  0 ok, others error
309 */
310DLL_API_PUBLIC int32_t receive_data_with_json_object(hc_handle handle, const void *json_object);
311
312#ifndef _CUT_API_
313
314
315/*
316 * Start pake module
317 *
318 * para handle:  hichain instance
319 * para params:  operating parameter
320 * return  0 ok, others error
321 */
322DLL_API_PUBLIC int32_t start_pake(hc_handle handle, const struct operation_parameter *params);
323
324/*
325 * Authenticate peer identity and build session key
326 *
327 * para handle:  hichain instance
328 * para params:  operating parameter
329 * return  0 ok, others error
330 */
331DLL_API_PUBLIC int32_t authenticate_peer(hc_handle handle, struct operation_parameter *params);
332
333/*
334 * Delete local saved authentication
335 *
336 * para handle:  hichain instance
337 * para hc_user_info:  user to be deleted
338 * return  0 ok, others error
339 */
340DLL_API_PUBLIC int32_t delete_local_auth_info(hc_handle handle, struct hc_user_info *user_info);
341
342/*
343 * Share the bound device to other users
344 *
345 * para handle:  hichain instance
346 * para params:  operating parameter, self_auth_id: controller auth id, peer_auth_id: peer auth id
347 * para auth_id:  authorized auth id
348 * para user_type:  authorized user type. 0 : ACCESSORY ; 1 : CONTROLLER
349 * return  0 ok, others error
350 */
351int32_t add_auth_info(hc_handle handle, const struct operation_parameter *params,
352    const struct hc_auth_id *auth_id, int32_t user_type);
353
354/*
355 * Remove user authorization of an accessory
356 *
357 * para handle:  hichain instance
358 * para params:  operating parameter, self_auth_id: controller auth id, peer_auth_id: peer auth id
359 * para auth_id:  unauthorized auth id
360 * para user_type:  unauthorized user type. 0 : ACCESSORY ; 1 : CONTROLLER
361 * return  0 ok, others error
362 */
363int32_t remove_auth_info(hc_handle handle, const struct operation_parameter *params,
364    const struct hc_auth_id *auth_id, int32_t user_type);
365
366/*
367 * Delete local saved authentication
368 *
369 * para handle:  hichain instance
370 * para user_info:  user to be deleted
371 * return  0 ok, others error
372 */
373int32_t delete_local_auth_info(hc_handle handle, struct hc_user_info *user_info);
374
375/*
376 * Judge trusted peer
377 *
378 * para handle:  hichain instance
379 * para hc_user_info:  user to be judged
380 * return 0 untrusted, 1 trusted(bind), 2 trusted(auth)
381 */
382DLL_API_PUBLIC int32_t is_trust_peer(hc_handle handle, struct hc_user_info *user_info);
383
384/*
385 * List trusted peers
386 *
387 * para handle:  hichain instance
388 * para trust_user_type:  the type of peer. 0 : ACCESSORY ; 1 : CONTROLLER
389 * para owner_auth_id:  input null, output binding list; input owner, output auth list;others, output null
390 * para auth_id_list:  list to receive auth id
391 * return  number of trusted peers
392 */
393DLL_API_PUBLIC uint32_t list_trust_peers(hc_handle handle, int32_t trust_user_type,
394    struct hc_auth_id *owner_auth_id, struct hc_auth_id **auth_id_list);
395#endif /* _CUT_XXX_ */
396
397/*
398 * Set self authId
399 *
400 * para handle:  hichain instance
401 * para data:  the data of auth id
402 * return  void
403 */
404DLL_API_PUBLIC void set_self_auth_id(hc_handle handle, struct uint8_buff *data);
405
406#ifdef __cplusplus
407}
408#endif
409
410#endif /* __HICHAIN_H__ */
411