nrf_crypto_ecdh.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Copyright (c) 2018 - 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 NRF_CRYPTO_ECDH_H__
  41. #define NRF_CRYPTO_ECDH_H__
  42. /** @addtogroup nrf_crypto
  43. * @{
  44. * @addtogroup nrf_crypto_ecdh Elliptic Curve Diffie-Hellman (ECDH)
  45. * @{
  46. * @brief Provides elliptic curve cryptography functions for Diffie-Hellman shared secret exchange.
  47. *
  48. * @addtogroup nrf_crypto_ecdh_secp160r1 Definitions specific to secp160r1 (NIST 160-bit)
  49. * @addtogroup nrf_crypto_ecdh_secp160r2 Definitions specific to secp160r2 (NIST 160-bit)
  50. * @addtogroup nrf_crypto_ecdh_secp192r1 Definitions specific to secp192r1 (NIST 192-bit)
  51. * @addtogroup nrf_crypto_ecdh_secp224r1 Definitions specific to secp224r1 (NIST 224-bit)
  52. * @addtogroup nrf_crypto_ecdh_secp256r1 Definitions specific to secp256r1 (NIST 256-bit)
  53. * @addtogroup nrf_crypto_ecdh_secp384r1 Definitions specific to secp384r1 (NIST 384-bit)
  54. * @addtogroup nrf_crypto_ecdh_secp521r1 Definitions specific to secp521r1 (NIST 521-bit)
  55. * @addtogroup nrf_crypto_ecdh_secp160k1 Definitions specific to secp160k1 (Koblitz 160-bit)
  56. * @addtogroup nrf_crypto_ecdh_secp192k1 Definitions specific to secp192k1 (Koblitz 192-bit)
  57. * @addtogroup nrf_crypto_ecdh_secp224k1 Definitions specific to secp224k1 (Koblitz 224-bit)
  58. * @addtogroup nrf_crypto_ecdh_secp256k1 Definitions specific to secp256k1 (Koblitz 256-bit)
  59. * @addtogroup nrf_crypto_ecdh_bp256r1 Definitions specific to bp256r1 (Brainpool 256-bit)
  60. * @addtogroup nrf_crypto_ecdh_bp384r1 Definitions specific to bp384r1 (Brainpool 384-bit)
  61. * @addtogroup nrf_crypto_ecdh_bp512r1 Definitions specific to bp512r1 (Brainpool 512-bit)
  62. * @addtogroup nrf_crypto_ecdh_curve25519 Definitions specific to Curve25519
  63. */
  64. #include <stdint.h>
  65. #include <stddef.h>
  66. #include "nrf_crypto_error.h"
  67. #include "nrf_crypto_ecc.h"
  68. #include "nrf_crypto_ecdh_shared.h"
  69. #include "nrf_crypto_ecdh_backend.h"
  70. #ifdef __cplusplus
  71. extern "C" {
  72. #endif
  73. #define NRF_CRYPTO_ECDH_SECP160R1_SHARED_SECRET_SIZE (160 / 8) /**< @brief Number of bytes in a shared secret using secp160r1 (NIST 160-bit). @ingroup nrf_crypto_ecdh_secp160r1 */
  74. #define NRF_CRYPTO_ECDH_SECP160R2_SHARED_SECRET_SIZE (160 / 8) /**< @brief Number of bytes in a shared secret using secp160r2 (NIST 160-bit). @ingroup nrf_crypto_ecdh_secp160r2 */
  75. #define NRF_CRYPTO_ECDH_SECP192R1_SHARED_SECRET_SIZE (192 / 8) /**< @brief Number of bytes in a shared secret using secp192r1 (NIST 192-bit). @ingroup nrf_crypto_ecdh_secp192r1 */
  76. #define NRF_CRYPTO_ECDH_SECP224R1_SHARED_SECRET_SIZE (224 / 8) /**< @brief Number of bytes in a shared secret using secp224r1 (NIST 224-bit). @ingroup nrf_crypto_ecdh_secp224r1 */
  77. #define NRF_CRYPTO_ECDH_SECP256R1_SHARED_SECRET_SIZE (256 / 8) /**< @brief Number of bytes in a shared secret using secp256r1 (NIST 256-bit). @ingroup nrf_crypto_ecdh_secp256r1 */
  78. #define NRF_CRYPTO_ECDH_SECP384R1_SHARED_SECRET_SIZE (384 / 8) /**< @brief Number of bytes in a shared secret using secp384r1 (NIST 384-bit). @ingroup nrf_crypto_ecdh_secp384r1 */
  79. #define NRF_CRYPTO_ECDH_SECP521R1_SHARED_SECRET_SIZE (528 / 8) /**< @brief Number of bytes in a shared secret using secp521r1 (NIST 521-bit). @ingroup nrf_crypto_ecdh_secp521r1 */
  80. #define NRF_CRYPTO_ECDH_SECP160K1_SHARED_SECRET_SIZE (160 / 8) /**< @brief Number of bytes in a shared secret using secp160k1 (Koblitz 160-bit). @ingroup nrf_crypto_ecdh_secp160k1 */
  81. #define NRF_CRYPTO_ECDH_SECP192K1_SHARED_SECRET_SIZE (192 / 8) /**< @brief Number of bytes in a shared secret using secp192k1 (Koblitz 192-bit). @ingroup nrf_crypto_ecdh_secp192k1 */
  82. #define NRF_CRYPTO_ECDH_SECP224K1_SHARED_SECRET_SIZE (224 / 8) /**< @brief Number of bytes in a shared secret using secp224k1 (Koblitz 224-bit). @ingroup nrf_crypto_ecdh_secp224k1 */
  83. #define NRF_CRYPTO_ECDH_SECP256K1_SHARED_SECRET_SIZE (256 / 8) /**< @brief Number of bytes in a shared secret using secp256k1 (Koblitz 256-bit). @ingroup nrf_crypto_ecdh_secp256k1 */
  84. #define NRF_CRYPTO_ECDH_BP256R1_SHARED_SECRET_SIZE (256 / 8) /**< @brief Number of bytes in a shared secret using bp256r1 (Brainpool 256-bit). @ingroup nrf_crypto_ecdh_bp256r1 */
  85. #define NRF_CRYPTO_ECDH_BP384R1_SHARED_SECRET_SIZE (384 / 8) /**< @brief Number of bytes in a shared secret using bp384r1 (Brainpool 384-bit). @ingroup nrf_crypto_ecdh_bp384r1 */
  86. #define NRF_CRYPTO_ECDH_BP512R1_SHARED_SECRET_SIZE (512 / 8) /**< @brief Number of bytes in a shared secret using bp512r1 (Brainpool 512-bit). @ingroup nrf_crypto_ecdh_bp512r1 */
  87. #define NRF_CRYPTO_ECDH_CURVE25519_SHARED_SECRET_SIZE (256 / 8) /**< @brief Number of bytes in a shared secret using Curve25519. @ingroup nrf_crypto_ecdh_curve25519 */
  88. #define NRF_CRYPTO_ECDH_SHARED_SECRET_MAX_SIZE NRF_CRYPTO_ECC_RAW_PRIVATE_KEY_MAX_SIZE /**< @brief Maximum size of a shared secret in bytes for all enabled curves. */
  89. typedef nrf_crypto_backend_secp160r1_ecdh_context_t nrf_crypto_ecdh_secp160r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp160r1 (NIST 160-bit). @ingroup nrf_crypto_ecdh_secp160r1 */
  90. typedef nrf_crypto_backend_secp160r2_ecdh_context_t nrf_crypto_ecdh_secp160r2_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp160r2 (NIST 160-bit). @ingroup nrf_crypto_ecdh_secp160r2 */
  91. typedef nrf_crypto_backend_secp192r1_ecdh_context_t nrf_crypto_ecdh_secp192r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp192r1 (NIST 192-bit). @ingroup nrf_crypto_ecdh_secp192r1 */
  92. typedef nrf_crypto_backend_secp224r1_ecdh_context_t nrf_crypto_ecdh_secp224r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp224r1 (NIST 224-bit). @ingroup nrf_crypto_ecdh_secp224r1 */
  93. typedef nrf_crypto_backend_secp256r1_ecdh_context_t nrf_crypto_ecdh_secp256r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp256r1 (NIST 256-bit). @ingroup nrf_crypto_ecdh_secp256r1 */
  94. typedef nrf_crypto_backend_secp384r1_ecdh_context_t nrf_crypto_ecdh_secp384r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp384r1 (NIST 384-bit). @ingroup nrf_crypto_ecdh_secp384r1 */
  95. typedef nrf_crypto_backend_secp521r1_ecdh_context_t nrf_crypto_ecdh_secp521r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp521r1 (NIST 521-bit). @ingroup nrf_crypto_ecdh_secp521r1 */
  96. typedef nrf_crypto_backend_secp160k1_ecdh_context_t nrf_crypto_ecdh_secp160k1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp160k1 (Koblitz 160-bit). @ingroup nrf_crypto_ecdh_secp160k1 */
  97. typedef nrf_crypto_backend_secp192k1_ecdh_context_t nrf_crypto_ecdh_secp192k1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp192k1 (Koblitz 192-bit). @ingroup nrf_crypto_ecdh_secp192k1 */
  98. typedef nrf_crypto_backend_secp224k1_ecdh_context_t nrf_crypto_ecdh_secp224k1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp224k1 (Koblitz 224-bit). @ingroup nrf_crypto_ecdh_secp224k1 */
  99. typedef nrf_crypto_backend_secp256k1_ecdh_context_t nrf_crypto_ecdh_secp256k1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve secp256k1 (Koblitz 256-bit). @ingroup nrf_crypto_ecdh_secp256k1 */
  100. typedef nrf_crypto_backend_bp256r1_ecdh_context_t nrf_crypto_ecdh_bp256r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve bp256r1 (Brainpool 256-bit). @ingroup nrf_crypto_ecdh_bp256r1 */
  101. typedef nrf_crypto_backend_bp384r1_ecdh_context_t nrf_crypto_ecdh_bp384r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve bp384r1 (Brainpool 384-bit). @ingroup nrf_crypto_ecdh_bp384r1 */
  102. typedef nrf_crypto_backend_bp512r1_ecdh_context_t nrf_crypto_ecdh_bp512r1_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve bp512r1 (Brainpool 512-bit). @ingroup nrf_crypto_ecdh_bp512r1 */
  103. typedef nrf_crypto_backend_curve25519_ecdh_context_t nrf_crypto_ecdh_curve25519_context_t; /**< @brief Context used to store temporary data during computing ECDH for curve Curve25519. @ingroup nrf_crypto_ecdh_curve25519 */
  104. typedef uint8_t nrf_crypto_ecdh_secp160r1_shared_secret_t
  105. [NRF_CRYPTO_ECDH_SECP160R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp160r1 (NIST 160-bit). @ingroup nrf_crypto_ecdh_secp160r1 */
  106. typedef uint8_t nrf_crypto_ecdh_secp160r2_shared_secret_t
  107. [NRF_CRYPTO_ECDH_SECP160R2_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp160r2 (NIST 160-bit). @ingroup nrf_crypto_ecdh_secp160r2 */
  108. typedef uint8_t nrf_crypto_ecdh_secp192r1_shared_secret_t
  109. [NRF_CRYPTO_ECDH_SECP192R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp192r1 (NIST 192-bit). @ingroup nrf_crypto_ecdh_secp192r1 */
  110. typedef uint8_t nrf_crypto_ecdh_secp224r1_shared_secret_t
  111. [NRF_CRYPTO_ECDH_SECP224R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp224r1 (NIST 224-bit). @ingroup nrf_crypto_ecdh_secp224r1 */
  112. typedef uint8_t nrf_crypto_ecdh_secp256r1_shared_secret_t
  113. [NRF_CRYPTO_ECDH_SECP256R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp256r1 (NIST 256-bit). @ingroup nrf_crypto_ecdh_secp256r1 */
  114. typedef uint8_t nrf_crypto_ecdh_secp384r1_shared_secret_t
  115. [NRF_CRYPTO_ECDH_SECP384R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp384r1 (NIST 384-bit). @ingroup nrf_crypto_ecdh_secp384r1 */
  116. typedef uint8_t nrf_crypto_ecdh_secp521r1_shared_secret_t
  117. [NRF_CRYPTO_ECDH_SECP521R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp521r1 (NIST 521-bit). @ingroup nrf_crypto_ecdh_secp521r1 */
  118. typedef uint8_t nrf_crypto_ecdh_secp160k1_shared_secret_t
  119. [NRF_CRYPTO_ECDH_SECP160K1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp160k1 (Koblitz 160-bit). @ingroup nrf_crypto_ecdh_secp160k1 */
  120. typedef uint8_t nrf_crypto_ecdh_secp192k1_shared_secret_t
  121. [NRF_CRYPTO_ECDH_SECP192K1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp192k1 (Koblitz 192-bit). @ingroup nrf_crypto_ecdh_secp192k1 */
  122. typedef uint8_t nrf_crypto_ecdh_secp224k1_shared_secret_t
  123. [NRF_CRYPTO_ECDH_SECP224K1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp224k1 (Koblitz 224-bit). @ingroup nrf_crypto_ecdh_secp224k1 */
  124. typedef uint8_t nrf_crypto_ecdh_secp256k1_shared_secret_t
  125. [NRF_CRYPTO_ECDH_SECP256K1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve secp256k1 (Koblitz 256-bit). @ingroup nrf_crypto_ecdh_secp256k1 */
  126. typedef uint8_t nrf_crypto_ecdh_bp256r1_shared_secret_t
  127. [NRF_CRYPTO_ECDH_BP256R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve bp256r1 (Brainpool 256-bit). @ingroup nrf_crypto_ecdh_bp256r1 */
  128. typedef uint8_t nrf_crypto_ecdh_bp384r1_shared_secret_t
  129. [NRF_CRYPTO_ECDH_BP384R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve bp384r1 (Brainpool 384-bit). @ingroup nrf_crypto_ecdh_bp384r1 */
  130. typedef uint8_t nrf_crypto_ecdh_bp512r1_shared_secret_t
  131. [NRF_CRYPTO_ECDH_BP512R1_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve bp512r1 (Brainpool 512-bit). @ingroup nrf_crypto_ecdh_bp512r1 */
  132. typedef uint8_t nrf_crypto_ecdh_curve25519_shared_secret_t
  133. [NRF_CRYPTO_ECDH_CURVE25519_SHARED_SECRET_SIZE]; /**< @brief Array type of a shared secret for curve Curve25519. @ingroup nrf_crypto_ecdh_curve25519 */
  134. typedef uint8_t nrf_crypto_ecdh_shared_secret_t
  135. [NRF_CRYPTO_ECDH_SHARED_SECRET_MAX_SIZE]; /**< @brief Array type of a shared secret for any of the enabled curves. */
  136. /** @brief Union holding a context for ECDH computation.
  137. */
  138. typedef union
  139. {
  140. nrf_crypto_ecdh_secp160r1_context_t context_secp160r1; /**< @brief Occupies space for secp160r1 (NIST 160-bit). */
  141. nrf_crypto_ecdh_secp160r2_context_t context_secp160r2; /**< @brief Occupies space for secp160r2 (NIST 160-bit). */
  142. nrf_crypto_ecdh_secp192r1_context_t context_secp192r1; /**< @brief Occupies space for secp192r1 (NIST 192-bit). */
  143. nrf_crypto_ecdh_secp224r1_context_t context_secp224r1; /**< @brief Occupies space for secp224r1 (NIST 224-bit). */
  144. nrf_crypto_ecdh_secp256r1_context_t context_secp256r1; /**< @brief Occupies space for secp256r1 (NIST 256-bit). */
  145. nrf_crypto_ecdh_secp384r1_context_t context_secp384r1; /**< @brief Occupies space for secp384r1 (NIST 384-bit). */
  146. nrf_crypto_ecdh_secp521r1_context_t context_secp521r1; /**< @brief Occupies space for secp521r1 (NIST 521-bit). */
  147. nrf_crypto_ecdh_secp160k1_context_t context_secp160k1; /**< @brief Occupies space for secp160k1 (Koblitz 160-bit). */
  148. nrf_crypto_ecdh_secp192k1_context_t context_secp192k1; /**< @brief Occupies space for secp192k1 (Koblitz 192-bit). */
  149. nrf_crypto_ecdh_secp224k1_context_t context_secp224k1; /**< @brief Occupies space for secp224k1 (Koblitz 224-bit). */
  150. nrf_crypto_ecdh_secp256k1_context_t context_secp256k1; /**< @brief Occupies space for secp256k1 (Koblitz 256-bit). */
  151. nrf_crypto_ecdh_bp256r1_context_t context_bp256r1; /**< @brief Occupies space for bp256r1 (Brainpool 256-bit). */
  152. nrf_crypto_ecdh_bp384r1_context_t context_bp384r1; /**< @brief Occupies space for bp384r1 (Brainpool 384-bit). */
  153. nrf_crypto_ecdh_bp512r1_context_t context_bp512r1; /**< @brief Occupies space for bp512r1 (Brainpool 512-bit). */
  154. nrf_crypto_ecdh_curve25519_context_t context_curve25519; /**< @brief Occupies space for Curve25519. */
  155. } nrf_crypto_ecdh_context_t;
  156. /** @brief Computes shared secret using ECC Diffie-Hellman.
  157. *
  158. * @param[in] p_context Pointer to temporary structure holding context information.
  159. * If it is NULL, necessary data will be allocated with
  160. * @ref NRF_CRYPTO_ALLOC and freed at the end of the function.
  161. * @param[in] p_private_key Pointer to structure holding a private key.
  162. * @param[in] p_public_key Pointer to structure holding a public key received from the other party.
  163. * @param[out] p_shared_secret Pointer to buffer where shared secret will be put.
  164. * @param[in,out] p_shared_secret_size Maximum number of bytes that @p p_shared_secret buffer can hold on input
  165. * and the actual number of bytes used by the data on output.
  166. * Actual size for selected curve is defined by
  167. * the preprocessor definitions, e.g.
  168. * @ref NRF_CRYPTO_ECDH_SECP256R1_SHARED_SECRET_SIZE.
  169. */
  170. ret_code_t nrf_crypto_ecdh_compute(
  171. nrf_crypto_ecdh_context_t * p_context,
  172. nrf_crypto_ecc_private_key_t const * p_private_key,
  173. nrf_crypto_ecc_public_key_t const * p_public_key,
  174. uint8_t * p_shared_secret,
  175. size_t * p_shared_secret_size);
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. /** @}
  180. * @}
  181. */
  182. #endif // NRF_CRYPTO_ECDH_H__