es_security.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Copyright (c) 2016 - 2020, 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. #ifndef ES_SECURITY_H__
  41. #define ES_SECURITY_H__
  42. #include "app_error.h"
  43. #include "nrf_ble_escs.h"
  44. /**
  45. * @file
  46. * @defgroup eddystone_security Security
  47. * @brief Types and functions for dealing with security of Eddystone beacons.
  48. * @ingroup eddystone
  49. * @{
  50. */
  51. /**@brief Security events.
  52. */
  53. typedef enum
  54. {
  55. ES_SECURITY_MSG_UNLOCKED, //!< Beacon is unlocked.
  56. ES_SECURITY_MSG_EID, //!< EID has been generated.
  57. ES_SECURITY_MSG_IK, //!< IK has been generated.
  58. ES_SECURITY_MSG_ECDH, //!< Public ECDH has been generated.
  59. ES_SECURITY_MSG_STORE_TIME //!< EID slot time must be stored.
  60. } es_security_msg_t;
  61. /* @brief Callback for security events. */
  62. typedef void (*es_security_msg_cb_t)(uint8_t slot_no, es_security_msg_t msg_type);
  63. /**@brief EID configuration.
  64. *
  65. * @details This structure is used to preserve or restore an EID slot.
  66. *
  67. * @note This is a packed structure. Therefore, you should not change it.
  68. */
  69. typedef PACKED_STRUCT
  70. {
  71. es_frame_type_t frame_type;
  72. uint8_t k_scaler;
  73. uint32_t time_counter;
  74. uint8_t ik[ESCS_AES_KEY_SIZE];
  75. } es_eid_config_t;
  76. /**@brief Eddystone beacon lock state.
  77. */
  78. typedef nrf_ble_escs_lock_state_read_t es_security_lock_state_t;
  79. /**@brief Function for initializing the security module.
  80. *
  81. * @param[in] msg_cb Callback function.
  82. *
  83. * @return See @ref app_timer_start for possible return values.
  84. */
  85. ret_code_t es_security_init(es_security_msg_cb_t msg_cb);
  86. /**@brief Function for updating the lock code and storing it to flash.
  87. *
  88. * @param[in] p_encrypted_key Pointer to the new lock code.
  89. *
  90. * @return See @ref es_flash_access_lock_key for possible return values.
  91. */
  92. ret_code_t es_security_lock_code_update(uint8_t * p_encrypted_key);
  93. /**@brief Function for reading the challenge and encrypting it with AES_ECB.
  94. *
  95. * @details The result of the encryption is compared with the provided unlock token
  96. * in @ref es_security_unlock_verify.
  97. *
  98. * @param[in] p_challenge Pointer to the challenge buffer.
  99. *
  100. * @return See @ref sd_ecb_block_encrypt for possible return values.
  101. */
  102. void es_security_unlock_prepare(uint8_t * p_challenge);
  103. /**@brief Function for unlocking the beacon.
  104. *
  105. * @details This function compares the result from @ref es_security_unlock_prepare to the input
  106. * unlock token and unlocks the beacon if matching.
  107. *
  108. * @param[in] p_unlock_token The unlock token written by the client.
  109. */
  110. void es_security_unlock_verify(uint8_t * p_unlock_token);
  111. /**@brief Function for generating a random challenge for the unlock characteristic.
  112. *
  113. * @param[out] p_rand_chlg_buff Pointer to a buffer to which the random challenge is copied.
  114. *
  115. * @return See @ref sd_rand_application_vector_get for possible return values.
  116. */
  117. ret_code_t es_security_random_challenge_generate(uint8_t * p_rand_chlg_buff);
  118. /**@brief Function for storing the public ECDH key from the client in the beacon registration process.
  119. *
  120. * @details This function starts a series of cryptographic activities, including the generation of temporary keys and EIDs.
  121. *
  122. * @param[in] slot_no The index of the slot whose public ECDH key is retrieved.
  123. * @param[in] p_pub_ecdh Pointer to the public ECDH.
  124. * @param[in] scaler_k K rotation scaler.
  125. */
  126. void es_security_client_pub_ecdh_receive(uint8_t slot_no, uint8_t * p_pub_ecdh, uint8_t scaler_k);
  127. /**@brief Function for storing the shared IK from the client in the beacon registration process.
  128. *
  129. * @details This function starts a series of cryptographic activities, including the generation of temporary keys and EIDs.
  130. *
  131. * @param[in] slot_no The index of the slot whose public ECDH key is retrieved.
  132. * @param[in] p_encrypted_ik Pointer to the received IK.
  133. * @param[in] scaler_k K rotation scaler.
  134. */
  135. void es_security_shared_ik_receive(uint8_t slot_no, uint8_t * p_encrypted_ik, uint8_t scaler_k);
  136. /**@brief Function for copying the 32-byte ECDH key into the provided buffer.
  137. *
  138. * @param[in] slot_no The index of the slot whose public ECDH key is retrieved.
  139. * @param[out] p_edch_buffer Pointer to the buffer.
  140. */
  141. void es_security_pub_ecdh_get(uint8_t slot_no, uint8_t * p_edch_buffer);
  142. /**@brief Function for returning the beacon clock value (in little endian).
  143. *
  144. * @param[in] slot_no The index of the slot.
  145. *
  146. * @return 32-bit clock value.
  147. */
  148. uint32_t es_security_clock_get(uint8_t slot_no);
  149. /**@brief Function for updating the beacon time counter.
  150. *
  151. * @details This function checks how much time has passed since the last
  152. * invocation and, if required, updates the EID, the temporary key, or both.
  153. * The function generates an @ref ES_SECURITY_MSG_STORE_TIME event
  154. * for each active security slot every 24 hours.
  155. */
  156. void es_security_update_time(void);
  157. /**@brief Function for returning the rotation exponent scaler value.
  158. *
  159. * @param[in] slot_no The index of the slot.
  160. *
  161. * @return K rotation scaler.
  162. */
  163. uint8_t es_security_scaler_get(uint8_t slot_no);
  164. /**@brief Function for copying the 8-byte EID into the provided buffer.
  165. *
  166. * @param[in] slot_no The index of the slot whose EID is retrieved.
  167. * @param[out] p_eid_buffer Pointer to the buffer.
  168. */
  169. void es_security_eid_get(uint8_t slot_no, uint8_t * p_eid_buffer);
  170. /**@brief Function for restoring an EID slot.
  171. *
  172. * @param[in] slot_no The index of the slot to restore.
  173. * @param[in] k_scaler K rotation scaler.
  174. * @param[in] time_counter EID slot time counter value (in seconds).
  175. * @param[in] p_ik Pointer to the identity key of the specified slot.
  176. */
  177. void es_security_eid_slots_restore(uint8_t slot_no,
  178. uint8_t k_scaler,
  179. uint32_t time_counter,
  180. const uint8_t * p_ik);
  181. /**@brief Function for destroying stored EID states.
  182. *
  183. * @details This function should be called when the slot is either overwritten as another slot or
  184. * cleared by writing an empty byte or a single 0.
  185. *
  186. * @param[in] slot_no The index of the slot to destroy.
  187. */
  188. void es_security_eid_slot_destroy(uint8_t slot_no);
  189. /**@brief Function for copying the 16-byte EID ID key into the provided buffer.
  190. *
  191. * @param[in] slot_no The index of the EID slot whose IK is retrieved.
  192. * @param[out] p_key_buffer Buffer for the key.
  193. */
  194. void es_security_plain_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer);
  195. /**@brief Function for copying the 16-byte LK encrypted EID ID key into the provided buffer.
  196. *
  197. * @param[in] slot_no The index of the EID slot whose encrypted IK is retrieved.
  198. * @param[out] p_key_buffer Buffer for the key.
  199. */
  200. void es_security_encrypted_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer);
  201. /**@brief Function for converting a TLM frame into an eTLM frame using the EIK of the specified slot.
  202. *
  203. * @param[in] ik_slot_no The index of the EID slot whose IK is paired with the eTLM.
  204. * @param[in] p_tlm Pointer to the TLM frame buffer.
  205. * @param[out] p_etlm Pointer to the eTLM frame buffer.
  206. */
  207. void es_security_tlm_to_etlm(uint8_t ik_slot_no, es_tlm_frame_t * p_tlm, es_etlm_frame_t * p_etlm);
  208. /**
  209. * @}
  210. */
  211. #endif // ES_SECURITY_H__