blob: 6f3fe137c9964ed38a0ef04d134d480950b1ab7d [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
43 */
44typedef struct
45{
Manuel Pégourié-Gonnardfd838da2017-04-27 11:38:51 +020046 mbedtls_ecp_group grp; /*!< elliptic curve used */
47 mbedtls_mpi d; /*!< our secret value (private key) */
48 mbedtls_ecp_point Q; /*!< our public value (public key) */
49 mbedtls_ecp_point Qp; /*!< peer's public value (public key) */
50 mbedtls_mpi z; /*!< shared secret */
51 int point_format; /*!< format for point export in TLS */
52 mbedtls_ecp_point Vi; /*!< blinding value (for later) */
53 mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */
54 mbedtls_mpi _d; /*!< previous d (for later) */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020055#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +020056 int restart_enabled; /*!< enable restartalbe EC computations? */
Manuel Pégourié-Gonnard66ba48a2017-04-27 11:38:26 +020057 mbedtls_ecp_restart_ctx rs; /*!< restart context for EC computations */
58#endif
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060mbedtls_ecdh_context;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010061
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010062/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020063 * \brief Generate a public key.
64 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010065 *
66 * \param grp ECP group
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020067 * \param d Destination MPI (secret exponent, aka private key)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010068 * \param Q Destination point (public key)
69 * \param f_rng RNG function
70 * \param p_rng RNG parameter
71 *
72 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +020074 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
75 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010078 int (*f_rng)(void *, unsigned char *, size_t),
79 void *p_rng );
80
81/**
82 * \brief Compute shared secret
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020083 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010084 *
85 * \param grp ECP group
86 * \param z Destination MPI (shared secret)
87 * \param Q Public key from other party
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020088 * \param d Our secret exponent (private key)
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020089 * \param f_rng RNG function (see notes)
90 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010091 *
92 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +020094 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
95 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020096 *
97 * \note If f_rng is not NULL, it is used to implement
98 * countermeasures against potential elaborate timing
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 * attacks, see \c mbedtls_ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
102 const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200103 int (*f_rng)(void *, unsigned char *, size_t),
104 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100105
106/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100107 * \brief Initialize context
108 *
109 * \param ctx Context to initialize
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100112
113/**
114 * \brief Free context
115 *
116 * \param ctx Context to free
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100119
120/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200121 * \brief Generate a public key and a TLS ServerKeyExchange payload.
122 * (First function used by a TLS server for ECDHE.)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100123 *
124 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100125 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200126 * \param buf destination buffer
127 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100128 * \param f_rng RNG function
129 * \param p_rng RNG parameter
130 *
131 * \note This function assumes that ctx->grp has already been
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200132 * properly set (for example using mbedtls_ecp_group_load).
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100133 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200135 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
136 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100139 unsigned char *buf, size_t blen,
140 int (*f_rng)(void *, unsigned char *, size_t),
141 void *p_rng );
142
143/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200144 * \brief Parse and procress a TLS ServerKeyExhange payload.
145 * (First function used by a TLS client for ECDHE.)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100146 *
147 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200148 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100149 * \param end one past end of buffer
150 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100154 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100155
156/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200157 * \brief Setup an ECDH context from an EC key.
158 * (Used by clients and servers in place of the
159 * ServerKeyEchange for static ECDH: import ECDH parameters
160 * from a certificate's EC key information.)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100161 *
162 * \param ctx ECDH constext to set
163 * \param key EC key to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100164 * \param side Is it our key (1) or the peer's key (0) ?
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100165 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
169 mbedtls_ecdh_side side );
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100170
171/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200172 * \brief Generate a public key and a TLS ClientKeyExchange payload.
173 * (Second function used by a TLS client for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100174 *
175 * \param ctx ECDH context
176 * \param olen number of bytes actually written
177 * \param buf destination buffer
178 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200179 * \param f_rng RNG function
180 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100181 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200183 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
184 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100187 unsigned char *buf, size_t blen,
188 int (*f_rng)(void *, unsigned char *, size_t),
189 void *p_rng );
190
191/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200192 * \brief Parse and process a TLS ClientKeyExchange payload.
193 * (Second function used by a TLS server for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100194 *
195 * \param ctx ECDH context
196 * \param buf start of input buffer
197 * \param blen length of input buffer
198 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100202 const unsigned char *buf, size_t blen );
203
204/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200205 * \brief Derive and export the shared secret.
206 * (Last function used by both TLS client en servers.)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100207 *
208 * \param ctx ECDH context
209 * \param olen number of bytes written
210 * \param buf destination buffer
211 * \param blen buffer length
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared()
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200213 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100214 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code
Manuel Pégourié-Gonnardc90d3b02017-04-27 10:48:29 +0200216 * MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
217 * operations was reached: see \c mbedtls_ecp_set_max_ops().
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200220 unsigned char *buf, size_t blen,
221 int (*f_rng)(void *, unsigned char *, size_t),
222 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100223
Manuel Pégourié-Gonnard23e41622017-05-18 12:35:37 +0200224#if defined(MBEDTLS_ECP_RESTARTABLE)
225/**
226 * \brief Enable restartable EC computations for this context.
227 * (Default: disabled.)
228 *
229 * \sa \c mbedtls_ecp_set_max_ops()
230 *
231 * \note It is not possible to safely disable restartable
232 * computations once enabled, except by free-ing the context,
233 * which cancels possible in-progress operations.
234 *
235 * \param ctx ECDH context
236 */
237void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx );
238#endif /* MBEDTLS_ECP_RESTARTABLE */
239
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100240#ifdef __cplusplus
241}
242#endif
243
Paul Bakker9af723c2014-05-01 13:03:14 +0200244#endif /* ecdh.h */