blob: 6cc6cb92a77c33cecc47cdf9f5ee842bcab492e6 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/**
2 * \file ecdh.h
3 *
Rose Zadik68993282018-03-27 11:12:25 +01004 * \brief This file contains ECDH definitions and functions.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01005 *
Darryl Green11999bb2018-03-13 15:22:58 +00006 * The Elliptic Curve Diffie-Hellman (ECDH) protocol is an anonymous
Rose Zadik68993282018-03-27 11:12:25 +01007 * key agreement protocol allowing two parties to establish a shared
8 * secret over an insecure channel. Each party must have an
Rose Zadikde2d6222018-01-25 21:57:43 +00009 * 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>.
Darryl Greena40a1012018-01-05 15:33:17 +000014 */
15/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020016 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010018 */
Rose Zadikde2d6222018-01-25 21:57:43 +000019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#ifndef MBEDTLS_ECDH_H
21#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010022
Ron Eldor9cbd1b22018-12-16 12:14:37 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010024#include "mbedtls/config.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020025#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010029#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010030
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010031#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +000032#undef MBEDTLS_ECDH_LEGACY_CONTEXT
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010033#include "everest/everest.h"
34#endif
35
Paul Bakker407a0da2013-06-27 14:29:21 +020036#ifdef __cplusplus
37extern "C" {
38#endif
39
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010040/**
Rose Zadik68993282018-03-27 11:12:25 +010041 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010042 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043typedef enum {
Rose Zadik7375b0f2018-04-16 16:04:57 +010044 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010045 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010047
Janos Follathc9c32f32018-08-13 15:52:45 +010048#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
49/**
50 * Defines the ECDH implementation used.
51 *
52 * Later versions of the library may add new variants, therefore users should
53 * not make any assumptions about them.
54 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055typedef enum {
Janos Follathc9c32f32018-08-13 15:52:45 +010056 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
57 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010058#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
59 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
60#endif
Janos Follathc9c32f32018-08-13 15:52:45 +010061} mbedtls_ecdh_variant;
62
63/**
64 * The context used by the default ECDH implementation.
65 *
66 * Later versions might change the structure of this context, therefore users
67 * should not make any assumptions about the structure of
68 * mbedtls_ecdh_context_mbed.
69 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070typedef struct mbedtls_ecdh_context_mbed {
Janos Follathc9c32f32018-08-13 15:52:45 +010071 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
72 mbedtls_mpi d; /*!< The private key. */
73 mbedtls_ecp_point Q; /*!< The public key. */
74 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
75 mbedtls_mpi z; /*!< The shared secret. */
76#if defined(MBEDTLS_ECP_RESTARTABLE)
77 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
78#endif
79} mbedtls_ecdh_context_mbed;
80#endif
81
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010082/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020083 *
84 * \warning Performing multiple operations concurrently on the same
85 * ECDSA context is not supported; objects of this type
86 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +000087 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010088 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089typedef struct mbedtls_ecdh_context {
Janos Follathc9c32f32018-08-13 15:52:45 +010090#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Rose Zadikde2d6222018-01-25 21:57:43 +000091 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
92 mbedtls_mpi d; /*!< The private key. */
93 mbedtls_ecp_point Q; /*!< The public key. */
94 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
95 mbedtls_mpi z; /*!< The shared secret. */
96 int point_format; /*!< The format of point export in TLS messages. */
97 mbedtls_ecp_point Vi; /*!< The blinding value. */
98 mbedtls_ecp_point Vf; /*!< The unblinding value. */
99 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200100#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200101 int restart_enabled; /*!< The flag for restartable mode. */
102 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100103#endif /* MBEDTLS_ECP_RESTARTABLE */
104#else
105 uint8_t point_format; /*!< The format of point export in TLS messages
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 as defined in RFC 4492. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100107 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
108 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 union {
Janos Follathc9c32f32018-08-13 15:52:45 +0100110 mbedtls_ecdh_context_mbed mbed_ecdh;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100111#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
112 mbedtls_ecdh_context_everest everest_ecdh;
113#endif
Janos Follathc9c32f32018-08-13 15:52:45 +0100114 } ctx; /*!< Implementation-specific context. The
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 context in use is specified by the \c var
116 field. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100117#if defined(MBEDTLS_ECP_RESTARTABLE)
118 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 an alternative implementation not supporting
120 restartable mode must return
121 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
122 if this flag is set. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100123#endif /* MBEDTLS_ECP_RESTARTABLE */
124#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100127
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100128/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100129 * \brief Check whether a given group can be used for ECDH.
130 *
131 * \param gid The ECP group ID to check.
132 *
133 * \return \c 1 if the group can be used, \c 0 otherwise
134 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid);
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100136
137/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000138 * \brief This function generates an ECDH keypair on an elliptic
139 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100140 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000141 * This function performs the first of two core computations
142 * implemented during the ECDH key exchange. The second core
143 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100144 *
Rose Zadik68993282018-03-27 11:12:25 +0100145 * \see ecp.h
146 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000147 * \param grp The ECP group to use. This must be initialized and have
148 * domain parameters loaded, for example through
149 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000150 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000151 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000152 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000153 * This must be initialized.
154 * \param f_rng The RNG function to use. This must not be \c NULL.
155 * \param p_rng The RNG context to be passed to \p f_rng. This may be
156 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000157 *
Rose Zadik68993282018-03-27 11:12:25 +0100158 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200159 * \return Another \c MBEDTLS_ERR_ECP_XXX or
160 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100161 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
163 int (*f_rng)(void *, unsigned char *, size_t),
164 void *p_rng);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100165
166/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000167 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100168 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000169 * This function performs the second of two core computations
170 * implemented during the ECDH key exchange. The first core
171 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100172 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000173 * \see ecp.h
174 *
175 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100176 * countermeasures against side-channel attacks.
177 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100178 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000179 * \param grp The ECP group to use. This must be initialized and have
180 * domain parameters loaded, for example through
181 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100182 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000183 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100184 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000185 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100186 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000187 * This must be initialized.
188 * \param f_rng The RNG function. This may be \c NULL if randomization
189 * of intermediate results during the ECP computations is
190 * not needed (discouraged). See the documentation of
191 * mbedtls_ecp_mul() for more.
192 * \param p_rng The RNG context to be passed to \p f_rng. This may be
193 * \c NULL if \p f_rng is \c NULL or doesn't need a
194 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100195 *
196 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200197 * \return Another \c MBEDTLS_ERR_ECP_XXX or
198 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100199 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
201 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
202 int (*f_rng)(void *, unsigned char *, size_t),
203 void *p_rng);
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100204
205/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000206 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100207 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000208 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100211
212/**
Janos Follathf61e4862018-10-30 11:53:25 +0000213 * \brief This function sets up the ECDH context with the information
214 * given.
215 *
216 * This function should be called after mbedtls_ecdh_init() but
217 * before mbedtls_ecdh_make_params(). There is no need to call
218 * this function before mbedtls_ecdh_read_params().
219 *
220 * This is the first function used by a TLS server for ECDHE
221 * ciphersuites.
222 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000223 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000224 * \param grp_id The group id of the group to set up the context for.
225 *
226 * \return \c 0 on success.
227 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx,
229 mbedtls_ecp_group_id grp_id);
Janos Follathf61e4862018-10-30 11:53:25 +0000230
231/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000232 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100233 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000234 * \param ctx The context to free. This may be \c NULL, in which
235 * case this function does nothing. If it is not \c NULL,
236 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100237 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100239
240/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000241 * \brief This function generates an EC key pair and exports its
242 * in the format used in a TLS ServerKeyExchange handshake
243 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100244 *
Janos Follathf61e4862018-10-30 11:53:25 +0000245 * This is the second function used by a TLS server for ECDHE
246 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100247 *
Rose Zadik68993282018-03-27 11:12:25 +0100248 * \see ecp.h
249 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000250 * \param ctx The ECDH context to use. This must be initialized
251 * and bound to a group, for example via mbedtls_ecdh_setup().
252 * \param olen The address at which to store the number of Bytes written.
253 * \param buf The destination buffer. This must be a writable buffer of
254 * length \p blen Bytes.
255 * \param blen The length of the destination buffer \p buf in Bytes.
256 * \param f_rng The RNG function to use. This must not be \c NULL.
257 * \param p_rng The RNG context to be passed to \p f_rng. This may be
258 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100259 *
Rose Zadik68993282018-03-27 11:12:25 +0100260 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200261 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200262 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200263 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100264 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
266 unsigned char *buf, size_t blen,
267 int (*f_rng)(void *, unsigned char *, size_t),
268 void *p_rng);
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100269
270/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000271 * \brief This function parses the ECDHE parameters in a
272 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100273 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000274 * \note In a TLS handshake, this is the how the client
275 * sets up its ECDHE context from the server's public
276 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100277 *
Rose Zadik68993282018-03-27 11:12:25 +0100278 * \see ecp.h
279 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000280 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000281 * \param buf On input, \c *buf must be the start of the input buffer.
282 * On output, \c *buf is updated to point to the end of the
283 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000284 * past the end of the ServerKeyExchange parameters.
285 * On error, this is the point at which an error has been
286 * detected, which is usually not useful except to debug
287 * failures.
288 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000289 *
Rose Zadik68993282018-03-27 11:12:25 +0100290 * \return \c 0 on success.
291 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000292 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100293 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
295 const unsigned char **buf,
296 const unsigned char *end);
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100297
298/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000299 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100300 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000301 * It is used by clients and servers in place of the
302 * ServerKeyEchange for static ECDH, and imports ECDH
303 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100304 *
Rose Zadik68993282018-03-27 11:12:25 +0100305 * \see ecp.h
306 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000307 * \param ctx The ECDH context to set up. This must be initialized.
308 * \param key The EC key to use. This must be initialized.
309 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000310 * - #MBEDTLS_ECDH_OURS: The key is ours.
311 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000312 *
Rose Zadik68993282018-03-27 11:12:25 +0100313 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200314 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000315 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100316 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
318 const mbedtls_ecp_keypair *key,
319 mbedtls_ecdh_side side);
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100320
321/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000322 * \brief This function generates a public key and exports it
323 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100324 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000325 * This is the second function used by a TLS client for ECDH(E)
326 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100327 *
Rose Zadik68993282018-03-27 11:12:25 +0100328 * \see ecp.h
329 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000330 * \param ctx The ECDH context to use. This must be initialized
331 * and bound to a group, the latter usually by
332 * mbedtls_ecdh_read_params().
333 * \param olen The address at which to store the number of Bytes written.
334 * This must not be \c NULL.
335 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000336 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000337 * \param blen The size of the destination buffer \p buf in Bytes.
338 * \param f_rng The RNG function to use. This must not be \c NULL.
339 * \param p_rng The RNG context to be passed to \p f_rng. This may be
340 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000341 *
Rose Zadik68993282018-03-27 11:12:25 +0100342 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200343 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200344 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200345 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100346 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
348 unsigned char *buf, size_t blen,
349 int (*f_rng)(void *, unsigned char *, size_t),
350 void *p_rng);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100351
352/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000353 * \brief This function parses and processes the ECDHE payload of a
354 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100355 *
Janos Follathf61e4862018-10-30 11:53:25 +0000356 * This is the third function used by a TLS server for ECDH(E)
357 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
358 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100359 *
Rose Zadik68993282018-03-27 11:12:25 +0100360 * \see ecp.h
361 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000362 * \param ctx The ECDH context to use. This must be initialized
363 * and bound to a group, for example via mbedtls_ecdh_setup().
364 * \param buf The pointer to the ClientKeyExchange payload. This must
365 * be a readable buffer of length \p blen Bytes.
366 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000367 *
Rose Zadik68993282018-03-27 11:12:25 +0100368 * \return \c 0 on success.
369 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100370 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
372 const unsigned char *buf, size_t blen);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100373
374/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000375 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100376 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000377 * This is the last function used by both TLS client
378 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100379 *
Rose Zadik68993282018-03-27 11:12:25 +0100380 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100381 * countermeasures against side-channel attacks.
382 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100383 *
384 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000385
386 * \param ctx The ECDH context to use. This must be initialized
387 * and have its own private key generated and the peer's
388 * public key imported.
389 * \param olen The address at which to store the total number of
390 * Bytes written on success. This must not be \c NULL.
391 * \param buf The buffer to write the generated shared key to. This
392 * must be a writable buffer of size \p blen Bytes.
393 * \param blen The length of the destination buffer \p buf in Bytes.
394 * \param f_rng The RNG function, for blinding purposes. This may
395 * b \c NULL if blinding isn't needed.
396 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
397 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000398 *
Rose Zadik68993282018-03-27 11:12:25 +0100399 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200400 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200401 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200402 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100403 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
405 unsigned char *buf, size_t blen,
406 int (*f_rng)(void *, unsigned char *, size_t),
407 void *p_rng);
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100408
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200409#if defined(MBEDTLS_ECP_RESTARTABLE)
410/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200411 * \brief This function enables restartable EC computations for this
412 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200413 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200414 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200415 *
416 * \note It is not possible to safely disable restartable
417 * computations once enabled, except by free-ing the context,
418 * which cancels possible in-progress operations.
419 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000420 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200421 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200423#endif /* MBEDTLS_ECP_RESTARTABLE */
424
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100425#ifdef __cplusplus
426}
427#endif
428
Paul Bakker9af723c2014-05-01 13:03:14 +0200429#endif /* ecdh.h */