blob: 7fa7440a53b31891282f0085fe14f6facfabc3db [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
Bence Szépkútic662b362021-05-27 11:25:03 +020036#include "mbedtls/build_info.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020037
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010039
Thomas Daubney4e9fb392021-06-03 11:51:08 +010040/*
Thomas Daubney416c46f2021-05-27 15:51:44 +010041 * Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
42 * defined in `ecdh.h`). For most applications, the choice of format makes
43 * no difference, since all library functions can work with either format,
44 * except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
45
46 * The new format used when this option is disabled is smaller
47 * (56 bytes on a 32-bit platform). In future versions of the library, it
48 * will support alternative implementations of ECDH operations.
49 * The new format is incompatible with applications that access
50 * context fields directly and with restartable ECP operations.
Thomas Daubney416c46f2021-05-27 15:51:44 +010051 */
52
53#if defined(MBEDTLS_ECP_RESTARTABLE)
54#define MBEDTLS_ECDH_LEGACY_CONTEXT
55#else
56#undef MBEDTLS_ECDH_LEGACY_CONTEXT
57#endif
58
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010059#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +000060#undef MBEDTLS_ECDH_LEGACY_CONTEXT
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010061#include "everest/everest.h"
62#endif
63
Paul Bakker407a0da2013-06-27 14:29:21 +020064#ifdef __cplusplus
65extern "C" {
66#endif
67
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010068/**
Rose Zadik68993282018-03-27 11:12:25 +010069 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010070 */
71typedef enum
72{
Rose Zadik7375b0f2018-04-16 16:04:57 +010073 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010074 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010076
Janos Follathc9c32f32018-08-13 15:52:45 +010077#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
78/**
79 * Defines the ECDH implementation used.
80 *
81 * Later versions of the library may add new variants, therefore users should
82 * not make any assumptions about them.
83 */
84typedef enum
85{
86 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
87 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010088#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
89 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
90#endif
Janos Follathc9c32f32018-08-13 15:52:45 +010091} mbedtls_ecdh_variant;
92
93/**
94 * The context used by the default ECDH implementation.
95 *
96 * Later versions might change the structure of this context, therefore users
97 * should not make any assumptions about the structure of
98 * mbedtls_ecdh_context_mbed.
99 */
100typedef struct mbedtls_ecdh_context_mbed
101{
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200102 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */
103 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */
104 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */
105 mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */
106 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100107#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200108 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100109#endif
110} mbedtls_ecdh_context_mbed;
111#endif
112
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100113/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200114 *
115 * \warning Performing multiple operations concurrently on the same
116 * ECDSA context is not supported; objects of this type
117 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000118 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100119 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200120typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100121{
Janos Follathc9c32f32018-08-13 15:52:45 +0100122#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200123 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */
124 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */
125 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */
126 mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */
127 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
128 int MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages. */
129 mbedtls_ecp_point MBEDTLS_PRIVATE(Vi); /*!< The blinding value. */
130 mbedtls_ecp_point MBEDTLS_PRIVATE(Vf); /*!< The unblinding value. */
131 mbedtls_mpi MBEDTLS_PRIVATE(_d); /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200132#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk2abe51c2021-06-07 11:08:01 +0200133 int MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. */
134 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100135#endif /* MBEDTLS_ECP_RESTARTABLE */
136#else
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200137 uint8_t MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages
Janos Follathc9c32f32018-08-13 15:52:45 +0100138 as defined in RFC 4492. */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200139 mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id);/*!< The elliptic curve used. */
140 mbedtls_ecdh_variant MBEDTLS_PRIVATE(var); /*!< The ECDH implementation/structure used. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100141 union
142 {
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200143 mbedtls_ecdh_context_mbed MBEDTLS_PRIVATE(mbed_ecdh);
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100144#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200145 mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100146#endif
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200147 } MBEDTLS_PRIVATE(ctx); /*!< Implementation-specific context. The
Janos Follathc9c32f32018-08-13 15:52:45 +0100148 context in use is specified by the \c var
149 field. */
150#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200151 uint8_t MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. Functions of
Janos Follathc9c32f32018-08-13 15:52:45 +0100152 an alternative implementation not supporting
153 restartable mode must return
154 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
155 if this flag is set. */
156#endif /* MBEDTLS_ECP_RESTARTABLE */
157#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100160
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100161/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100162 * \brief Check whether a given group can be used for ECDH.
163 *
164 * \param gid The ECP group ID to check.
165 *
166 * \return \c 1 if the group can be used, \c 0 otherwise
167 */
168int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid );
169
170/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000171 * \brief This function generates an ECDH keypair on an elliptic
172 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100173 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000174 * This function performs the first of two core computations
175 * implemented during the ECDH key exchange. The second core
176 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100177 *
Rose Zadik68993282018-03-27 11:12:25 +0100178 * \see ecp.h
179 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000180 * \param grp The ECP group to use. This must be initialized and have
181 * domain parameters loaded, for example through
182 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000183 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000184 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000185 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000186 * This must be initialized.
187 * \param f_rng The RNG function to use. This must not be \c NULL.
188 * \param p_rng The RNG context to be passed to \p f_rng. This may be
189 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000190 *
Rose Zadik68993282018-03-27 11:12:25 +0100191 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200192 * \return Another \c MBEDTLS_ERR_ECP_XXX or
193 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100196 int (*f_rng)(void *, unsigned char *, size_t),
197 void *p_rng );
198
199/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000200 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100201 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000202 * This function performs the second of two core computations
203 * implemented during the ECDH key exchange. The first core
204 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100205 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000206 * \see ecp.h
207 *
208 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100209 * countermeasures against side-channel attacks.
210 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100211 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000212 * \param grp The ECP group to use. This must be initialized and have
213 * domain parameters loaded, for example through
214 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100215 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000216 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100217 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000218 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100219 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000220 * This must be initialized.
Manuel Pégourié-Gonnard7861ecf2021-06-15 11:29:26 +0200221 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000222 * \param p_rng The RNG context to be passed to \p f_rng. This may be
223 * \c NULL if \p f_rng is \c NULL or doesn't need a
224 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100225 *
226 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200227 * \return Another \c MBEDTLS_ERR_ECP_XXX or
228 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
231 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200232 int (*f_rng)(void *, unsigned char *, size_t),
233 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100234
235/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000236 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100237 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000238 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100241
242/**
Janos Follathf61e4862018-10-30 11:53:25 +0000243 * \brief This function sets up the ECDH context with the information
244 * given.
245 *
246 * This function should be called after mbedtls_ecdh_init() but
247 * before mbedtls_ecdh_make_params(). There is no need to call
248 * this function before mbedtls_ecdh_read_params().
249 *
250 * This is the first function used by a TLS server for ECDHE
251 * ciphersuites.
252 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000253 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000254 * \param grp_id The group id of the group to set up the context for.
255 *
256 * \return \c 0 on success.
257 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000258int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
259 mbedtls_ecp_group_id grp_id );
Janos Follathf61e4862018-10-30 11:53:25 +0000260
261/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000262 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100263 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000264 * \param ctx The context to free. This may be \c NULL, in which
265 * case this function does nothing. If it is not \c NULL,
266 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100269
270/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000271 * \brief This function generates an EC key pair and exports its
272 * in the format used in a TLS ServerKeyExchange handshake
273 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100274 *
Janos Follathf61e4862018-10-30 11:53:25 +0000275 * This is the second function used by a TLS server for ECDHE
276 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100277 *
Rose Zadik68993282018-03-27 11:12:25 +0100278 * \see ecp.h
279 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000280 * \param ctx The ECDH context to use. This must be initialized
281 * and bound to a group, for example via mbedtls_ecdh_setup().
282 * \param olen The address at which to store the number of Bytes written.
283 * \param buf The destination buffer. This must be a writable buffer of
284 * length \p blen Bytes.
285 * \param blen The length of the destination buffer \p buf in Bytes.
286 * \param f_rng The RNG function to use. This must not be \c NULL.
287 * \param p_rng The RNG context to be passed to \p f_rng. This may be
288 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100289 *
Rose Zadik68993282018-03-27 11:12:25 +0100290 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200291 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200292 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200293 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100296 unsigned char *buf, size_t blen,
297 int (*f_rng)(void *, unsigned char *, size_t),
298 void *p_rng );
299
300/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000301 * \brief This function parses the ECDHE parameters in a
302 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100303 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000304 * \note In a TLS handshake, this is the how the client
305 * sets up its ECDHE context from the server's public
306 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100307 *
Rose Zadik68993282018-03-27 11:12:25 +0100308 * \see ecp.h
309 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000310 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000311 * \param buf On input, \c *buf must be the start of the input buffer.
312 * On output, \c *buf is updated to point to the end of the
313 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000314 * past the end of the ServerKeyExchange parameters.
315 * On error, this is the point at which an error has been
316 * detected, which is usually not useful except to debug
317 * failures.
318 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000319 *
Rose Zadik68993282018-03-27 11:12:25 +0100320 * \return \c 0 on success.
321 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000322 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000325 const unsigned char **buf,
326 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100327
328/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000329 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100330 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000331 * It is used by clients and servers in place of the
332 * ServerKeyEchange for static ECDH, and imports ECDH
333 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100334 *
Rose Zadik68993282018-03-27 11:12:25 +0100335 * \see ecp.h
336 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000337 * \param ctx The ECDH context to set up. This must be initialized.
338 * \param key The EC key to use. This must be initialized.
339 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000340 * - #MBEDTLS_ECDH_OURS: The key is ours.
341 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000342 *
Rose Zadik68993282018-03-27 11:12:25 +0100343 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200344 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000345 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100346 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000347int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
348 const mbedtls_ecp_keypair *key,
349 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100350
351/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000352 * \brief This function generates a public key and exports it
353 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100354 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000355 * This is the second function used by a TLS client for ECDH(E)
356 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100357 *
Rose Zadik68993282018-03-27 11:12:25 +0100358 * \see ecp.h
359 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000360 * \param ctx The ECDH context to use. This must be initialized
361 * and bound to a group, the latter usually by
362 * mbedtls_ecdh_read_params().
363 * \param olen The address at which to store the number of Bytes written.
364 * This must not be \c NULL.
365 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000366 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000367 * \param blen The size of the destination buffer \p buf in Bytes.
368 * \param f_rng The RNG function to use. This must not be \c NULL.
369 * \param p_rng The RNG context to be passed to \p f_rng. This may be
370 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000371 *
Rose Zadik68993282018-03-27 11:12:25 +0100372 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200373 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200374 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200375 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100376 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100378 unsigned char *buf, size_t blen,
379 int (*f_rng)(void *, unsigned char *, size_t),
380 void *p_rng );
381
382/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000383 * \brief This function parses and processes the ECDHE payload of a
384 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100385 *
Janos Follathf61e4862018-10-30 11:53:25 +0000386 * This is the third function used by a TLS server for ECDH(E)
387 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
388 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100389 *
Rose Zadik68993282018-03-27 11:12:25 +0100390 * \see ecp.h
391 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000392 * \param ctx The ECDH context to use. This must be initialized
393 * and bound to a group, for example via mbedtls_ecdh_setup().
394 * \param buf The pointer to the ClientKeyExchange payload. This must
395 * be a readable buffer of length \p blen Bytes.
396 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000397 *
Rose Zadik68993282018-03-27 11:12:25 +0100398 * \return \c 0 on success.
399 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000402 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100403
404/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000405 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100406 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000407 * This is the last function used by both TLS client
408 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100409 *
Rose Zadik68993282018-03-27 11:12:25 +0100410 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100411 * countermeasures against side-channel attacks.
412 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100413 *
414 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000415
416 * \param ctx The ECDH context to use. This must be initialized
417 * and have its own private key generated and the peer's
418 * public key imported.
419 * \param olen The address at which to store the total number of
420 * Bytes written on success. This must not be \c NULL.
421 * \param buf The buffer to write the generated shared key to. This
422 * must be a writable buffer of size \p blen Bytes.
423 * \param blen The length of the destination buffer \p buf in Bytes.
Manuel Pégourié-Gonnard7861ecf2021-06-15 11:29:26 +0200424 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000425 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
426 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000427 *
Rose Zadik68993282018-03-27 11:12:25 +0100428 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200429 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200430 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200431 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200434 unsigned char *buf, size_t blen,
435 int (*f_rng)(void *, unsigned char *, size_t),
436 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100437
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200438#if defined(MBEDTLS_ECP_RESTARTABLE)
439/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200440 * \brief This function enables restartable EC computations for this
441 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200442 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200443 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200444 *
445 * \note It is not possible to safely disable restartable
446 * computations once enabled, except by free-ing the context,
447 * which cancels possible in-progress operations.
448 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000449 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200450 */
451void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
452#endif /* MBEDTLS_ECP_RESTARTABLE */
453
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100454#ifdef __cplusplus
455}
456#endif
457
Paul Bakker9af723c2014-05-01 13:03:14 +0200458#endif /* ecdh.h */