Microchip® Advanced Software Framework

sm.c File Reference
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "ble/le_device_db.h"
#include "ble/core.h"
#include "ble/sm.h"
#include "bluetooth_company_id.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_linked_list.h"
#include "btstack_memory.h"
#include "gap.h"
#include "hci.h"
#include "hci_dump.h"
#include "l2cap.h"

Data Structures

struct  sm_setup_context
 

Macros

#define __BTSTACK_FILE__   "sm.c"
 
#define IS_RESPONDER(role)   (1 || role)
 

Typedefs

typedef struct sm_setup_context sm_setup_context_t
 

Enumerations

enum  address_resolution_event_t {
  ADDRESS_RESOLUTION_SUCEEDED,
  ADDRESS_RESOLUTION_FAILED
}
 
enum  address_resolution_mode_t {
  ADDRESS_RESOLUTION_IDLE,
  ADDRESS_RESOLUTION_GENERAL,
  ADDRESS_RESOLUTION_FOR_CONNECTION
}
 
enum  cmac_state_t {
  CMAC_IDLE,
  CMAC_CALC_SUBKEYS,
  CMAC_W4_SUBKEYS,
  CMAC_CALC_MI,
  CMAC_W4_MI,
  CMAC_CALC_MLAST,
  CMAC_W4_MLAST
}
 
enum  derived_key_generation_t {
  DKG_W4_WORKING,
  DKG_CALC_IRK,
  DKG_W4_IRK,
  DKG_CALC_DHK,
  DKG_W4_DHK,
  DKG_READY
}
 
enum  ec_key_generation_state_t {
  EC_KEY_GENERATION_IDLE,
  EC_KEY_GENERATION_ACTIVE,
  EC_KEY_GENERATION_W4_KEY,
  EC_KEY_GENERATION_DONE
}
 
enum  random_address_update_t {
  RAU_W4_WORKING,
  RAU_IDLE,
  RAU_GET_RANDOM,
  RAU_W4_RANDOM,
  RAU_GET_ENC,
  RAU_W4_ENC,
  RAU_SET_ADDRESS
}
 
enum  sm_aes128_state_t {
  SM_AES128_IDLE,
  SM_AES128_ACTIVE
}
 
enum  sm_state_var_t {
  SM_STATE_VAR_DHKEY_NEEDED = 1 << 0,
  SM_STATE_VAR_DHKEY_CALCULATED = 1 << 1,
  SM_STATE_VAR_DHKEY_COMMAND_RECEIVED = 1 << 2
}
 
enum  sm_user_response_t {
  SM_USER_RESPONSE_IDLE,
  SM_USER_RESPONSE_PENDING,
  SM_USER_RESPONSE_CONFIRM,
  SM_USER_RESPONSE_PASSKEY,
  SM_USER_RESPONSE_DECLINE
}
 
enum  stk_generation_method_t {
  JUST_WORKS,
  PK_RESP_INPUT,
  PK_INIT_INPUT,
  OK_BOTH_INPUT,
  NK_BOTH_INPUT,
  OOB
}
 

Functions

static void dkg_next_state (void)
 
gap_random_address_type_t gap_random_address_get_mode (void)
 Get privacy mode. More...
 
void gap_random_address_set (bd_addr_t addr)
 Sets a fixed random address for advertising. More...
 
void gap_random_address_set_mode (gap_random_address_type_t random_address_type)
 Enable privacy by using random addresses. More...
 
void gap_random_address_set_update_period (int period_ms)
 Sets update period for random address. More...
 
static void gap_random_address_trigger (void)
 
static int gap_random_address_type_requires_updates (void)
 
static void gap_random_address_update_handler (btstack_timer_source_t *timer)
 
static void gap_random_address_update_start (void)
 
static void gap_random_address_update_stop (void)
 
static void log_info_hex16 (const char *name, uint16_t value)
 
static uint8_t own_address_type (void)
 
static void rau_next_state (void)
 
void sm_add_event_handler (btstack_packet_callback_registration_t *callback_handler)
 Add event packet handler. More...
 
static void sm_address_resolution_handle_event (address_resolution_event_t event)
 
static int sm_address_resolution_idle (void)
 
int sm_address_resolution_lookup (uint8_t address_type, bd_addr_t address)
 
static void sm_address_resolution_start_lookup (uint8_t addr_type, hci_con_handle_t con_handle, bd_addr_t addr, address_resolution_mode_t mode, void *context)
 
static void sm_aes128_start (sm_key_t key, sm_key_t plaintext, void *context)
 
static void sm_ah_r_prime (uint8_t r[3], uint8_t *r_prime)
 
void sm_allow_ltk_reconstruction_without_le_device_db_entry (int allow)
 Allow connection re-encryption in Peripheral (Responder) role for LE Legacy Pairing without entry for Central device stored in LE Device DB. More...
 
int sm_authenticated (hci_con_handle_t con_handle)
 Get authentication property. More...
 
void sm_authorization_decline (hci_con_handle_t con_handle)
 Report user authorization decline. More...
 
void sm_authorization_grant (hci_con_handle_t con_handle)
 Report user authorization grant. More...
 
authorization_state_t sm_authorization_state (hci_con_handle_t con_handle)
 Queries authorization state. More...
 
