sm_internal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 SM_INTERNAL_H
  31. #define SM_INTERNAL_H
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif
  36. /*********************************************************************
  37. * INCLUDES
  38. */
  39. #include "bcomdef.h"
  40. #include "l2cap.h"
  41. #include "smp.h"
  42. #include "linkdb.h"
  43. /*********************************************************************
  44. * MACROS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. // Security Manager Task Events
  50. #define SM_TIMEOUT_EVT 0x0001 // Message timeout event
  51. #define SM_PAIRING_STATE_EVT 0x0002 // Event used to progress to the next pairing state
  52. // Pairing states
  53. #define SM_PAIRING_STATE_INITIALIZE 0 // Pairing has started
  54. #define SM_PAIRING_STATE_PAIRING_REQ_SENT 1 // Initiator: Pairing Request has been sent, Responder: waiting for Pairing Request.
  55. #define SM_PAIRING_STATE_WAIT_CONFIRM 2 // Waiting for Confirm message
  56. #define SM_PAIRING_STATE_WAIT_PASSKEY 3 // Waiting for Passkey from app/profile
  57. #define SM_PAIRING_STATE_WAIT_CONFIRM_PASSKEY 4 // Received Initiator Confirm message and waiting for Passkey from app/profile (responder only)
  58. #define SM_PAIRING_STATE_WAIT_RANDOM 5 // Waiting for Random message
  59. #define SM_PAIRING_STATE_WAIT_STK 6 // Waiting for STK process to finish
  60. #define SM_PAIRING_STATE_WAIT_SLAVE_ENCRYPTION_INFO 7 // Waiting for Slave Encryption Info to be sent
  61. #define SM_PAIRING_STATE_WAIT_SLAVE_MASTER_INFO 8 // Waiting for Slave Master Info to be sent
  62. #define SM_PAIRING_STATE_WAIT_SLAVE_IDENTITY_INFO 9 // Waiting for Slave Identity Info to be sent
  63. #define SM_PAIRING_STATE_WAIT_SLAVE_IDENTITY_ADDR_INFO 10 // Waiting for Slave Identity Addr Info to be sent
  64. #define SM_PAIRING_STATE_WAIT_SLAVE_SIGNING_INFO 11 // Waiting for Slave Signing Info to be sent
  65. #define SM_PAIRING_STATE_WAIT_MASTER_ENCRYPTION_INFO 12 // Waiting for Master Encryption Info to be sent
  66. #define SM_PAIRING_STATE_WAIT_MASTER_MASTER_INFO 13 // Waiting for Master Master Info to be sent
  67. #define SM_PAIRING_STATE_WAIT_MASTER_IDENTITY_INFO 14 // Waiting for Master Identity Info to be sent
  68. #define SM_PAIRING_STATE_WAIT_MASTER_IDENTITY_ADDR_INFO 15 // Waiting for Master Identity Addr Info to be sent
  69. #define SM_PAIRING_STATE_WAIT_MASTER_SIGNING_INFO 16 // Waiting for Master Signing Info to be sent
  70. #define SM_PAIRING_STATE_WAIT_ENCRYPT 17 // Waiting for LTK process to finish
  71. #define SM_PAIRING_STATE_DONE 18 // Closing out the pairing process
  72. #if defined ( TESTMODES )
  73. // SM TestModes
  74. #define SM_TESTMODE_OFF 0 // No Test mode
  75. #define SM_TESTMODE_NO_RESPONSE 1 // Don't respond to any SM message
  76. #define SM_TESTMODE_SEND_BAD_CONFIRM 2 // Force a bad confirm value in the Confirm Message
  77. #define SM_TESTMODE_BAD_CONFIRM_VERIFY 3 // Force a bad confirm check of the received Confirm Message
  78. #define SM_TESTMODE_SEND_CONFIRM 4 // Force a SMP Confirm message
  79. #endif // TESTMODES
  80. // Pairing Types
  81. #define SM_PAIRING_TYPE_INIT 0 // Pairing has been started but the type hasn't been determined yet
  82. #define SM_PAIRING_TYPE_JUST_WORKS 1 // Pairing is Just Works
  83. #define SM_PAIRING_TYPE_PASSKEY_INITIATOR_INPUTS 2 // Pairing is MITM Passkey with initiator inputs passkey
  84. #define SM_PAIRING_TYPE_PASSKEY_RESPONDER_INPUTS 3 // Pairing is MITM Passkey with responder inputs passkey
  85. #define SM_PAIRING_TYPE_PASSKEY_BOTH_INPUTS 4 // Pairing is MITM Passkey with both initiator and responder input passkey
  86. #define SM_PAIRING_TYPE_OOB 5 // Pairing is MITM OOB
  87. #define SM_PAIRING_STATE_WAIT 500 // The default wait time between key distribution messages.
  88. /*********************************************************************
  89. * TYPEDEFS
  90. */
  91. typedef struct
  92. {
  93. uint8 confirm[KEYLEN]; // calculated confirm value
  94. uint8 rand[SMP_RANDOM_LEN]; // First MRand or Srand, then RAND
  95. } devPairing_t;
  96. typedef struct
  97. {
  98. // From the start
  99. uint8 initiator; // TRUE if initiator
  100. uint8 state; // Pairing state
  101. uint8 taskID; // Task ID of the app/profile that requested the pairing
  102. uint16 connectionHandle; // Connection Handle from controller,
  103. smLinkSecurityReq_t *pSecReqs; // Pairing Control info
  104. uint8 tk[KEYLEN]; // Holds tk from app
  105. uint8 authState; // uses SM_AUTH_STATE_AUTHENTICATED & SM_AUTH_STATE_BONDING
  106. // During pairing
  107. smpPairingReq_t *pPairDev; // Info of paired device.
  108. uint8 type; // ie. SM_PAIRING_TYPE_JUST_WORKS
  109. // device information
  110. devPairing_t myComp; // This device's pairing components
  111. devPairing_t devComp; // The other device's components
  112. // Encrypt Params
  113. smSecurityInfo_t *pEncParams; // Your (device's) encryption parameters
  114. smSecurityInfo_t *pDevEncParams; // Connected device's encryption parameters
  115. smIdentityInfo_t *pIdInfo; // Connected device's identity parameters
  116. smSigningInfo_t *pSigningInfo; // Connected device's signing parameters
  117. } smPairingParams_t;
  118. // Callback when an SMP message has been received on the Initiator or Responder.
  119. typedef uint8 (*smProcessMsg_t)( linkDBItem_t *pLinkItem, uint8 cmdID, smpMsgs_t *pParsedMsg );
  120. // Callback to send next key message, and sets state for next event on the Initiator or Responder.
  121. typedef void (*smSendNextKeyInfo_t)( void );
  122. // Callback to send Start Encrypt through HCI on the Initiator.
  123. typedef bStatus_t (*smStartEncryption_t)( uint16 connHandle, uint8 *pLTK, uint16 div,
  124. uint8 *pRandNum, uint8 keyLen );
  125. // Callback when an HCI BLE LTK Request has been received on the Responder.
  126. typedef uint8 (*smProcessLTKReq_t)( uint16 connectionHandle, uint8 *pRandom, uint16 encDiv );
  127. // Initiator callback structure - must be setup by the Initiator.
  128. typedef struct
  129. {
  130. smProcessMsg_t pfnProcessMsg; // When SMP message received
  131. smSendNextKeyInfo_t pfnSendNextKeyInfo; // When need to send next key message
  132. smStartEncryption_t pfnStartEncryption; // When Start Encrypt requested
  133. } smInitiatorCBs_t;
  134. // Responder callback structure - must be setup by the Initiator.
  135. typedef struct
  136. {
  137. smProcessMsg_t pfnProcessMsg; // When SMP message received
  138. smSendNextKeyInfo_t pfnSendNextKeyInfo; // When need to send next key message
  139. smProcessLTKReq_t pfnProcessLTKReq; // When HCI BLE LTK Request received
  140. } smResponderCBs_t;
  141. /*********************************************************************
  142. * GLOBAL VARIABLES
  143. */
  144. // Security Manager's OSAL task ID
  145. extern uint8 smTaskID;
  146. extern smPairingParams_t *pPairingParams;
  147. extern smResponderCBs_t *pfnResponderCBs;
  148. /*********************************************************************
  149. * FUNCTIONS - API
  150. */
  151. /*********************************************************************
  152. * Application Level Functions
  153. */
  154. /*
  155. * smLinkCheck - link database callback function.
  156. */
  157. extern void smLinkCheck( uint16 connectionHandle, uint8 changeType );
  158. /*
  159. * smProcessRandComplete - Process the HCI Random Complete Event.
  160. */
  161. extern uint8 smProcessRandComplete( uint8 status, uint8 *rand );
  162. /*
  163. * smTimedOut - Process the SM timeout.
  164. */
  165. extern void smTimedOut( void );
  166. /*
  167. * smStartRspTimer - Start the SM Response Timer.
  168. */
  169. extern void smStartRspTimer( void );
  170. /*
  171. * smStopRspTimer - Stop the SM Response Timer.
  172. */
  173. extern void smStopRspTimer( void );
  174. /*
  175. * smProcessDataMsg - Process incoming L2CAP messages.
  176. */
  177. extern void smProcessDataMsg( l2capDataEvent_t *pMsg );
  178. /*
  179. * smProcessEncryptChange - Process the HCI BLE Encrypt Change Event.
  180. */
  181. extern uint8 smProcessEncryptChange( uint16 connectionHandle, uint8 reason );
  182. /*
  183. * smInProcess - Is SM already processing something?
  184. */
  185. extern uint8 smInProcess( void );
  186. /*
  187. * sm_d1 - SM diversifying function d1
  188. */
  189. extern bStatus_t sm_d1( uint8 *pK, uint16 d, uint8 *pD1 );
  190. /*
  191. * sm_ah - Random address hash function
  192. */
  193. extern bStatus_t sm_ah( uint8 *pK, uint8 *pR, uint8 *pAh );
  194. /*
  195. * sm_dm - SM DIV Maxk generation function dm
  196. */
  197. extern bStatus_t sm_dm( uint8 *pK, uint8 *pR, uint16 *pDm );
  198. /*
  199. * sm_c1 - SM Confirm value generation function c1
  200. */
  201. extern bStatus_t sm_c1( uint8 *pK, uint8 *pR, uint8 *pC1 );
  202. /*
  203. * sm_c1new - SM Confirm value generation function c1
  204. */
  205. extern bStatus_t sm_c1new( uint8 *pK, uint8 *pR, uint8 *pRes, uint8 *pReq,
  206. uint8 iat, uint8 *pIA, uint8 rat, uint8 *pRA, uint8 *pC1 );
  207. /*
  208. * sm_s1 - SM key generation function s1
  209. */
  210. extern bStatus_t sm_s1( uint8 *pK, uint8 *pR1, uint8 *pR2, uint8 *pS1 );
  211. /*
  212. * smGenerateRandBuf - generate a buffer of random numbers
  213. */
  214. extern void smGenerateRandBuf( uint8 *pRandNum, uint8 len );
  215. /*
  216. * smEncLTK - start LTK Encryption
  217. */
  218. extern void smEncLTK( void );
  219. /*
  220. * smNextPairingState - trigger next state machine
  221. */
  222. extern void smNextPairingState( void );
  223. /*
  224. * smAuthReqToUint8 - conversion function
  225. */
  226. extern uint8 smAuthReqToUint8( authReq_t *pAuthReq );
  227. /*
  228. * smUint8ToAuthReq - conversion function
  229. */
  230. extern void smUint8ToAuthReq( authReq_t *pAuthReq, uint8 authReqUint8 );
  231. /*
  232. * smpResponderProcessPairingReq - Process an incoming Pairing Request message
  233. */
  234. extern uint8 smpResponderProcessPairingReq( smpPairingReq_t *pParsedMsg );
  235. /*
  236. * smSendFailAndEnd - Send the pairing failed message and end existing pairing
  237. */
  238. extern bStatus_t smSendFailAndEnd( uint16 connHandle, smpPairingFailed_t *pFailedMsg );
  239. /*
  240. * generateRandMsg - Generate a Pairing Random
  241. */
  242. extern bStatus_t smGenerateRandMsg( void );
  243. /*
  244. * smSavePairInfo - Save the Pairing Req or Rsp information
  245. */
  246. extern bStatus_t smSavePairInfo( smpPairingReq_t *pPair );
  247. /*
  248. * generateConfirm - Generate a Pairing Confirm
  249. */
  250. extern bStatus_t smGenerateConfirm( void );
  251. /*
  252. * smEndPairing - Pairing mode has ended. Yeah. Notify the GAP and free
  253. * up the memory used.
  254. */
  255. extern void smEndPairing( uint8 status );
  256. /*
  257. * determineKeySize - Determine the maximum encryption key size
  258. */
  259. extern uint8 smDetermineKeySize( void );
  260. /*
  261. * smGeneratePairingReqRsp - Generate a pairing req or response
  262. */
  263. extern bStatus_t smGeneratePairingReqRsp( void );
  264. /*
  265. * smPairingSendEncInfo - Send SM Encryption Information message
  266. */
  267. extern void smPairingSendEncInfo( uint16 connHandle, uint8 *pLTK );
  268. /*
  269. * smPairingSendMasterID - Send SM Master Identification message
  270. */
  271. extern void smPairingSendMasterID( uint16 connHandle, uint16 ediv, uint8 *pRand );
  272. /*
  273. * smPairingSendIdentityInfo - Send SM Identity Information message
  274. */
  275. extern void smPairingSendIdentityInfo( uint16 connHandle, uint8 *pIRK );
  276. /*
  277. * smPairingSendIdentityAddrInfo - Send SM Identity Addr Information message
  278. */
  279. extern void smPairingSendIdentityAddrInfo( uint16 connHandle, uint8 addrType, uint8 *pMACAddr );
  280. /*
  281. * smPairingSendSingingInfo - Send SM Signing Information message
  282. */
  283. extern void smPairingSendSingingInfo( uint16 connHandle, uint8 *pSRK );
  284. /*
  285. * smPairingSendEncInfo - Send SM Encryption Information message
  286. */
  287. extern void smPairingSendEncInfo( uint16 connHandle, uint8 *pLTK );
  288. /*
  289. * smProcessPairingReq - Process Pairing Request
  290. */
  291. extern void smProcessPairingReq( linkDBItem_t *pLinkItem, gapPairingReq_t *pPairReq );
  292. /*
  293. * smStartEncryption - Perform Encrypt through HCI
  294. */
  295. extern bStatus_t smStartEncryption( uint16 connHandle, uint8 *pLTK, uint16 div,
  296. uint8 *pRandNum, uint8 keyLen );
  297. /*
  298. * smRegisterInitiator - egister Initiator's processing function with SM task
  299. */
  300. extern void smRegisterInitiator( smInitiatorCBs_t *pfnCBs );
  301. /*
  302. * smRegisterResponder - Register Responder's processing function with SM task
  303. */
  304. extern void smRegisterResponder( smResponderCBs_t *pfnCBs );
  305. /*********************************************************************
  306. *********************************************************************/
  307. #ifdef __cplusplus
  308. }
  309. #endif
  310. #endif /* SM_INTERNAL_H */