blob: 384c3dc0769f486551e34ecadd3c59af0d3ac2e5 [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)
38#include "config.h"
39#else
40#include MBEDTLS_CONFIG_FILE
41#endif
42
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020043#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010044
Paul Bakker407a0da2013-06-27 14:29:21 +020045#ifdef __cplusplus
46extern "C" {
47#endif
48
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010049/**
Rose Zadik68993282018-03-27 11:12:25 +010050 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010051 */
52typedef enum
53{
Rose Zadik7375b0f2018-04-16 16:04:57 +010054 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010055 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010057
Janos Follathc9c32f32018-08-13 15:52:45 +010058#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
59/**
60 * Defines the ECDH implementation used.
61 *
62 * Later versions of the library may add new variants, therefore users should
63 * not make any assumptions about them.
64 */
65typedef enum
66{
67 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
68 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
69} mbedtls_ecdh_variant;
70
71/**
72 * The context used by the default ECDH implementation.
73 *
74 * Later versions might change the structure of this context, therefore users
75 * should not make any assumptions about the structure of
76 * mbedtls_ecdh_context_mbed.
77 */
78typedef struct mbedtls_ecdh_context_mbed
79{
80 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
81 mbedtls_mpi d; /*!< The private key. */
82 mbedtls_ecp_point Q; /*!< The public key. */
83 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
84 mbedtls_mpi z; /*!< The shared secret. */
85#if defined(MBEDTLS_ECP_RESTARTABLE)
86 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
87#endif
88} mbedtls_ecdh_context_mbed;
89#endif
90
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010091/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020092 *
93 * \warning Performing multiple operations concurrently on the same
94 * ECDSA context is not supported; objects of this type
95 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +000096 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010097 */
Dawid Drozd428cc522018-07-24 10:02:47 +020098typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010099{
Janos Follathc9c32f32018-08-13 15:52:45 +0100100#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Rose Zadikde2d6222018-01-25 21:57:43 +0000101 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
102 mbedtls_mpi d; /*!< The private key. */
103 mbedtls_ecp_point Q; /*!< The public key. */
104 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
105 mbedtls_mpi z; /*!< The shared secret. */
106 int point_format; /*!< The format of point export in TLS messages. */
107 mbedtls_ecp_point Vi; /*!< The blinding value. */
108 mbedtls_ecp_point Vf; /*!< The unblinding value. */
109 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200110#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200111 int restart_enabled; /*!< The flag for restartable mode. */
112 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100113#endif /* MBEDTLS_ECP_RESTARTABLE */
114#else
115 uint8_t point_format; /*!< The format of point export in TLS messages
116 as defined in RFC 4492. */
117 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
118 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
119 union
120 {
121 mbedtls_ecdh_context_mbed mbed_ecdh;
122 } ctx; /*!< Implementation-specific context. The
123 context in use is specified by the \c var
124 field. */
125#if defined(MBEDTLS_ECP_RESTARTABLE)
126 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
127 an alternative implementation not supporting
128 restartable mode must return
129 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
130 if this flag is set. */
131#endif /* MBEDTLS_ECP_RESTARTABLE */
132#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100135
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100136/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000137 * \brief This function generates an ECDH keypair on an elliptic
138 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100139 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000140 * This function performs the first of two core computations
141 * implemented during the ECDH key exchange. The second core
142 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100143 *
Rose Zadik68993282018-03-27 11:12:25 +0100144 * \see ecp.h
145 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000146 * \param grp The ECP group to use. This must be initialized and have
147 * domain parameters loaded, for example through
148 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000149 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000150 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000151 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000152 * This must be initialized.
153 * \param f_rng The RNG function to use. This must not be \c NULL.
154 * \param p_rng The RNG context to be passed to \p f_rng. This may be
155 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000156 *
Rose Zadik68993282018-03-27 11:12:25 +0100157 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200158 * \return Another \c MBEDTLS_ERR_ECP_XXX or
159 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100162 int (*f_rng)(void *, unsigned char *, size_t),
163 void *p_rng );
164
165/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000166 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100167 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000168 * This function performs the second of two core computations
169 * implemented during the ECDH key exchange. The first core
170 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100171 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000172 * \see ecp.h
173 *
174 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100175 * countermeasures against side-channel attacks.
176 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100177 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000178 * \param grp The ECP group to use. This must be initialized and have
179 * domain parameters loaded, for example through
180 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100181 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000182 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100183 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000184 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100185 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000186 * This must be initialized.
187 * \param f_rng The RNG function. This may be \c NULL if randomization
188 * of intermediate results during the ECP computations is
189 * not needed (discouraged). See the documentation of
190 * mbedtls_ecp_mul() for more.
191 * \param p_rng The RNG context to be passed to \p f_rng. This may be
192 * \c NULL if \p f_rng is \c NULL or doesn't need a
193 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100194 *
195 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200196 * \return Another \c MBEDTLS_ERR_ECP_XXX or
197 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
200 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200201 int (*f_rng)(void *, unsigned char *, size_t),
202 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100203
204/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000205 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100206 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000207 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100210
211/**
Janos Follathf61e4862018-10-30 11:53:25 +0000212 * \brief This function sets up the ECDH context with the information
213 * given.
214 *
215 * This function should be called after mbedtls_ecdh_init() but
216 * before mbedtls_ecdh_make_params(). There is no need to call
217 * this function before mbedtls_ecdh_read_params().
218 *
219 * This is the first function used by a TLS server for ECDHE
220 * ciphersuites.
221 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000222 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000223 * \param grp_id The group id of the group to set up the context for.
224 *
225 * \return \c 0 on success.
226 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000227int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
228 mbedtls_ecp_group_id grp_id );
Janos Follathf61e4862018-10-30 11:53:25 +0000229
230/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000231 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100232 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000233 * \param ctx The context to free. This may be \c NULL, in which
234 * case this function does nothing. If it is not \c NULL,
235 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100238
239/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000240 * \brief This function generates an EC key pair and exports its
241 * in the format used in a TLS ServerKeyExchange handshake
242 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100243 *
Janos Follathf61e4862018-10-30 11:53:25 +0000244 * This is the second function used by a TLS server for ECDHE
245 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100246 *
Rose Zadik68993282018-03-27 11:12:25 +0100247 * \see ecp.h
248 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000249 * \param ctx The ECDH context to use. This must be initialized
250 * and bound to a group, for example via mbedtls_ecdh_setup().
251 * \param olen The address at which to store the number of Bytes written.
252 * \param buf The destination buffer. This must be a writable buffer of
253 * length \p blen Bytes.
254 * \param blen The length of the destination buffer \p buf in Bytes.
255 * \param f_rng The RNG function to use. This must not be \c NULL.
256 * \param p_rng The RNG context to be passed to \p f_rng. This may be
257 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100258 *
Rose Zadik68993282018-03-27 11:12:25 +0100259 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200260 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200261 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200262 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100265 unsigned char *buf, size_t blen,
266 int (*f_rng)(void *, unsigned char *, size_t),
267 void *p_rng );
268
269/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000270 * \brief This function parses the ECDHE parameters in a
271 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100272 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000273 * \note In a TLS handshake, this is the how the client
274 * sets up its ECDHE context from the server's public
275 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100276 *
Rose Zadik68993282018-03-27 11:12:25 +0100277 * \see ecp.h
278 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000279 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000280 * \param buf On input, \c *buf must be the start of the input buffer.
281 * On output, \c *buf is updated to point to the end of the
282 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000283 * past the end of the ServerKeyExchange parameters.
284 * On error, this is the point at which an error has been
285 * detected, which is usually not useful except to debug
286 * failures.
287 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000288 *
Rose Zadik68993282018-03-27 11:12:25 +0100289 * \return \c 0 on success.
290 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000291 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000294 const unsigned char **buf,
295 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100296
297/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000298 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100299 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000300 * It is used by clients and servers in place of the
301 * ServerKeyEchange for static ECDH, and imports ECDH
302 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100303 *
Rose Zadik68993282018-03-27 11:12:25 +0100304 * \see ecp.h
305 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000306 * \param ctx The ECDH context to set up. This must be initialized.
307 * \param key The EC key to use. This must be initialized.
308 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000309 * - #MBEDTLS_ECDH_OURS: The key is ours.
310 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000311 *
Rose Zadik68993282018-03-27 11:12:25 +0100312 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200313 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000314 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100315 */
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000316int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
317 const mbedtls_ecp_keypair *key,
318 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100319
320/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000321 * \brief This function generates a public key and exports it
322 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100323 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000324 * This is the second function used by a TLS client for ECDH(E)
325 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100326 *
Rose Zadik68993282018-03-27 11:12:25 +0100327 * \see ecp.h
328 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000329 * \param ctx The ECDH context to use. This must be initialized
330 * and bound to a group, the latter usually by
331 * mbedtls_ecdh_read_params().
332 * \param olen The address at which to store the number of Bytes written.
333 * This must not be \c NULL.
334 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000335 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000336 * \param blen The size of the destination buffer \p buf in Bytes.
337 * \param f_rng The RNG function to use. This must not be \c NULL.
338 * \param p_rng The RNG context to be passed to \p f_rng. This may be
339 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000340 *
Rose Zadik68993282018-03-27 11:12:25 +0100341 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200342 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200343 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200344 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100347 unsigned char *buf, size_t blen,
348 int (*f_rng)(void *, unsigned char *, size_t),
349 void *p_rng );
350
351/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000352 * \brief This function parses and processes the ECDHE payload of a
353 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100354 *
Janos Follathf61e4862018-10-30 11:53:25 +0000355 * This is the third function used by a TLS server for ECDH(E)
356 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
357 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100358 *
Rose Zadik68993282018-03-27 11:12:25 +0100359 * \see ecp.h
360 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000361 * \param ctx The ECDH context to use. This must be initialized
362 * and bound to a group, for example via mbedtls_ecdh_setup().
363 * \param buf The pointer to the ClientKeyExchange payload. This must
364 * be a readable buffer of length \p blen Bytes.
365 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000366 *
Rose Zadik68993282018-03-27 11:12:25 +0100367 * \return \c 0 on success.
368 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000371 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100372
373/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000374 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100375 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000376 * This is the last function used by both TLS client
377 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100378 *
Rose Zadik68993282018-03-27 11:12:25 +0100379 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100380 * countermeasures against side-channel attacks.
381 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100382 *
383 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000384
385 * \param ctx The ECDH context to use. This must be initialized
386 * and have its own private key generated and the peer's
387 * public key imported.
388 * \param olen The address at which to store the total number of
389 * Bytes written on success. This must not be \c NULL.
390 * \param buf The buffer to write the generated shared key to. This
391 * must be a writable buffer of size \p blen Bytes.
392 * \param blen The length of the destination buffer \p buf in Bytes.
393 * \param f_rng The RNG function, for blinding purposes. This may
394 * b \c NULL if blinding isn't needed.
395 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
396 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000397 *
Rose Zadik68993282018-03-27 11:12:25 +0100398 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200399 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200400 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200401 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200404 unsigned char *buf, size_t blen,
405 int (*f_rng)(void *, unsigned char *, size_t),
406 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100407
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200408#if defined(MBEDTLS_ECP_RESTARTABLE)
409/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200410 * \brief This function enables restartable EC computations for this
411 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200412 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200413 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200414 *
415 * \note It is not possible to safely disable restartable
416 * computations once enabled, except by free-ing the context,
417 * which cancels possible in-progress operations.
418 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000419 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200420 */
421void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
422#endif /* MBEDTLS_ECP_RESTARTABLE */
423
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100424#ifdef __cplusplus
425}
426#endif
427
Paul Bakker9af723c2014-05-01 13:03:14 +0200428#endif /* ecdh.h */