void sm_bonding_decline (hci_con_handle_t con_handle)
 Decline bonding triggered by event before. More...
 
static void sm_c1_t1 (sm_key_t r, uint8_t preq[7], uint8_t pres[7], uint8_t iat, uint8_t rat, uint8_t *t1)
 
static void sm_c1_t3 (sm_key_t t2, bd_addr_t ia, bd_addr_t ra, uint8_t *t3)
 
static int sm_calc_actual_encryption_key_size (int other)
 
static void sm_d1_d_prime (uint16_t d, uint16_t r, uint8_t *d1_prime)
 
static void sm_dispatch_event (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
 
static void sm_dm_r_prime (uint8_t r[8], uint8_t *r_prime)
 
static void sm_done_for_handle (hci_con_handle_t con_handle)
 
int sm_encryption_key_size (hci_con_handle_t con_handle)
 Get encryption key size. More...
 
static void sm_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)
 
static sm_connection_t * sm_get_connection_for_handle (hci_con_handle_t con_handle)
 
static void sm_handle_encryption_result (uint8_t *data)
 
static void sm_handle_random_result (uint8_t *data)
 
void sm_init (void)
 Initializes the Security Manager, connects to L2CAP. More...
 
static void sm_init_setup (sm_connection_t *sm_conn)
 
static int sm_is_null (uint8_t *data, int size)
 
static int sm_is_null_key (uint8_t *key)
 
static int sm_is_null_random (uint8_t random[8])
 
void sm_just_works_confirm (hci_con_handle_t con_handle)
 Confirm Just Works bonding. More...
 
static int sm_key_distribution_all_received (sm_connection_t *sm_conn)
 
static int sm_key_distribution_flags_for_auth_req (void)
 
static int sm_key_distribution_flags_for_set (uint8_t key_set)
 
static void sm_key_distribution_handle_all_received (sm_connection_t *sm_conn)
 
void sm_keypress_notification (hci_con_handle_t con_handle, uint8_t action)
 Send keypress notification for keyboard only devices. More...
 
int sm_le_device_index (hci_con_handle_t con_handle)
 Identify device in LE Device DB. More...
 
static void sm_next_responding_state (sm_connection_t *sm_conn)
 
static void sm_notify_client_authorization (uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address, uint8_t result)
 
static void sm_notify_client_base (uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address)
 
static void sm_notify_client_index (uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address, uint16_t index)
 
static void sm_notify_client_passkey (uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address, uint32_t passkey)
 
void sm_numeric_comparison_confirm (hci_con_handle_t con_handle)
 Confirm value from SM_EVENT_NUMERIC_COMPARISON_REQUEST for Numeric Comparison bonding. More...
 
static void sm_pairing_error (sm_connection_t *sm_conn, uint8_t reason)
 
void sm_passkey_input (hci_con_handle_t con_handle, uint32_t passkey)
 Reports passkey input by user. More...
 
static void sm_pdu_handler (uint8_t packet_type, hci_con_handle_t con_handle, uint8_t *packet, uint16_t size)
 
static void sm_pdu_received_in_wrong_state (sm_connection_t *sm_conn)
 
static void sm_random_start (void *context)
 
void sm_register_oob_data_callback (int(*get_oob_data_callback)(uint8_t addres_type, bd_addr_t addr, uint8_t *oob_data))
 Registers OOB Data Callback. More...
 
void sm_request_pairing (hci_con_handle_t con_handle)
 Used by att_server.c to request user authorization. More...
 
static void sm_reset_setup (void)
 
static void sm_reset_tk (void)
 
static void sm_run (void)
 
static void sm_s1_r_prime (sm_key_t r1, sm_key_t r2, uint8_t *r_prime)
 
void sm_send_security_request (hci_con_handle_t con_handle)
 Trigger Security Request. More...
 
static void sm_send_security_request_for_connection (sm_connection_t *sm_conn)
 
void sm_set_accepted_stk_generation_methods (uint8_t accepted_stk_generation_methods)
 Limit the STK generation methods. More...
 
void sm_set_authentication_requirements (uint8_t auth_req)
 Sets the requested authentication requirements, bonding yes/no, MITM yes/no, SC yes/no, keypress yes/no. More...
 
void sm_set_encryption_key_size_range (uint8_t min_size, uint8_t max_size)
 Set the accepted encryption key size range. More...
 
void sm_set_er (sm_key_t er)
 Set secret ER key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2. More...
 
void sm_set_io_capabilities (io_capability_t io_capability)
 Sets the available IO Capabilities. More...
 
void sm_set_ir (sm_key_t ir)
 Set secret IR key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2. More...
 
static void sm_setup_event_base (uint8_t *event, int event_size, uint8_t type, hci_con_handle_t con_handle, uint8_t addr_type, bd_addr_t address)
 
static void sm_setup_key_distribution (uint8_t key_set)
 
static void sm_setup_tk (void)
 
static int sm_stk_generation_init (sm_connection_t *sm_conn)
 
void sm_test_set_irk (sm_key_t irk)
 
void sm_test_use_fixed_ec_keypair (void)
 
void sm_test_use_fixed_local_csrk (void)
 
static void sm_timeout_handler (btstack_timer_source_t *timer)
 
