blob: 41c573d29c3fde697dea69738de6d79d29fd91bf [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 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200405int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
406 size_t dhminlen )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200407{
408 int ret;
409 size_t len;
410 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200411#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200412 pem_context pem;
413
414 pem_init( &pem );
415 memset( dhm, 0, sizeof( dhm_context ) );
416
417 ret = pem_read_buffer( &pem,
418 "-----BEGIN DH PARAMETERS-----",
419 "-----END DH PARAMETERS-----",
420 dhmin, NULL, 0, &dhminlen );
421
422 if( ret == 0 )
423 {
424 /*
425 * Was PEM encoded
426 */
427 dhminlen = pem.buflen;
428 }
429 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
430 goto exit;
431
432 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
433#else
434 p = (unsigned char *) dhmin;
Paul Bakker9af723c2014-05-01 13:03:14 +0200435#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200436 end = p + dhminlen;
437
438 /*
439 * DHParams ::= SEQUENCE {
440 * prime INTEGER, -- P
441 * generator INTEGER, -- g
442 * }
443 */
444 if( ( ret = asn1_get_tag( &p, end, &len,
445 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
446 {
447 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
448 goto exit;
449 }
450
451 end = p + len;
452
453 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
454 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
455 {
456 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
457 goto exit;
458 }
459
460 if( p != end )
461 {
462 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
463 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
464 goto exit;
465 }
466
467 ret = 0;
468
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100469 dhm->len = mpi_size( &dhm->P );
470
Paul Bakker40ce79f2013-09-15 17:43:54 +0200471exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200472#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200473 pem_free( &pem );
474#endif
475 if( ret != 0 )
476 dhm_free( dhm );
477
478 return( ret );
479}
480
481#if defined(POLARSSL_FS_IO)
482/*
483 * Load all data from a file into a given buffer.
484 */
485static int load_file( const char *path, unsigned char **buf, size_t *n )
486{
487 FILE *f;
488 long size;
489
490 if( ( f = fopen( path, "rb" ) ) == NULL )
491 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
492
493 fseek( f, 0, SEEK_END );
494 if( ( size = ftell( f ) ) == -1 )
495 {
496 fclose( f );
497 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
498 }
499 fseek( f, 0, SEEK_SET );
500
501 *n = (size_t) size;
502
503 if( *n + 1 == 0 ||
504 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
505 {
506 fclose( f );
507 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
508 }
509
510 if( fread( *buf, 1, *n, f ) != *n )
511 {
512 fclose( f );
513 polarssl_free( *buf );
514 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
515 }
516
517 fclose( f );
518
519 (*buf)[*n] = '\0';
520
521 return( 0 );
522}
523
524/*
525 * Load and parse DHM parameters
526 */
527int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
528{
529 int ret;
530 size_t n;
531 unsigned char *buf;
532
533 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
534 return( ret );
535
536 ret = dhm_parse_dhm( dhm, buf, n );
537
538 memset( buf, 0, n + 1 );
539 polarssl_free( buf );
540
541 return( ret );
542}
543#endif /* POLARSSL_FS_IO */
544#endif /* POLARSSL_ASN1_PARSE_C */
545
Paul Bakker40e46942009-01-03 21:51:57 +0000546#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Paul Bakker40ce79f2013-09-15 17:43:54 +0200548#include "polarssl/certs.h"
549
Paul Bakker5121ce52009-01-03 21:22:43 +0000550/*
551 * Checkup routine
552 */
553int dhm_self_test( int verbose )
554{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200555#if defined(POLARSSL_CERTS_C)
556 int ret;
557 dhm_context dhm;
558
559 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100560 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200561
562 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
563 strlen( test_dhm_params ) ) ) != 0 )
564 {
565 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100566 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200567
568 return( ret );
569 }
570
571 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100572 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200573
574 dhm_free( &dhm );
575
576 return( 0 );
577#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100578 if( verbose != 0 )
579 polarssl_printf( " DHM parameter load: skipped\n" );
580
581 return( 0 );
Paul Bakker9af723c2014-05-01 13:03:14 +0200582#endif /* POLARSSL_CERTS_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000583}
584
Paul Bakker9af723c2014-05-01 13:03:14 +0200585#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Paul Bakker9af723c2014-05-01 13:03:14 +0200587#endif /* POLARSSL_DHM_C */