xref: /btstack/src/ble/sm.h (revision 728f67579f9d27e206f0cfb02cf8e84e85a8d33b)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title Security Manager
40fe5a6c4eSMilanka Ringwald  *
41fe5a6c4eSMilanka Ringwald  */
42fe5a6c4eSMilanka Ringwald 
4380e33422SMatthias Ringwald #ifndef SM_H
4480e33422SMatthias Ringwald #define SM_H
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #if defined __cplusplus
473deb3ec6SMatthias Ringwald extern "C" {
483deb3ec6SMatthias Ringwald #endif
493deb3ec6SMatthias Ringwald 
508974fcd6SMatthias Ringwald #include <stdint.h>
518974fcd6SMatthias Ringwald #include "btstack_util.h"
528974fcd6SMatthias Ringwald #include "btstack_defines.h"
538974fcd6SMatthias Ringwald #include "hci.h"
543deb3ec6SMatthias Ringwald 
553deb3ec6SMatthias Ringwald typedef struct {
56665d90f2SMatthias Ringwald     btstack_linked_item_t  item;
573deb3ec6SMatthias Ringwald     bd_addr_t      address;
583deb3ec6SMatthias Ringwald     bd_addr_type_t address_type;
593deb3ec6SMatthias Ringwald } sm_lookup_entry_t;
603deb3ec6SMatthias Ringwald 
613deb3ec6SMatthias Ringwald /* API_START */
623deb3ec6SMatthias Ringwald 
633deb3ec6SMatthias Ringwald /**
643deb3ec6SMatthias Ringwald  * @brief Initializes the Security Manager, connects to L2CAP
653deb3ec6SMatthias Ringwald  */
663deb3ec6SMatthias Ringwald void sm_init(void);
673deb3ec6SMatthias Ringwald 
683deb3ec6SMatthias Ringwald /**
693deb3ec6SMatthias Ringwald  * @brief Set secret ER key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2
70899e6e02SMatthias Ringwald  * @note If not set and btstack_tlv is configured, ER key is generated and stored in TLV by SM
71b45b7749SMilanka Ringwald  * @param er key
723deb3ec6SMatthias Ringwald  */
733deb3ec6SMatthias Ringwald void sm_set_er(sm_key_t er);
743deb3ec6SMatthias Ringwald 
753deb3ec6SMatthias Ringwald /**
763deb3ec6SMatthias Ringwald  * @brief Set secret IR key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2
77899e6e02SMatthias Ringwald  * @note If not set and btstack_tlv is configured, IR key is generated and stored in TLV by SM
78b45b7749SMilanka Ringwald  * @param ir key
793deb3ec6SMatthias Ringwald  */
803deb3ec6SMatthias Ringwald void sm_set_ir(sm_key_t ir);
813deb3ec6SMatthias Ringwald 
823deb3ec6SMatthias Ringwald /**
833deb3ec6SMatthias Ringwald  * @brief Registers OOB Data Callback. The callback should set the oob_data and return 1 if OOB data is availble
843deb3ec6SMatthias Ringwald  * @param get_oob_data_callback
853deb3ec6SMatthias Ringwald  */
86a680ba6bSMatthias Ringwald void sm_register_oob_data_callback( int (*get_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_data));
873deb3ec6SMatthias Ringwald 
883deb3ec6SMatthias Ringwald /**
8989a78d34SMatthias Ringwald  * @brief Add event packet handler.
90b45b7749SMilanka Ringwald  * @param callback_handler
9189a78d34SMatthias Ringwald  */
9289a78d34SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler);
9389a78d34SMatthias Ringwald 
9489a78d34SMatthias Ringwald /**
9567f708e0SMatthias Ringwald  * @brief Remove event packet handler.
9667f708e0SMatthias Ringwald  * @param callback_handler
9767f708e0SMatthias Ringwald  */
9867f708e0SMatthias Ringwald void sm_remove_event_handler(btstack_packet_callback_registration_t * callback_handler);
9967f708e0SMatthias Ringwald 
10067f708e0SMatthias Ringwald /**
1013deb3ec6SMatthias Ringwald  * @brief Limit the STK generation methods. Bonding is stopped if the resulting one isn't in the list
1023deb3ec6SMatthias Ringwald  * @param OR combination of SM_STK_GENERATION_METHOD_
1033deb3ec6SMatthias Ringwald  */
1043deb3ec6SMatthias Ringwald void sm_set_accepted_stk_generation_methods(uint8_t accepted_stk_generation_methods);
1053deb3ec6SMatthias Ringwald 
1063deb3ec6SMatthias Ringwald /**
1073deb3ec6SMatthias Ringwald  * @brief Set the accepted encryption key size range. Bonding is stopped if the result isn't within the range
1083deb3ec6SMatthias Ringwald  * @param min_size (default 7)
1093deb3ec6SMatthias Ringwald  * @param max_size (default 16)
1103deb3ec6SMatthias Ringwald  */
1113deb3ec6SMatthias Ringwald void sm_set_encryption_key_size_range(uint8_t min_size, uint8_t max_size);
1123deb3ec6SMatthias Ringwald 
1133deb3ec6SMatthias Ringwald /**
11427c32905SMatthias Ringwald  * @brief Sets the requested authentication requirements, bonding yes/no, MITM yes/no, SC yes/no, keypress yes/no
1153deb3ec6SMatthias Ringwald  * @param OR combination of SM_AUTHREQ_ flags
1163deb3ec6SMatthias Ringwald  */
1173deb3ec6SMatthias Ringwald void sm_set_authentication_requirements(uint8_t auth_req);
1183deb3ec6SMatthias Ringwald 
1193deb3ec6SMatthias Ringwald /**
1203deb3ec6SMatthias Ringwald  * @brief Sets the available IO Capabilities
1213deb3ec6SMatthias Ringwald  * @param IO_CAPABILITY_
1223deb3ec6SMatthias Ringwald  */
1233deb3ec6SMatthias Ringwald void sm_set_io_capabilities(io_capability_t io_capability);
1243deb3ec6SMatthias Ringwald 
1253deb3ec6SMatthias Ringwald /**
1263cdbe9dbSMatthias Ringwald  * @brief Enable/disable Secure Connections Mode only
1273cdbe9dbSMatthias Ringwald  * @param enable secure connections only mode
1283cdbe9dbSMatthias Ringwald  */
1293cdbe9dbSMatthias Ringwald void sm_set_secure_connections_only_mode(bool enable);
1303cdbe9dbSMatthias Ringwald 
1313cdbe9dbSMatthias Ringwald /**
1323deb3ec6SMatthias Ringwald  * @brief Let Peripheral request an encrypted connection right after connecting
133*728f6757SMatthias Ringwald  * @param enable
1343deb3ec6SMatthias Ringwald  * @note Not used normally. Bonding is triggered by access to protected attributes in ATT Server
1353deb3ec6SMatthias Ringwald  */
136*728f6757SMatthias Ringwald void sm_set_request_security(bool enable);
1373deb3ec6SMatthias Ringwald 
1383deb3ec6SMatthias Ringwald /**
1393deb3ec6SMatthias Ringwald  * @brief Trigger Security Request
1406bc3aba4SMatthias Ringwald  * @deprecated please use sm_request_pairing instead
1413deb3ec6SMatthias Ringwald  */
142711e6c80SMatthias Ringwald void sm_send_security_request(hci_con_handle_t con_handle);
1433deb3ec6SMatthias Ringwald 
1443deb3ec6SMatthias Ringwald /**
1453deb3ec6SMatthias Ringwald  * @brief Decline bonding triggered by event before
146c8c46d51SMatthias Ringwald  * @param con_handle
1473deb3ec6SMatthias Ringwald  */
148711e6c80SMatthias Ringwald void sm_bonding_decline(hci_con_handle_t con_handle);
1493deb3ec6SMatthias Ringwald 
1503deb3ec6SMatthias Ringwald /**
1513deb3ec6SMatthias Ringwald  * @brief Confirm Just Works bonding
152c8c46d51SMatthias Ringwald  * @param con_handle
1533deb3ec6SMatthias Ringwald  */
154711e6c80SMatthias Ringwald void sm_just_works_confirm(hci_con_handle_t con_handle);
1553deb3ec6SMatthias Ringwald 
1563deb3ec6SMatthias Ringwald /**
157c8c46d51SMatthias Ringwald  * @brief Confirm value from SM_EVENT_NUMERIC_COMPARISON_REQUEST for Numeric Comparison bonding
158c8c46d51SMatthias Ringwald  * @param con_handle
159c8c46d51SMatthias Ringwald  */
160c8c46d51SMatthias Ringwald void sm_numeric_comparison_confirm(hci_con_handle_t con_handle);
161c8c46d51SMatthias Ringwald 
162c8c46d51SMatthias Ringwald /**
1633deb3ec6SMatthias Ringwald  * @brief Reports passkey input by user
164c8c46d51SMatthias Ringwald  * @param con_handle
1653deb3ec6SMatthias Ringwald  * @param passkey in [0..999999]
1663deb3ec6SMatthias Ringwald  */
167711e6c80SMatthias Ringwald void sm_passkey_input(hci_con_handle_t con_handle, uint32_t passkey);
1683deb3ec6SMatthias Ringwald 
1693deb3ec6SMatthias Ringwald /**
1703d7fe1e9SMatthias Ringwald  * @brief Send keypress notification for keyboard only devices
1713d7fe1e9SMatthias Ringwald  * @param con_handle
1723d7fe1e9SMatthias Ringwald  * @param action see SM_KEYPRESS_* in bluetooth.h
1733d7fe1e9SMatthias Ringwald  */
1743d7fe1e9SMatthias Ringwald void sm_keypress_notification(hci_con_handle_t con_handle, uint8_t action);
1753d7fe1e9SMatthias Ringwald 
1763d7fe1e9SMatthias Ringwald /**
1776bc3aba4SMatthias Ringwald  * @brief Used by att_server.c and gatt_client.c to request user authentication
178c8c46d51SMatthias Ringwald  * @param con_handle
1793deb3ec6SMatthias Ringwald  */
180711e6c80SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle);
1813deb3ec6SMatthias Ringwald 
1823deb3ec6SMatthias Ringwald /**
1833deb3ec6SMatthias Ringwald  * @brief Report user authorization decline.
184c8c46d51SMatthias Ringwald  * @param con_handle
1853deb3ec6SMatthias Ringwald  */
186711e6c80SMatthias Ringwald void sm_authorization_decline(hci_con_handle_t con_handle);
1873deb3ec6SMatthias Ringwald 
1883deb3ec6SMatthias Ringwald /**
1893deb3ec6SMatthias Ringwald  * @brief Report user authorization grant.
190c8c46d51SMatthias Ringwald  * @param con_handle
1913deb3ec6SMatthias Ringwald  */
192711e6c80SMatthias Ringwald void sm_authorization_grant(hci_con_handle_t con_handle);
1933deb3ec6SMatthias Ringwald 
1944dfd504aSMatthias Ringwald /**
195d1a1f6a4SMatthias Ringwald  * @brief Support for signed writes, used by att_server.
1966b65794dSMilanka Ringwald  * @return ready
1974dfd504aSMatthias Ringwald  */
1984dfd504aSMatthias Ringwald int sm_cmac_ready(void);
1994dfd504aSMatthias Ringwald 
2003deb3ec6SMatthias Ringwald /**
2013deb3ec6SMatthias Ringwald  * @brief Support for signed writes, used by att_server.
2029fa6f18cSMatthias Ringwald  * @note Message is in little endian to allows passing in ATT PDU without flipping.
203514d35fcSMatthias Ringwald  * @note signing data: [opcode, attribute_handle, message, sign_counter]
204514d35fcSMatthias Ringwald  * @note calculated hash in done_callback is big endian and has 16 byte.
205514d35fcSMatthias Ringwald  * @param key
206514d35fcSMatthias Ringwald  * @param opcde
207514d35fcSMatthias Ringwald  * @param attribute_handle
208514d35fcSMatthias Ringwald  * @param message_len
209514d35fcSMatthias Ringwald  * @param message
210514d35fcSMatthias Ringwald  * @param sign_counter
2113deb3ec6SMatthias Ringwald  */
2124dfd504aSMatthias Ringwald void sm_cmac_signed_write_start(const sm_key_t key, uint8_t opcode, uint16_t attribute_handle, uint16_t message_len, const uint8_t * message, uint32_t sign_counter, void (*done_callback)(uint8_t * hash));
2133deb3ec6SMatthias Ringwald 
214b45b7749SMilanka Ringwald /**
2153deb3ec6SMatthias Ringwald  * @brief Match address against bonded devices
216b45b7749SMilanka Ringwald  * @param address_type
217b45b7749SMilanka Ringwald  * @param address
2183deb3ec6SMatthias Ringwald  * @return 0 if successfully added to lookup queue
2193deb3ec6SMatthias Ringwald  * @note Triggers SM_IDENTITY_RESOLVING_* events
2203deb3ec6SMatthias Ringwald  */
221b45b7749SMilanka Ringwald int sm_address_resolution_lookup(uint8_t address_type, bd_addr_t address);
2223deb3ec6SMatthias Ringwald 
2233deb3ec6SMatthias Ringwald /**
224ba394633SMatthias Ringwald  * @brief Get Identity Resolving state
225ba394633SMatthias Ringwald  * @param con_handle
226ba394633SMatthias Ringwald  * @return irk_lookup_state_t
2276b65794dSMilanka Ringwald  * @note return IRK_LOOKUP_IDLE if connection does not exist
228ba394633SMatthias Ringwald  */
229ba394633SMatthias Ringwald irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle);
230ba394633SMatthias Ringwald 
231ba394633SMatthias Ringwald /**
2323deb3ec6SMatthias Ringwald  * @brief Identify device in LE Device DB.
233b45b7749SMilanka Ringwald  * @param con_handle
2343deb3ec6SMatthias Ringwald  * @return index from le_device_db or -1 if not found/identified
2353deb3ec6SMatthias Ringwald  */
236711e6c80SMatthias Ringwald int sm_le_device_index(hci_con_handle_t con_handle);
237df86eb96SMatthias Ringwald 
238df86eb96SMatthias Ringwald /**
239916ea5b2SMatthias Ringwald  * @brief Get LTK for encrypted connection
240916ea5b2SMatthias Ringwald  * @param con_handle
241916ea5b2SMatthias Ringwald  * @param ltk buffer to store long term key
242916ea5b2SMatthias Ringwald  * @return ERROR_CODE_SUCCESS ok
243916ea5b2SMatthias Ringwald  *         ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if no connection for this con handle exists
244916ea5b2SMatthias Ringwald  *         ERROR_CODE_PIN_OR_KEY_MISSING if connection is not encrypted
245916ea5b2SMatthias Ringwald  */
246916ea5b2SMatthias Ringwald uint8_t sm_get_ltk(hci_con_handle_t con_handle, sm_key_t ltk);
247916ea5b2SMatthias Ringwald 
248916ea5b2SMatthias Ringwald /**
2494b8c611fSMatthias Ringwald  * @brief Use fixec passkey for Legacy and SC instead of generating a random number
250caf15bf3SMatthias Ringwald  * @note Can be used to improve security over Just Works if no keyboard or displary are present and
251caf15bf3SMatthias Ringwald  *       individual random passkey can be printed on the device during production
252caf15bf3SMatthias Ringwald  * @param passkey
253caf15bf3SMatthias Ringwald  */
2544b8c611fSMatthias Ringwald void sm_use_fixed_passkey_in_display_role(uint32_t passkey);
255caf15bf3SMatthias Ringwald 
2566c39055aSMatthias Ringwald /**
2576c39055aSMatthias Ringwald  * @brief Allow connection re-encryption in Peripheral (Responder) role for LE Legacy Pairing
2586c39055aSMatthias Ringwald  *       without entry for Central device stored in LE Device DB
2596c39055aSMatthias Ringwald  * @note BTstack in Peripheral Role (Responder) supports LE Legacy Pairing without a persistent LE Device DB as
2606c39055aSMatthias Ringwald  *       the LTK is reconstructed from a local secret IRK and EDIV + Random stored on Central (Initiator) device
2616c39055aSMatthias Ringwald  *       On the downside, it's not really possible to delete a pairing if this is enabled.
2626c39055aSMatthias Ringwald  * @param allow encryption using reconstructed LTK without stored entry (Default: 1)
2636c39055aSMatthias Ringwald  */
2646c39055aSMatthias Ringwald void sm_allow_ltk_reconstruction_without_le_device_db_entry(int allow);
2656c39055aSMatthias Ringwald 
266c59d0c92SMatthias Ringwald /**
267c59d0c92SMatthias Ringwald  * @brief Generate OOB data for LE Secure Connections
268c59d0c92SMatthias Ringwald  * @note This generates a 128 bit random number ra and then calculates Ca = f4(PKa, PKa, ra, 0)
2694acf7b7bSMatthias Ringwald  *       New OOB data should be generated for each pairing. Ra is used for subsequent OOB pairings
270c59d0c92SMatthias Ringwald  * @param callback
2716b65794dSMilanka Ringwald  * @return status
272c59d0c92SMatthias Ringwald  */
273c59d0c92SMatthias Ringwald uint8_t sm_generate_sc_oob_data(void (*callback)(const uint8_t * confirm_value, const uint8_t * random_value));
274c59d0c92SMatthias Ringwald 
275a680ba6bSMatthias Ringwald /**
276a680ba6bSMatthias Ringwald  * @brief Registers OOB Data Callback for LE Secure Conections. The callback should set all arguments and return 1 if OOB data is availble
277a680ba6bSMatthias Ringwald  * @note the oob_sc_local_random usually is the random_value returend by sm_generate_sc_oob_data
278a680ba6bSMatthias Ringwald  * @param get_oob_data_callback
279a680ba6bSMatthias Ringwald  */
2804acf7b7bSMatthias Ringwald void sm_register_sc_oob_data_callback( int (*get_sc_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_sc_peer_confirm, uint8_t * oob_sc_peer_random));
281a680ba6bSMatthias Ringwald 
282b96d60a6SMatthias Ringwald /**
283b96d60a6SMatthias Ringwald  * @bbrief Register LTK Callback that allows to provide a custom LTK on re-encryption. The callback returns true if LTK was modified
284b96d60a6SMatthias Ringwald  * @param get_ltk_callback
285b96d60a6SMatthias Ringwald  */
286b96d60a6SMatthias Ringwald void sm_register_ltk_callback( bool (*get_ltk_callback)(hci_con_handle_t con_handle, uint8_t address_type, bd_addr_t addr, uint8_t * ltk));
287b96d60a6SMatthias Ringwald 
2883deb3ec6SMatthias Ringwald /* API_END */
2893deb3ec6SMatthias Ringwald 
29015537ea4SMatthias Ringwald /**
29115537ea4SMatthias Ringwald  * @brief De-Init SM
29215537ea4SMatthias Ringwald  */
29315537ea4SMatthias Ringwald void sm_deinit(void);
29415537ea4SMatthias Ringwald 
295db88441fSMatthias Ringwald /**
296db88441fSMatthias Ringwald  * @brief Use Debug Keys for LE Secure Connections for testing until restart
297db88441fSMatthias Ringwald  * @note Requires ENABLE_LE_SECURE_CONNECTIONS and ENABLE_LE_SECURE_CONNECTIONS_DEBUG_KEY
298db88441fSMatthias Ringwald  */
299db88441fSMatthias Ringwald void sm_test_enable_secure_connections_debug_keys(void);
300db88441fSMatthias Ringwald 
3018974fcd6SMatthias Ringwald // PTS testing
3028974fcd6SMatthias Ringwald void sm_test_set_irk(sm_key_t irk);
3038974fcd6SMatthias Ringwald void sm_test_use_fixed_local_csrk(void);
3048974fcd6SMatthias Ringwald 
305db88441fSMatthias Ringwald 
306192365feSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
307192365feSMatthias Ringwald void sm_test_set_pairing_failure(int reason);
308192365feSMatthias Ringwald #endif
309192365feSMatthias Ringwald 
3103deb3ec6SMatthias Ringwald #if defined __cplusplus
3113deb3ec6SMatthias Ringwald }
3123deb3ec6SMatthias Ringwald #endif
3133deb3ec6SMatthias Ringwald 
31480e33422SMatthias Ringwald #endif // SM_H
315