blob: eb77871f5a68f4a298e5b3918964531ceadc834f [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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 * Reference:
27 *
28 * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
29 */
30
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Paul Bakker40e46942009-01-03 21:51:57 +000033#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker5121ce52009-01-03 21:22:43 +000037/*
38 * helper to validate the mpi size and import it
39 */
40static int dhm_read_bignum( mpi *X,
41 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000042 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000043{
44 int ret, n;
45
46 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000047 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49 n = ( (*p)[0] << 8 ) | (*p)[1];
50 (*p) += 2;
51
52 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000053 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000054
55 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000056 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000057
58 (*p) += n;
59
60 return( 0 );
61}
62
63/*
Paul Bakker345a6fe2011-02-28 21:20:02 +000064 * Verify sanity of public parameter with regards to P
65 *
66 * Public parameter should be: 2 <= public_param <= P - 2
67 *
68 * For more information on the attack, see:
69 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
70 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000071 */
Paul Bakker345a6fe2011-02-28 21:20:02 +000072static int dhm_check_range( const mpi *public_param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000073{
Paul Bakker345a6fe2011-02-28 21:20:02 +000074 mpi L, U;
75 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +000076
Paul Bakker6c591fa2011-05-05 11:49:20 +000077 mpi_init( &L ); mpi_init( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +000078 mpi_lset( &L, 2 );
79 mpi_sub_int( &U, P, 2 );
Paul Bakkerc47840e2011-02-20 16:37:30 +000080
Paul Bakker345a6fe2011-02-28 21:20:02 +000081 if( mpi_cmp_mpi( public_param, &L ) >= 0 &&
82 mpi_cmp_mpi( public_param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +000083 {
Paul Bakker345a6fe2011-02-28 21:20:02 +000084 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +000085 }
86
Paul Bakker6c591fa2011-05-05 11:49:20 +000087 mpi_free( &L ); mpi_free( &U );
Paul Bakkerc47840e2011-02-20 16:37:30 +000088
Paul Bakker345a6fe2011-02-28 21:20:02 +000089 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +000090}
91
92/*
Paul Bakker5121ce52009-01-03 21:22:43 +000093 * Parse the ServerKeyExchange parameters
94 */
95int dhm_read_params( dhm_context *ctx,
96 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000097 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000098{
99 int ret, n;
100
101 memset( ctx, 0, sizeof( dhm_context ) );
102
103 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
104 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
105 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
106 return( ret );
107
Paul Bakker345a6fe2011-02-28 21:20:02 +0000108 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
109 return( ret );
110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 ctx->len = mpi_size( &ctx->P );
112
113 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +0000114 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 n = ( (*p)[0] << 8 ) | (*p)[1];
117 (*p) += 2;
118
119 if( end != *p + n )
Paul Bakker40e46942009-01-03 21:51:57 +0000120 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122 return( 0 );
123}
124
125/*
126 * Setup and write the ServerKeyExchange parameters
127 */
128int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000129 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000130 int (*f_rng)(void *, unsigned char *, size_t),
131 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132{
Paul Bakkere2f8ff62012-04-20 13:33:14 +0000133 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000134 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 unsigned char *p;
136
137 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000138 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Paul Bakkere2f8ff62012-04-20 13:33:14 +0000140 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
143 mpi_shift_r( &ctx->X, 1 );
144
Paul Bakkerff7fe672010-07-18 09:45:05 +0000145 /*
146 * Calculate GX = G^X mod P
147 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
149 &ctx->P , &ctx->RP ) );
150
Paul Bakker345a6fe2011-02-28 21:20:02 +0000151 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000152 return( ret );
153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 /*
155 * export P, G, GX
156 */
157#define DHM_MPI_EXPORT(X,n) \
158 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
159 *p++ = (unsigned char)( n >> 8 ); \
160 *p++ = (unsigned char)( n ); p += n;
161
162 n1 = mpi_size( &ctx->P );
163 n2 = mpi_size( &ctx->G );
164 n3 = mpi_size( &ctx->GX );
165
166 p = output;
167 DHM_MPI_EXPORT( &ctx->P , n1 );
168 DHM_MPI_EXPORT( &ctx->G , n2 );
169 DHM_MPI_EXPORT( &ctx->GX, n3 );
170
171 *olen = p - output;
172
173 ctx->len = n1;
174
175cleanup:
176
177 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000178 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180 return( 0 );
181}
182
183/*
184 * Import the peer's public value G^Y
185 */
186int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000187 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188{
189 int ret;
190
191 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000192 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 return( 0 );
198}
199
200/*
201 * Create own private value X and export G^X
202 */
203int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000204 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000205 int (*f_rng)(void *, unsigned char *, size_t),
206 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
Paul Bakkere2f8ff62012-04-20 13:33:14 +0000208 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000211 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213 /*
214 * generate X and calculate GX = G^X mod P
215 */
Paul Bakkere2f8ff62012-04-20 13:33:14 +0000216 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
218 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
219 mpi_shift_r( &ctx->X, 1 );
220
221 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
222 &ctx->P , &ctx->RP ) );
223
Paul Bakker345a6fe2011-02-28 21:20:02 +0000224 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
225 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000226
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
228
229cleanup:
230
231 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000232 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234 return( 0 );
235}
236
237/*
238 * Derive and export the shared secret (G^Y)^X mod P
239 */
240int dhm_calc_secret( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000241 unsigned char *output, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242{
243 int ret;
244
245 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000246 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
249 &ctx->P, &ctx->RP ) );
250
Paul Bakker345a6fe2011-02-28 21:20:02 +0000251 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000252 return( ret );
253
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 *olen = mpi_size( &ctx->K );
255
256 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
257
258cleanup:
259
260 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000261 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263 return( 0 );
264}
265
266/*
267 * Free the components of a DHM key
268 */
269void dhm_free( dhm_context *ctx )
270{
Paul Bakker6c591fa2011-05-05 11:49:20 +0000271 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
272 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
273 mpi_free( &ctx->P );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274}
275
Paul Bakker40e46942009-01-03 21:51:57 +0000276#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278/*
279 * Checkup routine
280 */
281int dhm_self_test( int verbose )
282{
283 return( verbose++ );
284}
285
286#endif
287
288#endif