blob: a0ef89772c366c53097c3a18760e48d302d00656 [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. Donenfeld329d8232017-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
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010035#include "ecdh_helper.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 Hedberga62da6f2016-12-08 08:32:54 +020061 0x3f : 0x07)
Johan Hedberg0edb14d2014-05-26 13:29:28 +030062#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 Hedberga62da6f2016-12-08 08:32:54 +020080 SMP_FLAG_CT2,
Johan Hedberg533e35d2014-06-16 19:25:18 +030081};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030082
Marcel Holtmann88a479d2015-03-16 01:10:19 -070083struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070084 /* Secure Connections OOB data */
85 u8 local_pk[64];
86 u8 local_sk[32];
Marcel Holtmannfb334fe2015-03-16 12:34:57 -070087 u8 local_rand[16];
Marcel Holtmann60a27d62015-03-16 01:10:22 -070088 bool debug_key;
89
Johan Hedbergb1f663c2015-06-11 13:52:27 +030090 u8 min_key_size;
Johan Hedberg2fd36552015-06-11 13:52:26 +030091 u8 max_key_size;
92
Andy Lutomirskia4770e12016-06-26 14:55:23 -070093 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +080094 struct crypto_shash *tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070095};
96
Johan Hedberg4bc58f52014-05-20 09:45:47 +030097struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030098 struct l2cap_conn *conn;
99 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +0300100 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +0300101
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300102 u8 preq[7]; /* SMP Pairing Request */
103 u8 prsp[7]; /* SMP Pairing Response */
104 u8 prnd[16]; /* SMP Pairing Random (local) */
105 u8 rrnd[16]; /* SMP Pairing Random (remote) */
106 u8 pcnf[16]; /* SMP Pairing Confirm */
107 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +0200108 u8 rr[16]; /* Remote OOB ra/rb value */
109 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300110 u8 enc_key_size;
111 u8 remote_key_dist;
112 bdaddr_t id_addr;
113 u8 id_addr_type;
114 u8 irk[16];
115 struct smp_csrk *csrk;
116 struct smp_csrk *slave_csrk;
117 struct smp_ltk *ltk;
118 struct smp_ltk *slave_ltk;
119 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300120 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300121 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300122 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300123 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300124
Johan Hedberg3b191462014-06-06 10:50:15 +0300125 /* Secure Connections variables */
126 u8 local_pk[64];
127 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300128 u8 remote_pk[64];
129 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300130 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300131
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700132 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +0800133 struct crypto_shash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300134};
135
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300136/* These debug key values are defined in the SMP section of the core
137 * specification. debug_pk is the public debug key and debug_sk the
138 * private debug key.
139 */
140static const u8 debug_pk[64] = {
141 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
142 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
143 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
144 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
145
146 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
147 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
148 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
149 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
150};
151
152static const u8 debug_sk[32] = {
153 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
154 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
155 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
156 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
157};
158
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300159static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300160{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300161 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300162
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300163 for (i = 0; i < len; i++)
164 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300165}
166
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200167/* The following functions map to the LE SC SMP crypto functions
168 * AES-CMAC, f4, f5, f6, g2 and h6.
169 */
170
Herbert Xu71af2f62016-01-24 21:18:30 +0800171static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m,
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300172 size_t len, u8 mac[16])
173{
174 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
Herbert Xu71af2f62016-01-24 21:18:30 +0800175 SHASH_DESC_ON_STACK(desc, tfm);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300176 int err;
177
178 if (len > CMAC_MSG_MAX)
179 return -EFBIG;
180
181 if (!tfm) {
182 BT_ERR("tfm %p", tfm);
183 return -EINVAL;
184 }
185
Herbert Xu71af2f62016-01-24 21:18:30 +0800186 desc->tfm = tfm;
187 desc->flags = 0;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300188
189 /* Swap key and message from LSB to MSB */
190 swap_buf(k, tmp, 16);
191 swap_buf(m, msg_msb, len);
192
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200193 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
194 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300195
Herbert Xu71af2f62016-01-24 21:18:30 +0800196 err = crypto_shash_setkey(tfm, tmp, 16);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300197 if (err) {
198 BT_ERR("cipher setkey failed: %d", err);
199 return err;
200 }
201
Herbert Xu71af2f62016-01-24 21:18:30 +0800202 err = crypto_shash_digest(desc, msg_msb, len, mac_msb);
203 shash_desc_zero(desc);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300204 if (err) {
Herbert Xu71af2f62016-01-24 21:18:30 +0800205 BT_ERR("Hash computation error %d", err);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300206 return err;
207 }
208
209 swap_buf(mac_msb, mac, 16);
210
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200211 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300212
213 return 0;
214}
215
Herbert Xu71af2f62016-01-24 21:18:30 +0800216static int smp_f4(struct crypto_shash *tfm_cmac, const u8 u[32],
217 const u8 v[32], const u8 x[16], u8 z, u8 res[16])
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300218{
219 u8 m[65];
220 int err;
221
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200222 SMP_DBG("u %32phN", u);
223 SMP_DBG("v %32phN", v);
224 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300225
226 m[0] = z;
227 memcpy(m + 1, v, 32);
228 memcpy(m + 33, u, 32);
229
230 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
231 if (err)
232 return err;
233
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200234 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300235
236 return err;
237}
238
Herbert Xu71af2f62016-01-24 21:18:30 +0800239static int smp_f5(struct crypto_shash *tfm_cmac, const u8 w[32],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200240 const u8 n1[16], const u8 n2[16], const u8 a1[7],
241 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300242{
243 /* The btle, salt and length "magic" values are as defined in
244 * the SMP section of the Bluetooth core specification. In ASCII
245 * the btle value ends up being 'btle'. The salt is just a
246 * random number whereas length is the value 256 in little
247 * endian format.
248 */
249 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
250 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
251 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
252 const u8 length[2] = { 0x00, 0x01 };
253 u8 m[53], t[16];
254 int err;
255
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200256 SMP_DBG("w %32phN", w);
257 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
258 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300259
260 err = aes_cmac(tfm_cmac, salt, w, 32, t);
261 if (err)
262 return err;
263
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200264 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300265
266 memcpy(m, length, 2);
267 memcpy(m + 2, a2, 7);
268 memcpy(m + 9, a1, 7);
269 memcpy(m + 16, n2, 16);
270 memcpy(m + 32, n1, 16);
271 memcpy(m + 48, btle, 4);
272
273 m[52] = 0; /* Counter */
274
275 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
276 if (err)
277 return err;
278
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200279 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300280
281 m[52] = 1; /* Counter */
282
283 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
284 if (err)
285 return err;
286
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200287 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300288
289 return 0;
290}
291
Herbert Xu71af2f62016-01-24 21:18:30 +0800292static int smp_f6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200293 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300294 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
295 u8 res[16])
296{
297 u8 m[65];
298 int err;
299
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200300 SMP_DBG("w %16phN", w);
301 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
302 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300303
304 memcpy(m, a2, 7);
305 memcpy(m + 7, a1, 7);
306 memcpy(m + 14, io_cap, 3);
307 memcpy(m + 17, r, 16);
308 memcpy(m + 33, n2, 16);
309 memcpy(m + 49, n1, 16);
310
311 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
312 if (err)
313 return err;
314
Marcel Holtmann203de212014-12-31 20:01:22 -0800315 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300316
317 return err;
318}
319
Herbert Xu71af2f62016-01-24 21:18:30 +0800320static int smp_g2(struct crypto_shash *tfm_cmac, const u8 u[32], const u8 v[32],
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300321 const u8 x[16], const u8 y[16], u32 *val)
322{
323 u8 m[80], tmp[16];
324 int err;
325
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200326 SMP_DBG("u %32phN", u);
327 SMP_DBG("v %32phN", v);
328 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300329
330 memcpy(m, y, 16);
331 memcpy(m + 16, v, 32);
332 memcpy(m + 48, u, 32);
333
334 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
335 if (err)
336 return err;
337
338 *val = get_unaligned_le32(tmp);
339 *val %= 1000000;
340
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200341 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300342
343 return 0;
344}
345
Herbert Xu71af2f62016-01-24 21:18:30 +0800346static int smp_h6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200347 const u8 key_id[4], u8 res[16])
348{
349 int err;
350
351 SMP_DBG("w %16phN key_id %4phN", w, key_id);
352
353 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
354 if (err)
355 return err;
356
357 SMP_DBG("res %16phN", res);
358
359 return err;
360}
361
Johan Hedberga62da6f2016-12-08 08:32:54 +0200362static int smp_h7(struct crypto_shash *tfm_cmac, const u8 w[16],
363 const u8 salt[16], u8 res[16])
364{
365 int err;
366
367 SMP_DBG("w %16phN salt %16phN", w, salt);
368
369 err = aes_cmac(tfm_cmac, salt, w, 16, res);
370 if (err)
371 return err;
372
373 SMP_DBG("res %16phN", res);
374
375 return err;
376}
377
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200378/* The following functions map to the legacy SMP crypto functions e, c1,
379 * s1 and ah.
380 */
381
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700382static int smp_e(struct crypto_cipher *tfm, const u8 *k, u8 *r)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300383{
Johan Hedberg943a7322014-03-18 12:58:24 +0200384 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200385 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300386
Johan Hedberg011c3912015-05-19 21:06:04 +0300387 SMP_DBG("k %16phN r %16phN", k, r);
388
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200389 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300390 BT_ERR("tfm %p", tfm);
391 return -EINVAL;
392 }
393
Johan Hedberg943a7322014-03-18 12:58:24 +0200394 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300395 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200396
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700397 err = crypto_cipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300398 if (err) {
399 BT_ERR("cipher setkey failed: %d", err);
400 return err;
401 }
402
Johan Hedberg943a7322014-03-18 12:58:24 +0200403 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300404 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200405
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700406 crypto_cipher_encrypt_one(tfm, data, data);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300407
Johan Hedberg943a7322014-03-18 12:58:24 +0200408 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300409 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200410
Johan Hedberg011c3912015-05-19 21:06:04 +0300411 SMP_DBG("r %16phN", r);
412
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300413 return err;
414}
415
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700416static int smp_c1(struct crypto_cipher *tfm_aes, const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200417 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
418 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
419{
420 u8 p1[16], p2[16];
421 int err;
422
Johan Hedberg011c3912015-05-19 21:06:04 +0300423 SMP_DBG("k %16phN r %16phN", k, r);
424 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
425 SMP_DBG("preq %7phN pres %7phN", preq, pres);
426
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200427 memset(p1, 0, 16);
428
429 /* p1 = pres || preq || _rat || _iat */
430 p1[0] = _iat;
431 p1[1] = _rat;
432 memcpy(p1 + 2, preq, 7);
433 memcpy(p1 + 9, pres, 7);
434
Johan Hedberg011c3912015-05-19 21:06:04 +0300435 SMP_DBG("p1 %16phN", p1);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200436
437 /* res = r XOR p1 */
438 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
439
440 /* res = e(k, res) */
441 err = smp_e(tfm_aes, k, res);
442 if (err) {
443 BT_ERR("Encrypt data error");
444 return err;
445 }
446
Johan Hedberg011c3912015-05-19 21:06:04 +0300447 /* p2 = padding || ia || ra */
448 memcpy(p2, ra, 6);
449 memcpy(p2 + 6, ia, 6);
450 memset(p2 + 12, 0, 4);
451
452 SMP_DBG("p2 %16phN", p2);
453
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200454 /* res = res XOR p2 */
455 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
456
457 /* res = e(k, res) */
458 err = smp_e(tfm_aes, k, res);
459 if (err)
460 BT_ERR("Encrypt data error");
461
462 return err;
463}
464
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700465static int smp_s1(struct crypto_cipher *tfm_aes, const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200466 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300467{
468 int err;
469
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200470 /* Just least significant octets from r1 and r2 are considered */
471 memcpy(_r, r2, 8);
472 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300473
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200474 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300475 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200476 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300477
478 return err;
479}
480
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700481static int smp_ah(struct crypto_cipher *tfm, const u8 irk[16],
Johan Hedbergcd082792014-12-02 13:37:41 +0200482 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200483{
Johan Hedberg943a7322014-03-18 12:58:24 +0200484 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200485 int err;
486
487 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200488 memcpy(_res, r, 3);
489 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200490
Johan Hedberg943a7322014-03-18 12:58:24 +0200491 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200492 if (err) {
493 BT_ERR("Encrypt error");
494 return err;
495 }
496
497 /* The output of the random address function ah is:
Marcel Holtmannc5080d42015-09-04 17:08:18 +0200498 * ah(k, r) = e(k, r') mod 2^24
Johan Hedberg60478052014-02-18 10:19:31 +0200499 * The output of the security function e is then truncated to 24 bits
500 * by taking the least significant 24 bits of the output of e as the
501 * result of ah.
502 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200503 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200504
505 return 0;
506}
507
Johan Hedbergcd082792014-12-02 13:37:41 +0200508bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
509 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200510{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300511 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700512 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200513 u8 hash[3];
514 int err;
515
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300516 if (!chan || !chan->data)
517 return false;
518
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700519 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300520
Johan Hedberg60478052014-02-18 10:19:31 +0200521 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
522
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700523 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200524 if (err)
525 return false;
526
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200527 return !crypto_memneq(bdaddr->b, hash, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200528}
529
Johan Hedbergcd082792014-12-02 13:37:41 +0200530int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200531{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300532 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700533 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200534 int err;
535
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300536 if (!chan || !chan->data)
537 return -EOPNOTSUPP;
538
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700539 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300540
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200541 get_random_bytes(&rpa->b[3], 3);
542
543 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
544 rpa->b[5] |= 0x40; /* Set second most significant bit */
545
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700546 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200547 if (err < 0)
548 return err;
549
550 BT_DBG("RPA %pMR", rpa);
551
552 return 0;
553}
554
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700555int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
556{
557 struct l2cap_chan *chan = hdev->smp_data;
558 struct smp_dev *smp;
559 int err;
560
561 if (!chan || !chan->data)
562 return -EOPNOTSUPP;
563
564 smp = chan->data;
565
566 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
567 BT_DBG("Using debug keys");
568 memcpy(smp->local_pk, debug_pk, 64);
569 memcpy(smp->local_sk, debug_sk, 32);
570 smp->debug_key = true;
571 } else {
572 while (true) {
Marcel Holtmann71653eb2017-04-30 06:51:41 -0700573 /* Seed private key with random number */
574 get_random_bytes(smp->local_sk, 32);
575
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700576 /* Generate local key pair for Secure Connections */
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100577 if (!generate_ecdh_keys(smp->local_pk, smp->local_sk))
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700578 return -EIO;
579
580 /* This is unlikely, but we need to check that
581 * we didn't accidentially generate a debug key.
582 */
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200583 if (crypto_memneq(smp->local_sk, debug_sk, 32))
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700584 break;
585 }
586 smp->debug_key = false;
587 }
588
589 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
590 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
591 SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
592
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700593 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700594
595 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700596 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700597 if (err < 0)
598 return err;
599
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700600 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700601
602 return 0;
603}
604
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300605static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
606{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300607 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300608 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300609 struct kvec iv[2];
610 struct msghdr msg;
611
612 if (!chan)
613 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300614
615 BT_DBG("code 0x%2.2x", code);
616
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300617 iv[0].iov_base = &code;
618 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300619
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300620 iv[1].iov_base = data;
621 iv[1].iov_len = len;
622
623 memset(&msg, 0, sizeof(msg));
624
Al Viro17836392014-11-24 17:07:38 -0500625 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300626
627 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300628
Johan Hedbergb68fda62014-08-11 22:06:40 +0300629 if (!chan->data)
630 return;
631
632 smp = chan->data;
633
634 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300635 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300636}
637
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300638static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800639{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300640 if (authreq & SMP_AUTH_MITM) {
641 if (authreq & SMP_AUTH_SC)
642 return BT_SECURITY_FIPS;
643 else
644 return BT_SECURITY_HIGH;
645 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800646 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300647 }
Brian Gix2b64d152011-12-21 16:12:12 -0800648}
649
650static __u8 seclevel_to_authreq(__u8 sec_level)
651{
652 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300653 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800654 case BT_SECURITY_HIGH:
655 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
656 case BT_SECURITY_MEDIUM:
657 return SMP_AUTH_BONDING;
658 default:
659 return SMP_AUTH_NONE;
660 }
661}
662
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300663static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700664 struct smp_cmd_pairing *req,
665 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300666{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300667 struct l2cap_chan *chan = conn->smp;
668 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200669 struct hci_conn *hcon = conn->hcon;
670 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100671 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300672
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700673 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700674 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
675 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300676 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800677 } else {
678 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300679 }
680
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700681 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200682 remote_dist |= SMP_DIST_ID_KEY;
683
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700684 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200685 local_dist |= SMP_DIST_ID_KEY;
686
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700687 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100688 (authreq & SMP_AUTH_SC)) {
689 struct oob_data *oob_data;
690 u8 bdaddr_type;
691
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700692 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300693 local_dist |= SMP_DIST_LINK_KEY;
694 remote_dist |= SMP_DIST_LINK_KEY;
695 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100696
697 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
698 bdaddr_type = BDADDR_LE_PUBLIC;
699 else
700 bdaddr_type = BDADDR_LE_RANDOM;
701
702 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
703 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800704 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200705 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100706 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100707 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100708 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700709 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
710 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100711 }
712
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300713 } else {
714 authreq &= ~SMP_AUTH_SC;
715 }
716
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300717 if (rsp == NULL) {
718 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100719 req->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300720 req->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200721 req->init_key_dist = local_dist;
722 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300723 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200724
725 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300726 return;
727 }
728
729 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100730 rsp->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300731 rsp->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200732 rsp->init_key_dist = req->init_key_dist & remote_dist;
733 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300734 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200735
736 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300737}
738
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300739static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
740{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300741 struct l2cap_chan *chan = conn->smp;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300742 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300743 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300744
Johan Hedberg2fd36552015-06-11 13:52:26 +0300745 if (max_key_size > SMP_DEV(hdev)->max_key_size ||
746 max_key_size < SMP_MIN_ENC_KEY_SIZE)
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300747 return SMP_ENC_KEY_SIZE;
748
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300749 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300750
751 return 0;
752}
753
Johan Hedberg6f48e262014-08-11 22:06:44 +0300754static void smp_chan_destroy(struct l2cap_conn *conn)
755{
756 struct l2cap_chan *chan = conn->smp;
757 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200758 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300759 bool complete;
760
761 BUG_ON(!smp);
762
763 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300764
Johan Hedberg6f48e262014-08-11 22:06:44 +0300765 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200766 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300767
Marcel Holtmann276812e2015-03-16 01:10:18 -0700768 kzfree(smp->csrk);
769 kzfree(smp->slave_csrk);
770 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300771
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700772 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +0800773 crypto_free_shash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300774
Johan Hedberg923e2412014-12-03 12:43:39 +0200775 /* Ensure that we don't leave any debug key around if debug key
776 * support hasn't been explicitly enabled.
777 */
778 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700779 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200780 list_del_rcu(&smp->ltk->list);
781 kfree_rcu(smp->ltk, rcu);
782 smp->ltk = NULL;
783 }
784
Johan Hedberg6f48e262014-08-11 22:06:44 +0300785 /* If pairing failed clean up any keys we might have */
786 if (!complete) {
787 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200788 list_del_rcu(&smp->ltk->list);
789 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300790 }
791
792 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200793 list_del_rcu(&smp->slave_ltk->list);
794 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300795 }
796
797 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200798 list_del_rcu(&smp->remote_irk->list);
799 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300800 }
801 }
802
803 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700804 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200805 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300806}
807
Johan Hedberg84794e12013-11-06 11:24:57 +0200808static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800809{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200810 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300811 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200812
Johan Hedberg84794e12013-11-06 11:24:57 +0200813 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800814 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700815 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800816
Johan Hedberge1e930f2014-09-08 17:09:49 -0700817 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300818
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300819 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300820 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800821}
822
Brian Gix2b64d152011-12-21 16:12:12 -0800823#define JUST_WORKS 0x00
824#define JUST_CFM 0x01
825#define REQ_PASSKEY 0x02
826#define CFM_PASSKEY 0x03
827#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300828#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800829#define OVERLAP 0xFF
830
831static const u8 gen_method[5][5] = {
832 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
833 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
834 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
835 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
836 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
837};
838
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300839static const u8 sc_method[5][5] = {
840 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
841 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
842 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
843 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
844 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
845};
846
Johan Hedberg581370c2014-06-17 13:07:38 +0300847static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
848{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300849 /* If either side has unknown io_caps, use JUST_CFM (which gets
850 * converted later to JUST_WORKS if we're initiators.
851 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300852 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
853 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300854 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300855
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300856 if (test_bit(SMP_FLAG_SC, &smp->flags))
857 return sc_method[remote_io][local_io];
858
Johan Hedberg581370c2014-06-17 13:07:38 +0300859 return gen_method[remote_io][local_io];
860}
861
Brian Gix2b64d152011-12-21 16:12:12 -0800862static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
863 u8 local_io, u8 remote_io)
864{
865 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300866 struct l2cap_chan *chan = conn->smp;
867 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800868 u32 passkey = 0;
869 int ret = 0;
870
871 /* Initialize key for JUST WORKS */
872 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300873 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800874
875 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
876
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300877 /* If neither side wants MITM, either "just" confirm an incoming
878 * request or use just-works for outgoing ones. The JUST_CFM
879 * will be converted to JUST_WORKS if necessary later in this
880 * function. If either side has MITM look up the method from the
881 * table.
882 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300883 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300884 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800885 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300886 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800887
Johan Hedberga82505c2014-03-24 14:39:07 +0200888 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300889 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
890 &smp->flags))
891 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200892
Johan Hedberg02f3e252014-07-16 15:09:13 +0300893 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300894 if (smp->method == JUST_CFM &&
895 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
896 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300897
Brian Gix2b64d152011-12-21 16:12:12 -0800898 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300899 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300900 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800901 return 0;
902 }
903
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200904 /* If this function is used for SC -> legacy fallback we
905 * can only recover the just-works case.
906 */
907 if (test_bit(SMP_FLAG_SC, &smp->flags))
908 return -EINVAL;
909
Brian Gix2b64d152011-12-21 16:12:12 -0800910 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300911 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300912 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300913 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
914 hcon->pending_sec_level = BT_SECURITY_HIGH;
915 }
Brian Gix2b64d152011-12-21 16:12:12 -0800916
917 /* If both devices have Keyoard-Display I/O, the master
918 * Confirms and the slave Enters the passkey.
919 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300920 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300921 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300922 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800923 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300924 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800925 }
926
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200927 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300928 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200929 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800930 get_random_bytes(&passkey, sizeof(passkey));
931 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200932 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800933 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300934 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800935 }
936
Johan Hedberg783e0572014-05-31 18:48:26 +0300937 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700938 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200939 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300940 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200941 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
942 hcon->type, hcon->dst_type,
943 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800944 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200945 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200946 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200947 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800948
Brian Gix2b64d152011-12-21 16:12:12 -0800949 return ret;
950}
951
Johan Hedberg1cc61142014-05-20 09:45:52 +0300952static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300953{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300954 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300955 struct smp_cmd_pairing_confirm cp;
956 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300957
958 BT_DBG("conn %p", conn);
959
Johan Hedberge491eaf2014-10-25 21:15:37 +0200960 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200961 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200962 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
963 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300964 if (ret)
965 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300966
Johan Hedberg4a74d652014-05-20 09:45:50 +0300967 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800968
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300969 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
970
Johan Hedbergb28b4942014-09-05 22:19:55 +0300971 if (conn->hcon->out)
972 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
973 else
974 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
975
Johan Hedberg1cc61142014-05-20 09:45:52 +0300976 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300977}
978
Johan Hedberg861580a2014-05-20 09:45:51 +0300979static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300980{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300981 struct l2cap_conn *conn = smp->conn;
982 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300983 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300984 int ret;
985
Johan Hedbergec70f362014-06-27 14:23:04 +0300986 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300987 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300988
989 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
990
Johan Hedberge491eaf2014-10-25 21:15:37 +0200991 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200992 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200993 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300994 if (ret)
995 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300996
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200997 if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) {
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300998 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300999 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001000 }
1001
1002 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001003 u8 stk[16];
1004 __le64 rand = 0;
1005 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001006
Johan Hedberge491eaf2014-10-25 21:15:37 +02001007 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001008
Johan Hedberg861580a2014-05-20 09:45:51 +03001009 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1010 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001011
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001012 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001013 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001014 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001015 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001016 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001017 __le64 rand = 0;
1018 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001019
Johan Hedberg943a7322014-03-18 12:58:24 +02001020 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1021 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001022
Johan Hedberge491eaf2014-10-25 21:15:37 +02001023 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001024
Johan Hedbergfff34902014-06-10 15:19:50 +03001025 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1026 auth = 1;
1027 else
1028 auth = 0;
1029
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001030 /* Even though there's no _SLAVE suffix this is the
1031 * slave STK we're adding for later lookup (the master
1032 * STK never needs to be stored).
1033 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001034 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001035 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001036 }
1037
Johan Hedberg861580a2014-05-20 09:45:51 +03001038 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001039}
1040
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001041static void smp_notify_keys(struct l2cap_conn *conn)
1042{
1043 struct l2cap_chan *chan = conn->smp;
1044 struct smp_chan *smp = chan->data;
1045 struct hci_conn *hcon = conn->hcon;
1046 struct hci_dev *hdev = hcon->hdev;
1047 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1048 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1049 bool persistent;
1050
Johan Hedbergcad20c22015-10-12 13:36:19 +02001051 if (hcon->type == ACL_LINK) {
1052 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1053 persistent = false;
1054 else
1055 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1056 &hcon->flags);
1057 } else {
1058 /* The LTKs, IRKs and CSRKs should be persistent only if
1059 * both sides had the bonding bit set in their
1060 * authentication requests.
1061 */
1062 persistent = !!((req->auth_req & rsp->auth_req) &
1063 SMP_AUTH_BONDING);
1064 }
1065
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001066 if (smp->remote_irk) {
Johan Hedbergcad20c22015-10-12 13:36:19 +02001067 mgmt_new_irk(hdev, smp->remote_irk, persistent);
1068
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001069 /* Now that user space can be considered to know the
1070 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001071 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001072 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001073 if (hcon->type == LE_LINK) {
1074 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1075 hcon->dst_type = smp->remote_irk->addr_type;
1076 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1077 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001078 }
1079
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001080 if (smp->csrk) {
1081 smp->csrk->bdaddr_type = hcon->dst_type;
1082 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1083 mgmt_new_csrk(hdev, smp->csrk, persistent);
1084 }
1085
1086 if (smp->slave_csrk) {
1087 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1088 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1089 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1090 }
1091
1092 if (smp->ltk) {
1093 smp->ltk->bdaddr_type = hcon->dst_type;
1094 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1095 mgmt_new_ltk(hdev, smp->ltk, persistent);
1096 }
1097
1098 if (smp->slave_ltk) {
1099 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1100 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1101 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1102 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001103
1104 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001105 struct link_key *key;
1106 u8 type;
1107
1108 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1109 type = HCI_LK_DEBUG_COMBINATION;
1110 else if (hcon->sec_level == BT_SECURITY_FIPS)
1111 type = HCI_LK_AUTH_COMBINATION_P256;
1112 else
1113 type = HCI_LK_UNAUTH_COMBINATION_P256;
1114
1115 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1116 smp->link_key, type, 0, &persistent);
1117 if (key) {
1118 mgmt_new_link_key(hdev, key, persistent);
1119
1120 /* Don't keep debug keys around if the relevant
1121 * flag is not set.
1122 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001123 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001124 key->type == HCI_LK_DEBUG_COMBINATION) {
1125 list_del_rcu(&key->list);
1126 kfree_rcu(key, rcu);
1127 }
1128 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001129 }
1130}
1131
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001132static void sc_add_ltk(struct smp_chan *smp)
1133{
1134 struct hci_conn *hcon = smp->conn->hcon;
1135 u8 key_type, auth;
1136
1137 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1138 key_type = SMP_LTK_P256_DEBUG;
1139 else
1140 key_type = SMP_LTK_P256;
1141
1142 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1143 auth = 1;
1144 else
1145 auth = 0;
1146
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001147 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1148 key_type, auth, smp->tk, smp->enc_key_size,
1149 0, 0);
1150}
1151
Johan Hedberg6a770832014-06-06 11:54:04 +03001152static void sc_generate_link_key(struct smp_chan *smp)
1153{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001154 /* From core spec. Spells out in ASCII as 'lebr'. */
Johan Hedberg6a770832014-06-06 11:54:04 +03001155 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1156
1157 smp->link_key = kzalloc(16, GFP_KERNEL);
1158 if (!smp->link_key)
1159 return;
1160
Johan Hedberga62da6f2016-12-08 08:32:54 +02001161 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1162 /* SALT = 0x00000000000000000000000000000000746D7031 */
1163 const u8 salt[16] = { 0x31, 0x70, 0x6d, 0x74 };
1164
1165 if (smp_h7(smp->tfm_cmac, smp->tk, salt, smp->link_key)) {
1166 kzfree(smp->link_key);
1167 smp->link_key = NULL;
1168 return;
1169 }
1170 } else {
1171 /* From core spec. Spells out in ASCII as 'tmp1'. */
1172 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1173
1174 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1175 kzfree(smp->link_key);
1176 smp->link_key = NULL;
1177 return;
1178 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001179 }
1180
1181 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001182 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001183 smp->link_key = NULL;
1184 return;
1185 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001186}
1187
Johan Hedbergb28b4942014-09-05 22:19:55 +03001188static void smp_allow_key_dist(struct smp_chan *smp)
1189{
1190 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1191 * will be allowed in each PDU handler to ensure we receive
1192 * them in the correct order.
1193 */
1194 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1195 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1196 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1197 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1198 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1199 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1200}
1201
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001202static void sc_generate_ltk(struct smp_chan *smp)
1203{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001204 /* From core spec. Spells out in ASCII as 'brle'. */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001205 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1206 struct hci_conn *hcon = smp->conn->hcon;
1207 struct hci_dev *hdev = hcon->hdev;
1208 struct link_key *key;
1209
1210 key = hci_find_link_key(hdev, &hcon->dst);
1211 if (!key) {
1212 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1213 return;
1214 }
1215
1216 if (key->type == HCI_LK_DEBUG_COMBINATION)
1217 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1218
Johan Hedberga62da6f2016-12-08 08:32:54 +02001219 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1220 /* SALT = 0x00000000000000000000000000000000746D7032 */
1221 const u8 salt[16] = { 0x32, 0x70, 0x6d, 0x74 };
1222
1223 if (smp_h7(smp->tfm_cmac, key->val, salt, smp->tk))
1224 return;
1225 } else {
1226 /* From core spec. Spells out in ASCII as 'tmp2'. */
1227 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1228
1229 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1230 return;
1231 }
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001232
1233 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1234 return;
1235
1236 sc_add_ltk(smp);
1237}
1238
Johan Hedbergd6268e82014-09-05 22:19:51 +03001239static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001240{
1241 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001242 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001243 struct hci_conn *hcon = conn->hcon;
1244 struct hci_dev *hdev = hcon->hdev;
1245 __u8 *keydist;
1246
1247 BT_DBG("conn %p", conn);
1248
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001249 rsp = (void *) &smp->prsp[1];
1250
1251 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001252 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1253 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001254 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001255 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001256
1257 req = (void *) &smp->preq[1];
1258
1259 if (hcon->out) {
1260 keydist = &rsp->init_key_dist;
1261 *keydist &= req->init_key_dist;
1262 } else {
1263 keydist = &rsp->resp_key_dist;
1264 *keydist &= req->resp_key_dist;
1265 }
1266
Johan Hedberg6a770832014-06-06 11:54:04 +03001267 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001268 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001269 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001270 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1271 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001272
1273 /* Clear the keys which are generated but not distributed */
1274 *keydist &= ~SMP_SC_NO_DIST;
1275 }
1276
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001277 BT_DBG("keydist 0x%x", *keydist);
1278
1279 if (*keydist & SMP_DIST_ENC_KEY) {
1280 struct smp_cmd_encrypt_info enc;
1281 struct smp_cmd_master_ident ident;
1282 struct smp_ltk *ltk;
1283 u8 authenticated;
1284 __le16 ediv;
1285 __le64 rand;
1286
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001287 /* Make sure we generate only the significant amount of
1288 * bytes based on the encryption key size, and set the rest
1289 * of the value to zeroes.
1290 */
1291 get_random_bytes(enc.ltk, smp->enc_key_size);
1292 memset(enc.ltk + smp->enc_key_size, 0,
1293 sizeof(enc.ltk) - smp->enc_key_size);
1294
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001295 get_random_bytes(&ediv, sizeof(ediv));
1296 get_random_bytes(&rand, sizeof(rand));
1297
1298 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1299
1300 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1301 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1302 SMP_LTK_SLAVE, authenticated, enc.ltk,
1303 smp->enc_key_size, ediv, rand);
1304 smp->slave_ltk = ltk;
1305
1306 ident.ediv = ediv;
1307 ident.rand = rand;
1308
1309 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1310
1311 *keydist &= ~SMP_DIST_ENC_KEY;
1312 }
1313
1314 if (*keydist & SMP_DIST_ID_KEY) {
1315 struct smp_cmd_ident_addr_info addrinfo;
1316 struct smp_cmd_ident_info idinfo;
1317
1318 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1319
1320 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1321
1322 /* The hci_conn contains the local identity address
1323 * after the connection has been established.
1324 *
1325 * This is true even when the connection has been
1326 * established using a resolvable random address.
1327 */
1328 bacpy(&addrinfo.bdaddr, &hcon->src);
1329 addrinfo.addr_type = hcon->src_type;
1330
1331 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1332 &addrinfo);
1333
1334 *keydist &= ~SMP_DIST_ID_KEY;
1335 }
1336
1337 if (*keydist & SMP_DIST_SIGN) {
1338 struct smp_cmd_sign_info sign;
1339 struct smp_csrk *csrk;
1340
1341 /* Generate a new random key */
1342 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1343
1344 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1345 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001346 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1347 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1348 else
1349 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001350 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1351 }
1352 smp->slave_csrk = csrk;
1353
1354 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1355
1356 *keydist &= ~SMP_DIST_SIGN;
1357 }
1358
1359 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001360 if (smp->remote_key_dist & KEY_DIST_MASK) {
1361 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001362 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001363 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001364
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001365 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1366 smp_notify_keys(conn);
1367
1368 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001369}
1370
Johan Hedbergb68fda62014-08-11 22:06:40 +03001371static void smp_timeout(struct work_struct *work)
1372{
1373 struct smp_chan *smp = container_of(work, struct smp_chan,
1374 security_timer.work);
1375 struct l2cap_conn *conn = smp->conn;
1376
1377 BT_DBG("conn %p", conn);
1378
Johan Hedberg1e91c292014-08-18 20:33:29 +03001379 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001380}
1381
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001382static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1383{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001384 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001385 struct smp_chan *smp;
1386
Marcel Holtmannf1560462013-10-13 05:43:25 -07001387 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001388 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001389 return NULL;
1390
Andy Lutomirskia4770e12016-06-26 14:55:23 -07001391 smp->tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001392 if (IS_ERR(smp->tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07001393 BT_ERR("Unable to create AES crypto context");
Marcel Holtmann276812e2015-03-16 01:10:18 -07001394 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001395 return NULL;
1396 }
1397
Herbert Xu71af2f62016-01-24 21:18:30 +08001398 smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001399 if (IS_ERR(smp->tfm_cmac)) {
1400 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07001401 crypto_free_cipher(smp->tfm_aes);
Marcel Holtmann276812e2015-03-16 01:10:18 -07001402 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001403 return NULL;
1404 }
1405
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001406 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001407 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001408
Johan Hedbergb28b4942014-09-05 22:19:55 +03001409 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1410
Johan Hedbergb68fda62014-08-11 22:06:40 +03001411 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1412
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001413 hci_conn_hold(conn->hcon);
1414
1415 return smp;
1416}
1417
Johan Hedberg760b0182014-06-06 11:44:05 +03001418static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1419{
1420 struct hci_conn *hcon = smp->conn->hcon;
1421 u8 *na, *nb, a[7], b[7];
1422
1423 if (hcon->out) {
1424 na = smp->prnd;
1425 nb = smp->rrnd;
1426 } else {
1427 na = smp->rrnd;
1428 nb = smp->prnd;
1429 }
1430
1431 memcpy(a, &hcon->init_addr, 6);
1432 memcpy(b, &hcon->resp_addr, 6);
1433 a[6] = hcon->init_addr_type;
1434 b[6] = hcon->resp_addr_type;
1435
1436 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1437}
1438
Johan Hedberg38606f12014-06-25 11:10:28 +03001439static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001440{
1441 struct hci_conn *hcon = smp->conn->hcon;
1442 struct smp_cmd_dhkey_check check;
1443 u8 a[7], b[7], *local_addr, *remote_addr;
1444 u8 io_cap[3], r[16];
1445
Johan Hedberg760b0182014-06-06 11:44:05 +03001446 memcpy(a, &hcon->init_addr, 6);
1447 memcpy(b, &hcon->resp_addr, 6);
1448 a[6] = hcon->init_addr_type;
1449 b[6] = hcon->resp_addr_type;
1450
1451 if (hcon->out) {
1452 local_addr = a;
1453 remote_addr = b;
1454 memcpy(io_cap, &smp->preq[1], 3);
1455 } else {
1456 local_addr = b;
1457 remote_addr = a;
1458 memcpy(io_cap, &smp->prsp[1], 3);
1459 }
1460
Johan Hedbergdddd3052014-06-01 15:38:09 +03001461 memset(r, 0, sizeof(r));
1462
1463 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001464 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001465
Johan Hedberga29b0732014-10-28 15:17:05 +01001466 if (smp->method == REQ_OOB)
1467 memcpy(r, smp->rr, 16);
1468
Johan Hedberg760b0182014-06-06 11:44:05 +03001469 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1470 local_addr, remote_addr, check.e);
1471
1472 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001473}
1474
Johan Hedberg38606f12014-06-25 11:10:28 +03001475static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1476{
1477 struct l2cap_conn *conn = smp->conn;
1478 struct hci_conn *hcon = conn->hcon;
1479 struct smp_cmd_pairing_confirm cfm;
1480 u8 r;
1481
1482 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1483 r |= 0x80;
1484
1485 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1486
1487 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1488 cfm.confirm_val))
1489 return SMP_UNSPECIFIED;
1490
1491 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1492
1493 return 0;
1494}
1495
1496static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1497{
1498 struct l2cap_conn *conn = smp->conn;
1499 struct hci_conn *hcon = conn->hcon;
1500 struct hci_dev *hdev = hcon->hdev;
1501 u8 cfm[16], r;
1502
1503 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1504 if (smp->passkey_round >= 20)
1505 return 0;
1506
1507 switch (smp_op) {
1508 case SMP_CMD_PAIRING_RANDOM:
1509 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1510 r |= 0x80;
1511
1512 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1513 smp->rrnd, r, cfm))
1514 return SMP_UNSPECIFIED;
1515
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02001516 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg38606f12014-06-25 11:10:28 +03001517 return SMP_CONFIRM_FAILED;
1518
1519 smp->passkey_round++;
1520
1521 if (smp->passkey_round == 20) {
1522 /* Generate MacKey and LTK */
1523 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1524 return SMP_UNSPECIFIED;
1525 }
1526
1527 /* The round is only complete when the initiator
1528 * receives pairing random.
1529 */
1530 if (!hcon->out) {
1531 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1532 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001533 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001534 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001535 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001536 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001537 return 0;
1538 }
1539
1540 /* Start the next round */
1541 if (smp->passkey_round != 20)
1542 return sc_passkey_round(smp, 0);
1543
1544 /* Passkey rounds are complete - start DHKey Check */
1545 sc_dhkey_check(smp);
1546 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1547
1548 break;
1549
1550 case SMP_CMD_PAIRING_CONFIRM:
1551 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1552 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1553 return 0;
1554 }
1555
1556 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1557
1558 if (hcon->out) {
1559 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1560 sizeof(smp->prnd), smp->prnd);
1561 return 0;
1562 }
1563
1564 return sc_passkey_send_confirm(smp);
1565
1566 case SMP_CMD_PUBLIC_KEY:
1567 default:
1568 /* Initiating device starts the round */
1569 if (!hcon->out)
1570 return 0;
1571
1572 BT_DBG("%s Starting passkey round %u", hdev->name,
1573 smp->passkey_round + 1);
1574
1575 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1576
1577 return sc_passkey_send_confirm(smp);
1578 }
1579
1580 return 0;
1581}
1582
Johan Hedbergdddd3052014-06-01 15:38:09 +03001583static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1584{
Johan Hedberg38606f12014-06-25 11:10:28 +03001585 struct l2cap_conn *conn = smp->conn;
1586 struct hci_conn *hcon = conn->hcon;
1587 u8 smp_op;
1588
1589 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1590
Johan Hedbergdddd3052014-06-01 15:38:09 +03001591 switch (mgmt_op) {
1592 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1593 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1594 return 0;
1595 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1596 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1597 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001598 case MGMT_OP_USER_PASSKEY_REPLY:
1599 hcon->passkey_notify = le32_to_cpu(passkey);
1600 smp->passkey_round = 0;
1601
1602 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1603 smp_op = SMP_CMD_PAIRING_CONFIRM;
1604 else
1605 smp_op = 0;
1606
1607 if (sc_passkey_round(smp, smp_op))
1608 return -EIO;
1609
1610 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001611 }
1612
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001613 /* Initiator sends DHKey check first */
1614 if (hcon->out) {
1615 sc_dhkey_check(smp);
1616 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1617 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1618 sc_dhkey_check(smp);
1619 sc_add_ltk(smp);
1620 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001621
1622 return 0;
1623}
1624
Brian Gix2b64d152011-12-21 16:12:12 -08001625int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1626{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001627 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001628 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001629 struct smp_chan *smp;
1630 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001631 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001632
1633 BT_DBG("");
1634
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001635 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001636 return -ENOTCONN;
1637
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001638 chan = conn->smp;
1639 if (!chan)
1640 return -ENOTCONN;
1641
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001642 l2cap_chan_lock(chan);
1643 if (!chan->data) {
1644 err = -ENOTCONN;
1645 goto unlock;
1646 }
1647
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001648 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001649
Johan Hedberg760b0182014-06-06 11:44:05 +03001650 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1651 err = sc_user_reply(smp, mgmt_op, passkey);
1652 goto unlock;
1653 }
1654
Brian Gix2b64d152011-12-21 16:12:12 -08001655 switch (mgmt_op) {
1656 case MGMT_OP_USER_PASSKEY_REPLY:
1657 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001658 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001659 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001660 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001661 /* Fall Through */
1662 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001663 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001664 break;
1665 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1666 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001667 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001668 err = 0;
1669 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001670 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001671 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001672 err = -EOPNOTSUPP;
1673 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001674 }
1675
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001676 err = 0;
1677
Brian Gix2b64d152011-12-21 16:12:12 -08001678 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001679 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1680 u8 rsp = smp_confirm(smp);
1681 if (rsp)
1682 smp_failure(conn, rsp);
1683 }
Brian Gix2b64d152011-12-21 16:12:12 -08001684
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001685unlock:
1686 l2cap_chan_unlock(chan);
1687 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001688}
1689
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001690static void build_bredr_pairing_cmd(struct smp_chan *smp,
1691 struct smp_cmd_pairing *req,
1692 struct smp_cmd_pairing *rsp)
1693{
1694 struct l2cap_conn *conn = smp->conn;
1695 struct hci_dev *hdev = conn->hcon->hdev;
1696 u8 local_dist = 0, remote_dist = 0;
1697
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001698 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001699 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1700 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1701 }
1702
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001703 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001704 remote_dist |= SMP_DIST_ID_KEY;
1705
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001706 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001707 local_dist |= SMP_DIST_ID_KEY;
1708
1709 if (!rsp) {
1710 memset(req, 0, sizeof(*req));
1711
Johan Hedberga62da6f2016-12-08 08:32:54 +02001712 req->auth_req = SMP_AUTH_CT2;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001713 req->init_key_dist = local_dist;
1714 req->resp_key_dist = remote_dist;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001715 req->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001716
1717 smp->remote_key_dist = remote_dist;
1718
1719 return;
1720 }
1721
1722 memset(rsp, 0, sizeof(*rsp));
1723
Johan Hedberga62da6f2016-12-08 08:32:54 +02001724 rsp->auth_req = SMP_AUTH_CT2;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001725 rsp->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001726 rsp->init_key_dist = req->init_key_dist & remote_dist;
1727 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1728
1729 smp->remote_key_dist = rsp->init_key_dist;
1730}
1731
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001732static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001733{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001734 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001735 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001736 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001737 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001738 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001739 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001740
1741 BT_DBG("conn %p", conn);
1742
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001743 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001744 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001745
Johan Hedberg40bef302014-07-16 11:42:27 +03001746 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001747 return SMP_CMD_NOTSUPP;
1748
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001749 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001750 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001751 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001752 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001753
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001754 if (!smp)
1755 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001756
Johan Hedbergc05b9332014-09-10 17:37:42 -07001757 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001758 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001759
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001760 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001761 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001762 return SMP_PAIRING_NOTSUPP;
1763
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001764 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001765 return SMP_AUTH_REQUIREMENTS;
1766
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001767 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1768 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001769 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001770
Johan Hedbergcb06d362015-03-16 21:12:34 +02001771 /* If the remote side's OOB flag is set it means it has
1772 * successfully received our local OOB data - therefore set the
1773 * flag to indicate that local OOB is in use.
1774 */
Johan Hedberg58428562015-03-16 11:45:45 +02001775 if (req->oob_flag == SMP_OOB_PRESENT)
1776 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1777
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001778 /* SMP over BR/EDR requires special treatment */
1779 if (conn->hcon->type == ACL_LINK) {
1780 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001781 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001782 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001783 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1784
1785 set_bit(SMP_FLAG_SC, &smp->flags);
1786
1787 build_bredr_pairing_cmd(smp, req, &rsp);
1788
Johan Hedberga62da6f2016-12-08 08:32:54 +02001789 if (req->auth_req & SMP_AUTH_CT2)
1790 set_bit(SMP_FLAG_CT2, &smp->flags);
1791
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001792 key_size = min(req->max_key_size, rsp.max_key_size);
1793 if (check_enc_key_size(conn, key_size))
1794 return SMP_ENC_KEY_SIZE;
1795
1796 /* Clear bits which are generated but not distributed */
1797 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1798
1799 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1800 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1801 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1802
1803 smp_distribute_keys(smp);
1804 return 0;
1805 }
1806
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001807 build_pairing_cmd(conn, req, &rsp, auth);
1808
Johan Hedberga62da6f2016-12-08 08:32:54 +02001809 if (rsp.auth_req & SMP_AUTH_SC) {
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001810 set_bit(SMP_FLAG_SC, &smp->flags);
1811
Johan Hedberga62da6f2016-12-08 08:32:54 +02001812 if (rsp.auth_req & SMP_AUTH_CT2)
1813 set_bit(SMP_FLAG_CT2, &smp->flags);
1814 }
1815
Johan Hedberg5be5e272014-09-10 17:58:54 -07001816 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001817 sec_level = BT_SECURITY_MEDIUM;
1818 else
1819 sec_level = authreq_to_seclevel(auth);
1820
Johan Hedbergc7262e72014-06-17 13:07:37 +03001821 if (sec_level > conn->hcon->pending_sec_level)
1822 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001823
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001824 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001825 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1826 u8 method;
1827
1828 method = get_auth_method(smp, conn->hcon->io_capability,
1829 req->io_capability);
1830 if (method == JUST_WORKS || method == JUST_CFM)
1831 return SMP_AUTH_REQUIREMENTS;
1832 }
1833
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001834 key_size = min(req->max_key_size, rsp.max_key_size);
1835 if (check_enc_key_size(conn, key_size))
1836 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001837
Johan Hedberge84a6b12013-12-02 10:49:03 +02001838 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001839
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001840 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1841 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001842
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001843 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001844
1845 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1846
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001847 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1848 * SC case, however some implementations incorrectly copy RFU auth
1849 * req bits from our security request, which may create a false
1850 * positive SC enablement.
1851 */
1852 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1853
Johan Hedberg3b191462014-06-06 10:50:15 +03001854 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1855 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1856 /* Clear bits which are generated but not distributed */
1857 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1858 /* Wait for Public Key from Initiating Device */
1859 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001860 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001861
Brian Gix2b64d152011-12-21 16:12:12 -08001862 /* Request setup of TK */
1863 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1864 if (ret)
1865 return SMP_UNSPECIFIED;
1866
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001867 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001868}
1869
Johan Hedberg3b191462014-06-06 10:50:15 +03001870static u8 sc_send_public_key(struct smp_chan *smp)
1871{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001872 struct hci_dev *hdev = smp->conn->hcon->hdev;
1873
Johan Hedberg3b191462014-06-06 10:50:15 +03001874 BT_DBG("");
1875
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001876 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001877 struct l2cap_chan *chan = hdev->smp_data;
1878 struct smp_dev *smp_dev;
1879
1880 if (!chan || !chan->data)
1881 return SMP_UNSPECIFIED;
1882
1883 smp_dev = chan->data;
1884
1885 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1886 memcpy(smp->local_sk, smp_dev->local_sk, 32);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001887 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001888
1889 if (smp_dev->debug_key)
1890 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1891
1892 goto done;
1893 }
1894
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001895 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001896 BT_DBG("Using debug keys");
1897 memcpy(smp->local_pk, debug_pk, 64);
1898 memcpy(smp->local_sk, debug_sk, 32);
1899 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1900 } else {
1901 while (true) {
Marcel Holtmann71653eb2017-04-30 06:51:41 -07001902 /* Seed private key with random number */
1903 get_random_bytes(smp->local_sk, 32);
1904
Johan Hedberg70157ef2014-06-24 15:22:59 +03001905 /* Generate local key pair for Secure Connections */
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +01001906 if (!generate_ecdh_keys(smp->local_pk, smp->local_sk))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001907 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001908
Johan Hedberg70157ef2014-06-24 15:22:59 +03001909 /* This is unlikely, but we need to check that
1910 * we didn't accidentially generate a debug key.
1911 */
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02001912 if (crypto_memneq(smp->local_sk, debug_sk, 32))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001913 break;
1914 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001915 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001916
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001917done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001918 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001919 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001920 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001921
1922 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1923
1924 return 0;
1925}
1926
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001927static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001928{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001929 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001930 struct l2cap_chan *chan = conn->smp;
1931 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001932 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001933 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001934 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001935
1936 BT_DBG("conn %p", conn);
1937
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001938 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001939 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001940
Johan Hedberg40bef302014-07-16 11:42:27 +03001941 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001942 return SMP_CMD_NOTSUPP;
1943
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001944 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001945
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001946 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001947
1948 key_size = min(req->max_key_size, rsp->max_key_size);
1949 if (check_enc_key_size(conn, key_size))
1950 return SMP_ENC_KEY_SIZE;
1951
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001952 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001953
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001954 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001955 return SMP_AUTH_REQUIREMENTS;
1956
Johan Hedbergcb06d362015-03-16 21:12:34 +02001957 /* If the remote side's OOB flag is set it means it has
1958 * successfully received our local OOB data - therefore set the
1959 * flag to indicate that local OOB is in use.
1960 */
Johan Hedberg58428562015-03-16 11:45:45 +02001961 if (rsp->oob_flag == SMP_OOB_PRESENT)
1962 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1963
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001964 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1965 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1966
1967 /* Update remote key distribution in case the remote cleared
1968 * some bits that we had enabled in our request.
1969 */
1970 smp->remote_key_dist &= rsp->resp_key_dist;
1971
Johan Hedberga62da6f2016-12-08 08:32:54 +02001972 if ((req->auth_req & SMP_AUTH_CT2) && (auth & SMP_AUTH_CT2))
1973 set_bit(SMP_FLAG_CT2, &smp->flags);
1974
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001975 /* For BR/EDR this means we're done and can start phase 3 */
1976 if (conn->hcon->type == ACL_LINK) {
1977 /* Clear bits which are generated but not distributed */
1978 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1979 smp_distribute_keys(smp);
1980 return 0;
1981 }
1982
Johan Hedberg65668772014-05-16 11:03:34 +03001983 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1984 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001985 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1986 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001987
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001988 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001989 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1990 u8 method;
1991
1992 method = get_auth_method(smp, req->io_capability,
1993 rsp->io_capability);
1994 if (method == JUST_WORKS || method == JUST_CFM)
1995 return SMP_AUTH_REQUIREMENTS;
1996 }
1997
Johan Hedberge84a6b12013-12-02 10:49:03 +02001998 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001999
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02002000 /* Update remote key distribution in case the remote cleared
2001 * some bits that we had enabled in our request.
2002 */
2003 smp->remote_key_dist &= rsp->resp_key_dist;
2004
Johan Hedberg3b191462014-06-06 10:50:15 +03002005 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2006 /* Clear bits which are generated but not distributed */
2007 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
2008 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
2009 return sc_send_public_key(smp);
2010 }
2011
Johan Hedbergc05b9332014-09-10 17:37:42 -07002012 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08002013
Johan Hedberg476585e2012-06-06 18:54:15 +08002014 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08002015 if (ret)
2016 return SMP_UNSPECIFIED;
2017
Johan Hedberg4a74d652014-05-20 09:45:50 +03002018 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08002019
2020 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03002021 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002022 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002023
2024 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002025}
2026
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002027static u8 sc_check_confirm(struct smp_chan *smp)
2028{
2029 struct l2cap_conn *conn = smp->conn;
2030
2031 BT_DBG("");
2032
Johan Hedberg38606f12014-06-25 11:10:28 +03002033 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2034 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2035
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002036 if (conn->hcon->out) {
2037 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2038 smp->prnd);
2039 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2040 }
2041
2042 return 0;
2043}
2044
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002045/* Work-around for some implementations that incorrectly copy RFU bits
2046 * from our security request and thereby create the impression that
2047 * we're doing SC when in fact the remote doesn't support it.
2048 */
2049static int fixup_sc_false_positive(struct smp_chan *smp)
2050{
2051 struct l2cap_conn *conn = smp->conn;
2052 struct hci_conn *hcon = conn->hcon;
2053 struct hci_dev *hdev = hcon->hdev;
2054 struct smp_cmd_pairing *req, *rsp;
2055 u8 auth;
2056
2057 /* The issue is only observed when we're in slave role */
2058 if (hcon->out)
2059 return SMP_UNSPECIFIED;
2060
2061 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
2062 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2063 return SMP_UNSPECIFIED;
2064 }
2065
2066 BT_ERR("Trying to fall back to legacy SMP");
2067
2068 req = (void *) &smp->preq[1];
2069 rsp = (void *) &smp->prsp[1];
2070
2071 /* Rebuild key dist flags which may have been cleared for SC */
2072 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2073
2074 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2075
2076 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
2077 BT_ERR("Failed to fall back to legacy SMP");
2078 return SMP_UNSPECIFIED;
2079 }
2080
2081 clear_bit(SMP_FLAG_SC, &smp->flags);
2082
2083 return 0;
2084}
2085
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002086static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002087{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002088 struct l2cap_chan *chan = conn->smp;
2089 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002090
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002091 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2092
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002093 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002094 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002095
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002096 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2097 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002098
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002099 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2100 int ret;
2101
2102 /* Public Key exchange must happen before any other steps */
2103 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2104 return sc_check_confirm(smp);
2105
2106 BT_ERR("Unexpected SMP Pairing Confirm");
2107
2108 ret = fixup_sc_false_positive(smp);
2109 if (ret)
2110 return ret;
2111 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002112
Johan Hedbergb28b4942014-09-05 22:19:55 +03002113 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002114 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2115 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002116 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2117 return 0;
2118 }
2119
2120 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002121 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002122
2123 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002124
2125 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002126}
2127
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002128static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002129{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002130 struct l2cap_chan *chan = conn->smp;
2131 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002132 struct hci_conn *hcon = conn->hcon;
2133 u8 *pkax, *pkbx, *na, *nb;
2134 u32 passkey;
2135 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002136
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002137 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002138
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002139 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002140 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002141
Johan Hedberg943a7322014-03-18 12:58:24 +02002142 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002143 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002144
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002145 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2146 return smp_random(smp);
2147
Johan Hedberg580039e2014-12-03 16:26:37 +02002148 if (hcon->out) {
2149 pkax = smp->local_pk;
2150 pkbx = smp->remote_pk;
2151 na = smp->prnd;
2152 nb = smp->rrnd;
2153 } else {
2154 pkax = smp->remote_pk;
2155 pkbx = smp->local_pk;
2156 na = smp->rrnd;
2157 nb = smp->prnd;
2158 }
2159
Johan Hedberga29b0732014-10-28 15:17:05 +01002160 if (smp->method == REQ_OOB) {
2161 if (!hcon->out)
2162 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2163 sizeof(smp->prnd), smp->prnd);
2164 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2165 goto mackey_and_ltk;
2166 }
2167
Johan Hedberg38606f12014-06-25 11:10:28 +03002168 /* Passkey entry has special treatment */
2169 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2170 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2171
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002172 if (hcon->out) {
2173 u8 cfm[16];
2174
2175 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2176 smp->rrnd, 0, cfm);
2177 if (err)
2178 return SMP_UNSPECIFIED;
2179
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002180 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002181 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002182 } else {
2183 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2184 smp->prnd);
2185 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002186 }
2187
Johan Hedberga29b0732014-10-28 15:17:05 +01002188mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002189 /* Generate MacKey and LTK */
2190 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2191 if (err)
2192 return SMP_UNSPECIFIED;
2193
Johan Hedberga29b0732014-10-28 15:17:05 +01002194 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002195 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002196 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002197 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2198 }
2199 return 0;
2200 }
2201
Johan Hedberg38606f12014-06-25 11:10:28 +03002202 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002203 if (err)
2204 return SMP_UNSPECIFIED;
2205
Johan Hedberg38606f12014-06-25 11:10:28 +03002206 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2207 hcon->dst_type, passkey, 0);
2208 if (err)
2209 return SMP_UNSPECIFIED;
2210
2211 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2212
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002213 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002214}
2215
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002216static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002217{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002218 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002219 struct hci_conn *hcon = conn->hcon;
2220
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002221 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002222 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002223 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002224
Johan Hedberga6f78332014-09-10 17:37:45 -07002225 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002226 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002227
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002228 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002229 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002230
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002231 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002232 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002233
Johan Hedbergfe59a052014-07-01 19:14:12 +03002234 /* We never store STKs for master role, so clear this flag */
2235 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2236
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002237 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002238}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002239
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002240bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2241 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002242{
2243 if (sec_level == BT_SECURITY_LOW)
2244 return true;
2245
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002246 /* If we're encrypted with an STK but the caller prefers using
2247 * LTK claim insufficient security. This way we allow the
2248 * connection to be re-encrypted with an LTK, even if the LTK
2249 * provides the same level of security. Only exception is if we
2250 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002251 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002252 if (key_pref == SMP_USE_LTK &&
2253 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002254 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002255 return false;
2256
Johan Hedberg854f4722014-07-01 18:40:20 +03002257 if (hcon->sec_level >= sec_level)
2258 return true;
2259
2260 return false;
2261}
2262
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002263static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002264{
2265 struct smp_cmd_security_req *rp = (void *) skb->data;
2266 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002267 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002268 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002269 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002270 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002271
2272 BT_DBG("conn %p", conn);
2273
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002274 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002275 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002276
Johan Hedberg40bef302014-07-16 11:42:27 +03002277 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002278 return SMP_CMD_NOTSUPP;
2279
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002280 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002281
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002282 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002283 return SMP_AUTH_REQUIREMENTS;
2284
Johan Hedberg5be5e272014-09-10 17:58:54 -07002285 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002286 sec_level = BT_SECURITY_MEDIUM;
2287 else
2288 sec_level = authreq_to_seclevel(auth);
2289
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002290 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002291 return 0;
2292
Johan Hedbergc7262e72014-06-17 13:07:37 +03002293 if (sec_level > hcon->pending_sec_level)
2294 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002295
Johan Hedberg4dab7862012-06-07 14:58:37 +08002296 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002297 return 0;
2298
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002299 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002300 if (!smp)
2301 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002302
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002303 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002304 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55be42014-07-29 14:18:48 +03002305 return SMP_PAIRING_NOTSUPP;
2306
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002307 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002308
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002309 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002310 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002311
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002312 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2313 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002314
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002315 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002316 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002317
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002318 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002319}
2320
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002321int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002322{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002323 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002324 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002325 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002326 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002327 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002328
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002329 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2330
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002331 /* This may be NULL if there's an unexpected disconnection */
2332 if (!conn)
2333 return 1;
2334
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002335 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002336 return 1;
2337
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002338 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002339 return 1;
2340
Johan Hedbergc7262e72014-06-17 13:07:37 +03002341 if (sec_level > hcon->pending_sec_level)
2342 hcon->pending_sec_level = sec_level;
2343
Johan Hedberg40bef302014-07-16 11:42:27 +03002344 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002345 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2346 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002347
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002348 chan = conn->smp;
2349 if (!chan) {
2350 BT_ERR("SMP security requested but not available");
2351 return 1;
2352 }
2353
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002354 l2cap_chan_lock(chan);
2355
2356 /* If SMP is already in progress ignore this request */
2357 if (chan->data) {
2358 ret = 0;
2359 goto unlock;
2360 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002361
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002362 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002363 if (!smp) {
2364 ret = 1;
2365 goto unlock;
2366 }
Brian Gix2b64d152011-12-21 16:12:12 -08002367
2368 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002369
Johan Hedberga62da6f2016-12-08 08:32:54 +02002370 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED)) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002371 authreq |= SMP_AUTH_SC;
Johan Hedberga62da6f2016-12-08 08:32:54 +02002372 if (hci_dev_test_flag(hcon->hdev, HCI_SSP_ENABLED))
2373 authreq |= SMP_AUTH_CT2;
2374 }
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002375
Johan Hedberg79897d22014-06-01 09:45:24 +03002376 /* Require MITM if IO Capability allows or the security level
2377 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002378 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002379 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002380 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002381 authreq |= SMP_AUTH_MITM;
2382
Johan Hedberg40bef302014-07-16 11:42:27 +03002383 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002384 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002385
Brian Gix2b64d152011-12-21 16:12:12 -08002386 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002387 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2388 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002389
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002390 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002391 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002392 } else {
2393 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002394 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002395 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002396 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002397 }
2398
Johan Hedberg4a74d652014-05-20 09:45:50 +03002399 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002400 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002401
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002402unlock:
2403 l2cap_chan_unlock(chan);
2404 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002405}
2406
Johan Hedbergc81d5552015-10-22 09:38:35 +03002407void smp_cancel_pairing(struct hci_conn *hcon)
2408{
2409 struct l2cap_conn *conn = hcon->l2cap_data;
2410 struct l2cap_chan *chan;
2411 struct smp_chan *smp;
2412
2413 if (!conn)
2414 return;
2415
2416 chan = conn->smp;
2417 if (!chan)
2418 return;
2419
2420 l2cap_chan_lock(chan);
2421
2422 smp = chan->data;
2423 if (smp) {
2424 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags))
2425 smp_failure(conn, 0);
2426 else
2427 smp_failure(conn, SMP_UNSPECIFIED);
2428 }
2429
2430 l2cap_chan_unlock(chan);
2431}
2432
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002433static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2434{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002435 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002436 struct l2cap_chan *chan = conn->smp;
2437 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002438
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002439 BT_DBG("conn %p", conn);
2440
2441 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002442 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002443
Johan Hedbergb28b4942014-09-05 22:19:55 +03002444 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002445
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002446 skb_pull(skb, sizeof(*rp));
2447
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002448 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002449
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002450 return 0;
2451}
2452
2453static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2454{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002455 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002456 struct l2cap_chan *chan = conn->smp;
2457 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002458 struct hci_dev *hdev = conn->hcon->hdev;
2459 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002460 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002461 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002462
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002463 BT_DBG("conn %p", conn);
2464
2465 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002466 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002467
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002468 /* Mark the information as received */
2469 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2470
Johan Hedbergb28b4942014-09-05 22:19:55 +03002471 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2472 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002473 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2474 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002475
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002476 skb_pull(skb, sizeof(*rp));
2477
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002478 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002479 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002480 authenticated, smp->tk, smp->enc_key_size,
2481 rp->ediv, rp->rand);
2482 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002483 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002484 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002485
2486 return 0;
2487}
2488
Johan Hedbergfd349c02014-02-18 10:19:36 +02002489static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2490{
2491 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002492 struct l2cap_chan *chan = conn->smp;
2493 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002494
2495 BT_DBG("");
2496
2497 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002498 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002499
Johan Hedbergb28b4942014-09-05 22:19:55 +03002500 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002501
Johan Hedbergfd349c02014-02-18 10:19:36 +02002502 skb_pull(skb, sizeof(*info));
2503
2504 memcpy(smp->irk, info->irk, 16);
2505
2506 return 0;
2507}
2508
2509static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2510 struct sk_buff *skb)
2511{
2512 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002513 struct l2cap_chan *chan = conn->smp;
2514 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002515 struct hci_conn *hcon = conn->hcon;
2516 bdaddr_t rpa;
2517
2518 BT_DBG("");
2519
2520 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002521 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002522
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002523 /* Mark the information as received */
2524 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2525
Johan Hedbergb28b4942014-09-05 22:19:55 +03002526 if (smp->remote_key_dist & SMP_DIST_SIGN)
2527 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2528
Johan Hedbergfd349c02014-02-18 10:19:36 +02002529 skb_pull(skb, sizeof(*info));
2530
Johan Hedberga9a58f82014-02-25 22:24:37 +02002531 /* Strictly speaking the Core Specification (4.1) allows sending
2532 * an empty address which would force us to rely on just the IRK
2533 * as "identity information". However, since such
2534 * implementations are not known of and in order to not over
2535 * complicate our implementation, simply pretend that we never
2536 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002537 *
2538 * The Identity Address must also be a Static Random or Public
2539 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002540 */
Johan Hedberge12af482015-01-14 20:51:37 +02002541 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2542 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002543 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002544 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002545 }
2546
Johan Hedbergfd349c02014-02-18 10:19:36 +02002547 bacpy(&smp->id_addr, &info->bdaddr);
2548 smp->id_addr_type = info->addr_type;
2549
2550 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2551 bacpy(&rpa, &hcon->dst);
2552 else
2553 bacpy(&rpa, BDADDR_ANY);
2554
Johan Hedberg23d0e122014-02-19 14:57:46 +02002555 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2556 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002557
Johan Hedberg31dd6242014-06-27 14:23:02 +03002558distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002559 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2560 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002561
2562 return 0;
2563}
2564
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002565static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2566{
2567 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002568 struct l2cap_chan *chan = conn->smp;
2569 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002570 struct smp_csrk *csrk;
2571
2572 BT_DBG("conn %p", conn);
2573
2574 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002575 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002576
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002577 /* Mark the information as received */
2578 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2579
2580 skb_pull(skb, sizeof(*rp));
2581
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002582 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2583 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002584 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2585 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2586 else
2587 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002588 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2589 }
2590 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002591 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002592
2593 return 0;
2594}
2595
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002596static u8 sc_select_method(struct smp_chan *smp)
2597{
2598 struct l2cap_conn *conn = smp->conn;
2599 struct hci_conn *hcon = conn->hcon;
2600 struct smp_cmd_pairing *local, *remote;
2601 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2602
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002603 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2604 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002605 return REQ_OOB;
2606
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002607 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2608 * which are needed as inputs to some crypto functions. To get
2609 * the "struct smp_cmd_pairing" from them we need to skip the
2610 * first byte which contains the opcode.
2611 */
2612 if (hcon->out) {
2613 local = (void *) &smp->preq[1];
2614 remote = (void *) &smp->prsp[1];
2615 } else {
2616 local = (void *) &smp->prsp[1];
2617 remote = (void *) &smp->preq[1];
2618 }
2619
2620 local_io = local->io_capability;
2621 remote_io = remote->io_capability;
2622
2623 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2624 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2625
2626 /* If either side wants MITM, look up the method from the table,
2627 * otherwise use JUST WORKS.
2628 */
2629 if (local_mitm || remote_mitm)
2630 method = get_auth_method(smp, local_io, remote_io);
2631 else
2632 method = JUST_WORKS;
2633
2634 /* Don't confirm locally initiated pairing attempts */
2635 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2636 method = JUST_WORKS;
2637
2638 return method;
2639}
2640
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002641static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2642{
2643 struct smp_cmd_public_key *key = (void *) skb->data;
2644 struct hci_conn *hcon = conn->hcon;
2645 struct l2cap_chan *chan = conn->smp;
2646 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002647 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002648 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002649 int err;
2650
2651 BT_DBG("conn %p", conn);
2652
2653 if (skb->len < sizeof(*key))
2654 return SMP_INVALID_PARAMS;
2655
2656 memcpy(smp->remote_pk, key, 64);
2657
Johan Hedberga8ca6172015-03-16 18:12:57 +02002658 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2659 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2660 smp->rr, 0, cfm.confirm_val);
2661 if (err)
2662 return SMP_UNSPECIFIED;
2663
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002664 if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16))
Johan Hedberga8ca6172015-03-16 18:12:57 +02002665 return SMP_CONFIRM_FAILED;
2666 }
2667
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002668 /* Non-initiating device sends its public key after receiving
2669 * the key from the initiating device.
2670 */
2671 if (!hcon->out) {
2672 err = sc_send_public_key(smp);
2673 if (err)
2674 return err;
2675 }
2676
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002677 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002678 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002679
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +01002680 if (!compute_ecdh_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002681 return SMP_UNSPECIFIED;
2682
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002683 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002684
2685 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2686
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002687 smp->method = sc_select_method(smp);
2688
2689 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2690
2691 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2692 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2693 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2694 else
2695 hcon->pending_sec_level = BT_SECURITY_FIPS;
2696
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002697 if (!crypto_memneq(debug_pk, smp->remote_pk, 64))
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002698 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2699
Johan Hedberg38606f12014-06-25 11:10:28 +03002700 if (smp->method == DSP_PASSKEY) {
2701 get_random_bytes(&hcon->passkey_notify,
2702 sizeof(hcon->passkey_notify));
2703 hcon->passkey_notify %= 1000000;
2704 hcon->passkey_entered = 0;
2705 smp->passkey_round = 0;
2706 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2707 hcon->dst_type,
2708 hcon->passkey_notify,
2709 hcon->passkey_entered))
2710 return SMP_UNSPECIFIED;
2711 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2712 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2713 }
2714
Johan Hedberg94ea7252015-03-16 11:45:46 +02002715 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002716 if (hcon->out)
2717 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2718 sizeof(smp->prnd), smp->prnd);
2719
2720 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2721
2722 return 0;
2723 }
2724
Johan Hedberg38606f12014-06-25 11:10:28 +03002725 if (hcon->out)
2726 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2727
2728 if (smp->method == REQ_PASSKEY) {
2729 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2730 hcon->dst_type))
2731 return SMP_UNSPECIFIED;
2732 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2733 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2734 return 0;
2735 }
2736
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002737 /* The Initiating device waits for the non-initiating device to
2738 * send the confirm value.
2739 */
2740 if (conn->hcon->out)
2741 return 0;
2742
2743 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2744 0, cfm.confirm_val);
2745 if (err)
2746 return SMP_UNSPECIFIED;
2747
2748 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2749 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2750
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002751 return 0;
2752}
2753
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002754static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2755{
2756 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2757 struct l2cap_chan *chan = conn->smp;
2758 struct hci_conn *hcon = conn->hcon;
2759 struct smp_chan *smp = chan->data;
2760 u8 a[7], b[7], *local_addr, *remote_addr;
2761 u8 io_cap[3], r[16], e[16];
2762 int err;
2763
2764 BT_DBG("conn %p", conn);
2765
2766 if (skb->len < sizeof(*check))
2767 return SMP_INVALID_PARAMS;
2768
2769 memcpy(a, &hcon->init_addr, 6);
2770 memcpy(b, &hcon->resp_addr, 6);
2771 a[6] = hcon->init_addr_type;
2772 b[6] = hcon->resp_addr_type;
2773
2774 if (hcon->out) {
2775 local_addr = a;
2776 remote_addr = b;
2777 memcpy(io_cap, &smp->prsp[1], 3);
2778 } else {
2779 local_addr = b;
2780 remote_addr = a;
2781 memcpy(io_cap, &smp->preq[1], 3);
2782 }
2783
2784 memset(r, 0, sizeof(r));
2785
Johan Hedberg38606f12014-06-25 11:10:28 +03002786 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2787 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002788 else if (smp->method == REQ_OOB)
2789 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002790
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002791 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2792 io_cap, remote_addr, local_addr, e);
2793 if (err)
2794 return SMP_UNSPECIFIED;
2795
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002796 if (crypto_memneq(check->e, e, 16))
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002797 return SMP_DHKEY_CHECK_FAILED;
2798
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002799 if (!hcon->out) {
2800 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2801 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2802 return 0;
2803 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002804
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002805 /* Slave sends DHKey check as response to master */
2806 sc_dhkey_check(smp);
2807 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002808
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002809 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002810
2811 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002812 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002813 hcon->enc_key_size = smp->enc_key_size;
2814 }
2815
2816 return 0;
2817}
2818
Johan Hedberg1408bb62014-06-04 22:45:57 +03002819static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2820 struct sk_buff *skb)
2821{
2822 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2823
2824 BT_DBG("value 0x%02x", kp->value);
2825
2826 return 0;
2827}
2828
Johan Hedberg4befb862014-08-11 22:06:38 +03002829static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002830{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002831 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002832 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002833 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002834 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002835 int err = 0;
2836
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002837 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002838 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002839
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002840 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002841 reason = SMP_PAIRING_NOTSUPP;
2842 goto done;
2843 }
2844
Marcel Holtmann92381f52013-10-03 01:23:08 -07002845 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002846 skb_pull(skb, sizeof(code));
2847
Johan Hedbergb28b4942014-09-05 22:19:55 +03002848 smp = chan->data;
2849
2850 if (code > SMP_CMD_MAX)
2851 goto drop;
2852
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002853 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002854 goto drop;
2855
2856 /* If we don't have a context the only allowed commands are
2857 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002858 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002859 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2860 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002861
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002862 switch (code) {
2863 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002864 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002865 break;
2866
2867 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002868 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002869 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002870 break;
2871
2872 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002873 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002874 break;
2875
2876 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002877 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002878 break;
2879
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002880 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002881 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002882 break;
2883
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002884 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002885 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002886 break;
2887
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002888 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002889 reason = smp_cmd_encrypt_info(conn, skb);
2890 break;
2891
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002892 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002893 reason = smp_cmd_master_ident(conn, skb);
2894 break;
2895
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002896 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002897 reason = smp_cmd_ident_info(conn, skb);
2898 break;
2899
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002900 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002901 reason = smp_cmd_ident_addr_info(conn, skb);
2902 break;
2903
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002904 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002905 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002906 break;
2907
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002908 case SMP_CMD_PUBLIC_KEY:
2909 reason = smp_cmd_public_key(conn, skb);
2910 break;
2911
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002912 case SMP_CMD_DHKEY_CHECK:
2913 reason = smp_cmd_dhkey_check(conn, skb);
2914 break;
2915
Johan Hedberg1408bb62014-06-04 22:45:57 +03002916 case SMP_CMD_KEYPRESS_NOTIFY:
2917 reason = smp_cmd_keypress_notify(conn, skb);
2918 break;
2919
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002920 default:
2921 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002922 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002923 goto done;
2924 }
2925
2926done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002927 if (!err) {
2928 if (reason)
2929 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002930 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002931 }
2932
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002933 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002934
2935drop:
2936 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2937 code, &hcon->dst);
2938 kfree_skb(skb);
2939 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002940}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002941
Johan Hedberg70db83c2014-08-08 09:37:16 +03002942static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2943{
2944 struct l2cap_conn *conn = chan->conn;
2945
2946 BT_DBG("chan %p", chan);
2947
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002948 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002949 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002950
Johan Hedberg70db83c2014-08-08 09:37:16 +03002951 conn->smp = NULL;
2952 l2cap_chan_put(chan);
2953}
2954
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002955static void bredr_pairing(struct l2cap_chan *chan)
2956{
2957 struct l2cap_conn *conn = chan->conn;
2958 struct hci_conn *hcon = conn->hcon;
2959 struct hci_dev *hdev = hcon->hdev;
2960 struct smp_cmd_pairing req;
2961 struct smp_chan *smp;
2962
2963 BT_DBG("chan %p", chan);
2964
2965 /* Only new pairings are interesting */
2966 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2967 return;
2968
2969 /* Don't bother if we're not encrypted */
2970 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2971 return;
2972
2973 /* Only master may initiate SMP over BR/EDR */
2974 if (hcon->role != HCI_ROLE_MASTER)
2975 return;
2976
2977 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002978 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002979 return;
2980
2981 /* BR/EDR must use Secure Connections for SMP */
2982 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002983 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002984 return;
2985
2986 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002987 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002988 return;
2989
2990 /* Don't bother if remote LE support is not enabled */
2991 if (!lmp_host_le_capable(hcon))
2992 return;
2993
2994 /* Remote must support SMP fixed chan for BR/EDR */
2995 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2996 return;
2997
2998 /* Don't bother if SMP is already ongoing */
2999 if (chan->data)
3000 return;
3001
3002 smp = smp_chan_create(conn);
3003 if (!smp) {
3004 BT_ERR("%s unable to create SMP context for BR/EDR",
3005 hdev->name);
3006 return;
3007 }
3008
3009 set_bit(SMP_FLAG_SC, &smp->flags);
3010
3011 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
3012
3013 /* Prepare and send the BR/EDR SMP Pairing Request */
3014 build_bredr_pairing_cmd(smp, &req, NULL);
3015
3016 smp->preq[0] = SMP_CMD_PAIRING_REQ;
3017 memcpy(&smp->preq[1], &req, sizeof(req));
3018
3019 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
3020 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
3021}
3022
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003023static void smp_resume_cb(struct l2cap_chan *chan)
3024{
Johan Hedbergb68fda62014-08-11 22:06:40 +03003025 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003026 struct l2cap_conn *conn = chan->conn;
3027 struct hci_conn *hcon = conn->hcon;
3028
3029 BT_DBG("chan %p", chan);
3030
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003031 if (hcon->type == ACL_LINK) {
3032 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003033 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003034 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003035
Johan Hedberg86d14072014-08-11 22:06:43 +03003036 if (!smp)
3037 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03003038
Johan Hedberg84bc0db2014-09-05 22:19:49 +03003039 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3040 return;
3041
Johan Hedberg86d14072014-08-11 22:06:43 +03003042 cancel_delayed_work(&smp->security_timer);
3043
Johan Hedbergd6268e82014-09-05 22:19:51 +03003044 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003045}
3046
Johan Hedberg70db83c2014-08-08 09:37:16 +03003047static void smp_ready_cb(struct l2cap_chan *chan)
3048{
3049 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003050 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003051
3052 BT_DBG("chan %p", chan);
3053
Johan Hedberg78837462015-11-11 21:47:12 +02003054 /* No need to call l2cap_chan_hold() here since we already own
3055 * the reference taken in smp_new_conn_cb(). This is just the
3056 * first time that we tie it to a specific pointer. The code in
3057 * l2cap_core.c ensures that there's no risk this function wont
3058 * get called if smp_new_conn_cb was previously called.
3059 */
Johan Hedberg70db83c2014-08-08 09:37:16 +03003060 conn->smp = chan;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003061
3062 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3063 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003064}
3065
Johan Hedberg4befb862014-08-11 22:06:38 +03003066static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3067{
3068 int err;
3069
3070 BT_DBG("chan %p", chan);
3071
3072 err = smp_sig_channel(chan, skb);
3073 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003074 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003075
Johan Hedbergb68fda62014-08-11 22:06:40 +03003076 if (smp)
3077 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003078
Johan Hedberg1e91c292014-08-18 20:33:29 +03003079 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003080 }
3081
3082 return err;
3083}
3084
Johan Hedberg70db83c2014-08-08 09:37:16 +03003085static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3086 unsigned long hdr_len,
3087 unsigned long len, int nb)
3088{
3089 struct sk_buff *skb;
3090
3091 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3092 if (!skb)
3093 return ERR_PTR(-ENOMEM);
3094
3095 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003096 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003097
3098 return skb;
3099}
3100
3101static const struct l2cap_ops smp_chan_ops = {
3102 .name = "Security Manager",
3103 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003104 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003105 .alloc_skb = smp_alloc_skb_cb,
3106 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003107 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003108
3109 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003110 .state_change = l2cap_chan_no_state_change,
3111 .close = l2cap_chan_no_close,
3112 .defer = l2cap_chan_no_defer,
3113 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003114 .set_shutdown = l2cap_chan_no_set_shutdown,
3115 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003116};
3117
3118static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3119{
3120 struct l2cap_chan *chan;
3121
3122 BT_DBG("pchan %p", pchan);
3123
3124 chan = l2cap_chan_create();
3125 if (!chan)
3126 return NULL;
3127
3128 chan->chan_type = pchan->chan_type;
3129 chan->ops = &smp_chan_ops;
3130 chan->scid = pchan->scid;
3131 chan->dcid = chan->scid;
3132 chan->imtu = pchan->imtu;
3133 chan->omtu = pchan->omtu;
3134 chan->mode = pchan->mode;
3135
Johan Hedbergabe84902014-11-12 22:22:21 +02003136 /* Other L2CAP channels may request SMP routines in order to
3137 * change the security level. This means that the SMP channel
3138 * lock must be considered in its own category to avoid lockdep
3139 * warnings.
3140 */
3141 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3142
Johan Hedberg70db83c2014-08-08 09:37:16 +03003143 BT_DBG("created chan %p", chan);
3144
3145 return chan;
3146}
3147
3148static const struct l2cap_ops smp_root_chan_ops = {
3149 .name = "Security Manager Root",
3150 .new_connection = smp_new_conn_cb,
3151
3152 /* None of these are implemented for the root channel */
3153 .close = l2cap_chan_no_close,
3154 .alloc_skb = l2cap_chan_no_alloc_skb,
3155 .recv = l2cap_chan_no_recv,
3156 .state_change = l2cap_chan_no_state_change,
3157 .teardown = l2cap_chan_no_teardown,
3158 .ready = l2cap_chan_no_ready,
3159 .defer = l2cap_chan_no_defer,
3160 .suspend = l2cap_chan_no_suspend,
3161 .resume = l2cap_chan_no_resume,
3162 .set_shutdown = l2cap_chan_no_set_shutdown,
3163 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003164};
3165
Johan Hedbergef8efe42014-08-13 15:12:32 +03003166static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003167{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003168 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003169 struct smp_dev *smp;
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003170 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +08003171 struct crypto_shash *tfm_cmac;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003172
Johan Hedbergef8efe42014-08-13 15:12:32 +03003173 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003174 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003175 goto create_chan;
3176 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003177
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003178 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3179 if (!smp)
3180 return ERR_PTR(-ENOMEM);
3181
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003182 tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003183 if (IS_ERR(tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003184 BT_ERR("Unable to create AES crypto context");
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003185 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003186 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003187 }
3188
Herbert Xu71af2f62016-01-24 21:18:30 +08003189 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003190 if (IS_ERR(tfm_cmac)) {
3191 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003192 crypto_free_cipher(tfm_aes);
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003193 kzfree(smp);
3194 return ERR_CAST(tfm_cmac);
3195 }
3196
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003197 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003198 smp->tfm_cmac = tfm_cmac;
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003199 smp->min_key_size = SMP_MIN_ENC_KEY_SIZE;
Johan Hedberg2fd36552015-06-11 13:52:26 +03003200 smp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003201
Johan Hedbergef8efe42014-08-13 15:12:32 +03003202create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003203 chan = l2cap_chan_create();
3204 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003205 if (smp) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003206 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +08003207 crypto_free_shash(smp->tfm_cmac);
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003208 kzfree(smp);
3209 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003210 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003211 }
3212
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003213 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003214
Johan Hedbergef8efe42014-08-13 15:12:32 +03003215 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003216
3217 l2cap_chan_set_defaults(chan);
3218
Marcel Holtmann157029b2015-01-14 15:43:09 -08003219 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003220 u8 bdaddr_type;
3221
3222 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3223
3224 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003225 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003226 else
3227 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003228 } else {
3229 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003230 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003231 }
3232
Johan Hedberg70db83c2014-08-08 09:37:16 +03003233 chan->state = BT_LISTEN;
3234 chan->mode = L2CAP_MODE_BASIC;
3235 chan->imtu = L2CAP_DEFAULT_MTU;
3236 chan->ops = &smp_root_chan_ops;
3237
Johan Hedbergabe84902014-11-12 22:22:21 +02003238 /* Set correct nesting level for a parent/listening channel */
3239 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3240
Johan Hedbergef8efe42014-08-13 15:12:32 +03003241 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003242}
3243
Johan Hedbergef8efe42014-08-13 15:12:32 +03003244static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003245{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003246 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003247
Johan Hedbergef8efe42014-08-13 15:12:32 +03003248 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003249
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003250 smp = chan->data;
3251 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003252 chan->data = NULL;
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003253 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +08003254 crypto_free_shash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003255 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003256 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003257
Johan Hedberg70db83c2014-08-08 09:37:16 +03003258 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003259}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003260
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003261static ssize_t force_bredr_smp_read(struct file *file,
3262 char __user *user_buf,
3263 size_t count, loff_t *ppos)
3264{
3265 struct hci_dev *hdev = file->private_data;
3266 char buf[3];
3267
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003268 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003269 buf[1] = '\n';
3270 buf[2] = '\0';
3271 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3272}
3273
3274static ssize_t force_bredr_smp_write(struct file *file,
3275 const char __user *user_buf,
3276 size_t count, loff_t *ppos)
3277{
3278 struct hci_dev *hdev = file->private_data;
3279 char buf[32];
3280 size_t buf_size = min(count, (sizeof(buf)-1));
3281 bool enable;
3282
3283 if (copy_from_user(buf, user_buf, buf_size))
3284 return -EFAULT;
3285
3286 buf[buf_size] = '\0';
3287 if (strtobool(buf, &enable))
3288 return -EINVAL;
3289
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003290 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003291 return -EALREADY;
3292
3293 if (enable) {
3294 struct l2cap_chan *chan;
3295
3296 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3297 if (IS_ERR(chan))
3298 return PTR_ERR(chan);
3299
3300 hdev->smp_bredr_data = chan;
3301 } else {
3302 struct l2cap_chan *chan;
3303
3304 chan = hdev->smp_bredr_data;
3305 hdev->smp_bredr_data = NULL;
3306 smp_del_chan(chan);
3307 }
3308
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003309 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003310
3311 return count;
3312}
3313
3314static const struct file_operations force_bredr_smp_fops = {
3315 .open = simple_open,
3316 .read = force_bredr_smp_read,
3317 .write = force_bredr_smp_write,
3318 .llseek = default_llseek,
3319};
3320
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003321static ssize_t le_min_key_size_read(struct file *file,
3322 char __user *user_buf,
3323 size_t count, loff_t *ppos)
3324{
3325 struct hci_dev *hdev = file->private_data;
3326 char buf[4];
3327
3328 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->min_key_size);
3329
3330 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3331}
3332
3333static ssize_t le_min_key_size_write(struct file *file,
3334 const char __user *user_buf,
3335 size_t count, loff_t *ppos)
3336{
3337 struct hci_dev *hdev = file->private_data;
3338 char buf[32];
3339 size_t buf_size = min(count, (sizeof(buf) - 1));
3340 u8 key_size;
3341
3342 if (copy_from_user(buf, user_buf, buf_size))
3343 return -EFAULT;
3344
3345 buf[buf_size] = '\0';
3346
3347 sscanf(buf, "%hhu", &key_size);
3348
3349 if (key_size > SMP_DEV(hdev)->max_key_size ||
3350 key_size < SMP_MIN_ENC_KEY_SIZE)
3351 return -EINVAL;
3352
3353 SMP_DEV(hdev)->min_key_size = key_size;
3354
3355 return count;
3356}
3357
3358static const struct file_operations le_min_key_size_fops = {
3359 .open = simple_open,
3360 .read = le_min_key_size_read,
3361 .write = le_min_key_size_write,
3362 .llseek = default_llseek,
3363};
3364
Johan Hedberg2fd36552015-06-11 13:52:26 +03003365static ssize_t le_max_key_size_read(struct file *file,
3366 char __user *user_buf,
3367 size_t count, loff_t *ppos)
3368{
3369 struct hci_dev *hdev = file->private_data;
3370 char buf[4];
3371
3372 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->max_key_size);
3373
3374 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3375}
3376
3377static ssize_t le_max_key_size_write(struct file *file,
3378 const char __user *user_buf,
3379 size_t count, loff_t *ppos)
3380{
3381 struct hci_dev *hdev = file->private_data;
3382 char buf[32];
3383 size_t buf_size = min(count, (sizeof(buf) - 1));
3384 u8 key_size;
3385
3386 if (copy_from_user(buf, user_buf, buf_size))
3387 return -EFAULT;
3388
3389 buf[buf_size] = '\0';
3390
3391 sscanf(buf, "%hhu", &key_size);
3392
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003393 if (key_size > SMP_MAX_ENC_KEY_SIZE ||
3394 key_size < SMP_DEV(hdev)->min_key_size)
Johan Hedberg2fd36552015-06-11 13:52:26 +03003395 return -EINVAL;
3396
3397 SMP_DEV(hdev)->max_key_size = key_size;
3398
3399 return count;
3400}
3401
3402static const struct file_operations le_max_key_size_fops = {
3403 .open = simple_open,
3404 .read = le_max_key_size_read,
3405 .write = le_max_key_size_write,
3406 .llseek = default_llseek,
3407};
3408
Johan Hedbergef8efe42014-08-13 15:12:32 +03003409int smp_register(struct hci_dev *hdev)
3410{
3411 struct l2cap_chan *chan;
3412
3413 BT_DBG("%s", hdev->name);
3414
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003415 /* If the controller does not support Low Energy operation, then
3416 * there is also no need to register any SMP channel.
3417 */
3418 if (!lmp_le_capable(hdev))
3419 return 0;
3420
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003421 if (WARN_ON(hdev->smp_data)) {
3422 chan = hdev->smp_data;
3423 hdev->smp_data = NULL;
3424 smp_del_chan(chan);
3425 }
3426
Johan Hedbergef8efe42014-08-13 15:12:32 +03003427 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3428 if (IS_ERR(chan))
3429 return PTR_ERR(chan);
3430
3431 hdev->smp_data = chan;
3432
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003433 debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
3434 &le_min_key_size_fops);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003435 debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3436 &le_max_key_size_fops);
3437
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003438 /* If the controller does not support BR/EDR Secure Connections
3439 * feature, then the BR/EDR SMP channel shall not be present.
3440 *
3441 * To test this with Bluetooth 4.0 controllers, create a debugfs
3442 * switch that allows forcing BR/EDR SMP support and accepting
3443 * cross-transport pairing on non-AES encrypted connections.
3444 */
3445 if (!lmp_sc_capable(hdev)) {
3446 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3447 hdev, &force_bredr_smp_fops);
Szymon Janc83ebb9e2016-09-09 20:24:40 +02003448
3449 /* Flag can be already set here (due to power toggle) */
3450 if (!hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
3451 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003452 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003453
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003454 if (WARN_ON(hdev->smp_bredr_data)) {
3455 chan = hdev->smp_bredr_data;
3456 hdev->smp_bredr_data = NULL;
3457 smp_del_chan(chan);
3458 }
3459
Johan Hedbergef8efe42014-08-13 15:12:32 +03003460 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3461 if (IS_ERR(chan)) {
3462 int err = PTR_ERR(chan);
3463 chan = hdev->smp_data;
3464 hdev->smp_data = NULL;
3465 smp_del_chan(chan);
3466 return err;
3467 }
3468
3469 hdev->smp_bredr_data = chan;
3470
3471 return 0;
3472}
3473
3474void smp_unregister(struct hci_dev *hdev)
3475{
3476 struct l2cap_chan *chan;
3477
3478 if (hdev->smp_bredr_data) {
3479 chan = hdev->smp_bredr_data;
3480 hdev->smp_bredr_data = NULL;
3481 smp_del_chan(chan);
3482 }
3483
3484 if (hdev->smp_data) {
3485 chan = hdev->smp_data;
3486 hdev->smp_data = NULL;
3487 smp_del_chan(chan);
3488 }
3489}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003490
3491#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3492
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003493static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
3494{
3495 int i;
3496
3497 for (i = 0; i < ndigits; i++)
3498 out[i] = __swab64(in[ndigits - 1 - i]);
3499}
3500
3501static int __init test_debug_key(void)
3502{
3503 u8 pk[64], sk[32];
3504
3505 swap_digits((u64 *)debug_sk, (u64 *)sk, 4);
3506
3507 if (!generate_ecdh_keys(pk, sk))
3508 return -EINVAL;
3509
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003510 if (crypto_memneq(sk, debug_sk, 32))
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003511 return -EINVAL;
3512
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003513 if (crypto_memneq(pk, debug_pk, 64))
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003514 return -EINVAL;
3515
3516 return 0;
3517}
3518
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003519static int __init test_ah(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003520{
3521 const u8 irk[16] = {
3522 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3523 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3524 const u8 r[3] = { 0x94, 0x81, 0x70 };
3525 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3526 u8 res[3];
3527 int err;
3528
3529 err = smp_ah(tfm_aes, irk, r, res);
3530 if (err)
3531 return err;
3532
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003533 if (crypto_memneq(res, exp, 3))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003534 return -EINVAL;
3535
3536 return 0;
3537}
3538
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003539static int __init test_c1(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003540{
3541 const u8 k[16] = {
3542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3543 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3544 const u8 r[16] = {
3545 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3546 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3547 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3548 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3549 const u8 _iat = 0x01;
3550 const u8 _rat = 0x00;
3551 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3552 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3553 const u8 exp[16] = {
3554 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3555 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3556 u8 res[16];
3557 int err;
3558
3559 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3560 if (err)
3561 return err;
3562
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003563 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003564 return -EINVAL;
3565
3566 return 0;
3567}
3568
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003569static int __init test_s1(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003570{
3571 const u8 k[16] = {
3572 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3573 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3574 const u8 r1[16] = {
3575 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3576 const u8 r2[16] = {
3577 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3578 const u8 exp[16] = {
3579 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3580 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3581 u8 res[16];
3582 int err;
3583
3584 err = smp_s1(tfm_aes, k, r1, r2, res);
3585 if (err)
3586 return err;
3587
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003588 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003589 return -EINVAL;
3590
3591 return 0;
3592}
3593
Herbert Xu71af2f62016-01-24 21:18:30 +08003594static int __init test_f4(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003595{
3596 const u8 u[32] = {
3597 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3598 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3599 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3600 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3601 const u8 v[32] = {
3602 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3603 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3604 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3605 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3606 const u8 x[16] = {
3607 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3608 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3609 const u8 z = 0x00;
3610 const u8 exp[16] = {
3611 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3612 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3613 u8 res[16];
3614 int err;
3615
3616 err = smp_f4(tfm_cmac, u, v, x, z, res);
3617 if (err)
3618 return err;
3619
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003620 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003621 return -EINVAL;
3622
3623 return 0;
3624}
3625
Herbert Xu71af2f62016-01-24 21:18:30 +08003626static int __init test_f5(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003627{
3628 const u8 w[32] = {
3629 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3630 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3631 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3632 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3633 const u8 n1[16] = {
3634 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3635 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3636 const u8 n2[16] = {
3637 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3638 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3639 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3640 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3641 const u8 exp_ltk[16] = {
3642 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3643 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3644 const u8 exp_mackey[16] = {
3645 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3646 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3647 u8 mackey[16], ltk[16];
3648 int err;
3649
3650 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3651 if (err)
3652 return err;
3653
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003654 if (crypto_memneq(mackey, exp_mackey, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003655 return -EINVAL;
3656
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003657 if (crypto_memneq(ltk, exp_ltk, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003658 return -EINVAL;
3659
3660 return 0;
3661}
3662
Herbert Xu71af2f62016-01-24 21:18:30 +08003663static int __init test_f6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003664{
3665 const u8 w[16] = {
3666 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3667 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3668 const u8 n1[16] = {
3669 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3670 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3671 const u8 n2[16] = {
3672 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3673 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3674 const u8 r[16] = {
3675 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3676 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3677 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3678 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3679 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3680 const u8 exp[16] = {
3681 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3682 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3683 u8 res[16];
3684 int err;
3685
3686 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3687 if (err)
3688 return err;
3689
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003690 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003691 return -EINVAL;
3692
3693 return 0;
3694}
3695
Herbert Xu71af2f62016-01-24 21:18:30 +08003696static int __init test_g2(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003697{
3698 const u8 u[32] = {
3699 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3700 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3701 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3702 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3703 const u8 v[32] = {
3704 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3705 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3706 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3707 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3708 const u8 x[16] = {
3709 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3710 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3711 const u8 y[16] = {
3712 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3713 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3714 const u32 exp_val = 0x2f9ed5ba % 1000000;
3715 u32 val;
3716 int err;
3717
3718 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3719 if (err)
3720 return err;
3721
3722 if (val != exp_val)
3723 return -EINVAL;
3724
3725 return 0;
3726}
3727
Herbert Xu71af2f62016-01-24 21:18:30 +08003728static int __init test_h6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003729{
3730 const u8 w[16] = {
3731 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3732 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3733 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3734 const u8 exp[16] = {
3735 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3736 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3737 u8 res[16];
3738 int err;
3739
3740 err = smp_h6(tfm_cmac, w, key_id, res);
3741 if (err)
3742 return err;
3743
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003744 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003745 return -EINVAL;
3746
3747 return 0;
3748}
3749
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003750static char test_smp_buffer[32];
3751
3752static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3753 size_t count, loff_t *ppos)
3754{
3755 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3756 strlen(test_smp_buffer));
3757}
3758
3759static const struct file_operations test_smp_fops = {
3760 .open = simple_open,
3761 .read = test_smp_read,
3762 .llseek = default_llseek,
3763};
3764
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003765static int __init run_selftests(struct crypto_cipher *tfm_aes,
Herbert Xu71af2f62016-01-24 21:18:30 +08003766 struct crypto_shash *tfm_cmac)
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003767{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003768 ktime_t calltime, delta, rettime;
3769 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003770 int err;
3771
Marcel Holtmann255047b2014-12-30 00:11:20 -08003772 calltime = ktime_get();
3773
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003774 err = test_debug_key();
3775 if (err) {
3776 BT_ERR("debug_key test failed");
3777 goto done;
3778 }
3779
Johan Hedbergcfc41982014-12-30 09:50:40 +02003780 err = test_ah(tfm_aes);
3781 if (err) {
3782 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003783 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003784 }
3785
3786 err = test_c1(tfm_aes);
3787 if (err) {
3788 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003789 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003790 }
3791
3792 err = test_s1(tfm_aes);
3793 if (err) {
3794 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003795 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003796 }
3797
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003798 err = test_f4(tfm_cmac);
3799 if (err) {
3800 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003801 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003802 }
3803
3804 err = test_f5(tfm_cmac);
3805 if (err) {
3806 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003807 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003808 }
3809
3810 err = test_f6(tfm_cmac);
3811 if (err) {
3812 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003813 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003814 }
3815
3816 err = test_g2(tfm_cmac);
3817 if (err) {
3818 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003819 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003820 }
3821
3822 err = test_h6(tfm_cmac);
3823 if (err) {
3824 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003825 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003826 }
3827
Marcel Holtmann255047b2014-12-30 00:11:20 -08003828 rettime = ktime_get();
3829 delta = ktime_sub(rettime, calltime);
3830 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3831
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003832 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003833
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003834done:
3835 if (!err)
3836 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3837 "PASS (%llu usecs)\n", duration);
3838 else
3839 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3840
3841 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3842 &test_smp_fops);
3843
3844 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003845}
3846
3847int __init bt_selftest_smp(void)
3848{
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003849 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +08003850 struct crypto_shash *tfm_cmac;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003851 int err;
3852
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003853 tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003854 if (IS_ERR(tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003855 BT_ERR("Unable to create AES crypto context");
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003856 return PTR_ERR(tfm_aes);
3857 }
3858
Herbert Xu71af2f62016-01-24 21:18:30 +08003859 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003860 if (IS_ERR(tfm_cmac)) {
3861 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003862 crypto_free_cipher(tfm_aes);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003863 return PTR_ERR(tfm_cmac);
3864 }
3865
3866 err = run_selftests(tfm_aes, tfm_cmac);
3867
Herbert Xu71af2f62016-01-24 21:18:30 +08003868 crypto_free_shash(tfm_cmac);
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003869 crypto_free_cipher(tfm_aes);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003870
3871 return err;
3872}
3873
3874#endif