blob: d16bad2d8e6f56658cd4c5bb2af24b1e9ee9f8da [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/*
Rose Zadikde2d6222018-01-25 21:57:43 +000015 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 * SPDX-License-Identifier: Apache-2.0
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010029 *
Rose Zadikde2d6222018-01-25 21:57:43 +000030 * This file is part of Mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010031 */
Rose Zadikde2d6222018-01-25 21:57:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#ifndef MBEDTLS_ECDH_H
34#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010035
Ron Eldor3625e562018-12-16 12:14:37 +020036#if !defined(MBEDTLS_CONFIG_FILE)
37#include "config.h"
38#else
39#include MBEDTLS_CONFIG_FILE
40#endif
41
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020042#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010048/**
Rose Zadikde2d6222018-01-25 21:57:43 +000049 * Defines the source of the imported EC key:
50 * <ul><li>Our key.</li>
51 * <li>The key of the peer.</li></ul>
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010052 */
53typedef enum
54{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 MBEDTLS_ECDH_OURS,
56 MBEDTLS_ECDH_THEIRS,
57} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010058
59/**
Rose Zadikde2d6222018-01-25 21:57:43 +000060 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010061 */
62typedef struct
63{
Rose Zadikde2d6222018-01-25 21:57:43 +000064 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
65 mbedtls_mpi d; /*!< The private key. */
66 mbedtls_ecp_point Q; /*!< The public key. */
67 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
68 mbedtls_mpi z; /*!< The shared secret. */
69 int point_format; /*!< The format of point export in TLS messages. */
70 mbedtls_ecp_point Vi; /*!< The blinding value. */
71 mbedtls_ecp_point Vf; /*!< The unblinding value. */
72 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010075
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010076/**
Rose Zadikde2d6222018-01-25 21:57:43 +000077 * \brief This function generates an ECDH keypair on an elliptic
78 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010079 *
Rose Zadikde2d6222018-01-25 21:57:43 +000080 * This function performs the first of two core computations
81 * implemented during the ECDH key exchange. The second core
82 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010083 *
Rose Zadikde2d6222018-01-25 21:57:43 +000084 * \param grp The ECP group.
85 * \param d The destination MPI (private key).
86 * \param Q The destination point (public key).
87 * \param f_rng The RNG function.
88 * \param p_rng The RNG parameter.
89 *
90 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX or
91 * \c MBEDTLS_MPI_XXX error code on failure.
92 *
93 * \see ecp.h
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010096 int (*f_rng)(void *, unsigned char *, size_t),
97 void *p_rng );
98
99/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000100 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100101 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000102 * This function performs the second of two core computations
103 * implemented during the ECDH key exchange. The first core
104 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100105 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000106 * \param grp The ECP group.
107 * \param z The destination MPI (shared secret).
108 * \param Q The public key from another party.
109 * \param d Our secret exponent (private key).
110 * \param f_rng The RNG function.
111 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200112 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000113 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX or
114 * \c MBEDTLS_MPI_XXX error code on failure.
115 *
116 * \see ecp.h
117 *
118 * \note If \p f_rng is not NULL, it is used to implement
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200119 * countermeasures against potential elaborate timing
Rose Zadikde2d6222018-01-25 21:57:43 +0000120 * attacks. For more information, see mbedtls_ecp_mul().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
123 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200124 int (*f_rng)(void *, unsigned char *, size_t),
125 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100126
127/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000128 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100129 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000130 * \param ctx The ECDH context to initialize.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100133
134/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000135 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000137 * \param ctx The context to free.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100140
141/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000142 * \brief This function generates a public key and a TLS
143 * ServerKeyExchange payload.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100144 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000145 * This is the first function used by a TLS server for ECDHE
146 * ciphersuites.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100147 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000148 * \param ctx The ECDH context.
149 * \param olen The number of characters written.
150 * \param buf The destination buffer.
151 * \param blen The length of the destination buffer.
152 * \param f_rng The RNG function.
153 * \param p_rng The RNG parameter.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100154 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000155 * \note This function assumes that the ECP group (grp) of the
156 * \p ctx context has already been properly set,
157 * for example, using mbedtls_ecp_group_load().
158 *
159 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
160 * on failure.
161 *
162 * \see ecp.h
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100165 unsigned char *buf, size_t blen,
166 int (*f_rng)(void *, unsigned char *, size_t),
167 void *p_rng );
168
169/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000170 * \brief This function parses and processes a TLS ServerKeyExhange
171 * payload.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100172 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000173 * This is the first function used by a TLS client for ECDHE
174 * ciphersuites.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100175 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000176 * \param ctx The ECDH context.
177 * \param buf The pointer to the start of the input buffer.
178 * \param end The address for one Byte past the end of the buffer.
179 *
180 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
181 * on failure.
182 *
183 * \see ecp.h
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100186 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100187
188/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000189 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100190 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000191 * It is used by clients and servers in place of the
192 * ServerKeyEchange for static ECDH, and imports ECDH
193 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100194 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000195 * \param ctx The ECDH context to set up.
196 * \param key The EC key to use.
197 * \param side Defines the source of the key:
198 * <ul><li>1: Our key.</li>
199 <li>0: The key of the peer.</li></ul>
200 *
201 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
202 * on failure.
203 *
204 * \see ecp.h
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
207 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100208
209/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000210 * \brief This function generates a public key and a TLS
211 * ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100212 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000213 * This is the second function used by a TLS client for ECDH(E)
214 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100215 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000216 * \param ctx The ECDH context.
217 * \param olen The number of Bytes written.
218 * \param buf The destination buffer.
219 * \param blen The size of the destination buffer.
220 * \param f_rng The RNG function.
221 * \param p_rng The RNG parameter.
222 *
223 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
224 * on failure.
225 *
226 * \see ecp.h
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100229 unsigned char *buf, size_t blen,
230 int (*f_rng)(void *, unsigned char *, size_t),
231 void *p_rng );
232
233/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000234 * \brief This function parses and processes a TLS ClientKeyExchange
235 * payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100236 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000237 * This is the second function used by a TLS server for ECDH(E)
238 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100239 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000240 * \param ctx The ECDH context.
241 * \param buf The start of the input buffer.
242 * \param blen The length of the input buffer.
243 *
244 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
245 * on failure.
246 *
247 * \see ecp.h
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100250 const unsigned char *buf, size_t blen );
251
252/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000253 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100254 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000255 * This is the last function used by both TLS client
256 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100257 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000258 * \param ctx The ECDH context.
259 * \param olen The number of Bytes written.
260 * \param buf The destination buffer.
261 * \param blen The length of the destination buffer.
262 * \param f_rng The RNG function.
263 * \param p_rng The RNG parameter.
264 *
265 * \return \c 0 on success, or an \c MBEDTLS_ERR_ECP_XXX error code
266 * on failure.
267 *
268 * \see ecp.h
269 *
270 * \note If \p f_rng is not NULL, it is used to implement
271 * countermeasures against potential elaborate timing
272 * attacks. For more information, see mbedtls_ecp_mul().
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200275 unsigned char *buf, size_t blen,
276 int (*f_rng)(void *, unsigned char *, size_t),
277 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100278
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100279#ifdef __cplusplus
280}
281#endif
282
Paul Bakker9af723c2014-05-01 13:03:14 +0200283#endif /* ecdh.h */