static void sm_timeout_reset (sm_connection_t *sm_conn)
 
static void sm_timeout_start (sm_connection_t *sm_conn)
 
static void sm_timeout_stop (void)
 
static void sm_trigger_user_response (sm_connection_t *sm_conn)
 
static void sm_truncate_key (sm_key_t key, int max_encryption_size)
 
void sm_use_fixed_ec_keypair (uint8_t *qx, uint8_t *qy, uint8_t *d)
 Set Elliptic Key Public/Private Keypair. More...
 
void sm_use_fixed_legacy_pairing_passkey_in_display_role (uint32_t passkey)
 Set passkey used with LE Legacy Pairing when we generate and show it instead of random number. More...
 
static int sm_validate_stk_generation_method (void)
 

Variables

static derived_key_generation_t dkg_state
 
static btstack_timer_source_t gap_random_address_update_timer
 
static gap_random_address_type_t gap_random_adress_type
 
static uint32_t gap_random_adress_update_period
 
static
btstack_packet_callback_registration_t 
hci_event_callback_registration
 
static random_address_update_t rau_state
 
static sm_setup_context_tsetup = &the_setup
 
static uint8_t sm_accepted_stk_generation_methods
 
static uint16_t sm_active_connection_handle = HCI_CON_HANDLE_INVALID
 
static uint8_t sm_address_resolution_addr_type
 
static bd_addr_t sm_address_resolution_address
 
static int sm_address_resolution_ah_calculation_active
 
static void * sm_address_resolution_context
 
static btstack_linked_list_t sm_address_resolution_general_queue
 
static address_resolution_mode_t sm_address_resolution_mode
 
static int sm_address_resolution_test
 
static void * sm_aes128_context
 
static sm_aes128_state_t sm_aes128_state
 
static uint8_t sm_auth_req = 0
 
static btstack_linked_list_t sm_event_handlers
 
static uint32_t sm_fixed_legacy_pairing_passkey_in_display_role
 
static int(* sm_get_oob_data )(uint8_t addres_type, bd_addr_t addr, uint8_t *oob_data) = NULL
 
static uint8_t sm_io_capabilities = IO_CAPABILITY_NO_INPUT_NO_OUTPUT
 
static uint8_t sm_max_encryption_key_size
 
static uint8_t sm_min_encryption_key_size
 
static const uint8_t sm_pdu_size []
 
static sm_key_t sm_persistent_dhk
 
static sm_key_t sm_persistent_er
 
static sm_key_t sm_persistent_ir
 
static sm_key_t sm_persistent_irk
 
static uint8_t sm_persistent_irk_ready = 0
 
static bd_addr_t sm_random_address
 
static void * sm_random_context
 
static uint8_t sm_reconstruct_ltk_without_le_device_db_entry
 
static uint8_t sm_slave_request_security
 
static const
stk_generation_method_t 
stk_generation_method [5][5]
 
static uint8_t test_use_fixed_local_csrk
 
static sm_setup_context_t the_setup
 

#define __BTSTACK_FILE__   "sm.c"

Enumerator
ADDRESS_RESOLUTION_SUCEEDED 
ADDRESS_RESOLUTION_FAILED 
Enumerator
ADDRESS_RESOLUTION_IDLE 
ADDRESS_RESOLUTION_GENERAL 
ADDRESS_RESOLUTION_FOR_CONNECTION 
Enumerator
CMAC_IDLE 
CMAC_CALC_SUBKEYS 
CMAC_W4_SUBKEYS 
CMAC_CALC_MI 
CMAC_W4_MI 
CMAC_CALC_MLAST 
CMAC_W4_MLAST 
Enumerator
DKG_W4_WORKING 
DKG_CALC_IRK 
DKG_W4_IRK 
DKG_CALC_DHK 
DKG_W4_DHK 
DKG_READY 
Enumerator
EC_KEY_GENERATION_IDLE 
EC_KEY_GENERATION_ACTIVE 
EC_KEY_GENERATION_W4_KEY 
EC_KEY_GENERATION_DONE 
Enumerator
RAU_W4_WORKING 
RAU_IDLE 
RAU_GET_RANDOM 
RAU_W4_RANDOM 
RAU_GET_ENC 
RAU_W4_ENC 
RAU_SET_ADDRESS 
Enumerator
SM_AES128_IDLE 
SM_AES128_ACTIVE 
Enumerator
SM_STATE_VAR_DHKEY_NEEDED 
SM_STATE_VAR_DHKEY_CALCULATED 
SM_STATE_VAR_DHKEY_COMMAND_RECEIVED 
Enumerator
SM_USER_RESPONSE_IDLE 
SM_USER_RESPONSE_PENDING 
SM_USER_RESPONSE_CONFIRM 
SM_USER_RESPONSE_PASSKEY 
SM_USER_RESPONSE_DECLINE 
Enumerator
JUST_WORKS 
PK_RESP_INPUT 
PK_INIT_INPUT 
OK_BOTH_INPUT 
NK_BOTH_INPUT 
OOB 

static void dkg_next_state ( void  )
inlinestatic

References dkg_state.

Referenced by sm_handle_encryption_result(), and sm_run().

gap_random_address_type_t gap_random_address_get_mode ( void  )

