blob: 635e63e2abb39736d6222c050b9f54899fba7645 [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 Bakker3d8fb632014-04-17 12:42:41 +020095
96 MPI_CHK( mpi_lset( &L, 2 ) );
97 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +000098
Paul Bakkeraec37cb2012-04-26 18:59:59 +000099 if( mpi_cmp_mpi( param, &L ) >= 0 &&
100 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000101 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000102 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000103 }
104
Paul Bakker3d8fb632014-04-17 12:42:41 +0200105cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000106 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +0000107 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000108}
109
110/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 * Parse the ServerKeyExchange parameters
112 */
113int dhm_read_params( dhm_context *ctx,
114 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000117 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200119 dhm_free( ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
122 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
123 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
124 return( ret );
125
Paul Bakker345a6fe2011-02-28 21:20:02 +0000126 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
127 return( ret );
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 ctx->len = mpi_size( &ctx->P );
130
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 return( 0 );
132}
133
134/*
135 * Setup and write the ServerKeyExchange parameters
136 */
137int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000138 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000139 int (*f_rng)(void *, unsigned char *, size_t),
140 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000141{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000142 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000143 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 unsigned char *p;
145
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000146 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
147 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
148
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000150 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000152 do
153 {
154 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000156 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200157 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000158
159 if( count++ > 10 )
160 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
161 }
162 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakkerff7fe672010-07-18 09:45:05 +0000164 /*
165 * Calculate GX = G^X mod P
166 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
168 &ctx->P , &ctx->RP ) );
169
Paul Bakker345a6fe2011-02-28 21:20:02 +0000170 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000171 return( ret );
172
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 /*
174 * export P, G, GX
175 */
176#define DHM_MPI_EXPORT(X,n) \
177 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
178 *p++ = (unsigned char)( n >> 8 ); \
179 *p++ = (unsigned char)( n ); p += n;
180
181 n1 = mpi_size( &ctx->P );
182 n2 = mpi_size( &ctx->G );
183 n3 = mpi_size( &ctx->GX );
184
185 p = output;
186 DHM_MPI_EXPORT( &ctx->P , n1 );
187 DHM_MPI_EXPORT( &ctx->G , n2 );
188 DHM_MPI_EXPORT( &ctx->GX, n3 );
189
190 *olen = p - output;
191
192 ctx->len = n1;
193
194cleanup:
195
196 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000197 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
199 return( 0 );
200}
201
202/*
203 * Import the peer's public value G^Y
204 */
205int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000206 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
208 int ret;
209
210 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000211 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000214 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
216 return( 0 );
217}
218
219/*
220 * Create own private value X and export G^X
221 */
222int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000223 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000224 int (*f_rng)(void *, unsigned char *, size_t),
225 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000227 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000230 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000232 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
233 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
234
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 /*
236 * generate X and calculate GX = G^X mod P
237 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000238 do
239 {
240 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000242 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200243 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000244
245 if( count++ > 10 )
246 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
247 }
248 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
250 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
251 &ctx->P , &ctx->RP ) );
252
Paul Bakker345a6fe2011-02-28 21:20:02 +0000253 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
254 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000255
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
257
258cleanup:
259
260 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000261 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
263 return( 0 );
264}
265
266/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200267 * Use the blinding method and optimisation suggested in section 10 of:
268 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
269 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
270 * Berlin Heidelberg, 1996. p. 104-113.
271 */
272static int dhm_update_blinding( dhm_context *ctx,
273 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
274{
275 int ret, count;
276
277 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200278 * Don't use any blinding the first time a particular X is used,
279 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200280 */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200281 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200282 {
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200283 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200284 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
285 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200286
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200287 return( 0 );
288 }
289
290 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200291 * Ok, we need blinding. Can we re-use existing values?
292 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200293 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200294 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
295 {
296 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
297 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
298
299 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
300 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
301
302 return( 0 );
303 }
304
305 /*
306 * We need to generate blinding values from scratch
307 */
308
309 /* Vi = random( 2, P-1 ) */
310 count = 0;
311 do
312 {
313 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
314
315 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200316 MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200317
318 if( count++ > 10 )
319 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
320 }
321 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200322
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200323 /* Vf = Vi^-X mod P */
324 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
325 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
326
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200327cleanup:
328 return( ret );
329}
330
331/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 * Derive and export the shared secret (G^Y)^X mod P
333 */
334int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200335 unsigned char *output, size_t *olen,
336 int (*f_rng)(void *, unsigned char *, size_t),
337 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000338{
339 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200340 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200341
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000343 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Paul Bakker345a6fe2011-02-28 21:20:02 +0000345 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000346 return( ret );
347
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200348 mpi_init( &GYb );
349
350 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200351 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200352 {
353 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
354 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
355 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
356 }
357 else
358 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
359
360 /* Do modular exponentiation */
361 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
362 &ctx->P, &ctx->RP ) );
363
364 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200365 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200366 {
367 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
368 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
369 }
370
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 *olen = mpi_size( &ctx->K );
372
373 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
374
375cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200376 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000379 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381 return( 0 );
382}
383
384/*
385 * Free the components of a DHM key
386 */
387void dhm_free( dhm_context *ctx )
388{
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200389 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000390 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
391 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
392 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200393
394 memset( ctx, 0, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395}
396
Paul Bakker40ce79f2013-09-15 17:43:54 +0200397#if defined(POLARSSL_ASN1_PARSE_C)
398/*
399 * Parse DHM parameters
400 */
401int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
402{
403 int ret;
404 size_t len;
405 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200406#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200407 pem_context pem;
408
409 pem_init( &pem );
410 memset( dhm, 0, sizeof( dhm_context ) );
411
412 ret = pem_read_buffer( &pem,
413 "-----BEGIN DH PARAMETERS-----",
414 "-----END DH PARAMETERS-----",
415 dhmin, NULL, 0, &dhminlen );
416
417 if( ret == 0 )
418 {
419 /*
420 * Was PEM encoded
421 */
422 dhminlen = pem.buflen;
423 }
424 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
425 goto exit;
426
427 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
428#else
429 p = (unsigned char *) dhmin;
430#endif
431 end = p + dhminlen;
432
433 /*
434 * DHParams ::= SEQUENCE {
435 * prime INTEGER, -- P
436 * generator INTEGER, -- g
437 * }
438 */
439 if( ( ret = asn1_get_tag( &p, end, &len,
440 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
441 {
442 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
443 goto exit;
444 }
445
446 end = p + len;
447
448 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
449 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
450 {
451 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
452 goto exit;
453 }
454
455 if( p != end )
456 {
457 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
458 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
459 goto exit;
460 }
461
462 ret = 0;
463
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100464 dhm->len = mpi_size( &dhm->P );
465
Paul Bakker40ce79f2013-09-15 17:43:54 +0200466exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200467#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200468 pem_free( &pem );
469#endif
470 if( ret != 0 )
471 dhm_free( dhm );
472
473 return( ret );
474}
475
476#if defined(POLARSSL_FS_IO)
477/*
478 * Load all data from a file into a given buffer.
479 */
480static int load_file( const char *path, unsigned char **buf, size_t *n )
481{
482 FILE *f;
483 long size;
484
485 if( ( f = fopen( path, "rb" ) ) == NULL )
486 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
487
488 fseek( f, 0, SEEK_END );
489 if( ( size = ftell( f ) ) == -1 )
490 {
491 fclose( f );
492 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
493 }
494 fseek( f, 0, SEEK_SET );
495
496 *n = (size_t) size;
497
498 if( *n + 1 == 0 ||
499 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
500 {
501 fclose( f );
502 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
503 }
504
505 if( fread( *buf, 1, *n, f ) != *n )
506 {
507 fclose( f );
508 polarssl_free( *buf );
509 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
510 }
511
512 fclose( f );
513
514 (*buf)[*n] = '\0';
515
516 return( 0 );
517}
518
519/*
520 * Load and parse DHM parameters
521 */
522int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
523{
524 int ret;
525 size_t n;
526 unsigned char *buf;
527
528 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
529 return( ret );
530
531 ret = dhm_parse_dhm( dhm, buf, n );
532
533 memset( buf, 0, n + 1 );
534 polarssl_free( buf );
535
536 return( ret );
537}
538#endif /* POLARSSL_FS_IO */
539#endif /* POLARSSL_ASN1_PARSE_C */
540
Paul Bakker40e46942009-01-03 21:51:57 +0000541#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000542
Paul Bakker40ce79f2013-09-15 17:43:54 +0200543#include "polarssl/certs.h"
544
Paul Bakker5121ce52009-01-03 21:22:43 +0000545/*
546 * Checkup routine
547 */
548int dhm_self_test( int verbose )
549{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200550#if defined(POLARSSL_CERTS_C)
551 int ret;
552 dhm_context dhm;
553
554 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100555 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200556
557 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
558 strlen( test_dhm_params ) ) ) != 0 )
559 {
560 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100561 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200562
563 return( ret );
564 }
565
566 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100567 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200568
569 dhm_free( &dhm );
570
571 return( 0 );
572#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100573 if( verbose != 0 )
574 polarssl_printf( " DHM parameter load: skipped\n" );
575
576 return( 0 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200577#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000578}
579
580#endif
581
582#endif