nrf_crypto_ecc_shared.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright (c) 2018, 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 NRF_CRYPTO_ECC_SHARED_H__
  41. #define NRF_CRYPTO_ECC_SHARED_H__
  42. #if !defined(__SDK_DOXYGEN__)
  43. #include <stdint.h>
  44. #include <stddef.h>
  45. #include <stdbool.h>
  46. #include "sdk_errors.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #define NRF_CRYPTO_INTERNAL_ECC_PRIVATE_KEY_INIT_VALUE (0x4D465276) /**< @internal @brief Init value for all ECC private keys. ASCII "nRFv". */
  51. #define NRF_CRYPTO_INTERNAL_ECC_PUBLIC_KEY_INIT_VALUE (0x4D465270) /**< @internal @brief Init value for all ECC public keys. ASCII "nRFp". */
  52. // Forward declaration only
  53. struct nrf_crypto_ecc_curve_info_s;
  54. /** @brief Header structure at the beginning of each key structure.
  55. */
  56. typedef struct
  57. {
  58. uint32_t init_value; /**< @internal @brief Init value to check if key was correctly initialized. */
  59. struct nrf_crypto_ecc_curve_info_s const * p_info; /**< @internal @brief Points to information structure of an associated curve type. */
  60. } nrf_crypto_internal_ecc_key_header_t;
  61. /** @internal @brief Function pointer for backend implementation of a key pair garatarion.
  62. *
  63. * @note All parameters provided to the backend are vefified in frontend. Verification includes
  64. * checking of NULL pointers, buffer size, initialization values. Front end also take full care of
  65. * common ECC key hearder @ref nrf_crypto_internal_ecc_key_header_t.
  66. *
  67. * @param[in] p_context Pointer to context.
  68. * @param[out] p_private_key Pointer where to put new private key.
  69. * @param[out] p_public_key Pointer where to put new public key.
  70. */
  71. typedef ret_code_t (*nrf_crypto_backend_ecc_key_pair_generate_fn_t)(
  72. void * p_context,
  73. void * p_private_key,
  74. void * p_public_key);
  75. /** @internal @brief Function pointer for backend implementation of a public key calculation.
  76. *
  77. * @note All parameters provided to the backend are vefified in frontend. Verification includes
  78. * checking of NULL pointers, buffer size, initialization values. Front end also take full care of
  79. * common ECC key hearder @ref nrf_crypto_internal_ecc_key_header_t.
  80. *
  81. * @param[in] p_context Pointer to context.
  82. * @param[in] p_private_key Pointer to private key.
  83. * @param[out] p_public_key Pointer where to put new public key.
  84. */
  85. typedef ret_code_t (*nrf_crypto_backend_ecc_public_key_calculate_fn_t)(
  86. void * p_context,
  87. void const * p_private_key,
  88. void * p_public_key);
  89. /** @internal @brief Function pointer for backend implementation of raw to private key conversion.
  90. *
  91. * @note All parameters provided to the backend are vefified in frontend. Verification includes
  92. * checking of NULL pointers, buffer size, initialization values. Front end also take full care of
  93. * common ECC key hearder @ref nrf_crypto_internal_ecc_key_header_t.
  94. *
  95. * @param[out] p_private_key Pointer where to put new private key.
  96. * @param[in] p_raw_data Pointer to raw data.
  97. */
  98. typedef ret_code_t (*nrf_crypto_backend_ecc_private_key_from_raw_fn_t)(
  99. void * p_private_key,
  100. uint8_t const * p_raw_data);
  101. /** @internal @brief Function pointer for backend implementation of private key to raw conversion.
  102. *
  103. * @note All parameters provided to the backend are vefified in frontend. Verification includes
  104. * checking of NULL pointers, buffer size, initialization values. Front end also take full care of
  105. * common ECC key hearder @ref nrf_crypto_internal_ecc_key_header_t.
  106. *
  107. * @param[in] p_private_key Pointer to private key.
  108. * @param[out] p_raw_data Pointer where to put raw data.
  109. */
  110. typedef ret_code_t (*nrf_crypto_backend_ecc_private_key_to_raw_fn_t)(
  111. void const * p_private_key,
  112. uint8_t * p_raw_data);
  113. /** @internal @brief Function pointer for backend implementation of raw to public key conversion.
  114. *
  115. * @note All parameters provided to the backend are vefified in frontend. Verification includes
  116. * checking of NULL pointers, buffer size, initialization values. Front end also take full care of
  117. * common ECC key hearder @ref nrf_crypto_internal_ecc_key_header_t.
  118. *
  119. * @param[out] p_public_key Pointer where to put new public key.
  120. * @param[in] p_raw_data Pointer to raw data.
  121. */
  122. typedef ret_code_t (*nrf_crypto_backend_ecc_public_key_from_raw_fn_t)(
  123. void * p_public_key,
  124. uint8_t const * p_raw_data);
  125. /** @internal @brief Function pointer for backend implementation of public key to raw conversion.
  126. *
  127. * @note All parameters provided to the backend are vefified in frontend. Verification includes
  128. * checking of NULL pointers, buffer size, initialization values. Front end also take full care of
  129. * common ECC key hearder @ref nrf_crypto_internal_ecc_key_header_t.
  130. *
  131. * @param[in] p_public_key Pointer to public key.
  132. * @param[out] p_raw_data Pointer where to put raw data.
  133. */
  134. typedef ret_code_t (*nrf_crypto_backend_ecc_public_key_to_raw_fn_t)(
  135. void const * p_public_key,
  136. uint8_t * p_raw_data);
  137. /** @internal @brief Function pointer for backend implementation of key (public or private) deallocation.
  138. *
  139. * @note All parameters provided to the backend are vefified in frontend. Verification includes
  140. * checking of NULL pointers, buffer size, initialization values. Front end also take full care of
  141. * common ECC key hearder @ref nrf_crypto_internal_ecc_key_header_t.
  142. *
  143. * @param[in] p_key Pointer to public or private key.
  144. */
  145. typedef ret_code_t (*nrf_crypto_backend_ecc_key_free_fn_t)(
  146. void * p_key);
  147. /** @internal @brief Function for checking and preparing ECC key output parameter.
  148. *
  149. * @param[in] p_curve_info Curve info provided by user that will be used to create a new key.
  150. * @param[out] p_key_header Key header that have to be prepared.
  151. * @return NRF_SUCCESS if parameters are valid, error otherwise.
  152. */
  153. ret_code_t nrf_crypto_internal_ecc_key_output_prepare(
  154. struct nrf_crypto_ecc_curve_info_s const * p_curve_info,
  155. nrf_crypto_internal_ecc_key_header_t * p_key_header);
  156. /** @internal @brief Function for checking ECC key input parameter.
  157. *
  158. * @param[in] p_key_header Key header that have to be checked.
  159. * @param[in] init_value Expected init value in this key.
  160. * @return NRF_SUCCESS if parameter is valid, error otherwise.
  161. */
  162. ret_code_t nrf_crypto_internal_ecc_key_input_check(
  163. nrf_crypto_internal_ecc_key_header_t const * p_key_header,
  164. uint32_t init_value);
  165. /** @internal @brief Function for checking and preparing raw data output parameter.
  166. *
  167. * @param[out] p_raw_data Buffer where output will be written.
  168. * @param[in,out] p_raw_data_size Pointer to size of the data. On input this is size of provided by
  169. * the user buffer. On output is equal @p expected_size. This pointer
  170. * can be NULL if used does not want to do size checking.
  171. * @param[in] expected_size Size of output data that will be written to the buffer.
  172. * @return NRF_SUCCESS if parameters are valid, error otherwise.
  173. */
  174. ret_code_t nrf_crypto_internal_ecc_raw_output_prepare(
  175. uint8_t * p_raw_data,
  176. size_t * p_raw_data_size,
  177. size_t expected_size);
  178. /** @internal @brief Function for checking raw data input parameter.
  179. *
  180. * @param[in] p_raw_data Buffer where data is located.
  181. * @param[in] raw_data_size Size of the data.Function will fail if it is different than @p expected_size.
  182. * @param[in] expected_size Expected size of the data.
  183. * @return NRF_SUCCESS if parameters are valid, error otherwise.
  184. */
  185. ret_code_t nrf_crypto_internal_ecc_raw_input_check(
  186. uint8_t const * p_raw_data,
  187. size_t raw_data_size,
  188. size_t expected_size);
  189. #ifdef __cplusplus
  190. }
  191. #endif
  192. #endif // !defined(__SDK_DOXYGEN__)
  193. #endif // NRF_CRYPTO_ECC_SHARED_H__