blob: 5a38c148de7f4e8f7d7487d581b4e5351896746b [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker40e46942009-01-03 21:51:57 +000039#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Paul Bakkercff68422013-09-15 20:43:33 +020041#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +020042#include "polarssl/pem.h"
43#endif
44
45#if defined(POLARSSL_ASN1_PARSE_C)
46#include "polarssl/asn1.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020051#else
52#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010053#define polarssl_printf printf
Paul Bakker40ce79f2013-09-15 17:43:54 +020054#define polarssl_malloc malloc
55#define polarssl_free free
56#endif
57
Paul Bakker5121ce52009-01-03 21:22:43 +000058/*
59 * helper to validate the mpi size and import it
60 */
61static int dhm_read_bignum( mpi *X,
62 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000063 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000064{
65 int ret, n;
66
67 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000068 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70 n = ( (*p)[0] << 8 ) | (*p)[1];
71 (*p) += 2;
72
73 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000074 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000077 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79 (*p) += n;
80
81 return( 0 );
82}
83
84/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000085 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000086 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000087 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000088 *
89 * For more information on the attack, see:
90 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
91 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000092 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000093static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000094{
Paul Bakker345a6fe2011-02-28 21:20:02 +000095 mpi L, U;
96 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +000097
Paul Bakker6c591fa2011-05-05 11:49:20 +000098 mpi_init( &L ); mpi_init( &U );
Paul Bakker3d8fb632014-04-17 12:42:41 +020099
100 MPI_CHK( mpi_lset( &L, 2 ) );
101 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000102
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000103 if( mpi_cmp_mpi( param, &L ) >= 0 &&
104 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000105 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000106 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000107 }
108
Paul Bakker3d8fb632014-04-17 12:42:41 +0200109cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000110 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +0000111 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000112}
113
114/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 * Parse the ServerKeyExchange parameters
116 */
117int dhm_read_params( dhm_context *ctx,
118 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000119 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000121 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200123 dhm_free( ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
126 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
127 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
128 return( ret );
129
Paul Bakker345a6fe2011-02-28 21:20:02 +0000130 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
131 return( ret );
132
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 ctx->len = mpi_size( &ctx->P );
134
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 return( 0 );
136}
137
138/*
139 * Setup and write the ServerKeyExchange parameters
140 */
141int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000142 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000143 int (*f_rng)(void *, unsigned char *, size_t),
144 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000146 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000147 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 unsigned char *p;
149
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000150 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
151 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000154 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000156 do
157 {
158 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000160 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200161 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000162
163 if( count++ > 10 )
164 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
165 }
166 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakkerff7fe672010-07-18 09:45:05 +0000168 /*
169 * Calculate GX = G^X mod P
170 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
172 &ctx->P , &ctx->RP ) );
173
Paul Bakker345a6fe2011-02-28 21:20:02 +0000174 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000175 return( ret );
176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 /*
178 * export P, G, GX
179 */
180#define DHM_MPI_EXPORT(X,n) \
181 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
182 *p++ = (unsigned char)( n >> 8 ); \
183 *p++ = (unsigned char)( n ); p += n;
184
185 n1 = mpi_size( &ctx->P );
186 n2 = mpi_size( &ctx->G );
187 n3 = mpi_size( &ctx->GX );
188
189 p = output;
190 DHM_MPI_EXPORT( &ctx->P , n1 );
191 DHM_MPI_EXPORT( &ctx->G , n2 );
192 DHM_MPI_EXPORT( &ctx->GX, n3 );
193
194 *olen = p - output;
195
196 ctx->len = n1;
197
198cleanup:
199
200 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000201 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 return( 0 );
204}
205
206/*
207 * Import the peer's public value G^Y
208 */
209int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000210 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
212 int ret;
213
214 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000215 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000218 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220 return( 0 );
221}
222
223/*
224 * Create own private value X and export G^X
225 */
226int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000227 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000228 int (*f_rng)(void *, unsigned char *, size_t),
229 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000231 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000234 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000236 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
237 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
238
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 /*
240 * generate X and calculate GX = G^X mod P
241 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000242 do
243 {
244 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000246 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200247 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000248
249 if( count++ > 10 )
250 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
251 }
252 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
254 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
255 &ctx->P , &ctx->RP ) );
256
Paul Bakker345a6fe2011-02-28 21:20:02 +0000257 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
258 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000259
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
261
262cleanup:
263
264 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000265 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 return( 0 );
268}
269
270/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200271 * Use the blinding method and optimisation suggested in section 10 of:
272 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
273 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
274 * Berlin Heidelberg, 1996. p. 104-113.
275 */
276static int dhm_update_blinding( dhm_context *ctx,
277 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
278{
279 int ret, count;
280
281 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200282 * Don't use any blinding the first time a particular X is used,
283 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200284 */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200285 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200286 {
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200287 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200288 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
289 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200290
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200291 return( 0 );
292 }
293
294 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200295 * Ok, we need blinding. Can we re-use existing values?
296 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200297 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200298 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
299 {
300 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
301 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
302
303 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
304 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
305
306 return( 0 );
307 }
308
309 /*
310 * We need to generate blinding values from scratch
311 */
312
313 /* Vi = random( 2, P-1 ) */
314 count = 0;
315 do
316 {
317 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
318
319 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200320 MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200321
322 if( count++ > 10 )
323 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
324 }
325 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200326
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200327 /* Vf = Vi^-X mod P */
328 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
329 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
330
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200331cleanup:
332 return( ret );
333}
334
335/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 * Derive and export the shared secret (G^Y)^X mod P
337 */
338int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200339 unsigned char *output, size_t *olen,
340 int (*f_rng)(void *, unsigned char *, size_t),
341 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342{
343 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200344 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200345
Paul Bakker5121ce52009-01-03 21:22:43 +0000346 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000347 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
Paul Bakker345a6fe2011-02-28 21:20:02 +0000349 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000350 return( ret );
351
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200352 mpi_init( &GYb );
353
354 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200355 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200356 {
357 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
358 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
359 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
360 }
361 else
362 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
363
364 /* Do modular exponentiation */
365 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
366 &ctx->P, &ctx->RP ) );
367
368 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200369 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200370 {
371 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
372 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
373 }
374
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 *olen = mpi_size( &ctx->K );
376
377 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
378
379cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200380 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
382 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000383 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 return( 0 );
386}
387
388/*
389 * Free the components of a DHM key
390 */
391void dhm_free( dhm_context *ctx )
392{
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200393 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000394 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
395 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
396 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200397
398 memset( ctx, 0, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399}
400
Paul Bakker40ce79f2013-09-15 17:43:54 +0200401#if defined(POLARSSL_ASN1_PARSE_C)
402/*
403 * Parse DHM parameters
404 */
405int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin, size_t dhminlen )
406{
407 int ret;
408 size_t len;
409 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200410#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200411 pem_context pem;
412
413 pem_init( &pem );
414 memset( dhm, 0, sizeof( dhm_context ) );
415
416 ret = pem_read_buffer( &pem,
417 "-----BEGIN DH PARAMETERS-----",
418 "-----END DH PARAMETERS-----",
419 dhmin, NULL, 0, &dhminlen );
420
421 if( ret == 0 )
422 {
423 /*
424 * Was PEM encoded
425 */
426 dhminlen = pem.buflen;
427 }
428 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
429 goto exit;
430
431 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
432#else
433 p = (unsigned char *) dhmin;
434#endif
435 end = p + dhminlen;
436
437 /*
438 * DHParams ::= SEQUENCE {
439 * prime INTEGER, -- P
440 * generator INTEGER, -- g
441 * }
442 */
443 if( ( ret = asn1_get_tag( &p, end, &len,
444 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
445 {
446 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
447 goto exit;
448 }
449
450 end = p + len;
451
452 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
453 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
454 {
455 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
456 goto exit;
457 }
458
459 if( p != end )
460 {
461 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
462 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
463 goto exit;
464 }
465
466 ret = 0;
467
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100468 dhm->len = mpi_size( &dhm->P );
469
Paul Bakker40ce79f2013-09-15 17:43:54 +0200470exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200471#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200472 pem_free( &pem );
473#endif
474 if( ret != 0 )
475 dhm_free( dhm );
476
477 return( ret );
478}
479
480#if defined(POLARSSL_FS_IO)
481/*
482 * Load all data from a file into a given buffer.
483 */
484static int load_file( const char *path, unsigned char **buf, size_t *n )
485{
486 FILE *f;
487 long size;
488
489 if( ( f = fopen( path, "rb" ) ) == NULL )
490 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
491
492 fseek( f, 0, SEEK_END );
493 if( ( size = ftell( f ) ) == -1 )
494 {
495 fclose( f );
496 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
497 }
498 fseek( f, 0, SEEK_SET );
499
500 *n = (size_t) size;
501
502 if( *n + 1 == 0 ||
503 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
504 {
505 fclose( f );
506 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
507 }
508
509 if( fread( *buf, 1, *n, f ) != *n )
510 {
511 fclose( f );
512 polarssl_free( *buf );
513 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
514 }
515
516 fclose( f );
517
518 (*buf)[*n] = '\0';
519
520 return( 0 );
521}
522
523/*
524 * Load and parse DHM parameters
525 */
526int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
527{
528 int ret;
529 size_t n;
530 unsigned char *buf;
531
532 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
533 return( ret );
534
535 ret = dhm_parse_dhm( dhm, buf, n );
536
537 memset( buf, 0, n + 1 );
538 polarssl_free( buf );
539
540 return( ret );
541}
542#endif /* POLARSSL_FS_IO */
543#endif /* POLARSSL_ASN1_PARSE_C */
544
Paul Bakker40e46942009-01-03 21:51:57 +0000545#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Paul Bakker40ce79f2013-09-15 17:43:54 +0200547#include "polarssl/certs.h"
548
Paul Bakker5121ce52009-01-03 21:22:43 +0000549/*
550 * Checkup routine
551 */
552int dhm_self_test( int verbose )
553{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200554#if defined(POLARSSL_CERTS_C)
555 int ret;
556 dhm_context dhm;
557
558 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100559 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200560
561 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
562 strlen( test_dhm_params ) ) ) != 0 )
563 {
564 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100565 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200566
567 return( ret );
568 }
569
570 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100571 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200572
573 dhm_free( &dhm );
574
575 return( 0 );
576#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100577 if( verbose != 0 )
578 polarssl_printf( " DHM parameter load: skipped\n" );
579
580 return( 0 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000582}
583
584#endif
585
586#endif