Get privacy mode.

References gap_random_adress_type.

Referenced by sm_run().

void gap_random_address_set ( bd_addr_t  addr)

Sets a fixed random address for advertising.

Parameters
addr
Note
Sets random address mode to type off

References gap_random_address_set_mode(), GAP_RANDOM_ADDRESS_TYPE_STATIC, RAU_SET_ADDRESS, rau_state, RAU_W4_WORKING, sm_random_address, and sm_run().

void gap_random_address_set_mode ( gap_random_address_type_t  random_address_type)
void gap_random_address_set_update_period ( int  period_ms)
static void gap_random_address_trigger ( void  )
static
static int gap_random_address_type_requires_updates ( void  )
static
static void gap_random_address_update_stop ( void  )
static
static void log_info_hex16 ( const char *  name,
uint16_t  value 
)
static
static uint8_t own_address_type ( void  )
static
static void rau_next_state ( void  )
inlinestatic

References rau_state.

Referenced by sm_handle_encryption_result(), and sm_run().

void sm_add_event_handler ( btstack_packet_callback_registration_t callback_handler)

Add event packet handler.

References btstack_linked_list_add_tail().

Referenced by att_server_init().

static int sm_address_resolution_idle ( void  )
static
static void sm_address_resolution_start_lookup ( uint8_t  addr_type,
hci_con_handle_t  con_handle,
bd_addr_t  addr,
address_resolution_mode_t  mode,
void *  context 
)
static
static void sm_ah_r_prime ( uint8_t  r[3],
uint8_t *  r_prime 
)
static

Referenced by sm_run().

void sm_allow_ltk_reconstruction_without_le_device_db_entry ( int  allow)

Allow connection re-encryption in Peripheral (Responder) role for LE Legacy Pairing without entry for Central device stored in LE Device DB.

Note
BTstack in Peripheral Role (Responder) supports LE Legacy Pairing without a persistent LE Device DB as the LTK is reconstructed from a local secret IRK and EDIV + Random stored on Central (Initiator) device On the downside, it's not really possible to delete a pairing if this is enabled.
Parameters
allowencryption using reconstructed LTK without stored entry (Default: 1)

References sm_reconstruct_ltk_without_le_device_db_entry.

int sm_authenticated ( hci_con_handle_t  con_handle)

Get authentication property.

Parameters
con_handle
Returns
1 if bonded with OOB/Passkey (AND MITM protection)

References sm_get_connection_for_handle().

Referenced by att_event_packet_handler().

void sm_authorization_decline ( hci_con_handle_t  con_handle)

Report user authorization decline.

Parameters
con_handle

References SM_EVENT_AUTHORIZATION_RESULT, sm_get_connection_for_handle(), and sm_notify_client_authorization().

void sm_authorization_grant ( hci_con_handle_t  con_handle)

Report user authorization grant.

Parameters
con_handle

References SM_EVENT_AUTHORIZATION_RESULT, sm_get_connection_for_handle(), and sm_notify_client_authorization().

authorization_state_t sm_authorization_state ( hci_con_handle_t  con_handle)

Queries authorization state.

Parameters
con_handle
Returns
authorization_state for the current session

References sm_get_connection_for_handle().

Referenced by att_server_process_validated_request().

static void sm_c1_t1 ( sm_key_t  r,
uint8_t  preq[7],
uint8_t  pres[7],
uint8_t  iat,
uint8_t  rat,
uint8_t *  t1 
)
static

References log_info_key(), and reverse_56().

Referenced by sm_run().

static void sm_c1_t3 ( sm_key_t  t2,
bd_addr_t  ia,
bd_addr_t  ra,
uint8_t *  t3 
)
static

References log_info_key().

Referenced by sm_handle_encryption_result().

static int sm_calc_actual_encryption_key_size ( int  other)
inlinestatic
static void sm_d1_d_prime ( uint16_t  d,
uint16_t  r,
uint8_t *  d1_prime 
)
static

References big_endian_store_16().

Referenced by sm_run().

static void sm_dm_r_prime ( uint8_t  r[8],
uint8_t *  r_prime 
)
static

Referenced by sm_run().

int sm_encryption_key_size ( hci_con_handle_t  con_handle)

Get encryption key size.

Parameters
con_handle
Returns
0 if not encrypted, 7-16 otherwise

References sm_get_connection_for_handle().

Referenced by att_event_packet_handler().

static void sm_event_packet_handler ( uint8_t  packet_type,
uint16_t  channel,
uint8_t *  packet,
uint16_t  size 
)
static

