blob: 54be9f5a6a030cf7cea3482acfc71184fa181c76 [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 Amero203fdf72019-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 Amero203fdf72019-07-04 20:01:14 +010043#include "mbedtls/ecp.h"
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +010044#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010045
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010050/**
Rose Zadik68993282018-03-27 11:12:25 +010051 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010052 */
53typedef enum
54{
Rose Zadik7375b0f2018-04-16 16:04:57 +010055 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010056 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010058
Janos Follathc9c32f32018-08-13 15:52:45 +010059#if !defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
60/**
61 * Defines the ECDH implementation used.
62 *
63 * Later versions of the library may add new variants, therefore users should
64 * not make any assumptions about them.
65 */
66typedef enum
67{
68 MBEDTLS_ECDH_VARIANT_NONE = 0, /*!< Implementation not defined. */
69 MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0,/*!< The default Mbed TLS implementation */
70} mbedtls_ecdh_variant;
71
72/**
73 * The context used by the default ECDH implementation.
74 *
75 * Later versions might change the structure of this context, therefore users
76 * should not make any assumptions about the structure of
77 * mbedtls_ecdh_context_mbed.
78 */
79typedef struct mbedtls_ecdh_context_mbed
80{
81 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
82 mbedtls_mpi d; /*!< The private key. */
83 mbedtls_ecp_point Q; /*!< The public key. */
84 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
85 mbedtls_mpi z; /*!< The shared secret. */
86#if defined(MBEDTLS_ECP_RESTARTABLE)
87 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
88#endif
89} mbedtls_ecdh_context_mbed;
90#endif
91
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010092/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020093 *
94 * \warning Performing multiple operations concurrently on the same
95 * ECDSA context is not supported; objects of this type
96 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +000097 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010098 */
Dawid Drozd428cc522018-07-24 10:02:47 +020099typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100100{
Janos Follathc9c32f32018-08-13 15:52:45 +0100101#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Rose Zadikde2d6222018-01-25 21:57:43 +0000102 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
103 mbedtls_mpi d; /*!< The private key. */
104 mbedtls_ecp_point Q; /*!< The public key. */
105 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
106 mbedtls_mpi z; /*!< The shared secret. */
107 int point_format; /*!< The format of point export in TLS messages. */
108 mbedtls_ecp_point Vi; /*!< The blinding value. */
109 mbedtls_ecp_point Vf; /*!< The unblinding value. */
110 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +0200111#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200112 int restart_enabled; /*!< The flag for restartable mode. */
113 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Janos Follathc9c32f32018-08-13 15:52:45 +0100114#endif /* MBEDTLS_ECP_RESTARTABLE */
115#else
116 uint8_t point_format; /*!< The format of point export in TLS messages
117 as defined in RFC 4492. */
118 mbedtls_ecp_group_id grp_id;/*!< The elliptic curve used. */
119 mbedtls_ecdh_variant var; /*!< The ECDH implementation/structure used. */
120 union
121 {
122 mbedtls_ecdh_context_mbed mbed_ecdh;
123 } ctx; /*!< Implementation-specific context. The
124 context in use is specified by the \c var
125 field. */
126#if defined(MBEDTLS_ECP_RESTARTABLE)
127 uint8_t restart_enabled; /*!< The flag for restartable mode. Functions of
128 an alternative implementation not supporting
129 restartable mode must return
130 MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED error
131 if this flag is set. */
132#endif /* MBEDTLS_ECP_RESTARTABLE */
133#endif /* MBEDTLS_ECDH_LEGACY_CONTEXT */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100137/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000138 * \brief This function generates an ECDH keypair on an elliptic
139 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100140 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000141 * This function performs the first of two core computations
142 * implemented during the ECDH key exchange. The second core
143 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100144 *
Rose Zadik68993282018-03-27 11:12:25 +0100145 * \see ecp.h
146 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000147 * \param grp The ECP group to use. This must be initialized and have
148 * domain parameters loaded, for example through
149 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadikde2d6222018-01-25 21:57:43 +0000150 * \param d The destination MPI (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000151 * This must be initialized.
Rose Zadikde2d6222018-01-25 21:57:43 +0000152 * \param Q The destination point (public key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000153 * This must be initialized.
154 * \param f_rng The RNG function to use. This must not be \c NULL.
155 * \param p_rng The RNG context to be passed to \p f_rng. This may be
156 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000157 *
Rose Zadik68993282018-03-27 11:12:25 +0100158 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200159 * \return Another \c MBEDTLS_ERR_ECP_XXX or
160 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100161 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100162MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100164 int (*f_rng)(void *, unsigned char *, size_t),
165 void *p_rng );
166
167/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000168 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100169 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000170 * This function performs the second of two core computations
171 * implemented during the ECDH key exchange. The first core
172 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100173 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000174 * \see ecp.h
175 *
176 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100177 * countermeasures against side-channel attacks.
178 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100179 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000180 * \param grp The ECP group to use. This must be initialized and have
181 * domain parameters loaded, for example through
182 * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group().
Rose Zadik68993282018-03-27 11:12:25 +0100183 * \param z The destination MPI (shared secret).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000184 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100185 * \param Q The public key from another party.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000186 * This must be initialized.
Rose Zadik68993282018-03-27 11:12:25 +0100187 * \param d Our secret exponent (private key).
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000188 * This must be initialized.
189 * \param f_rng The RNG function. This may be \c NULL if randomization
190 * of intermediate results during the ECP computations is
191 * not needed (discouraged). See the documentation of
192 * mbedtls_ecp_mul() for more.
193 * \param p_rng The RNG context to be passed to \p f_rng. This may be
194 * \c NULL if \p f_rng is \c NULL or doesn't need a
195 * context argument.
Rose Zadik68993282018-03-27 11:12:25 +0100196 *
197 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200198 * \return Another \c MBEDTLS_ERR_ECP_XXX or
199 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100200 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100201MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
203 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200204 int (*f_rng)(void *, unsigned char *, size_t),
205 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100206
207/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000208 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100209 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000210 * \param ctx The ECDH context to initialize. This must not be \c NULL.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100211 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100212MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100214
215/**
Janos Follathf61e4862018-10-30 11:53:25 +0000216 * \brief This function sets up the ECDH context with the information
217 * given.
218 *
219 * This function should be called after mbedtls_ecdh_init() but
220 * before mbedtls_ecdh_make_params(). There is no need to call
221 * this function before mbedtls_ecdh_read_params().
222 *
223 * This is the first function used by a TLS server for ECDHE
224 * ciphersuites.
225 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000226 * \param ctx The ECDH context to set up. This must be initialized.
Janos Follathf61e4862018-10-30 11:53:25 +0000227 * \param grp_id The group id of the group to set up the context for.
228 *
229 * \return \c 0 on success.
230 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100231MBEDTLS_DEPRECATED
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000232int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx,
233 mbedtls_ecp_group_id grp_id );
Janos Follathf61e4862018-10-30 11:53:25 +0000234
235/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000236 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100237 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000238 * \param ctx The context to free. This may be \c NULL, in which
239 * case this function does nothing. If it is not \c NULL,
240 * it must point to an initialized ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100241 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100242MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100244
245/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000246 * \brief This function generates an EC key pair and exports its
247 * in the format used in a TLS ServerKeyExchange handshake
248 * message.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100249 *
Janos Follathf61e4862018-10-30 11:53:25 +0000250 * This is the second function used by a TLS server for ECDHE
251 * ciphersuites. (It is called after mbedtls_ecdh_setup().)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100252 *
Rose Zadik68993282018-03-27 11:12:25 +0100253 * \see ecp.h
254 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000255 * \param ctx The ECDH context to use. This must be initialized
256 * and bound to a group, for example via mbedtls_ecdh_setup().
257 * \param olen The address at which to store the number of Bytes written.
258 * \param buf The destination buffer. This must be a writable buffer of
259 * length \p blen Bytes.
260 * \param blen The length of the destination buffer \p buf in Bytes.
261 * \param f_rng The RNG function to use. This must not be \c NULL.
262 * \param p_rng The RNG context to be passed to \p f_rng. This may be
263 * \c NULL in case \p f_rng doesn't need a context argument.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100264 *
Rose Zadik68993282018-03-27 11:12:25 +0100265 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200266 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200267 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200268 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100269 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100270MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100272 unsigned char *buf, size_t blen,
273 int (*f_rng)(void *, unsigned char *, size_t),
274 void *p_rng );
275
276/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000277 * \brief This function parses the ECDHE parameters in a
278 * TLS ServerKeyExchange handshake message.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100279 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000280 * \note In a TLS handshake, this is the how the client
281 * sets up its ECDHE context from the server's public
282 * ECDHE key material.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100283 *
Rose Zadik68993282018-03-27 11:12:25 +0100284 * \see ecp.h
285 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000286 * \param ctx The ECDHE context to use. This must be initialized.
Hanno Becker60b65042018-12-17 22:59:13 +0000287 * \param buf On input, \c *buf must be the start of the input buffer.
288 * On output, \c *buf is updated to point to the end of the
289 * data that has been read. On success, this is the first byte
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000290 * past the end of the ServerKeyExchange parameters.
291 * On error, this is the point at which an error has been
292 * detected, which is usually not useful except to debug
293 * failures.
294 * \param end The end of the input buffer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000295 *
Rose Zadik68993282018-03-27 11:12:25 +0100296 * \return \c 0 on success.
297 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000298 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100299 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100300MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000302 const unsigned char **buf,
303 const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100304
305/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000306 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100307 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000308 * It is used by clients and servers in place of the
309 * ServerKeyEchange for static ECDH, and imports ECDH
310 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100311 *
Rose Zadik68993282018-03-27 11:12:25 +0100312 * \see ecp.h
313 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000314 * \param ctx The ECDH context to set up. This must be initialized.
315 * \param key The EC key to use. This must be initialized.
316 * \param side Defines the source of the key. Possible values are:
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000317 * - #MBEDTLS_ECDH_OURS: The key is ours.
318 * - #MBEDTLS_ECDH_THEIRS: The key is that of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000319 *
Rose Zadik68993282018-03-27 11:12:25 +0100320 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200321 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000322 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100323 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100324MBEDTLS_DEPRECATED
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000325int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx,
326 const mbedtls_ecp_keypair *key,
327 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100328
329/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000330 * \brief This function generates a public key and exports it
331 * as a TLS ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100332 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000333 * This is the second function used by a TLS client for ECDH(E)
334 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100335 *
Rose Zadik68993282018-03-27 11:12:25 +0100336 * \see ecp.h
337 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000338 * \param ctx The ECDH context to use. This must be initialized
339 * and bound to a group, the latter usually by
340 * mbedtls_ecdh_read_params().
341 * \param olen The address at which to store the number of Bytes written.
342 * This must not be \c NULL.
343 * \param buf The destination buffer. This must be a writable buffer
Hanno Beckerc81cfec2018-12-18 23:32:42 +0000344 * of length \p blen Bytes.
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000345 * \param blen The size of the destination buffer \p buf in Bytes.
346 * \param f_rng The RNG function to use. This must not be \c NULL.
347 * \param p_rng The RNG context to be passed to \p f_rng. This may be
348 * \c NULL in case \p f_rng doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000349 *
Rose Zadik68993282018-03-27 11:12:25 +0100350 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200351 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200352 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200353 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100354 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100355MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100357 unsigned char *buf, size_t blen,
358 int (*f_rng)(void *, unsigned char *, size_t),
359 void *p_rng );
360
361/**
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000362 * \brief This function parses and processes the ECDHE payload of a
363 * TLS ClientKeyExchange message.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100364 *
Janos Follathf61e4862018-10-30 11:53:25 +0000365 * This is the third function used by a TLS server for ECDH(E)
366 * ciphersuites. (It is called after mbedtls_ecdh_setup() and
367 * mbedtls_ecdh_make_params().)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100368 *
Rose Zadik68993282018-03-27 11:12:25 +0100369 * \see ecp.h
370 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000371 * \param ctx The ECDH context to use. This must be initialized
372 * and bound to a group, for example via mbedtls_ecdh_setup().
373 * \param buf The pointer to the ClientKeyExchange payload. This must
374 * be a readable buffer of length \p blen Bytes.
375 * \param blen The length of the input buffer \p buf in Bytes.
Rose Zadikde2d6222018-01-25 21:57:43 +0000376 *
Rose Zadik68993282018-03-27 11:12:25 +0100377 * \return \c 0 on success.
378 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100379 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100380MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000382 const unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100383
384/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000385 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100386 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000387 * This is the last function used by both TLS client
388 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100389 *
Rose Zadik68993282018-03-27 11:12:25 +0100390 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100391 * countermeasures against side-channel attacks.
392 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100393 *
394 * \see ecp.h
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000395
396 * \param ctx The ECDH context to use. This must be initialized
397 * and have its own private key generated and the peer's
398 * public key imported.
399 * \param olen The address at which to store the total number of
400 * Bytes written on success. This must not be \c NULL.
401 * \param buf The buffer to write the generated shared key to. This
402 * must be a writable buffer of size \p blen Bytes.
403 * \param blen The length of the destination buffer \p buf in Bytes.
404 * \param f_rng The RNG function, for blinding purposes. This may
405 * b \c NULL if blinding isn't needed.
406 * \param p_rng The RNG context. This may be \c NULL if \p f_rng
407 * doesn't need a context argument.
Rose Zadikde2d6222018-01-25 21:57:43 +0000408 *
Rose Zadik68993282018-03-27 11:12:25 +0100409 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200410 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200411 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200412 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100413 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100414MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200416 unsigned char *buf, size_t blen,
417 int (*f_rng)(void *, unsigned char *, size_t),
418 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100419
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200420#if defined(MBEDTLS_ECP_RESTARTABLE)
421/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200422 * \brief This function enables restartable EC computations for this
423 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200424 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200425 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200426 *
427 * \note It is not possible to safely disable restartable
428 * computations once enabled, except by free-ing the context,
429 * which cancels possible in-progress operations.
430 *
Hanno Beckere77ef2a2018-12-17 18:10:43 +0000431 * \param ctx The ECDH context to use. This must be initialized.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200432 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100433MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200434void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
435#endif /* MBEDTLS_ECP_RESTARTABLE */
436
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100437#ifdef __cplusplus
438}
439#endif
440
Paul Bakker9af723c2014-05-01 13:03:14 +0200441#endif /* ecdh.h */