ant_encrypt_config.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef ANT_ENCRYPT_CONFIG__
  13. #define ANT_ENCRYPT_CONFIG__
  14. /**@file
  15. *
  16. * @defgroup ant_encrypt_config ANT encryption configuration
  17. * @{
  18. * @ingroup ant_sdk_utils
  19. *
  20. * @brief Encryption configuration for the ANT stack and channels.
  21. *
  22. */
  23. #include <stdint.h>
  24. #include "sdk_errors.h"
  25. #include "ant_stack_handler_types.h"
  26. /** @name Advanced burst configuration for encryption modules
  27. * @{
  28. */
  29. #define ADV_BURST_CFG_MIN_SIZE 8 ///< Minimum size of the advance burst configuration data.
  30. #define ADV_BURST_CFG_PACKET_SIZE_INDEX 1 ///< Index of the packet size field in the configuration data.
  31. #define ADV_BURST_CFG_REQUIRED_FEATURES 2 ///< Index of the required features field in the configuration data.
  32. #define ADV_BURST_CFG_OPTIONAL_FEATURES 5 ///< Index of the optional features field in the configuration data.
  33. /**@} */
  34. /** @brief ANT channel cryptographic configuration. */
  35. typedef struct
  36. {
  37. uint8_t mode; ///< Encryption mode. See the encrypted channel defines in ant_parameters.h.
  38. uint8_t key_index; ///< Index of encryption key.
  39. uint8_t decimation_rate; ///< Division of the master channel rate by the slave’s tracking channel rate.
  40. } ant_encrypt_channel_settings_t;
  41. /** @brief ANT encryption information. */
  42. typedef union
  43. {
  44. uint8_t * pp_array[3]; // For array access support.
  45. struct
  46. {
  47. uint8_t * p_encryption_id; ///< Pointer to the encryption ID of the device (4 bytes).
  48. uint8_t * p_user_info; ///< Pointer to the user information string (19 bytes).
  49. uint8_t * p_random_num_seed; ///< Pointer to the random number seed (16 bytes).
  50. } items;
  51. } ant_encrypt_info_settings_t;
  52. /** @brief Advanced burst settings used by the encrypted channel. */
  53. typedef struct
  54. {
  55. uint8_t packet_length; ///< RF payload size. See the advanced burst configuration defines in ant_parameters.h.
  56. uint8_t required_feature; ///< Required advanced burst modes. See the advanced burst configuration defines in ant_parameters.h.
  57. uint8_t optional_feature; ///< Optional advanced burst modes. See the advanced burst configuration defines in ant_parameters.h.
  58. } ant_encrypt_adv_burst_settings_t;
  59. /**@brief ANT stack cryptographic configuration. */
  60. typedef struct
  61. {
  62. ant_encrypt_info_settings_t info; ///< Pointer to the encryption information structure.
  63. uint8_t * * pp_key; ///< Pointer to an array for pointers to encryption keys. Each key must have a length of 16 bytes.
  64. uint8_t key_number; ///< Number of encryption keys.
  65. ant_encrypt_adv_burst_settings_t * p_adv_burst_config; ///< Advanced burst configuration. If NULL, the advanced burst must be configured externally.
  66. } ant_encrypt_stack_settings_t;
  67. /**
  68. * @brief ANT encryption negotiation events.
  69. */
  70. typedef enum
  71. {
  72. ANT_ENC_EVT_NEGOTIATION_SUCCESS, ///< Negotiation success.
  73. ANT_ENC_EVT_NEGOTIATION_FAIL, ///< Negotiation failure.
  74. ANT_ENC_EVT_CHANNEL_LOST ///< Lost a channel. It's relevant only for slave channels.
  75. } ant_encrypt_user_evt_t;
  76. /**
  77. * @brief Event handler for ANT encryption user events.
  78. */
  79. typedef void (* ant_encryp_user_handler_t)(uint8_t channel, ant_encrypt_user_evt_t event);
  80. /**
  81. * @brief Macro for initializing an ANT encryption information structure.
  82. *
  83. * @param[in] P_ENC_ID Pointer to the encryption ID of the device (4 bytes).
  84. * @param[in] P_USER_INFO Pointer to the user information string (19 bytes).
  85. * @param[in] P_RAND_NUM_SEED Pointer to the random number seed (16 bytes).
  86. */
  87. #define ANT_CRYPTO_INFO_SETTINGS_INIT(P_ENC_ID, P_USER_INFO, P_RAND_NUM_SEED) \
  88. { \
  89. .items = \
  90. { \
  91. .p_encryption_id = P_ENC_ID, \
  92. .p_user_info = P_USER_INFO, \
  93. .p_random_num_seed = P_RAND_NUM_SEED \
  94. } \
  95. }
  96. /**
  97. * @brief Macro for declaring the basic cryptographic configuration for the ANT stack.
  98. *
  99. * This macro configures the following settings:
  100. * - Cryptographic key
  101. * - Encryption ID
  102. * - Advanced burst mode with the maximum RF payload size
  103. *
  104. * Use @ref ANT_ENCRYPT_STACK_SETTINGS_BASE to access the created configuration instance.
  105. *
  106. * @param[in] NAME Name for the created data instance.
  107. * @param[in] P_KEY Pointer to the cryptographic key (16 bytes).
  108. * @param[in] P_ENC_ID Pointer to the encryption ID (4 bytes).
  109. */
  110. #define ANT_ENCRYPT_STACK_SETTINGS_BASE_DEF(NAME, P_KEY, P_ENC_ID) \
  111. ant_encrypt_adv_burst_settings_t NAME##_ant_enc_adv_burst_set = \
  112. { \
  113. .packet_length = ADV_BURST_MODES_MAX_SIZE, \
  114. .required_feature = 0, \
  115. .optional_feature = 0 \
  116. }; \
  117. uint8_t * pp_##NAME##_key[1] = {P_KEY}; \
  118. ant_encrypt_stack_settings_t NAME ## _ant_crypto_settings = \
  119. { \
  120. .info = ANT_CRYPTO_INFO_SETTINGS_INIT(P_ENC_ID, NULL, NULL), \
  121. .pp_key = pp_##NAME##_key, \
  122. .key_number = 1, \
  123. .p_adv_burst_config = &NAME##_ant_enc_adv_burst_set \
  124. }
  125. /** @brief Macro for accessing the configuration instance created
  126. * by @ref ANT_ENCRYPT_STACK_SETTINGS_BASE_DEF.
  127. *
  128. * @param[in] NAME Name of the settings instance.
  129. */
  130. #define ANT_ENCRYPT_STACK_SETTINGS_BASE(NAME) (NAME##_ant_crypto_settings)
  131. /**
  132. * @brief Function for applying an encryption configuration to a slave channel.
  133. *
  134. * This function enables encryption on a channel.
  135. *
  136. * This function should be used by the @ref ant_encrypt_negotiation_slave module and this module.
  137. *
  138. * @param[in] channel_number ANT channel number.
  139. * @param[in] p_crypto_config Pointer to the encryption configuration.
  140. *
  141. * @return Value returned by @ref sd_ant_crypto_channel_enable (for example, NRF_SUCCESS if
  142. * the configuration was successful).
  143. */
  144. ret_code_t ant_channel_encrypt_config_perform(uint8_t channel_number,
  145. ant_encrypt_channel_settings_t * p_crypto_config);
  146. /**
  147. * @brief Function for applying an encryption configuration to a master or slave channel.
  148. *
  149. * When called for a master channel, this function enables encryption
  150. * for that channel. When called for a slave channel, it saves
  151. * the encryption configuration for future use.
  152. *
  153. * This function should be used by the @ref ant_sdk_channel_config module.
  154. *
  155. * @param[in] channel_type ANT channel type: CHANNEL_TYPE_SLAVE or CHANNEL_TYPE_MASTER.
  156. * @param[in] channel_num ANT channel number.
  157. * @param[in] p_crypto_config Pointer to the encryption configuration.
  158. *
  159. * @retval NRF_SUCCESS If the function completed successfully.
  160. * @retval NRF_ERROR_INVALID_PARAM If the channel type is invalid.
  161. * @retval MODULE_NOT_INITIALZED If the stack is not configured for encryption.
  162. * @retval Other Otherwise, the error value returned by the
  163. * @ref ant_channel_encrypt_config_perform function is returned.
  164. */
  165. ret_code_t ant_channel_encrypt_config(uint8_t channel_type,
  166. uint8_t channel_num,
  167. ant_encrypt_channel_settings_t * p_crypto_config);
  168. /**
  169. * @brief Function for configuring the cryptographic settings of the ANT stack.
  170. *
  171. * @param[in] p_crypto_info_set Pointer to the settings.
  172. */
  173. ret_code_t ant_stack_encryption_config(ant_encrypt_stack_settings_t const * const p_crypto_info_set);
  174. /**
  175. * @brief Function for handling ANT encryption events.
  176. *
  177. * This function should be used directly in the ANT event dispatching process.
  178. * It serves the ANT encryption events to the registered event handler.
  179. * If @ref ant_encrypt_negotiation_slave is used, this function is required.
  180. *
  181. * This function should be used by the @ref ant_encrypt_config module.
  182. *
  183. * @param[in] p_ant_evt Pointer to the ANT stack event message structure.
  184. */
  185. void ant_encrypt_event_handler(ant_evt_t * p_ant_evt);
  186. /**
  187. * @brief Function for registering an event handler for ANT encryption events.
  188. *
  189. * The event handler should support all of the events in @ref ant_encrypt_user_evt_t.
  190. *
  191. * @param[in] p_handler Pointer to a handler function.
  192. */
  193. void ant_enc_event_handler_register(ant_encryp_user_handler_t p_handler);
  194. /**
  195. * @}
  196. */
  197. #endif // ANT_ENCRYPT_CONFIG__