References BTSTACK_EVENT_STATE, btstack_event_state_get_state(), con_handle, DKG_CALC_DHK, DKG_CALC_IRK, dkg_state, EC_KEY_GENERATION_ACTIVE, EC_KEY_GENERATION_DONE, ERROR_CODE_AUTHENTICATION_FAILURE, GAP_RANDOM_ADDRESS_TYPE_OFF, GAP_RANDOM_ADDRESS_TYPE_STATIC, gap_random_adress_type, HCI_EVENT_COMMAND_COMPLETE, HCI_EVENT_DISCONNECTION_COMPLETE, HCI_EVENT_ENCRYPTION_CHANGE, HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE, HCI_EVENT_IS_COMMAND_COMPLETE, HCI_EVENT_LE_META, HCI_EVENT_PACKET, hci_event_packet_get_type(), hci_le_encrypt, hci_le_rand, hci_read_bd_addr, hci_read_local_supported_commands, HCI_STATE_WORKING, HCI_SUBEVENT_LE_CONNECTION_COMPLETE, HCI_SUBEVENT_LE_GENERATE_DHKEY_COMPLETE, hci_subevent_le_generate_dhkey_complete_get_dhkey(), hci_subevent_le_generate_dhkey_complete_get_status(), HCI_SUBEVENT_LE_LONG_TERM_KEY_REQUEST, HCI_SUBEVENT_LE_READ_LOCAL_P256_PUBLIC_KEY_COMPLETE, hci_subevent_le_read_local_p256_public_key_complete_get_dhkey_x(), hci_subevent_le_read_local_p256_public_key_complete_get_dhkey_y(), hci_subevent_le_read_local_p256_public_key_complete_get_status(), IS_RESPONDER, le_device_db_remove(), le_device_db_set_local_bd_addr(), little_endian_read_16(), log_error, log_info, log_info_hexdump(), OFFSET_OF_DATA_IN_COMMAND_COMPLETE, RAU_GET_RANDOM, RAU_IDLE, RAU_SET_ADDRESS, rau_state, reverse_64(), reverse_bd_addr(), sm_active_connection_handle, sm_done_for_handle(), sm_get_connection_for_handle(), sm_handle_encryption_result(), sm_handle_random_result(), sm_is_null_random(), sm_key_distribution_all_received(), sm_key_distribution_handle_all_received(), sm_pdu_received_in_wrong_state(), sm_persistent_irk_ready, sm_reconstruct_ltk_without_le_device_db_entry, sm_run(), sm_slave_request_security, SM_STATE_VAR_DHKEY_CALCULATED, sm_setup_context::sm_state_vars, sm_setup_context::sm_use_secure_connections, and UNUSED.

Referenced by sm_init().

static int sm_is_null ( uint8_t *  data,
int  size 
)
static
static int sm_is_null_key ( uint8_t *  key)
static
static int sm_is_null_random ( uint8_t  random[8])
static

References sm_is_null().

Referenced by sm_event_packet_handler(), and sm_run().

void sm_just_works_confirm ( hci_con_handle_t  con_handle)
static int sm_key_distribution_flags_for_auth_req ( void  )
static
void sm_keypress_notification ( hci_con_handle_t  con_handle,
uint8_t  action 
)

Send keypress notification for keyboard only devices.

Parameters
con_handle
actionsee SM_KEYPRESS_* in bluetooth.h

References sm_get_connection_for_handle(), sm_setup_context::sm_keypress_notification, SM_KEYPRESS_PASSKEY_ENTRY_COMPLETED, and sm_run().

int sm_le_device_index ( hci_con_handle_t  con_handle)

Identify device in LE Device DB.

Parameters
handle
Returns
index from le_device_db or -1 if not found/identified

References sm_get_connection_for_handle().

static void sm_next_responding_state ( sm_connection_t *  sm_conn)
inlinestatic
static void sm_notify_client_authorization ( uint8_t  type,
hci_con_handle_t  con_handle,
uint8_t  addr_type,
bd_addr_t  address,
uint8_t  result 
)
static
static void sm_notify_client_base ( uint8_t  type,
hci_con_handle_t  con_handle,
uint8_t  addr_type,
bd_addr_t  address 
)
static
static void sm_notify_client_index ( uint8_t  type,
hci_con_handle_t  con_handle,
uint8_t  addr_type,
bd_addr_t  address,
uint16_t  index 
)
static
static void sm_notify_client_passkey ( uint8_t  type,
hci_con_handle_t  con_handle,
uint8_t  addr_type,
bd_addr_t  address,
uint32_t  passkey 
)
static
void sm_numeric_comparison_confirm ( hci_con_handle_t  con_handle)

Confirm value from SM_EVENT_NUMERIC_COMPARISON_REQUEST for Numeric Comparison bonding.

Parameters
con_handle

References sm_just_works_confirm().

static void sm_pairing_error ( sm_connection_t *  sm_conn,
uint8_t  reason 
)
static
void sm_passkey_input ( hci_con_handle_t  con_handle,
uint32_t  passkey 
)

Reports passkey input by user.

Parameters
con_handle
passkeyin [0..999999]

References big_endian_store_32(), sm_get_connection_for_handle(), sm_reset_tk(), sm_run(), sm_setup_context::sm_tk, sm_setup_context::sm_user_response, and SM_USER_RESPONSE_PASSKEY.

static void sm_pdu_handler ( uint8_t  packet_type,
hci_con_handle_t  con_handle,
uint8_t *  packet,
uint16_t  size 
)
static

