1 /**
2  * @file smp.h
3  * Security Manager Protocol implementation header
4  */
5 
6 /*
7  * Copyright (c) 2015-2016 Intel Corporation
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #include <stdbool.h>
13 #include <stdint.h>
14 
15 #include <zephyr/autoconf.h>
16 #include <zephyr/bluetooth/addr.h>
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/conn.h>
19 #include <zephyr/net_buf.h>
20 #include <zephyr/toolchain.h>
21 
22 struct bt_smp_hdr {
23 	uint8_t  code;
24 } __packed;
25 
26 #define BT_SMP_ERR_SUCCESS                      0x00
27 #define BT_SMP_ERR_PASSKEY_ENTRY_FAILED		0x01
28 #define BT_SMP_ERR_OOB_NOT_AVAIL		0x02
29 #define BT_SMP_ERR_AUTH_REQUIREMENTS		0x03
30 #define BT_SMP_ERR_CONFIRM_FAILED		0x04
31 #define BT_SMP_ERR_PAIRING_NOTSUPP		0x05
32 #define BT_SMP_ERR_ENC_KEY_SIZE			0x06
33 #define BT_SMP_ERR_CMD_NOTSUPP			0x07
34 #define BT_SMP_ERR_UNSPECIFIED			0x08
35 #define BT_SMP_ERR_REPEATED_ATTEMPTS		0x09
36 #define BT_SMP_ERR_INVALID_PARAMS		0x0a
37 #define BT_SMP_ERR_DHKEY_CHECK_FAILED		0x0b
38 #define BT_SMP_ERR_NUMERIC_COMP_FAILED		0x0c
39 #define BT_SMP_ERR_BREDR_PAIRING_IN_PROGRESS	0x0d
40 #define BT_SMP_ERR_CROSS_TRANSP_NOT_ALLOWED	0x0e
41 #define BT_SMP_ERR_KEY_REJECTED			0x0f
42 
43 #define BT_SMP_IO_DISPLAY_ONLY			0x00
44 #define BT_SMP_IO_DISPLAY_YESNO			0x01
45 #define BT_SMP_IO_KEYBOARD_ONLY			0x02
46 #define BT_SMP_IO_NO_INPUT_OUTPUT		0x03
47 #define BT_SMP_IO_KEYBOARD_DISPLAY		0x04
48 
49 #define BT_SMP_OOB_DATA_MASK			0x01
50 #define BT_SMP_OOB_NOT_PRESENT			0x00
51 #define BT_SMP_OOB_PRESENT			0x01
52 
53 #define BT_SMP_MIN_ENC_KEY_SIZE			CONFIG_BT_SMP_MIN_ENC_KEY_SIZE
54 #define BT_SMP_MAX_ENC_KEY_SIZE			16
55 
56 #define BT_SMP_DIST_ENC_KEY			0x01
57 #define BT_SMP_DIST_ID_KEY			0x02
58 #define BT_SMP_DIST_SIGN			0x04
59 #define BT_SMP_DIST_LINK_KEY			0x08
60 
61 #define BT_SMP_DIST_MASK			0x0f
62 
63 #define BT_SMP_AUTH_NONE			0x00
64 #define BT_SMP_AUTH_BONDING			0x01
65 #define BT_SMP_AUTH_MITM			0x04
66 #define BT_SMP_AUTH_SC				0x08
67 #define BT_SMP_AUTH_KEYPRESS			0x10
68 #define BT_SMP_AUTH_CT2				0x20
69 
70 #define BT_SMP_CMD_PAIRING_REQ			0x01
71 #define BT_SMP_CMD_PAIRING_RSP			0x02
72 struct bt_smp_pairing {
73 	uint8_t  io_capability;
74 	uint8_t  oob_flag;
75 	uint8_t  auth_req;
76 	uint8_t  max_key_size;
77 	uint8_t  init_key_dist;
78 	uint8_t  resp_key_dist;
79 } __packed;
80 
81 #define BT_SMP_CMD_PAIRING_CONFIRM		0x03
82 struct bt_smp_pairing_confirm {
83 	uint8_t  val[16];
84 } __packed;
85 
86 #define BT_SMP_CMD_PAIRING_RANDOM		0x04
87 struct bt_smp_pairing_random {
88 	uint8_t  val[16];
89 } __packed;
90 
91 #define BT_SMP_CMD_PAIRING_FAIL			0x05
92 struct bt_smp_pairing_fail {
93 	uint8_t  reason;
94 } __packed;
95 
96 #define BT_SMP_CMD_ENCRYPT_INFO			0x06
97 struct bt_smp_encrypt_info {
98 	uint8_t  ltk[16];
99 } __packed;
100 
101 #define BT_SMP_CMD_CENTRAL_IDENT		0x07
102 struct bt_smp_central_ident {
103 	uint8_t ediv[2];
104 	uint8_t rand[8];
105 } __packed;
106 
107 #define BT_SMP_CMD_IDENT_INFO			0x08
108 struct bt_smp_ident_info {
109 	uint8_t  irk[16];
110 } __packed;
111 
112 #define BT_SMP_CMD_IDENT_ADDR_INFO		0x09
113 struct bt_smp_ident_addr_info {
114 	bt_addr_le_t addr;
115 } __packed;
116 
117 #define BT_SMP_CMD_SIGNING_INFO			0x0a
118 struct bt_smp_signing_info {
119 	uint8_t csrk[16];
120 } __packed;
121 
122 #define BT_SMP_CMD_SECURITY_REQUEST		0x0b
123 struct bt_smp_security_request {
124 	uint8_t  auth_req;
125 } __packed;
126 
127 #define BT_SMP_CMD_PUBLIC_KEY			0x0c
128 struct bt_smp_public_key {
129 	uint8_t x[32];
130 	uint8_t y[32];
131 } __packed;
132 
133 #define BT_SMP_DHKEY_CHECK			0x0d
134 struct bt_smp_dhkey_check {
135 	uint8_t e[16];
136 } __packed;
137 
138 #define BT_SMP_KEYPRESS_NOTIFICATION		0x0e
139 struct bt_smp_keypress_notif {
140 	uint8_t type;
141 } __packed;
142 
143 #define BT_SMP_NUM_CMDS                         0x0f
144 
145 int bt_smp_start_security(struct bt_conn *conn);
146 bool bt_smp_request_ltk(struct bt_conn *conn, uint64_t rand, uint16_t ediv,
147 			uint8_t *ltk);
148 
149 void bt_smp_update_keys(struct bt_conn *conn);
150 
151 int bt_smp_br_send_pairing_req(struct bt_conn *conn);
152 
153 int bt_smp_init(void);
154 
155 int bt_smp_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb *cb);
156 int bt_smp_auth_keypress_notify(struct bt_conn *conn,
157 				enum bt_conn_auth_keypress type);
158 int bt_smp_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey);
159 int bt_smp_auth_passkey_confirm(struct bt_conn *conn);
160 int bt_smp_auth_pairing_confirm(struct bt_conn *conn);
161 int bt_smp_auth_cancel(struct bt_conn *conn);
162 
163 int bt_smp_le_oob_set_tk(struct bt_conn *conn, const uint8_t *tk);
164 int bt_smp_le_oob_generate_sc_data(struct bt_le_oob_sc_data *le_sc_oob);
165 int bt_smp_le_oob_set_sc_data(struct bt_conn *conn,
166 			      const struct bt_le_oob_sc_data *oobd_local,
167 			      const struct bt_le_oob_sc_data *oobd_remote);
168 int bt_smp_le_oob_get_sc_data(struct bt_conn *conn,
169 			      const struct bt_le_oob_sc_data **oobd_local,
170 			      const struct bt_le_oob_sc_data **oobd_remote);
171 
172 /** brief Verify signed message
173  *
174  *  @param conn Bluetooth connection
175  *  @param buf received packet buffer with message and signature
176  *
177  *  @return 0 in success, error code otherwise
178  */
179 int bt_smp_sign_verify(struct bt_conn *conn, struct net_buf *buf);
180 
181 /** brief Sign message
182  *
183  *  @param conn Bluetooth connection
184  *  @param buf message buffer
185  *
186  *  @return 0 in success, error code otherwise
187  */
188 int bt_smp_sign(struct bt_conn *conn, struct net_buf *buf);
189 
190 /** Generate IRK from Identity Root (IR) */
191 int bt_smp_irk_get(uint8_t *ir, uint8_t *irk);
192 
193 /** Converts a SMP error to string.
194  *
195  * The error codes are described in the Bluetooth Core specification,
196  * Vol 3, Part H, Section 3.5.5.
197  *
198  * The Security Manager Protocol documentation found in Vol 4, Part H,
199  * describes when the different error codes are used.
200  *
201  * See also the defined BT_SMP_ERR_* macros.
202  *
203  * @return The string representation of the SMP error code.
204  */
205 #if defined(CONFIG_BT_SMP_ERR_TO_STR)
206 const char *bt_smp_err_to_str(uint8_t smp_err);
207 #else
bt_smp_err_to_str(uint8_t smp_err)208 static inline const char *bt_smp_err_to_str(uint8_t smp_err)
209 {
210 	ARG_UNUSED(smp_err);
211 
212 	return "";
213 }
214 #endif
215