• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdint.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     const struct ble_sm_sc_oob_data *oob_data_local;
273     const struct ble_sm_sc_oob_data *oob_data_remote;
274 #endif
275 };
276 
277 struct ble_sm_result {
278     int app_status;
279     uint8_t sm_err;
280     struct ble_gap_passkey_params passkey_params;
281     void *state_arg;
282     unsigned execute : 1;
283     unsigned enc_cb : 1;
284     unsigned persist_keys : 1;
285     unsigned restore : 1;
286 };
287 
288 #if MYNEWT_VAL(BLE_HS_DEBUG)
289 void ble_sm_dbg_set_next_pair_rand(uint8_t *next_pair_rand);
290 void ble_sm_dbg_set_next_ediv(uint16_t next_ediv);
291 void ble_sm_dbg_set_next_master_id_rand(uint64_t next_master_id_rand);
292 void ble_sm_dbg_set_next_ltk(uint8_t *next_ltk);
293 void ble_sm_dbg_set_next_csrk(uint8_t *next_csrk);
294 void ble_sm_dbg_set_sc_keys(uint8_t *pubkey, uint8_t *privkey);
295 #endif
296 
297 int ble_sm_num_procs(void);
298 
299 int ble_sm_alg_s1(const uint8_t *k, const uint8_t *r1, const uint8_t *r2,
300                   uint8_t *out);
301 int ble_sm_alg_c1(const uint8_t *k, const uint8_t *r,
302                   const uint8_t *preq, const uint8_t *pres,
303                   uint8_t iat, uint8_t rat,
304                   const uint8_t *ia, const uint8_t *ra,
305                   uint8_t *out_enc_data);
306 int ble_sm_alg_f4(const uint8_t *u, const uint8_t *v, const uint8_t *x,
307                   uint8_t z, uint8_t *out_enc_data);
308 int ble_sm_alg_g2(const uint8_t *u, const uint8_t *v, const uint8_t *x,
309                   const uint8_t *y, uint32_t *passkey);
310 int ble_sm_alg_f5(const uint8_t *w, const uint8_t *n1, const uint8_t *n2,
311                   uint8_t a1t, const uint8_t *a1, uint8_t a2t,
312                   const uint8_t *a2, uint8_t *mackey, uint8_t *ltk);
313 int ble_sm_alg_f6(const uint8_t *w, const uint8_t *n1, const uint8_t *n2,
314                   const uint8_t *r, const uint8_t *iocap, uint8_t a1t,
315                   const uint8_t *a1, uint8_t a2t, const uint8_t *a2,
316                   uint8_t *check);
317 int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x,
318                          const uint8_t *peer_pub_key_y,
319                          const uint8_t *our_priv_key, uint8_t *out_dhkey);
320 int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv);
321 void ble_sm_alg_ecc_init(void);
322 
323 void ble_sm_enc_change_rx(const struct ble_hci_ev_enrypt_chg *ev);
324 void ble_sm_enc_key_refresh_rx(const struct ble_hci_ev_enc_key_refresh *ev);
325 int ble_sm_ltk_req_rx(const struct ble_hci_ev_le_subev_lt_key_req *ev);
326 
327 #if MYNEWT_VAL(BLE_SM_LEGACY)
328 int ble_sm_lgcy_io_action(struct ble_sm_proc *proc, uint8_t *action);
329 void ble_sm_lgcy_confirm_exec(struct ble_sm_proc *proc,
330                               struct ble_sm_result *res);
331 void ble_sm_lgcy_random_exec(struct ble_sm_proc *proc,
332                              struct ble_sm_result *res);
333 void ble_sm_lgcy_random_rx(struct ble_sm_proc *proc,
334                            struct ble_sm_result *res);
335 #else
336 #define ble_sm_lgcy_io_action(proc, action) (BLE_HS_ENOTSUP)
337 #define ble_sm_lgcy_confirm_exec(proc, res)
338 #define ble_sm_lgcy_random_exec(proc, res)
339 #define ble_sm_lgcy_random_rx(proc, res)
340 #endif
341 
342 #if MYNEWT_VAL(BLE_SM_SC)
343 int ble_sm_sc_io_action(struct ble_sm_proc *proc, uint8_t *action);
344 void ble_sm_sc_confirm_exec(struct ble_sm_proc *proc,
345                             struct ble_sm_result *res);
346 void ble_sm_sc_random_exec(struct ble_sm_proc *proc,
347                            struct ble_sm_result *res);
348 void ble_sm_sc_random_rx(struct ble_sm_proc *proc, struct ble_sm_result *res);
349 void ble_sm_sc_public_key_exec(struct ble_sm_proc *proc,
350                                struct ble_sm_result *res,
351                                void *arg);
352 void ble_sm_sc_public_key_rx(uint16_t conn_handle, struct os_mbuf **rxom,
353                              struct ble_sm_result *res);
354 void ble_sm_sc_dhkey_check_exec(struct ble_sm_proc *proc,
355                                 struct ble_sm_result *res, void *arg);
356 void ble_sm_sc_dhkey_check_rx(uint16_t conn_handle, struct os_mbuf **rxom,
357                               struct ble_sm_result *res);
358 bool ble_sm_sc_oob_data_check(struct ble_sm_proc *proc,
359                               bool oob_data_local_present,
360                               bool oob_data_remote_present);
361 void ble_sm_sc_oob_confirm(struct ble_sm_proc *proc, struct ble_sm_result *res);
362 void ble_sm_sc_init(void);
363 #else
364 #define ble_sm_sc_io_action(proc, action) (BLE_HS_ENOTSUP)
365 #define ble_sm_sc_confirm_exec(proc, res)
366 #define ble_sm_sc_random_exec(proc, res)
367 #define ble_sm_sc_random_rx(proc, res)
368 #define ble_sm_sc_public_key_exec(proc, res, arg)
369 #define ble_sm_sc_public_key_rx(conn_handle, op, om, res)
370 #define ble_sm_sc_dhkey_check_exec(proc, res, arg)
371 #define ble_sm_sc_dhkey_check_rx(conn_handle, op, om, res)
372 #define ble_sm_sc_init()
373 
374 #endif
375 
376 struct ble_sm_proc *ble_sm_proc_find(uint16_t conn_handle, uint8_t state,
377                                      int is_initiator,
378                                      struct ble_sm_proc **out_prev);
379 int ble_sm_gen_pair_rand(uint8_t *pair_rand);
380 uint8_t *ble_sm_our_pair_rand(struct ble_sm_proc *proc);
381 uint8_t *ble_sm_peer_pair_rand(struct ble_sm_proc *proc);
382 int ble_sm_ioact_state(uint8_t action);
383 int ble_sm_proc_can_advance(struct ble_sm_proc *proc);
384 void ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res);
385 void ble_sm_confirm_advance(struct ble_sm_proc *proc);
386 void ble_sm_ia_ra(struct ble_sm_proc *proc,
387                   uint8_t *out_iat, uint8_t *out_ia,
388                   uint8_t *out_rat, uint8_t *out_ra);
389 
390 int32_t ble_sm_timer(void);
391 void ble_sm_connection_broken(uint16_t conn_handle);
392 int ble_sm_pair_initiate(uint16_t conn_handle);
393 int ble_sm_slave_initiate(uint16_t conn_handle);
394 int ble_sm_enc_initiate(uint16_t conn_handle, uint8_t key_size,
395                         const uint8_t *ltk, uint16_t ediv,
396                         uint64_t rand_val, int auth);
397 int ble_sm_init(void);
398 #else
399 
400 #define ble_sm_enc_change_rx(evt) ((void)(evt))
401 #define ble_sm_ltk_req_rx(evt) ((void)(evt))
402 #define ble_sm_enc_key_refresh_rx(evt) ((void)(evt))
403 
404 #define ble_sm_timer() BLE_HS_FOREVER
405 #define ble_sm_connection_broken(conn_handle)
406 #define ble_sm_pair_initiate(conn_handle)   BLE_HS_ENOTSUP
407 #define ble_sm_slave_initiate(conn_handle)  BLE_HS_ENOTSUP
408 #define ble_sm_enc_initiate(conn_handle, keysize, ltk, ediv, rand_val, auth) \
409         BLE_HS_ENOTSUP
410 
411 #define ble_sm_init() 0
412 
413 #endif
414 
415 struct ble_l2cap_chan *ble_sm_create_chan(uint16_t handle);
416 void *ble_sm_cmd_get(uint8_t opcode, size_t len, struct os_mbuf **txom);
417 int ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom);
418 
419 #ifdef __cplusplus
420 }
421 #endif
422 
423 #endif
424