24#include "nimble/porting/nimble/include/syscfg/syscfg.h"
25#include "nimble/porting/nimble/include/os/queue.h"
26#include "nimble/nimble/include/nimble/nimble_opt.h"
27#if MYNEWT_VAL(ENC_ADV_DATA)
28#include "../include/host/ble_ead.h"
36struct hci_le_lt_key_req;
37struct hci_encrypt_change;
41#define BLE_SM_OP_PAIR_REQ 0x01
42#define BLE_SM_OP_PAIR_RSP 0x02
43#define BLE_SM_OP_PAIR_CONFIRM 0x03
44#define BLE_SM_OP_PAIR_RANDOM 0x04
45#define BLE_SM_OP_PAIR_FAIL 0x05
46#define BLE_SM_OP_ENC_INFO 0x06
47#define BLE_SM_OP_MASTER_ID 0x07
48#define BLE_SM_OP_IDENTITY_INFO 0x08
49#define BLE_SM_OP_IDENTITY_ADDR_INFO 0x09
50#define BLE_SM_OP_SIGN_INFO 0x0a
51#define BLE_SM_OP_SEC_REQ 0x0b
52#define BLE_SM_OP_PAIR_PUBLIC_KEY 0x0c
53#define BLE_SM_OP_PAIR_DHKEY_CHECK 0x0d
54#define BLE_SM_OP_PAIR_KEYPRESS_NOTIFY 0x0e
59} __attribute__((packed));
75 uint8_t oob_data_flag;
77 uint8_t max_enc_key_size;
78 uint8_t init_key_dist;
79 uint8_t resp_key_dist;
80} __attribute__((packed));
91} __attribute__((packed));
101} __attribute__((packed));
111} __attribute__((packed));
121} __attribute__((packed));
133} __attribute__((packed));
143} __attribute__((packed));
155} __attribute__((packed));
165} __attribute__((packed));
175} __attribute__((packed));
187} __attribute__((packed));
197} __attribute__((packed));
201#define BLE_SM_PROC_STATE_NONE ((uint8_t)-1)
203#define BLE_SM_PROC_STATE_PAIR 0
204#define BLE_SM_PROC_STATE_CONFIRM 1
205#define BLE_SM_PROC_STATE_RANDOM 2
206#define BLE_SM_PROC_STATE_LTK_START 3
207#define BLE_SM_PROC_STATE_LTK_RESTORE 4
208#define BLE_SM_PROC_STATE_ENC_START 5
209#define BLE_SM_PROC_STATE_ENC_RESTORE 6
210#define BLE_SM_PROC_STATE_KEY_EXCH 7
211#define BLE_SM_PROC_STATE_SEC_REQ 8
212#define BLE_SM_PROC_STATE_PUBLIC_KEY 9
213#define BLE_SM_PROC_STATE_DHKEY_CHECK 10
214#define BLE_SM_PROC_STATE_CNT 11
216#define BLE_SM_PROC_F_INITIATOR 0x01
217#define BLE_SM_PROC_F_IO_INJECTED 0x02
218#define BLE_SM_PROC_F_ADVANCE_ON_IO 0x04
219#define BLE_SM_PROC_F_AUTHENTICATED 0x08
220#define BLE_SM_PROC_F_SC 0x10
221#define BLE_SM_PROC_F_BONDING 0x20
223#define BLE_SM_KE_F_ENC_INFO 0x01
224#define BLE_SM_KE_F_MASTER_ID 0x02
225#define BLE_SM_KE_F_ID_INFO 0x04
226#define BLE_SM_KE_F_ADDR_INFO 0x08
227#define BLE_SM_KE_F_SIGN_INFO 0x10
229typedef uint8_t ble_sm_proc_flags;
232 unsigned ltk_valid:1;
233 unsigned ediv_rand_valid:1;
234 unsigned irk_valid:1;
235 unsigned csrk_valid:1;
236 unsigned addr_valid:1;
237 uint32_t sign_counter;
249 STAILQ_ENTRY(ble_sm_proc) next;
251 ble_npl_time_t exp_os_ticks;
252 ble_sm_proc_flags flags;
253 uint16_t conn_handle;
256 uint8_t rx_key_flags;
259 uint8_t pair_req[
sizeof(
struct ble_sm_hdr) + sizeof(struct
ble_sm_pair_cmd)];
260 uint8_t pair_rsp[
sizeof(
struct ble_sm_hdr) + sizeof(struct
ble_sm_pair_cmd)];
262 uint8_t confirm_peer[16];
266 struct ble_sm_keys our_keys;
267 struct ble_sm_keys peer_keys;
269#if MYNEWT_VAL(BLE_SM_SC)
271 uint8_t passkey_bits_exchanged;
276 const struct ble_sm_sc_oob_data *oob_data_local;
277 const struct ble_sm_sc_oob_data *oob_data_remote;
281struct ble_sm_result {
286 unsigned execute : 1;
289 unsigned restore : 1;
290 unsigned out_of_order : 1;
293#if MYNEWT_VAL(BLE_HS_DEBUG)
294void ble_sm_dbg_set_next_pair_rand(uint8_t *next_pair_rand);
295void ble_sm_dbg_set_next_ediv(uint16_t next_ediv);
296void ble_sm_dbg_set_next_master_id_rand(uint64_t next_master_id_rand);
297void ble_sm_dbg_set_next_ltk(uint8_t *next_ltk);
298void ble_sm_dbg_set_next_csrk(uint8_t *next_csrk);
299void ble_sm_dbg_set_sc_keys(uint8_t *pubkey, uint8_t *privkey);
302int ble_sm_num_procs(
void);
304int ble_sm_alg_s1(
const uint8_t *k,
const uint8_t *r1,
const uint8_t *r2,
306int ble_sm_alg_c1(
const uint8_t *k,
const uint8_t *r,
307 const uint8_t *preq,
const uint8_t *pres,
308 uint8_t iat, uint8_t rat,
309 const uint8_t *ia,
const uint8_t *ra,
310 uint8_t *out_enc_data);
311int ble_sm_alg_f4(
const uint8_t *u,
const uint8_t *v,
const uint8_t *x,
312 uint8_t z, uint8_t *out_enc_data);
313int ble_sm_alg_g2(
const uint8_t *u,
const uint8_t *v,
const uint8_t *x,
314 const uint8_t *y, uint32_t *passkey);
315int ble_sm_alg_f5(
const uint8_t *w,
const uint8_t *n1,
const uint8_t *n2,
316 uint8_t a1t,
const uint8_t *a1, uint8_t a2t,
317 const uint8_t *a2, uint8_t *mackey, uint8_t *ltk);
318int ble_sm_alg_f6(
const uint8_t *w,
const uint8_t *n1,
const uint8_t *n2,
319 const uint8_t *r,
const uint8_t *iocap, uint8_t a1t,
320 const uint8_t *a1, uint8_t a2t,
const uint8_t *a2,
322int ble_sm_alg_gen_dhkey(
const uint8_t *peer_pub_key_x,
323 const uint8_t *peer_pub_key_y,
324 const uint8_t *our_priv_key, uint8_t *out_dhkey);
325int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv);
326void ble_sm_alg_ecc_init(
void);
328void ble_sm_enc_change_rx(
const struct ble_hci_ev_enrypt_chg *ev);
329void ble_sm_enc_key_refresh_rx(
const struct ble_hci_ev_enc_key_refresh *ev);
330int ble_sm_ltk_req_rx(
const struct ble_hci_ev_le_subev_lt_key_req *ev);
332#if MYNEWT_VAL(BLE_SM_LEGACY)
333int ble_sm_lgcy_io_action(
struct ble_sm_proc *proc, uint8_t *
action);
334void ble_sm_lgcy_confirm_exec(
struct ble_sm_proc *proc,
335 struct ble_sm_result *res);
336void ble_sm_lgcy_random_exec(
struct ble_sm_proc *proc,
337 struct ble_sm_result *res);
338void ble_sm_lgcy_random_rx(
struct ble_sm_proc *proc,
339 struct ble_sm_result *res);
341#define ble_sm_lgcy_io_action(proc, action) (BLE_HS_ENOTSUP)
342#define ble_sm_lgcy_confirm_exec(proc, res)
343#define ble_sm_lgcy_random_exec(proc, res)
344#define ble_sm_lgcy_random_rx(proc, res)
347#if MYNEWT_VAL(BLE_SM_SC)
348int ble_sm_sc_io_action(
struct ble_sm_proc *proc, uint8_t *
action);
349void ble_sm_sc_confirm_exec(
struct ble_sm_proc *proc,
350 struct ble_sm_result *res);
351void ble_sm_sc_random_exec(
struct ble_sm_proc *proc,
352 struct ble_sm_result *res);
353void ble_sm_sc_random_rx(
struct ble_sm_proc *proc,
struct ble_sm_result *res);
354void ble_sm_sc_public_key_exec(
struct ble_sm_proc *proc,
355 struct ble_sm_result *res,
357void ble_sm_sc_public_key_rx(uint16_t conn_handle,
struct os_mbuf **rxom,
358 struct ble_sm_result *res);
359void ble_sm_sc_dhkey_check_exec(
struct ble_sm_proc *proc,
360 struct ble_sm_result *res,
void *arg);
361void ble_sm_sc_dhkey_check_rx(uint16_t conn_handle,
struct os_mbuf **rxom,
362 struct ble_sm_result *res);
363bool ble_sm_sc_oob_data_check(
struct ble_sm_proc *proc,
364 bool oob_data_local_present,
365 bool oob_data_remote_present);
366void ble_sm_sc_oob_confirm(
struct ble_sm_proc *proc,
struct ble_sm_result *res);
367void ble_sm_sc_init(
void);
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()
381struct ble_sm_proc *ble_sm_proc_find(uint16_t conn_handle, uint8_t state,
383 struct ble_sm_proc **out_prev);
384int ble_sm_gen_pair_rand(uint8_t *pair_rand);
385uint8_t *ble_sm_our_pair_rand(
struct ble_sm_proc *proc);
386uint8_t *ble_sm_peer_pair_rand(
struct ble_sm_proc *proc);
387int ble_sm_ioact_state(uint8_t action);
388int ble_sm_proc_can_advance(
struct ble_sm_proc *proc);
389void ble_sm_process_result(uint16_t conn_handle,
struct ble_sm_result *res);
390void ble_sm_confirm_advance(
struct ble_sm_proc *proc);
391void 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);
395int ble_sm_incr_our_sign_counter(uint16_t conn_handle);
396int ble_sm_incr_peer_sign_counter(uint16_t conn_handle);
397int ble_sm_alg_aes_cmac(
const uint8_t *key,
const uint8_t *in,
size_t len,
399int32_t ble_sm_timer(
void);
400void ble_sm_connection_broken(uint16_t conn_handle);
401int ble_sm_pair_initiate(uint16_t conn_handle);
402int ble_sm_slave_initiate(uint16_t conn_handle);
403int ble_sm_enc_initiate(uint16_t conn_handle, uint8_t key_size,
404 const uint8_t *ltk, uint16_t ediv,
405 uint64_t rand_val,
int auth);
406int ble_sm_alg_encrypt(
const uint8_t *key,
const uint8_t *plaintext,
408int ble_sm_init(
void);
411#define ble_sm_incr_our_sign_counter(conn_handle) BLE_HS_ENOTSUP
412#define ble_sm_incr_peer_sign_counter(conn_handle) BLE_HS_ENOTSUP
413#define ble_sm_alg_aes_cmac(key, in, len, out) BLE_HS_ENOTSUP
414#define ble_sm_enc_change_rx(evt) ((void)(evt))
415#define ble_sm_ltk_req_rx(evt) ((void)(evt))
416#define ble_sm_enc_key_refresh_rx(evt) ((void)(evt))
418#define ble_sm_timer() BLE_HS_FOREVER
419#define ble_sm_connection_broken(conn_handle)
420#define ble_sm_pair_initiate(conn_handle) BLE_HS_ENOTSUP
421#define ble_sm_slave_initiate(conn_handle) BLE_HS_ENOTSUP
422#define ble_sm_enc_initiate(conn_handle, keysize, ltk, ediv, rand_val, auth) \
425#define ble_sm_init() 0
427#define ble_sm_alg_encrypt(key, plaintext, enc_data) \
432struct ble_l2cap_chan *ble_sm_create_chan(uint16_t handle);
433void *ble_sm_cmd_get(uint8_t opcode,
size_t len,
struct os_mbuf **txom);
434int ble_sm_tx(uint16_t conn_handle,
struct os_mbuf *txom);
Passkey query.
Definition ble_gap.h:368
uint8_t action
Definition ble_gap.h:376
Definition ble_sm_priv.h:195
Definition ble_sm_priv.h:119
Definition ble_sm_priv.h:152
Definition ble_sm_priv.h:141
Definition ble_sm_priv.h:130
Definition ble_sm_priv.h:73
Definition ble_sm_priv.h:89
Definition ble_sm_priv.h:109
Definition ble_sm_priv.h:99
Definition ble_sm_priv.h:184
Definition ble_sm_priv.h:173
Definition ble_sm_priv.h:163