blob: 3948d7c98f8650bc4f90b91991d76a3daebecd46 [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
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 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010032 */
Rose Zadikde2d6222018-01-25 21:57:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#ifndef MBEDTLS_ECDH_H
35#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010036
Ron Eldor9cbd1b22018-12-16 12:14:37 +020037#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/config.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020039#else
40#include MBEDTLS_CONFIG_FILE
41#endif
42
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010043#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010044
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010045#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
Christoph M. Wintersteigerc3cbdde2018-12-14 11:03:02 +000046#undef MBEDTLS_ECDH_LEGACY_CONTEXT
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +010047#include "everest/everest.h"
48#endif
49
Paul Bakker407a0da2013-06-27 14:29:21 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010054/**
Rose Zadik68993282018-03-27 11:12:25 +010055 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010056 */
57typedef enum
58{
Rose Zadik7375b0f2018-04-16 16:04:57 +010059 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010060 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010062
Janos Follathc9c32f32018-08-13 15:52:45 +010063#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
64/**
65 * Defines the ECDH implementation used.
66 *
67 * Later versions of the library may add new variants, therefore users should
68 * not make any assumptions about them.
69 */
70typedef enum
71{
72 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 */
86typedef struct mbedtls_ecdh_context_mbed
87{
88 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
89 mbedtls_mpi d; /*!< The private key. */
90 mbedtls_ecp_point Q; /*!< The public key. */
91 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
92 mbedtls_mpi z; /*!< The shared secret. */
93#if defined(MBEDTLS_ECP_RESTARTABLE)
94 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
95#endif
96} mbedtls_ecdh_context_mbed;
97#endif
98
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010099/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200100 *
101 * \warning Performing multiple operations concurrently on the same
102 * ECDSA context is not supported; objects of this type
103 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +0000104 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100105 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200106typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100107{
Janos Follathc9c32f32018-08-13 15:52:45 +0100108#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Rose Zadikde2d6222018-01-25 21:57:43 +0000109 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
110 mbedtls_mpi d; /*!< The private key. */
111 mbedtls_ecp_point Q; /*!< The public key. */
112 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
113 mbedtls_mpi z; /*!< The shared secret. */
114 int point_format; /*!< The format of point export in TLS messages. */
115 mbedtls_ecp_point Vi; /*!< The blinding value. */
116 mbedtls_ecp_point Vf; /*!< The unblinding value. */
117 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200118#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200119 int restart_enabled; /*!< The flag for restartable mode. */
120 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100121#endif /* MBEDTLS_ECP_RESTARTABLE */
122#else
123 uint8_t point_format; /*!< The format of point export in TLS messages
124 as defined in RFC 4492. */
125 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
126 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
127 union
128 {
129 mbedtls_ecdh_context_mbed mbed_ecdh;
Christoph M. Wintersteigerde4fcf22018-10-25 12:41:04 +0100130#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
131 mbedtls_ecdh_context_everest everest_ecdh;
132#endif
Janos Follathc9c32f32018-08-13 15:52:45 +0100133 } ctx; /*!< Implementation-specific context. The
134 context in use is specified by the \c var
135 field. */
136#if defined(MBEDTLS_ECP_RESTARTABLE)
137 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
138 an alternative implementation not supporting
139 restartable mode must return
140 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
141 if this flag is set. */
142#endif /* MBEDTLS_ECP_RESTARTABLE */
143#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100146
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100147/**
Gilles Peskine20b3ef32019-02-11 18:41:27 +0100148 * \brief Check whether a given group can be used for ECDH.
149 *
150 * \param gid The ECP group ID to check.
151 *
152 * \return \c 1 if the group can be used, \c 0 otherwise
153 */
154int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid );
155
156/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000157 * \brief This function generates an ECDH keypair on an elliptic
158 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100159 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000160 * This function performs the first of two core computations
161 * implemented during the ECDH key exchange. The second core
162 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100163 *
Rose Zadik68993282018-03-27 11:12:25 +0100164 * \see ecp.h
165 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000166 * \param grp The ECP group to use. This must be initialized and have
167 * domain parameters loaded, for example through
168 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000169 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000170 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000171 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000172 * This must be initialized.
173 * \param f_rng The RNG function to use. This must not be \c NULL.
174 * \param p_rng The RNG context to be passed to \p f_rng. This may be
175 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000176 *
Rose Zadik68993282018-03-27 11:12:25 +0100177 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200178 * \return Another \c MBEDTLS_ERR_ECP_XXX or
179 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100182 int (*f_rng)(void *, unsigned char *, size_t),
183 void *p_rng );
184
185/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000186 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100187 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000188 * This function performs the second of two core computations
189 * implemented during the ECDH key exchange. The first core
190 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100191 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000192 * \see ecp.h
193 *
194 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100195 * countermeasures against side-channel attacks.
196 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100197 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000198 * \param grp The ECP group to use. This must be initialized and have
199 * domain parameters loaded, for example through
200 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100201 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000202 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100203 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000204 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100205 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000206 * This must be initialized.
207 * \param f_rng The RNG function. This may be \c NULL if randomization
208 * of intermediate results during the ECP computations is
209 * not needed (discouraged). See the documentation of
210 * mbedtls_ecp_mul() for more.
211 * \param p_rng The RNG context to be passed to \p f_rng. This may be
212 * \c NULL if \p f_rng is \c NULL or doesn't need a
213 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100214 *
215 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200216 * \return Another \c MBEDTLS_ERR_ECP_XXX or
217 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
220 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200221 int (*f_rng)(void *, unsigned char *, size_t),
222 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100223
224/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000225 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100226 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000227 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100230
231/**
Janos Follathf61e4862018-10-30 11:53:25 +0000232 * \brief This function sets up the ECDH context with the information
233 * given.
234 *
235 * This function should be called after mbedtls_ecdh_init() but
236 * before mbedtls_ecdh_make_params(). There is no need to call
237 * this function before mbedtls_ecdh_read_params().
238 *
239 * This is the first function used by a TLS server for ECDHE
240 * ciphersuites.
241 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000242 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000243 * \param grp_id The group id of the group to set up the context for.
244 *
245 * \return \c 0 on success.
246 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000247int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
248 mbedtls_ecp_group_id grp_id );
Janos Follathf61e4862018-10-30 11:53:25 +0000249
250/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000251 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100252 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000253 * \param ctx The context to free. This may be \c NULL, in which
254 * case this function does nothing. If it is not \c NULL,
255 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100258
259/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000260 * \brief This function generates an EC key pair and exports its
261 * in the format used in a TLS ServerKeyExchange handshake
262 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100263 *
Janos Follathf61e4862018-10-30 11:53:25 +0000264 * This is the second function used by a TLS server for ECDHE
265 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100266 *
Rose Zadik68993282018-03-27 11:12:25 +0100267 * \see ecp.h
268 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000269 * \param ctx The ECDH context to use. This must be initialized
270 * and bound to a group, for example via mbedtls_ecdh_setup().
271 * \param olen The address at which to store the number of Bytes written.
272 * \param buf The destination buffer. This must be a writable buffer of
273 * length \p blen Bytes.
274 * \param blen The length of the destination buffer \p buf in Bytes.
275 * \param f_rng The RNG function to use. This must not be \c NULL.
276 * \param p_rng The RNG context to be passed to \p f_rng. This may be
277 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100278 *
Rose Zadik68993282018-03-27 11:12:25 +0100279 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200280 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200281 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200282 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100285 unsigned char *buf, size_t blen,
286 int (*f_rng)(void *, unsigned char *, size_t),
287 void *p_rng );
288
289/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000290 * \brief This function parses the ECDHE parameters in a
291 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100292 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000293 * \note In a TLS handshake, this is the how the client
294 * sets up its ECDHE context from the server's public
295 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100296 *
Rose Zadik68993282018-03-27 11:12:25 +0100297 * \see ecp.h
298 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000299 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000300 * \param buf On input, \c *buf must be the start of the input buffer.
301 * On output, \c *buf is updated to point to the end of the
302 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000303 * past the end of the ServerKeyExchange parameters.
304 * On error, this is the point at which an error has been
305 * detected, which is usually not useful except to debug
306 * failures.
307 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000308 *
Rose Zadik68993282018-03-27 11:12:25 +0100309 * \return \c 0 on success.
310 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000311 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000314 const unsigned char **buf,
315 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100316
317/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000318 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100319 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000320 * It is used by clients and servers in place of the
321 * ServerKeyEchange for static ECDH, and imports ECDH
322 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100323 *
Rose Zadik68993282018-03-27 11:12:25 +0100324 * \see ecp.h
325 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000326 * \param ctx The ECDH context to set up. This must be initialized.
327 * \param key The EC key to use. This must be initialized.
328 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000329 * - #MBEDTLS_ECDH_OURS: The key is ours.
330 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000331 *
Rose Zadik68993282018-03-27 11:12:25 +0100332 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200333 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000334 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100335 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000336int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
337 const mbedtls_ecp_keypair *key,
338 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100339
340/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000341 * \brief This function generates a public key and exports it
342 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100343 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000344 * This is the second function used by a TLS client for ECDH(E)
345 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100346 *
Rose Zadik68993282018-03-27 11:12:25 +0100347 * \see ecp.h
348 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000349 * \param ctx The ECDH context to use. This must be initialized
350 * and bound to a group, the latter usually by
351 * mbedtls_ecdh_read_params().
352 * \param olen The address at which to store the number of Bytes written.
353 * This must not be \c NULL.
354 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000355 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000356 * \param blen The size of the destination buffer \p buf in Bytes.
357 * \param f_rng The RNG function to use. This must not be \c NULL.
358 * \param p_rng The RNG context to be passed to \p f_rng. This may be
359 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000360 *
Rose Zadik68993282018-03-27 11:12:25 +0100361 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200362 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200363 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200364 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100367 unsigned char *buf, size_t blen,
368 int (*f_rng)(void *, unsigned char *, size_t),
369 void *p_rng );
370
371/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000372 * \brief This function parses and processes the ECDHE payload of a
373 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100374 *
Janos Follathf61e4862018-10-30 11:53:25 +0000375 * This is the third function used by a TLS server for ECDH(E)
376 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
377 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100378 *
Rose Zadik68993282018-03-27 11:12:25 +0100379 * \see ecp.h
380 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000381 * \param ctx The ECDH context to use. This must be initialized
382 * and bound to a group, for example via mbedtls_ecdh_setup().
383 * \param buf The pointer to the ClientKeyExchange payload. This must
384 * be a readable buffer of length \p blen Bytes.
385 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000386 *
Rose Zadik68993282018-03-27 11:12:25 +0100387 * \return \c 0 on success.
388 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000391 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100392
393/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000394 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100395 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000396 * This is the last function used by both TLS client
397 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100398 *
Rose Zadik68993282018-03-27 11:12:25 +0100399 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100400 * countermeasures against side-channel attacks.
401 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100402 *
403 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000404
405 * \param ctx The ECDH context to use. This must be initialized
406 * and have its own private key generated and the peer's
407 * public key imported.
408 * \param olen The address at which to store the total number of
409 * Bytes written on success. This must not be \c NULL.
410 * \param buf The buffer to write the generated shared key to. This
411 * must be a writable buffer of size \p blen Bytes.
412 * \param blen The length of the destination buffer \p buf in Bytes.
413 * \param f_rng The RNG function, for blinding purposes. This may
414 * b \c NULL if blinding isn't needed.
415 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
416 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000417 *
Rose Zadik68993282018-03-27 11:12:25 +0100418 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200419 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200420 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200421 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200424 unsigned char *buf, size_t blen,
425 int (*f_rng)(void *, unsigned char *, size_t),
426 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100427
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200428#if defined(MBEDTLS_ECP_RESTARTABLE)
429/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200430 * \brief This function enables restartable EC computations for this
431 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200432 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200433 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200434 *
435 * \note It is not possible to safely disable restartable
436 * computations once enabled, except by free-ing the context,
437 * which cancels possible in-progress operations.
438 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000439 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200440 */
441void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
442#endif /* MBEDTLS_ECP_RESTARTABLE */
443
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100444#ifdef __cplusplus
445}
446#endif
447
Paul Bakker9af723c2014-05-01 13:03:14 +0200448#endif /* ecdh.h */