sm.h 11 KB

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