nrf_ble_lesc.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright (c) 2018 - 2019, 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. /** @file
  41. *
  42. * @defgroup nrf_ble_lesc LESC module
  43. * @{
  44. * @ingroup peer_manager
  45. * @brief Module for handling LESC related events.
  46. */
  47. #ifndef NRF_BLE_LESC_H__
  48. #define NRF_BLE_LESC_H__
  49. #include <stdint.h>
  50. #include <string.h>
  51. #include "ble.h"
  52. #include "sdk_errors.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /**@brief Peer OOB Data handler prototype. */
  57. typedef ble_gap_lesc_oob_data_t * (* nrf_ble_lesc_peer_oob_data_handler)(uint16_t conn_handle);
  58. /**@brief Function for initializing the LESC module.
  59. *
  60. * @details This function initializes the nrf_crypto for ECC and ECDH calculations, which are
  61. * required to handle LESC authentication procedures.
  62. *
  63. * @retval NRF_SUCCESS If the operation was successful.
  64. * @retval Other Other error codes might be returned by the @ref nrf_crypto_init or
  65. * @ref nrf_ble_lesc_keypair_generate functions.
  66. */
  67. ret_code_t nrf_ble_lesc_init(void);
  68. /**@brief Function for generating ECC keypair used for the LESC procedure.
  69. *
  70. * @details This function generates an ECC key pair, which consists of a private and public key. Keys are
  71. * generated using ECC and are used to create LESC DH key during authentication procedures.
  72. *
  73. * @retval NRF_SUCCESS If the operation was successful.
  74. * @retval NRF_ERROR_BUSY If any pending request needs to be processed by @ref nrf_ble_lesc_request_handler.
  75. * @retval Other Other error codes might be returned by the @ref nrf_crypto_ecc_key_pair_generate,
  76. * @ref nrf_crypto_ecc_public_key_to_raw and @ref nrf_crypto_ecc_byte_order_invert
  77. * functions.
  78. */
  79. ret_code_t nrf_ble_lesc_keypair_generate(void);
  80. /**@brief Function for generating LESC OOB data.
  81. *
  82. * @details This function generates LESC OOB data, which can be transmitted Out-Of-Band to the peer
  83. * device and used during LESC procedure. It is required to generate ECC keypair with @ref
  84. * nrf_ble_lesc_keypair_generate before calling this function.
  85. *
  86. * @retval NRF_SUCCESS If the operation was successful.
  87. * @retval NRF_ERROR_INVALID_STATE If the ECC keypair hasn't been generated or is currently
  88. * being generated.
  89. */
  90. ret_code_t nrf_ble_lesc_own_oob_data_generate(void);
  91. /**@brief Function for accessing the ECC public key used for LESC DH key generation.
  92. *
  93. * @details This function can be used to access the ECC public key, which is required to generate a LESC DH key
  94. * at the peer side.
  95. *
  96. * @return Pointer to the generated public key or NULL if the key has not been generated yet.
  97. */
  98. ble_gap_lesc_p256_pk_t * nrf_ble_lesc_public_key_get(void);
  99. /**@brief Function for accessing LESC OOB data.
  100. *
  101. * @details This function can be used to access LESC OOB data that is associated with this device.
  102. * It is required to regenerate LESC OOB data with @ref nrf_ble_lesc_own_oob_data_generate,
  103. * after each change of ECC keypair with @ref nrf_ble_lesc_keypair_generate.
  104. *
  105. * @return Pointer to the LESC OOB data or NULL if the data has not been generated yet or is no
  106. * no longer valid.
  107. */
  108. ble_gap_lesc_oob_data_t * nrf_ble_lesc_own_oob_data_get(void);
  109. /**@brief Function for setting the handler used to retrieve peer OOB data.
  110. *
  111. * @param[in] handler Function to retrieve peer OOB data.
  112. */
  113. void nrf_ble_lesc_peer_oob_data_handler_set(nrf_ble_lesc_peer_oob_data_handler handler);
  114. /**@brief Function for responding to a DH key requests.
  115. *
  116. * @details This function calculates DH keys and supplies them to the SoftDevice if there are any
  117. * pending requests for keys.
  118. *
  119. * @note This function should be called systematically (e.g. in the main application loop) to handle
  120. * any pending DH key requests.
  121. *
  122. * @retval NRF_SUCCESS If the operation was successful.
  123. * @retval NRF_ERROR_INTERNAL If the LESC module encountered an internal error. The only way to recover from
  124. * this type of error is to reset the application.
  125. * @retval Other Other error codes might be returned by the @ref nrf_crypto_ecdh_compute,
  126. * @ref nrf_crypto_ecc_byte_order_invert, and @ref sd_ble_gap_lesc_dhkey_reply
  127. * functions.
  128. */
  129. ret_code_t nrf_ble_lesc_request_handler(void);
  130. /**@brief Function for handling BLE stack events.
  131. *
  132. * @details This function handles events from the BLE stack that are of interest to the module.
  133. *
  134. * @param[in] p_ble_evt Event received from the BLE stack.
  135. */
  136. void nrf_ble_lesc_on_ble_evt(ble_evt_t const * p_ble_evt);
  137. #ifdef __cplusplus
  138. }
  139. #endif
  140. #endif // NRF_BLE_LESC_H__
  141. /** @} */