1 /* keys_br.c - Bluetooth BR/EDR key handling */
2
3 /*
4 * Copyright (c) 2015-2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <ble_os.h>
10 #include <string.h>
11 #include <atomic.h>
12 #include <misc/util.h>
13
14 #include <bluetooth/bluetooth.h>
15 #include <bluetooth/conn.h>
16 #include <bluetooth/hci.h>
17
18 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_KEYS)
19 #define LOG_MODULE_NAME bt_keys_br
20 #include "common/log.h"
21
22 #include "hci_core.h"
23 #include "keys.h"
24
25 static struct bt_keys_link_key key_pool[CONFIG_BT_MAX_PAIRED];
26
bt_keys_find_link_key(const bt_addr_t * addr)27 struct bt_keys_link_key *bt_keys_find_link_key(const bt_addr_t *addr)
28 {
29 struct bt_keys_link_key *key;
30 int i;
31
32 BT_DBG("%s", bt_addr_str(addr));
33
34 for (i = 0; i < ARRAY_SIZE(key_pool); i++) {
35 key = &key_pool[i];
36
37 if (!bt_addr_cmp(&key->addr, addr)) {
38 return key;
39 }
40 }
41
42 return NULL;
43 }
44
bt_keys_get_link_key(const bt_addr_t * addr)45 struct bt_keys_link_key *bt_keys_get_link_key(const bt_addr_t *addr)
46 {
47 struct bt_keys_link_key *key;
48
49 key = bt_keys_find_link_key(addr);
50 if (key) {
51 return key;
52 }
53
54 key = bt_keys_find_link_key(BT_ADDR_ANY);
55 if (key) {
56 bt_addr_copy(&key->addr, addr);
57 BT_DBG("created %p for %s", key, bt_addr_str(addr));
58
59 return key;
60 }
61
62 BT_DBG("unable to create link key for %s", bt_addr_str(addr));
63
64 return NULL;
65 }
66
bt_keys_link_key_clear(struct bt_keys_link_key * link_key)67 void bt_keys_link_key_clear(struct bt_keys_link_key *link_key)
68 {
69 BT_DBG("%s", bt_addr_str(&link_key->addr));
70
71 (void)memset(link_key, 0, sizeof(*link_key));
72 }
73
bt_keys_link_key_clear_addr(const bt_addr_t * addr)74 void bt_keys_link_key_clear_addr(const bt_addr_t *addr)
75 {
76 struct bt_keys_link_key *key;
77
78 if (!addr) {
79 (void)memset(key_pool, 0, sizeof(key_pool));
80 return;
81 }
82
83 key = bt_keys_find_link_key(addr);
84 if (key) {
85 bt_keys_link_key_clear(key);
86 }
87 }
88