blob: b0bbde06127033e13b75b159fe42520f4cdb07a4 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01009 *
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24#ifndef POLARSSL_ECDH_H
25#define POLARSSL_ECDH_H
26
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020027#include "ecp.h"
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010028
Paul Bakker407a0da2013-06-27 14:29:21 +020029#ifdef __cplusplus
30extern "C" {
31#endif
32
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010033/**
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +010034 * When importing from an EC key, select if it is our key or the peer's key
35 */
36typedef enum
37{
38 POLARSSL_ECDH_OURS,
39 POLARSSL_ECDH_THEIRS,
40} ecdh_side;
41
42/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010043 * \brief ECDH context structure
44 */
45typedef struct
46{
Paul Bakker237a8472014-06-25 14:45:24 +020047 ecp_group grp; /*!< elliptic curve used */
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020048 mpi d; /*!< our secret value (private key) */
49 ecp_point Q; /*!< our public value (public key) */
50 ecp_point Qp; /*!< peer's public value (public key) */
51 mpi z; /*!< shared secret */
52 int point_format; /*!< format for point export in TLS messages */
53 ecp_point Vi; /*!< blinding value (for later) */
54 ecp_point Vf; /*!< un-blinding value (for later) */
55 mpi _d; /*!< previous d (for later) */
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010056}
57ecdh_context;
58
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010059/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020060 * \brief Generate a public key.
61 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010062 *
63 * \param grp ECP group
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020064 * \param d Destination MPI (secret exponent, aka private key)
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010065 * \param Q Destination point (public key)
66 * \param f_rng RNG function
67 * \param p_rng RNG parameter
68 *
69 * \return 0 if successful,
70 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
71 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020072int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010073 int (*f_rng)(void *, unsigned char *, size_t),
74 void *p_rng );
75
76/**
77 * \brief Compute shared secret
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020078 * Raw function that only does the core computation.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010079 *
80 * \param grp ECP group
81 * \param z Destination MPI (shared secret)
82 * \param Q Public key from other party
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +020083 * \param d Our secret exponent (private key)
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020084 * \param f_rng RNG function (see notes)
85 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010086 *
87 * \return 0 if successful,
88 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020089 *
90 * \note If f_rng is not NULL, it is used to implement
91 * countermeasures against potential elaborate timing
92 * attacks, see \c ecp_mul() for details.
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010093 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020094int ecdh_compute_shared( ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020095 const ecp_point *Q, const mpi *d,
96 int (*f_rng)(void *, unsigned char *, size_t),
97 void *p_rng );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010098
99/**
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100100 * \brief Initialize context
101 *
102 * \param ctx Context to initialize
103 */
104void ecdh_init( ecdh_context *ctx );
105
106/**
107 * \brief Free context
108 *
109 * \param ctx Context to free
110 */
111void ecdh_free( ecdh_context *ctx );
112
113/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200114 * \brief Generate a public key and a TLS ServerKeyExchange payload.
115 * (First function used by a TLS server for ECDHE.)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100116 *
117 * \param ctx ECDH context
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100118 * \param olen number of chars written
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200119 * \param buf destination buffer
120 * \param blen length of buffer
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100121 * \param f_rng RNG function
122 * \param p_rng RNG parameter
123 *
124 * \note This function assumes that ctx->grp has already been
125 * properly set (for example using ecp_use_known_dp).
126 *
127 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
128 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100129int ecdh_make_params( ecdh_context *ctx, size_t *olen,
130 unsigned char *buf, size_t blen,
131 int (*f_rng)(void *, unsigned char *, size_t),
132 void *p_rng );
133
134/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200135 * \brief Parse and procress a TLS ServerKeyExhange payload.
136 * (First function used by a TLS client for ECDHE.)
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100137 *
138 * \param ctx ECDH context
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200139 * \param buf pointer to start of input buffer
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100140 * \param end one past end of buffer
141 *
142 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
143 */
144int ecdh_read_params( ecdh_context *ctx,
145 const unsigned char **buf, const unsigned char *end );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100146
147/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200148 * \brief Setup an ECDH context from an EC key.
149 * (Used by clients and servers in place of the
150 * ServerKeyEchange for static ECDH: import ECDH parameters
151 * from a certificate's EC key information.)
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100152 *
153 * \param ctx ECDH constext to set
154 * \param key EC key to use
Paul Bakkera36d23e2013-12-30 17:57:27 +0100155 * \param side Is it our key (1) or the peer's key (0) ?
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100156 *
157 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
158 */
159int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key,
160 ecdh_side side );
161
162/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200163 * \brief Generate a public key and a TLS ClientKeyExchange payload.
164 * (Second function used by a TLS client for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100165 *
166 * \param ctx ECDH context
167 * \param olen number of bytes actually written
168 * \param buf destination buffer
169 * \param blen size of destination buffer
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200170 * \param f_rng RNG function
171 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100172 *
173 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
174 */
175int ecdh_make_public( ecdh_context *ctx, size_t *olen,
176 unsigned char *buf, size_t blen,
177 int (*f_rng)(void *, unsigned char *, size_t),
178 void *p_rng );
179
180/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200181 * \brief Parse and process a TLS ClientKeyExchange payload.
182 * (Second function used by a TLS server for ECDH(E).)
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100183 *
184 * \param ctx ECDH context
185 * \param buf start of input buffer
186 * \param blen length of input buffer
187 *
188 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
189 */
190int ecdh_read_public( ecdh_context *ctx,
191 const unsigned char *buf, size_t blen );
192
193/**
Manuel Pégourié-Gonnard23617462014-06-25 13:55:10 +0200194 * \brief Derive and export the shared secret.
195 * (Last function used by both TLS client en servers.)
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100196 *
197 * \param ctx ECDH context
198 * \param olen number of bytes written
199 * \param buf destination buffer
200 * \param blen buffer length
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200201 * \param f_rng RNG function, see notes for \c ecdh_compute_shared()
202 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100203 *
204 * \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
205 */
206int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200207 unsigned char *buf, size_t blen,
208 int (*f_rng)(void *, unsigned char *, size_t),
209 void *p_rng );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100210
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100211#ifdef __cplusplus
212}
213#endif
214
Paul Bakker9af723c2014-05-01 13:03:14 +0200215#endif /* ecdh.h */