1 /******************************************************************************
2  *
3  *  Copyright (C) 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 /******************************************************************************
20  *
21  *  This file contains the SMP API function external definitions.
22  *
23  ******************************************************************************/
24 #ifndef SMP_API_H
25 #define SMP_API_H
26 
27 #include "common/bt_target.h"
28 
29 #define SMP_PIN_CODE_LEN_MAX    PIN_CODE_LEN
30 #define SMP_PIN_CODE_LEN_MIN    6
31 
32 // #if SMP_INCLUDED == TRUE
33 /* SMP command code */
34 #define SMP_OPCODE_PAIRING_REQ            0x01
35 #define SMP_OPCODE_PAIRING_RSP            0x02
36 #define SMP_OPCODE_CONFIRM                0x03
37 #define SMP_OPCODE_RAND                   0x04
38 #define SMP_OPCODE_PAIRING_FAILED         0x05
39 #define SMP_OPCODE_ENCRYPT_INFO           0x06
40 #define SMP_OPCODE_MASTER_ID              0x07
41 #define SMP_OPCODE_IDENTITY_INFO          0x08
42 #define SMP_OPCODE_ID_ADDR                0x09
43 #define SMP_OPCODE_SIGN_INFO              0x0A
44 #define SMP_OPCODE_SEC_REQ                0x0B
45 #define SMP_OPCODE_PAIR_PUBLIC_KEY        0x0C
46 #define SMP_OPCODE_PAIR_DHKEY_CHECK       0x0D
47 #define SMP_OPCODE_PAIR_KEYPR_NOTIF       0x0E
48 #define SMP_OPCODE_MAX                    SMP_OPCODE_PAIR_KEYPR_NOTIF
49 #define SMP_OPCODE_MIN                    SMP_OPCODE_PAIRING_REQ
50 #define SMP_OPCODE_PAIR_COMMITM           0x0F
51 // #endif
52 
53 /* SMP event type */
54 #define SMP_IO_CAP_REQ_EVT      1       /* IO capability request event */
55 #define SMP_SEC_REQUEST_EVT     2       /* SMP pairing request */
56 #define SMP_PASSKEY_NOTIF_EVT   3       /* passkey notification event */
57 #define SMP_PASSKEY_REQ_EVT     4       /* passkey request event */
58 #define SMP_OOB_REQ_EVT         5       /* OOB request event */
59 #define SMP_NC_REQ_EVT          6       /* Numeric Comparison request event */
60 #define SMP_COMPLT_EVT          7       /* SMP complete event */
61 #define SMP_PEER_KEYPR_NOT_EVT  8       /* Peer keypress notification received event */
62 #define SMP_SC_OOB_REQ_EVT      9       /* SC OOB request event (both local and peer OOB data */
63 /* can be expected in response) */
64 #define SMP_SC_LOC_OOB_DATA_UP_EVT  10  /* SC OOB local data set is created */
65 /* (as result of SMP_CrLocScOobData(...)) */
66 #define SMP_BR_KEYS_REQ_EVT     12      /* SMP over BR keys request event */
67 typedef UINT8   tSMP_EVT;
68 
69 
70 /* pairing failure reason code */
71 #define SMP_PASSKEY_ENTRY_FAIL      0x01
72 #define SMP_OOB_FAIL                0x02
73 #define SMP_PAIR_AUTH_FAIL          0x03
74 #define SMP_CONFIRM_VALUE_ERR       0x04
75 #define SMP_PAIR_NOT_SUPPORT        0x05
76 #define SMP_ENC_KEY_SIZE            0x06
77 #define SMP_INVALID_CMD             0x07
78 #define SMP_PAIR_FAIL_UNKNOWN       0x08
79 #define SMP_REPEATED_ATTEMPTS       0x09
80 #define SMP_INVALID_PARAMETERS      0x0A
81 #define SMP_DHKEY_CHK_FAIL          0x0B
82 #define SMP_NUMERIC_COMPAR_FAIL     0x0C
83 #define SMP_BR_PARING_IN_PROGR      0x0D
84 #define SMP_XTRANS_DERIVE_NOT_ALLOW 0x0E
85 #define SMP_MAX_FAIL_RSN_PER_SPEC   SMP_XTRANS_DERIVE_NOT_ALLOW
86 
87 /* self defined error code */
88 #define SMP_PAIR_INTERNAL_ERR       (SMP_MAX_FAIL_RSN_PER_SPEC + 0x01) /* 0x0F */
89 
90 /* 0x0F unknown IO capability, unable to decide association model */
91 #define SMP_UNKNOWN_IO_CAP          (SMP_MAX_FAIL_RSN_PER_SPEC + 0x02) /* 0x10 */
92 
93 #define SMP_INIT_FAIL               (SMP_MAX_FAIL_RSN_PER_SPEC + 0x03) /* 0x11 */
94 #define SMP_CONFIRM_FAIL            (SMP_MAX_FAIL_RSN_PER_SPEC + 0x04) /* 0x12 */
95 #define SMP_BUSY                    (SMP_MAX_FAIL_RSN_PER_SPEC + 0x05) /* 0x13 */
96 #define SMP_ENC_FAIL                (SMP_MAX_FAIL_RSN_PER_SPEC + 0x06) /* 0x14 */
97 #define SMP_STARTED                 (SMP_MAX_FAIL_RSN_PER_SPEC + 0x07) /* 0x15 */
98 #define SMP_RSP_TIMEOUT             (SMP_MAX_FAIL_RSN_PER_SPEC + 0x08) /* 0x16 */
99 #define SMP_DIV_NOT_AVAIL           (SMP_MAX_FAIL_RSN_PER_SPEC + 0x09) /* 0x17 */
100 
101 /* 0x17 unspecified failed reason */
102 #define SMP_FAIL                    (SMP_MAX_FAIL_RSN_PER_SPEC + 0x0A) /* 0x18 */
103 
104 #define SMP_CONN_TOUT               (SMP_MAX_FAIL_RSN_PER_SPEC + 0x0B) /* 0x19 */
105 #define SMP_SUCCESS                 0
106 
107 typedef UINT8 tSMP_STATUS;
108 
109 
110 /* Device IO capability */
111 #define SMP_IO_CAP_OUT      BTM_IO_CAP_OUT   /* DisplayOnly */
112 #define SMP_IO_CAP_IO       BTM_IO_CAP_IO   /* DisplayYesNo */
113 #define SMP_IO_CAP_IN       BTM_IO_CAP_IN   /* KeyboardOnly */
114 #define SMP_IO_CAP_NONE     BTM_IO_CAP_NONE   /* NoInputNoOutput */
115 #define SMP_IO_CAP_KBDISP   BTM_IO_CAP_KBDISP   /* Keyboard Display */
116 #define SMP_IO_CAP_MAX      BTM_IO_CAP_MAX
117 typedef UINT8  tSMP_IO_CAP;
118 
119 #ifndef SMP_DEFAULT_IO_CAPS
120 #define SMP_DEFAULT_IO_CAPS     SMP_IO_CAP_KBDISP
121 #endif
122 
123 /* OOB data present or not */
124 enum {
125     SMP_OOB_NONE,
126     SMP_OOB_PRESENT,
127     SMP_OOB_UNKNOWN
128 };
129 typedef UINT8  tSMP_OOB_FLAG;
130 
131 /* type of OOB data required from application */
132 enum {
133     SMP_OOB_INVALID_TYPE,
134     SMP_OOB_PEER,
135     SMP_OOB_LOCAL,
136     SMP_OOB_BOTH
137 };
138 typedef UINT8   tSMP_OOB_DATA_TYPE;
139 
140 #define SMP_AUTH_NO_BOND        0x00
141 #define SMP_AUTH_GEN_BOND       0x01 //todo sdh change GEN_BOND to BOND
142 
143 /* SMP Authentication requirement */
144 #define SMP_AUTH_YN_BIT         (1 << 2)
145 #define SMP_SC_SUPPORT_BIT      (1 << 3)
146 #define SMP_KP_SUPPORT_BIT      (1 << 4)
147 
148 #define SMP_AUTH_MASK    (SMP_AUTH_GEN_BOND|SMP_AUTH_YN_BIT|SMP_SC_SUPPORT_BIT|SMP_KP_SUPPORT_BIT)
149 
150 #define SMP_AUTH_BOND           SMP_AUTH_GEN_BOND
151 
152 /* no MITM, No Bonding, encryption only */
153 #define SMP_AUTH_NB_ENC_ONLY    0x00 //(SMP_AUTH_MASK | BTM_AUTH_SP_NO)
154 
155 /* MITM, No Bonding, Use IO Capability to determine authentication procedure */
156 #define SMP_AUTH_NB_IOCAP       (SMP_AUTH_NO_BOND | SMP_AUTH_YN_BIT)
157 
158 /* No MITM, General Bonding, Encryption only */
159 #define SMP_AUTH_GB_ENC_ONLY    (SMP_AUTH_GEN_BOND )
160 
161 /* MITM, General Bonding, Use IO Capability to determine authentication procedure */
162 #define SMP_AUTH_GB_IOCAP       (SMP_AUTH_GEN_BOND | SMP_AUTH_YN_BIT)
163 
164 /* Secure Connections, no MITM, no Bonding */
165 #define SMP_AUTH_SC_ENC_ONLY    (SMP_SC_SUPPORT_BIT)
166 
167 /* Secure Connections, no MITM, Bonding */
168 #define SMP_AUTH_SC_GB          (SMP_SC_SUPPORT_BIT | SMP_AUTH_GEN_BOND)
169 
170 /* Secure Connections, MITM, no Bonding */
171 #define SMP_AUTH_SC_MITM_NB     (SMP_SC_SUPPORT_BIT | SMP_AUTH_YN_BIT | SMP_AUTH_NO_BOND)
172 
173 /* Secure Connections, MITM, Bonding */
174 #define SMP_AUTH_SC_MITM_GB     (SMP_SC_SUPPORT_BIT | SMP_AUTH_YN_BIT | SMP_AUTH_GEN_BOND)
175 
176 /* All AuthReq RFU bits are set to 1 - NOTE: reserved bit in Bonding_Flags is not set */
177 #define SMP_AUTH_ALL_RFU_SET    0xF8
178 
179 typedef UINT8 tSMP_AUTH_REQ;
180 
181 #define SMP_SEC_NONE                 0
182 #define SMP_SEC_UNAUTHENTICATE      (1 << 0)
183 #define SMP_SEC_AUTHENTICATED       (1 << 2)
184 typedef UINT8 tSMP_SEC_LEVEL;
185 
186 /* Maximum Encryption Key Size range */
187 #define SMP_ENCR_KEY_SIZE_MIN       7
188 #define SMP_ENCR_KEY_SIZE_MAX       16
189 
190 /* SMP key types */
191 #define SMP_SEC_KEY_TYPE_ENC                (1 << 0)    /* encryption key */
192 #define SMP_SEC_KEY_TYPE_ID                 (1 << 1)    /* identity key */
193 #define SMP_SEC_KEY_TYPE_CSRK               (1 << 2)    /* slave CSRK */
194 #define SMP_SEC_KEY_TYPE_LK                 (1 << 3)    /* BR/EDR link key */
195 typedef UINT8 tSMP_KEYS;
196 
197 #define SMP_BR_SEC_DEFAULT_KEY   (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | \
198                                   SMP_SEC_KEY_TYPE_CSRK)
199 
200 /* default security key distribution value */
201 #define SMP_SEC_DEFAULT_KEY      (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | \
202                                   SMP_SEC_KEY_TYPE_CSRK | SMP_SEC_KEY_TYPE_LK)
203 
204 #define SMP_SC_KEY_STARTED      0   /* passkey entry started */
205 #define SMP_SC_KEY_ENTERED      1   /* passkey digit entered */
206 #define SMP_SC_KEY_ERASED       2   /* passkey digit erased */
207 #define SMP_SC_KEY_CLEARED      3   /* passkey cleared */
208 #define SMP_SC_KEY_COMPLT       4   /* passkey entry completed */
209 #define SMP_SC_KEY_OUT_OF_RANGE 5   /* out of range */
210 typedef UINT8 tSMP_SC_KEY_TYPE;
211 
212 /* data type for BTM_SP_IO_REQ_EVT */
213 typedef struct {
214     tSMP_IO_CAP     io_cap;         /* local IO capabilities */
215     tSMP_OOB_FLAG   oob_data;       /* OOB data present (locally) for the peer device */
216     tSMP_AUTH_REQ   auth_req;       /* Authentication required (for local device) */
217     UINT8           max_key_size;   /* max encryption key size */
218     tSMP_KEYS       init_keys;      /* initiator keys to be distributed */
219     tSMP_KEYS       resp_keys;      /* responder keys */
220 } tSMP_IO_REQ;
221 
222 typedef struct {
223     tSMP_STATUS reason;
224     tSMP_SEC_LEVEL sec_level;
225     BOOLEAN is_pair_cancel;
226     BOOLEAN smp_over_br;
227     tSMP_AUTH_REQ auth_mode;
228 } tSMP_CMPL;
229 
230 typedef struct {
231     BT_OCTET32  x;
232     BT_OCTET32  y;
233 } tSMP_PUBLIC_KEY;
234 
235 /* the data associated with the info sent to the peer via OOB interface */
236 typedef struct {
237     BOOLEAN         present;
238     BT_OCTET16      randomizer;
239     BT_OCTET16      commitment;
240 
241     tBLE_BD_ADDR    addr_sent_to;
242     BT_OCTET32      private_key_used;   /* is used to calculate: */
243     /* publ_key_used = P-256(private_key_used, curve_p256.G) - send it to the */
244     /* other side */
245     /* dhkey = P-256(private_key_used, publ key rcvd from the other side) */
246     tSMP_PUBLIC_KEY publ_key_used; /* P-256(private_key_used, curve_p256.G) */
247 } tSMP_LOC_OOB_DATA;
248 
249 /* the data associated with the info received from the peer via OOB interface */
250 typedef struct {
251     BOOLEAN         present;
252     BT_OCTET16      randomizer;
253     BT_OCTET16      commitment;
254     tBLE_BD_ADDR    addr_rcvd_from;
255 } tSMP_PEER_OOB_DATA;
256 
257 typedef struct {
258     tSMP_LOC_OOB_DATA   loc_oob_data;
259     tSMP_PEER_OOB_DATA  peer_oob_data;
260 } tSMP_SC_OOB_DATA;
261 
262 
263 typedef union {
264     UINT32          passkey;
265     tSMP_IO_REQ     io_req;     /* IO request */
266     tSMP_CMPL       cmplt;
267     tSMP_OOB_DATA_TYPE  req_oob_type;
268     tSMP_LOC_OOB_DATA   loc_oob_data;
269 } tSMP_EVT_DATA;
270 
271 
272 /* AES Encryption output */
273 typedef struct {
274     UINT8   status;
275     UINT8   param_len;
276     UINT16  opcode;
277     UINT8   param_buf[BT_OCTET16_LEN];
278 } tSMP_ENC;
279 
280 /* Security Manager events - Called by the stack when Security Manager related events occur.*/
281 typedef UINT8 (tSMP_CALLBACK) (tSMP_EVT event, BD_ADDR bd_addr, tSMP_EVT_DATA *p_data);
282 
283 /* callback function for CMAC algorithm
284 */
285 typedef void (tCMAC_CMPL_CBACK)(UINT8 *p_mac, UINT16 tlen, UINT32 sign_counter);
286 
287 /*****************************************************************************
288 **  External Function Declarations
289 *****************************************************************************/
290 #ifdef __cplusplus
291 extern "C"
292 {
293 #endif
294 /* API of SMP */
295 
296 /*******************************************************************************
297 **
298 ** Function         SMP_Init
299 **
300 ** Description      This function initializes the SMP unit.
301 **
302 ** Returns          void
303 **
304 *******************************************************************************/
305 extern void SMP_Init(void);
306 
307 /*******************************************************************************
308 **
309 ** Function         SMP_Free
310 **
311 ** Description      This function de initializes the SMP unit.
312 **
313 ** Returns          void
314 **
315 *******************************************************************************/
316 extern void SMP_Free(void);
317 
318 
319 /*******************************************************************************
320 **
321 ** Function         SMP_SetTraceLevel
322 **
323 ** Description      This function sets the trace level for SMP.  If called with
324 **                  a value of 0xFF, it simply returns the current trace level.
325 **
326 ** Returns          The new or current trace level
327 **
328 *******************************************************************************/
329 extern UINT8 SMP_SetTraceLevel (UINT8 new_level);
330 
331 /*******************************************************************************
332 **
333 ** Function         SMP_Register
334 **
335 ** Description      This function register for the SMP service callback.
336 **
337 ** Returns          void
338 **
339 *******************************************************************************/
340 extern BOOLEAN SMP_Register (tSMP_CALLBACK *p_cback);
341 
342 /*******************************************************************************
343 **
344 ** Function         SMP_Pair
345 **
346 ** Description      This function is called to start a SMP pairing.
347 **
348 ** Returns          SMP_STARTED if bond started, else otherwise exception.
349 **
350 *******************************************************************************/
351 extern tSMP_STATUS SMP_Pair (BD_ADDR bd_addr);
352 
353 /*******************************************************************************
354 **
355 ** Function         SMP_BR_PairWith
356 **
357 ** Description      This function is called to start a SMP pairing over BR/EDR.
358 **
359 ** Returns          SMP_STARTED if pairing started, otherwise reason for failure.
360 **
361 *******************************************************************************/
362 extern tSMP_STATUS SMP_BR_PairWith (BD_ADDR bd_addr);
363 
364 /*******************************************************************************
365 **
366 ** Function         SMP_PairCancel
367 **
368 ** Description      This function is called to cancel a SMP pairing.
369 **
370 ** Returns          TRUE - pairing cancelled
371 **
372 *******************************************************************************/
373 extern  BOOLEAN SMP_PairCancel (BD_ADDR bd_addr);
374 
375 /*******************************************************************************
376 **
377 ** Function         SMP_SecurityGrant
378 **
379 ** Description      This function is called to grant security process.
380 **
381 ** Parameters       bd_addr - peer device bd address.
382 **                  res     - result of the operation SMP_SUCCESS if success.
383 **                            Otherwise, SMP_REPEATED_ATTEMPTS is too many attempts.
384 **
385 ** Returns          None
386 **
387 *******************************************************************************/
388 extern void SMP_SecurityGrant(BD_ADDR bd_addr, UINT8 res);
389 
390 /*******************************************************************************
391 **
392 ** Function         SMP_PasskeyReply
393 **
394 ** Description      This function is called after Security Manager submitted
395 **                  Passkey request to the application.
396 **
397 ** Parameters:      bd_addr      - Address of the device for which PIN was requested
398 **                  res          - result of the operation SMP_SUCCESS if success
399 **                  passkey      - numeric value in the range of
400 **                  BTM_MIN_PASSKEY_VAL(0) - BTM_MAX_PASSKEY_VAL(999999(0xF423F)).
401 **
402 *******************************************************************************/
403 extern void SMP_PasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey);
404 
405 /*******************************************************************************
406 **
407 ** Function         SMP_SetStaticPasskey
408 **
409 ** Description      This function is called to set static passkey
410 **
411 **
412 ** Parameters:      add          - set static passkey when add is TRUE
413 **                                 clear static passkey when add is FALSE
414 **                  passkey      - static passkey
415 **
416 **
417 *******************************************************************************/
418 extern void SMP_SetStaticPasskey (BOOLEAN add, UINT32 passkey);
419 
420 /*******************************************************************************
421 **
422 ** Function         SMP_ConfirmReply
423 **
424 ** Description      This function is called after Security Manager submitted
425 **                  numeric comparison request to the application.
426 **
427 ** Parameters:      bd_addr      - Address of the device with which numeric
428 **                                 comparison was requested
429 **                  res          - comparison result SMP_SUCCESS if success
430 **
431 *******************************************************************************/
432 extern void SMP_ConfirmReply (BD_ADDR bd_addr, UINT8 res);
433 
434 /*******************************************************************************
435 **
436 ** Function         SMP_OobDataReply
437 **
438 ** Description      This function is called to provide the OOB data for
439 **                  SMP in response to SMP_OOB_REQ_EVT
440 **
441 ** Parameters:      bd_addr     - Address of the peer device
442 **                  res         - result of the operation SMP_SUCCESS if success
443 **                  p_data      - SM Randomizer  C.
444 **
445 *******************************************************************************/
446 extern void SMP_OobDataReply(BD_ADDR bd_addr, tSMP_STATUS res, UINT8 len,
447                              UINT8 *p_data);
448 
449 /*******************************************************************************
450 **
451 ** Function         SMP_SecureConnectionOobDataReply
452 **
453 ** Description      This function is called to provide the SC OOB data for
454 **                  SMP in response to SMP_SC_OOB_REQ_EVT
455 **
456 ** Parameters:      p_data      - pointer to the data
457 **
458 *******************************************************************************/
459 extern void SMP_SecureConnectionOobDataReply(UINT8 *p_data);
460 
461 /*******************************************************************************
462 **
463 ** Function         SMP_Encrypt
464 **
465 ** Description      This function is called to encrypt the data with the specified
466 **                  key
467 **
468 ** Parameters:      key                 - Pointer to key key[0] conatins the MSB
469 **                  key_len             - key length
470 **                  plain_text          - Pointer to data to be encrypted
471 **                                        plain_text[0] conatins the MSB
472 **                  pt_len              - plain text length
473 **                  p_out               - pointer to the encrypted outputs
474 **
475 **  Returns         Boolean - TRUE: encryption is successful
476 *******************************************************************************/
477 extern BOOLEAN SMP_Encrypt (UINT8 *key, UINT8 key_len,
478                             UINT8 *plain_text, UINT8 pt_len,
479                             tSMP_ENC *p_out);
480 
481 /*******************************************************************************
482 **
483 ** Function         SMP_KeypressNotification
484 **
485 ** Description      This function is called to notify SM about Keypress Notification.
486 **
487 ** Parameters:      bd_addr      - Address of the device to send keypress
488 **                                 notification to
489 **                  value        - keypress notification parameter value
490 **
491 *******************************************************************************/
492 extern void SMP_KeypressNotification (BD_ADDR bd_addr, UINT8 value);
493 
494 /*******************************************************************************
495 **
496 ** Function         SMP_CreateLocalSecureConnectionsOobData
497 **
498 ** Description      This function is called to start creation of local SC OOB
499 **                  data set (tSMP_LOC_OOB_DATA).
500 **
501 **  Returns         Boolean - TRUE: creation of local SC OOB data set started.
502 *******************************************************************************/
503 extern BOOLEAN SMP_CreateLocalSecureConnectionsOobData (void);
504 
505 #ifdef __cplusplus
506 }
507 #endif
508 #endif /* SMP_API_H */
509