blob: d1d8484de675eab6842a8114e945713b63358be8 [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)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020054# define MBEDTLS_ECDH_LEGACY_CONTEXT
Thomas Daubney416c46f2021-05-27 15:51:44 +010055#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# undef MBEDTLS_ECDH_LEGACY_CONTEXT
Thomas Daubney416c46f2021-05-27 15:51:44 +010057#endif
58
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010059#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060# undef MBEDTLS_ECDH_LEGACY_CONTEXT
61# include "everest/everest.h"
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010062#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{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073 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{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
87 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0, /*!< The default Mbed TLS implementation
88 */
89# if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
90 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
91# endif
Janos Follathc9c32f32018-08-13 15:52:45 +010092} mbedtls_ecdh_variant;
93
94/**
95 * The context used by the default ECDH implementation.
96 *
97 * Later versions might change the structure of this context, therefore users
98 * should not make any assumptions about the structure of
99 * mbedtls_ecdh_context_mbed.
100 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101typedef struct mbedtls_ecdh_context_mbed {
102 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
106 the peer. */
107 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
108# if defined(MBEDTLS_ECP_RESTARTABLE)
109 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC
110 computations. */
111# endif
Janos Follathc9c32f32018-08-13 15:52:45 +0100112} mbedtls_ecdh_context_mbed;
113#endif
114
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100115/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200116 *
117 * \warning Performing multiple operations concurrently on the same
118 * ECDSA context is not supported; objects of this type
119 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000120 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100121 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122typedef struct mbedtls_ecdh_context {
Janos Follathc9c32f32018-08-13 15:52:45 +0100123#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200124 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */
125 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */
126 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */
127 mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of
128 the peer. */
129 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
130 int MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS
131 messages. */
132 mbedtls_ecp_point MBEDTLS_PRIVATE(Vi); /*!< The blinding value. */
133 mbedtls_ecp_point MBEDTLS_PRIVATE(Vf); /*!< The unblinding value. */
134 mbedtls_mpi MBEDTLS_PRIVATE(_d); /*!< The previous \p d. */
135# if defined(MBEDTLS_ECP_RESTARTABLE)
136 int MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. */
137 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC
138 computations. */
139# endif /* MBEDTLS_ECP_RESTARTABLE */
Janos Follathc9c32f32018-08-13 15:52:45 +0100140#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141 uint8_t MBEDTLS_PRIVATE(point_format); /*!< The format of point export in
142 TLS messages as defined in RFC 4492. */
143 mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id); /*!< The elliptic curve used.
144 */
145 mbedtls_ecdh_variant MBEDTLS_PRIVATE(var); /*!< The ECDH
146 implementation/structure used.
147 */
148 union {
149 mbedtls_ecdh_context_mbed MBEDTLS_PRIVATE(mbed_ecdh);
150# if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200151 mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200152# endif
153 } MBEDTLS_PRIVATE(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 MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable
158 mode. Functions of an alternative implementation
159 not supporting restartable mode must return
160 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
161 if this flag is set. */
162# endif /* MBEDTLS_ECP_RESTARTABLE */
Janos Follathc9c32f32018-08-13 15:52:45 +0100163#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164} mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100165
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100166/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100167 * \brief Check whether a given group can be used for ECDH.
168 *
169 * \param gid The ECP group ID to check.
170 *
171 * \return \c 1 if the group can be used, \c 0 otherwise
172 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid);
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100174
175/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000176 * \brief This function generates an ECDH keypair on an elliptic
177 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100178 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000179 * This function performs the first of two core computations
180 * implemented during the ECDH key exchange. The second core
181 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100182 *
Rose Zadik68993282018-03-27 11:12:25 +0100183 * \see ecp.h
184 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000185 * \param grp The ECP group to use. This must be initialized and have
186 * domain parameters loaded, for example through
187 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000188 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000189 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000190 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000191 * This must be initialized.
192 * \param f_rng The RNG function to use. This must not be \c NULL.
193 * \param p_rng The RNG context to be passed to \p f_rng. This may be
194 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000195 *
Rose Zadik68993282018-03-27 11:12:25 +0100196 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200197 * \return Another \c MBEDTLS_ERR_ECP_XXX or
198 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100199 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200200int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp,
201 mbedtls_mpi *d,
202 mbedtls_ecp_point *Q,
203 int (*f_rng)(void *, unsigned char *, size_t),
204 void *p_rng);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100205
206/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000207 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100208 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000209 * This function performs the second of two core computations
210 * implemented during the ECDH key exchange. The first core
211 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100212 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000213 * \see ecp.h
214 *
215 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100216 * countermeasures against side-channel attacks.
217 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100218 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000219 * \param grp The ECP group to use. This must be initialized and have
220 * domain parameters loaded, for example through
221 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100222 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000223 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100224 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000225 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100226 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000227 * This must be initialized.
Manuel Pégourié-Gonnard7861ecf2021-06-15 11:29:26 +0200228 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000229 * \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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp,
238 mbedtls_mpi *z,
239 const mbedtls_ecp_point *Q,
240 const mbedtls_mpi *d,
241 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id);
Janos Follathf61e4862018-10-30 11:53:25 +0000268
269/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000270 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100271 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000272 * \param ctx The context to free. This may be \c NULL, in which
273 * case this function does nothing. If it is not \c NULL,
274 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100275 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200276void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100277
278/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000279 * \brief This function generates an EC key pair and exports its
280 * in the format used in a TLS ServerKeyExchange handshake
281 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100282 *
Janos Follathf61e4862018-10-30 11:53:25 +0000283 * This is the second function used by a TLS server for ECDHE
284 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100285 *
Rose Zadik68993282018-03-27 11:12:25 +0100286 * \see ecp.h
287 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000288 * \param ctx The ECDH context to use. This must be initialized
289 * and bound to a group, for example via mbedtls_ecdh_setup().
290 * \param olen The address at which to store the number of Bytes written.
291 * \param buf The destination buffer. This must be a writable buffer of
292 * length \p blen Bytes.
293 * \param blen The length of the destination buffer \p buf in Bytes.
294 * \param f_rng The RNG function to use. This must not be \c NULL.
295 * \param p_rng The RNG context to be passed to \p f_rng. This may be
296 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100297 *
Rose Zadik68993282018-03-27 11:12:25 +0100298 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200299 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200300 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200301 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100302 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200303int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx,
304 size_t *olen,
305 unsigned char *buf,
306 size_t blen,
307 int (*f_rng)(void *, unsigned char *, size_t),
308 void *p_rng);
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100309
310/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000311 * \brief This function parses the ECDHE parameters in a
312 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100313 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000314 * \note In a TLS handshake, this is the how the client
315 * sets up its ECDHE context from the server's public
316 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100317 *
Rose Zadik68993282018-03-27 11:12:25 +0100318 * \see ecp.h
319 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000320 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000321 * \param buf On input, \c *buf must be the start of the input buffer.
322 * On output, \c *buf is updated to point to the end of the
323 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000324 * past the end of the ServerKeyExchange parameters.
325 * On error, this is the point at which an error has been
326 * detected, which is usually not useful except to debug
327 * failures.
328 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000329 *
Rose Zadik68993282018-03-27 11:12:25 +0100330 * \return \c 0 on success.
331 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000332 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100333 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
335 const unsigned char **buf,
336 const unsigned char *end);
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100337
338/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000339 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100340 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000341 * It is used by clients and servers in place of the
342 * ServerKeyEchange for static ECDH, and imports ECDH
343 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100344 *
Rose Zadik68993282018-03-27 11:12:25 +0100345 * \see ecp.h
346 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000347 * \param ctx The ECDH context to set up. This must be initialized.
348 * \param key The EC key to use. This must be initialized.
349 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000350 * - #MBEDTLS_ECDH_OURS: The key is ours.
351 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000352 *
Rose Zadik68993282018-03-27 11:12:25 +0100353 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200354 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000355 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100356 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200357int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
358 const mbedtls_ecp_keypair *key,
359 mbedtls_ecdh_side side);
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100360
361/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000362 * \brief This function generates a public key and exports it
363 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100364 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000365 * This is the second function used by a TLS client for ECDH(E)
366 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100367 *
Rose Zadik68993282018-03-27 11:12:25 +0100368 * \see ecp.h
369 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000370 * \param ctx The ECDH context to use. This must be initialized
371 * and bound to a group, the latter usually by
372 * mbedtls_ecdh_read_params().
373 * \param olen The address at which to store the number of Bytes written.
374 * This must not be \c NULL.
375 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000376 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000377 * \param blen The size of the destination buffer \p buf in Bytes.
378 * \param f_rng The RNG function to use. This must not be \c NULL.
379 * \param p_rng The RNG context to be passed to \p f_rng. This may be
380 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000381 *
Rose Zadik68993282018-03-27 11:12:25 +0100382 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200383 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200384 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200385 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100386 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx,
388 size_t *olen,
389 unsigned char *buf,
390 size_t blen,
391 int (*f_rng)(void *, unsigned char *, size_t),
392 void *p_rng);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100393
394/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000395 * \brief This function parses and processes the ECDHE payload of a
396 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100397 *
Janos Follathf61e4862018-10-30 11:53:25 +0000398 * This is the third function used by a TLS server for ECDH(E)
399 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
400 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100401 *
Rose Zadik68993282018-03-27 11:12:25 +0100402 * \see ecp.h
403 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000404 * \param ctx The ECDH context to use. This must be initialized
405 * and bound to a group, for example via mbedtls_ecdh_setup().
406 * \param buf The pointer to the ClientKeyExchange payload. This must
407 * be a readable buffer of length \p blen Bytes.
408 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000409 *
Rose Zadik68993282018-03-27 11:12:25 +0100410 * \return \c 0 on success.
411 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100412 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200413int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
414 const unsigned char *buf,
415 size_t blen);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100416
417/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000418 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100419 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000420 * This is the last function used by both TLS client
421 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100422 *
Rose Zadik68993282018-03-27 11:12:25 +0100423 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100424 * countermeasures against side-channel attacks.
425 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100426 *
427 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000428
429 * \param ctx The ECDH context to use. This must be initialized
430 * and have its own private key generated and the peer's
431 * public key imported.
432 * \param olen The address at which to store the total number of
433 * Bytes written on success. This must not be \c NULL.
434 * \param buf The buffer to write the generated shared key to. This
435 * must be a writable buffer of size \p blen Bytes.
436 * \param blen The length of the destination buffer \p buf in Bytes.
Manuel Pégourié-Gonnard7861ecf2021-06-15 11:29:26 +0200437 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000438 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
439 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000440 *
Rose Zadik68993282018-03-27 11:12:25 +0100441 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200442 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200443 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200444 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100445 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200446int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx,
447 size_t *olen,
448 unsigned char *buf,
449 size_t blen,
450 int (*f_rng)(void *, unsigned char *, size_t),
451 void *p_rng);
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100452
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200453#if defined(MBEDTLS_ECP_RESTARTABLE)
454/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200455 * \brief This function enables restartable EC computations for this
456 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200457 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200458 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200459 *
460 * \note It is not possible to safely disable restartable
461 * computations once enabled, except by free-ing the context,
462 * which cancels possible in-progress operations.
463 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000464 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200465 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200466void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200467#endif /* MBEDTLS_ECP_RESTARTABLE */
468
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100469#ifdef __cplusplus
470}
471#endif
472
Paul Bakker9af723c2014-05-01 13:03:14 +0200473#endif /* ecdh.h */