blob: 658c900752c67a65d939997f1a713ac015c3f950 [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
Marcel Holtmann300acfde2014-12-31 14:43:16 -080023#include <linux/debugfs.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030024#include <linux/scatterlist.h>
Andy Lutomirskia4770e12016-06-26 14:55:23 -070025#include <linux/crypto.h>
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +020026#include <crypto/algapi.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030027#include <crypto/b128ops.h>
Herbert Xu71af2f62016-01-24 21:18:30 +080028#include <crypto/hash.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030029
Anderson Brigliaeb492e02011-06-09 18:50:40 -030030#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
32#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080033#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070034
Johan Hedberg3b191462014-06-06 10:50:15 +030035#include "ecc.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070036#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030037
Johan Hedberg2fd36552015-06-11 13:52:26 +030038#define SMP_DEV(hdev) \
39 ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data)
40
Johan Hedbergc7a3d572014-12-01 22:03:16 +020041/* Low-level debug macros to be used for stuff that we don't want
42 * accidentially in dmesg, i.e. the values of the various crypto keys
43 * and the inputs & outputs of crypto functions.
44 */
45#ifdef DEBUG
46#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
47 ##__VA_ARGS__)
48#else
49#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
50 ##__VA_ARGS__)
51#endif
52
Johan Hedbergb28b4942014-09-05 22:19:55 +030053#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030054
Johan Hedberg3b191462014-06-06 10:50:15 +030055/* Keys which are not distributed with Secure Connections */
56#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
57
Marcel Holtmann17b02e62012-03-01 14:32:37 -080058#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030059
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070060#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberg0edb14d2014-05-26 13:29:28 +030061 0x1f : 0x07)
62#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020063
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030064/* Maximum message length that can be passed to aes_cmac */
65#define CMAC_MSG_MAX 80
66
Johan Hedberg533e35d2014-06-16 19:25:18 +030067enum {
68 SMP_FLAG_TK_VALID,
69 SMP_FLAG_CFM_PENDING,
70 SMP_FLAG_MITM_AUTH,
71 SMP_FLAG_COMPLETE,
72 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030073 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030074 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030075 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030076 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030077 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg1a8bab42015-03-16 11:45:44 +020078 SMP_FLAG_REMOTE_OOB,
79 SMP_FLAG_LOCAL_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030080};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030081
Marcel Holtmann88a479d2015-03-16 01:10:19 -070082struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070083 /* Secure Connections OOB data */
84 u8 local_pk[64];
85 u8 local_sk[32];
Marcel Holtmannfb334fe2015-03-16 12:34:57 -070086 u8 local_rand[16];
Marcel Holtmann60a27d62015-03-16 01:10:22 -070087 bool debug_key;
88
Johan Hedbergb1f663c2015-06-11 13:52:27 +030089 u8 min_key_size;
Johan Hedberg2fd36552015-06-11 13:52:26 +030090 u8 max_key_size;
91
Andy Lutomirskia4770e12016-06-26 14:55:23 -070092 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +080093 struct crypto_shash *tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070094};
95
Johan Hedberg4bc58f52014-05-20 09:45:47 +030096struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030097 struct l2cap_conn *conn;
98 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030099 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +0300100
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300101 u8 preq[7]; /* SMP Pairing Request */
102 u8 prsp[7]; /* SMP Pairing Response */
103 u8 prnd[16]; /* SMP Pairing Random (local) */
104 u8 rrnd[16]; /* SMP Pairing Random (remote) */
105 u8 pcnf[16]; /* SMP Pairing Confirm */
106 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +0200107 u8 rr[16]; /* Remote OOB ra/rb value */
108 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300109 u8 enc_key_size;
110 u8 remote_key_dist;
111 bdaddr_t id_addr;
112 u8 id_addr_type;
113 u8 irk[16];
114 struct smp_csrk *csrk;
115 struct smp_csrk *slave_csrk;
116 struct smp_ltk *ltk;
117 struct smp_ltk *slave_ltk;
118 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300119 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300120 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300121 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300122 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300123
Johan Hedberg3b191462014-06-06 10:50:15 +0300124 /* Secure Connections variables */
125 u8 local_pk[64];
126 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300127 u8 remote_pk[64];
128 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300129 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300130
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700131 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +0800132 struct crypto_shash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300133};
134
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300135/* These debug key values are defined in the SMP section of the core
136 * specification. debug_pk is the public debug key and debug_sk the
137 * private debug key.
138 */
139static const u8 debug_pk[64] = {
140 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
141 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
142 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
143 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
144
145 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
146 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
147 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
148 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
149};
150
151static const u8 debug_sk[32] = {
152 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
153 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
154 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
155 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
156};
157
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300158static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300159{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300160 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300161
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300162 for (i = 0; i < len; i++)
163 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300164}
165
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200166/* The following functions map to the LE SC SMP crypto functions
167 * AES-CMAC, f4, f5, f6, g2 and h6.
168 */
169
Herbert Xu71af2f62016-01-24 21:18:30 +0800170static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m,
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300171 size_t len, u8 mac[16])
172{
173 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
Herbert Xu71af2f62016-01-24 21:18:30 +0800174 SHASH_DESC_ON_STACK(desc, tfm);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300175 int err;
176
177 if (len > CMAC_MSG_MAX)
178 return -EFBIG;
179
180 if (!tfm) {
181 BT_ERR("tfm %p", tfm);
182 return -EINVAL;
183 }
184
Herbert Xu71af2f62016-01-24 21:18:30 +0800185 desc->tfm = tfm;
186 desc->flags = 0;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300187
188 /* Swap key and message from LSB to MSB */
189 swap_buf(k, tmp, 16);
190 swap_buf(m, msg_msb, len);
191
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200192 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
193 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300194
Herbert Xu71af2f62016-01-24 21:18:30 +0800195 err = crypto_shash_setkey(tfm, tmp, 16);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300196 if (err) {
197 BT_ERR("cipher setkey failed: %d", err);
198 return err;
199 }
200
Herbert Xu71af2f62016-01-24 21:18:30 +0800201 err = crypto_shash_digest(desc, msg_msb, len, mac_msb);
202 shash_desc_zero(desc);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300203 if (err) {
Herbert Xu71af2f62016-01-24 21:18:30 +0800204 BT_ERR("Hash computation error %d", err);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300205 return err;
206 }
207
208 swap_buf(mac_msb, mac, 16);
209
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200210 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300211
212 return 0;
213}
214
Herbert Xu71af2f62016-01-24 21:18:30 +0800215static int smp_f4(struct crypto_shash *tfm_cmac, const u8 u[32],
216 const u8 v[32], const u8 x[16], u8 z, u8 res[16])
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300217{
218 u8 m[65];
219 int err;
220
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200221 SMP_DBG("u %32phN", u);
222 SMP_DBG("v %32phN", v);
223 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300224
225 m[0] = z;
226 memcpy(m + 1, v, 32);
227 memcpy(m + 33, u, 32);
228
229 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
230 if (err)
231 return err;
232
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200233 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300234
235 return err;
236}
237
Herbert Xu71af2f62016-01-24 21:18:30 +0800238static int smp_f5(struct crypto_shash *tfm_cmac, const u8 w[32],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200239 const u8 n1[16], const u8 n2[16], const u8 a1[7],
240 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300241{
242 /* The btle, salt and length "magic" values are as defined in
243 * the SMP section of the Bluetooth core specification. In ASCII
244 * the btle value ends up being 'btle'. The salt is just a
245 * random number whereas length is the value 256 in little
246 * endian format.
247 */
248 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
249 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
250 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
251 const u8 length[2] = { 0x00, 0x01 };
252 u8 m[53], t[16];
253 int err;
254
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200255 SMP_DBG("w %32phN", w);
256 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
257 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300258
259 err = aes_cmac(tfm_cmac, salt, w, 32, t);
260 if (err)
261 return err;
262
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200263 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300264
265 memcpy(m, length, 2);
266 memcpy(m + 2, a2, 7);
267 memcpy(m + 9, a1, 7);
268 memcpy(m + 16, n2, 16);
269 memcpy(m + 32, n1, 16);
270 memcpy(m + 48, btle, 4);
271
272 m[52] = 0; /* Counter */
273
274 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
275 if (err)
276 return err;
277
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200278 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300279
280 m[52] = 1; /* Counter */
281
282 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
283 if (err)
284 return err;
285
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200286 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300287
288 return 0;
289}
290
Herbert Xu71af2f62016-01-24 21:18:30 +0800291static int smp_f6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200292 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300293 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
294 u8 res[16])
295{
296 u8 m[65];
297 int err;
298
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200299 SMP_DBG("w %16phN", w);
300 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
301 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300302
303 memcpy(m, a2, 7);
304 memcpy(m + 7, a1, 7);
305 memcpy(m + 14, io_cap, 3);
306 memcpy(m + 17, r, 16);
307 memcpy(m + 33, n2, 16);
308 memcpy(m + 49, n1, 16);
309
310 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
311 if (err)
312 return err;
313
Marcel Holtmann203de212014-12-31 20:01:22 -0800314 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300315
316 return err;
317}
318
Herbert Xu71af2f62016-01-24 21:18:30 +0800319static int smp_g2(struct crypto_shash *tfm_cmac, const u8 u[32], const u8 v[32],
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300320 const u8 x[16], const u8 y[16], u32 *val)
321{
322 u8 m[80], tmp[16];
323 int err;
324
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200325 SMP_DBG("u %32phN", u);
326 SMP_DBG("v %32phN", v);
327 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300328
329 memcpy(m, y, 16);
330 memcpy(m + 16, v, 32);
331 memcpy(m + 48, u, 32);
332
333 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
334 if (err)
335 return err;
336
337 *val = get_unaligned_le32(tmp);
338 *val %= 1000000;
339
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200340 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300341
342 return 0;
343}
344
Herbert Xu71af2f62016-01-24 21:18:30 +0800345static int smp_h6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200346 const u8 key_id[4], u8 res[16])
347{
348 int err;
349
350 SMP_DBG("w %16phN key_id %4phN", w, key_id);
351
352 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
353 if (err)
354 return err;
355
356 SMP_DBG("res %16phN", res);
357
358 return err;
359}
360
361/* The following functions map to the legacy SMP crypto functions e, c1,
362 * s1 and ah.
363 */
364
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700365static int smp_e(struct crypto_cipher *tfm, const u8 *k, u8 *r)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300366{
Johan Hedberg943a7322014-03-18 12:58:24 +0200367 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200368 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300369
Johan Hedberg011c3912015-05-19 21:06:04 +0300370 SMP_DBG("k %16phN r %16phN", k, r);
371
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200372 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300373 BT_ERR("tfm %p", tfm);
374 return -EINVAL;
375 }
376
Johan Hedberg943a7322014-03-18 12:58:24 +0200377 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300378 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200379
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700380 err = crypto_cipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300381 if (err) {
382 BT_ERR("cipher setkey failed: %d", err);
383 return err;
384 }
385
Johan Hedberg943a7322014-03-18 12:58:24 +0200386 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300387 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200388
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700389 crypto_cipher_encrypt_one(tfm, data, data);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300390
Johan Hedberg943a7322014-03-18 12:58:24 +0200391 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300392 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200393
Johan Hedberg011c3912015-05-19 21:06:04 +0300394 SMP_DBG("r %16phN", r);
395
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300396 return err;
397}
398
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700399static int smp_c1(struct crypto_cipher *tfm_aes, const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200400 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
401 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
402{
403 u8 p1[16], p2[16];
404 int err;
405
Johan Hedberg011c3912015-05-19 21:06:04 +0300406 SMP_DBG("k %16phN r %16phN", k, r);
407 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
408 SMP_DBG("preq %7phN pres %7phN", preq, pres);
409
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200410 memset(p1, 0, 16);
411
412 /* p1 = pres || preq || _rat || _iat */
413 p1[0] = _iat;
414 p1[1] = _rat;
415 memcpy(p1 + 2, preq, 7);
416 memcpy(p1 + 9, pres, 7);
417
Johan Hedberg011c3912015-05-19 21:06:04 +0300418 SMP_DBG("p1 %16phN", p1);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200419
420 /* res = r XOR p1 */
421 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
422
423 /* res = e(k, res) */
424 err = smp_e(tfm_aes, k, res);
425 if (err) {
426 BT_ERR("Encrypt data error");
427 return err;
428 }
429
Johan Hedberg011c3912015-05-19 21:06:04 +0300430 /* p2 = padding || ia || ra */
431 memcpy(p2, ra, 6);
432 memcpy(p2 + 6, ia, 6);
433 memset(p2 + 12, 0, 4);
434
435 SMP_DBG("p2 %16phN", p2);
436
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200437 /* res = res XOR p2 */
438 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
439
440 /* res = e(k, res) */
441 err = smp_e(tfm_aes, k, res);
442 if (err)
443 BT_ERR("Encrypt data error");
444
445 return err;
446}
447
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700448static int smp_s1(struct crypto_cipher *tfm_aes, const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200449 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300450{
451 int err;
452
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200453 /* Just least significant octets from r1 and r2 are considered */
454 memcpy(_r, r2, 8);
455 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300456
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200457 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300458 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200459 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300460
461 return err;
462}
463
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700464static int smp_ah(struct crypto_cipher *tfm, const u8 irk[16],
Johan Hedbergcd082792014-12-02 13:37:41 +0200465 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200466{
Johan Hedberg943a7322014-03-18 12:58:24 +0200467 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200468 int err;
469
470 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200471 memcpy(_res, r, 3);
472 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200473
Johan Hedberg943a7322014-03-18 12:58:24 +0200474 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200475 if (err) {
476 BT_ERR("Encrypt error");
477 return err;
478 }
479
480 /* The output of the random address function ah is:
Marcel Holtmannc5080d42015-09-04 17:08:18 +0200481 * ah(k, r) = e(k, r') mod 2^24
Johan Hedberg60478052014-02-18 10:19:31 +0200482 * The output of the security function e is then truncated to 24 bits
483 * by taking the least significant 24 bits of the output of e as the
484 * result of ah.
485 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200486 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200487
488 return 0;
489}
490
Johan Hedbergcd082792014-12-02 13:37:41 +0200491bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
492 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200493{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300494 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700495 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200496 u8 hash[3];
497 int err;
498
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300499 if (!chan || !chan->data)
500 return false;
501
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700502 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300503
Johan Hedberg60478052014-02-18 10:19:31 +0200504 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
505
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700506 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200507 if (err)
508 return false;
509
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +0200510 return !crypto_memneq(bdaddr->b, hash, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200511}
512
Johan Hedbergcd082792014-12-02 13:37:41 +0200513int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200514{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300515 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700516 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200517 int err;
518
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300519 if (!chan || !chan->data)
520 return -EOPNOTSUPP;
521
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700522 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300523
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200524 get_random_bytes(&rpa->b[3], 3);
525
526 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
527 rpa->b[5] |= 0x40; /* Set second most significant bit */
528
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700529 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200530 if (err < 0)
531 return err;
532
533 BT_DBG("RPA %pMR", rpa);
534
535 return 0;
536}
537
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700538int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
539{
540 struct l2cap_chan *chan = hdev->smp_data;
541 struct smp_dev *smp;
542 int err;
543
544 if (!chan || !chan->data)
545 return -EOPNOTSUPP;
546
547 smp = chan->data;
548
549 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
550 BT_DBG("Using debug keys");
551 memcpy(smp->local_pk, debug_pk, 64);
552 memcpy(smp->local_sk, debug_sk, 32);
553 smp->debug_key = true;
554 } else {
555 while (true) {
556 /* Generate local key pair for Secure Connections */
557 if (!ecc_make_key(smp->local_pk, smp->local_sk))
558 return -EIO;
559
560 /* This is unlikely, but we need to check that
561 * we didn't accidentially generate a debug key.
562 */
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +0200563 if (crypto_memneq(smp->local_sk, debug_sk, 32))
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700564 break;
565 }
566 smp->debug_key = false;
567 }
568
569 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
570 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
571 SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
572
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700573 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700574
575 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700576 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700577 if (err < 0)
578 return err;
579
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700580 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700581
582 return 0;
583}
584
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300585static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
586{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300587 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300588 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300589 struct kvec iv[2];
590 struct msghdr msg;
591
592 if (!chan)
593 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300594
595 BT_DBG("code 0x%2.2x", code);
596
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300597 iv[0].iov_base = &code;
598 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300599
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300600 iv[1].iov_base = data;
601 iv[1].iov_len = len;
602
603 memset(&msg, 0, sizeof(msg));
604
Al Viro17836392014-11-24 17:07:38 -0500605 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300606
607 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300608
Johan Hedbergb68fda62014-08-11 22:06:40 +0300609 if (!chan->data)
610 return;
611
612 smp = chan->data;
613
614 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300615 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300616}
617
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300618static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800619{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300620 if (authreq & SMP_AUTH_MITM) {
621 if (authreq & SMP_AUTH_SC)
622 return BT_SECURITY_FIPS;
623 else
624 return BT_SECURITY_HIGH;
625 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800626 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300627 }
Brian Gix2b64d152011-12-21 16:12:12 -0800628}
629
630static __u8 seclevel_to_authreq(__u8 sec_level)
631{
632 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300633 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800634 case BT_SECURITY_HIGH:
635 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
636 case BT_SECURITY_MEDIUM:
637 return SMP_AUTH_BONDING;
638 default:
639 return SMP_AUTH_NONE;
640 }
641}
642
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300643static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700644 struct smp_cmd_pairing *req,
645 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300646{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300647 struct l2cap_chan *chan = conn->smp;
648 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200649 struct hci_conn *hcon = conn->hcon;
650 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100651 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300652
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700653 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700654 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
655 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300656 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800657 } else {
658 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300659 }
660
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700661 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200662 remote_dist |= SMP_DIST_ID_KEY;
663
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700664 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200665 local_dist |= SMP_DIST_ID_KEY;
666
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700667 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100668 (authreq & SMP_AUTH_SC)) {
669 struct oob_data *oob_data;
670 u8 bdaddr_type;
671
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700672 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300673 local_dist |= SMP_DIST_LINK_KEY;
674 remote_dist |= SMP_DIST_LINK_KEY;
675 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100676
677 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
678 bdaddr_type = BDADDR_LE_PUBLIC;
679 else
680 bdaddr_type = BDADDR_LE_RANDOM;
681
682 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
683 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800684 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200685 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100686 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100687 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100688 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700689 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
690 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100691 }
692
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300693 } else {
694 authreq &= ~SMP_AUTH_SC;
695 }
696
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300697 if (rsp == NULL) {
698 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100699 req->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300700 req->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200701 req->init_key_dist = local_dist;
702 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300703 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200704
705 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300706 return;
707 }
708
709 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100710 rsp->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300711 rsp->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200712 rsp->init_key_dist = req->init_key_dist & remote_dist;
713 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300714 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200715
716 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300717}
718
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300719static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
720{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300721 struct l2cap_chan *chan = conn->smp;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300722 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300723 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300724
Johan Hedberg2fd36552015-06-11 13:52:26 +0300725 if (max_key_size > SMP_DEV(hdev)->max_key_size ||
726 max_key_size < SMP_MIN_ENC_KEY_SIZE)
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300727 return SMP_ENC_KEY_SIZE;
728
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300729 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300730
731 return 0;
732}
733
Johan Hedberg6f48e262014-08-11 22:06:44 +0300734static void smp_chan_destroy(struct l2cap_conn *conn)
735{
736 struct l2cap_chan *chan = conn->smp;
737 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200738 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300739 bool complete;
740
741 BUG_ON(!smp);
742
743 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300744
Johan Hedberg6f48e262014-08-11 22:06:44 +0300745 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200746 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300747
Marcel Holtmann276812e2015-03-16 01:10:18 -0700748 kzfree(smp->csrk);
749 kzfree(smp->slave_csrk);
750 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300751
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700752 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +0800753 crypto_free_shash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300754
Johan Hedberg923e2412014-12-03 12:43:39 +0200755 /* Ensure that we don't leave any debug key around if debug key
756 * support hasn't been explicitly enabled.
757 */
758 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700759 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200760 list_del_rcu(&smp->ltk->list);
761 kfree_rcu(smp->ltk, rcu);
762 smp->ltk = NULL;
763 }
764
Johan Hedberg6f48e262014-08-11 22:06:44 +0300765 /* If pairing failed clean up any keys we might have */
766 if (!complete) {
767 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200768 list_del_rcu(&smp->ltk->list);
769 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300770 }
771
772 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200773 list_del_rcu(&smp->slave_ltk->list);
774 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300775 }
776
777 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200778 list_del_rcu(&smp->remote_irk->list);
779 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300780 }
781 }
782
783 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700784 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200785 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300786}
787
Johan Hedberg84794e12013-11-06 11:24:57 +0200788static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800789{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200790 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300791 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200792
Johan Hedberg84794e12013-11-06 11:24:57 +0200793 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800794 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700795 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800796
Johan Hedberge1e930f2014-09-08 17:09:49 -0700797 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300798
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300799 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300800 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800801}
802
Brian Gix2b64d152011-12-21 16:12:12 -0800803#define JUST_WORKS 0x00
804#define JUST_CFM 0x01
805#define REQ_PASSKEY 0x02
806#define CFM_PASSKEY 0x03
807#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300808#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800809#define OVERLAP 0xFF
810
811static const u8 gen_method[5][5] = {
812 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
813 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
814 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
815 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
816 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
817};
818
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300819static const u8 sc_method[5][5] = {
820 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
821 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
822 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
823 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
824 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
825};
826
Johan Hedberg581370c2014-06-17 13:07:38 +0300827static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
828{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300829 /* If either side has unknown io_caps, use JUST_CFM (which gets
830 * converted later to JUST_WORKS if we're initiators.
831 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300832 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
833 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300834 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300835
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300836 if (test_bit(SMP_FLAG_SC, &smp->flags))
837 return sc_method[remote_io][local_io];
838
Johan Hedberg581370c2014-06-17 13:07:38 +0300839 return gen_method[remote_io][local_io];
840}
841
Brian Gix2b64d152011-12-21 16:12:12 -0800842static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
843 u8 local_io, u8 remote_io)
844{
845 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300846 struct l2cap_chan *chan = conn->smp;
847 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800848 u32 passkey = 0;
849 int ret = 0;
850
851 /* Initialize key for JUST WORKS */
852 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300853 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800854
855 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
856
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300857 /* If neither side wants MITM, either "just" confirm an incoming
858 * request or use just-works for outgoing ones. The JUST_CFM
859 * will be converted to JUST_WORKS if necessary later in this
860 * function. If either side has MITM look up the method from the
861 * table.
862 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300863 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300864 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800865 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300866 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800867
Johan Hedberga82505c2014-03-24 14:39:07 +0200868 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300869 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
870 &smp->flags))
871 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200872
Johan Hedberg02f3e252014-07-16 15:09:13 +0300873 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300874 if (smp->method == JUST_CFM &&
875 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
876 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300877
Brian Gix2b64d152011-12-21 16:12:12 -0800878 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300879 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300880 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800881 return 0;
882 }
883
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200884 /* If this function is used for SC -> legacy fallback we
885 * can only recover the just-works case.
886 */
887 if (test_bit(SMP_FLAG_SC, &smp->flags))
888 return -EINVAL;
889
Brian Gix2b64d152011-12-21 16:12:12 -0800890 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300891 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300892 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300893 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
894 hcon->pending_sec_level = BT_SECURITY_HIGH;
895 }
Brian Gix2b64d152011-12-21 16:12:12 -0800896
897 /* If both devices have Keyoard-Display I/O, the master
898 * Confirms and the slave Enters the passkey.
899 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300900 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300901 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300902 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800903 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300904 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800905 }
906
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200907 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300908 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200909 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800910 get_random_bytes(&passkey, sizeof(passkey));
911 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200912 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800913 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300914 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800915 }
916
Johan Hedberg783e0572014-05-31 18:48:26 +0300917 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700918 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200919 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300920 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200921 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
922 hcon->type, hcon->dst_type,
923 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800924 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200925 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200926 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200927 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800928
Brian Gix2b64d152011-12-21 16:12:12 -0800929 return ret;
930}
931
Johan Hedberg1cc61142014-05-20 09:45:52 +0300932static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300933{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300934 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300935 struct smp_cmd_pairing_confirm cp;
936 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300937
938 BT_DBG("conn %p", conn);
939
Johan Hedberge491eaf2014-10-25 21:15:37 +0200940 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200941 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200942 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
943 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300944 if (ret)
945 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300946
Johan Hedberg4a74d652014-05-20 09:45:50 +0300947 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800948
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300949 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
950
Johan Hedbergb28b4942014-09-05 22:19:55 +0300951 if (conn->hcon->out)
952 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
953 else
954 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
955
Johan Hedberg1cc61142014-05-20 09:45:52 +0300956 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300957}
958
Johan Hedberg861580a2014-05-20 09:45:51 +0300959static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300960{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300961 struct l2cap_conn *conn = smp->conn;
962 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300963 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300964 int ret;
965
Johan Hedbergec70f362014-06-27 14:23:04 +0300966 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300967 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300968
969 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
970
Johan Hedberge491eaf2014-10-25 21:15:37 +0200971 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200972 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200973 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300974 if (ret)
975 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300976
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +0200977 if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) {
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300978 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300979 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300980 }
981
982 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800983 u8 stk[16];
984 __le64 rand = 0;
985 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300986
Johan Hedberge491eaf2014-10-25 21:15:37 +0200987 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300988
Johan Hedberg861580a2014-05-20 09:45:51 +0300989 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
990 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300991
Johan Hedberg8b76ce32015-06-08 18:14:39 +0300992 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300993 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300994 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300995 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300996 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800997 __le64 rand = 0;
998 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300999
Johan Hedberg943a7322014-03-18 12:58:24 +02001000 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1001 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001002
Johan Hedberge491eaf2014-10-25 21:15:37 +02001003 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001004
Johan Hedbergfff34902014-06-10 15:19:50 +03001005 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1006 auth = 1;
1007 else
1008 auth = 0;
1009
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001010 /* Even though there's no _SLAVE suffix this is the
1011 * slave STK we're adding for later lookup (the master
1012 * STK never needs to be stored).
1013 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001014 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001015 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001016 }
1017
Johan Hedberg861580a2014-05-20 09:45:51 +03001018 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001019}
1020
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001021static void smp_notify_keys(struct l2cap_conn *conn)
1022{
1023 struct l2cap_chan *chan = conn->smp;
1024 struct smp_chan *smp = chan->data;
1025 struct hci_conn *hcon = conn->hcon;
1026 struct hci_dev *hdev = hcon->hdev;
1027 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1028 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1029 bool persistent;
1030
Johan Hedbergcad20c22015-10-12 13:36:19 +02001031 if (hcon->type == ACL_LINK) {
1032 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1033 persistent = false;
1034 else
1035 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1036 &hcon->flags);
1037 } else {
1038 /* The LTKs, IRKs and CSRKs should be persistent only if
1039 * both sides had the bonding bit set in their
1040 * authentication requests.
1041 */
1042 persistent = !!((req->auth_req & rsp->auth_req) &
1043 SMP_AUTH_BONDING);
1044 }
1045
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001046 if (smp->remote_irk) {
Johan Hedbergcad20c22015-10-12 13:36:19 +02001047 mgmt_new_irk(hdev, smp->remote_irk, persistent);
1048
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001049 /* Now that user space can be considered to know the
1050 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001051 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001052 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001053 if (hcon->type == LE_LINK) {
1054 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1055 hcon->dst_type = smp->remote_irk->addr_type;
1056 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1057 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001058 }
1059
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001060 if (smp->csrk) {
1061 smp->csrk->bdaddr_type = hcon->dst_type;
1062 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1063 mgmt_new_csrk(hdev, smp->csrk, persistent);
1064 }
1065
1066 if (smp->slave_csrk) {
1067 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1068 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1069 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1070 }
1071
1072 if (smp->ltk) {
1073 smp->ltk->bdaddr_type = hcon->dst_type;
1074 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1075 mgmt_new_ltk(hdev, smp->ltk, persistent);
1076 }
1077
1078 if (smp->slave_ltk) {
1079 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1080 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1081 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1082 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001083
1084 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001085 struct link_key *key;
1086 u8 type;
1087
1088 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1089 type = HCI_LK_DEBUG_COMBINATION;
1090 else if (hcon->sec_level == BT_SECURITY_FIPS)
1091 type = HCI_LK_AUTH_COMBINATION_P256;
1092 else
1093 type = HCI_LK_UNAUTH_COMBINATION_P256;
1094
1095 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1096 smp->link_key, type, 0, &persistent);
1097 if (key) {
1098 mgmt_new_link_key(hdev, key, persistent);
1099
1100 /* Don't keep debug keys around if the relevant
1101 * flag is not set.
1102 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001103 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001104 key->type == HCI_LK_DEBUG_COMBINATION) {
1105 list_del_rcu(&key->list);
1106 kfree_rcu(key, rcu);
1107 }
1108 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001109 }
1110}
1111
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001112static void sc_add_ltk(struct smp_chan *smp)
1113{
1114 struct hci_conn *hcon = smp->conn->hcon;
1115 u8 key_type, auth;
1116
1117 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1118 key_type = SMP_LTK_P256_DEBUG;
1119 else
1120 key_type = SMP_LTK_P256;
1121
1122 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1123 auth = 1;
1124 else
1125 auth = 0;
1126
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001127 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1128 key_type, auth, smp->tk, smp->enc_key_size,
1129 0, 0);
1130}
1131
Johan Hedberg6a770832014-06-06 11:54:04 +03001132static void sc_generate_link_key(struct smp_chan *smp)
1133{
1134 /* These constants are as specified in the core specification.
1135 * In ASCII they spell out to 'tmp1' and 'lebr'.
1136 */
1137 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1138 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1139
1140 smp->link_key = kzalloc(16, GFP_KERNEL);
1141 if (!smp->link_key)
1142 return;
1143
1144 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001145 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001146 smp->link_key = NULL;
1147 return;
1148 }
1149
1150 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001151 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001152 smp->link_key = NULL;
1153 return;
1154 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001155}
1156
Johan Hedbergb28b4942014-09-05 22:19:55 +03001157static void smp_allow_key_dist(struct smp_chan *smp)
1158{
1159 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1160 * will be allowed in each PDU handler to ensure we receive
1161 * them in the correct order.
1162 */
1163 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1164 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1165 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1166 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1167 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1168 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1169}
1170
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001171static void sc_generate_ltk(struct smp_chan *smp)
1172{
1173 /* These constants are as specified in the core specification.
1174 * In ASCII they spell out to 'tmp2' and 'brle'.
1175 */
1176 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1177 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1178 struct hci_conn *hcon = smp->conn->hcon;
1179 struct hci_dev *hdev = hcon->hdev;
1180 struct link_key *key;
1181
1182 key = hci_find_link_key(hdev, &hcon->dst);
1183 if (!key) {
1184 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1185 return;
1186 }
1187
1188 if (key->type == HCI_LK_DEBUG_COMBINATION)
1189 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1190
1191 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1192 return;
1193
1194 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1195 return;
1196
1197 sc_add_ltk(smp);
1198}
1199
Johan Hedbergd6268e82014-09-05 22:19:51 +03001200static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001201{
1202 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001203 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001204 struct hci_conn *hcon = conn->hcon;
1205 struct hci_dev *hdev = hcon->hdev;
1206 __u8 *keydist;
1207
1208 BT_DBG("conn %p", conn);
1209
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001210 rsp = (void *) &smp->prsp[1];
1211
1212 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001213 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1214 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001215 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001216 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001217
1218 req = (void *) &smp->preq[1];
1219
1220 if (hcon->out) {
1221 keydist = &rsp->init_key_dist;
1222 *keydist &= req->init_key_dist;
1223 } else {
1224 keydist = &rsp->resp_key_dist;
1225 *keydist &= req->resp_key_dist;
1226 }
1227
Johan Hedberg6a770832014-06-06 11:54:04 +03001228 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001229 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001230 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001231 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1232 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001233
1234 /* Clear the keys which are generated but not distributed */
1235 *keydist &= ~SMP_SC_NO_DIST;
1236 }
1237
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001238 BT_DBG("keydist 0x%x", *keydist);
1239
1240 if (*keydist & SMP_DIST_ENC_KEY) {
1241 struct smp_cmd_encrypt_info enc;
1242 struct smp_cmd_master_ident ident;
1243 struct smp_ltk *ltk;
1244 u8 authenticated;
1245 __le16 ediv;
1246 __le64 rand;
1247
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001248 /* Make sure we generate only the significant amount of
1249 * bytes based on the encryption key size, and set the rest
1250 * of the value to zeroes.
1251 */
1252 get_random_bytes(enc.ltk, smp->enc_key_size);
1253 memset(enc.ltk + smp->enc_key_size, 0,
1254 sizeof(enc.ltk) - smp->enc_key_size);
1255
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001256 get_random_bytes(&ediv, sizeof(ediv));
1257 get_random_bytes(&rand, sizeof(rand));
1258
1259 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1260
1261 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1262 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1263 SMP_LTK_SLAVE, authenticated, enc.ltk,
1264 smp->enc_key_size, ediv, rand);
1265 smp->slave_ltk = ltk;
1266
1267 ident.ediv = ediv;
1268 ident.rand = rand;
1269
1270 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1271
1272 *keydist &= ~SMP_DIST_ENC_KEY;
1273 }
1274
1275 if (*keydist & SMP_DIST_ID_KEY) {
1276 struct smp_cmd_ident_addr_info addrinfo;
1277 struct smp_cmd_ident_info idinfo;
1278
1279 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1280
1281 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1282
1283 /* The hci_conn contains the local identity address
1284 * after the connection has been established.
1285 *
1286 * This is true even when the connection has been
1287 * established using a resolvable random address.
1288 */
1289 bacpy(&addrinfo.bdaddr, &hcon->src);
1290 addrinfo.addr_type = hcon->src_type;
1291
1292 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1293 &addrinfo);
1294
1295 *keydist &= ~SMP_DIST_ID_KEY;
1296 }
1297
1298 if (*keydist & SMP_DIST_SIGN) {
1299 struct smp_cmd_sign_info sign;
1300 struct smp_csrk *csrk;
1301
1302 /* Generate a new random key */
1303 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1304
1305 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1306 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001307 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1308 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1309 else
1310 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001311 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1312 }
1313 smp->slave_csrk = csrk;
1314
1315 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1316
1317 *keydist &= ~SMP_DIST_SIGN;
1318 }
1319
1320 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001321 if (smp->remote_key_dist & KEY_DIST_MASK) {
1322 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001323 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001324 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001325
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001326 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1327 smp_notify_keys(conn);
1328
1329 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001330}
1331
Johan Hedbergb68fda62014-08-11 22:06:40 +03001332static void smp_timeout(struct work_struct *work)
1333{
1334 struct smp_chan *smp = container_of(work, struct smp_chan,
1335 security_timer.work);
1336 struct l2cap_conn *conn = smp->conn;
1337
1338 BT_DBG("conn %p", conn);
1339
Johan Hedberg1e91c292014-08-18 20:33:29 +03001340 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001341}
1342
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001343static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1344{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001345 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001346 struct smp_chan *smp;
1347
Marcel Holtmannf1560462013-10-13 05:43:25 -07001348 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001349 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001350 return NULL;
1351
Andy Lutomirskia4770e12016-06-26 14:55:23 -07001352 smp->tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001353 if (IS_ERR(smp->tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07001354 BT_ERR("Unable to create AES crypto context");
Marcel Holtmann276812e2015-03-16 01:10:18 -07001355 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001356 return NULL;
1357 }
1358
Herbert Xu71af2f62016-01-24 21:18:30 +08001359 smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001360 if (IS_ERR(smp->tfm_cmac)) {
1361 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07001362 crypto_free_cipher(smp->tfm_aes);
Marcel Holtmann276812e2015-03-16 01:10:18 -07001363 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001364 return NULL;
1365 }
1366
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001367 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001368 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001369
Johan Hedbergb28b4942014-09-05 22:19:55 +03001370 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1371
Johan Hedbergb68fda62014-08-11 22:06:40 +03001372 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1373
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001374 hci_conn_hold(conn->hcon);
1375
1376 return smp;
1377}
1378
Johan Hedberg760b0182014-06-06 11:44:05 +03001379static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1380{
1381 struct hci_conn *hcon = smp->conn->hcon;
1382 u8 *na, *nb, a[7], b[7];
1383
1384 if (hcon->out) {
1385 na = smp->prnd;
1386 nb = smp->rrnd;
1387 } else {
1388 na = smp->rrnd;
1389 nb = smp->prnd;
1390 }
1391
1392 memcpy(a, &hcon->init_addr, 6);
1393 memcpy(b, &hcon->resp_addr, 6);
1394 a[6] = hcon->init_addr_type;
1395 b[6] = hcon->resp_addr_type;
1396
1397 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1398}
1399
Johan Hedberg38606f12014-06-25 11:10:28 +03001400static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001401{
1402 struct hci_conn *hcon = smp->conn->hcon;
1403 struct smp_cmd_dhkey_check check;
1404 u8 a[7], b[7], *local_addr, *remote_addr;
1405 u8 io_cap[3], r[16];
1406
Johan Hedberg760b0182014-06-06 11:44:05 +03001407 memcpy(a, &hcon->init_addr, 6);
1408 memcpy(b, &hcon->resp_addr, 6);
1409 a[6] = hcon->init_addr_type;
1410 b[6] = hcon->resp_addr_type;
1411
1412 if (hcon->out) {
1413 local_addr = a;
1414 remote_addr = b;
1415 memcpy(io_cap, &smp->preq[1], 3);
1416 } else {
1417 local_addr = b;
1418 remote_addr = a;
1419 memcpy(io_cap, &smp->prsp[1], 3);
1420 }
1421
Johan Hedbergdddd3052014-06-01 15:38:09 +03001422 memset(r, 0, sizeof(r));
1423
1424 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001425 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001426
Johan Hedberga29b0732014-10-28 15:17:05 +01001427 if (smp->method == REQ_OOB)
1428 memcpy(r, smp->rr, 16);
1429
Johan Hedberg760b0182014-06-06 11:44:05 +03001430 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1431 local_addr, remote_addr, check.e);
1432
1433 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001434}
1435
Johan Hedberg38606f12014-06-25 11:10:28 +03001436static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1437{
1438 struct l2cap_conn *conn = smp->conn;
1439 struct hci_conn *hcon = conn->hcon;
1440 struct smp_cmd_pairing_confirm cfm;
1441 u8 r;
1442
1443 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1444 r |= 0x80;
1445
1446 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1447
1448 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1449 cfm.confirm_val))
1450 return SMP_UNSPECIFIED;
1451
1452 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1453
1454 return 0;
1455}
1456
1457static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1458{
1459 struct l2cap_conn *conn = smp->conn;
1460 struct hci_conn *hcon = conn->hcon;
1461 struct hci_dev *hdev = hcon->hdev;
1462 u8 cfm[16], r;
1463
1464 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1465 if (smp->passkey_round >= 20)
1466 return 0;
1467
1468 switch (smp_op) {
1469 case SMP_CMD_PAIRING_RANDOM:
1470 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1471 r |= 0x80;
1472
1473 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1474 smp->rrnd, r, cfm))
1475 return SMP_UNSPECIFIED;
1476
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02001477 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg38606f12014-06-25 11:10:28 +03001478 return SMP_CONFIRM_FAILED;
1479
1480 smp->passkey_round++;
1481
1482 if (smp->passkey_round == 20) {
1483 /* Generate MacKey and LTK */
1484 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1485 return SMP_UNSPECIFIED;
1486 }
1487
1488 /* The round is only complete when the initiator
1489 * receives pairing random.
1490 */
1491 if (!hcon->out) {
1492 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1493 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001494 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001495 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001496 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001497 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001498 return 0;
1499 }
1500
1501 /* Start the next round */
1502 if (smp->passkey_round != 20)
1503 return sc_passkey_round(smp, 0);
1504
1505 /* Passkey rounds are complete - start DHKey Check */
1506 sc_dhkey_check(smp);
1507 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1508
1509 break;
1510
1511 case SMP_CMD_PAIRING_CONFIRM:
1512 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1513 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1514 return 0;
1515 }
1516
1517 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1518
1519 if (hcon->out) {
1520 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1521 sizeof(smp->prnd), smp->prnd);
1522 return 0;
1523 }
1524
1525 return sc_passkey_send_confirm(smp);
1526
1527 case SMP_CMD_PUBLIC_KEY:
1528 default:
1529 /* Initiating device starts the round */
1530 if (!hcon->out)
1531 return 0;
1532
1533 BT_DBG("%s Starting passkey round %u", hdev->name,
1534 smp->passkey_round + 1);
1535
1536 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1537
1538 return sc_passkey_send_confirm(smp);
1539 }
1540
1541 return 0;
1542}
1543
Johan Hedbergdddd3052014-06-01 15:38:09 +03001544static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1545{
Johan Hedberg38606f12014-06-25 11:10:28 +03001546 struct l2cap_conn *conn = smp->conn;
1547 struct hci_conn *hcon = conn->hcon;
1548 u8 smp_op;
1549
1550 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1551
Johan Hedbergdddd3052014-06-01 15:38:09 +03001552 switch (mgmt_op) {
1553 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1554 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1555 return 0;
1556 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1557 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1558 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001559 case MGMT_OP_USER_PASSKEY_REPLY:
1560 hcon->passkey_notify = le32_to_cpu(passkey);
1561 smp->passkey_round = 0;
1562
1563 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1564 smp_op = SMP_CMD_PAIRING_CONFIRM;
1565 else
1566 smp_op = 0;
1567
1568 if (sc_passkey_round(smp, smp_op))
1569 return -EIO;
1570
1571 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001572 }
1573
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001574 /* Initiator sends DHKey check first */
1575 if (hcon->out) {
1576 sc_dhkey_check(smp);
1577 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1578 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1579 sc_dhkey_check(smp);
1580 sc_add_ltk(smp);
1581 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001582
1583 return 0;
1584}
1585
Brian Gix2b64d152011-12-21 16:12:12 -08001586int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1587{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001588 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001589 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001590 struct smp_chan *smp;
1591 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001592 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001593
1594 BT_DBG("");
1595
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001596 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001597 return -ENOTCONN;
1598
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001599 chan = conn->smp;
1600 if (!chan)
1601 return -ENOTCONN;
1602
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001603 l2cap_chan_lock(chan);
1604 if (!chan->data) {
1605 err = -ENOTCONN;
1606 goto unlock;
1607 }
1608
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001609 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001610
Johan Hedberg760b0182014-06-06 11:44:05 +03001611 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1612 err = sc_user_reply(smp, mgmt_op, passkey);
1613 goto unlock;
1614 }
1615
Brian Gix2b64d152011-12-21 16:12:12 -08001616 switch (mgmt_op) {
1617 case MGMT_OP_USER_PASSKEY_REPLY:
1618 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001619 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001620 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001621 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001622 /* Fall Through */
1623 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001624 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001625 break;
1626 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1627 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001628 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001629 err = 0;
1630 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001631 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001632 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001633 err = -EOPNOTSUPP;
1634 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001635 }
1636
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001637 err = 0;
1638
Brian Gix2b64d152011-12-21 16:12:12 -08001639 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001640 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1641 u8 rsp = smp_confirm(smp);
1642 if (rsp)
1643 smp_failure(conn, rsp);
1644 }
Brian Gix2b64d152011-12-21 16:12:12 -08001645
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001646unlock:
1647 l2cap_chan_unlock(chan);
1648 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001649}
1650
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001651static void build_bredr_pairing_cmd(struct smp_chan *smp,
1652 struct smp_cmd_pairing *req,
1653 struct smp_cmd_pairing *rsp)
1654{
1655 struct l2cap_conn *conn = smp->conn;
1656 struct hci_dev *hdev = conn->hcon->hdev;
1657 u8 local_dist = 0, remote_dist = 0;
1658
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001659 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001660 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1661 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1662 }
1663
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001664 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001665 remote_dist |= SMP_DIST_ID_KEY;
1666
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001667 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001668 local_dist |= SMP_DIST_ID_KEY;
1669
1670 if (!rsp) {
1671 memset(req, 0, sizeof(*req));
1672
1673 req->init_key_dist = local_dist;
1674 req->resp_key_dist = remote_dist;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001675 req->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001676
1677 smp->remote_key_dist = remote_dist;
1678
1679 return;
1680 }
1681
1682 memset(rsp, 0, sizeof(*rsp));
1683
Johan Hedberge3f6a252015-06-11 13:52:30 +03001684 rsp->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001685 rsp->init_key_dist = req->init_key_dist & remote_dist;
1686 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1687
1688 smp->remote_key_dist = rsp->init_key_dist;
1689}
1690
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001691static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001692{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001693 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001694 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001695 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001696 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001697 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001698 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001699
1700 BT_DBG("conn %p", conn);
1701
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001702 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001703 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001704
Johan Hedberg40bef302014-07-16 11:42:27 +03001705 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001706 return SMP_CMD_NOTSUPP;
1707
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001708 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001709 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001710 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001711 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001712
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001713 if (!smp)
1714 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001715
Johan Hedbergc05b9332014-09-10 17:37:42 -07001716 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001717 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001718
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001719 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001720 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001721 return SMP_PAIRING_NOTSUPP;
1722
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001723 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001724 return SMP_AUTH_REQUIREMENTS;
1725
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001726 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1727 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001728 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001729
Johan Hedbergcb06d362015-03-16 21:12:34 +02001730 /* If the remote side's OOB flag is set it means it has
1731 * successfully received our local OOB data - therefore set the
1732 * flag to indicate that local OOB is in use.
1733 */
Johan Hedberg58428562015-03-16 11:45:45 +02001734 if (req->oob_flag == SMP_OOB_PRESENT)
1735 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1736
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001737 /* SMP over BR/EDR requires special treatment */
1738 if (conn->hcon->type == ACL_LINK) {
1739 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001740 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001741 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001742 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1743
1744 set_bit(SMP_FLAG_SC, &smp->flags);
1745
1746 build_bredr_pairing_cmd(smp, req, &rsp);
1747
1748 key_size = min(req->max_key_size, rsp.max_key_size);
1749 if (check_enc_key_size(conn, key_size))
1750 return SMP_ENC_KEY_SIZE;
1751
1752 /* Clear bits which are generated but not distributed */
1753 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1754
1755 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1756 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1757 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1758
1759 smp_distribute_keys(smp);
1760 return 0;
1761 }
1762
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001763 build_pairing_cmd(conn, req, &rsp, auth);
1764
1765 if (rsp.auth_req & SMP_AUTH_SC)
1766 set_bit(SMP_FLAG_SC, &smp->flags);
1767
Johan Hedberg5be5e272014-09-10 17:58:54 -07001768 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001769 sec_level = BT_SECURITY_MEDIUM;
1770 else
1771 sec_level = authreq_to_seclevel(auth);
1772
Johan Hedbergc7262e72014-06-17 13:07:37 +03001773 if (sec_level > conn->hcon->pending_sec_level)
1774 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001775
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001776 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001777 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1778 u8 method;
1779
1780 method = get_auth_method(smp, conn->hcon->io_capability,
1781 req->io_capability);
1782 if (method == JUST_WORKS || method == JUST_CFM)
1783 return SMP_AUTH_REQUIREMENTS;
1784 }
1785
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001786 key_size = min(req->max_key_size, rsp.max_key_size);
1787 if (check_enc_key_size(conn, key_size))
1788 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001789
Johan Hedberge84a6b12013-12-02 10:49:03 +02001790 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001791
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001792 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1793 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001794
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001795 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001796
1797 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1798
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001799 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1800 * SC case, however some implementations incorrectly copy RFU auth
1801 * req bits from our security request, which may create a false
1802 * positive SC enablement.
1803 */
1804 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1805
Johan Hedberg3b191462014-06-06 10:50:15 +03001806 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1807 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1808 /* Clear bits which are generated but not distributed */
1809 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1810 /* Wait for Public Key from Initiating Device */
1811 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001812 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001813
Brian Gix2b64d152011-12-21 16:12:12 -08001814 /* Request setup of TK */
1815 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1816 if (ret)
1817 return SMP_UNSPECIFIED;
1818
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001819 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001820}
1821
Johan Hedberg3b191462014-06-06 10:50:15 +03001822static u8 sc_send_public_key(struct smp_chan *smp)
1823{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001824 struct hci_dev *hdev = smp->conn->hcon->hdev;
1825
Johan Hedberg3b191462014-06-06 10:50:15 +03001826 BT_DBG("");
1827
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001828 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001829 struct l2cap_chan *chan = hdev->smp_data;
1830 struct smp_dev *smp_dev;
1831
1832 if (!chan || !chan->data)
1833 return SMP_UNSPECIFIED;
1834
1835 smp_dev = chan->data;
1836
1837 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1838 memcpy(smp->local_sk, smp_dev->local_sk, 32);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001839 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001840
1841 if (smp_dev->debug_key)
1842 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1843
1844 goto done;
1845 }
1846
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001847 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001848 BT_DBG("Using debug keys");
1849 memcpy(smp->local_pk, debug_pk, 64);
1850 memcpy(smp->local_sk, debug_sk, 32);
1851 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1852 } else {
1853 while (true) {
1854 /* Generate local key pair for Secure Connections */
1855 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1856 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001857
Johan Hedberg70157ef2014-06-24 15:22:59 +03001858 /* This is unlikely, but we need to check that
1859 * we didn't accidentially generate a debug key.
1860 */
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02001861 if (crypto_memneq(smp->local_sk, debug_sk, 32))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001862 break;
1863 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001864 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001865
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001866done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001867 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001868 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001869 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001870
1871 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1872
1873 return 0;
1874}
1875
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001876static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001877{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001878 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001879 struct l2cap_chan *chan = conn->smp;
1880 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001881 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001882 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001883 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001884
1885 BT_DBG("conn %p", conn);
1886
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001887 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001888 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001889
Johan Hedberg40bef302014-07-16 11:42:27 +03001890 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001891 return SMP_CMD_NOTSUPP;
1892
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001893 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001894
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001895 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001896
1897 key_size = min(req->max_key_size, rsp->max_key_size);
1898 if (check_enc_key_size(conn, key_size))
1899 return SMP_ENC_KEY_SIZE;
1900
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001901 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001902
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001903 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001904 return SMP_AUTH_REQUIREMENTS;
1905
Johan Hedbergcb06d362015-03-16 21:12:34 +02001906 /* If the remote side's OOB flag is set it means it has
1907 * successfully received our local OOB data - therefore set the
1908 * flag to indicate that local OOB is in use.
1909 */
Johan Hedberg58428562015-03-16 11:45:45 +02001910 if (rsp->oob_flag == SMP_OOB_PRESENT)
1911 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1912
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001913 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1914 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1915
1916 /* Update remote key distribution in case the remote cleared
1917 * some bits that we had enabled in our request.
1918 */
1919 smp->remote_key_dist &= rsp->resp_key_dist;
1920
1921 /* For BR/EDR this means we're done and can start phase 3 */
1922 if (conn->hcon->type == ACL_LINK) {
1923 /* Clear bits which are generated but not distributed */
1924 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1925 smp_distribute_keys(smp);
1926 return 0;
1927 }
1928
Johan Hedberg65668772014-05-16 11:03:34 +03001929 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1930 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001931 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1932 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001933
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001934 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001935 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1936 u8 method;
1937
1938 method = get_auth_method(smp, req->io_capability,
1939 rsp->io_capability);
1940 if (method == JUST_WORKS || method == JUST_CFM)
1941 return SMP_AUTH_REQUIREMENTS;
1942 }
1943
Johan Hedberge84a6b12013-12-02 10:49:03 +02001944 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001945
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001946 /* Update remote key distribution in case the remote cleared
1947 * some bits that we had enabled in our request.
1948 */
1949 smp->remote_key_dist &= rsp->resp_key_dist;
1950
Johan Hedberg3b191462014-06-06 10:50:15 +03001951 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1952 /* Clear bits which are generated but not distributed */
1953 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1954 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1955 return sc_send_public_key(smp);
1956 }
1957
Johan Hedbergc05b9332014-09-10 17:37:42 -07001958 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001959
Johan Hedberg476585e2012-06-06 18:54:15 +08001960 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001961 if (ret)
1962 return SMP_UNSPECIFIED;
1963
Johan Hedberg4a74d652014-05-20 09:45:50 +03001964 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001965
1966 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001967 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001968 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001969
1970 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001971}
1972
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001973static u8 sc_check_confirm(struct smp_chan *smp)
1974{
1975 struct l2cap_conn *conn = smp->conn;
1976
1977 BT_DBG("");
1978
Johan Hedberg38606f12014-06-25 11:10:28 +03001979 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1980 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1981
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001982 if (conn->hcon->out) {
1983 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1984 smp->prnd);
1985 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1986 }
1987
1988 return 0;
1989}
1990
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001991/* Work-around for some implementations that incorrectly copy RFU bits
1992 * from our security request and thereby create the impression that
1993 * we're doing SC when in fact the remote doesn't support it.
1994 */
1995static int fixup_sc_false_positive(struct smp_chan *smp)
1996{
1997 struct l2cap_conn *conn = smp->conn;
1998 struct hci_conn *hcon = conn->hcon;
1999 struct hci_dev *hdev = hcon->hdev;
2000 struct smp_cmd_pairing *req, *rsp;
2001 u8 auth;
2002
2003 /* The issue is only observed when we're in slave role */
2004 if (hcon->out)
2005 return SMP_UNSPECIFIED;
2006
2007 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
2008 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2009 return SMP_UNSPECIFIED;
2010 }
2011
2012 BT_ERR("Trying to fall back to legacy SMP");
2013
2014 req = (void *) &smp->preq[1];
2015 rsp = (void *) &smp->prsp[1];
2016
2017 /* Rebuild key dist flags which may have been cleared for SC */
2018 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2019
2020 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2021
2022 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
2023 BT_ERR("Failed to fall back to legacy SMP");
2024 return SMP_UNSPECIFIED;
2025 }
2026
2027 clear_bit(SMP_FLAG_SC, &smp->flags);
2028
2029 return 0;
2030}
2031
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002032static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002033{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002034 struct l2cap_chan *chan = conn->smp;
2035 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002036
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002037 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2038
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002039 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002040 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002041
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002042 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2043 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002044
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002045 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2046 int ret;
2047
2048 /* Public Key exchange must happen before any other steps */
2049 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2050 return sc_check_confirm(smp);
2051
2052 BT_ERR("Unexpected SMP Pairing Confirm");
2053
2054 ret = fixup_sc_false_positive(smp);
2055 if (ret)
2056 return ret;
2057 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002058
Johan Hedbergb28b4942014-09-05 22:19:55 +03002059 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002060 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2061 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002062 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2063 return 0;
2064 }
2065
2066 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002067 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002068
2069 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002070
2071 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002072}
2073
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002074static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002075{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002076 struct l2cap_chan *chan = conn->smp;
2077 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002078 struct hci_conn *hcon = conn->hcon;
2079 u8 *pkax, *pkbx, *na, *nb;
2080 u32 passkey;
2081 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002082
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002083 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002084
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002085 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002086 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002087
Johan Hedberg943a7322014-03-18 12:58:24 +02002088 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002089 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002090
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002091 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2092 return smp_random(smp);
2093
Johan Hedberg580039e2014-12-03 16:26:37 +02002094 if (hcon->out) {
2095 pkax = smp->local_pk;
2096 pkbx = smp->remote_pk;
2097 na = smp->prnd;
2098 nb = smp->rrnd;
2099 } else {
2100 pkax = smp->remote_pk;
2101 pkbx = smp->local_pk;
2102 na = smp->rrnd;
2103 nb = smp->prnd;
2104 }
2105
Johan Hedberga29b0732014-10-28 15:17:05 +01002106 if (smp->method == REQ_OOB) {
2107 if (!hcon->out)
2108 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2109 sizeof(smp->prnd), smp->prnd);
2110 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2111 goto mackey_and_ltk;
2112 }
2113
Johan Hedberg38606f12014-06-25 11:10:28 +03002114 /* Passkey entry has special treatment */
2115 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2116 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2117
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002118 if (hcon->out) {
2119 u8 cfm[16];
2120
2121 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2122 smp->rrnd, 0, cfm);
2123 if (err)
2124 return SMP_UNSPECIFIED;
2125
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02002126 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002127 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002128 } else {
2129 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2130 smp->prnd);
2131 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002132 }
2133
Johan Hedberga29b0732014-10-28 15:17:05 +01002134mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002135 /* Generate MacKey and LTK */
2136 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2137 if (err)
2138 return SMP_UNSPECIFIED;
2139
Johan Hedberga29b0732014-10-28 15:17:05 +01002140 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002141 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002142 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002143 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2144 }
2145 return 0;
2146 }
2147
Johan Hedberg38606f12014-06-25 11:10:28 +03002148 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002149 if (err)
2150 return SMP_UNSPECIFIED;
2151
Johan Hedberg38606f12014-06-25 11:10:28 +03002152 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2153 hcon->dst_type, passkey, 0);
2154 if (err)
2155 return SMP_UNSPECIFIED;
2156
2157 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2158
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002159 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002160}
2161
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002162static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002163{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002164 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002165 struct hci_conn *hcon = conn->hcon;
2166
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002167 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002168 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002169 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002170
Johan Hedberga6f78332014-09-10 17:37:45 -07002171 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002172 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002173
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002174 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002175 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002176
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002177 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002178 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002179
Johan Hedbergfe59a052014-07-01 19:14:12 +03002180 /* We never store STKs for master role, so clear this flag */
2181 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2182
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002183 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002184}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002185
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002186bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2187 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002188{
2189 if (sec_level == BT_SECURITY_LOW)
2190 return true;
2191
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002192 /* If we're encrypted with an STK but the caller prefers using
2193 * LTK claim insufficient security. This way we allow the
2194 * connection to be re-encrypted with an LTK, even if the LTK
2195 * provides the same level of security. Only exception is if we
2196 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002197 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002198 if (key_pref == SMP_USE_LTK &&
2199 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002200 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002201 return false;
2202
Johan Hedberg854f4722014-07-01 18:40:20 +03002203 if (hcon->sec_level >= sec_level)
2204 return true;
2205
2206 return false;
2207}
2208
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002209static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002210{
2211 struct smp_cmd_security_req *rp = (void *) skb->data;
2212 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002213 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002214 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002215 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002216 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002217
2218 BT_DBG("conn %p", conn);
2219
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002220 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002221 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002222
Johan Hedberg40bef302014-07-16 11:42:27 +03002223 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002224 return SMP_CMD_NOTSUPP;
2225
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002226 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002227
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002228 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002229 return SMP_AUTH_REQUIREMENTS;
2230
Johan Hedberg5be5e272014-09-10 17:58:54 -07002231 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002232 sec_level = BT_SECURITY_MEDIUM;
2233 else
2234 sec_level = authreq_to_seclevel(auth);
2235
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002236 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002237 return 0;
2238
Johan Hedbergc7262e72014-06-17 13:07:37 +03002239 if (sec_level > hcon->pending_sec_level)
2240 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002241
Johan Hedberg4dab7862012-06-07 14:58:37 +08002242 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002243 return 0;
2244
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002245 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002246 if (!smp)
2247 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002248
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002249 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002250 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002251 return SMP_PAIRING_NOTSUPP;
2252
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002253 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002254
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002255 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002256 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002257
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002258 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2259 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002260
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002261 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002262 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002263
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002264 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002265}
2266
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002267int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002268{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002269 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002270 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002271 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002272 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002273 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002274
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002275 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2276
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002277 /* This may be NULL if there's an unexpected disconnection */
2278 if (!conn)
2279 return 1;
2280
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002281 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002282 return 1;
2283
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002284 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002285 return 1;
2286
Johan Hedbergc7262e72014-06-17 13:07:37 +03002287 if (sec_level > hcon->pending_sec_level)
2288 hcon->pending_sec_level = sec_level;
2289
Johan Hedberg40bef302014-07-16 11:42:27 +03002290 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002291 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2292 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002293
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002294 chan = conn->smp;
2295 if (!chan) {
2296 BT_ERR("SMP security requested but not available");
2297 return 1;
2298 }
2299
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002300 l2cap_chan_lock(chan);
2301
2302 /* If SMP is already in progress ignore this request */
2303 if (chan->data) {
2304 ret = 0;
2305 goto unlock;
2306 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002307
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002308 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002309 if (!smp) {
2310 ret = 1;
2311 goto unlock;
2312 }
Brian Gix2b64d152011-12-21 16:12:12 -08002313
2314 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002315
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002316 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002317 authreq |= SMP_AUTH_SC;
2318
Johan Hedberg79897d22014-06-01 09:45:24 +03002319 /* Require MITM if IO Capability allows or the security level
2320 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002321 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002322 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002323 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002324 authreq |= SMP_AUTH_MITM;
2325
Johan Hedberg40bef302014-07-16 11:42:27 +03002326 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002327 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002328
Brian Gix2b64d152011-12-21 16:12:12 -08002329 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002330 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2331 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002332
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002333 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002334 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002335 } else {
2336 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002337 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002338 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002339 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002340 }
2341
Johan Hedberg4a74d652014-05-20 09:45:50 +03002342 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002343 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002344
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002345unlock:
2346 l2cap_chan_unlock(chan);
2347 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002348}
2349
Johan Hedbergc81d5552015-10-22 09:38:35 +03002350void smp_cancel_pairing(struct hci_conn *hcon)
2351{
2352 struct l2cap_conn *conn = hcon->l2cap_data;
2353 struct l2cap_chan *chan;
2354 struct smp_chan *smp;
2355
2356 if (!conn)
2357 return;
2358
2359 chan = conn->smp;
2360 if (!chan)
2361 return;
2362
2363 l2cap_chan_lock(chan);
2364
2365 smp = chan->data;
2366 if (smp) {
2367 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags))
2368 smp_failure(conn, 0);
2369 else
2370 smp_failure(conn, SMP_UNSPECIFIED);
2371 }
2372
2373 l2cap_chan_unlock(chan);
2374}
2375
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002376static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2377{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002378 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002379 struct l2cap_chan *chan = conn->smp;
2380 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002381
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002382 BT_DBG("conn %p", conn);
2383
2384 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002385 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002386
Johan Hedbergb28b4942014-09-05 22:19:55 +03002387 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002388
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002389 skb_pull(skb, sizeof(*rp));
2390
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002391 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002392
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002393 return 0;
2394}
2395
2396static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2397{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002398 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002399 struct l2cap_chan *chan = conn->smp;
2400 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002401 struct hci_dev *hdev = conn->hcon->hdev;
2402 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002403 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002404 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002405
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002406 BT_DBG("conn %p", conn);
2407
2408 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002409 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002410
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002411 /* Mark the information as received */
2412 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2413
Johan Hedbergb28b4942014-09-05 22:19:55 +03002414 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2415 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002416 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2417 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002418
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002419 skb_pull(skb, sizeof(*rp));
2420
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002421 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002422 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002423 authenticated, smp->tk, smp->enc_key_size,
2424 rp->ediv, rp->rand);
2425 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002426 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002427 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002428
2429 return 0;
2430}
2431
Johan Hedbergfd349c02014-02-18 10:19:36 +02002432static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2433{
2434 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002435 struct l2cap_chan *chan = conn->smp;
2436 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002437
2438 BT_DBG("");
2439
2440 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002441 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002442
Johan Hedbergb28b4942014-09-05 22:19:55 +03002443 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002444
Johan Hedbergfd349c02014-02-18 10:19:36 +02002445 skb_pull(skb, sizeof(*info));
2446
2447 memcpy(smp->irk, info->irk, 16);
2448
2449 return 0;
2450}
2451
2452static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2453 struct sk_buff *skb)
2454{
2455 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002456 struct l2cap_chan *chan = conn->smp;
2457 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002458 struct hci_conn *hcon = conn->hcon;
2459 bdaddr_t rpa;
2460
2461 BT_DBG("");
2462
2463 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002464 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002465
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002466 /* Mark the information as received */
2467 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2468
Johan Hedbergb28b4942014-09-05 22:19:55 +03002469 if (smp->remote_key_dist & SMP_DIST_SIGN)
2470 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2471
Johan Hedbergfd349c02014-02-18 10:19:36 +02002472 skb_pull(skb, sizeof(*info));
2473
Johan Hedberga9a58f82014-02-25 22:24:37 +02002474 /* Strictly speaking the Core Specification (4.1) allows sending
2475 * an empty address which would force us to rely on just the IRK
2476 * as "identity information". However, since such
2477 * implementations are not known of and in order to not over
2478 * complicate our implementation, simply pretend that we never
2479 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002480 *
2481 * The Identity Address must also be a Static Random or Public
2482 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002483 */
Johan Hedberge12af482015-01-14 20:51:37 +02002484 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2485 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002486 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002487 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002488 }
2489
Johan Hedbergfd349c02014-02-18 10:19:36 +02002490 bacpy(&smp->id_addr, &info->bdaddr);
2491 smp->id_addr_type = info->addr_type;
2492
2493 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2494 bacpy(&rpa, &hcon->dst);
2495 else
2496 bacpy(&rpa, BDADDR_ANY);
2497
Johan Hedberg23d0e122014-02-19 14:57:46 +02002498 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2499 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002500
Johan Hedberg31dd6242014-06-27 14:23:02 +03002501distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002502 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2503 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002504
2505 return 0;
2506}
2507
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002508static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2509{
2510 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002511 struct l2cap_chan *chan = conn->smp;
2512 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002513 struct smp_csrk *csrk;
2514
2515 BT_DBG("conn %p", conn);
2516
2517 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002518 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002519
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002520 /* Mark the information as received */
2521 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2522
2523 skb_pull(skb, sizeof(*rp));
2524
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002525 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2526 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002527 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2528 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2529 else
2530 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002531 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2532 }
2533 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002534 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002535
2536 return 0;
2537}
2538
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002539static u8 sc_select_method(struct smp_chan *smp)
2540{
2541 struct l2cap_conn *conn = smp->conn;
2542 struct hci_conn *hcon = conn->hcon;
2543 struct smp_cmd_pairing *local, *remote;
2544 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2545
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002546 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2547 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002548 return REQ_OOB;
2549
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002550 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2551 * which are needed as inputs to some crypto functions. To get
2552 * the "struct smp_cmd_pairing" from them we need to skip the
2553 * first byte which contains the opcode.
2554 */
2555 if (hcon->out) {
2556 local = (void *) &smp->preq[1];
2557 remote = (void *) &smp->prsp[1];
2558 } else {
2559 local = (void *) &smp->prsp[1];
2560 remote = (void *) &smp->preq[1];
2561 }
2562
2563 local_io = local->io_capability;
2564 remote_io = remote->io_capability;
2565
2566 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2567 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2568
2569 /* If either side wants MITM, look up the method from the table,
2570 * otherwise use JUST WORKS.
2571 */
2572 if (local_mitm || remote_mitm)
2573 method = get_auth_method(smp, local_io, remote_io);
2574 else
2575 method = JUST_WORKS;
2576
2577 /* Don't confirm locally initiated pairing attempts */
2578 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2579 method = JUST_WORKS;
2580
2581 return method;
2582}
2583
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002584static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2585{
2586 struct smp_cmd_public_key *key = (void *) skb->data;
2587 struct hci_conn *hcon = conn->hcon;
2588 struct l2cap_chan *chan = conn->smp;
2589 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002590 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002591 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002592 int err;
2593
2594 BT_DBG("conn %p", conn);
2595
2596 if (skb->len < sizeof(*key))
2597 return SMP_INVALID_PARAMS;
2598
2599 memcpy(smp->remote_pk, key, 64);
2600
Johan Hedberga8ca6172015-03-16 18:12:57 +02002601 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2602 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2603 smp->rr, 0, cfm.confirm_val);
2604 if (err)
2605 return SMP_UNSPECIFIED;
2606
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02002607 if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16))
Johan Hedberga8ca6172015-03-16 18:12:57 +02002608 return SMP_CONFIRM_FAILED;
2609 }
2610
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002611 /* Non-initiating device sends its public key after receiving
2612 * the key from the initiating device.
2613 */
2614 if (!hcon->out) {
2615 err = sc_send_public_key(smp);
2616 if (err)
2617 return err;
2618 }
2619
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002620 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002621 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002622
2623 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2624 return SMP_UNSPECIFIED;
2625
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002626 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002627
2628 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2629
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002630 smp->method = sc_select_method(smp);
2631
2632 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2633
2634 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2635 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2636 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2637 else
2638 hcon->pending_sec_level = BT_SECURITY_FIPS;
2639
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02002640 if (!crypto_memneq(debug_pk, smp->remote_pk, 64))
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002641 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2642
Johan Hedberg38606f12014-06-25 11:10:28 +03002643 if (smp->method == DSP_PASSKEY) {
2644 get_random_bytes(&hcon->passkey_notify,
2645 sizeof(hcon->passkey_notify));
2646 hcon->passkey_notify %= 1000000;
2647 hcon->passkey_entered = 0;
2648 smp->passkey_round = 0;
2649 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2650 hcon->dst_type,
2651 hcon->passkey_notify,
2652 hcon->passkey_entered))
2653 return SMP_UNSPECIFIED;
2654 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2655 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2656 }
2657
Johan Hedberg94ea7252015-03-16 11:45:46 +02002658 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002659 if (hcon->out)
2660 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2661 sizeof(smp->prnd), smp->prnd);
2662
2663 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2664
2665 return 0;
2666 }
2667
Johan Hedberg38606f12014-06-25 11:10:28 +03002668 if (hcon->out)
2669 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2670
2671 if (smp->method == REQ_PASSKEY) {
2672 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2673 hcon->dst_type))
2674 return SMP_UNSPECIFIED;
2675 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2676 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2677 return 0;
2678 }
2679
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002680 /* The Initiating device waits for the non-initiating device to
2681 * send the confirm value.
2682 */
2683 if (conn->hcon->out)
2684 return 0;
2685
2686 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2687 0, cfm.confirm_val);
2688 if (err)
2689 return SMP_UNSPECIFIED;
2690
2691 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2692 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2693
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002694 return 0;
2695}
2696
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002697static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2698{
2699 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2700 struct l2cap_chan *chan = conn->smp;
2701 struct hci_conn *hcon = conn->hcon;
2702 struct smp_chan *smp = chan->data;
2703 u8 a[7], b[7], *local_addr, *remote_addr;
2704 u8 io_cap[3], r[16], e[16];
2705 int err;
2706
2707 BT_DBG("conn %p", conn);
2708
2709 if (skb->len < sizeof(*check))
2710 return SMP_INVALID_PARAMS;
2711
2712 memcpy(a, &hcon->init_addr, 6);
2713 memcpy(b, &hcon->resp_addr, 6);
2714 a[6] = hcon->init_addr_type;
2715 b[6] = hcon->resp_addr_type;
2716
2717 if (hcon->out) {
2718 local_addr = a;
2719 remote_addr = b;
2720 memcpy(io_cap, &smp->prsp[1], 3);
2721 } else {
2722 local_addr = b;
2723 remote_addr = a;
2724 memcpy(io_cap, &smp->preq[1], 3);
2725 }
2726
2727 memset(r, 0, sizeof(r));
2728
Johan Hedberg38606f12014-06-25 11:10:28 +03002729 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2730 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002731 else if (smp->method == REQ_OOB)
2732 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002733
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002734 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2735 io_cap, remote_addr, local_addr, e);
2736 if (err)
2737 return SMP_UNSPECIFIED;
2738
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02002739 if (crypto_memneq(check->e, e, 16))
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002740 return SMP_DHKEY_CHECK_FAILED;
2741
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002742 if (!hcon->out) {
2743 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2744 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2745 return 0;
2746 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002747
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002748 /* Slave sends DHKey check as response to master */
2749 sc_dhkey_check(smp);
2750 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002751
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002752 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002753
2754 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002755 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002756 hcon->enc_key_size = smp->enc_key_size;
2757 }
2758
2759 return 0;
2760}
2761
Johan Hedberg1408bb62014-06-04 22:45:57 +03002762static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2763 struct sk_buff *skb)
2764{
2765 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2766
2767 BT_DBG("value 0x%02x", kp->value);
2768
2769 return 0;
2770}
2771
Johan Hedberg4befb862014-08-11 22:06:38 +03002772static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002773{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002774 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002775 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002776 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002777 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002778 int err = 0;
2779
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002780 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002781 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002782
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002783 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002784 reason = SMP_PAIRING_NOTSUPP;
2785 goto done;
2786 }
2787
Marcel Holtmann92381f52013-10-03 01:23:08 -07002788 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002789 skb_pull(skb, sizeof(code));
2790
Johan Hedbergb28b4942014-09-05 22:19:55 +03002791 smp = chan->data;
2792
2793 if (code > SMP_CMD_MAX)
2794 goto drop;
2795
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002796 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002797 goto drop;
2798
2799 /* If we don't have a context the only allowed commands are
2800 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002801 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002802 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2803 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002804
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002805 switch (code) {
2806 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002807 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002808 break;
2809
2810 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002811 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002812 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002813 break;
2814
2815 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002816 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002817 break;
2818
2819 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002820 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002821 break;
2822
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002823 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002824 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002825 break;
2826
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002827 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002828 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002829 break;
2830
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002831 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002832 reason = smp_cmd_encrypt_info(conn, skb);
2833 break;
2834
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002835 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002836 reason = smp_cmd_master_ident(conn, skb);
2837 break;
2838
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002839 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002840 reason = smp_cmd_ident_info(conn, skb);
2841 break;
2842
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002843 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002844 reason = smp_cmd_ident_addr_info(conn, skb);
2845 break;
2846
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002847 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002848 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002849 break;
2850
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002851 case SMP_CMD_PUBLIC_KEY:
2852 reason = smp_cmd_public_key(conn, skb);
2853 break;
2854
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002855 case SMP_CMD_DHKEY_CHECK:
2856 reason = smp_cmd_dhkey_check(conn, skb);
2857 break;
2858
Johan Hedberg1408bb62014-06-04 22:45:57 +03002859 case SMP_CMD_KEYPRESS_NOTIFY:
2860 reason = smp_cmd_keypress_notify(conn, skb);
2861 break;
2862
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002863 default:
2864 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002865 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002866 goto done;
2867 }
2868
2869done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002870 if (!err) {
2871 if (reason)
2872 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002873 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002874 }
2875
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002876 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002877
2878drop:
2879 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2880 code, &hcon->dst);
2881 kfree_skb(skb);
2882 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002883}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002884
Johan Hedberg70db83c2014-08-08 09:37:16 +03002885static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2886{
2887 struct l2cap_conn *conn = chan->conn;
2888
2889 BT_DBG("chan %p", chan);
2890
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002891 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002892 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002893
Johan Hedberg70db83c2014-08-08 09:37:16 +03002894 conn->smp = NULL;
2895 l2cap_chan_put(chan);
2896}
2897
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002898static void bredr_pairing(struct l2cap_chan *chan)
2899{
2900 struct l2cap_conn *conn = chan->conn;
2901 struct hci_conn *hcon = conn->hcon;
2902 struct hci_dev *hdev = hcon->hdev;
2903 struct smp_cmd_pairing req;
2904 struct smp_chan *smp;
2905
2906 BT_DBG("chan %p", chan);
2907
2908 /* Only new pairings are interesting */
2909 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2910 return;
2911
2912 /* Don't bother if we're not encrypted */
2913 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2914 return;
2915
2916 /* Only master may initiate SMP over BR/EDR */
2917 if (hcon->role != HCI_ROLE_MASTER)
2918 return;
2919
2920 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002921 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002922 return;
2923
2924 /* BR/EDR must use Secure Connections for SMP */
2925 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002926 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002927 return;
2928
2929 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002930 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002931 return;
2932
2933 /* Don't bother if remote LE support is not enabled */
2934 if (!lmp_host_le_capable(hcon))
2935 return;
2936
2937 /* Remote must support SMP fixed chan for BR/EDR */
2938 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2939 return;
2940
2941 /* Don't bother if SMP is already ongoing */
2942 if (chan->data)
2943 return;
2944
2945 smp = smp_chan_create(conn);
2946 if (!smp) {
2947 BT_ERR("%s unable to create SMP context for BR/EDR",
2948 hdev->name);
2949 return;
2950 }
2951
2952 set_bit(SMP_FLAG_SC, &smp->flags);
2953
2954 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2955
2956 /* Prepare and send the BR/EDR SMP Pairing Request */
2957 build_bredr_pairing_cmd(smp, &req, NULL);
2958
2959 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2960 memcpy(&smp->preq[1], &req, sizeof(req));
2961
2962 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2963 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2964}
2965
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002966static void smp_resume_cb(struct l2cap_chan *chan)
2967{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002968 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002969 struct l2cap_conn *conn = chan->conn;
2970 struct hci_conn *hcon = conn->hcon;
2971
2972 BT_DBG("chan %p", chan);
2973
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002974 if (hcon->type == ACL_LINK) {
2975 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002976 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002977 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002978
Johan Hedberg86d14072014-08-11 22:06:43 +03002979 if (!smp)
2980 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002981
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002982 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2983 return;
2984
Johan Hedberg86d14072014-08-11 22:06:43 +03002985 cancel_delayed_work(&smp->security_timer);
2986
Johan Hedbergd6268e82014-09-05 22:19:51 +03002987 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002988}
2989
Johan Hedberg70db83c2014-08-08 09:37:16 +03002990static void smp_ready_cb(struct l2cap_chan *chan)
2991{
2992 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002993 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002994
2995 BT_DBG("chan %p", chan);
2996
Johan Hedberg78837462015-11-11 21:47:12 +02002997 /* No need to call l2cap_chan_hold() here since we already own
2998 * the reference taken in smp_new_conn_cb(). This is just the
2999 * first time that we tie it to a specific pointer. The code in
3000 * l2cap_core.c ensures that there's no risk this function wont
3001 * get called if smp_new_conn_cb was previously called.
3002 */
Johan Hedberg70db83c2014-08-08 09:37:16 +03003003 conn->smp = chan;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003004
3005 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3006 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003007}
3008
Johan Hedberg4befb862014-08-11 22:06:38 +03003009static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3010{
3011 int err;
3012
3013 BT_DBG("chan %p", chan);
3014
3015 err = smp_sig_channel(chan, skb);
3016 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003017 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003018
Johan Hedbergb68fda62014-08-11 22:06:40 +03003019 if (smp)
3020 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003021
Johan Hedberg1e91c292014-08-18 20:33:29 +03003022 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003023 }
3024
3025 return err;
3026}
3027
Johan Hedberg70db83c2014-08-08 09:37:16 +03003028static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3029 unsigned long hdr_len,
3030 unsigned long len, int nb)
3031{
3032 struct sk_buff *skb;
3033
3034 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3035 if (!skb)
3036 return ERR_PTR(-ENOMEM);
3037
3038 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003039 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003040
3041 return skb;
3042}
3043
3044static const struct l2cap_ops smp_chan_ops = {
3045 .name = "Security Manager",
3046 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003047 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003048 .alloc_skb = smp_alloc_skb_cb,
3049 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003050 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003051
3052 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003053 .state_change = l2cap_chan_no_state_change,
3054 .close = l2cap_chan_no_close,
3055 .defer = l2cap_chan_no_defer,
3056 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003057 .set_shutdown = l2cap_chan_no_set_shutdown,
3058 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003059};
3060
3061static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3062{
3063 struct l2cap_chan *chan;
3064
3065 BT_DBG("pchan %p", pchan);
3066
3067 chan = l2cap_chan_create();
3068 if (!chan)
3069 return NULL;
3070
3071 chan->chan_type = pchan->chan_type;
3072 chan->ops = &smp_chan_ops;
3073 chan->scid = pchan->scid;
3074 chan->dcid = chan->scid;
3075 chan->imtu = pchan->imtu;
3076 chan->omtu = pchan->omtu;
3077 chan->mode = pchan->mode;
3078
Johan Hedbergabe84902014-11-12 22:22:21 +02003079 /* Other L2CAP channels may request SMP routines in order to
3080 * change the security level. This means that the SMP channel
3081 * lock must be considered in its own category to avoid lockdep
3082 * warnings.
3083 */
3084 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3085
Johan Hedberg70db83c2014-08-08 09:37:16 +03003086 BT_DBG("created chan %p", chan);
3087
3088 return chan;
3089}
3090
3091static const struct l2cap_ops smp_root_chan_ops = {
3092 .name = "Security Manager Root",
3093 .new_connection = smp_new_conn_cb,
3094
3095 /* None of these are implemented for the root channel */
3096 .close = l2cap_chan_no_close,
3097 .alloc_skb = l2cap_chan_no_alloc_skb,
3098 .recv = l2cap_chan_no_recv,
3099 .state_change = l2cap_chan_no_state_change,
3100 .teardown = l2cap_chan_no_teardown,
3101 .ready = l2cap_chan_no_ready,
3102 .defer = l2cap_chan_no_defer,
3103 .suspend = l2cap_chan_no_suspend,
3104 .resume = l2cap_chan_no_resume,
3105 .set_shutdown = l2cap_chan_no_set_shutdown,
3106 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003107};
3108
Johan Hedbergef8efe42014-08-13 15:12:32 +03003109static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003110{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003111 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003112 struct smp_dev *smp;
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003113 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +08003114 struct crypto_shash *tfm_cmac;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003115
Johan Hedbergef8efe42014-08-13 15:12:32 +03003116 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003117 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003118 goto create_chan;
3119 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003120
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003121 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3122 if (!smp)
3123 return ERR_PTR(-ENOMEM);
3124
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003125 tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003126 if (IS_ERR(tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003127 BT_ERR("Unable to create AES crypto context");
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003128 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003129 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003130 }
3131
Herbert Xu71af2f62016-01-24 21:18:30 +08003132 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003133 if (IS_ERR(tfm_cmac)) {
3134 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003135 crypto_free_cipher(tfm_aes);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003136 kzfree(smp);
3137 return ERR_CAST(tfm_cmac);
3138 }
3139
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003140 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003141 smp->tfm_cmac = tfm_cmac;
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003142 smp->min_key_size = SMP_MIN_ENC_KEY_SIZE;
Johan Hedberg2fd36552015-06-11 13:52:26 +03003143 smp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003144
Johan Hedbergef8efe42014-08-13 15:12:32 +03003145create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003146 chan = l2cap_chan_create();
3147 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003148 if (smp) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003149 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +08003150 crypto_free_shash(smp->tfm_cmac);
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003151 kzfree(smp);
3152 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003153 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003154 }
3155
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003156 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003157
Johan Hedbergef8efe42014-08-13 15:12:32 +03003158 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003159
3160 l2cap_chan_set_defaults(chan);
3161
Marcel Holtmann157029b2015-01-14 15:43:09 -08003162 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003163 u8 bdaddr_type;
3164
3165 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3166
3167 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003168 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003169 else
3170 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003171 } else {
3172 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003173 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003174 }
3175
Johan Hedberg70db83c2014-08-08 09:37:16 +03003176 chan->state = BT_LISTEN;
3177 chan->mode = L2CAP_MODE_BASIC;
3178 chan->imtu = L2CAP_DEFAULT_MTU;
3179 chan->ops = &smp_root_chan_ops;
3180
Johan Hedbergabe84902014-11-12 22:22:21 +02003181 /* Set correct nesting level for a parent/listening channel */
3182 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3183
Johan Hedbergef8efe42014-08-13 15:12:32 +03003184 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003185}
3186
Johan Hedbergef8efe42014-08-13 15:12:32 +03003187static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003188{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003189 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003190
Johan Hedbergef8efe42014-08-13 15:12:32 +03003191 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003192
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003193 smp = chan->data;
3194 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003195 chan->data = NULL;
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003196 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +08003197 crypto_free_shash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003198 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003199 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003200
Johan Hedberg70db83c2014-08-08 09:37:16 +03003201 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003202}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003203
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003204static ssize_t force_bredr_smp_read(struct file *file,
3205 char __user *user_buf,
3206 size_t count, loff_t *ppos)
3207{
3208 struct hci_dev *hdev = file->private_data;
3209 char buf[3];
3210
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003211 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003212 buf[1] = '\n';
3213 buf[2] = '\0';
3214 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3215}
3216
3217static ssize_t force_bredr_smp_write(struct file *file,
3218 const char __user *user_buf,
3219 size_t count, loff_t *ppos)
3220{
3221 struct hci_dev *hdev = file->private_data;
3222 char buf[32];
3223 size_t buf_size = min(count, (sizeof(buf)-1));
3224 bool enable;
3225
3226 if (copy_from_user(buf, user_buf, buf_size))
3227 return -EFAULT;
3228
3229 buf[buf_size] = '\0';
3230 if (strtobool(buf, &enable))
3231 return -EINVAL;
3232
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003233 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003234 return -EALREADY;
3235
3236 if (enable) {
3237 struct l2cap_chan *chan;
3238
3239 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3240 if (IS_ERR(chan))
3241 return PTR_ERR(chan);
3242
3243 hdev->smp_bredr_data = chan;
3244 } else {
3245 struct l2cap_chan *chan;
3246
3247 chan = hdev->smp_bredr_data;
3248 hdev->smp_bredr_data = NULL;
3249 smp_del_chan(chan);
3250 }
3251
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003252 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003253
3254 return count;
3255}
3256
3257static const struct file_operations force_bredr_smp_fops = {
3258 .open = simple_open,
3259 .read = force_bredr_smp_read,
3260 .write = force_bredr_smp_write,
3261 .llseek = default_llseek,
3262};
3263
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003264static ssize_t le_min_key_size_read(struct file *file,
3265 char __user *user_buf,
3266 size_t count, loff_t *ppos)
3267{
3268 struct hci_dev *hdev = file->private_data;
3269 char buf[4];
3270
3271 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->min_key_size);
3272
3273 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3274}
3275
3276static ssize_t le_min_key_size_write(struct file *file,
3277 const char __user *user_buf,
3278 size_t count, loff_t *ppos)
3279{
3280 struct hci_dev *hdev = file->private_data;
3281 char buf[32];
3282 size_t buf_size = min(count, (sizeof(buf) - 1));
3283 u8 key_size;
3284
3285 if (copy_from_user(buf, user_buf, buf_size))
3286 return -EFAULT;
3287
3288 buf[buf_size] = '\0';
3289
3290 sscanf(buf, "%hhu", &key_size);
3291
3292 if (key_size > SMP_DEV(hdev)->max_key_size ||
3293 key_size < SMP_MIN_ENC_KEY_SIZE)
3294 return -EINVAL;
3295
3296 SMP_DEV(hdev)->min_key_size = key_size;
3297
3298 return count;
3299}
3300
3301static const struct file_operations le_min_key_size_fops = {
3302 .open = simple_open,
3303 .read = le_min_key_size_read,
3304 .write = le_min_key_size_write,
3305 .llseek = default_llseek,
3306};
3307
Johan Hedberg2fd36552015-06-11 13:52:26 +03003308static ssize_t le_max_key_size_read(struct file *file,
3309 char __user *user_buf,
3310 size_t count, loff_t *ppos)
3311{
3312 struct hci_dev *hdev = file->private_data;
3313 char buf[4];
3314
3315 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->max_key_size);
3316
3317 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3318}
3319
3320static ssize_t le_max_key_size_write(struct file *file,
3321 const char __user *user_buf,
3322 size_t count, loff_t *ppos)
3323{
3324 struct hci_dev *hdev = file->private_data;
3325 char buf[32];
3326 size_t buf_size = min(count, (sizeof(buf) - 1));
3327 u8 key_size;
3328
3329 if (copy_from_user(buf, user_buf, buf_size))
3330 return -EFAULT;
3331
3332 buf[buf_size] = '\0';
3333
3334 sscanf(buf, "%hhu", &key_size);
3335
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003336 if (key_size > SMP_MAX_ENC_KEY_SIZE ||
3337 key_size < SMP_DEV(hdev)->min_key_size)
Johan Hedberg2fd36552015-06-11 13:52:26 +03003338 return -EINVAL;
3339
3340 SMP_DEV(hdev)->max_key_size = key_size;
3341
3342 return count;
3343}
3344
3345static const struct file_operations le_max_key_size_fops = {
3346 .open = simple_open,
3347 .read = le_max_key_size_read,
3348 .write = le_max_key_size_write,
3349 .llseek = default_llseek,
3350};
3351
Johan Hedbergef8efe42014-08-13 15:12:32 +03003352int smp_register(struct hci_dev *hdev)
3353{
3354 struct l2cap_chan *chan;
3355
3356 BT_DBG("%s", hdev->name);
3357
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003358 /* If the controller does not support Low Energy operation, then
3359 * there is also no need to register any SMP channel.
3360 */
3361 if (!lmp_le_capable(hdev))
3362 return 0;
3363
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003364 if (WARN_ON(hdev->smp_data)) {
3365 chan = hdev->smp_data;
3366 hdev->smp_data = NULL;
3367 smp_del_chan(chan);
3368 }
3369
Johan Hedbergef8efe42014-08-13 15:12:32 +03003370 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3371 if (IS_ERR(chan))
3372 return PTR_ERR(chan);
3373
3374 hdev->smp_data = chan;
3375
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003376 debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
3377 &le_min_key_size_fops);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003378 debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3379 &le_max_key_size_fops);
3380
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003381 /* If the controller does not support BR/EDR Secure Connections
3382 * feature, then the BR/EDR SMP channel shall not be present.
3383 *
3384 * To test this with Bluetooth 4.0 controllers, create a debugfs
3385 * switch that allows forcing BR/EDR SMP support and accepting
3386 * cross-transport pairing on non-AES encrypted connections.
3387 */
3388 if (!lmp_sc_capable(hdev)) {
3389 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3390 hdev, &force_bredr_smp_fops);
Szymon Janc83ebb9e2016-09-09 20:24:40 +02003391
3392 /* Flag can be already set here (due to power toggle) */
3393 if (!hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
3394 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003395 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003396
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003397 if (WARN_ON(hdev->smp_bredr_data)) {
3398 chan = hdev->smp_bredr_data;
3399 hdev->smp_bredr_data = NULL;
3400 smp_del_chan(chan);
3401 }
3402
Johan Hedbergef8efe42014-08-13 15:12:32 +03003403 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3404 if (IS_ERR(chan)) {
3405 int err = PTR_ERR(chan);
3406 chan = hdev->smp_data;
3407 hdev->smp_data = NULL;
3408 smp_del_chan(chan);
3409 return err;
3410 }
3411
3412 hdev->smp_bredr_data = chan;
3413
3414 return 0;
3415}
3416
3417void smp_unregister(struct hci_dev *hdev)
3418{
3419 struct l2cap_chan *chan;
3420
3421 if (hdev->smp_bredr_data) {
3422 chan = hdev->smp_bredr_data;
3423 hdev->smp_bredr_data = NULL;
3424 smp_del_chan(chan);
3425 }
3426
3427 if (hdev->smp_data) {
3428 chan = hdev->smp_data;
3429 hdev->smp_data = NULL;
3430 smp_del_chan(chan);
3431 }
3432}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003433
3434#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3435
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003436static int __init test_ah(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003437{
3438 const u8 irk[16] = {
3439 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3440 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3441 const u8 r[3] = { 0x94, 0x81, 0x70 };
3442 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3443 u8 res[3];
3444 int err;
3445
3446 err = smp_ah(tfm_aes, irk, r, res);
3447 if (err)
3448 return err;
3449
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003450 if (crypto_memneq(res, exp, 3))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003451 return -EINVAL;
3452
3453 return 0;
3454}
3455
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003456static int __init test_c1(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003457{
3458 const u8 k[16] = {
3459 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3460 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3461 const u8 r[16] = {
3462 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3463 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3464 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3465 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3466 const u8 _iat = 0x01;
3467 const u8 _rat = 0x00;
3468 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3469 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3470 const u8 exp[16] = {
3471 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3472 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3473 u8 res[16];
3474 int err;
3475
3476 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3477 if (err)
3478 return err;
3479
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003480 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003481 return -EINVAL;
3482
3483 return 0;
3484}
3485
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003486static int __init test_s1(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003487{
3488 const u8 k[16] = {
3489 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3490 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3491 const u8 r1[16] = {
3492 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3493 const u8 r2[16] = {
3494 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3495 const u8 exp[16] = {
3496 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3497 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3498 u8 res[16];
3499 int err;
3500
3501 err = smp_s1(tfm_aes, k, r1, r2, res);
3502 if (err)
3503 return err;
3504
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003505 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003506 return -EINVAL;
3507
3508 return 0;
3509}
3510
Herbert Xu71af2f62016-01-24 21:18:30 +08003511static int __init test_f4(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003512{
3513 const u8 u[32] = {
3514 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3515 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3516 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3517 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3518 const u8 v[32] = {
3519 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3520 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3521 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3522 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3523 const u8 x[16] = {
3524 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3525 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3526 const u8 z = 0x00;
3527 const u8 exp[16] = {
3528 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3529 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3530 u8 res[16];
3531 int err;
3532
3533 err = smp_f4(tfm_cmac, u, v, x, z, res);
3534 if (err)
3535 return err;
3536
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003537 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003538 return -EINVAL;
3539
3540 return 0;
3541}
3542
Herbert Xu71af2f62016-01-24 21:18:30 +08003543static int __init test_f5(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003544{
3545 const u8 w[32] = {
3546 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3547 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3548 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3549 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3550 const u8 n1[16] = {
3551 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3552 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3553 const u8 n2[16] = {
3554 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3555 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3556 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3557 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3558 const u8 exp_ltk[16] = {
3559 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3560 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3561 const u8 exp_mackey[16] = {
3562 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3563 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3564 u8 mackey[16], ltk[16];
3565 int err;
3566
3567 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3568 if (err)
3569 return err;
3570
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003571 if (crypto_memneq(mackey, exp_mackey, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003572 return -EINVAL;
3573
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003574 if (crypto_memneq(ltk, exp_ltk, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003575 return -EINVAL;
3576
3577 return 0;
3578}
3579
Herbert Xu71af2f62016-01-24 21:18:30 +08003580static int __init test_f6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003581{
3582 const u8 w[16] = {
3583 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3584 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3585 const u8 n1[16] = {
3586 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3587 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3588 const u8 n2[16] = {
3589 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3590 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3591 const u8 r[16] = {
3592 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3593 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3594 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3595 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3596 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3597 const u8 exp[16] = {
3598 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3599 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3600 u8 res[16];
3601 int err;
3602
3603 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3604 if (err)
3605 return err;
3606
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003607 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003608 return -EINVAL;
3609
3610 return 0;
3611}
3612
Herbert Xu71af2f62016-01-24 21:18:30 +08003613static int __init test_g2(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003614{
3615 const u8 u[32] = {
3616 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3617 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3618 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3619 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3620 const u8 v[32] = {
3621 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3622 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3623 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3624 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3625 const u8 x[16] = {
3626 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3627 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3628 const u8 y[16] = {
3629 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3630 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3631 const u32 exp_val = 0x2f9ed5ba % 1000000;
3632 u32 val;
3633 int err;
3634
3635 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3636 if (err)
3637 return err;
3638
3639 if (val != exp_val)
3640 return -EINVAL;
3641
3642 return 0;
3643}
3644
Herbert Xu71af2f62016-01-24 21:18:30 +08003645static int __init test_h6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003646{
3647 const u8 w[16] = {
3648 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3649 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3650 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3651 const u8 exp[16] = {
3652 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3653 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3654 u8 res[16];
3655 int err;
3656
3657 err = smp_h6(tfm_cmac, w, key_id, res);
3658 if (err)
3659 return err;
3660
Jason A. Donenfeld3da27a9d2017-06-10 04:59:11 +02003661 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003662 return -EINVAL;
3663
3664 return 0;
3665}
3666
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003667static char test_smp_buffer[32];
3668
3669static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3670 size_t count, loff_t *ppos)
3671{
3672 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3673 strlen(test_smp_buffer));
3674}
3675
3676static const struct file_operations test_smp_fops = {
3677 .open = simple_open,
3678 .read = test_smp_read,
3679 .llseek = default_llseek,
3680};
3681
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003682static int __init run_selftests(struct crypto_cipher *tfm_aes,
Herbert Xu71af2f62016-01-24 21:18:30 +08003683 struct crypto_shash *tfm_cmac)
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003684{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003685 ktime_t calltime, delta, rettime;
3686 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003687 int err;
3688
Marcel Holtmann255047b2014-12-30 00:11:20 -08003689 calltime = ktime_get();
3690
Johan Hedbergcfc41982014-12-30 09:50:40 +02003691 err = test_ah(tfm_aes);
3692 if (err) {
3693 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003694 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003695 }
3696
3697 err = test_c1(tfm_aes);
3698 if (err) {
3699 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003700 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003701 }
3702
3703 err = test_s1(tfm_aes);
3704 if (err) {
3705 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003706 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003707 }
3708
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003709 err = test_f4(tfm_cmac);
3710 if (err) {
3711 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003712 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003713 }
3714
3715 err = test_f5(tfm_cmac);
3716 if (err) {
3717 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003718 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003719 }
3720
3721 err = test_f6(tfm_cmac);
3722 if (err) {
3723 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003724 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003725 }
3726
3727 err = test_g2(tfm_cmac);
3728 if (err) {
3729 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003730 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003731 }
3732
3733 err = test_h6(tfm_cmac);
3734 if (err) {
3735 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003736 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003737 }
3738
Marcel Holtmann255047b2014-12-30 00:11:20 -08003739 rettime = ktime_get();
3740 delta = ktime_sub(rettime, calltime);
3741 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3742
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003743 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003744
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003745done:
3746 if (!err)
3747 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3748 "PASS (%llu usecs)\n", duration);
3749 else
3750 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3751
3752 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3753 &test_smp_fops);
3754
3755 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003756}
3757
3758int __init bt_selftest_smp(void)
3759{
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003760 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +08003761 struct crypto_shash *tfm_cmac;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003762 int err;
3763
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003764 tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003765 if (IS_ERR(tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003766 BT_ERR("Unable to create AES crypto context");
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003767 return PTR_ERR(tfm_aes);
3768 }
3769
Herbert Xu71af2f62016-01-24 21:18:30 +08003770 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003771 if (IS_ERR(tfm_cmac)) {
3772 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003773 crypto_free_cipher(tfm_aes);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003774 return PTR_ERR(tfm_cmac);
3775 }
3776
3777 err = run_selftests(tfm_aes, tfm_cmac);
3778
Herbert Xu71af2f62016-01-24 21:18:30 +08003779 crypto_free_shash(tfm_cmac);
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003780 crypto_free_cipher(tfm_aes);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003781
3782 return err;
3783}
3784
3785#endif