NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
ble_sm_priv.h
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 "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"
29#endif
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
36struct hci_le_lt_key_req;
37struct hci_encrypt_change;
38
39#define BLE_SM_MTU 65
40
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
55
56struct ble_sm_hdr {
57 uint8_t opcode;
58 uint8_t data[0];
59} __attribute__((packed));
60
74 uint8_t io_cap;
75 uint8_t oob_data_flag;
76 uint8_t authreq;
77 uint8_t max_enc_key_size;
78 uint8_t init_key_dist;
79 uint8_t resp_key_dist;
80} __attribute__((packed));
81
90 uint8_t value[16];
91} __attribute__((packed));
92
100 uint8_t value[16];
101} __attribute__((packed));
102
110 uint8_t reason;
111} __attribute__((packed));
112
120 uint8_t ltk[16];
121} __attribute__((packed));
122
131 uint16_t ediv;
132 uint64_t rand_val;
133} __attribute__((packed));
134
142 uint8_t irk[16];
143} __attribute__((packed));
144
153 uint8_t addr_type;
154 uint8_t bd_addr[6];
155} __attribute__((packed));
156
164 uint8_t sig_key[16];
165} __attribute__((packed));
166
174 uint8_t authreq;
175} __attribute__((packed));
176
185 uint8_t x[32];
186 uint8_t y[32];
187} __attribute__((packed));
188
196 uint8_t value[16];
197} __attribute__((packed));
198
199#if NIMBLE_BLE_SM
200
201#define BLE_SM_PROC_STATE_NONE ((uint8_t)-1)
202
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
215
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
222
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
228
229typedef uint8_t ble_sm_proc_flags;
230
231struct ble_sm_keys {
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;
238 uint16_t ediv;
239 uint64_t rand_val;
240 uint8_t addr_type;
241 uint8_t key_size;
242 uint8_t ltk[16]; /* Little endian. */
243 uint8_t irk[16]; /* Little endian. */
244 uint8_t csrk[16]; /* Little endian. */
245 uint8_t addr[6]; /* Little endian. */
246};
247
248struct ble_sm_proc {
249 STAILQ_ENTRY(ble_sm_proc) next;
250
251 ble_npl_time_t exp_os_ticks;
252 ble_sm_proc_flags flags;
253 uint16_t conn_handle;
254 uint8_t pair_alg;
255 uint8_t state;
256 uint8_t rx_key_flags;
257 uint8_t key_size;
258
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)];
261 uint8_t tk[16];
262 uint8_t confirm_peer[16];
263 uint8_t randm[16];
264 uint8_t rands[16];
265 uint8_t ltk[16]; /* Little endian. */
266 struct ble_sm_keys our_keys;
267 struct ble_sm_keys peer_keys;
268
269#if MYNEWT_VAL(BLE_SM_SC)
270 /* Secure connections. */
271 uint8_t passkey_bits_exchanged;
272 uint8_t ri;
273 struct ble_sm_public_key pub_key_peer;
274 uint8_t mackey[16];
275 uint8_t dhkey[32];
276 const struct ble_sm_sc_oob_data *oob_data_local;
277 const struct ble_sm_sc_oob_data *oob_data_remote;
278#endif
279};
280
281struct ble_sm_result {
282 int app_status;
283 uint8_t sm_err;
284 struct ble_gap_passkey_params passkey_params;
285 void *state_arg;
286 unsigned execute : 1;
287 unsigned enc_cb : 1;
288 unsigned bonded : 1;
289 unsigned restore : 1;
290 unsigned out_of_order : 1;
291};
292
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);
300#endif
301
302int ble_sm_num_procs(void);
303
304int ble_sm_alg_s1(const uint8_t *k, const uint8_t *r1, const uint8_t *r2,
305 uint8_t *out);
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,
321 uint8_t *check);
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);
327
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);
331
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);
340#else
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)
345#endif
346
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,
356 void *arg);
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);
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
381struct 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);
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);
394
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,
398 uint8_t *out);
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,
407 uint8_t *enc_data);
408int ble_sm_init(void);
409#else
410
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))
417
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) \
423 BLE_HS_ENOTSUP
424
425#define ble_sm_init() 0
426
427#define ble_sm_alg_encrypt(key, plaintext, enc_data) \
428 BLE_HS_ENOTSUP
429
430#endif
431
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);
435
436
437#ifdef __cplusplus
438}
439#endif
440
441#endif
Passkey query.
Definition ble_gap.h:368
uint8_t action
Definition ble_gap.h:376
Definition ble_gap.h:202
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
Definition os_mbuf.h:86