blob: 5861f94622a6b748cf6a7c4eba42957c9c06b1df [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * Reference:
24 *
25 * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
26 */
27
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
31#include POLARSSL_CONFIG_FILE
32#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <string.h>
39
Paul Bakkercff68422013-09-15 20:43:33 +020040#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +020041#include "polarssl/pem.h"
42#endif
43
44#if defined(POLARSSL_ASN1_PARSE_C)
45#include "polarssl/asn1.h"
46#endif
47
Paul Bakker7dc4c442014-02-01 22:50:26 +010048#if defined(POLARSSL_PLATFORM_C)
49#include "polarssl/platform.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020050#else
51#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010052#define polarssl_printf printf
Paul Bakker40ce79f2013-09-15 17:43:54 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker34617722014-06-13 17:20:13 +020057/* Implementation that should never be optimized out by the compiler */
58static void polarssl_zeroize( void *v, size_t n ) {
59 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
60}
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062/*
63 * helper to validate the mpi size and import it
64 */
65static int dhm_read_bignum( mpi *X,
66 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000067 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000068{
69 int ret, n;
70
71 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000072 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74 n = ( (*p)[0] << 8 ) | (*p)[1];
75 (*p) += 2;
76
77 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000078 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000081 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83 (*p) += n;
84
85 return( 0 );
86}
87
88/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000089 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000090 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000091 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000092 *
93 * For more information on the attack, see:
94 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
95 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000096 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000097static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000098{
Paul Bakker345a6fe2011-02-28 21:20:02 +000099 mpi L, U;
100 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000101
Paul Bakker6c591fa2011-05-05 11:49:20 +0000102 mpi_init( &L ); mpi_init( &U );
Paul Bakker3d8fb632014-04-17 12:42:41 +0200103
104 MPI_CHK( mpi_lset( &L, 2 ) );
105 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000106
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000107 if( mpi_cmp_mpi( param, &L ) >= 0 &&
108 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000109 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000110 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000111 }
112
Paul Bakker3d8fb632014-04-17 12:42:41 +0200113cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000114 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +0000115 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000116}
117
Paul Bakker8f870b02014-06-20 13:32:38 +0200118void dhm_init( dhm_context *ctx )
119{
120 memset( ctx, 0, sizeof( dhm_context ) );
121}
122
Paul Bakkerc47840e2011-02-20 16:37:30 +0000123/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 * Parse the ServerKeyExchange parameters
125 */
126int dhm_read_params( dhm_context *ctx,
127 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000128 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000130 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
133 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
134 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
135 return( ret );
136
Paul Bakker345a6fe2011-02-28 21:20:02 +0000137 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
138 return( ret );
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 ctx->len = mpi_size( &ctx->P );
141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 return( 0 );
143}
144
145/*
146 * Setup and write the ServerKeyExchange parameters
147 */
148int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000149 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000150 int (*f_rng)(void *, unsigned char *, size_t),
151 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000152{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000153 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000154 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 unsigned char *p;
156
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000157 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
158 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
159
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000161 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000163 do
164 {
165 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000167 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200168 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000169
170 if( count++ > 10 )
171 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
172 }
173 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Paul Bakkerff7fe672010-07-18 09:45:05 +0000175 /*
176 * Calculate GX = G^X mod P
177 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
179 &ctx->P , &ctx->RP ) );
180
Paul Bakker345a6fe2011-02-28 21:20:02 +0000181 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000182 return( ret );
183
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 /*
185 * export P, G, GX
186 */
187#define DHM_MPI_EXPORT(X,n) \
188 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
189 *p++ = (unsigned char)( n >> 8 ); \
190 *p++ = (unsigned char)( n ); p += n;
191
192 n1 = mpi_size( &ctx->P );
193 n2 = mpi_size( &ctx->G );
194 n3 = mpi_size( &ctx->GX );
195
196 p = output;
197 DHM_MPI_EXPORT( &ctx->P , n1 );
198 DHM_MPI_EXPORT( &ctx->G , n2 );
199 DHM_MPI_EXPORT( &ctx->GX, n3 );
200
201 *olen = p - output;
202
203 ctx->len = n1;
204
205cleanup:
206
207 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000208 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210 return( 0 );
211}
212
213/*
214 * Import the peer's public value G^Y
215 */
216int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000217 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218{
219 int ret;
220
221 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000222 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000225 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 return( 0 );
228}
229
230/*
231 * Create own private value X and export G^X
232 */
233int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000234 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000235 int (*f_rng)(void *, unsigned char *, size_t),
236 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000237{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000238 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000241 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000243 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
244 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
245
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 /*
247 * generate X and calculate GX = G^X mod P
248 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000249 do
250 {
251 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000253 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200254 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000255
256 if( count++ > 10 )
257 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
258 }
259 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
262 &ctx->P , &ctx->RP ) );
263
Paul Bakker345a6fe2011-02-28 21:20:02 +0000264 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
265 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000266
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
268
269cleanup:
270
271 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000272 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274 return( 0 );
275}
276
277/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200278 * Use the blinding method and optimisation suggested in section 10 of:
279 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
280 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
281 * Berlin Heidelberg, 1996. p. 104-113.
282 */
283static int dhm_update_blinding( dhm_context *ctx,
284 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
285{
286 int ret, count;
287
288 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200289 * Don't use any blinding the first time a particular X is used,
290 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200291 */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200292 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200293 {
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200294 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200295 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
296 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200297
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200298 return( 0 );
299 }
300
301 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200302 * Ok, we need blinding. Can we re-use existing values?
303 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200304 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200305 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
306 {
307 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
308 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
309
310 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
311 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
312
313 return( 0 );
314 }
315
316 /*
317 * We need to generate blinding values from scratch
318 */
319
320 /* Vi = random( 2, P-1 ) */
321 count = 0;
322 do
323 {
324 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
325
326 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200327 MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200328
329 if( count++ > 10 )
330 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
331 }
332 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200333
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200334 /* Vf = Vi^-X mod P */
335 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
336 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
337
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200338cleanup:
339 return( ret );
340}
341
342/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 * Derive and export the shared secret (G^Y)^X mod P
344 */
345int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200346 unsigned char *output, size_t *olen,
347 int (*f_rng)(void *, unsigned char *, size_t),
348 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000349{
350 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200351 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200352
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000354 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Paul Bakker345a6fe2011-02-28 21:20:02 +0000356 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000357 return( ret );
358
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200359 mpi_init( &GYb );
360
361 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200362 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200363 {
364 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
365 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
366 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
367 }
368 else
369 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
370
371 /* Do modular exponentiation */
372 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
373 &ctx->P, &ctx->RP ) );
374
375 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200376 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200377 {
378 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
379 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
380 }
381
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 *olen = mpi_size( &ctx->K );
383
384 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
385
386cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200387 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
389 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000390 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
392 return( 0 );
393}
394
395/*
396 * Free the components of a DHM key
397 */
398void dhm_free( dhm_context *ctx )
399{
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200400 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000401 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
402 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
403 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200404
Paul Bakker34617722014-06-13 17:20:13 +0200405 polarssl_zeroize( ctx, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406}
407
Paul Bakker40ce79f2013-09-15 17:43:54 +0200408#if defined(POLARSSL_ASN1_PARSE_C)
409/*
410 * Parse DHM parameters
411 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200412int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
413 size_t dhminlen )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200414{
415 int ret;
416 size_t len;
417 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200418#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200419 pem_context pem;
420
421 pem_init( &pem );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200422
423 ret = pem_read_buffer( &pem,
424 "-----BEGIN DH PARAMETERS-----",
425 "-----END DH PARAMETERS-----",
426 dhmin, NULL, 0, &dhminlen );
427
428 if( ret == 0 )
429 {
430 /*
431 * Was PEM encoded
432 */
433 dhminlen = pem.buflen;
434 }
435 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
436 goto exit;
437
438 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
439#else
440 p = (unsigned char *) dhmin;
Paul Bakker9af723c2014-05-01 13:03:14 +0200441#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200442 end = p + dhminlen;
443
444 /*
445 * DHParams ::= SEQUENCE {
446 * prime INTEGER, -- P
447 * generator INTEGER, -- g
448 * }
449 */
450 if( ( ret = asn1_get_tag( &p, end, &len,
451 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
452 {
453 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
454 goto exit;
455 }
456
457 end = p + len;
458
459 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
460 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
461 {
462 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
463 goto exit;
464 }
465
466 if( p != end )
467 {
468 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
469 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
470 goto exit;
471 }
472
473 ret = 0;
474
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100475 dhm->len = mpi_size( &dhm->P );
476
Paul Bakker40ce79f2013-09-15 17:43:54 +0200477exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200478#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200479 pem_free( &pem );
480#endif
481 if( ret != 0 )
482 dhm_free( dhm );
483
484 return( ret );
485}
486
487#if defined(POLARSSL_FS_IO)
488/*
489 * Load all data from a file into a given buffer.
490 */
491static int load_file( const char *path, unsigned char **buf, size_t *n )
492{
493 FILE *f;
494 long size;
495
496 if( ( f = fopen( path, "rb" ) ) == NULL )
497 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
498
499 fseek( f, 0, SEEK_END );
500 if( ( size = ftell( f ) ) == -1 )
501 {
502 fclose( f );
503 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
504 }
505 fseek( f, 0, SEEK_SET );
506
507 *n = (size_t) size;
508
509 if( *n + 1 == 0 ||
510 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
511 {
512 fclose( f );
513 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
514 }
515
516 if( fread( *buf, 1, *n, f ) != *n )
517 {
518 fclose( f );
519 polarssl_free( *buf );
520 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
521 }
522
523 fclose( f );
524
525 (*buf)[*n] = '\0';
526
527 return( 0 );
528}
529
530/*
531 * Load and parse DHM parameters
532 */
533int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
534{
535 int ret;
536 size_t n;
537 unsigned char *buf;
538
Paul Bakker66d5d072014-06-17 16:39:18 +0200539 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200540 return( ret );
541
542 ret = dhm_parse_dhm( dhm, buf, n );
543
Paul Bakker34617722014-06-13 17:20:13 +0200544 polarssl_zeroize( buf, n + 1 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200545 polarssl_free( buf );
546
547 return( ret );
548}
549#endif /* POLARSSL_FS_IO */
550#endif /* POLARSSL_ASN1_PARSE_C */
551
Paul Bakker40e46942009-01-03 21:51:57 +0000552#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Paul Bakker40ce79f2013-09-15 17:43:54 +0200554#include "polarssl/certs.h"
555
Paul Bakker5121ce52009-01-03 21:22:43 +0000556/*
557 * Checkup routine
558 */
559int dhm_self_test( int verbose )
560{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200561#if defined(POLARSSL_CERTS_C)
562 int ret;
563 dhm_context dhm;
564
Paul Bakker8f870b02014-06-20 13:32:38 +0200565 dhm_init( &dhm );
566
Paul Bakker40ce79f2013-09-15 17:43:54 +0200567 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100568 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200569
570 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
571 strlen( test_dhm_params ) ) ) != 0 )
572 {
573 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100574 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200575
Manuel Pégourié-Gonnardb196fc22014-07-09 16:53:29 +0200576 ret = 1;
Paul Bakker8f870b02014-06-20 13:32:38 +0200577 goto exit;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200578 }
579
580 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100581 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200582
Paul Bakker8f870b02014-06-20 13:32:38 +0200583exit:
Paul Bakker40ce79f2013-09-15 17:43:54 +0200584 dhm_free( &dhm );
585
Paul Bakker8f870b02014-06-20 13:32:38 +0200586 return( ret );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200587#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100588 if( verbose != 0 )
589 polarssl_printf( " DHM parameter load: skipped\n" );
590
591 return( 0 );
Paul Bakker9af723c2014-05-01 13:03:14 +0200592#endif /* POLARSSL_CERTS_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000593}
594
Paul Bakker9af723c2014-05-01 13:03:14 +0200595#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Paul Bakker9af723c2014-05-01 13:03:14 +0200597#endif /* POLARSSL_DHM_C */