xref: /btstack/src/ble/le_device_db.h (revision c37cd8f3d1350b92a2f66c31b2a5fcd75f8c91a4)
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 MATTHIAS
24  * RINGWALD 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 #ifndef __LE_DEVICE_DB_H
39 #define __LE_DEVICE_DB_H
40 
41 #include "btstack_util.h"
42 #include "btstack_config.h"
43 
44 #if defined __cplusplus
45 extern "C" {
46 #endif
47 
48 /**
49 
50 	Note: LE Device DB for pure LE Peripherals is not required if only LE Legacy Pairing without signed writes is used
51 
52 	Per bonded device:
53 	- it stores the Identity Resolving Key (IRK) and its address to resolve private addresses
54     - it stores the LTK + EDIV, RAND. EDIV + RAND allows a LE Peripheral to reconstruct the LTK
55     - it stores the Connection Signature Resolving Key (CSRK) and the last used counter.
56     	The CSRK is used to generate the signatur on the remote device and is needed to verify the signature itself
57 		The Counter is necessary to prevent reply attacks
58 
59 */
60 
61 
62 // LE Device db interface
63 
64 /* API_START */
65 
66 /**
67  * @brief init
68  */
69 void le_device_db_init(void);
70 
71 
72 /**
73  * @brief sets local bd addr. allows for db per Bluetooth controller
74  * @param bd_addr
75  */
76 void le_device_db_set_local_bd_addr(bd_addr_t bd_addr);
77 
78 /**
79  * @brief add device to db
80  * @param addr_type, address of the device
81  * @param irk of the device
82  * @returns index if successful, -1 otherwise
83  */
84 int le_device_db_add(int addr_type, bd_addr_t addr, sm_key_t irk);
85 
86 /**
87  * @brief get number of devices in db for enumeration
88  * @returns number of device in db
89  */
90 int le_device_db_count(void);
91 
92 /**
93  * @brief get device information: addr type and address needed to identify device
94  * @param index
95  * @param addr_type, address of the device as output
96  * @param irk of the device
97  */
98 void le_device_db_info(int index, int * addr_type, bd_addr_t addr, sm_key_t irk);
99 
100 
101 /**
102  * @brief set remote encryption info
103  * @brief index
104  * @brief ediv
105  * @brief rand
106  * @brief ltk
107  * @brief key size
108  * @brief authenticated
109  * @brief authorized
110  */
111 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);
112 
113 /**
114  * @brief get remote encryption info
115  * @brief index
116  * @brief ediv
117  * @brief rand
118  * @brief ltk
119  * @brief key size
120  * @brief authenticated
121  * @brief authorized
122  */
123 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);
124 
125 #ifdef ENABLE_LE_SIGNED_WRITE
126 
127 /**
128  * @brief set local signing key for this device
129  * @param index
130  * @param signing key as input
131  */
132 void le_device_db_local_csrk_set(int index, sm_key_t csrk);
133 
134 /**
135  * @brief get local signing key for this device
136  * @param index
137  * @param signing key as output
138  */
139 void le_device_db_local_csrk_get(int index, sm_key_t csrk);
140 
141 /**
142  * @brief set remote signing key for this device
143  * @param index
144  * @param signing key as input
145  */
146 void le_device_db_remote_csrk_set(int index, sm_key_t csrk);
147 
148 /**
149  * @brief get remote signing key for this device
150  * @param index
151  * @param signing key as output
152  */
153 void le_device_db_remote_csrk_get(int index, sm_key_t csrk);
154 
155 /**
156  * @brief query last used/seen signing counter
157  * @param index
158  * @returns next expected counter, 0 after devices was added
159  */
160 uint32_t le_device_db_remote_counter_get(int index);
161 
162 /**
163  * @brief update signing counter
164  * @param index
165  * @param counter to store
166  */
167 void le_device_db_remote_counter_set(int index, uint32_t counter);
168 
169 /**
170  * @brief query last used/seen signing counter
171  * @param index
172  * @returns next expected counter, 0 after devices was added
173  */
174 uint32_t le_device_db_local_counter_get(int index);
175 
176 /**
177  * @brief update signing counter
178  * @param index
179  * @param counter to store
180  */
181 void le_device_db_local_counter_set(int index, uint32_t counter);
182 
183 #endif
184 
185 /**
186  * @brief free device
187  * @param index
188  */
189 void le_device_db_remove(int index);
190 
191 void le_device_db_dump(void);
192 
193 /* API_END */
194 
195 #if defined __cplusplus
196 }
197 #endif
198 
199 #endif // __LE_DEVICE_DB_H
200