smp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /**************************************************************************************************
  29. **************************************************************************************************/
  30. #ifndef SMP_H
  31. #define SMP_H
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. /*********************************************************************
  37. * INCLUDES
  38. */
  39. #include "bcomdef.h"
  40. #include "sm_internal.h"
  41. /*********************************************************************
  42. * MACROS
  43. */
  44. /*********************************************************************
  45. * CONSTANTS
  46. */
  47. // Code field of the SMP Command format
  48. #define SMP_PAIRING_REQ 0x01
  49. #define SMP_PAIRING_RSP 0x02
  50. #define SMP_PAIRING_CONFIRM 0x03
  51. #define SMP_PAIRING_RANDOM 0x04
  52. #define SMP_PAIRING_FAILED 0x05
  53. #define SMP_ENCRYPTION_INFORMATION 0x06
  54. #define SMP_MASTER_IDENTIFICATION 0x07
  55. #define SMP_IDENTITY_INFORMATION 0x08
  56. #define SMP_IDENTITY_ADDR_INFORMATION 0x09
  57. #define SMP_SIGNING_INFORMATION 0x0A
  58. #define SMP_SECURITY_REQUEST 0x0B
  59. // Pairing Request & Response - IO Capabilities
  60. #define SMP_IO_CAP_DISPLAY_ONLY 0x00
  61. #define SMP_IO_CAP_DISPLAY_YES_NO 0x01
  62. #define SMP_IO_CAP_KEYBOARD_ONLY 0x02
  63. #define SMP_IO_CAP_NO_INPUT_NO_OUTPUT 0x03
  64. #define SMP_IO_CAP_KEYBOARD_DISPLAY 0x04
  65. // Pairing Request & Response - Out Of Bound (OOB) data flag values
  66. #define SMP_OOB_AUTH_DATA_NOT_PRESENT 0x00
  67. #define SMP_OOB_AUTH_DATA_REMOTE_DEVICE_PRESENT 0x01
  68. // Pairing Request & Response - authReq field
  69. // - This field contains 2 sub-fields:
  70. // bonding flags - bits 1 & 0
  71. #define SMP_AUTHREQ_BONDING 0x01
  72. // Man-In-The-Middle (MITM) - bit 2
  73. #define SMP_AUTHREQ_MITM 0x04
  74. #define SMP_CONFIRM_LEN 16
  75. #define SMP_RANDOM_LEN 16
  76. // Pairing Failed - "reason" field
  77. #define SMP_PAIRING_FAILED_PASSKEY_ENTRY_FAILED 0x01 //!< The user input of the passkey failed, for example, the user cancelled the operation.
  78. #define SMP_PAIRING_FAILED_OOB_NOT_AVAIL 0x02 //!< The OOB data is not available
  79. #define SMP_PAIRING_FAILED_AUTH_REQ 0x03 //!< The pairing procedure can't be performed as authentication requirements can't be met due to IO capabilities of one or both devices
  80. #define SMP_PAIRING_FAILED_CONFIRM_VALUE 0x04 //!< The confirm value doesn't match the calculated compare value
  81. #define SMP_PAIRING_FAILED_NOT_SUPPORTED 0x05 //!< Pairing isn't supported by the device
  82. #define SMP_PAIRING_FAILED_ENC_KEY_SIZE 0x06 //!< The resultant encryption key size is insufficient for the security requirements of this device.
  83. #define SMP_PAIRING_FAILED_CMD_NOT_SUPPORTED 0x07 //!< The SMP command received is not supported on this device.
  84. #define SMP_PAIRING_FAILED_UNSPECIFIED 0x08 //!< Pairing failed due to an unspecified reason
  85. #define SMP_PAIRING_FAILED_REPEATED_ATTEMPTS 0x09 //!< Pairing or authenication procedure is disallowed because too little time has elapsed since the last pairing request or security request.
  86. #define SMP_PAIRING_FAILED_LOCAL_KEY_FAILURE 0x0A // Local value - not sent over the air
  87. // Message lengths
  88. #define SMP_PAIRING_REQ_LEN 7
  89. #define SMP_PAIRING_RSP_LEN 7
  90. #define SMP_PAIRING_CONFIRM_LEN 17
  91. #define SMP_PAIRING_RANDOM_LEN 17
  92. #define SMP_PAIRING_FAILED_LEN 2
  93. #define SMP_ENCRYPTION_INFORMATION_LEN 17
  94. #define SMP_MASTER_IDENTIFICATION_LEN 11
  95. #define SMP_IDENTITY_INFORMATION_LEN 17
  96. #define SMP_IDENTITY_ADDR_INFORMATION_LEN 8
  97. #define SMP_SIGNING_INFORMATION_LEN 17
  98. #define SMP_SECURITY_REQUEST_LEN 2
  99. // Macros to use the smSendSMMsg() function to send all of the Security Manager Protocol messages
  100. #define smSendPairingReq( connHandle, msgStruct ) \
  101. smSendSMMsg( (connHandle), SMP_PAIRING_REQ_LEN, \
  102. (smpMsgs_t *)(msgStruct), \
  103. (pfnSMBuildCmd_t)(smpBuildPairingReq) )
  104. #define smSendPairingRsp( connHandle, msgStruct ) \
  105. smSendSMMsg( (connHandle), SMP_PAIRING_RSP_LEN, \
  106. (smpMsgs_t *)(msgStruct), \
  107. (pfnSMBuildCmd_t)(smpBuildPairingRsp) )
  108. #define smSendPairingConfirm( connHandle, msgStruct ) \
  109. smSendSMMsg( (connHandle), SMP_PAIRING_CONFIRM_LEN, \
  110. (smpMsgs_t *)(msgStruct), \
  111. (pfnSMBuildCmd_t)(smpBuildPairingConfirm) )
  112. #define smSendPairingRandom( connHandle, msgStruct ) \
  113. smSendSMMsg( (connHandle), SMP_PAIRING_RANDOM_LEN, \
  114. (smpMsgs_t *)(msgStruct), \
  115. (pfnSMBuildCmd_t)(smpBuildPairingRandom) )
  116. #define smSendPairingFailed( connHandle, msgStruct ) \
  117. smSendSMMsg( (connHandle), SMP_PAIRING_FAILED_LEN, \
  118. (smpMsgs_t *)(msgStruct), \
  119. (pfnSMBuildCmd_t)(smpBuildPairingFailed) )
  120. #define smSendEncInfo( connHandle, msgStruct ) \
  121. smSendSMMsg( (connHandle), SMP_ENCRYPTION_INFORMATION_LEN, \
  122. (smpMsgs_t *)(msgStruct), \
  123. (pfnSMBuildCmd_t)(smpBuildEncInfo) )
  124. #define smSendMasterID( connHandle, msgStruct ) \
  125. smSendSMMsg( (connHandle), SMP_MASTER_IDENTIFICATION_LEN, \
  126. (smpMsgs_t *)(msgStruct), \
  127. (pfnSMBuildCmd_t)(smpBuildMasterID) )
  128. #define smSendIdentityInfo( connHandle, msgStruct ) \
  129. smSendSMMsg( (connHandle), SMP_IDENTITY_INFORMATION_LEN, \
  130. (smpMsgs_t *)(msgStruct), \
  131. (pfnSMBuildCmd_t)(smpBuildIdentityInfo) )
  132. #define smSendIdentityAddrInfo( connHandle, msgStruct ) \
  133. smSendSMMsg( (connHandle), SMP_IDENTITY_ADDR_INFORMATION_LEN, \
  134. (smpMsgs_t *)(msgStruct), \
  135. (pfnSMBuildCmd_t)(smpBuildIdentityAddrInfo) )
  136. #define smSendSigningInfo( connHandle, msgStruct ) \
  137. smSendSMMsg( (connHandle), SMP_SIGNING_INFORMATION_LEN, \
  138. (smpMsgs_t *)(msgStruct), \
  139. (pfnSMBuildCmd_t)(smpBuildSigningInfo) )
  140. #define smSendSecurityReq( connHandle, msgStruct ) \
  141. smSendSMMsg( (connHandle), SMP_SECURITY_REQUEST_LEN, \
  142. (smpMsgs_t *)(msgStruct), \
  143. (pfnSMBuildCmd_t)(smpBuildSecurityReq) )
  144. /*********************************************************************
  145. * TYPEDEFS
  146. */
  147. // Pairing Request
  148. typedef struct
  149. {
  150. uint8 ioCapability; // ex. SMP_IO_CAP_DISPLAY_YES_NO
  151. uint8 oobDataFlag; // Out of Bound data flag
  152. authReq_t authReq; // Authentication fields
  153. uint8 maxEncKeySize; // Encryption Key size max bytes (7 - 16)
  154. keyDist_t keyDist; // Key Distribution Field - bit struct
  155. } smpPairingReq_t;
  156. // Pairing Response - same as Pairing Request
  157. typedef smpPairingReq_t smpPairingRsp_t;
  158. // Pairing Confirm
  159. typedef struct
  160. {
  161. uint8 confirmValue[SMP_CONFIRM_LEN];
  162. } smpPairingConfirm_t;
  163. // Pairing Random
  164. typedef struct
  165. {
  166. uint8 randomValue[SMP_RANDOM_LEN];
  167. } smpPairingRandom_t;
  168. // Pairing Failed
  169. typedef struct
  170. {
  171. uint8 reason;
  172. } smpPairingFailed_t;
  173. // Encryption Information
  174. typedef struct
  175. {
  176. uint8 ltk[KEYLEN];
  177. } smpEncInfo_t;
  178. // Master Identification
  179. typedef struct
  180. {
  181. uint16 ediv;
  182. uint16 rand[B_RANDOM_NUM_SIZE];
  183. } smpMasterID_t;
  184. // Identity Information
  185. typedef struct
  186. {
  187. uint8 irk[KEYLEN];
  188. } smpIdentityInfo_t;
  189. // Identity Address Information
  190. typedef struct
  191. {
  192. uint8 addrType;
  193. uint8 bdAddr[B_ADDR_LEN];
  194. } smpIdentityAddrInfo_t;
  195. // Signing Information
  196. typedef struct
  197. {
  198. uint8 signature[KEYLEN];
  199. } smpSigningInfo_t;
  200. // Slave Security Request
  201. typedef struct
  202. {
  203. authReq_t authReq;
  204. } smpSecurityReq_t;
  205. // Union with all of the SM messages.
  206. typedef union
  207. {
  208. smpPairingReq_t pairingReq;
  209. smpPairingReq_t pairingRsp;
  210. smpPairingConfirm_t pairingConfirm;
  211. smpPairingRandom_t pairingRandom;
  212. smpPairingFailed_t pairingFailed;
  213. smpEncInfo_t encInfo;
  214. smpMasterID_t masterID;
  215. smpIdentityInfo_t idInfo;
  216. smpIdentityAddrInfo_t idAddrInfo;
  217. smpSigningInfo_t signingInfo;
  218. smpSecurityReq_t secReq;
  219. } smpMsgs_t;
  220. typedef uint8 (*pfnSMBuildCmd_t)( smpMsgs_t *pMsgStruct, uint8 *pBuf );
  221. /*********************************************************************
  222. * GLOBAL VARIABLES
  223. */
  224. extern smpPairingReq_t pairingReg;
  225. /*********************************************************************
  226. * FUNCTIONS
  227. */
  228. /*
  229. * smpBuildPairingReq - Build an SM Pairing Request
  230. */
  231. extern bStatus_t smpBuildPairingReq( smpPairingReq_t *pPairingReq, uint8 *pBuf );
  232. /*
  233. * smpBuildPairingRsp - Build an SM Pairing Response
  234. */
  235. extern bStatus_t smpBuildPairingRsp( smpPairingRsp_t *pPairingRsp, uint8 *pBuf );
  236. /*
  237. * smpBuildPairingReqRsp - Build an SM Pairing Request or Response
  238. */
  239. extern bStatus_t smpBuildPairingReqRsp( uint8 opCode, smpPairingReq_t *pPairingReq, uint8 *pBuf );
  240. /*
  241. * smpParsePairingReq - Parse an SM Pairing Request
  242. */
  243. extern bStatus_t smpParsePairingReq( uint8 *pBuf, smpPairingReq_t *pPairingReq );
  244. /*
  245. * smpParsePairingRsp - Parse an SM Pairing Response
  246. */
  247. #define smpParsePairingRsp( a, b ) smpParsePairingReq( (a), (b) )
  248. /*
  249. * smpBuildPairingConfirm - Build an SM Pairing Confirm
  250. */
  251. extern bStatus_t smpBuildPairingConfirm( smpPairingConfirm_t *pPairingConfirm,
  252. uint8 *pBuf );
  253. /*
  254. * smpParsePairingConfirm - Parse an SM Pairing Confirm
  255. */
  256. extern bStatus_t smpParsePairingConfirm( uint8 *pBuf,
  257. smpPairingConfirm_t *pPairingConfirm );
  258. /*
  259. * smpBuildPairingRandom - Build an SM Pairing Random
  260. */
  261. extern bStatus_t smpBuildPairingRandom( smpPairingRandom_t *pPairingRandom,
  262. uint8 *pBuf );
  263. /*
  264. * smpParsePairingRandom - Parse an SM Pairing Random
  265. */
  266. extern bStatus_t smpParsePairingRandom( uint8 *pBuf,
  267. smpPairingRandom_t *pPairingRandom );
  268. /*
  269. * smpBuildPairingFailed - Build an SM Pairing Failed
  270. */
  271. extern bStatus_t smpBuildPairingFailed( smpPairingFailed_t *pPairingFailed,
  272. uint8 *pBuf );
  273. /*
  274. * smpParsePairingFailed - Parse an SM Pairing Failed
  275. */
  276. extern bStatus_t smpParsePairingFailed( uint8 *pBuf,
  277. smpPairingFailed_t *pPairingFailed );
  278. /*
  279. * smpBuildEncInfo - Build an SM Encryption Information
  280. */
  281. extern bStatus_t smpBuildEncInfo( smpEncInfo_t *pEncInfo, uint8 *pBuf );
  282. /*
  283. * smpParseEncInfo - Parse an SM Encryption Information
  284. */
  285. extern bStatus_t smpParseEncInfo( uint8 *buf, smpEncInfo_t *encInfo );
  286. /*
  287. * smpBuildMasterID - Build an SM Master Identification
  288. */
  289. extern bStatus_t smpBuildMasterID( smpMasterID_t *pMasterID, uint8 *pBuf );
  290. /*
  291. * smpParseMasterID - Parse an SM Master Identification
  292. */
  293. extern bStatus_t smpParseMasterID( uint8 *pBuf, smpMasterID_t *pMasterID );
  294. /*
  295. * smpBuildIdentityInfo - Build an SM Identity Information
  296. */
  297. extern bStatus_t smpBuildIdentityInfo( smpIdentityInfo_t *pIdInfo, uint8 *pBuf );
  298. /*
  299. * smpBuildIdentityAddrInfo - Build an SM Identity Address Information
  300. */
  301. extern bStatus_t smpBuildIdentityAddrInfo( smpIdentityAddrInfo_t *pIdInfo, uint8 *pBuf );
  302. /*
  303. * smpParseIdentityInfo - Parse an SM Identity Information
  304. */
  305. extern bStatus_t smpParseIdentityInfo( uint8 *pBuf, smpIdentityInfo_t *pIdInfo );
  306. /*
  307. * smpParseIdentityAddrInfo - Parse an SM Identity Address Information
  308. */
  309. extern bStatus_t smpParseIdentityAddrInfo( uint8 *pBuf, smpIdentityAddrInfo_t *pIdInfo );
  310. /*
  311. * smpBuildSigningInfo - Build an SM Signing Information
  312. */
  313. extern bStatus_t smpBuildSigningInfo( smpSigningInfo_t *pSigningInfo, uint8 *pBuf );
  314. /*
  315. * smpParseSigningInfo - Parse an SM Signing Information
  316. */
  317. extern bStatus_t smpParseSigningInfo( uint8 *pBuf, smpSigningInfo_t *pSigningInfo );
  318. /*
  319. * smpBuildSecurityReq - Build an SM Slave Security Request
  320. */
  321. extern bStatus_t smpBuildSecurityReq( smpSecurityReq_t *pSecReq, uint8 *pBuf );
  322. /*
  323. * smpParseSecurityReq - Parse an SM Slave Security Request
  324. */
  325. extern bStatus_t smpParseSecurityReq( uint8 *pBuf, smpSecurityReq_t *pSecReq );
  326. /*
  327. * smSendSMMsg - Generic Send L2CAP SM message function
  328. */
  329. extern bStatus_t smSendSMMsg( uint16 connHandle, uint8 bufLen, smpMsgs_t *pMsg, pfnSMBuildCmd_t buildFn );
  330. /*********************************************************************
  331. *********************************************************************/
  332. #ifdef __cplusplus
  333. }
  334. #endif
  335. #endif /* SMP_H */