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 #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
37 /* Keys which are not distributed with Secure Connections */
38 #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
40 #define SMP_TIMEOUT msecs_to_jiffies(30000)
42 #define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
44 #define KEY_DIST_MASK 0x07
46 /* Maximum message length that can be passed to aes_cmac */
47 #define CMAC_MSG_MAX 80
60 struct l2cap_conn *conn;
61 struct delayed_work security_timer;
62 unsigned long allow_cmd; /* Bitmask of allowed commands */
64 u8 preq[7]; /* SMP Pairing Request */
65 u8 prsp[7]; /* SMP Pairing Response */
66 u8 prnd[16]; /* SMP Pairing Random (local) */
67 u8 rrnd[16]; /* SMP Pairing Random (remote) */
68 u8 pcnf[16]; /* SMP Pairing Confirm */
69 u8 tk[16]; /* SMP Temporary Key */
75 struct smp_csrk *csrk;
76 struct smp_csrk *slave_csrk;
78 struct smp_ltk *slave_ltk;
79 struct smp_irk *remote_irk;
84 /* Secure Connections variables */
91 struct crypto_blkcipher *tfm_aes;
92 struct crypto_hash *tfm_cmac;
95 static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
99 for (i = 0; i < len; i++)
100 dst[len - 1 - i] = src[i];
103 static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
104 size_t len, u8 mac[16])
106 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
107 struct hash_desc desc;
108 struct scatterlist sg;
111 if (len > CMAC_MSG_MAX)
115 BT_ERR("tfm %p", tfm);
122 crypto_hash_init(&desc);
124 /* Swap key and message from LSB to MSB */
125 swap_buf(k, tmp, 16);
126 swap_buf(m, msg_msb, len);
128 BT_DBG("msg (len %zu) %*phN", len, (int) len, m);
129 BT_DBG("key %16phN", k);
131 err = crypto_hash_setkey(tfm, tmp, 16);
133 BT_ERR("cipher setkey failed: %d", err);
137 sg_init_one(&sg, msg_msb, len);
139 err = crypto_hash_update(&desc, &sg, len);
141 BT_ERR("Hash update error %d", err);
145 err = crypto_hash_final(&desc, mac_msb);
147 BT_ERR("Hash final error %d", err);
151 swap_buf(mac_msb, mac, 16);
153 BT_DBG("mac %16phN", mac);
158 static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
159 const u8 x[16], u8 z, u8 res[16])
164 BT_DBG("u %32phN", u);
165 BT_DBG("v %32phN", v);
166 BT_DBG("x %16phN z %02x", x, z);
169 memcpy(m + 1, v, 32);
170 memcpy(m + 33, u, 32);
172 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
176 BT_DBG("res %16phN", res);
181 static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
182 u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
184 /* The btle, salt and length "magic" values are as defined in
185 * the SMP section of the Bluetooth core specification. In ASCII
186 * the btle value ends up being 'btle'. The salt is just a
187 * random number whereas length is the value 256 in little
190 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
191 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
192 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
193 const u8 length[2] = { 0x00, 0x01 };
197 BT_DBG("w %32phN", w);
198 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
199 BT_DBG("a1 %7phN a2 %7phN", a1, a2);
201 err = aes_cmac(tfm_cmac, salt, w, 32, t);
205 BT_DBG("t %16phN", t);
207 memcpy(m, length, 2);
208 memcpy(m + 2, a2, 7);
209 memcpy(m + 9, a1, 7);
210 memcpy(m + 16, n2, 16);
211 memcpy(m + 32, n1, 16);
212 memcpy(m + 48, btle, 4);
214 m[52] = 0; /* Counter */
216 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
220 BT_DBG("mackey %16phN", mackey);
222 m[52] = 1; /* Counter */
224 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
228 BT_DBG("ltk %16phN", ltk);
233 static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
234 const u8 n1[16], u8 n2[16], const u8 r[16],
235 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
241 BT_DBG("w %16phN", w);
242 BT_DBG("n1 %16phN n2 %16phN", n1, n2);
243 BT_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
246 memcpy(m + 7, a1, 7);
247 memcpy(m + 14, io_cap, 3);
248 memcpy(m + 17, r, 16);
249 memcpy(m + 33, n2, 16);
250 memcpy(m + 49, n1, 16);
252 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
256 BT_DBG("res %16phN", res);
261 static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
262 const u8 x[16], const u8 y[16], u32 *val)
267 BT_DBG("u %32phN", u);
268 BT_DBG("v %32phN", v);
269 BT_DBG("x %16phN y %16phN", x, y);
272 memcpy(m + 16, v, 32);
273 memcpy(m + 48, u, 32);
275 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
279 *val = get_unaligned_le32(tmp);
282 BT_DBG("val %06u", *val);
287 static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
289 struct blkcipher_desc desc;
290 struct scatterlist sg;
291 uint8_t tmp[16], data[16];
295 BT_ERR("tfm %p", tfm);
302 /* The most significant octet of key corresponds to k[0] */
303 swap_buf(k, tmp, 16);
305 err = crypto_blkcipher_setkey(tfm, tmp, 16);
307 BT_ERR("cipher setkey failed: %d", err);
311 /* Most significant octet of plaintextData corresponds to data[0] */
312 swap_buf(r, data, 16);
314 sg_init_one(&sg, data, 16);
316 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
318 BT_ERR("Encrypt data error %d", err);
320 /* Most significant octet of encryptedData corresponds to data[0] */
321 swap_buf(data, r, 16);
326 static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
327 const u8 key_id[4], u8 res[16])
331 BT_DBG("w %16phN key_id %4phN", w, key_id);
333 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
337 BT_DBG("res %16phN", res);
342 static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3])
347 /* r' = padding || r */
349 memset(_res + 3, 0, 13);
351 err = smp_e(tfm, irk, _res);
353 BT_ERR("Encrypt error");
357 /* The output of the random address function ah is:
358 * ah(h, r) = e(k, r') mod 2^24
359 * The output of the security function e is then truncated to 24 bits
360 * by taking the least significant 24 bits of the output of e as the
363 memcpy(res, _res, 3);
368 bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr)
370 struct l2cap_chan *chan = hdev->smp_data;
371 struct crypto_blkcipher *tfm;
375 if (!chan || !chan->data)
380 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
382 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
386 return !memcmp(bdaddr->b, hash, 3);
389 int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa)
391 struct l2cap_chan *chan = hdev->smp_data;
392 struct crypto_blkcipher *tfm;
395 if (!chan || !chan->data)
400 get_random_bytes(&rpa->b[3], 3);
402 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
403 rpa->b[5] |= 0x40; /* Set second most significant bit */
405 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
409 BT_DBG("RPA %pMR", rpa);
414 static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16],
415 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat,
416 bdaddr_t *ra, u8 res[16])
423 /* p1 = pres || preq || _rat || _iat */
426 memcpy(p1 + 2, preq, 7);
427 memcpy(p1 + 9, pres, 7);
429 /* p2 = padding || ia || ra */
431 memcpy(p2 + 6, ia, 6);
432 memset(p2 + 12, 0, 4);
435 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
437 /* res = e(k, res) */
438 err = smp_e(tfm_aes, k, res);
440 BT_ERR("Encrypt data error");
444 /* res = res XOR p2 */
445 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
447 /* res = e(k, res) */
448 err = smp_e(tfm_aes, k, res);
450 BT_ERR("Encrypt data error");
455 static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16],
456 u8 r2[16], u8 _r[16])
460 /* Just least significant octets from r1 and r2 are considered */
462 memcpy(_r + 8, r1, 8);
464 err = smp_e(tfm_aes, k, _r);
466 BT_ERR("Encrypt data error");
471 static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
473 struct l2cap_chan *chan = conn->smp;
474 struct smp_chan *smp;
481 BT_DBG("code 0x%2.2x", code);
483 iv[0].iov_base = &code;
486 iv[1].iov_base = data;
489 memset(&msg, 0, sizeof(msg));
491 msg.msg_iov = (struct iovec *) &iv;
494 l2cap_chan_send(chan, &msg, 1 + len);
501 cancel_delayed_work_sync(&smp->security_timer);
502 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
505 static u8 authreq_to_seclevel(u8 authreq)
507 if (authreq & SMP_AUTH_MITM) {
508 if (authreq & SMP_AUTH_SC)
509 return BT_SECURITY_FIPS;
511 return BT_SECURITY_HIGH;
513 return BT_SECURITY_MEDIUM;
517 static __u8 seclevel_to_authreq(__u8 sec_level)
520 case BT_SECURITY_FIPS:
521 case BT_SECURITY_HIGH:
522 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
523 case BT_SECURITY_MEDIUM:
524 return SMP_AUTH_BONDING;
526 return SMP_AUTH_NONE;
530 static void build_pairing_cmd(struct l2cap_conn *conn,
531 struct smp_cmd_pairing *req,
532 struct smp_cmd_pairing *rsp, __u8 authreq)
534 struct l2cap_chan *chan = conn->smp;
535 struct smp_chan *smp = chan->data;
536 struct hci_conn *hcon = conn->hcon;
537 struct hci_dev *hdev = hcon->hdev;
538 u8 local_dist = 0, remote_dist = 0;
540 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
541 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
542 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
543 authreq |= SMP_AUTH_BONDING;
545 authreq &= ~SMP_AUTH_BONDING;
548 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
549 remote_dist |= SMP_DIST_ID_KEY;
551 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
552 local_dist |= SMP_DIST_ID_KEY;
554 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
555 if ((authreq & SMP_AUTH_SC) &&
556 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
557 local_dist |= SMP_DIST_LINK_KEY;
558 remote_dist |= SMP_DIST_LINK_KEY;
561 authreq &= ~SMP_AUTH_SC;
565 req->io_capability = conn->hcon->io_capability;
566 req->oob_flag = SMP_OOB_NOT_PRESENT;
567 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
568 req->init_key_dist = local_dist;
569 req->resp_key_dist = remote_dist;
570 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
572 smp->remote_key_dist = remote_dist;
576 rsp->io_capability = conn->hcon->io_capability;
577 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
578 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
579 rsp->init_key_dist = req->init_key_dist & remote_dist;
580 rsp->resp_key_dist = req->resp_key_dist & local_dist;
581 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
583 smp->remote_key_dist = rsp->init_key_dist;
586 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
588 struct l2cap_chan *chan = conn->smp;
589 struct smp_chan *smp = chan->data;
591 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
592 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
593 return SMP_ENC_KEY_SIZE;
595 smp->enc_key_size = max_key_size;
600 static void smp_chan_destroy(struct l2cap_conn *conn)
602 struct l2cap_chan *chan = conn->smp;
603 struct smp_chan *smp = chan->data;
608 cancel_delayed_work_sync(&smp->security_timer);
610 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
611 mgmt_smp_complete(conn->hcon, complete);
614 kfree(smp->slave_csrk);
615 kfree(smp->link_key);
617 crypto_free_blkcipher(smp->tfm_aes);
618 crypto_free_hash(smp->tfm_cmac);
620 /* If pairing failed clean up any keys we might have */
623 list_del_rcu(&smp->ltk->list);
624 kfree_rcu(smp->ltk, rcu);
627 if (smp->slave_ltk) {
628 list_del_rcu(&smp->slave_ltk->list);
629 kfree_rcu(smp->slave_ltk, rcu);
632 if (smp->remote_irk) {
633 list_del_rcu(&smp->remote_irk->list);
634 kfree_rcu(smp->remote_irk, rcu);
640 hci_conn_drop(conn->hcon);
643 static void smp_failure(struct l2cap_conn *conn, u8 reason)
645 struct hci_conn *hcon = conn->hcon;
646 struct l2cap_chan *chan = conn->smp;
649 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
652 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
653 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
656 smp_chan_destroy(conn);
659 #define JUST_WORKS 0x00
660 #define JUST_CFM 0x01
661 #define REQ_PASSKEY 0x02
662 #define CFM_PASSKEY 0x03
666 static const u8 gen_method[5][5] = {
667 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
668 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
669 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
670 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
671 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
674 static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
676 /* If either side has unknown io_caps, use JUST_CFM (which gets
677 * converted later to JUST_WORKS if we're initiators.
679 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
680 remote_io > SMP_IO_KEYBOARD_DISPLAY)
683 return gen_method[remote_io][local_io];
686 static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
687 u8 local_io, u8 remote_io)
689 struct hci_conn *hcon = conn->hcon;
690 struct l2cap_chan *chan = conn->smp;
691 struct smp_chan *smp = chan->data;
695 /* Initialize key for JUST WORKS */
696 memset(smp->tk, 0, sizeof(smp->tk));
697 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
699 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
701 /* If neither side wants MITM, either "just" confirm an incoming
702 * request or use just-works for outgoing ones. The JUST_CFM
703 * will be converted to JUST_WORKS if necessary later in this
704 * function. If either side has MITM look up the method from the
707 if (!(auth & SMP_AUTH_MITM))
708 smp->method = JUST_CFM;
710 smp->method = get_auth_method(smp, local_io, remote_io);
712 /* Don't confirm locally initiated pairing attempts */
713 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
715 smp->method = JUST_WORKS;
717 /* Don't bother user space with no IO capabilities */
718 if (smp->method == JUST_CFM &&
719 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
720 smp->method = JUST_WORKS;
722 /* If Just Works, Continue with Zero TK */
723 if (smp->method == JUST_WORKS) {
724 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
728 /* Not Just Works/Confirm results in MITM Authentication */
729 if (smp->method != JUST_CFM) {
730 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
731 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
732 hcon->pending_sec_level = BT_SECURITY_HIGH;
735 /* If both devices have Keyoard-Display I/O, the master
736 * Confirms and the slave Enters the passkey.
738 if (smp->method == OVERLAP) {
739 if (hcon->role == HCI_ROLE_MASTER)
740 smp->method = CFM_PASSKEY;
742 smp->method = REQ_PASSKEY;
745 /* Generate random passkey. */
746 if (smp->method == CFM_PASSKEY) {
747 memset(smp->tk, 0, sizeof(smp->tk));
748 get_random_bytes(&passkey, sizeof(passkey));
750 put_unaligned_le32(passkey, smp->tk);
751 BT_DBG("PassKey: %d", passkey);
752 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
755 if (smp->method == REQ_PASSKEY)
756 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
757 hcon->type, hcon->dst_type);
758 else if (smp->method == JUST_CFM)
759 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
760 hcon->type, hcon->dst_type,
763 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
764 hcon->type, hcon->dst_type,
770 static u8 smp_confirm(struct smp_chan *smp)
772 struct l2cap_conn *conn = smp->conn;
773 struct smp_cmd_pairing_confirm cp;
776 BT_DBG("conn %p", conn);
778 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
779 conn->hcon->init_addr_type, &conn->hcon->init_addr,
780 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
783 return SMP_UNSPECIFIED;
785 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
787 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
790 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
792 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
797 static u8 smp_random(struct smp_chan *smp)
799 struct l2cap_conn *conn = smp->conn;
800 struct hci_conn *hcon = conn->hcon;
804 if (IS_ERR_OR_NULL(smp->tfm_aes))
805 return SMP_UNSPECIFIED;
807 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
809 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
810 hcon->init_addr_type, &hcon->init_addr,
811 hcon->resp_addr_type, &hcon->resp_addr, confirm);
813 return SMP_UNSPECIFIED;
815 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
816 BT_ERR("Pairing failed (confirmation values mismatch)");
817 return SMP_CONFIRM_FAILED;
825 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
827 memset(stk + smp->enc_key_size, 0,
828 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
830 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
831 return SMP_UNSPECIFIED;
833 hci_le_start_enc(hcon, ediv, rand, stk);
834 hcon->enc_key_size = smp->enc_key_size;
835 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
841 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
844 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
846 memset(stk + smp->enc_key_size, 0,
847 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
849 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
854 /* Even though there's no _SLAVE suffix this is the
855 * slave STK we're adding for later lookup (the master
856 * STK never needs to be stored).
858 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
859 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
865 static void smp_notify_keys(struct l2cap_conn *conn)
867 struct l2cap_chan *chan = conn->smp;
868 struct smp_chan *smp = chan->data;
869 struct hci_conn *hcon = conn->hcon;
870 struct hci_dev *hdev = hcon->hdev;
871 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
872 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
875 if (smp->remote_irk) {
876 mgmt_new_irk(hdev, smp->remote_irk);
877 /* Now that user space can be considered to know the
878 * identity address track the connection based on it
881 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
882 hcon->dst_type = smp->remote_irk->addr_type;
883 queue_work(hdev->workqueue, &conn->id_addr_update_work);
885 /* When receiving an indentity resolving key for
886 * a remote device that does not use a resolvable
887 * private address, just remove the key so that
888 * it is possible to use the controller white
891 * Userspace will have been told to not store
892 * this key at this point. So it is safe to
895 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
896 list_del_rcu(&smp->remote_irk->list);
897 kfree_rcu(smp->remote_irk, rcu);
898 smp->remote_irk = NULL;
902 /* The LTKs and CSRKs should be persistent only if both sides
903 * had the bonding bit set in their authentication requests.
905 persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING);
908 smp->csrk->bdaddr_type = hcon->dst_type;
909 bacpy(&smp->csrk->bdaddr, &hcon->dst);
910 mgmt_new_csrk(hdev, smp->csrk, persistent);
913 if (smp->slave_csrk) {
914 smp->slave_csrk->bdaddr_type = hcon->dst_type;
915 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
916 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
920 smp->ltk->bdaddr_type = hcon->dst_type;
921 bacpy(&smp->ltk->bdaddr, &hcon->dst);
922 mgmt_new_ltk(hdev, smp->ltk, persistent);
925 if (smp->slave_ltk) {
926 smp->slave_ltk->bdaddr_type = hcon->dst_type;
927 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
928 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
932 hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
933 smp->link_key, HCI_LK_AUTH_COMBINATION_P256,
938 static void sc_generate_link_key(struct smp_chan *smp)
940 /* These constants are as specified in the core specification.
941 * In ASCII they spell out to 'tmp1' and 'lebr'.
943 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
944 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
946 smp->link_key = kzalloc(16, GFP_KERNEL);
950 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
951 kfree(smp->link_key);
952 smp->link_key = NULL;
956 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
957 kfree(smp->link_key);
958 smp->link_key = NULL;
963 static void smp_allow_key_dist(struct smp_chan *smp)
965 /* Allow the first expected phase 3 PDU. The rest of the PDUs
966 * will be allowed in each PDU handler to ensure we receive
967 * them in the correct order.
969 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
970 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
971 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
972 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
973 else if (smp->remote_key_dist & SMP_DIST_SIGN)
974 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
977 static void smp_distribute_keys(struct smp_chan *smp)
979 struct smp_cmd_pairing *req, *rsp;
980 struct l2cap_conn *conn = smp->conn;
981 struct hci_conn *hcon = conn->hcon;
982 struct hci_dev *hdev = hcon->hdev;
985 BT_DBG("conn %p", conn);
987 rsp = (void *) &smp->prsp[1];
989 /* The responder sends its keys first */
990 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
991 smp_allow_key_dist(smp);
995 req = (void *) &smp->preq[1];
998 keydist = &rsp->init_key_dist;
999 *keydist &= req->init_key_dist;
1001 keydist = &rsp->resp_key_dist;
1002 *keydist &= req->resp_key_dist;
1005 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1006 if (*keydist & SMP_DIST_LINK_KEY)
1007 sc_generate_link_key(smp);
1009 /* Clear the keys which are generated but not distributed */
1010 *keydist &= ~SMP_SC_NO_DIST;
1013 BT_DBG("keydist 0x%x", *keydist);
1015 if (*keydist & SMP_DIST_ENC_KEY) {
1016 struct smp_cmd_encrypt_info enc;
1017 struct smp_cmd_master_ident ident;
1018 struct smp_ltk *ltk;
1023 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1024 get_random_bytes(&ediv, sizeof(ediv));
1025 get_random_bytes(&rand, sizeof(rand));
1027 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1029 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1030 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1031 SMP_LTK_SLAVE, authenticated, enc.ltk,
1032 smp->enc_key_size, ediv, rand);
1033 smp->slave_ltk = ltk;
1038 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1040 *keydist &= ~SMP_DIST_ENC_KEY;
1043 if (*keydist & SMP_DIST_ID_KEY) {
1044 struct smp_cmd_ident_addr_info addrinfo;
1045 struct smp_cmd_ident_info idinfo;
1047 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1049 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1051 /* The hci_conn contains the local identity address
1052 * after the connection has been established.
1054 * This is true even when the connection has been
1055 * established using a resolvable random address.
1057 bacpy(&addrinfo.bdaddr, &hcon->src);
1058 addrinfo.addr_type = hcon->src_type;
1060 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1063 *keydist &= ~SMP_DIST_ID_KEY;
1066 if (*keydist & SMP_DIST_SIGN) {
1067 struct smp_cmd_sign_info sign;
1068 struct smp_csrk *csrk;
1070 /* Generate a new random key */
1071 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1073 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1075 csrk->master = 0x00;
1076 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1078 smp->slave_csrk = csrk;
1080 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1082 *keydist &= ~SMP_DIST_SIGN;
1085 /* If there are still keys to be received wait for them */
1086 if (smp->remote_key_dist & KEY_DIST_MASK) {
1087 smp_allow_key_dist(smp);
1091 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1092 smp_notify_keys(conn);
1094 smp_chan_destroy(conn);
1097 static void smp_timeout(struct work_struct *work)
1099 struct smp_chan *smp = container_of(work, struct smp_chan,
1100 security_timer.work);
1101 struct l2cap_conn *conn = smp->conn;
1103 BT_DBG("conn %p", conn);
1105 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
1108 static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1110 struct l2cap_chan *chan = conn->smp;
1111 struct smp_chan *smp;
1113 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
1117 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1118 if (IS_ERR(smp->tfm_aes)) {
1119 BT_ERR("Unable to create ECB crypto context");
1124 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1125 if (IS_ERR(smp->tfm_cmac)) {
1126 BT_ERR("Unable to create CMAC crypto context");
1127 crypto_free_blkcipher(smp->tfm_aes);
1135 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1137 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1139 hci_conn_hold(conn->hcon);
1144 static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1146 struct hci_conn *hcon = smp->conn->hcon;
1147 u8 *na, *nb, a[7], b[7];
1157 memcpy(a, &hcon->init_addr, 6);
1158 memcpy(b, &hcon->resp_addr, 6);
1159 a[6] = hcon->init_addr_type;
1160 b[6] = hcon->resp_addr_type;
1162 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1165 static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1167 struct hci_conn *hcon = smp->conn->hcon;
1168 struct smp_cmd_dhkey_check check;
1169 u8 a[7], b[7], *local_addr, *remote_addr;
1170 u8 io_cap[3], r[16];
1173 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1174 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1176 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1177 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1181 memcpy(a, &hcon->init_addr, 6);
1182 memcpy(b, &hcon->resp_addr, 6);
1183 a[6] = hcon->init_addr_type;
1184 b[6] = hcon->resp_addr_type;
1189 memcpy(io_cap, &smp->preq[1], 3);
1193 memcpy(io_cap, &smp->prsp[1], 3);
1196 memcpy(r, &passkey, sizeof(passkey));
1197 memset(r + sizeof(passkey), 0, sizeof(r) - sizeof(passkey));
1199 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1200 local_addr, remote_addr, check.e);
1202 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
1207 int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1209 struct l2cap_conn *conn = hcon->l2cap_data;
1210 struct l2cap_chan *chan;
1211 struct smp_chan *smp;
1224 l2cap_chan_lock(chan);
1232 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1233 err = sc_user_reply(smp, mgmt_op, passkey);
1238 case MGMT_OP_USER_PASSKEY_REPLY:
1239 value = le32_to_cpu(passkey);
1240 memset(smp->tk, 0, sizeof(smp->tk));
1241 BT_DBG("PassKey: %d", value);
1242 put_unaligned_le32(value, smp->tk);
1244 case MGMT_OP_USER_CONFIRM_REPLY:
1245 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
1247 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1248 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1249 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
1253 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
1260 /* If it is our turn to send Pairing Confirm, do so now */
1261 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1262 u8 rsp = smp_confirm(smp);
1264 smp_failure(conn, rsp);
1268 l2cap_chan_unlock(chan);
1272 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
1274 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
1275 struct l2cap_chan *chan = conn->smp;
1276 struct hci_dev *hdev = conn->hcon->hdev;
1277 struct smp_chan *smp;
1278 u8 key_size, auth, sec_level;
1281 BT_DBG("conn %p", conn);
1283 if (skb->len < sizeof(*req))
1284 return SMP_INVALID_PARAMS;
1286 if (conn->hcon->role != HCI_ROLE_SLAVE)
1287 return SMP_CMD_NOTSUPP;
1290 smp = smp_chan_create(conn);
1295 return SMP_UNSPECIFIED;
1297 /* We didn't start the pairing, so match remote */
1298 auth = req->auth_req & AUTH_REQ_MASK(hdev);
1300 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
1301 (auth & SMP_AUTH_BONDING))
1302 return SMP_PAIRING_NOTSUPP;
1304 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1305 memcpy(&smp->preq[1], req, sizeof(*req));
1306 skb_pull(skb, sizeof(*req));
1308 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1309 sec_level = BT_SECURITY_MEDIUM;
1311 sec_level = authreq_to_seclevel(auth);
1313 if (sec_level > conn->hcon->pending_sec_level)
1314 conn->hcon->pending_sec_level = sec_level;
1316 /* If we need MITM check that it can be achieved */
1317 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1320 method = get_auth_method(smp, conn->hcon->io_capability,
1321 req->io_capability);
1322 if (method == JUST_WORKS || method == JUST_CFM)
1323 return SMP_AUTH_REQUIREMENTS;
1326 build_pairing_cmd(conn, req, &rsp, auth);
1328 if (rsp.auth_req & SMP_AUTH_SC)
1329 set_bit(SMP_FLAG_SC, &smp->flags);
1331 key_size = min(req->max_key_size, rsp.max_key_size);
1332 if (check_enc_key_size(conn, key_size))
1333 return SMP_ENC_KEY_SIZE;
1335 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1337 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1338 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1340 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1342 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1344 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1345 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1346 /* Clear bits which are generated but not distributed */
1347 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1348 /* Wait for Public Key from Initiating Device */
1351 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1354 /* Request setup of TK */
1355 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1357 return SMP_UNSPECIFIED;
1362 static u8 sc_send_public_key(struct smp_chan *smp)
1366 /* Generate local key pair for Secure Connections */
1367 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1368 return SMP_UNSPECIFIED;
1370 BT_DBG("Local Public Key X: %32phN", smp->local_pk);
1371 BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1372 BT_DBG("Local Private Key: %32phN", smp->local_sk);
1374 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1379 static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
1381 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
1382 struct l2cap_chan *chan = conn->smp;
1383 struct smp_chan *smp = chan->data;
1384 struct hci_dev *hdev = conn->hcon->hdev;
1388 BT_DBG("conn %p", conn);
1390 if (skb->len < sizeof(*rsp))
1391 return SMP_INVALID_PARAMS;
1393 if (conn->hcon->role != HCI_ROLE_MASTER)
1394 return SMP_CMD_NOTSUPP;
1396 skb_pull(skb, sizeof(*rsp));
1398 req = (void *) &smp->preq[1];
1400 key_size = min(req->max_key_size, rsp->max_key_size);
1401 if (check_enc_key_size(conn, key_size))
1402 return SMP_ENC_KEY_SIZE;
1404 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
1406 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1407 set_bit(SMP_FLAG_SC, &smp->flags);
1408 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1409 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
1411 /* If we need MITM check that it can be achieved */
1412 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1415 method = get_auth_method(smp, req->io_capability,
1416 rsp->io_capability);
1417 if (method == JUST_WORKS || method == JUST_CFM)
1418 return SMP_AUTH_REQUIREMENTS;
1421 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1423 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1424 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1426 /* Update remote key distribution in case the remote cleared
1427 * some bits that we had enabled in our request.
1429 smp->remote_key_dist &= rsp->resp_key_dist;
1431 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1432 /* Clear bits which are generated but not distributed */
1433 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1434 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1435 return sc_send_public_key(smp);
1438 auth |= req->auth_req;
1440 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
1442 return SMP_UNSPECIFIED;
1444 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1446 /* Can't compose response until we have been confirmed */
1447 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1448 return smp_confirm(smp);
1453 static u8 sc_check_confirm(struct smp_chan *smp)
1455 struct l2cap_conn *conn = smp->conn;
1459 /* Public Key exchange must happen before any other steps */
1460 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1461 return SMP_UNSPECIFIED;
1463 if (conn->hcon->out) {
1464 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1466 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1472 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
1474 struct l2cap_chan *chan = conn->smp;
1475 struct smp_chan *smp = chan->data;
1477 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1479 if (skb->len < sizeof(smp->pcnf))
1480 return SMP_INVALID_PARAMS;
1482 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1483 skb_pull(skb, sizeof(smp->pcnf));
1485 if (test_bit(SMP_FLAG_SC, &smp->flags))
1486 return sc_check_confirm(smp);
1488 if (conn->hcon->out) {
1489 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1491 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1495 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1496 return smp_confirm(smp);
1498 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1503 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
1505 struct l2cap_chan *chan = conn->smp;
1506 struct smp_chan *smp = chan->data;
1507 struct hci_conn *hcon = conn->hcon;
1508 u8 *pkax, *pkbx, *na, *nb;
1512 BT_DBG("conn %p", conn);
1514 if (skb->len < sizeof(smp->rrnd))
1515 return SMP_INVALID_PARAMS;
1517 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
1518 skb_pull(skb, sizeof(smp->rrnd));
1520 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1521 return smp_random(smp);
1526 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1529 return SMP_UNSPECIFIED;
1531 if (memcmp(smp->pcnf, cfm, 16))
1532 return SMP_CONFIRM_FAILED;
1534 pkax = smp->local_pk;
1535 pkbx = smp->remote_pk;
1539 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1541 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1543 pkax = smp->remote_pk;
1544 pkbx = smp->local_pk;
1549 /* Generate MacKey and LTK */
1550 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1552 return SMP_UNSPECIFIED;
1554 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
1556 return SMP_UNSPECIFIED;
1558 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
1559 hcon->type, hcon->dst_type,
1562 return SMP_UNSPECIFIED;
1567 static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
1569 struct smp_ltk *key;
1570 struct hci_conn *hcon = conn->hcon;
1572 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
1576 if (smp_ltk_sec_level(key) < sec_level)
1579 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1582 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
1583 hcon->enc_key_size = key->enc_size;
1585 /* We never store STKs for master role, so clear this flag */
1586 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
1591 bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1592 enum smp_key_pref key_pref)
1594 if (sec_level == BT_SECURITY_LOW)
1597 /* If we're encrypted with an STK but the caller prefers using
1598 * LTK claim insufficient security. This way we allow the
1599 * connection to be re-encrypted with an LTK, even if the LTK
1600 * provides the same level of security. Only exception is if we
1601 * don't have an LTK (e.g. because of key distribution bits).
1603 if (key_pref == SMP_USE_LTK &&
1604 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
1605 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
1608 if (hcon->sec_level >= sec_level)
1614 static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
1616 struct smp_cmd_security_req *rp = (void *) skb->data;
1617 struct smp_cmd_pairing cp;
1618 struct hci_conn *hcon = conn->hcon;
1619 struct hci_dev *hdev = hcon->hdev;
1620 struct smp_chan *smp;
1623 BT_DBG("conn %p", conn);
1625 if (skb->len < sizeof(*rp))
1626 return SMP_INVALID_PARAMS;
1628 if (hcon->role != HCI_ROLE_MASTER)
1629 return SMP_CMD_NOTSUPP;
1631 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
1633 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
1634 sec_level = BT_SECURITY_MEDIUM;
1636 sec_level = authreq_to_seclevel(auth);
1638 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
1641 if (sec_level > hcon->pending_sec_level)
1642 hcon->pending_sec_level = sec_level;
1644 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1647 smp = smp_chan_create(conn);
1649 return SMP_UNSPECIFIED;
1651 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
1652 (auth & SMP_AUTH_BONDING))
1653 return SMP_PAIRING_NOTSUPP;
1655 skb_pull(skb, sizeof(*rp));
1657 memset(&cp, 0, sizeof(cp));
1658 build_pairing_cmd(conn, &cp, NULL, auth);
1660 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1661 memcpy(&smp->preq[1], &cp, sizeof(cp));
1663 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1664 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
1669 int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
1671 struct l2cap_conn *conn = hcon->l2cap_data;
1672 struct l2cap_chan *chan;
1673 struct smp_chan *smp;
1677 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
1679 /* This may be NULL if there's an unexpected disconnection */
1685 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
1688 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
1691 if (sec_level > hcon->pending_sec_level)
1692 hcon->pending_sec_level = sec_level;
1694 if (hcon->role == HCI_ROLE_MASTER)
1695 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
1698 l2cap_chan_lock(chan);
1700 /* If SMP is already in progress ignore this request */
1706 smp = smp_chan_create(conn);
1712 authreq = seclevel_to_authreq(sec_level);
1714 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
1715 authreq |= SMP_AUTH_SC;
1717 /* Require MITM if IO Capability allows or the security level
1720 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
1721 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
1722 authreq |= SMP_AUTH_MITM;
1724 if (hcon->role == HCI_ROLE_MASTER) {
1725 struct smp_cmd_pairing cp;
1727 build_pairing_cmd(conn, &cp, NULL, authreq);
1728 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1729 memcpy(&smp->preq[1], &cp, sizeof(cp));
1731 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
1732 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
1734 struct smp_cmd_security_req cp;
1735 cp.auth_req = authreq;
1736 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
1737 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
1740 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
1744 l2cap_chan_unlock(chan);
1748 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
1750 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
1751 struct l2cap_chan *chan = conn->smp;
1752 struct smp_chan *smp = chan->data;
1754 BT_DBG("conn %p", conn);
1756 if (skb->len < sizeof(*rp))
1757 return SMP_INVALID_PARAMS;
1759 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
1761 skb_pull(skb, sizeof(*rp));
1763 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
1768 static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
1770 struct smp_cmd_master_ident *rp = (void *) skb->data;
1771 struct l2cap_chan *chan = conn->smp;
1772 struct smp_chan *smp = chan->data;
1773 struct hci_dev *hdev = conn->hcon->hdev;
1774 struct hci_conn *hcon = conn->hcon;
1775 struct smp_ltk *ltk;
1778 BT_DBG("conn %p", conn);
1780 if (skb->len < sizeof(*rp))
1781 return SMP_INVALID_PARAMS;
1783 /* Mark the information as received */
1784 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
1786 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1787 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1788 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1789 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1791 skb_pull(skb, sizeof(*rp));
1793 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
1794 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
1795 authenticated, smp->tk, smp->enc_key_size,
1796 rp->ediv, rp->rand);
1798 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1799 smp_distribute_keys(smp);
1804 static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
1806 struct smp_cmd_ident_info *info = (void *) skb->data;
1807 struct l2cap_chan *chan = conn->smp;
1808 struct smp_chan *smp = chan->data;
1812 if (skb->len < sizeof(*info))
1813 return SMP_INVALID_PARAMS;
1815 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
1817 skb_pull(skb, sizeof(*info));
1819 memcpy(smp->irk, info->irk, 16);
1824 static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
1825 struct sk_buff *skb)
1827 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
1828 struct l2cap_chan *chan = conn->smp;
1829 struct smp_chan *smp = chan->data;
1830 struct hci_conn *hcon = conn->hcon;
1835 if (skb->len < sizeof(*info))
1836 return SMP_INVALID_PARAMS;
1838 /* Mark the information as received */
1839 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
1841 if (smp->remote_key_dist & SMP_DIST_SIGN)
1842 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1844 skb_pull(skb, sizeof(*info));
1846 /* Strictly speaking the Core Specification (4.1) allows sending
1847 * an empty address which would force us to rely on just the IRK
1848 * as "identity information". However, since such
1849 * implementations are not known of and in order to not over
1850 * complicate our implementation, simply pretend that we never
1851 * received an IRK for such a device.
1853 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
1854 BT_ERR("Ignoring IRK with no identity address");
1858 bacpy(&smp->id_addr, &info->bdaddr);
1859 smp->id_addr_type = info->addr_type;
1861 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
1862 bacpy(&rpa, &hcon->dst);
1864 bacpy(&rpa, BDADDR_ANY);
1866 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
1867 smp->id_addr_type, smp->irk, &rpa);
1870 if (!(smp->remote_key_dist & KEY_DIST_MASK))
1871 smp_distribute_keys(smp);
1876 static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
1878 struct smp_cmd_sign_info *rp = (void *) skb->data;
1879 struct l2cap_chan *chan = conn->smp;
1880 struct smp_chan *smp = chan->data;
1881 struct smp_csrk *csrk;
1883 BT_DBG("conn %p", conn);
1885 if (skb->len < sizeof(*rp))
1886 return SMP_INVALID_PARAMS;
1888 /* Mark the information as received */
1889 smp->remote_key_dist &= ~SMP_DIST_SIGN;
1891 skb_pull(skb, sizeof(*rp));
1893 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1895 csrk->master = 0x01;
1896 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
1899 smp_distribute_keys(smp);
1904 static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
1906 struct smp_cmd_public_key *key = (void *) skb->data;
1907 struct hci_conn *hcon = conn->hcon;
1908 struct l2cap_chan *chan = conn->smp;
1909 struct smp_chan *smp = chan->data;
1910 struct smp_cmd_pairing_confirm cfm;
1913 BT_DBG("conn %p", conn);
1915 if (skb->len < sizeof(*key))
1916 return SMP_INVALID_PARAMS;
1918 memcpy(smp->remote_pk, key, 64);
1920 /* Non-initiating device sends its public key after receiving
1921 * the key from the initiating device.
1924 err = sc_send_public_key(smp);
1929 BT_DBG("Remote Public Key X: %32phN", smp->remote_pk);
1930 BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
1932 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
1933 return SMP_UNSPECIFIED;
1935 BT_DBG("DHKey %32phN", smp->dhkey);
1937 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
1939 /* The Initiating device waits for the non-initiating device to
1940 * send the confirm value.
1942 if (conn->hcon->out)
1945 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
1946 0, cfm.confirm_val);
1948 return SMP_UNSPECIFIED;
1950 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1951 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1956 static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
1958 struct smp_cmd_dhkey_check *check = (void *) skb->data;
1959 struct l2cap_chan *chan = conn->smp;
1960 struct hci_conn *hcon = conn->hcon;
1961 struct smp_chan *smp = chan->data;
1962 u8 a[7], b[7], *local_addr, *remote_addr;
1963 u8 io_cap[3], r[16], e[16];
1966 BT_DBG("conn %p", conn);
1968 if (skb->len < sizeof(*check))
1969 return SMP_INVALID_PARAMS;
1971 memcpy(a, &hcon->init_addr, 6);
1972 memcpy(b, &hcon->resp_addr, 6);
1973 a[6] = hcon->init_addr_type;
1974 b[6] = hcon->resp_addr_type;
1979 memcpy(io_cap, &smp->prsp[1], 3);
1983 memcpy(io_cap, &smp->preq[1], 3);
1986 memset(r, 0, sizeof(r));
1988 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
1989 io_cap, remote_addr, local_addr, e);
1991 return SMP_UNSPECIFIED;
1993 if (memcmp(check->e, e, 16))
1994 return SMP_DHKEY_CHECK_FAILED;
1996 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1997 SMP_LTK_P256, 0, smp->tk, smp->enc_key_size,
2001 hci_le_start_enc(hcon, 0, 0, smp->tk);
2002 hcon->enc_key_size = smp->enc_key_size;
2008 static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
2010 struct l2cap_conn *conn = chan->conn;
2011 struct hci_conn *hcon = conn->hcon;
2012 struct smp_chan *smp;
2016 if (hcon->type != LE_LINK) {
2024 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
2025 reason = SMP_PAIRING_NOTSUPP;
2029 code = skb->data[0];
2030 skb_pull(skb, sizeof(code));
2034 if (code > SMP_CMD_MAX)
2037 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
2040 /* If we don't have a context the only allowed commands are
2041 * pairing request and security request.
2043 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2047 case SMP_CMD_PAIRING_REQ:
2048 reason = smp_cmd_pairing_req(conn, skb);
2051 case SMP_CMD_PAIRING_FAIL:
2052 smp_failure(conn, 0);
2056 case SMP_CMD_PAIRING_RSP:
2057 reason = smp_cmd_pairing_rsp(conn, skb);
2060 case SMP_CMD_SECURITY_REQ:
2061 reason = smp_cmd_security_req(conn, skb);
2064 case SMP_CMD_PAIRING_CONFIRM:
2065 reason = smp_cmd_pairing_confirm(conn, skb);
2068 case SMP_CMD_PAIRING_RANDOM:
2069 reason = smp_cmd_pairing_random(conn, skb);
2072 case SMP_CMD_ENCRYPT_INFO:
2073 reason = smp_cmd_encrypt_info(conn, skb);
2076 case SMP_CMD_MASTER_IDENT:
2077 reason = smp_cmd_master_ident(conn, skb);
2080 case SMP_CMD_IDENT_INFO:
2081 reason = smp_cmd_ident_info(conn, skb);
2084 case SMP_CMD_IDENT_ADDR_INFO:
2085 reason = smp_cmd_ident_addr_info(conn, skb);
2088 case SMP_CMD_SIGN_INFO:
2089 reason = smp_cmd_sign_info(conn, skb);
2092 case SMP_CMD_PUBLIC_KEY:
2093 reason = smp_cmd_public_key(conn, skb);
2096 case SMP_CMD_DHKEY_CHECK:
2097 reason = smp_cmd_dhkey_check(conn, skb);
2101 BT_DBG("Unknown command code 0x%2.2x", code);
2102 reason = SMP_CMD_NOTSUPP;
2109 smp_failure(conn, reason);
2116 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2122 static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2124 struct l2cap_conn *conn = chan->conn;
2126 BT_DBG("chan %p", chan);
2129 smp_chan_destroy(conn);
2132 l2cap_chan_put(chan);
2135 static void smp_resume_cb(struct l2cap_chan *chan)
2137 struct smp_chan *smp = chan->data;
2138 struct l2cap_conn *conn = chan->conn;
2139 struct hci_conn *hcon = conn->hcon;
2141 BT_DBG("chan %p", chan);
2146 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2149 cancel_delayed_work(&smp->security_timer);
2151 smp_distribute_keys(smp);
2154 static void smp_ready_cb(struct l2cap_chan *chan)
2156 struct l2cap_conn *conn = chan->conn;
2158 BT_DBG("chan %p", chan);
2161 l2cap_chan_hold(chan);
2164 static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2168 BT_DBG("chan %p", chan);
2170 err = smp_sig_channel(chan, skb);
2172 struct smp_chan *smp = chan->data;
2175 cancel_delayed_work_sync(&smp->security_timer);
2177 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
2183 static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2184 unsigned long hdr_len,
2185 unsigned long len, int nb)
2187 struct sk_buff *skb;
2189 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2191 return ERR_PTR(-ENOMEM);
2193 skb->priority = HCI_PRIO_MAX;
2194 bt_cb(skb)->chan = chan;
2199 static const struct l2cap_ops smp_chan_ops = {
2200 .name = "Security Manager",
2201 .ready = smp_ready_cb,
2202 .recv = smp_recv_cb,
2203 .alloc_skb = smp_alloc_skb_cb,
2204 .teardown = smp_teardown_cb,
2205 .resume = smp_resume_cb,
2207 .new_connection = l2cap_chan_no_new_connection,
2208 .state_change = l2cap_chan_no_state_change,
2209 .close = l2cap_chan_no_close,
2210 .defer = l2cap_chan_no_defer,
2211 .suspend = l2cap_chan_no_suspend,
2212 .set_shutdown = l2cap_chan_no_set_shutdown,
2213 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2214 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2217 static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2219 struct l2cap_chan *chan;
2221 BT_DBG("pchan %p", pchan);
2223 chan = l2cap_chan_create();
2227 chan->chan_type = pchan->chan_type;
2228 chan->ops = &smp_chan_ops;
2229 chan->scid = pchan->scid;
2230 chan->dcid = chan->scid;
2231 chan->imtu = pchan->imtu;
2232 chan->omtu = pchan->omtu;
2233 chan->mode = pchan->mode;
2235 /* Other L2CAP channels may request SMP routines in order to
2236 * change the security level. This means that the SMP channel
2237 * lock must be considered in its own category to avoid lockdep
2240 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2242 BT_DBG("created chan %p", chan);
2247 static const struct l2cap_ops smp_root_chan_ops = {
2248 .name = "Security Manager Root",
2249 .new_connection = smp_new_conn_cb,
2251 /* None of these are implemented for the root channel */
2252 .close = l2cap_chan_no_close,
2253 .alloc_skb = l2cap_chan_no_alloc_skb,
2254 .recv = l2cap_chan_no_recv,
2255 .state_change = l2cap_chan_no_state_change,
2256 .teardown = l2cap_chan_no_teardown,
2257 .ready = l2cap_chan_no_ready,
2258 .defer = l2cap_chan_no_defer,
2259 .suspend = l2cap_chan_no_suspend,
2260 .resume = l2cap_chan_no_resume,
2261 .set_shutdown = l2cap_chan_no_set_shutdown,
2262 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
2263 .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
2266 int smp_register(struct hci_dev *hdev)
2268 struct l2cap_chan *chan;
2269 struct crypto_blkcipher *tfm_aes;
2271 BT_DBG("%s", hdev->name);
2273 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
2274 if (IS_ERR(tfm_aes)) {
2275 int err = PTR_ERR(tfm_aes);
2276 BT_ERR("Unable to create crypto context");
2280 chan = l2cap_chan_create();
2282 crypto_free_blkcipher(tfm_aes);
2286 chan->data = tfm_aes;
2288 l2cap_add_scid(chan, L2CAP_CID_SMP);
2290 l2cap_chan_set_defaults(chan);
2292 bacpy(&chan->src, &hdev->bdaddr);
2293 chan->src_type = BDADDR_LE_PUBLIC;
2294 chan->state = BT_LISTEN;
2295 chan->mode = L2CAP_MODE_BASIC;
2296 chan->imtu = L2CAP_DEFAULT_MTU;
2297 chan->ops = &smp_root_chan_ops;
2299 /* Set correct nesting level for a parent/listening channel */
2300 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2302 hdev->smp_data = chan;
2307 void smp_unregister(struct hci_dev *hdev)
2309 struct l2cap_chan *chan = hdev->smp_data;
2310 struct crypto_blkcipher *tfm_aes;
2315 BT_DBG("%s chan %p", hdev->name, chan);
2317 tfm_aes = chan->data;
2320 crypto_free_blkcipher(tfm_aes);
2323 hdev->smp_data = NULL;
2324 l2cap_chan_put(chan);