ant_encrypt_config.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef ANT_ENCRYPT_CONFIG__
  41. #define ANT_ENCRYPT_CONFIG__
  42. /**@file
  43. *
  44. * @defgroup ant_encrypt_config ANT encryption configuration
  45. * @{
  46. * @ingroup ant_sdk_utils
  47. *
  48. * @brief Encryption configuration for the ANT stack and channels.
  49. *
  50. */
  51. #include <stdint.h>
  52. #include "sdk_errors.h"
  53. #include "nrf_sdh_ant.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /** @name Advanced burst configuration for encryption modules
  58. * @{
  59. */
  60. #define ADV_BURST_CFG_MIN_SIZE 8 ///< Minimum size of the advance burst configuration data.
  61. #define ADV_BURST_CFG_PACKET_SIZE_INDEX 1 ///< Index of the packet size field in the configuration data.
  62. #define ADV_BURST_CFG_REQUIRED_FEATURES 2 ///< Index of the required features field in the configuration data.
  63. #define ADV_BURST_CFG_OPTIONAL_FEATURES 5 ///< Index of the optional features field in the configuration data.
  64. /**@} */
  65. /** @brief ANT channel cryptographic configuration. */
  66. typedef struct
  67. {
  68. uint8_t mode; ///< Encryption mode. See the encrypted channel defines in ant_parameters.h.
  69. uint8_t key_index; ///< Index of encryption key.
  70. uint8_t decimation_rate; ///< Division of the master channel rate by the slave’s tracking channel rate.
  71. } ant_encrypt_channel_settings_t;
  72. /** @brief ANT encryption information. */
  73. typedef union
  74. {
  75. uint8_t * pp_array[3]; // For array access support.
  76. struct
  77. {
  78. uint8_t * p_encryption_id; ///< Pointer to the encryption ID of the device (4 bytes).
  79. uint8_t * p_user_info; ///< Pointer to the user information string (19 bytes).
  80. uint8_t * p_random_num_seed; ///< Pointer to the random number seed (16 bytes).
  81. } items;
  82. } ant_encrypt_info_settings_t;
  83. /** @brief Advanced burst settings used by the encrypted channel. */
  84. typedef struct
  85. {
  86. uint8_t packet_length; ///< RF payload size. See the advanced burst configuration defines in ant_parameters.h.
  87. uint8_t required_feature; ///< Required advanced burst modes. See the advanced burst configuration defines in ant_parameters.h.
  88. uint8_t optional_feature; ///< Optional advanced burst modes. See the advanced burst configuration defines in ant_parameters.h.
  89. } ant_encrypt_adv_burst_settings_t;
  90. /**@brief ANT stack cryptographic configuration. */
  91. typedef struct
  92. {
  93. ant_encrypt_info_settings_t info; ///< Pointer to the encryption information structure.
  94. uint8_t * * pp_key; ///< Pointer to an array for pointers to encryption keys. Each key must have a length of 16 bytes.
  95. uint8_t key_number; ///< Number of encryption keys.
  96. ant_encrypt_adv_burst_settings_t * p_adv_burst_config; ///< Advanced burst configuration. If NULL, the advanced burst must be configured externally.
  97. } ant_encrypt_stack_settings_t;
  98. /**
  99. * @brief ANT encryption negotiation events.
  100. */
  101. typedef enum
  102. {
  103. ANT_ENC_EVT_NEGOTIATION_SUCCESS, ///< Negotiation success.
  104. ANT_ENC_EVT_NEGOTIATION_FAIL, ///< Negotiation failure.
  105. ANT_ENC_EVT_CHANNEL_LOST ///< Lost a channel. It's relevant only for slave channels.
  106. } ant_encrypt_user_evt_t;
  107. /**
  108. * @brief Event handler for ANT encryption user events.
  109. */
  110. typedef void (* ant_encryp_user_handler_t)(uint8_t channel, ant_encrypt_user_evt_t event);
  111. /**
  112. * @brief Macro for initializing an ANT encryption information structure.
  113. *
  114. * @param[in] P_ENC_ID Pointer to the encryption ID of the device (4 bytes).
  115. * @param[in] P_USER_INFO Pointer to the user information string (19 bytes).
  116. * @param[in] P_RAND_NUM_SEED Pointer to the random number seed (16 bytes).
  117. */
  118. #define ANT_CRYPTO_INFO_SETTINGS_INIT(P_ENC_ID, P_USER_INFO, P_RAND_NUM_SEED) \
  119. { \
  120. .items = \
  121. { \
  122. .p_encryption_id = P_ENC_ID, \
  123. .p_user_info = P_USER_INFO, \
  124. .p_random_num_seed = P_RAND_NUM_SEED \
  125. } \
  126. }
  127. /**
  128. * @brief Macro for declaring the basic cryptographic configuration for the ANT stack.
  129. *
  130. * This macro configures the following settings:
  131. * - Cryptographic key
  132. * - Encryption ID
  133. * - Advanced burst mode with the maximum RF payload size
  134. *
  135. * Use @ref ANT_ENCRYPT_STACK_SETTINGS_BASE to access the created configuration instance.
  136. *
  137. * @param[in] NAME Name for the created data instance.
  138. * @param[in] P_KEY Pointer to the cryptographic key (16 bytes).
  139. * @param[in] P_ENC_ID Pointer to the encryption ID (4 bytes).
  140. */
  141. #define ANT_ENCRYPT_STACK_SETTINGS_BASE_DEF(NAME, P_KEY, P_ENC_ID) \
  142. ant_encrypt_adv_burst_settings_t NAME##_ant_enc_adv_burst_set = \
  143. { \
  144. .packet_length = ADV_BURST_MODES_MAX_SIZE, \
  145. .required_feature = 0, \
  146. .optional_feature = 0 \
  147. }; \
  148. uint8_t * pp_##NAME##_key[1] = {P_KEY}; \
  149. ant_encrypt_stack_settings_t NAME ## _ant_crypto_settings = \
  150. { \
  151. .info = ANT_CRYPTO_INFO_SETTINGS_INIT(P_ENC_ID, NULL, NULL), \
  152. .pp_key = pp_##NAME##_key, \
  153. .key_number = 1, \
  154. .p_adv_burst_config = &NAME##_ant_enc_adv_burst_set \
  155. }
  156. /** @brief Macro for accessing the configuration instance created
  157. * by @ref ANT_ENCRYPT_STACK_SETTINGS_BASE_DEF.
  158. *
  159. * @param[in] NAME Name of the settings instance.
  160. */
  161. #define ANT_ENCRYPT_STACK_SETTINGS_BASE(NAME) (NAME##_ant_crypto_settings)
  162. /**
  163. * @brief Function for applying an encryption configuration to a slave channel.
  164. *
  165. * This function enables encryption on a channel.
  166. *
  167. * This function should be used by the @ref ant_encrypt_negotiation_slave module and this module.
  168. *
  169. * @param[in] channel_number ANT channel number.
  170. * @param[in] p_crypto_config Pointer to the encryption configuration.
  171. *
  172. * @return Value returned by @ref sd_ant_crypto_channel_enable (for example, NRF_SUCCESS if
  173. * the configuration was successful).
  174. */
  175. ret_code_t ant_channel_encrypt_config_perform(uint8_t channel_number,
  176. ant_encrypt_channel_settings_t * p_crypto_config);
  177. /**
  178. * @brief Function for applying an encryption configuration to a master or slave channel.
  179. *
  180. * When called for a master channel, this function enables encryption
  181. * for that channel. When called for a slave channel, it saves
  182. * the encryption configuration for future use.
  183. *
  184. * This function should be used by the @ref ant_channel_config module.
  185. *
  186. * @param[in] channel_type ANT channel type: CHANNEL_TYPE_SLAVE or CHANNEL_TYPE_MASTER.
  187. * @param[in] channel_num ANT channel number.
  188. * @param[in] p_crypto_config Pointer to the encryption configuration.
  189. *
  190. * @retval NRF_SUCCESS If the function completed successfully.
  191. * @retval NRF_ERROR_INVALID_PARAM If the channel type is invalid.
  192. * @retval NRF_ERROR_MODULE_NOT_INITIALIZED If the stack is not configured for encryption.
  193. * @retval Other Otherwise, the error value returned by the @ref
  194. * ant_channel_encrypt_config_perform function is returned.
  195. */
  196. ret_code_t ant_channel_encrypt_config(uint8_t channel_type,
  197. uint8_t channel_num,
  198. ant_encrypt_channel_settings_t * p_crypto_config);
  199. /**
  200. * @brief Function for configuring the cryptographic settings of the ANT stack.
  201. *
  202. * @param[in] p_crypto_info_set Pointer to the settings.
  203. */
  204. ret_code_t ant_stack_encryption_config(ant_encrypt_stack_settings_t const * const p_crypto_info_set);
  205. /**
  206. * @brief Function for registering an event handler for ANT encryption events.
  207. *
  208. * The event handler should support all of the events in @ref ant_encrypt_user_evt_t.
  209. *
  210. * @param[in] p_handler Pointer to a handler function.
  211. */
  212. void ant_enc_event_handler_register(ant_encryp_user_handler_t p_handler);
  213. /**
  214. * @}
  215. */
  216. #ifdef __cplusplus
  217. }
  218. #endif
  219. #endif // ANT_ENCRYPT_CONFIG__