blob: e5edb46e6bab2dd1e70a930decc3f7aaf42642f4 [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 Daubney4e9fb392021-06-03 11:51:08 +010043/*
Thomas Daubney416c46f2021-05-27 15:51:44 +010044 * Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
45 * defined in `ecdh.h`). For most applications, the choice of format makes
46 * no difference, since all library functions can work with either format,
47 * except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
48
49 * The new format used when this option is disabled is smaller
50 * (56 bytes on a 32-bit platform). In future versions of the library, it
51 * will support alternative implementations of ECDH operations.
52 * The new format is incompatible with applications that access
53 * context fields directly and with restartable ECP operations.
54 *
Thomas Daubney416c46f2021-05-27 15:51:44 +010055 * This option has no effect if #MBEDTLS_ECDH_C is not enabled.
56 *
Thomas Daubney416c46f2021-05-27 15:51:44 +010057 */
58
59#if defined(MBEDTLS_ECP_RESTARTABLE)
60#define MBEDTLS_ECDH_LEGACY_CONTEXT
61#else
62#undef MBEDTLS_ECDH_LEGACY_CONTEXT
63#endif
64
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010065#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +000066#undef MBEDTLS_ECDH_LEGACY_CONTEXT
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010067#include "everest/everest.h"
68#endif
69
Paul Bakker407a0da2013-06-27 14:29:21 +020070#ifdef __cplusplus
71extern "C" {
72#endif
73
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010074/**
Rose Zadik68993282018-03-27 11:12:25 +010075 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010076 */
77typedef enum
78{
Rose Zadik7375b0f2018-04-16 16:04:57 +010079 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010080 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010082
Janos Follathc9c32f32018-08-13 15:52:45 +010083#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
84/**
85 * Defines the ECDH implementation used.
86 *
87 * Later versions of the library may add new variants, therefore users should
88 * not make any assumptions about them.
89 */
90typedef enum
91{
92 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
93 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010094#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
95 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
96#endif
Janos Follathc9c32f32018-08-13 15:52:45 +010097} mbedtls_ecdh_variant;
98
99/**
100 * The context used by the default ECDH implementation.
101 *
102 * Later versions might change the structure of this context, therefore users
103 * should not make any assumptions about the structure of
104 * mbedtls_ecdh_context_mbed.
105 */
106typedef struct mbedtls_ecdh_context_mbed
107{
108 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
109 mbedtls_mpi d; /*!< The private key. */
110 mbedtls_ecp_point Q; /*!< The public key. */
111 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
112 mbedtls_mpi z; /*!< The shared secret. */
113#if defined(MBEDTLS_ECP_RESTARTABLE)
114 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
115#endif
116} mbedtls_ecdh_context_mbed;
117#endif
118
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100119/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200120 *
121 * \warning Performing multiple operations concurrently on the same
122 * ECDSA context is not supported; objects of this type
123 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000124 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100125 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200126typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100127{
Janos Follathc9c32f32018-08-13 15:52:45 +0100128#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Rose Zadikde2d6222018-01-25 21:57:43 +0000129 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
130 mbedtls_mpi d; /*!< The private key. */
131 mbedtls_ecp_point Q; /*!< The public key. */
132 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
133 mbedtls_mpi z; /*!< The shared secret. */
134 int point_format; /*!< The format of point export in TLS messages. */
135 mbedtls_ecp_point Vi; /*!< The blinding value. */
136 mbedtls_ecp_point Vf; /*!< The unblinding value. */
137 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200138#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200139 int restart_enabled; /*!< The flag for restartable mode. */
140 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100141#endif /* MBEDTLS_ECP_RESTARTABLE */
142#else
143 uint8_t point_format; /*!< The format of point export in TLS messages
144 as defined in RFC 4492. */
145 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
146 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
147 union
148 {
149 mbedtls_ecdh_context_mbed mbed_ecdh;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100150#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
151 mbedtls_ecdh_context_everest everest_ecdh;
152#endif
Janos Follathc9c32f32018-08-13 15:52:45 +0100153 } ctx; /*!< Implementation-specific context. The
154 context in use is specified by the \c var
155 field. */
156#if defined(MBEDTLS_ECP_RESTARTABLE)
157 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
158 an alternative implementation not supporting
159 restartable mode must return
160 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
161 if this flag is set. */
162#endif /* MBEDTLS_ECP_RESTARTABLE */
163#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100166
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100167/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100168 * \brief Check whether a given group can be used for ECDH.
169 *
170 * \param gid The ECP group ID to check.
171 *
172 * \return \c 1 if the group can be used, \c 0 otherwise
173 */
174int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid );
175
176/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000177 * \brief This function generates an ECDH keypair on an elliptic
178 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100179 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000180 * This function performs the first of two core computations
181 * implemented during the ECDH key exchange. The second core
182 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100183 *
Rose Zadik68993282018-03-27 11:12:25 +0100184 * \see ecp.h
185 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000186 * \param grp The ECP group to use. This must be initialized and have
187 * domain parameters loaded, for example through
188 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000189 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000190 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000191 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000192 * This must be initialized.
193 * \param f_rng The RNG function to use. This must not be \c NULL.
194 * \param p_rng The RNG context to be passed to \p f_rng. This may be
195 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000196 *
Rose Zadik68993282018-03-27 11:12:25 +0100197 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200198 * \return Another \c MBEDTLS_ERR_ECP_XXX or
199 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100202 int (*f_rng)(void *, unsigned char *, size_t),
203 void *p_rng );
204
205/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000206 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100207 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000208 * This function performs the second of two core computations
209 * implemented during the ECDH key exchange. The first core
210 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100211 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000212 * \see ecp.h
213 *
214 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100215 * countermeasures against side-channel attacks.
216 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100217 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000218 * \param grp The ECP group to use. This must be initialized and have
219 * domain parameters loaded, for example through
220 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100221 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000222 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100223 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000224 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100225 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000226 * This must be initialized.
227 * \param f_rng The RNG function. This may be \c NULL if randomization
228 * of intermediate results during the ECP computations is
229 * not needed (discouraged). See the documentation of
230 * mbedtls_ecp_mul() for more.
231 * \param p_rng The RNG context to be passed to \p f_rng. This may be
232 * \c NULL if \p f_rng is \c NULL or doesn't need a
233 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100234 *
235 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200236 * \return Another \c MBEDTLS_ERR_ECP_XXX or
237 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
240 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200241 int (*f_rng)(void *, unsigned char *, size_t),
242 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100243
244/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000245 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100246 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000247 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100250
251/**
Janos Follathf61e4862018-10-30 11:53:25 +0000252 * \brief This function sets up the ECDH context with the information
253 * given.
254 *
255 * This function should be called after mbedtls_ecdh_init() but
256 * before mbedtls_ecdh_make_params(). There is no need to call
257 * this function before mbedtls_ecdh_read_params().
258 *
259 * This is the first function used by a TLS server for ECDHE
260 * ciphersuites.
261 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000262 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000263 * \param grp_id The group id of the group to set up the context for.
264 *
265 * \return \c 0 on success.
266 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000267int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
268 mbedtls_ecp_group_id grp_id );
Janos Follathf61e4862018-10-30 11:53:25 +0000269
270/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000271 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100272 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000273 * \param ctx The context to free. This may be \c NULL, in which
274 * case this function does nothing. If it is not \c NULL,
275 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100278
279/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000280 * \brief This function generates an EC key pair and exports its
281 * in the format used in a TLS ServerKeyExchange handshake
282 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100283 *
Janos Follathf61e4862018-10-30 11:53:25 +0000284 * This is the second function used by a TLS server for ECDHE
285 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100286 *
Rose Zadik68993282018-03-27 11:12:25 +0100287 * \see ecp.h
288 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000289 * \param ctx The ECDH context to use. This must be initialized
290 * and bound to a group, for example via mbedtls_ecdh_setup().
291 * \param olen The address at which to store the number of Bytes written.
292 * \param buf The destination buffer. This must be a writable buffer of
293 * length \p blen Bytes.
294 * \param blen The length of the destination buffer \p buf in Bytes.
295 * \param f_rng The RNG function to use. This must not be \c NULL.
296 * \param p_rng The RNG context to be passed to \p f_rng. This may be
297 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100298 *
Rose Zadik68993282018-03-27 11:12:25 +0100299 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200300 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200301 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200302 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100305 unsigned char *buf, size_t blen,
306 int (*f_rng)(void *, unsigned char *, size_t),
307 void *p_rng );
308
309/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000310 * \brief This function parses the ECDHE parameters in a
311 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100312 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000313 * \note In a TLS handshake, this is the how the client
314 * sets up its ECDHE context from the server's public
315 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100316 *
Rose Zadik68993282018-03-27 11:12:25 +0100317 * \see ecp.h
318 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000319 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000320 * \param buf On input, \c *buf must be the start of the input buffer.
321 * On output, \c *buf is updated to point to the end of the
322 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000323 * past the end of the ServerKeyExchange parameters.
324 * On error, this is the point at which an error has been
325 * detected, which is usually not useful except to debug
326 * failures.
327 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000328 *
Rose Zadik68993282018-03-27 11:12:25 +0100329 * \return \c 0 on success.
330 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000331 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000334 const unsigned char **buf,
335 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100336
337/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000338 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100339 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000340 * It is used by clients and servers in place of the
341 * ServerKeyEchange for static ECDH, and imports ECDH
342 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100343 *
Rose Zadik68993282018-03-27 11:12:25 +0100344 * \see ecp.h
345 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000346 * \param ctx The ECDH context to set up. This must be initialized.
347 * \param key The EC key to use. This must be initialized.
348 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000349 * - #MBEDTLS_ECDH_OURS: The key is ours.
350 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000351 *
Rose Zadik68993282018-03-27 11:12:25 +0100352 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200353 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000354 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100355 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000356int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
357 const mbedtls_ecp_keypair *key,
358 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100359
360/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000361 * \brief This function generates a public key and exports it
362 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100363 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000364 * This is the second function used by a TLS client for ECDH(E)
365 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100366 *
Rose Zadik68993282018-03-27 11:12:25 +0100367 * \see ecp.h
368 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000369 * \param ctx The ECDH context to use. This must be initialized
370 * and bound to a group, the latter usually by
371 * mbedtls_ecdh_read_params().
372 * \param olen The address at which to store the number of Bytes written.
373 * This must not be \c NULL.
374 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000375 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000376 * \param blen The size of the destination buffer \p buf in Bytes.
377 * \param f_rng The RNG function to use. This must not be \c NULL.
378 * \param p_rng The RNG context to be passed to \p f_rng. This may be
379 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000380 *
Rose Zadik68993282018-03-27 11:12:25 +0100381 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200382 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200383 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200384 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100387 unsigned char *buf, size_t blen,
388 int (*f_rng)(void *, unsigned char *, size_t),
389 void *p_rng );
390
391/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000392 * \brief This function parses and processes the ECDHE payload of a
393 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100394 *
Janos Follathf61e4862018-10-30 11:53:25 +0000395 * This is the third function used by a TLS server for ECDH(E)
396 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
397 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100398 *
Rose Zadik68993282018-03-27 11:12:25 +0100399 * \see ecp.h
400 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000401 * \param ctx The ECDH context to use. This must be initialized
402 * and bound to a group, for example via mbedtls_ecdh_setup().
403 * \param buf The pointer to the ClientKeyExchange payload. This must
404 * be a readable buffer of length \p blen Bytes.
405 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000406 *
Rose Zadik68993282018-03-27 11:12:25 +0100407 * \return \c 0 on success.
408 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000411 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100412
413/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000414 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100415 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000416 * This is the last function used by both TLS client
417 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100418 *
Rose Zadik68993282018-03-27 11:12:25 +0100419 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100420 * countermeasures against side-channel attacks.
421 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100422 *
423 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000424
425 * \param ctx The ECDH context to use. This must be initialized
426 * and have its own private key generated and the peer's
427 * public key imported.
428 * \param olen The address at which to store the total number of
429 * Bytes written on success. This must not be \c NULL.
430 * \param buf The buffer to write the generated shared key to. This
431 * must be a writable buffer of size \p blen Bytes.
432 * \param blen The length of the destination buffer \p buf in Bytes.
433 * \param f_rng The RNG function, for blinding purposes. This may
434 * b \c NULL if blinding isn't needed.
435 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
436 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000437 *
Rose Zadik68993282018-03-27 11:12:25 +0100438 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200439 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200440 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200441 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100442 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200444 unsigned char *buf, size_t blen,
445 int (*f_rng)(void *, unsigned char *, size_t),
446 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100447
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200448#if defined(MBEDTLS_ECP_RESTARTABLE)
449/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200450 * \brief This function enables restartable EC computations for this
451 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200452 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200453 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200454 *
455 * \note It is not possible to safely disable restartable
456 * computations once enabled, except by free-ing the context,
457 * which cancels possible in-progress operations.
458 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000459 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200460 */
461void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
462#endif /* MBEDTLS_ECP_RESTARTABLE */
463
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100464#ifdef __cplusplus
465}
466#endif
467
Paul Bakker9af723c2014-05-01 13:03:14 +0200468#endif /* ecdh.h */