1 /*
2  *
3  * Copyright 2023 The Android Open Source Project
4  *
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:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #pragma once
20 
21 #include <cstdint>
22 #include "stack/include/bt_octets.h"
23 #include "stack/include/btm_sec_api_types.h"
24 
25 //////////////////////////////////////////////////////////
26 ////// from btm_ble_api_types.h
27 /////////////////////////////////////////////////////////
28 /* BLE encryption keys */
29 typedef struct {
30   Octet16 ltk;
31   BT_OCTET8 rand;
32   uint16_t ediv;
33   uint8_t sec_level;
34   uint8_t key_size;
35 } tBTM_LE_PENC_KEYS;
36 
37 /* BLE CSRK keys */
38 typedef struct {
39   uint32_t counter;
40   Octet16 csrk;
41   uint8_t sec_level;
42 } tBTM_LE_PCSRK_KEYS;
43 
44 /* BLE Encryption reproduction keys */
45 typedef struct {
46   Octet16 ltk;
47   uint16_t div;
48   uint8_t key_size;
49   uint8_t sec_level;
50 } tBTM_LE_LENC_KEYS;
51 
52 /* BLE SRK keys */
53 typedef struct {
54   uint32_t counter;
55   uint16_t div;
56   uint8_t sec_level;
57   Octet16 csrk;
58 } tBTM_LE_LCSRK_KEYS;
59 
60 typedef struct {
61   Octet16 irk;
62   tBLE_ADDR_TYPE identity_addr_type;
63   RawAddress identity_addr;
64 } tBTM_LE_PID_KEYS;
65 
66 typedef union {
67   tBTM_LE_PENC_KEYS penc_key;   /* received peer encryption key */
68   tBTM_LE_PCSRK_KEYS pcsrk_key; /* received peer device SRK */
69   tBTM_LE_PID_KEYS pid_key;     /* peer device ID key */
70   tBTM_LE_LENC_KEYS lenc_key;   /* local encryption reproduction keys
71                                  * LTK = = d1(ER,DIV,0) */
72   tBTM_LE_LCSRK_KEYS lcsrk_key; /* local device CSRK = d1(ER,DIV,1)*/
73 } tBTM_LE_KEY_VALUE;
74 
75 typedef struct {
76   tBTM_LE_KEY_TYPE key_type;
77   tBTM_LE_KEY_VALUE* p_key_value;
78 } tBTM_LE_KEY;
79 
80 typedef union {
81   tBTM_LE_IO_REQ io_req; /* BTM_LE_IO_REQ_EVT      */
82   uint32_t key_notif;    /* BTM_LE_KEY_NOTIF_EVT   */
83                          /* BTM_LE_NC_REQ_EVT */
84                          /* no callback data for
85                           * BTM_LE_KEY_REQ_EVT
86                           * and BTM_LE_OOB_REQ_EVT  */
87   tBTM_LE_COMPLT complt; /* BTM_LE_COMPLT_EVT      */
88   tSMP_OOB_DATA_TYPE req_oob_type;
89   tBTM_LE_KEY key;
90   tSMP_LOC_OOB_DATA local_oob_data;
91   RawAddress id_addr;
92 } tBTM_LE_EVT_DATA;
93 
94 /* Simple Pairing Events.  Called by the stack when Simple Pairing related
95  * events occur.
96  */
97 typedef uint8_t(tBTM_LE_CALLBACK)(tBTM_LE_EVT event, const RawAddress& bda,
98                                   tBTM_LE_EVT_DATA* p_data);
99 
100 #define BTM_BLE_KEY_TYPE_ID 1
101 #define BTM_BLE_KEY_TYPE_ER 2
102 #define BTM_BLE_KEY_TYPE_COUNTER 3  // tobe obsolete
103 
104 typedef struct {
105   Octet16 ir;
106   Octet16 irk;
107   Octet16 dhk;
108 
109 } tBTM_BLE_LOCAL_ID_KEYS;
110 
111 typedef union {
112   tBTM_BLE_LOCAL_ID_KEYS id_keys;
113   Octet16 er;
114 } tBTM_BLE_LOCAL_KEYS;
115 
116 /* New LE identity key for local device.
117  */
118 typedef void(tBTM_LE_KEY_CALLBACK)(uint8_t key_type,
119                                    tBTM_BLE_LOCAL_KEYS* p_key);
120 
121