1 /******************************************************************************
2  *
3  *  Copyright 1999-2012 Broadcom Corporation
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 #ifndef SMP_API_TYPES_H
20 #define SMP_API_TYPES_H
21 
22 #include "bt_target.h"  // Must be first to define build configuration
23 
24 #include "stack/include/btm_status.h"
25 #include "types/ble_address_with_type.h"
26 
27 /* SMP command code */
28 typedef enum : uint8_t {
29   SMP_OPCODE_PAIRING_REQ = 0x01,
30   SMP_OPCODE_PAIRING_RSP = 0x02,
31   SMP_OPCODE_CONFIRM = 0x03,
32   SMP_OPCODE_RAND = 0x04,
33   SMP_OPCODE_PAIRING_FAILED = 0x05,
34   SMP_OPCODE_ENCRYPT_INFO = 0x06,
35   SMP_OPCODE_CENTRAL_ID = 0x07,
36   SMP_OPCODE_IDENTITY_INFO = 0x08,
37   SMP_OPCODE_ID_ADDR = 0x09,
38   SMP_OPCODE_SIGN_INFO = 0x0A,
39   SMP_OPCODE_SEC_REQ = 0x0B,
40   SMP_OPCODE_PAIR_PUBLIC_KEY = 0x0C,
41   SMP_OPCODE_PAIR_DHKEY_CHECK = 0x0D,
42   SMP_OPCODE_PAIR_KEYPR_NOTIF = 0x0E,
43   SMP_OPCODE_MAX = SMP_OPCODE_PAIR_KEYPR_NOTIF,
44   SMP_OPCODE_MIN = SMP_OPCODE_PAIRING_REQ,
45   // NOTE: For some reason this is outside the MAX/MIN values
46   SMP_OPCODE_PAIR_COMMITM = 0x0F,
47 } tSMP_OPCODE;
48 
49 #define CASE_RETURN_TEXT(code) \
50   case code:                   \
51     return #code
52 
smp_opcode_text(const tSMP_OPCODE & opcode)53 inline std::string smp_opcode_text(const tSMP_OPCODE& opcode) {
54   switch (opcode) {
55     CASE_RETURN_TEXT(SMP_OPCODE_PAIRING_REQ);
56     CASE_RETURN_TEXT(SMP_OPCODE_PAIRING_RSP);
57     CASE_RETURN_TEXT(SMP_OPCODE_CONFIRM);
58     CASE_RETURN_TEXT(SMP_OPCODE_RAND);
59     CASE_RETURN_TEXT(SMP_OPCODE_PAIRING_FAILED);
60     CASE_RETURN_TEXT(SMP_OPCODE_ENCRYPT_INFO);
61     CASE_RETURN_TEXT(SMP_OPCODE_CENTRAL_ID);
62     CASE_RETURN_TEXT(SMP_OPCODE_IDENTITY_INFO);
63     CASE_RETURN_TEXT(SMP_OPCODE_ID_ADDR);
64     CASE_RETURN_TEXT(SMP_OPCODE_SIGN_INFO);
65     CASE_RETURN_TEXT(SMP_OPCODE_SEC_REQ);
66     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_PUBLIC_KEY);
67     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_DHKEY_CHECK);
68     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_KEYPR_NOTIF);
69     CASE_RETURN_TEXT(SMP_OPCODE_PAIR_COMMITM);
70     default:
71       return std::string("UNKNOWN:%hhu", opcode);
72   }
73 }
74 #undef CASE_RETURN_TEXT
75 
76 /* SMP event type */
77 typedef enum : uint8_t {
78   SMP_EVT_NONE = 0,           /* Default no event */
79   SMP_IO_CAP_REQ_EVT = 1,     /* IO capability request event */
80   SMP_SEC_REQUEST_EVT = 2,    /* SMP pairing request */
81   SMP_PASSKEY_NOTIF_EVT = 3,  /* passkey notification event */
82   SMP_PASSKEY_REQ_EVT = 4,    /* passkey request event */
83   SMP_OOB_REQ_EVT = 5,        /* OOB request event */
84   SMP_NC_REQ_EVT = 6,         /* Numeric Comparison request event */
85   SMP_COMPLT_EVT = 7,         /* SMP complete event */
86   SMP_PEER_KEYPR_NOT_EVT = 8, /* Peer keypress notification */
87 
88   /* SC OOB request event (both local and peer OOB data can be expected in
89    * response) */
90   SMP_SC_OOB_REQ_EVT = 9,
91   /* SC OOB local data set is created (as result of SMP_CrLocScOobData(...)) */
92   SMP_SC_LOC_OOB_DATA_UP_EVT = 10,
93   SMP_UNUSED11 = 11,
94   SMP_BR_KEYS_REQ_EVT = 12, /* SMP over BR keys request event */
95   SMP_UNUSED13 = 13,
96   SMP_CONSENT_REQ_EVT = 14, /* Consent request event */
97 } tSMP_EVT;
98 
99 /* pairing failure reason code */
100 typedef enum : uint8_t {
101   SMP_SUCCESS = 0,
102   SMP_PASSKEY_ENTRY_FAIL = 0x01,
103   SMP_OOB_FAIL = 0x02,
104   SMP_PAIR_AUTH_FAIL = 0x03,
105   SMP_CONFIRM_VALUE_ERR = 0x04,
106   SMP_PAIR_NOT_SUPPORT = 0x05,
107   SMP_ENC_KEY_SIZE = 0x06,
108   SMP_INVALID_CMD = 0x07,
109   SMP_PAIR_FAIL_UNKNOWN = 0x08,
110   SMP_REPEATED_ATTEMPTS = 0x09,
111   SMP_INVALID_PARAMETERS = 0x0A,
112   SMP_DHKEY_CHK_FAIL = 0x0B,
113   SMP_NUMERIC_COMPAR_FAIL = 0x0C,
114   SMP_BR_PARING_IN_PROGR = 0x0D,
115   SMP_XTRANS_DERIVE_NOT_ALLOW = 0x0E,
116   SMP_MAX_FAIL_RSN_PER_SPEC = SMP_XTRANS_DERIVE_NOT_ALLOW,
117 
118   /* self defined error code */
119   SMP_PAIR_INTERNAL_ERR = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x01), /* 0x0F */
120 
121   /* Unknown IO capability, unable to decide association model */
122   SMP_UNKNOWN_IO_CAP = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x02), /* 0x10 */
123 
124   SMP_BUSY = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x05),        /* 0x13 */
125   SMP_ENC_FAIL = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x06),    /* 0x14 */
126   SMP_STARTED = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x07),     /* 0x15 */
127   SMP_RSP_TIMEOUT = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x08), /* 0x16 */
128 
129   /* Unspecified failure reason */
130   SMP_FAIL = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x0A), /* 0x18 */
131 
132   SMP_CONN_TOUT = (SMP_MAX_FAIL_RSN_PER_SPEC + 0x0B), /* 0x19 */
133 } tSMP_STATUS;
134 
135 /* Device IO capability */
136 #define SMP_IO_CAP_IO BTM_IO_CAP_IO         /* DisplayYesNo */
137 #define SMP_IO_CAP_KBDISP BTM_IO_CAP_KBDISP /* Keyboard Display */
138 #define SMP_IO_CAP_MAX BTM_IO_CAP_MAX
139 typedef uint8_t tSMP_IO_CAP;
140 
141 /* OOB data present or not */
142 enum { SMP_OOB_NONE, SMP_OOB_PRESENT, SMP_OOB_UNKNOWN };
143 typedef uint8_t tSMP_OOB_FLAG;
144 
145 /* type of OOB data required from application */
146 enum { SMP_OOB_INVALID_TYPE, SMP_OOB_PEER, SMP_OOB_LOCAL, SMP_OOB_BOTH };
147 typedef uint8_t tSMP_OOB_DATA_TYPE;
148 
149 enum : uint8_t {
150   SMP_AUTH_NO_BOND = 0x00,
151   /* no MITM, No Bonding, encryption only */
152   SMP_AUTH_NB_ENC_ONLY = 0x00,  //(SMP_AUTH_MASK | BTM_AUTH_SP_NO)
153   SMP_AUTH_BOND = (1u << 0),
154   SMP_AUTH_UNUSED = (1u << 1),
155   /* SMP Authentication requirement */
156   SMP_AUTH_YN_BIT = (1u << 2),
157   SMP_SC_SUPPORT_BIT = (1u << 3),
158   SMP_KP_SUPPORT_BIT = (1u << 4),
159   SMP_H7_SUPPORT_BIT = (1u << 5),
160 };
161 
162 #define SMP_AUTH_MASK                                                          \
163   (SMP_AUTH_BOND | SMP_AUTH_YN_BIT | SMP_SC_SUPPORT_BIT | SMP_KP_SUPPORT_BIT | \
164    SMP_H7_SUPPORT_BIT)
165 
166 /* Secure Connections, no MITM, no Bonding */
167 #define SMP_AUTH_SC_ENC_ONLY (SMP_H7_SUPPORT_BIT | SMP_SC_SUPPORT_BIT)
168 
169 /* Secure Connections, MITM, Bonding */
170 #define SMP_AUTH_SC_MITM_GB \
171   (SMP_H7_SUPPORT_BIT | SMP_SC_SUPPORT_BIT | SMP_AUTH_YN_BIT | SMP_AUTH_BOND)
172 
173 typedef uint8_t tSMP_AUTH_REQ;
174 
175 #define SMP_SEC_NONE 0
176 #define SMP_SEC_UNAUTHENTICATE (1 << 0)
177 #define SMP_SEC_AUTHENTICATED (1 << 2)
178 typedef uint8_t tSMP_SEC_LEVEL;
179 
180 /* Maximum Encryption Key Size range */
181 #define SMP_ENCR_KEY_SIZE_MIN 7
182 #define SMP_ENCR_KEY_SIZE_MAX 16
183 
184 /* SMP key types */
185 #define SMP_SEC_KEY_TYPE_ENC (1 << 0)  /* encryption key */
186 #define SMP_SEC_KEY_TYPE_ID (1 << 1)   /* identity key */
187 #define SMP_SEC_KEY_TYPE_CSRK (1 << 2) /* peripheral CSRK */
188 #define SMP_SEC_KEY_TYPE_LK (1 << 3)   /* BR/EDR link key */
189 typedef uint8_t tSMP_KEYS;
190 
191 #define SMP_BR_SEC_DEFAULT_KEY \
192   (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK)
193 
194 /* default security key distribution value */
195 #define SMP_SEC_DEFAULT_KEY                                             \
196   (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | SMP_SEC_KEY_TYPE_CSRK | \
197    SMP_SEC_KEY_TYPE_LK)
198 
199 #define SMP_SC_KEY_OUT_OF_RANGE 5 /* out of range */
200 typedef uint8_t tSMP_SC_KEY_TYPE;
201 
202 /* data type for BTM_SP_IO_REQ_EVT */
203 typedef struct {
204   tSMP_IO_CAP io_cap;     /* local IO capabilities */
205   tSMP_OOB_FLAG oob_data; /* OOB data present (locally) for the peer device */
206   tSMP_AUTH_REQ auth_req; /* Authentication required (for local device) */
207   uint8_t max_key_size;   /* max encryption key size */
208   tSMP_KEYS init_keys;    /* initiator keys to be distributed */
209   tSMP_KEYS resp_keys;    /* responder keys */
210 } tSMP_IO_REQ;
211 
212 typedef struct {
213   tSMP_STATUS reason;
214   tSMP_SEC_LEVEL sec_level;
215   bool is_pair_cancel;
216   bool smp_over_br;
217 } tSMP_CMPL;
218 
219 typedef struct {
220   BT_OCTET32 x;
221   BT_OCTET32 y;
222 } tSMP_PUBLIC_KEY;
223 
224 /* the data associated with the info sent to the peer via OOB interface */
225 typedef struct {
226   bool present;
227   Octet16 randomizer;
228   Octet16 commitment;
229 
230   tBLE_BD_ADDR addr_sent_to;
231   BT_OCTET32 private_key_used; /* is used to calculate: */
232   /* publ_key_used = P-256(private_key_used, curve_p256.G) - send it to the */
233   /* other side */
234   /* dhkey = P-256(private_key_used, publ key rcvd from the other side) */
235   tSMP_PUBLIC_KEY publ_key_used; /* P-256(private_key_used, curve_p256.G) */
236 } tSMP_LOC_OOB_DATA;
237 
238 /* the data associated with the info received from the peer via OOB interface */
239 typedef struct {
240   bool present;
241   Octet16 randomizer;
242   Octet16 commitment;
243   tBLE_BD_ADDR addr_rcvd_from;
244 } tSMP_PEER_OOB_DATA;
245 
246 typedef struct {
247   tSMP_LOC_OOB_DATA loc_oob_data;
248   tSMP_PEER_OOB_DATA peer_oob_data;
249 } tSMP_SC_OOB_DATA;
250 
251 typedef union {
252   uint32_t passkey;
253   tSMP_IO_REQ io_req; /* IO request */
254   tSMP_CMPL cmplt;
255   tSMP_OOB_DATA_TYPE req_oob_type;
256   tSMP_LOC_OOB_DATA loc_oob_data;
257 } tSMP_EVT_DATA;
258 
259 /* AES Encryption output */
260 typedef struct {
261   uint8_t status;
262   uint8_t param_len;
263   uint16_t opcode;
264   uint8_t param_buf[OCTET16_LEN];
265 } tSMP_ENC;
266 
267 /* Security Manager events - Called by the stack when Security Manager related
268  * events occur.*/
269 typedef tBTM_STATUS(tSMP_CALLBACK)(tSMP_EVT event, const RawAddress& bd_addr,
270                                    tSMP_EVT_DATA* p_data);
271 
272 #endif  // SMP_API_TYPES_H
273