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 the SMP API function external definitions.
23 ******************************************************************************/
27 #include "bt_target.h"
29 #define SMP_PIN_CODE_LEN_MAX PIN_CODE_LEN
30 #define SMP_PIN_CODE_LEN_MIN 6
32 #if BLE_INCLUDED == TRUE && 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
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;
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
87 /* self defined error code */
88 #define SMP_PAIR_INTERNAL_ERR (SMP_MAX_FAIL_RSN_PER_SPEC + 0x01) /* 0x0E */
90 /* 0x0F unknown IO capability, unable to decide association model */
91 #define SMP_UNKNOWN_IO_CAP (SMP_MAX_FAIL_RSN_PER_SPEC + 0x02) /* 0x0F */
93 #define SMP_INIT_FAIL (SMP_MAX_FAIL_RSN_PER_SPEC + 0x03) /* 0x10 */
94 #define SMP_CONFIRM_FAIL (SMP_MAX_FAIL_RSN_PER_SPEC + 0x04) /* 0x11 */
95 #define SMP_BUSY (SMP_MAX_FAIL_RSN_PER_SPEC + 0x05) /* 0x12 */
96 #define SMP_ENC_FAIL (SMP_MAX_FAIL_RSN_PER_SPEC + 0x06) /* 0x13 */
97 #define SMP_STARTED (SMP_MAX_FAIL_RSN_PER_SPEC + 0x07) /* 0x14 */
98 #define SMP_RSP_TIMEOUT (SMP_MAX_FAIL_RSN_PER_SPEC + 0x08) /* 0x15 */
99 #define SMP_DIV_NOT_AVAIL (SMP_MAX_FAIL_RSN_PER_SPEC + 0x09) /* 0x16 */
101 /* 0x17 unspecified failed reason */
102 #define SMP_FAIL (SMP_MAX_FAIL_RSN_PER_SPEC + 0x0A) /* 0x17 */
104 #define SMP_CONN_TOUT (SMP_MAX_FAIL_RSN_PER_SPEC + 0x0B)
105 #define SMP_SUCCESS 0
107 typedef UINT8 tSMP_STATUS;
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;
119 #ifndef SMP_DEFAULT_IO_CAPS
120 #define SMP_DEFAULT_IO_CAPS SMP_IO_CAP_KBDISP
123 /* OOB data present or not */
130 typedef UINT8 tSMP_OOB_FLAG;
132 /* type of OOB data required from application */
135 SMP_OOB_INVALID_TYPE,
140 typedef UINT8 tSMP_OOB_DATA_TYPE;
142 #define SMP_AUTH_NO_BOND 0x00
143 #define SMP_AUTH_GEN_BOND 0x01 //todo sdh change GEN_BOND to BOND
145 /* SMP Authentication requirement */
146 #define SMP_AUTH_YN_BIT (1 << 2)
147 #define SMP_SC_SUPPORT_BIT (1 << 3)
148 #define SMP_KP_SUPPORT_BIT (1 << 4)
150 #define SMP_AUTH_MASK (SMP_AUTH_GEN_BOND|SMP_AUTH_YN_BIT|SMP_SC_SUPPORT_BIT|SMP_KP_SUPPORT_BIT)
152 #define SMP_AUTH_BOND SMP_AUTH_GEN_BOND
154 /* no MITM, No Bonding, encryption only */
155 #define SMP_AUTH_NB_ENC_ONLY 0x00 //(SMP_AUTH_MASK | BTM_AUTH_SP_NO)
157 /* MITM, No Bonding, Use IO Capability to determine authentication procedure */
158 #define SMP_AUTH_NB_IOCAP (SMP_AUTH_NO_BOND | SMP_AUTH_YN_BIT)
160 /* No MITM, General Bonding, Encryption only */
161 #define SMP_AUTH_GB_ENC_ONLY (SMP_AUTH_GEN_BOND )
163 /* MITM, General Bonding, Use IO Capability to determine authentication procedure */
164 #define SMP_AUTH_GB_IOCAP (SMP_AUTH_GEN_BOND | SMP_AUTH_YN_BIT)
166 /* Secure Connections, no MITM, no Bonding */
167 #define SMP_AUTH_SC_ENC_ONLY (SMP_SC_SUPPORT_BIT)
169 /* Secure Connections, no MITM, Bonding */
170 #define SMP_AUTH_SC_GB (SMP_SC_SUPPORT_BIT | SMP_AUTH_GEN_BOND)
172 /* Secure Connections, MITM, no Bonding */
173 #define SMP_AUTH_SC_MITM_NB (SMP_SC_SUPPORT_BIT | SMP_AUTH_YN_BIT | SMP_AUTH_NO_BOND)
175 /* Secure Connections, MITM, Bonding */
176 #define SMP_AUTH_SC_MITM_GB (SMP_SC_SUPPORT_BIT | SMP_AUTH_YN_BIT | SMP_AUTH_GEN_BOND)
178 /* All AuthReq RFU bits are set to 1 - NOTE: reserved bit in Bonding_Flags is not set */
179 #define SMP_AUTH_ALL_RFU_SET 0xF8
181 typedef UINT8 tSMP_AUTH_REQ;
183 #define SMP_SEC_NONE 0
184 #define SMP_SEC_UNAUTHENTICATE (1 << 0)
185 #define SMP_SEC_AUTHENTICATED (1 << 2)
186 typedef UINT8 tSMP_SEC_LEVEL;
188 /* Maximum Encryption Key Size range */
189 #define SMP_ENCR_KEY_SIZE_MIN 7
190 #define SMP_ENCR_KEY_SIZE_MAX 16
193 #define SMP_SEC_KEY_TYPE_ENC (1 << 0) /* encryption key */
194 #define SMP_SEC_KEY_TYPE_ID (1 << 1) /* identity key */
195 #define SMP_SEC_KEY_TYPE_CSRK (1 << 2) /* slave CSRK */
196 #define SMP_SEC_KEY_TYPE_LK (1 << 3) /* BR/EDR link key */
197 typedef UINT8 tSMP_KEYS;
199 #define SMP_BR_SEC_DEFAULT_KEY (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | \
200 SMP_SEC_KEY_TYPE_CSRK)
202 /* default security key distribution value */
203 #define SMP_SEC_DEFAULT_KEY (SMP_SEC_KEY_TYPE_ENC | SMP_SEC_KEY_TYPE_ID | \
204 SMP_SEC_KEY_TYPE_CSRK | SMP_SEC_KEY_TYPE_LK)
206 /* data type for BTM_SP_IO_REQ_EVT */
209 tSMP_IO_CAP io_cap; /* local IO capabilities */
210 tSMP_OOB_FLAG oob_data; /* OOB data present (locally) for the peer device */
211 tSMP_AUTH_REQ auth_req; /* Authentication required (for local device) */
212 UINT8 max_key_size; /* max encryption key size */
213 tSMP_KEYS init_keys; /* initiator keys to be distributed */
214 tSMP_KEYS resp_keys; /* responder keys */
220 tSMP_SEC_LEVEL sec_level;
221 BOOLEAN is_pair_cancel;
230 /* the data associated with the info sent to the peer via OOB interface */
234 BT_OCTET16 randomizer;
235 BT_OCTET16 commitment;
237 tBLE_BD_ADDR addr_sent_to;
238 BT_OCTET32 private_key_used; /* is used to calculate: */
239 /* publ_key_used = P-256(private_key_used, curve_p256.G) - send it to the */
241 /* dhkey = P-256(private_key_used, publ key rcvd from the other side) */
242 tSMP_PUBLIC_KEY publ_key_used; /* P-256(private_key_used, curve_p256.G) */
245 /* the data associated with the info received from the peer via OOB interface */
249 BT_OCTET16 randomizer;
250 BT_OCTET16 commitment;
251 tBLE_BD_ADDR addr_rcvd_from;
252 } tSMP_PEER_OOB_DATA;
256 tSMP_LOC_OOB_DATA loc_oob_data;
257 tSMP_PEER_OOB_DATA peer_oob_data;
264 tSMP_IO_REQ io_req; /* IO request */
266 tSMP_OOB_DATA_TYPE req_oob_type;
267 tSMP_LOC_OOB_DATA loc_oob_data;
271 /* AES Encryption output */
277 UINT8 param_buf[BT_OCTET16_LEN];
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);
283 /* callback function for CMAC algorithm
285 typedef void (tCMAC_CMPL_CBACK)(UINT8 *p_mac, UINT16 tlen, UINT32 sign_counter);
287 /*****************************************************************************
288 ** External Function Declarations
289 *****************************************************************************/
296 /*******************************************************************************
300 ** Description This function initializes the SMP unit.
304 *******************************************************************************/
305 extern void SMP_Init(void);
307 /*******************************************************************************
309 ** Function SMP_SetTraceLevel
311 ** Description This function sets the trace level for SMP. If called with
312 ** a value of 0xFF, it simply returns the current trace level.
314 ** Returns The new or current trace level
316 *******************************************************************************/
317 extern UINT8 SMP_SetTraceLevel (UINT8 new_level);
319 /*******************************************************************************
321 ** Function SMP_Register
323 ** Description This function register for the SMP service callback.
327 *******************************************************************************/
328 extern BOOLEAN SMP_Register (tSMP_CALLBACK *p_cback);
330 /*******************************************************************************
334 ** Description This function is called to start a SMP pairing.
336 ** Returns SMP_STARTED if bond started, else otherwise exception.
338 *******************************************************************************/
339 extern tSMP_STATUS SMP_Pair (BD_ADDR bd_addr);
341 /*******************************************************************************
343 ** Function SMP_BR_PairWith
345 ** Description This function is called to start a SMP pairing over BR/EDR.
347 ** Returns SMP_STARTED if pairing started, otherwise reason for failure.
349 *******************************************************************************/
350 extern tSMP_STATUS SMP_BR_PairWith (BD_ADDR bd_addr);
352 /*******************************************************************************
354 ** Function SMP_PairCancel
356 ** Description This function is called to cancel a SMP pairing.
358 ** Returns TRUE - pairing cancelled
360 *******************************************************************************/
361 extern BOOLEAN SMP_PairCancel (BD_ADDR bd_addr);
363 /*******************************************************************************
365 ** Function SMP_SecurityGrant
367 ** Description This function is called to grant security process.
369 ** Parameters bd_addr - peer device bd address.
370 ** res - result of the operation SMP_SUCCESS if success.
371 ** Otherwise, SMP_REPEATED_ATTEMPTS is too many attempts.
375 *******************************************************************************/
376 extern void SMP_SecurityGrant(BD_ADDR bd_addr, UINT8 res);
378 /*******************************************************************************
380 ** Function SMP_PasskeyReply
382 ** Description This function is called after Security Manager submitted
383 ** Passkey request to the application.
385 ** Parameters: bd_addr - Address of the device for which PIN was requested
386 ** res - result of the operation SMP_SUCCESS if success
387 ** passkey - numeric value in the range of
388 ** BTM_MIN_PASSKEY_VAL(0) - BTM_MAX_PASSKEY_VAL(999999(0xF423F)).
390 *******************************************************************************/
391 extern void SMP_PasskeyReply (BD_ADDR bd_addr, UINT8 res, UINT32 passkey);
393 /*******************************************************************************
395 ** Function SMP_ConfirmReply
397 ** Description This function is called after Security Manager submitted
398 ** numeric comparison request to the application.
400 ** Parameters: bd_addr - Address of the device with which numeric
401 ** comparison was requested
402 ** res - comparison result SMP_SUCCESS if success
404 *******************************************************************************/
405 extern void SMP_ConfirmReply (BD_ADDR bd_addr, UINT8 res);
407 /*******************************************************************************
409 ** Function SMP_OobDataReply
411 ** Description This function is called to provide the OOB data for
412 ** SMP in response to SMP_OOB_REQ_EVT
414 ** Parameters: bd_addr - Address of the peer device
415 ** res - result of the operation SMP_SUCCESS if success
416 ** p_data - SM Randomizer C.
418 *******************************************************************************/
419 extern void SMP_OobDataReply(BD_ADDR bd_addr, tSMP_STATUS res, UINT8 len,
422 /*******************************************************************************
424 ** Function SMP_SecureConnectionOobDataReply
426 ** Description This function is called to provide the SC OOB data for
427 ** SMP in response to SMP_SC_OOB_REQ_EVT
429 ** Parameters: p_data - pointer to the data
431 *******************************************************************************/
432 extern void SMP_SecureConnectionOobDataReply(UINT8 *p_data);
434 /*******************************************************************************
436 ** Function SMP_Encrypt
438 ** Description This function is called to encrypt the data with the specified
441 ** Parameters: key - Pointer to key key[0] conatins the MSB
442 ** key_len - key length
443 ** plain_text - Pointer to data to be encrypted
444 ** plain_text[0] conatins the MSB
445 ** pt_len - plain text length
446 ** p_out - pointer to the encrypted outputs
448 ** Returns Boolean - TRUE: encryption is successful
449 *******************************************************************************/
450 extern BOOLEAN SMP_Encrypt (UINT8 *key, UINT8 key_len,
451 UINT8 *plain_text, UINT8 pt_len,
454 /*******************************************************************************
456 ** Function SMP_KeypressNotification
458 ** Description This function is called to notify SM about Keypress Notification.
460 ** Parameters: bd_addr - Address of the device to send keypress
462 ** value - keypress notification parameter value
464 *******************************************************************************/
465 extern void SMP_KeypressNotification (BD_ADDR bd_addr, UINT8 value);
467 /*******************************************************************************
469 ** Function SMP_CreateLocalSecureConnectionsOobData
471 ** Description This function is called to start creation of local SC OOB
472 ** data set (tSMP_LOC_OOB_DATA).
474 ** Parameters: bd_addr - Address of the device to send OOB data block
477 ** Returns Boolean - TRUE: creation of local SC OOB data set started.
478 *******************************************************************************/
479 extern BOOLEAN SMP_CreateLocalSecureConnectionsOobData (
480 tBLE_BD_ADDR *addr_to_send_to);
485 #endif /* SMP_API_H */