xref: /btstack/src/ble/le_device_db.h (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /**
39  * @title Device Database
40  *
41  */
42 
43 #ifndef LE_DEVICE_DB_H
44 #define LE_DEVICE_DB_H
45 
46 #include "btstack_util.h"
47 #include "btstack_config.h"
48 
49 #if defined __cplusplus
50 extern "C" {
51 #endif
52 
53 /**
54 
55 	Note: LE Device DB for pure LE Peripherals is not required if only LE Legacy Pairing without signed writes is used
56 
57 	Per bonded device:
58 	- it stores the Identity Resolving Key (IRK) and its address to resolve private addresses
59     - it stores the LTK + EDIV, RAND. EDIV + RAND allows a LE Peripheral to reconstruct the LTK
60     - it stores the Connection Signature Resolving Key (CSRK) and the last used counter.
61     	The CSRK is used to generate the signatur on the remote device and is needed to verify the signature itself
62 		The Counter is necessary to prevent reply attacks
63 
64 */
65 
66 
67 // LE Device db interface
68 
69 /* API_START */
70 
71 /**
72  * @brief init
73  */
74 void le_device_db_init(void);
75 
76 
77 /**
78  * @brief sets local bd addr. allows for db per Bluetooth controller
79  * @param bd_addr
80  */
81 void le_device_db_set_local_bd_addr(bd_addr_t bd_addr);
82 
83 /**
84  * @brief add device to db
85  * @param addr_type, address of the device
86  * @param irk of the device
87  * @return index if successful, -1 otherwise
88  */
89 int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk);
90 
91 /**
92  * @brief get number of devices in db
93  * @return number of device in db
94  */
95 int le_device_db_count(void);
96 
97 /**
98  * @brief get max number of devices in db for enumeration
99  * @return max number of device in db
100  */
101 int le_device_db_max_count(void);
102 
103 /**
104  * @brief get device information: addr type and address needed to identify device
105  * @param index
106  * @param addr_type, address of the device as output
107  * @param irk of the device
108  */
109 void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk);
110 
111 
112 /**
113  * @brief set remote encryption info
114  * @brief index
115  * @brief ediv
116  * @brief rand
117  * @brief ltk
118  * @brief key size
119  * @brief authenticated
120  * @brief authorized
121  * @breif secure_connection
122  */
123 void le_device_db_encryption_set(int index, uint16_t ediv, uint8_t rand[8], sm_key_t ltk, int key_size, int authenticated, int authorized, int secure_connection);
124 
125 /**
126  * @brief get remote encryption info
127  * @brief index
128  * @brief ediv
129  * @brief rand
130  * @brief ltk
131  * @brief key size
132  * @brief authenticated
133  * @brief authorized
134  * @breif secure_connection
135  */
136 void le_device_db_encryption_get(int index, uint16_t * ediv, uint8_t rand[8], sm_key_t ltk,  int * key_size, int * authenticated, int * authorized, int * secure_connection);
137 
138 #ifdef ENABLE_LE_SIGNED_WRITE
139 
140 /**
141  * @brief set local signing key for this device
142  * @param index
143  * @param signing key as input
144  */
145 void le_device_db_local_csrk_set(int index, sm_key_t csrk);
146 
147 /**
148  * @brief get local signing key for this device
149  * @param index
150  * @param signing key as output
151  */
152 void le_device_db_local_csrk_get(int index, sm_key_t csrk);
153 
154 /**
155  * @brief set remote signing key for this device
156  * @param index
157  * @param signing key as input
158  */
159 void le_device_db_remote_csrk_set(int index, sm_key_t csrk);
160 
161 /**
162  * @brief get remote signing key for this device
163  * @param index
164  * @param signing key as output
165  */
166 void le_device_db_remote_csrk_get(int index, sm_key_t csrk);
167 
168 /**
169  * @brief query last used/seen signing counter
170  * @param index
171  * @return next expected counter, 0 after devices was added
172  */
173 uint32_t le_device_db_remote_counter_get(int index);
174 
175 /**
176  * @brief update signing counter
177  * @param index
178  * @param counter to store
179  */
180 void le_device_db_remote_counter_set(int index, uint32_t counter);
181 
182 /**
183  * @brief query last used/seen signing counter
184  * @param index
185  * @return next expected counter, 0 after devices was added
186  */
187 uint32_t le_device_db_local_counter_get(int index);
188 
189 /**
190  * @brief update signing counter
191  * @param index
192  * @param counter to store
193  */
194 void le_device_db_local_counter_set(int index, uint32_t counter);
195 
196 #endif
197 
198 /**
199  * @brief free device
200  * @param index
201  */
202 void le_device_db_remove(int index);
203 
204 void le_device_db_dump(void);
205 
206 /* API_END */
207 
208 #if defined __cplusplus
209 }
210 #endif
211 
212 #endif // LE_DEVICE_DB_H
213