1 /* keys.h - Bluetooth key handling */
2 
3 /*
4  * Copyright (c) 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 enum {
10 	BT_KEYS_PERIPH_LTK     = BIT(0),
11 	BT_KEYS_IRK            = BIT(1),
12 	BT_KEYS_LTK            = BIT(2),
13 	BT_KEYS_LOCAL_CSRK     = BIT(3),
14 	BT_KEYS_REMOTE_CSRK    = BIT(4),
15 	BT_KEYS_LTK_P256       = BIT(5),
16 
17 	BT_KEYS_ALL            = (BT_KEYS_PERIPH_LTK | BT_KEYS_IRK |
18 				  BT_KEYS_LTK | BT_KEYS_LOCAL_CSRK |
19 				  BT_KEYS_REMOTE_CSRK | BT_KEYS_LTK_P256),
20 };
21 
22 enum {
23 	BT_KEYS_ID_PENDING_ADD  = BIT(0),
24 	BT_KEYS_ID_PENDING_DEL  = BIT(1),
25 	BT_KEYS_ID_ADDED        = BIT(2),
26 };
27 
28 enum {
29 	BT_KEYS_AUTHENTICATED   = BIT(0),
30 	BT_KEYS_DEBUG           = BIT(1),
31 	/* Bit 2 and 3 might accidentally exist in old stored keys */
32 	BT_KEYS_SC              = BIT(4),
33 };
34 
35 struct bt_ltk {
36 	uint8_t                    rand[8];
37 	uint8_t                    ediv[2];
38 	uint8_t                    val[16];
39 };
40 
41 struct bt_irk {
42 	uint8_t                    val[16];
43 	bt_addr_t               rpa;
44 };
45 
46 struct bt_csrk {
47 	uint8_t                    val[16];
48 	uint32_t                   cnt;
49 };
50 
51 struct bt_keys {
52 	uint8_t                    id;
53 	bt_addr_le_t            addr;
54 	uint8_t                    state;
55 	uint8_t                    storage_start[0] __aligned(sizeof(void *));
56 	uint8_t                    enc_size;
57 	uint8_t                    flags;
58 	uint16_t                   keys;
59 	struct bt_ltk           ltk;
60 	struct bt_irk           irk;
61 #if defined(CONFIG_BT_SIGNING)
62 	struct bt_csrk          local_csrk;
63 	struct bt_csrk          remote_csrk;
64 #endif /* BT_SIGNING */
65 #if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
66 	struct bt_ltk           periph_ltk;
67 #endif /* CONFIG_BT_SMP_SC_PAIR_ONLY */
68 #if (defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST))
69 	uint32_t                   aging_counter;
70 #endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
71 };
72 
73 #define BT_KEYS_STORAGE_LEN     (sizeof(struct bt_keys) - \
74 				 offsetof(struct bt_keys, storage_start))
75 
76 void bt_keys_foreach(int type, void (*func)(struct bt_keys *keys, void *data),
77 		     void *data);
78 
79 struct bt_keys *bt_keys_get_addr(uint8_t id, const bt_addr_le_t *addr);
80 struct bt_keys *bt_keys_get_type(int type, uint8_t id, const bt_addr_le_t *addr);
81 struct bt_keys *bt_keys_find(int type, uint8_t id, const bt_addr_le_t *addr);
82 struct bt_keys *bt_keys_find_irk(uint8_t id, const bt_addr_le_t *addr);
83 struct bt_keys *bt_keys_find_addr(uint8_t id, const bt_addr_le_t *addr);
84 
85 void bt_keys_add_type(struct bt_keys *keys, int type);
86 void bt_keys_clear(struct bt_keys *keys);
87 
88 #if defined(CONFIG_BT_SETTINGS)
89 int bt_keys_store(struct bt_keys *keys);
90 #else
bt_keys_store(struct bt_keys * keys)91 static inline int bt_keys_store(struct bt_keys *keys)
92 {
93 	return 0;
94 }
95 #endif
96 
97 enum {
98 	BT_LINK_KEY_AUTHENTICATED  = BIT(0),
99 	BT_LINK_KEY_DEBUG          = BIT(1),
100 	BT_LINK_KEY_SC             = BIT(2),
101 };
102 
103 struct bt_keys_link_key {
104 	bt_addr_t               addr;
105 	uint8_t                 storage_start[0]  __aligned(sizeof(void *));
106 	uint8_t                 flags;
107 	uint8_t                 val[16];
108 #if (defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST))
109 	uint32_t                aging_counter;
110 #endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
111 };
112 #define BT_KEYS_LINK_KEY_STORAGE_LEN     (sizeof(struct bt_keys_link_key) - \
113 	offsetof(struct bt_keys_link_key, storage_start))
114 
115 struct bt_keys_link_key *bt_keys_get_link_key(const bt_addr_t *addr);
116 struct bt_keys_link_key *bt_keys_find_link_key(const bt_addr_t *addr);
117 void bt_keys_link_key_clear(struct bt_keys_link_key *link_key);
118 void bt_keys_link_key_clear_addr(const bt_addr_t *addr);
119 void bt_keys_link_key_store(struct bt_keys_link_key *link_key);
120 
121 
122 /* This function is used to signal that the key has been used for paring */
123 /* It updates the aging counter and saves it to flash if configuration option */
124 /* BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING is enabled */
125 void bt_keys_update_usage(uint8_t id, const bt_addr_le_t *addr);
126 void bt_keys_link_key_update_usage(const bt_addr_t *addr);
127 
128 void bt_keys_show_sniffer_info(struct bt_keys *keys, void *data);
129