blob: a0052df4719ef7facbeee97957e1af0159466b1a [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/*
Rose Zadikde2d6222018-01-25 21:57:43 +000016 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Bence Szépkútif744bd72020-06-05 13:02:18 +020017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
18 *
19 * This file is provided under the Apache License 2.0, or the
20 * GNU General Public License v2.0 or later.
21 *
22 * **********
23 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020024 *
25 * Licensed under the Apache License, Version 2.0 (the "License"); you may
26 * not use this file except in compliance with the License.
27 * You may obtain a copy of the License at
28 *
29 * http://www.apache.org/licenses/LICENSE-2.0
30 *
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010036 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020037 * **********
38 *
39 * **********
40 * GNU General Public License v2.0 or later:
41 *
42 * This program is free software; you can redistribute it and/or modify
43 * it under the terms of the GNU General Public License as published by
44 * the Free Software Foundation; either version 2 of the License, or
45 * (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License along
53 * with this program; if not, write to the Free Software Foundation, Inc.,
54 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55 *
56 * **********
57 *
Rose Zadikde2d6222018-01-25 21:57:43 +000058 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010059 */
Rose Zadikde2d6222018-01-25 21:57:43 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#ifndef MBEDTLS_ECDH_H
62#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010063
Ron Eldor9cbd1b22018-12-16 12:14:37 +020064#if !defined(MBEDTLS_CONFIG_FILE)
65#include "config.h"
66#else
67#include MBEDTLS_CONFIG_FILE
68#endif
69
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020070#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010071
Janos Follathc9c32f32018-08-13 15:52:45 +010072/*
73 * Use a backward compatible ECDH context.
74 *
75 * This flag is always enabled for now and future versions might add a
76 * configuration option that conditionally undefines this flag.
77 * The configuration option in question may have a different name.
78 *
79 * Features undefining this flag, must have a warning in their description in
80 * config.h stating that the feature breaks backward compatibility.
81 */
82#define MBEDTLS_ECDH_LEGACY_CONTEXT
83
Paul Bakker407a0da2013-06-27 14:29:21 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010088/**
Rose Zadik68993282018-03-27 11:12:25 +010089 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010090 */
91typedef enum
92{
Rose Zadik7375b0f2018-04-16 16:04:57 +010093 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010094 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010096
Janos Follathc9c32f32018-08-13 15:52:45 +010097#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
98/**
99 * Defines the ECDH implementation used.
100 *
101 * Later versions of the library may add new variants, therefore users should
102 * not make any assumptions about them.
103 */
104typedef enum
105{
106 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
107 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
108} mbedtls_ecdh_variant;
109
110/**
111 * The context used by the default ECDH implementation.
112 *
113 * Later versions might change the structure of this context, therefore users
114 * should not make any assumptions about the structure of
115 * mbedtls_ecdh_context_mbed.
116 */
117typedef struct mbedtls_ecdh_context_mbed
118{
119 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
120 mbedtls_mpi d; /*!< The private key. */
121 mbedtls_ecp_point Q; /*!< The public key. */
122 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
123 mbedtls_mpi z; /*!< The shared secret. */
124#if defined(MBEDTLS_ECP_RESTARTABLE)
125 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
126#endif
127} mbedtls_ecdh_context_mbed;
128#endif
129
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100130/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200131 *
132 * \warning Performing multiple operations concurrently on the same
133 * ECDSA context is not supported; objects of this type
134 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000135 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200137typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100138{
Janos Follathc9c32f32018-08-13 15:52:45 +0100139#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Rose Zadikde2d6222018-01-25 21:57:43 +0000140 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
141 mbedtls_mpi d; /*!< The private key. */
142 mbedtls_ecp_point Q; /*!< The public key. */
143 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
144 mbedtls_mpi z; /*!< The shared secret. */
145 int point_format; /*!< The format of point export in TLS messages. */
146 mbedtls_ecp_point Vi; /*!< The blinding value. */
147 mbedtls_ecp_point Vf; /*!< The unblinding value. */
148 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200149#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200150 int restart_enabled; /*!< The flag for restartable mode. */
151 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100152#endif /* MBEDTLS_ECP_RESTARTABLE */
153#else
154 uint8_t point_format; /*!< The format of point export in TLS messages
155 as defined in RFC 4492. */
156 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
157 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
158 union
159 {
160 mbedtls_ecdh_context_mbed mbed_ecdh;
161 } ctx; /*!< Implementation-specific context. The
162 context in use is specified by the \c var
163 field. */
164#if defined(MBEDTLS_ECP_RESTARTABLE)
165 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
166 an alternative implementation not supporting
167 restartable mode must return
168 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
169 if this flag is set. */
170#endif /* MBEDTLS_ECP_RESTARTABLE */
171#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100174
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100175/**
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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100201 int (*f_rng)(void *, unsigned char *, size_t),
202 void *p_rng );
203
204/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000205 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100206 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000207 * This function performs the second of two core computations
208 * implemented during the ECDH key exchange. The first core
209 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100210 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000211 * \see ecp.h
212 *
213 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100214 * countermeasures against side-channel attacks.
215 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100216 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000217 * \param grp The ECP group to use. This must be initialized and have
218 * domain parameters loaded, for example through
219 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100220 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000221 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100222 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000223 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100224 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000225 * This must be initialized.
226 * \param f_rng The RNG function. This may be \c NULL if randomization
227 * of intermediate results during the ECP computations is
228 * not needed (discouraged). See the documentation of
229 * mbedtls_ecp_mul() for more.
230 * \param p_rng The RNG context to be passed to \p f_rng. This may be
231 * \c NULL if \p f_rng is \c NULL or doesn't need a
232 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100233 *
234 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200235 * \return Another \c MBEDTLS_ERR_ECP_XXX or
236 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
239 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200240 int (*f_rng)(void *, unsigned char *, size_t),
241 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100242
243/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000244 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100245 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000246 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100249
250/**
Janos Follathf61e4862018-10-30 11:53:25 +0000251 * \brief This function sets up the ECDH context with the information
252 * given.
253 *
254 * This function should be called after mbedtls_ecdh_init() but
255 * before mbedtls_ecdh_make_params(). There is no need to call
256 * this function before mbedtls_ecdh_read_params().
257 *
258 * This is the first function used by a TLS server for ECDHE
259 * ciphersuites.
260 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000261 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000262 * \param grp_id The group id of the group to set up the context for.
263 *
264 * \return \c 0 on success.
265 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000266int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
267 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100304 unsigned char *buf, size_t blen,
305 int (*f_rng)(void *, unsigned char *, size_t),
306 void *p_rng );
307
308/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000309 * \brief This function parses the ECDHE parameters in a
310 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100311 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000312 * \note In a TLS handshake, this is the how the client
313 * sets up its ECDHE context from the server's public
314 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100315 *
Rose Zadik68993282018-03-27 11:12:25 +0100316 * \see ecp.h
317 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000318 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000319 * \param buf On input, \c *buf must be the start of the input buffer.
320 * On output, \c *buf is updated to point to the end of the
321 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000322 * past the end of the ServerKeyExchange parameters.
323 * On error, this is the point at which an error has been
324 * detected, which is usually not useful except to debug
325 * failures.
326 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000327 *
Rose Zadik68993282018-03-27 11:12:25 +0100328 * \return \c 0 on success.
329 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000330 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000333 const unsigned char **buf,
334 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100335
336/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000337 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100338 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000339 * It is used by clients and servers in place of the
340 * ServerKeyEchange for static ECDH, and imports ECDH
341 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100342 *
Rose Zadik68993282018-03-27 11:12:25 +0100343 * \see ecp.h
344 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000345 * \param ctx The ECDH context to set up. This must be initialized.
346 * \param key The EC key to use. This must be initialized.
347 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000348 * - #MBEDTLS_ECDH_OURS: The key is ours.
349 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000350 *
Rose Zadik68993282018-03-27 11:12:25 +0100351 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200352 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000353 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100354 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000355int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
356 const mbedtls_ecp_keypair *key,
357 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100358
359/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000360 * \brief This function generates a public key and exports it
361 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100362 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000363 * This is the second function used by a TLS client for ECDH(E)
364 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100365 *
Rose Zadik68993282018-03-27 11:12:25 +0100366 * \see ecp.h
367 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000368 * \param ctx The ECDH context to use. This must be initialized
369 * and bound to a group, the latter usually by
370 * mbedtls_ecdh_read_params().
371 * \param olen The address at which to store the number of Bytes written.
372 * This must not be \c NULL.
373 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000374 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000375 * \param blen The size of the destination buffer \p buf in Bytes.
376 * \param f_rng The RNG function to use. This must not be \c NULL.
377 * \param p_rng The RNG context to be passed to \p f_rng. This may be
378 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000379 *
Rose Zadik68993282018-03-27 11:12:25 +0100380 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200381 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200382 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200383 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100386 unsigned char *buf, size_t blen,
387 int (*f_rng)(void *, unsigned char *, size_t),
388 void *p_rng );
389
390/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000391 * \brief This function parses and processes the ECDHE payload of a
392 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100393 *
Janos Follathf61e4862018-10-30 11:53:25 +0000394 * This is the third function used by a TLS server for ECDH(E)
395 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
396 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100397 *
Rose Zadik68993282018-03-27 11:12:25 +0100398 * \see ecp.h
399 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000400 * \param ctx The ECDH context to use. This must be initialized
401 * and bound to a group, for example via mbedtls_ecdh_setup().
402 * \param buf The pointer to the ClientKeyExchange payload. This must
403 * be a readable buffer of length \p blen Bytes.
404 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000405 *
Rose Zadik68993282018-03-27 11:12:25 +0100406 * \return \c 0 on success.
407 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000410 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100411
412/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000413 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100414 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000415 * This is the last function used by both TLS client
416 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100417 *
Rose Zadik68993282018-03-27 11:12:25 +0100418 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100419 * countermeasures against side-channel attacks.
420 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100421 *
422 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000423
424 * \param ctx The ECDH context to use. This must be initialized
425 * and have its own private key generated and the peer's
426 * public key imported.
427 * \param olen The address at which to store the total number of
428 * Bytes written on success. This must not be \c NULL.
429 * \param buf The buffer to write the generated shared key to. This
430 * must be a writable buffer of size \p blen Bytes.
431 * \param blen The length of the destination buffer \p buf in Bytes.
432 * \param f_rng The RNG function, for blinding purposes. This may
433 * b \c NULL if blinding isn't needed.
434 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
435 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000436 *
Rose Zadik68993282018-03-27 11:12:25 +0100437 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200438 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200439 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200440 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200443 unsigned char *buf, size_t blen,
444 int (*f_rng)(void *, unsigned char *, size_t),
445 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100446
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200447#if defined(MBEDTLS_ECP_RESTARTABLE)
448/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200449 * \brief This function enables restartable EC computations for this
450 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200451 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200452 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200453 *
454 * \note It is not possible to safely disable restartable
455 * computations once enabled, except by free-ing the context,
456 * which cancels possible in-progress operations.
457 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000458 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200459 */
460void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
461#endif /* MBEDTLS_ECP_RESTARTABLE */
462
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100463#ifdef __cplusplus
464}
465#endif
466
Paul Bakker9af723c2014-05-01 13:03:14 +0200467#endif /* ecdh.h */