blob: d287948066ef5e25421d083d2db044d1e26a2513 [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
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <string.h>
41
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010042/*
43 * Generate public key: simple wrapper around ecp_gen_keypair
44 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020045int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010046 int (*f_rng)(void *, unsigned char *, size_t),
47 void *p_rng )
48{
49 return ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
50}
51
52/*
53 * Compute shared secret (SEC1 3.3.1)
54 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +020055int ecdh_compute_shared( ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020056 const ecp_point *Q, const mpi *d,
57 int (*f_rng)(void *, unsigned char *, size_t),
58 void *p_rng )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010059{
60 int ret;
61 ecp_point P;
62
63 ecp_point_init( &P );
64
65 /*
66 * Make sure Q is a valid pubkey before using it
67 */
68 MPI_CHK( ecp_check_pubkey( grp, Q ) );
69
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020070 MPI_CHK( ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010071
72 if( ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020073 {
74 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
75 goto cleanup;
76 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010077
78 MPI_CHK( mpi_copy( z, &P.X ) );
79
80cleanup:
81 ecp_point_free( &P );
82
83 return( ret );
84}
85
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010086/*
87 * Initialize context
88 */
89void ecdh_init( ecdh_context *ctx )
90{
Manuel Pégourié-Gonnardc83e4182013-09-17 10:48:41 +020091 memset( ctx, 0, sizeof( ecdh_context ) );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010092}
93
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010094/*
95 * Free context
96 */
97void ecdh_free( ecdh_context *ctx )
98{
99 if( ctx == NULL )
100 return;
101
102 ecp_group_free( &ctx->grp );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100103 ecp_point_free( &ctx->Q );
104 ecp_point_free( &ctx->Qp );
Manuel Pégourié-Gonnardc83e4182013-09-17 10:48:41 +0200105 ecp_point_free( &ctx->Vi );
106 ecp_point_free( &ctx->Vf );
Paul Bakker66d5d072014-06-17 16:39:18 +0200107 mpi_free( &ctx->d );
108 mpi_free( &ctx->z );
109 mpi_free( &ctx->_d );
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +0100110}
111
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100112/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100113 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100114 * struct {
115 * ECParameters curve_params;
116 * ECPoint public;
117 * } ServerECDHParams;
118 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100119int ecdh_make_params( ecdh_context *ctx, size_t *olen,
120 unsigned char *buf, size_t blen,
121 int (*f_rng)(void *, unsigned char *, size_t),
122 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100123{
124 int ret;
125 size_t grp_len, pt_len;
126
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100127 if( ctx == NULL || ctx->grp.pbits == 0 )
128 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
129
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100130 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
131 != 0 )
132 return( ret );
133
134 if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
135 != 0 )
136 return( ret );
137
138 buf += grp_len;
139 blen -= grp_len;
140
141 if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
142 &pt_len, buf, blen ) ) != 0 )
143 return( ret );
144
145 *olen = grp_len + pt_len;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200146 return( 0 );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100147}
148
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100149/*
150 * Read the ServerKeyExhange parameters (RFC 4492)
151 * struct {
152 * ECParameters curve_params;
153 * ECPoint public;
154 * } ServerECDHParams;
155 */
156int ecdh_read_params( ecdh_context *ctx,
157 const unsigned char **buf, const unsigned char *end )
158{
159 int ret;
160
161 if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
162 return( ret );
163
164 if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
165 != 0 )
166 return( ret );
167
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200168 return( 0 );
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100169}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100170
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100171/*
Manuel Pégourié-Gonnardcdff3cf2013-12-12 09:55:52 +0100172 * Get parameters from a keypair
173 */
174int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key,
175 ecdh_side side )
176{
177 int ret;
178
179 if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
180 return( ret );
181
182 /* If it's not our key, just import the public part as Qp */
183 if( side == POLARSSL_ECDH_THEIRS )
184 return( ecp_copy( &ctx->Qp, &key->Q ) );
185
186 /* Our key: import public (as Q) and private parts */
187 if( side != POLARSSL_ECDH_OURS )
188 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
189
190 if( ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
191 ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 )
192 return( ret );
193
194 return( 0 );
195}
196
197/*
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100198 * Setup and export the client public value
199 */
200int ecdh_make_public( ecdh_context *ctx, size_t *olen,
201 unsigned char *buf, size_t blen,
202 int (*f_rng)(void *, unsigned char *, size_t),
203 void *p_rng )
204{
205 int ret;
206
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100207 if( ctx == NULL || ctx->grp.pbits == 0 )
208 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
209
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100210 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
211 != 0 )
212 return( ret );
213
214 return ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
215 olen, buf, blen );
216}
217
218/*
219 * Parse and import the client's public value
220 */
221int ecdh_read_public( ecdh_context *ctx,
222 const unsigned char *buf, size_t blen )
223{
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100224 int ret;
225 const unsigned char *p = buf;
226
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100227 if( ctx == NULL )
228 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
229
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +0100230 if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
231 return( ret );
232
233 if( (size_t)( p - buf ) != blen )
234 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
235
236 return( 0 );
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100237}
238
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100239/*
240 * Derive and export the shared secret
241 */
242int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200243 unsigned char *buf, size_t blen,
244 int (*f_rng)(void *, unsigned char *, size_t),
245 void *p_rng )
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100246{
247 int ret;
248
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100249 if( ctx == NULL )
250 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
251
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200252 if( ( ret = ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
253 f_rng, p_rng ) ) != 0 )
254 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100255 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200256 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100257
Paul Bakker41c83d32013-03-20 14:39:14 +0100258 if( mpi_size( &ctx->z ) > blen )
259 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
260
Manuel Pégourié-Gonnard0a56c2c2014-01-17 21:24:04 +0100261 *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
Paul Bakker41c83d32013-03-20 14:39:14 +0100262 return mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100263}
264
265
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100266#if defined(POLARSSL_SELF_TEST)
267
268/*
269 * Checkup routine
270 */
271int ecdh_self_test( int verbose )
272{
Manuel Pégourié-Gonnard7c593632014-01-20 10:27:13 +0100273 ((void) verbose );
274 return( 0 );
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100275}
276
Paul Bakker9af723c2014-05-01 13:03:14 +0200277#endif /* POLARSSL_SELF_TEST */
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100278
Paul Bakker9af723c2014-05-01 13:03:14 +0200279#endif /* POLARSSL_ECDH_C */