blob: 807c884205fc216aa6e2df5dc0b765249f2c370e [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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020034#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010035
Ron Eldor9cbd1b22018-12-16 12:14:37 +020036#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/config.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020038#else
39#include MBEDTLS_CONFIG_FILE
40#endif
41
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010042#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010043
Thomas Daubney4e9fb392021-06-03 11:51:08 +010044/*
Thomas Daubney416c46f2021-05-27 15:51:44 +010045 * Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
46 * defined in `ecdh.h`). For most applications, the choice of format makes
47 * no difference, since all library functions can work with either format,
48 * except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
49
50 * The new format used when this option is disabled is smaller
51 * (56 bytes on a 32-bit platform). In future versions of the library, it
52 * will support alternative implementations of ECDH operations.
53 * The new format is incompatible with applications that access
54 * context fields directly and with restartable ECP operations.
Thomas Daubney416c46f2021-05-27 15:51:44 +010055 */
56
57#if defined(MBEDTLS_ECP_RESTARTABLE)
58#define MBEDTLS_ECDH_LEGACY_CONTEXT
59#else
60#undef MBEDTLS_ECDH_LEGACY_CONTEXT
61#endif
62
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010063#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +000064#undef MBEDTLS_ECDH_LEGACY_CONTEXT
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010065#include "everest/everest.h"
66#endif
67
Paul Bakker407a0da2013-06-27 14:29:21 +020068#ifdef __cplusplus
69extern "C" {
70#endif
71
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010072/**
Rose Zadik68993282018-03-27 11:12:25 +010073 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010074 */
75typedef enum
76{
Rose Zadik7375b0f2018-04-16 16:04:57 +010077 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010078 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010080
Janos Follathc9c32f32018-08-13 15:52:45 +010081#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
82/**
83 * Defines the ECDH implementation used.
84 *
85 * Later versions of the library may add new variants, therefore users should
86 * not make any assumptions about them.
87 */
88typedef enum
89{
90 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
91 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010092#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
93 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
94#endif
Janos Follathc9c32f32018-08-13 15:52:45 +010095} mbedtls_ecdh_variant;
96
97/**
98 * The context used by the default ECDH implementation.
99 *
100 * Later versions might change the structure of this context, therefore users
101 * should not make any assumptions about the structure of
102 * mbedtls_ecdh_context_mbed.
103 */
104typedef struct mbedtls_ecdh_context_mbed
105{
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200106 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */
107 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */
108 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */
109 mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */
110 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100111#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200112 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100113#endif
114} mbedtls_ecdh_context_mbed;
115#endif
116
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100117/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200118 *
119 * \warning Performing multiple operations concurrently on the same
120 * ECDSA context is not supported; objects of this type
121 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000122 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100123 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200124typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100125{
Janos Follathc9c32f32018-08-13 15:52:45 +0100126#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200127 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */
128 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */
129 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */
130 mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */
131 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
132 int MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages. */
133 mbedtls_ecp_point MBEDTLS_PRIVATE(Vi); /*!< The blinding value. */
134 mbedtls_ecp_point MBEDTLS_PRIVATE(Vf); /*!< The unblinding value. */
135 mbedtls_mpi MBEDTLS_PRIVATE(_d); /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200136#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200137 int restart_enabled; /*!< The flag for restartable mode. */
138 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100139#endif /* MBEDTLS_ECP_RESTARTABLE */
140#else
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200141 uint8_t MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages
Janos Follathc9c32f32018-08-13 15:52:45 +0100142 as defined in RFC 4492. */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200143 mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id);/*!< The elliptic curve used. */
144 mbedtls_ecdh_variant MBEDTLS_PRIVATE(var); /*!< The ECDH implementation/structure used. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100145 union
146 {
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200147 mbedtls_ecdh_context_mbed MBEDTLS_PRIVATE(mbed_ecdh);
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100148#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200149 mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100150#endif
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200151 } MBEDTLS_PRIVATE(ctx); /*!< Implementation-specific context. The
Janos Follathc9c32f32018-08-13 15:52:45 +0100152 context in use is specified by the \c var
153 field. */
154#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200155 uint8_t MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. Functions of
Janos Follathc9c32f32018-08-13 15:52:45 +0100156 an alternative implementation not supporting
157 restartable mode must return
158 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
159 if this flag is set. */
160#endif /* MBEDTLS_ECP_RESTARTABLE */
161#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100162}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100164
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100165/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100166 * \brief Check whether a given group can be used for ECDH.
167 *
168 * \param gid The ECP group ID to check.
169 *
170 * \return \c 1 if the group can be used, \c 0 otherwise
171 */
172int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid );
173
174/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000175 * \brief This function generates an ECDH keypair on an elliptic
176 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100177 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000178 * This function performs the first of two core computations
179 * implemented during the ECDH key exchange. The second core
180 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100181 *
Rose Zadik68993282018-03-27 11:12:25 +0100182 * \see ecp.h
183 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000184 * \param grp The ECP group to use. This must be initialized and have
185 * domain parameters loaded, for example through
186 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000187 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000188 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000189 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000190 * This must be initialized.
191 * \param f_rng The RNG function to use. This must not be \c NULL.
192 * \param p_rng The RNG context to be passed to \p f_rng. This may be
193 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000194 *
Rose Zadik68993282018-03-27 11:12:25 +0100195 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200196 * \return Another \c MBEDTLS_ERR_ECP_XXX or
197 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100200 int (*f_rng)(void *, unsigned char *, size_t),
201 void *p_rng );
202
203/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000204 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100205 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000206 * This function performs the second of two core computations
207 * implemented during the ECDH key exchange. The first core
208 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100209 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000210 * \see ecp.h
211 *
212 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100213 * countermeasures against side-channel attacks.
214 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100215 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000216 * \param grp The ECP group to use. This must be initialized and have
217 * domain parameters loaded, for example through
218 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100219 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000220 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100221 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000222 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100223 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000224 * This must be initialized.
225 * \param f_rng The RNG function. This may be \c NULL if randomization
226 * of intermediate results during the ECP computations is
227 * not needed (discouraged). See the documentation of
228 * mbedtls_ecp_mul() for more.
229 * \param p_rng The RNG context to be passed to \p f_rng. This may be
230 * \c NULL if \p f_rng is \c NULL or doesn't need a
231 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100232 *
233 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200234 * \return Another \c MBEDTLS_ERR_ECP_XXX or
235 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
238 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200239 int (*f_rng)(void *, unsigned char *, size_t),
240 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100241
242/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000243 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100244 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000245 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100248
249/**
Janos Follathf61e4862018-10-30 11:53:25 +0000250 * \brief This function sets up the ECDH context with the information
251 * given.
252 *
253 * This function should be called after mbedtls_ecdh_init() but
254 * before mbedtls_ecdh_make_params(). There is no need to call
255 * this function before mbedtls_ecdh_read_params().
256 *
257 * This is the first function used by a TLS server for ECDHE
258 * ciphersuites.
259 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000260 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000261 * \param grp_id The group id of the group to set up the context for.
262 *
263 * \return \c 0 on success.
264 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000265int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
266 mbedtls_ecp_group_id grp_id );
Janos Follathf61e4862018-10-30 11:53:25 +0000267
268/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000269 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100270 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000271 * \param ctx The context to free. This may be \c NULL, in which
272 * case this function does nothing. If it is not \c NULL,
273 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100276
277/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000278 * \brief This function generates an EC key pair and exports its
279 * in the format used in a TLS ServerKeyExchange handshake
280 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100281 *
Janos Follathf61e4862018-10-30 11:53:25 +0000282 * This is the second function used by a TLS server for ECDHE
283 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100284 *
Rose Zadik68993282018-03-27 11:12:25 +0100285 * \see ecp.h
286 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000287 * \param ctx The ECDH context to use. This must be initialized
288 * and bound to a group, for example via mbedtls_ecdh_setup().
289 * \param olen The address at which to store the number of Bytes written.
290 * \param buf The destination buffer. This must be a writable buffer of
291 * length \p blen Bytes.
292 * \param blen The length of the destination buffer \p buf in Bytes.
293 * \param f_rng The RNG function to use. This must not be \c NULL.
294 * \param p_rng The RNG context to be passed to \p f_rng. This may be
295 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100296 *
Rose Zadik68993282018-03-27 11:12:25 +0100297 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200298 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200299 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200300 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100303 unsigned char *buf, size_t blen,
304 int (*f_rng)(void *, unsigned char *, size_t),
305 void *p_rng );
306
307/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000308 * \brief This function parses the ECDHE parameters in a
309 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100310 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000311 * \note In a TLS handshake, this is the how the client
312 * sets up its ECDHE context from the server's public
313 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100314 *
Rose Zadik68993282018-03-27 11:12:25 +0100315 * \see ecp.h
316 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000317 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000318 * \param buf On input, \c *buf must be the start of the input buffer.
319 * On output, \c *buf is updated to point to the end of the
320 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000321 * past the end of the ServerKeyExchange parameters.
322 * On error, this is the point at which an error has been
323 * detected, which is usually not useful except to debug
324 * failures.
325 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000326 *
Rose Zadik68993282018-03-27 11:12:25 +0100327 * \return \c 0 on success.
328 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000329 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000332 const unsigned char **buf,
333 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100334
335/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000336 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100337 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000338 * It is used by clients and servers in place of the
339 * ServerKeyEchange for static ECDH, and imports ECDH
340 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100341 *
Rose Zadik68993282018-03-27 11:12:25 +0100342 * \see ecp.h
343 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000344 * \param ctx The ECDH context to set up. This must be initialized.
345 * \param key The EC key to use. This must be initialized.
346 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000347 * - #MBEDTLS_ECDH_OURS: The key is ours.
348 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000349 *
Rose Zadik68993282018-03-27 11:12:25 +0100350 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200351 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000352 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100353 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000354int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
355 const mbedtls_ecp_keypair *key,
356 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100357
358/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000359 * \brief This function generates a public key and exports it
360 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100361 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000362 * This is the second function used by a TLS client for ECDH(E)
363 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100364 *
Rose Zadik68993282018-03-27 11:12:25 +0100365 * \see ecp.h
366 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000367 * \param ctx The ECDH context to use. This must be initialized
368 * and bound to a group, the latter usually by
369 * mbedtls_ecdh_read_params().
370 * \param olen The address at which to store the number of Bytes written.
371 * This must not be \c NULL.
372 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000373 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000374 * \param blen The size of the destination buffer \p buf in Bytes.
375 * \param f_rng The RNG function to use. This must not be \c NULL.
376 * \param p_rng The RNG context to be passed to \p f_rng. This may be
377 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000378 *
Rose Zadik68993282018-03-27 11:12:25 +0100379 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200380 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200381 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200382 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100385 unsigned char *buf, size_t blen,
386 int (*f_rng)(void *, unsigned char *, size_t),
387 void *p_rng );
388
389/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000390 * \brief This function parses and processes the ECDHE payload of a
391 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100392 *
Janos Follathf61e4862018-10-30 11:53:25 +0000393 * This is the third function used by a TLS server for ECDH(E)
394 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
395 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100396 *
Rose Zadik68993282018-03-27 11:12:25 +0100397 * \see ecp.h
398 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000399 * \param ctx The ECDH context to use. This must be initialized
400 * and bound to a group, for example via mbedtls_ecdh_setup().
401 * \param buf The pointer to the ClientKeyExchange payload. This must
402 * be a readable buffer of length \p blen Bytes.
403 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000404 *
Rose Zadik68993282018-03-27 11:12:25 +0100405 * \return \c 0 on success.
406 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000409 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100410
411/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000412 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100413 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000414 * This is the last function used by both TLS client
415 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100416 *
Rose Zadik68993282018-03-27 11:12:25 +0100417 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100418 * countermeasures against side-channel attacks.
419 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100420 *
421 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000422
423 * \param ctx The ECDH context to use. This must be initialized
424 * and have its own private key generated and the peer's
425 * public key imported.
426 * \param olen The address at which to store the total number of
427 * Bytes written on success. This must not be \c NULL.
428 * \param buf The buffer to write the generated shared key to. This
429 * must be a writable buffer of size \p blen Bytes.
430 * \param blen The length of the destination buffer \p buf in Bytes.
431 * \param f_rng The RNG function, for blinding purposes. This may
432 * b \c NULL if blinding isn't needed.
433 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
434 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000435 *
Rose Zadik68993282018-03-27 11:12:25 +0100436 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200437 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200438 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200439 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100440 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200442 unsigned char *buf, size_t blen,
443 int (*f_rng)(void *, unsigned char *, size_t),
444 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100445
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200446#if defined(MBEDTLS_ECP_RESTARTABLE)
447/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200448 * \brief This function enables restartable EC computations for this
449 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200450 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200451 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200452 *
453 * \note It is not possible to safely disable restartable
454 * computations once enabled, except by free-ing the context,
455 * which cancels possible in-progress operations.
456 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000457 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200458 */
459void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
460#endif /* MBEDTLS_ECP_RESTARTABLE */
461
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100462#ifdef __cplusplus
463}
464#endif
465
Paul Bakker9af723c2014-05-01 13:03:14 +0200466#endif /* ecdh.h */