2015-05-30 13:05:26 +02:00
|
|
|
/* keys.h - Bluetooth key handling */
|
|
|
|
|
|
|
|
/*
|
2016-06-10 11:10:18 +02:00
|
|
|
* Copyright (c) 2015-2016 Intel Corporation
|
2015-05-30 13:05:26 +02:00
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-05-30 13:05:26 +02:00
|
|
|
*/
|
|
|
|
|
2015-05-31 17:17:19 +02:00
|
|
|
enum {
|
2016-02-19 08:43:37 +01:00
|
|
|
BT_KEYS_SLAVE_LTK = BIT(0),
|
|
|
|
BT_KEYS_IRK = BIT(1),
|
|
|
|
BT_KEYS_LTK = BIT(2),
|
|
|
|
BT_KEYS_LOCAL_CSRK = BIT(3),
|
|
|
|
BT_KEYS_REMOTE_CSRK = BIT(4),
|
|
|
|
BT_KEYS_LTK_P256 = BIT(5),
|
2015-05-31 17:17:19 +02:00
|
|
|
|
2015-06-29 23:05:21 +02:00
|
|
|
BT_KEYS_ALL = (BT_KEYS_SLAVE_LTK | BT_KEYS_IRK | \
|
2015-07-15 15:05:10 +02:00
|
|
|
BT_KEYS_LTK | BT_KEYS_LOCAL_CSRK | \
|
2016-08-08 16:40:30 +02:00
|
|
|
BT_KEYS_REMOTE_CSRK | BT_KEYS_LTK_P256),
|
2015-05-31 17:17:19 +02:00
|
|
|
};
|
|
|
|
|
2020-03-25 16:00:35 +01:00
|
|
|
enum {
|
|
|
|
BT_KEYS_ID_PENDING_ADD = BIT(0),
|
|
|
|
BT_KEYS_ID_PENDING_DEL = BIT(1),
|
2020-03-25 17:28:28 +01:00
|
|
|
BT_KEYS_ID_ADDED = BIT(2),
|
2020-03-25 16:00:35 +01:00
|
|
|
};
|
|
|
|
|
2015-08-17 18:01:03 +02:00
|
|
|
enum {
|
2018-04-27 19:28:20 +02:00
|
|
|
BT_KEYS_AUTHENTICATED = BIT(0),
|
|
|
|
BT_KEYS_DEBUG = BIT(1),
|
2020-03-25 16:00:35 +01:00
|
|
|
/* Bit 2 and 3 might accidentally exist in old stored keys */
|
2018-09-18 10:15:42 +02:00
|
|
|
BT_KEYS_SC = BIT(4),
|
2015-08-17 18:01:03 +02:00
|
|
|
};
|
|
|
|
|
2015-05-30 13:05:26 +02:00
|
|
|
struct bt_ltk {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t rand[8];
|
|
|
|
uint8_t ediv[2];
|
|
|
|
uint8_t val[16];
|
2015-05-30 13:05:26 +02:00
|
|
|
};
|
|
|
|
|
2015-05-31 17:36:44 +02:00
|
|
|
struct bt_irk {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t val[16];
|
2020-03-26 17:08:43 +01:00
|
|
|
bt_addr_t rpa;
|
2015-05-31 17:36:44 +02:00
|
|
|
};
|
|
|
|
|
2015-07-15 15:05:10 +02:00
|
|
|
struct bt_csrk {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t val[16];
|
|
|
|
uint32_t cnt;
|
2015-07-15 15:05:10 +02:00
|
|
|
};
|
|
|
|
|
2015-05-30 13:05:26 +02:00
|
|
|
struct bt_keys {
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t id;
|
2020-03-26 17:08:43 +01:00
|
|
|
bt_addr_le_t addr;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint8_t state;
|
|
|
|
uint8_t storage_start[0] __aligned(sizeof(void *));
|
|
|
|
uint8_t enc_size;
|
|
|
|
uint8_t flags;
|
|
|
|
uint16_t keys;
|
2020-03-26 17:08:43 +01:00
|
|
|
struct bt_ltk ltk;
|
|
|
|
struct bt_irk irk;
|
2017-08-09 08:21:11 +02:00
|
|
|
#if defined(CONFIG_BT_SIGNING)
|
2020-03-26 17:08:43 +01:00
|
|
|
struct bt_csrk local_csrk;
|
|
|
|
struct bt_csrk remote_csrk;
|
2017-08-09 08:21:11 +02:00
|
|
|
#endif /* BT_SIGNING */
|
2018-10-11 13:39:44 +02:00
|
|
|
#if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
|
2020-03-26 17:08:43 +01:00
|
|
|
struct bt_ltk slave_ltk;
|
2018-10-11 13:39:44 +02:00
|
|
|
#endif /* CONFIG_BT_SMP_SC_PAIR_ONLY */
|
2019-09-26 16:27:50 +02:00
|
|
|
#if (defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST))
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t aging_counter;
|
2019-09-26 16:27:50 +02:00
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
2015-05-30 13:05:26 +02:00
|
|
|
};
|
|
|
|
|
2018-04-27 19:01:51 +02:00
|
|
|
#define BT_KEYS_STORAGE_LEN (sizeof(struct bt_keys) - \
|
|
|
|
offsetof(struct bt_keys, storage_start))
|
|
|
|
|
2018-07-18 12:45:44 +02:00
|
|
|
void bt_keys_foreach(int type, void (*func)(struct bt_keys *keys, void *data),
|
|
|
|
void *data);
|
2017-10-03 13:57:03 +02:00
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
struct bt_keys *bt_keys_get_addr(uint8_t id, const bt_addr_le_t *addr);
|
|
|
|
struct bt_keys *bt_keys_get_type(int type, uint8_t id, const bt_addr_le_t *addr);
|
|
|
|
struct bt_keys *bt_keys_find(int type, uint8_t id, const bt_addr_le_t *addr);
|
|
|
|
struct bt_keys *bt_keys_find_irk(uint8_t id, const bt_addr_le_t *addr);
|
|
|
|
struct bt_keys *bt_keys_find_addr(uint8_t id, const bt_addr_le_t *addr);
|
2016-03-23 09:49:19 +01:00
|
|
|
|
|
|
|
void bt_keys_add_type(struct bt_keys *keys, int type);
|
2016-08-08 16:40:30 +02:00
|
|
|
void bt_keys_clear(struct bt_keys *keys);
|
|
|
|
|
2018-04-27 19:01:51 +02:00
|
|
|
#if defined(CONFIG_BT_SETTINGS)
|
|
|
|
int bt_keys_store(struct bt_keys *keys);
|
|
|
|
#else
|
|
|
|
static inline int bt_keys_store(struct bt_keys *keys)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-08 16:40:30 +02:00
|
|
|
enum {
|
2018-04-27 19:28:20 +02:00
|
|
|
BT_LINK_KEY_AUTHENTICATED = BIT(0),
|
|
|
|
BT_LINK_KEY_DEBUG = BIT(1),
|
|
|
|
BT_LINK_KEY_SC = BIT(2),
|
2016-08-08 16:40:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bt_keys_link_key {
|
2020-03-26 17:08:43 +01:00
|
|
|
bt_addr_t addr;
|
2020-01-03 02:28:45 +01:00
|
|
|
uint8_t storage_start[0] __aligned(sizeof(void *));
|
|
|
|
uint8_t flags;
|
|
|
|
uint8_t val[16];
|
|
|
|
#if (defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST))
|
|
|
|
uint32_t aging_counter;
|
|
|
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
2016-08-08 16:40:30 +02:00
|
|
|
};
|
2020-01-03 02:28:45 +01:00
|
|
|
#define BT_KEYS_LINK_KEY_STORAGE_LEN (sizeof(struct bt_keys_link_key) - \
|
|
|
|
offsetof(struct bt_keys_link_key, storage_start))
|
2016-08-08 16:40:30 +02:00
|
|
|
|
|
|
|
struct bt_keys_link_key *bt_keys_get_link_key(const bt_addr_t *addr);
|
|
|
|
struct bt_keys_link_key *bt_keys_find_link_key(const bt_addr_t *addr);
|
|
|
|
void bt_keys_link_key_clear(struct bt_keys_link_key *link_key);
|
2016-11-04 20:34:05 +01:00
|
|
|
void bt_keys_link_key_clear_addr(const bt_addr_t *addr);
|
2020-01-03 02:28:45 +01:00
|
|
|
void bt_keys_link_key_store(struct bt_keys_link_key *link_key);
|
|
|
|
|
2019-09-26 16:27:50 +02:00
|
|
|
|
|
|
|
/* This function is used to signal that the key has been used for paring */
|
|
|
|
/* It updates the aging counter and saves it to flash if configuration option */
|
|
|
|
/* BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING is enabled */
|
2020-05-27 18:26:57 +02:00
|
|
|
void bt_keys_update_usage(uint8_t id, const bt_addr_le_t *addr);
|
2020-01-03 02:28:45 +01:00
|
|
|
void bt_keys_link_key_update_usage(const bt_addr_t *addr);
|