blob: 21823c6517edffd50abb7c3c6d482499a054c9fe [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01007 *
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23/*
24 * References:
25 *
26 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010027 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010028 */
29
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010031#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010035
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010036#if defined(POLARSSL_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010037
38#include "polarssl/ecdh.h"
39
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010040/*
41 * Generate public key: simple wrapper around ecp_gen_keypair
42 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020043int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010044 int (*f_rng)(void *, unsigned char *, size_t),
45 void *p_rng )
46{
47 return ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
48}
49
50/*
51 * Compute shared secret (SEC1 3.3.1)
52 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020053int ecdh_compute_shared( ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020054 const ecp_point *Q, const mpi *d,
55 int (*f_rng)(void *, unsigned char *, size_t),
56 void *p_rng )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010057{
58 int ret;
59 ecp_point P;
60
61 ecp_point_init( &P );
62
63 /*
64 * Make sure Q is a valid pubkey before using it
65 */
66 MPI_CHK( ecp_check_pubkey( grp, Q ) );
67
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020068 MPI_CHK( ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010069
70 if( ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020071 {
72 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
73 goto cleanup;
74 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010075
76 MPI_CHK( mpi_copy( z, &P.X ) );
77
78cleanup:
79 ecp_point_free( &P );
80
81 return( ret );
82}
83
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010084/*
85 * Initialize context
86 */
87void ecdh_init( ecdh_context *ctx )
88{
Manuel Pégourié-Gonnardc83e4182013-09-17 10:48:41 +020089 memset( ctx, 0, sizeof( ecdh_context ) );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010090}
91
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010092/*
93 * Free context
94 */
95void ecdh_free( ecdh_context *ctx )
96{
97 if( ctx == NULL )
98 return;
99
100 ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100101 ecp_point_free( &ctx->Q );
102 ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnardc83e4182013-09-17 10:48:41 +0200103 ecp_point_free( &ctx->Vi );
104 ecp_point_free( &ctx->Vf );
Paul Bakker66d5d072014-06-17 16:39:18 +0200105 mpi_free( &ctx->d );
106 mpi_free( &ctx->z );
107 mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100108}
109
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100110/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100111 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100112 * struct {
113 * ECParameters curve_params;
114 * ECPoint public;
115 * } ServerECDHParams;
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 )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100121{
122 int ret;
123 size_t grp_len, pt_len;
124
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100125 if( ctx == NULL || ctx->grp.pbits == 0 )
126 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
127
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100128 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
129 != 0 )
130 return( ret );
131
132 if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
133 != 0 )
134 return( ret );
135
136 buf += grp_len;
137 blen -= grp_len;
138
139 if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
140 &pt_len, buf, blen ) ) != 0 )
141 return( ret );
142
143 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200144 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100145}
146
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100147/*
148 * Read the ServerKeyExhange parameters (RFC 4492)
149 * struct {
150 * ECParameters curve_params;
151 * ECPoint public;
152 * } ServerECDHParams;
153 */
154int ecdh_read_params( ecdh_context *ctx,
155 const unsigned char **buf, const unsigned char *end )
156{
157 int ret;
158
159 if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
160 return( ret );
161
162 if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
163 != 0 )
164 return( ret );
165
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200166 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100167}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100168
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100169/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100170 * Get parameters from a keypair
171 */
172int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key,
173 ecdh_side side )
174{
175 int ret;
176
177 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
178 return( ret );
179
180 /* If it's not our key, just import the public part as Qp */
181 if( side == POLARSSL_ECDH_THEIRS )
182 return( ecp_copy( &ctx->Qp, &key->Q ) );
183
184 /* Our key: import public (as Q) and private parts */
185 if( side != POLARSSL_ECDH_OURS )
186 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
187
188 if( ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
189 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 )
190 return( ret );
191
192 return( 0 );
193}
194
195/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100196 * Setup and export the client public value
197 */
198int ecdh_make_public( ecdh_context *ctx, size_t *olen,
199 unsigned char *buf, size_t blen,
200 int (*f_rng)(void *, unsigned char *, size_t),
201 void *p_rng )
202{
203 int ret;
204
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100205 if( ctx == NULL || ctx->grp.pbits == 0 )
206 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
207
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100208 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
209 != 0 )
210 return( ret );
211
212 return ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
213 olen, buf, blen );
214}
215
216/*
217 * Parse and import the client's public value
218 */
219int ecdh_read_public( ecdh_context *ctx,
220 const unsigned char *buf, size_t blen )
221{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100222 int ret;
223 const unsigned char *p = buf;
224
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100225 if( ctx == NULL )
226 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
227
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100228 if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
229 return( ret );
230
231 if( (size_t)( p - buf ) != blen )
232 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
233
234 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100235}
236
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100237/*
238 * Derive and export the shared secret
239 */
240int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200241 unsigned char *buf, size_t blen,
242 int (*f_rng)(void *, unsigned char *, size_t),
243 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100244{
245 int ret;
246
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100247 if( ctx == NULL )
248 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
249
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200250 if( ( ret = ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
251 f_rng, p_rng ) ) != 0 )
252 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100253 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200254 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100255
Paul Bakker41c83d32013-03-20 14:39:14 +0100256 if( mpi_size( &ctx->z ) > blen )
257 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
258
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100259 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Paul Bakker41c83d32013-03-20 14:39:14 +0100260 return mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100261}
262
263
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100264#if defined(POLARSSL_SELF_TEST)
265
266/*
267 * Checkup routine
268 */
269int ecdh_self_test( int verbose )
270{
Manuel Pégourié-Gonnard7c593632014-01-20 10:27:13 +0100271 ((void) verbose );
272 return( 0 );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100273}
274
Paul Bakker9af723c2014-05-01 13:03:14 +0200275#endif /* POLARSSL_SELF_TEST */
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100276
Paul Bakker9af723c2014-05-01 13:03:14 +0200277#endif /* POLARSSL_ECDH_C */