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 35 enum 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 95 enum hc_type { 96 HC_CENTRE = 1, 97 HC_ACCESSORY, 98 }; 99 100 enum hc_user_type { 101 HC_USER_TYPE_ACCESSORY = 0, 102 HC_USER_TYPE_CONTROLLER = 1 103 }; 104 105 enum 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 121 enum 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 130 enum 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 161 enum 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 168 typedef void *hc_handle; 169 170 struct uint8_buff { 171 uint8_t *val; 172 uint32_t size; 173 uint32_t length; 174 }; 175 176 struct hc_pin { 177 uint32_t length; 178 uint8_t pin[HC_PIN_BUFF_LEN]; 179 }; 180 181 struct hc_salt { 182 uint32_t length; 183 uint8_t salt[HC_SALT_BUFF_LEN]; 184 }; 185 186 struct hc_auth_id { 187 uint32_t length; 188 uint8_t auth_id[HC_AUTH_ID_BUFF_LEN]; 189 }; 190 191 struct hc_session_key { 192 uint32_t length; 193 uint8_t session_key[HC_SESSION_KEY_LEN]; 194 }; 195 196 struct hc_package_name { 197 uint32_t length; 198 uint8_t name[HC_PACKAGE_NAME_BUFF_LEN]; 199 }; 200 201 struct hc_service_type { 202 uint32_t length; 203 uint8_t type[HC_SERVICE_TYPE_BUFF_LEN]; 204 }; 205 206 struct hc_user_info { 207 struct hc_auth_id auth_id; 208 int32_t user_type; 209 }; 210 211 struct 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 217 struct hc_key_alias { 218 uint32_t length; 219 uint8_t key_alias[HC_KEY_ALIAS_MAX_LEN]; 220 }; 221 222 struct 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 229 typedef void (*transmit_cb)(const struct session_identity *identity, const void *data, uint32_t length); 230 typedef void (*get_protocol_params_cb)(const struct session_identity *identity, int32_t operation_code, 231 struct hc_pin *pin, struct operation_parameter *para); 232 typedef void (*set_session_key_func)(const struct session_identity *identity, 233 const struct hc_session_key *session_key); 234 typedef void (*set_service_result_func)(const struct session_identity *identity, int32_t result); 235 typedef int32_t (*confirm_receive_request_func)(const struct session_identity *identity, int32_t operation_code); 236 237 struct 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 245 typedef void (*log_func)(const char *tag, const char *func_name, const char *format, ...); 246 247 struct 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 255 extern "C" { 256 #endif 257 258 /* 259 * Register log operation callback 260 * 261 * para log: The log callback functions group 262 * return void 263 */ 264 DLL_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 */ 274 DLL_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 */ 283 DLL_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 */ 292 DLL_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 */ 301 DLL_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 */ 310 DLL_API_PUBLIC int32_t receive_data_with_json_object(hc_handle handle, const void *json_object); 311 312 #ifndef _CUT_API_ 313 314 /* 315 * Initialize the center device 316 * 317 * para hc_package_name: the package name of the product 318 * para hc_service_type: the type of the product 319 * para hc_auth_id: the auth id of controller 320 * para dek: the alias of secret key used for encryption and decryption of data 321 * return 0 ok, others error 322 */ 323 DLL_API_PUBLIC int32_t init_center(const struct hc_package_name *package_name, 324 const struct hc_service_type *service_type, const struct hc_auth_id *auth_id, struct hc_key_alias *dek); 325 326 /* 327 * Start pake module 328 * 329 * para handle: hichain instance 330 * para params: operating parameter 331 * return 0 ok, others error 332 */ 333 DLL_API_PUBLIC int32_t start_pake(hc_handle handle, const struct operation_parameter *params); 334 335 /* 336 * Authenticate peer identity and build session key 337 * 338 * para handle: hichain instance 339 * para params: operating parameter 340 * return 0 ok, others error 341 */ 342 DLL_API_PUBLIC int32_t authenticate_peer(hc_handle handle, struct operation_parameter *params); 343 344 /* 345 * Delete local saved authentication 346 * 347 * para handle: hichain instance 348 * para hc_user_info: user to be deleted 349 * return 0 ok, others error 350 */ 351 DLL_API_PUBLIC int32_t delete_local_auth_info(hc_handle handle, struct hc_user_info *user_info); 352 353 /* 354 * Import auth info of bounded trust accessory 355 * 356 * para handle: hichain instance 357 * para hc_user_info: the information of Authorized user 358 * para hc_auth_id: the auth id of device 359 * para auth_info_type: the export auth info type: 0: full authentication data 360 * 1: lite authentication data 361 * 2: signed authentication data 362 * para auth_info: auth info of accessory 363 * return 0 ok, others error 364 */ 365 DLL_API_PUBLIC int32_t import_auth_info(hc_handle handle, struct hc_user_info *user_info, struct hc_auth_id *auth_id, 366 enum hc_export_type auth_info_type, struct uint8_buff *auth_info); 367 368 /* 369 * Share the bound device to other users 370 * 371 * para handle: hichain instance 372 * para params: operating parameter, self_auth_id: controller auth id, peer_auth_id: peer auth id 373 * para auth_id: authorized auth id 374 * para user_type: authorized user type. 0 : ACCESSORY ; 1 : CONTROLLER 375 * return 0 ok, others error 376 */ 377 int32_t add_auth_info(hc_handle handle, const struct operation_parameter *params, 378 const struct hc_auth_id *auth_id, int32_t user_type); 379 380 /* 381 * Remove user authorization of an accessory 382 * 383 * para handle: hichain instance 384 * para params: operating parameter, self_auth_id: controller auth id, peer_auth_id: peer auth id 385 * para auth_id: unauthorized auth id 386 * para user_type: unauthorized user type. 0 : ACCESSORY ; 1 : CONTROLLER 387 * return 0 ok, others error 388 */ 389 int32_t remove_auth_info(hc_handle handle, const struct operation_parameter *params, 390 const struct hc_auth_id *auth_id, int32_t user_type); 391 392 /* 393 * Delete local saved authentication 394 * 395 * para handle: hichain instance 396 * para user_info: user to be deleted 397 * return 0 ok, others error 398 */ 399 int32_t delete_local_auth_info(hc_handle handle, struct hc_user_info *user_info); 400 401 /* 402 * Judge trusted peer 403 * 404 * para handle: hichain instance 405 * para hc_user_info: user to be judged 406 * return 0 untrusted, 1 trusted(bind), 2 trusted(auth) 407 */ 408 DLL_API_PUBLIC int32_t is_trust_peer(hc_handle handle, struct hc_user_info *user_info); 409 410 /* 411 * List trusted peers 412 * 413 * para handle: hichain instance 414 * para trust_user_type: the type of peer. 0 : ACCESSORY ; 1 : CONTROLLER 415 * para owner_auth_id: input null, output binding list; input owner, output auth list;others, output null 416 * para auth_id_list: list to receive auth id 417 * return number of trusted peers 418 */ 419 DLL_API_PUBLIC uint32_t list_trust_peers(hc_handle handle, int32_t trust_user_type, 420 struct hc_auth_id *owner_auth_id, struct hc_auth_id **auth_id_list); 421 #endif /* _CUT_XXX_ */ 422 423 /* 424 * Set self authId 425 * 426 * para handle: hichain instance 427 * para data: the data of auth id 428 * return void 429 */ 430 DLL_API_PUBLIC void set_self_auth_id(hc_handle handle, struct uint8_buff *data); 431 432 #ifdef __cplusplus 433 } 434 #endif 435 436 #endif /* __HICHAIN_H__ */ 437