blob: 2184ab95f03a45be5d3428261ea9465a009520e2 [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
73 *
74 * \return 0 if successful,
75 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
76 */
77int ecdh_compute_shared( const ecp_group *grp, mpi *z,
78 const ecp_point *Q, const mpi *d );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010079
80/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010081 * \brief Initialize context
82 *
83 * \param ctx Context to initialize
84 */
85void ecdh_init( ecdh_context *ctx );
86
87/**
88 * \brief Free context
89 *
90 * \param ctx Context to free
91 */
92void ecdh_free( ecdh_context *ctx );
93
94/**
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +010095 * \brief Setup and write the ServerKeyExhange parameters
96 *
97 * \param ctx ECDH context
98 * \param buf destination buffer
99 * \param olen number of chars written
100 * \param f_rng RNG function
101 * \param p_rng RNG parameter
102 *
103 * \note This function assumes that ctx->grp has already been
104 * properly set (for example using ecp_use_known_dp).
105 *
106 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
107 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100108int ecdh_make_params( ecdh_context *ctx, size_t *olen,
109 unsigned char *buf, size_t blen,
110 int (*f_rng)(void *, unsigned char *, size_t),
111 void *p_rng );
112
113/**
114 * \brief Parse the ServerKeyExhange parameters
115 *
116 * \param ctx ECDH context
117 * \param buf $(start of input buffer)
118 * \param end one past end of buffer
119 *
120 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
121 */
122int ecdh_read_params( ecdh_context *ctx,
123 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100124
125/**
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100126 * \brief Setup and export the client's public value
127 *
128 * \param ctx ECDH context
129 * \param olen number of bytes actually written
130 * \param buf destination buffer
131 * \param blen size of destination buffer
132 *
133 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
134 */
135int ecdh_make_public( ecdh_context *ctx, size_t *olen,
136 unsigned char *buf, size_t blen,
137 int (*f_rng)(void *, unsigned char *, size_t),
138 void *p_rng );
139
140/**
141 * \brief Parse and import the client's public value
142 *
143 * \param ctx ECDH context
144 * \param buf start of input buffer
145 * \param blen length of input buffer
146 *
147 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
148 */
149int ecdh_read_public( ecdh_context *ctx,
150 const unsigned char *buf, size_t blen );
151
152/**
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100153 * \brief Derive and export the shared secret
154 *
155 * \param ctx ECDH context
156 * \param olen number of bytes written
157 * \param buf destination buffer
158 * \param blen buffer length
159 *
160 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
161 */
162int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
163 unsigned char *buf, size_t blen );
164
165/**
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100166 * \brief Checkup routine
167 *
168 * \return 0 if successful, or 1 if the test failed
169 */
170int ecdh_self_test( int verbose );
171
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100172#ifdef __cplusplus
173}
174#endif
175
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100176#endif