blob: 362cd1dd3660f852b01301f88066eee08cefeca1 [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 Bakker34617722014-06-13 17:20:13 +020058/* Implementation that should never be optimized out by the compiler */
59static void polarssl_zeroize( void *v, size_t n ) {
60 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
61}
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063/*
64 * helper to validate the mpi size and import it
65 */
66static int dhm_read_bignum( mpi *X,
67 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000068 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
70 int ret, n;
71
72 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000073 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75 n = ( (*p)[0] << 8 ) | (*p)[1];
76 (*p) += 2;
77
78 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000079 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +000082 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84 (*p) += n;
85
86 return( 0 );
87}
88
89/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000090 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000091 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000092 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000093 *
94 * For more information on the attack, see:
95 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
96 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000097 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +000098static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000099{
Paul Bakker345a6fe2011-02-28 21:20:02 +0000100 mpi L, U;
101 int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000102
Paul Bakker6c591fa2011-05-05 11:49:20 +0000103 mpi_init( &L ); mpi_init( &U );
Paul Bakker3d8fb632014-04-17 12:42:41 +0200104
105 MPI_CHK( mpi_lset( &L, 2 ) );
106 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000107
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000108 if( mpi_cmp_mpi( param, &L ) >= 0 &&
109 mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000110 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000111 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000112 }
113
Paul Bakker3d8fb632014-04-17 12:42:41 +0200114cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000115 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +0000116 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000117}
118
119/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 * Parse the ServerKeyExchange parameters
121 */
122int dhm_read_params( dhm_context *ctx,
123 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000124 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000126 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200128 dhm_free( ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130 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 );
420 memset( dhm, 0, sizeof( dhm_context ) );
421
422 ret = pem_read_buffer( &pem,
423 "-----BEGIN DH PARAMETERS-----",
424 "-----END DH PARAMETERS-----",
425 dhmin, NULL, 0, &dhminlen );
426
427 if( ret == 0 )
428 {
429 /*
430 * Was PEM encoded
431 */
432 dhminlen = pem.buflen;
433 }
434 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
435 goto exit;
436
437 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
438#else
439 p = (unsigned char *) dhmin;
Paul Bakker9af723c2014-05-01 13:03:14 +0200440#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200441 end = p + dhminlen;
442
443 /*
444 * DHParams ::= SEQUENCE {
445 * prime INTEGER, -- P
446 * generator INTEGER, -- g
447 * }
448 */
449 if( ( ret = asn1_get_tag( &p, end, &len,
450 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
451 {
452 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
453 goto exit;
454 }
455
456 end = p + len;
457
458 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
459 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
460 {
461 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
462 goto exit;
463 }
464
465 if( p != end )
466 {
467 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
468 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
469 goto exit;
470 }
471
472 ret = 0;
473
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100474 dhm->len = mpi_size( &dhm->P );
475
Paul Bakker40ce79f2013-09-15 17:43:54 +0200476exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200477#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200478 pem_free( &pem );
479#endif
480 if( ret != 0 )
481 dhm_free( dhm );
482
483 return( ret );
484}
485
486#if defined(POLARSSL_FS_IO)
487/*
488 * Load all data from a file into a given buffer.
489 */
490static int load_file( const char *path, unsigned char **buf, size_t *n )
491{
492 FILE *f;
493 long size;
494
495 if( ( f = fopen( path, "rb" ) ) == NULL )
496 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
497
498 fseek( f, 0, SEEK_END );
499 if( ( size = ftell( f ) ) == -1 )
500 {
501 fclose( f );
502 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
503 }
504 fseek( f, 0, SEEK_SET );
505
506 *n = (size_t) size;
507
508 if( *n + 1 == 0 ||
509 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
510 {
511 fclose( f );
512 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
513 }
514
515 if( fread( *buf, 1, *n, f ) != *n )
516 {
517 fclose( f );
518 polarssl_free( *buf );
519 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
520 }
521
522 fclose( f );
523
524 (*buf)[*n] = '\0';
525
526 return( 0 );
527}
528
529/*
530 * Load and parse DHM parameters
531 */
532int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
533{
534 int ret;
535 size_t n;
536 unsigned char *buf;
537
538 if ( ( ret = load_file( path, &buf, &n ) ) != 0 )
539 return( ret );
540
541 ret = dhm_parse_dhm( dhm, buf, n );
542
Paul Bakker34617722014-06-13 17:20:13 +0200543 polarssl_zeroize( buf, n + 1 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200544 polarssl_free( buf );
545
546 return( ret );
547}
548#endif /* POLARSSL_FS_IO */
549#endif /* POLARSSL_ASN1_PARSE_C */
550
Paul Bakker40e46942009-01-03 21:51:57 +0000551#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Paul Bakker40ce79f2013-09-15 17:43:54 +0200553#include "polarssl/certs.h"
554
Paul Bakker5121ce52009-01-03 21:22:43 +0000555/*
556 * Checkup routine
557 */
558int dhm_self_test( int verbose )
559{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200560#if defined(POLARSSL_CERTS_C)
561 int ret;
562 dhm_context dhm;
563
564 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100565 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200566
567 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
568 strlen( test_dhm_params ) ) ) != 0 )
569 {
570 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100571 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200572
573 return( ret );
574 }
575
576 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100577 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200578
579 dhm_free( &dhm );
580
581 return( 0 );
582#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100583 if( verbose != 0 )
584 polarssl_printf( " DHM parameter load: skipped\n" );
585
586 return( 0 );
Paul Bakker9af723c2014-05-01 13:03:14 +0200587#endif /* POLARSSL_CERTS_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000588}
589
Paul Bakker9af723c2014-05-01 13:03:14 +0200590#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Paul Bakker9af723c2014-05-01 13:03:14 +0200592#endif /* POLARSSL_DHM_C */