1 /******************************************************************************
3 * Copyright (C) 1999-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains functions for BLE device control utilities, and LE
24 ******************************************************************************/
25 #include "bt_target.h"
27 #if BLE_INCLUDED == TRUE
35 #include "btm_ble_api.h"
41 #include "vendor_ble.h"
43 #if SMP_INCLUDED == TRUE
44 extern BOOLEAN AES_CMAC ( BT_OCTET16 key, UINT8 *input, UINT16 length, UINT16 tlen, UINT8 *p_signature);
45 extern void smp_link_encrypted(BD_ADDR bda, UINT8 encr_enable);
46 extern BOOLEAN smp_proc_ltk_request(BD_ADDR bda);
48 extern void gatt_notify_enc_cmpl(BD_ADDR bd_addr);
49 /*******************************************************************************/
50 /* External Function to be called by other modules */
51 /*******************************************************************************/
52 /********************************************************
54 ** Function BTM_SecAddBleDevice
56 ** Description Add/modify device. This function will be normally called
57 ** during host startup to restore all required information
58 ** for a LE device stored in the NVRAM.
60 ** Parameters: bd_addr - BD address of the peer
61 ** bd_name - Name of the peer device. NULL if unknown.
62 ** dev_type - Remote device's device type.
63 ** addr_type - LE device address type.
65 ** Returns TRUE if added OK, else FALSE
67 *******************************************************************************/
68 BOOLEAN BTM_SecAddBleDevice (BD_ADDR bd_addr, BD_NAME bd_name, tBT_DEVICE_TYPE dev_type,
69 tBLE_ADDR_TYPE addr_type)
71 tBTM_SEC_DEV_REC *p_dev_rec;
73 tBTM_INQ_INFO *p_info=NULL;
75 BTM_TRACE_DEBUG ("BTM_SecAddBleDevice dev_type=0x%x", dev_type);
76 p_dev_rec = btm_find_dev (bd_addr);
80 BTM_TRACE_DEBUG("Add a new device");
82 /* There is no device record, allocate one.
83 * If we can not find an empty spot for this one, let it fail. */
84 for (i = 0; i < BTM_SEC_MAX_DEVICE_RECORDS; i++)
86 if (!(btm_cb.sec_dev_rec[i].sec_flags & BTM_SEC_IN_USE))
88 BTM_TRACE_DEBUG ("allocate a new dev rec idx=0x%x ", i );
89 p_dev_rec = &btm_cb.sec_dev_rec[i];
91 /* Mark this record as in use and initialize */
92 memset (p_dev_rec, 0, sizeof (tBTM_SEC_DEV_REC));
93 p_dev_rec->sec_flags = BTM_SEC_IN_USE;
94 memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
95 p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
96 p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE);
98 /* update conn params, use default value for background connection params */
99 p_dev_rec->conn_params.min_conn_int =
100 p_dev_rec->conn_params.max_conn_int =
101 p_dev_rec->conn_params.supervision_tout =
102 p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_PARAM_UNDEF;
104 BTM_TRACE_DEBUG ("hci_handl=0x%x ", p_dev_rec->ble_hci_handle );
114 BTM_TRACE_DEBUG("Device already exist");
117 memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
119 if (bd_name && bd_name[0])
121 p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
122 BCM_STRNCPY_S ((char *)p_dev_rec->sec_bd_name, sizeof (p_dev_rec->sec_bd_name),
123 (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN);
125 p_dev_rec->device_type = dev_type;
126 p_dev_rec->ble.ble_addr_type = addr_type;
127 BTM_TRACE_DEBUG ("p_dev_rec->device_type =0x%x addr_type=0x%x sec_flags=0x%x",
128 dev_type, addr_type, p_dev_rec->sec_flags);
130 /* sync up with the Inq Data base*/
131 p_info = BTM_InqDbRead(bd_addr);
134 p_info->results.ble_addr_type = p_dev_rec->ble.ble_addr_type ;
135 p_info->results.device_type = p_dev_rec->device_type;
136 BTM_TRACE_DEBUG ("InqDb device_type =0x%x addr_type=0x%x",
137 p_info->results.device_type, p_info->results.ble_addr_type);
143 /*******************************************************************************
145 ** Function BTM_SecAddBleKey
147 ** Description Add/modify LE device information. This function will be
148 ** normally called during host startup to restore all required
149 ** information stored in the NVRAM.
151 ** Parameters: bd_addr - BD address of the peer
152 ** p_le_key - LE key values.
153 ** key_type - LE SMP key type.
155 ** Returns TRUE if added OK, else FALSE
157 *******************************************************************************/
158 BOOLEAN BTM_SecAddBleKey (BD_ADDR bd_addr, tBTM_LE_KEY_VALUE *p_le_key, tBTM_LE_KEY_TYPE key_type)
160 #if SMP_INCLUDED == TRUE
161 tBTM_SEC_DEV_REC *p_dev_rec;
162 BTM_TRACE_DEBUG ("BTM_SecAddBleKey");
163 p_dev_rec = btm_find_dev (bd_addr);
164 if (!p_dev_rec || !p_le_key ||
165 (key_type != BTM_LE_KEY_PENC && key_type != BTM_LE_KEY_PID &&
166 key_type != BTM_LE_KEY_PCSRK && key_type != BTM_LE_KEY_LENC &&
167 key_type != BTM_LE_KEY_LCSRK))
169 BTM_TRACE_WARNING ("BTM_SecAddBleKey() Wrong Type, or No Device record \
170 for bdaddr: %08x%04x, Type: %d",
171 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
172 (bd_addr[4]<<8)+bd_addr[5], key_type);
176 BTM_TRACE_DEBUG ("BTM_SecAddLeKey() BDA: %08x%04x, Type: 0x%02x",
177 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
178 (bd_addr[4]<<8)+bd_addr[5], key_type);
180 if (key_type == BTM_LE_KEY_PENC || key_type == BTM_LE_KEY_PID ||
181 key_type == BTM_LE_KEY_PCSRK || key_type == BTM_LE_KEY_LENC ||
182 key_type == BTM_LE_KEY_LCSRK)
184 btm_sec_save_le_key (bd_addr, key_type, p_le_key, FALSE);
192 /*******************************************************************************
194 ** Function BTM_BleLoadLocalKeys
196 ** Description Local local identity key, encryption root or sign counter.
198 ** Parameters: key_type: type of key, can be BTM_BLE_KEY_TYPE_ID, BTM_BLE_KEY_TYPE_ER
199 ** or BTM_BLE_KEY_TYPE_COUNTER.
200 ** p_key: pointer to the key.
204 *******************************************************************************/
205 void BTM_BleLoadLocalKeys(UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key)
207 tBTM_DEVCB *p_devcb = &btm_cb.devcb;
208 BTM_TRACE_DEBUG ("BTM_BleLoadLocalKeys");
213 case BTM_BLE_KEY_TYPE_ID:
214 memcpy(&p_devcb->id_keys, &p_key->id_keys, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
217 case BTM_BLE_KEY_TYPE_ER:
218 memcpy(p_devcb->er, p_key->er, sizeof(BT_OCTET16));
222 BTM_TRACE_ERROR("unknow local key type: %d", key_type);
228 /*******************************************************************************
230 ** Function BTM_GetDeviceEncRoot
232 ** Description This function is called to read the local device encryption
236 ** the local device ER is copied into er
238 *******************************************************************************/
239 void BTM_GetDeviceEncRoot (BT_OCTET16 er)
241 BTM_TRACE_DEBUG ("BTM_GetDeviceEncRoot");
243 memcpy (er, btm_cb.devcb.er, BT_OCTET16_LEN);
246 /*******************************************************************************
248 ** Function BTM_GetDeviceIDRoot
250 ** Description This function is called to read the local device identity
254 ** the local device IR is copied into irk
256 *******************************************************************************/
257 void BTM_GetDeviceIDRoot (BT_OCTET16 irk)
259 BTM_TRACE_DEBUG ("BTM_GetDeviceIDRoot ");
261 memcpy (irk, btm_cb.devcb.id_keys.irk, BT_OCTET16_LEN);
264 /*******************************************************************************
266 ** Function BTM_GetDeviceDHK
268 ** Description This function is called to read the local device DHK.
271 ** the local device DHK is copied into dhk
273 *******************************************************************************/
274 void BTM_GetDeviceDHK (BT_OCTET16 dhk)
276 BTM_TRACE_DEBUG ("BTM_GetDeviceDHK");
277 memcpy (dhk, btm_cb.devcb.id_keys.dhk, BT_OCTET16_LEN);
280 /*******************************************************************************
282 ** Function BTM_ReadConnectionAddr
284 ** Description This function is called to get the local device address information
289 *******************************************************************************/
290 void BTM_ReadConnectionAddr (BD_ADDR remote_bda, BD_ADDR local_conn_addr, tBLE_ADDR_TYPE *p_addr_type)
292 tACL_CONN *p_acl = btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
296 BTM_TRACE_ERROR("No connection exist!");
299 memcpy(local_conn_addr, p_acl->conn_addr, BD_ADDR_LEN);
300 * p_addr_type = p_acl->conn_addr_type;
302 BTM_TRACE_DEBUG ("BTM_ReadConnectionAddr address type: %d addr: 0x%02x",
303 p_acl->conn_addr_type, p_acl->conn_addr[0]);
306 /*******************************************************************************
308 ** Function BTM_IsBleConnection
310 ** Description This function is called to check if the connection handle
313 ** Returns TRUE if connection is LE link, otherwise FALSE.
315 *******************************************************************************/
316 BOOLEAN BTM_IsBleConnection (UINT16 conn_handle)
318 #if (BLE_INCLUDED == TRUE)
322 BTM_TRACE_API ("BTM_IsBleConnection: conn_handle: %d", conn_handle);
324 xx = btm_handle_to_acl_index (conn_handle);
325 if (xx >= MAX_L2CAP_LINKS)
328 p = &btm_cb.acl_db[xx];
330 return (p->transport == BT_TRANSPORT_LE);
336 /*******************************************************************************
338 ** Function BTM_ReadRemoteConnectionAddr
340 ** Description This function is read the remote device address currently used
342 ** Parameters pseudo_addr: pseudo random address available
343 ** conn_addr:connection address used
344 ** p_addr_type : BD Address type, Public or Random of the address used
346 ** Returns BOOLEAN , TRUE if connection to remote device exists, else FALSE
348 *******************************************************************************/
349 BOOLEAN BTM_ReadRemoteConnectionAddr(BD_ADDR pseudo_addr, BD_ADDR conn_addr,
350 tBLE_ADDR_TYPE *p_addr_type)
353 #if (BLE_PRIVACY_SPT == TRUE)
354 tACL_CONN *p = btm_bda_to_acl (pseudo_addr, BT_TRANSPORT_LE);
358 BTM_TRACE_ERROR("BTM_ReadRemoteConnectionAddr can not find connection"
359 " with matching address");
363 memcpy(conn_addr, p->active_remote_addr, BD_ADDR_LEN);
364 *p_addr_type = p->active_remote_addr_type;
366 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(pseudo_addr);
368 memcpy(conn_addr, pseudo_addr, BD_ADDR_LEN);
369 if (p_dev_rec != NULL)
371 *p_addr_type = p_dev_rec->ble.ble_addr_type;
377 /*******************************************************************************
379 ** Function BTM_SecurityGrant
381 ** Description This function is called to grant security process.
383 ** Parameters bd_addr - peer device bd address.
384 ** res - result of the operation BTM_SUCCESS if success.
385 ** Otherwise, BTM_REPEATED_ATTEMPTS is too many attempts.
389 *******************************************************************************/
390 void BTM_SecurityGrant(BD_ADDR bd_addr, UINT8 res)
392 #if SMP_INCLUDED == TRUE
393 tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_REPEATED_ATTEMPTS;
394 BTM_TRACE_DEBUG ("BTM_SecurityGrant");
395 SMP_SecurityGrant(bd_addr, res_smp);
399 /*******************************************************************************
401 ** Function BTM_BlePasskeyReply
403 ** Description This function is called after Security Manager submitted
404 ** passkey request to the application.
406 ** Parameters: bd_addr - Address of the device for which passkey was requested
407 ** res - result of the operation BTM_SUCCESS if success
408 ** key_len - length in bytes of the Passkey
409 ** p_passkey - pointer to array with the passkey
410 ** trusted_mask - bitwise OR of trusted services (array of UINT32)
412 *******************************************************************************/
413 void BTM_BlePasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey)
415 #if SMP_INCLUDED == TRUE
416 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
417 tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_PASSKEY_ENTRY_FAIL;
419 if (p_dev_rec == NULL)
421 BTM_TRACE_ERROR("Passkey reply to Unknown device");
425 p_dev_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
426 BTM_TRACE_DEBUG ("BTM_BlePasskeyReply");
427 SMP_PasskeyReply(bd_addr, res_smp, passkey);
431 /*******************************************************************************
433 ** Function BTM_BleOobDataReply
435 ** Description This function is called to provide the OOB data for
436 ** SMP in response to BTM_LE_OOB_REQ_EVT
438 ** Parameters: bd_addr - Address of the peer device
439 ** res - result of the operation SMP_SUCCESS if success
440 ** p_data - simple pairing Randomizer C.
442 *******************************************************************************/
443 void BTM_BleOobDataReply(BD_ADDR bd_addr, UINT8 res, UINT8 len, UINT8 *p_data)
445 #if SMP_INCLUDED == TRUE
446 tSMP_STATUS res_smp = (res == BTM_SUCCESS) ? SMP_SUCCESS : SMP_OOB_FAIL;
447 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
449 BTM_TRACE_DEBUG ("BTM_BleOobDataReply");
451 if (p_dev_rec == NULL)
453 BTM_TRACE_ERROR("BTM_BleOobDataReply() to Unknown device");
457 p_dev_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
458 SMP_OobDataReply(bd_addr, res_smp, len, p_data);
462 /******************************************************************************
464 ** Function BTM_BleSetConnScanParams
466 ** Description Set scan parameter used in BLE connection request
468 ** Parameters: scan_interval: scan interval
469 ** scan_window: scan window
473 *******************************************************************************/
474 void BTM_BleSetConnScanParams (UINT16 scan_interval, UINT16 scan_window)
476 #if SMP_INCLUDED == TRUE
477 tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
478 BOOLEAN new_param = FALSE;
480 if (BTM_BLE_VALID_PRAM(scan_interval, BTM_BLE_SCAN_INT_MIN, BTM_BLE_SCAN_INT_MAX) &&
481 BTM_BLE_VALID_PRAM(scan_window, BTM_BLE_SCAN_WIN_MIN, BTM_BLE_SCAN_WIN_MAX))
483 btu_stop_timer(&p_ble_cb->scan_param_idle_timer);
485 if (p_ble_cb->scan_int != scan_interval)
487 p_ble_cb->scan_int = scan_interval;
491 if (p_ble_cb->scan_win != scan_window)
493 p_ble_cb->scan_win = scan_window;
497 if (new_param && p_ble_cb->conn_state == BLE_BG_CONN)
499 btm_ble_suspend_bg_conn();
504 BTM_TRACE_ERROR("Illegal Connection Scan Parameters");
509 /********************************************************
511 ** Function BTM_BleSetPrefConnParams
513 ** Description Set a peripheral's preferred connection parameters
515 ** Parameters: bd_addr - BD address of the peripheral
516 ** scan_interval: scan interval
517 ** scan_window: scan window
518 ** min_conn_int - minimum preferred connection interval
519 ** max_conn_int - maximum preferred connection interval
520 ** slave_latency - preferred slave latency
521 ** supervision_tout - preferred supervision timeout
525 *******************************************************************************/
526 void BTM_BleSetPrefConnParams (BD_ADDR bd_addr,
527 UINT16 min_conn_int, UINT16 max_conn_int,
528 UINT16 slave_latency, UINT16 supervision_tout)
530 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
532 BTM_TRACE_API ("BTM_BleSetPrefConnParams min: %u max: %u latency: %u \
534 min_conn_int, max_conn_int, slave_latency, supervision_tout);
536 if (BTM_BLE_VALID_PRAM(min_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
537 BTM_BLE_VALID_PRAM(max_conn_int, BTM_BLE_CONN_INT_MIN, BTM_BLE_CONN_INT_MAX) &&
538 BTM_BLE_VALID_PRAM(supervision_tout, BTM_BLE_CONN_SUP_TOUT_MIN, BTM_BLE_CONN_SUP_TOUT_MAX) &&
539 (slave_latency <= BTM_BLE_CONN_LATENCY_MAX || slave_latency == BTM_BLE_CONN_PARAM_UNDEF))
543 /* expect conn int and stout and slave latency to be updated all together */
544 if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF || max_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
546 if (min_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
547 p_dev_rec->conn_params.min_conn_int = min_conn_int;
549 p_dev_rec->conn_params.min_conn_int = max_conn_int;
551 if (max_conn_int != BTM_BLE_CONN_PARAM_UNDEF)
552 p_dev_rec->conn_params.max_conn_int = max_conn_int;
554 p_dev_rec->conn_params.max_conn_int = min_conn_int;
556 if (slave_latency != BTM_BLE_CONN_PARAM_UNDEF)
557 p_dev_rec->conn_params.slave_latency = slave_latency;
559 p_dev_rec->conn_params.slave_latency = BTM_BLE_CONN_SLAVE_LATENCY_DEF;
561 if (supervision_tout != BTM_BLE_CONN_PARAM_UNDEF)
562 p_dev_rec->conn_params.supervision_tout = supervision_tout;
564 p_dev_rec->conn_params.supervision_tout = BTM_BLE_CONN_TIMEOUT_DEF;
571 BTM_TRACE_ERROR("Unknown Device, setting rejected");
576 BTM_TRACE_ERROR("Illegal Connection Parameters");
580 /*******************************************************************************
582 ** Function BTM_ReadDevInfo
584 ** Description This function is called to read the device/address type
587 ** Parameter remote_bda: remote device address
588 ** p_dev_type: output parameter to read the device type.
589 ** p_addr_type: output parameter to read the address type.
591 *******************************************************************************/
592 void BTM_ReadDevInfo (BD_ADDR remote_bda, tBT_DEVICE_TYPE *p_dev_type, tBLE_ADDR_TYPE *p_addr_type)
594 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (remote_bda);
595 tBTM_INQ_INFO *p_inq_info = BTM_InqDbRead(remote_bda);
597 *p_dev_type = BT_DEVICE_TYPE_BREDR;
598 *p_addr_type = BLE_ADDR_PUBLIC;
602 /* Check with the BT manager if details about remote device are known */
603 if (p_inq_info != NULL)
605 *p_dev_type = p_inq_info->results.device_type ;
606 *p_addr_type = p_inq_info->results.ble_addr_type;
608 /* unknown device, assume BR/EDR */
609 BTM_TRACE_DEBUG ("btm_find_dev_type - unknown device, BR/EDR assumed");
612 else /* there is a security device record exisitng */
614 /* new inquiry result, overwrite device type in security device record */
617 p_dev_rec->device_type = p_inq_info->results.device_type;
618 p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
620 *p_dev_type = p_dev_rec->device_type;
621 *p_addr_type = p_dev_rec->ble.ble_addr_type;
625 BTM_TRACE_DEBUG ("btm_find_dev_type - device_type = %d addr_type = %d", *p_dev_type , *p_addr_type);
628 /*******************************************************************************
630 ** Function BTM_BleReceiverTest
632 ** Description This function is called to start the LE Receiver test
634 ** Parameter rx_freq - Frequency Range
635 ** p_cmd_cmpl_cback - Command Complete callback
637 *******************************************************************************/
638 void BTM_BleReceiverTest(UINT8 rx_freq, tBTM_CMPL_CB *p_cmd_cmpl_cback)
640 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
642 if (btsnd_hcic_ble_receiver_test(rx_freq) == FALSE)
644 BTM_TRACE_ERROR("%s: Unable to Trigger LE receiver test", __FUNCTION__);
648 /*******************************************************************************
650 ** Function BTM_BleTransmitterTest
652 ** Description This function is called to start the LE Transmitter test
654 ** Parameter tx_freq - Frequency Range
655 ** test_data_len - Length in bytes of payload data in each packet
656 ** packet_payload - Pattern to use in the payload
657 ** p_cmd_cmpl_cback - Command Complete callback
659 *******************************************************************************/
660 void BTM_BleTransmitterTest(UINT8 tx_freq, UINT8 test_data_len,
661 UINT8 packet_payload, tBTM_CMPL_CB *p_cmd_cmpl_cback)
663 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
664 if (btsnd_hcic_ble_transmitter_test(tx_freq, test_data_len, packet_payload) == FALSE)
666 BTM_TRACE_ERROR("%s: Unable to Trigger LE transmitter test", __FUNCTION__);
670 /*******************************************************************************
672 ** Function BTM_BleTestEnd
674 ** Description This function is called to stop the in-progress TX or RX test
676 ** Parameter p_cmd_cmpl_cback - Command complete callback
678 *******************************************************************************/
679 void BTM_BleTestEnd(tBTM_CMPL_CB *p_cmd_cmpl_cback)
681 btm_cb.devcb.p_le_test_cmd_cmpl_cb = p_cmd_cmpl_cback;
683 if (btsnd_hcic_ble_test_end() == FALSE)
685 BTM_TRACE_ERROR("%s: Unable to End the LE TX/RX test", __FUNCTION__);
689 /*******************************************************************************
690 ** Internal Functions
691 *******************************************************************************/
692 void btm_ble_test_command_complete(UINT8 *p)
694 tBTM_CMPL_CB *p_cb = btm_cb.devcb.p_le_test_cmd_cmpl_cb;
697 btm_cb.devcb.p_le_test_cmd_cmpl_cb = NULL;
705 /*******************************************************************************
707 ** Function BTM_UseLeLink
709 ** Description This function is to select the underneath physical link to use.
711 ** Returns TRUE to use LE, FALSE use BR/EDR.
713 *******************************************************************************/
714 BOOLEAN BTM_UseLeLink (BD_ADDR bd_addr)
717 tBT_DEVICE_TYPE dev_type;
718 tBLE_ADDR_TYPE addr_type;
719 BOOLEAN use_le = FALSE;
721 if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR)) != NULL)
725 else if ((p = btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE)) != NULL)
731 BTM_ReadDevInfo(bd_addr, &dev_type, &addr_type);
732 use_le = (dev_type == BT_DEVICE_TYPE_BLE);
736 /*******************************************************************************
738 ** Function btm_ble_rand_enc_complete
740 ** Description This function is the callback functions for HCI_Rand command
741 ** and HCI_Encrypt command is completed.
742 ** This message is received from the HCI.
746 *******************************************************************************/
747 void btm_ble_rand_enc_complete (UINT8 *p, UINT16 op_code, tBTM_RAND_ENC_CB *p_enc_cplt_cback)
749 tBTM_RAND_ENC params;
750 UINT8 *p_dest = params.param_buf;
752 BTM_TRACE_DEBUG ("btm_ble_rand_enc_complete");
754 memset(¶ms, 0, sizeof(tBTM_RAND_ENC));
756 /* If there was a callback address for vcs complete, call it */
757 if (p_enc_cplt_cback && p)
759 /* Pass paramters to the callback function */
760 STREAM_TO_UINT8(params.status, p); /* command status */
762 if (params.status == HCI_SUCCESS)
764 params.opcode = op_code;
766 if (op_code == HCI_BLE_RAND)
767 params.param_len = BT_OCTET8_LEN;
769 params.param_len = BT_OCTET16_LEN;
771 memcpy(p_dest, p, params.param_len); /* Fetch return info from HCI event message */
773 if (p_enc_cplt_cback)
774 (*p_enc_cplt_cback)(¶ms); /* Call the Encryption complete callback function */
779 #if (SMP_INCLUDED == TRUE)
781 /*******************************************************************************
783 ** Function btm_ble_get_enc_key_type
785 ** Description This function is to increment local sign counter
788 *******************************************************************************/
789 void btm_ble_increment_sign_ctr(BD_ADDR bd_addr, BOOLEAN is_local )
791 tBTM_SEC_DEV_REC *p_dev_rec;
793 BTM_TRACE_DEBUG ("btm_ble_increment_sign_ctr is_local=%d", is_local);
795 if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL)
798 p_dev_rec->ble.keys.local_counter++;
800 p_dev_rec->ble.keys.counter++;
801 BTM_TRACE_DEBUG ("is_local=%d local sign counter=%d peer sign counter=%d",
803 p_dev_rec->ble.keys.local_counter,
804 p_dev_rec->ble.keys.counter);
808 /*******************************************************************************
810 ** Function btm_ble_get_enc_key_type
812 ** Description This function is to get the BLE key type that has been exchanged
813 ** in betweem local device and peer device.
815 ** Returns p_key_type: output parameter to carry the key type value.
817 *******************************************************************************/
818 BOOLEAN btm_ble_get_enc_key_type(BD_ADDR bd_addr, UINT8 *p_key_types)
820 tBTM_SEC_DEV_REC *p_dev_rec;
822 BTM_TRACE_DEBUG ("btm_ble_get_enc_key_type");
824 if ((p_dev_rec = btm_find_dev (bd_addr)) != NULL)
826 *p_key_types = p_dev_rec->ble.key_type;
832 /*******************************************************************************
834 ** Function btm_get_local_div
836 ** Description This function is called to read the local DIV
838 ** Returns TURE - if a valid DIV is availavle
839 *******************************************************************************/
840 BOOLEAN btm_get_local_div (BD_ADDR bd_addr, UINT16 *p_div)
842 tBTM_SEC_DEV_REC *p_dev_rec;
843 BOOLEAN status = FALSE;
844 BTM_TRACE_DEBUG ("btm_get_local_div");
846 BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
847 bd_addr[0],bd_addr[1],
848 bd_addr[2],bd_addr[3],
849 bd_addr[4],bd_addr[5]);
852 p_dev_rec = btm_find_dev (bd_addr);
854 if (p_dev_rec && p_dev_rec->ble.keys.div)
857 *p_div = p_dev_rec->ble.keys.div;
859 BTM_TRACE_DEBUG ("btm_get_local_div status=%d (1-OK) DIV=0x%x", status, *p_div);
863 /*******************************************************************************
865 ** Function btm_sec_save_le_key
867 ** Description This function is called by the SMP to update
868 ** an BLE key. SMP is internal, whereas all the keys shall
869 ** be sent to the application. The function is also called
870 ** when application passes ble key stored in NVRAM to the btm_sec.
871 ** pass_to_application parameter is false in this case.
875 *******************************************************************************/
876 void btm_sec_save_le_key(BD_ADDR bd_addr, tBTM_LE_KEY_TYPE key_type, tBTM_LE_KEY_VALUE *p_keys,
877 BOOLEAN pass_to_application)
879 tBTM_SEC_DEV_REC *p_rec;
880 tBTM_LE_EVT_DATA cb_data;
883 BTM_TRACE_DEBUG ("btm_sec_save_le_key key_type=0x%x pass_to_application=%d",key_type, pass_to_application);
884 /* Store the updated key in the device database */
886 BTM_TRACE_DEBUG("bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
887 bd_addr[0],bd_addr[1],
888 bd_addr[2],bd_addr[3],
889 bd_addr[4],bd_addr[5]);
891 if ((p_rec = btm_find_dev (bd_addr)) != NULL && p_keys)
895 case BTM_LE_KEY_PENC:
896 memcpy(p_rec->ble.keys.ltk, p_keys->penc_key.ltk, BT_OCTET16_LEN);
897 memcpy(p_rec->ble.keys.rand, p_keys->penc_key.rand, BT_OCTET8_LEN);
898 p_rec->ble.keys.sec_level = p_keys->penc_key.sec_level;
899 p_rec->ble.keys.ediv = p_keys->penc_key.ediv;
900 p_rec->ble.keys.key_size = p_keys->penc_key.key_size;
901 p_rec->ble.key_type |= BTM_LE_KEY_PENC;
902 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
903 if (p_keys->penc_key.sec_level == SMP_SEC_AUTHENTICATED)
904 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
906 p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
907 BTM_TRACE_DEBUG("BTM_LE_KEY_PENC key_type=0x%x sec_flags=0x%x sec_leve=0x%x",
910 p_rec->ble.keys.sec_level);
914 for (i=0; i<BT_OCTET16_LEN; i++)
916 p_rec->ble.keys.irk[i] = p_keys->pid_key.irk[i];
919 //memcpy( p_rec->ble.keys.irk, p_keys->pid_key, BT_OCTET16_LEN); todo will crash the system
920 memcpy(p_rec->ble.static_addr, p_keys->pid_key.static_addr, BD_ADDR_LEN);
921 p_rec->ble.static_addr_type = p_keys->pid_key.addr_type;
922 p_rec->ble.key_type |= BTM_LE_KEY_PID;
923 BTM_TRACE_DEBUG("BTM_LE_KEY_PID key_type=0x%x save peer IRK", p_rec->ble.key_type);
926 case BTM_LE_KEY_PCSRK:
927 memcpy(p_rec->ble.keys.csrk, p_keys->pcsrk_key.csrk, BT_OCTET16_LEN);
928 p_rec->ble.keys.srk_sec_level = p_keys->pcsrk_key.sec_level;
929 p_rec->ble.keys.counter = p_keys->pcsrk_key.counter;
930 p_rec->ble.key_type |= BTM_LE_KEY_PCSRK;
931 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_KNOWN;
932 if ( p_keys->pcsrk_key.sec_level== SMP_SEC_AUTHENTICATED)
933 p_rec->sec_flags |= BTM_SEC_LE_LINK_KEY_AUTHED;
935 p_rec->sec_flags &= ~BTM_SEC_LE_LINK_KEY_AUTHED;
937 BTM_TRACE_DEBUG("BTM_LE_KEY_PCSRK key_type=0x%x sec_flags=0x%x sec_level=0x%x peer_counter=%d",
940 p_rec->ble.keys.srk_sec_level,
941 p_rec->ble.keys.counter );
944 case BTM_LE_KEY_LENC:
945 p_rec->ble.keys.div = p_keys->lenc_key.div; /* update DIV */
946 p_rec->ble.keys.sec_level = p_keys->lenc_key.sec_level;
947 p_rec->ble.keys.key_size = p_keys->lenc_key.key_size;
948 p_rec->ble.key_type |= BTM_LE_KEY_LENC;
950 BTM_TRACE_DEBUG("BTM_LE_KEY_LENC key_type=0x%x DIV=0x%x key_size=0x%x sec_level=0x%x",
953 p_rec->ble.keys.key_size,
954 p_rec->ble.keys.sec_level );
957 case BTM_LE_KEY_LCSRK:/* local CSRK has been delivered */
958 p_rec->ble.keys.div = p_keys->lcsrk_key.div; /* update DIV */
959 p_rec->ble.keys.local_csrk_sec_level = p_keys->lcsrk_key.sec_level;
960 p_rec->ble.keys.local_counter = p_keys->lcsrk_key.counter;
961 p_rec->ble.key_type |= BTM_LE_KEY_LCSRK;
962 BTM_TRACE_DEBUG("BTM_LE_KEY_LCSRK key_type=0x%x DIV=0x%x scrk_sec_level=0x%x local_counter=%d",
965 p_rec->ble.keys.local_csrk_sec_level,
966 p_rec->ble.keys.local_counter );
970 BTM_TRACE_WARNING("btm_sec_save_le_key (Bad key_type 0x%02x)", key_type);
974 BTM_TRACE_DEBUG ("BLE key type 0x%02x updated for BDA: %08x%04x (btm_sec_save_le_key)", key_type,
975 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
976 (bd_addr[4]<<8)+bd_addr[5]);
978 /* Notify the application that one of the BLE keys has been updated
979 If link key is in progress, it will get sent later.*/
980 if (pass_to_application && btm_cb.api.p_le_callback)
982 cb_data.key.p_key_value = p_keys;
983 cb_data.key.key_type = key_type;
984 (*btm_cb.api.p_le_callback) (BTM_LE_KEY_EVT, bd_addr, &cb_data);
989 BTM_TRACE_WARNING ("BLE key type 0x%02x called for Unknown BDA or type: %08x%04x !! (btm_sec_save_le_key)", key_type,
990 (bd_addr[0]<<24)+(bd_addr[1]<<16)+(bd_addr[2]<<8)+bd_addr[3],
991 (bd_addr[4]<<8)+bd_addr[5]);
995 BTM_TRACE_DEBUG ("sec_flags=0x%x", p_rec->sec_flags);
999 /*******************************************************************************
1001 ** Function btm_ble_update_sec_key_size
1003 ** Description update the current lin kencryption key size
1007 *******************************************************************************/
1008 void btm_ble_update_sec_key_size(BD_ADDR bd_addr, UINT8 enc_key_size)
1010 tBTM_SEC_DEV_REC *p_rec;
1012 BTM_TRACE_DEBUG("btm_ble_update_sec_key_size enc_key_size = %d", enc_key_size);
1014 if ((p_rec = btm_find_dev (bd_addr)) != NULL )
1016 p_rec->enc_key_size = enc_key_size;
1020 /*******************************************************************************
1022 ** Function btm_ble_read_sec_key_size
1024 ** Description update the current lin kencryption key size
1028 *******************************************************************************/
1029 UINT8 btm_ble_read_sec_key_size(BD_ADDR bd_addr)
1031 tBTM_SEC_DEV_REC *p_rec;
1033 if ((p_rec = btm_find_dev (bd_addr)) != NULL )
1035 return p_rec->enc_key_size;
1041 /*******************************************************************************
1043 ** Function btm_ble_link_sec_check
1045 ** Description Check BLE link security level match.
1047 ** Returns TRUE: check is OK and the *p_sec_req_act contain the action
1049 *******************************************************************************/
1050 void btm_ble_link_sec_check(BD_ADDR bd_addr, tBTM_LE_AUTH_REQ auth_req, tBTM_BLE_SEC_REQ_ACT *p_sec_req_act)
1052 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
1053 UINT8 req_sec_level = BTM_LE_SEC_NONE, cur_sec_level = BTM_LE_SEC_NONE;
1055 BTM_TRACE_DEBUG ("btm_ble_link_sec_check auth_req =0x%x", auth_req);
1057 if (p_dev_rec == NULL)
1059 BTM_TRACE_ERROR ("btm_ble_link_sec_check received for unknown device");
1063 if (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
1064 p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING)
1066 /* race condition: discard the security request while master is encrypting the link */
1067 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_DISCARD;
1071 req_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
1072 if ((auth_req == (BTM_LE_AUTH_REQ_BOND|BTM_LE_AUTH_REQ_MITM)) ||
1073 (auth_req == (BTM_LE_AUTH_REQ_MITM)) )
1075 req_sec_level = BTM_LE_SEC_AUTHENTICATED;
1078 BTM_TRACE_DEBUG ("dev_rec sec_flags=0x%x", p_dev_rec->sec_flags);
1080 /* currently encrpted */
1081 if (p_dev_rec->sec_flags & BTM_SEC_LE_ENCRYPTED)
1083 if (p_dev_rec->sec_flags & BTM_SEC_LE_LINK_KEY_AUTHED)
1084 cur_sec_level = BTM_LE_SEC_AUTHENTICATED;
1086 cur_sec_level = BTM_LE_SEC_UNAUTHENTICATE;
1088 else /* unencrypted link */
1090 /* if bonded, get the key security level */
1091 if (p_dev_rec->ble.key_type & BTM_LE_KEY_PENC)
1092 cur_sec_level = p_dev_rec->ble.keys.sec_level;
1094 cur_sec_level = BTM_LE_SEC_NONE;
1097 if (cur_sec_level >= req_sec_level)
1099 if (cur_sec_level == BTM_LE_SEC_NONE)
1101 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_NONE;
1105 /* To avoid re-encryption on an encrypted link for an equal condition encryption */
1106 /* if link has been encrypted, do nothing, go straight to furhter action
1107 if (p_dev_rec->sec_flags & BTM_SEC_ENCRYPTED)
1108 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_DISCARD;
1111 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_ENCRYPT;
1116 *p_sec_req_act = BTM_BLE_SEC_REQ_ACT_PAIR; /* start the pariring process to upgrade the keys*/
1120 BTM_TRACE_DEBUG("cur_sec_level=%d req_sec_level=%d sec_req_act=%d",
1127 /*******************************************************************************
1129 ** Function btm_ble_set_encryption
1131 ** Description This function is called to ensure that LE connection is
1132 ** encrypted. Should be called only on an open connection.
1133 ** Typically only needed for connections that first want to
1134 ** bring up unencrypted links, then later encrypt them.
1137 ** the local device ER is copied into er
1139 *******************************************************************************/
1140 tBTM_STATUS btm_ble_set_encryption (BD_ADDR bd_addr, void *p_ref_data, UINT8 link_role)
1142 tBTM_STATUS cmd = BTM_NO_RESOURCES;
1143 tBTM_BLE_SEC_ACT sec_act = *(tBTM_BLE_SEC_ACT *)p_ref_data ;
1144 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
1148 BTM_TRACE_WARNING ("btm_ble_set_encryption (NULL device record!! sec_act=0x%x", sec_act);
1149 return(BTM_WRONG_MODE);
1152 BTM_TRACE_DEBUG ("btm_ble_set_encryption sec_act=0x%x role_master=%d", sec_act, p_rec->role_master);
1154 if (sec_act == BTM_BLE_SEC_ENCRYPT_MITM)
1156 p_rec->security_required |= BTM_SEC_IN_MITM;
1161 case BTM_BLE_SEC_ENCRYPT:
1162 if (link_role == BTM_ROLE_MASTER)
1164 if(p_rec->sec_state == BTM_SEC_STATE_ENCRYPTING) {
1165 BTM_TRACE_DEBUG ("State is already encrypting::");
1166 cmd = BTM_CMD_STARTED;
1169 /* start link layer encryption using the security info stored */
1170 if (btm_ble_start_encrypt(bd_addr, FALSE, NULL))
1172 p_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
1173 cmd = BTM_CMD_STARTED;
1178 /* if salve role then fall through to call SMP_Pair below which will send a
1179 sec_request to request the master to encrypt the link */
1180 case BTM_BLE_SEC_ENCRYPT_NO_MITM:
1181 case BTM_BLE_SEC_ENCRYPT_MITM:
1183 if (SMP_Pair(bd_addr) == SMP_STARTED)
1185 cmd = BTM_CMD_STARTED;
1186 p_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
1191 cmd = BTM_WRONG_MODE;
1197 /*******************************************************************************
1199 ** Function btm_ble_ltk_request
1201 ** Description This function is called when encryption request is received
1202 ** on a slave device.
1207 *******************************************************************************/
1208 void btm_ble_ltk_request(UINT16 handle, UINT8 rand[8], UINT16 ediv)
1210 tBTM_CB *p_cb = &btm_cb;
1211 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev_by_handle (handle);
1212 BT_OCTET8 dummy_stk = {0};
1214 BTM_TRACE_DEBUG ("btm_ble_ltk_request");
1218 memcpy(p_cb->enc_rand, rand, BT_OCTET8_LEN);
1220 if (p_dev_rec != NULL)
1222 if (!smp_proc_ltk_request(p_dev_rec->bd_addr))
1223 btm_ble_ltk_request_reply(p_dev_rec->bd_addr, FALSE, dummy_stk);
1228 /*******************************************************************************
1230 ** Function btm_ble_start_encrypt
1232 ** Description This function is called to start LE encryption.
1235 ** Returns BTM_SUCCESS if encryption was started successfully
1237 *******************************************************************************/
1238 tBTM_STATUS btm_ble_start_encrypt(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
1240 tBTM_CB *p_cb = &btm_cb;
1241 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
1242 BT_OCTET8 dummy_rand = {0};
1243 tBTM_STATUS rt = BTM_NO_RESOURCES;
1245 BTM_TRACE_DEBUG ("btm_ble_start_encrypt");
1249 BTM_TRACE_ERROR("Link is not active, can not encrypt!");
1250 return BTM_WRONG_MODE;
1253 if (p_rec->sec_state == BTM_SEC_STATE_ENCRYPTING)
1255 BTM_TRACE_WARNING("Link Encryption is active, Busy!");
1259 p_cb->enc_handle = p_rec->ble_hci_handle;
1263 if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, dummy_rand, 0, stk))
1264 rt = BTM_CMD_STARTED;
1266 else if (p_rec->ble.key_type & BTM_LE_KEY_PENC)
1268 if (btsnd_hcic_ble_start_enc(p_rec->ble_hci_handle, p_rec->ble.keys.rand,
1269 p_rec->ble.keys.ediv, p_rec->ble.keys.ltk))
1270 rt = BTM_CMD_STARTED;
1274 BTM_TRACE_ERROR("No key available to encrypt the link");
1276 if (rt == BTM_CMD_STARTED)
1278 if (p_rec->sec_state == BTM_SEC_STATE_IDLE)
1279 p_rec->sec_state = BTM_SEC_STATE_ENCRYPTING;
1285 /*******************************************************************************
1287 ** Function btm_ble_link_encrypted
1289 ** Description This function is called when LE link encrption status is changed.
1293 *******************************************************************************/
1294 void btm_ble_link_encrypted(BD_ADDR bd_addr, UINT8 encr_enable)
1296 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
1301 BTM_TRACE_WARNING ("btm_ble_link_encrypted (No Device Found!) encr_enable=%d", encr_enable);
1305 BTM_TRACE_DEBUG ("btm_ble_link_encrypted encr_enable=%d", encr_enable);
1307 enc_cback = (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING);
1309 smp_link_encrypted(bd_addr, encr_enable);
1311 BTM_TRACE_DEBUG(" p_dev_rec->sec_flags=0x%x", p_dev_rec->sec_flags);
1313 if (encr_enable && p_dev_rec->enc_key_size == 0)
1314 p_dev_rec->enc_key_size = p_dev_rec->ble.keys.key_size;
1316 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
1317 if (p_dev_rec->p_callback && enc_cback)
1320 btm_sec_dev_rec_cback_event(p_dev_rec, BTM_SUCCESS, TRUE);
1321 else if (p_dev_rec->role_master)
1322 btm_sec_dev_rec_cback_event(p_dev_rec, BTM_ERR_PROCESSING, TRUE);
1325 /* to notify GATT to send data if any request is pending */
1326 gatt_notify_enc_cmpl(p_dev_rec->bd_addr);
1329 /*******************************************************************************
1330 ** Function btm_enc_proc_ltk
1331 ** Description send LTK reply when it's ready.
1332 *******************************************************************************/
1333 static void btm_enc_proc_ltk(tSMP_ENC *p)
1336 BTM_TRACE_DEBUG ("btm_enc_proc_ltk");
1337 if (p && p->param_len == BT_OCTET16_LEN)
1339 for (i = 0; i < (BT_OCTET16_LEN - btm_cb.key_size); i ++)
1340 p->param_buf[BT_OCTET16_LEN - i - 1] = 0;
1341 btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, p->param_buf);
1345 /*******************************************************************************
1346 ** Function btm_enc_proc_slave_y
1347 ** Description calculate LTK when Y is ready
1348 *******************************************************************************/
1349 static void btm_enc_proc_slave_y(tSMP_ENC *p)
1352 UINT8 *pp = p->param_buf;
1353 tBTM_CB *p_cb = &btm_cb;
1355 tBTM_SEC_DEV_REC *p_dev_rec;
1357 BTM_TRACE_DEBUG ("btm_enc_proc_slave_y");
1360 STREAM_TO_UINT16(y, pp);
1362 div = p_cb->ediv ^ y;
1363 p_dev_rec = btm_find_dev_by_handle (p_cb->enc_handle);
1366 p_dev_rec->ble.keys.div == div )
1368 BTM_TRACE_DEBUG ("LTK request OK");
1369 /* calculating LTK , LTK = E er(div) */
1370 SMP_Encrypt(p_cb->devcb.er, BT_OCTET16_LEN, (UINT8 *)&div, 2, &output);
1371 btm_enc_proc_ltk(&output);
1375 BTM_TRACE_DEBUG ("LTK request failed - send negative reply");
1376 btsnd_hcic_ble_ltk_req_neg_reply(p_cb->enc_handle);
1378 btm_ble_link_encrypted(p_dev_rec->bd_addr, 0);
1384 /*******************************************************************************
1386 ** Function btm_ble_ltk_request_reply
1388 ** Description This function is called to send a LTK request reply on a slave
1393 *******************************************************************************/
1394 void btm_ble_ltk_request_reply(BD_ADDR bda, BOOLEAN use_stk, BT_OCTET16 stk)
1396 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bda);
1397 tBTM_CB *p_cb = &btm_cb;
1402 BTM_TRACE_ERROR("btm_ble_ltk_request_reply received for unknown device");
1406 BTM_TRACE_DEBUG ("btm_ble_ltk_request_reply");
1407 p_cb->enc_handle = p_rec->ble_hci_handle;
1408 p_cb->key_size = p_rec->ble.keys.key_size;
1410 BTM_TRACE_ERROR("key size = %d", p_rec->ble.keys.key_size);
1413 btsnd_hcic_ble_ltk_req_reply(btm_cb.enc_handle, stk);
1415 else /* calculate LTK using peer device */
1417 /* generate Y= Encrypt(DHK, Rand) received from encrypt request */
1418 SMP_Encrypt(p_cb->devcb.id_keys.dhk, BT_OCTET16_LEN, p_cb->enc_rand,
1419 BT_OCTET8_LEN, &output);
1420 btm_enc_proc_slave_y(&output);
1424 /*******************************************************************************
1426 ** Function btm_ble_io_capabilities_req
1428 ** Description This function is called to handle SMP get IO capability request.
1432 *******************************************************************************/
1433 UINT8 btm_ble_io_capabilities_req(tBTM_SEC_DEV_REC *p_dev_rec, tBTM_LE_IO_REQ *p_data)
1435 UINT8 callback_rc = BTM_SUCCESS;
1436 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req");
1437 if (btm_cb.api.p_le_callback)
1439 /* the callback function implementation may change the IO capability... */
1440 callback_rc = (*btm_cb.api.p_le_callback) (BTM_LE_IO_REQ_EVT, p_dev_rec->bd_addr, (tBTM_LE_EVT_DATA *)p_data);
1442 #if BTM_OOB_INCLUDED == TRUE
1443 if ((callback_rc == BTM_SUCCESS) || (BTM_OOB_UNKNOWN != p_data->oob_data))
1445 if (callback_rc == BTM_SUCCESS)
1448 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
1449 if (btm_cb.devcb.keep_rfu_in_auth_req)
1451 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req keep_rfu_in_auth_req = %u",
1452 btm_cb.devcb.keep_rfu_in_auth_req);
1453 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK_KEEP_RFU;
1454 btm_cb.devcb.keep_rfu_in_auth_req = FALSE;
1458 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
1461 p_data->auth_req &= BTM_LE_AUTH_REQ_MASK;
1464 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 1: p_dev_rec->security_required = %d auth_req:%d",
1465 p_dev_rec->security_required, p_data->auth_req);
1466 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 2: i_keys=0x%x r_keys=0x%x (bit 0-LTK 1-IRK 2-CSRK)",
1470 /* if authentication requires MITM protection, put on the mask */
1471 if (p_dev_rec->security_required & BTM_SEC_IN_MITM)
1472 p_data->auth_req |= BTM_LE_AUTH_REQ_MITM;
1474 if (!(p_data->auth_req & SMP_AUTH_BOND))
1476 BTM_TRACE_DEBUG("Non bonding: No keys should be exchanged");
1477 p_data->init_keys = 0;
1478 p_data->resp_keys = 0;
1481 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 3: auth_req:%d", p_data->auth_req);
1482 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 4: i_keys=0x%x r_keys=0x%x",
1486 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 5: p_data->io_cap = %d auth_req:%d",
1487 p_data->io_cap, p_data->auth_req);
1489 /* remove MITM protection requirement if IO cap does not allow it */
1490 if ((p_data->io_cap == BTM_IO_CAP_NONE) && p_data->oob_data == SMP_OOB_NONE)
1491 p_data->auth_req &= ~BTM_LE_AUTH_REQ_MITM;
1493 BTM_TRACE_DEBUG ("btm_ble_io_capabilities_req 6: IO_CAP:%d oob_data:%d auth_req:%d",
1494 p_data->io_cap, p_data->oob_data, p_data->auth_req);
1499 #if (BLE_PRIVACY_SPT == TRUE )
1500 /*******************************************************************************
1502 ** Function btm_ble_resolve_random_addr_on_conn_cmpl
1504 ** Description resolve random address complete on connection complete event.
1508 *******************************************************************************/
1509 static void btm_ble_resolve_random_addr_on_conn_cmpl(void * p_rec, void *p_data)
1511 UINT8 *p = (UINT8 *)p_data;
1512 tBTM_SEC_DEV_REC *match_rec = (tBTM_SEC_DEV_REC *) p_rec;
1513 UINT8 role, status, bda_type;
1516 UINT16 conn_interval, conn_latency, conn_timeout;
1517 BOOLEAN match = FALSE;
1519 STREAM_TO_UINT8 (status, p);
1520 STREAM_TO_UINT16 (handle, p);
1521 STREAM_TO_UINT8 (role, p);
1522 STREAM_TO_UINT8 (bda_type, p);
1523 STREAM_TO_BDADDR (bda, p);
1524 STREAM_TO_UINT16 (conn_interval, p);
1525 STREAM_TO_UINT16 (conn_latency, p);
1526 STREAM_TO_UINT16 (conn_timeout, p);
1528 handle = HCID_GET_HANDLE (handle);
1530 BTM_TRACE_EVENT ("btm_ble_resolve_random_addr_master_cmpl");
1534 BTM_TRACE_ERROR("Random match");
1536 match_rec->ble.active_addr_type = BTM_BLE_ADDR_RRA;
1537 memcpy(match_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
1538 memcpy(bda, match_rec->bd_addr, BD_ADDR_LEN);
1542 BTM_TRACE_ERROR("Random unmatch");
1545 btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
1547 l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
1548 conn_latency, conn_timeout);
1554 /*******************************************************************************
1556 ** Function btm_ble_connected
1558 ** Description This function is when a LE connection to the peer device is
1563 *******************************************************************************/
1564 void btm_ble_connected (UINT8 *bda, UINT16 handle, UINT8 enc_mode, UINT8 role,
1565 tBLE_ADDR_TYPE addr_type, BOOLEAN addr_matched)
1567 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bda);
1568 tBTM_BLE_CB *p_cb = &btm_cb.ble_ctr_cb;
1569 UNUSED(addr_matched);
1571 BTM_TRACE_EVENT ("btm_ble_connected");
1573 /* Commenting out trace due to obf/compilation problems.
1575 #if (BT_USE_TRACES == TRUE)
1578 BTM_TRACE_EVENT ("Security Manager: btm_ble_connected : handle:%d enc_mode:%d bda:%x RName:%s",
1580 (bda[2]<<24)+(bda[3]<<16)+(bda[4]<<8)+bda[5],
1581 p_dev_rec->sec_bd_name);
1583 BTM_TRACE_DEBUG ("btm_ble_connected sec_flags=0x%x",p_dev_rec->sec_flags);
1587 BTM_TRACE_EVENT ("Security Manager: btm_ble_connected: handle:%d enc_mode:%d bda:%x ",
1589 (bda[2]<<24)+(bda[3]<<16)+(bda[4]<<8)+bda[5]);
1595 /* There is no device record for new connection. Allocate one */
1596 p_dev_rec = btm_sec_alloc_dev (bda);
1598 else /* Update the timestamp for this device */
1600 p_dev_rec->timestamp = btm_cb.dev_rec_count++;
1603 /* update device information */
1604 p_dev_rec->device_type |= BT_DEVICE_TYPE_BLE;
1605 p_dev_rec->ble_hci_handle = handle;
1606 p_dev_rec->ble.ble_addr_type = addr_type;
1608 p_dev_rec->role_master = FALSE;
1609 if (role == HCI_ROLE_MASTER)
1610 p_dev_rec->role_master = TRUE;
1612 #if (defined BLE_PRIVACY_SPT && BLE_PRIVACY_SPT == TRUE)
1614 p_dev_rec->ble.active_addr_type = BTM_BLE_ADDR_PSEUDO;
1616 if (p_dev_rec->ble.ble_addr_type == BLE_ADDR_RANDOM && !addr_matched)
1617 memcpy(p_dev_rec->ble.cur_rand_addr, bda, BD_ADDR_LEN);
1620 if (role == HCI_ROLE_SLAVE)
1621 p_cb->inq_var.adv_mode = BTM_BLE_ADV_DISABLE;
1622 p_cb->inq_var.directed_conn = FALSE;
1627 /*****************************************************************************
1628 ** Function btm_ble_conn_complete
1630 ** Description LE connection complete.
1632 ******************************************************************************/
1633 void btm_ble_conn_complete(UINT8 *p, UINT16 evt_len)
1635 #if (BLE_PRIVACY_SPT == TRUE )
1638 UINT8 role, status, bda_type;
1641 UINT16 conn_interval, conn_latency, conn_timeout;
1642 BOOLEAN match = FALSE;
1645 STREAM_TO_UINT8 (status, p);
1646 STREAM_TO_UINT16 (handle, p);
1647 STREAM_TO_UINT8 (role, p);
1648 STREAM_TO_UINT8 (bda_type, p);
1649 STREAM_TO_BDADDR (bda, p);
1653 #if (BLE_PRIVACY_SPT == TRUE )
1655 if (btm_cb.cmn_ble_vsc_cb.rpa_offloading == TRUE)
1656 match = btm_public_addr_to_random_pseudo (bda, &bda_type);
1658 /* possiblly receive connection complete with resolvable random on
1659 slave role while the device has been paired */
1660 if (!match && /*role == HCI_ROLE_SLAVE && */BTM_BLE_IS_RESOLVE_BDA(bda))
1662 btm_ble_resolve_random_addr(bda, btm_ble_resolve_random_addr_on_conn_cmpl, p_data);
1667 STREAM_TO_UINT16 (conn_interval, p);
1668 STREAM_TO_UINT16 (conn_latency, p);
1669 STREAM_TO_UINT16 (conn_timeout, p);
1670 handle = HCID_GET_HANDLE (handle);
1672 btm_ble_connected(bda, handle, HCI_ENCRYPT_MODE_DISABLED, role, bda_type, match);
1673 l2cble_conn_comp (handle, role, bda, bda_type, conn_interval,
1674 conn_latency, conn_timeout);
1679 role = HCI_ROLE_UNKNOWN;
1681 if (status != HCI_ERR_DIRECTED_ADVERTISING_TIMEOUT)
1683 btm_ble_set_conn_st(BLE_CONN_IDLE);
1685 /* this is to work around broadcom firmware problem to handle
1686 * unsolicited command complete event for HCI_LE_Create_Connection_Cancel
1687 * and LE connection complete event with status error code (0x2)
1688 * unknown connection identifier from bluetooth controller
1689 * the workaround is to release the HCI connection to avoid out of sync
1690 * with bluetooth controller, which cause BT can't be turned off.
1692 else if ((status == HCI_ERR_NO_CONNECTION) &&
1693 (btm_ble_get_conn_st() != BLE_CONN_CANCEL))
1696 handle = HCID_GET_HANDLE (handle);
1697 p_lcb = l2cu_find_lcb_by_handle (handle);
1700 l2c_link_hci_disc_comp (handle, HCI_ERR_PEER_USER);
1701 btm_sec_disconnected (handle, HCI_ERR_PEER_USER);
1705 btm_ble_set_conn_st(BLE_CONN_IDLE);
1706 btm_ble_update_mode_operation(role, bda, status);
1709 /*****************************************************************************
1710 ** Function btm_ble_create_ll_conn_complete
1712 ** Description LE connection complete.
1714 ******************************************************************************/
1715 void btm_ble_create_ll_conn_complete (UINT8 status)
1719 btm_ble_set_conn_st(BLE_CONN_IDLE);
1720 btm_ble_update_mode_operation(HCI_ROLE_UNKNOWN, NULL, status);
1723 /*****************************************************************************
1724 ** Function btm_proc_smp_cback
1726 ** Description This function is the SMP callback handler.
1728 ******************************************************************************/
1729 UINT8 btm_proc_smp_cback(tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data)
1731 tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
1734 BTM_TRACE_DEBUG ("btm_proc_smp_cback event = %d", event);
1736 if (p_dev_rec != NULL)
1740 case SMP_IO_CAP_REQ_EVT:
1741 btm_ble_io_capabilities_req(p_dev_rec, (tBTM_LE_IO_REQ *)&p_data->io_req);
1744 case SMP_PASSKEY_REQ_EVT:
1745 case SMP_PASSKEY_NOTIF_EVT:
1746 case SMP_OOB_REQ_EVT:
1747 p_dev_rec->sec_flags |= BTM_SEC_LE_AUTHENTICATED;
1749 case SMP_SEC_REQUEST_EVT:
1750 memcpy (btm_cb.pairing_bda, bd_addr, BD_ADDR_LEN);
1751 p_dev_rec->sec_state = BTM_SEC_STATE_AUTHENTICATING;
1752 btm_cb.pairing_flags |= BTM_PAIR_FLAGS_LE_ACTIVE;
1754 case SMP_COMPLT_EVT:
1755 if (btm_cb.api.p_le_callback)
1757 /* the callback function implementation may change the IO capability... */
1758 BTM_TRACE_DEBUG ("btm_cb.api.p_le_callback=0x%x", btm_cb.api.p_le_callback );
1759 (*btm_cb.api.p_le_callback) (event, bd_addr, (tBTM_LE_EVT_DATA *)p_data);
1762 if (event == SMP_COMPLT_EVT)
1764 BTM_TRACE_DEBUG ("evt=SMP_COMPLT_EVT before update sec_level=0x%x sec_flags=0x%x", p_data->cmplt.sec_level , p_dev_rec->sec_flags );
1766 res = (p_data->cmplt.reason == SMP_SUCCESS) ? BTM_SUCCESS : BTM_ERR_PROCESSING;
1768 BTM_TRACE_DEBUG ("after update result=%d sec_level=0x%x sec_flags=0x%x",
1769 res, p_data->cmplt.sec_level , p_dev_rec->sec_flags );
1771 btm_sec_dev_rec_cback_event(p_dev_rec, res, TRUE);
1773 if (p_data->cmplt.is_pair_cancel && btm_cb.api.p_bond_cancel_cmpl_callback )
1775 BTM_TRACE_DEBUG ("Pairing Cancel completed");
1776 (*btm_cb.api.p_bond_cancel_cmpl_callback)(BTM_SUCCESS);
1778 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
1779 if (res != BTM_SUCCESS)
1781 if (!btm_cb.devcb.no_disc_if_pair_fail && p_data->cmplt.reason != SMP_CONN_TOUT)
1783 BTM_TRACE_DEBUG ("Pairing failed - Remove ACL");
1784 btm_remove_acl(bd_addr, BT_TRANSPORT_LE);
1788 BTM_TRACE_DEBUG ("Pairing failed - Not Removing ACL");
1789 p_dev_rec->sec_state = BTM_SEC_STATE_IDLE;
1793 if (res != BTM_SUCCESS && p_data->cmplt.reason != SMP_CONN_TOUT)
1794 btm_remove_acl(bd_addr, BT_TRANSPORT_LE);
1797 BTM_TRACE_DEBUG ("btm_cb pairing_state=%x pairing_flags=%x pin_code_len=%x",
1798 btm_cb.pairing_state,
1799 btm_cb.pairing_flags,
1800 btm_cb.pin_code_len );
1801 BTM_TRACE_DEBUG ("btm_cb.pairing_bda %02x:%02x:%02x:%02x:%02x:%02x",
1802 btm_cb.pairing_bda[0], btm_cb.pairing_bda[1], btm_cb.pairing_bda[2],
1803 btm_cb.pairing_bda[3], btm_cb.pairing_bda[4], btm_cb.pairing_bda[5]);
1805 memset (btm_cb.pairing_bda, 0xff, BD_ADDR_LEN);
1806 btm_cb.pairing_state = BTM_PAIR_STATE_IDLE;
1807 btm_cb.pairing_flags = 0;
1812 BTM_TRACE_DEBUG ("unknown event = %d", event);
1820 BTM_TRACE_ERROR("btm_proc_smp_cback received for unknown device");
1826 #endif /* SMP_INCLUDED */
1828 /*******************************************************************************
1830 ** Function BTM_BleDataSignature
1832 ** Description This function is called to sign the data using AES128 CMAC
1835 ** Parameter bd_addr: target device the data to be signed for.
1836 ** p_text: singing data
1837 ** len: length of the data to be signed.
1838 ** signature: output parameter where data signature is going to
1841 ** Returns TRUE if signing sucessul, otherwise FALSE.
1843 *******************************************************************************/
1844 BOOLEAN BTM_BleDataSignature (BD_ADDR bd_addr, UINT8 *p_text, UINT16 len,
1845 BLE_SIGNATURE signature)
1847 BOOLEAN ret = FALSE;
1848 #if SMP_INCLUDED == TRUE
1849 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
1854 UINT8 temp[4]; /* for (r || DIV) r=1*/
1856 UINT8 *p=temp, *p_mac = (UINT8 *)signature;
1858 BT_OCTET16 local_csrk;
1860 BTM_TRACE_DEBUG ("BTM_BleDataSignature");
1863 BTM_TRACE_ERROR("data signing can not be done from unknow device");
1867 if ((p_buf = (UINT8 *)GKI_getbuf((UINT16)(len + 4))) != NULL)
1869 BTM_TRACE_DEBUG("Start to generate Local CSRK");
1871 /* prepare plain text */
1874 memcpy(p_buf, p_text, len);
1878 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
1879 if ( btm_cb.devcb.enable_test_local_sign_cntr)
1881 BTM_TRACE_DEBUG ("Use Test local counter value from script counter_val=%d", btm_cb.devcb.test_local_sign_cntr);
1882 UINT32_TO_STREAM(pp, btm_cb.devcb.test_local_sign_cntr);
1886 UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
1889 UINT32_TO_STREAM(pp, p_rec->ble.keys.local_counter);
1891 /* compute local csrk */
1892 if (btm_get_local_div(bd_addr, &div))
1894 BTM_TRACE_DEBUG ("compute_csrk div=%x", div);
1895 BTM_GetDeviceEncRoot(er);
1897 /* CSRK = d1(ER, DIV, 1) */
1898 UINT16_TO_STREAM(p, div);
1899 UINT16_TO_STREAM(p, r);
1901 if (!SMP_Encrypt(er, BT_OCTET16_LEN, temp, 4, &output))
1903 BTM_TRACE_ERROR("Local CSRK generation failed ");
1907 BTM_TRACE_DEBUG("local CSRK generation success");
1908 memcpy((void *)local_csrk, output.param_buf, BT_OCTET16_LEN);
1911 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
1912 if (btm_cb.devcb.enable_test_local_sign_cntr)
1914 UINT32_TO_STREAM(p_mac, btm_cb.devcb.test_local_sign_cntr);
1918 UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
1921 UINT32_TO_STREAM(p_mac, p_rec->ble.keys.local_counter);
1924 if ((ret = AES_CMAC(local_csrk, p_buf, (UINT16)(len + 4), BTM_CMAC_TLEN_SIZE, p_mac)) == TRUE)
1926 btm_ble_increment_sign_ctr(bd_addr, TRUE);
1928 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
1929 if ( btm_cb.devcb.enable_test_mac_val)
1931 BTM_TRACE_DEBUG ("Use MAC value from script");
1932 memcpy(p_mac, btm_cb.devcb.test_mac, BTM_CMAC_TLEN_SIZE);
1936 BTM_TRACE_DEBUG("BTM_BleDataSignature p_mac = %d", p_mac);
1937 BTM_TRACE_DEBUG("p_mac[0] = 0x%02x p_mac[1] = 0x%02x p_mac[2] = 0x%02x p_mac[3] = 0x%02x",
1938 *p_mac, *(p_mac + 1), *(p_mac + 2), *(p_mac + 3));
1939 BTM_TRACE_DEBUG("p_mac[4] = 0x%02x p_mac[5] = 0x%02x p_mac[6] = 0x%02x p_mac[7] = 0x%02x",
1940 *(p_mac + 4), *(p_mac + 5), *(p_mac + 6), *(p_mac + 7));
1947 #endif /* SMP_INCLUDED */
1951 /*******************************************************************************
1953 ** Function BTM_BleVerifySignature
1955 ** Description This function is called to verify the data signature
1957 ** Parameter bd_addr: target device the data to be signed for.
1958 ** p_orig: original data before signature.
1959 ** len: length of the signing data
1960 ** counter: counter used when doing data signing
1961 ** p_comp: signature to be compared against.
1963 ** Returns TRUE if signature verified correctly; otherwise FALSE.
1965 *******************************************************************************/
1966 BOOLEAN BTM_BleVerifySignature (BD_ADDR bd_addr, UINT8 *p_orig, UINT16 len, UINT32 counter,
1969 BOOLEAN verified = FALSE;
1970 #if SMP_INCLUDED == TRUE
1971 tBTM_SEC_DEV_REC *p_rec = btm_find_dev (bd_addr);
1972 UINT8 p_mac[BTM_CMAC_TLEN_SIZE];
1974 if (p_rec == NULL || (p_rec && !(p_rec->ble.key_type & BTM_LE_KEY_PCSRK)))
1976 BTM_TRACE_ERROR("can not verify signature for unknown device");
1978 else if (counter < p_rec->ble.keys.counter)
1980 BTM_TRACE_ERROR("signature received with out dated sign counter");
1982 else if (p_orig == NULL)
1984 BTM_TRACE_ERROR("No signature to verify");
1988 BTM_TRACE_DEBUG ("BTM_BleVerifySignature rcv_cnt=%d >= expected_cnt=%d", counter, p_rec->ble.keys.counter);
1990 if (AES_CMAC(p_rec->ble.keys.csrk, p_orig, len, BTM_CMAC_TLEN_SIZE, p_mac))
1992 if (memcmp(p_mac, p_comp, BTM_CMAC_TLEN_SIZE) == 0)
1994 btm_ble_increment_sign_ctr(bd_addr, FALSE);
1999 #endif /* SMP_INCLUDED */
2003 /*******************************************************************************
2004 ** Utility functions for LE device IR/ER generation
2005 *******************************************************************************/
2006 /*******************************************************************************
2008 ** Function btm_notify_new_key
2010 ** Description This function is to notify application new keys have been
2015 *******************************************************************************/
2016 static void btm_notify_new_key(UINT8 key_type)
2018 tBTM_BLE_LOCAL_KEYS *p_locak_keys = NULL;
2020 BTM_TRACE_DEBUG ("btm_notify_new_key key_type=%d", key_type);
2022 if (btm_cb.api.p_le_key_callback)
2026 case BTM_BLE_KEY_TYPE_ID:
2027 BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ID");
2028 p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.id_keys;
2031 case BTM_BLE_KEY_TYPE_ER:
2032 BTM_TRACE_DEBUG ("BTM_BLE_KEY_TYPE_ER");
2033 p_locak_keys = (tBTM_BLE_LOCAL_KEYS *)&btm_cb.devcb.er;
2037 BTM_TRACE_ERROR("unknown key type: %d", key_type);
2040 if (p_locak_keys != NULL)
2041 (*btm_cb.api.p_le_key_callback) (key_type, p_locak_keys);
2045 /*******************************************************************************
2047 ** Function btm_ble_process_er2
2049 ** Description This function is called when ER is generated, store it in
2050 ** local control block.
2054 *******************************************************************************/
2055 static void btm_ble_process_er2(tBTM_RAND_ENC *p)
2057 BTM_TRACE_DEBUG ("btm_ble_process_er2");
2059 if (p &&p->opcode == HCI_BLE_RAND)
2061 memcpy(&btm_cb.devcb.er[8], p->param_buf, BT_OCTET8_LEN);
2062 btm_notify_new_key(BTM_BLE_KEY_TYPE_ER);
2066 BTM_TRACE_ERROR("Generating ER2 exception.");
2067 memset(&btm_cb.devcb.er, 0, sizeof(BT_OCTET16));
2071 /*******************************************************************************
2073 ** Function btm_ble_process_er
2075 ** Description This function is called when ER is generated, store it in
2076 ** local control block.
2080 *******************************************************************************/
2081 static void btm_ble_process_er(tBTM_RAND_ENC *p)
2083 BTM_TRACE_DEBUG ("btm_ble_process_er");
2085 if (p &&p->opcode == HCI_BLE_RAND)
2087 memcpy(&btm_cb.devcb.er[0], p->param_buf, BT_OCTET8_LEN);
2089 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er2))
2091 memset(&btm_cb.devcb.er, 0, sizeof(BT_OCTET16));
2092 BTM_TRACE_ERROR("Generating ER2 failed.");
2097 BTM_TRACE_ERROR("Generating ER1 exception.");
2101 /*******************************************************************************
2103 ** Function btm_ble_process_irk
2105 ** Description This function is called when IRK is generated, store it in
2106 ** local control block.
2110 *******************************************************************************/
2111 static void btm_ble_process_irk(tSMP_ENC *p)
2113 BTM_TRACE_DEBUG ("btm_ble_process_irk");
2114 if (p &&p->opcode == HCI_BLE_ENCRYPT)
2116 memcpy(btm_cb.devcb.id_keys.irk, p->param_buf, BT_OCTET16_LEN);
2117 btm_notify_new_key(BTM_BLE_KEY_TYPE_ID);
2121 BTM_TRACE_ERROR("Generating IRK exception.");
2124 /* proceed generate ER */
2125 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_er))
2127 BTM_TRACE_ERROR("Generating ER failed.");
2131 /*******************************************************************************
2133 ** Function btm_ble_process_dhk
2135 ** Description This function is called when DHK is calculated, store it in
2136 ** local control block, and proceed to generate ER, a 128-bits
2141 *******************************************************************************/
2142 static void btm_ble_process_dhk(tSMP_ENC *p)
2144 #if SMP_INCLUDED == TRUE
2145 UINT8 btm_ble_irk_pt = 0x01;
2148 BTM_TRACE_DEBUG ("btm_ble_process_dhk");
2150 if (p && p->opcode == HCI_BLE_ENCRYPT)
2152 memcpy(btm_cb.devcb.id_keys.dhk, p->param_buf, BT_OCTET16_LEN);
2153 BTM_TRACE_DEBUG("BLE DHK generated.");
2155 /* IRK = D1(IR, 1) */
2156 if (!SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_irk_pt,
2159 /* reset all identity root related key */
2160 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2164 btm_ble_process_irk(&output);
2169 /* reset all identity root related key */
2170 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2175 /*******************************************************************************
2177 ** Function btm_ble_process_ir2
2179 ** Description This function is called when IR is generated, proceed to calculate
2180 ** DHK = Eir({0x03, 0, 0 ...})
2185 *******************************************************************************/
2186 static void btm_ble_process_ir2(tBTM_RAND_ENC *p)
2188 #if SMP_INCLUDED == TRUE
2189 UINT8 btm_ble_dhk_pt = 0x03;
2192 BTM_TRACE_DEBUG ("btm_ble_process_ir2");
2194 if (p && p->opcode == HCI_BLE_RAND)
2196 /* remembering in control block */
2197 memcpy(&btm_cb.devcb.id_keys.ir[8], p->param_buf, BT_OCTET8_LEN);
2198 /* generate DHK= Eir({0x03, 0x00, 0x00 ...}) */
2201 SMP_Encrypt(btm_cb.devcb.id_keys.ir, BT_OCTET16_LEN, &btm_ble_dhk_pt,
2203 btm_ble_process_dhk(&output);
2205 BTM_TRACE_DEBUG("BLE IR generated.");
2209 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2214 /*******************************************************************************
2216 ** Function btm_ble_process_ir
2218 ** Description This function is called when IR is generated, proceed to calculate
2219 ** DHK = Eir({0x02, 0, 0 ...})
2224 *******************************************************************************/
2225 static void btm_ble_process_ir(tBTM_RAND_ENC *p)
2227 BTM_TRACE_DEBUG ("btm_ble_process_ir");
2229 if (p && p->opcode == HCI_BLE_RAND)
2231 /* remembering in control block */
2232 memcpy(btm_cb.devcb.id_keys.ir, p->param_buf, BT_OCTET8_LEN);
2234 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir2))
2236 BTM_TRACE_ERROR("Generating IR2 failed.");
2237 memset(&btm_cb.devcb.id_keys, 0, sizeof(tBTM_BLE_LOCAL_ID_KEYS));
2242 /*******************************************************************************
2244 ** Function btm_ble_reset_id
2246 ** Description This function is called to reset LE device identity.
2250 *******************************************************************************/
2251 void btm_ble_reset_id( void )
2253 BTM_TRACE_DEBUG ("btm_ble_reset_id");
2255 /* regenrate Identity Root*/
2256 if (!btsnd_hcic_ble_rand((void *)btm_ble_process_ir))
2258 BTM_TRACE_DEBUG("Generating IR failed.");
2262 #if BTM_BLE_CONFORMANCE_TESTING == TRUE
2263 /*******************************************************************************
2265 ** Function btm_ble_set_no_disc_if_pair_fail
2267 ** Description This function indicates that whether no disconnect of the ACL
2268 ** should be used if pairing failed
2272 *******************************************************************************/
2273 void btm_ble_set_no_disc_if_pair_fail(BOOLEAN disable_disc )
2275 BTM_TRACE_DEBUG ("btm_ble_set_disc_enable_if_pair_fail disable_disc=%d", disable_disc);
2276 btm_cb.devcb.no_disc_if_pair_fail = disable_disc;
2279 /*******************************************************************************
2281 ** Function btm_ble_set_test_mac_value
2283 ** Description This function set test MAC value
2287 *******************************************************************************/
2288 void btm_ble_set_test_mac_value(BOOLEAN enable, UINT8 *p_test_mac_val )
2290 BTM_TRACE_DEBUG ("btm_ble_set_test_mac_value enable=%d", enable);
2291 btm_cb.devcb.enable_test_mac_val = enable;
2292 memcpy(btm_cb.devcb.test_mac, p_test_mac_val, BT_OCTET8_LEN);
2295 /*******************************************************************************
2297 ** Function btm_ble_set_test_local_sign_cntr_value
2299 ** Description This function set test local sign counter value
2303 *******************************************************************************/
2304 void btm_ble_set_test_local_sign_cntr_value(BOOLEAN enable, UINT32 test_local_sign_cntr )
2306 BTM_TRACE_DEBUG ("btm_ble_set_test_local_sign_cntr_value enable=%d local_sign_cntr=%d",
2307 enable, test_local_sign_cntr);
2308 btm_cb.devcb.enable_test_local_sign_cntr = enable;
2309 btm_cb.devcb.test_local_sign_cntr = test_local_sign_cntr;
2312 /*******************************************************************************
2314 ** Function btm_set_random_address
2316 ** Description This function set a random address to local controller.
2320 *******************************************************************************/
2321 void btm_set_random_address(BD_ADDR random_bda)
2323 tBTM_LE_RANDOM_CB *p_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb;
2324 BOOLEAN adv_mode = btm_cb.ble_ctr_cb.inq_var.adv_mode ;
2326 BTM_TRACE_DEBUG ("btm_set_random_address");
2328 if (adv_mode == BTM_BLE_ADV_ENABLE)
2329 btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_DISABLE);
2331 memcpy(p_cb->private_addr, random_bda, BD_ADDR_LEN);
2332 btsnd_hcic_ble_set_random_addr(p_cb->private_addr);
2334 if (adv_mode == BTM_BLE_ADV_ENABLE)
2335 btsnd_hcic_ble_set_adv_enable (BTM_BLE_ADV_ENABLE);
2340 /*******************************************************************************
2342 ** Function btm_ble_set_keep_rfu_in_auth_req
2344 ** Description This function indicates if RFU bits have to be kept as is
2345 ** (by default they have to be set to 0 by the sender).
2349 *******************************************************************************/
2350 void btm_ble_set_keep_rfu_in_auth_req(BOOLEAN keep_rfu)
2352 BTM_TRACE_DEBUG ("btm_ble_set_keep_rfu_in_auth_req keep_rfus=%d", keep_rfu);
2353 btm_cb.devcb.keep_rfu_in_auth_req = keep_rfu;
2356 #endif /* BTM_BLE_CONFORMANCE_TESTING */
2358 #endif /* BLE_INCLUDED */