References HCI_EVENT_PACKET, IS_RESPONDER, JUST_WORKS, L2CAP_EVENT_CAN_SEND_NOW, le_device_db_encryption_get(), little_endian_read_16(), little_endian_store_16(), log_debug, log_error, log_info, NK_BOTH_INPUT, NULL, OK_BOTH_INPUT, OOB, PK_INIT_INPUT, PK_RESP_INPUT, reverse_128(), reverse_256(), reverse_64(), reverse_bd_addr(), SM_CODE_ENCRYPTION_INFORMATION, SM_CODE_IDENTITY_ADDRESS_INFORMATION, SM_CODE_IDENTITY_INFORMATION, SM_CODE_KEYPRESS_NOTIFICATION, SM_CODE_MASTER_IDENTIFICATION, SM_CODE_PAIRING_CONFIRM, SM_CODE_PAIRING_DHKEY_CHECK, SM_CODE_PAIRING_FAILED, SM_CODE_PAIRING_PUBLIC_KEY, SM_CODE_PAIRING_RANDOM, SM_CODE_PAIRING_REQUEST, SM_CODE_PAIRING_RESPONSE, SM_CODE_SECURITY_REQUEST, SM_CODE_SIGNING_INFORMATION, SM_DATA_PACKET, sm_dispatch_event(), sm_done_for_handle(), SM_EVENT_KEYPRESS_NOTIFICATION, SM_EVENT_PASSKEY_DISPLAY_CANCEL, sm_get_connection_for_handle(), sm_is_null_key(), sm_key_distribution_all_received(), sm_key_distribution_handle_all_received(), sm_setup_context::sm_key_distribution_received_set, SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION, SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION, SM_KEYDIST_FLAG_IDENTITY_INFORMATION, SM_KEYDIST_FLAG_MASTER_IDENTIFICATION, SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION, sm_notify_client_base(), sm_setup_context::sm_pairing_failed_reason, sm_pdu_received_in_wrong_state(), sm_setup_context::sm_peer_addr_type, sm_setup_context::sm_peer_address, sm_setup_context::sm_peer_confirm, sm_setup_context::sm_peer_csrk, sm_setup_context::sm_peer_ediv, sm_setup_context::sm_peer_irk, sm_setup_context::sm_peer_ltk, sm_setup_context::sm_peer_rand, sm_setup_context::sm_peer_random, SM_REASON_PASSKEYT_ENTRY_FAILED, sm_run(), sm_setup_context::sm_s_pres, SM_STATE_VAR_DHKEY_COMMAND_RECEIVED, SM_STATE_VAR_DHKEY_NEEDED, sm_setup_context::sm_state_vars, sm_stk_generation_init(), sm_setup_context::sm_stk_generation_method, sm_trigger_user_response(), sm_setup_context::sm_use_secure_connections, sm_setup_context::sm_user_response, SM_USER_RESPONSE_DECLINE, SM_USER_RESPONSE_IDLE, SM_USER_RESPONSE_PASSKEY, SM_USER_RESPONSE_PENDING, and UNUSED.

Referenced by sm_init().

static void sm_pdu_received_in_wrong_state ( sm_connection_t *  sm_conn)
inlinestatic
static void sm_random_start ( void *  context)
static

References hci_le_rand, hci_send_cmd(), and sm_random_context.

Referenced by sm_run().

void sm_register_oob_data_callback ( int(*)(uint8_t addres_type, bd_addr_t addr, uint8_t *oob_data)  get_oob_data_callback)

Registers OOB Data Callback.

The callback should set the oob_data and return 1 if OOB data is availble

Parameters
get_oob_data_callback

References sm_get_oob_data.

void sm_request_pairing ( hci_con_handle_t  con_handle)
static void sm_reset_setup ( void  )
static
static void sm_reset_tk ( void  )
static
static void sm_run ( void  )
static

