2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
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;
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.
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.
23 #include <linux/crypto.h>
24 #include <linux/scatterlist.h>
25 #include <crypto/b128ops.h>
27 #include <net/bluetooth/bluetooth.h>
28 #include <net/bluetooth/hci_core.h>
29 #include <net/bluetooth/l2cap.h>
30 #include <net/bluetooth/mgmt.h>
35 /* Low-level debug macros to be used for stuff that we don't want
36 * accidentially in dmesg, i.e. the values of the various crypto keys
37 * and the inputs & outputs of crypto functions.
40 #define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
43 #define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
47 #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
49 /* Keys which are not distributed with Secure Connections */
50 #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
52 #define SMP_TIMEOUT msecs_to_jiffies(30000)
54 #define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
56 #define KEY_DIST_MASK 0x07
58 /* Maximum message length that can be passed to aes_cmac */
59 #define CMAC_MSG_MAX 80
71 SMP_FLAG_DHKEY_PENDING,
76 struct l2cap_conn *conn;
77 struct delayed_work security_timer;
78 unsigned long allow_cmd; /* Bitmask of allowed commands */
80 u8 preq[7]; /* SMP Pairing Request */
81 u8 prsp[7]; /* SMP Pairing Response */
82 u8 prnd[16]; /* SMP Pairing Random (local) */
83 u8 rrnd[16]; /* SMP Pairing Random (remote) */
84 u8 pcnf[16]; /* SMP Pairing Confirm */
85 u8 tk[16]; /* SMP Temporary Key */
92 struct smp_csrk *csrk;
93 struct smp_csrk *slave_csrk;
95 struct smp_ltk *slave_ltk;
96 struct smp_irk *remote_irk;
102 /* Secure Connections variables */
109 struct crypto_blkcipher *tfm_aes;
110 struct crypto_hash *tfm_cmac;
113 /* These debug key values are defined in the SMP section of the core
114 * specification. debug_pk is the public debug key and debug_sk the
117 static const u8 debug_pk[64] = {
118 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
119 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
120 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
121 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
123 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
124 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
125 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
126 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
129 static const u8 debug_sk[32] = {
130 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
131 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
132 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
133 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
136 static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
140 for (i = 0; i < len; i++)
141 dst[len - 1 - i] = src[i];
144 /* The following functions map to the LE SC SMP crypto functions
145 * AES-CMAC, f4, f5, f6, g2 and h6.
148 static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
149 size_t len, u8 mac[16])
151 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
152 struct hash_desc desc;
153 struct scatterlist sg;
156 if (len > CMAC_MSG_MAX)
160 BT_ERR("tfm %p", tfm);
167 crypto_hash_init(&desc);
169 /* Swap key and message from LSB to MSB */
170 swap_buf(k, tmp, 16);
171 swap_buf(m, msg_msb, len);
173 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
174 SMP_DBG("key %16phN", k);
176 err = crypto_hash_setkey(tfm, tmp, 16);
178 BT_ERR("cipher setkey failed: %d", err);
182 sg_init_one(&sg, msg_msb, len);
184 err = crypto_hash_update(&desc, &sg, len);
186 BT_ERR("Hash update error %d", err);
190 err = crypto_hash_final(&desc, mac_msb);
192 BT_ERR("Hash final error %d", err);
196 swap_buf(mac_msb, mac, 16);
198 SMP_DBG("mac %16phN", mac);
203 static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
204 const u8 x[16], u8 z, u8 res[16])
209 SMP_DBG("u %32phN", u);
210 SMP_DBG("v %32phN", v);
211 SMP_DBG("x %16phN z %02x", x, z);
214 memcpy(m + 1, v, 32);
215 memcpy(m + 33, u, 32);
217 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
221 SMP_DBG("res %16phN", res);
226 static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
227 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
229 /* The btle, salt and length "magic" values are as defined in
230 * the SMP section of the Bluetooth core specification. In ASCII
231 * the btle value ends up being 'btle'. The salt is just a
232 * random number whereas length is the value 256 in little
235 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
236 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
237 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
238 const u8 length[2] = { 0x00, 0x01 };
242 SMP_DBG("w %32phN", w);
243 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
244 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
246 err = aes_cmac(tfm_cmac, salt, w, 32, t);
250 SMP_DBG("t %16phN", t);
252 memcpy(m, length, 2);
253 memcpy(m + 2, a2, 7);
254 memcpy(m + 9, a1, 7);
255 memcpy(m + 16, n2, 16);
256 memcpy(m + 32, n1, 16);
257 memcpy(m + 48, btle, 4);
259 m[52] = 0; /* Counter */
261 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
265 SMP_DBG("mackey %16phN", mackey);
267 m[52] = 1; /* Counter */
269 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
273 SMP_DBG("ltk %16phN", ltk);
278 static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
279 const u8 n1[16], u8 n2[16], const u8 r[16],
280 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
286 SMP_DBG("w %16phN", w);
287 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
288 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
291 memcpy(m + 7, a1, 7);
292 memcpy(m + 14, io_cap, 3);
293 memcpy(m + 17, r, 16);
294 memcpy(m + 33, n2, 16);
295 memcpy(m + 49, n1, 16);
297 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
301 BT_DBG("res %16phN", res);
306 static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
307 const u8 x[16], const u8 y[16], u32 *val)
312 SMP_DBG("u %32phN", u);
313 SMP_DBG("v %32phN", v);
314 SMP_DBG("x %16phN y %16phN", x, y);
317 memcpy(m + 16, v, 32);
318 memcpy(m + 48, u, 32);
320 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
324 *val = get_unaligned_le32(tmp);
327 SMP_DBG("val %06u", *val);
332 static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
333 const u8 key_id[4], u8 res[16])
337 SMP_DBG("w %16phN key_id %4phN", w, key_id);
339 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
343 SMP_DBG("res %16phN", res);
348 /* The following functions map to the legacy SMP crypto functions e, c1,
352 static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
354 struct blkcipher_desc desc;
355 struct scatterlist sg;
356 uint8_t tmp[16], data[16];
360 BT_ERR("tfm %p", tfm);
367 /* The most significant octet of key corresponds to k[0] */
368 swap_buf(k, tmp, 16);
370 err = crypto_blkcipher_setkey(tfm, tmp, 16);
372 BT_ERR("cipher setkey failed: %d", err);
376 /* Most significant octet of plaintextData corresponds to data[0] */
377 swap_buf(r, data, 16);
379 sg_init_one(&sg, data, 16);
381 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
383 BT_ERR("Encrypt data error %d", err);
385 /* Most significant octet of encryptedData corresponds to data[0] */
386 swap_buf(data, r, 16);
391 static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
392 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
393 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
400 /* p1 = pres || preq || _rat || _iat */
403 memcpy(p1 + 2, preq, 7);
404 memcpy(p1 + 9, pres, 7);
406 /* p2 = padding || ia || ra */
408 memcpy(p2 + 6, ia, 6);
409 memset(p2 + 12, 0, 4);
412 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
414 /* res = e(k, res) */
415 err = smp_e(tfm_aes, k, res);
417 BT_ERR("Encrypt data error");
421 /* res = res XOR p2 */
422 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
424 /* res = e(k, res) */
425 err = smp_e(tfm_aes, k, res);
427 BT_ERR("Encrypt data error");
432 static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
433 const u8 r1[16], const u8 r2[16], u8 _r[16])
437 /* Just least significant octets from r1 and r2 are considered */
439 memcpy(_r + 8, r1, 8);
441 err = smp_e(tfm_aes, k, _r);
443 BT_ERR("Encrypt data error");
448 static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
449 const u8 r[3], u8 res[3])
454 /* r' = padding || r */
456 memset(_res + 3, 0, 13);
458 err = smp_e(tfm, irk, _res);
460 BT_ERR("Encrypt error");
464 /* The output of the random address function ah is:
465 * ah(h, r) = e(k, r') mod 2^24
466 * The output of the security function e is then truncated to 24 bits
467 * by taking the least significant 24 bits of the output of e as the
470 memcpy(res, _res, 3);
475 bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
476 const bdaddr_t *bdaddr)
478 struct l2cap_chan *chan = hdev->smp_data;
479 struct crypto_blkcipher *tfm;
483 if (!chan || !chan->data)
488 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
490 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
494 return !memcmp(bdaddr->b, hash, 3);
497 int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
499 struct l2cap_chan *chan = hdev->smp_data;
500 struct crypto_blkcipher *tfm;
503 if (!chan || !chan->data)
508 get_random_bytes(&rpa->b[3], 3);
510 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
511 rpa->b[5] |= 0x40; /* Set second most significant bit */
513 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
517 BT_DBG("RPA %pMR", rpa);
522 static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
524 struct l2cap_chan *chan = conn->smp;
525 struct smp_chan *smp;
532 BT_DBG("code 0x%2.2x", code);
534 iv[0].iov_base = &code;
537 iv[1].iov_base = data;
540 memset(&msg, 0, sizeof(msg));
542 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
544 l2cap_chan_send(chan, &msg, 1 + len);
551 cancel_delayed_work_sync(&smp->security_timer);
552 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
555 static u8 authreq_to_seclevel(u8 authreq)
557 if (authreq & SMP_AUTH_MITM) {
558 if (authreq & SMP_AUTH_SC)
559 return BT_SECURITY_FIPS;
561 return BT_SECURITY_HIGH;
563 return BT_SECURITY_MEDIUM;
567 static __u8 seclevel_to_authreq(__u8 sec_level)
570 case BT_SECURITY_FIPS:
571 case BT_SECURITY_HIGH:
572 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
573 case BT_SECURITY_MEDIUM:
574 return SMP_AUTH_BONDING;
576 return SMP_AUTH_NONE;
580 static void build_pairing_cmd(struct l2cap_conn *conn,
581 struct smp_cmd_pairing *req,
582 struct smp_cmd_pairing *rsp, __u8 authreq)
584 struct l2cap_chan *chan = conn->smp;
585 struct smp_chan *smp = chan->data;
586 struct hci_conn *hcon = conn->hcon;
587 struct hci_dev *hdev = hcon->hdev;
588 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
590 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
591 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
592 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
593 authreq |= SMP_AUTH_BONDING;
595 authreq &= ~SMP_AUTH_BONDING;
598 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
599 remote_dist |= SMP_DIST_ID_KEY;
601 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
602 local_dist |= SMP_DIST_ID_KEY;
604 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
605 (authreq & SMP_AUTH_SC)) {
606 struct oob_data *oob_data;
609 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
610 local_dist |= SMP_DIST_LINK_KEY;
611 remote_dist |= SMP_DIST_LINK_KEY;
614 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
615 bdaddr_type = BDADDR_LE_PUBLIC;
617 bdaddr_type = BDADDR_LE_RANDOM;
619 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
622 set_bit(SMP_FLAG_OOB, &smp->flags);
623 oob_flag = SMP_OOB_PRESENT;
624 memcpy(smp->rr, oob_data->rand256, 16);
625 memcpy(smp->pcnf, oob_data->hash256, 16);
629 authreq &= ~SMP_AUTH_SC;
633 req->io_capability = conn->hcon->io_capability;
634 req->oob_flag = oob_flag;
635 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
636 req->init_key_dist = local_dist;
637 req->resp_key_dist = remote_dist;
638 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
640 smp->remote_key_dist = remote_dist;
644 rsp->io_capability = conn->hcon->io_capability;
645 rsp->oob_flag = oob_flag;
646 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
647 rsp->init_key_dist = req->init_key_dist & remote_dist;
648 rsp->resp_key_dist = req->resp_key_dist & local_dist;
649 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
651 smp->remote_key_dist = rsp->init_key_dist;
654 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
656 struct l2cap_chan *chan = conn->smp;
657 struct smp_chan *smp = chan->data;
659 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
660 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
661 return SMP_ENC_KEY_SIZE;
663 smp->enc_key_size = max_key_size;
668 static void smp_chan_destroy(struct l2cap_conn *conn)
670 struct l2cap_chan *chan = conn->smp;
671 struct smp_chan *smp = chan->data;
672 struct hci_conn *hcon = conn->hcon;
677 cancel_delayed_work_sync(&smp->security_timer);
679 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
680 mgmt_smp_complete(hcon, complete);
683 kfree(smp->slave_csrk);
684 kfree(smp->link_key);
686 crypto_free_blkcipher(smp->tfm_aes);
687 crypto_free_hash(smp->tfm_cmac);
689 /* Ensure that we don't leave any debug key around if debug key
690 * support hasn't been explicitly enabled.
692 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
693 !test_bit(HCI_KEEP_DEBUG_KEYS, &hcon->hdev->dev_flags)) {
694 list_del_rcu(&smp->ltk->list);
695 kfree_rcu(smp->ltk, rcu);
699 /* If pairing failed clean up any keys we might have */
702 list_del_rcu(&smp->ltk->list);
703 kfree_rcu(smp->ltk, rcu);
706 if (smp->slave_ltk) {
707 list_del_rcu(&smp->slave_ltk->list);
708 kfree_rcu(smp->slave_ltk, rcu);
711 if (smp->remote_irk) {
712 list_del_rcu(&smp->remote_irk->list);
713 kfree_rcu(smp->remote_irk, rcu);
722 static void smp_failure(struct l2cap_conn *conn, u8 reason)
724 struct hci_conn *hcon = conn->hcon;
725 struct l2cap_chan *chan = conn->smp;
728 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
731 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
732 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
735 smp_chan_destroy(conn);
738 #define JUST_WORKS 0x00
739 #define JUST_CFM 0x01
740 #define REQ_PASSKEY 0x02
741 #define CFM_PASSKEY 0x03
743 #define DSP_PASSKEY 0x05
746 static const u8 gen_method[5][5] = {
747 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
748 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
749 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
750 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
751 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
754 static const u8 sc_method[5][5] = {
755 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
756 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
757 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
758 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
759 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
762 static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
764 /* If either side has unknown io_caps, use JUST_CFM (which gets
765 * converted later to JUST_WORKS if we're initiators.
767 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
768 remote_io > SMP_IO_KEYBOARD_DISPLAY)
771 if (test_bit(SMP_FLAG_SC, &smp->flags))
772 return sc_method[remote_io][local_io];
774 return gen_method[remote_io][local_io];
777 static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
778 u8 local_io, u8 remote_io)
780 struct hci_conn *hcon = conn->hcon;
781 struct l2cap_chan *chan = conn->smp;
782 struct smp_chan *smp = chan->data;
786 /* Initialize key for JUST WORKS */
787 memset(smp->tk, 0, sizeof(smp->tk));
788 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
790 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
792 /* If neither side wants MITM, either "just" confirm an incoming
793 * request or use just-works for outgoing ones. The JUST_CFM
794 * will be converted to JUST_WORKS if necessary later in this
795 * function. If either side has MITM look up the method from the
798 if (!(auth & SMP_AUTH_MITM))
799 smp->method = JUST_CFM;
801 smp->method = get_auth_method(smp, local_io, remote_io);
803 /* Don't confirm locally initiated pairing attempts */
804 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
806 smp->method = JUST_WORKS;
808 /* Don't bother user space with no IO capabilities */
809 if (smp->method == JUST_CFM &&
810 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
811 smp->method = JUST_WORKS;
813 /* If Just Works, Continue with Zero TK */
814 if (smp->method == JUST_WORKS) {
815 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
819 /* Not Just Works/Confirm results in MITM Authentication */
820 if (smp->method != JUST_CFM) {
821 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
822 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
823 hcon->pending_sec_level = BT_SECURITY_HIGH;
826 /* If both devices have Keyoard-Display I/O, the master
827 * Confirms and the slave Enters the passkey.
829 if (smp->method == OVERLAP) {
830 if (hcon->role == HCI_ROLE_MASTER)
831 smp->method = CFM_PASSKEY;
833 smp->method = REQ_PASSKEY;
836 /* Generate random passkey. */
837 if (smp->method == CFM_PASSKEY) {
838 memset(smp->tk, 0, sizeof(smp->tk));
839 get_random_bytes(&passkey, sizeof(passkey));
841 put_unaligned_le32(passkey, smp->tk);
842 BT_DBG("PassKey: %d", passkey);
843 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
846 if (smp->method == REQ_PASSKEY)
847 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
848 hcon->type, hcon->dst_type);
849 else if (smp->method == JUST_CFM)
850 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
851 hcon->type, hcon->dst_type,
854 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
855 hcon->type, hcon->dst_type,
861 static u8 smp_confirm(struct smp_chan *smp)
863 struct l2cap_conn *conn = smp->conn;
864 struct smp_cmd_pairing_confirm cp;
867 BT_DBG("conn %p", conn);
869 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
870 conn->hcon->init_addr_type, &conn->hcon->init_addr,
871 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
874 return SMP_UNSPECIFIED;
876 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
878 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
881 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
883 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
888 static u8 smp_random(struct smp_chan *smp)
890 struct l2cap_conn *conn = smp->conn;
891 struct hci_conn *hcon = conn->hcon;
895 if (IS_ERR_OR_NULL(smp->tfm_aes))
896 return SMP_UNSPECIFIED;
898 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
900 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
901 hcon->init_addr_type, &hcon->init_addr,
902 hcon->resp_addr_type, &hcon->resp_addr, confirm);
904 return SMP_UNSPECIFIED;
906 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
907 BT_ERR("Pairing failed (confirmation values mismatch)");
908 return SMP_CONFIRM_FAILED;
916 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
918 memset(stk + smp->enc_key_size, 0,
919 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
921 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
922 return SMP_UNSPECIFIED;
924 hci_le_start_enc(hcon, ediv, rand, stk);
925 hcon->enc_key_size = smp->enc_key_size;
926 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
932 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
935 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
937 memset(stk + smp->enc_key_size, 0,
938 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
940 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
945 /* Even though there's no _SLAVE suffix this is the
946 * slave STK we're adding for later lookup (the master
947 * STK never needs to be stored).
949 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
950 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
956 static void smp_notify_keys(struct l2cap_conn *conn)
958 struct l2cap_chan *chan = conn->smp;
959 struct smp_chan *smp = chan->data;
960 struct hci_conn *hcon = conn->hcon;
961 struct hci_dev *hdev = hcon->hdev;
962 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
963 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
966 if (smp->remote_irk) {
967 mgmt_new_irk(hdev, smp->remote_irk);
968 /* Now that user space can be considered to know the
969 * identity address track the connection based on it
970 * from now on (assuming this is an LE link).
972 if (hcon->type == LE_LINK) {
973 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
974 hcon->dst_type = smp->remote_irk->addr_type;
975 queue_work(hdev->workqueue, &conn->id_addr_update_work);
978 /* When receiving an indentity resolving key for
979 * a remote device that does not use a resolvable
980 * private address, just remove the key so that
981 * it is possible to use the controller white
984 * Userspace will have been told to not store
985 * this key at this point. So it is safe to
988 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
989 list_del_rcu(&smp->remote_irk->list);
990 kfree_rcu(smp->remote_irk, rcu);
991 smp->remote_irk = NULL;
995 if (hcon->type == ACL_LINK) {
996 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
999 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1002 /* The LTKs and CSRKs should be persistent only if both sides
1003 * had the bonding bit set in their authentication requests.
1005 persistent = !!((req->auth_req & rsp->auth_req) &
1011 smp->csrk->bdaddr_type = hcon->dst_type;
1012 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1013 mgmt_new_csrk(hdev, smp->csrk, persistent);
1016 if (smp->slave_csrk) {
1017 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1018 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1019 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1023 smp->ltk->bdaddr_type = hcon->dst_type;
1024 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1025 mgmt_new_ltk(hdev, smp->ltk, persistent);
1028 if (smp->slave_ltk) {
1029 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1030 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1031 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1034 if (smp->link_key) {
1035 struct link_key *key;
1038 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1039 type = HCI_LK_DEBUG_COMBINATION;
1040 else if (hcon->sec_level == BT_SECURITY_FIPS)
1041 type = HCI_LK_AUTH_COMBINATION_P256;
1043 type = HCI_LK_UNAUTH_COMBINATION_P256;
1045 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1046 smp->link_key, type, 0, &persistent);
1048 mgmt_new_link_key(hdev, key, persistent);
1050 /* Don't keep debug keys around if the relevant
1053 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) &&
1054 key->type == HCI_LK_DEBUG_COMBINATION) {
1055 list_del_rcu(&key->list);
1056 kfree_rcu(key, rcu);
1062 static void sc_add_ltk(struct smp_chan *smp)
1064 struct hci_conn *hcon = smp->conn->hcon;
1067 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1068 key_type = SMP_LTK_P256_DEBUG;
1070 key_type = SMP_LTK_P256;
1072 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1077 memset(smp->tk + smp->enc_key_size, 0,
1078 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1080 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1081 key_type, auth, smp->tk, smp->enc_key_size,
1085 static void sc_generate_link_key(struct smp_chan *smp)
1087 /* These constants are as specified in the core specification.
1088 * In ASCII they spell out to 'tmp1' and 'lebr'.
1090 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1091 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1093 smp->link_key = kzalloc(16, GFP_KERNEL);
1097 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1098 kfree(smp->link_key);
1099 smp->link_key = NULL;
1103 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1104 kfree(smp->link_key);
1105 smp->link_key = NULL;
1110 static void smp_allow_key_dist(struct smp_chan *smp)
1112 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1113 * will be allowed in each PDU handler to ensure we receive
1114 * them in the correct order.
1116 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1117 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1118 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1119 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1120 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1121 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1124 static void sc_generate_ltk(struct smp_chan *smp)
1126 /* These constants are as specified in the core specification.
1127 * In ASCII they spell out to 'tmp2' and 'brle'.
1129 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1130 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1131 struct hci_conn *hcon = smp->conn->hcon;
1132 struct hci_dev *hdev = hcon->hdev;
1133 struct link_key *key;
1135 key = hci_find_link_key(hdev, &hcon->dst);
1137 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1141 if (key->type == HCI_LK_DEBUG_COMBINATION)
1142 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1144 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1147 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1153 static void smp_distribute_keys(struct smp_chan *smp)
1155 struct smp_cmd_pairing *req, *rsp;
1156 struct l2cap_conn *conn = smp->conn;
1157 struct hci_conn *hcon = conn->hcon;
1158 struct hci_dev *hdev = hcon->hdev;
1161 BT_DBG("conn %p", conn);
1163 rsp = (void *) &smp->prsp[1];
1165 /* The responder sends its keys first */
1166 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1167 smp_allow_key_dist(smp);
1171 req = (void *) &smp->preq[1];
1174 keydist = &rsp->init_key_dist;
1175 *keydist &= req->init_key_dist;
1177 keydist = &rsp->resp_key_dist;
1178 *keydist &= req->resp_key_dist;
1181 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1182 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
1183 sc_generate_link_key(smp);
1184 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1185 sc_generate_ltk(smp);
1187 /* Clear the keys which are generated but not distributed */
1188 *keydist &= ~SMP_SC_NO_DIST;
1191 BT_DBG("keydist 0x%x", *keydist);
1193 if (*keydist & SMP_DIST_ENC_KEY) {
1194 struct smp_cmd_encrypt_info enc;
1195 struct smp_cmd_master_ident ident;
1196 struct smp_ltk *ltk;
1201 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1202 get_random_bytes(&ediv, sizeof(ediv));
1203 get_random_bytes(&rand, sizeof(rand));
1205 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1207 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1208 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1209 SMP_LTK_SLAVE, authenticated, enc.ltk,
1210 smp->enc_key_size, ediv, rand);
1211 smp->slave_ltk = ltk;
1216 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1218 *keydist &= ~SMP_DIST_ENC_KEY;
1221 if (*keydist & SMP_DIST_ID_KEY) {
1222 struct smp_cmd_ident_addr_info addrinfo;
1223 struct smp_cmd_ident_info idinfo;
1225 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1227 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1229 /* The hci_conn contains the local identity address
1230 * after the connection has been established.
1232 * This is true even when the connection has been
1233 * established using a resolvable random address.
1235 bacpy(&addrinfo.bdaddr, &hcon->src);
1236 addrinfo.addr_type = hcon->src_type;
1238 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1241 *keydist &= ~SMP_DIST_ID_KEY;
1244 if (*keydist & SMP_DIST_SIGN) {
1245 struct smp_cmd_sign_info sign;
1246 struct smp_csrk *csrk;
1248 /* Generate a new random key */
1249 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1251 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1253 csrk->master = 0x00;
1254 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1256 smp->slave_csrk = csrk;
1258 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1260 *keydist &= ~SMP_DIST_SIGN;
1263 /* If there are still keys to be received wait for them */
1264 if (smp->remote_key_dist & KEY_DIST_MASK) {
1265 smp_allow_key_dist(smp);
1269 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1270 smp_notify_keys(conn);
1272 smp_chan_destroy(conn);
1275 static void smp_timeout(struct work_struct *work)
1277 struct smp_chan *smp = container_of(work, struct smp_chan,
1278 security_timer.work);
1279 struct l2cap_conn *conn = smp->conn;
1281 BT_DBG("conn %p", conn);
1283 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
1286 static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1288 struct l2cap_chan *chan = conn->smp;
1289 struct smp_chan *smp;
1291 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
1295 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1296 if (IS_ERR(smp->tfm_aes)) {
1297 BT_ERR("Unable to create ECB crypto context");
1302 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1303 if (IS_ERR(smp->tfm_cmac)) {
1304 BT_ERR("Unable to create CMAC crypto context");
1305 crypto_free_blkcipher(smp->tfm_aes);
1313 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1315 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1317 hci_conn_hold(conn->hcon);
1322 static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1324 struct hci_conn *hcon = smp->conn->hcon;
1325 u8 *na, *nb, a[7], b[7];
1335 memcpy(a, &hcon->init_addr, 6);
1336 memcpy(b, &hcon->resp_addr, 6);
1337 a[6] = hcon->init_addr_type;
1338 b[6] = hcon->resp_addr_type;
1340 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1343 static void sc_dhkey_check(struct smp_chan *smp)
1345 struct hci_conn *hcon = smp->conn->hcon;
1346 struct smp_cmd_dhkey_check check;
1347 u8 a[7], b[7], *local_addr, *remote_addr;
1348 u8 io_cap[3], r[16];
1350 memcpy(a, &hcon->init_addr, 6);
1351 memcpy(b, &hcon->resp_addr, 6);
1352 a[6] = hcon->init_addr_type;
1353 b[6] = hcon->resp_addr_type;
1358 memcpy(io_cap, &smp->preq[1], 3);
1362 memcpy(io_cap, &smp->prsp[1], 3);
1365 memset(r, 0, sizeof(r));
1367 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1368 put_unaligned_le32(hcon->passkey_notify, r);
1370 if (smp->method == REQ_OOB)
1371 memcpy(r, smp->rr, 16);
1373 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1374 local_addr, remote_addr, check.e);
1376 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
1379 static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1381 struct l2cap_conn *conn = smp->conn;
1382 struct hci_conn *hcon = conn->hcon;
1383 struct smp_cmd_pairing_confirm cfm;
1386 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1389 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1391 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1393 return SMP_UNSPECIFIED;
1395 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1400 static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1402 struct l2cap_conn *conn = smp->conn;
1403 struct hci_conn *hcon = conn->hcon;
1404 struct hci_dev *hdev = hcon->hdev;
1407 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1408 if (smp->passkey_round >= 20)
1412 case SMP_CMD_PAIRING_RANDOM:
1413 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1416 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1418 return SMP_UNSPECIFIED;
1420 if (memcmp(smp->pcnf, cfm, 16))
1421 return SMP_CONFIRM_FAILED;
1423 smp->passkey_round++;
1425 if (smp->passkey_round == 20) {
1426 /* Generate MacKey and LTK */
1427 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1428 return SMP_UNSPECIFIED;
1431 /* The round is only complete when the initiator
1432 * receives pairing random.
1435 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1436 sizeof(smp->prnd), smp->prnd);
1437 if (smp->passkey_round == 20)
1438 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1440 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1444 /* Start the next round */
1445 if (smp->passkey_round != 20)
1446 return sc_passkey_round(smp, 0);
1448 /* Passkey rounds are complete - start DHKey Check */
1449 sc_dhkey_check(smp);
1450 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1454 case SMP_CMD_PAIRING_CONFIRM:
1455 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1456 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1460 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1463 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1464 sizeof(smp->prnd), smp->prnd);
1468 return sc_passkey_send_confirm(smp);
1470 case SMP_CMD_PUBLIC_KEY:
1472 /* Initiating device starts the round */
1476 BT_DBG("%s Starting passkey round %u", hdev->name,
1477 smp->passkey_round + 1);
1479 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1481 return sc_passkey_send_confirm(smp);
1487 static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1489 struct l2cap_conn *conn = smp->conn;
1490 struct hci_conn *hcon = conn->hcon;
1493 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1496 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1497 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1499 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1500 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1502 case MGMT_OP_USER_PASSKEY_REPLY:
1503 hcon->passkey_notify = le32_to_cpu(passkey);
1504 smp->passkey_round = 0;
1506 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1507 smp_op = SMP_CMD_PAIRING_CONFIRM;
1511 if (sc_passkey_round(smp, smp_op))
1517 /* Initiator sends DHKey check first */
1519 sc_dhkey_check(smp);
1520 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1521 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1522 sc_dhkey_check(smp);
1529 int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1531 struct l2cap_conn *conn = hcon->l2cap_data;
1532 struct l2cap_chan *chan;
1533 struct smp_chan *smp;
1546 l2cap_chan_lock(chan);
1554 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1555 err = sc_user_reply(smp, mgmt_op, passkey);
1560 case MGMT_OP_USER_PASSKEY_REPLY:
1561 value = le32_to_cpu(passkey);
1562 memset(smp->tk, 0, sizeof(smp->tk));
1563 BT_DBG("PassKey: %d", value);
1564 put_unaligned_le32(value, smp->tk);
1566 case MGMT_OP_USER_CONFIRM_REPLY:
1567 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
1569 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1570 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1571 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
1575 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
1582 /* If it is our turn to send Pairing Confirm, do so now */
1583 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1584 u8 rsp = smp_confirm(smp);
1586 smp_failure(conn, rsp);
1590 l2cap_chan_unlock(chan);
1594 static void build_bredr_pairing_cmd(struct smp_chan *smp,
1595 struct smp_cmd_pairing *req,
1596 struct smp_cmd_pairing *rsp)
1598 struct l2cap_conn *conn = smp->conn;
1599 struct hci_dev *hdev = conn->hcon->hdev;
1600 u8 local_dist = 0, remote_dist = 0;
1602 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) {
1603 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1604 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1607 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
1608 remote_dist |= SMP_DIST_ID_KEY;
1610 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
1611 local_dist |= SMP_DIST_ID_KEY;
1614 memset(req, 0, sizeof(*req));
1616 req->init_key_dist = local_dist;
1617 req->resp_key_dist = remote_dist;
1618 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1620 smp->remote_key_dist = remote_dist;
1625 memset(rsp, 0, sizeof(*rsp));
1627 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1628 rsp->init_key_dist = req->init_key_dist & remote_dist;
1629 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1631 smp->remote_key_dist = rsp->init_key_dist;
1634 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
1636 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
1637 struct l2cap_chan *chan = conn->smp;
1638 struct hci_dev *hdev = conn->hcon->hdev;
1639 struct smp_chan *smp;
1640 u8 key_size, auth, sec_level;
1643 BT_DBG("conn %p", conn);
1645 if (skb->len < sizeof(*req))
1646 return SMP_INVALID_PARAMS;
1648 if (conn->hcon->role != HCI_ROLE_SLAVE)
1649 return SMP_CMD_NOTSUPP;
1652 smp = smp_chan_create(conn);
1657 return SMP_UNSPECIFIED;
1659 /* We didn't start the pairing, so match remote */
1660 auth = req->auth_req & AUTH_REQ_MASK(hdev);
1662 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
1663 (auth & SMP_AUTH_BONDING))
1664 return SMP_PAIRING_NOTSUPP;
1666 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1667 return SMP_AUTH_REQUIREMENTS;
1669 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1670 memcpy(&smp->preq[1], req, sizeof(*req));
1671 skb_pull(skb, sizeof(*req));
1673 /* SMP over BR/EDR requires special treatment */
1674 if (conn->hcon->type == ACL_LINK) {
1675 /* We must have a BR/EDR SC link */
1676 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
1677 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
1678 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1680 set_bit(SMP_FLAG_SC, &smp->flags);
1682 build_bredr_pairing_cmd(smp, req, &rsp);
1684 key_size = min(req->max_key_size, rsp.max_key_size);
1685 if (check_enc_key_size(conn, key_size))
1686 return SMP_ENC_KEY_SIZE;
1688 /* Clear bits which are generated but not distributed */
1689 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1691 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1692 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1693 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1695 smp_distribute_keys(smp);
1699 build_pairing_cmd(conn, req, &rsp, auth);
1701 if (rsp.auth_req & SMP_AUTH_SC)
1702 set_bit(SMP_FLAG_SC, &smp->flags);
1704 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1705 sec_level = BT_SECURITY_MEDIUM;
1707 sec_level = authreq_to_seclevel(auth);
1709 if (sec_level > conn->hcon->pending_sec_level)
1710 conn->hcon->pending_sec_level = sec_level;
1712 /* If we need MITM check that it can be achieved */
1713 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1716 method = get_auth_method(smp, conn->hcon->io_capability,
1717 req->io_capability);
1718 if (method == JUST_WORKS || method == JUST_CFM)
1719 return SMP_AUTH_REQUIREMENTS;
1722 key_size = min(req->max_key_size, rsp.max_key_size);
1723 if (check_enc_key_size(conn, key_size))
1724 return SMP_ENC_KEY_SIZE;
1726 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1728 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1729 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1731 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1733 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1735 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1736 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1737 /* Clear bits which are generated but not distributed */
1738 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1739 /* Wait for Public Key from Initiating Device */
1742 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1745 /* Request setup of TK */
1746 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1748 return SMP_UNSPECIFIED;
1753 static u8 sc_send_public_key(struct smp_chan *smp)
1755 struct hci_dev *hdev = smp->conn->hcon->hdev;
1759 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) {
1760 BT_DBG("Using debug keys");
1761 memcpy(smp->local_pk, debug_pk, 64);
1762 memcpy(smp->local_sk, debug_sk, 32);
1763 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1766 /* Generate local key pair for Secure Connections */
1767 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1768 return SMP_UNSPECIFIED;
1770 /* This is unlikely, but we need to check that
1771 * we didn't accidentially generate a debug key.
1773 if (memcmp(smp->local_sk, debug_sk, 32))
1778 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1779 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1780 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
1782 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1787 static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
1789 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
1790 struct l2cap_chan *chan = conn->smp;
1791 struct smp_chan *smp = chan->data;
1792 struct hci_dev *hdev = conn->hcon->hdev;
1796 BT_DBG("conn %p", conn);
1798 if (skb->len < sizeof(*rsp))
1799 return SMP_INVALID_PARAMS;
1801 if (conn->hcon->role != HCI_ROLE_MASTER)
1802 return SMP_CMD_NOTSUPP;
1804 skb_pull(skb, sizeof(*rsp));
1806 req = (void *) &smp->preq[1];
1808 key_size = min(req->max_key_size, rsp->max_key_size);
1809 if (check_enc_key_size(conn, key_size))
1810 return SMP_ENC_KEY_SIZE;
1812 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
1814 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1815 return SMP_AUTH_REQUIREMENTS;
1817 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1818 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1820 /* Update remote key distribution in case the remote cleared
1821 * some bits that we had enabled in our request.
1823 smp->remote_key_dist &= rsp->resp_key_dist;
1825 /* For BR/EDR this means we're done and can start phase 3 */
1826 if (conn->hcon->type == ACL_LINK) {
1827 /* Clear bits which are generated but not distributed */
1828 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1829 smp_distribute_keys(smp);
1833 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1834 set_bit(SMP_FLAG_SC, &smp->flags);
1835 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1836 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
1838 /* If we need MITM check that it can be achieved */
1839 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1842 method = get_auth_method(smp, req->io_capability,
1843 rsp->io_capability);
1844 if (method == JUST_WORKS || method == JUST_CFM)
1845 return SMP_AUTH_REQUIREMENTS;
1848 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1850 /* Update remote key distribution in case the remote cleared
1851 * some bits that we had enabled in our request.
1853 smp->remote_key_dist &= rsp->resp_key_dist;
1855 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1856 /* Clear bits which are generated but not distributed */
1857 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1858 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1859 return sc_send_public_key(smp);
1862 auth |= req->auth_req;
1864 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
1866 return SMP_UNSPECIFIED;
1868 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1870 /* Can't compose response until we have been confirmed */
1871 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1872 return smp_confirm(smp);
1877 static u8 sc_check_confirm(struct smp_chan *smp)
1879 struct l2cap_conn *conn = smp->conn;
1883 /* Public Key exchange must happen before any other steps */
1884 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1885 return SMP_UNSPECIFIED;
1887 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1888 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1890 if (conn->hcon->out) {
1891 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1893 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1899 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
1901 struct l2cap_chan *chan = conn->smp;
1902 struct smp_chan *smp = chan->data;
1904 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1906 if (skb->len < sizeof(smp->pcnf))
1907 return SMP_INVALID_PARAMS;
1909 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1910 skb_pull(skb, sizeof(smp->pcnf));
1912 if (test_bit(SMP_FLAG_SC, &smp->flags))
1913 return sc_check_confirm(smp);
1915 if (conn->hcon->out) {
1916 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1918 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1922 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1923 return smp_confirm(smp);
1925 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1930 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
1932 struct l2cap_chan *chan = conn->smp;
1933 struct smp_chan *smp = chan->data;
1934 struct hci_conn *hcon = conn->hcon;
1935 u8 *pkax, *pkbx, *na, *nb;
1939 BT_DBG("conn %p", conn);
1941 if (skb->len < sizeof(smp->rrnd))
1942 return SMP_INVALID_PARAMS;
1944 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
1945 skb_pull(skb, sizeof(smp->rrnd));
1947 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1948 return smp_random(smp);
1951 pkax = smp->local_pk;
1952 pkbx = smp->remote_pk;
1956 pkax = smp->remote_pk;
1957 pkbx = smp->local_pk;
1962 if (smp->method == REQ_OOB) {
1964 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1965 sizeof(smp->prnd), smp->prnd);
1966 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1967 goto mackey_and_ltk;
1970 /* Passkey entry has special treatment */
1971 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1972 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1977 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1980 return SMP_UNSPECIFIED;
1982 if (memcmp(smp->pcnf, cfm, 16))
1983 return SMP_CONFIRM_FAILED;
1985 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1987 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1991 /* Generate MacKey and LTK */
1992 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1994 return SMP_UNSPECIFIED;
1996 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
1998 sc_dhkey_check(smp);
1999 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2004 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
2006 return SMP_UNSPECIFIED;
2008 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2009 hcon->dst_type, passkey, 0);
2011 return SMP_UNSPECIFIED;
2013 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2018 static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
2020 struct smp_ltk *key;
2021 struct hci_conn *hcon = conn->hcon;
2023 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
2027 if (smp_ltk_sec_level(key) < sec_level)
2030 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
2033 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2034 hcon->enc_key_size = key->enc_size;
2036 /* We never store STKs for master role, so clear this flag */
2037 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2042 bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2043 enum smp_key_pref key_pref)
2045 if (sec_level == BT_SECURITY_LOW)
2048 /* If we're encrypted with an STK but the caller prefers using
2049 * LTK claim insufficient security. This way we allow the
2050 * connection to be re-encrypted with an LTK, even if the LTK
2051 * provides the same level of security. Only exception is if we
2052 * don't have an LTK (e.g. because of key distribution bits).
2054 if (key_pref == SMP_USE_LTK &&
2055 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
2056 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
2059 if (hcon->sec_level >= sec_level)
2065 static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
2067 struct smp_cmd_security_req *rp = (void *) skb->data;
2068 struct smp_cmd_pairing cp;
2069 struct hci_conn *hcon = conn->hcon;
2070 struct hci_dev *hdev = hcon->hdev;
2071 struct smp_chan *smp;
2074 BT_DBG("conn %p", conn);
2076 if (skb->len < sizeof(*rp))
2077 return SMP_INVALID_PARAMS;
2079 if (hcon->role != HCI_ROLE_MASTER)
2080 return SMP_CMD_NOTSUPP;
2082 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
2084 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
2085 return SMP_AUTH_REQUIREMENTS;
2087 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
2088 sec_level = BT_SECURITY_MEDIUM;
2090 sec_level = authreq_to_seclevel(auth);
2092 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
2095 if (sec_level > hcon->pending_sec_level)
2096 hcon->pending_sec_level = sec_level;
2098 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2101 smp = smp_chan_create(conn);
2103 return SMP_UNSPECIFIED;
2105 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
2106 (auth & SMP_AUTH_BONDING))
2107 return SMP_PAIRING_NOTSUPP;
2109 skb_pull(skb, sizeof(*rp));
2111 memset(&cp, 0, sizeof(cp));
2112 build_pairing_cmd(conn, &cp, NULL, auth);
2114 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2115 memcpy(&smp->preq[1], &cp, sizeof(cp));
2117 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
2118 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2123 int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
2125 struct l2cap_conn *conn = hcon->l2cap_data;
2126 struct l2cap_chan *chan;
2127 struct smp_chan *smp;
2131 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2133 /* This may be NULL if there's an unexpected disconnection */
2139 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
2142 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
2145 if (sec_level > hcon->pending_sec_level)
2146 hcon->pending_sec_level = sec_level;
2148 if (hcon->role == HCI_ROLE_MASTER)
2149 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2152 l2cap_chan_lock(chan);
2154 /* If SMP is already in progress ignore this request */
2160 smp = smp_chan_create(conn);
2166 authreq = seclevel_to_authreq(sec_level);
2168 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
2169 authreq |= SMP_AUTH_SC;
2171 /* Require MITM if IO Capability allows or the security level
2174 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
2175 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
2176 authreq |= SMP_AUTH_MITM;
2178 if (hcon->role == HCI_ROLE_MASTER) {
2179 struct smp_cmd_pairing cp;
2181 build_pairing_cmd(conn, &cp, NULL, authreq);
2182 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2183 memcpy(&smp->preq[1], &cp, sizeof(cp));
2185 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
2186 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2188 struct smp_cmd_security_req cp;
2189 cp.auth_req = authreq;
2190 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
2191 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
2194 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
2198 l2cap_chan_unlock(chan);
2202 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2204 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
2205 struct l2cap_chan *chan = conn->smp;
2206 struct smp_chan *smp = chan->data;
2208 BT_DBG("conn %p", conn);
2210 if (skb->len < sizeof(*rp))
2211 return SMP_INVALID_PARAMS;
2213 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
2215 skb_pull(skb, sizeof(*rp));
2217 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
2222 static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2224 struct smp_cmd_master_ident *rp = (void *) skb->data;
2225 struct l2cap_chan *chan = conn->smp;
2226 struct smp_chan *smp = chan->data;
2227 struct hci_dev *hdev = conn->hcon->hdev;
2228 struct hci_conn *hcon = conn->hcon;
2229 struct smp_ltk *ltk;
2232 BT_DBG("conn %p", conn);
2234 if (skb->len < sizeof(*rp))
2235 return SMP_INVALID_PARAMS;
2237 /* Mark the information as received */
2238 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2240 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2241 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
2242 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2243 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2245 skb_pull(skb, sizeof(*rp));
2247 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
2248 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
2249 authenticated, smp->tk, smp->enc_key_size,
2250 rp->ediv, rp->rand);
2252 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2253 smp_distribute_keys(smp);
2258 static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2260 struct smp_cmd_ident_info *info = (void *) skb->data;
2261 struct l2cap_chan *chan = conn->smp;
2262 struct smp_chan *smp = chan->data;
2266 if (skb->len < sizeof(*info))
2267 return SMP_INVALID_PARAMS;
2269 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
2271 skb_pull(skb, sizeof(*info));
2273 memcpy(smp->irk, info->irk, 16);
2278 static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2279 struct sk_buff *skb)
2281 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
2282 struct l2cap_chan *chan = conn->smp;
2283 struct smp_chan *smp = chan->data;
2284 struct hci_conn *hcon = conn->hcon;
2289 if (skb->len < sizeof(*info))
2290 return SMP_INVALID_PARAMS;
2292 /* Mark the information as received */
2293 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2295 if (smp->remote_key_dist & SMP_DIST_SIGN)
2296 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2298 skb_pull(skb, sizeof(*info));
2300 /* Strictly speaking the Core Specification (4.1) allows sending
2301 * an empty address which would force us to rely on just the IRK
2302 * as "identity information". However, since such
2303 * implementations are not known of and in order to not over
2304 * complicate our implementation, simply pretend that we never
2305 * received an IRK for such a device.
2307 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
2308 BT_ERR("Ignoring IRK with no identity address");
2312 bacpy(&smp->id_addr, &info->bdaddr);
2313 smp->id_addr_type = info->addr_type;
2315 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2316 bacpy(&rpa, &hcon->dst);
2318 bacpy(&rpa, BDADDR_ANY);
2320 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2321 smp->id_addr_type, smp->irk, &rpa);
2324 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2325 smp_distribute_keys(smp);
2330 static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2332 struct smp_cmd_sign_info *rp = (void *) skb->data;
2333 struct l2cap_chan *chan = conn->smp;
2334 struct smp_chan *smp = chan->data;
2335 struct smp_csrk *csrk;
2337 BT_DBG("conn %p", conn);
2339 if (skb->len < sizeof(*rp))
2340 return SMP_INVALID_PARAMS;
2342 /* Mark the information as received */
2343 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2345 skb_pull(skb, sizeof(*rp));
2347 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2349 csrk->master = 0x01;
2350 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2353 smp_distribute_keys(smp);
2358 static u8 sc_select_method(struct smp_chan *smp)
2360 struct l2cap_conn *conn = smp->conn;
2361 struct hci_conn *hcon = conn->hcon;
2362 struct smp_cmd_pairing *local, *remote;
2363 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2365 if (test_bit(SMP_FLAG_OOB, &smp->flags))
2368 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2369 * which are needed as inputs to some crypto functions. To get
2370 * the "struct smp_cmd_pairing" from them we need to skip the
2371 * first byte which contains the opcode.
2374 local = (void *) &smp->preq[1];
2375 remote = (void *) &smp->prsp[1];
2377 local = (void *) &smp->prsp[1];
2378 remote = (void *) &smp->preq[1];
2381 local_io = local->io_capability;
2382 remote_io = remote->io_capability;
2384 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2385 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2387 /* If either side wants MITM, look up the method from the table,
2388 * otherwise use JUST WORKS.
2390 if (local_mitm || remote_mitm)
2391 method = get_auth_method(smp, local_io, remote_io);
2393 method = JUST_WORKS;
2395 /* Don't confirm locally initiated pairing attempts */
2396 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2397 method = JUST_WORKS;
2402 static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2404 struct smp_cmd_public_key *key = (void *) skb->data;
2405 struct hci_conn *hcon = conn->hcon;
2406 struct l2cap_chan *chan = conn->smp;
2407 struct smp_chan *smp = chan->data;
2408 struct hci_dev *hdev = hcon->hdev;
2409 struct smp_cmd_pairing_confirm cfm;
2412 BT_DBG("conn %p", conn);
2414 if (skb->len < sizeof(*key))
2415 return SMP_INVALID_PARAMS;
2417 memcpy(smp->remote_pk, key, 64);
2419 /* Non-initiating device sends its public key after receiving
2420 * the key from the initiating device.
2423 err = sc_send_public_key(smp);
2428 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2429 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
2431 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2432 return SMP_UNSPECIFIED;
2434 SMP_DBG("DHKey %32phN", smp->dhkey);
2436 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2438 smp->method = sc_select_method(smp);
2440 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2442 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2443 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2444 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2446 hcon->pending_sec_level = BT_SECURITY_FIPS;
2448 if (!memcmp(debug_pk, smp->remote_pk, 64))
2449 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2451 if (smp->method == DSP_PASSKEY) {
2452 get_random_bytes(&hcon->passkey_notify,
2453 sizeof(hcon->passkey_notify));
2454 hcon->passkey_notify %= 1000000;
2455 hcon->passkey_entered = 0;
2456 smp->passkey_round = 0;
2457 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2459 hcon->passkey_notify,
2460 hcon->passkey_entered))
2461 return SMP_UNSPECIFIED;
2462 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2463 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2466 if (smp->method == REQ_OOB) {
2467 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2468 smp->rr, 0, cfm.confirm_val);
2470 return SMP_UNSPECIFIED;
2472 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2473 return SMP_CONFIRM_FAILED;
2476 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2477 sizeof(smp->prnd), smp->prnd);
2479 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2485 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2487 if (smp->method == REQ_PASSKEY) {
2488 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2490 return SMP_UNSPECIFIED;
2491 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2492 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2496 /* The Initiating device waits for the non-initiating device to
2497 * send the confirm value.
2499 if (conn->hcon->out)
2502 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2503 0, cfm.confirm_val);
2505 return SMP_UNSPECIFIED;
2507 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2508 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2513 static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2515 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2516 struct l2cap_chan *chan = conn->smp;
2517 struct hci_conn *hcon = conn->hcon;
2518 struct smp_chan *smp = chan->data;
2519 u8 a[7], b[7], *local_addr, *remote_addr;
2520 u8 io_cap[3], r[16], e[16];
2523 BT_DBG("conn %p", conn);
2525 if (skb->len < sizeof(*check))
2526 return SMP_INVALID_PARAMS;
2528 memcpy(a, &hcon->init_addr, 6);
2529 memcpy(b, &hcon->resp_addr, 6);
2530 a[6] = hcon->init_addr_type;
2531 b[6] = hcon->resp_addr_type;
2536 memcpy(io_cap, &smp->prsp[1], 3);
2540 memcpy(io_cap, &smp->preq[1], 3);
2543 memset(r, 0, sizeof(r));
2545 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2546 put_unaligned_le32(hcon->passkey_notify, r);
2548 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2549 io_cap, remote_addr, local_addr, e);
2551 return SMP_UNSPECIFIED;
2553 if (memcmp(check->e, e, 16))
2554 return SMP_DHKEY_CHECK_FAILED;
2557 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2558 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2562 /* Slave sends DHKey check as response to master */
2563 sc_dhkey_check(smp);
2569 hci_le_start_enc(hcon, 0, 0, smp->tk);
2570 hcon->enc_key_size = smp->enc_key_size;
2576 static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2577 struct sk_buff *skb)
2579 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2581 BT_DBG("value 0x%02x", kp->value);
2586 static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
2588 struct l2cap_conn *conn = chan->conn;
2589 struct hci_conn *hcon = conn->hcon;
2590 struct smp_chan *smp;
2597 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
2598 reason = SMP_PAIRING_NOTSUPP;
2602 code = skb->data[0];
2603 skb_pull(skb, sizeof(code));
2607 if (code > SMP_CMD_MAX)
2610 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
2613 /* If we don't have a context the only allowed commands are
2614 * pairing request and security request.
2616 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2620 case SMP_CMD_PAIRING_REQ:
2621 reason = smp_cmd_pairing_req(conn, skb);
2624 case SMP_CMD_PAIRING_FAIL:
2625 smp_failure(conn, 0);
2629 case SMP_CMD_PAIRING_RSP:
2630 reason = smp_cmd_pairing_rsp(conn, skb);
2633 case SMP_CMD_SECURITY_REQ:
2634 reason = smp_cmd_security_req(conn, skb);
2637 case SMP_CMD_PAIRING_CONFIRM:
2638 reason = smp_cmd_pairing_confirm(conn, skb);
2641 case SMP_CMD_PAIRING_RANDOM:
2642 reason = smp_cmd_pairing_random(conn, skb);
2645 case SMP_CMD_ENCRYPT_INFO:
2646 reason = smp_cmd_encrypt_info(conn, skb);
2649 case SMP_CMD_MASTER_IDENT:
2650 reason = smp_cmd_master_ident(conn, skb);
2653 case SMP_CMD_IDENT_INFO:
2654 reason = smp_cmd_ident_info(conn, skb);
2657 case SMP_CMD_IDENT_ADDR_INFO:
2658 reason = smp_cmd_ident_addr_info(conn, skb);
2661 case SMP_CMD_SIGN_INFO:
2662 reason = smp_cmd_sign_info(conn, skb);
2665 case SMP_CMD_PUBLIC_KEY:
2666 reason = smp_cmd_public_key(conn, skb);
2669 case SMP_CMD_DHKEY_CHECK:
2670 reason = smp_cmd_dhkey_check(conn, skb);
2673 case SMP_CMD_KEYPRESS_NOTIFY:
2674 reason = smp_cmd_keypress_notify(conn, skb);
2678 BT_DBG("Unknown command code 0x%2.2x", code);
2679 reason = SMP_CMD_NOTSUPP;
2686 smp_failure(conn, reason);
2693 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2699 static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2701 struct l2cap_conn *conn = chan->conn;
2703 BT_DBG("chan %p", chan);
2706 smp_chan_destroy(conn);
2709 l2cap_chan_put(chan);
2712 static void bredr_pairing(struct l2cap_chan *chan)
2714 struct l2cap_conn *conn = chan->conn;
2715 struct hci_conn *hcon = conn->hcon;
2716 struct hci_dev *hdev = hcon->hdev;
2717 struct smp_cmd_pairing req;
2718 struct smp_chan *smp;
2720 BT_DBG("chan %p", chan);
2722 /* Only new pairings are interesting */
2723 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2726 /* Don't bother if we're not encrypted */
2727 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2730 /* Only master may initiate SMP over BR/EDR */
2731 if (hcon->role != HCI_ROLE_MASTER)
2734 /* Secure Connections support must be enabled */
2735 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
2738 /* BR/EDR must use Secure Connections for SMP */
2739 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
2740 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2743 /* If our LE support is not enabled don't do anything */
2744 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
2747 /* Don't bother if remote LE support is not enabled */
2748 if (!lmp_host_le_capable(hcon))
2751 /* Remote must support SMP fixed chan for BR/EDR */
2752 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2755 /* Don't bother if SMP is already ongoing */
2759 smp = smp_chan_create(conn);
2761 BT_ERR("%s unable to create SMP context for BR/EDR",
2766 set_bit(SMP_FLAG_SC, &smp->flags);
2768 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2770 /* Prepare and send the BR/EDR SMP Pairing Request */
2771 build_bredr_pairing_cmd(smp, &req, NULL);
2773 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2774 memcpy(&smp->preq[1], &req, sizeof(req));
2776 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2777 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2780 static void smp_resume_cb(struct l2cap_chan *chan)
2782 struct smp_chan *smp = chan->data;
2783 struct l2cap_conn *conn = chan->conn;
2784 struct hci_conn *hcon = conn->hcon;
2786 BT_DBG("chan %p", chan);
2788 if (hcon->type == ACL_LINK) {
2789 bredr_pairing(chan);
2796 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2799 cancel_delayed_work(&smp->security_timer);
2801 smp_distribute_keys(smp);
2804 static void smp_ready_cb(struct l2cap_chan *chan)
2806 struct l2cap_conn *conn = chan->conn;
2807 struct hci_conn *hcon = conn->hcon;
2809 BT_DBG("chan %p", chan);
2812 l2cap_chan_hold(chan);
2814 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2815 bredr_pairing(chan);
2818 static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2822 BT_DBG("chan %p", chan);
2824 err = smp_sig_channel(chan, skb);
2826 struct smp_chan *smp = chan->data;
2829 cancel_delayed_work_sync(&smp->security_timer);
2831 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
2837 static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2838 unsigned long hdr_len,
2839 unsigned long len, int nb)
2841 struct sk_buff *skb;
2843 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2845 return ERR_PTR(-ENOMEM);
2847 skb->priority = HCI_PRIO_MAX;
2848 bt_cb(skb)->chan = chan;
2853 static const struct l2cap_ops smp_chan_ops = {
2854 .name = "Security Manager",
2855 .ready = smp_ready_cb,
2856 .recv = smp_recv_cb,
2857 .alloc_skb = smp_alloc_skb_cb,
2858 .teardown = smp_teardown_cb,
2859 .resume = smp_resume_cb,
2861 .new_connection = l2cap_chan_no_new_connection,
2862 .state_change = l2cap_chan_no_state_change,
2863 .close = l2cap_chan_no_close,
2864 .defer = l2cap_chan_no_defer,
2865 .suspend = l2cap_chan_no_suspend,
2866 .set_shutdown = l2cap_chan_no_set_shutdown,
2867 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2870 static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2872 struct l2cap_chan *chan;
2874 BT_DBG("pchan %p", pchan);
2876 chan = l2cap_chan_create();
2880 chan->chan_type = pchan->chan_type;
2881 chan->ops = &smp_chan_ops;
2882 chan->scid = pchan->scid;
2883 chan->dcid = chan->scid;
2884 chan->imtu = pchan->imtu;
2885 chan->omtu = pchan->omtu;
2886 chan->mode = pchan->mode;
2888 /* Other L2CAP channels may request SMP routines in order to
2889 * change the security level. This means that the SMP channel
2890 * lock must be considered in its own category to avoid lockdep
2893 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2895 BT_DBG("created chan %p", chan);
2900 static const struct l2cap_ops smp_root_chan_ops = {
2901 .name = "Security Manager Root",
2902 .new_connection = smp_new_conn_cb,
2904 /* None of these are implemented for the root channel */
2905 .close = l2cap_chan_no_close,
2906 .alloc_skb = l2cap_chan_no_alloc_skb,
2907 .recv = l2cap_chan_no_recv,
2908 .state_change = l2cap_chan_no_state_change,
2909 .teardown = l2cap_chan_no_teardown,
2910 .ready = l2cap_chan_no_ready,
2911 .defer = l2cap_chan_no_defer,
2912 .suspend = l2cap_chan_no_suspend,
2913 .resume = l2cap_chan_no_resume,
2914 .set_shutdown = l2cap_chan_no_set_shutdown,
2915 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2918 static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
2920 struct l2cap_chan *chan;
2921 struct crypto_blkcipher *tfm_aes;
2923 if (cid == L2CAP_CID_SMP_BREDR) {
2928 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
2929 if (IS_ERR(tfm_aes)) {
2930 BT_ERR("Unable to create crypto context");
2931 return ERR_CAST(tfm_aes);
2935 chan = l2cap_chan_create();
2937 crypto_free_blkcipher(tfm_aes);
2938 return ERR_PTR(-ENOMEM);
2941 chan->data = tfm_aes;
2943 l2cap_add_scid(chan, cid);
2945 l2cap_chan_set_defaults(chan);
2947 bacpy(&chan->src, &hdev->bdaddr);
2948 if (cid == L2CAP_CID_SMP)
2949 chan->src_type = BDADDR_LE_PUBLIC;
2951 chan->src_type = BDADDR_BREDR;
2952 chan->state = BT_LISTEN;
2953 chan->mode = L2CAP_MODE_BASIC;
2954 chan->imtu = L2CAP_DEFAULT_MTU;
2955 chan->ops = &smp_root_chan_ops;
2957 /* Set correct nesting level for a parent/listening channel */
2958 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2963 static void smp_del_chan(struct l2cap_chan *chan)
2965 struct crypto_blkcipher *tfm_aes;
2967 BT_DBG("chan %p", chan);
2969 tfm_aes = chan->data;
2972 crypto_free_blkcipher(tfm_aes);
2975 l2cap_chan_put(chan);
2978 int smp_register(struct hci_dev *hdev)
2980 struct l2cap_chan *chan;
2982 BT_DBG("%s", hdev->name);
2984 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
2986 return PTR_ERR(chan);
2988 hdev->smp_data = chan;
2990 if (!lmp_sc_capable(hdev) &&
2991 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2994 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
2996 int err = PTR_ERR(chan);
2997 chan = hdev->smp_data;
2998 hdev->smp_data = NULL;
3003 hdev->smp_bredr_data = chan;
3008 void smp_unregister(struct hci_dev *hdev)
3010 struct l2cap_chan *chan;
3012 if (hdev->smp_bredr_data) {
3013 chan = hdev->smp_bredr_data;
3014 hdev->smp_bredr_data = NULL;
3018 if (hdev->smp_data) {
3019 chan = hdev->smp_data;
3020 hdev->smp_data = NULL;