blob: 428275d7106c9bf75243ddfeac923b18ce4496c2 [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é-Gonnard23617462014-06-25 13:55:10 +020050 ecp_group grp; /*!< ellipitic curve used */
51 mpi d; /*!< our secret value (private key) */
52 ecp_point Q; /*!< our public value (public key) */
53 ecp_point Qp; /*!< peer's public value (public key) */
54 mpi z; /*!< shared secret */
55 int point_format; /*!< format for point export in TLS messages */
56 ecp_point Vi; /*!< blinding value (for later) */
57 ecp_point Vf; /*!< un-blinding value (for later) */
58 mpi _d; /*!< previous d (for later) */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010059}
60ecdh_context;
61
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010062/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020063 * \brief Generate a public key.
64 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010065 *
66 * \param grp ECP group
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020067 * \param d Destination MPI (secret exponent, aka private key)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010068 * \param Q Destination point (public key)
69 * \param f_rng RNG function
70 * \param p_rng RNG parameter
71 *
72 * \return 0 if successful,
73 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
74 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020075int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010076 int (*f_rng)(void *, unsigned char *, size_t),
77 void *p_rng );
78
79/**
80 * \brief Compute shared secret
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020081 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010082 *
83 * \param grp ECP group
84 * \param z Destination MPI (shared secret)
85 * \param Q Public key from other party
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020086 * \param d Our secret exponent (private key)
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020087 * \param f_rng RNG function (see notes)
88 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010089 *
90 * \return 0 if successful,
91 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020092 *
93 * \note If f_rng is not NULL, it is used to implement
94 * countermeasures against potential elaborate timing
95 * attacks, see \c ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010096 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020097int ecdh_compute_shared( ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020098 const ecp_point *Q, const mpi *d,
99 int (*f_rng)(void *, unsigned char *, size_t),
100 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100101
102/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100103 * \brief Initialize context
104 *
105 * \param ctx Context to initialize
106 */
107void ecdh_init( ecdh_context *ctx );
108
109/**
110 * \brief Free context
111 *
112 * \param ctx Context to free
113 */
114void ecdh_free( ecdh_context *ctx );
115
116/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200117 * \brief Generate a public key and a TLS ServerKeyExchange payload.
118 * (First function used by a TLS server for ECDHE.)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100119 *
120 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100121 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200122 * \param buf destination buffer
123 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100124 * \param f_rng RNG function
125 * \param p_rng RNG parameter
126 *
127 * \note This function assumes that ctx->grp has already been
128 * properly set (for example using ecp_use_known_dp).
129 *
130 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
131 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100132int ecdh_make_params( ecdh_context *ctx, size_t *olen,
133 unsigned char *buf, size_t blen,
134 int (*f_rng)(void *, unsigned char *, size_t),
135 void *p_rng );
136
137/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200138 * \brief Parse and procress a TLS ServerKeyExhange payload.
139 * (First function used by a TLS client for ECDHE.)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100140 *
141 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200142 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100143 * \param end one past end of buffer
144 *
145 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
146 */
147int ecdh_read_params( ecdh_context *ctx,
148 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100149
150/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200151 * \brief Setup an ECDH context from an EC key.
152 * (Used by clients and servers in place of the
153 * ServerKeyEchange for static ECDH: import ECDH parameters
154 * from a certificate's EC key information.)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100155 *
156 * \param ctx ECDH constext to set
157 * \param key EC key to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100158 * \param side Is it our key (1) or the peer's key (0) ?
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100159 *
160 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
161 */
162int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key,
163 ecdh_side side );
164
165/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200166 * \brief Generate a public key and a TLS ClientKeyExchange payload.
167 * (Second function used by a TLS client for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100168 *
169 * \param ctx ECDH context
170 * \param olen number of bytes actually written
171 * \param buf destination buffer
172 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200173 * \param f_rng RNG function
174 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100175 *
176 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
177 */
178int ecdh_make_public( ecdh_context *ctx, size_t *olen,
179 unsigned char *buf, size_t blen,
180 int (*f_rng)(void *, unsigned char *, size_t),
181 void *p_rng );
182
183/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200184 * \brief Parse and process a TLS ClientKeyExchange payload.
185 * (Second function used by a TLS server for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100186 *
187 * \param ctx ECDH context
188 * \param buf start of input buffer
189 * \param blen length of input buffer
190 *
191 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
192 */
193int ecdh_read_public( ecdh_context *ctx,
194 const unsigned char *buf, size_t blen );
195
196/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200197 * \brief Derive and export the shared secret.
198 * (Last function used by both TLS client en servers.)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100199 *
200 * \param ctx ECDH context
201 * \param olen number of bytes written
202 * \param buf destination buffer
203 * \param blen buffer length
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200204 * \param f_rng RNG function, see notes for \c ecdh_compute_shared()
205 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100206 *
207 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
208 */
209int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200210 unsigned char *buf, size_t blen,
211 int (*f_rng)(void *, unsigned char *, size_t),
212 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100213
214/**
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100215 * \brief Checkup routine
216 *
217 * \return 0 if successful, or 1 if the test failed
218 */
219int ecdh_self_test( int verbose );
220
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100221#ifdef __cplusplus
222}
223#endif
224
Paul Bakker9af723c2014-05-01 13:03:14 +0200225#endif /* ecdh.h */