ant_encrypt_config.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Copyright (c) 2015 - 2019, 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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(ANT_ENCRYPT_CONFIG)
  42. #include <stdlib.h>
  43. #include "ant_encrypt_config.h"
  44. #include "ant_interface.h"
  45. #include "ant_parameters.h"
  46. #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED
  47. #include "ant_encrypt_negotiation_slave.h"
  48. #endif
  49. /*lint -e551 -save*/
  50. /** Flag for checking if stack was configured for encryption. */
  51. static bool m_stack_encryption_configured = false;
  52. /*lint -restore */
  53. /** Pointer to handler of module's events. */
  54. static ant_encryp_user_handler_t m_ant_enc_evt_handler = NULL;
  55. static ret_code_t ant_enc_advance_burs_config_apply(
  56. ant_encrypt_adv_burst_settings_t const * const p_adv_burst_set);
  57. ret_code_t ant_stack_encryption_config(ant_encrypt_stack_settings_t const * const p_crypto_set)
  58. {
  59. ret_code_t err_code;
  60. for ( uint32_t i = 0; i < p_crypto_set->key_number; i++)
  61. {
  62. err_code = sd_ant_crypto_key_set(i, p_crypto_set->pp_key[i]);
  63. VERIFY_SUCCESS(err_code);
  64. }
  65. if (p_crypto_set->p_adv_burst_config != NULL)
  66. {
  67. err_code = ant_enc_advance_burs_config_apply(p_crypto_set->p_adv_burst_config);
  68. VERIFY_SUCCESS(err_code);
  69. }
  70. // subcomands LUT for @ref sd_ant_crypto_info_set calls
  71. const uint8_t set_enc_info_param_lut[] =
  72. {
  73. ENCRYPTION_INFO_SET_CRYPTO_ID,
  74. ENCRYPTION_INFO_SET_CUSTOM_USER_DATA,
  75. ENCRYPTION_INFO_SET_RNG_SEED
  76. };
  77. for ( uint32_t i = 0; i < sizeof(set_enc_info_param_lut); i++)
  78. {
  79. if ( p_crypto_set->info.pp_array[i] != NULL)
  80. {
  81. err_code = sd_ant_crypto_info_set(set_enc_info_param_lut[i],
  82. p_crypto_set->info.pp_array[i]);
  83. VERIFY_SUCCESS(err_code);
  84. }
  85. }
  86. #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED
  87. // all ANT channels have unsupported slave encryption tracking (even master's channel)
  88. ant_channel_encryp_negotiation_slave_init();
  89. #endif
  90. m_ant_enc_evt_handler = NULL;
  91. m_stack_encryption_configured = true;
  92. return NRF_SUCCESS;
  93. }
  94. /**
  95. * @brief Function for configuring advanced burst settings according to encryption requirements.
  96. *
  97. * @param p_adv_burst_set Pointer to ANT advanced burst settings.
  98. *
  99. * @retval Value returned by @ref sd_ant_adv_burst_config_set.
  100. */
  101. static ret_code_t ant_enc_advance_burs_config_apply(
  102. ant_encrypt_adv_burst_settings_t const * const p_adv_burst_set)
  103. {
  104. uint8_t adv_burst_conf_str[ADV_BURST_CFG_MIN_SIZE] =
  105. { ADV_BURST_MODE_ENABLE, 0, 0, 0, 0, 0, 0, 0 };
  106. adv_burst_conf_str[ADV_BURST_CFG_PACKET_SIZE_INDEX] = p_adv_burst_set->packet_length;
  107. adv_burst_conf_str[ADV_BURST_CFG_REQUIRED_FEATURES] = p_adv_burst_set->required_feature;
  108. adv_burst_conf_str[ADV_BURST_CFG_OPTIONAL_FEATURES] = p_adv_burst_set->optional_feature;
  109. return sd_ant_adv_burst_config_set(adv_burst_conf_str, sizeof(adv_burst_conf_str));
  110. }
  111. ret_code_t ant_channel_encrypt_config_perform(uint8_t channel_number,
  112. ant_encrypt_channel_settings_t * p_crypto_config)
  113. {
  114. return sd_ant_crypto_channel_enable(channel_number,
  115. p_crypto_config->mode,
  116. p_crypto_config->key_index,
  117. p_crypto_config->decimation_rate);
  118. }
  119. ret_code_t ant_channel_encrypt_config(uint8_t channel_type,
  120. uint8_t channel_number,
  121. ant_encrypt_channel_settings_t * p_crypto_config)
  122. {
  123. ret_code_t err_code;
  124. if (p_crypto_config != NULL)
  125. {
  126. // encryption of the stack should be initialized previously
  127. if (m_stack_encryption_configured == false)
  128. {
  129. return NRF_ERROR_MODULE_NOT_INITIALIZED;
  130. }
  131. switch (channel_type)
  132. {
  133. case CHANNEL_TYPE_MASTER:
  134. err_code = ant_channel_encrypt_config_perform(channel_number, p_crypto_config);
  135. #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED
  136. ant_channel_encryp_tracking_state_set(channel_number,
  137. ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED);
  138. #endif
  139. break;
  140. #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED
  141. case CHANNEL_TYPE_SLAVE:
  142. ant_slave_channel_encrypt_config(channel_number, p_crypto_config);
  143. if (p_crypto_config->mode == ENCRYPTION_DISABLED_MODE)
  144. {
  145. err_code = ant_channel_encrypt_config_perform(channel_number, p_crypto_config);
  146. ant_channel_encryp_tracking_state_set(channel_number,
  147. ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED);
  148. }
  149. else
  150. {
  151. ant_channel_encryp_tracking_state_set(channel_number,
  152. ANT_ENC_CHANNEL_STAT_NOT_TRACKING);
  153. err_code = NRF_SUCCESS;
  154. }
  155. break;
  156. #endif
  157. default:
  158. err_code = NRF_ERROR_INVALID_PARAM;
  159. break;
  160. }
  161. }
  162. else
  163. {
  164. #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED
  165. ant_channel_encryp_tracking_state_set(channel_number,
  166. ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED);
  167. #endif
  168. err_code = NRF_SUCCESS;
  169. }
  170. return err_code;
  171. }
  172. /** @brief Function for calling the handler of module events.*/
  173. static void ant_encrypt_user_handler_try_to_run(uint8_t ant_channel, ant_encrypt_user_evt_t event)
  174. {
  175. if (m_ant_enc_evt_handler != NULL)
  176. {
  177. m_ant_enc_evt_handler(ant_channel, event);
  178. }
  179. }
  180. /**@brief Function for handling an ANT stack event.
  181. * @param[in] p_ant_evt ANT stack event.
  182. * @param[in] p_context Context.
  183. */
  184. static void ant_evt_handler(ant_evt_t * p_ant_evt, void * p_context)
  185. {
  186. uint8_t const ant_channel = p_ant_evt->channel;
  187. #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED
  188. ant_slave_encrypt_negotiation(p_ant_evt);
  189. #endif
  190. switch (p_ant_evt->event)
  191. {
  192. case EVENT_RX_FAIL_GO_TO_SEARCH:
  193. ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_CHANNEL_LOST);
  194. break;
  195. case EVENT_ENCRYPT_NEGOTIATION_SUCCESS:
  196. ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_NEGOTIATION_SUCCESS);
  197. break;
  198. case EVENT_ENCRYPT_NEGOTIATION_FAIL:
  199. ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_NEGOTIATION_FAIL);
  200. break;
  201. }
  202. }
  203. NRF_SDH_ANT_OBSERVER(m_ant_observer, ANT_ENCRYPT_ANT_OBSERVER_PRIO, ant_evt_handler, NULL);
  204. void ant_enc_event_handler_register(ant_encryp_user_handler_t user_handler_func)
  205. {
  206. m_ant_enc_evt_handler = user_handler_func;
  207. }
  208. #endif // NRF_MODULE_ENABLED(ANT_ENCRYPT_CONFIG)