blob: 27f2ffc6aa74b537727a90d710d0043f8e895dfa [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
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020037#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010038
Paul Bakker407a0da2013-06-27 14:29:21 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010043/**
Rose Zadik68993282018-03-27 11:12:25 +010044 * Defines the source of the imported EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010045 */
46typedef enum
47{
Rose Zadik7375b0f2018-04-16 16:04:57 +010048 MBEDTLS_ECDH_OURS, /**< Our key. */
Rose Zadik68993282018-03-27 11:12:25 +010049 MBEDTLS_ECDH_THEIRS, /**< The key of the peer. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010051
52/**
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020053 *
54 * \warning Performing multiple operations concurrently on the same
55 * ECDSA context is not supported; objects of this type
56 * should not be shared between multiple threads.
Rose Zadikde2d6222018-01-25 21:57:43 +000057 * \brief The ECDH context structure.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010058 */
Dawid Drozd428cc522018-07-24 10:02:47 +020059typedef struct mbedtls_ecdh_context
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010060{
Rose Zadikde2d6222018-01-25 21:57:43 +000061 mbedtls_ecp_group grp; /*!< The elliptic curve used. */
62 mbedtls_mpi d; /*!< The private key. */
63 mbedtls_ecp_point Q; /*!< The public key. */
64 mbedtls_ecp_point Qp; /*!< The value of the public key of the peer. */
65 mbedtls_mpi z; /*!< The shared secret. */
66 int point_format; /*!< The format of point export in TLS messages. */
67 mbedtls_ecp_point Vi; /*!< The blinding value. */
68 mbedtls_ecp_point Vf; /*!< The unblinding value. */
69 mbedtls_mpi _d; /*!< The previous \p d. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020070#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +020071 int restart_enabled; /*!< The flag for restartable mode. */
72 mbedtls_ecp_restart_ctx rs; /*!< The restart context for EC computations. */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020073#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010076
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010077/**
Rose Zadikde2d6222018-01-25 21:57:43 +000078 * \brief This function generates an ECDH keypair on an elliptic
79 * curve.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010080 *
Rose Zadikde2d6222018-01-25 21:57:43 +000081 * This function performs the first of two core computations
82 * implemented during the ECDH key exchange. The second core
83 * computation is performed by mbedtls_ecdh_compute_shared().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010084 *
Rose Zadik68993282018-03-27 11:12:25 +010085 * \see ecp.h
86 *
Rose Zadikde2d6222018-01-25 21:57:43 +000087 * \param grp The ECP group.
88 * \param d The destination MPI (private key).
89 * \param Q The destination point (public key).
90 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +010091 * \param p_rng The RNG context.
Rose Zadikde2d6222018-01-25 21:57:43 +000092 *
Rose Zadik68993282018-03-27 11:12:25 +010093 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +020094 * \return Another \c MBEDTLS_ERR_ECP_XXX or
95 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010098 int (*f_rng)(void *, unsigned char *, size_t),
99 void *p_rng );
100
101/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000102 * \brief This function computes the shared secret.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100103 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000104 * This function performs the second of two core computations
105 * implemented during the ECDH key exchange. The first core
106 * computation is performed by mbedtls_ecdh_gen_public().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100107 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000108 * \see ecp.h
109 *
110 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100111 * countermeasures against side-channel attacks.
112 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100113 *
114 * \param grp The ECP group.
115 * \param z The destination MPI (shared secret).
116 * \param Q The public key from another party.
117 * \param d Our secret exponent (private key).
118 * \param f_rng The RNG function.
119 * \param p_rng The RNG context.
120 *
121 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200122 * \return Another \c MBEDTLS_ERR_ECP_XXX or
123 * \c MBEDTLS_MPI_XXX error code on failure.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
126 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200127 int (*f_rng)(void *, unsigned char *, size_t),
128 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100129
130/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000131 * \brief This function initializes an ECDH context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100132 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000133 * \param ctx The ECDH context to initialize.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100136
137/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000138 * \brief This function frees a context.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100139 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000140 * \param ctx The context to free.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100143
144/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000145 * \brief This function generates a public key and a TLS
146 * ServerKeyExchange payload.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100147 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000148 * This is the first function used by a TLS server for ECDHE
149 * ciphersuites.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100150 *
Rose Zadik68993282018-03-27 11:12:25 +0100151 * \note This function assumes that the ECP group (grp) of the
152 * \p ctx context has already been properly set,
153 * for example, using mbedtls_ecp_group_load().
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100154 *
Rose Zadik68993282018-03-27 11:12:25 +0100155 * \see ecp.h
156 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000157 * \param ctx The ECDH context.
158 * \param olen The number of characters written.
159 * \param buf The destination buffer.
160 * \param blen The length of the destination buffer.
161 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +0100162 * \param p_rng The RNG context.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100163 *
Rose Zadik68993282018-03-27 11:12:25 +0100164 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200165 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200166 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200167 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100170 unsigned char *buf, size_t blen,
171 int (*f_rng)(void *, unsigned char *, size_t),
172 void *p_rng );
173
174/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000175 * \brief This function parses and processes a TLS ServerKeyExhange
176 * payload.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100177 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000178 * This is the first function used by a TLS client for ECDHE
179 * ciphersuites.
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100180 *
Rose Zadik68993282018-03-27 11:12:25 +0100181 * \see ecp.h
182 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000183 * \param ctx The ECDH context.
184 * \param buf The pointer to the start of the input buffer.
185 * \param end The address for one Byte past the end of the buffer.
186 *
Rose Zadik68993282018-03-27 11:12:25 +0100187 * \return \c 0 on success.
188 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000189 *
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100192 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100193
194/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000195 * \brief This function sets up an ECDH context from an EC key.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100196 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000197 * It is used by clients and servers in place of the
198 * ServerKeyEchange for static ECDH, and imports ECDH
199 * parameters from the EC key information of a certificate.
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100200 *
Rose Zadik68993282018-03-27 11:12:25 +0100201 * \see ecp.h
202 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000203 * \param ctx The ECDH context to set up.
204 * \param key The EC key to use.
Rose Zadik68993282018-03-27 11:12:25 +0100205 * \param side Defines the source of the key: 1: Our key, or
206 * 0: The key of the peer.
Rose Zadikde2d6222018-01-25 21:57:43 +0000207 *
Rose Zadik68993282018-03-27 11:12:25 +0100208 * \return \c 0 on success.
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200209 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Rose Zadikde2d6222018-01-25 21:57:43 +0000210 *
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
213 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100214
215/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000216 * \brief This function generates a public key and a TLS
217 * ClientKeyExchange payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100218 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000219 * This is the second function used by a TLS client for ECDH(E)
220 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100221 *
Rose Zadik68993282018-03-27 11:12:25 +0100222 * \see ecp.h
223 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000224 * \param ctx The ECDH context.
225 * \param olen The number of Bytes written.
226 * \param buf The destination buffer.
227 * \param blen The size of the destination buffer.
228 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +0100229 * \param p_rng The RNG context.
Rose Zadikde2d6222018-01-25 21:57:43 +0000230 *
Rose Zadik68993282018-03-27 11:12:25 +0100231 * \return \c 0 on success.
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200232 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200233 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnardf0bbd7e2018-10-15 13:22:41 +0200234 * \return Another \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100237 unsigned char *buf, size_t blen,
238 int (*f_rng)(void *, unsigned char *, size_t),
239 void *p_rng );
240
241/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000242 * \brief This function parses and processes a TLS ClientKeyExchange
243 * payload.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100244 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000245 * This is the second function used by a TLS server for ECDH(E)
246 * ciphersuites.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100247 *
Rose Zadik68993282018-03-27 11:12:25 +0100248 * \see ecp.h
249 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000250 * \param ctx The ECDH context.
251 * \param buf The start of the input buffer.
252 * \param blen The length of the input buffer.
253 *
Rose Zadik68993282018-03-27 11:12:25 +0100254 * \return \c 0 on success.
255 * \return An \c MBEDTLS_ERR_ECP_XXX error code on failure.
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100258 const unsigned char *buf, size_t blen );
259
260/**
Rose Zadikde2d6222018-01-25 21:57:43 +0000261 * \brief This function derives and exports the shared secret.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100262 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000263 * This is the last function used by both TLS client
264 * and servers.
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100265 *
Rose Zadik68993282018-03-27 11:12:25 +0100266 * \note If \p f_rng is not NULL, it is used to implement
Rose Zadik7375b0f2018-04-16 16:04:57 +0100267 * countermeasures against side-channel attacks.
268 * For more information, see mbedtls_ecp_mul().
Rose Zadik68993282018-03-27 11:12:25 +0100269 *
270 * \see ecp.h
271 *
Rose Zadikde2d6222018-01-25 21:57:43 +0000272 * \param ctx The ECDH context.
273 * \param olen The number of Bytes written.
274 * \param buf The destination buffer.
275 * \param blen The length of the destination buffer.
276 * \param f_rng The RNG function.
Rose Zadik68993282018-03-27 11:12:25 +0100277 * \param p_rng The RNG context.
Rose Zadikde2d6222018-01-25 21:57:43 +0000278 *
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é-Gonnard424fda52013-02-11 22:05:42 +0100283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200285 unsigned char *buf, size_t blen,
286 int (*f_rng)(void *, unsigned char *, size_t),
287 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100288
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200289#if defined(MBEDTLS_ECP_RESTARTABLE)
290/**
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200291 * \brief This function enables restartable EC computations for this
292 * context. (Default: disabled.)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200293 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200294 * \see \c mbedtls_ecp_set_max_ops()
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200295 *
296 * \note It is not possible to safely disable restartable
297 * computations once enabled, except by free-ing the context,
298 * which cancels possible in-progress operations.
299 *
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +0200300 * \param ctx The ECDH context.
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200301 */
302void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
303#endif /* MBEDTLS_ECP_RESTARTABLE */
304
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100305#ifdef __cplusplus
306}
307#endif
308
Paul Bakker9af723c2014-05-01 13:03:14 +0200309#endif /* ecdh.h */