blob: 08de72cfcfe6ebe3740dba1b58217ce12350be93 [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
30#include "polarssl/ecp.h"
31
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é-Gonnard63533e42013-02-10 14:21:04 +010047}
48ecdh_context;
49
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010050/**
51 * \brief Generate a public key
52 *
53 * \param grp ECP group
54 * \param d Destination MPI (secret exponent)
55 * \param Q Destination point (public key)
56 * \param f_rng RNG function
57 * \param p_rng RNG parameter
58 *
59 * \return 0 if successful,
60 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
61 */
62int ecdh_gen_public( const ecp_group *grp, mpi *d, ecp_point *Q,
63 int (*f_rng)(void *, unsigned char *, size_t),
64 void *p_rng );
65
66/**
67 * \brief Compute shared secret
68 *
69 * \param grp ECP group
70 * \param z Destination MPI (shared secret)
71 * \param Q Public key from other party
72 * \param d Our secret exponent
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020073 * \param f_rng RNG function (see notes)
74 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010075 *
76 * \return 0 if successful,
77 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020078 *
79 * \note If f_rng is not NULL, it is used to implement
80 * countermeasures against potential elaborate timing
81 * attacks, see \c ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010082 */
83int ecdh_compute_shared( const ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020084 const ecp_point *Q, const mpi *d,
85 int (*f_rng)(void *, unsigned char *, size_t),
86 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010087
88/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010089 * \brief Initialize context
90 *
91 * \param ctx Context to initialize
92 */
93void ecdh_init( ecdh_context *ctx );
94
95/**
96 * \brief Free context
97 *
98 * \param ctx Context to free
99 */
100void ecdh_free( ecdh_context *ctx );
101
102/**
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100103 * \brief Setup and write the ServerKeyExhange parameters
104 *
105 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100106 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200107 * \param buf destination buffer
108 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100109 * \param f_rng RNG function
110 * \param p_rng RNG parameter
111 *
112 * \note This function assumes that ctx->grp has already been
113 * properly set (for example using ecp_use_known_dp).
114 *
115 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
116 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100117int ecdh_make_params( ecdh_context *ctx, size_t *olen,
118 unsigned char *buf, size_t blen,
119 int (*f_rng)(void *, unsigned char *, size_t),
120 void *p_rng );
121
122/**
123 * \brief Parse the ServerKeyExhange parameters
124 *
125 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200126 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100127 * \param end one past end of buffer
128 *
129 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
130 */
131int ecdh_read_params( ecdh_context *ctx,
132 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100133
134/**
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100135 * \brief Setup and export the client's public value
136 *
137 * \param ctx ECDH context
138 * \param olen number of bytes actually written
139 * \param buf destination buffer
140 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200141 * \param f_rng RNG function
142 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100143 *
144 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
145 */
146int ecdh_make_public( ecdh_context *ctx, size_t *olen,
147 unsigned char *buf, size_t blen,
148 int (*f_rng)(void *, unsigned char *, size_t),
149 void *p_rng );
150
151/**
152 * \brief Parse and import the client's public value
153 *
154 * \param ctx ECDH context
155 * \param buf start of input buffer
156 * \param blen length of input buffer
157 *
158 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
159 */
160int ecdh_read_public( ecdh_context *ctx,
161 const unsigned char *buf, size_t blen );
162
163/**
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100164 * \brief Derive and export the shared secret
165 *
166 * \param ctx ECDH context
167 * \param olen number of bytes written
168 * \param buf destination buffer
169 * \param blen buffer length
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200170 * \param f_rng RNG function, see notes for \c ecdh_compute_shared()
171 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100172 *
173 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
174 */
175int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200176 unsigned char *buf, size_t blen,
177 int (*f_rng)(void *, unsigned char *, size_t),
178 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100179
180/**
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100181 * \brief Checkup routine
182 *
183 * \return 0 if successful, or 1 if the test failed
184 */
185int ecdh_self_test( int verbose );
186
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100187#ifdef __cplusplus
188}
189#endif
190
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100191#endif