blob: 6564862bdf166f201f46fa00642102ff616fd93a [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01007 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00008 * This file is part of mbed TLS (http://www.polarssl.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25#ifndef POLARSSL_ECDH_H
26#define POLARSSL_ECDH_H
27
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020028#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010029
Paul Bakker407a0da2013-06-27 14:29:21 +020030#ifdef __cplusplus
31extern "C" {
32#endif
33
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010034/**
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010035 * When importing from an EC key, select if it is our key or the peer's key
36 */
37typedef enum
38{
39 POLARSSL_ECDH_OURS,
40 POLARSSL_ECDH_THEIRS,
41} ecdh_side;
42
43/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010044 * \brief ECDH context structure
45 */
46typedef struct
47{
Paul Bakker237a8472014-06-25 14:45:24 +020048 ecp_group grp; /*!< elliptic curve used */
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020049 mpi d; /*!< our secret value (private key) */
50 ecp_point Q; /*!< our public value (public key) */
51 ecp_point Qp; /*!< peer's public value (public key) */
52 mpi z; /*!< shared secret */
53 int point_format; /*!< format for point export in TLS messages */
54 ecp_point Vi; /*!< blinding value (for later) */
55 ecp_point Vf; /*!< un-blinding value (for later) */
56 mpi _d; /*!< previous d (for later) */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010057}
58ecdh_context;
59
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010060/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020061 * \brief Generate a public key.
62 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010063 *
64 * \param grp ECP group
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020065 * \param d Destination MPI (secret exponent, aka private key)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010066 * \param Q Destination point (public key)
67 * \param f_rng RNG function
68 * \param p_rng RNG parameter
69 *
70 * \return 0 if successful,
71 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
72 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020073int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010074 int (*f_rng)(void *, unsigned char *, size_t),
75 void *p_rng );
76
77/**
78 * \brief Compute shared secret
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020079 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010080 *
81 * \param grp ECP group
82 * \param z Destination MPI (shared secret)
83 * \param Q Public key from other party
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020084 * \param d Our secret exponent (private key)
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020085 * \param f_rng RNG function (see notes)
86 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010087 *
88 * \return 0 if successful,
89 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020090 *
91 * \note If f_rng is not NULL, it is used to implement
92 * countermeasures against potential elaborate timing
93 * attacks, see \c ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010094 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020095int ecdh_compute_shared( ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020096 const ecp_point *Q, const mpi *d,
97 int (*f_rng)(void *, unsigned char *, size_t),
98 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010099
100/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100101 * \brief Initialize context
102 *
103 * \param ctx Context to initialize
104 */
105void ecdh_init( ecdh_context *ctx );
106
107/**
108 * \brief Free context
109 *
110 * \param ctx Context to free
111 */
112void ecdh_free( ecdh_context *ctx );
113
114/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200115 * \brief Generate a public key and a TLS ServerKeyExchange payload.
116 * (First function used by a TLS server for ECDHE.)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100117 *
118 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100119 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200120 * \param buf destination buffer
121 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100122 * \param f_rng RNG function
123 * \param p_rng RNG parameter
124 *
125 * \note This function assumes that ctx->grp has already been
126 * properly set (for example using ecp_use_known_dp).
127 *
128 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
129 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100130int ecdh_make_params( ecdh_context *ctx, size_t *olen,
131 unsigned char *buf, size_t blen,
132 int (*f_rng)(void *, unsigned char *, size_t),
133 void *p_rng );
134
135/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200136 * \brief Parse and procress a TLS ServerKeyExhange payload.
137 * (First function used by a TLS client for ECDHE.)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100138 *
139 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200140 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100141 * \param end one past end of buffer
142 *
143 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
144 */
145int ecdh_read_params( ecdh_context *ctx,
146 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100147
148/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200149 * \brief Setup an ECDH context from an EC key.
150 * (Used by clients and servers in place of the
151 * ServerKeyEchange for static ECDH: import ECDH parameters
152 * from a certificate's EC key information.)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100153 *
154 * \param ctx ECDH constext to set
155 * \param key EC key to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100156 * \param side Is it our key (1) or the peer's key (0) ?
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100157 *
158 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
159 */
160int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key,
161 ecdh_side side );
162
163/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200164 * \brief Generate a public key and a TLS ClientKeyExchange payload.
165 * (Second function used by a TLS client for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100166 *
167 * \param ctx ECDH context
168 * \param olen number of bytes actually written
169 * \param buf destination buffer
170 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200171 * \param f_rng RNG function
172 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100173 *
174 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
175 */
176int ecdh_make_public( ecdh_context *ctx, size_t *olen,
177 unsigned char *buf, size_t blen,
178 int (*f_rng)(void *, unsigned char *, size_t),
179 void *p_rng );
180
181/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200182 * \brief Parse and process a TLS ClientKeyExchange payload.
183 * (Second function used by a TLS server for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100184 *
185 * \param ctx ECDH context
186 * \param buf start of input buffer
187 * \param blen length of input buffer
188 *
189 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
190 */
191int ecdh_read_public( ecdh_context *ctx,
192 const unsigned char *buf, size_t blen );
193
194/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200195 * \brief Derive and export the shared secret.
196 * (Last function used by both TLS client en servers.)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100197 *
198 * \param ctx ECDH context
199 * \param olen number of bytes written
200 * \param buf destination buffer
201 * \param blen buffer length
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200202 * \param f_rng RNG function, see notes for \c ecdh_compute_shared()
203 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100204 *
205 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
206 */
207int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200208 unsigned char *buf, size_t blen,
209 int (*f_rng)(void *, unsigned char *, size_t),
210 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100211
212/**
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100213 * \brief Checkup routine
214 *
215 * \return 0 if successful, or 1 if the test failed
216 */
217int ecdh_self_test( int verbose );
218
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100219#ifdef __cplusplus
220}
221#endif
222
Paul Bakker9af723c2014-05-01 13:03:14 +0200223#endif /* ecdh.h */