blob: e30ae3a7142895c863a657ac87313a154a218d58 [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,
53 const ecp_point *Q, const mpi *d )
54{
55 int ret;
56 ecp_point P;
57
58 ecp_point_init( &P );
59
60 /*
61 * Make sure Q is a valid pubkey before using it
62 */
63 MPI_CHK( ecp_check_pubkey( grp, Q ) );
64
65 MPI_CHK( ecp_mul( grp, &P, d, Q ) );
66
67 if( ecp_is_zero( &P ) )
68 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
69
70 MPI_CHK( mpi_copy( z, &P.X ) );
71
72cleanup:
73 ecp_point_free( &P );
74
75 return( ret );
76}
77
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010078/*
79 * Initialize context
80 */
81void ecdh_init( ecdh_context *ctx )
82{
83 ecp_group_init( &ctx->grp );
84 mpi_init ( &ctx->d );
85 ecp_point_init( &ctx->Q );
86 ecp_point_init( &ctx->Qp );
87 mpi_init ( &ctx->z );
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +010088 ctx->point_format = POLARSSL_ECP_PF_UNCOMPRESSED;
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010089}
90
Manuel Pégourié-Gonnard63533e42013-02-10 14:21:04 +010091/*
92 * Free context
93 */
94void ecdh_free( ecdh_context *ctx )
95{
96 if( ctx == NULL )
97 return;
98
99 ecp_group_free( &ctx->grp );
100 mpi_free ( &ctx->d );
101 ecp_point_free( &ctx->Q );
102 ecp_point_free( &ctx->Qp );
103 mpi_free ( &ctx->z );
104}
105
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100106/*
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100107 * Setup and write the ServerKeyExhange parameters (RFC 4492)
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100108 * struct {
109 * ECParameters curve_params;
110 * ECPoint public;
111 * } ServerECDHParams;
112 */
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100113int ecdh_make_params( ecdh_context *ctx, size_t *olen,
114 unsigned char *buf, size_t blen,
115 int (*f_rng)(void *, unsigned char *, size_t),
116 void *p_rng )
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100117{
118 int ret;
119 size_t grp_len, pt_len;
120
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100121 if( ctx == NULL || ctx->grp.pbits == 0 )
122 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
123
Manuel Pégourié-Gonnard13724762013-02-10 15:01:54 +0100124 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
125 != 0 )
126 return( ret );
127
128 if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
129 != 0 )
130 return( ret );
131
132 buf += grp_len;
133 blen -= grp_len;
134
135 if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
136 &pt_len, buf, blen ) ) != 0 )
137 return( ret );
138
139 *olen = grp_len + pt_len;
140 return 0;
141}
142
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100143/*
144 * Read the ServerKeyExhange parameters (RFC 4492)
145 * struct {
146 * ECParameters curve_params;
147 * ECPoint public;
148 * } ServerECDHParams;
149 */
150int ecdh_read_params( ecdh_context *ctx,
151 const unsigned char **buf, const unsigned char *end )
152{
153 int ret;
154
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100155 ecdh_init( ctx );
156
Manuel Pégourié-Gonnard854fbd72013-02-11 20:28:55 +0100157 if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
158 return( ret );
159
160 if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
161 != 0 )
162 return( ret );
163
164 return 0;
165}
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100166
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100167/*
168 * Setup and export the client public value
169 */
170int ecdh_make_public( ecdh_context *ctx, size_t *olen,
171 unsigned char *buf, size_t blen,
172 int (*f_rng)(void *, unsigned char *, size_t),
173 void *p_rng )
174{
175 int ret;
176
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100177 if( ctx == NULL || ctx->grp.pbits == 0 )
178 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
179
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100180 if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
181 != 0 )
182 return( ret );
183
184 return ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
185 olen, buf, blen );
186}
187
188/*
189 * Parse and import the client's public value
190 */
191int ecdh_read_public( ecdh_context *ctx,
192 const unsigned char *buf, size_t blen )
193{
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100194 if( ctx == NULL )
195 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
196
Manuel Pégourié-Gonnard5cceb412013-02-11 21:51:45 +0100197 return ecp_tls_read_point( &ctx->grp, &ctx->Qp, &buf, blen );
198}
199
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100200/*
201 * Derive and export the shared secret
202 */
203int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
204 unsigned char *buf, size_t blen )
205{
206 int ret;
207
Manuel Pégourié-Gonnardf35b7392013-02-11 22:12:39 +0100208 if( ctx == NULL )
209 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
210
Manuel Pégourié-Gonnard424fda52013-02-11 22:05:42 +0100211 if( ( ret = ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d ) )
212 != 0 )
213 return( ret );
214
215 *olen = mpi_size( &ctx->z );
216 return mpi_write_binary( &ctx->z, buf, blen );
217}
218
219
Manuel Pégourié-Gonnard0bad5c22013-01-26 15:30:46 +0100220#if defined(POLARSSL_SELF_TEST)
221
222/*
223 * Checkup routine
224 */
225int ecdh_self_test( int verbose )
226{
227 return( verbose++ );
228}
229
230#endif
231
Manuel Pégourié-Gonnard2aea1412013-01-26 16:33:44 +0100232#endif /* defined(POLARSSL_ECDH_C) */