blob: 3c4800bf0d6e8b7b147190f8f0c354b94ee2a801 [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 *
6 * Copyright (C) 2006-2013, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_ECDH_H
28#define POLARSSL_ECDH_H
29
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020030#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010031
Paul Bakker407a0da2013-06-27 14:29:21 +020032#ifdef __cplusplus
33extern "C" {
34#endif
35
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010036/**
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010037 * When importing from an EC key, select if it is our key or the peer's key
38 */
39typedef enum
40{
41 POLARSSL_ECDH_OURS,
42 POLARSSL_ECDH_THEIRS,
43} ecdh_side;
44
45/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010046 * \brief ECDH context structure
47 */
48typedef struct
49{
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +010050 ecp_group grp; /*!< ellipitic curve used */
51 mpi d; /*!< our secret value */
52 ecp_point Q; /*!< our public value */
53 ecp_point Qp; /*!< peer's public value */
54 mpi z; /*!< shared secret */
55 int point_format; /*!< format for point export */
Manuel Pégourié-Gonnardc83e4182013-09-17 10:48:41 +020056 ecp_point Vi; /*!< blinding value (for later) */
57 ecp_point Vf; /*!< un-blinding value (for later) */
58 mpi _d; /*!< previous d */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010059}
60ecdh_context;
61
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010062/**
63 * \brief Generate a public key
64 *
65 * \param grp ECP group
66 * \param d Destination MPI (secret exponent)
67 * \param Q Destination point (public key)
68 * \param f_rng RNG function
69 * \param p_rng RNG parameter
70 *
71 * \return 0 if successful,
72 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
73 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020074int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010075 int (*f_rng)(void *, unsigned char *, size_t),
76 void *p_rng );
77
78/**
79 * \brief Compute shared secret
80 *
81 * \param grp ECP group
82 * \param z Destination MPI (shared secret)
83 * \param Q Public key from other party
84 * \param d Our secret exponent
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é-Gonnard13724762013-02-10 15:01:54 +0100115 * \brief Setup and write the ServerKeyExhange parameters
116 *
117 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100118 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200119 * \param buf destination buffer
120 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100121 * \param f_rng RNG function
122 * \param p_rng RNG parameter
123 *
124 * \note This function assumes that ctx->grp has already been
125 * properly set (for example using ecp_use_known_dp).
126 *
127 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
128 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100129int ecdh_make_params( ecdh_context *ctx, size_t *olen,
130 unsigned char *buf, size_t blen,
131 int (*f_rng)(void *, unsigned char *, size_t),
132 void *p_rng );
133
134/**
135 * \brief Parse the ServerKeyExhange parameters
136 *
137 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200138 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100139 * \param end one past end of buffer
140 *
141 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
142 */
143int ecdh_read_params( ecdh_context *ctx,
144 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100145
146/**
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100147 * \brief Setup an ECDH context from an EC key
148 *
149 * \param ctx ECDH constext to set
150 * \param key EC key to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100151 * \param side Is it our key (1) or the peer's key (0) ?
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100152 *
153 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
154 */
155int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key,
156 ecdh_side side );
157
158/**
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100159 * \brief Setup and export the client's public value
160 *
161 * \param ctx ECDH context
162 * \param olen number of bytes actually written
163 * \param buf destination buffer
164 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200165 * \param f_rng RNG function
166 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100167 *
168 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
169 */
170int ecdh_make_public( ecdh_context *ctx, size_t *olen,
171 unsigned char *buf, size_t blen,
172 int (*f_rng)(void *, unsigned char *, size_t),
173 void *p_rng );
174
175/**
176 * \brief Parse and import the client's public value
177 *
178 * \param ctx ECDH context
179 * \param buf start of input buffer
180 * \param blen length of input buffer
181 *
182 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
183 */
184int ecdh_read_public( ecdh_context *ctx,
185 const unsigned char *buf, size_t blen );
186
187/**
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100188 * \brief Derive and export the shared secret
189 *
190 * \param ctx ECDH context
191 * \param olen number of bytes written
192 * \param buf destination buffer
193 * \param blen buffer length
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200194 * \param f_rng RNG function, see notes for \c ecdh_compute_shared()
195 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100196 *
197 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
198 */
199int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200200 unsigned char *buf, size_t blen,
201 int (*f_rng)(void *, unsigned char *, size_t),
202 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100203
204/**
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100205 * \brief Checkup routine
206 *
207 * \return 0 if successful, or 1 if the test failed
208 */
209int ecdh_self_test( int verbose );
210
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100211#ifdef __cplusplus
212}
213#endif
214
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100215#endif