blob: 6f1c51cc086b22e5a333f5105f7da0142768df3c [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)
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>
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 *
Janos Follath77359c92017-09-20 15:33:24 +010094 * This means that we need to return an error if
95 * public_param < 2 or public_param > P-2
96 *
Paul Bakker345a6fe2011-02-28 21:20:02 +000097 * For more information on the attack, see:
98 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
99 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +0000100 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000101static int dhm_check_range( const mpi *param, const mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000102{
Paul Bakker345a6fe2011-02-28 21:20:02 +0000103 mpi L, U;
Janos Follath77359c92017-09-20 15:33:24 +0100104 int ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000105
Paul Bakker6c591fa2011-05-05 11:49:20 +0000106 mpi_init( &L ); mpi_init( &U );
Paul Bakker3d8fb632014-04-17 12:42:41 +0200107
108 MPI_CHK( mpi_lset( &L, 2 ) );
109 MPI_CHK( mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000110
Janos Follath77359c92017-09-20 15:33:24 +0100111 if( mpi_cmp_mpi( param, &L ) < 0 ||
112 mpi_cmp_mpi( param, &U ) > 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000113 {
Janos Follath77359c92017-09-20 15:33:24 +0100114 ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000115 }
116
Paul Bakker3d8fb632014-04-17 12:42:41 +0200117cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000118 mpi_free( &L ); mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +0000119 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000120}
121
Paul Bakker8f870b02014-06-20 13:32:38 +0200122void dhm_init( dhm_context *ctx )
123{
124 memset( ctx, 0, sizeof( dhm_context ) );
125}
126
Paul Bakkerc47840e2011-02-20 16:37:30 +0000127/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 * Parse the ServerKeyExchange parameters
129 */
130int dhm_read_params( dhm_context *ctx,
131 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000132 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000134 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
137 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
138 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
139 return( ret );
140
Paul Bakker345a6fe2011-02-28 21:20:02 +0000141 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
142 return( ret );
143
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 ctx->len = mpi_size( &ctx->P );
145
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 return( 0 );
147}
148
149/*
150 * Setup and write the ServerKeyExchange parameters
151 */
152int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000153 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000154 int (*f_rng)(void *, unsigned char *, size_t),
155 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000156{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000157 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000158 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 unsigned char *p;
160
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000161 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
162 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
163
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000165 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000167 do
168 {
Ron Eldor57501ef2017-01-12 14:50:50 +0200169 MPI_CHK( mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000171 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200172 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000173
174 if( count++ > 10 )
175 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
176 }
177 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Paul Bakkerff7fe672010-07-18 09:45:05 +0000179 /*
180 * Calculate GX = G^X mod P
181 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
183 &ctx->P , &ctx->RP ) );
184
Paul Bakker345a6fe2011-02-28 21:20:02 +0000185 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000186 return( ret );
187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 /*
189 * export P, G, GX
190 */
191#define DHM_MPI_EXPORT(X,n) \
192 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
193 *p++ = (unsigned char)( n >> 8 ); \
194 *p++ = (unsigned char)( n ); p += n;
195
196 n1 = mpi_size( &ctx->P );
197 n2 = mpi_size( &ctx->G );
198 n3 = mpi_size( &ctx->GX );
199
200 p = output;
201 DHM_MPI_EXPORT( &ctx->P , n1 );
202 DHM_MPI_EXPORT( &ctx->G , n2 );
203 DHM_MPI_EXPORT( &ctx->GX, n3 );
204
205 *olen = p - output;
206
207 ctx->len = n1;
208
209cleanup:
210
211 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000212 return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214 return( 0 );
215}
216
217/*
218 * Import the peer's public value G^Y
219 */
220int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000221 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000222{
223 int ret;
224
225 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000226 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
228 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000229 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231 return( 0 );
232}
233
234/*
235 * Create own private value X and export G^X
236 */
237int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000238 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000239 int (*f_rng)(void *, unsigned char *, size_t),
240 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000241{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000242 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000245 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000247 if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
248 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 /*
251 * generate X and calculate GX = G^X mod P
252 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000253 do
254 {
Ron Eldor57501ef2017-01-12 14:50:50 +0200255 MPI_CHK( mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000257 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200258 MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000259
260 if( count++ > 10 )
261 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
262 }
263 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
266 &ctx->P , &ctx->RP ) );
267
Paul Bakker345a6fe2011-02-28 21:20:02 +0000268 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
269 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000270
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
272
273cleanup:
274
275 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000276 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
278 return( 0 );
279}
280
281/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200282 * Use the blinding method and optimisation suggested in section 10 of:
283 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
284 * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
285 * Berlin Heidelberg, 1996. p. 104-113.
286 */
287static int dhm_update_blinding( dhm_context *ctx,
288 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
289{
290 int ret, count;
291
292 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200293 * Don't use any blinding the first time a particular X is used,
294 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200295 */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200296 if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200297 {
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200298 MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200299 MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
300 MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200301
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200302 return( 0 );
303 }
304
305 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200306 * Ok, we need blinding. Can we re-use existing values?
307 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200308 */
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200309 if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
310 {
311 MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
312 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
313
314 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
315 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
316
317 return( 0 );
318 }
319
320 /*
321 * We need to generate blinding values from scratch
322 */
323
324 /* Vi = random( 2, P-1 ) */
325 count = 0;
326 do
327 {
Ron Eldor57501ef2017-01-12 14:50:50 +0200328 MPI_CHK( mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200329
330 while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +0200331 MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200332
333 if( count++ > 10 )
334 return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
335 }
336 while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200337
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200338 /* Vf = Vi^-X mod P */
339 MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
340 MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
341
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200342cleanup:
343 return( ret );
344}
345
346/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 * Derive and export the shared secret (G^Y)^X mod P
348 */
349int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200350 unsigned char *output, size_t *olen,
351 int (*f_rng)(void *, unsigned char *, size_t),
352 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000353{
354 int ret;
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200355 mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200356
Paul Bakker5121ce52009-01-03 21:22:43 +0000357 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000358 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000359
Paul Bakker345a6fe2011-02-28 21:20:02 +0000360 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000361 return( ret );
362
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200363 mpi_init( &GYb );
364
365 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200366 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200367 {
368 MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
369 MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
370 MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
371 }
372 else
373 MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
374
375 /* Do modular exponentiation */
376 MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
377 &ctx->P, &ctx->RP ) );
378
379 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200380 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200381 {
382 MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
383 MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
384 }
385
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 *olen = mpi_size( &ctx->K );
387
388 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
389
390cleanup:
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200391 mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000394 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
396 return( 0 );
397}
398
399/*
400 * Free the components of a DHM key
401 */
402void dhm_free( dhm_context *ctx )
403{
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200404 mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000405 mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
406 mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
407 mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200408
Paul Bakker34617722014-06-13 17:20:13 +0200409 polarssl_zeroize( ctx, sizeof( dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410}
411
Paul Bakker40ce79f2013-09-15 17:43:54 +0200412#if defined(POLARSSL_ASN1_PARSE_C)
413/*
414 * Parse DHM parameters
415 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200416int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
417 size_t dhminlen )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200418{
419 int ret;
420 size_t len;
421 unsigned char *p, *end;
Paul Bakkercff68422013-09-15 20:43:33 +0200422#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200423 pem_context pem;
424
425 pem_init( &pem );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200426
427 ret = pem_read_buffer( &pem,
428 "-----BEGIN DH PARAMETERS-----",
429 "-----END DH PARAMETERS-----",
430 dhmin, NULL, 0, &dhminlen );
431
432 if( ret == 0 )
433 {
434 /*
435 * Was PEM encoded
436 */
437 dhminlen = pem.buflen;
438 }
439 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
440 goto exit;
441
442 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
443#else
444 p = (unsigned char *) dhmin;
Paul Bakker9af723c2014-05-01 13:03:14 +0200445#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200446 end = p + dhminlen;
447
448 /*
449 * DHParams ::= SEQUENCE {
Daniel Kahn Gillmor2ed81732015-04-03 13:09:24 -0400450 * prime INTEGER, -- P
451 * generator INTEGER, -- g
452 * privateValueLength INTEGER OPTIONAL
Paul Bakker40ce79f2013-09-15 17:43:54 +0200453 * }
454 */
455 if( ( ret = asn1_get_tag( &p, end, &len,
456 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
457 {
458 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
459 goto exit;
460 }
461
462 end = p + len;
463
464 if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
465 ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
466 {
467 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
468 goto exit;
469 }
470
471 if( p != end )
472 {
Daniel Kahn Gillmor2ed81732015-04-03 13:09:24 -0400473 /* this might be the optional privateValueLength; If so, we
474 can cleanly discard it; */
475 mpi rec;
476 mpi_init( &rec );
477 ret = asn1_get_mpi( &p, end, &rec );
478 mpi_free( &rec );
479 if ( ret != 0 )
480 {
481 ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
482 goto exit;
483 }
484 if ( p != end )
485 {
486 ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
487 POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
488 goto exit;
489 }
Paul Bakker40ce79f2013-09-15 17:43:54 +0200490 }
491
492 ret = 0;
493
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100494 dhm->len = mpi_size( &dhm->P );
495
Paul Bakker40ce79f2013-09-15 17:43:54 +0200496exit:
Paul Bakkercff68422013-09-15 20:43:33 +0200497#if defined(POLARSSL_PEM_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200498 pem_free( &pem );
499#endif
500 if( ret != 0 )
501 dhm_free( dhm );
502
503 return( ret );
504}
505
506#if defined(POLARSSL_FS_IO)
507/*
508 * Load all data from a file into a given buffer.
509 */
510static int load_file( const char *path, unsigned char **buf, size_t *n )
511{
512 FILE *f;
513 long size;
514
515 if( ( f = fopen( path, "rb" ) ) == NULL )
516 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
517
518 fseek( f, 0, SEEK_END );
519 if( ( size = ftell( f ) ) == -1 )
520 {
521 fclose( f );
522 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
523 }
524 fseek( f, 0, SEEK_SET );
525
526 *n = (size_t) size;
527
528 if( *n + 1 == 0 ||
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500529 ( *buf = polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200530 {
531 fclose( f );
532 return( POLARSSL_ERR_DHM_MALLOC_FAILED );
533 }
534
535 if( fread( *buf, 1, *n, f ) != *n )
536 {
537 fclose( f );
538 polarssl_free( *buf );
539 return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
540 }
541
542 fclose( f );
543
544 (*buf)[*n] = '\0';
545
546 return( 0 );
547}
548
549/*
550 * Load and parse DHM parameters
551 */
552int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
553{
554 int ret;
555 size_t n;
556 unsigned char *buf;
557
Paul Bakker66d5d072014-06-17 16:39:18 +0200558 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200559 return( ret );
560
561 ret = dhm_parse_dhm( dhm, buf, n );
562
Paul Bakker34617722014-06-13 17:20:13 +0200563 polarssl_zeroize( buf, n + 1 );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200564 polarssl_free( buf );
565
566 return( ret );
567}
568#endif /* POLARSSL_FS_IO */
569#endif /* POLARSSL_ASN1_PARSE_C */
570
Paul Bakker40e46942009-01-03 21:51:57 +0000571#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000572
Paul Bakker40ce79f2013-09-15 17:43:54 +0200573#include "polarssl/certs.h"
574
Paul Bakker5121ce52009-01-03 21:22:43 +0000575/*
576 * Checkup routine
577 */
578int dhm_self_test( int verbose )
579{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200580#if defined(POLARSSL_CERTS_C)
581 int ret;
582 dhm_context dhm;
583
Paul Bakker8f870b02014-06-20 13:32:38 +0200584 dhm_init( &dhm );
585
Paul Bakker40ce79f2013-09-15 17:43:54 +0200586 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100587 polarssl_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200588
589 if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
590 strlen( test_dhm_params ) ) ) != 0 )
591 {
592 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100593 polarssl_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200594
Manuel Pégourié-Gonnardb196fc22014-07-09 16:53:29 +0200595 ret = 1;
Paul Bakker8f870b02014-06-20 13:32:38 +0200596 goto exit;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200597 }
598
599 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +0100600 polarssl_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200601
Paul Bakker8f870b02014-06-20 13:32:38 +0200602exit:
Paul Bakker40ce79f2013-09-15 17:43:54 +0200603 dhm_free( &dhm );
604
Paul Bakker8f870b02014-06-20 13:32:38 +0200605 return( ret );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200606#else
Manuel Pégourié-Gonnard648656a2014-03-10 11:06:32 +0100607 if( verbose != 0 )
608 polarssl_printf( " DHM parameter load: skipped\n" );
609
610 return( 0 );
Paul Bakker9af723c2014-05-01 13:03:14 +0200611#endif /* POLARSSL_CERTS_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000612}
613
Paul Bakker9af723c2014-05-01 13:03:14 +0200614#endif /* POLARSSL_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Paul Bakker9af723c2014-05-01 13:03:14 +0200616#endif /* POLARSSL_DHM_C */