zcl_key_establish.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**************************************************************************************************
  2. Filename: zcl_key_establish.h
  3. Revised: $Date: 2012-04-02 17:02:19 -0700 (Mon, 02 Apr 2012) $
  4. Revision: $Revision: 29996 $
  5. Description: This file contains the ZCL General Function Domain, key
  6. establishment cluster definitions.
  7. Copyright 2007-2011 Texas Instruments Incorporated. All rights reserved.
  8. IMPORTANT: Your use of this Software is limited to those specific rights
  9. granted under the terms of a software license agreement between the user
  10. who downloaded the software, his/her employer (which must be your employer)
  11. and Texas Instruments Incorporated (the "License"). You may not use this
  12. Software unless you agree to abide by the terms of the License. The License
  13. limits your use, and you acknowledge, that the Software may not be modified,
  14. copied or distributed unless embedded on a Texas Instruments microcontroller
  15. or used solely and exclusively in conjunction with a Texas Instruments radio
  16. frequency transceiver, which is integrated into your product. Other than for
  17. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18. works of, modify, distribute, perform, display or sell this Software and/or
  19. its documentation for any purpose.
  20. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  31. Should you have any questions regarding your right to use this Software,
  32. contact Texas Instruments Incorporated at www.TI.com.
  33. **************************************************************************************************/
  34. #ifndef ZCL_KEY_ESTABLISH_H
  35. #define ZCL_KEY_ESTABLISH_H
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40. /*********************************************************************
  41. * INCLUDES
  42. */
  43. #include "zcl.h"
  44. #include "eccapi.h"
  45. /*********************************************************************
  46. * CONSTANTS
  47. */
  48. #define ZCL_KEY_ESTABLISHMENT_ENDPOINT 10 // Reserved endpoint for key establishment cluster
  49. // Key Establishment Task Events
  50. #define KEY_ESTABLISHMENT_REC_AGING_EVT 0x01
  51. #define KEY_ESTABLISHMENT_CMD_PROCESS_EVT 0x02
  52. #define KEY_ESTABLISHMENT_RSP_PROCESS_EVT 0x04
  53. #define KEY_ESTABLISHMENT_REC_AGING_INTERVAL 1000 // in ms
  54. #define KEY_ESTABLISHMENT_WAIT_PERIOD 500
  55. // Key Establishment Cluster Attributes
  56. #define ATTRID_KEY_ESTABLISH_SUITE 0x0000
  57. #define CERTIFICATE_BASED_KEY_ESTABLISHMENT 0x0000
  58. // Key Establishment Cluster Command ID
  59. #define COMMAND_INITIATE_KEY_ESTABLISHMENT 0x00
  60. #define COMMAND_EPHEMERAL_DATA_REQUEST 0x01
  61. #define COMMAND_CONFIRM_KEY 0x02
  62. #define COMMAND_TERMINATE_KEY_ESTABLISHMENT 0x03
  63. #define COMMAND_INITIATE_KEY_ESTABLISHMENT_RESPONSE 0x00
  64. #define COMMAND_EPHEMERAL_DATA_RESPONSE 0x01
  65. #define COMMAND_CONFIRM_KEY_RESPONSE 0x02
  66. #define COMMAND_GET_LINK_KEY 0x04
  67. // Command Packet Length
  68. #define PACKET_LEN_INITIATE_KEY_EST_REQ (4+ZCL_KE_IMPLICIT_CERTIFICATE_LEN)
  69. #define PACKET_LEN_INITIATE_KEY_EST_RSP (4+ZCL_KE_IMPLICIT_CERTIFICATE_LEN)
  70. // Length of the implicit certificate
  71. #define KEY_ESTABLISH_SHARED_SECRET_LENGTH 21
  72. #define KEY_ESTABLISH_KEY_DATA_LENGTH 16
  73. #define KEY_ESTABLISH_MAC_KEY_LENGTH 16
  74. #define KEY_ESTABLISH_MAC_LENGTH 16
  75. #define KEY_ESTABLISH_POINT_ORDER_SIZE 21
  76. #define KEY_ESTABLISH_AES_MMO_HASH_SIZE 16
  77. #define KEY_ESTABLISH_CERT_EXT_ADDR_IDX 22
  78. #define KEY_ESTABLISH_CERT_ISSUER_IDX 30
  79. #define KEY_ESTABLISH_CERT_IDX 4
  80. #define KEY_ESTABLISH_CERT_ISSUER_LENTGH Z_EXTADDR_LEN
  81. // Max number of entries in the Key Establishment Rec Table
  82. #define MAX_KEY_ESTABLISHMENT_REC_ENTRY 2
  83. #define INVALID_PARTNER_ADDR 0xFFFE
  84. // Key Establishment Device Role
  85. #define KEY_ESTABLISHMENT_INITIATOR 0
  86. #define KEY_ESTABLISHMENT_RESPONDER 1
  87. #define KEY_ESTABLISHMENT_REC_EXPIRY_TIME 0xFF // In seconds
  88. #define KEY_ESTABLISHMENT_EPH_DATA_GEN_INVALID_TIME 0xFF // In seconds
  89. #define KEY_ESTABLISHMENT_CONF_KEY_GEN_INVALID_TIME 0xFF // In seconds
  90. // The time out for generating the key bits and mac (in sec)
  91. #if !defined ( ZCL_KEY_ESTABLISHMENT_KEY_GENERATE_TIMEOUT )
  92. #define ZCL_KEY_ESTABLISHMENT_KEY_GENERATE_TIMEOUT 4
  93. #endif
  94. #if !defined ( ZCL_KEY_ESTABLISHMENT_MAC_GENERATE_TIMEOUT )
  95. #define ZCL_KEY_ESTABLISHMENT_MAC_GENERATE_TIMEOUT 1
  96. #endif
  97. #if !defined ( ZCL_KEY_ESTABLISHMENT_EKEY_GENERATE_TIMEOUT )
  98. #define ZCL_KEY_ESTABLISHMENT_EKEY_GENERATE_TIMEOUT 1
  99. #endif
  100. // The poll rate for end device is set to this value
  101. // during the key establishment procedure
  102. #if !defined (ZCL_KEY_ESTABLISH_POLL_RATE)
  103. #define ZCL_KEY_ESTABLISH_POLL_RATE 1000
  104. #endif
  105. /*********************************************************************
  106. * TYPEDEFS
  107. */
  108. // The format of a Key Establishment Record
  109. typedef struct
  110. {
  111. afAddrType_t dstAddr; // Partner Address
  112. uint8 lastSeqNum;
  113. uint8 appTaskID; // Task ID of the application that initiates key establishment
  114. uint8 partnerExtAddr[Z_EXTADDR_LEN];
  115. uint8 role; // 0 @ initiator
  116. // 1 @ responder
  117. uint8 age; // Age in seconds
  118. uint8 state; // State
  119. // Key information
  120. uint8 *pLocalEPrivateKey;
  121. uint8 *pLocalEPublicKey;
  122. uint8 *pRemotePublicKey;
  123. uint8 *pRemoteCertificate;
  124. uint8 *pKey;
  125. uint8 *pMacKey;
  126. uint8 remoteEphDataGenTime; // partner Ephemeral Data Generate Time
  127. uint8 remoteConfKeyGenTime; // partner Confirm Key Generate Time
  128. } zclKeyEstablishRec_t;
  129. // Key Establishment Procedure internal State
  130. typedef enum
  131. {
  132. KeyEstablishState_Idle = 0,
  133. KeyEstablishState_InitiatePending, // Waiting for Initiate Key Establishment Rsp
  134. KeyEstablishState_EDataPending, // Waiting for the Ephemeral data
  135. KeyEstablishState_KeyCalculatePending,// Waiting for the key to be calcuated
  136. KeyEstablishState_ConfirmPending, // Waiting for Confirm Response
  137. KeyEstablishState_TerminationPending // Waiting for Terminate command
  138. } KeyEstablishState_t;
  139. // Terminate Key Establishment Status
  140. typedef enum
  141. {
  142. TermKeyStatus_Success = 0,
  143. TermKeyStatus_UnknowIssuer,
  144. TermKeyStatus_BadKeyConfirm,
  145. TermKeyStatus_BadMessage,
  146. TermKeyStatus_NoResources,
  147. TermKeyStatus_UnSupportedSuite
  148. } TermKeyStatus_t;
  149. // Osal message format of indication for key establishment completion
  150. typedef struct
  151. {
  152. osal_event_hdr_t hdr;
  153. uint8 waitTime;
  154. uint16 keyEstablishmentSuite;
  155. } keyEstablishmentInd_t;
  156. /*********************************************************************
  157. * FUNCTION MACROS
  158. */
  159. /*********************************************************************
  160. * FUNCTIONS
  161. */
  162. /*
  163. * Key Establishment Task initialization function
  164. */
  165. extern void zclGeneral_KeyEstablish_Init( uint8 task_id );
  166. /*
  167. * Event process for Key Establishment Task
  168. */
  169. extern uint16 zclKeyEstablish_event_loop( uint8 task_id, uint16 events );
  170. /*
  171. * Call to initiate key establishment procedure with partner device
  172. */
  173. extern ZStatus_t zclGeneral_KeyEstablish_InitiateKeyEstablishment(uint8 appTaskID,
  174. afAddrType_t *partnerAddr,
  175. uint8 seqNum);
  176. /*
  177. * Send Initiate Key Establishment Command
  178. */
  179. extern ZStatus_t zclGeneral_KeyEstablish_Send_InitiateKeyEstablishment( uint8 srcEP, afAddrType_t *dstAddr,
  180. uint16 keyEstablishmentSuite,
  181. uint8 keyGenerateTime,
  182. uint8 macGenerateTime,
  183. uint8 *certificate,
  184. uint8 disableDefaultRsp, uint8 seqNum );
  185. /*
  186. * Send Ephemeral Data Request
  187. */
  188. extern ZStatus_t zclGeneral_KeyEstablish_Send_EphemeralDataReq( uint8 srcEP, afAddrType_t *dstAddr,
  189. uint8 *eData,
  190. uint8 disableDefaultRsp, uint8 seqNum );
  191. /*
  192. * Send Confirm Key Command
  193. */
  194. extern ZStatus_t zclGeneral_KeyEstablish_Send_ConfirmKey( uint8 srcEP, afAddrType_t *dstAddr,
  195. uint8 *mac,
  196. uint8 disableDefaultRsp, uint8 seqNum );
  197. /*
  198. * Send Terminate Key Establishment Command
  199. */
  200. extern ZStatus_t zclGeneral_KeyEstablish_Send_TerminateKeyEstablishment( uint8 srcEP,
  201. afAddrType_t *dstAddr,
  202. TermKeyStatus_t status,
  203. uint8 waitTime,
  204. uint16 keyEstablishmentSuite, uint8 direction,
  205. uint8 disableDefaultRsp, uint8 seqNum );
  206. /*
  207. * Send Initiate Key Establishment Response
  208. */
  209. extern ZStatus_t zclGeneral_KeyEstablish_Send_InitiateKeyEstablishmentRsp( uint8 srcEP, afAddrType_t *dstAddr,
  210. uint16 keyEstablishmentSuite,
  211. uint8 keyGenerateTime,
  212. uint8 macGenerateTime,
  213. uint8 *certificate,
  214. uint8 disableDefaultRsp, uint8 seqNum );
  215. /*
  216. * Send Ephemeral Data Response
  217. */
  218. extern ZStatus_t zclGeneral_KeyEstablish_Send_EphemeralDataRsp( uint8 srcEP, afAddrType_t *dstAddr,
  219. uint8 *eData,
  220. uint8 disableDefaultRsp, uint8 seqNum );
  221. /*
  222. * Send Confirm Key Response
  223. */
  224. extern ZStatus_t zclGeneral_KeyEstablish_Send_ConfirmKeyRsp( uint8 srcEP, afAddrType_t *dstAddr,
  225. uint8 *mac,
  226. uint8 disableDefaultRsp, uint8 seqNum );
  227. /*
  228. * Sign the message using static private key
  229. */
  230. extern ZStatus_t zclGeneral_KeyEstablishment_ECDSASign( uint8 *input, uint8 inputLen,
  231. uint8 *output);
  232. /*
  233. * Verify the signature of the message digest
  234. */
  235. extern ZStatus_t zclGeneral_KeyEstablishment_ECDSAVerify( uint8 *input, uint8 inputLen,
  236. uint8 *signature);
  237. /*
  238. * Register the user defined yielding function
  239. */
  240. extern void zclGeneral_KeyEstablishment_RegYieldCB( YieldFunc *pFnYield,
  241. uint8 yieldLevel );
  242. /*********************************************************************
  243. *********************************************************************/
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif /* ZCL_KEY_ESTABLISH_H */