References sm_lookup_entry_t::address, ADDRESS_RESOLUTION_FAILED, ADDRESS_RESOLUTION_FOR_CONNECTION, ADDRESS_RESOLUTION_GENERAL, ADDRESS_RESOLUTION_SUCEEDED, sm_lookup_entry_t::address_type, bd_addr_to_str(), big_endian_read_32(), btstack_linked_list_empty(), btstack_linked_list_iterator_has_next(), btstack_linked_list_iterator_next(), btstack_linked_list_remove(), buffer, CMAC_CALC_MI, CMAC_CALC_MLAST, CMAC_CALC_SUBKEYS, DKG_CALC_DHK, DKG_CALC_IRK, dkg_next_state(), dkg_state, EC_KEY_GENERATION_ACTIVE, EC_KEY_GENERATION_W4_KEY, gap_le_get_own_address(), gap_local_bd_addr(), gap_random_address_get_mode(), GAP_RANDOM_ADDRESS_NON_RESOLVABLE, GAP_RANDOM_ADDRESS_RESOLVABLE, GAP_RANDOM_ADDRESS_TYPE_OFF, GAP_RANDOM_ADDRESS_TYPE_STATIC, hci_can_send_command_packet_now(), HCI_CON_HANDLE_INVALID, hci_connections_get_iterator(), hci_get_state(), hci_le_generate_dhkey, hci_le_long_term_key_negative_reply, hci_le_long_term_key_request_reply, hci_le_read_local_p256_public_key, hci_le_set_random_address, hci_le_start_encryption, hci_send_cmd(), HCI_STATE_WORKING, IS_RESPONDER, JUST_WORKS, l2cap_can_send_fixed_channel_packet_now(), L2CAP_CID_SECURITY_MANAGER_PROTOCOL, l2cap_request_can_send_fix_channel_now_event(), l2cap_send_connectionless(), le_device_db_count(), le_device_db_info(), little_endian_store_16(), log_info, log_info_key(), NK_BOTH_INPUT, NULL, OK_BOTH_INPUT, OOB, PK_INIT_INPUT, PK_RESP_INPUT, RAU_GET_ENC, RAU_GET_RANDOM, RAU_IDLE, rau_next_state(), RAU_SET_ADDRESS, rau_state, reverse_128(), reverse_256(), reverse_64(), reverse_bd_addr(), sm_active_connection_handle, sm_address_resolution_addr_type, sm_address_resolution_address, sm_address_resolution_ah_calculation_active, sm_address_resolution_context, sm_address_resolution_handle_event(), sm_address_resolution_idle(), sm_address_resolution_start_lookup(), sm_address_resolution_test, SM_AES128_ACTIVE, SM_AES128_IDLE, sm_aes128_start(), sm_aes128_state, sm_ah_r_prime(), SM_AUTHREQ_BONDING, sm_c1_t1(), sm_setup_context::sm_c1_t3_value, sm_cmac_ready(), SM_CODE_ENCRYPTION_INFORMATION, SM_CODE_IDENTITY_ADDRESS_INFORMATION, SM_CODE_IDENTITY_INFORMATION, SM_CODE_KEYPRESS_NOTIFICATION, SM_CODE_MASTER_IDENTIFICATION, SM_CODE_PAIRING_CONFIRM, SM_CODE_PAIRING_DHKEY_CHECK, SM_CODE_PAIRING_FAILED, SM_CODE_PAIRING_PUBLIC_KEY, SM_CODE_PAIRING_RANDOM, SM_CODE_PAIRING_REQUEST, SM_CODE_PAIRING_RESPONSE, SM_CODE_SECURITY_REQUEST, SM_CODE_SIGNING_INFORMATION, sm_d1_d_prime(), sm_dm_r_prime(), sm_done_for_handle(), sm_get_connection_for_handle(), sm_init_setup(), sm_is_null_key(), sm_is_null_random(), sm_key_distribution_all_received(), sm_key_distribution_flags_for_auth_req(), sm_key_distribution_handle_all_received(), sm_setup_context::sm_key_distribution_send_set, SM_KEYDIST_ENC_KEY, SM_KEYDIST_FLAG_ENCRYPTION_INFORMATION, SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION, SM_KEYDIST_FLAG_IDENTITY_INFORMATION, SM_KEYDIST_FLAG_MASTER_IDENTIFICATION, SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION, sm_setup_context::sm_keypress_notification, sm_setup_context::sm_local_confirm, sm_setup_context::sm_local_csrk, sm_setup_context::sm_local_div, sm_setup_context::sm_local_ediv, sm_setup_context::sm_local_rand, sm_setup_context::sm_local_random, sm_setup_context::sm_ltk, sm_setup_context::sm_m_addr_type, sm_setup_context::sm_m_preq, sm_next_responding_state(), sm_setup_context::sm_pairing_failed_reason, sm_pairing_packet_get_initiator_key_distribution(), sm_pairing_packet_get_responder_key_distribution(), sm_pairing_packet_set_code(), sm_pairing_packet_set_initiator_key_distribution(), sm_pairing_packet_set_responder_key_distribution(), sm_setup_context::sm_peer_ediv, sm_setup_context::sm_peer_ltk, sm_setup_context::sm_peer_rand, sm_setup_context::sm_peer_random, sm_persistent_dhk, sm_persistent_er, sm_persistent_ir, sm_persistent_irk, sm_random_address, sm_random_start(), sm_reset_setup(), sm_s1_r_prime(), sm_setup_context::sm_s_addr_type, sm_setup_context::sm_s_pres, sm_setup_key_distribution(), SM_STATE_VAR_DHKEY_NEEDED, sm_setup_context::sm_state_vars, sm_stk_generation_init(), sm_setup_context::sm_stk_generation_method, sm_timeout_reset(), sm_timeout_start(), sm_setup_context::sm_tk, sm_trigger_user_response(), sm_setup_context::sm_use_secure_connections, test_use_fixed_local_csrk, and UNUSED.

Referenced by gap_random_address_set(), gap_random_address_trigger(), sm_address_resolution_lookup(), sm_bonding_decline(), sm_event_packet_handler(), sm_just_works_confirm(), sm_keypress_notification(), sm_passkey_input(), sm_pdu_handler(), sm_request_pairing(), sm_send_security_request_for_connection(), and sm_timeout_handler().

static void sm_s1_r_prime ( sm_key_t  r1,
sm_key_t  r2,
uint8_t *  r_prime 
)
static

References log_info_key().

Referenced by sm_run().

void sm_send_security_request ( hci_con_handle_t  con_handle)

Trigger Security Request.

Note
Not used normally. Bonding is triggered by access to protected attributes in ATT Server

References sm_get_connection_for_handle(), and sm_send_security_request_for_connection().

Referenced by handle_hci_event().

static void sm_send_security_request_for_connection ( sm_connection_t *  sm_conn)
static

References sm_run().

Referenced by sm_request_pairing(), and sm_send_security_request().

