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 struct bt_smp_hdr {
13 	uint8_t  code;
14 } __packed;
15 
16 #define BT_SMP_ERR_PASSKEY_ENTRY_FAILED		0x01
17 #define BT_SMP_ERR_OOB_NOT_AVAIL		0x02
18 #define BT_SMP_ERR_AUTH_REQUIREMENTS		0x03
19 #define BT_SMP_ERR_CONFIRM_FAILED		0x04
20 #define BT_SMP_ERR_PAIRING_NOTSUPP		0x05
21 #define BT_SMP_ERR_ENC_KEY_SIZE			0x06
22 #define BT_SMP_ERR_CMD_NOTSUPP			0x07
23 #define BT_SMP_ERR_UNSPECIFIED			0x08
24 #define BT_SMP_ERR_REPEATED_ATTEMPTS		0x09
25 #define BT_SMP_ERR_INVALID_PARAMS		0x0a
26 #define BT_SMP_ERR_DHKEY_CHECK_FAILED		0x0b
27 #define BT_SMP_ERR_NUMERIC_COMP_FAILED		0x0c
28 #define BT_SMP_ERR_BREDR_PAIRING_IN_PROGRESS	0x0d
29 #define BT_SMP_ERR_CROSS_TRANSP_NOT_ALLOWED	0x0e
30 #define BT_SMP_ERR_KEY_REJECTED			0x0f
31 
32 #define BT_SMP_IO_DISPLAY_ONLY			0x00
33 #define BT_SMP_IO_DISPLAY_YESNO			0x01
34 #define BT_SMP_IO_KEYBOARD_ONLY			0x02
35 #define BT_SMP_IO_NO_INPUT_OUTPUT		0x03
36 #define BT_SMP_IO_KEYBOARD_DISPLAY		0x04
37 
38 #define BT_SMP_OOB_DATA_MASK			0x01
39 #define BT_SMP_OOB_NOT_PRESENT			0x00
40 #define BT_SMP_OOB_PRESENT			0x01
41 
42 #define BT_SMP_MIN_ENC_KEY_SIZE			CONFIG_BT_SMP_MIN_ENC_KEY_SIZE
43 #define BT_SMP_MAX_ENC_KEY_SIZE			16
44 
45 #define BT_SMP_DIST_ENC_KEY			0x01
46 #define BT_SMP_DIST_ID_KEY			0x02
47 #define BT_SMP_DIST_SIGN			0x04
48 #define BT_SMP_DIST_LINK_KEY			0x08
49 
50 #define BT_SMP_DIST_MASK			0x0f
51 
52 #define BT_SMP_AUTH_NONE			0x00
53 #define BT_SMP_AUTH_BONDING			0x01
54 #define BT_SMP_AUTH_MITM			0x04
55 #define BT_SMP_AUTH_SC				0x08
56 #define BT_SMP_AUTH_KEYPRESS			0x10
57 #define BT_SMP_AUTH_CT2				0x20
58 
59 #define BT_SMP_CMD_PAIRING_REQ			0x01
60 #define BT_SMP_CMD_PAIRING_RSP			0x02
61 struct bt_smp_pairing {
62 	uint8_t  io_capability;
63 	uint8_t  oob_flag;
64 	uint8_t  auth_req;
65 	uint8_t  max_key_size;
66 	uint8_t  init_key_dist;
67 	uint8_t  resp_key_dist;
68 } __packed;
69 
70 #define BT_SMP_CMD_PAIRING_CONFIRM		0x03
71 struct bt_smp_pairing_confirm {
72 	uint8_t  val[16];
73 } __packed;
74 
75 #define BT_SMP_CMD_PAIRING_RANDOM		0x04
76 struct bt_smp_pairing_random {
77 	uint8_t  val[16];
78 } __packed;
79 
80 #define BT_SMP_CMD_PAIRING_FAIL			0x05
81 struct bt_smp_pairing_fail {
82 	uint8_t  reason;
83 } __packed;
84 
85 #define BT_SMP_CMD_ENCRYPT_INFO			0x06
86 struct bt_smp_encrypt_info {
87 	uint8_t  ltk[16];
88 } __packed;
89 
90 #define BT_SMP_CMD_CENTRAL_IDENT		0x07
91 struct bt_smp_central_ident {
92 	uint8_t ediv[2];
93 	uint8_t rand[8];
94 } __packed;
95 
96 #define BT_SMP_CMD_IDENT_INFO			0x08
97 struct bt_smp_ident_info {
98 	uint8_t  irk[16];
99 } __packed;
100 
101 #define BT_SMP_CMD_IDENT_ADDR_INFO		0x09
102 struct bt_smp_ident_addr_info {
103 	bt_addr_le_t addr;
104 } __packed;
105 
106 #define BT_SMP_CMD_SIGNING_INFO			0x0a
107 struct bt_smp_signing_info {
108 	uint8_t csrk[16];
109 } __packed;
110 
111 #define BT_SMP_CMD_SECURITY_REQUEST		0x0b
112 struct bt_smp_security_request {
113 	uint8_t  auth_req;
114 } __packed;
115 
116 #define BT_SMP_CMD_PUBLIC_KEY			0x0c
117 struct bt_smp_public_key {
118 	uint8_t x[32];
119 	uint8_t y[32];
120 } __packed;
121 
122 #define BT_SMP_DHKEY_CHECK			0x0d
123 struct bt_smp_dhkey_check {
124 	uint8_t e[16];
125 } __packed;
126 
127 #define BT_SMP_KEYPRESS_NOTIFICATION		0x0e
128 struct bt_smp_keypress_notif {
129 	uint8_t type;
130 } __packed;
131 
132 #define BT_SMP_NUM_CMDS                         0x0f
133 
134 int bt_smp_start_security(struct bt_conn *conn);
135 bool bt_smp_request_ltk(struct bt_conn *conn, uint64_t rand, uint16_t ediv,
136 			uint8_t *ltk);
137 
138 void bt_smp_update_keys(struct bt_conn *conn);
139 
140 int bt_smp_br_send_pairing_req(struct bt_conn *conn);
141 
142 int bt_smp_init(void);
143 
144 int bt_smp_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey);
145 int bt_smp_auth_passkey_confirm(struct bt_conn *conn);
146 int bt_smp_auth_pairing_confirm(struct bt_conn *conn);
147 int bt_smp_auth_cancel(struct bt_conn *conn);
148 
149 int bt_smp_le_oob_set_tk(struct bt_conn *conn, const uint8_t *tk);
150 int bt_smp_le_oob_generate_sc_data(struct bt_le_oob_sc_data *le_sc_oob);
151 int bt_smp_le_oob_set_sc_data(struct bt_conn *conn,
152 			      const struct bt_le_oob_sc_data *oobd_local,
153 			      const struct bt_le_oob_sc_data *oobd_remote);
154 int bt_smp_le_oob_get_sc_data(struct bt_conn *conn,
155 			      const struct bt_le_oob_sc_data **oobd_local,
156 			      const struct bt_le_oob_sc_data **oobd_remote);
157 
158 /** brief Verify signed message
159  *
160  *  @param conn Bluetooth connection
161  *  @param buf received packet buffer with message and signature
162  *
163  *  @return 0 in success, error code otherwise
164  */
165 int bt_smp_sign_verify(struct bt_conn *conn, struct net_buf *buf);
166 
167 /** brief Sign message
168  *
169  *  @param conn Bluetooth connection
170  *  @param buf message buffer
171  *
172  *  @return 0 in success, error code otherwise
173  */
174 int bt_smp_sign(struct bt_conn *conn, struct net_buf *buf);
175 
176 /** Generate IRK from Identity Root (IR) */
177 int bt_smp_irk_get(uint8_t *ir, uint8_t *irk);
178