• Home
  • Raw
  • Download

Lines Matching full:smp

37 #include "smp.h"
54 #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd) argument
102 u8 preq[7]; /* SMP Pairing Request */
103 u8 prsp[7]; /* SMP Pairing Response */
104 u8 prnd[16]; /* SMP Pairing Random (local) */
105 u8 rrnd[16]; /* SMP Pairing Random (remote) */
106 u8 pcnf[16]; /* SMP Pairing Confirm */
107 u8 tk[16]; /* SMP Temporary Key */
135 /* These debug key values are defined in the SMP section of the core
166 /* The following functions map to the LE SC SMP crypto functions
238 * the SMP section of the Bluetooth core specification. In ASCII in smp_f5()
372 /* The following functions map to the legacy SMP crypto functions e, c1,
542 struct smp_dev *smp; in smp_generate_oob() local
548 smp = chan->data; in smp_generate_oob()
552 err = set_ecdh_privkey(smp->tfm_ecdh, debug_sk); in smp_generate_oob()
555 memcpy(smp->local_pk, debug_pk, 64); in smp_generate_oob()
556 smp->debug_key = true; in smp_generate_oob()
560 err = generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk); in smp_generate_oob()
567 if (crypto_memneq(smp->local_pk, debug_pk, 64)) in smp_generate_oob()
570 smp->debug_key = false; in smp_generate_oob()
573 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk); in smp_generate_oob()
574 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32); in smp_generate_oob()
576 get_random_bytes(smp->local_rand, 16); in smp_generate_oob()
578 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk, in smp_generate_oob()
579 smp->local_rand, 0, hash); in smp_generate_oob()
583 memcpy(rand, smp->local_rand, 16); in smp_generate_oob()
585 smp->local_oob = true; in smp_generate_oob()
592 struct l2cap_chan *chan = conn->smp; in smp_send_cmd()
593 struct smp_chan *smp; in smp_send_cmd() local
617 smp = chan->data; in smp_send_cmd()
619 cancel_delayed_work_sync(&smp->security_timer); in smp_send_cmd()
620 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); in smp_send_cmd()
652 struct l2cap_chan *chan = conn->smp; in build_pairing_cmd()
653 struct smp_chan *smp = chan->data; in build_pairing_cmd() local
690 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags); in build_pairing_cmd()
692 memcpy(smp->rr, oob_data->rand256, 16); in build_pairing_cmd()
693 memcpy(smp->pcnf, oob_data->hash256, 16); in build_pairing_cmd()
694 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf); in build_pairing_cmd()
695 SMP_DBG("OOB Remote Random: %16phN", smp->rr); in build_pairing_cmd()
710 smp->remote_key_dist = remote_dist; in build_pairing_cmd()
721 smp->remote_key_dist = rsp->init_key_dist; in build_pairing_cmd()
726 struct l2cap_chan *chan = conn->smp; in check_enc_key_size()
728 struct smp_chan *smp = chan->data; in check_enc_key_size() local
738 smp->enc_key_size = max_key_size; in check_enc_key_size()
745 struct l2cap_chan *chan = conn->smp; in smp_chan_destroy()
746 struct smp_chan *smp = chan->data; in smp_chan_destroy() local
750 BUG_ON(!smp); in smp_chan_destroy()
752 cancel_delayed_work_sync(&smp->security_timer); in smp_chan_destroy()
754 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); in smp_chan_destroy()
757 kfree_sensitive(smp->csrk); in smp_chan_destroy()
758 kfree_sensitive(smp->responder_csrk); in smp_chan_destroy()
759 kfree_sensitive(smp->link_key); in smp_chan_destroy()
761 crypto_free_shash(smp->tfm_cmac); in smp_chan_destroy()
762 crypto_free_kpp(smp->tfm_ecdh); in smp_chan_destroy()
767 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG && in smp_chan_destroy()
769 list_del_rcu(&smp->ltk->list); in smp_chan_destroy()
770 kfree_rcu(smp->ltk, rcu); in smp_chan_destroy()
771 smp->ltk = NULL; in smp_chan_destroy()
776 if (smp->ltk) { in smp_chan_destroy()
777 list_del_rcu(&smp->ltk->list); in smp_chan_destroy()
778 kfree_rcu(smp->ltk, rcu); in smp_chan_destroy()
781 if (smp->responder_ltk) { in smp_chan_destroy()
782 list_del_rcu(&smp->responder_ltk->list); in smp_chan_destroy()
783 kfree_rcu(smp->responder_ltk, rcu); in smp_chan_destroy()
786 if (smp->remote_irk) { in smp_chan_destroy()
787 list_del_rcu(&smp->remote_irk->list); in smp_chan_destroy()
788 kfree_rcu(smp->remote_irk, rcu); in smp_chan_destroy()
793 kfree_sensitive(smp); in smp_chan_destroy()
800 struct l2cap_chan *chan = conn->smp; in smp_failure()
836 static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) in get_auth_method() argument
845 if (test_bit(SMP_FLAG_SC, &smp->flags)) in get_auth_method()
855 struct l2cap_chan *chan = conn->smp; in tk_request()
856 struct smp_chan *smp = chan->data; in tk_request() local
861 memset(smp->tk, 0, sizeof(smp->tk)); in tk_request()
862 clear_bit(SMP_FLAG_TK_VALID, &smp->flags); in tk_request()
874 smp->method = JUST_CFM; in tk_request()
876 smp->method = get_auth_method(smp, local_io, remote_io); in tk_request()
879 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, in tk_request()
880 &smp->flags)) in tk_request()
881 smp->method = JUST_WORKS; in tk_request()
884 if (smp->method == JUST_CFM && in tk_request()
886 smp->method = JUST_WORKS; in tk_request()
890 if (smp->method == JUST_WORKS) { in tk_request()
897 set_bit(SMP_FLAG_WAIT_USER, &smp->flags); in tk_request()
904 if (test_bit(SMP_FLAG_SC, &smp->flags)) in tk_request()
908 if (smp->method != JUST_CFM) { in tk_request()
909 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); in tk_request()
917 if (smp->method == OVERLAP) { in tk_request()
918 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in tk_request()
919 smp->method = CFM_PASSKEY; in tk_request()
921 smp->method = REQ_PASSKEY; in tk_request()
925 if (smp->method == CFM_PASSKEY) { in tk_request()
926 memset(smp->tk, 0, sizeof(smp->tk)); in tk_request()
929 put_unaligned_le32(passkey, smp->tk); in tk_request()
931 set_bit(SMP_FLAG_TK_VALID, &smp->flags); in tk_request()
934 if (smp->method == REQ_PASSKEY) in tk_request()
937 else if (smp->method == JUST_CFM) in tk_request()
949 static u8 smp_confirm(struct smp_chan *smp) in smp_confirm() argument
951 struct l2cap_conn *conn = smp->conn; in smp_confirm()
957 ret = smp_c1(smp->tk, smp->prnd, smp->preq, smp->prsp, in smp_confirm()
964 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); in smp_confirm()
966 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); in smp_confirm()
968 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in smp_confirm()
969 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); in smp_confirm()
971 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); in smp_confirm()
976 static u8 smp_random(struct smp_chan *smp) in smp_random() argument
978 struct l2cap_conn *conn = smp->conn; in smp_random()
984 test_bit(SMP_FLAG_INITIATOR, &smp->flags) ? "initiator" : in smp_random()
987 ret = smp_c1(smp->tk, smp->rrnd, smp->preq, smp->prsp, in smp_random()
993 if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) { in smp_random()
999 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_random()
1004 smp_s1(smp->tk, smp->rrnd, smp->prnd, stk); in smp_random()
1009 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size); in smp_random()
1010 hcon->enc_key_size = smp->enc_key_size; in smp_random()
1017 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), in smp_random()
1018 smp->prnd); in smp_random()
1020 smp_s1(smp->tk, smp->prnd, smp->rrnd, stk); in smp_random()
1032 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); in smp_random()
1040 struct l2cap_chan *chan = conn->smp; in smp_notify_keys()
1041 struct smp_chan *smp = chan->data; in smp_notify_keys() local
1044 struct smp_cmd_pairing *req = (void *) &smp->preq[1]; in smp_notify_keys()
1045 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; in smp_notify_keys()
1063 if (smp->remote_irk) { in smp_notify_keys()
1064 mgmt_new_irk(hdev, smp->remote_irk, persistent); in smp_notify_keys()
1071 bacpy(&hcon->dst, &smp->remote_irk->bdaddr); in smp_notify_keys()
1072 hcon->dst_type = smp->remote_irk->addr_type; in smp_notify_keys()
1082 if (smp->csrk) { in smp_notify_keys()
1083 smp->csrk->bdaddr_type = hcon->dst_type; in smp_notify_keys()
1084 bacpy(&smp->csrk->bdaddr, &hcon->dst); in smp_notify_keys()
1085 mgmt_new_csrk(hdev, smp->csrk, persistent); in smp_notify_keys()
1088 if (smp->responder_csrk) { in smp_notify_keys()
1089 smp->responder_csrk->bdaddr_type = hcon->dst_type; in smp_notify_keys()
1090 bacpy(&smp->responder_csrk->bdaddr, &hcon->dst); in smp_notify_keys()
1091 mgmt_new_csrk(hdev, smp->responder_csrk, persistent); in smp_notify_keys()
1094 if (smp->ltk) { in smp_notify_keys()
1095 smp->ltk->bdaddr_type = hcon->dst_type; in smp_notify_keys()
1096 bacpy(&smp->ltk->bdaddr, &hcon->dst); in smp_notify_keys()
1097 mgmt_new_ltk(hdev, smp->ltk, persistent); in smp_notify_keys()
1100 if (smp->responder_ltk) { in smp_notify_keys()
1101 smp->responder_ltk->bdaddr_type = hcon->dst_type; in smp_notify_keys()
1102 bacpy(&smp->responder_ltk->bdaddr, &hcon->dst); in smp_notify_keys()
1103 mgmt_new_ltk(hdev, smp->responder_ltk, persistent); in smp_notify_keys()
1106 if (smp->link_key) { in smp_notify_keys()
1110 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags)) in smp_notify_keys()
1117 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst, in smp_notify_keys()
1118 smp->link_key, type, 0, &persistent); in smp_notify_keys()
1134 static void sc_add_ltk(struct smp_chan *smp) in sc_add_ltk() argument
1136 struct hci_conn *hcon = smp->conn->hcon; in sc_add_ltk()
1139 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags)) in sc_add_ltk()
1149 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, in sc_add_ltk()
1150 key_type, auth, smp->tk, smp->enc_key_size, in sc_add_ltk()
1154 static void sc_generate_link_key(struct smp_chan *smp) in sc_generate_link_key() argument
1159 smp->link_key = kzalloc(16, GFP_KERNEL); in sc_generate_link_key()
1160 if (!smp->link_key) in sc_generate_link_key()
1163 if (test_bit(SMP_FLAG_CT2, &smp->flags)) { in sc_generate_link_key()
1167 if (smp_h7(smp->tfm_cmac, smp->tk, salt, smp->link_key)) { in sc_generate_link_key()
1168 kfree_sensitive(smp->link_key); in sc_generate_link_key()
1169 smp->link_key = NULL; in sc_generate_link_key()
1176 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) { in sc_generate_link_key()
1177 kfree_sensitive(smp->link_key); in sc_generate_link_key()
1178 smp->link_key = NULL; in sc_generate_link_key()
1183 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) { in sc_generate_link_key()
1184 kfree_sensitive(smp->link_key); in sc_generate_link_key()
1185 smp->link_key = NULL; in sc_generate_link_key()
1190 static void smp_allow_key_dist(struct smp_chan *smp) in smp_allow_key_dist() argument
1196 if (smp->remote_key_dist & SMP_DIST_ENC_KEY) in smp_allow_key_dist()
1197 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); in smp_allow_key_dist()
1198 else if (smp->remote_key_dist & SMP_DIST_ID_KEY) in smp_allow_key_dist()
1199 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); in smp_allow_key_dist()
1200 else if (smp->remote_key_dist & SMP_DIST_SIGN) in smp_allow_key_dist()
1201 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); in smp_allow_key_dist()
1204 static void sc_generate_ltk(struct smp_chan *smp) in sc_generate_ltk() argument
1208 struct hci_conn *hcon = smp->conn->hcon; in sc_generate_ltk()
1219 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); in sc_generate_ltk()
1221 if (test_bit(SMP_FLAG_CT2, &smp->flags)) { in sc_generate_ltk()
1225 if (smp_h7(smp->tfm_cmac, key->val, salt, smp->tk)) in sc_generate_ltk()
1231 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk)) in sc_generate_ltk()
1235 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk)) in sc_generate_ltk()
1238 sc_add_ltk(smp); in sc_generate_ltk()
1241 static void smp_distribute_keys(struct smp_chan *smp) in smp_distribute_keys() argument
1244 struct l2cap_conn *conn = smp->conn; in smp_distribute_keys()
1251 rsp = (void *) &smp->prsp[1]; in smp_distribute_keys()
1254 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags) && in smp_distribute_keys()
1255 (smp->remote_key_dist & KEY_DIST_MASK)) { in smp_distribute_keys()
1256 smp_allow_key_dist(smp); in smp_distribute_keys()
1260 req = (void *) &smp->preq[1]; in smp_distribute_keys()
1262 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_distribute_keys()
1270 if (test_bit(SMP_FLAG_SC, &smp->flags)) { in smp_distribute_keys()
1272 sc_generate_link_key(smp); in smp_distribute_keys()
1274 sc_generate_ltk(smp); in smp_distribute_keys()
1294 get_random_bytes(enc.ltk, smp->enc_key_size); in smp_distribute_keys()
1295 memset(enc.ltk + smp->enc_key_size, 0, in smp_distribute_keys()
1296 sizeof(enc.ltk) - smp->enc_key_size); in smp_distribute_keys()
1306 smp->enc_key_size, ediv, rand); in smp_distribute_keys()
1307 smp->responder_ltk = ltk; in smp_distribute_keys()
1356 smp->responder_csrk = csrk; in smp_distribute_keys()
1364 if (smp->remote_key_dist & KEY_DIST_MASK) { in smp_distribute_keys()
1365 smp_allow_key_dist(smp); in smp_distribute_keys()
1369 set_bit(SMP_FLAG_COMPLETE, &smp->flags); in smp_distribute_keys()
1377 struct smp_chan *smp = container_of(work, struct smp_chan, in smp_timeout() local
1379 struct l2cap_conn *conn = smp->conn; in smp_timeout()
1389 struct l2cap_chan *chan = conn->smp; in smp_chan_create()
1390 struct smp_chan *smp; in smp_chan_create() local
1392 smp = kzalloc(sizeof(*smp), GFP_ATOMIC); in smp_chan_create()
1393 if (!smp) in smp_chan_create()
1396 smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0); in smp_chan_create()
1397 if (IS_ERR(smp->tfm_cmac)) { in smp_chan_create()
1402 smp->tfm_ecdh = crypto_alloc_kpp("ecdh-nist-p256", 0, 0); in smp_chan_create()
1403 if (IS_ERR(smp->tfm_ecdh)) { in smp_chan_create()
1408 smp->conn = conn; in smp_chan_create()
1409 chan->data = smp; in smp_chan_create()
1411 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL); in smp_chan_create()
1413 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); in smp_chan_create()
1417 return smp; in smp_chan_create()
1420 crypto_free_shash(smp->tfm_cmac); in smp_chan_create()
1422 kfree_sensitive(smp); in smp_chan_create()
1426 static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16]) in sc_mackey_and_ltk() argument
1428 struct hci_conn *hcon = smp->conn->hcon; in sc_mackey_and_ltk()
1431 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in sc_mackey_and_ltk()
1432 na = smp->prnd; in sc_mackey_and_ltk()
1433 nb = smp->rrnd; in sc_mackey_and_ltk()
1435 na = smp->rrnd; in sc_mackey_and_ltk()
1436 nb = smp->prnd; in sc_mackey_and_ltk()
1444 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk); in sc_mackey_and_ltk()
1447 static void sc_dhkey_check(struct smp_chan *smp) in sc_dhkey_check() argument
1449 struct hci_conn *hcon = smp->conn->hcon; in sc_dhkey_check()
1459 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in sc_dhkey_check()
1462 memcpy(io_cap, &smp->preq[1], 3); in sc_dhkey_check()
1466 memcpy(io_cap, &smp->prsp[1], 3); in sc_dhkey_check()
1471 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) in sc_dhkey_check()
1474 if (smp->method == REQ_OOB) in sc_dhkey_check()
1475 memcpy(r, smp->rr, 16); in sc_dhkey_check()
1477 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap, in sc_dhkey_check()
1480 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check); in sc_dhkey_check()
1483 static u8 sc_passkey_send_confirm(struct smp_chan *smp) in sc_passkey_send_confirm() argument
1485 struct l2cap_conn *conn = smp->conn; in sc_passkey_send_confirm()
1490 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01); in sc_passkey_send_confirm()
1493 get_random_bytes(smp->prnd, sizeof(smp->prnd)); in sc_passkey_send_confirm()
1495 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r, in sc_passkey_send_confirm()
1504 static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op) in sc_passkey_round() argument
1506 struct l2cap_conn *conn = smp->conn; in sc_passkey_round()
1512 if (smp->passkey_round >= 20) in sc_passkey_round()
1517 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01); in sc_passkey_round()
1520 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk, in sc_passkey_round()
1521 smp->rrnd, r, cfm)) in sc_passkey_round()
1524 if (crypto_memneq(smp->pcnf, cfm, 16)) in sc_passkey_round()
1527 smp->passkey_round++; in sc_passkey_round()
1529 if (smp->passkey_round == 20) { in sc_passkey_round()
1531 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk)) in sc_passkey_round()
1538 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in sc_passkey_round()
1540 sizeof(smp->prnd), smp->prnd); in sc_passkey_round()
1541 if (smp->passkey_round == 20) in sc_passkey_round()
1542 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); in sc_passkey_round()
1544 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); in sc_passkey_round()
1549 if (smp->passkey_round != 20) in sc_passkey_round()
1550 return sc_passkey_round(smp, 0); in sc_passkey_round()
1553 sc_dhkey_check(smp); in sc_passkey_round()
1554 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); in sc_passkey_round()
1559 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) { in sc_passkey_round()
1560 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); in sc_passkey_round()
1564 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); in sc_passkey_round()
1566 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in sc_passkey_round()
1568 sizeof(smp->prnd), smp->prnd); in sc_passkey_round()
1572 return sc_passkey_send_confirm(smp); in sc_passkey_round()
1577 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in sc_passkey_round()
1581 smp->passkey_round + 1); in sc_passkey_round()
1583 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); in sc_passkey_round()
1585 return sc_passkey_send_confirm(smp); in sc_passkey_round()
1591 static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey) in sc_user_reply() argument
1593 struct l2cap_conn *conn = smp->conn; in sc_user_reply()
1597 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags); in sc_user_reply()
1601 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED); in sc_user_reply()
1604 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED); in sc_user_reply()
1608 smp->passkey_round = 0; in sc_user_reply()
1610 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) in sc_user_reply()
1615 if (sc_passkey_round(smp, smp_op)) in sc_user_reply()
1622 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in sc_user_reply()
1623 sc_dhkey_check(smp); in sc_user_reply()
1624 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); in sc_user_reply()
1625 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) { in sc_user_reply()
1626 sc_dhkey_check(smp); in sc_user_reply()
1627 sc_add_ltk(smp); in sc_user_reply()
1637 struct smp_chan *smp; in smp_user_confirm_reply() local
1646 chan = conn->smp; in smp_user_confirm_reply()
1656 smp = chan->data; in smp_user_confirm_reply()
1658 if (test_bit(SMP_FLAG_SC, &smp->flags)) { in smp_user_confirm_reply()
1659 err = sc_user_reply(smp, mgmt_op, passkey); in smp_user_confirm_reply()
1666 memset(smp->tk, 0, sizeof(smp->tk)); in smp_user_confirm_reply()
1668 put_unaligned_le32(value, smp->tk); in smp_user_confirm_reply()
1671 set_bit(SMP_FLAG_TK_VALID, &smp->flags); in smp_user_confirm_reply()
1687 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { in smp_user_confirm_reply()
1688 u8 rsp = smp_confirm(smp); in smp_user_confirm_reply()
1698 static void build_bredr_pairing_cmd(struct smp_chan *smp, in build_bredr_pairing_cmd() argument
1702 struct l2cap_conn *conn = smp->conn; in build_bredr_pairing_cmd()
1725 smp->remote_key_dist = remote_dist; in build_bredr_pairing_cmd()
1737 smp->remote_key_dist = rsp->init_key_dist; in build_bredr_pairing_cmd()
1743 struct l2cap_chan *chan = conn->smp; in smp_cmd_pairing_req()
1745 struct smp_chan *smp = chan->data; in smp_cmd_pairing_req() local
1754 if (smp && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in smp_cmd_pairing_req()
1757 if (!smp) { in smp_cmd_pairing_req()
1758 smp = smp_chan_create(conn); in smp_cmd_pairing_req()
1759 if (!smp) in smp_cmd_pairing_req()
1773 smp->preq[0] = SMP_CMD_PAIRING_REQ; in smp_cmd_pairing_req()
1774 memcpy(&smp->preq[1], req, sizeof(*req)); in smp_cmd_pairing_req()
1782 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags); in smp_cmd_pairing_req()
1784 /* SMP over BR/EDR requires special treatment */ in smp_cmd_pairing_req()
1791 set_bit(SMP_FLAG_SC, &smp->flags); in smp_cmd_pairing_req()
1793 build_bredr_pairing_cmd(smp, req, &rsp); in smp_cmd_pairing_req()
1796 set_bit(SMP_FLAG_CT2, &smp->flags); in smp_cmd_pairing_req()
1803 smp->remote_key_dist &= ~SMP_SC_NO_DIST; in smp_cmd_pairing_req()
1805 smp->prsp[0] = SMP_CMD_PAIRING_RSP; in smp_cmd_pairing_req()
1806 memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); in smp_cmd_pairing_req()
1809 smp_distribute_keys(smp); in smp_cmd_pairing_req()
1816 set_bit(SMP_FLAG_SC, &smp->flags); in smp_cmd_pairing_req()
1819 set_bit(SMP_FLAG_CT2, &smp->flags); in smp_cmd_pairing_req()
1834 method = get_auth_method(smp, conn->hcon->io_capability, in smp_cmd_pairing_req()
1844 get_random_bytes(smp->prnd, sizeof(smp->prnd)); in smp_cmd_pairing_req()
1846 smp->prsp[0] = SMP_CMD_PAIRING_RSP; in smp_cmd_pairing_req()
1847 memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); in smp_cmd_pairing_req()
1851 clear_bit(SMP_FLAG_INITIATOR, &smp->flags); in smp_cmd_pairing_req()
1858 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); in smp_cmd_pairing_req()
1860 if (test_bit(SMP_FLAG_SC, &smp->flags)) { in smp_cmd_pairing_req()
1861 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); in smp_cmd_pairing_req()
1863 smp->remote_key_dist &= ~SMP_SC_NO_DIST; in smp_cmd_pairing_req()
1876 static u8 sc_send_public_key(struct smp_chan *smp) in sc_send_public_key() argument
1878 struct hci_dev *hdev = smp->conn->hcon->hdev; in sc_send_public_key()
1882 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) { in sc_send_public_key()
1891 memcpy(smp->local_pk, smp_dev->local_pk, 64); in sc_send_public_key()
1892 memcpy(smp->lr, smp_dev->local_rand, 16); in sc_send_public_key()
1895 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); in sc_send_public_key()
1902 if (set_ecdh_privkey(smp->tfm_ecdh, debug_sk)) in sc_send_public_key()
1904 memcpy(smp->local_pk, debug_pk, 64); in sc_send_public_key()
1905 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); in sc_send_public_key()
1909 if (generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk)) in sc_send_public_key()
1915 if (crypto_memneq(smp->local_pk, debug_pk, 64)) in sc_send_public_key()
1921 SMP_DBG("Local Public Key X: %32phN", smp->local_pk); in sc_send_public_key()
1922 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32); in sc_send_public_key()
1924 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk); in sc_send_public_key()
1932 struct l2cap_chan *chan = conn->smp; in smp_cmd_pairing_rsp()
1933 struct smp_chan *smp = chan->data; in smp_cmd_pairing_rsp() local
1943 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in smp_cmd_pairing_rsp()
1948 req = (void *) &smp->preq[1]; in smp_cmd_pairing_rsp()
1964 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags); in smp_cmd_pairing_rsp()
1966 smp->prsp[0] = SMP_CMD_PAIRING_RSP; in smp_cmd_pairing_rsp()
1967 memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); in smp_cmd_pairing_rsp()
1972 smp->remote_key_dist &= rsp->resp_key_dist; in smp_cmd_pairing_rsp()
1975 set_bit(SMP_FLAG_CT2, &smp->flags); in smp_cmd_pairing_rsp()
1980 smp->remote_key_dist &= ~SMP_SC_NO_DIST; in smp_cmd_pairing_rsp()
1981 smp_distribute_keys(smp); in smp_cmd_pairing_rsp()
1986 set_bit(SMP_FLAG_SC, &smp->flags); in smp_cmd_pairing_rsp()
1994 method = get_auth_method(smp, req->io_capability, in smp_cmd_pairing_rsp()
2000 get_random_bytes(smp->prnd, sizeof(smp->prnd)); in smp_cmd_pairing_rsp()
2005 smp->remote_key_dist &= rsp->resp_key_dist; in smp_cmd_pairing_rsp()
2007 if (test_bit(SMP_FLAG_SC, &smp->flags)) { in smp_cmd_pairing_rsp()
2009 smp->remote_key_dist &= ~SMP_SC_NO_DIST; in smp_cmd_pairing_rsp()
2010 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); in smp_cmd_pairing_rsp()
2011 return sc_send_public_key(smp); in smp_cmd_pairing_rsp()
2020 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); in smp_cmd_pairing_rsp()
2023 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) in smp_cmd_pairing_rsp()
2024 return smp_confirm(smp); in smp_cmd_pairing_rsp()
2029 static u8 sc_check_confirm(struct smp_chan *smp) in sc_check_confirm() argument
2031 struct l2cap_conn *conn = smp->conn; in sc_check_confirm()
2035 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) in sc_check_confirm()
2036 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM); in sc_check_confirm()
2038 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in sc_check_confirm()
2039 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), in sc_check_confirm()
2040 smp->prnd); in sc_check_confirm()
2041 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); in sc_check_confirm()
2051 static int fixup_sc_false_positive(struct smp_chan *smp) in fixup_sc_false_positive() argument
2053 struct l2cap_conn *conn = smp->conn; in fixup_sc_false_positive()
2060 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in fixup_sc_false_positive()
2068 bt_dev_err(hdev, "trying to fall back to legacy SMP"); in fixup_sc_false_positive()
2070 req = (void *) &smp->preq[1]; in fixup_sc_false_positive()
2071 rsp = (void *) &smp->prsp[1]; in fixup_sc_false_positive()
2074 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist); in fixup_sc_false_positive()
2079 bt_dev_err(hdev, "failed to fall back to legacy SMP"); in fixup_sc_false_positive()
2083 clear_bit(SMP_FLAG_SC, &smp->flags); in fixup_sc_false_positive()
2090 struct l2cap_chan *chan = conn->smp; in smp_cmd_pairing_confirm()
2091 struct smp_chan *smp = chan->data; in smp_cmd_pairing_confirm() local
2096 test_bit(SMP_FLAG_INITIATOR, &smp->flags) ? "initiator" : in smp_cmd_pairing_confirm()
2099 if (skb->len < sizeof(smp->pcnf)) in smp_cmd_pairing_confirm()
2102 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); in smp_cmd_pairing_confirm()
2103 skb_pull(skb, sizeof(smp->pcnf)); in smp_cmd_pairing_confirm()
2105 if (test_bit(SMP_FLAG_SC, &smp->flags)) { in smp_cmd_pairing_confirm()
2109 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags)) in smp_cmd_pairing_confirm()
2110 return sc_check_confirm(smp); in smp_cmd_pairing_confirm()
2112 bt_dev_err(hdev, "Unexpected SMP Pairing Confirm"); in smp_cmd_pairing_confirm()
2114 ret = fixup_sc_false_positive(smp); in smp_cmd_pairing_confirm()
2119 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_pairing_confirm()
2120 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), in smp_cmd_pairing_confirm()
2121 smp->prnd); in smp_cmd_pairing_confirm()
2122 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); in smp_cmd_pairing_confirm()
2126 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) in smp_cmd_pairing_confirm()
2127 return smp_confirm(smp); in smp_cmd_pairing_confirm()
2129 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); in smp_cmd_pairing_confirm()
2136 struct l2cap_chan *chan = conn->smp; in smp_cmd_pairing_random()
2137 struct smp_chan *smp = chan->data; in smp_cmd_pairing_random() local
2145 if (skb->len < sizeof(smp->rrnd)) in smp_cmd_pairing_random()
2148 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); in smp_cmd_pairing_random()
2149 skb_pull(skb, sizeof(smp->rrnd)); in smp_cmd_pairing_random()
2151 if (!test_bit(SMP_FLAG_SC, &smp->flags)) in smp_cmd_pairing_random()
2152 return smp_random(smp); in smp_cmd_pairing_random()
2154 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_pairing_random()
2155 pkax = smp->local_pk; in smp_cmd_pairing_random()
2156 pkbx = smp->remote_pk; in smp_cmd_pairing_random()
2157 na = smp->prnd; in smp_cmd_pairing_random()
2158 nb = smp->rrnd; in smp_cmd_pairing_random()
2160 pkax = smp->remote_pk; in smp_cmd_pairing_random()
2161 pkbx = smp->local_pk; in smp_cmd_pairing_random()
2162 na = smp->rrnd; in smp_cmd_pairing_random()
2163 nb = smp->prnd; in smp_cmd_pairing_random()
2166 if (smp->method == REQ_OOB) { in smp_cmd_pairing_random()
2167 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in smp_cmd_pairing_random()
2169 sizeof(smp->prnd), smp->prnd); in smp_cmd_pairing_random()
2170 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); in smp_cmd_pairing_random()
2175 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) in smp_cmd_pairing_random()
2176 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM); in smp_cmd_pairing_random()
2178 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_pairing_random()
2181 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk, in smp_cmd_pairing_random()
2182 smp->rrnd, 0, cfm); in smp_cmd_pairing_random()
2186 if (crypto_memneq(smp->pcnf, cfm, 16)) in smp_cmd_pairing_random()
2189 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), in smp_cmd_pairing_random()
2190 smp->prnd); in smp_cmd_pairing_random()
2191 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); in smp_cmd_pairing_random()
2194 if (smp->method != JUST_WORKS) in smp_cmd_pairing_random()
2214 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk); in smp_cmd_pairing_random()
2218 if (smp->method == REQ_OOB) { in smp_cmd_pairing_random()
2219 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_pairing_random()
2220 sc_dhkey_check(smp); in smp_cmd_pairing_random()
2221 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); in smp_cmd_pairing_random()
2226 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey); in smp_cmd_pairing_random()
2233 if (smp->method == JUST_WORKS) in smp_cmd_pairing_random()
2241 set_bit(SMP_FLAG_WAIT_USER, &smp->flags); in smp_cmd_pairing_random()
2293 static void smp_send_pairing_req(struct smp_chan *smp, __u8 auth) in smp_send_pairing_req() argument
2297 if (smp->conn->hcon->type == ACL_LINK) in smp_send_pairing_req()
2298 build_bredr_pairing_cmd(smp, &cp, NULL); in smp_send_pairing_req()
2300 build_pairing_cmd(smp->conn, &cp, NULL, auth); in smp_send_pairing_req()
2302 smp->preq[0] = SMP_CMD_PAIRING_REQ; in smp_send_pairing_req()
2303 memcpy(&smp->preq[1], &cp, sizeof(cp)); in smp_send_pairing_req()
2305 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); in smp_send_pairing_req()
2306 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); in smp_send_pairing_req()
2308 set_bit(SMP_FLAG_INITIATOR, &smp->flags); in smp_send_pairing_req()
2316 struct smp_chan *smp; in smp_cmd_security_req() local
2352 smp = smp_chan_create(conn); in smp_cmd_security_req()
2353 if (!smp) in smp_cmd_security_req()
2362 smp_send_pairing_req(smp, auth); in smp_cmd_security_req()
2367 static void smp_send_security_req(struct smp_chan *smp, __u8 auth) in smp_send_security_req() argument
2372 smp_send_cmd(smp->conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); in smp_send_security_req()
2373 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); in smp_send_security_req()
2375 clear_bit(SMP_FLAG_INITIATOR, &smp->flags); in smp_send_security_req()
2382 struct smp_chan *smp; in smp_conn_security() local
2406 chan = conn->smp; in smp_conn_security()
2414 /* If SMP is already in progress ignore this request */ in smp_conn_security()
2420 smp = smp_chan_create(conn); in smp_conn_security()
2421 if (!smp) { in smp_conn_security()
2447 smp_send_pairing_req(smp, authreq); in smp_conn_security()
2449 smp_send_security_req(smp, authreq); in smp_conn_security()
2464 struct smp_chan *smp; in smp_cancel_and_remove_pairing() local
2478 chan = conn->smp; in smp_cancel_and_remove_pairing()
2484 smp = chan->data; in smp_cancel_and_remove_pairing()
2485 if (smp) { in smp_cancel_and_remove_pairing()
2488 smp->ltk = NULL; in smp_cancel_and_remove_pairing()
2489 smp->responder_ltk = NULL; in smp_cancel_and_remove_pairing()
2490 smp->remote_irk = NULL; in smp_cancel_and_remove_pairing()
2492 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags)) in smp_cancel_and_remove_pairing()
2508 struct l2cap_chan *chan = conn->smp; in smp_cmd_encrypt_info()
2509 struct smp_chan *smp = chan->data; in smp_cmd_encrypt_info() local
2525 SMP_ALLOW_CMD(smp, SMP_CMD_INITIATOR_IDENT); in smp_cmd_encrypt_info()
2529 memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); in smp_cmd_encrypt_info()
2537 struct l2cap_chan *chan = conn->smp; in smp_cmd_initiator_ident()
2538 struct smp_chan *smp = chan->data; in smp_cmd_initiator_ident() local
2550 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; in smp_cmd_initiator_ident()
2552 if (smp->remote_key_dist & SMP_DIST_ID_KEY) in smp_cmd_initiator_ident()
2553 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); in smp_cmd_initiator_ident()
2554 else if (smp->remote_key_dist & SMP_DIST_SIGN) in smp_cmd_initiator_ident()
2555 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); in smp_cmd_initiator_ident()
2561 authenticated, smp->tk, smp->enc_key_size, in smp_cmd_initiator_ident()
2563 smp->ltk = ltk; in smp_cmd_initiator_ident()
2564 if (!(smp->remote_key_dist & KEY_DIST_MASK)) in smp_cmd_initiator_ident()
2565 smp_distribute_keys(smp); in smp_cmd_initiator_ident()
2573 struct l2cap_chan *chan = conn->smp; in smp_cmd_ident_info()
2574 struct smp_chan *smp = chan->data; in smp_cmd_ident_info() local
2590 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); in smp_cmd_ident_info()
2594 memcpy(smp->irk, info->irk, 16); in smp_cmd_ident_info()
2603 struct l2cap_chan *chan = conn->smp; in smp_cmd_ident_addr_info()
2604 struct smp_chan *smp = chan->data; in smp_cmd_ident_addr_info() local
2614 smp->remote_key_dist &= ~SMP_DIST_ID_KEY; in smp_cmd_ident_addr_info()
2616 if (smp->remote_key_dist & SMP_DIST_SIGN) in smp_cmd_ident_addr_info()
2617 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); in smp_cmd_ident_addr_info()
2650 bacpy(&smp->id_addr, &info->bdaddr); in smp_cmd_ident_addr_info()
2651 smp->id_addr_type = info->addr_type; in smp_cmd_ident_addr_info()
2658 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, in smp_cmd_ident_addr_info()
2659 smp->id_addr_type, smp->irk, &rpa); in smp_cmd_ident_addr_info()
2662 if (!(smp->remote_key_dist & KEY_DIST_MASK)) in smp_cmd_ident_addr_info()
2663 smp_distribute_keys(smp); in smp_cmd_ident_addr_info()
2671 struct l2cap_chan *chan = conn->smp; in smp_cmd_sign_info()
2672 struct smp_chan *smp = chan->data; in smp_cmd_sign_info() local
2681 smp->remote_key_dist &= ~SMP_DIST_SIGN; in smp_cmd_sign_info()
2693 smp->csrk = csrk; in smp_cmd_sign_info()
2694 smp_distribute_keys(smp); in smp_cmd_sign_info()
2699 static u8 sc_select_method(struct smp_chan *smp) in sc_select_method() argument
2704 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) || in sc_select_method()
2705 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) in sc_select_method()
2713 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in sc_select_method()
2714 local = (void *) &smp->preq[1]; in sc_select_method()
2715 remote = (void *) &smp->prsp[1]; in sc_select_method()
2717 local = (void *) &smp->prsp[1]; in sc_select_method()
2718 remote = (void *) &smp->preq[1]; in sc_select_method()
2731 method = get_auth_method(smp, local_io, remote_io); in sc_select_method()
2736 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in sc_select_method()
2746 struct l2cap_chan *chan = conn->smp; in smp_cmd_public_key()
2747 struct smp_chan *smp = chan->data; in smp_cmd_public_key() local
2761 if (!test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags) && in smp_cmd_public_key()
2762 !crypto_memneq(key, smp->local_pk, 64)) { in smp_cmd_public_key()
2767 memcpy(smp->remote_pk, key, 64); in smp_cmd_public_key()
2769 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) { in smp_cmd_public_key()
2770 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk, in smp_cmd_public_key()
2771 smp->rr, 0, cfm.confirm_val); in smp_cmd_public_key()
2775 if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16)) in smp_cmd_public_key()
2782 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_public_key()
2783 err = sc_send_public_key(smp); in smp_cmd_public_key()
2788 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk); in smp_cmd_public_key()
2789 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32); in smp_cmd_public_key()
2794 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) { in smp_cmd_public_key()
2805 tfm_ecdh = smp->tfm_ecdh; in smp_cmd_public_key()
2808 if (compute_ecdh_secret(tfm_ecdh, smp->remote_pk, smp->dhkey)) in smp_cmd_public_key()
2811 SMP_DBG("DHKey %32phN", smp->dhkey); in smp_cmd_public_key()
2813 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags); in smp_cmd_public_key()
2815 smp->method = sc_select_method(smp); in smp_cmd_public_key()
2817 bt_dev_dbg(hdev, "selected method 0x%02x", smp->method); in smp_cmd_public_key()
2820 if (smp->method == JUST_WORKS || smp->method == JUST_CFM) in smp_cmd_public_key()
2825 if (!crypto_memneq(debug_pk, smp->remote_pk, 64)) in smp_cmd_public_key()
2826 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); in smp_cmd_public_key()
2828 if (smp->method == DSP_PASSKEY) { in smp_cmd_public_key()
2833 smp->passkey_round = 0; in smp_cmd_public_key()
2839 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); in smp_cmd_public_key()
2840 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY); in smp_cmd_public_key()
2843 if (smp->method == REQ_OOB) { in smp_cmd_public_key()
2844 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in smp_cmd_public_key()
2846 sizeof(smp->prnd), smp->prnd); in smp_cmd_public_key()
2848 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); in smp_cmd_public_key()
2853 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in smp_cmd_public_key()
2854 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); in smp_cmd_public_key()
2856 if (smp->method == REQ_PASSKEY) { in smp_cmd_public_key()
2860 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); in smp_cmd_public_key()
2861 set_bit(SMP_FLAG_WAIT_USER, &smp->flags); in smp_cmd_public_key()
2868 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) in smp_cmd_public_key()
2871 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, in smp_cmd_public_key()
2877 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); in smp_cmd_public_key()
2885 struct l2cap_chan *chan = conn->smp; in smp_cmd_dhkey_check()
2887 struct smp_chan *smp = chan->data; in smp_cmd_dhkey_check() local
2902 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_dhkey_check()
2905 memcpy(io_cap, &smp->prsp[1], 3); in smp_cmd_dhkey_check()
2909 memcpy(io_cap, &smp->preq[1], 3); in smp_cmd_dhkey_check()
2914 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) in smp_cmd_dhkey_check()
2916 else if (smp->method == REQ_OOB) in smp_cmd_dhkey_check()
2917 memcpy(r, smp->lr, 16); in smp_cmd_dhkey_check()
2919 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r, in smp_cmd_dhkey_check()
2927 if (!test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_dhkey_check()
2928 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) { in smp_cmd_dhkey_check()
2929 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags); in smp_cmd_dhkey_check()
2934 sc_dhkey_check(smp); in smp_cmd_dhkey_check()
2937 sc_add_ltk(smp); in smp_cmd_dhkey_check()
2939 if (test_bit(SMP_FLAG_INITIATOR, &smp->flags)) { in smp_cmd_dhkey_check()
2940 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size); in smp_cmd_dhkey_check()
2941 hcon->enc_key_size = smp->enc_key_size; in smp_cmd_dhkey_check()
2961 struct smp_chan *smp; in smp_sig_channel() local
2976 smp = chan->data; in smp_sig_channel()
2981 if (smp && !test_and_clear_bit(code, &smp->allow_cmd)) { in smp_sig_channel()
3004 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ) in smp_sig_channel()
3081 bt_dev_err(hcon->hdev, "unexpected SMP command 0x%02x from %pMR", in smp_sig_channel()
3096 conn->smp = NULL; in smp_teardown_cb()
3105 struct smp_chan *smp; in bredr_pairing() local
3117 /* Only initiator may initiate SMP over BR/EDR */ in bredr_pairing()
3125 /* BR/EDR must use Secure Connections for SMP */ in bredr_pairing()
3138 /* Remote must support SMP fixed chan for BR/EDR */ in bredr_pairing()
3142 /* Don't bother if SMP is already ongoing */ in bredr_pairing()
3146 smp = smp_chan_create(conn); in bredr_pairing()
3147 if (!smp) { in bredr_pairing()
3148 bt_dev_err(hdev, "unable to create SMP context for BR/EDR"); in bredr_pairing()
3152 set_bit(SMP_FLAG_SC, &smp->flags); in bredr_pairing()
3154 bt_dev_dbg(hdev, "starting SMP over BR/EDR"); in bredr_pairing()
3156 smp_send_pairing_req(smp, 0x00); in bredr_pairing()
3161 struct smp_chan *smp = chan->data; in smp_resume_cb() local
3172 if (!smp) in smp_resume_cb()
3178 cancel_delayed_work(&smp->security_timer); in smp_resume_cb()
3180 smp_distribute_keys(smp); in smp_resume_cb()
3196 conn->smp = chan; in smp_ready_cb()
3210 struct smp_chan *smp = chan->data; in smp_recv_cb() local
3212 if (smp) in smp_recv_cb()
3213 cancel_delayed_work_sync(&smp->security_timer); in smp_recv_cb()
3272 /* Other L2CAP channels may request SMP routines in order to in smp_new_conn_cb()
3273 * change the security level. This means that the SMP channel in smp_new_conn_cb()
3305 struct smp_dev *smp; in smp_add_cid() local
3310 smp = NULL; in smp_add_cid()
3314 smp = kzalloc(sizeof(*smp), GFP_KERNEL); in smp_add_cid()
3315 if (!smp) in smp_add_cid()
3321 kfree_sensitive(smp); in smp_add_cid()
3329 kfree_sensitive(smp); in smp_add_cid()
3333 smp->local_oob = false; in smp_add_cid()
3334 smp->tfm_cmac = tfm_cmac; in smp_add_cid()
3335 smp->tfm_ecdh = tfm_ecdh; in smp_add_cid()
3340 if (smp) { in smp_add_cid()
3341 crypto_free_shash(smp->tfm_cmac); in smp_add_cid()
3342 crypto_free_kpp(smp->tfm_ecdh); in smp_add_cid()
3343 kfree_sensitive(smp); in smp_add_cid()
3348 chan->data = smp; in smp_add_cid()
3381 struct smp_dev *smp; in smp_del_chan() local
3385 smp = chan->data; in smp_del_chan()
3386 if (smp) { in smp_del_chan()
3388 crypto_free_shash(smp->tfm_cmac); in smp_del_chan()
3389 crypto_free_kpp(smp->tfm_ecdh); in smp_del_chan()
3390 kfree_sensitive(smp); in smp_del_chan()
3429 * there is also no need to register any SMP channel. in smp_register()
3823 BT_INFO("SMP test passed in %llu usecs", duration); in run_selftests()