ecdh.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * \file ecdh.h
  3. *
  4. * \brief Elliptic curve Diffie-Hellman
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_ECDH_H
  26. #define MBEDTLS_ECDH_H
  27. #include "ecp.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * When importing from an EC key, select if it is our key or the peer's key
  33. */
  34. typedef enum
  35. {
  36. MBEDTLS_ECDH_OURS,
  37. MBEDTLS_ECDH_THEIRS,
  38. } mbedtls_ecdh_side;
  39. /**
  40. * \brief ECDH context structure
  41. */
  42. typedef struct
  43. {
  44. mbedtls_ecp_group grp; /*!< elliptic curve used */
  45. mbedtls_mpi d; /*!< our secret value (private key) */
  46. mbedtls_ecp_point Q; /*!< our public value (public key) */
  47. mbedtls_ecp_point Qp; /*!< peer's public value (public key) */
  48. mbedtls_mpi z; /*!< shared secret */
  49. int point_format; /*!< format for point export in TLS messages */
  50. mbedtls_ecp_point Vi; /*!< blinding value (for later) */
  51. mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */
  52. mbedtls_mpi _d; /*!< previous d (for later) */
  53. }
  54. mbedtls_ecdh_context;
  55. /**
  56. * \brief Generate a public key.
  57. * Raw function that only does the core computation.
  58. *
  59. * \param grp ECP group
  60. * \param d Destination MPI (secret exponent, aka private key)
  61. * \param Q Destination point (public key)
  62. * \param f_rng RNG function
  63. * \param p_rng RNG parameter
  64. *
  65. * \return 0 if successful,
  66. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  67. */
  68. int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  69. int (*f_rng)(void *, unsigned char *, size_t),
  70. void *p_rng );
  71. /**
  72. * \brief Compute shared secret
  73. * Raw function that only does the core computation.
  74. *
  75. * \param grp ECP group
  76. * \param z Destination MPI (shared secret)
  77. * \param Q Public key from other party
  78. * \param d Our secret exponent (private key)
  79. * \param f_rng RNG function (see notes)
  80. * \param p_rng RNG parameter
  81. *
  82. * \return 0 if successful,
  83. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  84. *
  85. * \note If f_rng is not NULL, it is used to implement
  86. * countermeasures against potential elaborate timing
  87. * attacks, see \c mbedtls_ecp_mul() for details.
  88. */
  89. int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
  90. const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  91. int (*f_rng)(void *, unsigned char *, size_t),
  92. void *p_rng );
  93. /**
  94. * \brief Initialize context
  95. *
  96. * \param ctx Context to initialize
  97. */
  98. void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
  99. /**
  100. * \brief Free context
  101. *
  102. * \param ctx Context to free
  103. */
  104. void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
  105. /**
  106. * \brief Generate a public key and a TLS ServerKeyExchange payload.
  107. * (First function used by a TLS server for ECDHE.)
  108. *
  109. * \param ctx ECDH context
  110. * \param olen number of chars written
  111. * \param buf destination buffer
  112. * \param blen length of buffer
  113. * \param f_rng RNG function
  114. * \param p_rng RNG parameter
  115. *
  116. * \note This function assumes that ctx->grp has already been
  117. * properly set (for example using mbedtls_ecp_group_load).
  118. *
  119. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  120. */
  121. int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
  122. unsigned char *buf, size_t blen,
  123. int (*f_rng)(void *, unsigned char *, size_t),
  124. void *p_rng );
  125. /**
  126. * \brief Parse and procress a TLS ServerKeyExhange payload.
  127. * (First function used by a TLS client for ECDHE.)
  128. *
  129. * \param ctx ECDH context
  130. * \param buf pointer to start of input buffer
  131. * \param end one past end of buffer
  132. *
  133. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  134. */
  135. int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
  136. const unsigned char **buf, const unsigned char *end );
  137. /**
  138. * \brief Setup an ECDH context from an EC key.
  139. * (Used by clients and servers in place of the
  140. * ServerKeyEchange for static ECDH: import ECDH parameters
  141. * from a certificate's EC key information.)
  142. *
  143. * \param ctx ECDH constext to set
  144. * \param key EC key to use
  145. * \param side Is it our key (1) or the peer's key (0) ?
  146. *
  147. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  148. */
  149. int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
  150. mbedtls_ecdh_side side );
  151. /**
  152. * \brief Generate a public key and a TLS ClientKeyExchange payload.
  153. * (Second function used by a TLS client for ECDH(E).)
  154. *
  155. * \param ctx ECDH context
  156. * \param olen number of bytes actually written
  157. * \param buf destination buffer
  158. * \param blen size of destination buffer
  159. * \param f_rng RNG function
  160. * \param p_rng RNG parameter
  161. *
  162. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  163. */
  164. int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
  165. unsigned char *buf, size_t blen,
  166. int (*f_rng)(void *, unsigned char *, size_t),
  167. void *p_rng );
  168. /**
  169. * \brief Parse and process a TLS ClientKeyExchange payload.
  170. * (Second function used by a TLS server for ECDH(E).)
  171. *
  172. * \param ctx ECDH context
  173. * \param buf start of input buffer
  174. * \param blen length of input buffer
  175. *
  176. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  177. */
  178. int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
  179. const unsigned char *buf, size_t blen );
  180. /**
  181. * \brief Derive and export the shared secret.
  182. * (Last function used by both TLS client en servers.)
  183. *
  184. * \param ctx ECDH context
  185. * \param olen number of bytes written
  186. * \param buf destination buffer
  187. * \param blen buffer length
  188. * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared()
  189. * \param p_rng RNG parameter
  190. *
  191. * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
  192. */
  193. int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
  194. unsigned char *buf, size_t blen,
  195. int (*f_rng)(void *, unsigned char *, size_t),
  196. void *p_rng );
  197. #ifdef __cplusplus
  198. }
  199. #endif
  200. #endif /* ecdh.h */