blob: 4c82f25f2de0d453311fa1244f3c2cbe3894d1ba [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/**
37 * \brief ECDH context structure
38 */
39typedef struct
40{
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +010041 ecp_group grp; /*!< ellipitic curve used */
42 mpi d; /*!< our secret value */
43 ecp_point Q; /*!< our public value */
44 ecp_point Qp; /*!< peer's public value */
45 mpi z; /*!< shared secret */
46 int point_format; /*!< format for point export */
Manuel Pégourié-Gonnardc83e4182013-09-17 10:48:41 +020047 ecp_point Vi; /*!< blinding value (for later) */
48 ecp_point Vf; /*!< un-blinding value (for later) */
49 mpi _d; /*!< previous d */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010050}
51ecdh_context;
52
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010053/**
54 * \brief Generate a public key
55 *
56 * \param grp ECP group
57 * \param d Destination MPI (secret exponent)
58 * \param Q Destination point (public key)
59 * \param f_rng RNG function
60 * \param p_rng RNG parameter
61 *
62 * \return 0 if successful,
63 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
64 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020065int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010066 int (*f_rng)(void *, unsigned char *, size_t),
67 void *p_rng );
68
69/**
70 * \brief Compute shared secret
71 *
72 * \param grp ECP group
73 * \param z Destination MPI (shared secret)
74 * \param Q Public key from other party
75 * \param d Our secret exponent
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020076 * \param f_rng RNG function (see notes)
77 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010078 *
79 * \return 0 if successful,
80 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020081 *
82 * \note If f_rng is not NULL, it is used to implement
83 * countermeasures against potential elaborate timing
84 * attacks, see \c ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010085 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020086int ecdh_compute_shared( ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020087 const ecp_point *Q, const mpi *d,
88 int (*f_rng)(void *, unsigned char *, size_t),
89 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010090
91/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010092 * \brief Initialize context
93 *
94 * \param ctx Context to initialize
95 */
96void ecdh_init( ecdh_context *ctx );
97
98/**
99 * \brief Free context
100 *
101 * \param ctx Context to free
102 */
103void ecdh_free( ecdh_context *ctx );
104
105/**
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100106 * \brief Setup and write the ServerKeyExhange parameters
107 *
108 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100109 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200110 * \param buf destination buffer
111 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100112 * \param f_rng RNG function
113 * \param p_rng RNG parameter
114 *
115 * \note This function assumes that ctx->grp has already been
116 * properly set (for example using ecp_use_known_dp).
117 *
118 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
119 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100120int ecdh_make_params( ecdh_context *ctx, size_t *olen,
121 unsigned char *buf, size_t blen,
122 int (*f_rng)(void *, unsigned char *, size_t),
123 void *p_rng );
124
125/**
126 * \brief Parse the ServerKeyExhange parameters
127 *
128 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200129 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100130 * \param end one past end of buffer
131 *
132 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
133 */
134int ecdh_read_params( ecdh_context *ctx,
135 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100136
137/**
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100138 * \brief Setup and export the client's public value
139 *
140 * \param ctx ECDH context
141 * \param olen number of bytes actually written
142 * \param buf destination buffer
143 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200144 * \param f_rng RNG function
145 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100146 *
147 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
148 */
149int ecdh_make_public( ecdh_context *ctx, size_t *olen,
150 unsigned char *buf, size_t blen,
151 int (*f_rng)(void *, unsigned char *, size_t),
152 void *p_rng );
153
154/**
155 * \brief Parse and import the client's public value
156 *
157 * \param ctx ECDH context
158 * \param buf start of input buffer
159 * \param blen length of input buffer
160 *
161 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
162 */
163int ecdh_read_public( ecdh_context *ctx,
164 const unsigned char *buf, size_t blen );
165
166/**
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100167 * \brief Derive and export the shared secret
168 *
169 * \param ctx ECDH context
170 * \param olen number of bytes written
171 * \param buf destination buffer
172 * \param blen buffer length
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200173 * \param f_rng RNG function, see notes for \c ecdh_compute_shared()
174 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100175 *
176 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
177 */
178int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200179 unsigned char *buf, size_t blen,
180 int (*f_rng)(void *, unsigned char *, size_t),
181 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100182
183/**
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100184 * \brief Checkup routine
185 *
186 * \return 0 if successful, or 1 if the test failed
187 */
188int ecdh_self_test( int verbose );
189
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100190#ifdef __cplusplus
191}
192#endif
193
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100194#endif