blob: 9b99a2b17078ddae2eaaac83249e27f24d57f00f [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
37#include <string.h>
38
39/*
40 * helper to validate the mpi size and import it
41 */
42static int dhm_read_bignum( mpi *X,
43 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000044 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000045{
46 int ret, n;
47
48 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000049 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51 n = ( (*p)[0] << 8 ) | (*p)[1];
52 (*p) += 2;
53
54 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000055 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000056
57 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +000058 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60 (*p) += n;
61
62 return( 0 );
63}
64
65/*
Paul Bakkerd741cf42011-02-28 21:10:41 +000066 * Verify sanity of public parameter with regards to P
67 *
68 * Public parameter should be: 2 <= public_param <= P - 2
69 *
70 * For more information on the attack, see:
71 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
72 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakker02710262011-02-22 16:26:47 +000073 */
Paul Bakkerd741cf42011-02-28 21:10:41 +000074static int dhm_check_range( const mpi *public_param, const mpi *P )
Paul Bakker02710262011-02-22 16:26:47 +000075{
Paul Bakkerd741cf42011-02-28 21:10:41 +000076 mpi L, U;
77 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakker02710262011-02-22 16:26:47 +000078
Paul Bakkerd741cf42011-02-28 21:10:41 +000079 mpi_init( &L, &U, NULL );
80 mpi_lset( &L, 2 );
81 mpi_sub_int( &U, P, 2 );
Paul Bakker02710262011-02-22 16:26:47 +000082
Paul Bakkerd741cf42011-02-28 21:10:41 +000083 if( mpi_cmp_mpi( public_param, &L ) >= 0 &&
84 mpi_cmp_mpi( public_param, &U ) <= 0 )
Paul Bakker02710262011-02-22 16:26:47 +000085 {
Paul Bakkerd741cf42011-02-28 21:10:41 +000086 ret = 0;
Paul Bakker02710262011-02-22 16:26:47 +000087 }
88
Paul Bakkerd741cf42011-02-28 21:10:41 +000089 mpi_free( &L, &U, NULL );
Paul Bakker02710262011-02-22 16:26:47 +000090
Paul Bakkerd741cf42011-02-28 21:10:41 +000091 return( ret );
Paul Bakker02710262011-02-22 16:26:47 +000092}
93
94/*
Paul Bakker5121ce52009-01-03 21:22:43 +000095 * Parse the ServerKeyExchange parameters
96 */
97int dhm_read_params( dhm_context *ctx,
98 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000099 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000100{
101 int ret, n;
102
103 memset( ctx, 0, sizeof( dhm_context ) );
104
105 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
106 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
107 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
108 return( ret );
109
Paul Bakkerd741cf42011-02-28 21:10:41 +0000110 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
111 return( ret );
112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 ctx->len = mpi_size( &ctx->P );
114
115 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +0000116 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118 n = ( (*p)[0] << 8 ) | (*p)[1];
119 (*p) += 2;
120
121 if( end != *p + n )
Paul Bakker40e46942009-01-03 21:51:57 +0000122 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124 return( 0 );
125}
126
127/*
128 * Setup and write the ServerKeyExchange parameters
129 */
130int dhm_make_params( dhm_context *ctx, int x_size,
131 unsigned char *output, int *olen,
132 int (*f_rng)(void *), void *p_rng )
133{
134 int i, ret, n, n1, n2, n3;
135 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 Bakker02710262011-02-22 16:26:47 +0000140 n = x_size / sizeof( t_int ) + 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 MPI_CHK( mpi_grow( &ctx->X, n ) );
142 MPI_CHK( mpi_lset( &ctx->X, 0 ) );
143
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 p = (unsigned char *) ctx->X.p;
Paul Bakker02710262011-02-22 16:26:47 +0000145 for( i = 0; i < x_size; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 *p++ = (unsigned char) f_rng( p_rng );
147
148 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
149 mpi_shift_r( &ctx->X, 1 );
150
Paul Bakkerff7fe672010-07-18 09:45:05 +0000151 /*
152 * Calculate GX = G^X mod P
153 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
155 &ctx->P , &ctx->RP ) );
156
Paul Bakkerd741cf42011-02-28 21:10:41 +0000157 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakker02710262011-02-22 16:26:47 +0000158 return( ret );
159
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 /*
161 * export P, G, GX
162 */
163#define DHM_MPI_EXPORT(X,n) \
164 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
165 *p++ = (unsigned char)( n >> 8 ); \
166 *p++ = (unsigned char)( n ); p += n;
167
168 n1 = mpi_size( &ctx->P );
169 n2 = mpi_size( &ctx->G );
170 n3 = mpi_size( &ctx->GX );
171
172 p = output;
173 DHM_MPI_EXPORT( &ctx->P , n1 );
174 DHM_MPI_EXPORT( &ctx->G , n2 );
175 DHM_MPI_EXPORT( &ctx->GX, n3 );
176
177 *olen = p - output;
178
179 ctx->len = n1;
180
181cleanup:
182
183 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000184 return( ret | POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 return( 0 );
187}
188
189/*
190 * Import the peer's public value G^Y
191 */
192int dhm_read_public( dhm_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000193 const unsigned char *input, int ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000194{
195 int ret;
196
197 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000198 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000201 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 return( 0 );
204}
205
206/*
207 * Create own private value X and export G^X
208 */
209int dhm_make_public( dhm_context *ctx, int x_size,
210 unsigned char *output, int olen,
211 int (*f_rng)(void *), void *p_rng )
212{
213 int ret, i, n;
214 unsigned char *p;
215
216 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000217 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219 /*
220 * generate X and calculate GX = G^X mod P
221 */
Paul Bakker02710262011-02-22 16:26:47 +0000222 n = x_size / sizeof( t_int ) + 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 MPI_CHK( mpi_grow( &ctx->X, n ) );
224 MPI_CHK( mpi_lset( &ctx->X, 0 ) );
225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 p = (unsigned char *) ctx->X.p;
Paul Bakker02710262011-02-22 16:26:47 +0000227 for( i = 0; i < x_size; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 *p++ = (unsigned char) f_rng( p_rng );
229
230 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
231 mpi_shift_r( &ctx->X, 1 );
232
233 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
234 &ctx->P , &ctx->RP ) );
235
Paul Bakkerd741cf42011-02-28 21:10:41 +0000236 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
237 return( ret );
Paul Bakker02710262011-02-22 16:26:47 +0000238
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
240
241cleanup:
242
243 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000244 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
246 return( 0 );
247}
248
249/*
250 * Derive and export the shared secret (G^Y)^X mod P
251 */
252int dhm_calc_secret( dhm_context *ctx,
253 unsigned char *output, int *olen )
254{
255 int ret;
256
257 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000258 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
260 MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
261 &ctx->P, &ctx->RP ) );
262
Paul Bakkerd741cf42011-02-28 21:10:41 +0000263 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakker02710262011-02-22 16:26:47 +0000264 return( ret );
265
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 *olen = mpi_size( &ctx->K );
267
268 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
269
270cleanup:
271
272 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000273 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275 return( 0 );
276}
277
278/*
279 * Free the components of a DHM key
280 */
281void dhm_free( dhm_context *ctx )
282{
283 mpi_free( &ctx->RP, &ctx->K, &ctx->GY,
284 &ctx->GX, &ctx->X, &ctx->G,
285 &ctx->P, NULL );
286}
287
Paul Bakker40e46942009-01-03 21:51:57 +0000288#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290/*
291 * Checkup routine
292 */
293int dhm_self_test( int verbose )
294{
295 return( verbose++ );
296}
297
298#endif
299
300#endif