nrf_gzp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is confidential property of Nordic
  4. * Semiconductor ASA.Terms and conditions of usage are described in detail
  5. * in NORDIC SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. * $LastChangedRevision: 25890 $
  12. */
  13. /**
  14. * @file
  15. * @brief Implementation of Gazell Pairing Library (gzp), Common functions.
  16. * @defgroup gzp_source_common Gazell Pairing common functions implementation
  17. * @{
  18. * @ingroup gzp_04_source
  19. */
  20. #include "nrf_gzp.h"
  21. #include "nrf_gzll.h"
  22. #include "nrf_ecb.h"
  23. #include <string.h>
  24. #define SOURCE_FILE NRF_SOURCE_FILE_GZP ///< File identifer for asserts.
  25. /******************************************************************************/
  26. /** @name Global variables
  27. * @{ */
  28. /******************************************************************************/
  29. /**
  30. * Constant holding base address part of the pairing address.
  31. */
  32. static const uint8_t pairing_base_address[4] = { GZP_ADDRESS };
  33. /**
  34. * Constant holding prefix byte of the pairing address.
  35. */
  36. static const uint8_t pairing_address_prefix_byte = 0;
  37. /**
  38. * Constant holding pre-defined "validation ID".
  39. */
  40. static const uint8_t gzp_validation_id[GZP_VALIDATION_ID_LENGTH] = GZP_VALIDATION_ID;
  41. /**
  42. * Constant holding pre-defined "secret key".
  43. */
  44. static const uint8_t gzp_secret_key[16] = GZP_SECRET_KEY;
  45. /**
  46. * Variable used for AES key selection
  47. */
  48. static gzp_key_select_t gzp_key_select;
  49. /** @} */
  50. /******************************************************************************/
  51. /** @name Misc. external variables.
  52. * @{ */
  53. /******************************************************************************/
  54. static uint8_t gzp_session_token[GZP_SESSION_TOKEN_LENGTH];
  55. static uint8_t gzp_dyn_key[GZP_DYN_KEY_LENGTH];
  56. /** @} */
  57. /******************************************************************************/
  58. /** @name Implementation common internal GZP functions
  59. * @{ */
  60. /******************************************************************************/
  61. bool gzp_update_radio_params(const uint8_t* system_address)
  62. {
  63. uint8_t i;
  64. uint8_t channels[NRF_GZLL_CONST_MAX_CHANNEL_TABLE_SIZE];
  65. uint32_t channel_table_size;
  66. uint32_t pairing_base_address_32, system_address_32;
  67. bool update_ok = true;
  68. bool gzll_enabled_state;
  69. gzll_enabled_state = nrf_gzll_is_enabled();
  70. // Configure "global" pairing address
  71. pairing_base_address_32 = (pairing_base_address[0]) +
  72. ((uint32_t)pairing_base_address[1] << 8) +
  73. ((uint32_t)pairing_base_address[2] << 16) +
  74. ((uint32_t)pairing_base_address[3] << 24) ;
  75. if(system_address != NULL)
  76. {
  77. system_address_32 = (system_address[0]) +
  78. ((uint32_t)system_address[1] << 8) +
  79. ((uint32_t)system_address[2] << 16) +
  80. ((uint32_t)system_address[3] << 24) ;
  81. }
  82. else
  83. {
  84. return false;
  85. }
  86. nrf_gzp_disable_gzll();
  87. update_ok = update_ok && nrf_gzll_set_base_address_0(pairing_base_address_32);
  88. update_ok = update_ok && nrf_gzll_set_address_prefix_byte(GZP_PAIRING_PIPE, pairing_address_prefix_byte);
  89. update_ok = update_ok && nrf_gzll_set_base_address_1(system_address_32);
  90. // Configure address for pipe 1 - 5. Address byte set to equal pipe number.
  91. for(i = 1; i < NRF_GZLL_CONST_PIPE_COUNT; i++)
  92. {
  93. update_ok = update_ok && nrf_gzll_set_address_prefix_byte(i,i);
  94. }
  95. channel_table_size = nrf_gzll_get_channel_table_size();
  96. gzp_generate_channels(&channels[0], system_address, channel_table_size);
  97. // Write generated channel subset to Gazell Link Layer
  98. update_ok = update_ok && nrf_gzll_set_channel_table(&channels[0], channel_table_size);
  99. if(gzll_enabled_state)
  100. {
  101. update_ok = update_ok && nrf_gzll_enable();
  102. }
  103. return update_ok;
  104. }
  105. void gzp_generate_channels(uint8_t* ch_dst, const uint8_t* system_address, uint8_t channel_tab_size)
  106. {
  107. uint8_t binsize, spacing, i;
  108. binsize = (GZP_CHANNEL_MAX - GZP_CHANNEL_MIN) / channel_tab_size;
  109. ch_dst[0] = GZP_CHANNEL_LOW;
  110. ch_dst[channel_tab_size - 1] = GZP_CHANNEL_HIGH;
  111. if(system_address != NULL)
  112. {
  113. for(i = 1; i < (channel_tab_size - 1); i++)
  114. {
  115. ch_dst[i] = (binsize * i) + (system_address[i % 4] % binsize);
  116. }
  117. }
  118. // If channels are too close, shift them to better positions
  119. for(i = 1; i < channel_tab_size; i++)
  120. {
  121. spacing = (ch_dst[i] - ch_dst[i - 1]);
  122. if(spacing < GZP_CHANNEL_SPACING_MIN)
  123. {
  124. ch_dst[i] += (GZP_CHANNEL_SPACING_MIN - spacing);
  125. }
  126. }
  127. }
  128. __INLINE void nrf_gzp_disable_gzll(void)
  129. {
  130. if(nrf_gzll_is_enabled())
  131. {
  132. nrf_gzll_disable();
  133. __WFI();
  134. while(nrf_gzll_is_enabled())
  135. {
  136. }
  137. }
  138. }
  139. #ifndef GZP_CRYPT_DISABLE
  140. void gzp_xor_cipher(uint8_t* dst, const uint8_t* src, const uint8_t* pad, uint8_t length)
  141. {
  142. uint8_t i;
  143. for(i = 0; i < length; i++)
  144. {
  145. *dst = *src ^ *pad;
  146. dst++;
  147. src++;
  148. pad++;
  149. }
  150. }
  151. bool gzp_validate_id(const uint8_t* id)
  152. {
  153. return (memcmp(id, (void*)gzp_validation_id, GZP_VALIDATION_ID_LENGTH) == 0);
  154. }
  155. void gzp_add_validation_id(uint8_t* dst)
  156. {
  157. memcpy(dst, (void const*)gzp_validation_id, GZP_VALIDATION_ID_LENGTH);
  158. }
  159. void gzp_crypt_set_session_token(const uint8_t * token)
  160. {
  161. memcpy(gzp_session_token, (void const*)token, GZP_SESSION_TOKEN_LENGTH);
  162. }
  163. void gzp_crypt_set_dyn_key(const uint8_t* key)
  164. {
  165. memcpy(gzp_dyn_key, (void const*)key, GZP_DYN_KEY_LENGTH);
  166. }
  167. void gzp_crypt_get_session_token(uint8_t * dst_token)
  168. {
  169. memcpy(dst_token, (void const*)gzp_session_token, GZP_SESSION_TOKEN_LENGTH);
  170. }
  171. void gzp_crypt_get_dyn_key(uint8_t* dst_key)
  172. {
  173. memcpy(dst_key, (void const*)gzp_dyn_key, GZP_DYN_KEY_LENGTH);
  174. }
  175. void gzp_crypt_select_key(gzp_key_select_t key_select)
  176. {
  177. gzp_key_select = key_select;
  178. }
  179. void gzp_crypt(uint8_t* dst, const uint8_t* src, uint8_t length)
  180. {
  181. uint8_t i;
  182. uint8_t key[16];
  183. uint8_t iv[16];
  184. // Build AES key based on "gzp_key_select"
  185. switch(gzp_key_select)
  186. {
  187. case GZP_ID_EXCHANGE:
  188. memcpy(key, (void const*)gzp_secret_key, 16);
  189. break;
  190. case GZP_KEY_EXCHANGE:
  191. memcpy(key, (void const*)gzp_secret_key, 16);
  192. gzp_get_host_id(key);
  193. break;
  194. case GZP_DATA_EXCHANGE:
  195. memcpy(key, (void const*)gzp_secret_key, 16);
  196. memcpy(key, (void const*)gzp_dyn_key, GZP_DYN_KEY_LENGTH);
  197. break;
  198. default:
  199. return;
  200. }
  201. // Build init vector from "gzp_session_token"
  202. for(i = 0; i < 16; i++)
  203. {
  204. if(i < GZP_SESSION_TOKEN_LENGTH)
  205. {
  206. iv[i] = gzp_session_token[i];
  207. }
  208. else
  209. {
  210. iv[i] = 0;
  211. }
  212. }
  213. // Set up hal_aes using new key and init vector
  214. (void)nrf_ecb_init();
  215. nrf_ecb_set_key(key);
  216. //hal_aes_setup(false, ECB, key, NULL); // Note, here we skip the IV as we use ECB mode
  217. // Encrypt IV using ECB mode
  218. (void)nrf_ecb_crypt(iv, iv);
  219. // Encrypt data by XOR'ing with AES output
  220. gzp_xor_cipher(dst, src, iv, length);
  221. }
  222. void gzp_random_numbers_generate(uint8_t * dst, uint8_t n)
  223. {
  224. uint8_t i;
  225. NRF_RNG->EVENTS_VALRDY=0;
  226. NRF_RNG->TASKS_START = 1;
  227. for(i = 0; i < n; i++)
  228. {
  229. while(NRF_RNG->EVENTS_VALRDY==0)
  230. {}
  231. dst[i] = (uint8_t)NRF_RNG->VALUE;
  232. NRF_RNG->EVENTS_VALRDY=0;
  233. }
  234. NRF_RNG->TASKS_STOP = 1;
  235. }
  236. /******************************************************************************/
  237. /** @name Implementation of nRF51 specific GZP functions
  238. * @{ */
  239. /******************************************************************************/
  240. /**
  241. * @brief Function for setting the Primask variable. Only necessary if ARMCC
  242. * compiler skips __set_PRIMASK at high optimization levels.
  243. *
  244. * @param primask The primask value. 1 to disable interrupts, 0 otherwise.
  245. */
  246. static void nrf_gzp_set_primask(uint32_t primask)
  247. {
  248. #if defined(__CC_ARM)
  249. //lint -save -e10 -e618 -e438 -e550 -e526 -e628 -e526
  250. volatile register uint32_t __regPriMask __ASM("primask");
  251. __regPriMask = (primask);
  252. #else
  253. __set_PRIMASK(primask);
  254. #endif
  255. //lint -restore
  256. }
  257. void nrf_gzp_flush_rx_fifo(uint32_t pipe)
  258. {
  259. static uint8_t dummy_packet[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH];
  260. uint32_t length;
  261. nrf_gzp_set_primask(1);
  262. while(nrf_gzll_get_rx_fifo_packet_count(pipe) >0)
  263. {
  264. length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
  265. (void)nrf_gzll_fetch_packet_from_rx_fifo(pipe,dummy_packet,&length);
  266. }
  267. nrf_gzp_set_primask(0);
  268. }
  269. /** @} */
  270. /******************************************************************************/
  271. /** @name Implementation of debug functions
  272. * @{ */
  273. /******************************************************************************/
  274. /** @} */
  275. /** @} */
  276. #endif