blob: b6f9b9d0e255fa16d66e087bcca3dae9b5965957 [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 Bakkeraec37cb2012-04-26 18:59:59 +000064 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000065 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000066 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000067 *
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 Bakkeraec37cb2012-04-26 18:59:59 +000072static int dhm_check_range( const mpi *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 Bakker7890e622014-04-17 12:42:41 +020078
79 MPI_CHK( mpi_lset( &L, 2 ) );
80 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +000081
Paul Bakkeraec37cb2012-04-26 18:59:59 +000082 if( mpi_cmp_mpi( param, &L ) >= 0 &&
83 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +000084 {
Paul Bakker345a6fe2011-02-28 21:20:02 +000085 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +000086 }
87
Paul Bakker7890e622014-04-17 12:42:41 +020088cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +000089 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +000090 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +000091}
92
93/*
Paul Bakker5121ce52009-01-03 21:22:43 +000094 * Parse the ServerKeyExchange parameters
95 */
96int dhm_read_params( dhm_context *ctx,
97 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000098 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000100 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102 memset( ctx, 0, sizeof( dhm_context ) );
103
104 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
105 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
106 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
107 return( ret );
108
Paul Bakker345a6fe2011-02-28 21:20:02 +0000109 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
110 return( ret );
111
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 ctx->len = mpi_size( &ctx->P );
113
114 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +0000115 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 return( 0 );
118}
119
120/*
121 * Setup and write the ServerKeyExchange parameters
122 */
123int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000124 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000125 int (*f_rng)(void *, unsigned char *, size_t),
126 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000128 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000129 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 unsigned char *p;
131
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000132 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
133 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000136 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000138 do
139 {
140 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000142 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker7890e622014-04-17 12:42:41 +0200143 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000144
145 if( count++ > 10 )
146 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
147 }
148 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Paul Bakkerff7fe672010-07-18 09:45:05 +0000150 /*
151 * Calculate GX = G^X mod P
152 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
154 &ctx->P , &ctx->RP ) );
155
Paul Bakker345a6fe2011-02-28 21:20:02 +0000156 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000157 return( ret );
158
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 /*
160 * export P, G, GX
161 */
162#define DHM_MPI_EXPORT(X,n) \
163 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
164 *p++ = (unsigned char)( n >> 8 ); \
165 *p++ = (unsigned char)( n ); p += n;
166
167 n1 = mpi_size( &ctx->P );
168 n2 = mpi_size( &ctx->G );
169 n3 = mpi_size( &ctx->GX );
170
171 p = output;
172 DHM_MPI_EXPORT( &ctx->P , n1 );
173 DHM_MPI_EXPORT( &ctx->G , n2 );
174 DHM_MPI_EXPORT( &ctx->GX, n3 );
175
176 *olen = p - output;
177
178 ctx->len = n1;
179
180cleanup:
181
182 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000183 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
185 return( 0 );
186}
187
188/*
189 * Import the peer's public value G^Y
190 */
191int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000192 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
194 int ret;
195
196 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000197 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000200 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 return( 0 );
203}
204
205/*
206 * Create own private value X and export G^X
207 */
208int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000209 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000210 int (*f_rng)(void *, unsigned char *, size_t),
211 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000212{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000213 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000216 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000218 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
219 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
220
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 /*
222 * generate X and calculate GX = G^X mod P
223 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000224 do
225 {
226 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000228 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker7890e622014-04-17 12:42:41 +0200229 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000230
231 if( count++ > 10 )
232 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
233 }
234 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
237 &ctx->P , &ctx->RP ) );
238
Paul Bakker345a6fe2011-02-28 21:20:02 +0000239 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
240 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000241
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
243
244cleanup:
245
246 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000247 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249 return( 0 );
250}
251
252/*
253 * Derive and export the shared secret (G^Y)^X mod P
254 */
255int dhm_calc_secret( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000256 unsigned char *output, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000257{
258 int ret;
259
260 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000261 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263 MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
264 &ctx->P, &ctx->RP ) );
265
Paul Bakker345a6fe2011-02-28 21:20:02 +0000266 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000267 return( ret );
268
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 *olen = mpi_size( &ctx->K );
270
271 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
272
273cleanup:
274
275 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000276 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278 return( 0 );
279}
280
281/*
282 * Free the components of a DHM key
283 */
284void dhm_free( dhm_context *ctx )
285{
Paul Bakker6c591fa2011-05-05 11:49:20 +0000286 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
287 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
288 mpi_free( &ctx->P );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289}
290
Paul Bakker40e46942009-01-03 21:51:57 +0000291#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293/*
294 * Checkup routine
295 */
296int dhm_self_test( int verbose )
297{
298 return( verbose++ );
299}
300
301#endif
302
303#endif