1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 #ifndef H_BLE_SM_PRIV_ 21 #define H_BLE_SM_PRIV_ 22 23 #include <inttypes.h> 24 #include "syscfg/syscfg.h" 25 #include "os/queue.h" 26 #include "nimble/nimble_opt.h" 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif 31 32 struct ble_gap_sec_state; 33 struct hci_le_lt_key_req; 34 struct hci_encrypt_change; 35 36 #define BLE_SM_MTU 65 37 38 #define BLE_SM_OP_PAIR_REQ 0x01 39 #define BLE_SM_OP_PAIR_RSP 0x02 40 #define BLE_SM_OP_PAIR_CONFIRM 0x03 41 #define BLE_SM_OP_PAIR_RANDOM 0x04 42 #define BLE_SM_OP_PAIR_FAIL 0x05 43 #define BLE_SM_OP_ENC_INFO 0x06 44 #define BLE_SM_OP_MASTER_ID 0x07 45 #define BLE_SM_OP_IDENTITY_INFO 0x08 46 #define BLE_SM_OP_IDENTITY_ADDR_INFO 0x09 47 #define BLE_SM_OP_SIGN_INFO 0x0a 48 #define BLE_SM_OP_SEC_REQ 0x0b 49 #define BLE_SM_OP_PAIR_PUBLIC_KEY 0x0c 50 #define BLE_SM_OP_PAIR_DHKEY_CHECK 0x0d 51 #define BLE_SM_OP_PAIR_KEYPRESS_NOTIFY 0x0e 52 53 struct ble_sm_hdr { 54 uint8_t opcode; 55 uint8_t data[0]; 56 } __attribute__((packed)); 57 58 /** 59 * | Parameter | Size (octets) | 60 * +------------------------------------+-------------------+ 61 * | (Code=0x01/0x02 [req/rsp]) | 1 | 62 * | IO Capability | 1 | 63 * | OOB data flag | 1 | 64 * | AuthReq | 1 | 65 * | Maximum Encryption Key Size | 1 | 66 * | Initiator Key Distribution | 1 | 67 * | Responder Key Distribution | 1 | 68 */ 69 70 struct ble_sm_pair_cmd { 71 uint8_t io_cap; 72 uint8_t oob_data_flag; 73 uint8_t authreq; 74 uint8_t max_enc_key_size; 75 uint8_t init_key_dist; 76 uint8_t resp_key_dist; 77 } __attribute__((packed)); 78 79 /** 80 * | Parameter | Size (octets) | 81 * +------------------------------------+-------------------+ 82 * | (Code=0x03) | 1 | 83 * | Confirm Value | 16 | 84 */ 85 86 struct ble_sm_pair_confirm { 87 uint8_t value[16]; 88 } __attribute__((packed)); 89 90 /** 91 * | Parameter | Size (octets) | 92 * +------------------------------------+-------------------+ 93 * | (Code=0x04) | 1 | 94 * | Random Value | 16 | 95 */ 96 struct ble_sm_pair_random { 97 uint8_t value[16]; 98 } __attribute__((packed)); 99 100 /** 101 * | Parameter | Size (octets) | 102 * +------------------------------------+-------------------+ 103 * | (Code=0x05) | 1 | 104 * | Reason | 1 | 105 */ 106 struct ble_sm_pair_fail { 107 uint8_t reason; 108 } __attribute__((packed)); 109 110 /** 111 * | Parameter | Size (octets) | 112 * +------------------------------------+-------------------+ 113 * | (Code=0x06) | 1 | 114 * | ltk | 16 | 115 */ 116 struct ble_sm_enc_info { 117 uint8_t ltk[16]; 118 } __attribute__((packed)); 119 120 /** 121 * | Parameter | Size (octets) | 122 * +------------------------------------+-------------------+ 123 * | (Code=0x07) | 1 | 124 * | EDIV | 2 | 125 * | RAND | 8 | 126 */ 127 struct ble_sm_master_id { 128 uint16_t ediv; 129 uint64_t rand_val; 130 } __attribute__((packed)); 131 132 /** 133 * | Parameter | Size (octets) | 134 * +------------------------------------+-------------------+ 135 * | (Code=0x08) | 1 | 136 * | irk | 16 | 137 */ 138 struct ble_sm_id_info { 139 uint8_t irk[16]; 140 } __attribute__((packed)); 141 142 /** 143 * | Parameter | Size (octets) | 144 * +------------------------------------+-------------------+ 145 * | (Code=0x09) | 1 | 146 * | addr_type | 1 | 147 * | address | 6 | 148 */ 149 struct ble_sm_id_addr_info { 150 uint8_t addr_type; 151 uint8_t bd_addr[6]; 152 } __attribute__((packed)); 153 154 /** 155 * | Parameter | Size (octets) | 156 * +------------------------------------+-------------------+ 157 * | (Code=0x0A) | 1 | 158 * | csrk | 16 | 159 */ 160 struct ble_sm_sign_info { 161 uint8_t sig_key[16]; 162 } __attribute__((packed)); 163 164 /** 165 * | Parameter | Size (octets) | 166 * +------------------------------------+-------------------+ 167 * | (Code=0x0B) | 1 | 168 * | authreq | 1 | 169 */ 170 struct ble_sm_sec_req { 171 uint8_t authreq; 172 } __attribute__((packed)); 173 174 /** 175 * | Parameter | Size (octets) | 176 * +------------------------------------+-------------------+ 177 * | (Code=0x0c) | 1 | 178 * | Public Key X | 32 | 179 * | Public Key Y | 32 | 180 */ 181 struct ble_sm_public_key { 182 uint8_t x[32]; 183 uint8_t y[32]; 184 } __attribute__((packed)); 185 186 /** 187 * | Parameter | Size (octets) | 188 * +------------------------------------+-------------------+ 189 * | (Code=0x0d) | 1 | 190 * | DHKey Check | 16 | 191 */ 192 struct ble_sm_dhkey_check { 193 uint8_t value[16]; 194 } __attribute__((packed)); 195 196 #if NIMBLE_BLE_SM 197 198 #define BLE_SM_PROC_STATE_NONE ((uint8_t)-1) 199 200 #define BLE_SM_PROC_STATE_PAIR 0 201 #define BLE_SM_PROC_STATE_CONFIRM 1 202 #define BLE_SM_PROC_STATE_RANDOM 2 203 #define BLE_SM_PROC_STATE_LTK_START 3 204 #define BLE_SM_PROC_STATE_LTK_RESTORE 4 205 #define BLE_SM_PROC_STATE_ENC_START 5 206 #define BLE_SM_PROC_STATE_ENC_RESTORE 6 207 #define BLE_SM_PROC_STATE_KEY_EXCH 7 208 #define BLE_SM_PROC_STATE_SEC_REQ 8 209 #define BLE_SM_PROC_STATE_PUBLIC_KEY 9 210 #define BLE_SM_PROC_STATE_DHKEY_CHECK 10 211 #define BLE_SM_PROC_STATE_CNT 11 212 213 #define BLE_SM_PROC_F_INITIATOR 0x01 214 #define BLE_SM_PROC_F_IO_INJECTED 0x02 215 #define BLE_SM_PROC_F_ADVANCE_ON_IO 0x04 216 #define BLE_SM_PROC_F_AUTHENTICATED 0x08 217 #define BLE_SM_PROC_F_SC 0x10 218 #define BLE_SM_PROC_F_BONDING 0x20 219 220 #define BLE_SM_KE_F_ENC_INFO 0x01 221 #define BLE_SM_KE_F_MASTER_ID 0x02 222 #define BLE_SM_KE_F_ID_INFO 0x04 223 #define BLE_SM_KE_F_ADDR_INFO 0x08 224 #define BLE_SM_KE_F_SIGN_INFO 0x10 225 226 typedef uint8_t ble_sm_proc_flags; 227 228 struct ble_sm_keys { 229 unsigned ltk_valid:1; 230 unsigned ediv_rand_valid:1; 231 unsigned irk_valid:1; 232 unsigned csrk_valid:1; 233 unsigned addr_valid:1; 234 uint16_t ediv; 235 uint64_t rand_val; 236 uint8_t addr_type; 237 uint8_t key_size; 238 uint8_t ltk[16]; /* Little endian. */ 239 uint8_t irk[16]; /* Little endian. */ 240 uint8_t csrk[16]; /* Little endian. */ 241 uint8_t addr[6]; /* Little endian. */ 242 }; 243 244 struct ble_sm_proc { 245 STAILQ_ENTRY(ble_sm_proc) next; 246 247 ble_npl_time_t exp_os_ticks; 248 ble_sm_proc_flags flags; 249 uint16_t conn_handle; 250 uint8_t pair_alg; 251 uint8_t state; 252 uint8_t rx_key_flags; 253 uint8_t key_size; 254 255 uint8_t pair_req[sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_cmd)]; 256 uint8_t pair_rsp[sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_cmd)]; 257 uint8_t tk[16]; 258 uint8_t confirm_peer[16]; 259 uint8_t randm[16]; 260 uint8_t rands[16]; 261 uint8_t ltk[16]; /* Little endian. */ 262 struct ble_sm_keys our_keys; 263 struct ble_sm_keys peer_keys; 264 265 #if MYNEWT_VAL(BLE_SM_SC) 266 /* Secure connections. */ 267 uint8_t passkey_bits_exchanged; 268 uint8_t ri; 269 struct ble_sm_public_key pub_key_peer; 270 uint8_t mackey[16]; 271 uint8_t dhkey[32]; 272 #endif 273 }; 274 275 struct ble_sm_result { 276 int app_status; 277 uint8_t sm_err; 278 struct ble_gap_passkey_params passkey_params; 279 void *state_arg; 280 unsigned execute:1; 281 unsigned enc_cb:1; 282 unsigned persist_keys:1; 283 unsigned restore:1; 284 }; 285 286 #if MYNEWT_VAL(BLE_HS_DEBUG) 287 void ble_sm_dbg_set_next_pair_rand(uint8_t *next_pair_rand); 288 void ble_sm_dbg_set_next_ediv(uint16_t next_ediv); 289 void ble_sm_dbg_set_next_master_id_rand(uint64_t next_master_id_rand); 290 void ble_sm_dbg_set_next_ltk(uint8_t *next_ltk); 291 void ble_sm_dbg_set_next_csrk(uint8_t *next_csrk); 292 void ble_sm_dbg_set_sc_keys(uint8_t *pubkey, uint8_t *privkey); 293 #endif 294 295 int ble_sm_num_procs(void); 296 297 void ble_sm_pair_cmd_log(struct ble_sm_pair_cmd *cmd); 298 void ble_sm_pair_confirm_log(struct ble_sm_pair_confirm *cmd); 299 void ble_sm_pair_random_log(struct ble_sm_pair_random *cmd); 300 void ble_sm_pair_fail_log(struct ble_sm_pair_fail *cmd); 301 void ble_sm_enc_info_log(struct ble_sm_enc_info *cmd); 302 void ble_sm_master_id_log(struct ble_sm_master_id *cmd); 303 void ble_sm_id_info_log(struct ble_sm_id_info *cmd); 304 void ble_sm_id_addr_info_log(struct ble_sm_id_addr_info *cmd); 305 void ble_sm_sign_info_log(struct ble_sm_sign_info *cmd); 306 void ble_sm_sec_req_log(struct ble_sm_sec_req *cmd); 307 void ble_sm_public_key_log(struct ble_sm_public_key *cmd); 308 void ble_sm_dhkey_check_log(struct ble_sm_dhkey_check *cmd); 309 310 int ble_sm_alg_s1(uint8_t *k, uint8_t *r1, uint8_t *r2, uint8_t *out); 311 int ble_sm_alg_c1(uint8_t *k, uint8_t *r, 312 uint8_t *preq, uint8_t *pres, 313 uint8_t iat, uint8_t rat, 314 uint8_t *ia, uint8_t *ra, 315 uint8_t *out_enc_data); 316 int ble_sm_alg_f4(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t z, 317 uint8_t *out_enc_data); 318 int ble_sm_alg_g2(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t *y, 319 uint32_t *passkey); 320 int ble_sm_alg_f5(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t a1t, 321 uint8_t *a1, uint8_t a2t, uint8_t *a2, 322 uint8_t *mackey, uint8_t *ltk); 323 int ble_sm_alg_f6(const uint8_t *w, const uint8_t *n1, const uint8_t *n2, 324 const uint8_t *r, const uint8_t *iocap, uint8_t a1t, 325 const uint8_t *a1, uint8_t a2t, const uint8_t *a2, 326 uint8_t *check); 327 int ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y, 328 uint8_t *our_priv_key, uint8_t *out_dhkey); 329 int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv); 330 void ble_sm_alg_ecc_init(void); 331 332 void ble_sm_enc_change_rx(struct hci_encrypt_change *evt); 333 void ble_sm_enc_key_refresh_rx(struct hci_encrypt_key_refresh *evt); 334 int ble_sm_ltk_req_rx(struct hci_le_lt_key_req *evt); 335 336 #if MYNEWT_VAL(BLE_SM_LEGACY) 337 int ble_sm_lgcy_io_action(struct ble_sm_proc *proc, uint8_t *action); 338 void ble_sm_lgcy_confirm_exec(struct ble_sm_proc *proc, 339 struct ble_sm_result *res); 340 void ble_sm_lgcy_random_exec(struct ble_sm_proc *proc, 341 struct ble_sm_result *res); 342 void ble_sm_lgcy_random_rx(struct ble_sm_proc *proc, 343 struct ble_sm_result *res); 344 #else 345 #define ble_sm_lgcy_io_action(proc, action) (BLE_HS_ENOTSUP) 346 #define ble_sm_lgcy_confirm_exec(proc, res) 347 #define ble_sm_lgcy_random_exec(proc, res) 348 #define ble_sm_lgcy_random_rx(proc, res) 349 #endif 350 351 #if MYNEWT_VAL(BLE_SM_SC) 352 int ble_sm_sc_io_action(struct ble_sm_proc *proc, uint8_t *action); 353 void ble_sm_sc_confirm_exec(struct ble_sm_proc *proc, 354 struct ble_sm_result *res); 355 void ble_sm_sc_random_exec(struct ble_sm_proc *proc, 356 struct ble_sm_result *res); 357 void ble_sm_sc_random_rx(struct ble_sm_proc *proc, struct ble_sm_result *res); 358 void ble_sm_sc_public_key_exec(struct ble_sm_proc *proc, 359 struct ble_sm_result *res, 360 void *arg); 361 void ble_sm_sc_public_key_rx(uint16_t conn_handle, struct os_mbuf **rxom, 362 struct ble_sm_result *res); 363 void ble_sm_sc_dhkey_check_exec(struct ble_sm_proc *proc, 364 struct ble_sm_result *res, void *arg); 365 void ble_sm_sc_dhkey_check_rx(uint16_t conn_handle, struct os_mbuf **rxom, 366 struct ble_sm_result *res); 367 void ble_sm_sc_init(void); 368 #else 369 #define ble_sm_sc_io_action(proc, action) (BLE_HS_ENOTSUP) 370 #define ble_sm_sc_confirm_exec(proc, res) 371 #define ble_sm_sc_random_exec(proc, res) 372 #define ble_sm_sc_random_rx(proc, res) 373 #define ble_sm_sc_public_key_exec(proc, res, arg) 374 #define ble_sm_sc_public_key_rx(conn_handle, op, om, res) 375 #define ble_sm_sc_dhkey_check_exec(proc, res, arg) 376 #define ble_sm_sc_dhkey_check_rx(conn_handle, op, om, res) 377 #define ble_sm_sc_init() 378 379 #endif 380 381 struct ble_sm_proc *ble_sm_proc_find(uint16_t conn_handle, uint8_t state, 382 int is_initiator, 383 struct ble_sm_proc **out_prev); 384 int ble_sm_gen_pair_rand(uint8_t *pair_rand); 385 uint8_t *ble_sm_our_pair_rand(struct ble_sm_proc *proc); 386 uint8_t *ble_sm_peer_pair_rand(struct ble_sm_proc *proc); 387 int ble_sm_ioact_state(uint8_t action); 388 int ble_sm_proc_can_advance(struct ble_sm_proc *proc); 389 void ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res); 390 void ble_sm_confirm_advance(struct ble_sm_proc *proc); 391 void ble_sm_ia_ra(struct ble_sm_proc *proc, 392 uint8_t *out_iat, uint8_t *out_ia, 393 uint8_t *out_rat, uint8_t *out_ra); 394 395 int32_t ble_sm_timer(void); 396 void ble_sm_connection_broken(uint16_t conn_handle); 397 int ble_sm_pair_initiate(uint16_t conn_handle); 398 int ble_sm_slave_initiate(uint16_t conn_handle); 399 int ble_sm_enc_initiate(uint16_t conn_handle, const uint8_t *ltk, 400 uint16_t ediv, uint64_t rand_val, int auth); 401 int ble_sm_init(void); 402 403 #define BLE_SM_LOG_CMD(is_tx, cmd_name, conn_handle, log_cb, cmd) \ 404 BLE_HS_LOG_CMD((is_tx), "sm", (cmd_name), (conn_handle), (log_cb), (cmd)) 405 406 #else 407 408 #define ble_sm_enc_change_rx(evt) ((void)(evt)) 409 #define ble_sm_ltk_req_rx(evt) ((void)(evt)) 410 #define ble_sm_enc_key_refresh_rx(evt) ((void)(evt)) 411 412 #define ble_sm_timer() BLE_HS_FOREVER 413 #define ble_sm_connection_broken(conn_handle) 414 #define ble_sm_pair_initiate(conn_handle) BLE_HS_ENOTSUP 415 #define ble_sm_slave_initiate(conn_handle) BLE_HS_ENOTSUP 416 #define ble_sm_enc_initiate(conn_handle, ltk, ediv, rand_val, auth) \ 417 BLE_HS_ENOTSUP 418 419 #define ble_sm_init() 0 420 421 #endif 422 423 struct ble_l2cap_chan *ble_sm_create_chan(uint16_t handle); 424 void *ble_sm_cmd_get(uint8_t opcode, size_t len, struct os_mbuf **txom); 425 int ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom); 426 427 #ifdef __cplusplus 428 } 429 #endif 430 431 #endif 432