blob: 97d6c7a677327082ed7b505ec6b3e5138cb56924 [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 Bakker312da332014-06-13 17:20:13 +020037/* Implementation that should never be optimized out by the compiler */
38static void polarssl_zeroize( void *v, size_t n ) {
39 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
40}
41
Paul Bakker5121ce52009-01-03 21:22:43 +000042/*
43 * helper to validate the mpi size and import it
44 */
45static int dhm_read_bignum( mpi *X,
46 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000047 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000048{
49 int ret, n;
50
51 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000052 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54 n = ( (*p)[0] << 8 ) | (*p)[1];
55 (*p) += 2;
56
57 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000058 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000061 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000062
63 (*p) += n;
64
65 return( 0 );
66}
67
68/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000069 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000070 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000071 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000072 *
73 * For more information on the attack, see:
74 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
75 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000076 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000077static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000078{
Paul Bakker345a6fe2011-02-28 21:20:02 +000079 mpi L, U;
80 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +000081
Paul Bakker6c591fa2011-05-05 11:49:20 +000082 mpi_init( &L ); mpi_init( &U );
Paul Bakker7890e622014-04-17 12:42:41 +020083
84 MPI_CHK( mpi_lset( &L, 2 ) );
85 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +000086
Paul Bakkeraec37cb2012-04-26 18:59:59 +000087 if( mpi_cmp_mpi( param, &L ) >= 0 &&
88 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +000089 {
Paul Bakker345a6fe2011-02-28 21:20:02 +000090 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +000091 }
92
Paul Bakker7890e622014-04-17 12:42:41 +020093cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +000095 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +000096}
97
98/*
Paul Bakker5121ce52009-01-03 21:22:43 +000099 * Parse the ServerKeyExchange parameters
100 */
101int dhm_read_params( dhm_context *ctx,
102 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000103 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000105 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 memset( ctx, 0, sizeof( dhm_context ) );
108
109 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
110 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
111 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
112 return( ret );
113
Paul Bakker345a6fe2011-02-28 21:20:02 +0000114 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
115 return( ret );
116
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 ctx->len = mpi_size( &ctx->P );
118
119 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +0000120 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 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 Bakkeraec37cb2012-04-26 18:59:59 +0000133 int ret, count = 0;
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
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000137 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
138 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000141 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000143 do
144 {
145 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000147 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker7890e622014-04-17 12:42:41 +0200148 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000149
150 if( count++ > 10 )
151 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
152 }
153 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Paul Bakkerff7fe672010-07-18 09:45:05 +0000155 /*
156 * Calculate GX = G^X mod P
157 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
159 &ctx->P , &ctx->RP ) );
160
Paul Bakker345a6fe2011-02-28 21:20:02 +0000161 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000162 return( ret );
163
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 /*
165 * export P, G, GX
166 */
167#define DHM_MPI_EXPORT(X,n) \
168 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
169 *p++ = (unsigned char)( n >> 8 ); \
170 *p++ = (unsigned char)( n ); p += n;
171
172 n1 = mpi_size( &ctx->P );
173 n2 = mpi_size( &ctx->G );
174 n3 = mpi_size( &ctx->GX );
175
176 p = output;
177 DHM_MPI_EXPORT( &ctx->P , n1 );
178 DHM_MPI_EXPORT( &ctx->G , n2 );
179 DHM_MPI_EXPORT( &ctx->GX, n3 );
180
181 *olen = p - output;
182
183 ctx->len = n1;
184
185cleanup:
186
187 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000188 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190 return( 0 );
191}
192
193/*
194 * Import the peer's public value G^Y
195 */
196int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000197 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198{
199 int ret;
200
201 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000202 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
204 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000205 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207 return( 0 );
208}
209
210/*
211 * Create own private value X and export G^X
212 */
213int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000214 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000215 int (*f_rng)(void *, unsigned char *, size_t),
216 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000217{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000218 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000221 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000223 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
224 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 /*
227 * generate X and calculate GX = G^X mod P
228 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000229 do
230 {
231 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000233 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker7890e622014-04-17 12:42:41 +0200234 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000235
236 if( count++ > 10 )
237 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
238 }
239 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
242 &ctx->P , &ctx->RP ) );
243
Paul Bakker345a6fe2011-02-28 21:20:02 +0000244 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
245 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000246
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
248
249cleanup:
250
251 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000252 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
254 return( 0 );
255}
256
257/*
258 * Derive and export the shared secret (G^Y)^X mod P
259 */
260int dhm_calc_secret( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000261 unsigned char *output, size_t *olen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000262{
263 int ret;
264
265 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000266 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268 MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
269 &ctx->P, &ctx->RP ) );
270
Paul Bakker345a6fe2011-02-28 21:20:02 +0000271 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000272 return( ret );
273
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 *olen = mpi_size( &ctx->K );
275
276 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
277
278cleanup:
279
280 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000281 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
283 return( 0 );
284}
285
286/*
287 * Free the components of a DHM key
288 */
289void dhm_free( dhm_context *ctx )
290{
Paul Bakker6c591fa2011-05-05 11:49:20 +0000291 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
292 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
293 mpi_free( &ctx->P );
Paul Bakker312da332014-06-13 17:20:13 +0200294
295 polarssl_zeroize( ctx, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296}
297
Paul Bakker40e46942009-01-03 21:51:57 +0000298#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300/*
301 * Checkup routine
302 */
303int dhm_self_test( int verbose )
304{
305 return( verbose++ );
306}
307
308#endif
309
310#endif