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 hci_send_acl(conn->hcon, skb, 0);
187 static __u8 seclevel_to_authreq(__u8 level)
190 case BT_SECURITY_HIGH:
191 /* Right now we don't support bonding */
192 return SMP_AUTH_MITM;
195 return SMP_AUTH_NONE;
199 static void build_pairing_cmd(struct l2cap_conn *conn,
200 struct smp_cmd_pairing *req,
201 struct smp_cmd_pairing *rsp,
207 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->flags)) {
208 dist_keys = SMP_DIST_ENC_KEY | SMP_DIST_ID_KEY | SMP_DIST_SIGN;
209 authreq |= SMP_AUTH_BONDING;
213 req->io_capability = conn->hcon->io_capability;
214 req->oob_flag = SMP_OOB_NOT_PRESENT;
215 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
216 req->init_key_dist = dist_keys;
217 req->resp_key_dist = dist_keys;
218 req->auth_req = authreq;
222 rsp->io_capability = conn->hcon->io_capability;
223 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
224 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
225 rsp->init_key_dist = req->init_key_dist & dist_keys;
226 rsp->resp_key_dist = req->resp_key_dist & dist_keys;
227 rsp->auth_req = authreq;
230 static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
232 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
233 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
234 return SMP_ENC_KEY_SIZE;
236 conn->smp_key_size = max_key_size;
241 static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
243 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
246 BT_DBG("conn %p", conn);
248 conn->preq[0] = SMP_CMD_PAIRING_REQ;
249 memcpy(&conn->preq[1], req, sizeof(*req));
250 skb_pull(skb, sizeof(*req));
253 return SMP_OOB_NOT_AVAIL;
255 /* We didn't start the pairing, so no requirements */
256 build_pairing_cmd(conn, req, &rsp, SMP_AUTH_NONE);
258 key_size = min(req->max_key_size, rsp.max_key_size);
259 if (check_enc_key_size(conn, key_size))
260 return SMP_ENC_KEY_SIZE;
263 memset(conn->tk, 0, sizeof(conn->tk));
265 conn->prsp[0] = SMP_CMD_PAIRING_RSP;
266 memcpy(&conn->prsp[1], &rsp, sizeof(rsp));
268 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
270 mod_timer(&conn->security_timer, jiffies +
271 msecs_to_jiffies(SMP_TIMEOUT));
276 static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
278 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
279 struct smp_cmd_pairing_confirm cp;
280 struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm;
282 u8 res[16], key_size;
284 BT_DBG("conn %p", conn);
286 skb_pull(skb, sizeof(*rsp));
288 req = (void *) &conn->preq[1];
290 key_size = min(req->max_key_size, rsp->max_key_size);
291 if (check_enc_key_size(conn, key_size))
292 return SMP_ENC_KEY_SIZE;
295 return SMP_OOB_NOT_AVAIL;
298 memset(conn->tk, 0, sizeof(conn->tk));
300 conn->prsp[0] = SMP_CMD_PAIRING_RSP;
301 memcpy(&conn->prsp[1], rsp, sizeof(*rsp));
303 ret = smp_rand(conn->prnd);
305 return SMP_UNSPECIFIED;
307 ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp, 0,
308 conn->src, conn->hcon->dst_type, conn->dst, res);
310 return SMP_UNSPECIFIED;
312 swap128(res, cp.confirm_val);
314 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
319 static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
321 struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm;
323 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
325 memcpy(conn->pcnf, skb->data, sizeof(conn->pcnf));
326 skb_pull(skb, sizeof(conn->pcnf));
328 if (conn->hcon->out) {
331 swap128(conn->prnd, random);
332 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random),
335 struct smp_cmd_pairing_confirm cp;
339 ret = smp_rand(conn->prnd);
341 return SMP_UNSPECIFIED;
343 ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp,
344 conn->hcon->dst_type, conn->dst,
347 return SMP_CONFIRM_FAILED;
349 swap128(res, cp.confirm_val);
351 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
354 mod_timer(&conn->security_timer, jiffies +
355 msecs_to_jiffies(SMP_TIMEOUT));
360 static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
362 struct hci_conn *hcon = conn->hcon;
363 struct crypto_blkcipher *tfm = hcon->hdev->tfm;
365 u8 key[16], res[16], random[16], confirm[16];
367 swap128(skb->data, random);
368 skb_pull(skb, sizeof(random));
371 ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp, 0,
372 conn->src, conn->hcon->dst_type, conn->dst,
375 ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp,
376 conn->hcon->dst_type, conn->dst, 0, conn->src,
379 return SMP_UNSPECIFIED;
381 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
383 swap128(res, confirm);
385 if (memcmp(conn->pcnf, confirm, sizeof(conn->pcnf)) != 0) {
386 BT_ERR("Pairing failed (confirmation values mismatch)");
387 return SMP_CONFIRM_FAILED;
390 if (conn->hcon->out) {
394 memset(rand, 0, sizeof(rand));
397 smp_s1(tfm, conn->tk, random, conn->prnd, key);
400 memset(stk + conn->smp_key_size, 0,
401 SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size);
403 hci_le_start_enc(hcon, ediv, rand, stk);
405 u8 stk[16], r[16], rand[8];
408 memset(rand, 0, sizeof(rand));
411 swap128(conn->prnd, r);
412 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
414 smp_s1(tfm, conn->tk, conn->prnd, random, key);
417 memset(stk + conn->smp_key_size, 0,
418 SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size);
420 hci_add_ltk(conn->hcon->hdev, 0, conn->dst, ediv, rand, stk);
426 static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
428 struct smp_cmd_security_req *rp = (void *) skb->data;
429 struct smp_cmd_pairing cp;
430 struct hci_conn *hcon = conn->hcon;
432 BT_DBG("conn %p", conn);
434 if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend))
437 skb_pull(skb, sizeof(*rp));
439 memset(&cp, 0, sizeof(cp));
440 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
442 conn->preq[0] = SMP_CMD_PAIRING_REQ;
443 memcpy(&conn->preq[1], &cp, sizeof(cp));
445 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
447 mod_timer(&conn->security_timer, jiffies +
448 msecs_to_jiffies(SMP_TIMEOUT));
450 set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend);
455 int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
457 struct hci_conn *hcon = conn->hcon;
460 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
462 if (!lmp_host_le_capable(hcon->hdev))
465 if (IS_ERR(hcon->hdev->tfm))
468 if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend))
471 if (sec_level == BT_SECURITY_LOW)
474 if (hcon->sec_level >= sec_level)
477 authreq = seclevel_to_authreq(sec_level);
479 if (hcon->link_mode & HCI_LM_MASTER) {
480 struct smp_cmd_pairing cp;
481 struct link_key *key;
483 key = hci_find_link_key_type(hcon->hdev, conn->dst,
486 struct key_master_id *master = (void *) key->data;
488 hci_le_start_enc(hcon, master->ediv, master->rand,
493 build_pairing_cmd(conn, &cp, NULL, authreq);
494 conn->preq[0] = SMP_CMD_PAIRING_REQ;
495 memcpy(&conn->preq[1], &cp, sizeof(cp));
497 mod_timer(&conn->security_timer, jiffies +
498 msecs_to_jiffies(SMP_TIMEOUT));
500 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
502 struct smp_cmd_security_req cp;
503 cp.auth_req = authreq;
504 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
508 hcon->pending_sec_level = sec_level;
509 set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend);
514 static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
516 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
518 skb_pull(skb, sizeof(*rp));
520 memcpy(conn->tk, rp->ltk, sizeof(conn->tk));
525 static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
527 struct smp_cmd_master_ident *rp = (void *) skb->data;
529 skb_pull(skb, sizeof(*rp));
531 hci_add_ltk(conn->hcon->hdev, 1, conn->src, rp->ediv,
534 smp_distribute_keys(conn, 1);
539 int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
541 __u8 code = skb->data[0];
545 if (!lmp_host_le_capable(conn->hcon->hdev)) {
547 reason = SMP_PAIRING_NOTSUPP;
551 if (IS_ERR(conn->hcon->hdev->tfm)) {
552 err = PTR_ERR(conn->hcon->hdev->tfm);
553 reason = SMP_PAIRING_NOTSUPP;
557 skb_pull(skb, sizeof(code));
560 case SMP_CMD_PAIRING_REQ:
561 reason = smp_cmd_pairing_req(conn, skb);
564 case SMP_CMD_PAIRING_FAIL:
569 case SMP_CMD_PAIRING_RSP:
570 reason = smp_cmd_pairing_rsp(conn, skb);
573 case SMP_CMD_SECURITY_REQ:
574 reason = smp_cmd_security_req(conn, skb);
577 case SMP_CMD_PAIRING_CONFIRM:
578 reason = smp_cmd_pairing_confirm(conn, skb);
581 case SMP_CMD_PAIRING_RANDOM:
582 reason = smp_cmd_pairing_random(conn, skb);
585 case SMP_CMD_ENCRYPT_INFO:
586 reason = smp_cmd_encrypt_info(conn, skb);
589 case SMP_CMD_MASTER_IDENT:
590 reason = smp_cmd_master_ident(conn, skb);
593 case SMP_CMD_IDENT_INFO:
594 case SMP_CMD_IDENT_ADDR_INFO:
595 case SMP_CMD_SIGN_INFO:
601 BT_DBG("Unknown command code 0x%2.2x", code);
603 reason = SMP_CMD_NOTSUPP;
610 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
617 int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
619 struct smp_cmd_pairing *req, *rsp;
622 BT_DBG("conn %p force %d", conn, force);
624 if (IS_ERR(conn->hcon->hdev->tfm))
625 return PTR_ERR(conn->hcon->hdev->tfm);
627 rsp = (void *) &conn->prsp[1];
629 /* The responder sends its keys first */
630 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07))
633 req = (void *) &conn->preq[1];
635 if (conn->hcon->out) {
636 keydist = &rsp->init_key_dist;
637 *keydist &= req->init_key_dist;
639 keydist = &rsp->resp_key_dist;
640 *keydist &= req->resp_key_dist;
644 BT_DBG("keydist 0x%x", *keydist);
646 if (*keydist & SMP_DIST_ENC_KEY) {
647 struct smp_cmd_encrypt_info enc;
648 struct smp_cmd_master_ident ident;
651 get_random_bytes(enc.ltk, sizeof(enc.ltk));
652 get_random_bytes(&ediv, sizeof(ediv));
653 get_random_bytes(ident.rand, sizeof(ident.rand));
655 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
657 hci_add_ltk(conn->hcon->hdev, 1, conn->dst, ediv,
658 ident.rand, enc.ltk);
660 ident.ediv = cpu_to_le16(ediv);
662 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
664 *keydist &= ~SMP_DIST_ENC_KEY;
667 if (*keydist & SMP_DIST_ID_KEY) {
668 struct smp_cmd_ident_addr_info addrinfo;
669 struct smp_cmd_ident_info idinfo;
671 /* Send a dummy key */
672 get_random_bytes(idinfo.irk, sizeof(idinfo.irk));
674 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
676 /* Just public address */
677 memset(&addrinfo, 0, sizeof(addrinfo));
678 bacpy(&addrinfo.bdaddr, conn->src);
680 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
683 *keydist &= ~SMP_DIST_ID_KEY;
686 if (*keydist & SMP_DIST_SIGN) {
687 struct smp_cmd_sign_info sign;
689 /* Send a dummy key */
690 get_random_bytes(sign.csrk, sizeof(sign.csrk));
692 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
694 *keydist &= ~SMP_DIST_SIGN;