blob: b587fa6f079b490b4ca00b8959a3dfe89578654f [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 Bakkerc47840e2011-02-20 16:37:30 +000066 * Verify sanity of public value with regards to P
67 */
68static int dhm_verifypub( const mpi *P, const mpi *pub_value )
69{
70 mpi X;
71
72 mpi_init( &X, NULL );
73 mpi_lset( &X, 1 );
74
75 /* Check G^Y or G^X is valid */
76 if( mpi_cmp_mpi( pub_value, &X ) <= 0 )
77 {
78 mpi_free( &X, NULL );
79 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
80 }
81
82 /* Reset: x = P - 1 */
83 mpi_sub_int( &X, P, 1 );
84
85 if( mpi_cmp_mpi( pub_value, &X ) >= 0 )
86 {
87 mpi_free( &X, NULL );
88 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
89 }
90
91 mpi_free( &X, NULL );
92
93 return( 0 );
94}
95
96/*
Paul Bakker5121ce52009-01-03 21:22:43 +000097 * Parse the ServerKeyExchange parameters
98 */
99int dhm_read_params( dhm_context *ctx,
100 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
103 int ret, n;
104
105 memset( ctx, 0, sizeof( dhm_context ) );
106
107 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
108 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
109 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
110 return( ret );
111
112 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
117 n = ( (*p)[0] << 8 ) | (*p)[1];
118 (*p) += 2;
119
120 if( end != *p + n )
Paul Bakker40e46942009-01-03 21:51:57 +0000121 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Paul Bakkerc47840e2011-02-20 16:37:30 +0000123 if( ( ret = dhm_verifypub( &ctx->P, &ctx->GY ) ) != 0 )
124 return( ret );
125
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 return( 0 );
127}
128
129/*
130 * Setup and write the ServerKeyExchange parameters
131 */
132int dhm_make_params( dhm_context *ctx, int x_size,
133 unsigned char *output, int *olen,
134 int (*f_rng)(void *), void *p_rng )
135{
136 int i, ret, n, n1, n2, n3;
137 unsigned char *p;
138
139 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000140 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Paul Bakkerc47840e2011-02-20 16:37:30 +0000142 n = x_size / sizeof( t_int ) + 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 MPI_CHK( mpi_grow( &ctx->X, n ) );
144 MPI_CHK( mpi_lset( &ctx->X, 0 ) );
145
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 p = (unsigned char *) ctx->X.p;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000147 for( i = 0; i < x_size; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 *p++ = (unsigned char) f_rng( p_rng );
149
150 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
151 mpi_shift_r( &ctx->X, 1 );
152
Paul Bakkerff7fe672010-07-18 09:45:05 +0000153 /*
154 * Calculate GX = G^X mod P
155 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
157 &ctx->P , &ctx->RP ) );
158
Paul Bakkerc47840e2011-02-20 16:37:30 +0000159 if( ( ret = dhm_verifypub( &ctx->P, &ctx->GX ) ) != 0 )
160 return( ret );
161
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 /*
163 * export P, G, GX
164 */
165#define DHM_MPI_EXPORT(X,n) \
166 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
167 *p++ = (unsigned char)( n >> 8 ); \
168 *p++ = (unsigned char)( n ); p += n;
169
170 n1 = mpi_size( &ctx->P );
171 n2 = mpi_size( &ctx->G );
172 n3 = mpi_size( &ctx->GX );
173
174 p = output;
175 DHM_MPI_EXPORT( &ctx->P , n1 );
176 DHM_MPI_EXPORT( &ctx->G , n2 );
177 DHM_MPI_EXPORT( &ctx->GX, n3 );
178
179 *olen = p - output;
180
181 ctx->len = n1;
182
183cleanup:
184
185 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000186 return( ret | POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
188 return( 0 );
189}
190
191/*
192 * Import the peer's public value G^Y
193 */
194int dhm_read_public( dhm_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000195 const unsigned char *input, int ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000196{
197 int ret;
198
199 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000200 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000203 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205 return( 0 );
206}
207
208/*
209 * Create own private value X and export G^X
210 */
211int dhm_make_public( dhm_context *ctx, int x_size,
212 unsigned char *output, int olen,
213 int (*f_rng)(void *), void *p_rng )
214{
215 int ret, i, n;
216 unsigned char *p;
217
218 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000219 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 /*
222 * generate X and calculate GX = G^X mod P
223 */
Paul Bakkerc47840e2011-02-20 16:37:30 +0000224 n = x_size / sizeof( t_int ) + 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 MPI_CHK( mpi_grow( &ctx->X, n ) );
226 MPI_CHK( mpi_lset( &ctx->X, 0 ) );
227
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 p = (unsigned char *) ctx->X.p;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000229 for( i = 0; i < x_size; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *p++ = (unsigned char) f_rng( p_rng );
231
232 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
233 mpi_shift_r( &ctx->X, 1 );
234
235 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
236 &ctx->P , &ctx->RP ) );
237
Paul Bakkerc47840e2011-02-20 16:37:30 +0000238 if( dhm_verifypub( &ctx->P, &ctx->GX ) != 0 )
239 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
240
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
242
243cleanup:
244
245 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000246 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 return( 0 );
249}
250
251/*
252 * Derive and export the shared secret (G^Y)^X mod P
253 */
254int dhm_calc_secret( dhm_context *ctx,
255 unsigned char *output, int *olen )
256{
257 int ret;
258
259 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000260 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262 MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
263 &ctx->P, &ctx->RP ) );
264
Paul Bakkerc47840e2011-02-20 16:37:30 +0000265 if( ( ret = dhm_verifypub( &ctx->P, &ctx->GY ) ) != 0 )
266 return( ret );
267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 *olen = mpi_size( &ctx->K );
269
270 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
271
272cleanup:
273
274 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000275 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276
277 return( 0 );
278}
279
280/*
281 * Free the components of a DHM key
282 */
283void dhm_free( dhm_context *ctx )
284{
285 mpi_free( &ctx->RP, &ctx->K, &ctx->GY,
286 &ctx->GX, &ctx->X, &ctx->G,
287 &ctx->P, NULL );
288}
289
Paul Bakker40e46942009-01-03 21:51:57 +0000290#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292/*
293 * Checkup routine
294 */
295int dhm_self_test( int verbose )
296{
297 return( verbose++ );
298}
299
300#endif
301
302#endif