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