smp.c (890ca861f868a10617029ffc87eae7d48ea6876c) | smp.c (abe84903a8efc6b83fa92161429e0e3a28bde15c) |
---|---|
1/* 2 BlueZ - Bluetooth protocol stack for Linux 3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License version 2 as 7 published by the Free Software Foundation; 8 --- 177 unchanged lines hidden (view full) --- 186 if (err < 0) 187 return err; 188 189 BT_DBG("RPA %pMR", rpa); 190 191 return 0; 192} 193 | 1/* 2 BlueZ - Bluetooth protocol stack for Linux 3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License version 2 as 7 published by the Free Software Foundation; 8 --- 177 unchanged lines hidden (view full) --- 186 if (err < 0) 187 return err; 188 189 BT_DBG("RPA %pMR", rpa); 190 191 return 0; 192} 193 |
194static int smp_c1(struct smp_chan *smp, u8 k[16], u8 r[16], u8 preq[7], 195 u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, bdaddr_t *ra, 196 u8 res[16]) | 194static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16], 195 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, 196 bdaddr_t *ra, u8 res[16]) |
197{ | 197{ |
198 struct hci_dev *hdev = smp->conn->hcon->hdev; | |
199 u8 p1[16], p2[16]; 200 int err; 201 | 198 u8 p1[16], p2[16]; 199 int err; 200 |
202 BT_DBG("%s", hdev->name); 203 | |
204 memset(p1, 0, 16); 205 206 /* p1 = pres || preq || _rat || _iat */ 207 p1[0] = _iat; 208 p1[1] = _rat; 209 memcpy(p1 + 2, preq, 7); 210 memcpy(p1 + 9, pres, 7); 211 212 /* p2 = padding || ia || ra */ 213 memcpy(p2, ra, 6); 214 memcpy(p2 + 6, ia, 6); 215 memset(p2 + 12, 0, 4); 216 217 /* res = r XOR p1 */ 218 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); 219 220 /* res = e(k, res) */ | 201 memset(p1, 0, 16); 202 203 /* p1 = pres || preq || _rat || _iat */ 204 p1[0] = _iat; 205 p1[1] = _rat; 206 memcpy(p1 + 2, preq, 7); 207 memcpy(p1 + 9, pres, 7); 208 209 /* p2 = padding || ia || ra */ 210 memcpy(p2, ra, 6); 211 memcpy(p2 + 6, ia, 6); 212 memset(p2 + 12, 0, 4); 213 214 /* res = r XOR p1 */ 215 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); 216 217 /* res = e(k, res) */ |
221 err = smp_e(smp->tfm_aes, k, res); | 218 err = smp_e(tfm_aes, k, res); |
222 if (err) { 223 BT_ERR("Encrypt data error"); 224 return err; 225 } 226 227 /* res = res XOR p2 */ 228 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); 229 230 /* res = e(k, res) */ | 219 if (err) { 220 BT_ERR("Encrypt data error"); 221 return err; 222 } 223 224 /* res = res XOR p2 */ 225 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); 226 227 /* res = e(k, res) */ |
231 err = smp_e(smp->tfm_aes, k, res); | 228 err = smp_e(tfm_aes, k, res); |
232 if (err) 233 BT_ERR("Encrypt data error"); 234 235 return err; 236} 237 | 229 if (err) 230 BT_ERR("Encrypt data error"); 231 232 return err; 233} 234 |
238static int smp_s1(struct smp_chan *smp, u8 k[16], u8 r1[16], u8 r2[16], 239 u8 _r[16]) | 235static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16], 236 u8 r2[16], u8 _r[16]) |
240{ | 237{ |
241 struct hci_dev *hdev = smp->conn->hcon->hdev; | |
242 int err; 243 | 238 int err; 239 |
244 BT_DBG("%s", hdev->name); 245 | |
246 /* Just least significant octets from r1 and r2 are considered */ 247 memcpy(_r, r2, 8); 248 memcpy(_r + 8, r1, 8); 249 | 240 /* Just least significant octets from r1 and r2 are considered */ 241 memcpy(_r, r2, 8); 242 memcpy(_r + 8, r1, 8); 243 |
250 err = smp_e(smp->tfm_aes, k, _r); | 244 err = smp_e(tfm_aes, k, _r); |
251 if (err) 252 BT_ERR("Encrypt data error"); 253 254 return err; 255} 256 257static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) 258{ --- 256 unchanged lines hidden (view full) --- 515 memset(smp->tk, 0, sizeof(smp->tk)); 516 get_random_bytes(&passkey, sizeof(passkey)); 517 passkey %= 1000000; 518 put_unaligned_le32(passkey, smp->tk); 519 BT_DBG("PassKey: %d", passkey); 520 set_bit(SMP_FLAG_TK_VALID, &smp->flags); 521 } 522 | 245 if (err) 246 BT_ERR("Encrypt data error"); 247 248 return err; 249} 250 251static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) 252{ --- 256 unchanged lines hidden (view full) --- 509 memset(smp->tk, 0, sizeof(smp->tk)); 510 get_random_bytes(&passkey, sizeof(passkey)); 511 passkey %= 1000000; 512 put_unaligned_le32(passkey, smp->tk); 513 BT_DBG("PassKey: %d", passkey); 514 set_bit(SMP_FLAG_TK_VALID, &smp->flags); 515 } 516 |
523 hci_dev_lock(hcon->hdev); 524 | |
525 if (method == REQ_PASSKEY) 526 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, 527 hcon->type, hcon->dst_type); 528 else if (method == JUST_CFM) 529 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, 530 hcon->type, hcon->dst_type, 531 passkey, 1); 532 else 533 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, 534 hcon->type, hcon->dst_type, 535 passkey, 0); 536 | 517 if (method == REQ_PASSKEY) 518 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, 519 hcon->type, hcon->dst_type); 520 else if (method == JUST_CFM) 521 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, 522 hcon->type, hcon->dst_type, 523 passkey, 1); 524 else 525 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, 526 hcon->type, hcon->dst_type, 527 passkey, 0); 528 |
537 hci_dev_unlock(hcon->hdev); 538 | |
539 return ret; 540} 541 542static u8 smp_confirm(struct smp_chan *smp) 543{ 544 struct l2cap_conn *conn = smp->conn; 545 struct smp_cmd_pairing_confirm cp; 546 int ret; 547 548 BT_DBG("conn %p", conn); 549 | 529 return ret; 530} 531 532static u8 smp_confirm(struct smp_chan *smp) 533{ 534 struct l2cap_conn *conn = smp->conn; 535 struct smp_cmd_pairing_confirm cp; 536 int ret; 537 538 BT_DBG("conn %p", conn); 539 |
550 ret = smp_c1(smp, smp->tk, smp->prnd, smp->preq, smp->prsp, | 540 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp, |
551 conn->hcon->init_addr_type, &conn->hcon->init_addr, 552 conn->hcon->resp_addr_type, &conn->hcon->resp_addr, 553 cp.confirm_val); 554 if (ret) 555 return SMP_UNSPECIFIED; 556 557 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); 558 --- 14 unchanged lines hidden (view full) --- 573 u8 confirm[16]; 574 int ret; 575 576 if (IS_ERR_OR_NULL(smp->tfm_aes)) 577 return SMP_UNSPECIFIED; 578 579 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 580 | 541 conn->hcon->init_addr_type, &conn->hcon->init_addr, 542 conn->hcon->resp_addr_type, &conn->hcon->resp_addr, 543 cp.confirm_val); 544 if (ret) 545 return SMP_UNSPECIFIED; 546 547 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); 548 --- 14 unchanged lines hidden (view full) --- 563 u8 confirm[16]; 564 int ret; 565 566 if (IS_ERR_OR_NULL(smp->tfm_aes)) 567 return SMP_UNSPECIFIED; 568 569 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 570 |
581 ret = smp_c1(smp, smp->tk, smp->rrnd, smp->preq, smp->prsp, | 571 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
582 hcon->init_addr_type, &hcon->init_addr, 583 hcon->resp_addr_type, &hcon->resp_addr, confirm); 584 if (ret) 585 return SMP_UNSPECIFIED; 586 587 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { 588 BT_ERR("Pairing failed (confirmation values mismatch)"); 589 return SMP_CONFIRM_FAILED; 590 } 591 592 if (hcon->out) { 593 u8 stk[16]; 594 __le64 rand = 0; 595 __le16 ediv = 0; 596 | 572 hcon->init_addr_type, &hcon->init_addr, 573 hcon->resp_addr_type, &hcon->resp_addr, confirm); 574 if (ret) 575 return SMP_UNSPECIFIED; 576 577 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { 578 BT_ERR("Pairing failed (confirmation values mismatch)"); 579 return SMP_CONFIRM_FAILED; 580 } 581 582 if (hcon->out) { 583 u8 stk[16]; 584 __le64 rand = 0; 585 __le16 ediv = 0; 586 |
597 smp_s1(smp, smp->tk, smp->rrnd, smp->prnd, stk); | 587 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk); |
598 599 memset(stk + smp->enc_key_size, 0, 600 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); 601 602 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) 603 return SMP_UNSPECIFIED; 604 605 hci_le_start_enc(hcon, ediv, rand, stk); 606 hcon->enc_key_size = smp->enc_key_size; 607 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); 608 } else { 609 u8 stk[16], auth; 610 __le64 rand = 0; 611 __le16 ediv = 0; 612 613 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 614 smp->prnd); 615 | 588 589 memset(stk + smp->enc_key_size, 0, 590 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); 591 592 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) 593 return SMP_UNSPECIFIED; 594 595 hci_le_start_enc(hcon, ediv, rand, stk); 596 hcon->enc_key_size = smp->enc_key_size; 597 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); 598 } else { 599 u8 stk[16], auth; 600 __le64 rand = 0; 601 __le16 ediv = 0; 602 603 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 604 smp->prnd); 605 |
616 smp_s1(smp, smp->tk, smp->prnd, smp->rrnd, stk); | 606 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk); |
617 618 memset(stk + smp->enc_key_size, 0, 619 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); 620 621 if (hcon->pending_sec_level == BT_SECURITY_HIGH) 622 auth = 1; 623 else 624 auth = 0; --- 340 unchanged lines hidden (view full) --- 965 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) 966 sec_level = BT_SECURITY_MEDIUM; 967 else 968 sec_level = authreq_to_seclevel(auth); 969 970 if (sec_level > conn->hcon->pending_sec_level) 971 conn->hcon->pending_sec_level = sec_level; 972 | 607 608 memset(stk + smp->enc_key_size, 0, 609 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); 610 611 if (hcon->pending_sec_level == BT_SECURITY_HIGH) 612 auth = 1; 613 else 614 auth = 0; --- 340 unchanged lines hidden (view full) --- 955 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) 956 sec_level = BT_SECURITY_MEDIUM; 957 else 958 sec_level = authreq_to_seclevel(auth); 959 960 if (sec_level > conn->hcon->pending_sec_level) 961 conn->hcon->pending_sec_level = sec_level; 962 |
973 /* If we need MITM check that it can be acheived */ | 963 /* If we need MITM check that it can be achieved */ |
974 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { 975 u8 method; 976 977 method = get_auth_method(smp, conn->hcon->io_capability, 978 req->io_capability); 979 if (method == JUST_WORKS || method == JUST_CFM) 980 return SMP_AUTH_REQUIREMENTS; 981 } --- 41 unchanged lines hidden (view full) --- 1023 req = (void *) &smp->preq[1]; 1024 1025 key_size = min(req->max_key_size, rsp->max_key_size); 1026 if (check_enc_key_size(conn, key_size)) 1027 return SMP_ENC_KEY_SIZE; 1028 1029 auth = rsp->auth_req & AUTH_REQ_MASK; 1030 | 964 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { 965 u8 method; 966 967 method = get_auth_method(smp, conn->hcon->io_capability, 968 req->io_capability); 969 if (method == JUST_WORKS || method == JUST_CFM) 970 return SMP_AUTH_REQUIREMENTS; 971 } --- 41 unchanged lines hidden (view full) --- 1013 req = (void *) &smp->preq[1]; 1014 1015 key_size = min(req->max_key_size, rsp->max_key_size); 1016 if (check_enc_key_size(conn, key_size)) 1017 return SMP_ENC_KEY_SIZE; 1018 1019 auth = rsp->auth_req & AUTH_REQ_MASK; 1020 |
1031 /* If we need MITM check that it can be acheived */ | 1021 /* If we need MITM check that it can be achieved */ |
1032 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { 1033 u8 method; 1034 1035 method = get_auth_method(smp, req->io_capability, 1036 rsp->io_capability); 1037 if (method == JUST_WORKS || method == JUST_CFM) 1038 return SMP_AUTH_REQUIREMENTS; 1039 } --- 623 unchanged lines hidden (view full) --- 1663 chan->chan_type = pchan->chan_type; 1664 chan->ops = &smp_chan_ops; 1665 chan->scid = pchan->scid; 1666 chan->dcid = chan->scid; 1667 chan->imtu = pchan->imtu; 1668 chan->omtu = pchan->omtu; 1669 chan->mode = pchan->mode; 1670 | 1022 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { 1023 u8 method; 1024 1025 method = get_auth_method(smp, req->io_capability, 1026 rsp->io_capability); 1027 if (method == JUST_WORKS || method == JUST_CFM) 1028 return SMP_AUTH_REQUIREMENTS; 1029 } --- 623 unchanged lines hidden (view full) --- 1653 chan->chan_type = pchan->chan_type; 1654 chan->ops = &smp_chan_ops; 1655 chan->scid = pchan->scid; 1656 chan->dcid = chan->scid; 1657 chan->imtu = pchan->imtu; 1658 chan->omtu = pchan->omtu; 1659 chan->mode = pchan->mode; 1660 |
1661 /* Other L2CAP channels may request SMP routines in order to 1662 * change the security level. This means that the SMP channel 1663 * lock must be considered in its own category to avoid lockdep 1664 * warnings. 1665 */ 1666 atomic_set(&chan->nesting, L2CAP_NESTING_SMP); 1667 |
|
1671 BT_DBG("created chan %p", chan); 1672 1673 return chan; 1674} 1675 1676static const struct l2cap_ops smp_root_chan_ops = { 1677 .name = "Security Manager Root", 1678 .new_connection = smp_new_conn_cb, --- 41 unchanged lines hidden (view full) --- 1720 1721 bacpy(&chan->src, &hdev->bdaddr); 1722 chan->src_type = BDADDR_LE_PUBLIC; 1723 chan->state = BT_LISTEN; 1724 chan->mode = L2CAP_MODE_BASIC; 1725 chan->imtu = L2CAP_DEFAULT_MTU; 1726 chan->ops = &smp_root_chan_ops; 1727 | 1668 BT_DBG("created chan %p", chan); 1669 1670 return chan; 1671} 1672 1673static const struct l2cap_ops smp_root_chan_ops = { 1674 .name = "Security Manager Root", 1675 .new_connection = smp_new_conn_cb, --- 41 unchanged lines hidden (view full) --- 1717 1718 bacpy(&chan->src, &hdev->bdaddr); 1719 chan->src_type = BDADDR_LE_PUBLIC; 1720 chan->state = BT_LISTEN; 1721 chan->mode = L2CAP_MODE_BASIC; 1722 chan->imtu = L2CAP_DEFAULT_MTU; 1723 chan->ops = &smp_root_chan_ops; 1724 |
1725 /* Set correct nesting level for a parent/listening channel */ 1726 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); 1727 |
|
1728 hdev->smp_data = chan; 1729 1730 return 0; 1731} 1732 1733void smp_unregister(struct hci_dev *hdev) 1734{ 1735 struct l2cap_chan *chan = hdev->smp_data; --- 16 unchanged lines hidden --- | 1728 hdev->smp_data = chan; 1729 1730 return 0; 1731} 1732 1733void smp_unregister(struct hci_dev *hdev) 1734{ 1735 struct l2cap_chan *chan = hdev->smp_data; --- 16 unchanged lines hidden --- |