blob: 98c8e9f869e09fc663e1b9d26b074d8c52860860 [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 Bakker40ce79f2013-09-15 17:43:54 +020037#if defined(POLARSSL_PEM_C)
38#include "polarssl/pem.h"
39#endif
40
41#if defined(POLARSSL_ASN1_PARSE_C)
42#include "polarssl/asn1.h"
43#endif
44
45#if defined(POLARSSL_MEMORY_C)
46#include "polarssl/memory.h"
47#else
48#include <stdlib.h>
49#define polarssl_malloc malloc
50#define polarssl_free free
51#endif
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/*
54 * helper to validate the mpi size and import it
55 */
56static int dhm_read_bignum( mpi *X,
57 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000058 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000059{
60 int ret, n;
61
62 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000063 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65 n = ( (*p)[0] << 8 ) | (*p)[1];
66 (*p) += 2;
67
68 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000069 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000070
71 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000072 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74 (*p) += n;
75
76 return( 0 );
77}
78
79/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000080 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000081 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000082 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000083 *
84 * For more information on the attack, see:
85 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
86 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000087 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000088static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000089{
Paul Bakker345a6fe2011-02-28 21:20:02 +000090 mpi L, U;
91 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +000092
Paul Bakker6c591fa2011-05-05 11:49:20 +000093 mpi_init( &L ); mpi_init( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +000094 mpi_lset( &L, 2 );
95 mpi_sub_int( &U, P, 2 );
Paul Bakkerc47840e2011-02-20 16:37:30 +000096
Paul Bakkeraec37cb2012-04-26 18:59:59 +000097 if( mpi_cmp_mpi( param, &L ) >= 0 &&
98 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +000099 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000100 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000101 }
102
Paul Bakker6c591fa2011-05-05 11:49:20 +0000103 mpi_free( &L ); mpi_free( &U );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000104
Paul Bakker345a6fe2011-02-28 21:20:02 +0000105 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000106}
107
108/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 * Parse the ServerKeyExchange parameters
110 */
111int dhm_read_params( dhm_context *ctx,
112 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000114{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000115 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200117 dhm_free( ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
120 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
121 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
122 return( ret );
123
Paul Bakker345a6fe2011-02-28 21:20:02 +0000124 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
125 return( ret );
126
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 ctx->len = mpi_size( &ctx->P );
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 return( 0 );
130}
131
132/*
133 * Setup and write the ServerKeyExchange parameters
134 */
135int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000136 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000137 int (*f_rng)(void *, unsigned char *, size_t),
138 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000140 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000141 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 unsigned char *p;
143
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000144 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
145 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
146
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000148 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000150 do
151 {
152 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000154 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
155 mpi_shift_r( &ctx->X, 1 );
156
157 if( count++ > 10 )
158 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
159 }
160 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakkerff7fe672010-07-18 09:45:05 +0000162 /*
163 * Calculate GX = G^X mod P
164 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
166 &ctx->P , &ctx->RP ) );
167
Paul Bakker345a6fe2011-02-28 21:20:02 +0000168 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000169 return( ret );
170
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 /*
172 * export P, G, GX
173 */
174#define DHM_MPI_EXPORT(X,n) \
175 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
176 *p++ = (unsigned char)( n >> 8 ); \
177 *p++ = (unsigned char)( n ); p += n;
178
179 n1 = mpi_size( &ctx->P );
180 n2 = mpi_size( &ctx->G );
181 n3 = mpi_size( &ctx->GX );
182
183 p = output;
184 DHM_MPI_EXPORT( &ctx->P , n1 );
185 DHM_MPI_EXPORT( &ctx->G , n2 );
186 DHM_MPI_EXPORT( &ctx->GX, n3 );
187
188 *olen = p - output;
189
190 ctx->len = n1;
191
192cleanup:
193
194 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000195 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197 return( 0 );
198}
199
200/*
201 * Import the peer's public value G^Y
202 */
203int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000204 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
206 int ret;
207
208 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214 return( 0 );
215}
216
217/*
218 * Create own private value X and export G^X
219 */
220int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000221 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000222 int (*f_rng)(void *, unsigned char *, size_t),
223 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000224{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000225 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000228 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000230 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
231 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
232
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 /*
234 * generate X and calculate GX = G^X mod P
235 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000236 do
237 {
238 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000240 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
241 mpi_shift_r( &ctx->X, 1 );
242
243 if( count++ > 10 )
244 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
245 }
246 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
249 &ctx->P , &ctx->RP ) );
250
Paul Bakker345a6fe2011-02-28 21:20:02 +0000251 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
252 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000253
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
255
256cleanup:
257
258 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000259 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261 return( 0 );
262}
263
264/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200265 * Use the blinding method and optimisation suggested in section 10 of:
266 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
267 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
268 * Berlin Heidelberg, 1996. p. 104-113.
269 */
270static int dhm_update_blinding( dhm_context *ctx,
271 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
272{
273 int ret, count;
274
275 /*
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200276 * If Vi is initialized, update it by squaring it
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200277 */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200278 if( ctx->Vi.p != NULL )
279 {
280 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
281 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
282 }
283 else
284 {
285 /* Vi = random( 2, P-1 ) */
286 count = 0;
287 do
288 {
289 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
290
291 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
292 mpi_shift_r( &ctx->Vi, 1 );
293
294 if( count++ > 10 )
295 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
296 }
297 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
298 }
299
300 /*
301 * If X did not change, update Vf by squaring it too
302 */
303 if( mpi_cmp_mpi( &ctx->X, &ctx->_X ) == 0 )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200304 {
305 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200306 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200307 return( 0 );
308 }
309
310 /*
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200311 * Otherwise, compute Vf from scratch
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200312 */
313
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200314 /* Vf = Vi^-X mod P */
315 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
316 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
317
318 /* Remember secret associated with Vi and Vf */
319 MPI_CHK( mpi_copy( &ctx->_X, &ctx->X ) );;
320
321cleanup:
322 return( ret );
323}
324
325/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 * Derive and export the shared secret (G^Y)^X mod P
327 */
328int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200329 unsigned char *output, size_t *olen,
330 int (*f_rng)(void *, unsigned char *, size_t),
331 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000332{
333 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200334 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200335
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000337 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
Paul Bakker345a6fe2011-02-28 21:20:02 +0000339 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000340 return( ret );
341
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200342 mpi_init( &GYb );
343
344 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200345 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200346 {
347 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
348 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
349 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
350 }
351 else
352 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
353
354 /* Do modular exponentiation */
355 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
356 &ctx->P, &ctx->RP ) );
357
358 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200359 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200360 {
361 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
362 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
363 }
364
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 *olen = mpi_size( &ctx->K );
366
367 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
368
369cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200370 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
372 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000373 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000374
375 return( 0 );
376}
377
378/*
379 * Free the components of a DHM key
380 */
381void dhm_free( dhm_context *ctx )
382{
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200383 mpi_free( &ctx->_X); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000384 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
385 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
386 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200387
388 memset( ctx, 0, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389}
390
Paul Bakker40ce79f2013-09-15 17:43:54 +0200391#if defined(POLARSSL_ASN1_PARSE_C)
392/*
393 * Parse DHM parameters
394 */
395int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
396{
397 int ret;
398 size_t len;
399 unsigned char *p, *end;
400#if defined(POLARSSL_PEM_C)
401 pem_context pem;
402
403 pem_init( &pem );
404 memset( dhm, 0, sizeof( dhm_context ) );
405
406 ret = pem_read_buffer( &pem,
407 "-----BEGIN DH PARAMETERS-----",
408 "-----END DH PARAMETERS-----",
409 dhmin, NULL, 0, &dhminlen );
410
411 if( ret == 0 )
412 {
413 /*
414 * Was PEM encoded
415 */
416 dhminlen = pem.buflen;
417 }
418 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
419 goto exit;
420
421 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
422#else
423 p = (unsigned char *) dhmin;
424#endif
425 end = p + dhminlen;
426
427 /*
428 * DHParams ::= SEQUENCE {
429 * prime INTEGER, -- P
430 * generator INTEGER, -- g
431 * }
432 */
433 if( ( ret = asn1_get_tag( &p, end, &len,
434 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
435 {
436 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
437 goto exit;
438 }
439
440 end = p + len;
441
442 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
443 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
444 {
445 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
446 goto exit;
447 }
448
449 if( p != end )
450 {
451 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
452 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
453 goto exit;
454 }
455
456 ret = 0;
457
458exit:
459#if defined(POLARSSL_PEM_C)
460 pem_free( &pem );
461#endif
462 if( ret != 0 )
463 dhm_free( dhm );
464
465 return( ret );
466}
467
468#if defined(POLARSSL_FS_IO)
469/*
470 * Load all data from a file into a given buffer.
471 */
472static int load_file( const char *path, unsigned char **buf, size_t *n )
473{
474 FILE *f;
475 long size;
476
477 if( ( f = fopen( path, "rb" ) ) == NULL )
478 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
479
480 fseek( f, 0, SEEK_END );
481 if( ( size = ftell( f ) ) == -1 )
482 {
483 fclose( f );
484 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
485 }
486 fseek( f, 0, SEEK_SET );
487
488 *n = (size_t) size;
489
490 if( *n + 1 == 0 ||
491 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
492 {
493 fclose( f );
494 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
495 }
496
497 if( fread( *buf, 1, *n, f ) != *n )
498 {
499 fclose( f );
500 polarssl_free( *buf );
501 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
502 }
503
504 fclose( f );
505
506 (*buf)[*n] = '\0';
507
508 return( 0 );
509}
510
511/*
512 * Load and parse DHM parameters
513 */
514int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
515{
516 int ret;
517 size_t n;
518 unsigned char *buf;
519
520 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
521 return( ret );
522
523 ret = dhm_parse_dhm( dhm, buf, n );
524
525 memset( buf, 0, n + 1 );
526 polarssl_free( buf );
527
528 return( ret );
529}
530#endif /* POLARSSL_FS_IO */
531#endif /* POLARSSL_ASN1_PARSE_C */
532
Paul Bakker40e46942009-01-03 21:51:57 +0000533#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
Paul Bakker40ce79f2013-09-15 17:43:54 +0200535#include "polarssl/certs.h"
536
Paul Bakker5121ce52009-01-03 21:22:43 +0000537/*
538 * Checkup routine
539 */
540int dhm_self_test( int verbose )
541{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200542#if defined(POLARSSL_CERTS_C)
543 int ret;
544 dhm_context dhm;
545
546 if( verbose != 0 )
547 printf( " DHM parameter load: " );
548
549 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
550 strlen( test_dhm_params ) ) ) != 0 )
551 {
552 if( verbose != 0 )
553 printf( "failed\n" );
554
555 return( ret );
556 }
557
558 if( verbose != 0 )
559 printf( "passed\n\n" );
560
561 dhm_free( &dhm );
562
563 return( 0 );
564#else
565 ((void) verbose);
566 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
567#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000568}
569
570#endif
571
572#endif