ecdh.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * \file ecdh.h
  3. *
  4. * \brief This file contains ECDH definitions and functions.
  5. *
  6. * The Elliptic Curve Diffie-Hellman (ECDH) protocol is an anonymous
  7. * key agreement protocol allowing two parties to establish a shared
  8. * secret over an insecure channel. Each party must have an
  9. * elliptic-curve public–private key pair.
  10. *
  11. * For more information, see <em>NIST SP 800-56A Rev. 2: Recommendation for
  12. * Pair-Wise Key Establishment Schemes Using Discrete Logarithm
  13. * Cryptography</em>.
  14. */
  15. /*
  16. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  17. * SPDX-License-Identifier: Apache-2.0
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  20. * not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * http://www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  27. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. *
  31. * This file is part of Mbed TLS (https://tls.mbed.org)
  32. */
  33. #ifndef MBEDTLS_ECDH_H
  34. #define MBEDTLS_ECDH_H
  35. #include "ecp.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /**
  40. * Defines the source of the imported EC key.
  41. */
  42. typedef enum
  43. {
  44. MBEDTLS_ECDH_OURS, /**< Our key. */
  45. MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
  46. } mbedtls_ecdh_side;
  47. /**
  48. * \brief The ECDH context structure.
  49. */
  50. typedef struct
  51. {
  52. mbedtls_ecp_group grp; /*!< The elliptic curve used. */
  53. mbedtls_mpi d; /*!< The private key. */
  54. mbedtls_ecp_point Q; /*!< The public key. */
  55. mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
  56. mbedtls_mpi z; /*!< The shared secret. */
  57. int point_format; /*!< The format of point export in TLS messages. */
  58. mbedtls_ecp_point Vi; /*!< The blinding value. */
  59. mbedtls_ecp_point Vf; /*!< The unblinding value. */
  60. mbedtls_mpi _d; /*!< The previous \p d. */
  61. }
  62. mbedtls_ecdh_context;
  63. /**
  64. * \brief This function generates an ECDH keypair on an elliptic
  65. * curve.
  66. *
  67. * This function performs the first of two core computations
  68. * implemented during the ECDH key exchange. The second core
  69. * computation is performed by mbedtls_ecdh_compute_shared().
  70. *
  71. * \see ecp.h
  72. *
  73. * \param grp The ECP group.
  74. * \param d The destination MPI (private key).
  75. * \param Q The destination point (public key).
  76. * \param f_rng The RNG function.
  77. * \param p_rng The RNG context.
  78. *
  79. * \return \c 0 on success.
  80. * \return An \c MBEDTLS_ERR_ECP_XXX or
  81. * \c MBEDTLS_MPI_XXX error code on failure.
  82. *
  83. */
  84. int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  85. int (*f_rng)(void *, unsigned char *, size_t),
  86. void *p_rng );
  87. /**
  88. * \brief This function computes the shared secret.
  89. *
  90. * This function performs the second of two core computations
  91. * implemented during the ECDH key exchange. The first core
  92. * computation is performed by mbedtls_ecdh_gen_public().
  93. *
  94. * \see ecp.h
  95. *
  96. * \note If \p f_rng is not NULL, it is used to implement
  97. * countermeasures against side-channel attacks.
  98. * For more information, see mbedtls_ecp_mul().
  99. *
  100. * \param grp The ECP group.
  101. * \param z The destination MPI (shared secret).
  102. * \param Q The public key from another party.
  103. * \param d Our secret exponent (private key).
  104. * \param f_rng The RNG function.
  105. * \param p_rng The RNG context.
  106. *
  107. * \return \c 0 on success.
  108. * \return An \c MBEDTLS_ERR_ECP_XXX or
  109. * \c MBEDTLS_MPI_XXX error code on failure.
  110. */
  111. int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
  112. const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  113. int (*f_rng)(void *, unsigned char *, size_t),
  114. void *p_rng );
  115. /**
  116. * \brief This function initializes an ECDH context.
  117. *
  118. * \param ctx The ECDH context to initialize.
  119. */
  120. void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
  121. /**
  122. * \brief This function frees a context.
  123. *
  124. * \param ctx The context to free.
  125. */
  126. void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
  127. /**
  128. * \brief This function generates a public key and a TLS
  129. * ServerKeyExchange payload.
  130. *
  131. * This is the first function used by a TLS server for ECDHE
  132. * ciphersuites.
  133. *
  134. * \note This function assumes that the ECP group (grp) of the
  135. * \p ctx context has already been properly set,
  136. * for example, using mbedtls_ecp_group_load().
  137. *
  138. * \see ecp.h
  139. *
  140. * \param ctx The ECDH context.
  141. * \param olen The number of characters written.
  142. * \param buf The destination buffer.
  143. * \param blen The length of the destination buffer.
  144. * \param f_rng The RNG function.
  145. * \param p_rng The RNG context.
  146. *
  147. * \return \c 0 on success.
  148. * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  149. */
  150. int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
  151. unsigned char *buf, size_t blen,
  152. int (*f_rng)(void *, unsigned char *, size_t),
  153. void *p_rng );
  154. /**
  155. * \brief This function parses and processes a TLS ServerKeyExhange
  156. * payload.
  157. *
  158. * This is the first function used by a TLS client for ECDHE
  159. * ciphersuites.
  160. *
  161. * \see ecp.h
  162. *
  163. * \param ctx The ECDH context.
  164. * \param buf The pointer to the start of the input buffer.
  165. * \param end The address for one Byte past the end of the buffer.
  166. *
  167. * \return \c 0 on success.
  168. * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  169. *
  170. */
  171. int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
  172. const unsigned char **buf, const unsigned char *end );
  173. /**
  174. * \brief This function sets up an ECDH context from an EC key.
  175. *
  176. * It is used by clients and servers in place of the
  177. * ServerKeyEchange for static ECDH, and imports ECDH
  178. * parameters from the EC key information of a certificate.
  179. *
  180. * \see ecp.h
  181. *
  182. * \param ctx The ECDH context to set up.
  183. * \param key The EC key to use.
  184. * \param side Defines the source of the key: 1: Our key, or
  185. * 0: The key of the peer.
  186. *
  187. * \return \c 0 on success.
  188. * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  189. *
  190. */
  191. int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
  192. mbedtls_ecdh_side side );
  193. /**
  194. * \brief This function generates a public key and a TLS
  195. * ClientKeyExchange payload.
  196. *
  197. * This is the second function used by a TLS client for ECDH(E)
  198. * ciphersuites.
  199. *
  200. * \see ecp.h
  201. *
  202. * \param ctx The ECDH context.
  203. * \param olen The number of Bytes written.
  204. * \param buf The destination buffer.
  205. * \param blen The size of the destination buffer.
  206. * \param f_rng The RNG function.
  207. * \param p_rng The RNG context.
  208. *
  209. * \return \c 0 on success.
  210. * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  211. */
  212. int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
  213. unsigned char *buf, size_t blen,
  214. int (*f_rng)(void *, unsigned char *, size_t),
  215. void *p_rng );
  216. /**
  217. * \brief This function parses and processes a TLS ClientKeyExchange
  218. * payload.
  219. *
  220. * This is the second function used by a TLS server for ECDH(E)
  221. * ciphersuites.
  222. *
  223. * \see ecp.h
  224. *
  225. * \param ctx The ECDH context.
  226. * \param buf The start of the input buffer.
  227. * \param blen The length of the input buffer.
  228. *
  229. * \return \c 0 on success.
  230. * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  231. */
  232. int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
  233. const unsigned char *buf, size_t blen );
  234. /**
  235. * \brief This function derives and exports the shared secret.
  236. *
  237. * This is the last function used by both TLS client
  238. * and servers.
  239. *
  240. * \note If \p f_rng is not NULL, it is used to implement
  241. * countermeasures against side-channel attacks.
  242. * For more information, see mbedtls_ecp_mul().
  243. *
  244. * \see ecp.h
  245. *
  246. * \param ctx The ECDH context.
  247. * \param olen The number of Bytes written.
  248. * \param buf The destination buffer.
  249. * \param blen The length of the destination buffer.
  250. * \param f_rng The RNG function.
  251. * \param p_rng The RNG context.
  252. *
  253. * \return \c 0 on success.
  254. * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
  255. */
  256. int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
  257. unsigned char *buf, size_t blen,
  258. int (*f_rng)(void *, unsigned char *, size_t),
  259. void *p_rng );
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif /* ecdh.h */