sm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. @headerfile: sm.h
  30. $Date:
  31. $Revision:
  32. @mainpage BLE SM API
  33. This file contains the interface to the SM.
  34. */
  35. #ifndef SM_H
  36. #define SM_H
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. /*-------------------------------------------------------------------
  42. * INCLUDES
  43. */
  44. #include "bcomdef.h"
  45. #include "OSAL.h"
  46. /*-------------------------------------------------------------------
  47. * MACROS
  48. */
  49. /*-------------------------------------------------------------------
  50. * CONSTANTS
  51. */
  52. /** @defgroup SM_IO_CAP_DEFINES SM I/O Capabilities
  53. * @{
  54. */
  55. #define DISPLAY_ONLY 0x00 //!< Display Only Device
  56. #define DISPLAY_YES_NO 0x01 //!< Display and Yes and No Capable
  57. #define KEYBOARD_ONLY 0x02 //!< Keyboard Only
  58. #define NO_INPUT_NO_OUTPUT 0x03 //!< No Display or Input Device
  59. #define KEYBOARD_DISPLAY 0x04 //!< Both Keyboard and Display Capable
  60. /** @} End SM_IO_CAP_DEFINES */
  61. #define SM_AUTH_MITM_MASK(a) (((a) & 0x04) >> 2)
  62. /** @defgroup SM_PASSKEY_TYPE_DEFINES SM Passkey Types (Bit Masks)
  63. * @{
  64. */
  65. #define SM_PASSKEY_TYPE_INPUT 0x01 //!< Input the passkey
  66. #define SM_PASSKEY_TYPE_DISPLAY 0x02 //!< Display the passkey
  67. /** @} End SM_PASSKEY_TYPE_DEFINES */
  68. /** @defgroup SM_BONDING_FLAGS_DEFINES SM AuthReq Bonding Flags
  69. * Bonding flags 0x02 and 0x03 are reserved.
  70. * @{
  71. */
  72. #define SM_AUTH_REQ_NO_BONDING 0x00 //!< No bonding
  73. #define SM_AUTH_REQ_BONDING 0x01 //!< Bonding
  74. /** @} End SM_BONDING_FLAGS_DEFINES */
  75. #define PASSKEY_LEN 6 //! Passkey Character Length (ASCII Characters)
  76. #define SM_AUTH_STATE_CT2 0x20
  77. #define SM_AUTH_STATE_KEYPRESS 0x10
  78. #define SM_AUTH_STATE_SC 0x08
  79. #define SM_AUTH_STATE_AUTHENTICATED 0x04 //! Authenticate requested
  80. #define SM_AUTH_STATE_BONDING 0x01 //! Bonding requested
  81. /*-------------------------------------------------------------------
  82. * General TYPEDEFS
  83. */
  84. /**
  85. * SM_NEW_RAND_KEY_EVENT message format. This message is sent to the
  86. * requesting task.
  87. */
  88. typedef struct
  89. {
  90. osal_event_hdr_t hdr; //!< SM_NEW_RAND_KEY_EVENT and status
  91. uint8 newKey[KEYLEN]; //!< New key value - if status is SUCCESS
  92. } smNewRandKeyEvent_t;
  93. /**
  94. * Key Distribution field - True or False fields
  95. */
  96. typedef struct
  97. {
  98. unsigned int sEncKey:1; //!< Set to distribute slave encryption key
  99. unsigned int sIdKey:1; //!< Set to distribute slave identity key
  100. unsigned int sSign:1; //!< Set to distribute slave signing key
  101. unsigned int sReserved:5; // bug fixed 2018-11-26, reserved bits should be saved for upward compatibility
  102. unsigned int mEncKey:1; //!< Set to distribute master encryption key
  103. unsigned int mIdKey:1; //!< Set to distribute master identity key
  104. unsigned int mSign:1; //!< Set to distribute master signing key
  105. unsigned int mReserved:5; // bug fixed 2018-11-26, reserved bits should be saved for upward compatibility
  106. } keyDist_t;
  107. /**
  108. * Link Security Requirements
  109. */
  110. typedef struct
  111. {
  112. uint8 ioCaps; //!< I/O Capabilities (ie.
  113. uint8 oobAvailable; //!< True if Out-of-band key available
  114. uint8 oob[KEYLEN]; //!< Out-Of-Bounds key
  115. uint8 authReq; //!< Authentication Requirements
  116. keyDist_t keyDist; //!< Key Distribution mask
  117. uint8 maxEncKeySize; //!< Maximum Encryption Key size (7-16 bytes)
  118. } smLinkSecurityReq_t;
  119. /**
  120. * Link Security Information
  121. */
  122. typedef struct
  123. {
  124. uint8 ltk[KEYLEN]; //!< Long Term Key (LTK)
  125. uint16 div; //!< LTK Diversifier
  126. uint8 rand[B_RANDOM_NUM_SIZE]; //!< LTK random number
  127. uint8 keySize; //!< LTK Key Size (7-16 bytes)
  128. } smSecurityInfo_t;
  129. /**
  130. * Link Identity Information
  131. */
  132. typedef struct
  133. {
  134. uint8 irk[KEYLEN]; //!< Identity Resolving Key (IRK)
  135. uint8 bd_addr[B_ADDR_LEN]; //!< The advertiser may set this to zeroes to not disclose its BD_ADDR (public address).
  136. } smIdentityInfo_t;
  137. /**
  138. * Signing Information
  139. */
  140. typedef struct
  141. {
  142. uint8 srk[KEYLEN]; //!< Signature Resolving Key (CSRK)
  143. uint32 signCounter; //!< Sign Counter
  144. } smSigningInfo_t;
  145. /**
  146. * Pairing Request & Response - authReq field
  147. */
  148. typedef struct
  149. {
  150. unsigned int bonding:2; //!< Bonding flags
  151. unsigned int mitm:1; //!< Man-In-The-Middle (MITM)
  152. unsigned int reserved:5; //!< Reserved - don't use
  153. } authReq_t;
  154. /*-------------------------------------------------------------------
  155. * GLOBAL VARIABLES
  156. */
  157. /**
  158. * @defgroup SM_API Security Manager API Functions
  159. *
  160. * @{
  161. */
  162. /*-------------------------------------------------------------------
  163. * FUNCTIONS - MASTER API - Only use these in a master device
  164. */
  165. /**
  166. * @brief Initialize SM Initiator on a master device.
  167. *
  168. * @return SUCCESS
  169. */
  170. extern bStatus_t SM_InitiatorInit( void );
  171. /**
  172. * @brief Start the pairing process. This function is also
  173. * called if the device is already bound.
  174. *
  175. * NOTE: Only one pairing process at a time per device.
  176. *
  177. * @param initiator - TRUE to start pairing as Initiator.
  178. * @param taskID - task ID to send results.
  179. * @param connectionHandle - Link's connection handle
  180. * @param pSecReqs - Security parameters for pairing
  181. *
  182. * @return SUCCESS,<BR>
  183. * INVALIDPARAMETER,<BR>
  184. * bleAlreadyInRequestedMode
  185. */
  186. extern bStatus_t SM_StartPairing( uint8 initiator,
  187. uint8 taskID,
  188. uint16 connectionHandle,
  189. smLinkSecurityReq_t *pSecReqs );
  190. /**
  191. * @brief Send Start Encrypt through HCI
  192. *
  193. * @param connHandle - Connection Handle
  194. * @param pLTK - pointer to 16 byte lkt
  195. * @param div - div or ediv
  196. * @param pRandNum - pointer to 8 byte random number
  197. * @param keyLen - length of LTK (bytes)
  198. *
  199. * @return SUCCESS,<BR>
  200. * INVALIDPARAMETER,<BR>
  201. * other from HCI/LL
  202. */
  203. extern bStatus_t SM_StartEncryption( uint16 connHandle, uint8 *pLTK,
  204. uint16 div, uint8 *pRandNum, uint8 keyLen );
  205. /*-------------------------------------------------------------------
  206. * FUNCTIONS - SLAVE API - Only use these in a slave device
  207. */
  208. /**
  209. * @brief Initialize SM Responder on a slave device.
  210. *
  211. * @return SUCCESS
  212. */
  213. extern bStatus_t SM_ResponderInit( void );
  214. /*-------------------------------------------------------------------
  215. * FUNCTIONS - GENERAL API - both master and slave
  216. */
  217. /**
  218. * @brief Generate a key with a random value.
  219. *
  220. * @param taskID - task ID to send results.
  221. *
  222. * @return SUCCESS,<BR>
  223. * bleNotReady,<BR>
  224. * bleMemAllocError,<BR>
  225. * FAILURE
  226. */
  227. extern bStatus_t SM_NewRandKey( uint8 taskID );
  228. /**
  229. * @brief Calculate a new Private Resolvable address.
  230. *
  231. * @param pIRK - Identity Root Key.
  232. * @param pNewAddr - pointer to place to put new calc'd address
  233. *
  234. * @return SUCCESS - if started,<BR>
  235. * INVALIDPARAMETER
  236. */
  237. extern bStatus_t SM_CalcRandomAddr( uint8 *pIRK, uint8 *pNewAddr );
  238. /**
  239. * @brief Resolve a Private Resolveable Address.
  240. *
  241. * @param pIRK - pointer to the IRK
  242. * @param pAddr - pointer to the random address
  243. *
  244. * @return SUCCESS - match,<BR>
  245. * FAILURE - don't match,<BR>
  246. * INVALIDPARAMETER - parameters invalid
  247. */
  248. extern bStatus_t SM_ResolveRandomAddrs( uint8 *pIRK, uint8 *pAddr );
  249. /**
  250. * @brief Encrypt the plain text data with the key..
  251. *
  252. * @param pKey - key data
  253. * @param pPlainText - Plain text data
  254. * @param pResult - place to put the encrypted result
  255. *
  256. * @return SUCCESS - if started,<BR>
  257. * INVALIDPARAMETER - one of the parameters are NULL,<BR>
  258. * bleAlreadyInRequestedMode,<BR>
  259. * bleMemAllocError
  260. */
  261. extern bStatus_t SM_Encrypt( uint8 *pKey, uint8 *pPlainText, uint8 *pResult );
  262. /**
  263. * @brief Generate an outgoing Authentication Signature.
  264. *
  265. * @param pData - message data
  266. * @param len - length of pData
  267. * @param pAuthenSig - place to put new signature
  268. *
  269. * @return SUCCESS - signature authentication generated,<BR>
  270. * INVALIDPARAMETER - pData or pAuthenSig is NULL,<BR>
  271. * bleMemAllocError
  272. */
  273. extern bStatus_t SM_GenerateAuthenSig( uint8 *pData, uint8 len, uint8 *pAuthenSig );
  274. /**
  275. * @brief Verify an Authentication Signature.
  276. *
  277. * @param connHandle - connection to verify against.
  278. * @param authentication - TRUE if requires an authenticated CSRK, FALSE if not
  279. * @param pData - message data
  280. * @param len - length of pData
  281. * @param pAuthenSig - message signature to verify
  282. *
  283. * @return SUCCESS - signature authentication verified,<BR>
  284. * FAILURE - if not verified,<BR>
  285. * bleNotConnected - Connection not found,<BR>
  286. * INVALIDPARAMETER - pData or pAuthenSig is NULL, or signCounter is invalid,<BR>
  287. * bleMemAllocError
  288. */
  289. extern bStatus_t SM_VerifyAuthenSig( uint16 connHandle,
  290. uint8 authentication,
  291. uint8 *pData,
  292. uint8 len,
  293. uint8 *pAuthenSig );
  294. /**
  295. * @brief Update the passkey for the pairing process.
  296. *
  297. * @param pPasskey - pointer to the 6 digit passkey
  298. * @param connectionHandle - connection handle to link.
  299. *
  300. * @return SUCCESS,<BR>
  301. * bleIncorrectMode - Not pairing,<BR>
  302. * INVALIDPARAMETER - link is incorrect
  303. */
  304. extern bStatus_t SM_PasskeyUpdate( uint8 *pPasskey, uint16 connectionHandle );
  305. /**
  306. * @} End SM_API
  307. */
  308. /*-------------------------------------------------------------------
  309. * TASK API - These functions must only be called OSAL.
  310. */
  311. /**
  312. * @internal
  313. *
  314. * @brief SM Task Initialization Function.
  315. *
  316. * @param taskID - SM task ID.
  317. *
  318. * @return void
  319. */
  320. extern void SM_Init( uint8 task_id );
  321. /**
  322. * @internal
  323. *
  324. * @brief SM Task event processing function.
  325. *
  326. * @param taskID - SM task ID
  327. * @param events - SM events.
  328. *
  329. * @return events not processed
  330. */
  331. extern uint16 SM_ProcessEvent( uint8 task_id, uint16 events );
  332. /*-------------------------------------------------------------------
  333. -------------------------------------------------------------------*/
  334. #ifdef __cplusplus
  335. }
  336. #endif
  337. #endif /* SM_H */