blob: 0f4d3164321dfa88661be92f0b32d20b59b99d01 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * Reference:
23 *
24 * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
25 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/pem.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020041#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ASN1_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/asn1.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020045#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/platform.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020049#else
50#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000051#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakker40ce79f2013-09-15 17:43:54 +020055#endif
56
Paul Bakker34617722014-06-13 17:20:13 +020057/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020059 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
60}
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 * helper to validate the mbedtls_mpi size and import it
Paul Bakker5121ce52009-01-03 21:22:43 +000064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065static int dhm_read_bignum( mbedtls_mpi *X,
Paul Bakker5121ce52009-01-03 21:22:43 +000066 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000067 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000068{
69 int ret, n;
70
71 if( end - *p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74 n = ( (*p)[0] << 8 ) | (*p)[1];
75 (*p) += 2;
76
77 if( (int)( end - *p ) < n )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
81 return( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83 (*p) += n;
84
85 return( 0 );
86}
87
88/*
Paul Bakkeraec37cb2012-04-26 18:59:59 +000089 * Verify sanity of parameter with regards to P
Paul Bakker345a6fe2011-02-28 21:20:02 +000090 *
Paul Bakkeraec37cb2012-04-26 18:59:59 +000091 * Parameter should be: 2 <= public_param <= P - 2
Paul Bakker345a6fe2011-02-28 21:20:02 +000092 *
93 * For more information on the attack, see:
94 * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
95 * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
Paul Bakkerc47840e2011-02-20 16:37:30 +000096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int dhm_check_range( const mbedtls_mpi *param, const mbedtls_mpi *P )
Paul Bakkerc47840e2011-02-20 16:37:30 +000098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_mpi L, U;
100 int ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U );
Paul Bakker3d8fb632014-04-17 12:42:41 +0200103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) );
105 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 if( mbedtls_mpi_cmp_mpi( param, &L ) >= 0 &&
108 mbedtls_mpi_cmp_mpi( param, &U ) <= 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000109 {
Paul Bakker345a6fe2011-02-28 21:20:02 +0000110 ret = 0;
Paul Bakkerc47840e2011-02-20 16:37:30 +0000111 }
112
Paul Bakker3d8fb632014-04-17 12:42:41 +0200113cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U );
Paul Bakker345a6fe2011-02-28 21:20:02 +0000115 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000116}
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
Paul Bakker8f870b02014-06-20 13:32:38 +0200119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 memset( ctx, 0, sizeof( mbedtls_dhm_context ) );
Paul Bakker8f870b02014-06-20 13:32:38 +0200121}
122
Paul Bakkerc47840e2011-02-20 16:37:30 +0000123/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 * Parse the ServerKeyExchange parameters
125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000128 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129{
Paul Bakker13ed9ab2012-04-16 09:43:49 +0000130 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
133 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
134 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
135 return( ret );
136
Paul Bakker345a6fe2011-02-28 21:20:02 +0000137 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
138 return( ret );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 ctx->len = mbedtls_mpi_size( &ctx->P );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 return( 0 );
143}
144
145/*
146 * Setup and write the ServerKeyExchange parameters
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000149 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000150 int (*f_rng)(void *, unsigned char *, size_t),
151 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000152{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000153 int ret, count = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000154 size_t n1, n2, n3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 unsigned char *p;
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
158 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000159
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000161 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000163 do
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
168 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000169
170 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000172 }
173 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Paul Bakkerff7fe672010-07-18 09:45:05 +0000175 /*
176 * Calculate GX = G^X mod P
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 &ctx->P , &ctx->RP ) );
180
Paul Bakker345a6fe2011-02-28 21:20:02 +0000181 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000182 return( ret );
183
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 /*
185 * export P, G, GX
186 */
187#define DHM_MPI_EXPORT(X,n) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, p + 2, n ) ); \
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 *p++ = (unsigned char)( n >> 8 ); \
190 *p++ = (unsigned char)( n ); p += n;
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 n1 = mbedtls_mpi_size( &ctx->P );
193 n2 = mbedtls_mpi_size( &ctx->G );
194 n3 = mbedtls_mpi_size( &ctx->GX );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196 p = output;
197 DHM_MPI_EXPORT( &ctx->P , n1 );
198 DHM_MPI_EXPORT( &ctx->G , n2 );
199 DHM_MPI_EXPORT( &ctx->GX, n3 );
200
201 *olen = p - output;
202
203 ctx->len = n1;
204
205cleanup:
206
207 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210 return( 0 );
211}
212
213/*
214 * Import the peer's public value G^Y
215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000217 const unsigned char *input, size_t ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218{
219 int ret;
220
221 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
225 return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 return( 0 );
228}
229
230/*
231 * Create own private value X and export G^X
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000234 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000235 int (*f_rng)(void *, unsigned char *, size_t),
236 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000237{
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000238 int ret, count = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240 if( ctx == NULL || olen < 1 || olen > ctx->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
244 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
Paul Bakkerb5b20f12012-09-16 15:07:49 +0000245
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 /*
247 * generate X and calculate GX = G^X mod P
248 */
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000249 do
250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
254 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000255
256 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
Paul Bakkeraec37cb2012-04-26 18:59:59 +0000258 }
259 while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 &ctx->P , &ctx->RP ) );
263
Paul Bakker345a6fe2011-02-28 21:20:02 +0000264 if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
265 return( ret );
Paul Bakkerc47840e2011-02-20 16:37:30 +0000266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
269cleanup:
270
271 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274 return( 0 );
275}
276
277/*
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200278 * Use the blinding method and optimisation suggested in section 10 of:
279 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200280 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200281 * Berlin Heidelberg, 1996. p. 104-113.
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283static int dhm_update_blinding( mbedtls_dhm_context *ctx,
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200284 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
285{
286 int ret, count;
287
288 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200289 * Don't use any blinding the first time a particular X is used,
290 * but remember it to use blinding next time.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
295 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
296 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) );
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200297
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200298 return( 0 );
299 }
300
301 /*
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200302 * Ok, we need blinding. Can we re-use existing values?
303 * If yes, just update them by squaring them.
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
308 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
311 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200312
313 return( 0 );
314 }
315
316 /*
317 * We need to generate blinding values from scratch
318 */
319
320 /* Vi = random( 2, P-1 ) */
321 count = 0;
322 do
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
327 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200328
329 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200333
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200334 /* Vf = Vi^-X mod P */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
336 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200337
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200338cleanup:
339 return( ret );
340}
341
342/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 * Derive and export the shared secret (G^Y)^X mod P
344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100346 unsigned char *output, size_t output_size, size_t *olen,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200347 int (*f_rng)(void *, unsigned char *, size_t),
348 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +0000349{
350 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_mpi GYb;
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200352
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100353 if( ctx == NULL || output_size < ctx->len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Paul Bakker345a6fe2011-02-28 21:20:02 +0000356 if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
Paul Bakkerc47840e2011-02-20 16:37:30 +0000357 return( ret );
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_mpi_init( &GYb );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200360
361 /* Blind peer's value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200362 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
365 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
366 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200367 }
368 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200370
371 /* Do modular exponentiation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200373 &ctx->P, &ctx->RP ) );
374
375 /* Unblind secret value */
Manuel Pégourié-Gonnarded8a02b2013-09-04 16:39:03 +0200376 if( f_rng != NULL )
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
379 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200380 }
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 *olen = mbedtls_mpi_size( &ctx->K );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000385
386cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_mpi_free( &GYb );
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
389 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
392 return( 0 );
393}
394
395/*
396 * Free the components of a DHM key
397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_mpi_free( &ctx->pX); mbedtls_mpi_free( &ctx->Vf ); mbedtls_mpi_free( &ctx->Vi );
401 mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->K ); mbedtls_mpi_free( &ctx->GY );
402 mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X ); mbedtls_mpi_free( &ctx->G );
403 mbedtls_mpi_free( &ctx->P );
Manuel Pégourié-Gonnardb72b4ed2013-09-13 13:55:26 +0200404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406}
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_ASN1_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200409/*
410 * Parse DHM parameters
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200413 size_t dhminlen )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200414{
415 int ret;
416 size_t len;
417 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_PEM_PARSE_C)
419 mbedtls_pem_context pem;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_pem_init( &pem );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200422
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200423 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200424 if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200425 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
426 else
427 ret = mbedtls_pem_read_buffer( &pem,
428 "-----BEGIN DH PARAMETERS-----",
429 "-----END DH PARAMETERS-----",
430 dhmin, NULL, 0, &dhminlen );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200431
432 if( ret == 0 )
433 {
434 /*
435 * Was PEM encoded
436 */
437 dhminlen = pem.buflen;
438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200440 goto exit;
441
442 p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
443#else
444 p = (unsigned char *) dhmin;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#endif /* MBEDTLS_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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
456 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200459 goto exit;
460 }
461
462 end = p + len;
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
465 ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200468 goto exit;
469 }
470
471 if( p != end )
472 {
Manuel Pégourié-Gonnardde9b3632015-04-17 20:06:31 +0200473 /* This might be the optional privateValueLength.
474 * If so, we can cleanly discard it */
475 mbedtls_mpi rec;
476 mbedtls_mpi_init( &rec );
477 ret = mbedtls_asn1_get_mpi( &p, end, &rec );
478 mbedtls_mpi_free( &rec );
Daniel Kahn Gillmor2ed81732015-04-03 13:09:24 -0400479 if ( ret != 0 )
480 {
Manuel Pégourié-Gonnardde9b3632015-04-17 20:06:31 +0200481 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
Daniel Kahn Gillmor2ed81732015-04-03 13:09:24 -0400482 goto exit;
483 }
484 if ( p != end )
485 {
Manuel Pégourié-Gonnardde9b3632015-04-17 20:06:31 +0200486 ret = MBEDTLS_ERR_DHM_INVALID_FORMAT +
487 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Daniel Kahn Gillmor2ed81732015-04-03 13:09:24 -0400488 goto exit;
489 }
Paul Bakker40ce79f2013-09-15 17:43:54 +0200490 }
491
492 ret = 0;
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 dhm->len = mbedtls_mpi_size( &dhm->P );
Manuel Pégourié-Gonnard3fec2202014-03-29 16:42:38 +0100495
Paul Bakker40ce79f2013-09-15 17:43:54 +0200496exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_PEM_PARSE_C)
498 mbedtls_pem_free( &pem );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200499#endif
500 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_dhm_free( dhm );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200502
503 return( ret );
504}
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_FS_IO)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200507/*
508 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200509 *
510 * The file is expected to contain either PEM or DER encoded data.
511 * A terminating null byte is always appended. It is included in the announced
512 * length only if the data looks like it is PEM encoded.
Paul Bakker40ce79f2013-09-15 17:43:54 +0200513 */
514static int load_file( const char *path, unsigned char **buf, size_t *n )
515{
516 FILE *f;
517 long size;
518
519 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200521
522 fseek( f, 0, SEEK_END );
523 if( ( size = ftell( f ) ) == -1 )
524 {
525 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200527 }
528 fseek( f, 0, SEEK_SET );
529
530 *n = (size_t) size;
531
532 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200533 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200534 {
535 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200536 return( MBEDTLS_ERR_DHM_ALLOC_FAILED );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200537 }
538
539 if( fread( *buf, 1, *n, f ) != *n )
540 {
541 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_free( *buf );
543 return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200544 }
545
546 fclose( f );
547
548 (*buf)[*n] = '\0';
549
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200550 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
551 ++*n;
552
Paul Bakker40ce79f2013-09-15 17:43:54 +0200553 return( 0 );
554}
555
556/*
557 * Load and parse DHM parameters
558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200560{
561 int ret;
562 size_t n;
563 unsigned char *buf;
564
Paul Bakker66d5d072014-06-17 16:39:18 +0200565 if( ( ret = load_file( path, &buf, &n ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200566 return( ret );
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200569
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200570 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_free( buf );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200572
573 return( ret );
574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_FS_IO */
576#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
Manuel Pégourié-Gonnard53585ee2015-06-25 08:52:25 +0200580static const char mbedtls_test_dhm_params[] =
581"-----BEGIN DH PARAMETERS-----\r\n"
582"MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
583"1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
584"9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
585"-----END DH PARAMETERS-----\r\n";
586
587static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200588
Paul Bakker5121ce52009-01-03 21:22:43 +0000589/*
590 * Checkup routine
591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592int mbedtls_dhm_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +0000593{
Paul Bakker40ce79f2013-09-15 17:43:54 +0200594 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 mbedtls_dhm_context dhm;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_dhm_init( &dhm );
Paul Bakker8f870b02014-06-20 13:32:38 +0200598
Paul Bakker40ce79f2013-09-15 17:43:54 +0200599 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_printf( " DHM parameter load: " );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200601
Manuel Pégourié-Gonnard53585ee2015-06-25 08:52:25 +0200602 if( ( ret = mbedtls_dhm_parse_dhm( &dhm,
603 (const unsigned char *) mbedtls_test_dhm_params,
604 mbedtls_test_dhm_params_len ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200605 {
606 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_printf( "failed\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200608
Manuel Pégourié-Gonnardb196fc22014-07-09 16:53:29 +0200609 ret = 1;
Paul Bakker8f870b02014-06-20 13:32:38 +0200610 goto exit;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200611 }
612
613 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_printf( "passed\n\n" );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200615
Paul Bakker8f870b02014-06-20 13:32:38 +0200616exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_dhm_free( &dhm );
Paul Bakker40ce79f2013-09-15 17:43:54 +0200618
Paul Bakker8f870b02014-06-20 13:32:38 +0200619 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620}
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624#endif /* MBEDTLS_DHM_C */