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 <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
25 #include <net/bluetooth/l2cap.h>
26 #include <net/bluetooth/smp.h>
27 #include <linux/crypto.h>
28 #include <linux/scatterlist.h>
29 #include <crypto/b128ops.h>
31 #define SMP_TIMEOUT 30000 /* 30 seconds */
33 static inline void swap128(u8 src[16], u8 dst[16])
36 for (i = 0; i < 16; i++)
40 static inline void swap56(u8 src[7], u8 dst[7])
43 for (i = 0; i < 7; i++)
47 static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
49 struct blkcipher_desc desc;
50 struct scatterlist sg;
52 unsigned char iv[128];
55 BT_ERR("tfm %p", tfm);
62 err = crypto_blkcipher_setkey(tfm, k, 16);
64 BT_ERR("cipher setkey failed: %d", err);
68 sg_init_one(&sg, r, 16);
70 iv_len = crypto_blkcipher_ivsize(tfm);
72 memset(&iv, 0xff, iv_len);
73 crypto_blkcipher_set_iv(tfm, iv, iv_len);
76 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
78 BT_ERR("Encrypt data error %d", err);
83 static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
84 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,
85 u8 _rat, bdaddr_t *ra, u8 res[16])
92 /* p1 = pres || preq || _rat || _iat */
100 /* p2 = padding || ia || ra */
101 baswap((bdaddr_t *) (p2 + 4), ia);
102 baswap((bdaddr_t *) (p2 + 10), ra);
105 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
107 /* res = e(k, res) */
108 err = smp_e(tfm, k, res);
110 BT_ERR("Encrypt data error");
114 /* res = res XOR p2 */
115 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
117 /* res = e(k, res) */
118 err = smp_e(tfm, k, res);
120 BT_ERR("Encrypt data error");
125 static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16],
126 u8 r1[16], u8 r2[16], u8 _r[16])
130 /* Just least significant octets from r1 and r2 are considered */
131 memcpy(_r, r1 + 8, 8);
132 memcpy(_r + 8, r2 + 8, 8);
134 err = smp_e(tfm, k, _r);
136 BT_ERR("Encrypt data error");
141 static int smp_rand(u8 *buf)
143 get_random_bytes(buf, 16);
148 static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
149 u16 dlen, void *data)
152 struct l2cap_hdr *lh;
155 len = L2CAP_HDR_SIZE + sizeof(code) + dlen;
160 skb = bt_skb_alloc(len, GFP_ATOMIC);
164 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
165 lh->len = cpu_to_le16(sizeof(code) + dlen);
166 lh->cid = cpu_to_le16(L2CAP_CID_SMP);
168 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code));
170 memcpy(skb_put(skb, dlen), data, dlen);
175 static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
177 struct sk_buff *skb = smp_build_cmd(conn, code, len, data);
179 BT_DBG("code 0x%2.2x", code);
184 skb->priority = HCI_PRIO_MAX;
185 hci_send_acl(conn->hchan, skb, 0);
187 mod_timer(&conn->security_timer, jiffies +
188 msecs_to_jiffies(SMP_TIMEOUT));
191 static void build_pairing_cmd(struct l2cap_conn *conn,
192 struct smp_cmd_pairing *req,
193 struct smp_cmd_pairing *rsp,
199 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->flags)) {
200 dist_keys = SMP_DIST_ENC_KEY;
201 authreq |= SMP_AUTH_BONDING;
205 req->io_capability = conn->hcon->io_capability;
206 req->oob_flag = SMP_OOB_NOT_PRESENT;
207 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
208 req->init_key_dist = dist_keys;
209 req->resp_key_dist = dist_keys;
210 req->auth_req = authreq;
214 rsp->io_capability = conn->hcon->io_capability;
215 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
216 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
217 rsp->init_key_dist = req->init_key_dist & dist_keys;
218 rsp->resp_key_dist = req->resp_key_dist & dist_keys;
219 rsp->auth_req = authreq;
222 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
224 struct smp_chan *smp = conn->smp_chan;
226 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
227 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
228 return SMP_ENC_KEY_SIZE;
230 smp->smp_key_size = max_key_size;
235 static void smp_failure(struct l2cap_conn *conn, u8 reason, u8 send)
238 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
241 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->hcon->pend);
242 mgmt_auth_failed(conn->hcon->hdev, conn->dst, reason);
243 del_timer(&conn->security_timer);
244 smp_chan_destroy(conn);
247 static void confirm_work(struct work_struct *work)
249 struct smp_chan *smp = container_of(work, struct smp_chan, confirm);
250 struct l2cap_conn *conn = smp->conn;
251 struct crypto_blkcipher *tfm;
252 struct smp_cmd_pairing_confirm cp;
256 BT_DBG("conn %p", conn);
258 tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
260 reason = SMP_UNSPECIFIED;
267 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0,
268 conn->src, conn->hcon->dst_type, conn->dst,
271 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
272 conn->hcon->dst_type, conn->dst, 0, conn->src,
275 reason = SMP_UNSPECIFIED;
279 swap128(res, cp.confirm_val);
280 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
285 smp_failure(conn, reason, 1);
288 static void random_work(struct work_struct *work)
290 struct smp_chan *smp = container_of(work, struct smp_chan, random);
291 struct l2cap_conn *conn = smp->conn;
292 struct hci_conn *hcon = conn->hcon;
293 struct crypto_blkcipher *tfm = smp->tfm;
294 u8 reason, confirm[16], res[16], key[16];
297 if (IS_ERR_OR_NULL(tfm)) {
298 reason = SMP_UNSPECIFIED;
302 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
305 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, 0,
306 conn->src, hcon->dst_type, conn->dst,
309 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
310 hcon->dst_type, conn->dst, 0, conn->src,
313 reason = SMP_UNSPECIFIED;
317 swap128(res, confirm);
319 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
320 BT_ERR("Pairing failed (confirmation values mismatch)");
321 reason = SMP_CONFIRM_FAILED;
329 memset(rand, 0, sizeof(rand));
332 smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key);
335 memset(stk + smp->smp_key_size, 0,
336 SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size);
338 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend)) {
339 reason = SMP_UNSPECIFIED;
343 hci_le_start_enc(hcon, ediv, rand, stk);
344 hcon->enc_key_size = smp->smp_key_size;
346 u8 stk[16], r[16], rand[8];
349 memset(rand, 0, sizeof(rand));
352 swap128(smp->prnd, r);
353 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
355 smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key);
358 memset(stk + smp->smp_key_size, 0,
359 SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size);
361 hci_add_ltk(hcon->hdev, 0, conn->dst, smp->smp_key_size,
368 smp_failure(conn, reason, 1);
371 static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
373 struct smp_chan *smp;
375 smp = kzalloc(sizeof(struct smp_chan), GFP_ATOMIC);
379 INIT_WORK(&smp->confirm, confirm_work);
380 INIT_WORK(&smp->random, random_work);
383 conn->smp_chan = smp;
385 hci_conn_hold(conn->hcon);
390 void smp_chan_destroy(struct l2cap_conn *conn)
392 struct smp_chan *smp = conn->smp_chan;
394 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend);
397 crypto_free_blkcipher(smp->tfm);
400 conn->smp_chan = NULL;
401 hci_conn_put(conn->hcon);
404 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
406 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
407 struct smp_chan *smp;
411 BT_DBG("conn %p", conn);
413 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend))
414 smp = smp_chan_create(conn);
416 smp = conn->smp_chan;
418 smp->preq[0] = SMP_CMD_PAIRING_REQ;
419 memcpy(&smp->preq[1], req, sizeof(*req));
420 skb_pull(skb, sizeof(*req));
423 return SMP_OOB_NOT_AVAIL;
425 /* We didn't start the pairing, so no requirements */
426 build_pairing_cmd(conn, req, &rsp, SMP_AUTH_NONE);
428 key_size = min(req->max_key_size, rsp.max_key_size);
429 if (check_enc_key_size(conn, key_size))
430 return SMP_ENC_KEY_SIZE;
433 memset(smp->tk, 0, sizeof(smp->tk));
435 ret = smp_rand(smp->prnd);
437 return SMP_UNSPECIFIED;
439 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
440 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
442 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
447 static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
449 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
450 struct smp_chan *smp = conn->smp_chan;
451 struct hci_dev *hdev = conn->hcon->hdev;
455 BT_DBG("conn %p", conn);
457 skb_pull(skb, sizeof(*rsp));
459 req = (void *) &smp->preq[1];
461 key_size = min(req->max_key_size, rsp->max_key_size);
462 if (check_enc_key_size(conn, key_size))
463 return SMP_ENC_KEY_SIZE;
466 return SMP_OOB_NOT_AVAIL;
469 memset(smp->tk, 0, sizeof(smp->tk));
471 ret = smp_rand(smp->prnd);
473 return SMP_UNSPECIFIED;
475 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
476 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
478 queue_work(hdev->workqueue, &smp->confirm);
483 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
485 struct smp_chan *smp = conn->smp_chan;
486 struct hci_dev *hdev = conn->hcon->hdev;
488 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
490 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
491 skb_pull(skb, sizeof(smp->pcnf));
493 if (conn->hcon->out) {
496 swap128(smp->prnd, random);
497 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random),
500 queue_work(hdev->workqueue, &smp->confirm);
506 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
508 struct smp_chan *smp = conn->smp_chan;
509 struct hci_dev *hdev = conn->hcon->hdev;
511 BT_DBG("conn %p", conn);
513 swap128(skb->data, smp->rrnd);
514 skb_pull(skb, sizeof(smp->rrnd));
516 queue_work(hdev->workqueue, &smp->random);
521 static u8 smp_ltk_encrypt(struct l2cap_conn *conn)
523 struct link_key *key;
524 struct key_master_id *master;
525 struct hci_conn *hcon = conn->hcon;
527 key = hci_find_link_key_type(hcon->hdev, conn->dst,
532 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND,
536 master = (void *) key->data;
537 hci_le_start_enc(hcon, master->ediv, master->rand,
539 hcon->enc_key_size = key->pin_len;
544 static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
546 struct smp_cmd_security_req *rp = (void *) skb->data;
547 struct smp_cmd_pairing cp;
548 struct hci_conn *hcon = conn->hcon;
549 struct smp_chan *smp;
551 BT_DBG("conn %p", conn);
553 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
555 if (smp_ltk_encrypt(conn))
558 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend))
561 smp = smp_chan_create(conn);
563 skb_pull(skb, sizeof(*rp));
565 memset(&cp, 0, sizeof(cp));
566 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
568 smp->preq[0] = SMP_CMD_PAIRING_REQ;
569 memcpy(&smp->preq[1], &cp, sizeof(cp));
571 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
576 int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
578 struct hci_conn *hcon = conn->hcon;
579 struct smp_chan *smp = conn->smp_chan;
581 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
583 if (!lmp_host_le_capable(hcon->hdev))
586 if (sec_level == BT_SECURITY_LOW)
589 if (hcon->sec_level >= sec_level)
592 if (hcon->link_mode & HCI_LM_MASTER)
593 if (smp_ltk_encrypt(conn))
596 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend))
599 smp = smp_chan_create(conn);
601 if (hcon->link_mode & HCI_LM_MASTER) {
602 struct smp_cmd_pairing cp;
604 build_pairing_cmd(conn, &cp, NULL, SMP_AUTH_NONE);
605 smp->preq[0] = SMP_CMD_PAIRING_REQ;
606 memcpy(&smp->preq[1], &cp, sizeof(cp));
608 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
610 struct smp_cmd_security_req cp;
611 cp.auth_req = SMP_AUTH_NONE;
612 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
616 hcon->pending_sec_level = sec_level;
621 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
623 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
624 struct smp_chan *smp = conn->smp_chan;
626 skb_pull(skb, sizeof(*rp));
628 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
633 static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
635 struct smp_cmd_master_ident *rp = (void *) skb->data;
636 struct smp_chan *smp = conn->smp_chan;
638 skb_pull(skb, sizeof(*rp));
640 hci_add_ltk(conn->hcon->hdev, 1, conn->src, smp->smp_key_size,
641 rp->ediv, rp->rand, smp->tk);
643 smp_distribute_keys(conn, 1);
648 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
650 __u8 code = skb->data[0];
654 if (!lmp_host_le_capable(conn->hcon->hdev)) {
656 reason = SMP_PAIRING_NOTSUPP;
660 skb_pull(skb, sizeof(code));
663 case SMP_CMD_PAIRING_REQ:
664 reason = smp_cmd_pairing_req(conn, skb);
667 case SMP_CMD_PAIRING_FAIL:
668 smp_failure(conn, skb->data[0], 0);
673 case SMP_CMD_PAIRING_RSP:
674 reason = smp_cmd_pairing_rsp(conn, skb);
677 case SMP_CMD_SECURITY_REQ:
678 reason = smp_cmd_security_req(conn, skb);
681 case SMP_CMD_PAIRING_CONFIRM:
682 reason = smp_cmd_pairing_confirm(conn, skb);
685 case SMP_CMD_PAIRING_RANDOM:
686 reason = smp_cmd_pairing_random(conn, skb);
689 case SMP_CMD_ENCRYPT_INFO:
690 reason = smp_cmd_encrypt_info(conn, skb);
693 case SMP_CMD_MASTER_IDENT:
694 reason = smp_cmd_master_ident(conn, skb);
697 case SMP_CMD_IDENT_INFO:
698 case SMP_CMD_IDENT_ADDR_INFO:
699 case SMP_CMD_SIGN_INFO:
705 BT_DBG("Unknown command code 0x%2.2x", code);
707 reason = SMP_CMD_NOTSUPP;
714 smp_failure(conn, reason, 1);
720 int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
722 struct smp_cmd_pairing *req, *rsp;
723 struct smp_chan *smp = conn->smp_chan;
726 BT_DBG("conn %p force %d", conn, force);
728 if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend))
731 rsp = (void *) &smp->prsp[1];
733 /* The responder sends its keys first */
734 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07))
737 req = (void *) &smp->preq[1];
739 if (conn->hcon->out) {
740 keydist = &rsp->init_key_dist;
741 *keydist &= req->init_key_dist;
743 keydist = &rsp->resp_key_dist;
744 *keydist &= req->resp_key_dist;
748 BT_DBG("keydist 0x%x", *keydist);
750 if (*keydist & SMP_DIST_ENC_KEY) {
751 struct smp_cmd_encrypt_info enc;
752 struct smp_cmd_master_ident ident;
755 get_random_bytes(enc.ltk, sizeof(enc.ltk));
756 get_random_bytes(&ediv, sizeof(ediv));
757 get_random_bytes(ident.rand, sizeof(ident.rand));
759 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
761 hci_add_ltk(conn->hcon->hdev, 1, conn->dst, smp->smp_key_size,
762 ediv, ident.rand, enc.ltk);
764 ident.ediv = cpu_to_le16(ediv);
766 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
768 *keydist &= ~SMP_DIST_ENC_KEY;
771 if (*keydist & SMP_DIST_ID_KEY) {
772 struct smp_cmd_ident_addr_info addrinfo;
773 struct smp_cmd_ident_info idinfo;
775 /* Send a dummy key */
776 get_random_bytes(idinfo.irk, sizeof(idinfo.irk));
778 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
780 /* Just public address */
781 memset(&addrinfo, 0, sizeof(addrinfo));
782 bacpy(&addrinfo.bdaddr, conn->src);
784 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
787 *keydist &= ~SMP_DIST_ID_KEY;
790 if (*keydist & SMP_DIST_SIGN) {
791 struct smp_cmd_sign_info sign;
793 /* Send a dummy key */
794 get_random_bytes(sign.csrk, sizeof(sign.csrk));
796 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
798 *keydist &= ~SMP_DIST_SIGN;
801 if (conn->hcon->out || force) {
802 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend);
803 del_timer(&conn->security_timer);
804 smp_chan_destroy(conn);