Bluetooth: Track local vs remote OOB data availability
[deliverable/linux.git] / net / bluetooth / smp.c
CommitLineData
eb492e01
AB
1/*
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
300acfde 23#include <linux/debugfs.h>
8c520a59
GP
24#include <linux/crypto.h>
25#include <linux/scatterlist.h>
26#include <crypto/b128ops.h>
27
eb492e01
AB
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30#include <net/bluetooth/l2cap.h>
2b64d153 31#include <net/bluetooth/mgmt.h>
ac4b7236 32
3b19146d 33#include "ecc.h"
ac4b7236 34#include "smp.h"
d22ef0bc 35
c7a3d57d
JH
36/* Low-level debug macros to be used for stuff that we don't want
37 * accidentially in dmesg, i.e. the values of the various crypto keys
38 * and the inputs & outputs of crypto functions.
39 */
40#ifdef DEBUG
41#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
42 ##__VA_ARGS__)
43#else
44#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
45 ##__VA_ARGS__)
46#endif
47
b28b4943 48#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
b28b4943 49
3b19146d
JH
50/* Keys which are not distributed with Secure Connections */
51#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
52
17b02e62 53#define SMP_TIMEOUT msecs_to_jiffies(30000)
5d3de7df 54
d7a5a11d 55#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
0edb14de
JH
56 0x1f : 0x07)
57#define KEY_DIST_MASK 0x07
065a13e2 58
cbbbe3e2
JH
59/* Maximum message length that can be passed to aes_cmac */
60#define CMAC_MSG_MAX 80
61
533e35d4
JH
62enum {
63 SMP_FLAG_TK_VALID,
64 SMP_FLAG_CFM_PENDING,
65 SMP_FLAG_MITM_AUTH,
66 SMP_FLAG_COMPLETE,
67 SMP_FLAG_INITIATOR,
65668776 68 SMP_FLAG_SC,
d8f8edbe 69 SMP_FLAG_REMOTE_PK,
aeb7d461 70 SMP_FLAG_DEBUG_KEY,
38606f14 71 SMP_FLAG_WAIT_USER,
d3e54a87 72 SMP_FLAG_DHKEY_PENDING,
1a8bab4f
JH
73 SMP_FLAG_REMOTE_OOB,
74 SMP_FLAG_LOCAL_OOB,
533e35d4 75};
4bc58f51 76
88a479d9 77struct smp_dev {
60a27d65
MH
78 /* Secure Connections OOB data */
79 u8 local_pk[64];
80 u8 local_sk[32];
81 u8 local_rr[16];
82 bool debug_key;
83
88a479d9 84 struct crypto_blkcipher *tfm_aes;
6e2dc6d1 85 struct crypto_hash *tfm_cmac;
88a479d9
MH
86};
87
4bc58f51 88struct smp_chan {
b68fda68
JH
89 struct l2cap_conn *conn;
90 struct delayed_work security_timer;
b28b4943 91 unsigned long allow_cmd; /* Bitmask of allowed commands */
b68fda68 92
4bc58f51
JH
93 u8 preq[7]; /* SMP Pairing Request */
94 u8 prsp[7]; /* SMP Pairing Response */
95 u8 prnd[16]; /* SMP Pairing Random (local) */
96 u8 rrnd[16]; /* SMP Pairing Random (remote) */
97 u8 pcnf[16]; /* SMP Pairing Confirm */
98 u8 tk[16]; /* SMP Temporary Key */
882fafad
JH
99 u8 rr[16]; /* Remote OOB ra/rb value */
100 u8 lr[16]; /* Local OOB ra/rb value */
4bc58f51
JH
101 u8 enc_key_size;
102 u8 remote_key_dist;
103 bdaddr_t id_addr;
104 u8 id_addr_type;
105 u8 irk[16];
106 struct smp_csrk *csrk;
107 struct smp_csrk *slave_csrk;
108 struct smp_ltk *ltk;
109 struct smp_ltk *slave_ltk;
110 struct smp_irk *remote_irk;
6a77083a 111 u8 *link_key;
4a74d658 112 unsigned long flags;
783e0574 113 u8 method;
38606f14 114 u8 passkey_round;
6a7bd103 115
3b19146d
JH
116 /* Secure Connections variables */
117 u8 local_pk[64];
118 u8 local_sk[32];
d8f8edbe
JH
119 u8 remote_pk[64];
120 u8 dhkey[32];
760b018b 121 u8 mackey[16];
3b19146d 122
6a7bd103 123 struct crypto_blkcipher *tfm_aes;
407cecf6 124 struct crypto_hash *tfm_cmac;
4bc58f51
JH
125};
126
aeb7d461
JH
127/* These debug key values are defined in the SMP section of the core
128 * specification. debug_pk is the public debug key and debug_sk the
129 * private debug key.
130 */
131static const u8 debug_pk[64] = {
132 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
133 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
134 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
135 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
136
137 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
138 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
139 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
140 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
141};
142
143static const u8 debug_sk[32] = {
144 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
145 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
146 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
147 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
148};
149
8a2936f4 150static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
d22ef0bc 151{
8a2936f4 152 size_t i;
d22ef0bc 153
8a2936f4
JH
154 for (i = 0; i < len; i++)
155 dst[len - 1 - i] = src[i];
d22ef0bc
AB
156}
157
06edf8de
JH
158/* The following functions map to the LE SC SMP crypto functions
159 * AES-CMAC, f4, f5, f6, g2 and h6.
160 */
161
cbbbe3e2
JH
162static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
163 size_t len, u8 mac[16])
164{
165 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
166 struct hash_desc desc;
167 struct scatterlist sg;
168 int err;
169
170 if (len > CMAC_MSG_MAX)
171 return -EFBIG;
172
173 if (!tfm) {
174 BT_ERR("tfm %p", tfm);
175 return -EINVAL;
176 }
177
178 desc.tfm = tfm;
179 desc.flags = 0;
180
181 crypto_hash_init(&desc);
182
183 /* Swap key and message from LSB to MSB */
184 swap_buf(k, tmp, 16);
185 swap_buf(m, msg_msb, len);
186
c7a3d57d
JH
187 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
188 SMP_DBG("key %16phN", k);
cbbbe3e2
JH
189
190 err = crypto_hash_setkey(tfm, tmp, 16);
191 if (err) {
192 BT_ERR("cipher setkey failed: %d", err);
193 return err;
194 }
195
196 sg_init_one(&sg, msg_msb, len);
197
198 err = crypto_hash_update(&desc, &sg, len);
199 if (err) {
200 BT_ERR("Hash update error %d", err);
201 return err;
202 }
203
204 err = crypto_hash_final(&desc, mac_msb);
205 if (err) {
206 BT_ERR("Hash final error %d", err);
207 return err;
208 }
209
210 swap_buf(mac_msb, mac, 16);
211
c7a3d57d 212 SMP_DBG("mac %16phN", mac);
cbbbe3e2
JH
213
214 return 0;
215}
216
217static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
218 const u8 x[16], u8 z, u8 res[16])
219{
220 u8 m[65];
221 int err;
222
c7a3d57d
JH
223 SMP_DBG("u %32phN", u);
224 SMP_DBG("v %32phN", v);
225 SMP_DBG("x %16phN z %02x", x, z);
cbbbe3e2
JH
226
227 m[0] = z;
228 memcpy(m + 1, v, 32);
229 memcpy(m + 33, u, 32);
230
231 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
232 if (err)
233 return err;
234
c7a3d57d 235 SMP_DBG("res %16phN", res);
cbbbe3e2
JH
236
237 return err;
238}
239
4da50de8
JH
240static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32],
241 const u8 n1[16], const u8 n2[16], const u8 a1[7],
242 const u8 a2[7], u8 mackey[16], u8 ltk[16])
760b018b
JH
243{
244 /* The btle, salt and length "magic" values are as defined in
245 * the SMP section of the Bluetooth core specification. In ASCII
246 * the btle value ends up being 'btle'. The salt is just a
247 * random number whereas length is the value 256 in little
248 * endian format.
249 */
250 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
251 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
252 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
253 const u8 length[2] = { 0x00, 0x01 };
254 u8 m[53], t[16];
255 int err;
256
c7a3d57d
JH
257 SMP_DBG("w %32phN", w);
258 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
259 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
760b018b
JH
260
261 err = aes_cmac(tfm_cmac, salt, w, 32, t);
262 if (err)
263 return err;
264
c7a3d57d 265 SMP_DBG("t %16phN", t);
760b018b
JH
266
267 memcpy(m, length, 2);
268 memcpy(m + 2, a2, 7);
269 memcpy(m + 9, a1, 7);
270 memcpy(m + 16, n2, 16);
271 memcpy(m + 32, n1, 16);
272 memcpy(m + 48, btle, 4);
273
274 m[52] = 0; /* Counter */
275
276 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
277 if (err)
278 return err;
279
c7a3d57d 280 SMP_DBG("mackey %16phN", mackey);
760b018b
JH
281
282 m[52] = 1; /* Counter */
283
284 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
285 if (err)
286 return err;
287
c7a3d57d 288 SMP_DBG("ltk %16phN", ltk);
760b018b
JH
289
290 return 0;
291}
292
293static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
4da50de8 294 const u8 n1[16], const u8 n2[16], const u8 r[16],
760b018b
JH
295 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
296 u8 res[16])
297{
298 u8 m[65];
299 int err;
300
c7a3d57d
JH
301 SMP_DBG("w %16phN", w);
302 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
303 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
760b018b
JH
304
305 memcpy(m, a2, 7);
306 memcpy(m + 7, a1, 7);
307 memcpy(m + 14, io_cap, 3);
308 memcpy(m + 17, r, 16);
309 memcpy(m + 33, n2, 16);
310 memcpy(m + 49, n1, 16);
311
312 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
313 if (err)
314 return err;
315
203de21b 316 SMP_DBG("res %16phN", res);
760b018b
JH
317
318 return err;
319}
320
191dc7fe
JH
321static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
322 const u8 x[16], const u8 y[16], u32 *val)
323{
324 u8 m[80], tmp[16];
325 int err;
326
c7a3d57d
JH
327 SMP_DBG("u %32phN", u);
328 SMP_DBG("v %32phN", v);
329 SMP_DBG("x %16phN y %16phN", x, y);
191dc7fe
JH
330
331 memcpy(m, y, 16);
332 memcpy(m + 16, v, 32);
333 memcpy(m + 48, u, 32);
334
335 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
336 if (err)
337 return err;
338
339 *val = get_unaligned_le32(tmp);
340 *val %= 1000000;
341
c7a3d57d 342 SMP_DBG("val %06u", *val);
191dc7fe
JH
343
344 return 0;
345}
346
06edf8de
JH
347static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
348 const u8 key_id[4], u8 res[16])
349{
350 int err;
351
352 SMP_DBG("w %16phN key_id %4phN", w, key_id);
353
354 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
355 if (err)
356 return err;
357
358 SMP_DBG("res %16phN", res);
359
360 return err;
361}
362
363/* The following functions map to the legacy SMP crypto functions e, c1,
364 * s1 and ah.
365 */
366
d22ef0bc
AB
367static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
368{
369 struct blkcipher_desc desc;
370 struct scatterlist sg;
943a732a 371 uint8_t tmp[16], data[16];
201a5929 372 int err;
d22ef0bc 373
7f376cd6 374 if (!tfm) {
d22ef0bc
AB
375 BT_ERR("tfm %p", tfm);
376 return -EINVAL;
377 }
378
379 desc.tfm = tfm;
380 desc.flags = 0;
381
943a732a 382 /* The most significant octet of key corresponds to k[0] */
8a2936f4 383 swap_buf(k, tmp, 16);
943a732a
JH
384
385 err = crypto_blkcipher_setkey(tfm, tmp, 16);
d22ef0bc
AB
386 if (err) {
387 BT_ERR("cipher setkey failed: %d", err);
388 return err;
389 }
390
943a732a 391 /* Most significant octet of plaintextData corresponds to data[0] */
8a2936f4 392 swap_buf(r, data, 16);
943a732a
JH
393
394 sg_init_one(&sg, data, 16);
d22ef0bc 395
d22ef0bc
AB
396 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
397 if (err)
398 BT_ERR("Encrypt data error %d", err);
399
943a732a 400 /* Most significant octet of encryptedData corresponds to data[0] */
8a2936f4 401 swap_buf(data, r, 16);
943a732a 402
d22ef0bc
AB
403 return err;
404}
405
06edf8de
JH
406static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
407 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
408 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
6a77083a 409{
06edf8de 410 u8 p1[16], p2[16];
6a77083a
JH
411 int err;
412
06edf8de 413 memset(p1, 0, 16);
6a77083a 414
06edf8de
JH
415 /* p1 = pres || preq || _rat || _iat */
416 p1[0] = _iat;
417 p1[1] = _rat;
418 memcpy(p1 + 2, preq, 7);
419 memcpy(p1 + 9, pres, 7);
420
421 /* p2 = padding || ia || ra */
422 memcpy(p2, ra, 6);
423 memcpy(p2 + 6, ia, 6);
424 memset(p2 + 12, 0, 4);
425
426 /* res = r XOR p1 */
427 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
428
429 /* res = e(k, res) */
430 err = smp_e(tfm_aes, k, res);
431 if (err) {
432 BT_ERR("Encrypt data error");
6a77083a 433 return err;
06edf8de 434 }
6a77083a 435
06edf8de
JH
436 /* res = res XOR p2 */
437 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
438
439 /* res = e(k, res) */
440 err = smp_e(tfm_aes, k, res);
441 if (err)
442 BT_ERR("Encrypt data error");
443
444 return err;
445}
446
447static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
448 const u8 r1[16], const u8 r2[16], u8 _r[16])
449{
450 int err;
451
452 /* Just least significant octets from r1 and r2 are considered */
453 memcpy(_r, r2, 8);
454 memcpy(_r + 8, r1, 8);
455
456 err = smp_e(tfm_aes, k, _r);
457 if (err)
458 BT_ERR("Encrypt data error");
6a77083a
JH
459
460 return err;
461}
462
cd082797
JH
463static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
464 const u8 r[3], u8 res[3])
60478054 465{
943a732a 466 u8 _res[16];
60478054
JH
467 int err;
468
469 /* r' = padding || r */
943a732a
JH
470 memcpy(_res, r, 3);
471 memset(_res + 3, 0, 13);
60478054 472
943a732a 473 err = smp_e(tfm, irk, _res);
60478054
JH
474 if (err) {
475 BT_ERR("Encrypt error");
476 return err;
477 }
478
479 /* The output of the random address function ah is:
480 * ah(h, r) = e(k, r') mod 2^24
481 * The output of the security function e is then truncated to 24 bits
482 * by taking the least significant 24 bits of the output of e as the
483 * result of ah.
484 */
943a732a 485 memcpy(res, _res, 3);
60478054
JH
486
487 return 0;
488}
489
cd082797
JH
490bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
491 const bdaddr_t *bdaddr)
60478054 492{
defce9e8 493 struct l2cap_chan *chan = hdev->smp_data;
88a479d9 494 struct smp_dev *smp;
60478054
JH
495 u8 hash[3];
496 int err;
497
defce9e8
JH
498 if (!chan || !chan->data)
499 return false;
500
88a479d9 501 smp = chan->data;
defce9e8 502
60478054
JH
503 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
504
88a479d9 505 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
60478054
JH
506 if (err)
507 return false;
508
509 return !memcmp(bdaddr->b, hash, 3);
510}
511
cd082797 512int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
b1e2b3ae 513{
defce9e8 514 struct l2cap_chan *chan = hdev->smp_data;
88a479d9 515 struct smp_dev *smp;
b1e2b3ae
JH
516 int err;
517
defce9e8
JH
518 if (!chan || !chan->data)
519 return -EOPNOTSUPP;
520
88a479d9 521 smp = chan->data;
defce9e8 522
b1e2b3ae
JH
523 get_random_bytes(&rpa->b[3], 3);
524
525 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
526 rpa->b[5] |= 0x40; /* Set second most significant bit */
527
88a479d9 528 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
b1e2b3ae
JH
529 if (err < 0)
530 return err;
531
532 BT_DBG("RPA %pMR", rpa);
533
534 return 0;
535}
536
60a27d65
MH
537int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
538{
539 struct l2cap_chan *chan = hdev->smp_data;
540 struct smp_dev *smp;
541 int err;
542
543 if (!chan || !chan->data)
544 return -EOPNOTSUPP;
545
546 smp = chan->data;
547
548 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
549 BT_DBG("Using debug keys");
550 memcpy(smp->local_pk, debug_pk, 64);
551 memcpy(smp->local_sk, debug_sk, 32);
552 smp->debug_key = true;
553 } else {
554 while (true) {
555 /* Generate local key pair for Secure Connections */
556 if (!ecc_make_key(smp->local_pk, smp->local_sk))
557 return -EIO;
558
559 /* This is unlikely, but we need to check that
560 * we didn't accidentially generate a debug key.
561 */
562 if (memcmp(smp->local_sk, debug_sk, 32))
563 break;
564 }
565 smp->debug_key = false;
566 }
567
568 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
569 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
570 SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
571
572 get_random_bytes(smp->local_rr, 16);
573
574 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
575 smp->local_rr, 0, hash);
576 if (err < 0)
577 return err;
578
579 memcpy(rand, smp->local_rr, 16);
580
581 return 0;
582}
583
5d88cc73 584static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
eb492e01 585{
5d88cc73 586 struct l2cap_chan *chan = conn->smp;
b68fda68 587 struct smp_chan *smp;
5d88cc73
JH
588 struct kvec iv[2];
589 struct msghdr msg;
eb492e01 590
5d88cc73
JH
591 if (!chan)
592 return;
eb492e01 593
5d88cc73 594 BT_DBG("code 0x%2.2x", code);
eb492e01 595
5d88cc73
JH
596 iv[0].iov_base = &code;
597 iv[0].iov_len = 1;
eb492e01 598
5d88cc73
JH
599 iv[1].iov_base = data;
600 iv[1].iov_len = len;
eb492e01 601
5d88cc73 602 memset(&msg, 0, sizeof(msg));
eb492e01 603
17836394 604 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
eb492e01 605
5d88cc73 606 l2cap_chan_send(chan, &msg, 1 + len);
e2dcd113 607
b68fda68
JH
608 if (!chan->data)
609 return;
610
611 smp = chan->data;
612
613 cancel_delayed_work_sync(&smp->security_timer);
1b0921d6 614 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
eb492e01
AB
615}
616
d2eb9e10 617static u8 authreq_to_seclevel(u8 authreq)
2b64d153 618{
d2eb9e10
JH
619 if (authreq & SMP_AUTH_MITM) {
620 if (authreq & SMP_AUTH_SC)
621 return BT_SECURITY_FIPS;
622 else
623 return BT_SECURITY_HIGH;
624 } else {
2b64d153 625 return BT_SECURITY_MEDIUM;
d2eb9e10 626 }
2b64d153
BG
627}
628
629static __u8 seclevel_to_authreq(__u8 sec_level)
630{
631 switch (sec_level) {
d2eb9e10 632 case BT_SECURITY_FIPS:
2b64d153
BG
633 case BT_SECURITY_HIGH:
634 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
635 case BT_SECURITY_MEDIUM:
636 return SMP_AUTH_BONDING;
637 default:
638 return SMP_AUTH_NONE;
639 }
640}
641
b8e66eac 642static void build_pairing_cmd(struct l2cap_conn *conn,
f1560463
MH
643 struct smp_cmd_pairing *req,
644 struct smp_cmd_pairing *rsp, __u8 authreq)
b8e66eac 645{
5d88cc73
JH
646 struct l2cap_chan *chan = conn->smp;
647 struct smp_chan *smp = chan->data;
fd349c02
JH
648 struct hci_conn *hcon = conn->hcon;
649 struct hci_dev *hdev = hcon->hdev;
02b05bd8 650 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
54790f73 651
d7a5a11d 652 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
7ee4ea36
MH
653 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
654 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
54790f73 655 authreq |= SMP_AUTH_BONDING;
2b64d153
BG
656 } else {
657 authreq &= ~SMP_AUTH_BONDING;
54790f73
VCG
658 }
659
d7a5a11d 660 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
fd349c02
JH
661 remote_dist |= SMP_DIST_ID_KEY;
662
d7a5a11d 663 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
863efaf2
JH
664 local_dist |= SMP_DIST_ID_KEY;
665
d7a5a11d 666 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
02b05bd8
JH
667 (authreq & SMP_AUTH_SC)) {
668 struct oob_data *oob_data;
669 u8 bdaddr_type;
670
d7a5a11d 671 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
df8e1a4c
JH
672 local_dist |= SMP_DIST_LINK_KEY;
673 remote_dist |= SMP_DIST_LINK_KEY;
674 }
02b05bd8
JH
675
676 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
677 bdaddr_type = BDADDR_LE_PUBLIC;
678 else
679 bdaddr_type = BDADDR_LE_RANDOM;
680
681 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
682 bdaddr_type);
4775a4ea 683 if (oob_data && oob_data->present) {
1a8bab4f 684 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
02b05bd8 685 oob_flag = SMP_OOB_PRESENT;
a29b0733 686 memcpy(smp->rr, oob_data->rand256, 16);
02b05bd8
JH
687 memcpy(smp->pcnf, oob_data->hash256, 16);
688 }
689
df8e1a4c
JH
690 } else {
691 authreq &= ~SMP_AUTH_SC;
692 }
693
54790f73
VCG
694 if (rsp == NULL) {
695 req->io_capability = conn->hcon->io_capability;
02b05bd8 696 req->oob_flag = oob_flag;
54790f73 697 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
fd349c02
JH
698 req->init_key_dist = local_dist;
699 req->resp_key_dist = remote_dist;
0edb14de 700 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
fd349c02
JH
701
702 smp->remote_key_dist = remote_dist;
54790f73
VCG
703 return;
704 }
705
706 rsp->io_capability = conn->hcon->io_capability;
02b05bd8 707 rsp->oob_flag = oob_flag;
54790f73 708 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
fd349c02
JH
709 rsp->init_key_dist = req->init_key_dist & remote_dist;
710 rsp->resp_key_dist = req->resp_key_dist & local_dist;
0edb14de 711 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
fd349c02
JH
712
713 smp->remote_key_dist = rsp->init_key_dist;
b8e66eac
VCG
714}
715
3158c50c
VCG
716static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
717{
5d88cc73
JH
718 struct l2cap_chan *chan = conn->smp;
719 struct smp_chan *smp = chan->data;
1c1def09 720
3158c50c 721 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
f1560463 722 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
3158c50c
VCG
723 return SMP_ENC_KEY_SIZE;
724
f7aa611a 725 smp->enc_key_size = max_key_size;
3158c50c
VCG
726
727 return 0;
728}
729
6f48e260
JH
730static void smp_chan_destroy(struct l2cap_conn *conn)
731{
732 struct l2cap_chan *chan = conn->smp;
733 struct smp_chan *smp = chan->data;
923e2414 734 struct hci_conn *hcon = conn->hcon;
6f48e260
JH
735 bool complete;
736
737 BUG_ON(!smp);
738
739 cancel_delayed_work_sync(&smp->security_timer);
6f48e260 740
6f48e260 741 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
923e2414 742 mgmt_smp_complete(hcon, complete);
6f48e260 743
276812ec
MH
744 kzfree(smp->csrk);
745 kzfree(smp->slave_csrk);
746 kzfree(smp->link_key);
6f48e260
JH
747
748 crypto_free_blkcipher(smp->tfm_aes);
407cecf6 749 crypto_free_hash(smp->tfm_cmac);
6f48e260 750
923e2414
JH
751 /* Ensure that we don't leave any debug key around if debug key
752 * support hasn't been explicitly enabled.
753 */
754 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
d7a5a11d 755 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
923e2414
JH
756 list_del_rcu(&smp->ltk->list);
757 kfree_rcu(smp->ltk, rcu);
758 smp->ltk = NULL;
759 }
760
6f48e260
JH
761 /* If pairing failed clean up any keys we might have */
762 if (!complete) {
763 if (smp->ltk) {
970d0f1b
JH
764 list_del_rcu(&smp->ltk->list);
765 kfree_rcu(smp->ltk, rcu);
6f48e260
JH
766 }
767
768 if (smp->slave_ltk) {
970d0f1b
JH
769 list_del_rcu(&smp->slave_ltk->list);
770 kfree_rcu(smp->slave_ltk, rcu);
6f48e260
JH
771 }
772
773 if (smp->remote_irk) {
adae20cb
JH
774 list_del_rcu(&smp->remote_irk->list);
775 kfree_rcu(smp->remote_irk, rcu);
6f48e260
JH
776 }
777 }
778
779 chan->data = NULL;
276812ec 780 kzfree(smp);
923e2414 781 hci_conn_drop(hcon);
6f48e260
JH
782}
783
84794e11 784static void smp_failure(struct l2cap_conn *conn, u8 reason)
4f957a76 785{
bab73cb6 786 struct hci_conn *hcon = conn->hcon;
b68fda68 787 struct l2cap_chan *chan = conn->smp;
bab73cb6 788
84794e11 789 if (reason)
4f957a76 790 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
f1560463 791 &reason);
4f957a76 792
ce39fb4e 793 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
e1e930f5 794 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
f1c09c07 795
fc75cc86 796 if (chan->data)
f1c09c07 797 smp_chan_destroy(conn);
4f957a76
BG
798}
799
2b64d153
BG
800#define JUST_WORKS 0x00
801#define JUST_CFM 0x01
802#define REQ_PASSKEY 0x02
803#define CFM_PASSKEY 0x03
804#define REQ_OOB 0x04
5e3d3d9b 805#define DSP_PASSKEY 0x05
2b64d153
BG
806#define OVERLAP 0xFF
807
808static const u8 gen_method[5][5] = {
809 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
810 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
811 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
812 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
813 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
814};
815
5e3d3d9b
JH
816static const u8 sc_method[5][5] = {
817 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
818 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
819 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
820 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
821 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
822};
823
581370cc
JH
824static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
825{
2bcd4003
JH
826 /* If either side has unknown io_caps, use JUST_CFM (which gets
827 * converted later to JUST_WORKS if we're initiators.
828 */
581370cc
JH
829 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
830 remote_io > SMP_IO_KEYBOARD_DISPLAY)
2bcd4003 831 return JUST_CFM;
581370cc 832
5e3d3d9b
JH
833 if (test_bit(SMP_FLAG_SC, &smp->flags))
834 return sc_method[remote_io][local_io];
835
581370cc
JH
836 return gen_method[remote_io][local_io];
837}
838
2b64d153
BG
839static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
840 u8 local_io, u8 remote_io)
841{
842 struct hci_conn *hcon = conn->hcon;
5d88cc73
JH
843 struct l2cap_chan *chan = conn->smp;
844 struct smp_chan *smp = chan->data;
2b64d153
BG
845 u32 passkey = 0;
846 int ret = 0;
847
848 /* Initialize key for JUST WORKS */
849 memset(smp->tk, 0, sizeof(smp->tk));
4a74d658 850 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
851
852 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
853
2bcd4003
JH
854 /* If neither side wants MITM, either "just" confirm an incoming
855 * request or use just-works for outgoing ones. The JUST_CFM
856 * will be converted to JUST_WORKS if necessary later in this
857 * function. If either side has MITM look up the method from the
858 * table.
859 */
581370cc 860 if (!(auth & SMP_AUTH_MITM))
783e0574 861 smp->method = JUST_CFM;
2b64d153 862 else
783e0574 863 smp->method = get_auth_method(smp, local_io, remote_io);
2b64d153 864
a82505c7 865 /* Don't confirm locally initiated pairing attempts */
783e0574
JH
866 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
867 &smp->flags))
868 smp->method = JUST_WORKS;
a82505c7 869
02f3e254 870 /* Don't bother user space with no IO capabilities */
783e0574
JH
871 if (smp->method == JUST_CFM &&
872 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
873 smp->method = JUST_WORKS;
02f3e254 874
2b64d153 875 /* If Just Works, Continue with Zero TK */
783e0574 876 if (smp->method == JUST_WORKS) {
4a74d658 877 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
878 return 0;
879 }
880
881 /* Not Just Works/Confirm results in MITM Authentication */
783e0574 882 if (smp->method != JUST_CFM) {
4a74d658 883 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
5eb596f5
JH
884 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
885 hcon->pending_sec_level = BT_SECURITY_HIGH;
886 }
2b64d153
BG
887
888 /* If both devices have Keyoard-Display I/O, the master
889 * Confirms and the slave Enters the passkey.
890 */
783e0574 891 if (smp->method == OVERLAP) {
40bef302 892 if (hcon->role == HCI_ROLE_MASTER)
783e0574 893 smp->method = CFM_PASSKEY;
2b64d153 894 else
783e0574 895 smp->method = REQ_PASSKEY;
2b64d153
BG
896 }
897
01ad34d2 898 /* Generate random passkey. */
783e0574 899 if (smp->method == CFM_PASSKEY) {
943a732a 900 memset(smp->tk, 0, sizeof(smp->tk));
2b64d153
BG
901 get_random_bytes(&passkey, sizeof(passkey));
902 passkey %= 1000000;
943a732a 903 put_unaligned_le32(passkey, smp->tk);
2b64d153 904 BT_DBG("PassKey: %d", passkey);
4a74d658 905 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
906 }
907
783e0574 908 if (smp->method == REQ_PASSKEY)
ce39fb4e 909 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
272d90df 910 hcon->type, hcon->dst_type);
783e0574 911 else if (smp->method == JUST_CFM)
4eb65e66
JH
912 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
913 hcon->type, hcon->dst_type,
914 passkey, 1);
2b64d153 915 else
01ad34d2 916 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
272d90df 917 hcon->type, hcon->dst_type,
39adbffe 918 passkey, 0);
2b64d153 919
2b64d153
BG
920 return ret;
921}
922
1cc61144 923static u8 smp_confirm(struct smp_chan *smp)
8aab4757 924{
8aab4757 925 struct l2cap_conn *conn = smp->conn;
8aab4757
VCG
926 struct smp_cmd_pairing_confirm cp;
927 int ret;
8aab4757
VCG
928
929 BT_DBG("conn %p", conn);
930
e491eaf3 931 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
b1cd5fd9 932 conn->hcon->init_addr_type, &conn->hcon->init_addr,
943a732a
JH
933 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
934 cp.confirm_val);
1cc61144
JH
935 if (ret)
936 return SMP_UNSPECIFIED;
8aab4757 937
4a74d658 938 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
2b64d153 939
8aab4757
VCG
940 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
941
b28b4943
JH
942 if (conn->hcon->out)
943 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
944 else
945 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
946
1cc61144 947 return 0;
8aab4757
VCG
948}
949
861580a9 950static u8 smp_random(struct smp_chan *smp)
8aab4757 951{
8aab4757
VCG
952 struct l2cap_conn *conn = smp->conn;
953 struct hci_conn *hcon = conn->hcon;
861580a9 954 u8 confirm[16];
8aab4757
VCG
955 int ret;
956
ec70f36f 957 if (IS_ERR_OR_NULL(smp->tfm_aes))
861580a9 958 return SMP_UNSPECIFIED;
8aab4757
VCG
959
960 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
961
e491eaf3 962 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
b1cd5fd9 963 hcon->init_addr_type, &hcon->init_addr,
943a732a 964 hcon->resp_addr_type, &hcon->resp_addr, confirm);
861580a9
JH
965 if (ret)
966 return SMP_UNSPECIFIED;
8aab4757 967
8aab4757
VCG
968 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
969 BT_ERR("Pairing failed (confirmation values mismatch)");
861580a9 970 return SMP_CONFIRM_FAILED;
8aab4757
VCG
971 }
972
973 if (hcon->out) {
fe39c7b2
MH
974 u8 stk[16];
975 __le64 rand = 0;
976 __le16 ediv = 0;
8aab4757 977
e491eaf3 978 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
8aab4757 979
f7aa611a 980 memset(stk + smp->enc_key_size, 0,
04124681 981 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
8aab4757 982
861580a9
JH
983 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
984 return SMP_UNSPECIFIED;
8aab4757
VCG
985
986 hci_le_start_enc(hcon, ediv, rand, stk);
f7aa611a 987 hcon->enc_key_size = smp->enc_key_size;
fe59a05f 988 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
8aab4757 989 } else {
fff3490f 990 u8 stk[16], auth;
fe39c7b2
MH
991 __le64 rand = 0;
992 __le16 ediv = 0;
8aab4757 993
943a732a
JH
994 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
995 smp->prnd);
8aab4757 996
e491eaf3 997 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
8aab4757 998
f7aa611a 999 memset(stk + smp->enc_key_size, 0,
f1560463 1000 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
8aab4757 1001
fff3490f
JH
1002 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1003 auth = 1;
1004 else
1005 auth = 0;
1006
7d5843b7
JH
1007 /* Even though there's no _SLAVE suffix this is the
1008 * slave STK we're adding for later lookup (the master
1009 * STK never needs to be stored).
1010 */
ce39fb4e 1011 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
2ceba539 1012 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
8aab4757
VCG
1013 }
1014
861580a9 1015 return 0;
8aab4757
VCG
1016}
1017
44f1a7ab
JH
1018static void smp_notify_keys(struct l2cap_conn *conn)
1019{
1020 struct l2cap_chan *chan = conn->smp;
1021 struct smp_chan *smp = chan->data;
1022 struct hci_conn *hcon = conn->hcon;
1023 struct hci_dev *hdev = hcon->hdev;
1024 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1025 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1026 bool persistent;
1027
1028 if (smp->remote_irk) {
1029 mgmt_new_irk(hdev, smp->remote_irk);
1030 /* Now that user space can be considered to know the
1031 * identity address track the connection based on it
b5ae344d 1032 * from now on (assuming this is an LE link).
44f1a7ab 1033 */
b5ae344d
JH
1034 if (hcon->type == LE_LINK) {
1035 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1036 hcon->dst_type = smp->remote_irk->addr_type;
1037 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1038 }
44f1a7ab
JH
1039
1040 /* When receiving an indentity resolving key for
1041 * a remote device that does not use a resolvable
1042 * private address, just remove the key so that
1043 * it is possible to use the controller white
1044 * list for scanning.
1045 *
1046 * Userspace will have been told to not store
1047 * this key at this point. So it is safe to
1048 * just remove it.
1049 */
1050 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
adae20cb
JH
1051 list_del_rcu(&smp->remote_irk->list);
1052 kfree_rcu(smp->remote_irk, rcu);
44f1a7ab
JH
1053 smp->remote_irk = NULL;
1054 }
1055 }
1056
b5ae344d
JH
1057 if (hcon->type == ACL_LINK) {
1058 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1059 persistent = false;
1060 else
1061 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1062 &hcon->flags);
1063 } else {
1064 /* The LTKs and CSRKs should be persistent only if both sides
1065 * had the bonding bit set in their authentication requests.
1066 */
1067 persistent = !!((req->auth_req & rsp->auth_req) &
1068 SMP_AUTH_BONDING);
1069 }
1070
44f1a7ab
JH
1071
1072 if (smp->csrk) {
1073 smp->csrk->bdaddr_type = hcon->dst_type;
1074 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1075 mgmt_new_csrk(hdev, smp->csrk, persistent);
1076 }
1077
1078 if (smp->slave_csrk) {
1079 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1080 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1081 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1082 }
1083
1084 if (smp->ltk) {
1085 smp->ltk->bdaddr_type = hcon->dst_type;
1086 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1087 mgmt_new_ltk(hdev, smp->ltk, persistent);
1088 }
1089
1090 if (smp->slave_ltk) {
1091 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1092 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1093 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1094 }
6a77083a
JH
1095
1096 if (smp->link_key) {
e3befab9
JH
1097 struct link_key *key;
1098 u8 type;
1099
1100 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1101 type = HCI_LK_DEBUG_COMBINATION;
1102 else if (hcon->sec_level == BT_SECURITY_FIPS)
1103 type = HCI_LK_AUTH_COMBINATION_P256;
1104 else
1105 type = HCI_LK_UNAUTH_COMBINATION_P256;
1106
1107 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1108 smp->link_key, type, 0, &persistent);
1109 if (key) {
1110 mgmt_new_link_key(hdev, key, persistent);
1111
1112 /* Don't keep debug keys around if the relevant
1113 * flag is not set.
1114 */
d7a5a11d 1115 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
e3befab9
JH
1116 key->type == HCI_LK_DEBUG_COMBINATION) {
1117 list_del_rcu(&key->list);
1118 kfree_rcu(key, rcu);
1119 }
1120 }
6a77083a
JH
1121 }
1122}
1123
d3e54a87
JH
1124static void sc_add_ltk(struct smp_chan *smp)
1125{
1126 struct hci_conn *hcon = smp->conn->hcon;
1127 u8 key_type, auth;
1128
1129 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1130 key_type = SMP_LTK_P256_DEBUG;
1131 else
1132 key_type = SMP_LTK_P256;
1133
1134 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1135 auth = 1;
1136 else
1137 auth = 0;
1138
1139 memset(smp->tk + smp->enc_key_size, 0,
1140 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1141
1142 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1143 key_type, auth, smp->tk, smp->enc_key_size,
1144 0, 0);
1145}
1146
6a77083a
JH
1147static void sc_generate_link_key(struct smp_chan *smp)
1148{
1149 /* These constants are as specified in the core specification.
1150 * In ASCII they spell out to 'tmp1' and 'lebr'.
1151 */
1152 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1153 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1154
1155 smp->link_key = kzalloc(16, GFP_KERNEL);
1156 if (!smp->link_key)
1157 return;
1158
1159 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
276812ec 1160 kzfree(smp->link_key);
6a77083a
JH
1161 smp->link_key = NULL;
1162 return;
1163 }
1164
1165 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
276812ec 1166 kzfree(smp->link_key);
6a77083a
JH
1167 smp->link_key = NULL;
1168 return;
1169 }
44f1a7ab
JH
1170}
1171
b28b4943
JH
1172static void smp_allow_key_dist(struct smp_chan *smp)
1173{
1174 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1175 * will be allowed in each PDU handler to ensure we receive
1176 * them in the correct order.
1177 */
1178 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1179 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1180 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1181 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1182 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1183 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1184}
1185
b5ae344d
JH
1186static void sc_generate_ltk(struct smp_chan *smp)
1187{
1188 /* These constants are as specified in the core specification.
1189 * In ASCII they spell out to 'tmp2' and 'brle'.
1190 */
1191 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1192 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1193 struct hci_conn *hcon = smp->conn->hcon;
1194 struct hci_dev *hdev = hcon->hdev;
1195 struct link_key *key;
1196
1197 key = hci_find_link_key(hdev, &hcon->dst);
1198 if (!key) {
1199 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1200 return;
1201 }
1202
1203 if (key->type == HCI_LK_DEBUG_COMBINATION)
1204 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1205
1206 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1207 return;
1208
1209 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1210 return;
1211
1212 sc_add_ltk(smp);
1213}
1214
d6268e86 1215static void smp_distribute_keys(struct smp_chan *smp)
44f1a7ab
JH
1216{
1217 struct smp_cmd_pairing *req, *rsp;
86d1407c 1218 struct l2cap_conn *conn = smp->conn;
44f1a7ab
JH
1219 struct hci_conn *hcon = conn->hcon;
1220 struct hci_dev *hdev = hcon->hdev;
1221 __u8 *keydist;
1222
1223 BT_DBG("conn %p", conn);
1224
44f1a7ab
JH
1225 rsp = (void *) &smp->prsp[1];
1226
1227 /* The responder sends its keys first */
b28b4943
JH
1228 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1229 smp_allow_key_dist(smp);
86d1407c 1230 return;
b28b4943 1231 }
44f1a7ab
JH
1232
1233 req = (void *) &smp->preq[1];
1234
1235 if (hcon->out) {
1236 keydist = &rsp->init_key_dist;
1237 *keydist &= req->init_key_dist;
1238 } else {
1239 keydist = &rsp->resp_key_dist;
1240 *keydist &= req->resp_key_dist;
1241 }
1242
6a77083a 1243 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
b5ae344d 1244 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
6a77083a 1245 sc_generate_link_key(smp);
b5ae344d
JH
1246 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1247 sc_generate_ltk(smp);
6a77083a
JH
1248
1249 /* Clear the keys which are generated but not distributed */
1250 *keydist &= ~SMP_SC_NO_DIST;
1251 }
1252
44f1a7ab
JH
1253 BT_DBG("keydist 0x%x", *keydist);
1254
1255 if (*keydist & SMP_DIST_ENC_KEY) {
1256 struct smp_cmd_encrypt_info enc;
1257 struct smp_cmd_master_ident ident;
1258 struct smp_ltk *ltk;
1259 u8 authenticated;
1260 __le16 ediv;
1261 __le64 rand;
1262
1263 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1264 get_random_bytes(&ediv, sizeof(ediv));
1265 get_random_bytes(&rand, sizeof(rand));
1266
1267 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1268
1269 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1270 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1271 SMP_LTK_SLAVE, authenticated, enc.ltk,
1272 smp->enc_key_size, ediv, rand);
1273 smp->slave_ltk = ltk;
1274
1275 ident.ediv = ediv;
1276 ident.rand = rand;
1277
1278 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1279
1280 *keydist &= ~SMP_DIST_ENC_KEY;
1281 }
1282
1283 if (*keydist & SMP_DIST_ID_KEY) {
1284 struct smp_cmd_ident_addr_info addrinfo;
1285 struct smp_cmd_ident_info idinfo;
1286
1287 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1288
1289 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1290
1291 /* The hci_conn contains the local identity address
1292 * after the connection has been established.
1293 *
1294 * This is true even when the connection has been
1295 * established using a resolvable random address.
1296 */
1297 bacpy(&addrinfo.bdaddr, &hcon->src);
1298 addrinfo.addr_type = hcon->src_type;
1299
1300 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1301 &addrinfo);
1302
1303 *keydist &= ~SMP_DIST_ID_KEY;
1304 }
1305
1306 if (*keydist & SMP_DIST_SIGN) {
1307 struct smp_cmd_sign_info sign;
1308 struct smp_csrk *csrk;
1309
1310 /* Generate a new random key */
1311 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1312
1313 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1314 if (csrk) {
4cd3928a
JH
1315 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1316 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1317 else
1318 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
44f1a7ab
JH
1319 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1320 }
1321 smp->slave_csrk = csrk;
1322
1323 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1324
1325 *keydist &= ~SMP_DIST_SIGN;
1326 }
1327
1328 /* If there are still keys to be received wait for them */
b28b4943
JH
1329 if (smp->remote_key_dist & KEY_DIST_MASK) {
1330 smp_allow_key_dist(smp);
86d1407c 1331 return;
b28b4943 1332 }
44f1a7ab 1333
44f1a7ab
JH
1334 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1335 smp_notify_keys(conn);
1336
1337 smp_chan_destroy(conn);
44f1a7ab
JH
1338}
1339
b68fda68
JH
1340static void smp_timeout(struct work_struct *work)
1341{
1342 struct smp_chan *smp = container_of(work, struct smp_chan,
1343 security_timer.work);
1344 struct l2cap_conn *conn = smp->conn;
1345
1346 BT_DBG("conn %p", conn);
1347
1e91c29e 1348 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
b68fda68
JH
1349}
1350
8aab4757
VCG
1351static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1352{
5d88cc73 1353 struct l2cap_chan *chan = conn->smp;
8aab4757
VCG
1354 struct smp_chan *smp;
1355
f1560463 1356 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
fc75cc86 1357 if (!smp)
8aab4757
VCG
1358 return NULL;
1359
6a7bd103
JH
1360 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1361 if (IS_ERR(smp->tfm_aes)) {
1362 BT_ERR("Unable to create ECB crypto context");
276812ec 1363 kzfree(smp);
6a7bd103
JH
1364 return NULL;
1365 }
1366
407cecf6
JH
1367 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1368 if (IS_ERR(smp->tfm_cmac)) {
1369 BT_ERR("Unable to create CMAC crypto context");
1370 crypto_free_blkcipher(smp->tfm_aes);
276812ec 1371 kzfree(smp);
407cecf6
JH
1372 return NULL;
1373 }
1374
8aab4757 1375 smp->conn = conn;
5d88cc73 1376 chan->data = smp;
8aab4757 1377
b28b4943
JH
1378 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1379
b68fda68
JH
1380 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1381
8aab4757
VCG
1382 hci_conn_hold(conn->hcon);
1383
1384 return smp;
1385}
1386
760b018b
JH
1387static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1388{
1389 struct hci_conn *hcon = smp->conn->hcon;
1390 u8 *na, *nb, a[7], b[7];
1391
1392 if (hcon->out) {
1393 na = smp->prnd;
1394 nb = smp->rrnd;
1395 } else {
1396 na = smp->rrnd;
1397 nb = smp->prnd;
1398 }
1399
1400 memcpy(a, &hcon->init_addr, 6);
1401 memcpy(b, &hcon->resp_addr, 6);
1402 a[6] = hcon->init_addr_type;
1403 b[6] = hcon->resp_addr_type;
1404
1405 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1406}
1407
38606f14 1408static void sc_dhkey_check(struct smp_chan *smp)
760b018b
JH
1409{
1410 struct hci_conn *hcon = smp->conn->hcon;
1411 struct smp_cmd_dhkey_check check;
1412 u8 a[7], b[7], *local_addr, *remote_addr;
1413 u8 io_cap[3], r[16];
1414
760b018b
JH
1415 memcpy(a, &hcon->init_addr, 6);
1416 memcpy(b, &hcon->resp_addr, 6);
1417 a[6] = hcon->init_addr_type;
1418 b[6] = hcon->resp_addr_type;
1419
1420 if (hcon->out) {
1421 local_addr = a;
1422 remote_addr = b;
1423 memcpy(io_cap, &smp->preq[1], 3);
1424 } else {
1425 local_addr = b;
1426 remote_addr = a;
1427 memcpy(io_cap, &smp->prsp[1], 3);
1428 }
1429
dddd3059
JH
1430 memset(r, 0, sizeof(r));
1431
1432 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
38606f14 1433 put_unaligned_le32(hcon->passkey_notify, r);
760b018b 1434
a29b0733
JH
1435 if (smp->method == REQ_OOB)
1436 memcpy(r, smp->rr, 16);
1437
760b018b
JH
1438 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1439 local_addr, remote_addr, check.e);
1440
1441 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
dddd3059
JH
1442}
1443
38606f14
JH
1444static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1445{
1446 struct l2cap_conn *conn = smp->conn;
1447 struct hci_conn *hcon = conn->hcon;
1448 struct smp_cmd_pairing_confirm cfm;
1449 u8 r;
1450
1451 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1452 r |= 0x80;
1453
1454 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1455
1456 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1457 cfm.confirm_val))
1458 return SMP_UNSPECIFIED;
1459
1460 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1461
1462 return 0;
1463}
1464
1465static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1466{
1467 struct l2cap_conn *conn = smp->conn;
1468 struct hci_conn *hcon = conn->hcon;
1469 struct hci_dev *hdev = hcon->hdev;
1470 u8 cfm[16], r;
1471
1472 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1473 if (smp->passkey_round >= 20)
1474 return 0;
1475
1476 switch (smp_op) {
1477 case SMP_CMD_PAIRING_RANDOM:
1478 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1479 r |= 0x80;
1480
1481 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1482 smp->rrnd, r, cfm))
1483 return SMP_UNSPECIFIED;
1484
1485 if (memcmp(smp->pcnf, cfm, 16))
1486 return SMP_CONFIRM_FAILED;
1487
1488 smp->passkey_round++;
1489
1490 if (smp->passkey_round == 20) {
1491 /* Generate MacKey and LTK */
1492 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1493 return SMP_UNSPECIFIED;
1494 }
1495
1496 /* The round is only complete when the initiator
1497 * receives pairing random.
1498 */
1499 if (!hcon->out) {
1500 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1501 sizeof(smp->prnd), smp->prnd);
d3e54a87 1502 if (smp->passkey_round == 20)
38606f14 1503 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
d3e54a87 1504 else
38606f14 1505 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
38606f14
JH
1506 return 0;
1507 }
1508
1509 /* Start the next round */
1510 if (smp->passkey_round != 20)
1511 return sc_passkey_round(smp, 0);
1512
1513 /* Passkey rounds are complete - start DHKey Check */
1514 sc_dhkey_check(smp);
1515 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1516
1517 break;
1518
1519 case SMP_CMD_PAIRING_CONFIRM:
1520 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1521 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1522 return 0;
1523 }
1524
1525 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1526
1527 if (hcon->out) {
1528 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1529 sizeof(smp->prnd), smp->prnd);
1530 return 0;
1531 }
1532
1533 return sc_passkey_send_confirm(smp);
1534
1535 case SMP_CMD_PUBLIC_KEY:
1536 default:
1537 /* Initiating device starts the round */
1538 if (!hcon->out)
1539 return 0;
1540
1541 BT_DBG("%s Starting passkey round %u", hdev->name,
1542 smp->passkey_round + 1);
1543
1544 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1545
1546 return sc_passkey_send_confirm(smp);
1547 }
1548
1549 return 0;
1550}
1551
dddd3059
JH
1552static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1553{
38606f14
JH
1554 struct l2cap_conn *conn = smp->conn;
1555 struct hci_conn *hcon = conn->hcon;
1556 u8 smp_op;
1557
1558 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1559
dddd3059
JH
1560 switch (mgmt_op) {
1561 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1562 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1563 return 0;
1564 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1565 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1566 return 0;
38606f14
JH
1567 case MGMT_OP_USER_PASSKEY_REPLY:
1568 hcon->passkey_notify = le32_to_cpu(passkey);
1569 smp->passkey_round = 0;
1570
1571 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1572 smp_op = SMP_CMD_PAIRING_CONFIRM;
1573 else
1574 smp_op = 0;
1575
1576 if (sc_passkey_round(smp, smp_op))
1577 return -EIO;
1578
1579 return 0;
dddd3059
JH
1580 }
1581
d3e54a87
JH
1582 /* Initiator sends DHKey check first */
1583 if (hcon->out) {
1584 sc_dhkey_check(smp);
1585 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1586 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1587 sc_dhkey_check(smp);
1588 sc_add_ltk(smp);
1589 }
760b018b
JH
1590
1591 return 0;
1592}
1593
2b64d153
BG
1594int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1595{
b10e8017 1596 struct l2cap_conn *conn = hcon->l2cap_data;
5d88cc73 1597 struct l2cap_chan *chan;
2b64d153
BG
1598 struct smp_chan *smp;
1599 u32 value;
fc75cc86 1600 int err;
2b64d153
BG
1601
1602 BT_DBG("");
1603
fc75cc86 1604 if (!conn)
2b64d153
BG
1605 return -ENOTCONN;
1606
5d88cc73
JH
1607 chan = conn->smp;
1608 if (!chan)
1609 return -ENOTCONN;
1610
fc75cc86
JH
1611 l2cap_chan_lock(chan);
1612 if (!chan->data) {
1613 err = -ENOTCONN;
1614 goto unlock;
1615 }
1616
5d88cc73 1617 smp = chan->data;
2b64d153 1618
760b018b
JH
1619 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1620 err = sc_user_reply(smp, mgmt_op, passkey);
1621 goto unlock;
1622 }
1623
2b64d153
BG
1624 switch (mgmt_op) {
1625 case MGMT_OP_USER_PASSKEY_REPLY:
1626 value = le32_to_cpu(passkey);
943a732a 1627 memset(smp->tk, 0, sizeof(smp->tk));
2b64d153 1628 BT_DBG("PassKey: %d", value);
943a732a 1629 put_unaligned_le32(value, smp->tk);
2b64d153
BG
1630 /* Fall Through */
1631 case MGMT_OP_USER_CONFIRM_REPLY:
4a74d658 1632 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
2b64d153
BG
1633 break;
1634 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1635 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
84794e11 1636 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
fc75cc86
JH
1637 err = 0;
1638 goto unlock;
2b64d153 1639 default:
84794e11 1640 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
fc75cc86
JH
1641 err = -EOPNOTSUPP;
1642 goto unlock;
2b64d153
BG
1643 }
1644
fc75cc86
JH
1645 err = 0;
1646
2b64d153 1647 /* If it is our turn to send Pairing Confirm, do so now */
1cc61144
JH
1648 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1649 u8 rsp = smp_confirm(smp);
1650 if (rsp)
1651 smp_failure(conn, rsp);
1652 }
2b64d153 1653
fc75cc86
JH
1654unlock:
1655 l2cap_chan_unlock(chan);
1656 return err;
2b64d153
BG
1657}
1658
b5ae344d
JH
1659static void build_bredr_pairing_cmd(struct smp_chan *smp,
1660 struct smp_cmd_pairing *req,
1661 struct smp_cmd_pairing *rsp)
1662{
1663 struct l2cap_conn *conn = smp->conn;
1664 struct hci_dev *hdev = conn->hcon->hdev;
1665 u8 local_dist = 0, remote_dist = 0;
1666
d7a5a11d 1667 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
b5ae344d
JH
1668 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1669 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1670 }
1671
d7a5a11d 1672 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
b5ae344d
JH
1673 remote_dist |= SMP_DIST_ID_KEY;
1674
d7a5a11d 1675 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
b5ae344d
JH
1676 local_dist |= SMP_DIST_ID_KEY;
1677
1678 if (!rsp) {
1679 memset(req, 0, sizeof(*req));
1680
1681 req->init_key_dist = local_dist;
1682 req->resp_key_dist = remote_dist;
1683 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1684
1685 smp->remote_key_dist = remote_dist;
1686
1687 return;
1688 }
1689
1690 memset(rsp, 0, sizeof(*rsp));
1691
1692 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1693 rsp->init_key_dist = req->init_key_dist & remote_dist;
1694 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1695
1696 smp->remote_key_dist = rsp->init_key_dist;
1697}
1698
da85e5e5 1699static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1700{
3158c50c 1701 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
fc75cc86 1702 struct l2cap_chan *chan = conn->smp;
b3c6410b 1703 struct hci_dev *hdev = conn->hcon->hdev;
8aab4757 1704 struct smp_chan *smp;
c7262e71 1705 u8 key_size, auth, sec_level;
8aab4757 1706 int ret;
88ba43b6
AB
1707
1708 BT_DBG("conn %p", conn);
1709
c46b98be 1710 if (skb->len < sizeof(*req))
38e4a915 1711 return SMP_INVALID_PARAMS;
c46b98be 1712
40bef302 1713 if (conn->hcon->role != HCI_ROLE_SLAVE)
2b64d153
BG
1714 return SMP_CMD_NOTSUPP;
1715
fc75cc86 1716 if (!chan->data)
8aab4757 1717 smp = smp_chan_create(conn);
fc75cc86 1718 else
5d88cc73 1719 smp = chan->data;
8aab4757 1720
d08fd0e7
AE
1721 if (!smp)
1722 return SMP_UNSPECIFIED;
d26a2345 1723
c05b9339 1724 /* We didn't start the pairing, so match remote */
0edb14de 1725 auth = req->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 1726
d7a5a11d 1727 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
c05b9339 1728 (auth & SMP_AUTH_BONDING))
b3c6410b
JH
1729 return SMP_PAIRING_NOTSUPP;
1730
d7a5a11d 1731 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
903b71c7
JH
1732 return SMP_AUTH_REQUIREMENTS;
1733
1c1def09
VCG
1734 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1735 memcpy(&smp->preq[1], req, sizeof(*req));
3158c50c 1736 skb_pull(skb, sizeof(*req));
88ba43b6 1737
b5ae344d
JH
1738 /* SMP over BR/EDR requires special treatment */
1739 if (conn->hcon->type == ACL_LINK) {
1740 /* We must have a BR/EDR SC link */
08f63cc5 1741 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
b7cb93e5 1742 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
b5ae344d
JH
1743 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1744
1745 set_bit(SMP_FLAG_SC, &smp->flags);
1746
1747 build_bredr_pairing_cmd(smp, req, &rsp);
1748
1749 key_size = min(req->max_key_size, rsp.max_key_size);
1750 if (check_enc_key_size(conn, key_size))
1751 return SMP_ENC_KEY_SIZE;
1752
1753 /* Clear bits which are generated but not distributed */
1754 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1755
1756 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1757 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1758 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1759
1760 smp_distribute_keys(smp);
1761 return 0;
1762 }
1763
5e3d3d9b
JH
1764 build_pairing_cmd(conn, req, &rsp, auth);
1765
1766 if (rsp.auth_req & SMP_AUTH_SC)
1767 set_bit(SMP_FLAG_SC, &smp->flags);
1768
5be5e275 1769 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1afc2a1a
JH
1770 sec_level = BT_SECURITY_MEDIUM;
1771 else
1772 sec_level = authreq_to_seclevel(auth);
1773
c7262e71
JH
1774 if (sec_level > conn->hcon->pending_sec_level)
1775 conn->hcon->pending_sec_level = sec_level;
fdde0a26 1776
49c922bb 1777 /* If we need MITM check that it can be achieved */
2ed8f65c
JH
1778 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1779 u8 method;
1780
1781 method = get_auth_method(smp, conn->hcon->io_capability,
1782 req->io_capability);
1783 if (method == JUST_WORKS || method == JUST_CFM)
1784 return SMP_AUTH_REQUIREMENTS;
1785 }
1786
3158c50c
VCG
1787 key_size = min(req->max_key_size, rsp.max_key_size);
1788 if (check_enc_key_size(conn, key_size))
1789 return SMP_ENC_KEY_SIZE;
88ba43b6 1790
e84a6b13 1791 get_random_bytes(smp->prnd, sizeof(smp->prnd));
8aab4757 1792
1c1def09
VCG
1793 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1794 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
f01ead31 1795
3158c50c 1796 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
3b19146d
JH
1797
1798 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1799
1800 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1801 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1802 /* Clear bits which are generated but not distributed */
1803 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1804 /* Wait for Public Key from Initiating Device */
1805 return 0;
3b19146d 1806 }
da85e5e5 1807
983f9814
MH
1808 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1809
2b64d153
BG
1810 /* Request setup of TK */
1811 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1812 if (ret)
1813 return SMP_UNSPECIFIED;
1814
da85e5e5 1815 return 0;
88ba43b6
AB
1816}
1817
3b19146d
JH
1818static u8 sc_send_public_key(struct smp_chan *smp)
1819{
70157ef5
JH
1820 struct hci_dev *hdev = smp->conn->hcon->hdev;
1821
3b19146d
JH
1822 BT_DBG("");
1823
1a8bab4f 1824 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
33d0c030
MH
1825 struct l2cap_chan *chan = hdev->smp_data;
1826 struct smp_dev *smp_dev;
1827
1828 if (!chan || !chan->data)
1829 return SMP_UNSPECIFIED;
1830
1831 smp_dev = chan->data;
1832
1833 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1834 memcpy(smp->local_sk, smp_dev->local_sk, 32);
882fafad 1835 memcpy(smp->lr, smp_dev->local_rr, 16);
33d0c030
MH
1836
1837 if (smp_dev->debug_key)
1838 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1839
1840 goto done;
1841 }
1842
d7a5a11d 1843 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
70157ef5
JH
1844 BT_DBG("Using debug keys");
1845 memcpy(smp->local_pk, debug_pk, 64);
1846 memcpy(smp->local_sk, debug_sk, 32);
1847 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1848 } else {
1849 while (true) {
1850 /* Generate local key pair for Secure Connections */
1851 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1852 return SMP_UNSPECIFIED;
6c0dcc50 1853
70157ef5
JH
1854 /* This is unlikely, but we need to check that
1855 * we didn't accidentially generate a debug key.
1856 */
1857 if (memcmp(smp->local_sk, debug_sk, 32))
1858 break;
1859 }
6c0dcc50 1860 }
3b19146d 1861
33d0c030 1862done:
c7a3d57d 1863 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
8e4e2ee5 1864 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
c7a3d57d 1865 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
3b19146d
JH
1866
1867 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1868
1869 return 0;
1870}
1871
da85e5e5 1872static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1873{
3158c50c 1874 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
5d88cc73
JH
1875 struct l2cap_chan *chan = conn->smp;
1876 struct smp_chan *smp = chan->data;
0edb14de 1877 struct hci_dev *hdev = conn->hcon->hdev;
3a7dbfb8 1878 u8 key_size, auth;
7d24ddcc 1879 int ret;
88ba43b6
AB
1880
1881 BT_DBG("conn %p", conn);
1882
c46b98be 1883 if (skb->len < sizeof(*rsp))
38e4a915 1884 return SMP_INVALID_PARAMS;
c46b98be 1885
40bef302 1886 if (conn->hcon->role != HCI_ROLE_MASTER)
2b64d153
BG
1887 return SMP_CMD_NOTSUPP;
1888
3158c50c
VCG
1889 skb_pull(skb, sizeof(*rsp));
1890
1c1def09 1891 req = (void *) &smp->preq[1];
da85e5e5 1892
3158c50c
VCG
1893 key_size = min(req->max_key_size, rsp->max_key_size);
1894 if (check_enc_key_size(conn, key_size))
1895 return SMP_ENC_KEY_SIZE;
1896
0edb14de 1897 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 1898
d7a5a11d 1899 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
903b71c7
JH
1900 return SMP_AUTH_REQUIREMENTS;
1901
b5ae344d
JH
1902 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1903 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1904
1905 /* Update remote key distribution in case the remote cleared
1906 * some bits that we had enabled in our request.
1907 */
1908 smp->remote_key_dist &= rsp->resp_key_dist;
1909
1910 /* For BR/EDR this means we're done and can start phase 3 */
1911 if (conn->hcon->type == ACL_LINK) {
1912 /* Clear bits which are generated but not distributed */
1913 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1914 smp_distribute_keys(smp);
1915 return 0;
1916 }
1917
65668776
JH
1918 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1919 set_bit(SMP_FLAG_SC, &smp->flags);
d2eb9e10
JH
1920 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1921 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
65668776 1922
49c922bb 1923 /* If we need MITM check that it can be achieved */
2ed8f65c
JH
1924 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1925 u8 method;
1926
1927 method = get_auth_method(smp, req->io_capability,
1928 rsp->io_capability);
1929 if (method == JUST_WORKS || method == JUST_CFM)
1930 return SMP_AUTH_REQUIREMENTS;
1931 }
1932
e84a6b13 1933 get_random_bytes(smp->prnd, sizeof(smp->prnd));
7d24ddcc 1934
fdcc4bec
JH
1935 /* Update remote key distribution in case the remote cleared
1936 * some bits that we had enabled in our request.
1937 */
1938 smp->remote_key_dist &= rsp->resp_key_dist;
1939
3b19146d
JH
1940 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1941 /* Clear bits which are generated but not distributed */
1942 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1943 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1944 return sc_send_public_key(smp);
1945 }
1946
c05b9339 1947 auth |= req->auth_req;
2b64d153 1948
476585ec 1949 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
2b64d153
BG
1950 if (ret)
1951 return SMP_UNSPECIFIED;
1952
4a74d658 1953 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
2b64d153
BG
1954
1955 /* Can't compose response until we have been confirmed */
4a74d658 1956 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1cc61144 1957 return smp_confirm(smp);
da85e5e5
VCG
1958
1959 return 0;
88ba43b6
AB
1960}
1961
dcee2b32
JH
1962static u8 sc_check_confirm(struct smp_chan *smp)
1963{
1964 struct l2cap_conn *conn = smp->conn;
1965
1966 BT_DBG("");
1967
1968 /* Public Key exchange must happen before any other steps */
1969 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1970 return SMP_UNSPECIFIED;
1971
38606f14
JH
1972 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1973 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1974
dcee2b32
JH
1975 if (conn->hcon->out) {
1976 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1977 smp->prnd);
1978 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1979 }
1980
1981 return 0;
1982}
1983
da85e5e5 1984static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 1985{
5d88cc73
JH
1986 struct l2cap_chan *chan = conn->smp;
1987 struct smp_chan *smp = chan->data;
7d24ddcc 1988
88ba43b6
AB
1989 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1990
c46b98be 1991 if (skb->len < sizeof(smp->pcnf))
38e4a915 1992 return SMP_INVALID_PARAMS;
c46b98be 1993
1c1def09
VCG
1994 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1995 skb_pull(skb, sizeof(smp->pcnf));
88ba43b6 1996
dcee2b32
JH
1997 if (test_bit(SMP_FLAG_SC, &smp->flags))
1998 return sc_check_confirm(smp);
1999
b28b4943 2000 if (conn->hcon->out) {
943a732a
JH
2001 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2002 smp->prnd);
b28b4943
JH
2003 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2004 return 0;
2005 }
2006
2007 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1cc61144 2008 return smp_confirm(smp);
983f9814
MH
2009
2010 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
da85e5e5
VCG
2011
2012 return 0;
88ba43b6
AB
2013}
2014
da85e5e5 2015static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 2016{
5d88cc73
JH
2017 struct l2cap_chan *chan = conn->smp;
2018 struct smp_chan *smp = chan->data;
191dc7fe
JH
2019 struct hci_conn *hcon = conn->hcon;
2020 u8 *pkax, *pkbx, *na, *nb;
2021 u32 passkey;
2022 int err;
7d24ddcc 2023
8aab4757 2024 BT_DBG("conn %p", conn);
3158c50c 2025
c46b98be 2026 if (skb->len < sizeof(smp->rrnd))
38e4a915 2027 return SMP_INVALID_PARAMS;
c46b98be 2028
943a732a 2029 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
8aab4757 2030 skb_pull(skb, sizeof(smp->rrnd));
e7e62c85 2031
191dc7fe
JH
2032 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2033 return smp_random(smp);
2034
580039e8
JH
2035 if (hcon->out) {
2036 pkax = smp->local_pk;
2037 pkbx = smp->remote_pk;
2038 na = smp->prnd;
2039 nb = smp->rrnd;
2040 } else {
2041 pkax = smp->remote_pk;
2042 pkbx = smp->local_pk;
2043 na = smp->rrnd;
2044 nb = smp->prnd;
2045 }
2046
a29b0733
JH
2047 if (smp->method == REQ_OOB) {
2048 if (!hcon->out)
2049 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2050 sizeof(smp->prnd), smp->prnd);
2051 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2052 goto mackey_and_ltk;
2053 }
2054
38606f14
JH
2055 /* Passkey entry has special treatment */
2056 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2057 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2058
191dc7fe
JH
2059 if (hcon->out) {
2060 u8 cfm[16];
2061
2062 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2063 smp->rrnd, 0, cfm);
2064 if (err)
2065 return SMP_UNSPECIFIED;
2066
2067 if (memcmp(smp->pcnf, cfm, 16))
2068 return SMP_CONFIRM_FAILED;
191dc7fe
JH
2069 } else {
2070 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2071 smp->prnd);
2072 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
191dc7fe
JH
2073 }
2074
a29b0733 2075mackey_and_ltk:
760b018b
JH
2076 /* Generate MacKey and LTK */
2077 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2078 if (err)
2079 return SMP_UNSPECIFIED;
2080
a29b0733 2081 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
dddd3059 2082 if (hcon->out) {
38606f14 2083 sc_dhkey_check(smp);
dddd3059
JH
2084 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2085 }
2086 return 0;
2087 }
2088
38606f14
JH
2089 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
2090 if (err)
2091 return SMP_UNSPECIFIED;
2092
2093 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2094 hcon->dst_type, passkey, 0);
191dc7fe
JH
2095 if (err)
2096 return SMP_UNSPECIFIED;
2097
38606f14
JH
2098 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2099
191dc7fe 2100 return 0;
88ba43b6
AB
2101}
2102
f81cd823 2103static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
988c5997 2104{
c9839a11 2105 struct smp_ltk *key;
988c5997
VCG
2106 struct hci_conn *hcon = conn->hcon;
2107
f3a73d97 2108 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
988c5997 2109 if (!key)
f81cd823 2110 return false;
988c5997 2111
a6f7833c 2112 if (smp_ltk_sec_level(key) < sec_level)
f81cd823 2113 return false;
4dab7864 2114
51a8efd7 2115 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
f81cd823 2116 return true;
988c5997 2117
c9839a11
VCG
2118 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2119 hcon->enc_key_size = key->enc_size;
988c5997 2120
fe59a05f
JH
2121 /* We never store STKs for master role, so clear this flag */
2122 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2123
f81cd823 2124 return true;
988c5997 2125}
f1560463 2126
35dc6f83
JH
2127bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2128 enum smp_key_pref key_pref)
854f4727
JH
2129{
2130 if (sec_level == BT_SECURITY_LOW)
2131 return true;
2132
35dc6f83
JH
2133 /* If we're encrypted with an STK but the caller prefers using
2134 * LTK claim insufficient security. This way we allow the
2135 * connection to be re-encrypted with an LTK, even if the LTK
2136 * provides the same level of security. Only exception is if we
2137 * don't have an LTK (e.g. because of key distribution bits).
9ab65d60 2138 */
35dc6f83
JH
2139 if (key_pref == SMP_USE_LTK &&
2140 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
f3a73d97 2141 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
9ab65d60
JH
2142 return false;
2143
854f4727
JH
2144 if (hcon->sec_level >= sec_level)
2145 return true;
2146
2147 return false;
2148}
2149
da85e5e5 2150static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6
AB
2151{
2152 struct smp_cmd_security_req *rp = (void *) skb->data;
2153 struct smp_cmd_pairing cp;
f1cb9af5 2154 struct hci_conn *hcon = conn->hcon;
0edb14de 2155 struct hci_dev *hdev = hcon->hdev;
8aab4757 2156 struct smp_chan *smp;
c05b9339 2157 u8 sec_level, auth;
88ba43b6
AB
2158
2159 BT_DBG("conn %p", conn);
2160
c46b98be 2161 if (skb->len < sizeof(*rp))
38e4a915 2162 return SMP_INVALID_PARAMS;
c46b98be 2163
40bef302 2164 if (hcon->role != HCI_ROLE_MASTER)
86ca9eac
JH
2165 return SMP_CMD_NOTSUPP;
2166
0edb14de 2167 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
c05b9339 2168
d7a5a11d 2169 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
903b71c7
JH
2170 return SMP_AUTH_REQUIREMENTS;
2171
5be5e275 2172 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1afc2a1a
JH
2173 sec_level = BT_SECURITY_MEDIUM;
2174 else
2175 sec_level = authreq_to_seclevel(auth);
2176
35dc6f83 2177 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
854f4727
JH
2178 return 0;
2179
c7262e71
JH
2180 if (sec_level > hcon->pending_sec_level)
2181 hcon->pending_sec_level = sec_level;
feb45eb5 2182
4dab7864 2183 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
988c5997
VCG
2184 return 0;
2185
8aab4757 2186 smp = smp_chan_create(conn);
c29d2444
JH
2187 if (!smp)
2188 return SMP_UNSPECIFIED;
d26a2345 2189
d7a5a11d 2190 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
c05b9339 2191 (auth & SMP_AUTH_BONDING))
616d55be
JH
2192 return SMP_PAIRING_NOTSUPP;
2193
88ba43b6 2194 skb_pull(skb, sizeof(*rp));
88ba43b6 2195
da85e5e5 2196 memset(&cp, 0, sizeof(cp));
c05b9339 2197 build_pairing_cmd(conn, &cp, NULL, auth);
88ba43b6 2198
1c1def09
VCG
2199 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2200 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 2201
88ba43b6 2202 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
b28b4943 2203 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
f1cb9af5 2204
da85e5e5 2205 return 0;
88ba43b6
AB
2206}
2207
cc110922 2208int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
eb492e01 2209{
cc110922 2210 struct l2cap_conn *conn = hcon->l2cap_data;
c68b7f12 2211 struct l2cap_chan *chan;
0a66cf20 2212 struct smp_chan *smp;
2b64d153 2213 __u8 authreq;
fc75cc86 2214 int ret;
eb492e01 2215
3a0259bb
VCG
2216 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2217
0a66cf20
JH
2218 /* This may be NULL if there's an unexpected disconnection */
2219 if (!conn)
2220 return 1;
2221
c68b7f12
JH
2222 chan = conn->smp;
2223
d7a5a11d 2224 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
2e65c9d2
AG
2225 return 1;
2226
35dc6f83 2227 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
eb492e01 2228 return 1;
f1cb9af5 2229
c7262e71
JH
2230 if (sec_level > hcon->pending_sec_level)
2231 hcon->pending_sec_level = sec_level;
2232
40bef302 2233 if (hcon->role == HCI_ROLE_MASTER)
c7262e71
JH
2234 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2235 return 0;
d26a2345 2236
fc75cc86
JH
2237 l2cap_chan_lock(chan);
2238
2239 /* If SMP is already in progress ignore this request */
2240 if (chan->data) {
2241 ret = 0;
2242 goto unlock;
2243 }
d26a2345 2244
8aab4757 2245 smp = smp_chan_create(conn);
fc75cc86
JH
2246 if (!smp) {
2247 ret = 1;
2248 goto unlock;
2249 }
2b64d153
BG
2250
2251 authreq = seclevel_to_authreq(sec_level);
d26a2345 2252
d7a5a11d 2253 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
d2eb9e10
JH
2254 authreq |= SMP_AUTH_SC;
2255
79897d20
JH
2256 /* Require MITM if IO Capability allows or the security level
2257 * requires it.
2e233644 2258 */
79897d20 2259 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
c7262e71 2260 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
2e233644
JH
2261 authreq |= SMP_AUTH_MITM;
2262
40bef302 2263 if (hcon->role == HCI_ROLE_MASTER) {
d26a2345 2264 struct smp_cmd_pairing cp;
f01ead31 2265
2b64d153 2266 build_pairing_cmd(conn, &cp, NULL, authreq);
1c1def09
VCG
2267 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2268 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 2269
eb492e01 2270 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
b28b4943 2271 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
eb492e01
AB
2272 } else {
2273 struct smp_cmd_security_req cp;
2b64d153 2274 cp.auth_req = authreq;
eb492e01 2275 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
b28b4943 2276 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
eb492e01
AB
2277 }
2278
4a74d658 2279 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
fc75cc86 2280 ret = 0;
edca792c 2281
fc75cc86
JH
2282unlock:
2283 l2cap_chan_unlock(chan);
2284 return ret;
eb492e01
AB
2285}
2286
7034b911
VCG
2287static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2288{
16b90839 2289 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
5d88cc73
JH
2290 struct l2cap_chan *chan = conn->smp;
2291 struct smp_chan *smp = chan->data;
16b90839 2292
c46b98be
JH
2293 BT_DBG("conn %p", conn);
2294
2295 if (skb->len < sizeof(*rp))
38e4a915 2296 return SMP_INVALID_PARAMS;
c46b98be 2297
b28b4943 2298 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
6131ddc8 2299
16b90839
VCG
2300 skb_pull(skb, sizeof(*rp));
2301
1c1def09 2302 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
16b90839 2303
7034b911
VCG
2304 return 0;
2305}
2306
2307static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2308{
16b90839 2309 struct smp_cmd_master_ident *rp = (void *) skb->data;
5d88cc73
JH
2310 struct l2cap_chan *chan = conn->smp;
2311 struct smp_chan *smp = chan->data;
c9839a11
VCG
2312 struct hci_dev *hdev = conn->hcon->hdev;
2313 struct hci_conn *hcon = conn->hcon;
23d0e128 2314 struct smp_ltk *ltk;
c9839a11 2315 u8 authenticated;
16b90839 2316
c46b98be
JH
2317 BT_DBG("conn %p", conn);
2318
2319 if (skb->len < sizeof(*rp))
38e4a915 2320 return SMP_INVALID_PARAMS;
c46b98be 2321
9747a9f3
JH
2322 /* Mark the information as received */
2323 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2324
b28b4943
JH
2325 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2326 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
196332f5
JH
2327 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2328 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
b28b4943 2329
16b90839 2330 skb_pull(skb, sizeof(*rp));
7034b911 2331
ce39fb4e 2332 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
2ceba539 2333 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
23d0e128
JH
2334 authenticated, smp->tk, smp->enc_key_size,
2335 rp->ediv, rp->rand);
2336 smp->ltk = ltk;
c6e81e9a 2337 if (!(smp->remote_key_dist & KEY_DIST_MASK))
d6268e86 2338 smp_distribute_keys(smp);
7034b911
VCG
2339
2340 return 0;
2341}
2342
fd349c02
JH
2343static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2344{
2345 struct smp_cmd_ident_info *info = (void *) skb->data;
5d88cc73
JH
2346 struct l2cap_chan *chan = conn->smp;
2347 struct smp_chan *smp = chan->data;
fd349c02
JH
2348
2349 BT_DBG("");
2350
2351 if (skb->len < sizeof(*info))
38e4a915 2352 return SMP_INVALID_PARAMS;
fd349c02 2353
b28b4943 2354 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
6131ddc8 2355
fd349c02
JH
2356 skb_pull(skb, sizeof(*info));
2357
2358 memcpy(smp->irk, info->irk, 16);
2359
2360 return 0;
2361}
2362
2363static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2364 struct sk_buff *skb)
2365{
2366 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
5d88cc73
JH
2367 struct l2cap_chan *chan = conn->smp;
2368 struct smp_chan *smp = chan->data;
fd349c02
JH
2369 struct hci_conn *hcon = conn->hcon;
2370 bdaddr_t rpa;
2371
2372 BT_DBG("");
2373
2374 if (skb->len < sizeof(*info))
38e4a915 2375 return SMP_INVALID_PARAMS;
fd349c02 2376
9747a9f3
JH
2377 /* Mark the information as received */
2378 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2379
b28b4943
JH
2380 if (smp->remote_key_dist & SMP_DIST_SIGN)
2381 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2382
fd349c02
JH
2383 skb_pull(skb, sizeof(*info));
2384
a9a58f86
JH
2385 /* Strictly speaking the Core Specification (4.1) allows sending
2386 * an empty address which would force us to rely on just the IRK
2387 * as "identity information". However, since such
2388 * implementations are not known of and in order to not over
2389 * complicate our implementation, simply pretend that we never
2390 * received an IRK for such a device.
e12af489
JH
2391 *
2392 * The Identity Address must also be a Static Random or Public
2393 * Address, which hci_is_identity_address() checks for.
a9a58f86 2394 */
e12af489
JH
2395 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2396 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
a9a58f86 2397 BT_ERR("Ignoring IRK with no identity address");
31dd624e 2398 goto distribute;
a9a58f86
JH
2399 }
2400
fd349c02
JH
2401 bacpy(&smp->id_addr, &info->bdaddr);
2402 smp->id_addr_type = info->addr_type;
2403
2404 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2405 bacpy(&rpa, &hcon->dst);
2406 else
2407 bacpy(&rpa, BDADDR_ANY);
2408
23d0e128
JH
2409 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2410 smp->id_addr_type, smp->irk, &rpa);
fd349c02 2411
31dd624e 2412distribute:
c6e81e9a
JH
2413 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2414 smp_distribute_keys(smp);
fd349c02
JH
2415
2416 return 0;
2417}
2418
7ee4ea36
MH
2419static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2420{
2421 struct smp_cmd_sign_info *rp = (void *) skb->data;
5d88cc73
JH
2422 struct l2cap_chan *chan = conn->smp;
2423 struct smp_chan *smp = chan->data;
7ee4ea36
MH
2424 struct smp_csrk *csrk;
2425
2426 BT_DBG("conn %p", conn);
2427
2428 if (skb->len < sizeof(*rp))
38e4a915 2429 return SMP_INVALID_PARAMS;
7ee4ea36 2430
7ee4ea36
MH
2431 /* Mark the information as received */
2432 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2433
2434 skb_pull(skb, sizeof(*rp));
2435
7ee4ea36
MH
2436 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2437 if (csrk) {
4cd3928a
JH
2438 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2439 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2440 else
2441 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
7ee4ea36
MH
2442 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2443 }
2444 smp->csrk = csrk;
d6268e86 2445 smp_distribute_keys(smp);
7ee4ea36
MH
2446
2447 return 0;
2448}
2449
5e3d3d9b
JH
2450static u8 sc_select_method(struct smp_chan *smp)
2451{
2452 struct l2cap_conn *conn = smp->conn;
2453 struct hci_conn *hcon = conn->hcon;
2454 struct smp_cmd_pairing *local, *remote;
2455 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2456
1a8bab4f
JH
2457 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2458 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
a29b0733
JH
2459 return REQ_OOB;
2460
5e3d3d9b
JH
2461 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2462 * which are needed as inputs to some crypto functions. To get
2463 * the "struct smp_cmd_pairing" from them we need to skip the
2464 * first byte which contains the opcode.
2465 */
2466 if (hcon->out) {
2467 local = (void *) &smp->preq[1];
2468 remote = (void *) &smp->prsp[1];
2469 } else {
2470 local = (void *) &smp->prsp[1];
2471 remote = (void *) &smp->preq[1];
2472 }
2473
2474 local_io = local->io_capability;
2475 remote_io = remote->io_capability;
2476
2477 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2478 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2479
2480 /* If either side wants MITM, look up the method from the table,
2481 * otherwise use JUST WORKS.
2482 */
2483 if (local_mitm || remote_mitm)
2484 method = get_auth_method(smp, local_io, remote_io);
2485 else
2486 method = JUST_WORKS;
2487
2488 /* Don't confirm locally initiated pairing attempts */
2489 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2490 method = JUST_WORKS;
2491
2492 return method;
2493}
2494
d8f8edbe
JH
2495static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2496{
2497 struct smp_cmd_public_key *key = (void *) skb->data;
2498 struct hci_conn *hcon = conn->hcon;
2499 struct l2cap_chan *chan = conn->smp;
2500 struct smp_chan *smp = chan->data;
5e3d3d9b 2501 struct hci_dev *hdev = hcon->hdev;
cbbbe3e2 2502 struct smp_cmd_pairing_confirm cfm;
d8f8edbe
JH
2503 int err;
2504
2505 BT_DBG("conn %p", conn);
2506
2507 if (skb->len < sizeof(*key))
2508 return SMP_INVALID_PARAMS;
2509
2510 memcpy(smp->remote_pk, key, 64);
2511
2512 /* Non-initiating device sends its public key after receiving
2513 * the key from the initiating device.
2514 */
2515 if (!hcon->out) {
2516 err = sc_send_public_key(smp);
2517 if (err)
2518 return err;
2519 }
2520
c7a3d57d
JH
2521 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2522 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
d8f8edbe
JH
2523
2524 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2525 return SMP_UNSPECIFIED;
2526
c7a3d57d 2527 SMP_DBG("DHKey %32phN", smp->dhkey);
d8f8edbe
JH
2528
2529 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2530
5e3d3d9b
JH
2531 smp->method = sc_select_method(smp);
2532
2533 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2534
2535 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2536 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2537 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2538 else
2539 hcon->pending_sec_level = BT_SECURITY_FIPS;
2540
aeb7d461
JH
2541 if (!memcmp(debug_pk, smp->remote_pk, 64))
2542 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2543
38606f14
JH
2544 if (smp->method == DSP_PASSKEY) {
2545 get_random_bytes(&hcon->passkey_notify,
2546 sizeof(hcon->passkey_notify));
2547 hcon->passkey_notify %= 1000000;
2548 hcon->passkey_entered = 0;
2549 smp->passkey_round = 0;
2550 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2551 hcon->dst_type,
2552 hcon->passkey_notify,
2553 hcon->passkey_entered))
2554 return SMP_UNSPECIFIED;
2555 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2556 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2557 }
2558
a29b0733
JH
2559 if (smp->method == REQ_OOB) {
2560 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2561 smp->rr, 0, cfm.confirm_val);
2562 if (err)
2563 return SMP_UNSPECIFIED;
2564
2565 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2566 return SMP_CONFIRM_FAILED;
2567
2568 if (hcon->out)
2569 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2570 sizeof(smp->prnd), smp->prnd);
2571
2572 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2573
2574 return 0;
2575 }
2576
38606f14
JH
2577 if (hcon->out)
2578 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2579
2580 if (smp->method == REQ_PASSKEY) {
2581 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2582 hcon->dst_type))
2583 return SMP_UNSPECIFIED;
2584 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2585 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2586 return 0;
2587 }
2588
cbbbe3e2
JH
2589 /* The Initiating device waits for the non-initiating device to
2590 * send the confirm value.
2591 */
2592 if (conn->hcon->out)
2593 return 0;
2594
2595 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2596 0, cfm.confirm_val);
2597 if (err)
2598 return SMP_UNSPECIFIED;
2599
2600 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2601 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2602
d8f8edbe
JH
2603 return 0;
2604}
2605
6433a9a2
JH
2606static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2607{
2608 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2609 struct l2cap_chan *chan = conn->smp;
2610 struct hci_conn *hcon = conn->hcon;
2611 struct smp_chan *smp = chan->data;
2612 u8 a[7], b[7], *local_addr, *remote_addr;
2613 u8 io_cap[3], r[16], e[16];
2614 int err;
2615
2616 BT_DBG("conn %p", conn);
2617
2618 if (skb->len < sizeof(*check))
2619 return SMP_INVALID_PARAMS;
2620
2621 memcpy(a, &hcon->init_addr, 6);
2622 memcpy(b, &hcon->resp_addr, 6);
2623 a[6] = hcon->init_addr_type;
2624 b[6] = hcon->resp_addr_type;
2625
2626 if (hcon->out) {
2627 local_addr = a;
2628 remote_addr = b;
2629 memcpy(io_cap, &smp->prsp[1], 3);
2630 } else {
2631 local_addr = b;
2632 remote_addr = a;
2633 memcpy(io_cap, &smp->preq[1], 3);
2634 }
2635
2636 memset(r, 0, sizeof(r));
2637
38606f14
JH
2638 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2639 put_unaligned_le32(hcon->passkey_notify, r);
882fafad
JH
2640 else if (smp->method == REQ_OOB)
2641 memcpy(r, smp->lr, 16);
38606f14 2642
6433a9a2
JH
2643 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2644 io_cap, remote_addr, local_addr, e);
2645 if (err)
2646 return SMP_UNSPECIFIED;
2647
2648 if (memcmp(check->e, e, 16))
2649 return SMP_DHKEY_CHECK_FAILED;
2650
d3e54a87
JH
2651 if (!hcon->out) {
2652 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2653 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2654 return 0;
2655 }
d378a2d7 2656
d3e54a87
JH
2657 /* Slave sends DHKey check as response to master */
2658 sc_dhkey_check(smp);
2659 }
d378a2d7 2660
d3e54a87 2661 sc_add_ltk(smp);
6433a9a2
JH
2662
2663 if (hcon->out) {
2664 hci_le_start_enc(hcon, 0, 0, smp->tk);
2665 hcon->enc_key_size = smp->enc_key_size;
2666 }
2667
2668 return 0;
2669}
2670
1408bb6e
JH
2671static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2672 struct sk_buff *skb)
2673{
2674 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2675
2676 BT_DBG("value 0x%02x", kp->value);
2677
2678 return 0;
2679}
2680
4befb867 2681static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
eb492e01 2682{
5d88cc73 2683 struct l2cap_conn *conn = chan->conn;
7b9899db 2684 struct hci_conn *hcon = conn->hcon;
b28b4943 2685 struct smp_chan *smp;
92381f5c 2686 __u8 code, reason;
eb492e01
AB
2687 int err = 0;
2688
8ae9b984 2689 if (skb->len < 1)
92381f5c 2690 return -EILSEQ;
92381f5c 2691
d7a5a11d 2692 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
2e65c9d2
AG
2693 reason = SMP_PAIRING_NOTSUPP;
2694 goto done;
2695 }
2696
92381f5c 2697 code = skb->data[0];
eb492e01
AB
2698 skb_pull(skb, sizeof(code));
2699
b28b4943
JH
2700 smp = chan->data;
2701
2702 if (code > SMP_CMD_MAX)
2703 goto drop;
2704
24bd0bd9 2705 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
b28b4943
JH
2706 goto drop;
2707
2708 /* If we don't have a context the only allowed commands are
2709 * pairing request and security request.
8cf9fa12 2710 */
b28b4943
JH
2711 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2712 goto drop;
8cf9fa12 2713
eb492e01
AB
2714 switch (code) {
2715 case SMP_CMD_PAIRING_REQ:
da85e5e5 2716 reason = smp_cmd_pairing_req(conn, skb);
eb492e01
AB
2717 break;
2718
2719 case SMP_CMD_PAIRING_FAIL:
84794e11 2720 smp_failure(conn, 0);
da85e5e5 2721 err = -EPERM;
eb492e01
AB
2722 break;
2723
2724 case SMP_CMD_PAIRING_RSP:
da85e5e5 2725 reason = smp_cmd_pairing_rsp(conn, skb);
88ba43b6
AB
2726 break;
2727
2728 case SMP_CMD_SECURITY_REQ:
da85e5e5 2729 reason = smp_cmd_security_req(conn, skb);
88ba43b6
AB
2730 break;
2731
eb492e01 2732 case SMP_CMD_PAIRING_CONFIRM:
da85e5e5 2733 reason = smp_cmd_pairing_confirm(conn, skb);
88ba43b6
AB
2734 break;
2735
eb492e01 2736 case SMP_CMD_PAIRING_RANDOM:
da85e5e5 2737 reason = smp_cmd_pairing_random(conn, skb);
88ba43b6
AB
2738 break;
2739
eb492e01 2740 case SMP_CMD_ENCRYPT_INFO:
7034b911
VCG
2741 reason = smp_cmd_encrypt_info(conn, skb);
2742 break;
2743
eb492e01 2744 case SMP_CMD_MASTER_IDENT:
7034b911
VCG
2745 reason = smp_cmd_master_ident(conn, skb);
2746 break;
2747
eb492e01 2748 case SMP_CMD_IDENT_INFO:
fd349c02
JH
2749 reason = smp_cmd_ident_info(conn, skb);
2750 break;
2751
eb492e01 2752 case SMP_CMD_IDENT_ADDR_INFO:
fd349c02
JH
2753 reason = smp_cmd_ident_addr_info(conn, skb);
2754 break;
2755
eb492e01 2756 case SMP_CMD_SIGN_INFO:
7ee4ea36 2757 reason = smp_cmd_sign_info(conn, skb);
7034b911
VCG
2758 break;
2759
d8f8edbe
JH
2760 case SMP_CMD_PUBLIC_KEY:
2761 reason = smp_cmd_public_key(conn, skb);
2762 break;
2763
6433a9a2
JH
2764 case SMP_CMD_DHKEY_CHECK:
2765 reason = smp_cmd_dhkey_check(conn, skb);
2766 break;
2767
1408bb6e
JH
2768 case SMP_CMD_KEYPRESS_NOTIFY:
2769 reason = smp_cmd_keypress_notify(conn, skb);
2770 break;
2771
eb492e01
AB
2772 default:
2773 BT_DBG("Unknown command code 0x%2.2x", code);
eb492e01 2774 reason = SMP_CMD_NOTSUPP;
3a0259bb 2775 goto done;
eb492e01
AB
2776 }
2777
3a0259bb 2778done:
9b7b18ef
JH
2779 if (!err) {
2780 if (reason)
2781 smp_failure(conn, reason);
8ae9b984 2782 kfree_skb(skb);
9b7b18ef
JH
2783 }
2784
eb492e01 2785 return err;
b28b4943
JH
2786
2787drop:
2788 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2789 code, &hcon->dst);
2790 kfree_skb(skb);
2791 return 0;
eb492e01 2792}
7034b911 2793
70db83c4
JH
2794static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2795{
2796 struct l2cap_conn *conn = chan->conn;
2797
2798 BT_DBG("chan %p", chan);
2799
fc75cc86 2800 if (chan->data)
5d88cc73 2801 smp_chan_destroy(conn);
5d88cc73 2802
70db83c4
JH
2803 conn->smp = NULL;
2804 l2cap_chan_put(chan);
2805}
2806
b5ae344d
JH
2807static void bredr_pairing(struct l2cap_chan *chan)
2808{
2809 struct l2cap_conn *conn = chan->conn;
2810 struct hci_conn *hcon = conn->hcon;
2811 struct hci_dev *hdev = hcon->hdev;
2812 struct smp_cmd_pairing req;
2813 struct smp_chan *smp;
2814
2815 BT_DBG("chan %p", chan);
2816
2817 /* Only new pairings are interesting */
2818 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2819 return;
2820
2821 /* Don't bother if we're not encrypted */
2822 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2823 return;
2824
2825 /* Only master may initiate SMP over BR/EDR */
2826 if (hcon->role != HCI_ROLE_MASTER)
2827 return;
2828
2829 /* Secure Connections support must be enabled */
d7a5a11d 2830 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
b5ae344d
JH
2831 return;
2832
2833 /* BR/EDR must use Secure Connections for SMP */
2834 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
b7cb93e5 2835 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
b5ae344d
JH
2836 return;
2837
2838 /* If our LE support is not enabled don't do anything */
d7a5a11d 2839 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
b5ae344d
JH
2840 return;
2841
2842 /* Don't bother if remote LE support is not enabled */
2843 if (!lmp_host_le_capable(hcon))
2844 return;
2845
2846 /* Remote must support SMP fixed chan for BR/EDR */
2847 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2848 return;
2849
2850 /* Don't bother if SMP is already ongoing */
2851 if (chan->data)
2852 return;
2853
2854 smp = smp_chan_create(conn);
2855 if (!smp) {
2856 BT_ERR("%s unable to create SMP context for BR/EDR",
2857 hdev->name);
2858 return;
2859 }
2860
2861 set_bit(SMP_FLAG_SC, &smp->flags);
2862
2863 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2864
2865 /* Prepare and send the BR/EDR SMP Pairing Request */
2866 build_bredr_pairing_cmd(smp, &req, NULL);
2867
2868 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2869 memcpy(&smp->preq[1], &req, sizeof(req));
2870
2871 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2872 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2873}
2874
44f1a7ab
JH
2875static void smp_resume_cb(struct l2cap_chan *chan)
2876{
b68fda68 2877 struct smp_chan *smp = chan->data;
44f1a7ab
JH
2878 struct l2cap_conn *conn = chan->conn;
2879 struct hci_conn *hcon = conn->hcon;
2880
2881 BT_DBG("chan %p", chan);
2882
b5ae344d
JH
2883 if (hcon->type == ACL_LINK) {
2884 bredr_pairing(chan);
ef8efe4b 2885 return;
b5ae344d 2886 }
ef8efe4b 2887
86d1407c
JH
2888 if (!smp)
2889 return;
b68fda68 2890
84bc0db5
JH
2891 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2892 return;
2893
86d1407c
JH
2894 cancel_delayed_work(&smp->security_timer);
2895
d6268e86 2896 smp_distribute_keys(smp);
44f1a7ab
JH
2897}
2898
70db83c4
JH
2899static void smp_ready_cb(struct l2cap_chan *chan)
2900{
2901 struct l2cap_conn *conn = chan->conn;
b5ae344d 2902 struct hci_conn *hcon = conn->hcon;
70db83c4
JH
2903
2904 BT_DBG("chan %p", chan);
2905
2906 conn->smp = chan;
2907 l2cap_chan_hold(chan);
b5ae344d
JH
2908
2909 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2910 bredr_pairing(chan);
70db83c4
JH
2911}
2912
4befb867
JH
2913static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2914{
2915 int err;
2916
2917 BT_DBG("chan %p", chan);
2918
2919 err = smp_sig_channel(chan, skb);
2920 if (err) {
b68fda68 2921 struct smp_chan *smp = chan->data;
4befb867 2922
b68fda68
JH
2923 if (smp)
2924 cancel_delayed_work_sync(&smp->security_timer);
4befb867 2925
1e91c29e 2926 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
4befb867
JH
2927 }
2928
2929 return err;
2930}
2931
70db83c4
JH
2932static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2933 unsigned long hdr_len,
2934 unsigned long len, int nb)
2935{
2936 struct sk_buff *skb;
2937
2938 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2939 if (!skb)
2940 return ERR_PTR(-ENOMEM);
2941
2942 skb->priority = HCI_PRIO_MAX;
2943 bt_cb(skb)->chan = chan;
2944
2945 return skb;
2946}
2947
2948static const struct l2cap_ops smp_chan_ops = {
2949 .name = "Security Manager",
2950 .ready = smp_ready_cb,
5d88cc73 2951 .recv = smp_recv_cb,
70db83c4
JH
2952 .alloc_skb = smp_alloc_skb_cb,
2953 .teardown = smp_teardown_cb,
44f1a7ab 2954 .resume = smp_resume_cb,
70db83c4
JH
2955
2956 .new_connection = l2cap_chan_no_new_connection,
70db83c4
JH
2957 .state_change = l2cap_chan_no_state_change,
2958 .close = l2cap_chan_no_close,
2959 .defer = l2cap_chan_no_defer,
2960 .suspend = l2cap_chan_no_suspend,
70db83c4
JH
2961 .set_shutdown = l2cap_chan_no_set_shutdown,
2962 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
70db83c4
JH
2963};
2964
2965static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2966{
2967 struct l2cap_chan *chan;
2968
2969 BT_DBG("pchan %p", pchan);
2970
2971 chan = l2cap_chan_create();
2972 if (!chan)
2973 return NULL;
2974
2975 chan->chan_type = pchan->chan_type;
2976 chan->ops = &smp_chan_ops;
2977 chan->scid = pchan->scid;
2978 chan->dcid = chan->scid;
2979 chan->imtu = pchan->imtu;
2980 chan->omtu = pchan->omtu;
2981 chan->mode = pchan->mode;
2982
abe84903
JH
2983 /* Other L2CAP channels may request SMP routines in order to
2984 * change the security level. This means that the SMP channel
2985 * lock must be considered in its own category to avoid lockdep
2986 * warnings.
2987 */
2988 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2989
70db83c4
JH
2990 BT_DBG("created chan %p", chan);
2991
2992 return chan;
2993}
2994
2995static const struct l2cap_ops smp_root_chan_ops = {
2996 .name = "Security Manager Root",
2997 .new_connection = smp_new_conn_cb,
2998
2999 /* None of these are implemented for the root channel */
3000 .close = l2cap_chan_no_close,
3001 .alloc_skb = l2cap_chan_no_alloc_skb,
3002 .recv = l2cap_chan_no_recv,
3003 .state_change = l2cap_chan_no_state_change,
3004 .teardown = l2cap_chan_no_teardown,
3005 .ready = l2cap_chan_no_ready,
3006 .defer = l2cap_chan_no_defer,
3007 .suspend = l2cap_chan_no_suspend,
3008 .resume = l2cap_chan_no_resume,
3009 .set_shutdown = l2cap_chan_no_set_shutdown,
3010 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
70db83c4
JH
3011};
3012
ef8efe4b 3013static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
711eafe3 3014{
70db83c4 3015 struct l2cap_chan *chan;
88a479d9
MH
3016 struct smp_dev *smp;
3017 struct crypto_blkcipher *tfm_aes;
6e2dc6d1 3018 struct crypto_hash *tfm_cmac;
70db83c4 3019
ef8efe4b 3020 if (cid == L2CAP_CID_SMP_BREDR) {
88a479d9 3021 smp = NULL;
ef8efe4b
JH
3022 goto create_chan;
3023 }
711eafe3 3024
88a479d9
MH
3025 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3026 if (!smp)
3027 return ERR_PTR(-ENOMEM);
3028
3029 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
defce9e8 3030 if (IS_ERR(tfm_aes)) {
88a479d9
MH
3031 BT_ERR("Unable to create ECB crypto context");
3032 kzfree(smp);
fe700771 3033 return ERR_CAST(tfm_aes);
711eafe3
JH
3034 }
3035
6e2dc6d1
MH
3036 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3037 if (IS_ERR(tfm_cmac)) {
3038 BT_ERR("Unable to create CMAC crypto context");
3039 crypto_free_blkcipher(tfm_aes);
3040 kzfree(smp);
3041 return ERR_CAST(tfm_cmac);
3042 }
3043
88a479d9 3044 smp->tfm_aes = tfm_aes;
6e2dc6d1 3045 smp->tfm_cmac = tfm_cmac;
88a479d9 3046
ef8efe4b 3047create_chan:
70db83c4
JH
3048 chan = l2cap_chan_create();
3049 if (!chan) {
88a479d9 3050 crypto_free_blkcipher(smp->tfm_aes);
6e2dc6d1 3051 crypto_free_hash(smp->tfm_cmac);
88a479d9 3052 kzfree(smp);
ef8efe4b 3053 return ERR_PTR(-ENOMEM);
70db83c4
JH
3054 }
3055
88a479d9 3056 chan->data = smp;
defce9e8 3057
ef8efe4b 3058 l2cap_add_scid(chan, cid);
70db83c4
JH
3059
3060 l2cap_chan_set_defaults(chan);
3061
157029ba 3062 if (cid == L2CAP_CID_SMP) {
39e3e744
JH
3063 u8 bdaddr_type;
3064
3065 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3066
3067 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
157029ba 3068 chan->src_type = BDADDR_LE_PUBLIC;
39e3e744
JH
3069 else
3070 chan->src_type = BDADDR_LE_RANDOM;
157029ba
MH
3071 } else {
3072 bacpy(&chan->src, &hdev->bdaddr);
ef8efe4b 3073 chan->src_type = BDADDR_BREDR;
157029ba
MH
3074 }
3075
70db83c4
JH
3076 chan->state = BT_LISTEN;
3077 chan->mode = L2CAP_MODE_BASIC;
3078 chan->imtu = L2CAP_DEFAULT_MTU;
3079 chan->ops = &smp_root_chan_ops;
3080
abe84903
JH
3081 /* Set correct nesting level for a parent/listening channel */
3082 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3083
ef8efe4b 3084 return chan;
711eafe3
JH
3085}
3086
ef8efe4b 3087static void smp_del_chan(struct l2cap_chan *chan)
711eafe3 3088{
88a479d9 3089 struct smp_dev *smp;
70db83c4 3090
ef8efe4b 3091 BT_DBG("chan %p", chan);
711eafe3 3092
88a479d9
MH
3093 smp = chan->data;
3094 if (smp) {
defce9e8 3095 chan->data = NULL;
88a479d9
MH
3096 if (smp->tfm_aes)
3097 crypto_free_blkcipher(smp->tfm_aes);
6e2dc6d1
MH
3098 if (smp->tfm_cmac)
3099 crypto_free_hash(smp->tfm_cmac);
88a479d9 3100 kzfree(smp);
711eafe3 3101 }
70db83c4 3102
70db83c4 3103 l2cap_chan_put(chan);
711eafe3 3104}
ef8efe4b 3105
300acfde
MH
3106static ssize_t force_bredr_smp_read(struct file *file,
3107 char __user *user_buf,
3108 size_t count, loff_t *ppos)
3109{
3110 struct hci_dev *hdev = file->private_data;
3111 char buf[3];
3112
b7cb93e5 3113 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
300acfde
MH
3114 buf[1] = '\n';
3115 buf[2] = '\0';
3116 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3117}
3118
3119static ssize_t force_bredr_smp_write(struct file *file,
3120 const char __user *user_buf,
3121 size_t count, loff_t *ppos)
3122{
3123 struct hci_dev *hdev = file->private_data;
3124 char buf[32];
3125 size_t buf_size = min(count, (sizeof(buf)-1));
3126 bool enable;
3127
3128 if (copy_from_user(buf, user_buf, buf_size))
3129 return -EFAULT;
3130
3131 buf[buf_size] = '\0';
3132 if (strtobool(buf, &enable))
3133 return -EINVAL;
3134
b7cb93e5 3135 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
300acfde
MH
3136 return -EALREADY;
3137
3138 if (enable) {
3139 struct l2cap_chan *chan;
3140
3141 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3142 if (IS_ERR(chan))
3143 return PTR_ERR(chan);
3144
3145 hdev->smp_bredr_data = chan;
3146 } else {
3147 struct l2cap_chan *chan;
3148
3149 chan = hdev->smp_bredr_data;
3150 hdev->smp_bredr_data = NULL;
3151 smp_del_chan(chan);
3152 }
3153
b7cb93e5 3154 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
300acfde
MH
3155
3156 return count;
3157}
3158
3159static const struct file_operations force_bredr_smp_fops = {
3160 .open = simple_open,
3161 .read = force_bredr_smp_read,
3162 .write = force_bredr_smp_write,
3163 .llseek = default_llseek,
3164};
3165
ef8efe4b
JH
3166int smp_register(struct hci_dev *hdev)
3167{
3168 struct l2cap_chan *chan;
3169
3170 BT_DBG("%s", hdev->name);
3171
7e7ec445
MH
3172 /* If the controller does not support Low Energy operation, then
3173 * there is also no need to register any SMP channel.
3174 */
3175 if (!lmp_le_capable(hdev))
3176 return 0;
3177
2b8df323
MH
3178 if (WARN_ON(hdev->smp_data)) {
3179 chan = hdev->smp_data;
3180 hdev->smp_data = NULL;
3181 smp_del_chan(chan);
3182 }
3183
ef8efe4b
JH
3184 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3185 if (IS_ERR(chan))
3186 return PTR_ERR(chan);
3187
3188 hdev->smp_data = chan;
3189
300acfde
MH
3190 /* If the controller does not support BR/EDR Secure Connections
3191 * feature, then the BR/EDR SMP channel shall not be present.
3192 *
3193 * To test this with Bluetooth 4.0 controllers, create a debugfs
3194 * switch that allows forcing BR/EDR SMP support and accepting
3195 * cross-transport pairing on non-AES encrypted connections.
3196 */
3197 if (!lmp_sc_capable(hdev)) {
3198 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3199 hdev, &force_bredr_smp_fops);
ef8efe4b 3200 return 0;
300acfde 3201 }
ef8efe4b 3202
2b8df323
MH
3203 if (WARN_ON(hdev->smp_bredr_data)) {
3204 chan = hdev->smp_bredr_data;
3205 hdev->smp_bredr_data = NULL;
3206 smp_del_chan(chan);
3207 }
3208
ef8efe4b
JH
3209 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3210 if (IS_ERR(chan)) {
3211 int err = PTR_ERR(chan);
3212 chan = hdev->smp_data;
3213 hdev->smp_data = NULL;
3214 smp_del_chan(chan);
3215 return err;
3216 }
3217
3218 hdev->smp_bredr_data = chan;
3219
3220 return 0;
3221}
3222
3223void smp_unregister(struct hci_dev *hdev)
3224{
3225 struct l2cap_chan *chan;
3226
3227 if (hdev->smp_bredr_data) {
3228 chan = hdev->smp_bredr_data;
3229 hdev->smp_bredr_data = NULL;
3230 smp_del_chan(chan);
3231 }
3232
3233 if (hdev->smp_data) {
3234 chan = hdev->smp_data;
3235 hdev->smp_data = NULL;
3236 smp_del_chan(chan);
3237 }
3238}
0a2b0f04
JH
3239
3240#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3241
cfc4198e
JH
3242static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3243{
3244 const u8 irk[16] = {
3245 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3246 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3247 const u8 r[3] = { 0x94, 0x81, 0x70 };
3248 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3249 u8 res[3];
3250 int err;
3251
3252 err = smp_ah(tfm_aes, irk, r, res);
3253 if (err)
3254 return err;
3255
3256 if (memcmp(res, exp, 3))
3257 return -EINVAL;
3258
3259 return 0;
3260}
3261
3262static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3263{
3264 const u8 k[16] = {
3265 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3266 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3267 const u8 r[16] = {
3268 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3269 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3270 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3271 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3272 const u8 _iat = 0x01;
3273 const u8 _rat = 0x00;
3274 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3275 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3276 const u8 exp[16] = {
3277 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3278 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3279 u8 res[16];
3280 int err;
3281
3282 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3283 if (err)
3284 return err;
3285
3286 if (memcmp(res, exp, 16))
3287 return -EINVAL;
3288
3289 return 0;
3290}
3291
3292static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3293{
3294 const u8 k[16] = {
3295 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3296 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3297 const u8 r1[16] = {
3298 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3299 const u8 r2[16] = {
3300 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3301 const u8 exp[16] = {
3302 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3303 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3304 u8 res[16];
3305 int err;
3306
3307 err = smp_s1(tfm_aes, k, r1, r2, res);
3308 if (err)
3309 return err;
3310
3311 if (memcmp(res, exp, 16))
3312 return -EINVAL;
3313
3314 return 0;
3315}
3316
fb2969a3
JH
3317static int __init test_f4(struct crypto_hash *tfm_cmac)
3318{
3319 const u8 u[32] = {
3320 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3321 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3322 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3323 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3324 const u8 v[32] = {
3325 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3326 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3327 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3328 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3329 const u8 x[16] = {
3330 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3331 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3332 const u8 z = 0x00;
3333 const u8 exp[16] = {
3334 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3335 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3336 u8 res[16];
3337 int err;
3338
3339 err = smp_f4(tfm_cmac, u, v, x, z, res);
3340 if (err)
3341 return err;
3342
3343 if (memcmp(res, exp, 16))
3344 return -EINVAL;
3345
3346 return 0;
3347}
3348
3349static int __init test_f5(struct crypto_hash *tfm_cmac)
3350{
3351 const u8 w[32] = {
3352 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3353 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3354 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3355 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3356 const u8 n1[16] = {
3357 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3358 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3359 const u8 n2[16] = {
3360 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3361 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3362 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3363 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3364 const u8 exp_ltk[16] = {
3365 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3366 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3367 const u8 exp_mackey[16] = {
3368 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3369 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3370 u8 mackey[16], ltk[16];
3371 int err;
3372
3373 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3374 if (err)
3375 return err;
3376
3377 if (memcmp(mackey, exp_mackey, 16))
3378 return -EINVAL;
3379
3380 if (memcmp(ltk, exp_ltk, 16))
3381 return -EINVAL;
3382
3383 return 0;
3384}
3385
3386static int __init test_f6(struct crypto_hash *tfm_cmac)
3387{
3388 const u8 w[16] = {
3389 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3390 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3391 const u8 n1[16] = {
3392 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3393 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3394 const u8 n2[16] = {
3395 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3396 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3397 const u8 r[16] = {
3398 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3399 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3400 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3401 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3402 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3403 const u8 exp[16] = {
3404 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3405 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3406 u8 res[16];
3407 int err;
3408
3409 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3410 if (err)
3411 return err;
3412
3413 if (memcmp(res, exp, 16))
3414 return -EINVAL;
3415
3416 return 0;
3417}
3418
3419static int __init test_g2(struct crypto_hash *tfm_cmac)
3420{
3421 const u8 u[32] = {
3422 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3423 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3424 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3425 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3426 const u8 v[32] = {
3427 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3428 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3429 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3430 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3431 const u8 x[16] = {
3432 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3433 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3434 const u8 y[16] = {
3435 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3436 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3437 const u32 exp_val = 0x2f9ed5ba % 1000000;
3438 u32 val;
3439 int err;
3440
3441 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3442 if (err)
3443 return err;
3444
3445 if (val != exp_val)
3446 return -EINVAL;
3447
3448 return 0;
3449}
3450
3451static int __init test_h6(struct crypto_hash *tfm_cmac)
3452{
3453 const u8 w[16] = {
3454 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3455 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3456 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3457 const u8 exp[16] = {
3458 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3459 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3460 u8 res[16];
3461 int err;
3462
3463 err = smp_h6(tfm_cmac, w, key_id, res);
3464 if (err)
3465 return err;
3466
3467 if (memcmp(res, exp, 16))
3468 return -EINVAL;
3469
3470 return 0;
3471}
3472
0a2b0f04
JH
3473static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3474 struct crypto_hash *tfm_cmac)
3475{
255047b0
MH
3476 ktime_t calltime, delta, rettime;
3477 unsigned long long duration;
cfc4198e
JH
3478 int err;
3479
255047b0
MH
3480 calltime = ktime_get();
3481
cfc4198e
JH
3482 err = test_ah(tfm_aes);
3483 if (err) {
3484 BT_ERR("smp_ah test failed");
3485 return err;
3486 }
3487
3488 err = test_c1(tfm_aes);
3489 if (err) {
3490 BT_ERR("smp_c1 test failed");
3491 return err;
3492 }
3493
3494 err = test_s1(tfm_aes);
3495 if (err) {
3496 BT_ERR("smp_s1 test failed");
3497 return err;
3498 }
3499
fb2969a3
JH
3500 err = test_f4(tfm_cmac);
3501 if (err) {
3502 BT_ERR("smp_f4 test failed");
3503 return err;
3504 }
3505
3506 err = test_f5(tfm_cmac);
3507 if (err) {
3508 BT_ERR("smp_f5 test failed");
3509 return err;
3510 }
3511
3512 err = test_f6(tfm_cmac);
3513 if (err) {
3514 BT_ERR("smp_f6 test failed");
3515 return err;
3516 }
3517
3518 err = test_g2(tfm_cmac);
3519 if (err) {
3520 BT_ERR("smp_g2 test failed");
3521 return err;
3522 }
3523
3524 err = test_h6(tfm_cmac);
3525 if (err) {
3526 BT_ERR("smp_h6 test failed");
3527 return err;
3528 }
3529
255047b0
MH
3530 rettime = ktime_get();
3531 delta = ktime_sub(rettime, calltime);
3532 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3533
5ced2464 3534 BT_INFO("SMP test passed in %llu usecs", duration);
0a2b0f04
JH
3535
3536 return 0;
3537}
3538
3539int __init bt_selftest_smp(void)
3540{
3541 struct crypto_blkcipher *tfm_aes;
3542 struct crypto_hash *tfm_cmac;
3543 int err;
3544
3545 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3546 if (IS_ERR(tfm_aes)) {
3547 BT_ERR("Unable to create ECB crypto context");
3548 return PTR_ERR(tfm_aes);
3549 }
3550
3551 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3552 if (IS_ERR(tfm_cmac)) {
3553 BT_ERR("Unable to create CMAC crypto context");
3554 crypto_free_blkcipher(tfm_aes);
3555 return PTR_ERR(tfm_cmac);
3556 }
3557
3558 err = run_selftests(tfm_aes, tfm_cmac);
3559
3560 crypto_free_hash(tfm_cmac);
3561 crypto_free_blkcipher(tfm_aes);
3562
3563 return err;
3564}
3565
3566#endif
This page took 0.486752 seconds and 5 git commands to generate.