blob: 8ef02f54bd2478e16888d6df230daab612934db2 [file] [log] [blame]
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +01001/*
2 * Elliptic curve Diffie-Hellman
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
29 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010030 * RFC 4492
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010031 */
32
33#include "polarssl/config.h"
34
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +010035#if defined(POLARSSL_ECDH_C)
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +010036
37#include "polarssl/ecdh.h"
38
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010039/*
40 * Generate public key: simple wrapper around ecp_gen_keypair
41 */
42int ecdh_gen_public( const ecp_group *grp, mpi *d, ecp_point *Q,
43 int (*f_rng)(void *, unsigned char *, size_t),
44 void *p_rng )
45{
46 return ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
47}
48
49/*
50 * Compute shared secret (SEC1 3.3.1)
51 */
52int ecdh_compute_shared( const ecp_group *grp, mpi *z,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020053 const ecp_point *Q, const mpi *d,
54 int (*f_rng)(void *, unsigned char *, size_t),
55 void *p_rng )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010056{
57 int ret;
58 ecp_point P;
59
60 ecp_point_init( &P );
61
62 /*
63 * Make sure Q is a valid pubkey before using it
64 */
65 MPI_CHK( ecp_check_pubkey( grp, Q ) );
66
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +020067 MPI_CHK( ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010068
69 if( ecp_is_zero( &P ) )
Paul Bakkerb548d772013-07-26 14:21:34 +020070 {
71 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
72 goto cleanup;
73 }
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +010074
75 MPI_CHK( mpi_copy( z, &P.X ) );
76
77cleanup:
78 ecp_point_free( &P );
79
80 return( ret );
81}
82
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010083/*
84 * Initialize context
85 */
86void ecdh_init( ecdh_context *ctx )
87{
88 ecp_group_init( &ctx->grp );
89 mpi_init ( &ctx->d );
90 ecp_point_init( &ctx->Q );
91 ecp_point_init( &ctx->Qp );
92 mpi_init ( &ctx->z );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +010093 ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010094}
95
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010096/*
97 * Free context
98 */
99void ecdh_free( ecdh_context *ctx )
100{
101 if( ctx == NULL )
102 return;
103
104 ecp_group_free( &ctx->grp );
105 mpi_free ( &ctx->d );
106 ecp_point_free( &ctx->Q );
107 ecp_point_free( &ctx->Qp );
108 mpi_free ( &ctx->z );
109}
110
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100111/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100112 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100113 * struct {
114 * ECParameters curve_params;
115 * ECPoint public;
116 * } ServerECDHParams;
117 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100118int ecdh_make_params( ecdh_context *ctx, size_t *olen,
119 unsigned char *buf, size_t blen,
120 int (*f_rng)(void *, unsigned char *, size_t),
121 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100122{
123 int ret;
124 size_t grp_len, pt_len;
125
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100126 if( ctx == NULL || ctx->grp.pbits == 0 )
127 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
128
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100129 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
130 != 0 )
131 return( ret );
132
133 if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
134 != 0 )
135 return( ret );
136
137 buf += grp_len;
138 blen -= grp_len;
139
140 if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
141 &pt_len, buf, blen ) ) != 0 )
142 return( ret );
143
144 *olen = grp_len + pt_len;
145 return 0;
146}
147
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100148/*
149 * Read the ServerKeyExhange parameters (RFC 4492)
150 * struct {
151 * ECParameters curve_params;
152 * ECPoint public;
153 * } ServerECDHParams;
154 */
155int ecdh_read_params( ecdh_context *ctx,
156 const unsigned char **buf, const unsigned char *end )
157{
158 int ret;
159
160 if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
161 return( ret );
162
163 if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
164 != 0 )
165 return( ret );
166
167 return 0;
168}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100169
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100170/*
171 * Setup and export the client public value
172 */
173int ecdh_make_public( ecdh_context *ctx, size_t *olen,
174 unsigned char *buf, size_t blen,
175 int (*f_rng)(void *, unsigned char *, size_t),
176 void *p_rng )
177{
178 int ret;
179
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100180 if( ctx == NULL || ctx->grp.pbits == 0 )
181 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
182
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100183 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
184 != 0 )
185 return( ret );
186
187 return ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
188 olen, buf, blen );
189}
190
191/*
192 * Parse and import the client's public value
193 */
194int ecdh_read_public( ecdh_context *ctx,
195 const unsigned char *buf, size_t blen )
196{
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100197 if( ctx == NULL )
198 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
199
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100200 return ecp_tls_read_point( &ctx->grp, &ctx->Qp, &buf, blen );
201}
202
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100203/*
204 * Derive and export the shared secret
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{
211 int ret;
212
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100213 if( ctx == NULL )
214 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
215
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200216 if( ( ret = ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
217 f_rng, p_rng ) ) != 0 )
218 {
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100219 return( ret );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200220 }
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100221
Paul Bakker41c83d32013-03-20 14:39:14 +0100222 if( mpi_size( &ctx->z ) > blen )
223 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
224
225 *olen = ctx->grp.nbits / 8 + ( ( ctx->grp.nbits % 8 ) != 0 );
226 return mpi_write_binary( &ctx->z, buf, *olen );
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100227}
228
229
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100230#if defined(POLARSSL_SELF_TEST)
231
232/*
233 * Checkup routine
234 */
235int ecdh_self_test( int verbose )
236{
237 return( verbose++ );
238}
239
240#endif
241
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100242#endif /* defined(POLARSSL_ECDH_C) */