1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains internally used SMP definitions
22  *
23  ******************************************************************************/
24 #ifndef SMP_INT_H
25 #define SMP_INT_H
26 
27 #include <bluetooth/log.h>
28 
29 #include <cstdint>
30 
31 #include "macros.h"
32 #include "osi/include/alarm.h"
33 #include "stack/include/bt_hdr.h"
34 #include "stack/include/bt_octets.h"
35 #include "stack/include/smp_api_types.h"
36 #include "types/hci_role.h"
37 #include "types/raw_address.h"
38 
39 typedef enum : uint16_t {
40   SMP_METRIC_COMMAND_LE_FLAG = 0x0000,
41   SMP_METRIC_COMMAND_BR_FLAG = 0x0100,
42   SMP_METRIC_COMMAND_LE_PAIRING_CMPL = 0xFF00,
43   SMP_METRIC_COMMAND_BR_PAIRING_CMPL = 0xFF01,
44 } tSMP_METRIC_COMMAND;
45 
46 constexpr uint16_t SMP_METRIC_STATUS_INTERNAL_FLAG = 0x0100;
47 
48 typedef enum : uint8_t {
49   /* Legacy mode */
50   SMP_MODEL_ENCRYPTION_ONLY = 0, /* Just Works model */
51   SMP_MODEL_PASSKEY = 1,         /* Passkey Entry model, input the key */
52   SMP_MODEL_OOB = 2,             /* OOB model */
53   SMP_MODEL_KEY_NOTIF = 3,       /* Passkey Entry model, display the key */
54   /* Secure connections mode */
55   SMP_MODEL_SEC_CONN_JUSTWORKS = 4,   /* Just Works model */
56   SMP_MODEL_SEC_CONN_NUM_COMP = 5,    /* Numeric Comparison model */
57   SMP_MODEL_SEC_CONN_PASSKEY_ENT = 6, /* Passkey Entry model, */
58   /* this side inputs the key */
59   SMP_MODEL_SEC_CONN_PASSKEY_DISP = 7, /* Passkey Entry model, */
60   /* this side displays the key */
61   SMP_MODEL_SEC_CONN_OOB = 8, /* Secure Connections mode, OOB model */
62   SMP_MODEL_OUT_OF_RANGE = 9,
63 } tSMP_ASSO_MODEL;
64 
65 #define SMP_WAIT_FOR_RSP_TIMEOUT_MS (30 * 1000)
66 #define SMP_DELAYED_AUTH_TIMEOUT_MS 500
67 
68 /* SMP command code */
69 typedef enum : uint8_t {
70   SMP_OPCODE_PAIRING_REQ = 0x01,
71   SMP_OPCODE_PAIRING_RSP = 0x02,
72   SMP_OPCODE_CONFIRM = 0x03,
73   SMP_OPCODE_RAND = 0x04,
74   SMP_OPCODE_PAIRING_FAILED = 0x05,
75   SMP_OPCODE_ENCRYPT_INFO = 0x06,
76   SMP_OPCODE_CENTRAL_ID = 0x07,
77   SMP_OPCODE_IDENTITY_INFO = 0x08,
78   SMP_OPCODE_ID_ADDR = 0x09,
79   SMP_OPCODE_SIGN_INFO = 0x0A,
80   SMP_OPCODE_SEC_REQ = 0x0B,
81   SMP_OPCODE_PAIR_PUBLIC_KEY = 0x0C,
82   SMP_OPCODE_PAIR_DHKEY_CHECK = 0x0D,
83   SMP_OPCODE_PAIR_KEYPR_NOTIF = 0x0E,
84   SMP_OPCODE_MAX = SMP_OPCODE_PAIR_KEYPR_NOTIF,
85   SMP_OPCODE_MIN = SMP_OPCODE_PAIRING_REQ,
86   // NOTE: For some reason this is outside the MAX/MIN values
87   SMP_OPCODE_PAIR_COMMITM = 0x0F,
88 } tSMP_OPCODE;
89 
smp_opcode_text(const tSMP_OPCODE opcode)90 inline std::string smp_opcode_text(const tSMP_OPCODE opcode) {
91   switch (opcode) {
92     CASE_RETURN_TEXT(SMP_OPCODE_PAIRING_REQ);
93     CASE_RETURN_TEXT(SMP_OPCODE_PAIRING_RSP);
94     CASE_RETURN_TEXT(SMP_OPCODE_CONFIRM);
95     CASE_RETURN_TEXT(SMP_OPCODE_RAND);
96     CASE_RETURN_TEXT(SMP_OPCODE_PAIRING_FAILED);
97     CASE_RETURN_TEXT(SMP_OPCODE_ENCRYPT_INFO);
98     CASE_RETURN_TEXT(SMP_OPCODE_CENTRAL_ID);
99     CASE_RETURN_TEXT(SMP_OPCODE_IDENTITY_INFO);
100     CASE_RETURN_TEXT(SMP_OPCODE_ID_ADDR);
101     CASE_RETURN_TEXT(SMP_OPCODE_SIGN_INFO);
102     CASE_RETURN_TEXT(SMP_OPCODE_SEC_REQ);
103     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_PUBLIC_KEY);
104     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_DHKEY_CHECK);
105     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_KEYPR_NOTIF);
106     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_COMMITM);
107     default:
108       return base::StringPrintf("UNKNOWN[%hhu]", opcode);
109   }
110 }
111 
112 /* SMP events */
113 typedef enum : uint8_t {
114   SMP_NOP_EVT = 0,
115   SMP_CONFIRM_EVT = SMP_OPCODE_CONFIRM,  // 0x03
116   SMP_RAND_EVT = SMP_OPCODE_RAND,        // 0x04
117 
118   SMP_PAIR_COMMITM_EVT = SMP_OPCODE_PAIR_COMMITM,     // 0x0f
119   SMP_SELF_DEF_EVT = (SMP_PAIR_COMMITM_EVT + 1),      // 0x10
120   SMP_KEY_READY_EVT = (SMP_SELF_DEF_EVT),             // 0x04
121   SMP_ENCRYPTED_EVT = (SMP_SELF_DEF_EVT + 1),         // 0x05
122   SMP_L2CAP_CONN_EVT = (SMP_SELF_DEF_EVT + 2),        // 0x06
123   SMP_L2CAP_DISCONN_EVT = (SMP_SELF_DEF_EVT + 3),     // 0x07
124   SMP_IO_RSP_EVT = (SMP_SELF_DEF_EVT + 4),            // 0x08
125   SMP_API_SEC_GRANT_EVT = (SMP_SELF_DEF_EVT + 5),     // 0x09
126   SMP_TK_REQ_EVT = (SMP_SELF_DEF_EVT + 6),            // 0x0a
127   SMP_AUTH_CMPL_EVT = (SMP_SELF_DEF_EVT + 7),         // 0x0b
128   SMP_ENC_REQ_EVT = (SMP_SELF_DEF_EVT + 8),           // 0x0c
129   SMP_BOND_REQ_EVT = (SMP_SELF_DEF_EVT + 9),          // 0x0d
130   SMP_DISCARD_SEC_REQ_EVT = (SMP_SELF_DEF_EVT + 10),  // 0x0e
131 
132   SMP_BR_PAIR_KEYPR_NOTIF_EVT = SMP_OPCODE_PAIR_KEYPR_NOTIF,
133   /* not over BR/EDR */                                     // 0x0e
134   SMP_BR_SELF_DEF_EVT = SMP_BR_PAIR_KEYPR_NOTIF_EVT,        // 0x0e
135   SMP_BR_KEY_READY_EVT = (SMP_BR_SELF_DEF_EVT + 1),         // 0x0f
136   SMP_BR_ENCRYPTED_EVT = (SMP_BR_SELF_DEF_EVT + 2),         // 0x10
137   SMP_BR_L2CAP_CONN_EVT = (SMP_BR_SELF_DEF_EVT + 3),        // 0x11
138   SMP_BR_L2CAP_DISCONN_EVT = (SMP_BR_SELF_DEF_EVT + 4),     // 0x12
139   SMP_BR_KEYS_RSP_EVT = (SMP_BR_SELF_DEF_EVT + 5),          // 0x13
140   SMP_BR_API_SEC_GRANT_EVT = (SMP_BR_SELF_DEF_EVT + 6),     // 0x14
141   SMP_BR_TK_REQ_EVT = (SMP_BR_SELF_DEF_EVT + 7),            // 0x15
142   SMP_BR_AUTH_CMPL_EVT = (SMP_BR_SELF_DEF_EVT + 8),         // 0x16
143   SMP_BR_ENC_REQ_EVT = (SMP_BR_SELF_DEF_EVT + 9),           // 0x17
144   SMP_BR_BOND_REQ_EVT = (SMP_BR_SELF_DEF_EVT + 10),         // 0x18
145   SMP_BR_DISCARD_SEC_REQ_EVT = (SMP_BR_SELF_DEF_EVT + 11),  // 0x19
146   SMP_BR_MAX_EVT = (SMP_BR_SELF_DEF_EVT + 12),              // 0x1a
147 
148   SMP_PAIR_DHKEY_CHCK_EVT = SMP_OPCODE_PAIR_DHKEY_CHECK,  // 0x0d
149 
150   /* request to start public  key exchange */
151   SMP_PUBL_KEY_EXCH_REQ_EVT = (SMP_SELF_DEF_EVT + 11),  // 0x1b
152 
153   /* local public key created */
154   SMP_LOC_PUBL_KEY_CRTD_EVT = (SMP_SELF_DEF_EVT + 12),  // 0x1c
155 
156   /* both local and peer public keys are saved in cb */
157   SMP_BOTH_PUBL_KEYS_RCVD_EVT = (SMP_SELF_DEF_EVT + 13),  // 0x1d
158 
159   /* DHKey computation is completed, time to start SC phase1 */
160   SMP_SC_DHKEY_CMPLT_EVT = (SMP_SELF_DEF_EVT + 14),  // 0x1d
161 
162   /* new local nonce is generated and saved in p_cb->rand */
163   SMP_HAVE_LOC_NONCE_EVT = (SMP_SELF_DEF_EVT + 15),  // 0x1f
164 
165   /* time to start SC phase2 */
166   SMP_SC_PHASE1_CMPLT_EVT = (SMP_SELF_DEF_EVT + 16),  // 0x20
167 
168   /* request to calculate number for user check. Used only in the numeric
169    * compare protocol */
170   SMP_SC_CALC_NC_EVT = (SMP_SELF_DEF_EVT + 17),  // 0x21
171 
172   /* Request to display the number for user check to the user.*/
173   /* Used only in the numeric compare protocol */
174   SMP_SC_DSPL_NC_EVT = (SMP_SELF_DEF_EVT + 18),  // 0x22
175 
176   /* user confirms 'OK' numeric comparison request */
177   SMP_SC_NC_OK_EVT = (SMP_SELF_DEF_EVT + 19),  // 0x23
178 
179   /* both local and peer DHKey Checks are already present - it is used on
180    * peripheral to prevent a race condition */
181   SMP_SC_2_DHCK_CHKS_PRES_EVT = (SMP_SELF_DEF_EVT + 20),  // 0x24
182 
183   /* same meaning as SMP_KEY_READY_EVT to separate between SC and legacy actions
184    */
185   SMP_SC_KEY_READY_EVT = (SMP_SELF_DEF_EVT + 21),             // 0x25
186   SMP_KEYPRESS_NOTIFICATION_EVENT = (SMP_SELF_DEF_EVT + 22),  // 0x26
187 
188   /* SC OOB data from some repository is provided */
189   SMP_SC_OOB_DATA_EVT = (SMP_SELF_DEF_EVT + 23),  // 0x27
190 
191   SMP_CR_LOC_SC_OOB_DATA_EVT = (SMP_SELF_DEF_EVT + 24),  // 0x28
192   SMP_SIRK_DEVICE_VALID_EVT = (SMP_SELF_DEF_EVT + 25),   // 0x29
193   SMP_MAX_EVT = SMP_SIRK_DEVICE_VALID_EVT,               // 0x29
194 } tSMP_EVENT;
195 typedef tSMP_EVENT tSMP_BR_EVENT;
196 
197 /* Assumption it's only using the low 8 bits, if bigger than that, need to
198  * expand it to 16 bits */
199 #define SMP_SEC_KEY_MASK 0x00ff
200 
201 /* SMP pairing state */
202 enum {
203   SMP_STATE_IDLE,
204   SMP_STATE_WAIT_APP_RSP,
205   SMP_STATE_SEC_REQ_PENDING,
206   SMP_STATE_PAIR_REQ_RSP,
207   SMP_STATE_WAIT_CONFIRM,
208   SMP_STATE_CONFIRM,
209   SMP_STATE_RAND,
210   SMP_STATE_PUBLIC_KEY_EXCH,
211   SMP_STATE_SEC_CONN_PHS1_START,
212   SMP_STATE_WAIT_COMMITMENT,
213   SMP_STATE_WAIT_NONCE,
214   SMP_STATE_SEC_CONN_PHS2_START,
215   SMP_STATE_WAIT_DHK_CHECK,
216   SMP_STATE_DHK_CHECK,
217   SMP_STATE_ENCRYPTION_PENDING,
218   SMP_STATE_BOND_PENDING,
219   SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA,
220   SMP_STATE_MAX
221 };
222 typedef uint8_t tSMP_STATE;
223 
224 /* SMP over BR/EDR pairing states */
225 enum {
226   SMP_BR_STATE_IDLE = SMP_STATE_IDLE,
227   SMP_BR_STATE_WAIT_APP_RSP,
228   SMP_BR_STATE_PAIR_REQ_RSP,
229   SMP_BR_STATE_BOND_PENDING,
230   SMP_BR_STATE_MAX
231 };
232 typedef uint8_t tSMP_BR_STATE;
233 
234 enum {
235   SMP_KEY_TYPE_TK,
236   SMP_KEY_TYPE_CFM,
237   SMP_KEY_TYPE_CMP,
238   SMP_KEY_TYPE_PEER_DHK_CHCK,
239   SMP_KEY_TYPE_STK,
240   SMP_KEY_TYPE_LTK
241 };
242 typedef struct {
243   uint8_t key_type;
244   uint8_t* p_data;
245 } tSMP_KEY;
246 
247 typedef union {
248   uint8_t* p_data; /* uint8_t type data pointer */
249   tSMP_KEY key;
250   tSMP_STATUS status;
251   uint32_t passkey;
252   tSMP_OOB_DATA_TYPE req_oob_type;
253 } tSMP_INT_DATA;
254 
255 /* internal status mask */
256 #define SMP_PAIR_FLAGS_WE_STARTED_DD (1)
257 #define SMP_PAIR_FLAGS_PEER_STARTED_DD (1 << 1)
258 #define SMP_PAIR_FLAGS_CMD_CONFIRM_RCVD (1 << SMP_OPCODE_CONFIRM) /* 1 << 3 */
259 #define SMP_PAIR_FLAG_ENC_AFTER_PAIR (1 << 4)
260 #define SMP_PAIR_FLAG_HAVE_PEER_DHK_CHK \
261   (1 << 5) /* used on peripheral to resolve race condition */
262 #define SMP_PAIR_FLAG_HAVE_PEER_PUBL_KEY \
263   (1 << 6) /* used on peripheral to resolve race condition */
264 #define SMP_PAIR_FLAG_HAVE_PEER_COMM \
265   (1 << 7) /* used to resolve race condition */
266 #define SMP_PAIR_FLAG_HAVE_LOCAL_PUBL_KEY \
267   (1 << 8) /* used on peripheral to resolve race condition */
268 
269 #define SMP_PAIR_FLAGS_CMD_CONFIRM_SENT (1 << 9)
270 
271 /* check if authentication requirement need MITM protection */
272 #define SMP_NO_MITM_REQUIRED(x) (((x)&SMP_AUTH_YN_BIT) == 0)
273 
274 /* SMP control block */
275 class tSMP_CB {
276  public:
277   void init(uint8_t security_mode);
278   void reset();
279 
280  public:
281   uint8_t init_security_mode{0};
282   tSMP_CALLBACK* p_callback;
283   alarm_t* smp_rsp_timer_ent;
284   RawAddress pairing_bda;
285   tSMP_STATE state;
286   bool derive_lk;
287   bool id_addr_rcvd;
288   tBLE_ADDR_TYPE id_addr_type;
289   RawAddress id_addr;
290   bool smp_over_br;
291   tSMP_BR_STATE br_state; /* if SMP over BR/ERD has priority over SMP */
292   uint8_t failure;
293   tSMP_STATUS status;
294   tHCI_ROLE role;
295   uint16_t flags;
296   tSMP_EVT cb_evt;
297   tSMP_SEC_LEVEL sec_level;
298   bool connect_initialized;
299   Octet16 confirm;
300   Octet16 rconfirm;
301   Octet16 rrand; /* for SC this is peer nonce */
302   Octet16 rand;  /* for SC this is local nonce */
303   BT_OCTET32 private_key;
304   BT_OCTET32 dhkey;
305   Octet16 commitment;
306   Octet16 remote_commitment;
307   Octet16 local_random; /* local randomizer - passkey or OOB randomizer */
308   Octet16 peer_random;  /* peer randomizer - passkey or OOB randomizer */
309   Octet16 dhkey_check;
310   Octet16 remote_dhkey_check;
311   tSMP_PUBLIC_KEY loc_publ_key;
312   tSMP_PUBLIC_KEY peer_publ_key;
313   tSMP_OOB_DATA_TYPE req_oob_type;
314   tSMP_SC_OOB_DATA sc_oob_data;
315   tSMP_IO_CAP peer_io_caps;
316   tSMP_IO_CAP local_io_capability;
317   tSMP_OOB_FLAG peer_oob_flag;
318   tSMP_OOB_FLAG loc_oob_flag;
319   tSMP_AUTH_REQ peer_auth_req;
320   tSMP_AUTH_REQ loc_auth_req;
321 
322   bool sc_only_mode_locally_required; /* true if sc_only required required
323                                          locally */
324   bool sc_mode_required_by_peer;      /* true if peer requires sc in pair_req or
325                                          pair_rsp */
326 
327   /* either in Secure Connections mode or not at all */
328   tSMP_ASSO_MODEL selected_association_model;
329   bool key_derivation_h7_used;
330   bool le_sc_kp_notif_is_used;
331   tSMP_SC_KEY_TYPE local_keypress_notification;
332   tSMP_SC_KEY_TYPE peer_keypress_notification;
333   uint8_t
334       round; /* authentication stage 1 round for passkey association model */
335   uint32_t number_to_display;
336   Octet16 mac_key;
337   uint8_t peer_enc_size;
338   uint8_t loc_enc_size;
339   uint8_t peer_i_key;
340   uint8_t peer_r_key;
341   uint8_t local_i_key;
342   uint8_t local_r_key;
343 
344   Octet16 tk;
345   Octet16 ltk;
346   uint16_t div;
347   Octet16 csrk; /* storage for local CSRK */
348   uint16_t ediv;
349   BT_OCTET8 enc_rand;
350   tBLE_ADDR_TYPE addr_type;
351   RawAddress local_bda;
352   bool is_pair_cancel;
353   bool discard_sec_req;
354   uint8_t rcvd_cmd_code;
355   uint8_t rcvd_cmd_len;
356   uint16_t total_tx_unacked;
357   bool wait_for_authorization_complete;
358   tSMP_STATUS cert_failure; /*failure case for certification */
359   alarm_t* delayed_auth_timer_ent;
360   tBLE_BD_ADDR pairing_ble_bd_addr;
361 };
362 
363 /* Server Action functions are of this type */
364 typedef void (*tSMP_ACT)(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
365 
366 extern tSMP_CB smp_cb;
367 
368 /* smp main */
369 bool smp_sm_event(tSMP_CB* p_cb, tSMP_EVENT event, tSMP_INT_DATA* p_data);
370 
371 tSMP_STATE smp_get_state(void);
372 void smp_set_state(tSMP_STATE state);
373 
374 /* smp_br_main */
375 void smp_br_state_machine_event(tSMP_CB* p_cb, tSMP_BR_EVENT event,
376                                 tSMP_INT_DATA* p_data);
377 tSMP_BR_STATE smp_get_br_state(void);
378 void smp_set_br_state(tSMP_BR_STATE state);
379 
380 /* smp_act.cc */
381 void smp_send_pair_req(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
382 void smp_send_confirm(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
383 void smp_send_pair_fail(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
384 void smp_send_rand(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
385 void smp_send_pair_public_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
386 void smp_send_commitment(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
387 void smp_send_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
388 void smp_send_keypress_notification(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
389 void smp_proc_pair_fail(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
390 void smp_proc_confirm(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
391 void smp_proc_rand(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
392 void smp_process_pairing_public_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
393 void smp_proc_enc_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
394 void smp_proc_central_id(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
395 void smp_proc_id_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
396 void smp_proc_id_addr(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
397 void smp_proc_sec_grant(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
398 void smp_proc_sec_req(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
399 void smp_proc_sl_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
400 void smp_start_enc(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
401 void smp_enc_cmpl(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
402 void smp_proc_discard(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
403 void smp_pairing_cmpl(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
404 void smp_decide_association_model(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
405 void smp_sirk_verify(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
406 void smp_send_app_cback(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
407 void smp_proc_compare(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
408 void smp_check_auth_req(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
409 void smp_process_io_response(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
410 void smp_send_id_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
411 void smp_send_enc_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
412 void smp_send_csrk_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
413 void smp_send_ltk_reply(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
414 void smp_proc_pair_cmd(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
415 void smp_pair_terminate(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
416 void smp_idle_terminate(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
417 void smp_send_pair_rsp(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
418 void smp_key_distribution(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
419 void smp_proc_srk_info(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
420 void smp_generate_csrk(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
421 void smp_key_pick_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
422 void smp_both_have_public_keys(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
423 void smp_start_secure_connection_phase1(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
424 void smp_process_local_nonce(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
425 void smp_process_pairing_commitment(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
426 void smp_process_peer_nonce(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
427 void smp_process_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
428 void smp_match_dhkey_checks(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
429 void smp_process_keypress_notification(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
430 void smp_move_to_secure_connections_phase2(tSMP_CB* p_cb,
431                                            tSMP_INT_DATA* p_data);
432 void smp_phase_2_dhkey_checks_are_present(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
433 void smp_wait_for_both_public_keys(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
434 void smp_start_passkey_verification(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
435 void smp_process_secure_connection_oob_data(tSMP_CB* p_cb,
436                                             tSMP_INT_DATA* p_data);
437 void smp_process_secure_connection_long_term_key(void);
438 void smp_set_local_oob_keys(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
439 void smp_set_local_oob_random_commitment(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
440 void smp_set_derive_link_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
441 void smp_derive_link_key_from_long_term_key(tSMP_CB* p_cb,
442                                             tSMP_INT_DATA* p_data);
443 void smp_br_process_pairing_command(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
444 void smp_br_process_security_grant(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
445 void smp_br_process_peripheral_keys_response(tSMP_CB* p_cb,
446                                              tSMP_INT_DATA* p_data);
447 void smp_br_send_pair_response(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
448 void smp_br_check_authorization_request(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
449 void smp_br_select_next_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
450 void smp_br_process_link_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
451 void smp_br_pairing_complete(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
452 
453 /* smp_l2c */
454 void smp_l2cap_if_init(void);
455 void smp_data_ind(const RawAddress& bd_addr, BT_HDR* p_buf);
456 
457 /* smp_util.cc */
458 void smp_log_metrics(const RawAddress& bd_addr, bool is_outgoing,
459                      const uint8_t* p_buf, size_t buf_len, bool is_over_br);
460 bool smp_send_cmd(uint8_t cmd_code, tSMP_CB* p_cb);
461 void smp_reset_control_value(tSMP_CB* p_cb);
462 void smp_proc_pairing_cmpl(tSMP_CB* p_cb);
463 void smp_convert_string_to_tk(Octet16* tk, uint32_t passkey);
464 void smp_mask_enc_key(uint8_t loc_enc_size, Octet16* p_data);
465 void smp_rsp_timeout(void* data);
466 void smp_delayed_auth_complete_timeout(void* data);
467 bool smp_command_has_invalid_length(tSMP_CB* p_cb);
468 bool smp_command_has_invalid_parameters(tSMP_CB* p_cb);
469 void smp_reject_unexpected_pairing_command(const RawAddress& bd_addr);
470 tSMP_ASSO_MODEL smp_select_association_model(tSMP_CB* p_cb);
471 uint8_t smp_calculate_random_input(uint8_t* random, uint8_t round);
472 void smp_collect_local_io_capabilities(uint8_t* iocap, tSMP_CB* p_cb);
473 void smp_collect_peer_io_capabilities(uint8_t* iocap, tSMP_CB* p_cb);
474 void smp_collect_local_ble_address(uint8_t* le_addr, tSMP_CB* p_cb);
475 void smp_collect_peer_ble_address(uint8_t* le_addr, tSMP_CB* p_cb);
476 bool smp_check_commitment(tSMP_CB* p_cb);
477 void smp_save_secure_connections_long_term_key(tSMP_CB* p_cb);
478 void smp_calculate_f5_mackey_and_long_term_key(tSMP_CB* p_cb);
479 void smp_remove_fixed_channel(tSMP_CB* p_cb);
480 bool smp_request_oob_data(tSMP_CB* p_cb);
481 
482 /* smp_keys.cc */
483 void smp_generate_srand_mrand_confirm(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
484 void smp_generate_compare(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
485 void smp_generate_stk(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
486 void smp_generate_ltk(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
487 void smp_generate_passkey(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
488 void smp_generate_rand_cont(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
489 void smp_create_private_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
490 void smp_use_oob_private_key(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
491 void smp_compute_dhkey(tSMP_CB* p_cb);
492 void smp_calculate_local_commitment(tSMP_CB* p_cb);
493 Octet16 smp_calculate_peer_commitment(tSMP_CB* p_cb);
494 void smp_calculate_numeric_comparison_display_number(tSMP_CB* p_cb,
495                                                      tSMP_INT_DATA* p_data);
496 void smp_calculate_local_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
497 void smp_calculate_peer_dhkey_check(tSMP_CB* p_cb, tSMP_INT_DATA* p_data);
498 void smp_start_nonce_generation(tSMP_CB* p_cb);
499 bool smp_calculate_link_key_from_long_term_key(tSMP_CB* p_cb);
500 bool smp_calculate_long_term_key_from_link_key(tSMP_CB* p_cb);
501 
502 void print128(const Octet16& x, const char* key_name);
503 void smp_xor_128(Octet16* a, const Octet16& b);
504 
505 /* Save the p_cb->sc_oob_data.loc_oob_data for later, since the p_cb gets
506  * cleaned up */
507 void smp_save_local_oob_data(tSMP_CB* p_cb);
508 void smp_clear_local_oob_data();
509 bool smp_has_local_oob_data();
510 
511 namespace fmt {
512 template <>
513 struct formatter<tSMP_EVENT> : enum_formatter<tSMP_EVENT> {};
514 template <>
515 struct formatter<tSMP_OPCODE> : enum_formatter<tSMP_OPCODE> {};
516 template <>
517 struct formatter<tSMP_ASSO_MODEL> : enum_formatter<tSMP_ASSO_MODEL> {};
518 }  // namespace fmt
519 
520 #endif /* SMP_INT_H */
521