diff options
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption')
4 files changed, 811 insertions, 0 deletions
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_config.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_config.c new file mode 100644 index 0000000..45e5614 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_config.c @@ -0,0 +1,245 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(ANT_ENCRYPT_CONFIG) +#include <stdlib.h> +#include "ant_encrypt_config.h" +#include "ant_interface.h" +#include "ant_parameters.h" + +#ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED + #include "ant_encrypt_negotiation_slave.h" +#endif + + /*lint -e551 -save*/ +/** Flag for checking if stack was configured for encryption. */ +static bool m_stack_encryption_configured = false; + /*lint -restore */ + + /** Pointer to handler of module's events. */ +static ant_encryp_user_handler_t m_ant_enc_evt_handler = NULL; + +static ret_code_t ant_enc_advance_burs_config_apply( + ant_encrypt_adv_burst_settings_t const * const p_adv_burst_set); + +ret_code_t ant_stack_encryption_config(ant_encrypt_stack_settings_t const * const p_crypto_set) +{ + ret_code_t err_code; + + for ( uint32_t i = 0; i < p_crypto_set->key_number; i++) + { + err_code = sd_ant_crypto_key_set(i, p_crypto_set->pp_key[i]); + VERIFY_SUCCESS(err_code); + } + + if (p_crypto_set->p_adv_burst_config != NULL) + { + err_code = ant_enc_advance_burs_config_apply(p_crypto_set->p_adv_burst_config); + VERIFY_SUCCESS(err_code); + } + + // subcomands LUT for @ref sd_ant_crypto_info_set calls + const uint8_t set_enc_info_param_lut[] = + { + ENCRYPTION_INFO_SET_CRYPTO_ID, + ENCRYPTION_INFO_SET_CUSTOM_USER_DATA, + ENCRYPTION_INFO_SET_RNG_SEED + }; + + for ( uint32_t i = 0; i < sizeof(set_enc_info_param_lut); i++) + { + if ( p_crypto_set->info.pp_array[i] != NULL) + { + err_code = sd_ant_crypto_info_set(set_enc_info_param_lut[i], + p_crypto_set->info.pp_array[i]); + + VERIFY_SUCCESS(err_code); + } + } + + #ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED + // all ANT channels have unsupported slave encryption tracking (even master's channel) + ant_channel_encryp_negotiation_slave_init(); + #endif + + m_ant_enc_evt_handler = NULL; + + m_stack_encryption_configured = true; + + return NRF_SUCCESS; +} + + +/** + * @brief Function for configuring advanced burst settings according to encryption requirements. + * + * @param p_adv_burst_set Pointer to ANT advanced burst settings. + * + * @retval Value returned by @ref sd_ant_adv_burst_config_set. + */ +static ret_code_t ant_enc_advance_burs_config_apply( + ant_encrypt_adv_burst_settings_t const * const p_adv_burst_set) +{ + uint8_t adv_burst_conf_str[ADV_BURST_CFG_MIN_SIZE] = + { ADV_BURST_MODE_ENABLE, 0, 0, 0, 0, 0, 0, 0 }; + + adv_burst_conf_str[ADV_BURST_CFG_PACKET_SIZE_INDEX] = p_adv_burst_set->packet_length; + adv_burst_conf_str[ADV_BURST_CFG_REQUIRED_FEATURES] = p_adv_burst_set->required_feature; + adv_burst_conf_str[ADV_BURST_CFG_OPTIONAL_FEATURES] = p_adv_burst_set->optional_feature; + + return sd_ant_adv_burst_config_set(adv_burst_conf_str, sizeof(adv_burst_conf_str)); +} + + +ret_code_t ant_channel_encrypt_config_perform(uint8_t channel_number, + ant_encrypt_channel_settings_t * p_crypto_config) +{ + return sd_ant_crypto_channel_enable(channel_number, + p_crypto_config->mode, + p_crypto_config->key_index, + p_crypto_config->decimation_rate); +} + + +ret_code_t ant_channel_encrypt_config(uint8_t channel_type, + uint8_t channel_number, + ant_encrypt_channel_settings_t * p_crypto_config) +{ + ret_code_t err_code; + + if (p_crypto_config != NULL) + { + // encryption of the stack should be initialized previously + if (m_stack_encryption_configured == false) + { + return NRF_ERROR_MODULE_NOT_INITIALIZED; + } + + switch (channel_type) + { + case CHANNEL_TYPE_MASTER: + err_code = ant_channel_encrypt_config_perform(channel_number, p_crypto_config); +#ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED + ant_channel_encryp_tracking_state_set(channel_number, + ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED); +#endif + break; + +#ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED + case CHANNEL_TYPE_SLAVE: + ant_slave_channel_encrypt_config(channel_number, p_crypto_config); + + if (p_crypto_config->mode == ENCRYPTION_DISABLED_MODE) + { + err_code = ant_channel_encrypt_config_perform(channel_number, p_crypto_config); + ant_channel_encryp_tracking_state_set(channel_number, + ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED); + } + else + { + ant_channel_encryp_tracking_state_set(channel_number, + ANT_ENC_CHANNEL_STAT_NOT_TRACKING); + err_code = NRF_SUCCESS; + } + break; +#endif + + default: + err_code = NRF_ERROR_INVALID_PARAM; + break; + } + } + else + { +#ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED + ant_channel_encryp_tracking_state_set(channel_number, + ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED); +#endif + err_code = NRF_SUCCESS; + } + + return err_code; +} + +/** @brief Function for calling the handler of module events.*/ +static void ant_encrypt_user_handler_try_to_run(uint8_t ant_channel, ant_encrypt_user_evt_t event) +{ + if (m_ant_enc_evt_handler != NULL) + { + m_ant_enc_evt_handler(ant_channel, event); + } +} + +/**@brief Function for handling an ANT stack event. + * @param[in] p_ant_evt ANT stack event. + * @param[in] p_context Context. + */ +static void ant_evt_handler(ant_evt_t * p_ant_evt, void * p_context) +{ + uint8_t const ant_channel = p_ant_evt->channel; + +#ifdef ANT_ENCRYPT_NEGOTIATION_SLAVE_ENABLED + ant_slave_encrypt_negotiation(p_ant_evt); +#endif + + switch (p_ant_evt->event) + { + case EVENT_RX_FAIL_GO_TO_SEARCH: + ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_CHANNEL_LOST); + break; + + case EVENT_ENCRYPT_NEGOTIATION_SUCCESS: + ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_NEGOTIATION_SUCCESS); + break; + + case EVENT_ENCRYPT_NEGOTIATION_FAIL: + ant_encrypt_user_handler_try_to_run(ant_channel, ANT_ENC_EVT_NEGOTIATION_FAIL); + break; + } +} + +NRF_SDH_ANT_OBSERVER(m_ant_observer, ANT_ENCRYPT_ANT_OBSERVER_PRIO, ant_evt_handler, NULL); + +void ant_enc_event_handler_register(ant_encryp_user_handler_t user_handler_func) +{ + m_ant_enc_evt_handler = user_handler_func; +} + +#endif // NRF_MODULE_ENABLED(ANT_ENCRYPT_CONFIG) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_config.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_config.h new file mode 100644 index 0000000..53f0a8f --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_config.h @@ -0,0 +1,241 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef ANT_ENCRYPT_CONFIG__ +#define ANT_ENCRYPT_CONFIG__ + +/**@file + * + * @defgroup ant_encrypt_config ANT encryption configuration + * @{ + * @ingroup ant_sdk_utils + * + * @brief Encryption configuration for the ANT stack and channels. + * + */ + +#include <stdint.h> + +#include "sdk_errors.h" +#include "nrf_sdh_ant.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** @name Advanced burst configuration for encryption modules + * @{ + */ +#define ADV_BURST_CFG_MIN_SIZE 8 ///< Minimum size of the advance burst configuration data. +#define ADV_BURST_CFG_PACKET_SIZE_INDEX 1 ///< Index of the packet size field in the configuration data. +#define ADV_BURST_CFG_REQUIRED_FEATURES 2 ///< Index of the required features field in the configuration data. +#define ADV_BURST_CFG_OPTIONAL_FEATURES 5 ///< Index of the optional features field in the configuration data. +/**@} */ + +/** @brief ANT channel cryptographic configuration. */ +typedef struct +{ + uint8_t mode; ///< Encryption mode. See the encrypted channel defines in ant_parameters.h. + uint8_t key_index; ///< Index of encryption key. + uint8_t decimation_rate; ///< Division of the master channel rate by the slave’s tracking channel rate. +} ant_encrypt_channel_settings_t; + +/** @brief ANT encryption information. */ +typedef union +{ + uint8_t * pp_array[3]; // For array access support. + struct + { + uint8_t * p_encryption_id; ///< Pointer to the encryption ID of the device (4 bytes). + uint8_t * p_user_info; ///< Pointer to the user information string (19 bytes). + uint8_t * p_random_num_seed; ///< Pointer to the random number seed (16 bytes). + } items; +} ant_encrypt_info_settings_t; + +/** @brief Advanced burst settings used by the encrypted channel. */ +typedef struct +{ + uint8_t packet_length; ///< RF payload size. See the advanced burst configuration defines in ant_parameters.h. + uint8_t required_feature; ///< Required advanced burst modes. See the advanced burst configuration defines in ant_parameters.h. + uint8_t optional_feature; ///< Optional advanced burst modes. See the advanced burst configuration defines in ant_parameters.h. +} ant_encrypt_adv_burst_settings_t; + +/**@brief ANT stack cryptographic configuration. */ +typedef struct +{ + ant_encrypt_info_settings_t info; ///< Pointer to the encryption information structure. + uint8_t * * pp_key; ///< Pointer to an array for pointers to encryption keys. Each key must have a length of 16 bytes. + uint8_t key_number; ///< Number of encryption keys. + ant_encrypt_adv_burst_settings_t * p_adv_burst_config; ///< Advanced burst configuration. If NULL, the advanced burst must be configured externally. +} ant_encrypt_stack_settings_t; + +/** + * @brief ANT encryption negotiation events. + */ +typedef enum +{ + ANT_ENC_EVT_NEGOTIATION_SUCCESS, ///< Negotiation success. + ANT_ENC_EVT_NEGOTIATION_FAIL, ///< Negotiation failure. + ANT_ENC_EVT_CHANNEL_LOST ///< Lost a channel. It's relevant only for slave channels. +} ant_encrypt_user_evt_t; + +/** + * @brief Event handler for ANT encryption user events. + */ +typedef void (* ant_encryp_user_handler_t)(uint8_t channel, ant_encrypt_user_evt_t event); + +/** + * @brief Macro for initializing an ANT encryption information structure. + * + * @param[in] P_ENC_ID Pointer to the encryption ID of the device (4 bytes). + * @param[in] P_USER_INFO Pointer to the user information string (19 bytes). + * @param[in] P_RAND_NUM_SEED Pointer to the random number seed (16 bytes). + */ +#define ANT_CRYPTO_INFO_SETTINGS_INIT(P_ENC_ID, P_USER_INFO, P_RAND_NUM_SEED) \ + { \ + .items = \ + { \ + .p_encryption_id = P_ENC_ID, \ + .p_user_info = P_USER_INFO, \ + .p_random_num_seed = P_RAND_NUM_SEED \ + } \ + } + +/** + * @brief Macro for declaring the basic cryptographic configuration for the ANT stack. + * + * This macro configures the following settings: + * - Cryptographic key + * - Encryption ID + * - Advanced burst mode with the maximum RF payload size + * + * Use @ref ANT_ENCRYPT_STACK_SETTINGS_BASE to access the created configuration instance. + * + * @param[in] NAME Name for the created data instance. + * @param[in] P_KEY Pointer to the cryptographic key (16 bytes). + * @param[in] P_ENC_ID Pointer to the encryption ID (4 bytes). + */ +#define ANT_ENCRYPT_STACK_SETTINGS_BASE_DEF(NAME, P_KEY, P_ENC_ID) \ + ant_encrypt_adv_burst_settings_t NAME##_ant_enc_adv_burst_set = \ + { \ + .packet_length = ADV_BURST_MODES_MAX_SIZE, \ + .required_feature = 0, \ + .optional_feature = 0 \ + }; \ + uint8_t * pp_##NAME##_key[1] = {P_KEY}; \ + ant_encrypt_stack_settings_t NAME ## _ant_crypto_settings = \ + { \ + .info = ANT_CRYPTO_INFO_SETTINGS_INIT(P_ENC_ID, NULL, NULL), \ + .pp_key = pp_##NAME##_key, \ + .key_number = 1, \ + .p_adv_burst_config = &NAME##_ant_enc_adv_burst_set \ + } + + +/** @brief Macro for accessing the configuration instance created + * by @ref ANT_ENCRYPT_STACK_SETTINGS_BASE_DEF. + * + * @param[in] NAME Name of the settings instance. + */ +#define ANT_ENCRYPT_STACK_SETTINGS_BASE(NAME) (NAME##_ant_crypto_settings) + +/** + * @brief Function for applying an encryption configuration to a slave channel. + * + * This function enables encryption on a channel. + * + * This function should be used by the @ref ant_encrypt_negotiation_slave module and this module. + * + * @param[in] channel_number ANT channel number. + * @param[in] p_crypto_config Pointer to the encryption configuration. + * + * @return Value returned by @ref sd_ant_crypto_channel_enable (for example, NRF_SUCCESS if + * the configuration was successful). + */ +ret_code_t ant_channel_encrypt_config_perform(uint8_t channel_number, + ant_encrypt_channel_settings_t * p_crypto_config); + +/** + * @brief Function for applying an encryption configuration to a master or slave channel. + * + * When called for a master channel, this function enables encryption + * for that channel. When called for a slave channel, it saves + * the encryption configuration for future use. + * + * This function should be used by the @ref ant_channel_config module. + * + * @param[in] channel_type ANT channel type: CHANNEL_TYPE_SLAVE or CHANNEL_TYPE_MASTER. + * @param[in] channel_num ANT channel number. + * @param[in] p_crypto_config Pointer to the encryption configuration. + * + * @retval NRF_SUCCESS If the function completed successfully. + * @retval NRF_ERROR_INVALID_PARAM If the channel type is invalid. + * @retval NRF_ERROR_MODULE_NOT_INITIALIZED If the stack is not configured for encryption. + * @retval Other Otherwise, the error value returned by the @ref + * ant_channel_encrypt_config_perform function is returned. + */ +ret_code_t ant_channel_encrypt_config(uint8_t channel_type, + uint8_t channel_num, + ant_encrypt_channel_settings_t * p_crypto_config); + +/** + * @brief Function for configuring the cryptographic settings of the ANT stack. + * + * @param[in] p_crypto_info_set Pointer to the settings. + */ +ret_code_t ant_stack_encryption_config(ant_encrypt_stack_settings_t const * const p_crypto_info_set); + +/** + * @brief Function for registering an event handler for ANT encryption events. + * + * The event handler should support all of the events in @ref ant_encrypt_user_evt_t. + * + * @param[in] p_handler Pointer to a handler function. + */ +void ant_enc_event_handler_register(ant_encryp_user_handler_t p_handler); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif // ANT_ENCRYPT_CONFIG__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_negotiation_slave.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_negotiation_slave.c new file mode 100644 index 0000000..715fe14 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_negotiation_slave.c @@ -0,0 +1,185 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#include "sdk_common.h" +#if NRF_MODULE_ENABLED(ANT_ENCRYPT_NEGOTIATION_SLAVE) +#include <stdlib.h> +#include <string.h> +#include "ant_encrypt_config.h" +#include "ant_interface.h" +#include "ant_parameters.h" +#include "nrf_error.h" +#include "app_error.h" + +#include "ant_encrypt_negotiation_slave.h" + +/** Number of supported channels. */ +#define NUMBER_OF_CHANNELS (NRF_SDH_ANT_TOTAL_CHANNELS_ALLOCATED) + +/** Flag to block other channels from attempting to enable encryption while + * another encryption is in the process. + */ +static volatile bool m_can_enable_crypto = true; + +/** Array to keep track of which channels are currently tracking. */ +static ant_encrypt_tracking_state_t m_encrypt_channel_states[NUMBER_OF_CHANNELS]; + +/** Array for the slave channels' encryption settings. */ +static ant_encrypt_channel_settings_t m_slave_channel_conf[MAX_ANT_CHANNELS]; + + + +void ant_channel_encryp_tracking_state_set(uint8_t channel_number, + ant_encrypt_tracking_state_t state) +{ + m_encrypt_channel_states[channel_number] = state; +} + + +void ant_channel_encryp_negotiation_slave_init(void) +{ + for (uint32_t channel = 0; channel < NUMBER_OF_CHANNELS; channel++) + { + ant_channel_encryp_tracking_state_set(channel, ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED); + } + + m_can_enable_crypto = true; +} + + +ant_encrypt_tracking_state_t ant_channel_encryp_tracking_state_get(uint8_t channel_number) +{ + return m_encrypt_channel_states[channel_number]; +} + + +void ant_slave_channel_encrypt_config(uint8_t channel_number, + ant_encrypt_channel_settings_t const * const p_crypto_config) +{ + memcpy(&m_slave_channel_conf[channel_number], p_crypto_config, + sizeof(ant_encrypt_channel_settings_t)); +} + + +/**@brief Function for handling ANT RX channel events. + * + * @param[in] p_event_message_buffer The ANT event message buffer. + */ +static void ant_slave_encrypt_try_enable(uint8_t ant_channel, + uint8_t ant_message_id) +{ + uint32_t err_code; + ant_encrypt_tracking_state_t track_stat; + + + switch (ant_message_id) + { + // Broadcast data received. + case MESG_BROADCAST_DATA_ID: + + track_stat = ant_channel_encryp_tracking_state_get(ant_channel); + // If the encryption has not yet been negotiated for this channel and another channel + // is not currently trying to enable encryption, enable encryption + if ((track_stat != ANT_ENC_CHANNEL_STAT_TRACKING_DECRYPTED) + && (track_stat != ANT_ENC_CHANNEL_STAT_NEGOTIATING) + && m_can_enable_crypto) + { + // Block other channels from trying to enable encryption until this channel + // is finished + m_can_enable_crypto = false; + ant_channel_encryp_tracking_state_set(ant_channel, + ANT_ENC_CHANNEL_STAT_NEGOTIATING); + + // Enable encryption on ant_channel + err_code = + ant_channel_encrypt_config_perform(ant_channel, + &m_slave_channel_conf[ant_channel]); + APP_ERROR_CHECK(err_code); + } + break; + + default: + break; + } +} + + +void ant_slave_encrypt_negotiation(ant_evt_t * p_ant_evt) +{ + ant_encrypt_tracking_state_t track_state = + ant_channel_encryp_tracking_state_get(p_ant_evt->channel); + + if (track_state == ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED) + return; + + switch (p_ant_evt->event) + { + case EVENT_RX_FAIL_GO_TO_SEARCH: + if (track_state == ANT_ENC_CHANNEL_STAT_NEGOTIATING) + { + m_can_enable_crypto = true; + } + + ant_channel_encryp_tracking_state_set(p_ant_evt->channel, + ANT_ENC_CHANNEL_STAT_NOT_TRACKING); + break; + + case EVENT_RX: + ant_slave_encrypt_try_enable(p_ant_evt->channel, + p_ant_evt->message.ANT_MESSAGE_ucMesgID); + break; + + case EVENT_ENCRYPT_NEGOTIATION_SUCCESS: + m_can_enable_crypto = true; + ant_channel_encryp_tracking_state_set(p_ant_evt->channel, + ANT_ENC_CHANNEL_STAT_TRACKING_DECRYPTED); + break; + + case EVENT_ENCRYPT_NEGOTIATION_FAIL: + m_can_enable_crypto = true; + ant_channel_encryp_tracking_state_set(p_ant_evt->channel, + ANT_ENC_CHANNEL_STAT_TRACKING_ENCRYPTED); + break; + + default: + break; + } +} + +#endif // NRF_MODULE_ENABLED(ANT_ENCRYPT_NEGOTIATION_SLAVE) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_negotiation_slave.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_negotiation_slave.h new file mode 100644 index 0000000..253517d --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/ant/ant_encryption/ant_encrypt_negotiation_slave.h @@ -0,0 +1,140 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef ANT_ENCRYPT_NEGOTIATION_SLAVE_H__ +#define ANT_ENCRYPT_NEGOTIATION_SLAVE_H__ + +/**@file + * + * @defgroup ant_encrypt_negotiation_slave ANT encryption negotiation + * @{ + * @ingroup ant_sdk_utils + * + * @brief Encryption negotiation for encrypted ANT slave channels. + * + * After pairing, the slave starts negotiating the encryption with the master. After + * successful negotiation, the slave can decrypt messages from the master, and all + * future messages are sent encrypted. + * + */ + +#include <stdint.h> +#include "nrf_sdh_ant.h" +#include "ant_encrypt_config.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Encryption negotiation states for a slave channel. */ +typedef enum +{ + ANT_ENC_CHANNEL_STAT_NOT_TRACKING, ///< Not tracking the master. + ANT_ENC_CHANNEL_STAT_TRACKING_ENCRYPTED, ///< Tracking the master, but cannot decrypt messages. + ANT_ENC_CHANNEL_STAT_NEGOTIATING, ///< Encryption has been enabled and negotiation is in progress. + ANT_ENC_CHANNEL_STAT_TRACKING_DECRYPTED, ///< Tracking the master and can decrypt messages. + ANT_ENC_CHANNEL_STAT_TRACKING_UNSUPPORTED ///< Tracking unsupported on this channel. +} ant_encrypt_tracking_state_t; + + +/** + * @brief Function for setting the encryption negotiation state of a slave ANT channel. + * + * This function should be used by the @ref ant_encrypt_config module. + * + * @param[in] channel_number ANT channel number. + * @param[in] state State to set. + */ +void ant_channel_encryp_tracking_state_set(uint8_t channel_number, + ant_encrypt_tracking_state_t state); + +/** + * @brief Function for getting the encryption negotiation state of a slave ANT channel. + * + * @param[in] channel_number ANT channel number. + */ +ant_encrypt_tracking_state_t ant_channel_encryp_tracking_state_get(uint8_t channel_number); + +/** + * @brief Function for initializing the module. + * + * This function initializes internal states of the module. It should + * only be used by the @ref ant_encrypt_config module. + * + */ +void ant_channel_encryp_negotiation_slave_init(void); + +/** + * @brief Function for setting the configuration for the slave channel. + * + * This function saves the channel's encryption configuration to a lookup table (LUT) for + * future usage. The configuration can then be used to enable encryption. + * + * This function is intended to be used by the @ref ant_encrypt_config module. + * + * @param[in] channel_number ANT channel number. + * @param[in] p_crypto_config Pointer to the encryption configuration. + */ +void ant_slave_channel_encrypt_config(uint8_t channel_number, + ant_encrypt_channel_settings_t const * const p_crypto_config); + + +/** + * @brief Function for handling ANT encryption negotiation on slave nodes. + * + * This function should be used directly in the ANT event dispatching process. It + * tries to enable slave channel encryption for all slave channels that are declared + * as encrypted channels (if appropriate master channels are found). + * + * This function should be used by the @ref ant_encrypt_config module. + * + * @param[in] p_ant_evt Pointer to the ANT stack event message structure. + */ +void ant_slave_encrypt_negotiation(ant_evt_t * p_ant_evt); + + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif // ANT_ENCRYPT_NEGOTIATION_SLAVE_H__ |