ant_encrypt_config.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include <stdlib.h>
  13. #include "ant_encrypt_config.h"
  14. #include "ant_interface.h"
  15. #include "ant_parameters.h"
  16. #include "sdk_common.h"
  17. #ifdef ANT_ENCRYPT_SLAVE_NEGOTIATION_USED
  18. #include "ant_encrypt_negotiation_slave.h"
  19. #endif
  20. /*lint -e551 -save*/
  21. /** Flag for checking if stack was configured for encryption. */
  22. static bool m_stack_encryption_configured = false;
  23. /*lint -restore */
  24. /** Pointer to handler of module's events. */
  25. static ant_encryp_user_handler_t m_ant_enc_evt_handler = NULL;
  26. static ret_code_t ant_enc_advance_burs_config_apply(
  27. ant_encrypt_adv_burst_settings_t const * const p_adv_burst_set);
  28. ret_code_t ant_stack_encryption_config(ant_encrypt_stack_settings_t const * const p_crypto_set)
  29. {
  30. ret_code_t err_code;
  31. for ( uint32_t i = 0; i < p_crypto_set->key_number; i++)
  32. {
  33. err_code = sd_ant_crypto_key_set(i, p_crypto_set->pp_key[i]);
  34. VERIFY_SUCCESS(err_code);
  35. }
  36. if (p_crypto_set->p_adv_burst_config != NULL)
  37. {
  38. err_code = ant_enc_advance_burs_config_apply(p_crypto_set->p_adv_burst_config);
  39. VERIFY_SUCCESS(err_code);
  40. }
  41. // subcomands LUT for @ref sd_ant_crypto_info_set calls
  42. const uint8_t set_enc_info_param_lut[] =
  43. {
  44. ENCRYPTION_INFO_SET_CRYPTO_ID,
  45. ENCRYPTION_INFO_SET_CUSTOM_USER_DATA,
  46. ENCRYPTION_INFO_SET_RNG_SEED
  47. };
  48. for ( uint32_t i = 0; i < sizeof(set_enc_info_param_lut); i++)
  49. {
  50. if ( p_crypto_set->info.pp_array[i] != NULL)
  51. {
  52. err_code = sd_ant_crypto_info_set(set_enc_info_param_lut[i],
  53. p_crypto_set->info.pp_array[i]);
  54. VERIFY_SUCCESS(err_code);
  55. }
  56. }
  57. #ifdef ANT_ENCRYPT_SLAVE_NEGOTIATION_USED
  58. // all ANT channels have unsupported slave encryption tracking (even master's channel)
  59. ant_channel_encryp_negotiation_slave_init();
  60. #endif
  61. m_ant_enc_evt_handler = NULL;
  62. m_stack_encryption_configured = true;
  63. return NRF_SUCCESS;
  64. }
  65. /**
  66. * @brief Function for configuring advanced burst settings according to encryption requirements.
  67. *
  68. * @param p_adv_burst_set Pointer to ANT advanced burst settings.
  69. *
  70. * @retval Value returned by @ref sd_ant_adv_burst_config_set.
  71. */
  72. static ret_code_t ant_enc_advance_burs_config_apply(
  73. ant_encrypt_adv_burst_settings_t const * const p_adv_burst_set)
  74. {
  75. uint8_t adv_burst_conf_str[ADV_BURST_CFG_MIN_SIZE] =
  76. { ADV_BURST_MODE_ENABLE, 0, 0, 0, 0, 0, 0, 0 };
  77. adv_burst_conf_str[ADV_BURST_CFG_PACKET_SIZE_INDEX] = p_adv_burst_set->packet_length;
  78. adv_burst_conf_str[ADV_BURST_CFG_REQUIRED_FEATURES] = p_adv_burst_set->required_feature;
  79. adv_burst_conf_str[ADV_BURST_CFG_OPTIONAL_FEATURES] = p_adv_burst_set->optional_feature;
  80. return sd_ant_adv_burst_config_set(adv_burst_conf_str, sizeof(adv_burst_conf_str));
  81. }
  82. ret_code_t ant_channel_encrypt_config_perform(uint8_t channel_number,
  83. ant_encrypt_channel_settings_t * p_crypto_config)
  84. {
  85. return sd_ant_crypto_channel_enable(channel_number,
  86. p_crypto_config->mode,
  87. p_crypto_config->key_index,
  88. p_crypto_config->decimation_rate);
  89. }
  90. ret_code_t ant_channel_encrypt_config(uint8_t channel_type,
  91. uint8_t channel_number,
  92. ant_encrypt_channel_settings_t * p_crypto_config)
  93. {
  94. ret_code_t err_code;
  95. if (p_crypto_config != NULL)
  96. {
  97. // encryption of the stack should be initialized previously
  98. if (m_stack_encryption_configured == false)
  99. {
  100. return MODULE_NOT_INITIALZED;
  101. }
  102. switch (channel_type)
  103. {
  104. case CHANNEL_TYPE_MASTER:
  105. err_code = ant_channel_encrypt_config_perform(channel_number, p_crypto_config);
  106. #ifdef ANT_ENCRYPT_SLAVE_NEGOTIATION_USED
  107. ant_channel_encryp_tracking_state_set(channel_number,
  108. ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED);
  109. #endif
  110. break;
  111. #ifdef ANT_ENCRYPT_SLAVE_NEGOTIATION_USED
  112. case CHANNEL_TYPE_SLAVE:
  113. ant_slave_channel_encrypt_config(channel_number, p_crypto_config);
  114. if (p_crypto_config->mode == ENCRYPTION_DISABLED_MODE)
  115. {
  116. err_code = ant_channel_encrypt_config_perform(channel_number, p_crypto_config);
  117. ant_channel_encryp_tracking_state_set(channel_number,
  118. ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED);
  119. }
  120. else
  121. {
  122. ant_channel_encryp_tracking_state_set(channel_number,
  123. ANT_ENC_CHANNEL_STAT_NOT_TRACKING);
  124. err_code = NRF_SUCCESS;
  125. }
  126. break;
  127. #endif
  128. default:
  129. err_code = NRF_ERROR_INVALID_PARAM;
  130. break;
  131. }
  132. }
  133. else
  134. {
  135. #ifdef ANT_ENCRYPT_SLAVE_NEGOTIATION_USED
  136. ant_channel_encryp_tracking_state_set(channel_number,
  137. ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED);
  138. #endif
  139. err_code = NRF_SUCCESS;
  140. }
  141. return err_code;
  142. }
  143. /** @brief Function for calling the handler of module events.*/
  144. static void ant_encrypt_user_handler_try_to_run(uint8_t ant_channel, ant_encrypt_user_evt_t event)
  145. {
  146. if (m_ant_enc_evt_handler != NULL)
  147. {
  148. m_ant_enc_evt_handler(ant_channel, event);
  149. }
  150. }
  151. void ant_encrypt_event_handler(ant_evt_t * p_ant_evt)
  152. {
  153. uint8_t const ant_channel = p_ant_evt->channel;
  154. #ifdef ANT_ENCRYPT_SLAVE_NEGOTIATION_USED
  155. ant_slave_encrypt_negotiation(p_ant_evt);
  156. #endif
  157. switch (p_ant_evt->event)
  158. {
  159. case EVENT_RX_FAIL_GO_TO_SEARCH:
  160. ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_CHANNEL_LOST);
  161. break;
  162. case EVENT_ENCRYPT_NEGOTIATION_SUCCESS:
  163. ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_NEGOTIATION_SUCCESS);
  164. break;
  165. case EVENT_ENCRYPT_NEGOTIATION_FAIL:
  166. ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_NEGOTIATION_FAIL);
  167. break;
  168. }
  169. }
  170. void ant_enc_event_handler_register(ant_encryp_user_handler_t user_handler_func)
  171. {
  172. m_ant_enc_evt_handler = user_handler_func;
  173. }