blob: ebc7c0c2f2ab3df54366ddc9bb3ca25aa1f225cd [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
Paul Bakkercff68422013-09-15 20:43:33 +020038#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +020039#include "polarssl/pem.h"
40#endif
41
42#if defined(POLARSSL_ASN1_PARSE_C)
43#include "polarssl/asn1.h"
44#endif
45
Paul Bakker7dc4c442014-02-01 22:50:26 +010046#if defined(POLARSSL_PLATFORM_C)
47#include "polarssl/platform.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020048#else
49#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#define polarssl_printf printf
Paul Bakker40ce79f2013-09-15 17:43:54 +020051#define polarssl_malloc malloc
52#define polarssl_free free
53#endif
54
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/*
61 * helper to validate the mpi size and import it
62 */
63static int dhm_read_bignum( mpi *X,
64 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000065 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000066{
67 int ret, n;
68
69 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000070 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72 n = ( (*p)[0] << 8 ) | (*p)[1];
73 (*p) += 2;
74
75 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000076 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000079 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 (*p) += n;
82
83 return( 0 );
84}
85
86/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000087 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000088 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000089 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000090 *
91 * For more information on the attack, see:
92 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
93 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000094 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000095static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000096{
Paul Bakker345a6fe2011-02-28 21:20:02 +000097 mpi L, U;
98 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +000099
Paul Bakker6c591fa2011-05-05 11:49:20 +0000100 mpi_init( &L ); mpi_init( &U );
Paul Bakker3d8fb632014-04-17 12:42:41 +0200101
102 MPI_CHK( mpi_lset( &L, 2 ) );
103 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000104
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000105 if( mpi_cmp_mpi( param, &L ) >= 0 &&
106 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000107 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000108 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000109 }
110
Paul Bakker3d8fb632014-04-17 12:42:41 +0200111cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000112 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +0000113 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000114}
115
Paul Bakker8f870b02014-06-20 13:32:38 +0200116void dhm_init( dhm_context *ctx )
117{
118 memset( ctx, 0, sizeof( dhm_context ) );
119}
120
Paul Bakkerc47840e2011-02-20 16:37:30 +0000121/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 * Parse the ServerKeyExchange parameters
123 */
124int dhm_read_params( dhm_context *ctx,
125 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000128 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
131 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
132 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
133 return( ret );
134
Paul Bakker345a6fe2011-02-28 21:20:02 +0000135 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
136 return( ret );
137
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 ctx->len = mpi_size( &ctx->P );
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 return( 0 );
141}
142
143/*
144 * Setup and write the ServerKeyExchange parameters
145 */
146int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000147 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000148 int (*f_rng)(void *, unsigned char *, size_t),
149 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000150{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000151 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000152 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 unsigned char *p;
154
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000155 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
156 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
157
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000159 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000161 do
162 {
163 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000165 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200166 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000167
168 if( count++ > 10 )
169 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
170 }
171 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Paul Bakkerff7fe672010-07-18 09:45:05 +0000173 /*
174 * Calculate GX = G^X mod P
175 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
177 &ctx->P , &ctx->RP ) );
178
Paul Bakker345a6fe2011-02-28 21:20:02 +0000179 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000180 return( ret );
181
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 /*
183 * export P, G, GX
184 */
185#define DHM_MPI_EXPORT(X,n) \
186 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
187 *p++ = (unsigned char)( n >> 8 ); \
188 *p++ = (unsigned char)( n ); p += n;
189
190 n1 = mpi_size( &ctx->P );
191 n2 = mpi_size( &ctx->G );
192 n3 = mpi_size( &ctx->GX );
193
194 p = output;
195 DHM_MPI_EXPORT( &ctx->P , n1 );
196 DHM_MPI_EXPORT( &ctx->G , n2 );
197 DHM_MPI_EXPORT( &ctx->GX, n3 );
198
199 *olen = p - output;
200
201 ctx->len = n1;
202
203cleanup:
204
205 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000206 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 return( 0 );
209}
210
211/*
212 * Import the peer's public value G^Y
213 */
214int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000215 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
217 int ret;
218
219 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000220 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000223 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225 return( 0 );
226}
227
228/*
229 * Create own private value X and export G^X
230 */
231int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000232 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000233 int (*f_rng)(void *, unsigned char *, size_t),
234 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000235{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000236 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000239 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000241 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
242 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 /*
245 * generate X and calculate GX = G^X mod P
246 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000247 do
248 {
249 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000251 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200252 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000253
254 if( count++ > 10 )
255 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
256 }
257 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
259 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
260 &ctx->P , &ctx->RP ) );
261
Paul Bakker345a6fe2011-02-28 21:20:02 +0000262 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
263 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000264
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
266
267cleanup:
268
269 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000270 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272 return( 0 );
273}
274
275/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200276 * Use the blinding method and optimisation suggested in section 10 of:
277 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
278 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
279 * Berlin Heidelberg, 1996. p. 104-113.
280 */
281static int dhm_update_blinding( dhm_context *ctx,
282 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
283{
284 int ret, count;
285
286 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200287 * Don't use any blinding the first time a particular X is used,
288 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200289 */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200290 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200291 {
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200292 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200293 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
294 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200295
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200296 return( 0 );
297 }
298
299 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200300 * Ok, we need blinding. Can we re-use existing values?
301 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200302 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200303 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
304 {
305 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
306 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
307
308 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
309 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
310
311 return( 0 );
312 }
313
314 /*
315 * We need to generate blinding values from scratch
316 */
317
318 /* Vi = random( 2, P-1 ) */
319 count = 0;
320 do
321 {
322 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
323
324 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200325 MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200326
327 if( count++ > 10 )
328 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
329 }
330 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200331
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200332 /* Vf = Vi^-X mod P */
333 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
334 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
335
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200336cleanup:
337 return( ret );
338}
339
340/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000341 * Derive and export the shared secret (G^Y)^X mod P
342 */
343int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200344 unsigned char *output, size_t *olen,
345 int (*f_rng)(void *, unsigned char *, size_t),
346 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347{
348 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200349 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200350
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000352 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
Paul Bakker345a6fe2011-02-28 21:20:02 +0000354 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000355 return( ret );
356
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200357 mpi_init( &GYb );
358
359 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200360 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200361 {
362 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
363 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
364 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
365 }
366 else
367 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
368
369 /* Do modular exponentiation */
370 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
371 &ctx->P, &ctx->RP ) );
372
373 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200374 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200375 {
376 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
377 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
378 }
379
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 *olen = mpi_size( &ctx->K );
381
382 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
383
384cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200385 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000388 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 return( 0 );
391}
392
393/*
394 * Free the components of a DHM key
395 */
396void dhm_free( dhm_context *ctx )
397{
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200398 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000399 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
400 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
401 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200402
Paul Bakker34617722014-06-13 17:20:13 +0200403 polarssl_zeroize( ctx, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404}
405
Paul Bakker40ce79f2013-09-15 17:43:54 +0200406#if defined(POLARSSL_ASN1_PARSE_C)
407/*
408 * Parse DHM parameters
409 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200410int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
411 size_t dhminlen )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200412{
413 int ret;
414 size_t len;
415 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200416#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200417 pem_context pem;
418
419 pem_init( &pem );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200420
421 ret = pem_read_buffer( &pem,
422 "-----BEGIN DH PARAMETERS-----",
423 "-----END DH PARAMETERS-----",
424 dhmin, NULL, 0, &dhminlen );
425
426 if( ret == 0 )
427 {
428 /*
429 * Was PEM encoded
430 */
431 dhminlen = pem.buflen;
432 }
433 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
434 goto exit;
435
436 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
437#else
438 p = (unsigned char *) dhmin;
Paul Bakker9af723c2014-05-01 13:03:14 +0200439#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200440 end = p + dhminlen;
441
442 /*
443 * DHParams ::= SEQUENCE {
444 * prime INTEGER, -- P
445 * generator INTEGER, -- g
446 * }
447 */
448 if( ( ret = asn1_get_tag( &p, end, &len,
449 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
450 {
451 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
452 goto exit;
453 }
454
455 end = p + len;
456
457 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
458 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
459 {
460 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
461 goto exit;
462 }
463
464 if( p != end )
465 {
466 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
467 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
468 goto exit;
469 }
470
471 ret = 0;
472
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100473 dhm->len = mpi_size( &dhm->P );
474
Paul Bakker40ce79f2013-09-15 17:43:54 +0200475exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200476#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200477 pem_free( &pem );
478#endif
479 if( ret != 0 )
480 dhm_free( dhm );
481
482 return( ret );
483}
484
485#if defined(POLARSSL_FS_IO)
486/*
487 * Load all data from a file into a given buffer.
488 */
489static int load_file( const char *path, unsigned char **buf, size_t *n )
490{
491 FILE *f;
492 long size;
493
494 if( ( f = fopen( path, "rb" ) ) == NULL )
495 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
496
497 fseek( f, 0, SEEK_END );
498 if( ( size = ftell( f ) ) == -1 )
499 {
500 fclose( f );
501 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
502 }
503 fseek( f, 0, SEEK_SET );
504
505 *n = (size_t) size;
506
507 if( *n + 1 == 0 ||
508 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
509 {
510 fclose( f );
511 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
512 }
513
514 if( fread( *buf, 1, *n, f ) != *n )
515 {
516 fclose( f );
517 polarssl_free( *buf );
518 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
519 }
520
521 fclose( f );
522
523 (*buf)[*n] = '\0';
524
525 return( 0 );
526}
527
528/*
529 * Load and parse DHM parameters
530 */
531int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
532{
533 int ret;
534 size_t n;
535 unsigned char *buf;
536
Paul Bakker66d5d072014-06-17 16:39:18 +0200537 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200538 return( ret );
539
540 ret = dhm_parse_dhm( dhm, buf, n );
541
Paul Bakker34617722014-06-13 17:20:13 +0200542 polarssl_zeroize( buf, n + 1 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200543 polarssl_free( buf );
544
545 return( ret );
546}
547#endif /* POLARSSL_FS_IO */
548#endif /* POLARSSL_ASN1_PARSE_C */
549
Paul Bakker40e46942009-01-03 21:51:57 +0000550#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
Paul Bakker40ce79f2013-09-15 17:43:54 +0200552#include "polarssl/certs.h"
553
Paul Bakker5121ce52009-01-03 21:22:43 +0000554/*
555 * Checkup routine
556 */
557int dhm_self_test( int verbose )
558{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200559#if defined(POLARSSL_CERTS_C)
560 int ret;
561 dhm_context dhm;
562
Paul Bakker8f870b02014-06-20 13:32:38 +0200563 dhm_init( &dhm );
564
Paul Bakker40ce79f2013-09-15 17:43:54 +0200565 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100566 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200567
568 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
569 strlen( test_dhm_params ) ) ) != 0 )
570 {
571 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100572 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200573
Manuel Pégourié-Gonnardb196fc22014-07-09 16:53:29 +0200574 ret = 1;
Paul Bakker8f870b02014-06-20 13:32:38 +0200575 goto exit;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200576 }
577
578 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100579 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200580
Paul Bakker8f870b02014-06-20 13:32:38 +0200581exit:
Paul Bakker40ce79f2013-09-15 17:43:54 +0200582 dhm_free( &dhm );
583
Paul Bakker8f870b02014-06-20 13:32:38 +0200584 return( ret );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200585#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100586 if( verbose != 0 )
587 polarssl_printf( " DHM parameter load: skipped\n" );
588
589 return( 0 );
Paul Bakker9af723c2014-05-01 13:03:14 +0200590#endif /* POLARSSL_CERTS_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000591}
592
Paul Bakker9af723c2014-05-01 13:03:14 +0200593#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Paul Bakker9af723c2014-05-01 13:03:14 +0200595#endif /* POLARSSL_DHM_C */