Lines Matching full:smp
2 * @file smp.c
40 #include "smp.h"
120 SMP_FLAG_TIMEOUT, /* if SMP timeout occurred */
142 /* SMP channel specific context */
147 /* Flags for SMP state machine */
219 #define DISPLAY_FIXED(smp) (IS_ENABLED(CONFIG_BT_FIXED_PASSKEY) && \ argument
221 (smp)->method == PASSKEY_DISPLAY)
251 /* SMP over BR/EDR channel specific context */
256 /* Flags for SMP state machine */
289 /* Pointer to internal data is used to mark that callbacks of given SMP channel are not initialized.
314 static const struct bt_conn_auth_cb *latch_auth_cb(struct bt_smp *smp) in latch_auth_cb() argument
316 (void)atomic_ptr_cas(&smp->auth_cb, BT_SMP_AUTH_CB_UNINITIALIZED, in latch_auth_cb()
319 return atomic_ptr_get(&smp->auth_cb); in latch_auth_cb()
322 static bool latch_bondable(struct bt_smp *smp) in latch_bondable() argument
324 (void)atomic_cas(&smp->bondable, BT_SMP_BONDABLE_UNINITIALIZED, (atomic_val_t)bondable); in latch_bondable()
326 return atomic_get(&smp->bondable); in latch_bondable()
329 static uint8_t get_io_capa(struct bt_smp *smp) in get_io_capa() argument
331 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in get_io_capa()
372 static uint8_t legacy_get_pair_method(struct bt_smp *smp, uint8_t remote_io);
411 static uint8_t get_pair_method(struct bt_smp *smp, uint8_t remote_io) in get_pair_method() argument
414 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in get_pair_method()
415 return legacy_get_pair_method(smp, remote_io); in get_pair_method()
422 req = (struct bt_smp_pairing *)&smp->preq[1]; in get_pair_method()
423 rsp = (struct bt_smp_pairing *)&smp->prsp[1]; in get_pair_method()
441 return gen_method_sc[remote_io][get_io_capa(smp)]; in get_pair_method()
502 static struct net_buf *smp_create_pdu(struct bt_smp *smp, uint8_t op, size_t len) in smp_create_pdu() argument
509 if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) { in smp_create_pdu()
523 atomic_set_bit(smp->flags, SMP_FLAG_TIMEOUT); in smp_create_pdu()
533 static uint8_t get_encryption_key_size(struct bt_smp *smp) in get_encryption_key_size() argument
537 req = (struct bt_smp_pairing *)&smp->preq[1]; in get_encryption_key_size()
538 rsp = (struct bt_smp_pairing *)&smp->prsp[1]; in get_encryption_key_size()
551 static bool update_keys_check(struct bt_smp *smp, struct bt_keys *keys) in update_keys_check() argument
554 !atomic_test_bit(smp->flags, SMP_FLAG_SC) && in update_keys_check()
555 smp->method != LEGACY_OOB) { in update_keys_check()
560 smp->method != LEGACY_OOB) { in update_keys_check()
569 if (keys->enc_size > get_encryption_key_size(smp)) { in update_keys_check()
574 !atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in update_keys_check()
579 smp->method == JUST_WORKS) { in update_keys_check()
585 && smp->method == JUST_WORKS)) { in update_keys_check()
587 (keys->id == smp->chan.chan.conn->id)) { in update_keys_check()
595 static bool update_debug_keys_check(struct bt_smp *smp) in update_debug_keys_check() argument
597 struct bt_conn *conn = smp->chan.chan.conn; in update_debug_keys_check()
618 static void smp_pairing_complete(struct bt_smp *smp, uint8_t status);
620 static void smp_pairing_br_complete(struct bt_smp_br *smp, uint8_t status);
628 struct bt_smp *smp; in smp_check_complete() local
631 __ASSERT(chan, "No SMP channel found"); in smp_check_complete()
633 smp = CONTAINER_OF(chan, struct bt_smp, chan.chan); in smp_check_complete()
634 smp->local_dist &= ~dist_complete; in smp_check_complete()
637 if (!smp->local_dist && !smp->remote_dist) { in smp_check_complete()
638 smp_pairing_complete(smp, 0); in smp_check_complete()
646 struct bt_smp_br *smp; in smp_check_complete() local
649 __ASSERT(chan, "No SMP channel found"); in smp_check_complete()
651 smp = CONTAINER_OF(chan, struct bt_smp_br, chan.chan); in smp_check_complete()
652 smp->local_dist &= ~dist_complete; in smp_check_complete()
655 if (!smp->local_dist && !smp->remote_dist) { in smp_check_complete()
656 smp_pairing_br_complete(smp, 0); in smp_check_complete()
682 static void sc_derive_link_key(struct bt_smp *smp) in sc_derive_link_key() argument
686 struct bt_conn *conn = smp->chan.chan.conn; in sc_derive_link_key()
703 if (atomic_test_bit(smp->flags, SMP_FLAG_CT2)) { in sc_derive_link_key()
737 static void smp_br_reset(struct bt_smp_br *smp) in smp_br_reset() argument
739 /* Clear flags first in case canceling of timeout fails. The SMP context in smp_br_reset()
742 atomic_set(smp->flags, 0); in smp_br_reset()
748 (void)k_work_cancel_delayable(&smp->work); in smp_br_reset()
750 atomic_set(smp->allowed_cmds, 0); in smp_br_reset()
752 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_REQ); in smp_br_reset()
755 static void smp_pairing_br_complete(struct bt_smp_br *smp, uint8_t status) in smp_pairing_br_complete() argument
757 struct bt_conn *conn = smp->chan.chan.conn; in smp_pairing_br_complete()
780 listener->pairing_failed(smp->chan.chan.conn, in smp_pairing_br_complete()
785 bool bond_flag = atomic_test_bit(smp->flags, SMP_FLAG_BOND); in smp_pairing_br_complete()
795 listener->pairing_complete(smp->chan.chan.conn, in smp_pairing_br_complete()
801 smp_br_reset(smp); in smp_pairing_br_complete()
807 struct bt_smp_br *smp = CONTAINER_OF(dwork, struct bt_smp_br, work); in smp_br_timeout() local
809 LOG_ERR("SMP Timeout"); in smp_br_timeout()
811 smp_pairing_br_complete(smp, BT_SMP_ERR_UNSPECIFIED); in smp_br_timeout()
812 atomic_set_bit(smp->flags, SMP_FLAG_TIMEOUT); in smp_br_timeout()
815 static void smp_br_send(struct bt_smp_br *smp, struct net_buf *buf, in smp_br_send() argument
818 int err = bt_l2cap_send_cb(smp->chan.chan.conn, BT_L2CAP_CID_BR_SMP, buf, cb, NULL); in smp_br_send()
829 k_work_reschedule(&smp->work, SMP_TIMEOUT); in smp_br_send()
834 struct bt_smp_br *smp = CONTAINER_OF(chan, struct bt_smp_br, chan.chan); in bt_smp_br_connected() local
839 atomic_set_bit(smp->flags, SMP_FLAG_BR_CONNECTED); in bt_smp_br_connected()
845 if (atomic_test_bit(smp->flags, SMP_FLAG_BR_PAIR)) { in bt_smp_br_connected()
852 struct bt_smp_br *smp = CONTAINER_OF(chan, struct bt_smp_br, chan.chan); in bt_smp_br_disconnected() local
860 (void)k_work_cancel_delayable(&smp->work); in bt_smp_br_disconnected()
862 (void)memset(smp, 0, sizeof(*smp)); in bt_smp_br_disconnected()
865 static void smp_br_init(struct bt_smp_br *smp) in smp_br_init() argument
867 /* Initialize SMP context exluding L2CAP channel context and anything in smp_br_init()
870 (void)memset(smp, 0, offsetof(struct bt_smp_br, chan)); in smp_br_init()
872 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_FAIL); in smp_br_init()
875 static void smp_br_derive_ltk(struct bt_smp_br *smp) in smp_br_derive_ltk() argument
879 struct bt_conn *conn = smp->chan.chan.conn; in smp_br_derive_ltk()
908 if (atomic_test_bit(smp->flags, SMP_FLAG_CT2)) { in smp_br_derive_ltk()
936 keys->enc_size = smp->enc_key_size; in smp_br_derive_ltk()
947 static struct net_buf *smp_br_create_pdu(struct bt_smp_br *smp, uint8_t op, in smp_br_create_pdu() argument
955 if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) { in smp_br_create_pdu()
969 atomic_set_bit(smp->flags, SMP_FLAG_TIMEOUT); in smp_br_create_pdu()
979 static void smp_br_distribute_keys(struct bt_smp_br *smp) in smp_br_distribute_keys() argument
981 struct bt_conn *conn = smp->chan.chan.conn; in smp_br_distribute_keys()
999 if (smp->local_dist & BT_SMP_DIST_ID_KEY) { in smp_br_distribute_keys()
1004 smp->local_dist &= ~BT_SMP_DIST_ID_KEY; in smp_br_distribute_keys()
1006 buf = smp_br_create_pdu(smp, BT_SMP_CMD_IDENT_INFO, in smp_br_distribute_keys()
1016 smp_br_send(smp, buf, NULL); in smp_br_distribute_keys()
1018 buf = smp_br_create_pdu(smp, BT_SMP_CMD_IDENT_ADDR_INFO, in smp_br_distribute_keys()
1028 smp_br_send(smp, buf, smp_id_sent); in smp_br_distribute_keys()
1033 if (smp->local_dist & BT_SMP_DIST_SIGN) { in smp_br_distribute_keys()
1037 smp->local_dist &= ~BT_SMP_DIST_SIGN; in smp_br_distribute_keys()
1039 buf = smp_br_create_pdu(smp, BT_SMP_CMD_SIGNING_INFO, in smp_br_distribute_keys()
1053 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in smp_br_distribute_keys()
1059 smp_br_send(smp, buf, smp_sign_info_sent); in smp_br_distribute_keys()
1064 static bool smp_br_pairing_allowed(struct bt_smp_br *smp) in smp_br_pairing_allowed() argument
1066 if (smp->chan.chan.conn->encrypt == 0x02) { in smp_br_pairing_allowed()
1071 smp->chan.chan.conn->encrypt == 0x01) { in smp_br_pairing_allowed()
1072 LOG_WRN("Allowing BR/EDR SMP with P-192 key"); in smp_br_pairing_allowed()
1079 static uint8_t smp_br_pairing_req(struct bt_smp_br *smp, struct net_buf *buf) in smp_br_pairing_req() argument
1082 struct bt_conn *conn = smp->chan.chan.conn; in smp_br_pairing_req()
1099 if (!smp_br_pairing_allowed(smp)) { in smp_br_pairing_req()
1113 rsp_buf = smp_br_create_pdu(smp, BT_SMP_CMD_PAIRING_RSP, sizeof(*rsp)); in smp_br_pairing_req()
1118 smp_br_init(smp); in smp_br_pairing_req()
1119 smp->enc_key_size = max_key_size; in smp_br_pairing_req()
1136 smp->local_dist = rsp->resp_key_dist; in smp_br_pairing_req()
1137 smp->remote_dist = rsp->init_key_dist; in smp_br_pairing_req()
1144 smp_br_send(smp, rsp_buf, NULL); in smp_br_pairing_req()
1146 atomic_set_bit(smp->flags, SMP_FLAG_PAIRING); in smp_br_pairing_req()
1149 if ((smp->local_dist & BT_SMP_DIST_ENC_KEY) && in smp_br_pairing_req()
1150 (smp->remote_dist & BT_SMP_DIST_ENC_KEY)) { in smp_br_pairing_req()
1151 smp_br_derive_ltk(smp); in smp_br_pairing_req()
1153 smp->local_dist &= ~BT_SMP_DIST_ENC_KEY; in smp_br_pairing_req()
1154 smp->remote_dist &= ~BT_SMP_DIST_ENC_KEY; in smp_br_pairing_req()
1157 smp_br_distribute_keys(smp); in smp_br_pairing_req()
1159 if (smp->remote_dist & BT_SMP_DIST_ID_KEY) { in smp_br_pairing_req()
1160 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_IDENT_INFO); in smp_br_pairing_req()
1161 } else if (smp->remote_dist & BT_SMP_DIST_SIGN) { in smp_br_pairing_req()
1162 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SIGNING_INFO); in smp_br_pairing_req()
1166 if (!smp->local_dist && !smp->remote_dist) { in smp_br_pairing_req()
1167 smp_pairing_br_complete(smp, 0); in smp_br_pairing_req()
1173 static uint8_t smp_br_pairing_rsp(struct bt_smp_br *smp, struct net_buf *buf) in smp_br_pairing_rsp() argument
1176 struct bt_conn *conn = smp->chan.chan.conn; in smp_br_pairing_rsp()
1194 smp->local_dist &= rsp->init_key_dist; in smp_br_pairing_rsp()
1195 smp->remote_dist &= rsp->resp_key_dist; in smp_br_pairing_rsp()
1197 smp->local_dist &= SEND_KEYS_SC; in smp_br_pairing_rsp()
1198 smp->remote_dist &= RECV_KEYS_SC; in smp_br_pairing_rsp()
1202 if (smp->remote_dist & BT_SMP_DIST_ID_KEY) { in smp_br_pairing_rsp()
1203 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_IDENT_INFO); in smp_br_pairing_rsp()
1204 } else if (smp->remote_dist & BT_SMP_DIST_SIGN) { in smp_br_pairing_rsp()
1205 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SIGNING_INFO); in smp_br_pairing_rsp()
1209 if ((smp->local_dist & BT_SMP_DIST_ENC_KEY) && in smp_br_pairing_rsp()
1210 (smp->remote_dist & BT_SMP_DIST_ENC_KEY)) { in smp_br_pairing_rsp()
1211 smp_br_derive_ltk(smp); in smp_br_pairing_rsp()
1213 smp->local_dist &= ~BT_SMP_DIST_ENC_KEY; in smp_br_pairing_rsp()
1214 smp->remote_dist &= ~BT_SMP_DIST_ENC_KEY; in smp_br_pairing_rsp()
1217 if (smp->remote_dist) { in smp_br_pairing_rsp()
1221 smp_br_distribute_keys(smp); in smp_br_pairing_rsp()
1224 if (!smp->local_dist && !smp->remote_dist) { in smp_br_pairing_rsp()
1225 smp_pairing_br_complete(smp, 0); in smp_br_pairing_rsp()
1231 static uint8_t smp_br_pairing_failed(struct bt_smp_br *smp, struct net_buf *buf) in smp_br_pairing_failed() argument
1237 smp_pairing_br_complete(smp, req->reason); in smp_br_pairing_failed()
1238 smp_br_reset(smp); in smp_br_pairing_failed()
1244 static uint8_t smp_br_ident_info(struct bt_smp_br *smp, struct net_buf *buf) in smp_br_ident_info() argument
1247 struct bt_conn *conn = smp->chan.chan.conn; in smp_br_ident_info()
1270 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_IDENT_ADDR_INFO); in smp_br_ident_info()
1275 static uint8_t smp_br_ident_addr_info(struct bt_smp_br *smp, in smp_br_ident_addr_info() argument
1278 struct bt_conn *conn = smp->chan.chan.conn; in smp_br_ident_addr_info()
1297 smp->remote_dist &= ~BT_SMP_DIST_ID_KEY; in smp_br_ident_addr_info()
1299 if (smp->remote_dist & BT_SMP_DIST_SIGN) { in smp_br_ident_addr_info()
1300 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SIGNING_INFO); in smp_br_ident_addr_info()
1303 if (conn->role == BT_CONN_ROLE_CENTRAL && !smp->remote_dist) { in smp_br_ident_addr_info()
1304 smp_br_distribute_keys(smp); in smp_br_ident_addr_info()
1308 if (!smp->local_dist && !smp->remote_dist) { in smp_br_ident_addr_info()
1309 smp_pairing_br_complete(smp, 0); in smp_br_ident_addr_info()
1316 static uint8_t smp_br_signing_info(struct bt_smp_br *smp, struct net_buf *buf) in smp_br_signing_info() argument
1319 struct bt_conn *conn = smp->chan.chan.conn; in smp_br_signing_info()
1340 smp->remote_dist &= ~BT_SMP_DIST_SIGN; in smp_br_signing_info()
1342 if (conn->role == BT_CONN_ROLE_CENTRAL && !smp->remote_dist) { in smp_br_signing_info()
1343 smp_br_distribute_keys(smp); in smp_br_signing_info()
1347 if (!smp->local_dist && !smp->remote_dist) { in smp_br_signing_info()
1348 smp_pairing_br_complete(smp, 0); in smp_br_signing_info()
1354 static uint8_t smp_br_signing_info(struct bt_smp_br *smp, struct net_buf *buf) in smp_br_signing_info() argument
1361 uint8_t (*func)(struct bt_smp_br *smp, struct net_buf *buf);
1380 static int smp_br_error(struct bt_smp_br *smp, uint8_t reason) in smp_br_error() argument
1386 smp_br_reset(smp); in smp_br_error()
1388 buf = smp_br_create_pdu(smp, BT_SMP_CMD_PAIRING_FAIL, sizeof(*rsp)); in smp_br_error()
1397 * SMP timer is not restarted for PairingFailed so don't use in smp_br_error()
1400 if (bt_l2cap_send(smp->chan.chan.conn, BT_L2CAP_CID_SMP, buf)) { in smp_br_error()
1409 struct bt_smp_br *smp = CONTAINER_OF(chan, struct bt_smp_br, chan.chan); in bt_smp_br_recv() local
1414 LOG_ERR("Too small SMP PDU received"); in bt_smp_br_recv()
1419 LOG_DBG("Received SMP code 0x%02x len %u", hdr->code, buf->len); in bt_smp_br_recv()
1422 * If SMP timeout occurred "no further SMP commands shall be sent over in bt_smp_br_recv()
1426 if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) { in bt_smp_br_recv()
1427 LOG_WRN("SMP command (code 0x%02x) received after timeout", hdr->code); in bt_smp_br_recv()
1433 LOG_WRN("Unhandled SMP code 0x%02x", hdr->code); in bt_smp_br_recv()
1434 smp_br_error(smp, BT_SMP_ERR_CMD_NOTSUPP); in bt_smp_br_recv()
1438 if (!atomic_test_and_clear_bit(smp->allowed_cmds, hdr->code)) { in bt_smp_br_recv()
1439 LOG_WRN("Unexpected SMP code 0x%02x", hdr->code); in bt_smp_br_recv()
1440 smp_br_error(smp, BT_SMP_ERR_UNSPECIFIED); in bt_smp_br_recv()
1446 smp_br_error(smp, BT_SMP_ERR_INVALID_PARAMS); in bt_smp_br_recv()
1450 err = br_handlers[hdr->code].func(smp, buf); in bt_smp_br_recv()
1452 smp_br_error(smp, err); in bt_smp_br_recv()
1461 LOG_WRN("Enabling BR/EDR SMP without BR/EDR SC support"); in br_sc_supported()
1485 struct bt_smp_br *smp = &bt_smp_br_pool[i]; in bt_smp_br_accept() local
1487 if (smp->chan.chan.conn) { in bt_smp_br_accept()
1491 smp->chan.chan.ops = &ops; in bt_smp_br_accept()
1493 *chan = &smp->chan.chan; in bt_smp_br_accept()
1495 k_work_init_delayable(&smp->work, smp_br_timeout); in bt_smp_br_accept()
1496 smp_br_reset(smp); in bt_smp_br_accept()
1501 LOG_ERR("No available SMP context for conn %p", conn); in bt_smp_br_accept()
1512 LOG_ERR("Unable to find SMP channel"); in smp_br_chan_get()
1524 struct bt_smp_br *smp; in bt_smp_br_send_pairing_req() local
1526 smp = smp_br_chan_get(conn); in bt_smp_br_send_pairing_req()
1527 if (!smp) { in bt_smp_br_send_pairing_req()
1531 /* SMP Timeout */ in bt_smp_br_send_pairing_req()
1532 if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) { in bt_smp_br_send_pairing_req()
1537 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in bt_smp_br_send_pairing_req()
1541 /* check if we are allowed to start SMP over BR/EDR */ in bt_smp_br_send_pairing_req()
1542 if (!smp_br_pairing_allowed(smp)) { in bt_smp_br_send_pairing_req()
1547 if (!atomic_test_bit(smp->flags, SMP_FLAG_BR_CONNECTED)) { in bt_smp_br_send_pairing_req()
1548 atomic_set_bit(smp->flags, SMP_FLAG_BR_PAIR); in bt_smp_br_send_pairing_req()
1558 smp_br_init(smp); in bt_smp_br_send_pairing_req()
1559 smp->enc_key_size = max_key_size; in bt_smp_br_send_pairing_req()
1561 req_buf = smp_br_create_pdu(smp, BT_SMP_CMD_PAIRING_REQ, sizeof(*req)); in bt_smp_br_send_pairing_req()
1582 smp_br_send(smp, req_buf, NULL); in bt_smp_br_send_pairing_req()
1584 smp->local_dist = BR_SEND_KEYS_SC; in bt_smp_br_send_pairing_req()
1585 smp->remote_dist = BR_RECV_KEYS_SC; in bt_smp_br_send_pairing_req()
1587 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RSP); in bt_smp_br_send_pairing_req()
1589 atomic_set_bit(smp->flags, SMP_FLAG_PAIRING); in bt_smp_br_send_pairing_req()
1595 static void smp_reset(struct bt_smp *smp) in smp_reset() argument
1597 struct bt_conn *conn = smp->chan.chan.conn; in smp_reset()
1599 /* Clear flags first in case canceling of timeout fails. The SMP context in smp_reset()
1602 atomic_set(smp->flags, 0); in smp_reset()
1608 (void)k_work_cancel_delayable(&smp->work); in smp_reset()
1610 smp->method = JUST_WORKS; in smp_reset()
1611 atomic_set(smp->allowed_cmds, 0); in smp_reset()
1615 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SECURITY_REQUEST); in smp_reset()
1620 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_REQ); in smp_reset()
1647 static void smp_pairing_complete(struct bt_smp *smp, uint8_t status) in smp_pairing_complete() argument
1649 struct bt_conn *conn = smp->chan.chan.conn; in smp_pairing_complete()
1667 if (atomic_test_bit(smp->flags, SMP_FLAG_DERIVE_LK) && in smp_pairing_complete()
1668 (!atomic_test_bit(smp->flags, SMP_FLAG_SC_DEBUG_KEY) || in smp_pairing_complete()
1670 sc_derive_link_key(smp); in smp_pairing_complete()
1673 bool bond_flag = atomic_test_bit(smp->flags, SMP_FLAG_BOND); in smp_pairing_complete()
1699 atomic_test_bit(smp->flags, SMP_FLAG_KEYS_DISTR))) { in smp_pairing_complete()
1704 if (!atomic_test_bit(smp->flags, SMP_FLAG_KEYS_DISTR)) { in smp_pairing_complete()
1713 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in smp_pairing_complete()
1726 smp_reset(smp); in smp_pairing_complete()
1736 struct bt_smp *smp = CONTAINER_OF(dwork, struct bt_smp, work); in smp_timeout() local
1738 LOG_ERR("SMP Timeout"); in smp_timeout()
1740 smp_pairing_complete(smp, BT_SMP_ERR_UNSPECIFIED); in smp_timeout()
1745 atomic_set_bit(smp->flags, SMP_FLAG_TIMEOUT); in smp_timeout()
1748 static void smp_send(struct bt_smp *smp, struct net_buf *buf, in smp_send() argument
1751 int err = bt_l2cap_send_cb(smp->chan.chan.conn, BT_L2CAP_CID_SMP, buf, cb, NULL); in smp_send()
1762 k_work_reschedule(&smp->work, SMP_TIMEOUT); in smp_send()
1765 static int smp_error(struct bt_smp *smp, uint8_t reason) in smp_error() argument
1771 /* By spec, SMP "pairing process" completes successfully when the last in smp_error()
1774 remote_already_completed = (atomic_test_bit(smp->flags, SMP_FLAG_KEYS_DISTR) && in smp_error()
1775 !smp->local_dist && !smp->remote_dist); in smp_error()
1777 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING) || in smp_error()
1778 atomic_test_bit(smp->flags, SMP_FLAG_ENC_PENDING) || in smp_error()
1779 atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ)) { in smp_error()
1781 smp_pairing_complete(smp, reason); in smp_error()
1785 LOG_WRN("SMP does not allow a pairing failure at this point. Known issue. " in smp_error()
1790 * The SMP protocol states that the pairing process is completed the moment the in smp_error()
1793 bt_conn_disconnect(smp->chan.chan.conn, BT_HCI_ERR_AUTH_FAIL); in smp_error()
1797 buf = smp_create_pdu(smp, BT_SMP_CMD_PAIRING_FAIL, sizeof(*rsp)); in smp_error()
1805 /* SMP timer is not restarted for PairingFailed so don't use smp_send */ in smp_error()
1806 if (bt_l2cap_send(smp->chan.chan.conn, BT_L2CAP_CID_SMP, buf)) { in smp_error()
1813 static uint8_t smp_send_pairing_random(struct bt_smp *smp) in smp_send_pairing_random() argument
1818 rsp_buf = smp_create_pdu(smp, BT_SMP_CMD_PAIRING_RANDOM, sizeof(*req)); in smp_send_pairing_random()
1824 memcpy(req->val, smp->prnd, sizeof(req->val)); in smp_send_pairing_random()
1826 smp_send(smp, rsp_buf, NULL, NULL); in smp_send_pairing_random()
1887 static uint8_t smp_send_pairing_confirm(struct bt_smp *smp) in smp_send_pairing_confirm() argument
1893 switch (smp->method) { in smp_send_pairing_confirm()
1907 r = (smp->passkey >> smp->passkey_round) & 0x01; in smp_send_pairing_confirm()
1911 LOG_ERR("Unknown pairing method (%u)", smp->method); in smp_send_pairing_confirm()
1915 buf = smp_create_pdu(smp, BT_SMP_CMD_PAIRING_CONFIRM, sizeof(*req)); in smp_send_pairing_confirm()
1922 if (bt_crypto_f4(sc_public_key, smp->pkey, smp->prnd, r, req->val)) { in smp_send_pairing_confirm()
1927 smp_send(smp, buf, NULL, NULL); in smp_send_pairing_confirm()
1929 atomic_clear_bit(smp->flags, SMP_FLAG_CFM_DELAYED); in smp_send_pairing_confirm()
1942 static void legacy_distribute_keys(struct bt_smp *smp) in legacy_distribute_keys() argument
1944 struct bt_conn *conn = smp->chan.chan.conn; in legacy_distribute_keys()
1947 if (smp->local_dist & BT_SMP_DIST_ENC_KEY) { in legacy_distribute_keys()
1963 buf = smp_create_pdu(smp, BT_SMP_CMD_ENCRYPT_INFO, in legacy_distribute_keys()
1979 smp_send(smp, buf, NULL, NULL); in legacy_distribute_keys()
1981 buf = smp_create_pdu(smp, BT_SMP_CMD_CENTRAL_IDENT, in legacy_distribute_keys()
1992 smp_send(smp, buf, smp_ident_sent, NULL); in legacy_distribute_keys()
1994 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in legacy_distribute_keys()
2008 static uint8_t bt_smp_distribute_keys(struct bt_smp *smp) in bt_smp_distribute_keys() argument
2010 struct bt_conn *conn = smp->chan.chan.conn; in bt_smp_distribute_keys()
2020 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in bt_smp_distribute_keys()
2021 legacy_distribute_keys(smp); in bt_smp_distribute_keys()
2026 if (smp->local_dist & BT_SMP_DIST_ID_KEY) { in bt_smp_distribute_keys()
2031 buf = smp_create_pdu(smp, BT_SMP_CMD_IDENT_INFO, in bt_smp_distribute_keys()
2041 smp_send(smp, buf, NULL, NULL); in bt_smp_distribute_keys()
2043 buf = smp_create_pdu(smp, BT_SMP_CMD_IDENT_ADDR_INFO, in bt_smp_distribute_keys()
2053 smp_send(smp, buf, smp_id_sent, NULL); in bt_smp_distribute_keys()
2058 if (smp->local_dist & BT_SMP_DIST_SIGN) { in bt_smp_distribute_keys()
2062 buf = smp_create_pdu(smp, BT_SMP_CMD_SIGNING_INFO, in bt_smp_distribute_keys()
2075 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in bt_smp_distribute_keys()
2081 smp_send(smp, buf, smp_sign_info_sent, NULL); in bt_smp_distribute_keys()
2089 static uint8_t send_pairing_rsp(struct bt_smp *smp) in send_pairing_rsp() argument
2094 rsp_buf = smp_create_pdu(smp, BT_SMP_CMD_PAIRING_RSP, sizeof(*rsp)); in send_pairing_rsp()
2100 memcpy(rsp, smp->prsp + 1, sizeof(*rsp)); in send_pairing_rsp()
2102 smp_send(smp, rsp_buf, NULL, NULL); in send_pairing_rsp()
2108 static uint8_t smp_pairing_accept_query(struct bt_smp *smp, struct bt_smp_pairing *pairing) in smp_pairing_accept_query() argument
2111 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_pairing_accept_query()
2112 struct bt_conn *conn = smp->chan.chan.conn; in smp_pairing_accept_query()
2149 static uint8_t legacy_get_pair_method(struct bt_smp *smp, uint8_t remote_io) in legacy_get_pair_method() argument
2158 req = (struct bt_smp_pairing *)&smp->preq[1]; in legacy_get_pair_method()
2159 rsp = (struct bt_smp_pairing *)&smp->prsp[1]; in legacy_get_pair_method()
2171 method = gen_method_legacy[remote_io][get_io_capa(smp)]; in legacy_get_pair_method()
2177 if (smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in legacy_get_pair_method()
2187 static uint8_t legacy_request_tk(struct bt_smp *smp) in legacy_request_tk() argument
2189 struct bt_conn *conn = smp->chan.chan.conn; in legacy_request_tk()
2190 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in legacy_request_tk()
2201 smp->method == JUST_WORKS) { in legacy_request_tk()
2206 switch (smp->method) { in legacy_request_tk()
2213 atomic_set_bit(smp->flags, SMP_FLAG_USER); in legacy_request_tk()
2214 smp_auth_cb->oob_data_request(smp->chan.chan.conn, &info); in legacy_request_tk()
2237 atomic_set_bit(smp->flags, SMP_FLAG_DISPLAY); in legacy_request_tk()
2241 sys_put_le32(passkey, smp->tk); in legacy_request_tk()
2245 atomic_set_bit(smp->flags, SMP_FLAG_USER); in legacy_request_tk()
2251 LOG_ERR("Unknown pairing method (%u)", smp->method); in legacy_request_tk()
2258 static uint8_t legacy_send_pairing_confirm(struct bt_smp *smp) in legacy_send_pairing_confirm() argument
2260 struct bt_conn *conn = smp->chan.chan.conn; in legacy_send_pairing_confirm()
2264 buf = smp_create_pdu(smp, BT_SMP_CMD_PAIRING_CONFIRM, sizeof(*req)); in legacy_send_pairing_confirm()
2271 if (smp_c1(smp->tk, smp->prnd, smp->preq, smp->prsp, in legacy_send_pairing_confirm()
2277 smp_send(smp, buf, NULL, NULL); in legacy_send_pairing_confirm()
2279 atomic_clear_bit(smp->flags, SMP_FLAG_CFM_DELAYED); in legacy_send_pairing_confirm()
2285 static uint8_t legacy_pairing_req(struct bt_smp *smp) in legacy_pairing_req() argument
2287 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in legacy_pairing_req()
2292 ret = legacy_request_tk(smp); in legacy_pairing_req()
2298 if ((DISPLAY_FIXED(smp) || smp->method == JUST_WORKS) && in legacy_pairing_req()
2299 !atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ) && in legacy_pairing_req()
2301 atomic_set_bit(smp->flags, SMP_FLAG_USER); in legacy_pairing_req()
2302 smp_auth_cb->pairing_confirm(smp->chan.chan.conn); in legacy_pairing_req()
2306 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_CONFIRM); in legacy_pairing_req()
2307 atomic_set_bit(smp->allowed_cmds, BT_SMP_KEYPRESS_NOTIFICATION); in legacy_pairing_req()
2308 return send_pairing_rsp(smp); in legacy_pairing_req()
2312 static uint8_t legacy_pairing_random(struct bt_smp *smp) in legacy_pairing_random() argument
2314 struct bt_conn *conn = smp->chan.chan.conn; in legacy_pairing_random()
2321 err = smp_c1(smp->tk, smp->rrnd, smp->preq, smp->prsp, in legacy_pairing_random()
2327 LOG_DBG("pcnf %s", bt_hex(smp->pcnf, 16)); in legacy_pairing_random()
2330 if (memcmp(smp->pcnf, tmp, sizeof(smp->pcnf))) { in legacy_pairing_random()
2339 err = smp_s1(smp->tk, smp->rrnd, smp->prnd, tmp); in legacy_pairing_random()
2348 get_encryption_key_size(smp))) { in legacy_pairing_random()
2353 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in legacy_pairing_random()
2356 if (smp->remote_dist & BT_SMP_DIST_ENC_KEY) { in legacy_pairing_random()
2357 atomic_set_bit(smp->allowed_cmds, in legacy_pairing_random()
2359 } else if (smp->remote_dist & BT_SMP_DIST_ID_KEY) { in legacy_pairing_random()
2360 atomic_set_bit(smp->allowed_cmds, in legacy_pairing_random()
2362 } else if (smp->remote_dist & BT_SMP_DIST_SIGN) { in legacy_pairing_random()
2363 atomic_set_bit(smp->allowed_cmds, in legacy_pairing_random()
2372 err = smp_s1(smp->tk, smp->prnd, smp->rrnd, tmp); in legacy_pairing_random()
2378 memcpy(smp->tk, tmp, sizeof(smp->tk)); in legacy_pairing_random()
2379 LOG_DBG("generated STK %s", bt_hex(smp->tk, 16)); in legacy_pairing_random()
2381 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in legacy_pairing_random()
2383 return smp_send_pairing_random(smp); in legacy_pairing_random()
2389 static uint8_t legacy_pairing_confirm(struct bt_smp *smp) in legacy_pairing_confirm() argument
2394 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in legacy_pairing_confirm()
2395 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_CONFIRM); in legacy_pairing_confirm()
2396 return legacy_send_pairing_confirm(smp); in legacy_pairing_confirm()
2400 if (!atomic_test_bit(smp->flags, SMP_FLAG_USER)) { in legacy_pairing_confirm()
2401 atomic_set_bit(smp->allowed_cmds, in legacy_pairing_confirm()
2403 return legacy_send_pairing_confirm(smp); in legacy_pairing_confirm()
2406 atomic_set_bit(smp->flags, SMP_FLAG_CFM_DELAYED); in legacy_pairing_confirm()
2412 static void legacy_user_tk_entry(struct bt_smp *smp) in legacy_user_tk_entry() argument
2414 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_CFM_DELAYED)) { in legacy_user_tk_entry()
2419 if (legacy_pairing_confirm(smp)) { in legacy_user_tk_entry()
2420 smp_error(smp, BT_SMP_ERR_PASSKEY_ENTRY_FAILED); in legacy_user_tk_entry()
2425 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in legacy_user_tk_entry()
2426 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_CONFIRM); in legacy_user_tk_entry()
2431 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in legacy_user_tk_entry()
2435 static void legacy_passkey_entry(struct bt_smp *smp, unsigned int passkey) in legacy_passkey_entry() argument
2438 memcpy(smp->tk, &passkey, sizeof(passkey)); in legacy_passkey_entry()
2440 legacy_user_tk_entry(smp); in legacy_passkey_entry()
2443 static uint8_t smp_encrypt_info(struct bt_smp *smp, struct net_buf *buf) in smp_encrypt_info() argument
2447 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in smp_encrypt_info()
2449 struct bt_conn *conn = smp->chan.chan.conn; in smp_encrypt_info()
2461 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_CENTRAL_IDENT); in smp_encrypt_info()
2466 static uint8_t smp_central_ident(struct bt_smp *smp, struct net_buf *buf) in smp_central_ident() argument
2468 struct bt_conn *conn = smp->chan.chan.conn; in smp_central_ident()
2473 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in smp_central_ident()
2487 smp->remote_dist &= ~BT_SMP_DIST_ENC_KEY; in smp_central_ident()
2489 if (smp->remote_dist & BT_SMP_DIST_ID_KEY) { in smp_central_ident()
2490 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_IDENT_INFO); in smp_central_ident()
2491 } else if (smp->remote_dist & BT_SMP_DIST_SIGN) { in smp_central_ident()
2492 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SIGNING_INFO); in smp_central_ident()
2496 conn->role == BT_HCI_ROLE_CENTRAL && !smp->remote_dist) { in smp_central_ident()
2497 err = bt_smp_distribute_keys(smp); in smp_central_ident()
2504 if (!smp->local_dist && !smp->remote_dist) { in smp_central_ident()
2505 smp_pairing_complete(smp, 0); in smp_central_ident()
2512 static uint8_t legacy_pairing_rsp(struct bt_smp *smp) in legacy_pairing_rsp() argument
2514 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in legacy_pairing_rsp()
2519 ret = legacy_request_tk(smp); in legacy_pairing_rsp()
2525 if ((DISPLAY_FIXED(smp) || smp->method == JUST_WORKS) && in legacy_pairing_rsp()
2526 atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ) && in legacy_pairing_rsp()
2528 atomic_set_bit(smp->flags, SMP_FLAG_USER); in legacy_pairing_rsp()
2529 smp_auth_cb->pairing_confirm(smp->chan.chan.conn); in legacy_pairing_rsp()
2533 if (!atomic_test_bit(smp->flags, SMP_FLAG_USER)) { in legacy_pairing_rsp()
2534 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_CONFIRM); in legacy_pairing_rsp()
2535 atomic_set_bit(smp->allowed_cmds, BT_SMP_KEYPRESS_NOTIFICATION); in legacy_pairing_rsp()
2536 return legacy_send_pairing_confirm(smp); in legacy_pairing_rsp()
2539 atomic_set_bit(smp->flags, SMP_FLAG_CFM_DELAYED); in legacy_pairing_rsp()
2545 static uint8_t smp_encrypt_info(struct bt_smp *smp, struct net_buf *buf) in smp_encrypt_info() argument
2550 static uint8_t smp_central_ident(struct bt_smp *smp, struct net_buf *buf) in smp_central_ident() argument
2556 static int smp_init(struct bt_smp *smp) in smp_init() argument
2558 /* Initialize SMP context exluding L2CAP channel context and anything in smp_init()
2561 (void)memset(smp, 0, offsetof(struct bt_smp, chan)); in smp_init()
2564 if (bt_rand(smp->prnd, 16)) { in smp_init()
2568 LOG_DBG("prnd %s", bt_hex(smp->prnd, 16)); in smp_init()
2570 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_FAIL); in smp_init()
2594 static uint8_t get_auth(struct bt_smp *smp, uint8_t auth) in get_auth() argument
2596 struct bt_conn *conn = smp->chan.chan.conn; in get_auth()
2604 if ((get_io_capa(smp) == BT_SMP_IO_NO_INPUT_OUTPUT) || in get_auth()
2612 if (latch_bondable(smp)) { in get_auth()
2627 static uint8_t remote_sec_level_reachable(struct bt_smp *smp) in remote_sec_level_reachable() argument
2629 bt_security_t sec = smp->chan.chan.conn->required_sec_level; in remote_sec_level_reachable()
2644 if (get_encryption_key_size(smp) != BT_SMP_MAX_ENC_KEY_SIZE) { in remote_sec_level_reachable()
2648 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in remote_sec_level_reachable()
2653 if (smp->method == JUST_WORKS) { in remote_sec_level_reachable()
2663 static bool sec_level_reachable(struct bt_smp *smp) in sec_level_reachable() argument
2665 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in sec_level_reachable()
2667 switch (smp->chan.chan.conn->required_sec_level) { in sec_level_reachable()
2672 return get_io_capa(smp) != BT_SMP_IO_NO_INPUT_OUTPUT || in sec_level_reachable()
2675 return (get_io_capa(smp) != BT_SMP_IO_NO_INPUT_OUTPUT || in sec_level_reachable()
2688 LOG_ERR("Unable to find SMP channel"); in smp_chan_get()
2697 struct bt_smp *smp; in bt_smp_request_ltk() local
2700 smp = smp_chan_get(conn); in bt_smp_request_ltk()
2701 if (!smp) { in bt_smp_request_ltk()
2710 atomic_test_bit(smp->flags, SMP_FLAG_PAIRING) && in bt_smp_request_ltk()
2711 atomic_test_bit(smp->flags, SMP_FLAG_ENC_PENDING)) { in bt_smp_request_ltk()
2712 enc_size = get_encryption_key_size(smp); in bt_smp_request_ltk()
2718 memcpy(ltk, smp->tk, enc_size); in bt_smp_request_ltk()
2724 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in bt_smp_request_ltk()
2747 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in bt_smp_request_ltk()
2763 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in bt_smp_request_ltk()
2768 if (atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ)) { in bt_smp_request_ltk()
2776 smp_reset(smp); in bt_smp_request_ltk()
2783 struct bt_smp *smp; in smp_send_security_req() local
2789 smp = smp_chan_get(conn); in smp_send_security_req()
2790 if (!smp) { in smp_send_security_req()
2794 /* SMP Timeout */ in smp_send_security_req()
2795 if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) { in smp_send_security_req()
2800 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in smp_send_security_req()
2804 if (atomic_test_bit(smp->flags, SMP_FLAG_ENC_PENDING)) { in smp_send_security_req()
2809 if (!(sec_level_reachable(smp) || smp_keys_check(conn))) { in smp_send_security_req()
2820 if (smp_init(smp) != 0) { in smp_send_security_req()
2824 req_buf = smp_create_pdu(smp, BT_SMP_CMD_SECURITY_REQUEST, in smp_send_security_req()
2831 req->auth_req = get_auth(smp, BT_SMP_AUTH_DEFAULT); in smp_send_security_req()
2833 /* SMP timer is not restarted for SecRequest so don't use smp_send */ in smp_send_security_req()
2840 atomic_set_bit(smp->flags, SMP_FLAG_SEC_REQ); in smp_send_security_req()
2841 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_REQ); in smp_send_security_req()
2846 static uint8_t smp_pairing_req(struct bt_smp *smp, struct net_buf *buf) in smp_pairing_req() argument
2848 struct bt_conn *conn = smp->chan.chan.conn; in smp_pairing_req()
2849 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_pairing_req()
2873 /* If we already sent a security request then the SMP context in smp_pairing_req()
2876 if (!atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ)) { in smp_pairing_req()
2877 int ret = smp_init(smp); in smp_pairing_req()
2885 smp->preq[0] = BT_SMP_CMD_PAIRING_REQ; in smp_pairing_req()
2886 memcpy(smp->preq + 1, req, sizeof(*req)); in smp_pairing_req()
2889 smp->prsp[0] = BT_SMP_CMD_PAIRING_RSP; in smp_pairing_req()
2890 rsp = (struct bt_smp_pairing *)&smp->prsp[1]; in smp_pairing_req()
2892 rsp->auth_req = get_auth(smp, req->auth_req); in smp_pairing_req()
2893 rsp->io_capability = get_io_capa(smp); in smp_pairing_req()
2900 atomic_set_bit(smp->flags, SMP_FLAG_SC); in smp_pairing_req()
2906 if (atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in smp_pairing_req()
2916 atomic_set_bit(smp->flags, SMP_FLAG_CT2); in smp_pairing_req()
2921 atomic_set_bit(smp->flags, SMP_FLAG_BOND); in smp_pairing_req()
2931 smp->local_dist = rsp->resp_key_dist; in smp_pairing_req()
2932 smp->remote_dist = rsp->init_key_dist; in smp_pairing_req()
2934 atomic_set_bit(smp->flags, SMP_FLAG_PAIRING); in smp_pairing_req()
2936 smp->method = get_pair_method(smp, req->io_capability); in smp_pairing_req()
2938 if (!update_keys_check(smp, conn->le.keys)) { in smp_pairing_req()
2942 err = remote_sec_level_reachable(smp); in smp_pairing_req()
2947 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in smp_pairing_req()
2952 err = smp_pairing_accept_query(smp, req); in smp_pairing_req()
2958 return legacy_pairing_req(smp); in smp_pairing_req()
2963 err = smp_pairing_accept_query(smp, req); in smp_pairing_req()
2970 (DISPLAY_FIXED(smp) || smp->method == JUST_WORKS) && in smp_pairing_req()
2971 !atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ) && in smp_pairing_req()
2973 atomic_set_bit(smp->flags, SMP_FLAG_USER); in smp_pairing_req()
2978 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PUBLIC_KEY); in smp_pairing_req()
2985 return send_pairing_rsp(smp); in smp_pairing_req()
2988 static uint8_t smp_pairing_req(struct bt_smp *smp, struct net_buf *buf) in smp_pairing_req() argument
2994 static uint8_t sc_send_public_key(struct bt_smp *smp) in sc_send_public_key() argument
2999 req_buf = smp_create_pdu(smp, BT_SMP_CMD_PUBLIC_KEY, sizeof(*req)); in sc_send_public_key()
3009 smp_send(smp, req_buf, NULL, NULL); in sc_send_public_key()
3012 atomic_set_bit(smp->flags, SMP_FLAG_SC_DEBUG_KEY); in sc_send_public_key()
3021 struct bt_smp *smp; in smp_send_pairing_req() local
3027 smp = smp_chan_get(conn); in smp_send_pairing_req()
3028 if (!smp) { in smp_send_pairing_req()
3032 /* SMP Timeout */ in smp_send_pairing_req()
3033 if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) { in smp_send_pairing_req()
3040 if (atomic_test_bit(smp->flags, SMP_FLAG_KEYS_DISTR)) { in smp_send_pairing_req()
3045 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in smp_send_pairing_req()
3050 if (atomic_test_bit(smp->flags, SMP_FLAG_ENC_PENDING)) { in smp_send_pairing_req()
3055 if (!sec_level_reachable(smp)) { in smp_send_pairing_req()
3066 if (smp_init(smp)) { in smp_send_pairing_req()
3070 req_buf = smp_create_pdu(smp, BT_SMP_CMD_PAIRING_REQ, sizeof(*req)); in smp_send_pairing_req()
3077 req->auth_req = get_auth(smp, BT_SMP_AUTH_DEFAULT); in smp_send_pairing_req()
3078 req->io_capability = get_io_capa(smp); in smp_send_pairing_req()
3097 smp->local_dist = req->init_key_dist; in smp_send_pairing_req()
3098 smp->remote_dist = req->resp_key_dist; in smp_send_pairing_req()
3101 smp->preq[0] = BT_SMP_CMD_PAIRING_REQ; in smp_send_pairing_req()
3102 memcpy(smp->preq + 1, req, sizeof(*req)); in smp_send_pairing_req()
3109 smp_send(smp, req_buf, NULL, NULL); in smp_send_pairing_req()
3111 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RSP); in smp_send_pairing_req()
3112 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SECURITY_REQUEST); in smp_send_pairing_req()
3113 atomic_set_bit(smp->flags, SMP_FLAG_PAIRING); in smp_send_pairing_req()
3118 static uint8_t smp_pairing_rsp(struct bt_smp *smp, struct net_buf *buf) in smp_pairing_rsp() argument
3120 struct bt_conn *conn = smp->chan.chan.conn; in smp_pairing_rsp()
3121 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_pairing_rsp()
3123 struct bt_smp_pairing *req = (struct bt_smp_pairing *)&smp->preq[1]; in smp_pairing_rsp()
3136 smp->local_dist &= rsp->init_key_dist; in smp_pairing_rsp()
3137 smp->remote_dist &= rsp->resp_key_dist; in smp_pairing_rsp()
3140 smp->prsp[0] = BT_SMP_CMD_PAIRING_RSP; in smp_pairing_rsp()
3141 memcpy(smp->prsp + 1, rsp, sizeof(*rsp)); in smp_pairing_rsp()
3145 atomic_set_bit(smp->flags, SMP_FLAG_SC); in smp_pairing_rsp()
3150 atomic_set_bit(smp->flags, SMP_FLAG_CT2); in smp_pairing_rsp()
3155 atomic_set_bit(smp->flags, SMP_FLAG_BOND); in smp_pairing_rsp()
3161 smp->local_dist = 0; in smp_pairing_rsp()
3162 smp->remote_dist = 0; in smp_pairing_rsp()
3165 smp->method = get_pair_method(smp, rsp->io_capability); in smp_pairing_rsp()
3167 if (!update_keys_check(smp, conn->le.keys)) { in smp_pairing_rsp()
3171 err = remote_sec_level_reachable(smp); in smp_pairing_rsp()
3176 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in smp_pairing_rsp()
3181 err = smp_pairing_accept_query(smp, rsp); in smp_pairing_rsp()
3187 return legacy_pairing_rsp(smp); in smp_pairing_rsp()
3191 smp->local_dist &= SEND_KEYS_SC; in smp_pairing_rsp()
3192 smp->remote_dist &= RECV_KEYS_SC; in smp_pairing_rsp()
3195 err = smp_pairing_accept_query(smp, rsp); in smp_pairing_rsp()
3202 (DISPLAY_FIXED(smp) || smp->method == JUST_WORKS) && in smp_pairing_rsp()
3203 atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ) && in smp_pairing_rsp()
3205 atomic_set_bit(smp->flags, SMP_FLAG_USER); in smp_pairing_rsp()
3211 atomic_set_bit(smp->flags, SMP_FLAG_PKEY_SEND); in smp_pairing_rsp()
3215 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PUBLIC_KEY); in smp_pairing_rsp()
3216 atomic_clear_bit(smp->allowed_cmds, BT_SMP_CMD_SECURITY_REQUEST); in smp_pairing_rsp()
3218 return sc_send_public_key(smp); in smp_pairing_rsp()
3221 static uint8_t smp_pairing_rsp(struct bt_smp *smp, struct net_buf *buf) in smp_pairing_rsp() argument
3227 static uint8_t smp_pairing_confirm(struct bt_smp *smp, struct net_buf *buf) in smp_pairing_confirm() argument
3233 atomic_clear_bit(smp->flags, SMP_FLAG_DISPLAY); in smp_pairing_confirm()
3235 memcpy(smp->pcnf, req->val, sizeof(smp->pcnf)); in smp_pairing_confirm()
3238 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in smp_pairing_confirm()
3239 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in smp_pairing_confirm()
3240 return smp_send_pairing_random(smp); in smp_pairing_confirm()
3248 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in smp_pairing_confirm()
3249 return legacy_pairing_confirm(smp); in smp_pairing_confirm()
3253 switch (smp->method) { in smp_pairing_confirm()
3255 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in smp_pairing_confirm()
3256 return smp_send_pairing_confirm(smp); in smp_pairing_confirm()
3258 if (atomic_test_bit(smp->flags, SMP_FLAG_USER)) { in smp_pairing_confirm()
3259 atomic_set_bit(smp->flags, SMP_FLAG_CFM_DELAYED); in smp_pairing_confirm()
3263 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in smp_pairing_confirm()
3264 return smp_send_pairing_confirm(smp); in smp_pairing_confirm()
3268 LOG_ERR("Unknown pairing method (%u)", smp->method); in smp_pairing_confirm()
3273 static uint8_t sc_smp_send_dhkey_check(struct bt_smp *smp, const uint8_t *e) in sc_smp_send_dhkey_check() argument
3280 buf = smp_create_pdu(smp, BT_SMP_DHKEY_CHECK, sizeof(*req)); in sc_smp_send_dhkey_check()
3288 smp_send(smp, buf, NULL, NULL); in sc_smp_send_dhkey_check()
3294 static uint8_t compute_and_send_central_dhcheck(struct bt_smp *smp) in compute_and_send_central_dhcheck() argument
3300 switch (smp->method) { in compute_and_send_central_dhcheck()
3306 memcpy(r, &smp->passkey, sizeof(smp->passkey)); in compute_and_send_central_dhcheck()
3309 if (smp->oobd_remote) { in compute_and_send_central_dhcheck()
3310 memcpy(r, smp->oobd_remote->r, sizeof(r)); in compute_and_send_central_dhcheck()
3314 LOG_ERR("Unknown pairing method (%u)", smp->method); in compute_and_send_central_dhcheck()
3319 if (bt_crypto_f5(smp->dhkey, smp->prnd, smp->rrnd, &smp->chan.chan.conn->le.init_addr, in compute_and_send_central_dhcheck()
3320 &smp->chan.chan.conn->le.resp_addr, smp->mackey, smp->tk)) { in compute_and_send_central_dhcheck()
3325 if (bt_crypto_f6(smp->mackey, smp->prnd, smp->rrnd, r, &smp->preq[1], in compute_and_send_central_dhcheck()
3326 &smp->chan.chan.conn->le.init_addr, &smp->chan.chan.conn->le.resp_addr, in compute_and_send_central_dhcheck()
3332 atomic_set_bit(smp->allowed_cmds, BT_SMP_DHKEY_CHECK); in compute_and_send_central_dhcheck()
3333 return sc_smp_send_dhkey_check(smp, e); in compute_and_send_central_dhcheck()
3338 static uint8_t compute_and_check_and_send_periph_dhcheck(struct bt_smp *smp) in compute_and_check_and_send_periph_dhcheck() argument
3345 switch (smp->method) { in compute_and_check_and_send_periph_dhcheck()
3351 memcpy(r, &smp->passkey, sizeof(smp->passkey)); in compute_and_check_and_send_periph_dhcheck()
3354 if (smp->oobd_remote) { in compute_and_check_and_send_periph_dhcheck()
3355 memcpy(r, smp->oobd_remote->r, sizeof(r)); in compute_and_check_and_send_periph_dhcheck()
3359 LOG_ERR("Unknown pairing method (%u)", smp->method); in compute_and_check_and_send_periph_dhcheck()
3364 if (bt_crypto_f5(smp->dhkey, smp->rrnd, smp->prnd, &smp->chan.chan.conn->le.init_addr, in compute_and_check_and_send_periph_dhcheck()
3365 &smp->chan.chan.conn->le.resp_addr, smp->mackey, smp->tk)) { in compute_and_check_and_send_periph_dhcheck()
3371 if (bt_crypto_f6(smp->mackey, smp->prnd, smp->rrnd, r, &smp->prsp[1], in compute_and_check_and_send_periph_dhcheck()
3372 &smp->chan.chan.conn->le.resp_addr, &smp->chan.chan.conn->le.init_addr, in compute_and_check_and_send_periph_dhcheck()
3378 if (smp->method == LE_SC_OOB) { in compute_and_check_and_send_periph_dhcheck()
3379 if (smp->oobd_local) { in compute_and_check_and_send_periph_dhcheck()
3380 memcpy(r, smp->oobd_local->r, sizeof(r)); in compute_and_check_and_send_periph_dhcheck()
3387 if (bt_crypto_f6(smp->mackey, smp->rrnd, smp->prnd, r, &smp->preq[1], in compute_and_check_and_send_periph_dhcheck()
3388 &smp->chan.chan.conn->le.init_addr, &smp->chan.chan.conn->le.resp_addr, in compute_and_check_and_send_periph_dhcheck()
3395 if (memcmp(smp->e, re, 16)) { in compute_and_check_and_send_periph_dhcheck()
3400 err = sc_smp_send_dhkey_check(smp, e); in compute_and_check_and_send_periph_dhcheck()
3405 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in compute_and_check_and_send_periph_dhcheck()
3411 static uint8_t smp_dhkey_generate(struct bt_smp *smp) in smp_dhkey_generate() argument
3415 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_GEN); in smp_dhkey_generate()
3416 err = bt_dh_key_gen(smp->pkey, bt_smp_dhkey_ready); in smp_dhkey_generate()
3418 atomic_clear_bit(smp->flags, SMP_FLAG_DHKEY_GEN); in smp_dhkey_generate()
3427 static uint8_t smp_dhkey_ready(struct bt_smp *smp, const uint8_t *dhkey) in smp_dhkey_ready() argument
3433 atomic_clear_bit(smp->flags, SMP_FLAG_DHKEY_PENDING); in smp_dhkey_ready()
3434 memcpy(smp->dhkey, dhkey, BT_DH_KEY_LEN); in smp_dhkey_ready()
3437 if (atomic_test_bit(smp->flags, SMP_FLAG_USER)) { in smp_dhkey_ready()
3438 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in smp_dhkey_ready()
3443 if (atomic_test_bit(smp->flags, SMP_FLAG_DHCHECK_WAIT)) { in smp_dhkey_ready()
3444 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in smp_dhkey_ready()
3448 if (atomic_test_bit(smp->flags, SMP_FLAG_DHKEY_SEND)) { in smp_dhkey_ready()
3450 if (smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in smp_dhkey_ready()
3451 return compute_and_send_central_dhcheck(smp); in smp_dhkey_ready()
3457 return compute_and_check_and_send_periph_dhcheck(smp); in smp_dhkey_ready()
3480 struct bt_smp *smp = smp_find(SMP_FLAG_DHKEY_GEN); in bt_smp_dhkey_ready() local
3481 if (smp) { in bt_smp_dhkey_ready()
3482 atomic_clear_bit(smp->flags, SMP_FLAG_DHKEY_GEN); in bt_smp_dhkey_ready()
3483 err = smp_dhkey_ready(smp, dhkey); in bt_smp_dhkey_ready()
3485 smp_error(smp, err); in bt_smp_dhkey_ready()
3491 smp = smp_find(SMP_FLAG_DHKEY_PENDING); in bt_smp_dhkey_ready()
3492 if (smp) { in bt_smp_dhkey_ready()
3493 err = smp_dhkey_generate(smp); in bt_smp_dhkey_ready()
3495 smp_error(smp, err); in bt_smp_dhkey_ready()
3498 } while (smp && err); in bt_smp_dhkey_ready()
3501 static uint8_t sc_smp_check_confirm(struct bt_smp *smp) in sc_smp_check_confirm() argument
3506 switch (smp->method) { in sc_smp_check_confirm()
3522 r = (smp->passkey >> smp->passkey_round) & 0x01; in sc_smp_check_confirm()
3526 LOG_ERR("Unknown pairing method (%u)", smp->method); in sc_smp_check_confirm()
3530 if (bt_crypto_f4(smp->pkey, sc_public_key, smp->rrnd, r, cfm)) { in sc_smp_check_confirm()
3535 LOG_DBG("pcnf %s", bt_hex(smp->pcnf, 16)); in sc_smp_check_confirm()
3538 if (memcmp(smp->pcnf, cfm, 16)) { in sc_smp_check_confirm()
3545 static bool le_sc_oob_data_req_check(struct bt_smp *smp) in le_sc_oob_data_req_check() argument
3547 struct bt_smp_pairing *req = (struct bt_smp_pairing *)&smp->preq[1]; in le_sc_oob_data_req_check()
3552 static bool le_sc_oob_data_rsp_check(struct bt_smp *smp) in le_sc_oob_data_rsp_check() argument
3554 struct bt_smp_pairing *rsp = (struct bt_smp_pairing *)&smp->prsp[1]; in le_sc_oob_data_rsp_check()
3559 static void le_sc_oob_config_set(struct bt_smp *smp, in le_sc_oob_config_set() argument
3562 bool req_oob_present = le_sc_oob_data_req_check(smp); in le_sc_oob_config_set()
3563 bool rsp_oob_present = le_sc_oob_data_rsp_check(smp); in le_sc_oob_config_set()
3567 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in le_sc_oob_config_set()
3590 static uint8_t smp_pairing_random(struct bt_smp *smp, struct net_buf *buf) in smp_pairing_random() argument
3592 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_pairing_random()
3599 memcpy(smp->rrnd, req->val, sizeof(smp->rrnd)); in smp_pairing_random()
3602 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in smp_pairing_random()
3603 return legacy_pairing_random(smp); in smp_pairing_random()
3608 if (smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in smp_pairing_random()
3609 err = sc_smp_check_confirm(smp); in smp_pairing_random()
3614 switch (smp->method) { in smp_pairing_random()
3617 if (bt_crypto_g2(sc_public_key, smp->pkey, smp->prnd, smp->rrnd, in smp_pairing_random()
3622 atomic_set_bit(smp->flags, SMP_FLAG_USER); in smp_pairing_random()
3623 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in smp_pairing_random()
3624 smp_auth_cb->passkey_confirm(smp->chan.chan.conn, passkey); in smp_pairing_random()
3632 smp->passkey_round++; in smp_pairing_random()
3633 if (smp->passkey_round == 20U) { in smp_pairing_random()
3637 if (bt_rand(smp->prnd, 16)) { in smp_pairing_random()
3641 atomic_set_bit(smp->allowed_cmds, in smp_pairing_random()
3643 return smp_send_pairing_confirm(smp); in smp_pairing_random()
3645 LOG_ERR("Unknown pairing method (%u)", smp->method); in smp_pairing_random()
3650 if (atomic_test_bit(smp->flags, SMP_FLAG_DHKEY_PENDING)) { in smp_pairing_random()
3651 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in smp_pairing_random()
3655 return compute_and_send_central_dhcheck(smp); in smp_pairing_random()
3660 switch (smp->method) { in smp_pairing_random()
3662 if (bt_crypto_g2(smp->pkey, sc_public_key, smp->rrnd, smp->prnd, &passkey)) { in smp_pairing_random()
3666 atomic_set_bit(smp->flags, SMP_FLAG_USER); in smp_pairing_random()
3667 smp_auth_cb->passkey_confirm(smp->chan.chan.conn, passkey); in smp_pairing_random()
3673 err = sc_smp_check_confirm(smp); in smp_pairing_random()
3678 atomic_set_bit(smp->allowed_cmds, in smp_pairing_random()
3680 err = smp_send_pairing_random(smp); in smp_pairing_random()
3685 smp->passkey_round++; in smp_pairing_random()
3686 if (smp->passkey_round == 20U) { in smp_pairing_random()
3687 atomic_set_bit(smp->allowed_cmds, BT_SMP_DHKEY_CHECK); in smp_pairing_random()
3688 atomic_set_bit(smp->flags, SMP_FLAG_DHCHECK_WAIT); in smp_pairing_random()
3692 if (bt_rand(smp->prnd, 16)) { in smp_pairing_random()
3699 if (bt_rand(smp->prnd, 16)) { in smp_pairing_random()
3709 le_sc_oob_config_set(smp, &info); in smp_pairing_random()
3711 smp->oobd_local = NULL; in smp_pairing_random()
3712 smp->oobd_remote = NULL; in smp_pairing_random()
3714 atomic_set_bit(smp->flags, SMP_FLAG_OOB_PENDING); in smp_pairing_random()
3715 smp_auth_cb->oob_data_request(smp->chan.chan.conn, &info); in smp_pairing_random()
3722 LOG_ERR("Unknown pairing method (%u)", smp->method); in smp_pairing_random()
3726 atomic_set_bit(smp->allowed_cmds, BT_SMP_DHKEY_CHECK); in smp_pairing_random()
3727 atomic_set_bit(smp->flags, SMP_FLAG_DHCHECK_WAIT); in smp_pairing_random()
3728 return smp_send_pairing_random(smp); in smp_pairing_random()
3734 static uint8_t smp_pairing_failed(struct bt_smp *smp, struct net_buf *buf) in smp_pairing_failed() argument
3736 struct bt_conn *conn = smp->chan.chan.conn; in smp_pairing_failed()
3737 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_pairing_failed()
3742 if (atomic_test_and_clear_bit(smp->flags, SMP_FLAG_USER) || in smp_pairing_failed()
3743 atomic_test_and_clear_bit(smp->flags, SMP_FLAG_DISPLAY)) { in smp_pairing_failed()
3749 smp_pairing_complete(smp, req->reason); in smp_pairing_failed()
3755 static uint8_t smp_ident_info(struct bt_smp *smp, struct net_buf *buf) in smp_ident_info() argument
3759 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in smp_ident_info()
3761 struct bt_conn *conn = smp->chan.chan.conn; in smp_ident_info()
3773 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_IDENT_ADDR_INFO); in smp_ident_info()
3778 static uint8_t smp_id_add_replace(struct bt_smp *smp, struct bt_keys *new_bond) in smp_id_add_replace() argument
3785 __ASSERT_NO_MSG(!(smp->remote_dist & BT_SMP_DIST_ID_KEY)); in smp_id_add_replace()
3801 trust_ok = update_keys_check(smp, conflict); in smp_id_add_replace()
3832 static uint8_t smp_ident_addr_info(struct bt_smp *smp, struct net_buf *buf) in smp_ident_addr_info() argument
3834 struct bt_conn *conn = smp->chan.chan.conn; in smp_ident_addr_info()
3840 smp->remote_dist &= ~BT_SMP_DIST_ID_KEY; in smp_ident_addr_info()
3852 if (!update_keys_check(smp, keys)) { in smp_ident_addr_info()
3860 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in smp_ident_addr_info()
3907 err = smp_id_add_replace(smp, keys); in smp_ident_addr_info()
3913 if (smp->remote_dist & BT_SMP_DIST_SIGN) { in smp_ident_addr_info()
3914 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SIGNING_INFO); in smp_ident_addr_info()
3918 conn->role == BT_HCI_ROLE_CENTRAL && !smp->remote_dist) { in smp_ident_addr_info()
3919 err = bt_smp_distribute_keys(smp); in smp_ident_addr_info()
3926 if (!smp->local_dist && !smp->remote_dist) { in smp_ident_addr_info()
3927 smp_pairing_complete(smp, 0); in smp_ident_addr_info()
3934 static uint8_t smp_signing_info(struct bt_smp *smp, struct net_buf *buf) in smp_signing_info() argument
3936 struct bt_conn *conn = smp->chan.chan.conn; in smp_signing_info()
3941 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in smp_signing_info()
3956 smp->remote_dist &= ~BT_SMP_DIST_SIGN; in smp_signing_info()
3959 conn->role == BT_HCI_ROLE_CENTRAL && !smp->remote_dist) { in smp_signing_info()
3960 err = bt_smp_distribute_keys(smp); in smp_signing_info()
3967 if (!smp->local_dist && !smp->remote_dist) { in smp_signing_info()
3968 smp_pairing_complete(smp, 0); in smp_signing_info()
3974 static uint8_t smp_signing_info(struct bt_smp *smp, struct net_buf *buf) in smp_signing_info() argument
3981 static uint8_t smp_security_request(struct bt_smp *smp, struct net_buf *buf) in smp_security_request() argument
3983 struct bt_conn *conn = smp->chan.chan.conn; in smp_security_request()
3992 if (atomic_test_bit(smp->flags, SMP_FLAG_KEYS_DISTR)) { in smp_security_request()
3996 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in smp_security_request()
4001 if (atomic_test_bit(smp->flags, SMP_FLAG_ENC_PENDING)) { in smp_security_request()
4018 !(latch_bondable(smp) && (auth & BT_SMP_AUTH_BONDING))) { in smp_security_request()
4045 if (get_io_capa(smp) != BT_SMP_IO_NO_INPUT_OUTPUT) { in smp_security_request()
4069 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in smp_security_request()
4077 atomic_set_bit(smp->flags, SMP_FLAG_SEC_REQ); in smp_security_request()
4082 static uint8_t smp_security_request(struct bt_smp *smp, struct net_buf *buf) in smp_security_request() argument
4088 static uint8_t generate_dhkey(struct bt_smp *smp) in generate_dhkey() argument
4094 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_PENDING); in generate_dhkey()
4096 return smp_dhkey_generate(smp); in generate_dhkey()
4102 static uint8_t display_passkey(struct bt_smp *smp) in display_passkey() argument
4104 struct bt_conn *conn = smp->chan.chan.conn; in display_passkey()
4105 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in display_passkey()
4109 smp->passkey = fixed_passkey; in display_passkey()
4111 if (bt_rand(&smp->passkey, sizeof(smp->passkey))) { in display_passkey()
4115 smp->passkey %= 1000000; in display_passkey()
4118 smp->passkey_round = 0U; in display_passkey()
4121 atomic_set_bit(smp->flags, SMP_FLAG_DISPLAY); in display_passkey()
4122 smp_auth_cb->passkey_display(conn, smp->passkey); in display_passkey()
4125 smp->passkey = sys_cpu_to_le32(smp->passkey); in display_passkey()
4131 static uint8_t smp_public_key_periph(struct bt_smp *smp) in smp_public_key_periph() argument
4133 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_public_key_periph()
4136 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC_DEBUG_KEY) && in smp_public_key_periph()
4137 memcmp(smp->pkey, sc_public_key, BT_PUB_KEY_COORD_LEN) == 0) { in smp_public_key_periph()
4145 err = sc_send_public_key(smp); in smp_public_key_periph()
4150 switch (smp->method) { in smp_public_key_periph()
4153 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in smp_public_key_periph()
4155 err = smp_send_pairing_confirm(smp); in smp_public_key_periph()
4161 err = display_passkey(smp); in smp_public_key_periph()
4166 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_CONFIRM); in smp_public_key_periph()
4167 atomic_set_bit(smp->allowed_cmds, BT_SMP_KEYPRESS_NOTIFICATION); in smp_public_key_periph()
4170 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_CONFIRM); in smp_public_key_periph()
4171 atomic_set_bit(smp->allowed_cmds, BT_SMP_KEYPRESS_NOTIFICATION); in smp_public_key_periph()
4172 atomic_set_bit(smp->flags, SMP_FLAG_USER); in smp_public_key_periph()
4173 smp_auth_cb->passkey_entry(smp->chan.chan.conn); in smp_public_key_periph()
4176 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in smp_public_key_periph()
4179 LOG_ERR("Unknown pairing method (%u)", smp->method); in smp_public_key_periph()
4183 return generate_dhkey(smp); in smp_public_key_periph()
4187 static uint8_t smp_public_key(struct bt_smp *smp, struct net_buf *buf) in smp_public_key() argument
4189 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_public_key()
4195 memcpy(smp->pkey, req->x, BT_PUB_KEY_COORD_LEN); in smp_public_key()
4196 memcpy(&smp->pkey[BT_PUB_KEY_COORD_LEN], req->y, BT_PUB_KEY_COORD_LEN); in smp_public_key()
4199 if (bt_pub_key_is_debug(smp->pkey)) { in smp_public_key()
4201 atomic_set_bit(smp->flags, SMP_FLAG_SC_DEBUG_KEY); in smp_public_key()
4206 if (!update_debug_keys_check(smp)) { in smp_public_key()
4212 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in smp_public_key()
4213 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC_DEBUG_KEY) && in smp_public_key()
4214 memcmp(smp->pkey, sc_public_key, BT_PUB_KEY_COORD_LEN) == 0) { in smp_public_key()
4222 switch (smp->method) { in smp_public_key()
4225 atomic_set_bit(smp->allowed_cmds, in smp_public_key()
4229 err = display_passkey(smp); in smp_public_key()
4234 atomic_set_bit(smp->allowed_cmds, in smp_public_key()
4237 atomic_set_bit(smp->allowed_cmds, in smp_public_key()
4240 err = smp_send_pairing_confirm(smp); in smp_public_key()
4246 atomic_set_bit(smp->flags, SMP_FLAG_USER); in smp_public_key()
4247 smp_auth_cb->passkey_entry(smp->chan.chan.conn); in smp_public_key()
4249 atomic_set_bit(smp->allowed_cmds, in smp_public_key()
4255 if (bt_rand(smp->prnd, 16)) { in smp_public_key()
4265 le_sc_oob_config_set(smp, &info); in smp_public_key()
4267 smp->oobd_local = NULL; in smp_public_key()
4268 smp->oobd_remote = NULL; in smp_public_key()
4270 atomic_set_bit(smp->flags, in smp_public_key()
4272 smp_auth_cb->oob_data_request(smp->chan.chan.conn, &info); in smp_public_key()
4278 LOG_ERR("Unknown pairing method (%u)", smp->method); in smp_public_key()
4282 return generate_dhkey(smp); in smp_public_key()
4287 atomic_set_bit(smp->flags, SMP_FLAG_PKEY_SEND); in smp_public_key()
4291 err = smp_public_key_periph(smp); in smp_public_key()
4300 static uint8_t smp_dhkey_check(struct bt_smp *smp, struct net_buf *buf) in smp_dhkey_check() argument
4307 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in smp_dhkey_check()
4313 switch (smp->method) { in smp_dhkey_check()
4319 memcpy(r, &smp->passkey, sizeof(smp->passkey)); in smp_dhkey_check()
4322 if (smp->oobd_local) { in smp_dhkey_check()
4323 memcpy(r, smp->oobd_local->r, sizeof(r)); in smp_dhkey_check()
4327 LOG_ERR("Unknown pairing method (%u)", smp->method); in smp_dhkey_check()
4332 if (bt_crypto_f6(smp->mackey, smp->rrnd, smp->prnd, r, &smp->prsp[1], in smp_dhkey_check()
4333 &smp->chan.chan.conn->le.resp_addr, in smp_dhkey_check()
4334 &smp->chan.chan.conn->le.init_addr, e)) { in smp_dhkey_check()
4342 enc_size = get_encryption_key_size(smp); in smp_dhkey_check()
4347 if (bt_conn_le_start_encryption(smp->chan.chan.conn, rand, ediv, in smp_dhkey_check()
4348 smp->tk, enc_size) < 0) { in smp_dhkey_check()
4353 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in smp_dhkey_check()
4356 if (smp->remote_dist & BT_SMP_DIST_ID_KEY) { in smp_dhkey_check()
4357 atomic_set_bit(smp->allowed_cmds, in smp_dhkey_check()
4359 } else if (smp->remote_dist & BT_SMP_DIST_SIGN) { in smp_dhkey_check()
4360 atomic_set_bit(smp->allowed_cmds, in smp_dhkey_check()
4369 if (smp->chan.chan.conn->role == BT_HCI_ROLE_PERIPHERAL) { in smp_dhkey_check()
4370 atomic_clear_bit(smp->flags, SMP_FLAG_DHCHECK_WAIT); in smp_dhkey_check()
4371 memcpy(smp->e, req->e, sizeof(smp->e)); in smp_dhkey_check()
4374 if (atomic_test_bit(smp->flags, SMP_FLAG_DHKEY_PENDING)) { in smp_dhkey_check()
4375 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in smp_dhkey_check()
4380 if (atomic_test_bit(smp->flags, SMP_FLAG_USER)) { in smp_dhkey_check()
4381 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in smp_dhkey_check()
4385 return compute_and_check_and_send_periph_dhcheck(smp); in smp_dhkey_check()
4393 static uint8_t smp_keypress_notif(struct bt_smp *smp, struct net_buf *buf) in smp_keypress_notif() argument
4395 const struct bt_conn_auth_cb *smp_auth_cb = latch_auth_cb(smp); in smp_keypress_notif()
4396 struct bt_conn *conn = smp->chan.chan.conn; in smp_keypress_notif()
4406 atomic_set_bit(smp->allowed_cmds, BT_SMP_KEYPRESS_NOTIFICATION); in smp_keypress_notif()
4415 /* Reset SMP timeout, like the spec says. */ in smp_keypress_notif()
4416 k_work_reschedule(&smp->work, SMP_TIMEOUT); in smp_keypress_notif()
4425 static uint8_t smp_keypress_notif(struct bt_smp *smp, struct net_buf *buf) in smp_keypress_notif() argument
4427 ARG_UNUSED(smp); in smp_keypress_notif()
4433 atomic_set_bit(smp->allowed_cmds, BT_SMP_KEYPRESS_NOTIFICATION); in smp_keypress_notif()
4439 uint8_t (*func)(struct bt_smp *smp, struct net_buf *buf);
4459 static bool is_in_pairing_procedure(struct bt_smp *smp) in is_in_pairing_procedure() argument
4461 return atomic_test_bit(smp->flags, SMP_FLAG_PAIRING); in is_in_pairing_procedure()
4466 struct bt_smp *smp = CONTAINER_OF(chan, struct bt_smp, chan.chan); in bt_smp_recv() local
4471 LOG_ERR("Too small SMP PDU received"); in bt_smp_recv()
4476 LOG_DBG("Received SMP code 0x%02x len %u", hdr->code, buf->len); in bt_smp_recv()
4479 * If SMP timeout occurred "no further SMP commands shall be sent over in bt_smp_recv()
4483 if (atomic_test_bit(smp->flags, SMP_FLAG_TIMEOUT)) { in bt_smp_recv()
4484 LOG_WRN("SMP command (code 0x%02x) received after timeout", hdr->code); in bt_smp_recv()
4494 LOG_WRN("Received reserved SMP code 0x%02x", hdr->code); in bt_smp_recv()
4499 LOG_WRN("Unhandled SMP code 0x%02x", hdr->code); in bt_smp_recv()
4500 smp_error(smp, BT_SMP_ERR_CMD_NOTSUPP); in bt_smp_recv()
4504 if (!atomic_test_and_clear_bit(smp->allowed_cmds, hdr->code)) { in bt_smp_recv()
4505 LOG_WRN("Unexpected SMP code 0x%02x", hdr->code); in bt_smp_recv()
4507 if (is_in_pairing_procedure(smp)) { in bt_smp_recv()
4508 smp_error(smp, BT_SMP_ERR_UNSPECIFIED); in bt_smp_recv()
4515 smp_error(smp, BT_SMP_ERR_INVALID_PARAMS); in bt_smp_recv()
4519 err = handlers[hdr->code].func(smp, buf); in bt_smp_recv()
4521 smp_error(smp, err); in bt_smp_recv()
4543 struct bt_smp *smp = &bt_smp_pool[i]; in bt_smp_pkey_ready() local
4546 if (!atomic_test_bit(smp->flags, SMP_FLAG_PKEY_SEND)) { in bt_smp_pkey_ready()
4551 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in bt_smp_pkey_ready()
4552 err = sc_send_public_key(smp); in bt_smp_pkey_ready()
4554 smp_error(smp, err); in bt_smp_pkey_ready()
4557 atomic_set_bit(smp->allowed_cmds, in bt_smp_pkey_ready()
4563 err = smp_public_key_periph(smp); in bt_smp_pkey_ready()
4565 smp_error(smp, err); in bt_smp_pkey_ready()
4573 struct bt_smp *smp = CONTAINER_OF(chan, struct bt_smp, chan.chan); in bt_smp_connected() local
4578 k_work_init_delayable(&smp->work, smp_timeout); in bt_smp_connected()
4579 smp_reset(smp); in bt_smp_connected()
4581 atomic_ptr_set(&smp->auth_cb, BT_SMP_AUTH_CB_UNINITIALIZED); in bt_smp_connected()
4582 atomic_set(&smp->bondable, BT_SMP_BONDABLE_UNINITIALIZED); in bt_smp_connected()
4587 struct bt_smp *smp = CONTAINER_OF(chan, struct bt_smp, chan.chan); in bt_smp_disconnected() local
4596 (void)k_work_cancel_delayable(&smp->work); in bt_smp_disconnected()
4598 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING) || in bt_smp_disconnected()
4599 atomic_test_bit(smp->flags, SMP_FLAG_ENC_PENDING) || in bt_smp_disconnected()
4600 atomic_test_bit(smp->flags, SMP_FLAG_SEC_REQ)) { in bt_smp_disconnected()
4602 smp_pairing_complete(smp, BT_SMP_ERR_UNSPECIFIED); in bt_smp_disconnected()
4616 (void)memset(smp, 0, sizeof(*smp)); in bt_smp_disconnected()
4622 struct bt_smp *smp = CONTAINER_OF(chan, struct bt_smp, chan.chan); in bt_smp_encrypt_change() local
4628 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_ENC_PENDING)) { in bt_smp_encrypt_change()
4637 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in bt_smp_encrypt_change()
4642 atomic_set_bit(smp->flags, SMP_FLAG_KEYS_DISTR); in bt_smp_encrypt_change()
4643 smp_pairing_complete(smp, smp_err); in bt_smp_encrypt_change()
4657 if (!atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in bt_smp_encrypt_change()
4658 smp_reset(smp); in bt_smp_encrypt_change()
4663 if (atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in bt_smp_encrypt_change()
4664 if ((smp->local_dist & BT_SMP_DIST_LINK_KEY) && in bt_smp_encrypt_change()
4665 (smp->remote_dist & BT_SMP_DIST_LINK_KEY)) { in bt_smp_encrypt_change()
4670 atomic_set_bit(smp->flags, SMP_FLAG_DERIVE_LK); in bt_smp_encrypt_change()
4676 smp->local_dist &= ~BT_SMP_DIST_LINK_KEY; in bt_smp_encrypt_change()
4677 smp->remote_dist &= ~BT_SMP_DIST_LINK_KEY; in bt_smp_encrypt_change()
4680 if (smp->remote_dist & BT_SMP_DIST_ENC_KEY) { in bt_smp_encrypt_change()
4681 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_ENCRYPT_INFO); in bt_smp_encrypt_change()
4682 } else if (smp->remote_dist & BT_SMP_DIST_ID_KEY) { in bt_smp_encrypt_change()
4683 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_IDENT_INFO); in bt_smp_encrypt_change()
4684 } else if (smp->remote_dist & BT_SMP_DIST_SIGN) { in bt_smp_encrypt_change()
4685 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SIGNING_INFO); in bt_smp_encrypt_change()
4710 !(smp->remote_dist & BT_SMP_DIST_ID_KEY)) { in bt_smp_encrypt_change()
4713 smp_err = smp_id_add_replace(smp, conn->le.keys); in bt_smp_encrypt_change()
4715 smp_pairing_complete(smp, smp_err); in bt_smp_encrypt_change()
4719 atomic_set_bit(smp->flags, SMP_FLAG_KEYS_DISTR); in bt_smp_encrypt_change()
4723 conn->role == BT_HCI_ROLE_CENTRAL && smp->remote_dist) { in bt_smp_encrypt_change()
4735 if (bt_smp_distribute_keys(smp)) { in bt_smp_encrypt_change()
4740 if (!smp->local_dist && !smp->remote_dist) { in bt_smp_encrypt_change()
4741 smp_pairing_complete(smp, 0); in bt_smp_encrypt_change()
5272 LOG_ERR("SMP AES-CMAC self tests failed"); in smp_self_test()
5278 LOG_ERR("SMP signing self tests failed"); in smp_self_test()
5284 LOG_ERR("SMP f4 self test failed"); in smp_self_test()
5290 LOG_ERR("SMP f5 self test failed"); in smp_self_test()
5296 LOG_ERR("SMP f6 self test failed"); in smp_self_test()
5302 LOG_ERR("SMP g2 self test failed"); in smp_self_test()
5309 LOG_ERR("SMP h6 self test failed"); in smp_self_test()
5315 LOG_ERR("SMP h7 self test failed"); in smp_self_test()
5321 LOG_ERR("SMP h8 self test failed"); in smp_self_test()
5337 struct bt_smp *smp; in bt_conn_set_bondable() local
5339 smp = smp_chan_get(conn); in bt_conn_set_bondable()
5340 if (!smp) { in bt_conn_set_bondable()
5344 if (atomic_cas(&smp->bondable, BT_SMP_BONDABLE_UNINITIALIZED, (atomic_val_t)enable)) { in bt_conn_set_bondable()
5354 struct bt_smp *smp; in bt_smp_auth_cb_overlay() local
5356 smp = smp_chan_get(conn); in bt_smp_auth_cb_overlay()
5357 if (!smp) { in bt_smp_auth_cb_overlay()
5361 if (atomic_ptr_cas(&smp->auth_cb, BT_SMP_AUTH_CB_UNINITIALIZED, (atomic_ptr_val_t)cb)) { in bt_smp_auth_cb_overlay()
5369 static int smp_send_keypress_notif(struct bt_smp *smp, uint8_t type) in smp_send_keypress_notif() argument
5374 buf = smp_create_pdu(smp, BT_SMP_KEYPRESS_NOTIFICATION, sizeof(*req)); in smp_send_keypress_notif()
5382 smp_send(smp, buf, NULL, NULL); in smp_send_keypress_notif()
5391 struct bt_smp *smp; in bt_smp_auth_keypress_notify() local
5393 smp = smp_chan_get(conn); in bt_smp_auth_keypress_notify()
5394 if (!smp) { in bt_smp_auth_keypress_notify()
5405 if (smp->method != PASSKEY_INPUT || in bt_smp_auth_keypress_notify()
5406 !atomic_test_bit(smp->flags, SMP_FLAG_USER)) { in bt_smp_auth_keypress_notify()
5411 return smp_send_keypress_notif(smp, type); in bt_smp_auth_keypress_notify()
5417 struct bt_smp *smp; in bt_smp_auth_passkey_entry() local
5420 smp = smp_chan_get(conn); in bt_smp_auth_passkey_entry()
5421 if (!smp) { in bt_smp_auth_passkey_entry()
5425 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_USER)) { in bt_smp_auth_passkey_entry()
5430 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in bt_smp_auth_passkey_entry()
5431 legacy_passkey_entry(smp, passkey); in bt_smp_auth_passkey_entry()
5436 smp->passkey = sys_cpu_to_le32(passkey); in bt_smp_auth_passkey_entry()
5439 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in bt_smp_auth_passkey_entry()
5440 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_CONFIRM); in bt_smp_auth_passkey_entry()
5441 err = smp_send_pairing_confirm(smp); in bt_smp_auth_passkey_entry()
5443 smp_error(smp, BT_SMP_ERR_PASSKEY_ENTRY_FAILED); in bt_smp_auth_passkey_entry()
5450 atomic_test_bit(smp->flags, SMP_FLAG_CFM_DELAYED)) { in bt_smp_auth_passkey_entry()
5451 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in bt_smp_auth_passkey_entry()
5452 err = smp_send_pairing_confirm(smp); in bt_smp_auth_passkey_entry()
5454 smp_error(smp, BT_SMP_ERR_PASSKEY_ENTRY_FAILED); in bt_smp_auth_passkey_entry()
5464 struct bt_smp *smp; in bt_smp_auth_passkey_confirm() local
5466 smp = smp_chan_get(conn); in bt_smp_auth_passkey_confirm()
5467 if (!smp) { in bt_smp_auth_passkey_confirm()
5471 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_USER)) { in bt_smp_auth_passkey_confirm()
5476 if (atomic_test_bit(smp->flags, SMP_FLAG_DHKEY_PENDING)) { in bt_smp_auth_passkey_confirm()
5477 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in bt_smp_auth_passkey_confirm()
5482 if (atomic_test_bit(smp->flags, SMP_FLAG_DHCHECK_WAIT)) { in bt_smp_auth_passkey_confirm()
5483 atomic_set_bit(smp->flags, SMP_FLAG_DHKEY_SEND); in bt_smp_auth_passkey_confirm()
5487 if (atomic_test_bit(smp->flags, SMP_FLAG_DHKEY_SEND)) { in bt_smp_auth_passkey_confirm()
5491 if (smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in bt_smp_auth_passkey_confirm()
5492 err = compute_and_send_central_dhcheck(smp); in bt_smp_auth_passkey_confirm()
5494 smp_error(smp, err); in bt_smp_auth_passkey_confirm()
5501 err = compute_and_check_and_send_periph_dhcheck(smp); in bt_smp_auth_passkey_confirm()
5503 smp_error(smp, err); in bt_smp_auth_passkey_confirm()
5514 struct bt_smp *smp; in bt_smp_le_oob_set_tk() local
5516 smp = smp_chan_get(conn); in bt_smp_le_oob_set_tk()
5517 if (!smp || !tk) { in bt_smp_le_oob_set_tk()
5523 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_USER)) { in bt_smp_le_oob_set_tk()
5534 memcpy(smp->tk, tk, 16*sizeof(uint8_t)); in bt_smp_le_oob_set_tk()
5536 legacy_user_tk_entry(smp); in bt_smp_le_oob_set_tk()
5581 static bool le_sc_oob_data_check(struct bt_smp *smp, bool oobd_local_present, in le_sc_oob_data_check() argument
5584 bool req_oob_present = le_sc_oob_data_req_check(smp); in le_sc_oob_data_check()
5585 bool rsp_oob_present = le_sc_oob_data_rsp_check(smp); in le_sc_oob_data_check()
5588 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in le_sc_oob_data_check()
5603 static int le_sc_oob_pairing_continue(struct bt_smp *smp) in le_sc_oob_pairing_continue() argument
5605 if (smp->oobd_remote) { in le_sc_oob_pairing_continue()
5609 err = bt_crypto_f4(smp->pkey, smp->pkey, smp->oobd_remote->r, 0, c); in le_sc_oob_pairing_continue()
5614 bool match = (memcmp(c, smp->oobd_remote->c, sizeof(c)) == 0); in le_sc_oob_pairing_continue()
5617 smp_error(smp, BT_SMP_ERR_CONFIRM_FAILED); in le_sc_oob_pairing_continue()
5623 smp->chan.chan.conn->role == BT_HCI_ROLE_CENTRAL) { in le_sc_oob_pairing_continue()
5624 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PAIRING_RANDOM); in le_sc_oob_pairing_continue()
5626 atomic_set_bit(smp->allowed_cmds, BT_SMP_DHKEY_CHECK); in le_sc_oob_pairing_continue()
5627 atomic_set_bit(smp->flags, SMP_FLAG_DHCHECK_WAIT); in le_sc_oob_pairing_continue()
5630 return smp_send_pairing_random(smp); in le_sc_oob_pairing_continue()
5637 struct bt_smp *smp; in bt_smp_le_oob_set_sc_data() local
5639 smp = smp_chan_get(conn); in bt_smp_le_oob_set_sc_data()
5640 if (!smp) { in bt_smp_le_oob_set_sc_data()
5644 if (!le_sc_oob_data_check(smp, (oobd_local != NULL), in bt_smp_le_oob_set_sc_data()
5649 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_OOB_PENDING)) { in bt_smp_le_oob_set_sc_data()
5653 smp->oobd_local = oobd_local; in bt_smp_le_oob_set_sc_data()
5654 smp->oobd_remote = oobd_remote; in bt_smp_le_oob_set_sc_data()
5656 return le_sc_oob_pairing_continue(smp); in bt_smp_le_oob_set_sc_data()
5663 struct bt_smp *smp; in bt_smp_le_oob_get_sc_data() local
5665 smp = smp_chan_get(conn); in bt_smp_le_oob_get_sc_data()
5666 if (!smp) { in bt_smp_le_oob_get_sc_data()
5670 if (!smp->oobd_local && !smp->oobd_remote) { in bt_smp_le_oob_get_sc_data()
5675 *oobd_local = smp->oobd_local; in bt_smp_le_oob_get_sc_data()
5679 *oobd_remote = smp->oobd_remote; in bt_smp_le_oob_get_sc_data()
5688 struct bt_smp *smp; in bt_smp_auth_cancel() local
5690 smp = smp_chan_get(conn); in bt_smp_auth_cancel()
5691 if (!smp) { in bt_smp_auth_cancel()
5695 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_USER)) { in bt_smp_auth_cancel()
5701 switch (smp->method) { in bt_smp_auth_cancel()
5704 return smp_error(smp, BT_SMP_ERR_PASSKEY_ENTRY_FAILED); in bt_smp_auth_cancel()
5706 return smp_error(smp, BT_SMP_ERR_CONFIRM_FAILED); in bt_smp_auth_cancel()
5709 return smp_error(smp, BT_SMP_ERR_OOB_NOT_AVAIL); in bt_smp_auth_cancel()
5711 return smp_error(smp, BT_SMP_ERR_UNSPECIFIED); in bt_smp_auth_cancel()
5713 LOG_ERR("Unknown pairing method (%u)", smp->method); in bt_smp_auth_cancel()
5721 struct bt_smp *smp; in bt_smp_auth_pairing_confirm() local
5723 smp = smp_chan_get(conn); in bt_smp_auth_pairing_confirm()
5724 if (!smp) { in bt_smp_auth_pairing_confirm()
5728 if (!atomic_test_and_clear_bit(smp->flags, SMP_FLAG_USER)) { in bt_smp_auth_pairing_confirm()
5734 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in bt_smp_auth_pairing_confirm()
5735 atomic_set_bit(smp->allowed_cmds, in bt_smp_auth_pairing_confirm()
5737 return legacy_send_pairing_confirm(smp); in bt_smp_auth_pairing_confirm()
5741 atomic_set_bit(smp->flags, SMP_FLAG_PKEY_SEND); in bt_smp_auth_pairing_confirm()
5745 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PUBLIC_KEY); in bt_smp_auth_pairing_confirm()
5746 return sc_send_public_key(smp); in bt_smp_auth_pairing_confirm()
5750 if (!atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in bt_smp_auth_pairing_confirm()
5751 atomic_set_bit(smp->allowed_cmds, in bt_smp_auth_pairing_confirm()
5753 return send_pairing_rsp(smp); in bt_smp_auth_pairing_confirm()
5756 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_PUBLIC_KEY); in bt_smp_auth_pairing_confirm()
5757 if (send_pairing_rsp(smp)) { in bt_smp_auth_pairing_confirm()
5796 struct bt_smp *smp; in bt_smp_start_security() local
5798 smp = smp_chan_get(conn); in bt_smp_start_security()
5799 if (!smp) { in bt_smp_start_security()
5804 if (atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in bt_smp_start_security()
5809 if (atomic_test_bit(smp->flags, SMP_FLAG_ENC_PENDING)) { in bt_smp_start_security()
5827 atomic_set_bit(smp->allowed_cmds, BT_SMP_CMD_SECURITY_REQUEST); in bt_smp_start_security()
5828 atomic_set_bit(smp->flags, SMP_FLAG_ENC_PENDING); in bt_smp_start_security()
5843 struct bt_smp *smp; in bt_smp_update_keys() local
5845 smp = smp_chan_get(conn); in bt_smp_update_keys()
5846 if (!smp) { in bt_smp_update_keys()
5850 if (!atomic_test_bit(smp->flags, SMP_FLAG_PAIRING)) { in bt_smp_update_keys()
5865 smp_error(smp, BT_SMP_ERR_UNSPECIFIED); in bt_smp_update_keys()
5870 if (atomic_test_bit(smp->flags, SMP_FLAG_SC_DEBUG_KEY)) { in bt_smp_update_keys()
5879 switch (smp->method) { in bt_smp_update_keys()
5897 conn->le.keys->enc_size = get_encryption_key_size(smp); in bt_smp_update_keys()
5904 if (atomic_test_bit(smp->flags, SMP_FLAG_SC)) { in bt_smp_update_keys()
5907 if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) { in bt_smp_update_keys()
5909 memcpy(conn->le.keys->ltk.val, smp->tk, in bt_smp_update_keys()
5918 sys_memcpy_swap(ltk, smp->tk, conn->le.keys->enc_size); in bt_smp_update_keys()
5939 struct bt_smp *smp = &bt_smp_pool[i]; in bt_smp_accept() local
5941 if (smp->chan.chan.conn) { in bt_smp_accept()
5945 smp->chan.chan.ops = &ops; in bt_smp_accept()
5947 *chan = &smp->chan.chan; in bt_smp_accept()
5952 LOG_ERR("No available SMP context for conn %p", conn); in bt_smp_accept()