xref: /btstack/src/ble/sm.h (revision d1a1f6a4cfae6dd341f1d30e1d0507bf3ca29c49)
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
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD 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 
383deb3ec6SMatthias Ringwald #ifndef __SM_H
393deb3ec6SMatthias Ringwald #define __SM_H
403deb3ec6SMatthias Ringwald 
413deb3ec6SMatthias Ringwald  #if defined __cplusplus
423deb3ec6SMatthias Ringwald extern "C" {
433deb3ec6SMatthias Ringwald #endif
443deb3ec6SMatthias Ringwald 
458974fcd6SMatthias Ringwald #include <stdint.h>
468974fcd6SMatthias Ringwald #include "btstack_util.h"
478974fcd6SMatthias Ringwald #include "btstack_defines.h"
488974fcd6SMatthias Ringwald #include "hci.h"
493deb3ec6SMatthias Ringwald 
503deb3ec6SMatthias Ringwald typedef struct {
51665d90f2SMatthias Ringwald     btstack_linked_item_t  item;
523deb3ec6SMatthias Ringwald     bd_addr_t      address;
533deb3ec6SMatthias Ringwald     bd_addr_type_t address_type;
543deb3ec6SMatthias Ringwald } sm_lookup_entry_t;
553deb3ec6SMatthias Ringwald 
563deb3ec6SMatthias Ringwald /* API_START */
573deb3ec6SMatthias Ringwald 
583deb3ec6SMatthias Ringwald /**
593deb3ec6SMatthias Ringwald  * @brief Initializes the Security Manager, connects to L2CAP
603deb3ec6SMatthias Ringwald  */
613deb3ec6SMatthias Ringwald void sm_init(void);
623deb3ec6SMatthias Ringwald 
633deb3ec6SMatthias Ringwald /**
643deb3ec6SMatthias Ringwald  * @brief Set secret ER key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2
653deb3ec6SMatthias Ringwald  * @param er
663deb3ec6SMatthias Ringwald  */
673deb3ec6SMatthias Ringwald void sm_set_er(sm_key_t er);
683deb3ec6SMatthias Ringwald 
693deb3ec6SMatthias Ringwald /**
703deb3ec6SMatthias Ringwald  * @brief Set secret IR key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2
713deb3ec6SMatthias Ringwald  */
723deb3ec6SMatthias Ringwald void sm_set_ir(sm_key_t ir);
733deb3ec6SMatthias Ringwald 
743deb3ec6SMatthias Ringwald /**
753deb3ec6SMatthias Ringwald  *
763deb3ec6SMatthias Ringwald  * @brief Registers OOB Data Callback. The callback should set the oob_data and return 1 if OOB data is availble
773deb3ec6SMatthias Ringwald  * @param get_oob_data_callback
783deb3ec6SMatthias Ringwald  */
79a680ba6bSMatthias Ringwald void sm_register_oob_data_callback( int (*get_oob_data_callback)(uint8_t address_type, bd_addr_t addr, uint8_t * oob_data));
803deb3ec6SMatthias Ringwald 
813deb3ec6SMatthias Ringwald /**
8289a78d34SMatthias Ringwald  * @brief Add event packet handler.
8389a78d34SMatthias Ringwald  */
8489a78d34SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler);
8589a78d34SMatthias Ringwald 
8689a78d34SMatthias Ringwald /**
873deb3ec6SMatthias Ringwald  * @brief Limit the STK generation methods. Bonding is stopped if the resulting one isn't in the list
883deb3ec6SMatthias Ringwald  * @param OR combination of SM_STK_GENERATION_METHOD_
893deb3ec6SMatthias Ringwald  */
903deb3ec6SMatthias Ringwald void sm_set_accepted_stk_generation_methods(uint8_t accepted_stk_generation_methods);
913deb3ec6SMatthias Ringwald 
923deb3ec6SMatthias Ringwald /**
933deb3ec6SMatthias Ringwald  * @brief Set the accepted encryption key size range. Bonding is stopped if the result isn't within the range
943deb3ec6SMatthias Ringwald  * @param min_size (default 7)
953deb3ec6SMatthias Ringwald  * @param max_size (default 16)
963deb3ec6SMatthias Ringwald  */
973deb3ec6SMatthias Ringwald void sm_set_encryption_key_size_range(uint8_t min_size, uint8_t max_size);
983deb3ec6SMatthias Ringwald 
993deb3ec6SMatthias Ringwald /**
10027c32905SMatthias Ringwald  * @brief Sets the requested authentication requirements, bonding yes/no, MITM yes/no, SC yes/no, keypress yes/no
1013deb3ec6SMatthias Ringwald  * @param OR combination of SM_AUTHREQ_ flags
1023deb3ec6SMatthias Ringwald  */
1033deb3ec6SMatthias Ringwald void sm_set_authentication_requirements(uint8_t auth_req);
1043deb3ec6SMatthias Ringwald 
1053deb3ec6SMatthias Ringwald /**
1063deb3ec6SMatthias Ringwald  * @brief Sets the available IO Capabilities
1073deb3ec6SMatthias Ringwald  * @param IO_CAPABILITY_
1083deb3ec6SMatthias Ringwald  */
1093deb3ec6SMatthias Ringwald void sm_set_io_capabilities(io_capability_t io_capability);
1103deb3ec6SMatthias Ringwald 
1113deb3ec6SMatthias Ringwald /**
1123deb3ec6SMatthias Ringwald  * @brief Let Peripheral request an encrypted connection right after connecting
1133deb3ec6SMatthias Ringwald  * @note Not used normally. Bonding is triggered by access to protected attributes in ATT Server
1143deb3ec6SMatthias Ringwald  */
1153deb3ec6SMatthias Ringwald void sm_set_request_security(int enable);
1163deb3ec6SMatthias Ringwald 
1173deb3ec6SMatthias Ringwald /**
1183deb3ec6SMatthias Ringwald  * @brief Trigger Security Request
1193deb3ec6SMatthias Ringwald  * @note Not used normally. Bonding is triggered by access to protected attributes in ATT Server
1203deb3ec6SMatthias Ringwald  */
121711e6c80SMatthias Ringwald void sm_send_security_request(hci_con_handle_t con_handle);
1223deb3ec6SMatthias Ringwald 
1233deb3ec6SMatthias Ringwald /**
1243deb3ec6SMatthias Ringwald  * @brief Decline bonding triggered by event before
125c8c46d51SMatthias Ringwald  * @param con_handle
1263deb3ec6SMatthias Ringwald  */
127711e6c80SMatthias Ringwald void sm_bonding_decline(hci_con_handle_t con_handle);
1283deb3ec6SMatthias Ringwald 
1293deb3ec6SMatthias Ringwald /**
1303deb3ec6SMatthias Ringwald  * @brief Confirm Just Works bonding
131c8c46d51SMatthias Ringwald  * @param con_handle
1323deb3ec6SMatthias Ringwald  */
133711e6c80SMatthias Ringwald void sm_just_works_confirm(hci_con_handle_t con_handle);
1343deb3ec6SMatthias Ringwald 
1353deb3ec6SMatthias Ringwald /**
136c8c46d51SMatthias Ringwald  * @brief Confirm value from SM_EVENT_NUMERIC_COMPARISON_REQUEST for Numeric Comparison bonding
137c8c46d51SMatthias Ringwald  * @param con_handle
138c8c46d51SMatthias Ringwald  */
139c8c46d51SMatthias Ringwald void sm_numeric_comparison_confirm(hci_con_handle_t con_handle);
140c8c46d51SMatthias Ringwald 
141c8c46d51SMatthias Ringwald /**
1423deb3ec6SMatthias Ringwald  * @brief Reports passkey input by user
143c8c46d51SMatthias Ringwald  * @param con_handle
1443deb3ec6SMatthias Ringwald  * @param passkey in [0..999999]
1453deb3ec6SMatthias Ringwald  */
146711e6c80SMatthias Ringwald void sm_passkey_input(hci_con_handle_t con_handle, uint32_t passkey);
1473deb3ec6SMatthias Ringwald 
1483deb3ec6SMatthias Ringwald /**
1493d7fe1e9SMatthias Ringwald  * @brief Send keypress notification for keyboard only devices
1503d7fe1e9SMatthias Ringwald  * @param con_handle
1513d7fe1e9SMatthias Ringwald  * @param action see SM_KEYPRESS_* in bluetooth.h
1523d7fe1e9SMatthias Ringwald  */
1533d7fe1e9SMatthias Ringwald void sm_keypress_notification(hci_con_handle_t con_handle, uint8_t action);
1543d7fe1e9SMatthias Ringwald 
1553d7fe1e9SMatthias Ringwald /**
1563deb3ec6SMatthias Ringwald  * @brief Used by att_server.c to request user authorization.
157c8c46d51SMatthias Ringwald  * @param con_handle
1583deb3ec6SMatthias Ringwald  */
159711e6c80SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle);
1603deb3ec6SMatthias Ringwald 
1613deb3ec6SMatthias Ringwald /**
1623deb3ec6SMatthias Ringwald  * @brief Report user authorization decline.
163c8c46d51SMatthias Ringwald  * @param con_handle
1643deb3ec6SMatthias Ringwald  */
165711e6c80SMatthias Ringwald void sm_authorization_decline(hci_con_handle_t con_handle);
1663deb3ec6SMatthias Ringwald 
1673deb3ec6SMatthias Ringwald /**
1683deb3ec6SMatthias Ringwald  * @brief Report user authorization grant.
169c8c46d51SMatthias Ringwald  * @param con_handle
1703deb3ec6SMatthias Ringwald  */
171711e6c80SMatthias Ringwald void sm_authorization_grant(hci_con_handle_t con_handle);
1723deb3ec6SMatthias Ringwald 
1734dfd504aSMatthias Ringwald /**
174*d1a1f6a4SMatthias Ringwald  * @brief Support for signed writes, used by att_server.
175*d1a1f6a4SMatthias Ringwald  * @returns ready
1764dfd504aSMatthias Ringwald  */
1774dfd504aSMatthias Ringwald int sm_cmac_ready(void);
1784dfd504aSMatthias Ringwald 
1793deb3ec6SMatthias Ringwald /**
1803deb3ec6SMatthias Ringwald  * @brief Support for signed writes, used by att_server.
1819fa6f18cSMatthias Ringwald  * @note Message is in little endian to allows passing in ATT PDU without flipping.
182514d35fcSMatthias Ringwald  * @note signing data: [opcode, attribute_handle, message, sign_counter]
183514d35fcSMatthias Ringwald  * @note calculated hash in done_callback is big endian and has 16 byte.
184514d35fcSMatthias Ringwald  * @param key
185514d35fcSMatthias Ringwald  * @param opcde
186514d35fcSMatthias Ringwald  * @param attribute_handle
187514d35fcSMatthias Ringwald  * @param message_len
188514d35fcSMatthias Ringwald  * @param message
189514d35fcSMatthias Ringwald  * @param sign_counter
1903deb3ec6SMatthias Ringwald  */
1914dfd504aSMatthias 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));
1923deb3ec6SMatthias Ringwald 
1933deb3ec6SMatthias Ringwald /*
1943deb3ec6SMatthias Ringwald  * @brief Match address against bonded devices
1953deb3ec6SMatthias Ringwald  * @return 0 if successfully added to lookup queue
1963deb3ec6SMatthias Ringwald  * @note Triggers SM_IDENTITY_RESOLVING_* events
1973deb3ec6SMatthias Ringwald  */
1983deb3ec6SMatthias Ringwald int sm_address_resolution_lookup(uint8_t addr_type, bd_addr_t addr);
1993deb3ec6SMatthias Ringwald 
2003deb3ec6SMatthias Ringwald /**
2013deb3ec6SMatthias Ringwald  * @brief Identify device in LE Device DB.
2023deb3ec6SMatthias Ringwald  * @param handle
2033deb3ec6SMatthias Ringwald  * @return index from le_device_db or -1 if not found/identified
2043deb3ec6SMatthias Ringwald  */
205711e6c80SMatthias Ringwald int sm_le_device_index(hci_con_handle_t con_handle );
206df86eb96SMatthias Ringwald 
207df86eb96SMatthias Ringwald /**
2084b8c611fSMatthias Ringwald  * @brief Use fixec passkey for Legacy and SC instead of generating a random number
209caf15bf3SMatthias Ringwald  * @note Can be used to improve security over Just Works if no keyboard or displary are present and
210caf15bf3SMatthias Ringwald  *       individual random passkey can be printed on the device during production
211caf15bf3SMatthias Ringwald  * @param passkey
212caf15bf3SMatthias Ringwald  */
2134b8c611fSMatthias Ringwald void sm_use_fixed_passkey_in_display_role(uint32_t passkey);
214caf15bf3SMatthias Ringwald 
2156c39055aSMatthias Ringwald /**
2166c39055aSMatthias Ringwald  * @brief Allow connection re-encryption in Peripheral (Responder) role for LE Legacy Pairing
2176c39055aSMatthias Ringwald  *       without entry for Central device stored in LE Device DB
2186c39055aSMatthias Ringwald  * @note BTstack in Peripheral Role (Responder) supports LE Legacy Pairing without a persistent LE Device DB as
2196c39055aSMatthias Ringwald  *       the LTK is reconstructed from a local secret IRK and EDIV + Random stored on Central (Initiator) device
2206c39055aSMatthias Ringwald  *       On the downside, it's not really possible to delete a pairing if this is enabled.
2216c39055aSMatthias Ringwald  * @param allow encryption using reconstructed LTK without stored entry (Default: 1)
2226c39055aSMatthias Ringwald  */
2236c39055aSMatthias Ringwald void sm_allow_ltk_reconstruction_without_le_device_db_entry(int allow);
2246c39055aSMatthias Ringwald 
225c59d0c92SMatthias Ringwald /**
226c59d0c92SMatthias Ringwald  * @brief Generate OOB data for LE Secure Connections
227c59d0c92SMatthias Ringwald  * @note This generates a 128 bit random number ra and then calculates Ca = f4(PKa, PKa, ra, 0)
2284acf7b7bSMatthias Ringwald  *       New OOB data should be generated for each pairing. Ra is used for subsequent OOB pairings
229c59d0c92SMatthias Ringwald  * @param callback
230c59d0c92SMatthias Ringwald  * @returns status
231c59d0c92SMatthias Ringwald  */
232c59d0c92SMatthias Ringwald uint8_t sm_generate_sc_oob_data(void (*callback)(const uint8_t * confirm_value, const uint8_t * random_value));
233c59d0c92SMatthias Ringwald 
234a680ba6bSMatthias Ringwald /**
235a680ba6bSMatthias Ringwald  *
236a680ba6bSMatthias Ringwald  * @brief Registers OOB Data Callback for LE Secure Conections. The callback should set all arguments and return 1 if OOB data is availble
237a680ba6bSMatthias Ringwald  * @note the oob_sc_local_random usually is the random_value returend by sm_generate_sc_oob_data
238a680ba6bSMatthias Ringwald  * @param get_oob_data_callback
239a680ba6bSMatthias Ringwald  */
2404acf7b7bSMatthias 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));
241a680ba6bSMatthias Ringwald 
2423deb3ec6SMatthias Ringwald /* API_END */
2433deb3ec6SMatthias Ringwald 
2448974fcd6SMatthias Ringwald // PTS testing
2458974fcd6SMatthias Ringwald void sm_test_set_irk(sm_key_t irk);
2468974fcd6SMatthias Ringwald void sm_test_use_fixed_local_csrk(void);
2478974fcd6SMatthias Ringwald 
248192365feSMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
249192365feSMatthias Ringwald void sm_test_set_pairing_failure(int reason);
250192365feSMatthias Ringwald #endif
251192365feSMatthias Ringwald 
2523deb3ec6SMatthias Ringwald #if defined __cplusplus
2533deb3ec6SMatthias Ringwald }
2543deb3ec6SMatthias Ringwald #endif
2553deb3ec6SMatthias Ringwald 
2563deb3ec6SMatthias Ringwald #endif // __SM_H
257