blob: e937c899fffded97674e4021c2b7381256bf9d71 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/**
2 * \file ecdh.h
3 *
Rose Zadikde2d6222018-01-25 21:57:43 +00004 * \brief The Elliptic Curve Diffie-Hellman (ECDH) protocol APIs.
5 *
6 * ECDH is an anonymous key agreement protocol allowing two parties to
7 * establish a shared secret over an insecure channel. Each party must have an
8 * elliptic-curve public–private key pair.
9 *
10 * For more information, see <em>NIST SP 800-56A Rev. 2: Recommendation for
11 * Pair-Wise Key Establishment Schemes Using Discrete Logarithm
12 * Cryptography</em>.
Darryl Greena40a1012018-01-05 15:33:17 +000013 */
14/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +020015 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020016 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
17 *
18 * This file is provided under the Apache License 2.0, or the
19 * GNU General Public License v2.0 or later.
20 *
21 * **********
22 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020023 *
24 * Licensed under the Apache License, Version 2.0 (the "License"); you may
25 * not use this file except in compliance with the License.
26 * You may obtain a copy of the License at
27 *
28 * http://www.apache.org/licenses/LICENSE-2.0
29 *
30 * Unless required by applicable law or agreed to in writing, software
31 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
32 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010035 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020036 * **********
37 *
38 * **********
39 * GNU General Public License v2.0 or later:
40 *
41 * This program is free software; you can redistribute it and/or modify
42 * it under the terms of the GNU General Public License as published by
43 * the Free Software Foundation; either version 2 of the License, or
44 * (at your option) any later version.
45 *
46 * This program is distributed in the hope that it will be useful,
47 * but WITHOUT ANY WARRANTY; without even the implied warranty of
48 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
49 * GNU General Public License for more details.
50 *
51 * You should have received a copy of the GNU General Public License along
52 * with this program; if not, write to the Free Software Foundation, Inc.,
53 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
54 *
55 * **********
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010056 */
Rose Zadikde2d6222018-01-25 21:57:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#ifndef MBEDTLS_ECDH_H
59#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010060
Ron Eldor3625e562018-12-16 12:14:37 +020061#if !defined(MBEDTLS_CONFIG_FILE)
62#include "config.h"
63#else
64#include MBEDTLS_CONFIG_FILE
65#endif
66
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020067#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010068
Paul Bakker407a0da2013-06-27 14:29:21 +020069#ifdef __cplusplus
70extern "C" {
71#endif
72
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010073/**
Rose Zadikde2d6222018-01-25 21:57:43 +000074 * Defines the source of the imported EC key:
75 * <ul><li>Our key.</li>
76 * <li>The key of the peer.</li></ul>
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010077 */
78typedef enum
79{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 MBEDTLS_ECDH_OURS,
81 MBEDTLS_ECDH_THEIRS,
82} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010083
84/**
Rose Zadikde2d6222018-01-25 21:57:43 +000085 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010086 */
87typedef struct
88{
Rose Zadikde2d6222018-01-25 21:57:43 +000089 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
90 mbedtls_mpi d; /*!< The private key. */
91 mbedtls_ecp_point Q; /*!< The public key. */
92 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
93 mbedtls_mpi z; /*!< The shared secret. */
94 int point_format; /*!< The format of point export in TLS messages. */
95 mbedtls_ecp_point Vi; /*!< The blinding value. */
96 mbedtls_ecp_point Vf; /*!< The unblinding value. */
97 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100100
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100101/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000102 * \brief This function generates an ECDH keypair on an elliptic
103 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100104 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000105 * This function performs the first of two core computations
106 * implemented during the ECDH key exchange. The second core
107 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100108 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000109 * \param grp The ECP group.
110 * \param d The destination MPI (private key).
111 * \param Q The destination point (public key).
112 * \param f_rng The RNG function.
113 * \param p_rng The RNG parameter.
114 *
115 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX or
116 * \c MBEDTLS_MPI_XXX error code on failure.
117 *
118 * \see ecp.h
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100121 int (*f_rng)(void *, unsigned char *, size_t),
122 void *p_rng );
123
124/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000125 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100126 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000127 * This function performs the second of two core computations
128 * implemented during the ECDH key exchange. The first core
129 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100130 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000131 * \param grp The ECP group.
132 * \param z The destination MPI (shared secret).
133 * \param Q The public key from another party.
134 * \param d Our secret exponent (private key).
135 * \param f_rng The RNG function.
136 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200137 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000138 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX or
139 * \c MBEDTLS_MPI_XXX error code on failure.
140 *
141 * \see ecp.h
142 *
143 * \note If \p f_rng is not NULL, it is used to implement
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200144 * countermeasures against potential elaborate timing
Rose Zadikde2d6222018-01-25 21:57:43 +0000145 * attacks. For more information, see mbedtls_ecp_mul().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
148 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200149 int (*f_rng)(void *, unsigned char *, size_t),
150 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100151
152/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000153 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100154 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000155 * \param ctx The ECDH context to initialize.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100158
159/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000160 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100161 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000162 * \param ctx The context to free.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100165
166/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000167 * \brief This function generates a public key and a TLS
168 * ServerKeyExchange payload.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100169 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000170 * This is the first function used by a TLS server for ECDHE
171 * ciphersuites.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100172 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000173 * \param ctx The ECDH context.
174 * \param olen The number of characters written.
175 * \param buf The destination buffer.
176 * \param blen The length of the destination buffer.
177 * \param f_rng The RNG function.
178 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100179 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000180 * \note This function assumes that the ECP group (grp) of the
181 * \p ctx context has already been properly set,
182 * for example, using mbedtls_ecp_group_load().
183 *
184 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
185 * on failure.
186 *
187 * \see ecp.h
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100190 unsigned char *buf, size_t blen,
191 int (*f_rng)(void *, unsigned char *, size_t),
192 void *p_rng );
193
194/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000195 * \brief This function parses and processes a TLS ServerKeyExhange
196 * payload.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100197 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000198 * This is the first function used by a TLS client for ECDHE
199 * ciphersuites.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100200 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000201 * \param ctx The ECDH context.
202 * \param buf The pointer to the start of the input buffer.
203 * \param end The address for one Byte past the end of the buffer.
204 *
205 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
206 * on failure.
207 *
208 * \see ecp.h
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100211 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100212
213/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000214 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100215 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000216 * It is used by clients and servers in place of the
217 * ServerKeyEchange for static ECDH, and imports ECDH
218 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100219 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000220 * \param ctx The ECDH context to set up.
221 * \param key The EC key to use.
222 * \param side Defines the source of the key:
223 * <ul><li>1: Our key.</li>
224 <li>0: The key of the peer.</li></ul>
225 *
226 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
227 * on failure.
228 *
229 * \see ecp.h
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
232 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100233
234/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000235 * \brief This function generates a public key and a TLS
236 * ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100237 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000238 * This is the second function used by a TLS client for ECDH(E)
239 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100240 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000241 * \param ctx The ECDH context.
242 * \param olen The number of Bytes written.
243 * \param buf The destination buffer.
244 * \param blen The size of the destination buffer.
245 * \param f_rng The RNG function.
246 * \param p_rng The RNG parameter.
247 *
248 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
249 * on failure.
250 *
251 * \see ecp.h
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100254 unsigned char *buf, size_t blen,
255 int (*f_rng)(void *, unsigned char *, size_t),
256 void *p_rng );
257
258/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000259 * \brief This function parses and processes a TLS ClientKeyExchange
260 * payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100261 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000262 * This is the second function used by a TLS server for ECDH(E)
263 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100264 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000265 * \param ctx The ECDH context.
266 * \param buf The start of the input buffer.
267 * \param blen The length of the input buffer.
268 *
269 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
270 * on failure.
271 *
272 * \see ecp.h
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100275 const unsigned char *buf, size_t blen );
276
277/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000278 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100279 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000280 * This is the last function used by both TLS client
281 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100282 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000283 * \param ctx The ECDH context.
284 * \param olen The number of Bytes written.
285 * \param buf The destination buffer.
286 * \param blen The length of the destination buffer.
287 * \param f_rng The RNG function.
288 * \param p_rng The RNG parameter.
289 *
290 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
291 * on failure.
292 *
293 * \see ecp.h
294 *
295 * \note If \p f_rng is not NULL, it is used to implement
296 * countermeasures against potential elaborate timing
297 * attacks. For more information, see mbedtls_ecp_mul().
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200300 unsigned char *buf, size_t blen,
301 int (*f_rng)(void *, unsigned char *, size_t),
302 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100303
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100304#ifdef __cplusplus
305}
306#endif
307
Paul Bakker9af723c2014-05-01 13:03:14 +0200308#endif /* ecdh.h */