blob: d91aea58d40b382d44c67e2e44c8e38be0eb30cf [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
106 * \param buf destination buffer
107 * \param olen number of chars written
108 * \param f_rng RNG function
109 * \param p_rng RNG parameter
110 *
111 * \note This function assumes that ctx->grp has already been
112 * properly set (for example using ecp_use_known_dp).
113 *
114 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
115 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100116int ecdh_make_params( ecdh_context *ctx, size_t *olen,
117 unsigned char *buf, size_t blen,
118 int (*f_rng)(void *, unsigned char *, size_t),
119 void *p_rng );
120
121/**
122 * \brief Parse the ServerKeyExhange parameters
123 *
124 * \param ctx ECDH context
125 * \param buf $(start of input buffer)
126 * \param end one past end of buffer
127 *
128 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
129 */
130int ecdh_read_params( ecdh_context *ctx,
131 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100132
133/**
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100134 * \brief Setup and export the client's public value
135 *
136 * \param ctx ECDH context
137 * \param olen number of bytes actually written
138 * \param buf destination buffer
139 * \param blen size of destination buffer
140 *
141 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
142 */
143int ecdh_make_public( ecdh_context *ctx, size_t *olen,
144 unsigned char *buf, size_t blen,
145 int (*f_rng)(void *, unsigned char *, size_t),
146 void *p_rng );
147
148/**
149 * \brief Parse and import the client's public value
150 *
151 * \param ctx ECDH context
152 * \param buf start of input buffer
153 * \param blen length of input buffer
154 *
155 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
156 */
157int ecdh_read_public( ecdh_context *ctx,
158 const unsigned char *buf, size_t blen );
159
160/**
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100161 * \brief Derive and export the shared secret
162 *
163 * \param ctx ECDH context
164 * \param olen number of bytes written
165 * \param buf destination buffer
166 * \param blen buffer length
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200167 * \param f_rng RNG function, see notes for \c ecdh_compute_shared()
168 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100169 *
170 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
171 */
172int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200173 unsigned char *buf, size_t blen,
174 int (*f_rng)(void *, unsigned char *, size_t),
175 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100176
177/**
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100178 * \brief Checkup routine
179 *
180 * \return 0 if successful, or 1 if the test failed
181 */
182int ecdh_self_test( int verbose );
183
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100184#ifdef __cplusplus
185}
186#endif
187
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100188#endif