123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- #include "nrf_gzp.h"
- #include "nrf_gzll.h"
- #include "nrf_ecb.h"
- #include <string.h>
- #define SOURCE_FILE NRF_SOURCE_FILE_GZP
- static const uint8_t pairing_base_address[4] = { GZP_ADDRESS };
- static const uint8_t pairing_address_prefix_byte = 0;
- static const uint8_t gzp_validation_id[GZP_VALIDATION_ID_LENGTH] = GZP_VALIDATION_ID;
- static const uint8_t gzp_secret_key[16] = GZP_SECRET_KEY;
- static gzp_key_select_t gzp_key_select;
- static uint8_t gzp_session_token[GZP_SESSION_TOKEN_LENGTH];
- static uint8_t gzp_dyn_key[GZP_DYN_KEY_LENGTH];
- bool gzp_update_radio_params(const uint8_t* system_address)
- {
- uint8_t i;
- uint8_t channels[NRF_GZLL_CONST_MAX_CHANNEL_TABLE_SIZE];
- uint32_t channel_table_size;
- uint32_t pairing_base_address_32, system_address_32;
- bool update_ok = true;
- bool gzll_enabled_state;
- gzll_enabled_state = nrf_gzll_is_enabled();
-
- pairing_base_address_32 = (pairing_base_address[0]) +
- ((uint32_t)pairing_base_address[1] << 8) +
- ((uint32_t)pairing_base_address[2] << 16) +
- ((uint32_t)pairing_base_address[3] << 24) ;
- if (system_address != NULL)
- {
- system_address_32 = (system_address[0]) +
- ((uint32_t)system_address[1] << 8) +
- ((uint32_t)system_address[2] << 16) +
- ((uint32_t)system_address[3] << 24) ;
- }
- else
- {
- return false;
- }
- nrf_gzp_disable_gzll();
- update_ok = update_ok && nrf_gzll_set_base_address_0(pairing_base_address_32);
- update_ok = update_ok && nrf_gzll_set_address_prefix_byte(GZP_PAIRING_PIPE, pairing_address_prefix_byte);
- update_ok = update_ok && nrf_gzll_set_base_address_1(system_address_32);
-
- for (i = 1; i < NRF_GZLL_CONST_PIPE_COUNT; i++)
- {
- update_ok = update_ok && nrf_gzll_set_address_prefix_byte(i,i);
- }
- channel_table_size = nrf_gzll_get_channel_table_size();
- gzp_generate_channels(&channels[0], system_address, channel_table_size);
-
- update_ok = update_ok && nrf_gzll_set_channel_table(&channels[0], channel_table_size);
- if (gzll_enabled_state)
- {
- update_ok = update_ok && nrf_gzll_enable();
- }
- return update_ok;
- }
- void gzp_generate_channels(uint8_t* ch_dst, const uint8_t* system_address, uint8_t channel_tab_size)
- {
- uint8_t binsize, spacing, i;
- binsize = (GZP_CHANNEL_MAX - GZP_CHANNEL_MIN) / channel_tab_size;
- ch_dst[0] = GZP_CHANNEL_LOW;
- ch_dst[channel_tab_size - 1] = GZP_CHANNEL_HIGH;
- if (system_address != NULL)
- {
- for (i = 1; i < (channel_tab_size - 1); i++)
- {
- ch_dst[i] = (binsize * i) + (system_address[i % 4] % binsize);
- }
- }
-
- for (i = 1; i < channel_tab_size; i++)
- {
- spacing = (ch_dst[i] - ch_dst[i - 1]);
- if (spacing < GZP_CHANNEL_SPACING_MIN)
- {
- ch_dst[i] += (GZP_CHANNEL_SPACING_MIN - spacing);
- }
- }
- }
- __INLINE void nrf_gzp_disable_gzll(void)
- {
- if (nrf_gzll_is_enabled())
- {
- nrf_gzll_disable();
- __WFI();
- while (nrf_gzll_is_enabled())
- {
- }
- }
- }
- #ifndef GZP_CRYPT_DISABLE
- void gzp_xor_cipher(uint8_t* dst, const uint8_t* src, const uint8_t* pad, uint8_t length)
- {
- uint8_t i;
- for (i = 0; i < length; i++)
- {
- *dst = *src ^ *pad;
- dst++;
- src++;
- pad++;
- }
- }
- bool gzp_validate_id(const uint8_t* id)
- {
- return (memcmp(id, (void*)gzp_validation_id, GZP_VALIDATION_ID_LENGTH) == 0);
- }
- void gzp_add_validation_id(uint8_t* dst)
- {
- memcpy(dst, (void const*)gzp_validation_id, GZP_VALIDATION_ID_LENGTH);
- }
- void gzp_crypt_set_session_token(const uint8_t * token)
- {
- memcpy(gzp_session_token, (void const*)token, GZP_SESSION_TOKEN_LENGTH);
- }
- void gzp_crypt_set_dyn_key(const uint8_t* key)
- {
- memcpy(gzp_dyn_key, (void const*)key, GZP_DYN_KEY_LENGTH);
- }
- void gzp_crypt_get_session_token(uint8_t * dst_token)
- {
- memcpy(dst_token, (void const*)gzp_session_token, GZP_SESSION_TOKEN_LENGTH);
- }
- void gzp_crypt_get_dyn_key(uint8_t* dst_key)
- {
- memcpy(dst_key, (void const*)gzp_dyn_key, GZP_DYN_KEY_LENGTH);
- }
- void gzp_crypt_select_key(gzp_key_select_t key_select)
- {
- gzp_key_select = key_select;
- }
- void gzp_crypt(uint8_t* dst, const uint8_t* src, uint8_t length)
- {
- uint8_t i;
- uint8_t key[16];
- uint8_t iv[16];
-
- switch (gzp_key_select)
- {
- case GZP_ID_EXCHANGE:
- memcpy(key, (void const*)gzp_secret_key, 16);
- break;
- case GZP_KEY_EXCHANGE:
- memcpy(key, (void const*)gzp_secret_key, 16);
- gzp_get_host_id(key);
- break;
- case GZP_DATA_EXCHANGE:
- memcpy(key, (void const*)gzp_secret_key, 16);
- memcpy(key, (void const*)gzp_dyn_key, GZP_DYN_KEY_LENGTH);
- break;
- default:
- return;
- }
-
- for (i = 0; i < 16; i++)
- {
- if (i < GZP_SESSION_TOKEN_LENGTH)
- {
- iv[i] = gzp_session_token[i];
- }
- else
- {
- iv[i] = 0;
- }
- }
-
- (void)nrf_ecb_init();
- nrf_ecb_set_key(key);
-
-
- (void)nrf_ecb_crypt(iv, iv);
-
- gzp_xor_cipher(dst, src, iv, length);
- }
- void gzp_random_numbers_generate(uint8_t * dst, uint8_t n)
- {
- uint8_t i;
- NRF_RNG->EVENTS_VALRDY=0;
- NRF_RNG->TASKS_START = 1;
- for (i = 0; i < n; i++)
- {
- while (NRF_RNG->EVENTS_VALRDY==0)
- {}
- dst[i] = (uint8_t)NRF_RNG->VALUE;
- NRF_RNG->EVENTS_VALRDY=0;
- }
- NRF_RNG->TASKS_STOP = 1;
- }
- static void nrf_gzp_set_primask(uint32_t primask)
- {
- #if defined(__CC_ARM)
-
- volatile register uint32_t __regPriMask __ASM("primask");
- __regPriMask = (primask);
- #else
- __set_PRIMASK(primask);
- #endif
-
- }
- void nrf_gzp_flush_rx_fifo(uint32_t pipe)
- {
- static uint8_t dummy_packet[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH];
- uint32_t length;
- nrf_gzp_set_primask(1);
- while (nrf_gzll_get_rx_fifo_packet_count(pipe) >0)
- {
- length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
- (void)nrf_gzll_fetch_packet_from_rx_fifo(pipe,dummy_packet,&length);
- }
- nrf_gzp_set_primask(0);
- }
- #endif
|