if (k->ediv != ediv || k->rand != rand)
continue;
- if (ltk_role(k->type) != role)
- continue;
+ if (smp_ltk_is_sc(k)) {
+ if (k->type == SMP_LTK_P256_DEBUG &&
+ !test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags))
+ continue;
+ rcu_read_unlock();
+ return k;
+ }
- rcu_read_unlock();
- return k;
+ if (ltk_role(k->type) == role) {
+ rcu_read_unlock();
+ return k;
+ }
}
rcu_read_unlock();
rcu_read_lock();
list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
- if (addr_type == k->bdaddr_type &&
- bacmp(bdaddr, &k->bdaddr) == 0 &&
- ltk_role(k->type) == role) {
+ if (addr_type != k->bdaddr_type || bacmp(bdaddr, &k->bdaddr))
+ continue;
+
+ if (smp_ltk_is_sc(k)) {
+ if (k->type == SMP_LTK_P256_DEBUG &&
+ !test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags))
+ continue;
+ rcu_read_unlock();
+ return k;
+ }
+
+ if (ltk_role(k->type) == role) {
rcu_read_unlock();
return k;
}
if (conn == NULL)
goto not_found;
- ltk = hci_find_ltk(hdev, ev->ediv, ev->rand, conn->role);
- if (ltk == NULL)
+ ltk = hci_find_ltk_by_addr(hdev, &conn->dst, conn->dst_type,
+ conn->role);
+ if (!ltk)
goto not_found;
+ if (smp_ltk_is_sc(ltk)) {
+ /* With SC both EDiv and Rand are set to zero */
+ if (ev->ediv || ev->rand)
+ goto not_found;
+ } else {
+ /* For non-SC keys check that EDiv and Rand match */
+ if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
+ goto not_found;
+ }
+
memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
cp.handle = cpu_to_le16(conn->handle);