sm.h 12 KB

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