void sm_set_accepted_stk_generation_methods ( uint8_t  accepted_stk_generation_methods)

Limit the STK generation methods.

Bonding is stopped if the resulting one isn't in the list

Parameters
ORcombination of SM_STK_GENERATION_METHOD_

References sm_accepted_stk_generation_methods.

void sm_set_authentication_requirements ( uint8_t  auth_req)

Sets the requested authentication requirements, bonding yes/no, MITM yes/no, SC yes/no, keypress yes/no.

Parameters
ORcombination of SM_AUTHREQ_ flags

References log_error, sm_auth_req, and SM_AUTHREQ_SECURE_CONNECTION.

void sm_set_encryption_key_size_range ( uint8_t  min_size,
uint8_t  max_size 
)

Set the accepted encryption key size range.

Bonding is stopped if the result isn't within the range

Parameters
min_size(default 7)
max_size(default 16)

References sm_max_encryption_key_size, and sm_min_encryption_key_size.

void sm_set_er ( sm_key_t  er)

Set secret ER key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2.

Parameters
er

References sm_persistent_er.

Referenced by sm_init().

void sm_set_io_capabilities ( io_capability_t  io_capability)

Sets the available IO Capabilities.

Parameters
IO_CAPABILITY_

References sm_io_capabilities.

void sm_set_ir ( sm_key_t  ir)

Set secret IR key for key generation as described in Core V4.0, Vol 3, Part G, 5.2.2.

References sm_persistent_ir.

Referenced by sm_init().

static void sm_setup_event_base ( uint8_t *  event,
int  event_size,
uint8_t  type,
hci_con_handle_t  con_handle,
uint8_t  addr_type,
bd_addr_t  address 
)
static
static void sm_setup_key_distribution ( uint8_t  key_set)
static
void sm_test_set_irk ( sm_key_t  irk)
void sm_test_use_fixed_ec_keypair ( void  )
void sm_test_use_fixed_local_csrk ( void  )
static void sm_timeout_handler ( btstack_timer_source_t timer)
static
static void sm_timeout_reset ( sm_connection_t *  sm_conn)
static

References sm_timeout_start(), and sm_timeout_stop().

Referenced by sm_run().

static void sm_timeout_stop ( void  )
static
static void sm_truncate_key ( sm_key_t  key,
int  max_encryption_size 
)
static
void sm_use_fixed_ec_keypair ( uint8_t *  qx,
uint8_t *  qy,
uint8_t *  d 
)

Set Elliptic Key Public/Private Keypair.

Note
Using the same key for more than one device is not recommended.
Parameters
qx32 bytes
qy32 bytes
d32 bytes

References EC_KEY_GENERATION_DONE, and UNUSED.

void sm_use_fixed_legacy_pairing_passkey_in_display_role ( uint32_t  passkey)

Set passkey used with LE Legacy Pairing when we generate and show it instead of random number.

Note
Can be used to improve security over Just Works if no keyboard or displary are present and individual random passkey can be printed on the device during production
Parameters
passkey

References sm_fixed_legacy_pairing_passkey_in_display_role.

btstack_timer_source_t gap_random_address_update_timer
static
btstack_packet_callback_registration_t hci_event_callback_registration
static
sm_setup_context_t* setup = &the_setup
static

Referenced by uhd_ctrl_phase_setup().

uint8_t sm_accepted_stk_generation_methods
static
uint16_t sm_active_connection_handle = HCI_CON_HANDLE_INVALID
static
uint8_t sm_address_resolution_addr_type
static
int sm_address_resolution_ah_calculation_active
static
void* sm_address_resolution_context
static
btstack_linked_list_t sm_address_resolution_general_queue
static
void* sm_aes128_context
static
btstack_linked_list_t sm_event_handlers
static
uint32_t sm_fixed_legacy_pairing_passkey_in_display_role
static
int(* sm_get_oob_data)(uint8_t addres_type, bd_addr_t addr, uint8_t *oob_data) = NULL
static
uint8_t sm_io_capabilities = IO_CAPABILITY_NO_INPUT_NO_OUTPUT
static
uint8_t sm_max_encryption_key_size
static
uint8_t sm_min_encryption_key_size
static
const uint8_t sm_pdu_size[]
static
Initial value:
= {
0,
7,
7,
17,
17,
2,
17,
11,
17,
8,
17,
2,
65,
17,
2,
}
sm_key_t sm_persistent_dhk
static
sm_key_t sm_persistent_er
static

Referenced by sm_run(), and sm_set_er().

sm_key_t sm_persistent_ir
static

Referenced by sm_run(), and sm_set_ir().

sm_key_t sm_persistent_irk
static
uint8_t sm_persistent_irk_ready = 0
static
void* sm_random_context
static
uint8_t sm_reconstruct_ltk_without_le_device_db_entry
static
uint8_t sm_slave_request_security
static

Referenced by sm_event_packet_handler().

const stk_generation_method_t stk_generation_method[5][5]
static
Initial value:
= {
}
Definition: sm.c:146
Definition: sm.c:145
Definition: sm.c:147
Definition: sm.c:148

Referenced by sm_setup_tk().

uint8_t test_use_fixed_local_csrk
static
sm_setup_context_t the_setup
static