smp_api.h 18 KB

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