blob: 99f0375b3ad1fb2d1731534e9445bcbe531ed1ba [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
Dave Rodgman16799db2023-11-02 19:47:20 +000017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010018 */
Rose Zadikde2d6222018-01-25 21:57:43 +000019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#ifndef MBEDTLS_ECDH_H
21#define MBEDTLS_ECDH_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020022#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010023
Bence Szépkútic662b362021-05-27 11:25:03 +020024#include "mbedtls/build_info.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020025
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010026#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010027
Thomas Daubney4e9fb392021-06-03 11:51:08 +010028/*
Thomas Daubney416c46f2021-05-27 15:51:44 +010029 * Mbed TLS supports two formats for ECDH contexts (#mbedtls_ecdh_context
30 * defined in `ecdh.h`). For most applications, the choice of format makes
31 * no difference, since all library functions can work with either format,
32 * except that the new format is incompatible with MBEDTLS_ECP_RESTARTABLE.
33
34 * The new format used when this option is disabled is smaller
35 * (56 bytes on a 32-bit platform). In future versions of the library, it
36 * will support alternative implementations of ECDH operations.
37 * The new format is incompatible with applications that access
38 * context fields directly and with restartable ECP operations.
Thomas Daubney416c46f2021-05-27 15:51:44 +010039 */
40
41#if defined(MBEDTLS_ECP_RESTARTABLE)
42#define MBEDTLS_ECDH_LEGACY_CONTEXT
43#else
44#undef MBEDTLS_ECDH_LEGACY_CONTEXT
45#endif
46
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010047#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +000048#undef MBEDTLS_ECDH_LEGACY_CONTEXT
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010049#include "everest/everest.h"
50#endif
51
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010056/**
Rose Zadik68993282018-03-27 11:12:25 +010057 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059typedef enum {
Rose Zadik7375b0f2018-04-16 16:04:57 +010060 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010061 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010063
Janos Follathc9c32f32018-08-13 15:52:45 +010064#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
65/**
66 * Defines the ECDH implementation used.
67 *
68 * Later versions of the library may add new variants, therefore users should
69 * not make any assumptions about them.
70 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071typedef enum {
Janos Follathc9c32f32018-08-13 15:52:45 +010072 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
73 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010074#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
75 MBEDTLS_ECDH_VARIANT_EVEREST /*!< Everest implementation */
76#endif
Janos Follathc9c32f32018-08-13 15:52:45 +010077} mbedtls_ecdh_variant;
78
79/**
80 * The context used by the default ECDH implementation.
81 *
82 * Later versions might change the structure of this context, therefore users
83 * should not make any assumptions about the structure of
84 * mbedtls_ecdh_context_mbed.
85 */
Gilles Peskine449bd832023-01-11 14:50:10 +010086typedef struct mbedtls_ecdh_context_mbed {
Mateusz Starzyk363eb292021-05-19 17:32:44 +020087 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */
88 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */
89 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */
90 mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */
91 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
Janos Follathc9c32f32018-08-13 15:52:45 +010092#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk363eb292021-05-19 17:32:44 +020093 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +010094#endif
95} mbedtls_ecdh_context_mbed;
96#endif
97
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +000098#if defined(MBEDTLS_ECP_RESTARTABLE)
Waleed Elmelegy7817da02024-11-06 13:52:34 +000099#define MBEDTLS_ECDH_CONTEXT_MBED_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \
100 MBEDTLS_ECP_POINT_INIT, \
101 MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \
102 MBEDTLS_ECP_RESTART_INIT }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000103#else
Waleed Elmelegy7817da02024-11-06 13:52:34 +0000104#define MBEDTLS_ECDH_CONTEXT_MBED_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \
105 MBEDTLS_ECP_POINT_INIT, \
106 MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000107#endif
108
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100109/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200110 *
111 * \warning Performing multiple operations concurrently on the same
112 * ECDSA context is not supported; objects of this type
113 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000114 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116typedef struct mbedtls_ecdh_context {
Janos Follathc9c32f32018-08-13 15:52:45 +0100117#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200118 mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< The elliptic curve used. */
119 mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< The private key. */
120 mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< The public key. */
121 mbedtls_ecp_point MBEDTLS_PRIVATE(Qp); /*!< The value of the public key of the peer. */
122 mbedtls_mpi MBEDTLS_PRIVATE(z); /*!< The shared secret. */
123 int MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages. */
124 mbedtls_ecp_point MBEDTLS_PRIVATE(Vi); /*!< The blinding value. */
125 mbedtls_ecp_point MBEDTLS_PRIVATE(Vf); /*!< The unblinding value. */
126 mbedtls_mpi MBEDTLS_PRIVATE(_d); /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200127#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk2abe51c2021-06-07 11:08:01 +0200128 int MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. */
129 mbedtls_ecp_restart_ctx MBEDTLS_PRIVATE(rs); /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100130#endif /* MBEDTLS_ECP_RESTARTABLE */
131#else
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200132 uint8_t MBEDTLS_PRIVATE(point_format); /*!< The format of point export in TLS messages
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 as defined in RFC 4492. */
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200134 mbedtls_ecp_group_id MBEDTLS_PRIVATE(grp_id);/*!< The elliptic curve used. */
135 mbedtls_ecdh_variant MBEDTLS_PRIVATE(var); /*!< The ECDH implementation/structure used. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 union {
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200137 mbedtls_ecdh_context_mbed MBEDTLS_PRIVATE(mbed_ecdh);
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100138#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200139 mbedtls_ecdh_context_everest MBEDTLS_PRIVATE(everest_ecdh);
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100140#endif
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200141 } MBEDTLS_PRIVATE(ctx); /*!< Implementation-specific context. The
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 context in use is specified by the \c var
143 field. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100144#if defined(MBEDTLS_ECP_RESTARTABLE)
Mateusz Starzyk363eb292021-05-19 17:32:44 +0200145 uint8_t MBEDTLS_PRIVATE(restart_enabled); /*!< The flag for restartable mode. Functions of
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 an alternative implementation not supporting
147 restartable mode must return
148 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
149 if this flag is set. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100150#endif /* MBEDTLS_ECP_RESTARTABLE */
151#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100154
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000155#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
156#if defined(MBEDTLS_ECP_RESTARTABLE)
Waleed Elmelegy7817da02024-11-06 13:52:34 +0000157#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \
158 MBEDTLS_ECP_POINT_INIT, \
159 MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \
160 MBEDTLS_ECP_PF_UNCOMPRESSED, \
161 MBEDTLS_ECP_POINT_INIT, MBEDTLS_ECP_POINT_INIT, \
162 MBEDTLS_MPI_INIT, 0, \
163 MBEDTLS_ECP_RESTART_INIT }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000164#else
Waleed Elmelegy7817da02024-11-06 13:52:34 +0000165#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \
166 MBEDTLS_ECP_POINT_INIT, \
167 MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \
168 MBEDTLS_ECP_PF_UNCOMPRESSED, \
169 MBEDTLS_ECP_POINT_INIT, MBEDTLS_ECP_POINT_INIT, \
170 MBEDTLS_MPI_INIT }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000171#endif /* MBEDTLS_ECP_RESTARTABLE */
172#else
173#if !defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
174#if defined(MBEDTLS_ECP_RESTARTABLE)
Waleed Elmelegy7817da02024-11-06 13:52:34 +0000175#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \
176 MBEDTLS_ECDH_VARIANT_NONE, \
177 { .mbed_ecdh = MBEDTLS_ECDH_CONTEXT_MBED_INIT }, 0 }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000178#else
Waleed Elmelegy7817da02024-11-06 13:52:34 +0000179#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \
180 MBEDTLS_ECDH_VARIANT_NONE, \
181 { .mbed_ecdh = MBEDTLS_ECDH_CONTEXT_MBED_INIT } }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000182#endif /* MBEDTLS_ECP_RESTARTABLE */
183#else
184#if defined(MBEDTLS_ECP_RESTARTABLE)
Waleed Elmelegy7817da02024-11-06 13:52:34 +0000185#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \
186 MBEDTLS_ECDH_VARIANT_NONE, \
187 { .everest_ecdh = MBEDTLS_ECDH_CONTEXT_EVEREST_INIT }, 0 }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000188#else
Waleed Elmelegy7817da02024-11-06 13:52:34 +0000189#define MBEDTLS_ECDH_CONTEXT_INIT { MBEDTLS_ECP_PF_UNCOMPRESSED, MBEDTLS_ECP_DP_NONE, \
190 MBEDTLS_ECDH_VARIANT_NONE, \
191 { .everest_ecdh = MBEDTLS_ECDH_CONTEXT_EVEREST_INIT } }
Waleed Elmelegyf840b3a2024-11-01 15:04:48 +0000192#endif /* MBEDTLS_ECP_RESTARTABLE */
193#endif /* !MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */
194#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
195
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100196/**
Minos Galanakisd7537382024-02-23 12:17:59 +0000197 * \brief Return the ECP group for provided context.
198 *
199 * \note To access group specific fields, users should use
200 * `mbedtls_ecp_curve_info_from_grp_id` or
201 * `mbedtls_ecp_group_load` on the extracted `group_id`.
202 *
203 * \param ctx The ECDH context to parse. This must not be \c NULL.
204 *
205 * \return The \c mbedtls_ecp_group_id of the context.
206 */
207mbedtls_ecp_group_id mbedtls_ecdh_get_grp_id(mbedtls_ecdh_context *ctx);
208
209/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100210 * \brief Check whether a given group can be used for ECDH.
211 *
212 * \param gid The ECP group ID to check.
213 *
214 * \return \c 1 if the group can be used, \c 0 otherwise
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid);
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100217
218/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000219 * \brief This function generates an ECDH keypair on an elliptic
220 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100221 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000222 * This function performs the first of two core computations
223 * implemented during the ECDH key exchange. The second core
224 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100225 *
Rose Zadik68993282018-03-27 11:12:25 +0100226 * \see ecp.h
227 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000228 * \param grp The ECP group to use. This must be initialized and have
229 * domain parameters loaded, for example through
230 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000231 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000232 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000233 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000234 * This must be initialized.
235 * \param f_rng The RNG function to use. This must not be \c NULL.
236 * \param p_rng The RNG context to be passed to \p f_rng. This may be
237 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000238 *
Rose Zadik68993282018-03-27 11:12:25 +0100239 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200240 * \return Another \c MBEDTLS_ERR_ECP_XXX or
241 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
244 int (*f_rng)(void *, unsigned char *, size_t),
245 void *p_rng);
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100246
247/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000248 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100249 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000250 * This function performs the second of two core computations
251 * implemented during the ECDH key exchange. The first core
252 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100253 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000254 * \see ecp.h
255 *
256 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100257 * countermeasures against side-channel attacks.
258 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100259 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000260 * \param grp The ECP group to use. This must be initialized and have
261 * domain parameters loaded, for example through
262 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100263 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000264 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100265 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000266 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100267 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000268 * This must be initialized.
Manuel Pégourié-Gonnard7861ecf2021-06-15 11:29:26 +0200269 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000270 * \param p_rng The RNG context to be passed to \p f_rng. This may be
271 * \c NULL if \p f_rng is \c NULL or doesn't need a
272 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100273 *
274 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200275 * \return Another \c MBEDTLS_ERR_ECP_XXX or
276 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100277 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
279 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
280 int (*f_rng)(void *, unsigned char *, size_t),
281 void *p_rng);
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100282
283/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000284 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100285 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000286 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100289
290/**
Janos Follathf61e4862018-10-30 11:53:25 +0000291 * \brief This function sets up the ECDH context with the information
292 * given.
293 *
294 * This function should be called after mbedtls_ecdh_init() but
295 * before mbedtls_ecdh_make_params(). There is no need to call
296 * this function before mbedtls_ecdh_read_params().
297 *
298 * This is the first function used by a TLS server for ECDHE
299 * ciphersuites.
300 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000301 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000302 * \param grp_id The group id of the group to set up the context for.
303 *
304 * \return \c 0 on success.
305 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100306int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx,
307 mbedtls_ecp_group_id grp_id);
Janos Follathf61e4862018-10-30 11:53:25 +0000308
309/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000310 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100311 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000312 * \param ctx The context to free. This may be \c NULL, in which
313 * case this function does nothing. If it is not \c NULL,
314 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100315 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100317
318/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000319 * \brief This function generates an EC key pair and exports its
320 * in the format used in a TLS ServerKeyExchange handshake
321 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100322 *
Janos Follathf61e4862018-10-30 11:53:25 +0000323 * This is the second function used by a TLS server for ECDHE
324 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100325 *
Rose Zadik68993282018-03-27 11:12:25 +0100326 * \see ecp.h
327 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000328 * \param ctx The ECDH context to use. This must be initialized
329 * and bound to a group, for example via mbedtls_ecdh_setup().
330 * \param olen The address at which to store the number of Bytes written.
331 * \param buf The destination buffer. This must be a writable buffer of
332 * length \p blen Bytes.
333 * \param blen The length of the destination buffer \p buf in Bytes.
334 * \param f_rng The RNG function to use. This must not be \c NULL.
335 * \param p_rng The RNG context to be passed to \p f_rng. This may be
336 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100337 *
Rose Zadik68993282018-03-27 11:12:25 +0100338 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200339 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200340 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200341 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100342 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
344 unsigned char *buf, size_t blen,
345 int (*f_rng)(void *, unsigned char *, size_t),
346 void *p_rng);
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100347
348/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000349 * \brief This function parses the ECDHE parameters in a
350 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100351 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000352 * \note In a TLS handshake, this is the how the client
353 * sets up its ECDHE context from the server's public
354 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100355 *
Rose Zadik68993282018-03-27 11:12:25 +0100356 * \see ecp.h
357 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000358 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000359 * \param buf On input, \c *buf must be the start of the input buffer.
360 * On output, \c *buf is updated to point to the end of the
361 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000362 * past the end of the ServerKeyExchange parameters.
363 * On error, this is the point at which an error has been
364 * detected, which is usually not useful except to debug
365 * failures.
366 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000367 *
Rose Zadik68993282018-03-27 11:12:25 +0100368 * \return \c 0 on success.
369 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000370 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100371 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
373 const unsigned char **buf,
374 const unsigned char *end);
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100375
376/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000377 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100378 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000379 * It is used by clients and servers in place of the
Wenxing Houb4d03cc2024-06-19 11:04:13 +0800380 * ServerKeyExchange for static ECDH, and imports ECDH
Rose Zadikde2d6222018-01-25 21:57:43 +0000381 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100382 *
Rose Zadik68993282018-03-27 11:12:25 +0100383 * \see ecp.h
384 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000385 * \param ctx The ECDH context to set up. This must be initialized.
386 * \param key The EC key to use. This must be initialized.
387 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000388 * - #MBEDTLS_ECDH_OURS: The key is ours.
389 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000390 *
Rose Zadik68993282018-03-27 11:12:25 +0100391 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200392 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000393 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
396 const mbedtls_ecp_keypair *key,
397 mbedtls_ecdh_side side);
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100398
399/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000400 * \brief This function generates a public key and exports it
401 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100402 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000403 * This is the second function used by a TLS client for ECDH(E)
404 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100405 *
Rose Zadik68993282018-03-27 11:12:25 +0100406 * \see ecp.h
407 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000408 * \param ctx The ECDH context to use. This must be initialized
409 * and bound to a group, the latter usually by
410 * mbedtls_ecdh_read_params().
411 * \param olen The address at which to store the number of Bytes written.
412 * This must not be \c NULL.
413 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000414 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000415 * \param blen The size of the destination buffer \p buf in Bytes.
416 * \param f_rng The RNG function to use. This must not be \c NULL.
417 * \param p_rng The RNG context to be passed to \p f_rng. This may be
418 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000419 *
Rose Zadik68993282018-03-27 11:12:25 +0100420 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200421 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200422 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200423 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100424 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
426 unsigned char *buf, size_t blen,
427 int (*f_rng)(void *, unsigned char *, size_t),
428 void *p_rng);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100429
430/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000431 * \brief This function parses and processes the ECDHE payload of a
432 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100433 *
Janos Follathf61e4862018-10-30 11:53:25 +0000434 * This is the third function used by a TLS server for ECDH(E)
435 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
436 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100437 *
Rose Zadik68993282018-03-27 11:12:25 +0100438 * \see ecp.h
439 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000440 * \param ctx The ECDH context to use. This must be initialized
441 * and bound to a group, for example via mbedtls_ecdh_setup().
442 * \param buf The pointer to the ClientKeyExchange payload. This must
443 * be a readable buffer of length \p blen Bytes.
444 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000445 *
Rose Zadik68993282018-03-27 11:12:25 +0100446 * \return \c 0 on success.
447 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100448 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
450 const unsigned char *buf, size_t blen);
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100451
452/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000453 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100454 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000455 * This is the last function used by both TLS client
456 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100457 *
Rose Zadik68993282018-03-27 11:12:25 +0100458 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100459 * countermeasures against side-channel attacks.
460 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100461 *
462 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000463
464 * \param ctx The ECDH context to use. This must be initialized
465 * and have its own private key generated and the peer's
466 * public key imported.
467 * \param olen The address at which to store the total number of
468 * Bytes written on success. This must not be \c NULL.
469 * \param buf The buffer to write the generated shared key to. This
470 * must be a writable buffer of size \p blen Bytes.
471 * \param blen The length of the destination buffer \p buf in Bytes.
Manuel Pégourié-Gonnard7861ecf2021-06-15 11:29:26 +0200472 * \param f_rng The RNG function to use. This must not be \c NULL.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000473 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
474 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000475 *
Rose Zadik68993282018-03-27 11:12:25 +0100476 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200477 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200478 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200479 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100480 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
482 unsigned char *buf, size_t blen,
483 int (*f_rng)(void *, unsigned char *, size_t),
484 void *p_rng);
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100485
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200486#if defined(MBEDTLS_ECP_RESTARTABLE)
487/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200488 * \brief This function enables restartable EC computations for this
489 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200490 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200491 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200492 *
493 * \note It is not possible to safely disable restartable
494 * computations once enabled, except by free-ing the context,
495 * which cancels possible in-progress operations.
496 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000497 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200498 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100499void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx);
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200500#endif /* MBEDTLS_ECP_RESTARTABLE */
501
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100502#ifdef __cplusplus
503}
504#endif
505
Paul Bakker9af723c2014-05-01 13:03:14 +0200506#endif /* ecdh.h */