blob: b72c7f055e8dd5f586d7c8abf29beaf064ae09b4 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/pem.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020042#endif
43
44#if defined(POLARSSL_ASN1_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/asn1.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020046#endif
47
Paul Bakker7dc4c442014-02-01 22:50:26 +010048#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/platform.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020050#else
51#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000052#include <stdio.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
Paul Bakker8f870b02014-06-20 13:32:38 +0200119void dhm_init( dhm_context *ctx )
120{
121 memset( ctx, 0, sizeof( dhm_context ) );
122}
123
Paul Bakkerc47840e2011-02-20 16:37:30 +0000124/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 * Parse the ServerKeyExchange parameters
126 */
127int dhm_read_params( dhm_context *ctx,
128 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000129 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000131 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
134 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
135 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
136 return( ret );
137
Paul Bakker345a6fe2011-02-28 21:20:02 +0000138 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
139 return( ret );
140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 ctx->len = mpi_size( &ctx->P );
142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 return( 0 );
144}
145
146/*
147 * Setup and write the ServerKeyExchange parameters
148 */
149int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000150 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000151 int (*f_rng)(void *, unsigned char *, size_t),
152 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000153{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000154 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000155 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 unsigned char *p;
157
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000158 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
159 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
160
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000162 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000164 do
165 {
166 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000168 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200169 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000170
171 if( count++ > 10 )
172 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
173 }
174 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
Paul Bakkerff7fe672010-07-18 09:45:05 +0000176 /*
177 * Calculate GX = G^X mod P
178 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
180 &ctx->P , &ctx->RP ) );
181
Paul Bakker345a6fe2011-02-28 21:20:02 +0000182 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000183 return( ret );
184
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 /*
186 * export P, G, GX
187 */
188#define DHM_MPI_EXPORT(X,n) \
189 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
190 *p++ = (unsigned char)( n >> 8 ); \
191 *p++ = (unsigned char)( n ); p += n;
192
193 n1 = mpi_size( &ctx->P );
194 n2 = mpi_size( &ctx->G );
195 n3 = mpi_size( &ctx->GX );
196
197 p = output;
198 DHM_MPI_EXPORT( &ctx->P , n1 );
199 DHM_MPI_EXPORT( &ctx->G , n2 );
200 DHM_MPI_EXPORT( &ctx->GX, n3 );
201
202 *olen = p - output;
203
204 ctx->len = n1;
205
206cleanup:
207
208 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000209 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 return( 0 );
212}
213
214/*
215 * Import the peer's public value G^Y
216 */
217int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000218 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000219{
220 int ret;
221
222 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000223 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000226 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228 return( 0 );
229}
230
231/*
232 * Create own private value X and export G^X
233 */
234int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000235 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000236 int (*f_rng)(void *, unsigned char *, size_t),
237 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000238{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000239 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
241 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000242 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000244 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
245 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
246
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 /*
248 * generate X and calculate GX = G^X mod P
249 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000250 do
251 {
252 mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000254 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200255 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000256
257 if( count++ > 10 )
258 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
259 }
260 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
263 &ctx->P , &ctx->RP ) );
264
Paul Bakker345a6fe2011-02-28 21:20:02 +0000265 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
266 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
269
270cleanup:
271
272 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000273 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
275 return( 0 );
276}
277
278/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200279 * Use the blinding method and optimisation suggested in section 10 of:
280 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
281 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
282 * Berlin Heidelberg, 1996. p. 104-113.
283 */
284static int dhm_update_blinding( dhm_context *ctx,
285 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
286{
287 int ret, count;
288
289 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200290 * Don't use any blinding the first time a particular X is used,
291 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200292 */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200293 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200294 {
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200295 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200296 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
297 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200298
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200299 return( 0 );
300 }
301
302 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200303 * Ok, we need blinding. Can we re-use existing values?
304 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200305 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200306 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
307 {
308 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
309 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
310
311 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
312 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
313
314 return( 0 );
315 }
316
317 /*
318 * We need to generate blinding values from scratch
319 */
320
321 /* Vi = random( 2, P-1 ) */
322 count = 0;
323 do
324 {
325 mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
326
327 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200328 MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200329
330 if( count++ > 10 )
331 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
332 }
333 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200334
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200335 /* Vf = Vi^-X mod P */
336 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
337 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
338
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200339cleanup:
340 return( ret );
341}
342
343/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 * Derive and export the shared secret (G^Y)^X mod P
345 */
346int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200347 unsigned char *output, size_t *olen,
348 int (*f_rng)(void *, unsigned char *, size_t),
349 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000350{
351 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200352 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200353
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000355 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Paul Bakker345a6fe2011-02-28 21:20:02 +0000357 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000358 return( ret );
359
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200360 mpi_init( &GYb );
361
362 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200363 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200364 {
365 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
366 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
367 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
368 }
369 else
370 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
371
372 /* Do modular exponentiation */
373 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
374 &ctx->P, &ctx->RP ) );
375
376 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200377 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200378 {
379 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
380 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
381 }
382
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 *olen = mpi_size( &ctx->K );
384
385 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
386
387cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200388 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
390 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000391 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 return( 0 );
394}
395
396/*
397 * Free the components of a DHM key
398 */
399void dhm_free( dhm_context *ctx )
400{
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200401 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000402 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
403 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
404 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200405
Paul Bakker34617722014-06-13 17:20:13 +0200406 polarssl_zeroize( ctx, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407}
408
Paul Bakker40ce79f2013-09-15 17:43:54 +0200409#if defined(POLARSSL_ASN1_PARSE_C)
410/*
411 * Parse DHM parameters
412 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200413int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
414 size_t dhminlen )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200415{
416 int ret;
417 size_t len;
418 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200419#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200420 pem_context pem;
421
422 pem_init( &pem );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200423
424 ret = pem_read_buffer( &pem,
425 "-----BEGIN DH PARAMETERS-----",
426 "-----END DH PARAMETERS-----",
427 dhmin, NULL, 0, &dhminlen );
428
429 if( ret == 0 )
430 {
431 /*
432 * Was PEM encoded
433 */
434 dhminlen = pem.buflen;
435 }
436 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
437 goto exit;
438
439 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
440#else
441 p = (unsigned char *) dhmin;
Paul Bakker9af723c2014-05-01 13:03:14 +0200442#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200443 end = p + dhminlen;
444
445 /*
446 * DHParams ::= SEQUENCE {
447 * prime INTEGER, -- P
448 * generator INTEGER, -- g
449 * }
450 */
451 if( ( ret = asn1_get_tag( &p, end, &len,
452 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
453 {
454 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
455 goto exit;
456 }
457
458 end = p + len;
459
460 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
461 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
462 {
463 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
464 goto exit;
465 }
466
467 if( p != end )
468 {
469 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
470 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
471 goto exit;
472 }
473
474 ret = 0;
475
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100476 dhm->len = mpi_size( &dhm->P );
477
Paul Bakker40ce79f2013-09-15 17:43:54 +0200478exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200479#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200480 pem_free( &pem );
481#endif
482 if( ret != 0 )
483 dhm_free( dhm );
484
485 return( ret );
486}
487
488#if defined(POLARSSL_FS_IO)
489/*
490 * Load all data from a file into a given buffer.
491 */
492static int load_file( const char *path, unsigned char **buf, size_t *n )
493{
494 FILE *f;
495 long size;
496
497 if( ( f = fopen( path, "rb" ) ) == NULL )
498 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
499
500 fseek( f, 0, SEEK_END );
501 if( ( size = ftell( f ) ) == -1 )
502 {
503 fclose( f );
504 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
505 }
506 fseek( f, 0, SEEK_SET );
507
508 *n = (size_t) size;
509
510 if( *n + 1 == 0 ||
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500511 ( *buf = polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200512 {
513 fclose( f );
514 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
515 }
516
517 if( fread( *buf, 1, *n, f ) != *n )
518 {
519 fclose( f );
520 polarssl_free( *buf );
521 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
522 }
523
524 fclose( f );
525
526 (*buf)[*n] = '\0';
527
528 return( 0 );
529}
530
531/*
532 * Load and parse DHM parameters
533 */
534int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
535{
536 int ret;
537 size_t n;
538 unsigned char *buf;
539
Paul Bakker66d5d072014-06-17 16:39:18 +0200540 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200541 return( ret );
542
543 ret = dhm_parse_dhm( dhm, buf, n );
544
Paul Bakker34617722014-06-13 17:20:13 +0200545 polarssl_zeroize( buf, n + 1 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200546 polarssl_free( buf );
547
548 return( ret );
549}
550#endif /* POLARSSL_FS_IO */
551#endif /* POLARSSL_ASN1_PARSE_C */
552
Paul Bakker40e46942009-01-03 21:51:57 +0000553#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000555#include "mbedtls/certs.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +0200556
Paul Bakker5121ce52009-01-03 21:22:43 +0000557/*
558 * Checkup routine
559 */
560int dhm_self_test( int verbose )
561{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200562#if defined(POLARSSL_CERTS_C)
563 int ret;
564 dhm_context dhm;
565
Paul Bakker8f870b02014-06-20 13:32:38 +0200566 dhm_init( &dhm );
567
Paul Bakker40ce79f2013-09-15 17:43:54 +0200568 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100569 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200570
571 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
572 strlen( test_dhm_params ) ) ) != 0 )
573 {
574 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100575 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200576
Manuel Pégourié-Gonnardb196fc22014-07-09 16:53:29 +0200577 ret = 1;
Paul Bakker8f870b02014-06-20 13:32:38 +0200578 goto exit;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200579 }
580
581 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100582 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200583
Paul Bakker8f870b02014-06-20 13:32:38 +0200584exit:
Paul Bakker40ce79f2013-09-15 17:43:54 +0200585 dhm_free( &dhm );
586
Paul Bakker8f870b02014-06-20 13:32:38 +0200587 return( ret );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200588#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100589 if( verbose != 0 )
590 polarssl_printf( " DHM parameter load: skipped\n" );
591
592 return( 0 );
Paul Bakker9af723c2014-05-01 13:03:14 +0200593#endif /* POLARSSL_CERTS_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000594}
595
Paul Bakker9af723c2014-05-01 13:03:14 +0200596#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
Paul Bakker9af723c2014-05-01 13:03:14 +0200598#endif /* POLARSSL_DHM_C */