blob: 435ba00cbed8b99655acb3ceee15600fcc8c7dbd [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/**
2 * \file ecdh.h
3 *
4 * \brief Elliptic curve Diffie-Hellman
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_ECDH_H
24#define MBEDTLS_ECDH_H
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010025
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020026#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010027
Paul Bakker407a0da2013-06-27 14:29:21 +020028#ifdef __cplusplus
29extern "C" {
30#endif
31
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010032/**
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010033 * When importing from an EC key, select if it is our key or the peer's key
34 */
35typedef enum
36{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 MBEDTLS_ECDH_OURS,
38 MBEDTLS_ECDH_THEIRS,
39} mbedtls_ecdh_side;
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010040
41/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010042 * \brief ECDH context structure
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +020043 *
44 * \warning Performing multiple operations concurrently on the same
45 * ECDSA context is not supported; objects of this type
46 * should not be shared between multiple threads.
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010047 */
48typedef struct
49{
Manuel Pégourié-Gonnardfd838da2017-04-27 11:38:51 +020050 mbedtls_ecp_group grp; /*!< elliptic curve used */
51 mbedtls_mpi d; /*!< our secret value (private key) */
52 mbedtls_ecp_point Q; /*!< our public value (public key) */
53 mbedtls_ecp_point Qp; /*!< peer's public value (public key) */
54 mbedtls_mpi z; /*!< shared secret */
55 int point_format; /*!< format for point export in TLS */
56 mbedtls_ecp_point Vi; /*!< blinding value (for later) */
57 mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */
58 mbedtls_mpi _d; /*!< previous d (for later) */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020059#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +020060 int restart_enabled; /*!< enable restartalbe EC computations? */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020061 mbedtls_ecp_restart_ctx rs; /*!< restart context for EC computations */
62#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010065
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010066/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020067 * \brief Generate a public key.
68 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010069 *
70 * \param grp ECP group
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020071 * \param d Destination MPI (secret exponent, aka private key)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010072 * \param Q Destination point (public key)
73 * \param f_rng RNG function
74 * \param p_rng RNG parameter
75 *
76 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +020078 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
79 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010082 int (*f_rng)(void *, unsigned char *, size_t),
83 void *p_rng );
84
85/**
86 * \brief Compute shared secret
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020087 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010088 *
89 * \param grp ECP group
90 * \param z Destination MPI (shared secret)
91 * \param Q Public key from other party
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020092 * \param d Our secret exponent (private key)
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020093 * \param f_rng RNG function (see notes)
94 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010095 *
96 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +020098 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
99 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200100 *
101 * \note If f_rng is not NULL, it is used to implement
102 * countermeasures against potential elaborate timing
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 * attacks, see \c mbedtls_ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
106 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200107 int (*f_rng)(void *, unsigned char *, size_t),
108 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100109
110/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100111 * \brief Initialize context
112 *
113 * \param ctx Context to initialize
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100116
117/**
118 * \brief Free context
119 *
120 * \param ctx Context to free
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100123
124/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200125 * \brief Generate a public key and a TLS ServerKeyExchange payload.
126 * (First function used by a TLS server for ECDHE.)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100127 *
128 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100129 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200130 * \param buf destination buffer
131 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100132 * \param f_rng RNG function
133 * \param p_rng RNG parameter
134 *
135 * \note This function assumes that ctx->grp has already been
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200136 * properly set (for example using mbedtls_ecp_group_load).
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100137 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200139 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
140 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100143 unsigned char *buf, size_t blen,
144 int (*f_rng)(void *, unsigned char *, size_t),
145 void *p_rng );
146
147/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200148 * \brief Parse and procress a TLS ServerKeyExhange payload.
149 * (First function used by a TLS client for ECDHE.)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100150 *
151 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200152 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100153 * \param end one past end of buffer
154 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100158 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100159
160/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200161 * \brief Setup an ECDH context from an EC key.
162 * (Used by clients and servers in place of the
163 * ServerKeyEchange for static ECDH: import ECDH parameters
164 * from a certificate's EC key information.)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100165 *
Manuel Pégourié-Gonnardeaf55be2017-08-23 14:40:21 +0200166 * \param ctx ECDH context to set
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100167 * \param key EC key to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100168 * \param side Is it our key (1) or the peer's key (0) ?
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100169 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
173 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100174
175/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200176 * \brief Generate a public key and a TLS ClientKeyExchange payload.
177 * (Second function used by a TLS client for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100178 *
179 * \param ctx ECDH context
180 * \param olen number of bytes actually written
181 * \param buf destination buffer
182 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200183 * \param f_rng RNG function
184 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100185 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200187 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
188 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100191 unsigned char *buf, size_t blen,
192 int (*f_rng)(void *, unsigned char *, size_t),
193 void *p_rng );
194
195/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200196 * \brief Parse and process a TLS ClientKeyExchange payload.
197 * (Second function used by a TLS server for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100198 *
199 * \param ctx ECDH context
200 * \param buf start of input buffer
201 * \param blen length of input buffer
202 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100206 const unsigned char *buf, size_t blen );
207
208/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200209 * \brief Derive and export the shared secret.
210 * (Last function used by both TLS client en servers.)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100211 *
212 * \param ctx ECDH context
213 * \param olen number of bytes written
214 * \param buf destination buffer
215 * \param blen buffer length
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared()
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200217 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100218 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200220 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
221 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200224 unsigned char *buf, size_t blen,
225 int (*f_rng)(void *, unsigned char *, size_t),
226 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100227
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200228#if defined(MBEDTLS_ECP_RESTARTABLE)
229/**
230 * \brief Enable restartable EC computations for this context.
231 * (Default: disabled.)
232 *
233 * \sa \c mbedtls_ecp_set_max_ops()
234 *
235 * \note It is not possible to safely disable restartable
236 * computations once enabled, except by free-ing the context,
237 * which cancels possible in-progress operations.
238 *
239 * \param ctx ECDH context
240 */
241void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
242#endif /* MBEDTLS_ECP_RESTARTABLE */
243
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100244#ifdef __cplusplus
245}
246#endif
247
Paul Bakker9af723c2014-05-01 13:03:14 +0200248#endif /* ecdh.h */