blob: 82d3de0084d1ce5b565944ef0fdd795e90a36d98 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 * 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.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010030 */
Rose Zadikde2d6222018-01-25 21:57:43 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#ifndef MBEDTLS_ECDH_H
33#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010034
Ron Eldor9cbd1b22018-12-16 12:14:37 +020035#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/config.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020037#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010041#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010042
Thomas Daubney416c46f2021-05-27 15:51:44 +010043/**
44 * \def MBEDTLS_ECDH_LEGACY_CONTEXT
45 *
46 * Use a backward compatible ECDH context.
47 *
48 * Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
49 * defined in `ecdh.h`). For most applications, the choice of format makes
50 * no difference, since all library functions can work with either format,
51 * except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
52
53 * The new format used when this option is disabled is smaller
54 * (56 bytes on a 32-bit platform). In future versions of the library, it
55 * will support alternative implementations of ECDH operations.
56 * The new format is incompatible with applications that access
57 * context fields directly and with restartable ECP operations.
58 *
59 * Define this macro if you enable MBEDTLS_ECP_RESTARTABLE or if you
60 * want to access ECDH context fields directly. Otherwise you should
61 * comment out this macro definition.
62 *
63 * This option has no effect if #MBEDTLS_ECDH_C is not enabled.
64 *
65 * \note This configuration option is experimental. Future versions of the
66 * library may modify the way the ECDH context layout is configured
67 * and may modify the layout of the new context type.
68 */
69
70#if defined(MBEDTLS_ECP_RESTARTABLE)
71#define MBEDTLS_ECDH_LEGACY_CONTEXT
72#else
73#undef MBEDTLS_ECDH_LEGACY_CONTEXT
74#endif
75
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010076#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +000077#undef MBEDTLS_ECDH_LEGACY_CONTEXT
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010078#include "everest/everest.h"
79#endif
80
Paul Bakker407a0da2013-06-27 14:29:21 +020081#ifdef __cplusplus
82extern "C" {
83#endif
84
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010085/**
Rose Zadik68993282018-03-27 11:12:25 +010086 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010087 */
88typedef enum
89{
Rose Zadik7375b0f2018-04-16 16:04:57 +010090 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010091 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010093
Janos Follathc9c32f32018-08-13 15:52:45 +010094#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
95/**
96 * Defines the ECDH implementation used.
97 *
98 * Later versions of the library may add new variants, therefore users should
99 * not make any assumptions about them.
100 */
101typedef enum
102{
103 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
104 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100105#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
106 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
107#endif
Janos Follathc9c32f32018-08-13 15:52:45 +0100108} mbedtls_ecdh_variant;
109
110/**
111 * The context used by the default ECDH implementation.
112 *
113 * Later versions might change the structure of this context, therefore users
114 * should not make any assumptions about the structure of
115 * mbedtls_ecdh_context_mbed.
116 */
117typedef struct mbedtls_ecdh_context_mbed
118{
119 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
120 mbedtls_mpi d; /*!< The private key. */
121 mbedtls_ecp_point Q; /*!< The public key. */
122 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
123 mbedtls_mpi z; /*!< The shared secret. */
124#if defined(MBEDTLS_ECP_RESTARTABLE)
125 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
126#endif
127} mbedtls_ecdh_context_mbed;
128#endif
129
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100130/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200131 *
132 * \warning Performing multiple operations concurrently on the same
133 * ECDSA context is not supported; objects of this type
134 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000135 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200137typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100138{
Janos Follathc9c32f32018-08-13 15:52:45 +0100139#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Rose Zadikde2d6222018-01-25 21:57:43 +0000140 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
141 mbedtls_mpi d; /*!< The private key. */
142 mbedtls_ecp_point Q; /*!< The public key. */
143 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
144 mbedtls_mpi z; /*!< The shared secret. */
145 int point_format; /*!< The format of point export in TLS messages. */
146 mbedtls_ecp_point Vi; /*!< The blinding value. */
147 mbedtls_ecp_point Vf; /*!< The unblinding value. */
148 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200149#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200150 int restart_enabled; /*!< The flag for restartable mode. */
151 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100152#endif /* MBEDTLS_ECP_RESTARTABLE */
153#else
154 uint8_t point_format; /*!< The format of point export in TLS messages
155 as defined in RFC 4492. */
156 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
157 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
158 union
159 {
160 mbedtls_ecdh_context_mbed mbed_ecdh;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100161#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
162 mbedtls_ecdh_context_everest everest_ecdh;
163#endif
Janos Follathc9c32f32018-08-13 15:52:45 +0100164 } ctx; /*!< Implementation-specific context. The
165 context in use is specified by the \c var
166 field. */
167#if defined(MBEDTLS_ECP_RESTARTABLE)
168 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
169 an alternative implementation not supporting
170 restartable mode must return
171 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
172 if this flag is set. */
173#endif /* MBEDTLS_ECP_RESTARTABLE */
174#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100177
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100178/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100179 * \brief Check whether a given group can be used for ECDH.
180 *
181 * \param gid The ECP group ID to check.
182 *
183 * \return \c 1 if the group can be used, \c 0 otherwise
184 */
185int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid );
186
187/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000188 * \brief This function generates an ECDH keypair on an elliptic
189 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100190 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000191 * This function performs the first of two core computations
192 * implemented during the ECDH key exchange. The second core
193 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100194 *
Rose Zadik68993282018-03-27 11:12:25 +0100195 * \see ecp.h
196 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000197 * \param grp The ECP group to use. This must be initialized and have
198 * domain parameters loaded, for example through
199 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000200 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000201 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000202 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000203 * This must be initialized.
204 * \param f_rng The RNG function to use. This must not be \c NULL.
205 * \param p_rng The RNG context to be passed to \p f_rng. This may be
206 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000207 *
Rose Zadik68993282018-03-27 11:12:25 +0100208 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200209 * \return Another \c MBEDTLS_ERR_ECP_XXX or
210 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100213 int (*f_rng)(void *, unsigned char *, size_t),
214 void *p_rng );
215
216/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000217 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100218 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000219 * This function performs the second of two core computations
220 * implemented during the ECDH key exchange. The first core
221 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100222 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000223 * \see ecp.h
224 *
225 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100226 * countermeasures against side-channel attacks.
227 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100228 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000229 * \param grp The ECP group to use. This must be initialized and have
230 * domain parameters loaded, for example through
231 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100232 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000233 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100234 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000235 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100236 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000237 * This must be initialized.
238 * \param f_rng The RNG function. This may be \c NULL if randomization
239 * of intermediate results during the ECP computations is
240 * not needed (discouraged). See the documentation of
241 * mbedtls_ecp_mul() for more.
242 * \param p_rng The RNG context to be passed to \p f_rng. This may be
243 * \c NULL if \p f_rng is \c NULL or doesn't need a
244 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100245 *
246 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200247 * \return Another \c MBEDTLS_ERR_ECP_XXX or
248 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
251 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200252 int (*f_rng)(void *, unsigned char *, size_t),
253 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100254
255/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000256 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100257 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000258 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100261
262/**
Janos Follathf61e4862018-10-30 11:53:25 +0000263 * \brief This function sets up the ECDH context with the information
264 * given.
265 *
266 * This function should be called after mbedtls_ecdh_init() but
267 * before mbedtls_ecdh_make_params(). There is no need to call
268 * this function before mbedtls_ecdh_read_params().
269 *
270 * This is the first function used by a TLS server for ECDHE
271 * ciphersuites.
272 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000273 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000274 * \param grp_id The group id of the group to set up the context for.
275 *
276 * \return \c 0 on success.
277 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000278int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
279 mbedtls_ecp_group_id grp_id );
Janos Follathf61e4862018-10-30 11:53:25 +0000280
281/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000282 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100283 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000284 * \param ctx The context to free. This may be \c NULL, in which
285 * case this function does nothing. If it is not \c NULL,
286 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100289
290/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000291 * \brief This function generates an EC key pair and exports its
292 * in the format used in a TLS ServerKeyExchange handshake
293 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100294 *
Janos Follathf61e4862018-10-30 11:53:25 +0000295 * This is the second function used by a TLS server for ECDHE
296 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100297 *
Rose Zadik68993282018-03-27 11:12:25 +0100298 * \see ecp.h
299 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000300 * \param ctx The ECDH context to use. This must be initialized
301 * and bound to a group, for example via mbedtls_ecdh_setup().
302 * \param olen The address at which to store the number of Bytes written.
303 * \param buf The destination buffer. This must be a writable buffer of
304 * length \p blen Bytes.
305 * \param blen The length of the destination buffer \p buf in Bytes.
306 * \param f_rng The RNG function to use. This must not be \c NULL.
307 * \param p_rng The RNG context to be passed to \p f_rng. This may be
308 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100309 *
Rose Zadik68993282018-03-27 11:12:25 +0100310 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200311 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200312 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200313 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100316 unsigned char *buf, size_t blen,
317 int (*f_rng)(void *, unsigned char *, size_t),
318 void *p_rng );
319
320/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000321 * \brief This function parses the ECDHE parameters in a
322 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100323 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000324 * \note In a TLS handshake, this is the how the client
325 * sets up its ECDHE context from the server's public
326 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100327 *
Rose Zadik68993282018-03-27 11:12:25 +0100328 * \see ecp.h
329 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000330 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000331 * \param buf On input, \c *buf must be the start of the input buffer.
332 * On output, \c *buf is updated to point to the end of the
333 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000334 * past the end of the ServerKeyExchange parameters.
335 * On error, this is the point at which an error has been
336 * detected, which is usually not useful except to debug
337 * failures.
338 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000339 *
Rose Zadik68993282018-03-27 11:12:25 +0100340 * \return \c 0 on success.
341 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000342 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000345 const unsigned char **buf,
346 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100347
348/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000349 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100350 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000351 * It is used by clients and servers in place of the
352 * ServerKeyEchange for static ECDH, and imports ECDH
353 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100354 *
Rose Zadik68993282018-03-27 11:12:25 +0100355 * \see ecp.h
356 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000357 * \param ctx The ECDH context to set up. This must be initialized.
358 * \param key The EC key to use. This must be initialized.
359 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000360 * - #MBEDTLS_ECDH_OURS: The key is ours.
361 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000362 *
Rose Zadik68993282018-03-27 11:12:25 +0100363 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200364 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000365 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100366 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000367int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
368 const mbedtls_ecp_keypair *key,
369 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100370
371/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000372 * \brief This function generates a public key and exports it
373 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100374 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000375 * This is the second function used by a TLS client for ECDH(E)
376 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100377 *
Rose Zadik68993282018-03-27 11:12:25 +0100378 * \see ecp.h
379 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000380 * \param ctx The ECDH context to use. This must be initialized
381 * and bound to a group, the latter usually by
382 * mbedtls_ecdh_read_params().
383 * \param olen The address at which to store the number of Bytes written.
384 * This must not be \c NULL.
385 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000386 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000387 * \param blen The size of the destination buffer \p buf in Bytes.
388 * \param f_rng The RNG function to use. This must not be \c NULL.
389 * \param p_rng The RNG context to be passed to \p f_rng. This may be
390 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000391 *
Rose Zadik68993282018-03-27 11:12:25 +0100392 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200393 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200394 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200395 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100398 unsigned char *buf, size_t blen,
399 int (*f_rng)(void *, unsigned char *, size_t),
400 void *p_rng );
401
402/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000403 * \brief This function parses and processes the ECDHE payload of a
404 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100405 *
Janos Follathf61e4862018-10-30 11:53:25 +0000406 * This is the third function used by a TLS server for ECDH(E)
407 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
408 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100409 *
Rose Zadik68993282018-03-27 11:12:25 +0100410 * \see ecp.h
411 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000412 * \param ctx The ECDH context to use. This must be initialized
413 * and bound to a group, for example via mbedtls_ecdh_setup().
414 * \param buf The pointer to the ClientKeyExchange payload. This must
415 * be a readable buffer of length \p blen Bytes.
416 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000417 *
Rose Zadik68993282018-03-27 11:12:25 +0100418 * \return \c 0 on success.
419 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000422 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100423
424/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000425 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100426 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000427 * This is the last function used by both TLS client
428 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100429 *
Rose Zadik68993282018-03-27 11:12:25 +0100430 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100431 * countermeasures against side-channel attacks.
432 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100433 *
434 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000435
436 * \param ctx The ECDH context to use. This must be initialized
437 * and have its own private key generated and the peer's
438 * public key imported.
439 * \param olen The address at which to store the total number of
440 * Bytes written on success. This must not be \c NULL.
441 * \param buf The buffer to write the generated shared key to. This
442 * must be a writable buffer of size \p blen Bytes.
443 * \param blen The length of the destination buffer \p buf in Bytes.
444 * \param f_rng The RNG function, for blinding purposes. This may
445 * b \c NULL if blinding isn't needed.
446 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
447 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000448 *
Rose Zadik68993282018-03-27 11:12:25 +0100449 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200450 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200451 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200452 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200455 unsigned char *buf, size_t blen,
456 int (*f_rng)(void *, unsigned char *, size_t),
457 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100458
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200459#if defined(MBEDTLS_ECP_RESTARTABLE)
460/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200461 * \brief This function enables restartable EC computations for this
462 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200463 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200464 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200465 *
466 * \note It is not possible to safely disable restartable
467 * computations once enabled, except by free-ing the context,
468 * which cancels possible in-progress operations.
469 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000470 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200471 */
472void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
473#endif /* MBEDTLS_ECP_RESTARTABLE */
474
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100475#ifdef __cplusplus
476}
477#endif
478
Paul Bakker9af723c2014-05-01 13:03:14 +0200479#endif /* ecdh.h */