blob: 13cd6b29c72da9c65f6aed4596100d27a83e8b63 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +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
Paul Bakker40e46942009-01-03 21:51:57 +000028#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/dhm.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34#include <string.h>
35
36/*
37 * helper to validate the mpi size and import it
38 */
39static int dhm_read_bignum( mpi *X,
40 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000041 const unsigned char *end )
Paul Bakker5121ce52009-01-03 21:22:43 +000042{
43 int ret, n;
44
45 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000046 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000047
48 n = ( (*p)[0] << 8 ) | (*p)[1];
49 (*p) += 2;
50
51 if( (int)( end - *p ) < n )
Paul Bakker40e46942009-01-03 21:51:57 +000052 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000053
54 if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +000055 return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000056
57 (*p) += n;
58
59 return( 0 );
60}
61
62/*
63 * Parse the ServerKeyExchange parameters
64 */
65int dhm_read_params( dhm_context *ctx,
66 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 memset( ctx, 0, sizeof( dhm_context ) );
72
73 if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
74 ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
75 ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
76 return( ret );
77
78 ctx->len = mpi_size( &ctx->P );
79
80 if( end - *p < 2 )
Paul Bakker40e46942009-01-03 21:51:57 +000081 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83 n = ( (*p)[0] << 8 ) | (*p)[1];
84 (*p) += 2;
85
86 if( end != *p + n )
Paul Bakker40e46942009-01-03 21:51:57 +000087 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89 return( 0 );
90}
91
92/*
93 * Setup and write the ServerKeyExchange parameters
94 */
95int dhm_make_params( dhm_context *ctx, int x_size,
96 unsigned char *output, int *olen,
97 int (*f_rng)(void *), void *p_rng )
98{
99 int i, ret, n, n1, n2, n3;
100 unsigned char *p;
101
102 /*
Paul Bakkerff7fe672010-07-18 09:45:05 +0000103 * Generate X as large as possible ( < P )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
105 n = x_size / sizeof( t_int );
106 MPI_CHK( mpi_grow( &ctx->X, n ) );
107 MPI_CHK( mpi_lset( &ctx->X, 0 ) );
108
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 p = (unsigned char *) ctx->X.p;
Paul Bakkerff7fe672010-07-18 09:45:05 +0000110 for( i = 0; i < x_size - 1; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 *p++ = (unsigned char) f_rng( p_rng );
112
113 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
114 mpi_shift_r( &ctx->X, 1 );
115
Paul Bakkerff7fe672010-07-18 09:45:05 +0000116 /*
117 * Calculate GX = G^X mod P
118 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
120 &ctx->P , &ctx->RP ) );
121
122 /*
123 * export P, G, GX
124 */
125#define DHM_MPI_EXPORT(X,n) \
126 MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
127 *p++ = (unsigned char)( n >> 8 ); \
128 *p++ = (unsigned char)( n ); p += n;
129
130 n1 = mpi_size( &ctx->P );
131 n2 = mpi_size( &ctx->G );
132 n3 = mpi_size( &ctx->GX );
133
134 p = output;
135 DHM_MPI_EXPORT( &ctx->P , n1 );
136 DHM_MPI_EXPORT( &ctx->G , n2 );
137 DHM_MPI_EXPORT( &ctx->GX, n3 );
138
139 *olen = p - output;
140
141 ctx->len = n1;
142
143cleanup:
144
145 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000146 return( ret | POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148 return( 0 );
149}
150
151/*
152 * Import the peer's public value G^Y
153 */
154int dhm_read_public( dhm_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000155 const unsigned char *input, int ilen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000156{
157 int ret;
158
159 if( ctx == NULL || ilen < 1 || ilen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000160 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162 if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000163 return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165 return( 0 );
166}
167
168/*
169 * Create own private value X and export G^X
170 */
171int dhm_make_public( dhm_context *ctx, int x_size,
172 unsigned char *output, int olen,
173 int (*f_rng)(void *), void *p_rng )
174{
175 int ret, i, n;
176 unsigned char *p;
177
178 if( ctx == NULL || olen < 1 || olen > ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000179 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181 /*
182 * generate X and calculate GX = G^X mod P
183 */
184 n = x_size / sizeof( t_int );
185 MPI_CHK( mpi_grow( &ctx->X, n ) );
186 MPI_CHK( mpi_lset( &ctx->X, 0 ) );
187
Paul Bakker2a1fadf2009-07-27 21:05:24 +0000188 n = x_size - 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 p = (unsigned char *) ctx->X.p;
190 for( i = 0; i < n; i++ )
191 *p++ = (unsigned char) f_rng( p_rng );
192
193 while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
194 mpi_shift_r( &ctx->X, 1 );
195
196 MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
197 &ctx->P , &ctx->RP ) );
198
199 MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
200
201cleanup:
202
203 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000204 return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
206 return( 0 );
207}
208
209/*
210 * Derive and export the shared secret (G^Y)^X mod P
211 */
212int dhm_calc_secret( dhm_context *ctx,
213 unsigned char *output, int *olen )
214{
215 int ret;
216
217 if( ctx == NULL || *olen < ctx->len )
Paul Bakker40e46942009-01-03 21:51:57 +0000218 return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
220 MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
221 &ctx->P, &ctx->RP ) );
222
223 *olen = mpi_size( &ctx->K );
224
225 MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
226
227cleanup:
228
229 if( ret != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000230 return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED | ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 return( 0 );
233}
234
235/*
236 * Free the components of a DHM key
237 */
238void dhm_free( dhm_context *ctx )
239{
240 mpi_free( &ctx->RP, &ctx->K, &ctx->GY,
241 &ctx->GX, &ctx->X, &ctx->G,
242 &ctx->P, NULL );
243}
244
Paul Bakker40e46942009-01-03 21:51:57 +0000245#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
247/*
248 * Checkup routine
249 */
250int dhm_self_test( int verbose )
251{
252 return( verbose++ );
253}
254
255#endif
256
257#endif