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