blob: 8bc9f0e341db67882dfb57d4334870fd625ab59c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, 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 Bakkercff68422013-09-15 20:43:33 +020037#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +020038#include "polarssl/pem.h"
39#endif
40
41#if defined(POLARSSL_ASN1_PARSE_C)
42#include "polarssl/asn1.h"
43#endif
44
Paul Bakker7dc4c442014-02-01 22:50:26 +010045#if defined(POLARSSL_PLATFORM_C)
46#include "polarssl/platform.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020047#else
48#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#define polarssl_printf printf
Paul Bakker40ce79f2013-09-15 17:43:54 +020050#define polarssl_malloc malloc
51#define polarssl_free free
52#endif
53
Paul Bakker5121ce52009-01-03 21:22:43 +000054/*
55 * helper to validate the mpi size and import it
56 */
57static int dhm_read_bignum( mpi *X,
58 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000059 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000060{
61 int ret, n;
62
63 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000064 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66 n = ( (*p)[0] << 8 ) | (*p)[1];
67 (*p) += 2;
68
69 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000070 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000073 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75 (*p) += n;
76
77 return( 0 );
78}
79
80/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000081 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000082 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000083 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000084 *
85 * For more information on the attack, see:
86 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
87 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000088 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000089static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000090{
Paul Bakker345a6fe2011-02-28 21:20:02 +000091 mpi L, U;
92 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +000093
Paul Bakker6c591fa2011-05-05 11:49:20 +000094 mpi_init( &L ); mpi_init( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +000095 mpi_lset( &L, 2 );
96 mpi_sub_int( &U, P, 2 );
Paul Bakkerc47840e2011-02-20 16:37:30 +000097
Paul Bakkeraec37cb2012-04-26 18:59:59 +000098 if( mpi_cmp_mpi( param, &L ) >= 0 &&
99 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000100 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000101 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000102 }
103
Paul Bakker6c591fa2011-05-05 11:49:20 +0000104 mpi_free( &L ); mpi_free( &U );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000105
Paul Bakker345a6fe2011-02-28 21:20:02 +0000106 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000107}
108
109/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 * Parse the ServerKeyExchange parameters
111 */
112int dhm_read_params( dhm_context *ctx,
113 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000114 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000116 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200118 dhm_free( ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
121 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
122 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
123 return( ret );
124
Paul Bakker345a6fe2011-02-28 21:20:02 +0000125 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
126 return( ret );
127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 ctx->len = mpi_size( &ctx->P );
129
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 return( 0 );
131}
132
133/*
134 * Setup and write the ServerKeyExchange parameters
135 */
136int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000137 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000138 int (*f_rng)(void *, unsigned char *, size_t),
139 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000141 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000142 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 unsigned char *p;
144
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000145 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
146 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
147
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000149 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000151 do
152 {
153 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000155 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
156 mpi_shift_r( &ctx->X, 1 );
157
158 if( count++ > 10 )
159 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
160 }
161 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Paul Bakkerff7fe672010-07-18 09:45:05 +0000163 /*
164 * Calculate GX = G^X mod P
165 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
167 &ctx->P , &ctx->RP ) );
168
Paul Bakker345a6fe2011-02-28 21:20:02 +0000169 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000170 return( ret );
171
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 /*
173 * export P, G, GX
174 */
175#define DHM_MPI_EXPORT(X,n) \
176 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
177 *p++ = (unsigned char)( n >> 8 ); \
178 *p++ = (unsigned char)( n ); p += n;
179
180 n1 = mpi_size( &ctx->P );
181 n2 = mpi_size( &ctx->G );
182 n3 = mpi_size( &ctx->GX );
183
184 p = output;
185 DHM_MPI_EXPORT( &ctx->P , n1 );
186 DHM_MPI_EXPORT( &ctx->G , n2 );
187 DHM_MPI_EXPORT( &ctx->GX, n3 );
188
189 *olen = p - output;
190
191 ctx->len = n1;
192
193cleanup:
194
195 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000196 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198 return( 0 );
199}
200
201/*
202 * Import the peer's public value G^Y
203 */
204int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000205 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206{
207 int ret;
208
209 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000210 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000213 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215 return( 0 );
216}
217
218/*
219 * Create own private value X and export G^X
220 */
221int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000222 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000223 int (*f_rng)(void *, unsigned char *, size_t),
224 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000226 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000229 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000231 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
232 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
233
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 /*
235 * generate X and calculate GX = G^X mod P
236 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000237 do
238 {
239 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000241 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
242 mpi_shift_r( &ctx->X, 1 );
243
244 if( count++ > 10 )
245 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
246 }
247 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
250 &ctx->P , &ctx->RP ) );
251
Paul Bakker345a6fe2011-02-28 21:20:02 +0000252 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
253 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000254
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
256
257cleanup:
258
259 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000260 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262 return( 0 );
263}
264
265/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200266 * Use the blinding method and optimisation suggested in section 10 of:
267 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
268 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
269 * Berlin Heidelberg, 1996. p. 104-113.
270 */
271static int dhm_update_blinding( dhm_context *ctx,
272 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
273{
274 int ret, count;
275
276 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200277 * Don't use any blinding the first time a particular X is used,
278 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200279 */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200280 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200281 {
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200282 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200283 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
284 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200285
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200286 return( 0 );
287 }
288
289 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200290 * Ok, we need blinding. Can we re-use existing values?
291 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200292 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200293 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
294 {
295 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
296 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
297
298 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
299 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
300
301 return( 0 );
302 }
303
304 /*
305 * We need to generate blinding values from scratch
306 */
307
308 /* Vi = random( 2, P-1 ) */
309 count = 0;
310 do
311 {
312 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
313
314 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
315 mpi_shift_r( &ctx->Vi, 1 );
316
317 if( count++ > 10 )
318 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
319 }
320 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200321
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200322 /* Vf = Vi^-X mod P */
323 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
324 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
325
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200326cleanup:
327 return( ret );
328}
329
330/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 * Derive and export the shared secret (G^Y)^X mod P
332 */
333int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200334 unsigned char *output, size_t *olen,
335 int (*f_rng)(void *, unsigned char *, size_t),
336 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000337{
338 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200339 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200340
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000342 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
Paul Bakker345a6fe2011-02-28 21:20:02 +0000344 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000345 return( ret );
346
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200347 mpi_init( &GYb );
348
349 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200350 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200351 {
352 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
353 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
354 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
355 }
356 else
357 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
358
359 /* Do modular exponentiation */
360 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
361 &ctx->P, &ctx->RP ) );
362
363 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200364 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200365 {
366 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
367 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
368 }
369
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 *olen = mpi_size( &ctx->K );
371
372 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
373
374cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200375 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
377 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000378 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380 return( 0 );
381}
382
383/*
384 * Free the components of a DHM key
385 */
386void dhm_free( dhm_context *ctx )
387{
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200388 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000389 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
390 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
391 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200392
393 memset( ctx, 0, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394}
395
Paul Bakker40ce79f2013-09-15 17:43:54 +0200396#if defined(POLARSSL_ASN1_PARSE_C)
397/*
398 * Parse DHM parameters
399 */
400int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
401{
402 int ret;
403 size_t len;
404 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200405#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200406 pem_context pem;
407
408 pem_init( &pem );
409 memset( dhm, 0, sizeof( dhm_context ) );
410
411 ret = pem_read_buffer( &pem,
412 "-----BEGIN DH PARAMETERS-----",
413 "-----END DH PARAMETERS-----",
414 dhmin, NULL, 0, &dhminlen );
415
416 if( ret == 0 )
417 {
418 /*
419 * Was PEM encoded
420 */
421 dhminlen = pem.buflen;
422 }
423 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
424 goto exit;
425
426 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
427#else
428 p = (unsigned char *) dhmin;
429#endif
430 end = p + dhminlen;
431
432 /*
433 * DHParams ::= SEQUENCE {
434 * prime INTEGER, -- P
435 * generator INTEGER, -- g
436 * }
437 */
438 if( ( ret = asn1_get_tag( &p, end, &len,
439 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
440 {
441 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
442 goto exit;
443 }
444
445 end = p + len;
446
447 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
448 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
449 {
450 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
451 goto exit;
452 }
453
454 if( p != end )
455 {
456 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
457 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
458 goto exit;
459 }
460
461 ret = 0;
462
463exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200464#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200465 pem_free( &pem );
466#endif
467 if( ret != 0 )
468 dhm_free( dhm );
469
470 return( ret );
471}
472
473#if defined(POLARSSL_FS_IO)
474/*
475 * Load all data from a file into a given buffer.
476 */
477static int load_file( const char *path, unsigned char **buf, size_t *n )
478{
479 FILE *f;
480 long size;
481
482 if( ( f = fopen( path, "rb" ) ) == NULL )
483 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
484
485 fseek( f, 0, SEEK_END );
486 if( ( size = ftell( f ) ) == -1 )
487 {
488 fclose( f );
489 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
490 }
491 fseek( f, 0, SEEK_SET );
492
493 *n = (size_t) size;
494
495 if( *n + 1 == 0 ||
496 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
497 {
498 fclose( f );
499 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
500 }
501
502 if( fread( *buf, 1, *n, f ) != *n )
503 {
504 fclose( f );
505 polarssl_free( *buf );
506 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
507 }
508
509 fclose( f );
510
511 (*buf)[*n] = '\0';
512
513 return( 0 );
514}
515
516/*
517 * Load and parse DHM parameters
518 */
519int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
520{
521 int ret;
522 size_t n;
523 unsigned char *buf;
524
525 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
526 return( ret );
527
528 ret = dhm_parse_dhm( dhm, buf, n );
529
530 memset( buf, 0, n + 1 );
531 polarssl_free( buf );
532
533 return( ret );
534}
535#endif /* POLARSSL_FS_IO */
536#endif /* POLARSSL_ASN1_PARSE_C */
537
Paul Bakker40e46942009-01-03 21:51:57 +0000538#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker40ce79f2013-09-15 17:43:54 +0200540#include "polarssl/certs.h"
541
Paul Bakker5121ce52009-01-03 21:22:43 +0000542/*
543 * Checkup routine
544 */
545int dhm_self_test( int verbose )
546{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200547#if defined(POLARSSL_CERTS_C)
548 int ret;
549 dhm_context dhm;
550
551 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100552 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200553
554 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
555 strlen( test_dhm_params ) ) ) != 0 )
556 {
557 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100558 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200559
560 return( ret );
561 }
562
563 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100564 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200565
566 dhm_free( &dhm );
567
568 return( 0 );
569#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100570 if( verbose != 0 )
571 polarssl_printf( " DHM parameter load: skipped\n" );
572
573 return( 0 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200574#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000575}
576
577#endif
578
579#endif