smp.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. **************************************************************************************************/
  12. #ifndef SMP_H
  13. #define SMP_H
  14. #ifdef __cplusplus
  15. extern "C"
  16. {
  17. #endif
  18. /*********************************************************************
  19. * INCLUDES
  20. */
  21. #include "bcomdef.h"
  22. #include "sm_internal.h"
  23. /*********************************************************************
  24. * MACROS
  25. */
  26. /*********************************************************************
  27. * CONSTANTS
  28. */
  29. // Code field of the SMP Command format
  30. #define SMP_PAIRING_REQ 0x01
  31. #define SMP_PAIRING_RSP 0x02
  32. #define SMP_PAIRING_CONFIRM 0x03
  33. #define SMP_PAIRING_RANDOM 0x04
  34. #define SMP_PAIRING_FAILED 0x05
  35. #define SMP_ENCRYPTION_INFORMATION 0x06
  36. #define SMP_MASTER_IDENTIFICATION 0x07
  37. #define SMP_IDENTITY_INFORMATION 0x08
  38. #define SMP_IDENTITY_ADDR_INFORMATION 0x09
  39. #define SMP_SIGNING_INFORMATION 0x0A
  40. #define SMP_SECURITY_REQUEST 0x0B
  41. // Pairing Request & Response - IO Capabilities
  42. #define SMP_IO_CAP_DISPLAY_ONLY 0x00
  43. #define SMP_IO_CAP_DISPLAY_YES_NO 0x01
  44. #define SMP_IO_CAP_KEYBOARD_ONLY 0x02
  45. #define SMP_IO_CAP_NO_INPUT_NO_OUTPUT 0x03
  46. #define SMP_IO_CAP_KEYBOARD_DISPLAY 0x04
  47. // Pairing Request & Response - Out Of Bound (OOB) data flag values
  48. #define SMP_OOB_AUTH_DATA_NOT_PRESENT 0x00
  49. #define SMP_OOB_AUTH_DATA_REMOTE_DEVICE_PRESENT 0x01
  50. // Pairing Request & Response - authReq field
  51. // - This field contains 2 sub-fields:
  52. // bonding flags - bits 1 & 0
  53. #define SMP_AUTHREQ_BONDING 0x01
  54. // Man-In-The-Middle (MITM) - bit 2
  55. #define SMP_AUTHREQ_MITM 0x04
  56. #define SMP_CONFIRM_LEN 16
  57. #define SMP_RANDOM_LEN 16
  58. // Pairing Failed - "reason" field
  59. #define SMP_PAIRING_FAILED_PASSKEY_ENTRY_FAILED 0x01 //!< The user input of the passkey failed, for example, the user cancelled the operation.
  60. #define SMP_PAIRING_FAILED_OOB_NOT_AVAIL 0x02 //!< The OOB data is not available
  61. #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
  62. #define SMP_PAIRING_FAILED_CONFIRM_VALUE 0x04 //!< The confirm value doesn't match the calculated compare value
  63. #define SMP_PAIRING_FAILED_NOT_SUPPORTED 0x05 //!< Pairing isn't supported by the device
  64. #define SMP_PAIRING_FAILED_ENC_KEY_SIZE 0x06 //!< The resultant encryption key size is insufficient for the security requirements of this device.
  65. #define SMP_PAIRING_FAILED_CMD_NOT_SUPPORTED 0x07 //!< The SMP command received is not supported on this device.
  66. #define SMP_PAIRING_FAILED_UNSPECIFIED 0x08 //!< Pairing failed due to an unspecified reason
  67. #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.
  68. #define SMP_PAIRING_FAILED_LOCAL_KEY_FAILURE 0x0A // Local value - not sent over the air
  69. // Message lengths
  70. #define SMP_PAIRING_REQ_LEN 7
  71. #define SMP_PAIRING_RSP_LEN 7
  72. #define SMP_PAIRING_CONFIRM_LEN 17
  73. #define SMP_PAIRING_RANDOM_LEN 17
  74. #define SMP_PAIRING_FAILED_LEN 2
  75. #define SMP_ENCRYPTION_INFORMATION_LEN 17
  76. #define SMP_MASTER_IDENTIFICATION_LEN 11
  77. #define SMP_IDENTITY_INFORMATION_LEN 17
  78. #define SMP_IDENTITY_ADDR_INFORMATION_LEN 8
  79. #define SMP_SIGNING_INFORMATION_LEN 17
  80. #define SMP_SECURITY_REQUEST_LEN 2
  81. // Macros to use the smSendSMMsg() function to send all of the Security Manager Protocol messages
  82. #define smSendPairingReq( connHandle, msgStruct ) \
  83. smSendSMMsg( (connHandle), SMP_PAIRING_REQ_LEN, \
  84. (smpMsgs_t *)(msgStruct), \
  85. (pfnSMBuildCmd_t)(smpBuildPairingReq) )
  86. #define smSendPairingRsp( connHandle, msgStruct ) \
  87. smSendSMMsg( (connHandle), SMP_PAIRING_RSP_LEN, \
  88. (smpMsgs_t *)(msgStruct), \
  89. (pfnSMBuildCmd_t)(smpBuildPairingRsp) )
  90. #define smSendPairingConfirm( connHandle, msgStruct ) \
  91. smSendSMMsg( (connHandle), SMP_PAIRING_CONFIRM_LEN, \
  92. (smpMsgs_t *)(msgStruct), \
  93. (pfnSMBuildCmd_t)(smpBuildPairingConfirm) )
  94. #define smSendPairingRandom( connHandle, msgStruct ) \
  95. smSendSMMsg( (connHandle), SMP_PAIRING_RANDOM_LEN, \
  96. (smpMsgs_t *)(msgStruct), \
  97. (pfnSMBuildCmd_t)(smpBuildPairingRandom) )
  98. #define smSendPairingFailed( connHandle, msgStruct ) \
  99. smSendSMMsg( (connHandle), SMP_PAIRING_FAILED_LEN, \
  100. (smpMsgs_t *)(msgStruct), \
  101. (pfnSMBuildCmd_t)(smpBuildPairingFailed) )
  102. #define smSendEncInfo( connHandle, msgStruct ) \
  103. smSendSMMsg( (connHandle), SMP_ENCRYPTION_INFORMATION_LEN, \
  104. (smpMsgs_t *)(msgStruct), \
  105. (pfnSMBuildCmd_t)(smpBuildEncInfo) )
  106. #define smSendMasterID( connHandle, msgStruct ) \
  107. smSendSMMsg( (connHandle), SMP_MASTER_IDENTIFICATION_LEN, \
  108. (smpMsgs_t *)(msgStruct), \
  109. (pfnSMBuildCmd_t)(smpBuildMasterID) )
  110. #define smSendIdentityInfo( connHandle, msgStruct ) \
  111. smSendSMMsg( (connHandle), SMP_IDENTITY_INFORMATION_LEN, \
  112. (smpMsgs_t *)(msgStruct), \
  113. (pfnSMBuildCmd_t)(smpBuildIdentityInfo) )
  114. #define smSendIdentityAddrInfo( connHandle, msgStruct ) \
  115. smSendSMMsg( (connHandle), SMP_IDENTITY_ADDR_INFORMATION_LEN, \
  116. (smpMsgs_t *)(msgStruct), \
  117. (pfnSMBuildCmd_t)(smpBuildIdentityAddrInfo) )
  118. #define smSendSigningInfo( connHandle, msgStruct ) \
  119. smSendSMMsg( (connHandle), SMP_SIGNING_INFORMATION_LEN, \
  120. (smpMsgs_t *)(msgStruct), \
  121. (pfnSMBuildCmd_t)(smpBuildSigningInfo) )
  122. #define smSendSecurityReq( connHandle, msgStruct ) \
  123. smSendSMMsg( (connHandle), SMP_SECURITY_REQUEST_LEN, \
  124. (smpMsgs_t *)(msgStruct), \
  125. (pfnSMBuildCmd_t)(smpBuildSecurityReq) )
  126. /*********************************************************************
  127. * TYPEDEFS
  128. */
  129. // Pairing Request
  130. typedef struct
  131. {
  132. uint8 ioCapability; // ex. SMP_IO_CAP_DISPLAY_YES_NO
  133. uint8 oobDataFlag; // Out of Bound data flag
  134. authReq_t authReq; // Authentication fields
  135. uint8 maxEncKeySize; // Encryption Key size max bytes (7 - 16)
  136. keyDist_t keyDist; // Key Distribution Field - bit struct
  137. } smpPairingReq_t;
  138. // Pairing Response - same as Pairing Request
  139. typedef smpPairingReq_t smpPairingRsp_t;
  140. // Pairing Confirm
  141. typedef struct
  142. {
  143. uint8 confirmValue[SMP_CONFIRM_LEN];
  144. } smpPairingConfirm_t;
  145. // Pairing Random
  146. typedef struct
  147. {
  148. uint8 randomValue[SMP_RANDOM_LEN];
  149. } smpPairingRandom_t;
  150. // Pairing Failed
  151. typedef struct
  152. {
  153. uint8 reason;
  154. } smpPairingFailed_t;
  155. // Encryption Information
  156. typedef struct
  157. {
  158. uint8 ltk[KEYLEN];
  159. } smpEncInfo_t;
  160. // Master Identification
  161. typedef struct
  162. {
  163. uint16 ediv;
  164. uint16 rand[B_RANDOM_NUM_SIZE];
  165. } smpMasterID_t;
  166. // Identity Information
  167. typedef struct
  168. {
  169. uint8 irk[KEYLEN];
  170. } smpIdentityInfo_t;
  171. // Identity Address Information
  172. typedef struct
  173. {
  174. uint8 addrType;
  175. uint8 bdAddr[B_ADDR_LEN];
  176. } smpIdentityAddrInfo_t;
  177. // Signing Information
  178. typedef struct
  179. {
  180. uint8 signature[KEYLEN];
  181. } smpSigningInfo_t;
  182. // Slave Security Request
  183. typedef struct
  184. {
  185. authReq_t authReq;
  186. } smpSecurityReq_t;
  187. // Union with all of the SM messages.
  188. typedef union
  189. {
  190. smpPairingReq_t pairingReq;
  191. smpPairingReq_t pairingRsp;
  192. smpPairingConfirm_t pairingConfirm;
  193. smpPairingRandom_t pairingRandom;
  194. smpPairingFailed_t pairingFailed;
  195. smpEncInfo_t encInfo;
  196. smpMasterID_t masterID;
  197. smpIdentityInfo_t idInfo;
  198. smpIdentityAddrInfo_t idAddrInfo;
  199. smpSigningInfo_t signingInfo;
  200. smpSecurityReq_t secReq;
  201. } smpMsgs_t;
  202. typedef uint8 (*pfnSMBuildCmd_t)( smpMsgs_t *pMsgStruct, uint8 *pBuf );
  203. /*********************************************************************
  204. * GLOBAL VARIABLES
  205. */
  206. extern smpPairingReq_t pairingReg;
  207. /*********************************************************************
  208. * FUNCTIONS
  209. */
  210. /*
  211. * smpBuildPairingReq - Build an SM Pairing Request
  212. */
  213. extern bStatus_t smpBuildPairingReq( smpPairingReq_t *pPairingReq, uint8 *pBuf );
  214. /*
  215. * smpBuildPairingRsp - Build an SM Pairing Response
  216. */
  217. extern bStatus_t smpBuildPairingRsp( smpPairingRsp_t *pPairingRsp, uint8 *pBuf );
  218. /*
  219. * smpBuildPairingReqRsp - Build an SM Pairing Request or Response
  220. */
  221. extern bStatus_t smpBuildPairingReqRsp( uint8 opCode, smpPairingReq_t *pPairingReq, uint8 *pBuf );
  222. /*
  223. * smpParsePairingReq - Parse an SM Pairing Request
  224. */
  225. extern bStatus_t smpParsePairingReq( uint8 *pBuf, smpPairingReq_t *pPairingReq );
  226. /*
  227. * smpParsePairingRsp - Parse an SM Pairing Response
  228. */
  229. #define smpParsePairingRsp( a, b ) smpParsePairingReq( (a), (b) )
  230. /*
  231. * smpBuildPairingConfirm - Build an SM Pairing Confirm
  232. */
  233. extern bStatus_t smpBuildPairingConfirm( smpPairingConfirm_t *pPairingConfirm,
  234. uint8 *pBuf );
  235. /*
  236. * smpParsePairingConfirm - Parse an SM Pairing Confirm
  237. */
  238. extern bStatus_t smpParsePairingConfirm( uint8 *pBuf,
  239. smpPairingConfirm_t *pPairingConfirm );
  240. /*
  241. * smpBuildPairingRandom - Build an SM Pairing Random
  242. */
  243. extern bStatus_t smpBuildPairingRandom( smpPairingRandom_t *pPairingRandom,
  244. uint8 *pBuf );
  245. /*
  246. * smpParsePairingRandom - Parse an SM Pairing Random
  247. */
  248. extern bStatus_t smpParsePairingRandom( uint8 *pBuf,
  249. smpPairingRandom_t *pPairingRandom );
  250. /*
  251. * smpBuildPairingFailed - Build an SM Pairing Failed
  252. */
  253. extern bStatus_t smpBuildPairingFailed( smpPairingFailed_t *pPairingFailed,
  254. uint8 *pBuf );
  255. /*
  256. * smpParsePairingFailed - Parse an SM Pairing Failed
  257. */
  258. extern bStatus_t smpParsePairingFailed( uint8 *pBuf,
  259. smpPairingFailed_t *pPairingFailed );
  260. /*
  261. * smpBuildEncInfo - Build an SM Encryption Information
  262. */
  263. extern bStatus_t smpBuildEncInfo( smpEncInfo_t *pEncInfo, uint8 *pBuf );
  264. /*
  265. * smpParseEncInfo - Parse an SM Encryption Information
  266. */
  267. extern bStatus_t smpParseEncInfo( uint8 *buf, smpEncInfo_t *encInfo );
  268. /*
  269. * smpBuildMasterID - Build an SM Master Identification
  270. */
  271. extern bStatus_t smpBuildMasterID( smpMasterID_t *pMasterID, uint8 *pBuf );
  272. /*
  273. * smpParseMasterID - Parse an SM Master Identification
  274. */
  275. extern bStatus_t smpParseMasterID( uint8 *pBuf, smpMasterID_t *pMasterID );
  276. /*
  277. * smpBuildIdentityInfo - Build an SM Identity Information
  278. */
  279. extern bStatus_t smpBuildIdentityInfo( smpIdentityInfo_t *pIdInfo, uint8 *pBuf );
  280. /*
  281. * smpBuildIdentityAddrInfo - Build an SM Identity Address Information
  282. */
  283. extern bStatus_t smpBuildIdentityAddrInfo( smpIdentityAddrInfo_t *pIdInfo, uint8 *pBuf );
  284. /*
  285. * smpParseIdentityInfo - Parse an SM Identity Information
  286. */
  287. extern bStatus_t smpParseIdentityInfo( uint8 *pBuf, smpIdentityInfo_t *pIdInfo );
  288. /*
  289. * smpParseIdentityAddrInfo - Parse an SM Identity Address Information
  290. */
  291. extern bStatus_t smpParseIdentityAddrInfo( uint8 *pBuf, smpIdentityAddrInfo_t *pIdInfo );
  292. /*
  293. * smpBuildSigningInfo - Build an SM Signing Information
  294. */
  295. extern bStatus_t smpBuildSigningInfo( smpSigningInfo_t *pSigningInfo, uint8 *pBuf );
  296. /*
  297. * smpParseSigningInfo - Parse an SM Signing Information
  298. */
  299. extern bStatus_t smpParseSigningInfo( uint8 *pBuf, smpSigningInfo_t *pSigningInfo );
  300. /*
  301. * smpBuildSecurityReq - Build an SM Slave Security Request
  302. */
  303. extern bStatus_t smpBuildSecurityReq( smpSecurityReq_t *pSecReq, uint8 *pBuf );
  304. /*
  305. * smpParseSecurityReq - Parse an SM Slave Security Request
  306. */
  307. extern bStatus_t smpParseSecurityReq( uint8 *pBuf, smpSecurityReq_t *pSecReq );
  308. /*
  309. * smSendSMMsg - Generic Send L2CAP SM message function
  310. */
  311. extern bStatus_t smSendSMMsg( uint16 connHandle, uint8 bufLen, smpMsgs_t *pMsg, pfnSMBuildCmd_t buildFn );
  312. /*********************************************************************
  313. *********************************************************************/
  314. #ifdef __cplusplus
  315. }
  316. #endif
  317. #endif /* SMP_H */